ReactOS 0.4.16-dev-2613-g9533ad7
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)
 
DWORD LoadAlternateConfiguration (PDHCP_ADAPTER Adapter, HKEY hAdapterKey)
 
BOOL PrepareAdapterForService (PDHCP_ADAPTER Adapter)
 
void AdapterInit ()
 
int InterfaceConnected (const MIB_IFROW *IfEntry)
 
BOOL IsReconnectHackNeeded (PDHCP_ADAPTER Adapter, const MIB_IFROW *IfEntry)
 
VOID FreeAdapter (PDHCP_ADAPTER Adapter)
 
DWORD WINAPI AdapterDiscoveryThread (LPVOID Context)
 
HANDLE StartAdapterDiscovery (HANDLE hStopEvent)
 
void AdapterStop ()
 
PDHCP_ADAPTER AdapterFindIndex (unsigned int indx)
 
PDHCP_ADAPTER AdapterFindName (const WCHAR *name)
 
PDHCP_ADAPTER AdapterFindInfo (struct interface_info *ip)
 
PDHCP_ADAPTER AdapterFindByHardwareAddress (u_int8_t *haddr, 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 399 of file adapter.c.

399 {
401 DWORD Error, Size = sizeof(MIB_IFTABLE);
402 PDHCP_ADAPTER Adapter = NULL;
404 struct interface_info *ifi = NULL;
405 struct protocol *proto;
406 int i, AdapterCount = 0, Broadcast;
407
408 while (TRUE)
409 {
410 DH_DbgPrint(MID_TRACE,("Getting Adapter List...\n"));
411
412 while( (Error = GetIfTable(Table, &Size, 0 )) ==
414 DH_DbgPrint(MID_TRACE,("Error %d, New Buffer Size: %d\n", Error, Size));
415 free( Table );
417 }
418
419 if( Error != NO_ERROR )
420 {
421 /* HACK: We are waiting until TCP/IP starts */
422 Sleep(2000);
423 continue;
424 }
425
426 DH_DbgPrint(MID_TRACE,("Got Adapter List (%d entries)\n", Table->dwNumEntries));
427
428 for( i = Table->dwNumEntries - 1; i >= 0; i-- ) {
429 DH_DbgPrint(MID_TRACE,("Getting adapter %d attributes\n",
430 Table->table[i].dwIndex));
431
432 ApiLock();
433
434 if ((Adapter = AdapterFindByHardwareAddress(Table->table[i].bPhysAddr, Table->table[i].dwPhysAddrLen)))
435 {
437
438 /* This is an existing adapter */
439 if (InterfaceConnected(&Table->table[i])) {
440 /* We're still active so we stay in the list */
441 ifi = &Adapter->DhclientInfo;
442
443 /* This is a hack because IP helper API sucks */
444 if (IsReconnectHackNeeded(Adapter, &Table->table[i]))
445 {
446 /* This handles a disconnect/reconnect */
447
448 if (proto)
450 Adapter->DhclientInfo.client->state = S_INIT;
451
452 /* These are already invalid since the media state change */
453 Adapter->RouterMib.dwForwardNextHop = 0;
454 Adapter->NteContext = 0;
455
456 add_protocol(Adapter->DhclientInfo.name,
457 Adapter->DhclientInfo.rfdesc,
458 got_one, &Adapter->DhclientInfo);
459 state_init(&Adapter->DhclientInfo);
460
462 }
463
464 } else {
465 if (proto)
467
468 /* We've lost our link so out we go */
469 RemoveEntryList(&Adapter->ListEntry);
470 free(Adapter);
471 }
472
473 ApiUnlock();
474
475 continue;
476 }
477
478 ApiUnlock();
479
480 Adapter = (DHCP_ADAPTER*) calloc( sizeof( DHCP_ADAPTER ) + Table->table[i].dwMtu, 1 );
481
482 if (Adapter &&
483 (Table->table[i].dwType == MIB_IF_TYPE_ETHERNET || Table->table[i].dwType == IF_TYPE_IEEE80211) &&
484 InterfaceConnected(&Table->table[i])) {
485 memcpy( &Adapter->IfMib, &Table->table[i],
486 sizeof(Adapter->IfMib) );
487 Adapter->DhclientInfo.client = &Adapter->DhclientState;
488 Adapter->DhclientInfo.rbuf = Adapter->recv_buf;
489 Adapter->DhclientInfo.rbuf_max = Table->table[i].dwMtu;
490 Adapter->DhclientInfo.rbuf_len =
491 Adapter->DhclientInfo.rbuf_offset = 0;
492 memcpy(Adapter->DhclientInfo.hw_address.haddr,
493 Adapter->IfMib.bPhysAddr,
494 Adapter->IfMib.dwPhysAddrLen);
495 Adapter->DhclientInfo.hw_address.hlen = Adapter->IfMib.dwPhysAddrLen;
496
497 /* I'm not sure where else to set this, but
498 some DHCP servers won't take a zero.
499 We checked the hardware type earlier in
500 the if statement. */
501 Adapter->DhclientInfo.hw_address.htype = HTYPE_ETHER;
502
503 if( DhcpSocket == INVALID_SOCKET ) {
504 DhcpSocket =
505 Adapter->DhclientInfo.rfdesc =
506 Adapter->DhclientInfo.wfdesc =
508
509 if (DhcpSocket != INVALID_SOCKET) {
510
511 /* Allow broadcast on this socket */
512 Broadcast = 1;
516 (const char *)&Broadcast,
517 sizeof(Broadcast));
518
519 Adapter->ListenAddr.sin_family = AF_INET;
520 Adapter->ListenAddr.sin_port = htons(LOCAL_PORT);
521 Adapter->BindStatus =
522 (bind( Adapter->DhclientInfo.rfdesc,
523 (struct sockaddr *)&Adapter->ListenAddr,
524 sizeof(Adapter->ListenAddr) ) == 0) ?
525 0 : WSAGetLastError();
526 } else {
527 error("socket() failed: %d\n", WSAGetLastError());
528 }
529 } else {
530 Adapter->DhclientInfo.rfdesc =
531 Adapter->DhclientInfo.wfdesc = DhcpSocket;
532 }
533
534 Adapter->DhclientConfig.timeout = DHCP_PANIC_TIMEOUT;
535 Adapter->DhclientConfig.initial_interval = DHCP_DISCOVER_INTERVAL;
536 Adapter->DhclientConfig.retry_interval = DHCP_DISCOVER_INTERVAL;
537 Adapter->DhclientConfig.select_interval = 1;
538 Adapter->DhclientConfig.reboot_timeout = DHCP_REBOOT_TIMEOUT;
539 Adapter->DhclientConfig.backoff_cutoff = DHCP_BACKOFF_MAX;
540 Adapter->DhclientState.interval =
541 Adapter->DhclientConfig.retry_interval;
542
543 if( PrepareAdapterForService( Adapter ) ) {
544 Adapter->DhclientInfo.next = ifi;
545 ifi = &Adapter->DhclientInfo;
546
548
549 if (Adapter->DhclientInfo.client->state == S_INIT)
550 {
551 add_protocol(Adapter->DhclientInfo.name,
552 Adapter->DhclientInfo.rfdesc,
553 got_one, &Adapter->DhclientInfo);
554
555 state_init(&Adapter->DhclientInfo);
556 }
557
558 ApiLock();
559 InsertTailList( &AdapterList, &Adapter->ListEntry );
560 AdapterCount++;
562 ApiUnlock();
563 } else { FreeAdapter( Adapter ); Adapter = 0; }
564 } else { FreeAdapter( Adapter ); Adapter = 0; }
565
566 if( !Adapter )
567 DH_DbgPrint(MID_TRACE,("Adapter %d was rejected\n",
568 Table->table[i].dwIndex));
569 }
570#if 0
572 if (Error != NO_ERROR)
573 break;
574#else
576 {
577 DPRINT("Stopping the discovery thread!\n");
578 break;
579 }
580#endif
581 }
582
583 if (Table)
584 free(Table);
585
586 DPRINT("Adapter discovery thread terminated! (Error: %d)\n", Error);
587
588 return Error;
589}
HANDLE hAdapterStateChangedEvent
Definition: dhcpcsvc.c:21
SOCKET DhcpSocket
Definition: adapter.c:8
PDHCP_ADAPTER AdapterFindByHardwareAddress(u_int8_t *haddr, u_int8_t hlen)
Definition: adapter.c:652
int InterfaceConnected(const MIB_IFROW *IfEntry)
Definition: adapter.c:296
BOOL PrepareAdapterForService(PDHCP_ADAPTER Adapter)
Definition: adapter.c:248
VOID FreeAdapter(PDHCP_ADAPTER Adapter)
Definition: adapter.c:386
static LIST_ENTRY AdapterList
Definition: adapter.c:9
BOOL IsReconnectHackNeeded(PDHCP_ADAPTER Adapter, const MIB_IFROW *IfEntry)
Definition: adapter.c:307
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:20
@ S_INIT
Definition: dhcpd.h:173
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define IPPROTO_UDP
Definition: ip.h:198
#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:31
#define DHCP_PANIC_TIMEOUT
Definition: rosdhcp.h:32
#define DHCP_DISCOVER_INTERVAL
Definition: rosdhcp.h:30
#define DHCP_BACKOFF_MAX
Definition: rosdhcp.h:33
#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
_In_ PVOID Context
Definition: storport.h:2269
ULONG NteContext
Definition: rosdhcp.h:85
MIB_IPFORWARDROW RouterMib
Definition: rosdhcp.h:82
unsigned char recv_buf[1]
Definition: rosdhcp.h:91
struct interface_info DhclientInfo
Definition: rosdhcp.h:86
struct client_state DhclientState
Definition: rosdhcp.h:87
struct client_config DhclientConfig
Definition: rosdhcp.h:88
LIST_ENTRY ListEntry
Definition: rosdhcp.h:79
struct sockaddr_in ListenAddr
Definition: rosdhcp.h:89
unsigned int BindStatus
Definition: rosdhcp.h:90
MIB_IFROW IfMib
Definition: rosdhcp.h:81
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:726
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:669
PVOID HANDLE
Definition: typedefs.h:73
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4539
#define WAIT_OBJECT_0
Definition: winbase.h:383
int PASCAL FAR WSAGetLastError(void)
Definition: dllmain.c:131
#define INVALID_SOCKET
Definition: winsock.h:326
#define SOCK_DGRAM
Definition: winsock.h:330
#define SOL_SOCKET
Definition: winsock.h:392
#define SO_BROADCAST
Definition: winsock.h:177

