From 7735f4484a7e18c72b148c67ceb8a003316b91b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9=20van=20Geldorp?= Date: Sun, 8 Jan 2006 22:14:26 +0000 Subject: [PATCH] Obtain DHCP info from DHCP client service svn path=/trunk/; revision=20736 --- reactos/include/libs/dhcp/rosdhcp_public.h | 7 +++ reactos/lib/dhcpcsvc/dhcpcsvc.c | 65 ++++++++++++++++++++++ reactos/lib/dhcpcsvc/dhcpcsvc.def | 16 ------ reactos/lib/dhcpcsvc/dhcpcsvc.spec | 47 ++++++++++++++++ reactos/lib/dhcpcsvc/dhcpcsvc.xml | 3 +- reactos/lib/iphlpapi/dhcp.h | 18 ++++++ reactos/lib/iphlpapi/dhcp_reactos.c | 30 ++++++++++ reactos/lib/iphlpapi/iphlpapi.xml | 2 + reactos/lib/iphlpapi/iphlpapi_main.c | 9 +++ reactos/services/dhcp/api.c | 40 +++++++++++++ reactos/services/dhcp/dhclient.c | 7 +++ reactos/services/dhcp/include/dhcpd.h | 4 ++ reactos/services/dhcp/include/rosdhcp.h | 2 + reactos/services/dhcp/pipe.c | 22 +++++++- 14 files changed, 253 insertions(+), 19 deletions(-) delete mode 100644 reactos/lib/dhcpcsvc/dhcpcsvc.def create mode 100644 reactos/lib/dhcpcsvc/dhcpcsvc.spec create mode 100644 reactos/lib/iphlpapi/dhcp.h create mode 100644 reactos/lib/iphlpapi/dhcp_reactos.c diff --git a/reactos/include/libs/dhcp/rosdhcp_public.h b/reactos/include/libs/dhcp/rosdhcp_public.h index c732e7f9ede..7e0c34a652b 100644 --- a/reactos/include/libs/dhcp/rosdhcp_public.h +++ b/reactos/include/libs/dhcp/rosdhcp_public.h @@ -7,6 +7,7 @@ enum { DhcpReqReleaseIpAddress, DhcpReqRenewIpAddress, DhcpReqStaticRefreshParams, + DhcpReqGetAdapterInfo, }; typedef struct _COMM_DHCP_REQ { @@ -39,6 +40,12 @@ typedef union _COMM_DHCP_REPLY { DWORD Mtu; DWORD Speed; } QueryHWInfo; + struct { + BOOL DhcpEnabled; + DWORD DhcpServer; + time_t LeaseObtained; + time_t LeaseExpires; + } GetAdapterInfo; } COMM_DHCP_REPLY; #define DHCP_PIPE_NAME "\\\\.\\pipe\\dhcpclient" diff --git a/reactos/lib/dhcpcsvc/dhcpcsvc.c b/reactos/lib/dhcpcsvc/dhcpcsvc.c index b596db14e7c..1d5fba63d53 100644 --- a/reactos/lib/dhcpcsvc/dhcpcsvc.c +++ b/reactos/lib/dhcpcsvc/dhcpcsvc.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #define DHCP_TIMEOUT 1000 @@ -117,6 +118,70 @@ DWORD APIENTRY DhcpStaticRefreshParams( DWORD AdapterIndex, return Reply.Reply; } +/*++ + * @name DhcpRosGetAdapterInfo + * @implemented ReactOS only + * + * Get DHCP info for an adapter + * + * @param AdapterIndex + * Index of the adapter (iphlpapi-style) for which info is + * requested + * + * @param DhcpEnabled + * Returns whether DHCP is enabled for the adapter + * + * @param DhcpServer + * Returns DHCP server IP address (255.255.255.255 if no + * server reached yet), in network byte order + * + * @param LeaseObtained + * Returns time at which the lease was obtained + * + * @param LeaseExpires + * Returns time at which the lease will expire + * + * @return non-zero on success + * + * @remarks This is a ReactOS-only routine + * + *--*/ +DWORD APIENTRY DhcpRosGetAdapterInfo( DWORD AdapterIndex, + PBOOL DhcpEnabled, + PDWORD DhcpServer, + time_t *LeaseObtained, + time_t *LeaseExpires ) +{ + COMM_DHCP_REQ Req; + COMM_DHCP_REPLY Reply; + DWORD BytesRead; + BOOL Result; + + Req.Type = DhcpReqGetAdapterInfo; + Req.AdapterIndex = AdapterIndex; + + Result = CallNamedPipe + ( DHCP_PIPE_NAME, &Req, sizeof(Req), &Reply, sizeof(Reply), + &BytesRead, DHCP_TIMEOUT ); + + if ( 0 != Result && 0 != Reply.Reply ) { + *DhcpEnabled = Reply.GetAdapterInfo.DhcpEnabled; + } else { + *DhcpEnabled = FALSE; + } + if ( *DhcpEnabled ) { + *DhcpServer = Reply.GetAdapterInfo.DhcpServer; + *LeaseObtained = Reply.GetAdapterInfo.LeaseObtained; + *LeaseExpires = Reply.GetAdapterInfo.LeaseExpires; + } else { + *DhcpServer = INADDR_NONE; + *LeaseObtained = 0; + *LeaseExpires = 0; + } + + return Reply.Reply; +} + INT STDCALL DllMain(PVOID hinstDll, ULONG dwReason, diff --git a/reactos/lib/dhcpcsvc/dhcpcsvc.def b/reactos/lib/dhcpcsvc/dhcpcsvc.def deleted file mode 100644 index 094e7e9f4eb..00000000000 --- a/reactos/lib/dhcpcsvc/dhcpcsvc.def +++ /dev/null @@ -1,16 +0,0 @@ -; $Id: dhcpcapi.def 14337 2005-03-26 22:10:04Z $ -; -; dhcpcsvc.def -; -; ReactOS Operating System -; -LIBRARY dhcpcsvc.dll -EXPORTS -DhcpCApiInitialize@4 -DhcpCApiCleanup@0 -DhcpQueryHWInfo@16 -DhcpLeaseIpAddress@4 -DhcpReleaseIpAddressLease@4 -DhcpRenewIpAddressLease@4 -DhcpStaticRefreshParams@12 -; EOF diff --git a/reactos/lib/dhcpcsvc/dhcpcsvc.spec b/reactos/lib/dhcpcsvc/dhcpcsvc.spec new file mode 100644 index 00000000000..666c0788fa6 --- /dev/null +++ b/reactos/lib/dhcpcsvc/dhcpcsvc.spec @@ -0,0 +1,47 @@ +# +# PROJECT: ReactOS Networking +# LICENSE: GPL - See COPYING in the top level directory +# FILE: lib/dhcpcsvc/dhcpcsvc.spec +# PURPOSE: dhcpcsvc exports +# COPYRIGHT: Copyright 2006 Ge van Geldorp +# +@ stub DhcpAcquireParameters +@ stub DhcpAcquireParametersByBroadcast +@ stdcall DhcpCApiCleanup() +@ stdcall DhcpCApiInitialize(ptr) +@ stub DhcpDelPersistentRequestParams +@ stub DhcpDeRegisterOptions +@ stub DhcpDeRegisterParamChange +@ stub DhcpEnumClasses +@ stub DhcpFallbackRefreshParams +@ stub DhcpHandlePnPEvent +@ stdcall DhcpLeaseIpAddress(long) +@ stub DhcpLeaseIpAddressEx +@ stub DhcpNotifyConfigChange +@ stub DhcpNotifyConfigChangeEx +@ stub DhcpNotifyMediaReconnected +@ stub DhcpOpenGlobalEvent +@ stub DhcpPersistentRequestParams +@ stdcall DhcpQueryHWInfo(long ptr ptr ptr) +@ stub DhcpRegisterOptions +@ stub DhcpRegisterParamChange +@ stdcall DhcpReleaseIpAddressLease(long) +@ stub DhcpReleaseIpAddressLeaseEx +@ stub DhcpReleaseParameters +@ stub DhcpRemoveDNSRegistrations +@ stdcall DhcpRenewIpAddressLease(long) +@ stub DhcpRenewIpAddressLeaseEx +@ stub DhcpRequestOptions +@ stub DhcpRequestParams +@ stdcall DhcpStaticRefreshParams(long long long) +@ stub DhcpUndoRequestParams +@ stub McastApiCleanup +@ stub McastApiStartup +@ stub McastEnumerateScopes +@ stub McastGenUID +@ stub McastReleaseAddress +@ stub McastRenewAddress +@ stub McastRequestAddress +@ stdcall DhcpRosGetAdapterInfo(long ptr ptr ptr ptr) +# The Windows DHCP client service is implemented in the DLL too +#@ stub ServiceMain diff --git a/reactos/lib/dhcpcsvc/dhcpcsvc.xml b/reactos/lib/dhcpcsvc/dhcpcsvc.xml index e0a2f02e6f7..7b6d01e00a3 100644 --- a/reactos/lib/dhcpcsvc/dhcpcsvc.xml +++ b/reactos/lib/dhcpcsvc/dhcpcsvc.xml @@ -1,5 +1,5 @@ - + include @@ -11,4 +11,5 @@ iphlpapi dhcpcsvc.c dhcpcsvc.rc + dhcpcsvc.spec diff --git a/reactos/lib/iphlpapi/dhcp.h b/reactos/lib/iphlpapi/dhcp.h new file mode 100644 index 00000000000..ba98fb62f09 --- /dev/null +++ b/reactos/lib/iphlpapi/dhcp.h @@ -0,0 +1,18 @@ +/* + * PROJECT: ReactOS Networking + * LICENSE: GPL - See COPYING in the top level directory + * FILE: lib/iphlpapi/dhcp_reactos.c + * PURPOSE: DHCP helper functions for ReactOS + * COPYRIGHT: Copyright 2006 Ge van Geldorp + */ + +#ifndef WINE_DHCP_H_ +#define WINE_DHCP_H_ + +DWORD getDhcpInfoForAdapter(DWORD AdapterIndex, + PBOOL DhcpEnabled, + PDWORD DhcpServer, + time_t *LeaseObtained, + time_t *LeaseExpires); + +#endif /* ndef WINE_DHCP_H_ */ diff --git a/reactos/lib/iphlpapi/dhcp_reactos.c b/reactos/lib/iphlpapi/dhcp_reactos.c new file mode 100644 index 00000000000..1dd4b87eee9 --- /dev/null +++ b/reactos/lib/iphlpapi/dhcp_reactos.c @@ -0,0 +1,30 @@ +/* + * PROJECT: ReactOS Networking + * LICENSE: GPL - See COPYING in the top level directory + * FILE: lib/iphlpapi/dhcp_reactos.c + * PURPOSE: DHCP helper functions for ReactOS + * COPYRIGHT: Copyright 2006 Ge van Geldorp + */ + +#include "iphlpapi_private.h" +#include "dhcp.h" +#include + +#define NDEBUG +#include "debug.h" + +DWORD APIENTRY DhcpRosGetAdapterInfo(DWORD AdapterIndex, + PBOOL DhcpEnabled, + PDWORD DhcpServer, + time_t *LeaseObtained, + time_t *LeaseExpires); + +DWORD getDhcpInfoForAdapter(DWORD AdapterIndex, + PBOOL DhcpEnabled, + PDWORD DhcpServer, + time_t *LeaseObtained, + time_t *LeaseExpires) +{ + return DhcpRosGetAdapterInfo(AdapterIndex, DhcpEnabled, DhcpServer, + LeaseObtained, LeaseExpires); +} diff --git a/reactos/lib/iphlpapi/iphlpapi.xml b/reactos/lib/iphlpapi/iphlpapi.xml index afdd1b1cec0..a35889f9572 100644 --- a/reactos/lib/iphlpapi/iphlpapi.xml +++ b/reactos/lib/iphlpapi/iphlpapi.xml @@ -11,6 +11,8 @@ kernel32 advapi32 ws2_32 + dhcpcsvc + dhcp_reactos.c ifenum_reactos.c ipstats_reactos.c iphlpapi_main.c diff --git a/reactos/lib/iphlpapi/iphlpapi_main.c b/reactos/lib/iphlpapi/iphlpapi_main.c index d5c0911098b..e5cf04f72a5 100644 --- a/reactos/lib/iphlpapi/iphlpapi_main.c +++ b/reactos/lib/iphlpapi/iphlpapi_main.c @@ -42,6 +42,7 @@ #include "winbase.h" #include "winreg.h" #include "iphlpapi.h" +#include "dhcp.h" #include "ifenum.h" #include "ipstats.h" #include "resinfo.h" @@ -559,6 +560,8 @@ DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex) DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen) { DWORD ret; + BOOL dhcpEnabled; + DWORD dhcpServer; TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen); if (!pOutBufLen) @@ -637,6 +640,12 @@ DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen) ptr->IpAddressList.IpAddress.String); toIPAddressString(getInterfaceMaskByIndex(table->indexes[ndx]), ptr->IpAddressList.IpMask.String); + getDhcpInfoForAdapter(table->indexes[ndx], &dhcpEnabled, + &dhcpServer, &ptr->LeaseObtained, + &ptr->LeaseExpires); + ptr->DhcpEnabled = (DWORD) dhcpEnabled; + toIPAddressString(dhcpServer, + ptr->DhcpServer.IpAddress.String); if (winsEnabled) { ptr->HaveWins = TRUE; memcpy(ptr->PrimaryWinsServer.IpAddress.String, diff --git a/reactos/services/dhcp/api.c b/reactos/services/dhcp/api.c index 05519142d14..b2d9c9e2ad5 100644 --- a/reactos/services/dhcp/api.c +++ b/reactos/services/dhcp/api.c @@ -11,6 +11,9 @@ #include #include +#define NDEBUG +#include + static CRITICAL_SECTION ApiCriticalSection; VOID ApiInit() { @@ -140,3 +143,40 @@ DWORD DSStaticRefreshParams( PipeSendFunc Send, COMM_DHCP_REQ *Req ) { return Send( &Reply ); } + +DWORD DSGetAdapterInfo( PipeSendFunc Send, COMM_DHCP_REQ *Req ) { + COMM_DHCP_REPLY Reply; + PDHCP_ADAPTER Adapter; + + ApiLock(); + + Adapter = AdapterFindIndex( Req->AdapterIndex ); + + Reply.Reply = Adapter ? 1 : 0; + + if( Adapter ) { + Reply.GetAdapterInfo.DhcpEnabled = (S_STATIC != Adapter->DhclientState.state); + if (S_BOUND == Adapter->DhclientState.state) { + if (sizeof(Reply.GetAdapterInfo.DhcpServer) == + Adapter->DhclientState.active->serveraddress.len) { + memcpy(&Reply.GetAdapterInfo.DhcpServer, + Adapter->DhclientState.active->serveraddress.iabuf, + Adapter->DhclientState.active->serveraddress.len); + } else { + DPRINT1("Unexpected server address len %d\n", + Adapter->DhclientState.active->serveraddress.len); + Reply.GetAdapterInfo.DhcpServer = htonl(INADDR_NONE); + } + Reply.GetAdapterInfo.LeaseObtained = Adapter->DhclientState.active->obtained; + Reply.GetAdapterInfo.LeaseExpires = Adapter->DhclientState.active->expiry; + } else { + Reply.GetAdapterInfo.DhcpServer = htonl(INADDR_NONE); + Reply.GetAdapterInfo.LeaseObtained = 0; + Reply.GetAdapterInfo.LeaseExpires = 0; + } + } + + ApiUnlock(); + + return Send( &Reply ); +} diff --git a/reactos/services/dhcp/dhclient.c b/reactos/services/dhcp/dhclient.c index 799b3c6441b..2d607d29885 100644 --- a/reactos/services/dhcp/dhclient.c +++ b/reactos/services/dhcp/dhclient.c @@ -420,6 +420,9 @@ dhcpack(struct packet *packet) ip->client->new->rebind = ip->client->new->renewal + ip->client->new->renewal / 2 + ip->client->new->renewal / 4; +#ifdef _REACTOS_ + ip->client->new->obtained = cur_time; +#endif ip->client->new->expiry += cur_time; /* Lease lengths can never be negative. */ if (ip->client->new->expiry < cur_time) @@ -809,6 +812,10 @@ packet_to_lease(struct packet *packet) lease->address.len = sizeof(packet->raw->yiaddr); memcpy(lease->address.iabuf, &packet->raw->yiaddr, lease->address.len); +#ifdef _REACTOS_ + lease->serveraddress.len = sizeof(packet->raw->siaddr); + memcpy(lease->serveraddress.iabuf, &packet->raw->siaddr, lease->address.len); +#endif /* If the server name was filled out, copy it. */ if ((!packet->options[DHO_DHCP_OPTION_OVERLOAD].len || diff --git a/reactos/services/dhcp/include/dhcpd.h b/reactos/services/dhcp/include/dhcpd.h index 0ac202a4133..3e9dd53d90c 100644 --- a/reactos/services/dhcp/include/dhcpd.h +++ b/reactos/services/dhcp/include/dhcpd.h @@ -158,6 +158,10 @@ struct client_lease { time_t expiry, renewal, rebind; struct iaddr address; char *server_name; +#ifdef _REACTOS_ + time_t obtained; + struct iaddr serveraddress; +#endif char *filename; struct string_list *medium; unsigned int is_static : 1; diff --git a/reactos/services/dhcp/include/rosdhcp.h b/reactos/services/dhcp/include/rosdhcp.h index 1e9b8f7f9f4..9141953f7ae 100644 --- a/reactos/services/dhcp/include/rosdhcp.h +++ b/reactos/services/dhcp/include/rosdhcp.h @@ -67,6 +67,8 @@ extern DWORD DSQueryHWInfo( PipeSendFunc Send, COMM_DHCP_REQ *Req ); extern DWORD DSLeaseIpAddress( PipeSendFunc Send, COMM_DHCP_REQ *Req ); extern DWORD DSRenewIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ); extern DWORD DSReleaseIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ); +extern DWORD DSStaticRefreshParams( PipeSendFunc Send, COMM_DHCP_REQ *Req ); +extern DWORD DSGetAdapterInfo( PipeSendFunc Send, COMM_DHCP_REQ *Req ); extern int inet_aton(const char *s, struct in_addr *addr); int warn( char *format, ... ); #endif/*ROSDHCP_H*/ diff --git a/reactos/services/dhcp/pipe.c b/reactos/services/dhcp/pipe.c index 2b98c30fdb9..fc1e8108424 100644 --- a/reactos/services/dhcp/pipe.c +++ b/reactos/services/dhcp/pipe.c @@ -9,6 +9,9 @@ #include +#define NDEBUG +#include + static HANDLE CommPipe = INVALID_HANDLE_VALUE, CommThread; DWORD CommThrId; @@ -30,8 +33,8 @@ DWORD PipeSend( COMM_DHCP_REPLY *Reply ) { DWORD WINAPI PipeThreadProc( LPVOID Parameter ) { DWORD BytesRead, BytesWritten; COMM_DHCP_REQ Req; - BOOL Result; - BOOLEAN Connection; + COMM_DHCP_REPLY Reply; + BOOL Result, Connection; while( (Connection = ConnectNamedPipe( CommPipe, NULL )) ) { Result = ReadFile( CommPipe, &Req, sizeof(Req), &BytesRead, NULL ); @@ -52,6 +55,21 @@ DWORD WINAPI PipeThreadProc( LPVOID Parameter ) { case DhcpReqRenewIpAddress: BytesWritten = DSRenewIpAddressLease( PipeSend, &Req ); break; + + case DhcpReqStaticRefreshParams: + BytesWritten = DSStaticRefreshParams( PipeSend, &Req ); + break; + + case DhcpReqGetAdapterInfo: + BytesWritten = DSGetAdapterInfo( PipeSend, &Req ); + break; + + default: + DPRINT1("Unrecognized request type %d\n", Req.Type); + ZeroMemory( &Reply, sizeof( COMM_DHCP_REPLY ) ); + Reply.Reply = 0; + BytesWritten = PipeSend( &Reply ); + break; } } DisconnectNamedPipe( CommPipe );