[DHCP]
[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
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 BOOLEAN AdapterDiscover() {
231 PMIB_IFTABLE Table = (PMIB_IFTABLE) malloc(sizeof(MIB_IFTABLE));
232 DWORD Error, Size = sizeof(MIB_IFTABLE);
233 PDHCP_ADAPTER Adapter = NULL;
234 struct interface_info *ifi = NULL;
235 int i;
236 BOOLEAN ret = TRUE;
237
238 DH_DbgPrint(MID_TRACE,("Getting Adapter List...\n"));
239
240 while( (Error = GetIfTable(Table, &Size, 0 )) ==
241 ERROR_INSUFFICIENT_BUFFER ) {
242 DH_DbgPrint(MID_TRACE,("Error %d, New Buffer Size: %d\n", Error, Size));
243 free( Table );
244 Table = (PMIB_IFTABLE) malloc( Size );
245 }
246
247 if( Error != NO_ERROR ) {
248 ret = FALSE;
249 goto term;
250 }
251
252 DH_DbgPrint(MID_TRACE,("Got Adapter List (%d entries)\n", Table->dwNumEntries));
253
254 for( i = Table->dwNumEntries - 1; i >= 0; i-- ) {
255 DH_DbgPrint(MID_TRACE,("Getting adapter %d attributes\n",
256 Table->table[i].dwIndex));
257
258 if ((Adapter = AdapterFindByHardwareAddress(Table->table[i].bPhysAddr, Table->table[i].dwPhysAddrLen)))
259 {
260 /* This is an existing adapter */
261 if (InterfaceConnected(Table->table[i])) {
262 /* We're still active so we stay in the list */
263 ifi = &Adapter->DhclientInfo;
264 } else {
265 /* We've lost our link so out we go */
266 RemoveEntryList(&Adapter->ListEntry);
267 free(Adapter);
268 }
269
270 continue;
271 }
272
273 Adapter = (DHCP_ADAPTER*) calloc( sizeof( DHCP_ADAPTER ) + Table->table[i].dwMtu, 1 );
274
275 if( Adapter && Table->table[i].dwType == MIB_IF_TYPE_ETHERNET && InterfaceConnected(Table->table[i])) {
276 memcpy( &Adapter->IfMib, &Table->table[i],
277 sizeof(Adapter->IfMib) );
278 Adapter->DhclientInfo.client = &Adapter->DhclientState;
279 Adapter->DhclientInfo.rbuf = Adapter->recv_buf;
280 Adapter->DhclientInfo.rbuf_max = Table->table[i].dwMtu;
281 Adapter->DhclientInfo.rbuf_len =
282 Adapter->DhclientInfo.rbuf_offset = 0;
283 memcpy(Adapter->DhclientInfo.hw_address.haddr,
284 Adapter->IfMib.bPhysAddr,
285 Adapter->IfMib.dwPhysAddrLen);
286 Adapter->DhclientInfo.hw_address.hlen =
287 Adapter->IfMib.dwPhysAddrLen;
288 /* I'm not sure where else to set this, but
289 some DHCP servers won't take a zero.
290 We checked the hardware type earlier in
291 the if statement. */
292 Adapter->DhclientInfo.hw_address.htype =
293 HTYPE_ETHER;
294
295 if( DhcpSocket == INVALID_SOCKET ) {
296 DhcpSocket =
297 Adapter->DhclientInfo.rfdesc =
298 Adapter->DhclientInfo.wfdesc =
299 socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
300
301 if (DhcpSocket != INVALID_SOCKET) {
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 InsertTailList( &AdapterList, &Adapter->ListEntry );
342 } else { free( Adapter ); Adapter = 0; }
343 } else { free( Adapter ); Adapter = 0; }
344
345 if( !Adapter )
346 DH_DbgPrint(MID_TRACE,("Adapter %d was rejected\n",
347 Table->table[i].dwIndex));
348 }
349
350 DH_DbgPrint(MID_TRACE,("done with AdapterInit\n"));
351
352 term:
353 if( Table ) free( Table );
354 return ret;
355 }
356
357 void AdapterStop() {
358 PLIST_ENTRY ListEntry;
359 PDHCP_ADAPTER Adapter;
360 while( !IsListEmpty( &AdapterList ) ) {
361 ListEntry = (PLIST_ENTRY)RemoveHeadList( &AdapterList );
362 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
363 free( Adapter );
364 }
365 WSACleanup();
366 }
367
368 PDHCP_ADAPTER AdapterFindIndex( unsigned int indx ) {
369 PDHCP_ADAPTER Adapter;
370 PLIST_ENTRY ListEntry;
371
372 for( ListEntry = AdapterList.Flink;
373 ListEntry != &AdapterList;
374 ListEntry = ListEntry->Flink ) {
375 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
376 if( Adapter->IfMib.dwIndex == indx ) return Adapter;
377 }
378
379 return NULL;
380 }
381
382 PDHCP_ADAPTER AdapterFindName( const WCHAR *name ) {
383 PDHCP_ADAPTER Adapter;
384 PLIST_ENTRY ListEntry;
385
386 for( ListEntry = AdapterList.Flink;
387 ListEntry != &AdapterList;
388 ListEntry = ListEntry->Flink ) {
389 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
390 if( !wcsicmp( Adapter->IfMib.wszName, name ) ) return Adapter;
391 }
392
393 return NULL;
394 }
395
396 PDHCP_ADAPTER AdapterFindInfo( struct interface_info *ip ) {
397 PDHCP_ADAPTER Adapter;
398 PLIST_ENTRY ListEntry;
399
400 for( ListEntry = AdapterList.Flink;
401 ListEntry != &AdapterList;
402 ListEntry = ListEntry->Flink ) {
403 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
404 if( ip == &Adapter->DhclientInfo ) return Adapter;
405 }
406
407 return NULL;
408 }
409
410 PDHCP_ADAPTER AdapterFindByHardwareAddress( u_int8_t haddr[16], u_int8_t hlen ) {
411 PDHCP_ADAPTER Adapter;
412 PLIST_ENTRY ListEntry;
413
414 for(ListEntry = AdapterList.Flink;
415 ListEntry != &AdapterList;
416 ListEntry = ListEntry->Flink) {
417 Adapter = CONTAINING_RECORD( ListEntry, DHCP_ADAPTER, ListEntry );
418 if (Adapter->DhclientInfo.hw_address.hlen == hlen &&
419 !memcmp(Adapter->DhclientInfo.hw_address.haddr,
420 haddr,
421 hlen)) return Adapter;
422 }
423
424 return NULL;
425 }
426
427 PDHCP_ADAPTER AdapterGetFirst() {
428 if( IsListEmpty( &AdapterList ) ) return NULL; else {
429 return CONTAINING_RECORD
430 ( AdapterList.Flink, DHCP_ADAPTER, ListEntry );
431 }
432 }
433
434 PDHCP_ADAPTER AdapterGetNext( PDHCP_ADAPTER This )
435 {
436 if( This->ListEntry.Flink == &AdapterList ) return NULL;
437 return CONTAINING_RECORD
438 ( This->ListEntry.Flink, DHCP_ADAPTER, ListEntry );
439 }
440
441 void if_register_send(struct interface_info *ip) {
442
443 }
444
445 void if_register_receive(struct interface_info *ip) {
446 }