ReactOS 0.4.16-dev-1494-gd054f63
adapter.c File Reference
#include <rosdhcp.h>
#include <reactos/debug.h>
Include dependency graph for adapter.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

PCHARGetSubkeyNames (PCHAR MainKeyName, PCHAR Append)
 
PCHAR RegReadString (HKEY Root, PCHAR Subkey, PCHAR Value)
 
HKEY FindAdapterKey (PDHCP_ADAPTER Adapter)
 
BOOL PrepareAdapterForService (PDHCP_ADAPTER Adapter)
 
void AdapterInit ()
 
int InterfaceConnected (const MIB_IFROW *IfEntry)
 
BOOL IsReconnectHackNeeded (PDHCP_ADAPTER Adapter, const MIB_IFROW *IfEntry)
 
DWORD WINAPI AdapterDiscoveryThread (LPVOID Context)
 
HANDLE StartAdapterDiscovery (HANDLE hStopEvent)
 
void AdapterStop ()
 
PDHCP_ADAPTER AdapterFindIndex (unsigned int indx)
 
PDHCP_ADAPTER AdapterFindName (const CHAR *name)
 
PDHCP_ADAPTER AdapterFindInfo (struct interface_info *ip)
 
PDHCP_ADAPTER AdapterFindByHardwareAddress (u_int8_t haddr[16], u_int8_t hlen)
 
PDHCP_ADAPTER AdapterGetFirst ()
 
PDHCP_ADAPTER AdapterGetNext (PDHCP_ADAPTER This)
 
void if_register_send (struct interface_info *ip)
 
void if_register_receive (struct interface_info *ip)
 

Variables

HANDLE hAdapterStateChangedEvent
 
SOCKET DhcpSocket = INVALID_SOCKET
 
static LIST_ENTRY AdapterList
 
static WSADATA wsd
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 3 of file adapter.c.

Function Documentation

◆ AdapterDiscoveryThread()

DWORD WINAPI AdapterDiscoveryThread ( LPVOID  Context)

Definition at line 291 of file adapter.c.