Referenced by StartAdapterDiscovery().

◆ AdapterFindByHardwareAddress()

PDHCP_ADAPTER AdapterFindByHardwareAddress ( u_int8_t haddr,
u_int8_t  hlen 
)

Definition at line 652 of file adapter.c.

652 {
653 PDHCP_ADAPTER Adapter;
654 PLIST_ENTRY ListEntry;
655
656 for(ListEntry = AdapterList.Flink;
657 ListEntry != &AdapterList;
658 ListEntry = ListEntry->Flink) {
659 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
660 if (Adapter->DhclientInfo.hw_address.hlen == hlen &&
661 !memcmp(Adapter->DhclientInfo.hw_address.haddr,
662 haddr,
663 hlen)) return Adapter;
664 }
665
666 return NULL;
667}
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2802
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 608 of file adapter.c.

608 {
609 PDHCP_ADAPTER Adapter;
610 PLIST_ENTRY ListEntry;
611
612 for( ListEntry = AdapterList.Flink;
613 ListEntry != &AdapterList;
614 ListEntry = ListEntry->Flink ) {
615 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
616 if( Adapter->IfMib.dwIndex == indx ) return Adapter;
617 }
618
619 return NULL;
620}
DWORD dwIndex
Definition: ifmib.h:38

Referenced by Server_StaticRefreshParams().

