ReactOS 0.4.16-dev-112-g52265ae
dhcpcsvc.c File Reference
#include <rosdhcp.h>
#include <winsvc.h>
#include <debug.h>
Include dependency graph for dhcpcsvc.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

DWORD APIENTRY DhcpCApiInitialize (LPDWORD Version)
 
VOID APIENTRY DhcpCApiCleanup (VOID)
 
DWORD APIENTRY DhcpQueryHWInfo (DWORD AdapterIndex, PDWORD MediaType, PDWORD Mtu, PDWORD Speed)
 
DWORD APIENTRY DhcpLeaseIpAddress (DWORD AdapterIndex)
 
DWORD APIENTRY DhcpReleaseIpAddressLease (DWORD AdapterIndex)
 
DWORD APIENTRY DhcpRenewIpAddressLease (DWORD AdapterIndex)
 
DWORD APIENTRY DhcpStaticRefreshParams (DWORD AdapterIndex, DWORD Address, DWORD Netmask)
 
DWORD APIENTRY DhcpNotifyConfigChange (LPWSTR ServerName, LPWSTR AdapterName, BOOL NewIpAddress, DWORD IpIndex, DWORD IpAddress, DWORD SubnetMask, INT DhcpAction)
 
DWORD APIENTRY DhcpRequestParams (DWORD Flags, PVOID Reserved, LPWSTR AdapterName, LPDHCPCAPI_CLASSID ClassId, DHCPCAPI_PARAMS_ARRAY SendParams, DHCPCAPI_PARAMS_ARRAY RecdParams, LPBYTE Buffer, LPDWORD pSize, LPWSTR RequestIdStr)
 
DWORD APIENTRY DhcpRosGetAdapterInfo (DWORD AdapterIndex, PBOOL DhcpEnabled, PDWORD DhcpServer, time_t *LeaseObtained, time_t *LeaseExpires)
 
static VOID UpdateServiceStatus (DWORD dwState)
 
static DWORD WINAPI ServiceControlHandler (DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
 
VOID WINAPI ServiceMain (DWORD argc, LPWSTR *argv)
 
BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
 

Variables

static WCHAR ServiceName [] = L"DHCP"
 
SERVICE_STATUS_HANDLE ServiceStatusHandle = 0
 
SERVICE_STATUS ServiceStatus
 
HANDLE hStopEvent = NULL
 
HANDLE hAdapterStateChangedEvent = NULL
 
static HANDLE PipeHandle = INVALID_HANDLE_VALUE
 
SOCKET DhcpSocket
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file dhcpcsvc.c.

Function Documentation

◆ DhcpCApiCleanup()

VOID APIENTRY DhcpCApiCleanup ( VOID  )

Definition at line 71 of file dhcpcsvc.c.

72{
75}
static HANDLE PipeHandle
Definition: dhcpcsvc.c:22
#define CloseHandle
Definition: compat.h:739
#define INVALID_HANDLE_VALUE
Definition: compat.h:731

Referenced by getDhcpInfoForAdapter(), INetCfgComponentControl_fnApplyRegistryChanges(), IpReleaseAddress(), and IpRenewAddress().

◆ DhcpCApiInitialize()

DWORD APIENTRY DhcpCApiInitialize ( LPDWORD  Version)

Definition at line 26 of file dhcpcsvc.c.

27{
28 DWORD PipeMode;
29
30 /* Wait for the pipe to be available */
32 {
33 /* No good, we failed */
34 return GetLastError();
35 }
36
37 /* It's available, let's try to open it */
41 NULL,
43 0,
44 NULL);
45
46 /* Check if we succeeded in opening the pipe */
48 {
49 /* We didn't */
50 return GetLastError();
51 }
52
53 /* Change the pipe into message mode */
54 PipeMode = PIPE_READMODE_MESSAGE;
56 {
57 /* Mode change failed */
60 return GetLastError();
61 }
62 else
63 {
64 /* We're good to go */
65 *Version = 2;
66 return NO_ERROR;
67 }
68}
#define NO_ERROR
Definition: dderror.h:5
#define NULL
Definition: types.h:112
#define OPEN_EXISTING
Definition: compat.h:775
#define GENERIC_READ
Definition: compat.h:135
#define CreateFileW
Definition: compat.h:741
#define FILE_SHARE_READ
Definition: compat.h:136
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL WINAPI SetNamedPipeHandleState(HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout)
Definition: npipe.c:774
BOOL WINAPI WaitNamedPipeW(LPCWSTR lpNamedPipeName, DWORD nTimeOut)
Definition: npipe.c:458
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define GENERIC_WRITE
Definition: nt_native.h:90
#define DHCP_PIPE_NAME
_Must_inspect_result_ _In_ WDFDEVICE _In_ LPCGUID _Out_ PINTERFACE _In_ USHORT _In_ USHORT Version
Definition: wdffdo.h:469
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define NMPWAIT_USE_DEFAULT_WAIT
Definition: winbase.h:134
#define PIPE_READMODE_MESSAGE
Definition: winbase.h:170