291 {
293 DWORD Error, Size = sizeof(MIB_IFTABLE);
294 PDHCP_ADAPTER Adapter = NULL;
296 struct interface_info *ifi = NULL;
297 struct protocol *proto;
298 int i, AdapterCount = 0, Broadcast;
299
300 while (TRUE)
301 {
302 DH_DbgPrint(MID_TRACE,("Getting Adapter List...\n"));
303
304 while( (Error = GetIfTable(Table, &Size, 0 )) ==
306 DH_DbgPrint(MID_TRACE,("Error %d, New Buffer Size: %d\n", Error, Size));
307 free( Table );
309 }
310
311 if( Error != NO_ERROR )
312 {
313 /* HACK: We are waiting until TCP/IP starts */
314 Sleep(2000);
315 continue;
316 }
317
318 DH_DbgPrint(MID_TRACE,("Got Adapter List (%d entries)\n", Table->dwNumEntries));
319
320 for( i = Table->dwNumEntries - 1; i >= 0; i-- ) {
321 DH_DbgPrint(MID_TRACE,("Getting adapter %d attributes\n",
322 Table->table[i].dwIndex));
323
324 ApiLock();
325
326 if ((Adapter = AdapterFindByHardwareAddress(Table->table[i].bPhysAddr, Table->table[i].dwPhysAddrLen)))
327 {
329
330 /* This is an existing adapter */
331 if (InterfaceConnected(&Table->table[i])) {
332 /* We're still active so we stay in the list */
333 ifi = &Adapter->DhclientInfo;
334
335 /* This is a hack because IP helper API sucks */
336 if (IsReconnectHackNeeded(Adapter, &Table->table[i]))
337 {
338 /* This handles a disconnect/reconnect */
339
340 if (proto)
342 Adapter->DhclientInfo.client->state = S_INIT;
343
344 /* These are already invalid since the media state change */
345 Adapter->RouterMib.dwForwardNextHop = 0;
346 Adapter->NteContext = 0;
347
348 add_protocol(Adapter->DhclientInfo.name,
349 Adapter->DhclientInfo.rfdesc,
350 got_one, &Adapter->DhclientInfo);
351 state_init(&Adapter->DhclientInfo);
352
354 }
355
356 } else {
357 if (proto)
359
360 /* We've lost our link so out we go */
361 RemoveEntryList(&Adapter->ListEntry);
362 free(Adapter);
363 }
364
365 ApiUnlock();
366
367 continue;
368 }
369
370 ApiUnlock();
371
372 Adapter = (DHCP_ADAPTER*) calloc( sizeof( DHCP_ADAPTER ) + Table->table[i].dwMtu, 1 );
373
374 if (Adapter &&
375 (Table->table[i].dwType == MIB_IF_TYPE_ETHERNET || Table->table[i].dwType == IF_TYPE_IEEE80211) &&
376 InterfaceConnected(&Table->table[i])) {
377 memcpy( &Adapter->IfMib, &Table->table[i],
378 sizeof(Adapter->IfMib) );
379 Adapter->DhclientInfo.client = &Adapter->DhclientState;
380 Adapter->DhclientInfo.rbuf = Adapter->recv_buf;
381 Adapter->DhclientInfo.rbuf_max = Table->table[i].dwMtu;
382 Adapter->DhclientInfo.rbuf_len =
383 Adapter->DhclientInfo.rbuf_offset = 0;
384 memcpy(Adapter->DhclientInfo.hw_address.haddr,
385 Adapter->IfMib.bPhysAddr,
386 Adapter->IfMib.dwPhysAddrLen);
387 Adapter->DhclientInfo.hw_address.hlen = Adapter->IfMib.dwPhysAddrLen;
388
389 /* I'm not sure where else to set this, but
390 some DHCP servers won't take a zero.
391 We checked the hardware type earlier in
392 the if statement. */
393 Adapter->DhclientInfo.hw_address.htype = HTYPE_ETHER;
394
395 if( DhcpSocket == INVALID_SOCKET ) {
396 DhcpSocket =
397 Adapter->DhclientInfo.rfdesc =
398 Adapter->DhclientInfo.wfdesc =
400
401 if (DhcpSocket != INVALID_SOCKET) {
402
403 /* Allow broadcast on this socket */
404 Broadcast = 1;
408 (const char *)&Broadcast,
409 sizeof(Broadcast));
410
411 Adapter->ListenAddr.sin_family = AF_INET;
412 Adapter->ListenAddr.sin_port = htons(LOCAL_PORT);
413 Adapter->BindStatus =
414 (bind( Adapter->DhclientInfo.rfdesc,
415 (struct sockaddr *)&Adapter->ListenAddr,
416 sizeof(Adapter->ListenAddr) ) == 0) ?
417 0 : WSAGetLastError();
418 } else {
419 error("socket() failed: %d\n", WSAGetLastError());
420 }
421 } else {
422 Adapter->DhclientInfo.rfdesc =
423 Adapter->DhclientInfo.wfdesc = DhcpSocket;
424 }
425
426 Adapter->DhclientConfig.timeout = DHCP_PANIC_TIMEOUT;
427 Adapter->DhclientConfig.initial_interval = DHCP_DISCOVER_INTERVAL;
428 Adapter->DhclientConfig.retry_interval = DHCP_DISCOVER_INTERVAL;
429 Adapter->DhclientConfig.select_interval = 1;
430 Adapter->DhclientConfig.reboot_timeout = DHCP_REBOOT_TIMEOUT;
431 Adapter->DhclientConfig.backoff_cutoff = DHCP_BACKOFF_MAX;
432 Adapter->DhclientState.interval =
433 Adapter->DhclientConfig.retry_interval;
434
435 if( PrepareAdapterForService( Adapter ) ) {
436 Adapter->DhclientInfo.next = ifi;
437 ifi = &Adapter->DhclientInfo;
438
440
441 if (Adapter->DhclientInfo.client->state == S_INIT)
442 {
443 add_protocol(Adapter->DhclientInfo.name,
444 Adapter->DhclientInfo.rfdesc,
445 got_one, &Adapter->DhclientInfo);
446
447 state_init(&Adapter->DhclientInfo);
448 }
449
450 ApiLock();
451 InsertTailList( &AdapterList, &Adapter->ListEntry );
452 AdapterCount++;
454 ApiUnlock();
455 } else { free( Adapter ); Adapter = 0; }
456 } else { free( Adapter ); Adapter = 0; }
457
458 if( !Adapter )
459 DH_DbgPrint(MID_TRACE,("Adapter %d was rejected\n",
460 Table->table[i].dwIndex));
461 }
462#if 0
464 if (Error != NO_ERROR)
465 break;
466#else
468 {
469 DPRINT("Stopping the discovery thread!\n");
470 break;
471 }
472#endif
473 }
474
475 if (Table)
476 free(Table);
477
478 DPRINT("Adapter discovery thread terminated! (Error: %d)\n", Error);
479
480 return Error;
481}
HANDLE hAdapterStateChangedEvent
Definition: dhcpcsvc.c:20
SOCKET DhcpSocket
Definition: adapter.c:8
int InterfaceConnected(const MIB_IFROW *IfEntry)
Definition: adapter.c:199
PDHCP_ADAPTER AdapterFindByHardwareAddress(u_int8_t haddr[16], u_int8_t hlen)
Definition: adapter.c:542
BOOL PrepareAdapterForService(PDHCP_ADAPTER Adapter)
Definition: adapter.c:153
static LIST_ENTRY AdapterList
Definition: adapter.c:9
BOOL IsReconnectHackNeeded(PDHCP_ADAPTER Adapter, const MIB_IFROW *IfEntry)
Definition: adapter.c:210
VOID ApiLock()
Definition: api.c:22
VOID ApiUnlock()
Definition: api.c:26
void add_protocol(char *name, int fd, void(*handler)(struct protocol *), void *local)
Definition: dispatch.c:342
void remove_protocol(struct protocol *proto)
Definition: dispatch.c:359
void got_one(struct protocol *l)
Definition: dispatch.c:194
struct protocol * find_protocol_by_adapter(struct interface_info *info)
Definition: dispatch.c:412
int read_client_conf(struct interface_info *ifi)
Definition: util.c:106
#define MID_TRACE
Definition: debug.h:15
#define DH_DbgPrint(_t_, _x_)
Definition: debug.h:49
#define HTYPE_ETHER
Definition: dhcp.h:85
BOOL Error
Definition: chkdsk.c:66
#define NO_ERROR
Definition: dderror.h:5
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
void state_init(void *ipp)
Definition: dhclient.c:210
HANDLE hStopEvent
Definition: dhcpcsvc.c:19
@ S_INIT
Definition: dhcpd.h:172
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define IPPROTO_UDP
Definition: ip.h:197
#define AF_INET
Definition: tcpip.h:117
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
#define InsertTailList(ListHead, Entry)
unsigned long DWORD
Definition: ntddk_ex.h:95
ASMGENDATA Table[]
Definition: genincdata.c:61
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
struct _MIB_IFTABLE * PMIB_IFTABLE
struct _MIB_IFTABLE MIB_IFTABLE
DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
#define MIB_IF_TYPE_ETHERNET
Definition: ipifcons.h:223
#define IF_TYPE_IEEE80211
Definition: ipifcons.h:91
#define error(str)
Definition: mkdosfs.c:1605
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define htons(x)
Definition: module.h:215
#define DHCP_REBOOT_TIMEOUT
Definition: rosdhcp.h:27
#define DHCP_PANIC_TIMEOUT
Definition: rosdhcp.h:28
#define DHCP_DISCOVER_INTERVAL
Definition: rosdhcp.h:26
#define DHCP_BACKOFF_MAX
Definition: rosdhcp.h:29
#define calloc
Definition: rosglue.h:14
#define DPRINT
Definition: sndvol32.h:73
INT WSAAPI setsockopt(IN SOCKET s, IN INT level, IN INT optname, IN CONST CHAR FAR *optval, IN INT optlen)
Definition: sockctrl.c:421
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
ULONG NteContext
Definition: rosdhcp.h:70
MIB_IPFORWARDROW RouterMib
Definition: rosdhcp.h:67
unsigned char recv_buf[1]
Definition: rosdhcp.h:76
struct interface_info DhclientInfo
Definition: rosdhcp.h:71
struct client_state DhclientState
Definition: rosdhcp.h:72
struct client_config DhclientConfig
Definition: rosdhcp.h:73
LIST_ENTRY ListEntry
Definition: rosdhcp.h:65
struct sockaddr_in ListenAddr
Definition: rosdhcp.h:74
unsigned int BindStatus
Definition: rosdhcp.h:75
MIB_IFROW IfMib
Definition: rosdhcp.h:66
UCHAR bPhysAddr[MAXLEN_PHYSADDR]
Definition: ifmib.h:43
DWORD dwPhysAddrLen
Definition: ifmib.h:42
DWORD dwForwardNextHop
Definition: ipmib.h:74
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
PVOID HANDLE
Definition: typedefs.h:73
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
#define WAIT_OBJECT_0
Definition: winbase.h:439
int PASCAL FAR WSAGetLastError(void)
Definition: dllmain.c:131
#define INVALID_SOCKET
Definition: winsock.h:332
#define SOCK_DGRAM
Definition: winsock.h:336
#define SOL_SOCKET
Definition: winsock.h:398
#define SO_BROADCAST
Definition: winsock.h:183

