[DHCPCSVC]: Revert part of r48215
[reactos.git] / reactos / dll / win32 / dhcpcsvc / dhcp / adapter.c
1 #include "rosdhcp.h"
2
3 static 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(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 int i, AdapterCount = 0;
213
214 /* FIXME: Kill this thread when the service is stopped */
215
216 do {
217 DH_DbgPrint(MID_TRACE,("Getting Adapter List...\n"));
218
219 while( (Error = GetIfTable(Table, &Size, 0 )) ==
220 ERROR_INSUFFICIENT_BUFFER ) {
221 DH_DbgPrint(MID_TRACE,("Error %d, New Buffer Size: %d\n", Error, Size));
222 free( Table );
223 Table = (PMIB_IFTABLE) malloc( Size );
224 }
225
226 if( Error != NO_ERROR )
227 {
228 /* HACK: We are waiting until TCP/IP starts */
229 Sleep(2000);
230 continue;
231 }
232
233 DH_DbgPrint(MID_TRACE,("Got Adapter List (%d entries)\n", Table->dwNumEntries));
234
235 for( i = Table->dwNumEntries - 1; i >= 0; i-- ) {
236 DH_DbgPrint(MID_TRACE,("Getting adapter %d attributes\n",
237 Table->table[i].dwIndex));
238
239 ApiLock();
240
241 if ((Adapter = AdapterFindByHardwareAddress(Table->table[i].bPhysAddr, Table->table[i].dwPhysAddrLen)))
242 {
243 /* This is an existing adapter */
244 if (InterfaceConnected(Table->table[i])) {
245 /* We're still active so we stay in the list */
246 ifi = &Adapter->DhclientInfo;
247 } else {
248 /* We've lost our link so out we go */
249 RemoveEntryList(&Adapter->ListEntry);
250 free(Adapter);
251 }
252
253 ApiUnlock();
254
255 continue;
256 }
257
258 ApiUnlock();
259
260 Adapter = (DHCP_ADAPTER*) calloc( sizeof( DHCP_ADAPTER ) + Table->table[i].dwMtu, 1 );
261
262 if( Adapter && Table->table[i].dwType == MIB_IF_TYPE_ETHERNET && InterfaceConnected(Table->table[i])) {
263 memcpy( &Adapter->IfMib, &Table->table[i],
264 sizeof(Adapter->IfMib) );
265 Adapter->DhclientInfo.client = &Adapter->DhclientState;
266 Adapter->DhclientInfo.rbuf = Adapter->recv_buf;
267 Adapter->DhclientInfo.rbuf_max = Table->table[i].dwMtu;
268 Adapter->DhclientInfo.rbuf_len =
269 Adapter->DhclientInfo.rbuf_offset = 0;
270 memcpy(Adapter->DhclientInfo.hw_address.haddr,
271 Adapter->IfMib.bPhysAddr,
272 Adapter->IfMib.dwPhysAddrLen);
273 Adapter->DhclientInfo.hw_address.hlen = Adapter->IfMib.dwPhysAddrLen;
274
275 /* I'm not sure where else to set this, but
276 some DHCP servers won't take a zero.
277 We checked the hardware type earlier in
278 the if statement. */
279 Adapter->DhclientInfo.hw_address.htype = HTYPE_ETHER;
280
281 if( DhcpSocket == INVALID_SOCKET ) {
282 DhcpSocket =
283 Adapter->DhclientInfo.rfdesc =
284 Adapter->DhclientInfo.wfdesc =
285 socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
286
287 if (DhcpSocket != INVALID_SOCKET) {
288 Adapter->ListenAddr.sin_family = AF_INET;
289 Adapter->ListenAddr.sin_port = htons(LOCAL_PORT);
290 Adapter->BindStatus =
291 (bind( Adapter->DhclientInfo.rfdesc,
292 (struct sockaddr *)&Adapter->ListenAddr,
293 sizeof(Adapter->ListenAddr) ) == 0) ?
294 0 : WSAGetLastError();
295 } else {
296 error("socket() failed: %d\n", WSAGetLastError());
297 }
298 } else {
299 Adapter->DhclientInfo.rfdesc =
300 Adapter->DhclientInfo.wfdesc = DhcpSocket;
301 }
302
303 Adapter->DhclientConfig.timeout = DHCP_PANIC_TIMEOUT;
304 Adapter->DhclientConfig.initial_interval = DHCP_DISCOVER_INTERVAL;
305 Adapter->DhclientConfig.retry_interval = DHCP_DISCOVER_INTERVAL;
306 Adapter->DhclientConfig.select_interval = 1;
307 Adapter->DhclientConfig.reboot_timeout = DHCP_REBOOT_TIMEOUT;
308 Adapter->DhclientConfig.backoff_cutoff = DHCP_BACKOFF_MAX;
309 Adapter->DhclientState.interval =
310 Adapter->DhclientConfig.retry_interval;
311
312 if( PrepareAdapterForService( Adapter ) ) {
313 Adapter->DhclientInfo.next = ifi;
314 ifi = &Adapter->DhclientInfo;
315
316 read_client_conf(&Adapter->DhclientInfo);
317
318 if (Adapter->DhclientInfo.client->state == S_INIT)
319 {
320 add_protocol(Adapter->DhclientInfo.name,
321 Adapter->DhclientInfo.rfdesc,
322 got_one, &Adapter->DhclientInfo);
323
324 state_init(&Adapter->DhclientInfo);
325 }
326
327 ApiLock();
328 InsertTailList( &AdapterList, &Adapter->ListEntry );
329 AdapterCount++;
330 SetEvent(AdapterStateChangedEvent);
331 ApiUnlock();
332 } else { free( Adapter ); Adapter = 0; }
333 } else { free( Adapter ); Adapter = 0; }
334
335 if( !Adapter )
336 DH_DbgPrint(MID_TRACE,("Adapter %d was rejected\n",
337 Table->table[i].dwIndex));
338 }
339 Error = NotifyAddrChange(NULL, NULL);
340 #if 0
341 if (Error != NO_ERROR)
342 break;
343 #else
344 if (AdapterCount)
345 break;
346 else
347 Sleep(3000);
348 #endif
349 } while (TRUE);
350
351 DbgPrint("DHCPCSVC: Adapter discovery thread is terminating! (Error: %d)\n", Error);
352
353 if( Table ) free( Table );
354 return Error;
355 }
356
357 HANDLE StartAdapterDiscovery(VOID) {
358 HANDLE /* ThreadHandle, */ EventHandle;
359
360 EventHandle = CreateEvent(NULL,
361 FALSE,
362 FALSE,
363 NULL);
364
365 #if 0
366 ThreadHandle = CreateThread(NULL,
367 0,
368 AdapterDiscoveryThread,
369 (LPVOID)EventHandle,
370 0,
371 NULL);
372
373 if (ThreadHandle == NULL)
374 return NULL;
375
376 CloseHandle(ThreadHandle);
377 #else
378 AdapterDiscoveryThread((LPVOID)EventHandle);
379 #endif
380
381 return EventHandle;
382 }
383
384 void AdapterStop() {
385 PLIST_ENTRY ListEntry;
386 PDHCP_ADAPTER Adapter;
387 ApiLock();
388 while( !IsListEmpty( &AdapterList ) ) {
389 ListEntry = (PLIST_ENTRY)RemoveHeadList( &AdapterList );
390 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
391 free( Adapter );
392 }
393 ApiUnlock();
394 WSACleanup();
395 }
396
397 PDHCP_ADAPTER AdapterFindIndex( unsigned int indx ) {
398 PDHCP_ADAPTER Adapter;
399 PLIST_ENTRY ListEntry;
400
401 for( ListEntry = AdapterList.Flink;
402 ListEntry != &AdapterList;
403 ListEntry = ListEntry->Flink ) {
404 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
405 if( Adapter->IfMib.dwIndex == indx ) return Adapter;
406 }
407
408 return NULL;
409 }
410
411 PDHCP_ADAPTER AdapterFindName( const WCHAR *name ) {
412 PDHCP_ADAPTER Adapter;
413 PLIST_ENTRY ListEntry;
414
415 for( ListEntry = AdapterList.Flink;
416 ListEntry != &AdapterList;
417 ListEntry = ListEntry->Flink ) {
418 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
419 if( !wcsicmp( Adapter->IfMib.wszName, name ) ) return Adapter;
420 }
421
422 return NULL;
423 }
424
425 PDHCP_ADAPTER AdapterFindInfo( struct interface_info *ip ) {
426 PDHCP_ADAPTER Adapter;
427 PLIST_ENTRY ListEntry;
428
429 for( ListEntry = AdapterList.Flink;
430 ListEntry != &AdapterList;
431 ListEntry = ListEntry->Flink ) {
432 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
433 if( ip == &Adapter->DhclientInfo ) return Adapter;
434 }
435
436 return NULL;
437 }
438
439 PDHCP_ADAPTER AdapterFindByHardwareAddress( u_int8_t haddr[16], u_int8_t hlen ) {
440 PDHCP_ADAPTER Adapter;
441 PLIST_ENTRY ListEntry;
442
443 for(ListEntry = AdapterList.Flink;
444 ListEntry != &AdapterList;
445 ListEntry = ListEntry->Flink) {
446 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
447 if (Adapter->DhclientInfo.hw_address.hlen == hlen &&
448 !memcmp(Adapter->DhclientInfo.hw_address.haddr,
449 haddr,
450 hlen)) return Adapter;
451 }
452
453 return NULL;
454 }
455
456 PDHCP_ADAPTER AdapterGetFirst() {
457 if( IsListEmpty( &AdapterList ) ) return NULL; else {
458 return CONTAINING_RECORD
459 ( AdapterList.Flink, DHCP_ADAPTER, ListEntry );
460 }
461 }
462
463 PDHCP_ADAPTER AdapterGetNext( PDHCP_ADAPTER This )
464 {
465 if( This->ListEntry.Flink == &AdapterList ) return NULL;
466 return CONTAINING_RECORD
467 ( This->ListEntry.Flink, DHCP_ADAPTER, ListEntry );
468 }
469
470 void if_register_send(struct interface_info *ip) {
471
472 }
473
474 void if_register_receive(struct interface_info *ip) {
475 }