Referenced by getDhcpInfoForAdapter(), INetCfgComponentControl_fnApplyRegistryChanges(), IpReleaseAddress(), and IpRenewAddress().

◆ DhcpLeaseIpAddress()

DWORD APIENTRY DhcpLeaseIpAddress ( DWORD  AdapterIndex)

Definition at line 113 of file dhcpcsvc.c.

114{
115 COMM_DHCP_REQ Req;
116 COMM_DHCP_REPLY Reply;
118 BOOL Result;
119
121
123 Req.AdapterIndex = AdapterIndex;
124
126 &Req, sizeof(Req),
127 &Reply, sizeof(Reply),
128 &BytesRead, NULL);
129 if (!Result)
130 {
131 /* Pipe transaction failed */
132 return 0;
133 }
134
135 return Reply.Reply;
136}
unsigned int BOOL
Definition: ntddk_ex.h:94
#define ASSERT(a)
Definition: mode.c:44
BOOL WINAPI TransactNamedPipe(IN HANDLE hNamedPipe, IN LPVOID lpInBuffer, IN DWORD nInBufferSize, OUT LPVOID lpOutBuffer, IN DWORD nOutBufferSize, OUT LPDWORD lpBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: npipe.c:1315
@ DhcpReqLeaseIpAddress
Definition: rosdhcp_public.h:5
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PLONGLONG _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesRead
Definition: wdfiotarget.h:870
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409

◆ DhcpNotifyConfigChange()

DWORD APIENTRY DhcpNotifyConfigChange ( LPWSTR  ServerName,
LPWSTR  AdapterName,
BOOL  NewIpAddress,
DWORD  IpIndex,
DWORD  IpAddress,
DWORD  SubnetMask,
INT  DhcpAction 
)

Set new TCP/IP parameters and notify DHCP client service of this

Parameters
[in]ServerNameNULL for local machine
[in]AdapterNameIPHLPAPI name of adapter to change
[in]NewIpAddressTRUE if IP address changes
[in]IpAddressNew IP address (network byte order)
[in]SubnetMaskNew subnet mask (network byte order)
[in]DhcpAction0 - don't modify 1 - enable DHCP 2 - disable DHCP
Returns
non-zero on success
Remarks
Undocumented by Microsoft

Definition at line 248 of file dhcpcsvc.c.

255{
256 DPRINT1("DHCPCSVC: DhcpNotifyConfigChange not implemented yet\n");
257 return 0;
258}
#define DPRINT1
Definition: precomp.h:8

◆ DhcpQueryHWInfo()

DWORD APIENTRY DhcpQueryHWInfo ( DWORD  AdapterIndex,
PDWORD  MediaType,
PDWORD  Mtu,
PDWORD  Speed 
)

Definition at line 78 of file dhcpcsvc.c.

82{
83 COMM_DHCP_REQ Req;
84 COMM_DHCP_REPLY Reply;
87
89
91 Req.AdapterIndex = AdapterIndex;
92
94 &Req, sizeof(Req),
95 &Reply, sizeof(Reply),
96 &BytesRead, NULL);
97 if (!Result)
98 {
99 /* Pipe transaction failed */
100 return 0;
101 }
102
103 if (Reply.Reply == 0)
104 return 0;
105
106 *MediaType = Reply.QueryHWInfo.MediaType;
107 *Mtu = Reply.QueryHWInfo.Mtu;
108 *Speed = Reply.QueryHWInfo.Speed;
109 return 1;
110}
@ DhcpReqQueryHWInfo
Definition: rosdhcp_public.h:6
struct _COMM_DHCP_REPLY::@3452 QueryHWInfo

◆ DhcpReleaseIpAddressLease()

DWORD APIENTRY DhcpReleaseIpAddressLease ( DWORD  AdapterIndex)