Referenced by StartAdapterDiscovery().

◆ AdapterFindByHardwareAddress()

PDHCP_ADAPTER AdapterFindByHardwareAddress ( u_int8_t  haddr[16],
u_int8_t  hlen 
)

Definition at line 542 of file adapter.c.

542 {
543 PDHCP_ADAPTER Adapter;
544 PLIST_ENTRY ListEntry;
545
546 for(ListEntry = AdapterList.Flink;
547 ListEntry != &AdapterList;
548 ListEntry = ListEntry->Flink) {
549 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
550 if (Adapter->DhclientInfo.hw_address.hlen == hlen &&
551 !memcmp(Adapter->DhclientInfo.hw_address.haddr,
552 haddr,
553 hlen)) return Adapter;
554 }
555
556 return NULL;
557}
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260

Referenced by AdapterDiscoveryThread(), and got_one().

◆ AdapterFindIndex()

PDHCP_ADAPTER AdapterFindIndex ( unsigned int  indx)

Definition at line 500 of file adapter.c.

500 {
501 PDHCP_ADAPTER Adapter;
502 PLIST_ENTRY ListEntry;
503
504 for( ListEntry = AdapterList.Flink;
505 ListEntry != &AdapterList;
506 ListEntry = ListEntry->Flink ) {
507 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
508 if( Adapter->IfMib.dwIndex == indx ) return Adapter;
509 }
510
511 return NULL;
512}
DWORD dwIndex
Definition: ifmib.h:38

