No more ick tabs.
[reactos.git] / reactos / lib / iphlpapi / ifenum_reactos.c
1 /* Copyright (C) 2003 Art Yerkes
2 * A reimplementation of ifenum.c by Juan Lang
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Implementation notes
19 * - Our bretheren use IOCTL_TCP_QUERY_INFORMATION_EX to get information
20 * from tcpip.sys and IOCTL_TCP_SET_INFORMATION_EX to set info (such as
21 * the route table). These ioctls mirror WsControl exactly in usage.
22 * - This iphlpapi does not rely on any reactos-only features.
23 * - This implementation is meant to be largely correct. I am not, however,
24 * paying any attention to performance. It can be done faster, and
25 * someone should definately optimize this code when speed is more of a
26 * priority than it is now.
27 *
28 * Edited implementation notes from the original -- Basically edited to add
29 * information and prune things which are not accurate about this file.
30 * Interface index fun:
31 * - Windows may rely on an index being cleared in the topmost 8 bits in some
32 * APIs; see GetFriendlyIfIndex and the mention of "backward compatible"
33 * indexes. It isn't clear which APIs might fail with non-backward-compatible
34 * indexes, but I'll keep them bits clear just in case.
35 * FIXME:
36 * - We don't support IPv6 addresses here yet -- I moved the upper edge
37 * functions into iphlpv6.c (arty)
38 */
39
40 #include "iphlpapi_private.h"
41 #include "ifenum.h"
42
43 /* Globals */
44 const PWCHAR TcpFileName = L"\\Device\\Tcp";
45
46 /* Functions */
47
48 /* I'm a bit skittish about maintaining this info in memory, as I'd rather
49 * not add any mutex or critical section blockers to these functions. I've
50 * encountered far too many windows functions that contribute to deadlock
51 * by not announcing themselves. */
52 void interfaceMapInit(void)
53 {
54 /* For now, nothing */
55 }
56
57 void interfaceMapFree(void)
58 {
59 /* Ditto. */
60 }
61
62 NTSTATUS openTcpFile(PHANDLE tcpFile) {
63 UNICODE_STRING fileName;
64 OBJECT_ATTRIBUTES objectAttributes;
65 IO_STATUS_BLOCK ioStatusBlock;
66 NTSTATUS status;
67
68 TRACE("called.\n");
69
70 /* Shamelessly ripped from CreateFileW */
71 RtlInitUnicodeString( &fileName, TcpFileName );
72
73 InitializeObjectAttributes( &objectAttributes,
74 &fileName,
75 OBJ_CASE_INSENSITIVE,
76 NULL,
77 NULL );
78
79 status = NtCreateFile( tcpFile,
80 SYNCHRONIZE | GENERIC_EXECUTE,
81 &objectAttributes,
82 &ioStatusBlock,
83 NULL,
84 FILE_ATTRIBUTE_NORMAL,
85 FILE_SHARE_READ | FILE_SHARE_WRITE,
86 FILE_OPEN_IF,
87 FILE_SYNCHRONOUS_IO_NONALERT,
88 0,
89 0 );
90
91 /* String does not need to be freed: it points to the constant
92 * string we provided */
93
94 TRACE("returning %08x\n", (int)status);
95
96 return status;
97 }
98
99 void closeTcpFile( HANDLE h ) {
100 TRACE("called.\n");
101 NtClose( h );
102 }
103
104 /* A generic thing-getting function which interacts in the right way with
105 * TDI. This may seem oblique, but I'm using it to reduce code and hopefully
106 * make this thing easier to debug.
107 *
108 * The things returned can be any of:
109 * TDIEntityID
110 * TDIObjectID
111 * IFEntry
112 * IPSNMPInfo
113 * IPAddrEntry
114 * IPInterfaceInfo
115 */
116 NTSTATUS tdiGetSetOfThings( HANDLE tcpFile,
117 DWORD toiClass,
118 DWORD toiType,
119 DWORD toiId,
120 DWORD teiEntity,
121 DWORD fixedPart,
122 DWORD entrySize,
123 PVOID *tdiEntitySet,
124 PDWORD numEntries ) {
125 TCP_REQUEST_QUERY_INFORMATION_EX req = TCP_REQUEST_QUERY_INFORMATION_INIT;
126 PVOID entitySet = 0;
127 NTSTATUS status = STATUS_SUCCESS;
128 DWORD allocationSizeForEntityArray = entrySize * MAX_TDI_ENTITIES,
129 arraySize = entrySize * MAX_TDI_ENTITIES;
130
131 DPRINT("TdiGetSetOfThings(tcpFile %x,toiClass %x,toiType %x,toiId %x,"
132 "teiEntity %x,fixedPart %d,entrySize %d)\n",
133 (int)tcpFile,
134 (int)toiClass,
135 (int)toiType,
136 (int)toiId,
137 (int)teiEntity,
138 (int)fixedPart,
139 (int)entrySize );
140
141 req.ID.toi_class = toiClass;
142 req.ID.toi_type = toiType;
143 req.ID.toi_id = toiId;
144 req.ID.toi_entity.tei_entity = teiEntity;
145
146 /* There's a subtle problem here...
147 * If an interface is added at this exact instant, (as if by a PCMCIA
148 * card insertion), the array will still not have enough entries after
149 * have allocated it after the first DeviceIoControl call.
150 *
151 * We'll get around this by repeating until the number of interfaces
152 * stabilizes.
153 */
154 do {
155 assert( !entitySet ); /* We must not have an entity set allocated */
156 status = DeviceIoControl( tcpFile,
157 IOCTL_TCP_QUERY_INFORMATION_EX,
158 &req,
159 sizeof(req),
160 0,
161 0,
162 &allocationSizeForEntityArray,
163 NULL );
164
165 if( !NT_SUCCESS(status) ) {
166 DPRINT("TdiGetSetOfThings() => %08x\n", (int)status);
167 return status;
168 }
169
170 arraySize = allocationSizeForEntityArray;
171 entitySet = HeapAlloc( GetProcessHeap(), 0, arraySize );
172
173 if( !entitySet ) {
174 status = STATUS_INSUFFICIENT_RESOURCES;
175 DPRINT("TdiGetSetOfThings() => %08x\n", (int)status);
176 return status;
177 }
178
179 status = DeviceIoControl( tcpFile,
180 IOCTL_TCP_QUERY_INFORMATION_EX,
181 &req,
182 sizeof(req),
183 entitySet,
184 arraySize,
185 &allocationSizeForEntityArray,
186 NULL );
187
188 /* This is why we have the loop -- we might have added an adapter */
189 if( arraySize == allocationSizeForEntityArray )
190 break;
191
192 HeapFree( GetProcessHeap(), 0, entitySet );
193 entitySet = 0;
194
195 if( !NT_SUCCESS(status) ) {
196 DPRINT("TdiGetSetOfThings() => %08x\n", (int)status);
197 return status;
198 }
199
200 DPRINT("TdiGetSetOfThings(): Array changed size: %d -> %d.\n",
201 arraySize, allocationSizeForEntityArray );
202 } while( TRUE ); /* We break if the array we received was the size we
203 * expected. Therefore, we got here because it wasn't */
204
205 *numEntries = (arraySize - fixedPart) / entrySize;
206 *tdiEntitySet = entitySet;
207
208 DPRINT("TdiGetSetOfThings() => Success: %d things @ %08x\n",
209 (int)*numEntries, (int)entitySet);
210
211 return STATUS_SUCCESS;
212 }
213
214 VOID tdiFreeThingSet( PVOID things ) {
215 HeapFree( GetProcessHeap(), 0, things );
216 }
217
218 NTSTATUS tdiGetMibForIfEntity
219 ( HANDLE tcpFile, TDIEntityID *ent, IFEntrySafelySized *entry ) {
220 TCP_REQUEST_QUERY_INFORMATION_EX req = TCP_REQUEST_QUERY_INFORMATION_INIT;
221 NTSTATUS status = STATUS_SUCCESS;
222 DWORD returnSize;
223
224 DPRINT("TdiGetMibForIfEntity(tcpFile %x,entityId %x)\n",
225 (int)tcpFile, (int)ent->tei_instance);
226
227 req.ID.toi_class = INFO_CLASS_PROTOCOL;
228 req.ID.toi_type = INFO_TYPE_PROVIDER;
229 req.ID.toi_id = IF_MIB_STATS_ID;
230 req.ID.toi_entity = *ent;
231
232 status = DeviceIoControl( tcpFile,
233 IOCTL_TCP_QUERY_INFORMATION_EX,
234 &req,
235 sizeof(req),
236 entry,
237 sizeof(*entry),
238 &returnSize,
239 NULL );
240
241 if( !NT_SUCCESS(status) ) {
242 TRACE("failure: %08x\n", status);
243 return status;
244 } else TRACE("Success.\n");
245
246 DPRINT("TdiGetMibForIfEntity() => {\n"
247 " if_index ....................... %x\n"
248 " if_type ........................ %x\n"
249 " if_mtu ......................... %d\n"
250 " if_speed ....................... %x\n"
251 " if_physaddrlen ................. %d\n",
252 entry->ent.if_index,
253 entry->ent.if_type,
254 entry->ent.if_mtu,
255 entry->ent.if_speed,
256 entry->ent.if_physaddrlen);
257 DPRINT(" if_physaddr .................... %02x:%02x:%02x:%02x:%02x:%02x\n",
258 " if_descr ....................... %s\n",
259 entry->ent.if_physaddr[0] & 0xff,
260 entry->ent.if_physaddr[1] & 0xff,
261 entry->ent.if_physaddr[2] & 0xff,
262 entry->ent.if_physaddr[3] & 0xff,
263 entry->ent.if_physaddr[4] & 0xff,
264 entry->ent.if_physaddr[5] & 0xff,
265 entry->ent.if_descr);
266 DPRINT("} status %08x\n",status);
267
268 return status;
269 }
270
271 NTSTATUS tdiGetEntityIDSet( HANDLE tcpFile,
272 TDIEntityID **entitySet,
273 PDWORD numEntities ) {
274 NTSTATUS status = tdiGetSetOfThings( tcpFile,
275 INFO_CLASS_GENERIC,
276 INFO_TYPE_PROVIDER,
277 ENTITY_LIST_ID,
278 GENERIC_ENTITY,
279 0,
280 sizeof(TDIEntityID),
281 (PVOID *)entitySet,
282 numEntities );
283 if( NT_SUCCESS(status) ) {
284 int i;
285
286 for( i = 0; i < *numEntities; i++ ) {
287 DPRINT("%-4d: %04x:%08x\n",
288 i,
289 (*entitySet)[i].tei_entity,
290 (*entitySet)[i].tei_instance );
291 }
292 }
293
294 return status;
295 }
296
297 static BOOL isInterface( TDIEntityID *if_maybe ) {
298 return
299 if_maybe->tei_entity == IF_ENTITY;
300 }
301
302 static BOOL isLoopback( HANDLE tcpFile, TDIEntityID *loop_maybe ) {
303 IFEntrySafelySized entryInfo;
304
305 tdiGetMibForIfEntity( tcpFile,
306 loop_maybe,
307 &entryInfo );
308
309 return !entryInfo.ent.if_type ||
310 entryInfo.ent.if_type == IFENT_SOFTWARE_LOOPBACK;
311 }
312
313 NTSTATUS tdiGetEntityType( HANDLE tcpFile, TDIEntityID *ent, PULONG type ) {
314 TCP_REQUEST_QUERY_INFORMATION_EX req = TCP_REQUEST_QUERY_INFORMATION_INIT;
315 NTSTATUS status = STATUS_SUCCESS;
316 DWORD returnSize;
317
318 DPRINT("TdiGetEntityType(tcpFile %x,entityId %x)\n",
319 (DWORD)tcpFile, ent->tei_instance);
320
321 req.ID.toi_class = INFO_CLASS_GENERIC;
322 req.ID.toi_type = INFO_TYPE_PROVIDER;
323 req.ID.toi_id = ENTITY_TYPE_ID;
324 req.ID.toi_entity.tei_entity = ent->tei_entity;
325 req.ID.toi_entity.tei_instance = ent->tei_instance;
326
327 status = DeviceIoControl( tcpFile,
328 IOCTL_TCP_QUERY_INFORMATION_EX,
329 &req,
330 sizeof(req),
331 type,
332 sizeof(*type),
333 &returnSize,
334 NULL );
335
336 DPRINT("TdiGetEntityType() => %08x %08x\n", *type, status);
337
338 return status;
339 }
340
341 static NTSTATUS getInterfaceInfoSet( HANDLE tcpFile,
342 IFInfo **infoSet,
343 PDWORD numInterfaces ) {
344 DWORD numEntities;
345 TDIEntityID *entIDSet = 0;
346 NTSTATUS status = tdiGetEntityIDSet( tcpFile, &entIDSet, &numEntities );
347 IFInfo *infoSetInt = 0;
348 BOOL interfaceInfoComplete;
349 int curInterf = 0, i;
350
351 if( NT_SUCCESS(status) )
352 infoSetInt = HeapAlloc( GetProcessHeap(), 0,
353 sizeof(IFInfo) * numEntities );
354
355 if( infoSetInt ) {
356 for( i = 0; i < numEntities; i++ ) {
357 if( isInterface( &entIDSet[i] ) ) {
358 status = tdiGetMibForIfEntity
359 ( tcpFile,
360 &entIDSet[i],
361 &infoSetInt[curInterf].if_info );
362 if( NT_SUCCESS(status) ) {
363 DWORD numAddrs;
364 IPAddrEntry *addrs;
365 TDIEntityID ip_ent;
366 int j,k;
367
368 interfaceInfoComplete = FALSE;
369 for( j = 0; NT_SUCCESS(status); j++ ) {
370 status = getNthIpEntity( tcpFile, j, &ip_ent );
371 if( NT_SUCCESS(status) )
372 status = tdiGetIpAddrsForIpEntity
373 ( tcpFile, &ip_ent, &addrs, &numAddrs );
374 for( k = 0; k < numAddrs && NT_SUCCESS(status); k++ ) {
375 if( addrs[k].iae_index ==
376 infoSetInt[curInterf].if_info.ent.if_index ) {
377 memcpy( &infoSetInt[curInterf].ip_addr,
378 &addrs[k],
379 sizeof( addrs[k] ) );
380 interfaceInfoComplete = TRUE;
381 break;
382 }
383 }
384 if( interfaceInfoComplete ) break;
385 }
386 }
387 if( NT_SUCCESS(status) ) curInterf++;
388 }
389 }
390 }
391
392 *infoSet = infoSetInt;
393 *numInterfaces = curInterf;
394
395 return STATUS_SUCCESS;
396 }
397
398 static DWORD getNumInterfacesInt(BOOL onlyLoopback)
399 {
400 DWORD numEntities, numInterfaces = 0;
401 TDIEntityID *entitySet;
402 HANDLE tcpFile;
403 NTSTATUS status;
404 int i;
405
406 status = openTcpFile( &tcpFile );
407
408 if( !NT_SUCCESS(status) ) {
409 DPRINT("getNumInterfaces: failed %08x\n", status );
410 return 0;
411 }
412
413 status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
414
415 if( !NT_SUCCESS(status) ) {
416 DPRINT("getNumInterfaces: failed %08x\n", status );
417 return 0;
418 }
419
420 closeTcpFile( tcpFile );
421
422 for( i = 0; i < numEntities; i++ ) {
423 if( isInterface( &entitySet[i] ) &&
424 (!onlyLoopback || isLoopback( tcpFile, &entitySet[i] )) )
425 numInterfaces++;
426 }
427
428 DPRINT("getNumInterfaces: success: %d %d %08x\n",
429 onlyLoopback, numInterfaces, status );
430
431 tdiFreeThingSet( entitySet );
432
433 return numInterfaces;
434 }
435
436 DWORD getNumInterfaces(void)
437 {
438 return getNumInterfacesInt( FALSE );
439 }
440
441 DWORD getNumNonLoopbackInterfaces(void)
442 {
443 return getNumInterfacesInt( TRUE );
444 }
445
446 DWORD getNthInterfaceEntity( HANDLE tcpFile, DWORD index, TDIEntityID *ent ) {
447 DWORD numEntities = 0;
448 DWORD numInterfaces = 0;
449 TDIEntityID *entitySet = 0;
450 NTSTATUS status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
451 int i;
452
453 if( !NT_SUCCESS(status) )
454 return status;
455
456 for( i = 0; i < numEntities; i++ ) {
457 if( isInterface( &entitySet[i] ) ) {
458 if( numInterfaces == index ) break;
459 else numInterfaces++;
460 }
461 }
462
463 DPRINT("Index %d is entity #%d - %04x:%08x\n", index, i,
464 entitySet[i].tei_entity, entitySet[i].tei_instance );
465
466 if( numInterfaces == index && i < numEntities ) {
467 memcpy( ent, &entitySet[i], sizeof(*ent) );
468 tdiFreeThingSet( entitySet );
469 return STATUS_SUCCESS;
470 } else {
471 tdiFreeThingSet( entitySet );
472 return STATUS_UNSUCCESSFUL;
473 }
474 }
475
476 NTSTATUS getInterfaceInfoByIndex( HANDLE tcpFile, DWORD index, IFInfo *info ) {
477 IFInfo *ifInfo;
478 DWORD numInterfaces;
479 NTSTATUS status = getInterfaceInfoSet( tcpFile, &ifInfo, &numInterfaces );
480 int i;
481
482 if( NT_SUCCESS(status) )
483 for( i = 0; i < numInterfaces; i++ ) {
484 if( ifInfo[i].if_info.ent.if_index == index ) {
485 memcpy( info, &ifInfo[i], sizeof(*info) );
486 break;
487 }
488 }
489
490 if( NT_SUCCESS(status) )
491 return i < numInterfaces ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
492 else
493 return status;
494 }
495
496 NTSTATUS getInterfaceInfoByName( HANDLE tcpFile, char *name, IFInfo *info ) {
497 IFInfo *ifInfo;
498 DWORD numInterfaces;
499 int i;
500 NTSTATUS status = getInterfaceInfoSet( tcpFile, &ifInfo, &numInterfaces );
501
502 if( NT_SUCCESS(status) )
503 for( i = 0; i < numInterfaces; i++ ) {
504 if( !strcmp(ifInfo[i].if_info.ent.if_descr, name) ) {
505 memcpy( info, &ifInfo[i], sizeof(*info) );
506 break;
507 }
508 }
509
510 if( NT_SUCCESS(status) )
511 return i < numInterfaces ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
512 else
513 return status;
514 }
515
516 /* Note that the result of this operation must be freed later */
517
518 const char *getInterfaceNameByIndex(DWORD index)
519 {
520 IFInfo ifInfo;
521 HANDLE tcpFile;
522 char *interfaceName = 0, *adapter_name = 0;
523 NTSTATUS status = openTcpFile( &tcpFile );
524
525 if( NT_SUCCESS(status) ) {
526 status = getInterfaceInfoByIndex( tcpFile, index, &ifInfo );
527
528 if( NT_SUCCESS(status) ) {
529 adapter_name = ifInfo.if_info.ent.if_descr;
530
531 interfaceName = HeapAlloc( GetProcessHeap(), 0,
532 strlen(adapter_name) + 1 );
533 strcpy( interfaceName, adapter_name );
534
535 closeTcpFile( tcpFile );
536 }
537 }
538
539 return interfaceName;
540 }
541
542 void consumeInterfaceName(const char *name) {
543 HeapFree( GetProcessHeap(), 0, (char *)name );
544 }
545
546 DWORD getInterfaceIndexByName(const char *name, PDWORD index)
547 {
548 IFInfo ifInfo;
549 HANDLE tcpFile;
550 NTSTATUS status = openTcpFile( &tcpFile );
551
552 if( NT_SUCCESS(status) ) {
553 status = getInterfaceInfoByName( tcpFile, (char *)name, &ifInfo );
554
555 if( NT_SUCCESS(status) ) {
556 *index = ifInfo.if_info.ent.if_index;
557 closeTcpFile( tcpFile );
558 }
559 }
560
561 return status;
562 }
563
564 InterfaceIndexTable *getInterfaceIndexTableInt( BOOL nonLoopbackOnly ) {
565 DWORD numInterfaces, curInterface = 0;
566 int i;
567 IFInfo *ifInfo;
568 InterfaceIndexTable *ret = 0;
569 HANDLE tcpFile;
570 NTSTATUS status = openTcpFile( &tcpFile );
571
572 if( NT_SUCCESS(status) ) {
573 status = getInterfaceInfoSet( tcpFile, &ifInfo, &numInterfaces );
574
575 if( NT_SUCCESS(status) ) {
576 ret = (InterfaceIndexTable *)
577 calloc(1,
578 sizeof(InterfaceIndexTable) +
579 (numInterfaces - 1) * sizeof(DWORD));
580
581 if (ret) {
582 ret->numAllocated = numInterfaces;
583
584 for( i = 0; i < numInterfaces; i++ ) {
585 if( !nonLoopbackOnly ||
586 !isLoopback( tcpFile, &ifInfo[i].entity_id ) ) {
587 ret->indexes[curInterface++] =
588 ifInfo[i].if_info.ent.if_index;
589 }
590 }
591
592 ret->numIndexes = curInterface;
593 }
594
595 tdiFreeThingSet( ifInfo );
596 }
597 closeTcpFile( tcpFile );
598 }
599
600 return ret;
601 }
602
603 InterfaceIndexTable *getInterfaceIndexTable(void) {
604 return getInterfaceIndexTableInt( FALSE );
605 }
606
607 InterfaceIndexTable *getNonLoopbackInterfaceIndexTable(void) {
608 return getInterfaceIndexTableInt( TRUE );
609 }
610
611 DWORD getInterfaceIPAddrByName(const char *name)
612 {
613 return INADDR_ANY;
614 }
615
616 NTSTATUS getIPAddrEntryForIf(HANDLE tcpFile,
617 char *name,
618 DWORD index,
619 IFInfo *ifInfo) {
620 NTSTATUS status =
621 name ?
622 getInterfaceInfoByName( tcpFile, name, ifInfo ) :
623 getInterfaceInfoByIndex( tcpFile, index, ifInfo );
624 return status;
625 }
626
627 DWORD getAddrByIndexOrName( char *name, DWORD index, IPHLPAddrType addrType ) {
628 IFInfo ifInfo;
629 HANDLE tcpFile = INVALID_HANDLE_VALUE;
630 NTSTATUS status = STATUS_SUCCESS;
631 DWORD addrOut = INADDR_ANY;
632
633 status = openTcpFile( &tcpFile );
634
635 if( NT_SUCCESS(status) ) {
636 status = getIPAddrEntryForIf( tcpFile, name, index, &ifInfo );
637 if( NT_SUCCESS(status) ) {
638 switch( addrType ) {
639 case IPAAddr: addrOut = ifInfo.ip_addr.iae_addr; break;
640 case IPABcast: addrOut = ifInfo.ip_addr.iae_bcastaddr; break;
641 case IPAMask: addrOut = ifInfo.ip_addr.iae_mask; break;
642 case IFMtu: addrOut = ifInfo.if_info.ent.if_mtu; break;
643 case IFStatus: addrOut = ifInfo.if_info.ent.if_operstatus; break;
644 }
645 }
646 closeTcpFile( &tcpFile );
647 }
648
649 return addrOut;
650 }
651
652 DWORD getInterfaceIPAddrByIndex(DWORD index) {
653 return getAddrByIndexOrName( 0, index, IPAAddr );
654 }
655
656 DWORD getInterfaceBCastAddrByName(const char *name) {
657 return getAddrByIndexOrName( (char *)name, 0, IPABcast );
658 }
659
660 DWORD getInterfaceBCastAddrByIndex(DWORD index) {
661 return getAddrByIndexOrName( 0, index, IPABcast );
662 }
663
664 DWORD getInterfaceMaskByName(const char *name) {
665 return getAddrByIndexOrName( (char *)name, 0, IPAMask );
666 }
667
668 DWORD getInterfaceMaskByIndex(DWORD index) {
669 return getAddrByIndexOrName( 0, index, IPAMask );
670 }
671
672 void getInterfacePhysicalFromInfo( IFInfo *info,
673 PDWORD len, PBYTE addr, PDWORD type ) {
674 *len = info->if_info.ent.if_physaddrlen;
675 memcpy( addr, info->if_info.ent.if_physaddr, *len );
676 *type = info->if_info.ent.if_type;
677 }
678
679 DWORD getInterfacePhysicalByName(const char *name, PDWORD len, PBYTE addr,
680 PDWORD type)
681 {
682 HANDLE tcpFile;
683 IFInfo info;
684 NTSTATUS status = openTcpFile( &tcpFile );
685
686 if( NT_SUCCESS(status) ) {
687 status = getInterfaceInfoByName( tcpFile, (char *)name, &info );
688 if( NT_SUCCESS(status) )
689 getInterfacePhysicalFromInfo( &info, len, addr, type );
690 closeTcpFile( tcpFile );
691 }
692
693 return status;
694 }
695
696 DWORD getInterfacePhysicalByIndex(DWORD index, PDWORD len, PBYTE addr,
697 PDWORD type)
698 {
699 HANDLE tcpFile;
700 IFInfo info;
701 NTSTATUS status = openTcpFile( &tcpFile );
702
703 if( NT_SUCCESS(status) ) {
704 status = getInterfaceInfoByIndex( tcpFile, index, &info );
705 if( NT_SUCCESS(status) )
706 getInterfacePhysicalFromInfo( &info, len, addr, type );
707 closeTcpFile( tcpFile );
708 }
709
710 return status;
711 }
712
713 DWORD getInterfaceMtuByName(const char *name, PDWORD mtu) {
714 *mtu = getAddrByIndexOrName( (char *)name, 0, IFMtu );
715 return STATUS_SUCCESS;
716 }
717
718 DWORD getInterfaceMtuByIndex(DWORD index, PDWORD mtu) {
719 *mtu = getAddrByIndexOrName( 0, index, IFMtu );
720 return STATUS_SUCCESS;
721 }
722
723 DWORD getInterfaceStatusByName(const char *name, PDWORD status) {
724 *status = getAddrByIndexOrName( (char *)name, 0, IFStatus );
725 return STATUS_SUCCESS;
726 }
727
728 DWORD getInterfaceStatusByIndex(DWORD index, PDWORD status)
729 {
730 *status = getAddrByIndexOrName( 0, index, IFStatus );
731 return STATUS_SUCCESS;
732 }
733
734 DWORD getInterfaceEntryByName(const char *name, PMIB_IFROW entry)
735 {
736 HANDLE tcpFile;
737 IFInfo info;
738 NTSTATUS status = openTcpFile( &tcpFile );
739
740 DPRINT("Called.\n");
741
742 if( NT_SUCCESS(status) ) {
743 status = getInterfaceInfoByName( tcpFile, (char *)name, &info );
744
745 if( NT_SUCCESS(status) ) {
746 memcpy( &entry->wszName[MAX_INTERFACE_NAME_LEN],
747 &info.if_info,
748 sizeof(info.if_info) );
749 }
750
751 closeTcpFile( tcpFile );
752 }
753
754 return status;
755 }
756
757 DWORD getInterfaceEntryByIndex(DWORD index, PMIB_IFROW entry)
758 {
759 HANDLE tcpFile;
760 IFInfo info;
761 NTSTATUS status = openTcpFile( &tcpFile );
762
763 DPRINT("Called.\n");
764
765 if( NT_SUCCESS(status) ) {
766 status = getInterfaceInfoByIndex( tcpFile, index, &info );
767
768 if( NT_SUCCESS(status) ) {
769 memcpy( &entry->wszName[MAX_INTERFACE_NAME_LEN],
770 &info.if_info,
771 sizeof(info.if_info) );
772 }
773
774 closeTcpFile( tcpFile );
775 }
776
777 return status;
778 }
779
780 char *toIPAddressString(unsigned int addr, char string[16])
781 {
782 if (string) {
783 struct in_addr iAddr;
784
785 iAddr.s_addr = addr;
786 /* extra-anal, just to make auditors happy */
787 strncpy(string, inet_ntoa(iAddr), 16);
788 string[16] = '\0';
789 }
790 return string;
791 }
792
793 DWORD createIpForwardEntryOS( PMIB_IPFORWARDROW pRoute ) {
794 HANDLE tcpFile = INVALID_HANDLE_VALUE;
795 NTSTATUS status = openTcpFile( &tcpFile );
796 TCP_REQUEST_SET_INFORMATION_EX_SAFELY_SIZED req =
797 TCP_REQUEST_SET_INFORMATION_INIT;
798 IPRouteEntry *rte;
799 TDIEntityID id;
800 DWORD returnSize = 0;
801
802 DPRINT("Called.\n");
803
804 if( NT_SUCCESS(status) )
805 status = getNthIpEntity( tcpFile, 0, &id );
806
807 if( NT_SUCCESS(status) ) {
808 req.Req.ID.toi_class = INFO_CLASS_PROTOCOL;
809 req.Req.ID.toi_type = INFO_TYPE_PROVIDER;
810 req.Req.ID.toi_id = IP_MIB_ROUTETABLE_ENTRY_ID;
811 req.Req.ID.toi_entity = id;
812 req.Req.BufferSize = sizeof(*rte);
813 rte =
814 (IPRouteEntry *)&req.Req.Buffer[0];
815
816 rte->ire_dest = pRoute->dwForwardDest;
817 rte->ire_index = pRoute->dwForwardIfIndex;
818 rte->ire_metric = pRoute->dwForwardMetric1;
819 rte->ire_gw = pRoute->dwForwardNextHopAS;
820 rte->ire_mask = pRoute->dwForwardMask;
821
822 status = DeviceIoControl( tcpFile,
823 IOCTL_TCP_SET_INFORMATION_EX,
824 &req,
825 sizeof(req),
826 NULL,
827 0,
828 &returnSize,
829 NULL );
830 }
831
832 if( tcpFile != INVALID_HANDLE_VALUE )
833 closeTcpFile( tcpFile );
834
835 return status;
836 }