Definition at line 139 of file dhcpcsvc.c.

140{
141 COMM_DHCP_REQ Req;
142 COMM_DHCP_REPLY Reply;
144 BOOL Result;
145
147
149 Req.AdapterIndex = AdapterIndex;
150
152 &Req, sizeof(Req),
153 &Reply, sizeof(Reply),
154 &BytesRead, NULL);
155 if (!Result)
156 {
157 /* Pipe transaction failed */
158 return 0;
159 }
160
161 return Reply.Reply;
162}
@ DhcpReqReleaseIpAddress
Definition: rosdhcp_public.h:7

Referenced by IpReleaseAddress().

◆ DhcpRenewIpAddressLease()

DWORD APIENTRY DhcpRenewIpAddressLease ( DWORD  AdapterIndex)

Definition at line 165 of file dhcpcsvc.c.

166{
167 COMM_DHCP_REQ Req;
168 COMM_DHCP_REPLY Reply;
170 BOOL Result;
171
173
175 Req.AdapterIndex = AdapterIndex;
176
178 &Req, sizeof(Req),
179 &Reply, sizeof(Reply),
180 &BytesRead, NULL);
181 if (!Result)
182 {
183 /* Pipe transaction failed */
184 return 0;
185 }
186
187 return Reply.Reply;
188}
@ DhcpReqRenewIpAddress
Definition: rosdhcp_public.h:8

Referenced by IpRenewAddress().

◆ DhcpRequestParams()

DWORD APIENTRY DhcpRequestParams ( DWORD  Flags,
PVOID  Reserved,
LPWSTR  AdapterName,
LPDHCPCAPI_CLASSID  ClassId,
DHCPCAPI_PARAMS_ARRAY  SendParams,
DHCPCAPI_PARAMS_ARRAY  RecdParams,
LPBYTE  Buffer,
LPDWORD  pSize,
LPWSTR  RequestIdStr 
)

Definition at line 261 of file dhcpcsvc.c.

270{
272 return 0;
273}
#define UNIMPLEMENTED
Definition: debug.h:118

Referenced by detect_proxy_autoconfig_url_dhcp().

◆ DhcpRosGetAdapterInfo()

DWORD APIENTRY DhcpRosGetAdapterInfo ( DWORD  AdapterIndex,
PBOOL  DhcpEnabled,
PDWORD  DhcpServer,
time_t LeaseObtained,
time_t LeaseExpires 
)

Get DHCP info for an adapter

Parameters
[in]AdapterIndexIndex of the adapter (iphlpapi-style) for which info is requested
[out]DhcpEnabledReturns whether DHCP is enabled for the adapter
[out]DhcpServerReturns DHCP server IP address (255.255.255.255 if no server reached yet), in network byte order
[out]LeaseObtainedReturns time at which the lease was obtained
[out]LeaseExpiresReturns time at which the lease will expire
Returns
non-zero on success
Remarks
This is a ReactOS-only routine

Definition at line 300 of file dhcpcsvc.c.

305{
306 COMM_DHCP_REQ Req;
307 COMM_DHCP_REPLY Reply;
309 BOOL Result;
310
312
314 Req.AdapterIndex = AdapterIndex;
315
317 &Req, sizeof(Req),
318 &Reply, sizeof(Reply),
319 &BytesRead, NULL);
320
321 if (Result && Reply.Reply != 0)
322 *DhcpEnabled = Reply.GetAdapterInfo.DhcpEnabled;
323 else
324 *DhcpEnabled = FALSE;
325
326 if (*DhcpEnabled)
327 {
328 *DhcpServer = Reply.GetAdapterInfo.DhcpServer;
329 *LeaseObtained = Reply.GetAdapterInfo.LeaseObtained;
330 *LeaseExpires = Reply.GetAdapterInfo.LeaseExpires;
331 }
332 else
333 {
334 *DhcpServer = INADDR_NONE;
335 *LeaseObtained = 0;
336 *LeaseExpires = 0;
337 }
338
339 return Reply.Reply;
340}
#define INADDR_NONE
Definition: tcp.c:42
#define FALSE
Definition: types.h:117
@ DhcpReqGetAdapterInfo
struct _COMM_DHCP_REPLY::@3453 GetAdapterInfo

Referenced by getDhcpInfoForAdapter().

◆ DhcpStaticRefreshParams()