Referenced by DSLeaseIpAddress(), DSQueryHWInfo(), DSReleaseIpAddressLease(), DSRenewIpAddressLease(), and DSStaticRefreshParams().

◆ AdapterFindInfo()

PDHCP_ADAPTER AdapterFindInfo ( struct interface_info ip)

Definition at line 528 of file adapter.c.

528 {
529 PDHCP_ADAPTER Adapter;
530 PLIST_ENTRY ListEntry;
531
532 for( ListEntry = AdapterList.Flink;
533 ListEntry != &AdapterList;
534 ListEntry = ListEntry->Flink ) {
535 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
536 if( ip == &Adapter->DhclientInfo ) return Adapter;
537 }
538
539 return NULL;
540}
Definition: dhcpd.h:62

Referenced by bind_lease(), got_one(), send_request(), and state_panic().

◆ AdapterFindName()

PDHCP_ADAPTER AdapterFindName ( const CHAR name)

Definition at line 514 of file adapter.c.

514 {
515 PDHCP_ADAPTER Adapter;
516 PLIST_ENTRY ListEntry;
517
518 for( ListEntry = AdapterList.Flink;
519 ListEntry != &AdapterList;
520 ListEntry = ListEntry->Flink ) {
521 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
522 if( !stricmp((const CHAR*)Adapter->IfMib.bDescr, name ) ) return Adapter;
523 }
524
525 return NULL;
526}
#define stricmp(_String1, _String2)
Definition: compat.h:24
BYTE bDescr[MAXLEN_IFDESCR]
Definition: ifmib.h:60
Definition: name.c:39
char CHAR
Definition: xmlstorage.h:175

Referenced by DSAcquireParams(), and DSReleaseParams().

◆ AdapterGetFirst()

PDHCP_ADAPTER AdapterGetFirst ( )

Definition at line 559 of file adapter.c.

