[DHCPCSVC]
[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 = NULL;
149 PCHAR IPAddress = NULL, Netmask = NULL, DefaultGateway = NULL;
150 NTSTATUS Status = STATUS_SUCCESS;
151 DWORD Error = ERROR_SUCCESS;
152
153 Adapter->DhclientState.config = &Adapter->DhclientConfig;
154 strncpy(Adapter->DhclientInfo.name, (char*)Adapter->IfMib.bDescr,
155 sizeof(Adapter->DhclientInfo.name));
156
157 AdapterKey = FindAdapterKey( Adapter );
158 if( AdapterKey )
159 IPAddress = RegReadString( AdapterKey, NULL, "IPAddress" );
160
161 if( IPAddress && strcmp( IPAddress, "0.0.0.0" ) ) {
162 /* Non-automatic case */
163 DH_DbgPrint
164 (MID_TRACE,("Adapter Name: [%s] (Bind Status %x) (static %s)\n",
165 Adapter->DhclientInfo.name,
166 Adapter->BindStatus,
167 IPAddress));
168
169 Adapter->DhclientState.state = S_STATIC;
170
171 Netmask = RegReadString( AdapterKey, NULL, "Subnetmask" );
172
173 Status = AddIPAddress( inet_addr( IPAddress ),
174 inet_addr( Netmask ? Netmask : "255.255.255.0" ),
175 Adapter->IfMib.dwIndex,
176 &Adapter->NteContext,
177 &Adapter->NteInstance );
178
179 DefaultGateway = RegReadString( AdapterKey, NULL, "DefaultGateway" );
180
181 if( DefaultGateway ) {
182 Adapter->RouterMib.dwForwardDest = 0;
183 Adapter->RouterMib.dwForwardMask = 0;
184 Adapter->RouterMib.dwForwardMetric1 = 1;
185 Adapter->RouterMib.dwForwardIfIndex = Adapter->IfMib.dwIndex;
186 Adapter->RouterMib.dwForwardNextHop = inet_addr(DefaultGateway);
187 Error = CreateIpForwardEntry( &Adapter->RouterMib );
188 if( Error )
189 warning("Failed to set default gateway %s: %ld\n",
190 DefaultGateway, Error);
191 }
192
193 if( DefaultGateway ) free( DefaultGateway );
194 if( Netmask ) free( Netmask );
195 } else {
196 /* Automatic case */
197 DH_DbgPrint
198 (MID_TRACE,("Adapter Name: [%s] (Bind Status %x) (dynamic)\n",
199 Adapter->DhclientInfo.name,
200 Adapter->BindStatus));
201
202 Adapter->DhclientInfo.client->state = S_INIT;
203 }
204
205 if( IPAddress ) free( IPAddress );
206
207 return TRUE;
208 }
209
210 void AdapterInit() {
211 WSAStartup(0x0101,&wsd);
212
213 InitializeListHead( &AdapterList );
214 }
215
216 int
217 InterfaceConnected(MIB_IFROW IfEntry)
218 {
219 if (IfEntry.dwOperStatus == IF_OPER_STATUS_CONNECTED ||
220 IfEntry.dwOperStatus == IF_OPER_STATUS_OPERATIONAL)
221 return 1;
222
223 DH_DbgPrint(MID_TRACE,("Interface %d is down\n", IfEntry.dwIndex));
224 return 0;
225 }
226
227 /*
228 * XXX Figure out the way to bind a specific adapter to a socket.
229 */
230 DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
231 PMIB_IFTABLE Table = (PMIB_IFTABLE) malloc(sizeof(MIB_IFTABLE));
232 DWORD Error, Size = sizeof(MIB_IFTABLE);
233 PDHCP_ADAPTER Adapter = NULL;
234 HANDLE AdapterStateChangedEvent = (HANDLE)Context;
235 struct interface_info *ifi = NULL;
236 int i;
237
238 /* FIXME: Kill this thread when the service is stopped */
239
240 do {
241 DH_DbgPrint(MID_TRACE,("Getting Adapter List...\n"));
242
243 while( (Error = GetIfTable(Table, &Size, 0 )) ==
244 ERROR_INSUFFICIENT_BUFFER ) {
245 DH_DbgPrint(MID_TRACE,("Error %d, New Buffer Size: %d\n", Error, Size));
246 free( Table );
247 Table = (PMIB_IFTABLE) malloc( Size );
248 }
249
250 if( Error != NO_ERROR )
251 break;
252
253 DH_DbgPrint(MID_TRACE,("Got Adapter List (%d entries)\n", Table->dwNumEntries));
254
255 for( i = Table->dwNumEntries - 1; i >= 0; i-- ) {
256 DH_DbgPrint(MID_TRACE,("Getting adapter %d attributes\n",
257 Table->table[i].dwIndex));
258
259 ApiLock();
260
261 if ((Adapter = AdapterFindByHardwareAddress(Table->table[i].bPhysAddr, Table->table[i].dwPhysAddrLen)))
262 {
263 /* This is an existing adapter */
264 if (InterfaceConnected(Table->table[i])) {
265 /* We're still active so we stay in the list */
266 ifi = &Adapter->DhclientInfo;
267 } else {
268 /* We've lost our link so out we go */
269 RemoveEntryList(&Adapter->ListEntry);
270 free(Adapter);
271 }
272
273 ApiUnlock();
274
275 continue;
276 }
277
278 ApiUnlock();
279
280 Adapter = (DHCP_ADAPTER*) calloc( sizeof( DHCP_ADAPTER ) + Table->table[i].dwMtu, 1 );
281
282 if( Adapter && Table->table[i].dwType == MIB_IF_TYPE_ETHERNET && InterfaceConnected(Table->table[i])) {
283 memcpy( &Adapter->IfMib, &Table->table[i],
284 sizeof(Adapter->IfMib) );
285 Adapter->DhclientInfo.client = &Adapter->DhclientState;
286 Adapter->DhclientInfo.rbuf = Adapter->recv_buf;
287 Adapter->DhclientInfo.rbuf_max = Table->table[i].dwMtu;
288 Adapter->DhclientInfo.rbuf_len =
289 Adapter->DhclientInfo.rbuf_offset = 0;
290 memcpy(Adapter->DhclientInfo.hw_address.haddr,
291 Adapter->IfMib.bPhysAddr,
292 Adapter->IfMib.dwPhysAddrLen);
293 Adapter->DhclientInfo.hw_address.hlen = Adapter->IfMib.dwPhysAddrLen;
294
295 /* I'm not sure where else to set this, but
296 some DHCP servers won't take a zero.
297 We checked the hardware type earlier in
298 the if statement. */
299 Adapter->DhclientInfo.hw_address.htype = HTYPE_ETHER;
300
301 if( DhcpSocket == INVALID_SOCKET ) {
302 DhcpSocket =
303 Adapter->DhclientInfo.rfdesc =
304 Adapter->DhclientInfo.wfdesc =
305 socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
306
307 if (DhcpSocket != INVALID_SOCKET) {
308 Adapter->ListenAddr.sin_family = AF_INET;
309 Adapter->ListenAddr.sin_port = htons(LOCAL_PORT);
310 Adapter->BindStatus =
311 (bind( Adapter->DhclientInfo.rfdesc,
312 (struct sockaddr *)&Adapter->ListenAddr,
313 sizeof(Adapter->ListenAddr) ) == 0) ?
314 0 : WSAGetLastError();
315 } else {
316 error("socket() failed: %d\n", WSAGetLastError());
317 }
318 } else {
319 Adapter->DhclientInfo.rfdesc =
320 Adapter->DhclientInfo.wfdesc = DhcpSocket;
321 }
322
323 Adapter->DhclientConfig.timeout = DHCP_PANIC_TIMEOUT;
324 Adapter->DhclientConfig.initial_interval = DHCP_DISCOVER_INTERVAL;
325 Adapter->DhclientConfig.retry_interval = DHCP_DISCOVER_INTERVAL;
326 Adapter->DhclientConfig.select_interval = 1;
327 Adapter->DhclientConfig.reboot_timeout = DHCP_REBOOT_TIMEOUT;
328 Adapter->DhclientConfig.backoff_cutoff = DHCP_BACKOFF_MAX;
329 Adapter->DhclientState.interval =
330 Adapter->DhclientConfig.retry_interval;
331
332 if( PrepareAdapterForService( Adapter ) ) {
333 Adapter->DhclientInfo.next = ifi;
334 ifi = &Adapter->DhclientInfo;
335
336 read_client_conf(&Adapter->DhclientInfo);
337
338 if (Adapter->DhclientInfo.client->state == S_INIT)
339 {
340 add_protocol(Adapter->DhclientInfo.name,
341 Adapter->DhclientInfo.rfdesc,
342 got_one, &Adapter->DhclientInfo);
343
344 state_init(&Adapter->DhclientInfo);
345 }
346
347 ApiLock();
348 InsertTailList( &AdapterList, &Adapter->ListEntry );
349 DbgPrint("DHCPCSVC: Discovered new adapter [%s]\n", Adapter->DhclientInfo.name);
350 SetEvent(AdapterStateChangedEvent);
351 ApiUnlock();
352 } else { free( Adapter ); Adapter = 0; }
353 } else { free( Adapter ); Adapter = 0; }
354
355 if( !Adapter )
356 DH_DbgPrint(MID_TRACE,("Adapter %d was rejected\n",
357 Table->table[i].dwIndex));
358 }
359 } while ((Error = NotifyAddrChange(NULL, NULL)) == NO_ERROR);
360
361 DbgPrint("DHCPCSVC: Adapter discovery thread is terminating! (Error: %d)\n", Error);
362
363 if( Table ) free( Table );
364 return Error;
365 }
366
367 HANDLE StartAdapterDiscovery(VOID) {
368 HANDLE /* ThreadHandle, */ EventHandle;
369
370 EventHandle = CreateEvent(NULL,
371 FALSE,
372 FALSE,
373 NULL);
374
375 #if 0
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 #else
388 AdapterDiscoveryThread((LPVOID)EventHandle);
389 #endif
390
391 return EventHandle;
392 }
393
394 void AdapterStop() {
395 PLIST_ENTRY ListEntry;
396 PDHCP_ADAPTER Adapter;
397 ApiLock();
398 while( !IsListEmpty( &AdapterList ) ) {
399 ListEntry = (PLIST_ENTRY)RemoveHeadList( &AdapterList );
400 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
401 free( Adapter );
402 }
403 ApiUnlock();
404 WSACleanup();
405 }
406
407 PDHCP_ADAPTER AdapterFindIndex( unsigned int indx ) {
408 PDHCP_ADAPTER Adapter;
409 PLIST_ENTRY ListEntry;
410
411 for( ListEntry = AdapterList.Flink;
412 ListEntry != &AdapterList;
413 ListEntry = ListEntry->Flink ) {
414 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
415 if( Adapter->IfMib.dwIndex == indx ) return Adapter;
416 }
417
418 return NULL;
419 }
420
421 PDHCP_ADAPTER AdapterFindName( const WCHAR *name ) {
422 PDHCP_ADAPTER Adapter;
423 PLIST_ENTRY ListEntry;
424
425 for( ListEntry = AdapterList.Flink;
426 ListEntry != &AdapterList;
427 ListEntry = ListEntry->Flink ) {
428 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
429 if( !wcsicmp( Adapter->IfMib.wszName, name ) ) return Adapter;
430 }
431
432 return NULL;
433 }
434
435 PDHCP_ADAPTER AdapterFindInfo( struct interface_info *ip ) {
436 PDHCP_ADAPTER Adapter;
437 PLIST_ENTRY ListEntry;
438
439 for( ListEntry = AdapterList.Flink;
440 ListEntry != &AdapterList;
441 ListEntry = ListEntry->Flink ) {
442 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
443 if( ip == &Adapter->DhclientInfo ) return Adapter;
444 }
445
446 return NULL;
447 }
448
449 PDHCP_ADAPTER AdapterFindByHardwareAddress( u_int8_t haddr[16], u_int8_t hlen ) {
450 PDHCP_ADAPTER Adapter;
451 PLIST_ENTRY ListEntry;
452
453 for(ListEntry = AdapterList.Flink;
454 ListEntry != &AdapterList;
455 ListEntry = ListEntry->Flink) {
456 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
457 if (Adapter->DhclientInfo.hw_address.hlen == hlen &&
458 !memcmp(Adapter->DhclientInfo.hw_address.haddr,
459 haddr,
460 hlen)) return Adapter;
461 }
462
463 return NULL;
464 }
465
466 PDHCP_ADAPTER AdapterGetFirst() {
467 if( IsListEmpty( &AdapterList ) ) return NULL; else {
468 return CONTAINING_RECORD
469 ( AdapterList.Flink, DHCP_ADAPTER, ListEntry );
470 }
471 }
472
473 PDHCP_ADAPTER AdapterGetNext( PDHCP_ADAPTER This )
474 {
475 if( This->ListEntry.Flink == &AdapterList ) return NULL;
476 return CONTAINING_RECORD
477 ( This->ListEntry.Flink, DHCP_ADAPTER, ListEntry );
478 }
479
480 void if_register_send(struct interface_info *ip) {
481
482 }
483
484 void if_register_receive(struct interface_info *ip) {
485 }