DWORD APIENTRY DhcpStaticRefreshParams ( DWORD  AdapterIndex,
DWORD  Address,
DWORD  Netmask 
)

Definition at line 191 of file dhcpcsvc.c.

194{
195 COMM_DHCP_REQ Req;
196 COMM_DHCP_REPLY Reply;
198 BOOL Result;
199
201
203 Req.AdapterIndex = AdapterIndex;
204 Req.Body.StaticRefreshParams.IPAddress = Address;
205 Req.Body.StaticRefreshParams.Netmask = Netmask;
206
208 &Req, sizeof(Req),
209 &Reply, sizeof(Reply),
210 &BytesRead, NULL);
211 if (!Result)
212 {
213 /* Pipe transaction failed */
214 return 0;
215 }
216
217 return Reply.Reply;
218}
static WCHAR Address[46]
Definition: ping.c:68
@ DhcpReqStaticRefreshParams
Definition: rosdhcp_public.h:9
struct _COMM_DHCP_REQ::@3447::@3451 StaticRefreshParams
union _COMM_DHCP_REQ::@3447 Body

Referenced by INetCfgComponentControl_fnApplyRegistryChanges().

◆ DllMain()

BOOL WINAPI DllMain ( HINSTANCE  hinstDLL,
DWORD  fdwReason,
LPVOID  lpvReserved 
)

Definition at line 507 of file dhcpcsvc.c.

510{
511 switch (fdwReason)
512 {
515 break;
516
518 break;
519 }
520
521 return TRUE;
522}
#define TRUE
Definition: types.h:120
#define DLL_PROCESS_ATTACH
Definition: compat.h:131
#define DLL_PROCESS_DETACH
Definition: compat.h:130
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85

◆ ServiceControlHandler()

static DWORD WINAPI ServiceControlHandler ( DWORD  dwControl,
DWORD  dwEventType,
LPVOID  lpEventData,
LPVOID  lpContext 
)
static

Definition at line 369 of file dhcpcsvc.c.

373{
374 switch (dwControl)
375 {
379 if (hStopEvent != NULL)
381 return ERROR_SUCCESS;
382
386 return ERROR_SUCCESS;
387
388 default:
390 }
391}
#define ERROR_SUCCESS
Definition: deptool.c:10
static VOID UpdateServiceStatus(DWORD dwState)
Definition: dhcpcsvc.c:344
SERVICE_STATUS_HANDLE ServiceStatusHandle
Definition: dhcpcsvc.c:17
HANDLE hStopEvent
Definition: dhcpcsvc.c:19
SERVICE_STATUS ServiceStatus
Definition: dhcpcsvc.c:18
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:997
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
#define SERVICE_CONTROL_INTERROGATE
Definition: winsvc.h:39

Referenced by ServiceMain().

◆ ServiceMain()

static VOID CALLBACK ServiceMain ( DWORD  argc,
LPWSTR argv 
)

Definition at line 394 of file dhcpcsvc.c.