559 {
560 if( IsListEmpty( &AdapterList ) ) return NULL; else {
561 return CONTAINING_RECORD
562 ( AdapterList.Flink, DHCP_ADAPTER, ListEntry );
563 }
564}
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954

◆ AdapterGetNext()

PDHCP_ADAPTER AdapterGetNext ( PDHCP_ADAPTER  This)

Definition at line 566 of file adapter.c.

567{
568 if( This->ListEntry.Flink == &AdapterList ) return NULL;
569 return CONTAINING_RECORD
570 ( This->ListEntry.Flink, DHCP_ADAPTER, ListEntry );
571}

◆ AdapterInit()

void AdapterInit ( )

Definition at line 192 of file adapter.c.

192 {
193 WSAStartup(0x0101,&wsd);
194
196}
static WSADATA wsd
Definition: adapter.c:10
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944

Referenced by init_client().

◆ AdapterStop()

void AdapterStop ( )

Definition at line 487 of file adapter.c.

487 {
488 PLIST_ENTRY ListEntry;
489 PDHCP_ADAPTER Adapter;
490 ApiLock();
491 while( !IsListEmpty( &AdapterList ) ) {
492 ListEntry = (PLIST_ENTRY)RemoveHeadList( &AdapterList );
493 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
494 free( Adapter );
495 }
496 ApiUnlock();
497 WSACleanup();
498}
#define RemoveHeadList(ListHead)
Definition: env_spec_w32.h:964
struct _LIST_ENTRY * PLIST_ENTRY
int PASCAL FAR WSACleanup(void)
Definition: startup.c:60

◆ FindAdapterKey()

HKEY FindAdapterKey ( PDHCP_ADAPTER  Adapter)

Definition at line 105 of file adapter.c.

105 {
106 int i = 0;
107 PCHAR EnumKeyName =
108 "SYSTEM\\CurrentControlSet\\Control\\Class\\"
109 "{4D36E972-E325-11CE-BFC1-08002BE10318}";
110 PCHAR TargetKeyNameStart =
111 "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
112 PCHAR TargetKeyName = NULL;
113 PCHAR *EnumKeysLinkage = GetSubkeyNames( EnumKeyName, "\\Linkage" );
114 PCHAR *EnumKeysTop = GetSubkeyNames( EnumKeyName, "" );
115 PCHAR RootDevice = NULL;
116 HKEY EnumKey, OutKey = NULL;
118
119 if( !EnumKeysLinkage || !EnumKeysTop ) goto cleanup;
120
121 Error = RegOpenKey( HKEY_LOCAL_MACHINE, EnumKeyName, &EnumKey );
122
123 if( Error ) goto cleanup;
124
125 for( i = 0; EnumKeysLinkage[i]; i++ ) {
126 RootDevice = RegReadString
127 ( EnumKey, EnumKeysLinkage[i], "RootDevice" );
128
129 if( RootDevice &&
130 !strcmp( RootDevice, Adapter->DhclientInfo.name ) ) {
131 TargetKeyName =
132 (CHAR*) malloc( strlen( TargetKeyNameStart ) +
133 strlen( RootDevice ) + 1);
134 if( !TargetKeyName ) goto cleanup;
135 sprintf( TargetKeyName, "%s%s",
136 TargetKeyNameStart, RootDevice );
137 Error = RegCreateKeyExA( HKEY_LOCAL_MACHINE, TargetKeyName, 0, NULL, 0, KEY_READ, NULL, &OutKey, NULL );
138 break;
139 } else {
140 free( RootDevice ); RootDevice = 0;
141 }
142 }
143
144cleanup:
145 if( RootDevice ) free( RootDevice );
146 if( EnumKeysLinkage ) free( EnumKeysLinkage );
147 if( EnumKeysTop ) free( EnumKeysTop );
148 if( TargetKeyName ) free( TargetKeyName );
149
150 return OutKey;
151}
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
PCHAR * GetSubkeyNames(PCHAR MainKeyName, PCHAR Append)
Definition: adapter.c:12
PCHAR RegReadString(HKEY Root, PCHAR Subkey, PCHAR Value)
Definition: adapter.c:61
#define ERROR_SUCCESS
Definition: deptool.c:10
LONG WINAPI RegCreateKeyExA(_In_ HKEY hKey, _In_ LPCSTR lpSubKey, _In_ DWORD Reserved, _In_ LPSTR lpClass, _In_ DWORD dwOptions, _In_ REGSAM samDesired, _In_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _Out_ PHKEY phkResult, _Out_ LPDWORD lpdwDisposition)
Definition: reg.c:1034
static void cleanup(void)
Definition: main.c:1335
static LONG WINAPI EnumKey(HANDLE hcKey, DWORD dwIndex, LPWSTR pszName, PDWORD pcchName, PFILETIME pftLastWriteTime, HANDLE hSpooler)
Definition: localmon.c:169
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define KEY_READ
Definition: nt_native.h:1023
char * PCHAR
Definition: typedefs.h:51
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define RegOpenKey
Definition: winreg.h:519