◆ AdapterFindInfo()

PDHCP_ADAPTER AdapterFindInfo ( struct interface_info ip)

Definition at line 638 of file adapter.c.

638 {
639 PDHCP_ADAPTER Adapter;
640 PLIST_ENTRY ListEntry;
641
642 for( ListEntry = AdapterList.Flink;
643 ListEntry != &AdapterList;
644 ListEntry = ListEntry->Flink ) {
645 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
646 if( ip == &Adapter->DhclientInfo ) return Adapter;
647 }
648
649 return NULL;
650}
Definition: dhcpd.h:62

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

◆ AdapterFindName()

PDHCP_ADAPTER AdapterFindName ( const WCHAR name)

Definition at line 622 of file adapter.c.

622 {
623 PDHCP_ADAPTER Adapter;
624 PLIST_ENTRY ListEntry;
625 WCHAR UnicodeName[45];
626
627 for( ListEntry = AdapterList.Flink;
628 ListEntry != &AdapterList;
629 ListEntry = ListEntry->Flink ) {
630 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
631 mbstowcs(UnicodeName, (const CHAR *)Adapter->IfMib.bDescr, strlen((const CHAR *)Adapter->IfMib.bDescr) + 1);
632 if( !wcsicmp(UnicodeName, name ) ) return Adapter;
633 }
634
635 return NULL;
636}
#define wcsicmp
Definition: compat.h:15
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1592
IN PDCB IN POEM_STRING IN PUNICODE_STRING UnicodeName
Definition: fatprocs.h:1306
short WCHAR
Definition: pedump.c:58
char CHAR
Definition: pedump.c:57
mbstowcs
Definition: stdlib.h:925
BYTE bDescr[MAXLEN_IFDESCR]
Definition: ifmib.h:60
Definition: name.c:39