395{
396 HANDLE hPipeThread = INVALID_HANDLE_VALUE;
397 HANDLE hDiscoveryThread = INVALID_HANDLE_VALUE;
398 DWORD ret;
399
402 NULL);
404 {
405 DPRINT1("DHCPCSVC: Unable to register service control handler (%lx)\n", GetLastError());
406 return;
407 }
408
410
411 /* Create the stop event */
413 if (hStopEvent == NULL)
414 {
416 return;
417 }
418
421 {
424 return;
425 }
426
428
429 if (!init_client())
430 {
431 DbgPrint("DHCPCSVC: init_client() failed!\n");
435 return;
436 }
437
439
440 hPipeThread = PipeInit(hStopEvent);
441 if (hPipeThread == INVALID_HANDLE_VALUE)
442 {
443 DbgPrint("DHCPCSVC: PipeInit() failed!\n");
444 stop_client();
448 return;
449 }
450
451 hDiscoveryThread = StartAdapterDiscovery(hStopEvent);
452 if (hDiscoveryThread == INVALID_HANDLE_VALUE)
453 {
454 DbgPrint("DHCPCSVC: StartAdapterDiscovery() failed!\n");
455 stop_client();
459 return;
460 }
461
462 DH_DbgPrint(MID_TRACE,("DHCP Service Started\n"));
463
465
466 DH_DbgPrint(MID_TRACE,("Going into dispatch()\n"));
467 DH_DbgPrint(MID_TRACE,("DHCPCSVC: DHCP service is starting up\n"));
468
470
471 DH_DbgPrint(MID_TRACE,("DHCPCSVC: DHCP service is shutting down\n"));
472
473 stop_client();
474
475 DPRINT("Wait for pipe thread to close! %p\n", hPipeThread);
476 if (hPipeThread != INVALID_HANDLE_VALUE)
477 {
478 DPRINT("Waiting for pipe thread\n");
479 ret = WaitForSingleObject(hPipeThread, 5000);
480 DPRINT("Done %lx\n", ret);
481 }
482
483 DPRINT("Wait for discovery thread to close! %p\n", hDiscoveryThread);
484 if (hDiscoveryThread != INVALID_HANDLE_VALUE)
485 {
486 DPRINT("Waiting for discovery thread\n");
487 ret = WaitForSingleObject(hDiscoveryThread, 5000);
488 DPRINT("Done %lx\n", ret);
489 }
490
491 DPRINT("Closing events!\n");
494
497
498 CloseHandle(hDiscoveryThread);
499 CloseHandle(hPipeThread);
500
501 DPRINT("Done!\n");
502
504}
HANDLE StartAdapterDiscovery(HANDLE hStopEvent)
Definition: adapter.c:481
void dispatch(HANDLE hStopEvent)
Definition: dispatch.c:70
HANDLE PipeInit(HANDLE hStopEvent)
Definition: pipe.c:422
#define MID_TRACE
Definition: debug.h:15
#define DH_DbgPrint(_t_, _x_)
Definition: debug.h:49
int init_client(void)
Definition: dhclient.c:111
void stop_client(void)
Definition: dhclient.c:129
static WCHAR ServiceName[]
Definition: dhcpcsvc.c:15
HANDLE hAdapterStateChangedEvent
Definition: dhcpcsvc.c:20
SOCKET DhcpSocket
Definition: adapter.c:8
static DWORD WINAPI ServiceControlHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
Definition: dhcpcsvc.c:369
#define DbgPrint
Definition: hal.h:12
#define closesocket
Definition: ncftp.h:477
SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExW(LPCWSTR lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, LPVOID lpContext)
Definition: sctrl.c:812
#define DPRINT
Definition: sndvol32.h:73
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:651
int ret
#define CreateEvent
Definition: winbase.h:3748
#define INVALID_SOCKET
Definition: winsock.h:332
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_START_PENDING
Definition: winsvc.h:22
#define SERVICE_RUNNING
Definition: winsvc.h:24

◆ UpdateServiceStatus()

static VOID UpdateServiceStatus ( DWORD  dwState)
static

Definition at line 344 of file dhcpcsvc.c.

345{
348
349 if (dwState == SERVICE_RUNNING)
351 else
353
357
358 if (dwState == SERVICE_START_PENDING ||
359 dwState == SERVICE_STOP_PENDING)
361 else
363
366}
DWORD dwServiceType
Definition: winsvc.h:99
DWORD dwWin32ExitCode
Definition: winsvc.h:102
DWORD dwControlsAccepted
Definition: winsvc.h:101
DWORD dwWaitHint
Definition: winsvc.h:105
DWORD dwCurrentState
Definition: winsvc.h:100
DWORD dwCheckPoint
Definition: winsvc.h:104
DWORD dwServiceSpecificExitCode
Definition: winsvc.h:103
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
#define SERVICE_WIN32_SHARE_PROCESS
Definition: cmtypes.h:963

Referenced by ServiceControlHandler(), and ServiceMain().

Variable Documentation

◆ DhcpSocket

SOCKET DhcpSocket
extern

Definition at line 8 of file adapter.c.

Referenced by AdapterDiscoveryThread(), and ServiceMain().

◆ hAdapterStateChangedEvent

◆ hStopEvent

◆ PipeHandle

◆ ServiceName

WCHAR ServiceName[] = L"DHCP"
static

Definition at line 15 of file dhcpcsvc.c.

Referenced by ServiceMain().

◆ ServiceStatus

SERVICE_STATUS ServiceStatus

Definition at line 18 of file dhcpcsvc.c.

Referenced by ServiceControlHandler(), and UpdateServiceStatus().

◆ ServiceStatusHandle

SERVICE_STATUS_HANDLE ServiceStatusHandle = 0

Definition at line 17 of file dhcpcsvc.c.

Referenced by ServiceControlHandler(), ServiceMain(), and UpdateServiceStatus().