Referenced by PrepareAdapterForService().

◆ GetSubkeyNames()

PCHAR * GetSubkeyNames ( PCHAR  MainKeyName,
PCHAR  Append 
)

Definition at line 12 of file adapter.c.

12 {
13 int i = 0;
15 HKEY MainKey;
16 PCHAR *Out, OutKeyName;
17 SIZE_T CharTotal = 0, AppendLen = 1 + strlen(Append);
18 DWORD MaxSubKeyLen = 0, MaxSubKeys = 0;
19
20 Error = RegOpenKey( HKEY_LOCAL_MACHINE, MainKeyName, &MainKey );
21
22 if( Error ) return NULL;
23
25 ( MainKey,
26 NULL, NULL, NULL,
27 &MaxSubKeys, &MaxSubKeyLen,
28 NULL, NULL, NULL, NULL, NULL, NULL );
29
30 MaxSubKeyLen++;
31 DH_DbgPrint(MID_TRACE,("MaxSubKeys: %d, MaxSubKeyLen %d\n",
32 MaxSubKeys, MaxSubKeyLen));
33
34 CharTotal = (sizeof(PCHAR) + MaxSubKeyLen + AppendLen) * (MaxSubKeys + 1);
35
36 DH_DbgPrint(MID_TRACE,("AppendLen: %d, CharTotal: %d\n",
37 AppendLen, CharTotal));
38
39 Out = (CHAR**) malloc( CharTotal );
40 OutKeyName = ((PCHAR)&Out[MaxSubKeys+1]);
41
42 if( !Out ) { RegCloseKey( MainKey ); return NULL; }
43
44 i = 0;
45 do {
46 Out[i] = OutKeyName;
47 Error = RegEnumKey( MainKey, i, OutKeyName, MaxSubKeyLen );
48 if( !Error ) {
49 strcat( OutKeyName, Append );
50 DH_DbgPrint(MID_TRACE,("[%d]: %s\n", i, OutKeyName));
51 OutKeyName += strlen(OutKeyName) + 1;
52 i++;
53 } else Out[i] = 0;
54 } while( Error == ERROR_SUCCESS );
55
56 RegCloseKey( MainKey );
57
58 return Out;
59}
#define RegCloseKey(hKey)
Definition: registry.h:49
@ Out
#define PCHAR
Definition: match.c:90
strcat
Definition: string.h:92
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RegEnumKey
Definition: winreg.h:509
#define RegQueryInfoKey
Definition: winreg.h:521

Referenced by FindAdapterKey().

◆ if_register_receive()

void if_register_receive ( struct interface_info ip)

Definition at line 577 of file adapter.c.

577 {
578}

◆ if_register_send()

void if_register_send ( struct interface_info ip)

Definition at line 573 of file adapter.c.

573 {
574
575}

◆ InterfaceConnected()

int InterfaceConnected ( const MIB_IFROW IfEntry)

Definition at line 199 of file adapter.c.

