[PCNET]
[reactos.git] / 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(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 int i, AdapterCount = 0, Broadcast;
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
289 /* Allow broadcast on this socket */
290 Broadcast = 1;
291 setsockopt(DhcpSocket,
292 SOL_SOCKET,
293 SO_BROADCAST,
294 (const char *)&Broadcast,
295 sizeof(Broadcast));
296
297 Adapter->ListenAddr.sin_family = AF_INET;
298 Adapter->ListenAddr.sin_port = htons(LOCAL_PORT);
299 Adapter->BindStatus =
300 (bind( Adapter->DhclientInfo.rfdesc,
301 (struct sockaddr *)&Adapter->ListenAddr,
302 sizeof(Adapter->ListenAddr) ) == 0) ?
303 0 : WSAGetLastError();
304 } else {
305 error("socket() failed: %d\n", WSAGetLastError());
306 }
307 } else {
308 Adapter->DhclientInfo.rfdesc =
309 Adapter->DhclientInfo.wfdesc = DhcpSocket;
310 }
311
312 Adapter->DhclientConfig.timeout = DHCP_PANIC_TIMEOUT;
313 Adapter->DhclientConfig.initial_interval = DHCP_DISCOVER_INTERVAL;
314 Adapter->DhclientConfig.retry_interval = DHCP_DISCOVER_INTERVAL;
315 Adapter->DhclientConfig.select_interval = 1;
316 Adapter->DhclientConfig.reboot_timeout = DHCP_REBOOT_TIMEOUT;
317 Adapter->DhclientConfig.backoff_cutoff = DHCP_BACKOFF_MAX;
318 Adapter->DhclientState.interval =
319 Adapter->DhclientConfig.retry_interval;
320
321 if( PrepareAdapterForService( Adapter ) ) {
322 Adapter->DhclientInfo.next = ifi;
323 ifi = &Adapter->DhclientInfo;
324
325 read_client_conf(&Adapter->DhclientInfo);
326
327 if (Adapter->DhclientInfo.client->state == S_INIT)
328 {
329 add_protocol(Adapter->DhclientInfo.name,
330 Adapter->DhclientInfo.rfdesc,
331 got_one, &Adapter->DhclientInfo);
332
333 state_init(&Adapter->DhclientInfo);
334 }
335
336 ApiLock();
337 InsertTailList( &AdapterList, &Adapter->ListEntry );
338 AdapterCount++;
339 SetEvent(AdapterStateChangedEvent);
340 ApiUnlock();
341 } else { free( Adapter ); Adapter = 0; }
342 } else { free( Adapter ); Adapter = 0; }
343
344 if( !Adapter )
345 DH_DbgPrint(MID_TRACE,("Adapter %d was rejected\n",
346 Table->table[i].dwIndex));
347 }
348 #if 0
349 Error = NotifyAddrChange(NULL, NULL);
350 if (Error != NO_ERROR)
351 break;
352 #else
353 Sleep(3000);
354 #endif
355 } while (TRUE);
356
357 DbgPrint("DHCPCSVC: Adapter discovery thread is terminating! (Error: %d)\n", Error);
358
359 if( Table ) free( Table );
360 return Error;
361 }
362
363 HANDLE StartAdapterDiscovery(VOID) {
364 HANDLE /* ThreadHandle, */ EventHandle;
365
366 EventHandle = CreateEvent(NULL,
367 FALSE,
368 FALSE,
369 NULL);
370
371 #if 0
372 ThreadHandle = CreateThread(NULL,
373 0,
374 AdapterDiscoveryThread,
375 (LPVOID)EventHandle,
376 0,
377 NULL);
378
379 if (ThreadHandle == NULL)
380 return NULL;
381
382 CloseHandle(ThreadHandle);
383 #else
384 AdapterDiscoveryThread((LPVOID)EventHandle);
385 #endif
386
387 return EventHandle;
388 }
389
390 void AdapterStop() {
391 PLIST_ENTRY ListEntry;
392 PDHCP_ADAPTER Adapter;
393 ApiLock();
394 while( !IsListEmpty( &AdapterList ) ) {
395 ListEntry = (PLIST_ENTRY)RemoveHeadList( &AdapterList );
396 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
397 free( Adapter );
398 }
399 ApiUnlock();
400 WSACleanup();
401 }
402
403 PDHCP_ADAPTER AdapterFindIndex( unsigned int indx ) {
404 PDHCP_ADAPTER Adapter;
405 PLIST_ENTRY ListEntry;
406
407 for( ListEntry = AdapterList.Flink;
408 ListEntry != &AdapterList;
409 ListEntry = ListEntry->Flink ) {
410 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
411 if( Adapter->IfMib.dwIndex == indx ) return Adapter;
412 }
413
414 return NULL;
415 }
416
417 PDHCP_ADAPTER AdapterFindName( const WCHAR *name ) {
418 PDHCP_ADAPTER Adapter;
419 PLIST_ENTRY ListEntry;
420
421 for( ListEntry = AdapterList.Flink;
422 ListEntry != &AdapterList;
423 ListEntry = ListEntry->Flink ) {
424 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
425 if( !wcsicmp( Adapter->IfMib.wszName, name ) ) return Adapter;
426 }
427
428 return NULL;
429 }
430
431 PDHCP_ADAPTER AdapterFindInfo( struct interface_info *ip ) {
432 PDHCP_ADAPTER Adapter;
433 PLIST_ENTRY ListEntry;
434
435 for( ListEntry = AdapterList.Flink;
436 ListEntry != &AdapterList;
437 ListEntry = ListEntry->Flink ) {
438 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
439 if( ip == &Adapter->DhclientInfo ) return Adapter;
440 }
441
442 return NULL;
443 }
444
445 PDHCP_ADAPTER AdapterFindByHardwareAddress( u_int8_t haddr[16], u_int8_t hlen ) {
446 PDHCP_ADAPTER Adapter;
447 PLIST_ENTRY ListEntry;
448
449 for(ListEntry = AdapterList.Flink;
450 ListEntry != &AdapterList;
451 ListEntry = ListEntry->Flink) {
452 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
453 if (Adapter->DhclientInfo.hw_address.hlen == hlen &&
454 !memcmp(Adapter->DhclientInfo.hw_address.haddr,
455 haddr,
456 hlen)) return Adapter;
457 }
458
459 return NULL;
460 }
461
462 PDHCP_ADAPTER AdapterGetFirst() {
463 if( IsListEmpty( &AdapterList ) ) return NULL; else {
464 return CONTAINING_RECORD
465 ( AdapterList.Flink, DHCP_ADAPTER, ListEntry );
466 }
467 }
468
469 PDHCP_ADAPTER AdapterGetNext( PDHCP_ADAPTER This )
470 {
471 if( This->ListEntry.Flink == &AdapterList ) return NULL;
472 return CONTAINING_RECORD
473 ( This->ListEntry.Flink, DHCP_ADAPTER, ListEntry );
474 }
475
476 void if_register_send(struct interface_info *ip) {
477
478 }
479
480 void if_register_receive(struct interface_info *ip) {
481 }