- Merge 54895, 54896, 54899, 54912, 54913, 54915, and 54916 from wlan-bringup
[reactos.git] / reactos / dll / win32 / dhcpcsvc / dhcp / adapter.c
1 #include "rosdhcp.h"
2
3 SOCKET DhcpSocket = INVALID_SOCKET;
4 static LIST_ENTRY AdapterList;
5 static WSADATA wsd;
6
7 PCHAR *GetSubkeyNames( PCHAR MainKeyName, PCHAR Append ) {
8 int i = 0;
9 DWORD Error;
10 HKEY MainKey;
11 PCHAR *Out, OutKeyName;
12 DWORD CharTotal = 0, AppendLen = 1 + strlen(Append);
13 DWORD MaxSubKeyLen = 0, MaxSubKeys = 0;
14
15 Error = RegOpenKey( HKEY_LOCAL_MACHINE, MainKeyName, &MainKey );
16
17 if( Error ) return NULL;
18
19 Error = RegQueryInfoKey
20 ( MainKey,
21 NULL, NULL, NULL,
22 &MaxSubKeys, &MaxSubKeyLen,
23 NULL, NULL, NULL, NULL, NULL, NULL );
24
25 DH_DbgPrint(MID_TRACE,("MaxSubKeys: %d, MaxSubKeyLen %d\n",
26 MaxSubKeys, MaxSubKeyLen));
27
28 CharTotal = (sizeof(PCHAR) + MaxSubKeyLen + AppendLen) * (MaxSubKeys + 1);
29
30 DH_DbgPrint(MID_TRACE,("AppendLen: %d, CharTotal: %d\n",
31 AppendLen, CharTotal));
32
33 Out = (CHAR**) malloc( CharTotal );
34 OutKeyName = ((PCHAR)&Out[MaxSubKeys+1]);
35
36 if( !Out ) { RegCloseKey( MainKey ); return NULL; }
37
38 i = 0;
39 do {
40 Out[i] = OutKeyName;
41 Error = RegEnumKey( MainKey, i, OutKeyName, MaxSubKeyLen );
42 if( !Error ) {
43 strcat( OutKeyName, Append );
44 DH_DbgPrint(MID_TRACE,("[%d]: %s\n", i, OutKeyName));
45 OutKeyName += strlen(OutKeyName) + 1;
46 i++;
47 } else Out[i] = 0;
48 } while( Error == ERROR_SUCCESS );
49
50 RegCloseKey( MainKey );
51
52 return Out;
53 }
54
55 PCHAR RegReadString( HKEY Root, PCHAR Subkey, PCHAR Value ) {
56 PCHAR SubOut = NULL;
57 DWORD SubOutLen = 0, Error = 0;
58 HKEY ValueKey = NULL;
59
60 DH_DbgPrint(MID_TRACE,("Looking in %x:%s:%s\n", Root, Subkey, Value ));
61
62 if( Subkey && strlen(Subkey) ) {
63 if( RegOpenKey( Root, Subkey, &ValueKey ) != ERROR_SUCCESS )
64 goto regerror;
65 } else ValueKey = Root;
66
67 DH_DbgPrint(MID_TRACE,("Got Key %x\n", ValueKey));
68
69 if( (Error = RegQueryValueEx( ValueKey, Value, NULL, NULL,
70 (LPBYTE)SubOut, &SubOutLen )) != ERROR_SUCCESS )
71 goto regerror;
72
73 DH_DbgPrint(MID_TRACE,("Value %s has size %d\n", Value, SubOutLen));
74
75 if( !(SubOut = (CHAR*) malloc(SubOutLen)) )
76 goto regerror;
77
78 if( (Error = RegQueryValueEx( ValueKey, Value, NULL, NULL,
79 (LPBYTE)SubOut, &SubOutLen )) != ERROR_SUCCESS )
80 goto regerror;
81
82 DH_DbgPrint(MID_TRACE,("Value %s is %s\n", Value, SubOut));
83
84 goto cleanup;
85
86 regerror:
87 if( SubOut ) { free( SubOut ); SubOut = NULL; }
88 cleanup:
89 if( ValueKey && ValueKey != Root ) {
90 DH_DbgPrint(MID_TRACE,("Closing key %x\n", ValueKey));
91 RegCloseKey( ValueKey );
92 }
93
94 DH_DbgPrint(MID_TRACE,("Returning %x with error %d\n", SubOut, Error));
95
96 return SubOut;
97 }
98
99 HKEY FindAdapterKey( PDHCP_ADAPTER Adapter ) {
100 int i = 0;
101 PCHAR EnumKeyName =
102 "SYSTEM\\CurrentControlSet\\Control\\Class\\"
103 "{4D36E972-E325-11CE-BFC1-08002BE10318}";
104 PCHAR TargetKeyNameStart =
105 "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
106 PCHAR TargetKeyName = NULL;
107 PCHAR *EnumKeysLinkage = GetSubkeyNames( EnumKeyName, "\\Linkage" );
108 PCHAR *EnumKeysTop = GetSubkeyNames( EnumKeyName, "" );
109 PCHAR RootDevice = NULL;
110 HKEY EnumKey, OutKey = NULL;
111 DWORD Error = ERROR_SUCCESS;
112
113 if( !EnumKeysLinkage || !EnumKeysTop ) goto cleanup;
114
115 Error = RegOpenKey( HKEY_LOCAL_MACHINE, EnumKeyName, &EnumKey );
116
117 if( Error ) goto cleanup;
118
119 for( i = 0; EnumKeysLinkage[i]; i++ ) {
120 RootDevice = RegReadString
121 ( EnumKey, EnumKeysLinkage[i], "RootDevice" );
122
123 if( RootDevice &&
124 !strcmp( RootDevice, Adapter->DhclientInfo.name ) ) {
125 TargetKeyName =
126 (CHAR*) malloc( strlen( TargetKeyNameStart ) +
127 strlen( RootDevice ) + 1);
128 if( !TargetKeyName ) goto cleanup;
129 sprintf( TargetKeyName, "%s%s",
130 TargetKeyNameStart, RootDevice );
131 Error = RegCreateKeyExA( HKEY_LOCAL_MACHINE, TargetKeyName, 0, NULL, 0, KEY_READ, NULL, &OutKey, NULL );
132 break;
133 } else {
134 free( RootDevice ); RootDevice = 0;
135 }
136 }
137
138 cleanup:
139 if( RootDevice ) free( RootDevice );
140 if( EnumKeysLinkage ) free( EnumKeysLinkage );
141 if( EnumKeysTop ) free( EnumKeysTop );
142 if( TargetKeyName ) free( TargetKeyName );
143
144 return OutKey;
145 }
146
147 BOOL PrepareAdapterForService( PDHCP_ADAPTER Adapter ) {
148 HKEY AdapterKey;
149 DWORD Error = ERROR_SUCCESS, DhcpEnabled, Length = sizeof(DWORD);
150
151 Adapter->DhclientState.config = &Adapter->DhclientConfig;
152 strncpy(Adapter->DhclientInfo.name, (char*)Adapter->IfMib.bDescr,
153 sizeof(Adapter->DhclientInfo.name));
154
155 AdapterKey = FindAdapterKey( Adapter );
156 if( AdapterKey )
157 {
158 Error = RegQueryValueEx(AdapterKey, "EnableDHCP", NULL, NULL, (LPBYTE)&DhcpEnabled, &Length);
159
160 if (Error != ERROR_SUCCESS || Length != sizeof(DWORD))
161 DhcpEnabled = 1;
162
163 CloseHandle(AdapterKey);
164 }
165 else
166 {
167 /* DHCP enabled by default */
168 DhcpEnabled = 1;
169 }
170
171 if( !DhcpEnabled ) {
172 /* Non-automatic case */
173 DbgPrint("DHCPCSVC: Adapter Name: [%s] (static)\n", Adapter->DhclientInfo.name);
174
175 Adapter->DhclientState.state = S_STATIC;
176 } else {
177 /* Automatic case */
178 DbgPrint("DHCPCSVC: Adapter Name: [%s] (dynamic)\n", Adapter->DhclientInfo.name);
179
180 Adapter->DhclientInfo.client->state = S_INIT;
181 }
182
183 return TRUE;
184 }
185
186 void AdapterInit() {
187 WSAStartup(0x0101,&wsd);
188
189 InitializeListHead( &AdapterList );
190 }
191
192 int
193 InterfaceConnected(const MIB_IFROW* IfEntry)
194 {
195 if (IfEntry->dwOperStatus == IF_OPER_STATUS_CONNECTED ||
196 IfEntry->dwOperStatus == IF_OPER_STATUS_OPERATIONAL)
197 return 1;
198
199 DH_DbgPrint(MID_TRACE,("Interface %d is down\n", IfEntry->dwIndex));
200 return 0;
201 }
202
203 /*
204 * XXX Figure out the way to bind a specific adapter to a socket.
205 */
206 DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
207 PMIB_IFTABLE Table = (PMIB_IFTABLE) malloc(sizeof(MIB_IFTABLE));
208 DWORD Error, Size = sizeof(MIB_IFTABLE);
209 PDHCP_ADAPTER Adapter = NULL;
210 HANDLE AdapterStateChangedEvent = (HANDLE)Context;
211 struct interface_info *ifi = NULL;
212 struct protocol *proto;
213 int i, AdapterCount = 0, Broadcast;
214
215 /* FIXME: Kill this thread when the service is stopped */
216
217 do {
218 DH_DbgPrint(MID_TRACE,("Getting Adapter List...\n"));
219
220 while( (Error = GetIfTable(Table, &Size, 0 )) ==
221 ERROR_INSUFFICIENT_BUFFER ) {
222 DH_DbgPrint(MID_TRACE,("Error %d, New Buffer Size: %d\n", Error, Size));
223 free( Table );
224 Table = (PMIB_IFTABLE) malloc( Size );
225 }
226
227 if( Error != NO_ERROR )
228 {
229 /* HACK: We are waiting until TCP/IP starts */
230 Sleep(2000);
231 continue;
232 }
233
234 DH_DbgPrint(MID_TRACE,("Got Adapter List (%d entries)\n", Table->dwNumEntries));
235
236 for( i = Table->dwNumEntries - 1; i >= 0; i-- ) {
237 DH_DbgPrint(MID_TRACE,("Getting adapter %d attributes\n",
238 Table->table[i].dwIndex));
239
240 ApiLock();
241
242 if ((Adapter = AdapterFindByHardwareAddress(Table->table[i].bPhysAddr, Table->table[i].dwPhysAddrLen)))
243 {
244 /* This is an existing adapter */
245 if (InterfaceConnected(&Table->table[i])) {
246 /* We're still active so we stay in the list */
247 ifi = &Adapter->DhclientInfo;
248 } else {
249 proto = find_protocol_by_adapter(&Adapter->DhclientInfo);
250 if (proto)
251 remove_protocol(proto);
252
253 /* We've lost our link so out we go */
254 RemoveEntryList(&Adapter->ListEntry);
255 free(Adapter);
256 }
257
258 ApiUnlock();
259
260 continue;
261 }
262
263 ApiUnlock();
264
265 Adapter = (DHCP_ADAPTER*) calloc( sizeof( DHCP_ADAPTER ) + Table->table[i].dwMtu, 1 );
266
267 if( Adapter && Table->table[i].dwType == MIB_IF_TYPE_ETHERNET && InterfaceConnected(&Table->table[i])) {
268 memcpy( &Adapter->IfMib, &Table->table[i],
269 sizeof(Adapter->IfMib) );
270 Adapter->DhclientInfo.client = &Adapter->DhclientState;
271 Adapter->DhclientInfo.rbuf = Adapter->recv_buf;
272 Adapter->DhclientInfo.rbuf_max = Table->table[i].dwMtu;
273 Adapter->DhclientInfo.rbuf_len =
274 Adapter->DhclientInfo.rbuf_offset = 0;
275 memcpy(Adapter->DhclientInfo.hw_address.haddr,
276 Adapter->IfMib.bPhysAddr,
277 Adapter->IfMib.dwPhysAddrLen);
278 Adapter->DhclientInfo.hw_address.hlen = Adapter->IfMib.dwPhysAddrLen;
279
280 /* I'm not sure where else to set this, but
281 some DHCP servers won't take a zero.
282 We checked the hardware type earlier in
283 the if statement. */
284 Adapter->DhclientInfo.hw_address.htype = HTYPE_ETHER;
285
286 if( DhcpSocket == INVALID_SOCKET ) {
287 DhcpSocket =
288 Adapter->DhclientInfo.rfdesc =
289 Adapter->DhclientInfo.wfdesc =
290 socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
291
292 if (DhcpSocket != INVALID_SOCKET) {
293
294 /* Allow broadcast on this socket */
295 Broadcast = 1;
296 setsockopt(DhcpSocket,
297 SOL_SOCKET,
298 SO_BROADCAST,
299 (const char *)&Broadcast,
300 sizeof(Broadcast));
301
302 Adapter->ListenAddr.sin_family = AF_INET;
303 Adapter->ListenAddr.sin_port = htons(LOCAL_PORT);
304 Adapter->BindStatus =
305 (bind( Adapter->DhclientInfo.rfdesc,
306 (struct sockaddr *)&Adapter->ListenAddr,
307 sizeof(Adapter->ListenAddr) ) == 0) ?
308 0 : WSAGetLastError();
309 } else {
310 error("socket() failed: %d\n", WSAGetLastError());
311 }
312 } else {
313 Adapter->DhclientInfo.rfdesc =
314 Adapter->DhclientInfo.wfdesc = DhcpSocket;
315 }
316
317 Adapter->DhclientConfig.timeout = DHCP_PANIC_TIMEOUT;
318 Adapter->DhclientConfig.initial_interval = DHCP_DISCOVER_INTERVAL;
319 Adapter->DhclientConfig.retry_interval = DHCP_DISCOVER_INTERVAL;
320 Adapter->DhclientConfig.select_interval = 1;
321 Adapter->DhclientConfig.reboot_timeout = DHCP_REBOOT_TIMEOUT;
322 Adapter->DhclientConfig.backoff_cutoff = DHCP_BACKOFF_MAX;
323 Adapter->DhclientState.interval =
324 Adapter->DhclientConfig.retry_interval;
325
326 if( PrepareAdapterForService( Adapter ) ) {
327 Adapter->DhclientInfo.next = ifi;
328 ifi = &Adapter->DhclientInfo;
329
330 read_client_conf(&Adapter->DhclientInfo);
331
332 if (Adapter->DhclientInfo.client->state == S_INIT)
333 {
334 add_protocol(Adapter->DhclientInfo.name,
335 Adapter->DhclientInfo.rfdesc,
336 got_one, &Adapter->DhclientInfo);
337
338 state_init(&Adapter->DhclientInfo);
339 }
340
341 ApiLock();
342 InsertTailList( &AdapterList, &Adapter->ListEntry );
343 AdapterCount++;
344 SetEvent(AdapterStateChangedEvent);
345 ApiUnlock();
346 } else { free( Adapter ); Adapter = 0; }
347 } else { free( Adapter ); Adapter = 0; }
348
349 if( !Adapter )
350 DH_DbgPrint(MID_TRACE,("Adapter %d was rejected\n",
351 Table->table[i].dwIndex));
352 }
353 #if 0
354 Error = NotifyAddrChange(NULL, NULL);
355 if (Error != NO_ERROR)
356 break;
357 #else
358 Sleep(3000);
359 #endif
360 } while (TRUE);
361
362 DbgPrint("DHCPCSVC: Adapter discovery thread is terminating! (Error: %d)\n", Error);
363
364 if( Table ) free( Table );
365 return Error;
366 }
367
368 HANDLE StartAdapterDiscovery(VOID) {
369 HANDLE ThreadHandle, EventHandle;
370
371 EventHandle = CreateEvent(NULL,
372 FALSE,
373 FALSE,
374 NULL);
375
376 ThreadHandle = CreateThread(NULL,
377 0,
378 AdapterDiscoveryThread,
379 (LPVOID)EventHandle,
380 0,
381 NULL);
382
383 if (ThreadHandle == NULL)
384 return NULL;
385
386 CloseHandle(ThreadHandle);
387
388 return EventHandle;
389 }
390
391 void AdapterStop() {
392 PLIST_ENTRY ListEntry;
393 PDHCP_ADAPTER Adapter;
394 ApiLock();
395 while( !IsListEmpty( &AdapterList ) ) {
396 ListEntry = (PLIST_ENTRY)RemoveHeadList( &AdapterList );
397 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
398 free( Adapter );
399 }
400 ApiUnlock();
401 WSACleanup();
402 }
403
404 PDHCP_ADAPTER AdapterFindIndex( unsigned int indx ) {
405 PDHCP_ADAPTER Adapter;
406 PLIST_ENTRY ListEntry;
407
408 for( ListEntry = AdapterList.Flink;
409 ListEntry != &AdapterList;
410 ListEntry = ListEntry->Flink ) {
411 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
412 if( Adapter->IfMib.dwIndex == indx ) return Adapter;
413 }
414
415 return NULL;
416 }
417
418 PDHCP_ADAPTER AdapterFindName( const WCHAR *name ) {
419 PDHCP_ADAPTER Adapter;
420 PLIST_ENTRY ListEntry;
421
422 for( ListEntry = AdapterList.Flink;
423 ListEntry != &AdapterList;
424 ListEntry = ListEntry->Flink ) {
425 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
426 if( !wcsicmp( Adapter->IfMib.wszName, name ) ) return Adapter;
427 }
428
429 return NULL;
430 }
431
432 PDHCP_ADAPTER AdapterFindInfo( struct interface_info *ip ) {
433 PDHCP_ADAPTER Adapter;
434 PLIST_ENTRY ListEntry;
435
436 for( ListEntry = AdapterList.Flink;
437 ListEntry != &AdapterList;
438 ListEntry = ListEntry->Flink ) {
439 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
440 if( ip == &Adapter->DhclientInfo ) return Adapter;
441 }
442
443 return NULL;
444 }
445
446 PDHCP_ADAPTER AdapterFindByHardwareAddress( u_int8_t haddr[16], u_int8_t hlen ) {
447 PDHCP_ADAPTER Adapter;
448 PLIST_ENTRY ListEntry;
449
450 for(ListEntry = AdapterList.Flink;
451 ListEntry != &AdapterList;
452 ListEntry = ListEntry->Flink) {
453 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
454 if (Adapter->DhclientInfo.hw_address.hlen == hlen &&
455 !memcmp(Adapter->DhclientInfo.hw_address.haddr,
456 haddr,
457 hlen)) return Adapter;
458 }
459
460 return NULL;
461 }
462
463 PDHCP_ADAPTER AdapterGetFirst() {
464 if( IsListEmpty( &AdapterList ) ) return NULL; else {
465 return CONTAINING_RECORD
466 ( AdapterList.Flink, DHCP_ADAPTER, ListEntry );
467 }
468 }
469
470 PDHCP_ADAPTER AdapterGetNext( PDHCP_ADAPTER This )
471 {
472 if( This->ListEntry.Flink == &AdapterList ) return NULL;
473 return CONTAINING_RECORD
474 ( This->ListEntry.Flink, DHCP_ADAPTER, ListEntry );
475 }
476
477 void if_register_send(struct interface_info *ip) {
478
479 }
480
481 void if_register_receive(struct interface_info *ip) {
482 }