Referenced by Server_AcquireParameters(), Server_EnableDhcp(), Server_FallbackRefreshParams(), and Server_ReleaseParameters().

◆ AdapterGetFirst()

PDHCP_ADAPTER AdapterGetFirst ( )

Definition at line 669 of file adapter.c.

669 {
670 if( IsListEmpty( &AdapterList ) ) return NULL; else {
671 return CONTAINING_RECORD
672 ( AdapterList.Flink, DHCP_ADAPTER, ListEntry );
673 }
674}
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954

◆ AdapterGetNext()

PDHCP_ADAPTER AdapterGetNext ( PDHCP_ADAPTER  This)

Definition at line 676 of file adapter.c.

677{
678 if( This->ListEntry.Flink == &AdapterList ) return NULL;
679 return CONTAINING_RECORD
680 ( This->ListEntry.Flink, DHCP_ADAPTER, ListEntry );
681}

◆ AdapterInit()

void AdapterInit ( )

Definition at line 289 of file adapter.c.

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

Referenced by init_client().

◆ AdapterStop()

void AdapterStop ( )

Definition at line 595 of file adapter.c.

595 {
596 PLIST_ENTRY ListEntry;
597 PDHCP_ADAPTER Adapter;
598 ApiLock();
599 while( !IsListEmpty( &AdapterList ) ) {
600 ListEntry = (PLIST_ENTRY)RemoveHeadList( &AdapterList );
601 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
602 FreeAdapter( Adapter );
603 }
604 ApiUnlock();
605 WSACleanup();
606}
#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}
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
_ACRTIMP int __cdecl strcmp(const char *, const char *)
Definition: string.c:3319
static LONG WINAPI EnumKey(HANDLE hcKey, DWORD dwIndex, LPWSTR pszName, PDWORD pcchName, PFILETIME pftLastWriteTime, HANDLE hSpooler)
Definition: localmon.c:169
#define sprintf
Definition: sprintf.c:45
#define KEY_READ
Definition: nt_native.h:1026
char * PCHAR
Definition: typedefs.h:51
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define RegOpenKey
Definition: winreg.h:551

Referenced by PrepareAdapterForService(), and Server_FallbackRefreshParams().

◆ FreeAdapter()

VOID FreeAdapter ( PDHCP_ADAPTER  Adapter)

Definition at line 386 of file adapter.c.

388{
389 if (Adapter == NULL)
390 return;
391 if (Adapter->AlternateConfiguration)
393 free(Adapter);
394}
PALTERNATE_CONFIGURATION AlternateConfiguration
Definition: rosdhcp.h:80

Referenced by AdapterDiscoveryThread(), and AdapterStop().

◆ 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:540
#define RegQueryInfoKey
Definition: winreg.h:553

Referenced by FindAdapterKey().

◆ if_register_receive()

void if_register_receive ( struct interface_info ip)

Definition at line 687 of file adapter.c.

687 {
688}

◆ if_register_send()

void if_register_send ( struct interface_info ip)

Definition at line 683 of file adapter.c.

683 {
684
685}

◆ InterfaceConnected()

int InterfaceConnected ( const MIB_IFROW IfEntry)

Definition at line 296 of file adapter.c.