200{
201 if (IfEntry->dwOperStatus == IF_OPER_STATUS_CONNECTED ||
203 return 1;
204
205 DH_DbgPrint(MID_TRACE,("Interface %d is down\n", IfEntry->dwIndex));
206 return 0;
207}
@ IF_OPER_STATUS_CONNECTED
Definition: ipifcons.h:242
@ IF_OPER_STATUS_OPERATIONAL
Definition: ipifcons.h:243
INTERNAL_IF_OPER_STATUS dwOperStatus
Definition: ifmib.h:45

Referenced by AdapterDiscoveryThread().

◆ IsReconnectHackNeeded()

BOOL IsReconnectHackNeeded ( PDHCP_ADAPTER  Adapter,
const MIB_IFROW IfEntry 
)

Definition at line 210 of file adapter.c.

211{
212 struct protocol *proto;
213 PIP_ADAPTER_INFO AdapterInfo, Orig;
214 DWORD Size, Ret;
215 char *ZeroAddress = "0.0.0.0";
216
218
219 if (Adapter->DhclientInfo.client->state == S_BOUND && !proto)
220 return FALSE;
221
222 if (Adapter->DhclientInfo.client->state != S_BOUND &&
223 Adapter->DhclientInfo.client->state != S_STATIC)
224 return FALSE;
225
226 ApiUnlock();
227
228 Orig = AdapterInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO));
229 Size = sizeof(IP_ADAPTER_INFO);
230 if (!AdapterInfo)
231 {
232 ApiLock();
233 return FALSE;
234 }
235
236 Ret = GetAdaptersInfo(AdapterInfo, &Size);
237 if (Ret == ERROR_BUFFER_OVERFLOW)
238 {
239 HeapFree(GetProcessHeap(), 0, AdapterInfo);
240 AdapterInfo = HeapAlloc(GetProcessHeap(), 0, Size);
241 if (!AdapterInfo)
242 {
243 ApiLock();
244 return FALSE;
245 }
246
247 if (GetAdaptersInfo(AdapterInfo, &Size) != NO_ERROR)
248 {
249 ApiLock();
250 return FALSE;
251 }
252
253 Orig = AdapterInfo;
254 for (; AdapterInfo != NULL; AdapterInfo = AdapterInfo->Next)
255 {
256 if (AdapterInfo->Index == IfEntry->dwIndex)
257 break;
258 }
259
260 if (AdapterInfo == NULL)
261 {
262 HeapFree(GetProcessHeap(), 0, Orig);
263 ApiLock();
264 return FALSE;
265 }
266 }
267 else if (Ret != NO_ERROR)
268 {
269 HeapFree(GetProcessHeap(), 0, Orig);
270 ApiLock();
271 return FALSE;
272 }
273
274 if (!strcmp(AdapterInfo->IpAddressList.IpAddress.String, ZeroAddress))
275 {
276 HeapFree(GetProcessHeap(), 0, Orig);
277 ApiLock();
278 return TRUE;
279 }
280 else
281 {
282 HeapFree(GetProcessHeap(), 0, Orig);
283 ApiLock();
284 return FALSE;
285 }
286}
@ S_BOUND
Definition: dhcpd.h:175
@ S_STATIC
Definition: dhcpd.h:178
#define FALSE
Definition: types.h:117
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
struct _IP_ADAPTER_INFO IP_ADAPTER_INFO
char String[4 *4]
Definition: iptypes.h:42
IP_ADDR_STRING IpAddressList
Definition: iptypes.h:63
struct _IP_ADAPTER_INFO * Next
Definition: iptypes.h:53
IP_ADDRESS_STRING IpAddress
Definition: iptypes.h:47
#define ERROR_BUFFER_OVERFLOW
Definition: winerror.h:185

Referenced by AdapterDiscoveryThread().

◆ PrepareAdapterForService()

BOOL PrepareAdapterForService ( PDHCP_ADAPTER  Adapter)

Definition at line 153 of file adapter.c.

