Obtain DHCP info from DHCP client service
authorGé van Geldorp <ge@gse.nl>
Sun, 8 Jan 2006 22:14:26 +0000 (22:14 +0000)
committerGé van Geldorp <ge@gse.nl>
Sun, 8 Jan 2006 22:14:26 +0000 (22:14 +0000)
svn path=/trunk/; revision=20736

14 files changed:
reactos/include/libs/dhcp/rosdhcp_public.h
reactos/lib/dhcpcsvc/dhcpcsvc.c
reactos/lib/dhcpcsvc/dhcpcsvc.def [deleted file]
reactos/lib/dhcpcsvc/dhcpcsvc.spec [new file with mode: 0644]
reactos/lib/dhcpcsvc/dhcpcsvc.xml
reactos/lib/iphlpapi/dhcp.h [new file with mode: 0644]
reactos/lib/iphlpapi/dhcp_reactos.c [new file with mode: 0644]
reactos/lib/iphlpapi/iphlpapi.xml
reactos/lib/iphlpapi/iphlpapi_main.c
reactos/services/dhcp/api.c
reactos/services/dhcp/dhclient.c
reactos/services/dhcp/include/dhcpd.h
reactos/services/dhcp/include/rosdhcp.h
reactos/services/dhcp/pipe.c

index c732e7f..7e0c34a 100644 (file)
@@ -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"
index b596db1..1d5fba6 100644 (file)
@@ -12,6 +12,7 @@
 #include <roscfg.h>
 #include <winsock2.h>
 #include <dhcpcsdk.h>
+#include <time.h>
 #include <dhcp/rosdhcp_public.h>
 
 #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 (file)
index 094e7e9..0000000
+++ /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 (file)
index 0000000..666c078
--- /dev/null
@@ -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 <gvg@reactos.org>
+#
+@ 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
index e0a2f02..7b6d01e 100644 (file)
@@ -1,5 +1,5 @@
 <module name="dhcpcsvc" type="win32dll" installbase="system32" installname="dhcpcsvc.dll">
-       <importlibrary definition="dhcpcsvc.def" />
+       <importlibrary definition="dhcpcsvc.spec.def" />
        <include base="dhcpcsvc">include</include>
        <define name="_DISABLE_TIDENTS" />
        <define name="__USE_W32API" />
@@ -11,4 +11,5 @@
        <library>iphlpapi</library>
        <file>dhcpcsvc.c</file>
        <file>dhcpcsvc.rc</file>
+       <file>dhcpcsvc.spec</file>
 </module>
diff --git a/reactos/lib/iphlpapi/dhcp.h b/reactos/lib/iphlpapi/dhcp.h
new file mode 100644 (file)
index 0000000..ba98fb6
--- /dev/null
@@ -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 <gvg@reactos.org>
+ */
+
+#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 (file)
index 0000000..1dd4b87
--- /dev/null
@@ -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 <gvg@reactos.org>
+ */
+
+#include "iphlpapi_private.h"
+#include "dhcp.h"
+#include <assert.h>
+
+#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);
+}
index afdd1b1..a35889f 100644 (file)
@@ -11,6 +11,8 @@
        <library>kernel32</library>
        <library>advapi32</library>
        <library>ws2_32</library>
+       <library>dhcpcsvc</library>
+       <file>dhcp_reactos.c</file>
        <file>ifenum_reactos.c</file>
        <file>ipstats_reactos.c</file>
        <file>iphlpapi_main.c</file>
index d5c0911..e5cf04f 100644 (file)
@@ -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,
index 0551914..b2d9c9e 100644 (file)
@@ -11,6 +11,9 @@
 #include <winsock2.h>
 #include <iphlpapi.h>
 
+#define NDEBUG
+#include <reactos/debug.h>
+
 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 );
+}
index 799b3c6..2d607d2 100644 (file)
@@ -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 ||
index 0ac202a..3e9dd53 100644 (file)
@@ -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;
index 1e9b8f7..9141953 100644 (file)
@@ -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*/
index 2b98c30..fc1e810 100644 (file)
@@ -9,6 +9,9 @@
 
 #include <rosdhcp.h>
 
+#define NDEBUG
+#include <reactos/debug.h>
+
 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 );