297{
298 if (IfEntry->dwOperStatus == IF_OPER_STATUS_CONNECTED ||
300 return 1;
301
302 DH_DbgPrint(MID_TRACE,("Interface %d is down\n", IfEntry->dwIndex));
303 return 0;
304}
@ 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 307 of file adapter.c.

308{
309 struct protocol *proto;
310 PIP_ADAPTER_INFO AdapterInfo, Orig;
311 DWORD Size, Ret;
312 char *ZeroAddress = "0.0.0.0";
313
315
316 if (Adapter->DhclientInfo.client->state == S_BOUND && !proto)
317 return FALSE;
318
319 if (Adapter->DhclientInfo.client->state != S_BOUND &&
320 Adapter->DhclientInfo.client->state != S_STATIC)
321 return FALSE;
322
323 ApiUnlock();
324
325 Orig = AdapterInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO));
326 Size = sizeof(IP_ADAPTER_INFO);
327 if (!AdapterInfo)
328 {
329 ApiLock();
330 return FALSE;
331 }
332
333 Ret = GetAdaptersInfo(AdapterInfo, &Size);
334 if (Ret == ERROR_BUFFER_OVERFLOW)
335 {
336 HeapFree(GetProcessHeap(), 0, AdapterInfo);
337 AdapterInfo = HeapAlloc(GetProcessHeap(), 0, Size);
338 if (!AdapterInfo)
339 {
340 ApiLock();
341 return FALSE;
342 }
343
344 if (GetAdaptersInfo(AdapterInfo, &Size) != NO_ERROR)
345 {
346 ApiLock();
347 return FALSE;
348 }
349
350 Orig = AdapterInfo;
351 for (; AdapterInfo != NULL; AdapterInfo = AdapterInfo->Next)
352 {
353 if (AdapterInfo->Index == IfEntry->dwIndex)
354 break;
355 }
356
357 if (AdapterInfo == NULL)
358 {
359 HeapFree(GetProcessHeap(), 0, Orig);
360 ApiLock();
361 return FALSE;
362 }
363 }
364 else if (Ret != NO_ERROR)
365 {
366 HeapFree(GetProcessHeap(), 0, Orig);
367 ApiLock();
368 return FALSE;
369 }
370
371 if (!strcmp(AdapterInfo->IpAddressList.IpAddress.String, ZeroAddress))
372 {
373 HeapFree(GetProcessHeap(), 0, Orig);
374 ApiLock();
375 return TRUE;
376 }
377 else
378 {
379 HeapFree(GetProcessHeap(), 0, Orig);
380 ApiLock();
381 return FALSE;
382 }
383}
@ S_BOUND
Definition: dhcpd.h:176
@ S_STATIC
Definition: dhcpd.h:179
#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:31
struct _IP_ADAPTER_INFO * Next
Definition: iptypes.h:42
IP_ADDR_STRING IpAddressList
Definition: iptypes.h:52
IP_ADDRESS_STRING IpAddress
Definition: iptypes.h:36
#define ERROR_BUFFER_OVERFLOW
Definition: winerror.h:307

Referenced by AdapterDiscoveryThread().

◆ LoadAlternateConfiguration()

DWORD LoadAlternateConfiguration ( PDHCP_ADAPTER  Adapter,
HKEY  hAdapterKey 
)

Definition at line 154 of file adapter.c.