153 {
154 HKEY AdapterKey;
155 DWORD Error = ERROR_SUCCESS, DhcpEnabled, Length = sizeof(DWORD);
156
157 Adapter->DhclientState.config = &Adapter->DhclientConfig;
158 strncpy(Adapter->DhclientInfo.name, (char*)Adapter->IfMib.bDescr,
159 sizeof(Adapter->DhclientInfo.name));
160
161 AdapterKey = FindAdapterKey( Adapter );
162 if( AdapterKey )
163 {
164 Error = RegQueryValueEx(AdapterKey, "EnableDHCP", NULL, NULL, (LPBYTE)&DhcpEnabled, &Length);
165
166 if (Error != ERROR_SUCCESS || Length != sizeof(DWORD))
167 DhcpEnabled = 1;
168
169 CloseHandle(AdapterKey);
170 }
171 else
172 {
173 /* DHCP enabled by default */
174 DhcpEnabled = 1;
175 }
176
177 if( !DhcpEnabled ) {
178 /* Non-automatic case */
179 DbgPrint("DHCPCSVC: Adapter Name: [%s] (static)\n", Adapter->DhclientInfo.name);
180
181 Adapter->DhclientState.state = S_STATIC;
182 } else {
183 /* Automatic case */
184 DbgPrint("DHCPCSVC: Adapter Name: [%s] (dynamic)\n", Adapter->DhclientInfo.name);
185
186 Adapter->DhclientInfo.client->state = S_INIT;
187 }
188
189 return TRUE;
190}
HKEY FindAdapterKey(PDHCP_ADAPTER Adapter)
Definition: adapter.c:105
#define CloseHandle
Definition: compat.h:739
#define DbgPrint
Definition: hal.h:12
#define DWORD
Definition: nt_native.h:44
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
strncpy
Definition: string.h:335
unsigned char * LPBYTE
Definition: typedefs.h:53
#define RegQueryValueEx
Definition: winreg.h:524

Referenced by AdapterDiscoveryThread().

◆ RegReadString()

PCHAR RegReadString ( HKEY  Root,
PCHAR  Subkey,
PCHAR  Value 
)

Definition at line 61 of file adapter.c.

61 {
62 PCHAR SubOut = NULL;
63 DWORD SubOutLen = 0, Error = 0;
64 HKEY ValueKey = NULL;
65
66 DH_DbgPrint(MID_TRACE,("Looking in %x:%s:%s\n", Root, Subkey, Value ));
67
68 if( Subkey && strlen(Subkey) ) {
69 if( RegOpenKey( Root, Subkey, &ValueKey ) != ERROR_SUCCESS )
70 goto regerror;
71 } else ValueKey = Root;
72
73 DH_DbgPrint(MID_TRACE,("Got Key %x\n", ValueKey));
74
75 if( (Error = RegQueryValueEx( ValueKey, Value, NULL, NULL,
76 (LPBYTE)SubOut, &SubOutLen )) != ERROR_SUCCESS )
77 goto regerror;
78
79 DH_DbgPrint(MID_TRACE,("Value %s has size %d\n", Value, SubOutLen));
80
81 if( !(SubOut = (CHAR*) malloc(SubOutLen)) )
82 goto regerror;
83
84 if( (Error = RegQueryValueEx( ValueKey, Value, NULL, NULL,
85 (LPBYTE)SubOut, &SubOutLen )) != ERROR_SUCCESS )
86 goto regerror;
87
88 DH_DbgPrint(MID_TRACE,("Value %s is %s\n", Value, SubOut));
89
90 goto cleanup;
91
92regerror:
93 if( SubOut ) { free( SubOut ); SubOut = NULL; }
95 if( ValueKey && ValueKey != Root ) {
96 DH_DbgPrint(MID_TRACE,("Closing key %x\n", ValueKey));
97 RegCloseKey( ValueKey );
98 }
99
100 DH_DbgPrint(MID_TRACE,("Returning %x with error %d\n", SubOut, Error));
101
102 return SubOut;
103}
root entry for file system trees
Definition: entries.h:148
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413

Referenced by FindAdapterKey().

◆ StartAdapterDiscovery()

HANDLE StartAdapterDiscovery ( HANDLE  hStopEvent)

Definition at line 483 of file adapter.c.

483 {
485}
DWORD WINAPI AdapterDiscoveryThread(LPVOID Context)
Definition: adapter.c:291
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:137

Referenced by ServiceMain().

Variable Documentation

◆ AdapterList

◆ DhcpSocket

SOCKET DhcpSocket = INVALID_SOCKET

Definition at line 8 of file adapter.c.

Referenced by AdapterDiscoveryThread(), dispatch(), and ServiceMain().

◆ hAdapterStateChangedEvent

HANDLE hAdapterStateChangedEvent
extern

Definition at line 20 of file dhcpcsvc.c.

Referenced by AdapterDiscoveryThread().

◆ wsd

WSADATA wsd
static

Definition at line 10 of file adapter.c.

Referenced by AdapterInit(), openlog(), and wmain().