route add and route delete now work on real windows with our iphlpapi.
[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 = ZwCreateFile( 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 DPRINT("tdiGetMibForIfEntity: %08x\n", status);
363 if( NT_SUCCESS(status) ) {
364 DWORD numAddrs;
365 IPAddrEntry *addrs;
366 TDIEntityID ip_ent;
367 int j,k;
368
369 interfaceInfoComplete = FALSE;
370 status = getNthIpEntity( tcpFile, 0, &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 DPRINT("ADDR %d: index %d (target %d)\n", k, addrs[k].iae_index, infoSetInt[curInterf].if_info.ent.if_index);
376 if( addrs[k].iae_index ==
377 infoSetInt[curInterf].if_info.ent.if_index ) {
378 memcpy( &infoSetInt[curInterf].ip_addr,
379 &addrs[k],
380 sizeof( addrs[k] ) );
381 curInterf++;
382 break;
383 }
384 }
385 }
386 }
387 }
388 }
389
390 *infoSet = infoSetInt;
391 *numInterfaces = curInterf;
392
393 return STATUS_SUCCESS;
394 }
395
396 static DWORD getNumInterfacesInt(BOOL onlyLoopback)
397 {
398 DWORD numEntities, numInterfaces = 0;
399 TDIEntityID *entitySet;
400 HANDLE tcpFile;
401 NTSTATUS status;
402 int i;
403
404 status = openTcpFile( &tcpFile );
405
406 if( !NT_SUCCESS(status) ) {
407 DPRINT("getNumInterfaces: failed %08x\n", status );
408 return 0;
409 }
410
411 status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
412
413 if( !NT_SUCCESS(status) ) {
414 DPRINT("getNumInterfaces: failed %08x\n", status );
415 return 0;
416 }
417
418 closeTcpFile( tcpFile );
419
420 for( i = 0; i < numEntities; i++ ) {
421 if( isInterface( &entitySet[i] ) &&
422 (!onlyLoopback || isLoopback( tcpFile, &entitySet[i] )) )
423 numInterfaces++;
424 }
425
426 DPRINT("getNumInterfaces: success: %d %d %08x\n",
427 onlyLoopback, numInterfaces, status );
428
429 tdiFreeThingSet( entitySet );
430
431 return numInterfaces;
432 }
433
434 DWORD getNumInterfaces(void)
435 {
436 return getNumInterfacesInt( FALSE );
437 }
438
439 DWORD getNumNonLoopbackInterfaces(void)
440 {
441 return getNumInterfacesInt( TRUE );
442 }
443
444 DWORD getNthInterfaceEntity( HANDLE tcpFile, DWORD index, TDIEntityID *ent ) {
445 DWORD numEntities = 0;
446 DWORD numInterfaces = 0;
447 TDIEntityID *entitySet = 0;
448 NTSTATUS status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
449 int i;
450
451 if( !NT_SUCCESS(status) )
452 return status;
453
454 for( i = 0; i < numEntities; i++ ) {
455 if( isInterface( &entitySet[i] ) ) {
456 if( numInterfaces == index ) break;
457 else numInterfaces++;
458 }
459 }
460
461 DPRINT("Index %d is entity #%d - %04x:%08x\n", index, i,
462 entitySet[i].tei_entity, entitySet[i].tei_instance );
463
464 if( numInterfaces == index && i < numEntities ) {
465 memcpy( ent, &entitySet[i], sizeof(*ent) );
466 tdiFreeThingSet( entitySet );
467 return STATUS_SUCCESS;
468 } else {
469 tdiFreeThingSet( entitySet );
470 return STATUS_UNSUCCESSFUL;
471 }
472 }
473
474 NTSTATUS getInterfaceInfoByIndex( HANDLE tcpFile, DWORD index, IFInfo *info ) {
475 IFInfo *ifInfo;
476 DWORD numInterfaces;
477 NTSTATUS status = getInterfaceInfoSet( tcpFile, &ifInfo, &numInterfaces );
478 int i;
479
480 if( NT_SUCCESS(status) )
481 for( i = 0; i < numInterfaces; i++ ) {
482 if( ifInfo[i].if_info.ent.if_index == index ) {
483 memcpy( info, &ifInfo[i], sizeof(*info) );
484 break;
485 }
486 }
487
488 if( NT_SUCCESS(status) )
489 return i < numInterfaces ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
490 else
491 return status;
492 }
493
494 NTSTATUS getInterfaceInfoByName( HANDLE tcpFile, char *name, IFInfo *info ) {
495 IFInfo *ifInfo;
496 DWORD numInterfaces;
497 int i;
498 NTSTATUS status = getInterfaceInfoSet( tcpFile, &ifInfo, &numInterfaces );
499
500 if( NT_SUCCESS(status) )
501 for( i = 0; i < numInterfaces; i++ ) {
502 if( !strcmp(ifInfo[i].if_info.ent.if_descr, name) ) {
503 memcpy( info, &ifInfo[i], sizeof(*info) );
504 break;
505 }
506 }
507
508 if( NT_SUCCESS(status) )
509 return i < numInterfaces ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
510 else
511 return status;
512 }
513
514 /* Note that the result of this operation must be freed later */
515
516 const char *getInterfaceNameByIndex(DWORD index)
517 {
518 IFInfo ifInfo;
519 HANDLE tcpFile;
520 char *interfaceName = 0, *adapter_name = 0;
521 NTSTATUS status = openTcpFile( &tcpFile );
522
523 if( NT_SUCCESS(status) ) {
524 status = getInterfaceInfoByIndex( tcpFile, index, &ifInfo );
525
526 if( NT_SUCCESS(status) ) {
527 adapter_name = ifInfo.if_info.ent.if_descr;
528
529 interfaceName = HeapAlloc( GetProcessHeap(), 0,
530 strlen(adapter_name) + 1 );
531 strcpy( interfaceName, adapter_name );
532
533 closeTcpFile( tcpFile );
534 }
535 }
536
537 return interfaceName;
538 }
539
540 void consumeInterfaceName(const char *name) {
541 HeapFree( GetProcessHeap(), 0, (char *)name );
542 }
543
544 DWORD getInterfaceIndexByName(const char *name, PDWORD index)
545 {
546 IFInfo ifInfo;
547 HANDLE tcpFile;
548 NTSTATUS status = openTcpFile( &tcpFile );
549
550 if( NT_SUCCESS(status) ) {
551 status = getInterfaceInfoByName( tcpFile, (char *)name, &ifInfo );
552
553 if( NT_SUCCESS(status) ) {
554 *index = ifInfo.if_info.ent.if_index;
555 closeTcpFile( tcpFile );
556 }
557 }
558
559 return status;
560 }
561
562 InterfaceIndexTable *getInterfaceIndexTableInt( BOOL nonLoopbackOnly ) {
563 DWORD numInterfaces, curInterface = 0;
564 int i;
565 IFInfo *ifInfo;
566 InterfaceIndexTable *ret = 0;
567 HANDLE tcpFile;
568 NTSTATUS status = openTcpFile( &tcpFile );
569
570 if( NT_SUCCESS(status) ) {
571 status = getInterfaceInfoSet( tcpFile, &ifInfo, &numInterfaces );
572
573 DPRINT("InterfaceInfoSet: %08x, %04x:%08x\n",
574 status,
575 ifInfo->entity_id.tei_entity,
576 ifInfo->entity_id.tei_instance);
577
578 if( NT_SUCCESS(status) ) {
579 ret = (InterfaceIndexTable *)
580 calloc(1,
581 sizeof(InterfaceIndexTable) +
582 (numInterfaces - 1) * sizeof(DWORD));
583
584 if (ret) {
585 ret->numAllocated = numInterfaces;
586 DPRINT("NumInterfaces = %d\n", numInterfaces);
587
588 for( i = 0; i < numInterfaces; i++ ) {
589 DPRINT("Examining interface %d\n", i);
590 if( !nonLoopbackOnly ||
591 !isLoopback( tcpFile, &ifInfo[i].entity_id ) ) {
592 DPRINT("Interface %d matches (%d)\n", i, curInterface);
593 ret->indexes[curInterface++] =
594 ifInfo[i].if_info.ent.if_index;
595 }
596 }
597
598 ret->numIndexes = curInterface;
599 }
600
601 tdiFreeThingSet( ifInfo );
602 }
603 closeTcpFile( tcpFile );
604 }
605
606 return ret;
607 }
608
609 InterfaceIndexTable *getInterfaceIndexTable(void) {
610 return getInterfaceIndexTableInt( FALSE );
611 }
612
613 InterfaceIndexTable *getNonLoopbackInterfaceIndexTable(void) {
614 return getInterfaceIndexTableInt( TRUE );
615 }
616
617 DWORD getInterfaceIPAddrByName(const char *name)
618 {
619 return INADDR_ANY;
620 }
621
622 NTSTATUS getIPAddrEntryForIf(HANDLE tcpFile,
623 char *name,
624 DWORD index,
625 IFInfo *ifInfo) {
626 NTSTATUS status =
627 name ?
628 getInterfaceInfoByName( tcpFile, name, ifInfo ) :
629 getInterfaceInfoByIndex( tcpFile, index, ifInfo );
630 return status;
631 }
632
633 DWORD getAddrByIndexOrName( char *name, DWORD index, IPHLPAddrType addrType ) {
634 IFInfo ifInfo;
635 HANDLE tcpFile = INVALID_HANDLE_VALUE;
636 NTSTATUS status = STATUS_SUCCESS;
637 DWORD addrOut = INADDR_ANY;
638
639 status = openTcpFile( &tcpFile );
640
641 if( NT_SUCCESS(status) ) {
642 status = getIPAddrEntryForIf( tcpFile, name, index, &ifInfo );
643 if( NT_SUCCESS(status) ) {
644 switch( addrType ) {
645 case IPAAddr: addrOut = ifInfo.ip_addr.iae_addr; break;
646 case IPABcast: addrOut = ifInfo.ip_addr.iae_bcastaddr; break;
647 case IPAMask: addrOut = ifInfo.ip_addr.iae_mask; break;
648 case IFMtu: addrOut = ifInfo.if_info.ent.if_mtu; break;
649 case IFStatus: addrOut = ifInfo.if_info.ent.if_operstatus; break;
650 }
651 }
652 closeTcpFile( &tcpFile );
653 }
654
655 return addrOut;
656 }
657
658 DWORD getInterfaceIPAddrByIndex(DWORD index) {
659 return getAddrByIndexOrName( 0, index, IPAAddr );
660 }
661
662 DWORD getInterfaceBCastAddrByName(const char *name) {
663 return getAddrByIndexOrName( (char *)name, 0, IPABcast );
664 }
665
666 DWORD getInterfaceBCastAddrByIndex(DWORD index) {
667 return getAddrByIndexOrName( 0, index, IPABcast );
668 }
669
670 DWORD getInterfaceMaskByName(const char *name) {
671 return getAddrByIndexOrName( (char *)name, 0, IPAMask );
672 }
673
674 DWORD getInterfaceMaskByIndex(DWORD index) {
675 return getAddrByIndexOrName( 0, index, IPAMask );
676 }
677
678 void getInterfacePhysicalFromInfo( IFInfo *info,
679 PDWORD len, PBYTE addr, PDWORD type ) {
680 *len = info->if_info.ent.if_physaddrlen;
681 memcpy( addr, info->if_info.ent.if_physaddr, *len );
682 *type = info->if_info.ent.if_type;
683 }
684
685 DWORD getInterfacePhysicalByName(const char *name, PDWORD len, PBYTE addr,
686 PDWORD type)
687 {
688 HANDLE tcpFile;
689 IFInfo info;
690 NTSTATUS status = openTcpFile( &tcpFile );
691
692 if( NT_SUCCESS(status) ) {
693 status = getInterfaceInfoByName( tcpFile, (char *)name, &info );
694 if( NT_SUCCESS(status) )
695 getInterfacePhysicalFromInfo( &info, len, addr, type );
696 closeTcpFile( tcpFile );
697 }
698
699 return status;
700 }
701
702 DWORD getInterfacePhysicalByIndex(DWORD index, PDWORD len, PBYTE addr,
703 PDWORD type)
704 {
705 HANDLE tcpFile;
706 IFInfo info;
707 NTSTATUS status = openTcpFile( &tcpFile );
708
709 if( NT_SUCCESS(status) ) {
710 status = getInterfaceInfoByIndex( tcpFile, index, &info );
711 if( NT_SUCCESS(status) )
712 getInterfacePhysicalFromInfo( &info, len, addr, type );
713 closeTcpFile( tcpFile );
714 }
715
716 return status;
717 }
718
719 DWORD getInterfaceMtuByName(const char *name, PDWORD mtu) {
720 *mtu = getAddrByIndexOrName( (char *)name, 0, IFMtu );
721 return STATUS_SUCCESS;
722 }
723
724 DWORD getInterfaceMtuByIndex(DWORD index, PDWORD mtu) {
725 *mtu = getAddrByIndexOrName( 0, index, IFMtu );
726 return STATUS_SUCCESS;
727 }
728
729 DWORD getInterfaceStatusByName(const char *name, PDWORD status) {
730 *status = getAddrByIndexOrName( (char *)name, 0, IFStatus );
731 return STATUS_SUCCESS;
732 }
733
734 DWORD getInterfaceStatusByIndex(DWORD index, PDWORD status)
735 {
736 *status = getAddrByIndexOrName( 0, index, IFStatus );
737 return STATUS_SUCCESS;
738 }
739
740 DWORD getInterfaceEntryByName(const char *name, PMIB_IFROW entry)
741 {
742 HANDLE tcpFile;
743 IFInfo info;
744 NTSTATUS status = openTcpFile( &tcpFile );
745
746 DPRINT("Called.\n");
747
748 if( NT_SUCCESS(status) ) {
749 status = getInterfaceInfoByName( tcpFile, (char *)name, &info );
750
751 if( NT_SUCCESS(status) ) {
752 memcpy( &entry->wszName[MAX_INTERFACE_NAME_LEN],
753 &info.if_info,
754 sizeof(info.if_info) );
755 }
756
757 closeTcpFile( tcpFile );
758 }
759
760 return status;
761 }
762
763 DWORD getInterfaceEntryByIndex(DWORD index, PMIB_IFROW entry)
764 {
765 HANDLE tcpFile;
766 IFInfo info;
767 NTSTATUS status = openTcpFile( &tcpFile );
768
769 DPRINT("Called.\n");
770
771 if( NT_SUCCESS(status) ) {
772 status = getInterfaceInfoByIndex( tcpFile, index, &info );
773
774 if( NT_SUCCESS(status) ) {
775 memcpy( &entry->wszName[MAX_INTERFACE_NAME_LEN],
776 &info.if_info,
777 sizeof(info.if_info) );
778 }
779
780 closeTcpFile( tcpFile );
781 }
782
783 return status;
784 }
785
786 char *toIPAddressString(unsigned int addr, char string[16])
787 {
788 if (string) {
789 struct in_addr iAddr;
790
791 iAddr.s_addr = addr;
792 /* extra-anal, just to make auditors happy */
793 strncpy(string, inet_ntoa(iAddr), 16);
794 string[16] = '\0';
795 }
796 return string;
797 }