- Stop trying to find more adapters if it failed to avoid spamming the debug log
[reactos.git] / reactos / base / services / 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 if( !Netmask ) Netmask = "255.255.255.0";
173
174 Status = AddIPAddress( inet_addr( IPAddress ),
175 inet_addr( Netmask ),
176 Adapter->IfMib.dwIndex,
177 &Adapter->NteContext,
178 &Adapter->NteInstance );
179
180 DefaultGateway = RegReadString( AdapterKey, NULL, "DefaultGateway" );
181
182 if( DefaultGateway ) {
183 Adapter->RouterMib.dwForwardDest = 0;
184 Adapter->RouterMib.dwForwardMask = 0;
185 Adapter->RouterMib.dwForwardMetric1 = 1;
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 add_protocol(Adapter->DhclientInfo.name, Adapter->DhclientInfo.rfdesc, got_one, &Adapter->DhclientInfo);
203 Adapter->DhclientInfo.client->state = S_INIT;
204 state_reboot(&Adapter->DhclientInfo);
205 }
206
207 if( IPAddress ) free( IPAddress );
208
209 return TRUE;
210 }
211
212 void AdapterInit() {
213 WSAStartup(0x0101,&wsd);
214
215 InitializeListHead( &AdapterList );
216 }
217
218 int
219 InterfaceConnected(MIB_IFROW IfEntry)
220 {
221 if (IfEntry.dwOperStatus == IF_OPER_STATUS_CONNECTED ||
222 IfEntry.dwOperStatus == IF_OPER_STATUS_OPERATIONAL)
223 return 1;
224
225 DH_DbgPrint(MID_TRACE,("Interface %d is down\n", IfEntry.dwIndex));
226 return 0;
227 }
228
229 /*
230 * XXX Figure out the way to bind a specific adapter to a socket.
231 */
232 BOOLEAN AdapterDiscover() {
233 PMIB_IFTABLE Table = (PMIB_IFTABLE) malloc(sizeof(MIB_IFTABLE));
234 DWORD Error, Size = sizeof(MIB_IFTABLE);
235 PDHCP_ADAPTER Adapter = NULL;
236 struct interface_info *ifi = NULL;
237 int i;
238 BOOLEAN ret = TRUE;
239
240 DH_DbgPrint(MID_TRACE,("Getting Adapter List...\n"));
241
242 while( (Error = GetIfTable(Table, &Size, 0 )) ==
243 ERROR_INSUFFICIENT_BUFFER ) {
244 DH_DbgPrint(MID_TRACE,("Error %d, New Buffer Size: %d\n", Error, Size));
245 free( Table );
246 Table = (PMIB_IFTABLE) malloc( Size );
247 }
248
249 if( Error != NO_ERROR ) {
250 ret = FALSE;
251 goto term;
252 }
253
254 DH_DbgPrint(MID_TRACE,("Got Adapter List (%d entries)\n", Table->dwNumEntries));
255
256 for( i = Table->dwNumEntries - 1; i >= 0; i-- ) {
257 DH_DbgPrint(MID_TRACE,("Getting adapter %d attributes\n",
258 Table->table[i].dwIndex));
259
260 if (AdapterFindByHardwareAddress(Table->table[i].bPhysAddr, Table->table[i].dwPhysAddrLen))
261 {
262 /* This is an existing adapter */
263 if (InterfaceConnected(Table->table[i])) {
264 /* We're still active so we stay in the list */
265 ifi = &Adapter->DhclientInfo;
266 } else {
267 /* We've lost our link so out we go */
268 RemoveEntryList(&Adapter->ListEntry);
269 free(Adapter);
270 }
271
272 continue;
273 }
274
275 Adapter = (DHCP_ADAPTER*) calloc( sizeof( DHCP_ADAPTER ) + Table->table[i].dwMtu, 1 );
276
277 if( Adapter && Table->table[i].dwType == MIB_IF_TYPE_ETHERNET && InterfaceConnected(Table->table[i])) {
278 memcpy( &Adapter->IfMib, &Table->table[i],
279 sizeof(Adapter->IfMib) );
280 Adapter->DhclientInfo.client = &Adapter->DhclientState;
281 Adapter->DhclientInfo.rbuf = Adapter->recv_buf;
282 Adapter->DhclientInfo.rbuf_max = Table->table[i].dwMtu;
283 Adapter->DhclientInfo.rbuf_len =
284 Adapter->DhclientInfo.rbuf_offset = 0;
285 memcpy(Adapter->DhclientInfo.hw_address.haddr,
286 Adapter->IfMib.bPhysAddr,
287 Adapter->IfMib.dwPhysAddrLen);
288 Adapter->DhclientInfo.hw_address.hlen =
289 Adapter->IfMib.dwPhysAddrLen;
290 /* I'm not sure where else to set this, but
291 some DHCP servers won't take a zero.
292 We checked the hardware type earlier in
293 the if statement. */
294 Adapter->DhclientInfo.hw_address.htype =
295 HTYPE_ETHER;
296
297 if( DhcpSocket == INVALID_SOCKET ) {
298 DhcpSocket =
299 Adapter->DhclientInfo.rfdesc =
300 Adapter->DhclientInfo.wfdesc =
301 socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
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 Adapter->DhclientInfo.rfdesc =
311 Adapter->DhclientInfo.wfdesc = DhcpSocket;
312 }
313
314 Adapter->DhclientConfig.timeout = DHCP_PANIC_TIMEOUT;
315 Adapter->DhclientConfig.initial_interval = DHCP_DISCOVER_INTERVAL;
316 Adapter->DhclientConfig.retry_interval = DHCP_DISCOVER_INTERVAL;
317 Adapter->DhclientConfig.select_interval = 1;
318 Adapter->DhclientConfig.reboot_timeout = DHCP_REBOOT_TIMEOUT;
319 Adapter->DhclientConfig.backoff_cutoff = DHCP_BACKOFF_MAX;
320 Adapter->DhclientState.interval =
321 Adapter->DhclientConfig.retry_interval;
322
323 if( PrepareAdapterForService( Adapter ) ) {
324 Adapter->DhclientInfo.next = ifi;
325 ifi = &Adapter->DhclientInfo;
326
327 read_client_conf(&Adapter->DhclientInfo);
328
329 InsertTailList( &AdapterList, &Adapter->ListEntry );
330 } else { free( Adapter ); Adapter = 0; }
331 } else { free( Adapter ); Adapter = 0; }
332
333 if( !Adapter )
334 DH_DbgPrint(MID_TRACE,("Adapter %d was rejected\n",
335 Table->table[i].dwIndex));
336 }
337
338 DH_DbgPrint(MID_TRACE,("done with AdapterInit\n"));
339
340 term:
341 if( Table ) free( Table );
342 return ret;
343 }
344
345 void AdapterStop() {
346 PLIST_ENTRY ListEntry;
347 PDHCP_ADAPTER Adapter;
348 while( !IsListEmpty( &AdapterList ) ) {
349 ListEntry = (PLIST_ENTRY)RemoveHeadList( &AdapterList );
350 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
351 free( Adapter );
352 }
353 WSACleanup();
354 }
355
356 PDHCP_ADAPTER AdapterFindIndex( unsigned int indx ) {
357 PDHCP_ADAPTER Adapter;
358 PLIST_ENTRY ListEntry;
359
360 for( ListEntry = AdapterList.Flink;
361 ListEntry != &AdapterList;
362 ListEntry = ListEntry->Flink ) {
363 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
364 if( Adapter->IfMib.dwIndex == indx ) return Adapter;
365 }
366
367 return NULL;
368 }
369
370 PDHCP_ADAPTER AdapterFindName( const WCHAR *name ) {
371 PDHCP_ADAPTER Adapter;
372 PLIST_ENTRY ListEntry;
373
374 for( ListEntry = AdapterList.Flink;
375 ListEntry != &AdapterList;
376 ListEntry = ListEntry->Flink ) {
377 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
378 if( !wcsicmp( Adapter->IfMib.wszName, name ) ) return Adapter;
379 }
380
381 return NULL;
382 }
383
384 PDHCP_ADAPTER AdapterFindInfo( struct interface_info *ip ) {
385 PDHCP_ADAPTER Adapter;
386 PLIST_ENTRY ListEntry;
387
388 for( ListEntry = AdapterList.Flink;
389 ListEntry != &AdapterList;
390 ListEntry = ListEntry->Flink ) {
391 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
392 if( ip == &Adapter->DhclientInfo ) return Adapter;
393 }
394
395 return NULL;
396 }
397
398 PDHCP_ADAPTER AdapterFindByHardwareAddress( u_int8_t haddr[16], u_int8_t hlen ) {
399 PDHCP_ADAPTER Adapter;
400 PLIST_ENTRY ListEntry;
401
402 for(ListEntry = AdapterList.Flink;
403 ListEntry != &AdapterList;
404 ListEntry = ListEntry->Flink) {
405 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
406 if (Adapter->DhclientInfo.hw_address.hlen == hlen &&
407 !memcmp(Adapter->DhclientInfo.hw_address.haddr,
408 haddr,
409 hlen)) return Adapter;
410 }
411
412 return NULL;
413 }
414
415 PDHCP_ADAPTER AdapterGetFirst() {
416 if( IsListEmpty( &AdapterList ) ) return NULL; else {
417 return CONTAINING_RECORD
418 ( AdapterList.Flink, DHCP_ADAPTER, ListEntry );
419 }
420 }
421
422 PDHCP_ADAPTER AdapterGetNext( PDHCP_ADAPTER This )
423 {
424 if( This->ListEntry.Flink == &AdapterList ) return NULL;
425 return CONTAINING_RECORD
426 ( This->ListEntry.Flink, DHCP_ADAPTER, ListEntry );
427 }
428
429 void if_register_send(struct interface_info *ip) {
430
431 }
432
433 void if_register_receive(struct interface_info *ip) {
434 }