ReactOS  0.4.12-dev-102-g4b7f1e0
dhcpcsvc.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS system libraries
4  * FILE: lib/dhcpcapi/dhcpcapi.c
5  * PURPOSE: Client API for DHCP
6  * COPYRIGHT: Copyright 2005 Art Yerkes <ayerkes@speakeasy.net>
7  */
8 
9 #include <rosdhcp.h>
10 #include <winsvc.h>
11 
12 #define NDEBUG
13 #include <debug.h>
14 
15 static WCHAR ServiceName[] = L"DHCP";
16 
20 
22 
25 {
26  DWORD PipeMode;
27 
28  /* Wait for the pipe to be available */
30  {
31  /* No good, we failed */
32  return GetLastError();
33  }
34 
35  /* It's available, let's try to open it */
36  PipeHandle = CreateFileW(DHCP_PIPE_NAME,
39  NULL,
41  0,
42  NULL);
43 
44  /* Check if we succeeded in opening the pipe */
45  if (PipeHandle == INVALID_HANDLE_VALUE)
46  {
47  /* We didn't */
48  return GetLastError();
49  }
50 
51  /* Change the pipe into message mode */
52  PipeMode = PIPE_READMODE_MESSAGE;
53  if (!SetNamedPipeHandleState(PipeHandle, &PipeMode, NULL, NULL))
54  {
55  /* Mode change failed */
56  CloseHandle(PipeHandle);
57  PipeHandle = INVALID_HANDLE_VALUE;
58  return GetLastError();
59  }
60  else
61  {
62  /* We're good to go */
63  *Version = 2;
64  return NO_ERROR;
65  }
66 }
67 
70 {
71  CloseHandle(PipeHandle);
72  PipeHandle = INVALID_HANDLE_VALUE;
73 }
74 
76 DhcpQueryHWInfo(DWORD AdapterIndex,
77  PDWORD MediaType,
78  PDWORD Mtu,
79  PDWORD Speed)
80 {
81  COMM_DHCP_REQ Req;
82  COMM_DHCP_REPLY Reply;
84  BOOL Result;
85 
86  ASSERT(PipeHandle != INVALID_HANDLE_VALUE);
87 
89  Req.AdapterIndex = AdapterIndex;
90 
91  Result = TransactNamedPipe(PipeHandle,
92  &Req, sizeof(Req),
93  &Reply, sizeof(Reply),
94  &BytesRead, NULL);
95  if (!Result)
96  {
97  /* Pipe transaction failed */
98  return 0;
99  }
100 
101  if (Reply.Reply == 0)
102  return 0;
103 
104  *MediaType = Reply.QueryHWInfo.MediaType;
105  *Mtu = Reply.QueryHWInfo.Mtu;
106  *Speed = Reply.QueryHWInfo.Speed;
107  return 1;
108 }
109 
112 {
113  COMM_DHCP_REQ Req;
114  COMM_DHCP_REPLY Reply;
116  BOOL Result;
117 
118  ASSERT(PipeHandle != INVALID_HANDLE_VALUE);
119 
121  Req.AdapterIndex = AdapterIndex;
122 
123  Result = TransactNamedPipe(PipeHandle,
124  &Req, sizeof(Req),
125  &Reply, sizeof(Reply),
126  &BytesRead, NULL);
127  if (!Result)
128  {
129  /* Pipe transaction failed */
130  return 0;
131  }
132 
133  return Reply.Reply;
134 }
135 
138 {
139  COMM_DHCP_REQ Req;
140  COMM_DHCP_REPLY Reply;
142  BOOL Result;
143 
144  ASSERT(PipeHandle != INVALID_HANDLE_VALUE);
145 
147  Req.AdapterIndex = AdapterIndex;
148 
149  Result = TransactNamedPipe(PipeHandle,
150  &Req, sizeof(Req),
151  &Reply, sizeof(Reply),
152  &BytesRead, NULL);
153  if (!Result)
154  {
155  /* Pipe transaction failed */
156  return 0;
157  }
158 
159  return Reply.Reply;
160 }
161 
164 {
165  COMM_DHCP_REQ Req;
166  COMM_DHCP_REPLY Reply;
168  BOOL Result;
169 
170  ASSERT(PipeHandle != INVALID_HANDLE_VALUE);
171 
173  Req.AdapterIndex = AdapterIndex;
174 
175  Result = TransactNamedPipe(PipeHandle,
176  &Req, sizeof(Req),
177  &Reply, sizeof(Reply),
178  &BytesRead, NULL);
179  if (!Result)
180  {
181  /* Pipe transaction failed */
182  return 0;
183  }
184 
185  return Reply.Reply;
186 }
187 
190  DWORD Address,
191  DWORD Netmask)
192 {
193  COMM_DHCP_REQ Req;
194  COMM_DHCP_REPLY Reply;
196  BOOL Result;
197 
198  ASSERT(PipeHandle != INVALID_HANDLE_VALUE);
199 
201  Req.AdapterIndex = AdapterIndex;
202  Req.Body.StaticRefreshParams.IPAddress = Address;
203  Req.Body.StaticRefreshParams.Netmask = Netmask;
204 
205  Result = TransactNamedPipe(PipeHandle,
206  &Req, sizeof(Req),
207  &Reply, sizeof(Reply),
208  &BytesRead, NULL);
209  if (!Result)
210  {
211  /* Pipe transaction failed */
212  return 0;
213  }
214 
215  return Reply.Reply;
216 }
217 
248  BOOL NewIpAddress,
249  DWORD IpIndex,
250  DWORD IpAddress,
251  DWORD SubnetMask,
252  INT DhcpAction)
253 {
254  DPRINT1("DHCPCSVC: DhcpNotifyConfigChange not implemented yet\n");
255  return 0;
256 }
257 
260  PVOID Reserved,
262  LPDHCPCAPI_CLASSID ClassId,
263  DHCPCAPI_PARAMS_ARRAY SendParams,
264  DHCPCAPI_PARAMS_ARRAY RecdParams,
265  LPBYTE Buffer,
266  LPDWORD pSize,
267  LPWSTR RequestIdStr)
268 {
270  return 0;
271 }
272 
299  PBOOL DhcpEnabled,
300  PDWORD DhcpServer,
301  time_t* LeaseObtained,
302  time_t* LeaseExpires)
303 {
304  COMM_DHCP_REQ Req;
305  COMM_DHCP_REPLY Reply;
307  BOOL Result;
308 
309  ASSERT(PipeHandle != INVALID_HANDLE_VALUE);
310 
312  Req.AdapterIndex = AdapterIndex;
313 
314  Result = TransactNamedPipe(PipeHandle,
315  &Req, sizeof(Req),
316  &Reply, sizeof(Reply),
317  &BytesRead, NULL);
318 
319  if (Result && Reply.Reply != 0)
320  *DhcpEnabled = Reply.GetAdapterInfo.DhcpEnabled;
321  else
322  *DhcpEnabled = FALSE;
323 
324  if (*DhcpEnabled)
325  {
326  *DhcpServer = Reply.GetAdapterInfo.DhcpServer;
327  *LeaseObtained = Reply.GetAdapterInfo.LeaseObtained;
328  *LeaseExpires = Reply.GetAdapterInfo.LeaseExpires;
329  }
330  else
331  {
332  *DhcpServer = INADDR_NONE;
333  *LeaseObtained = 0;
334  *LeaseExpires = 0;
335  }
336 
337  return Reply.Reply;
338 }
339 
340 
341 static VOID
343 {
345  ServiceStatus.dwCurrentState = dwState;
346 
347  if (dwState == SERVICE_RUNNING)
349  else
350  ServiceStatus.dwControlsAccepted = 0;
351 
352  ServiceStatus.dwWin32ExitCode = 0;
353  ServiceStatus.dwServiceSpecificExitCode = 0;
354  ServiceStatus.dwCheckPoint = 0;
355 
356  if (dwState == SERVICE_START_PENDING ||
357  dwState == SERVICE_STOP_PENDING)
358  ServiceStatus.dwWaitHint = 1000;
359  else
360  ServiceStatus.dwWaitHint = 0;
361 
363  &ServiceStatus);
364 }
365 
366 static DWORD WINAPI
368  DWORD dwEventType,
369  LPVOID lpEventData,
370  LPVOID lpContext)
371 {
372  switch (dwControl)
373  {
377  if (hStopEvent != NULL)
378  SetEvent(hStopEvent);
379  return ERROR_SUCCESS;
380 
383  &ServiceStatus);
384  return ERROR_SUCCESS;
385 
386  default:
388  }
389 }
390 
391 VOID WINAPI
393 {
396  NULL);
397  if (!ServiceStatusHandle)
398  {
399  DPRINT1("DHCPCSVC: Unable to register service control handler (%lx)\n", GetLastError());
400  return;
401  }
402 
404 
405  /* Create the stop event */
406  hStopEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
407  if (hStopEvent == NULL)
408  {
410  return;
411  }
412 
414 
415  if (!init_client())
416  {
417  DPRINT1("DHCPCSVC: init_client() failed!\n");
419  return;
420  }
421 
422  DH_DbgPrint(MID_TRACE,("DHCP Service Started\n"));
423 
425 
426  DH_DbgPrint(MID_TRACE,("Going into dispatch()\n"));
427  DH_DbgPrint(MID_TRACE,("DHCPCSVC: DHCP service is starting up\n"));
428 
429  dispatch(hStopEvent);
430 
431  DH_DbgPrint(MID_TRACE,("DHCPCSVC: DHCP service is shutting down\n"));
432  stop_client();
433 
435 }
436 
437 BOOL WINAPI
439  DWORD fdwReason,
441 {
442  switch (fdwReason)
443  {
444  case DLL_PROCESS_ATTACH:
445  DisableThreadLibraryCalls(hinstDLL);
446  break;
447 
448  case DLL_PROCESS_DETACH:
449  break;
450  }
451 
452  return TRUE;
453 }
454 
455 /* EOF */
static int argc
Definition: ServiceArgs.c:12
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
IN PVOID IN PVOID IN USHORT Version
Definition: pci.h:359
#define MID_TRACE
Definition: debug.h:15
struct _COMM_DHCP_REPLY::@3289 GetAdapterInfo
#define ERROR_SUCCESS
Definition: deptool.c:10
DWORD dwCurrentState
Definition: winsvc.h:100
static DWORD WINAPI ServiceControlHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
Definition: dhcpcsvc.c:367
__wchar_t WCHAR
Definition: xmlstorage.h:180
SERVICE_STATUS_HANDLE ServiceStatusHandle
Definition: dhcpcsvc.c:17
DWORD APIENTRY DhcpReleaseIpAddressLease(DWORD AdapterIndex)
Definition: dhcpcsvc.c:137
BOOL WINAPI WaitNamedPipeW(LPCWSTR lpNamedPipeName, DWORD nTimeOut)
Definition: npipe.c:458
#define INADDR_NONE
Definition: tcp.c:42
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85
static WCHAR ServiceName[]
Definition: dhcpcsvc.c:15
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define SERVICE_START_PENDING
Definition: winsvc.h:22
DWORD dwServiceSpecificExitCode
Definition: winsvc.h:103
DWORD APIENTRY DhcpQueryHWInfo(DWORD AdapterIndex, PDWORD MediaType, PDWORD Mtu, PDWORD Speed)
Definition: dhcpcsvc.c:76
#define DLL_PROCESS_ATTACH
Definition: compat.h:120
#define NMPWAIT_USE_DEFAULT_WAIT
Definition: winbase.h:134
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Definition: dhcpcsvc.c:438
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define NO_ERROR
Definition: dderror.h:5
union _COMM_DHCP_REQ::@3283 Body
int32_t INT
Definition: typedefs.h:56
static char ** argv
Definition: ServiceArgs.c:11
static VOID UpdateServiceStatus(DWORD dwState)
Definition: dhcpcsvc.c:342
static IN DWORD IN LPVOID lpvReserved
#define FILE_SHARE_READ
Definition: compat.h:125
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:597
VOID WINAPI ServiceMain(DWORD argc, LPWSTR *argv)
Definition: dhcpcsvc.c:392
DWORD APIENTRY DhcpNotifyConfigChange(LPWSTR ServerName, LPWSTR AdapterName, BOOL NewIpAddress, DWORD IpIndex, DWORD IpAddress, DWORD SubnetMask, INT DhcpAction)
Definition: dhcpcsvc.c:246
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:957
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
DWORD dwCheckPoint
Definition: winsvc.h:104
#define SERVICE_STOPPED
Definition: winsvc.h:21
DWORD APIENTRY DhcpLeaseIpAddress(DWORD AdapterIndex)
Definition: dhcpcsvc.c:111
void stop_client(void)
Definition: dhclient.c:137
unsigned char * LPBYTE
Definition: typedefs.h:52
HANDLE hStopEvent
Definition: dhcpcsvc.c:19
#define GENERIC_WRITE
Definition: nt_native.h:90
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define APIENTRY
Definition: nt_native.h:48
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
_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:426
#define DH_DbgPrint(_t_, _x_)
Definition: debug.h:49
_Reserved_ PVOID Reserved
Definition: winddi.h:3974
#define SERVICE_CONTROL_INTERROGATE
Definition: winsvc.h:39
Definition: bufpool.h:45
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:960
BOOL * PBOOL
Definition: windef.h:161
#define OPEN_EXISTING
Definition: compat.h:426
#define DLL_PROCESS_DETACH
Definition: compat.h:119
static SERVICE_STATUS_HANDLE(WINAPI *pRegisterServiceCtrlHandlerExA)(LPCSTR
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI SetNamedPipeHandleState(HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout)
Definition: npipe.c:774
DWORD dwWaitHint
Definition: winsvc.h:105
DWORD dwWin32ExitCode
Definition: winsvc.h:102
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD dwServiceType
Definition: winsvc.h:99
DWORD APIENTRY DhcpStaticRefreshParams(DWORD AdapterIndex, DWORD Address, DWORD Netmask)
Definition: dhcpcsvc.c:189
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
struct _COMM_DHCP_REPLY::@3288 QueryHWInfo
static const WCHAR L[]
Definition: oid.c:1087
DWORD APIENTRY DhcpCApiInitialize(LPDWORD Version)
Definition: dhcpcsvc.c:24
#define GENERIC_READ
Definition: compat.h:124
SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExW(LPCWSTR lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, LPVOID lpContext)
Definition: sctrl.c:773
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
void dispatch(HANDLE hStopEvent)
Definition: dispatch.c:66
#define WINAPI
Definition: msvc.h:20
#define PIPE_READMODE_MESSAGE
Definition: winbase.h:170
DWORD dwControlsAccepted
Definition: winsvc.h:101
#define DHCP_PIPE_NAME
__kernel_time_t time_t
Definition: linux.h:252
int init_client(void)
Definition: dhclient.c:111
static HANDLE PipeHandle
Definition: dhcpcsvc.c:21
DWORD * PDWORD
Definition: pedump.c:68
#define DPRINT1
Definition: precomp.h:8
#define CreateFileW
Definition: compat.h:400
SERVICE_STATUS ServiceStatus
Definition: dhcpcsvc.c:18
DWORD APIENTRY DhcpRosGetAdapterInfo(DWORD AdapterIndex, PBOOL DhcpEnabled, PDWORD DhcpServer, time_t *LeaseObtained, time_t *LeaseExpires)
Definition: dhcpcsvc.c:298
uint32_t * LPDWORD
Definition: typedefs.h:57
DWORD APIENTRY DhcpRenewIpAddressLease(DWORD AdapterIndex)
Definition: dhcpcsvc.c:163
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92
#define UNIMPLEMENTED
Definition: debug.h:114
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36
WCHAR * LPWSTR
Definition: xmlstorage.h:184
_Must_inspect_result_ _Out_ PNDIS_STATUS _Out_ PNDIS_STATUS _Out_ PNDIS_HANDLE _Out_ PUINT _In_ UINT _In_ NDIS_HANDLE _In_ NDIS_HANDLE _In_ PNDIS_STRING AdapterName
Definition: ndis.h:5995
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
_Must_inspect_result_ _In_ PFILE_OBJECT _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ FLT_IO_OPERATION_FLAGS _Out_opt_ PULONG BytesRead
Definition: fltkernel.h:1255
struct _COMM_DHCP_REQ::@3283::@3287 StaticRefreshParams
VOID APIENTRY DhcpCApiCleanup(VOID)
Definition: dhcpcsvc.c:69
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: dhcpcsvc.c:259
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40