157{
158 PWSTR pszConfigName = NULL;
159 HKEY hConfigsKey = NULL, hConfigKey = NULL;
160 PALTERNATE_CONFIGURATION pAltConfig = NULL;
162 DWORD dwError;
163
164 DPRINT("LoadAlternateConfiguration()\n");
165
166 dwSize = 0;
167 RegQueryValueExW(hAdapterKey, L"ActiveConfigurations", NULL, NULL, NULL, &dwSize);
168 if (dwSize == 0)
169 {
170 DPRINT1("There is no active configuration!\n");
171 return ERROR_SUCCESS;
172 }
173
174 pszConfigName = malloc(dwSize);
175 if (pszConfigName == NULL)
176 {
177 DPRINT1("Failed to allocate memory!\n");
179 }
180
181 dwError = RegQueryValueExW(hAdapterKey, L"ActiveConfigurations", NULL, NULL, (PBYTE)pszConfigName, &dwSize);
182 if (dwError != ERROR_SUCCESS)
183 {
184 DPRINT1("Failed to read the ActiveConfigurations value!\n");
185 goto done;
186 }
187
188 DPRINT("ActiveConfigurations: %S\n", pszConfigName);
189
190 dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Services\\DHCP\\Configurations", 0, KEY_READ, &hConfigsKey);
191 if (dwError != ERROR_SUCCESS)
192 {
193 DPRINT1("Failed to open configurations key!\n");
194 goto done;
195 }
196
197 dwError = RegOpenKeyExW(hConfigsKey, pszConfigName, 0, KEY_READ, &hConfigKey);
198 if (dwError != ERROR_SUCCESS)
199 {
200 DPRINT1("Failed to open configuration key %S!\n", pszConfigName);
201 goto done;
202 }
203
204 dwSize = 0;
205 RegQueryValueExW(hConfigKey, L"Options", NULL, NULL, NULL, &dwSize);
206
207 DbgPrint("Found alternate configuration: Size %lu\n", dwSize);
208 pAltConfig = malloc(dwSize);
209 if (pAltConfig == NULL)
210 {
211 DPRINT1("Failed to allocate memory!\n");
212 dwError = ERROR_NOT_ENOUGH_MEMORY;
213 goto done;
214 }
215
216 dwError = RegQueryValueExW(hConfigKey, L"Options", NULL, NULL, (PBYTE)pAltConfig, &dwSize);
217 if (dwError != ERROR_SUCCESS)
218 {
219 DPRINT1("Failed to read the alternate configuration!\n");
220 goto done;
221 }
222
223 DPRINT("IpAddress: %08lx\n", pAltConfig->IpAddress);
224 DPRINT("SubnetMask: %08lx\n", pAltConfig->SubnetMask);
225
226 Adapter->AlternateConfiguration = pAltConfig;
227 DPRINT("Success!\n");
228
229done:
230 if (dwError != ERROR_SUCCESS)
231 {
232 if (pAltConfig)
233 free(pAltConfig);
234 }
235
236 if (hConfigKey)
237 RegCloseKey(hConfigKey);
238
239 if (hConfigsKey)
240 RegCloseKey(hConfigsKey);
241
242 if (pszConfigName)
243 free(pszConfigName);
244
245 return dwError;
246}
#define DPRINT1
Definition: precomp.h:8
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3333
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4103
#define L(x)
Definition: resources.c:13
#define DbgPrint
Definition: hal.h:12
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
BYTE * PBYTE
Definition: pedump.c:66
uint16_t * PWSTR
Definition: typedefs.h:56

Referenced by PrepareAdapterForService(), and Server_FallbackRefreshParams().

◆ PrepareAdapterForService()

BOOL PrepareAdapterForService ( PDHCP_ADAPTER  Adapter)

Definition at line 248 of file adapter.c.

248 {
249 HKEY AdapterKey;
250 DWORD Error = ERROR_SUCCESS, DhcpEnabled, Length = sizeof(DWORD);
251
252 Adapter->DhclientState.config = &Adapter->DhclientConfig;
253 strncpy(Adapter->DhclientInfo.name, (char*)Adapter->IfMib.bDescr,
254 sizeof(Adapter->DhclientInfo.name));
255
256 AdapterKey = FindAdapterKey( Adapter );
257 if( AdapterKey )
258 {
259 Error = RegQueryValueEx(AdapterKey, "EnableDHCP", NULL, NULL, (LPBYTE)&DhcpEnabled, &Length);
260
261 if (Error != ERROR_SUCCESS || Length != sizeof(DWORD))
262 DhcpEnabled = 1;
263
264 LoadAlternateConfiguration(Adapter, AdapterKey);
265
266 CloseHandle(AdapterKey);
267 }
268 else
269 {
270 /* DHCP enabled by default */
271 DhcpEnabled = 1;
272 }
273
274 if( !DhcpEnabled ) {
275 /* Non-automatic case */
276 DbgPrint("DHCPCSVC: Adapter Name: [%s] (static)\n", Adapter->DhclientInfo.name);
277
278 Adapter->DhclientState.state = S_STATIC;
279 } else {
280 /* Automatic case */
281 DbgPrint("DHCPCSVC: Adapter Name: [%s] (dynamic)\n", Adapter->DhclientInfo.name);
282
283 Adapter->DhclientInfo.client->state = S_INIT;
284 }
285
286 return TRUE;
287}
HKEY FindAdapterKey(PDHCP_ADAPTER Adapter)
Definition: adapter.c:105
DWORD LoadAlternateConfiguration(PDHCP_ADAPTER Adapter, HKEY hAdapterKey)
Definition: adapter.c:154
#define CloseHandle
Definition: compat.h:739
#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:556

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
Definition: cmtypes.h:261
_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 591 of file adapter.c.

591 {
593}
DWORD WINAPI AdapterDiscoveryThread(LPVOID Context)
Definition: adapter.c:399
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 21 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().