1 /* Copyright (C) 2003 Art Yerkes
2 * A reimplementation of ifenum.c by Juan Lang
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.
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.
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
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.
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.
36 * - We don't support IPv6 addresses here yet -- I moved the upper edge
37 * functions into iphlpv6.c (arty)
39 #include "iphlpapi_private.h"
47 const PWCHAR TcpFileName
= L
"\\Device\\Tcp";
51 /* I'm a bit skittish about maintaining this info in memory, as I'd rather
52 * not add any mutex or critical section blockers to these functions. I've
53 * encountered far too many windows functions that contribute to deadlock
54 * by not announcing themselves. */
55 void interfaceMapInit(void)
57 /* For now, nothing */
60 void interfaceMapFree(void)
65 NTSTATUS
openTcpFile(PHANDLE tcpFile
) {
66 UNICODE_STRING fileName
;
67 OBJECT_ATTRIBUTES objectAttributes
;
68 IO_STATUS_BLOCK ioStatusBlock
;
73 /* Shamelessly ripped from CreateFileW */
74 RtlInitUnicodeString( &fileName
, TcpFileName
);
76 InitializeObjectAttributes( &objectAttributes
,
82 status
= ZwCreateFile( tcpFile
,
83 SYNCHRONIZE
| GENERIC_EXECUTE
|
84 GENERIC_READ
| GENERIC_WRITE
,
88 FILE_ATTRIBUTE_NORMAL
,
89 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
91 FILE_SYNCHRONOUS_IO_NONALERT
,
95 /* String does not need to be freed: it points to the constant
96 * string we provided */
98 if (!NT_SUCCESS(status
)) {
99 DPRINT1("openTcpFile for <%wZ> failed: 0x%lx\n", &fileName
, status
);
105 void closeTcpFile( HANDLE h
) {
110 /* A generic thing-getting function which interacts in the right way with
111 * TDI. This may seem oblique, but I'm using it to reduce code and hopefully
112 * make this thing easier to debug.
114 * The things returned can be any of:
122 NTSTATUS
tdiGetSetOfThings( HANDLE tcpFile
,
130 PDWORD numEntries
) {
131 TCP_REQUEST_QUERY_INFORMATION_EX req
= TCP_REQUEST_QUERY_INFORMATION_INIT
;
133 NTSTATUS status
= STATUS_SUCCESS
;
134 DWORD allocationSizeForEntityArray
= entrySize
* MAX_TDI_ENTITIES
,
135 arraySize
= entrySize
* MAX_TDI_ENTITIES
;
137 TRACE("TdiGetSetOfThings(tcpFile %x,toiClass %x,toiType %x,toiId %x,"
138 "teiEntity %x,fixedPart %d,entrySize %d)\n",
147 req
.ID
.toi_class
= toiClass
;
148 req
.ID
.toi_type
= toiType
;
149 req
.ID
.toi_id
= toiId
;
150 req
.ID
.toi_entity
.tei_entity
= teiEntity
;
152 /* There's a subtle problem here...
153 * If an interface is added at this exact instant, (as if by a PCMCIA
154 * card insertion), the array will still not have enough entries after
155 * have allocated it after the first DeviceIoControl call.
157 * We'll get around this by repeating until the number of interfaces
161 assert( !entitySet
); /* We must not have an entity set allocated */
162 status
= DeviceIoControl( tcpFile
,
163 IOCTL_TCP_QUERY_INFORMATION_EX
,
168 &allocationSizeForEntityArray
,
173 DPRINT("IOCTL Failed\n");
174 return STATUS_UNSUCCESSFUL
;
177 arraySize
= allocationSizeForEntityArray
;
178 entitySet
= HeapAlloc( GetProcessHeap(), 0, arraySize
);
181 status
= STATUS_INSUFFICIENT_RESOURCES
;
182 DPRINT("TdiGetSetOfThings() => %08x\n", (int)status
);
186 status
= DeviceIoControl( tcpFile
,
187 IOCTL_TCP_QUERY_INFORMATION_EX
,
192 &allocationSizeForEntityArray
,
195 /* This is why we have the loop -- we might have added an adapter */
196 if( arraySize
== allocationSizeForEntityArray
)
199 HeapFree( GetProcessHeap(), 0, entitySet
);
204 DPRINT("IOCTL Failed\n");
205 return STATUS_UNSUCCESSFUL
;
208 DPRINT("TdiGetSetOfThings(): Array changed size: %d -> %d.\n",
209 arraySize
, allocationSizeForEntityArray
);
210 } while( TRUE
); /* We break if the array we received was the size we
211 * expected. Therefore, we got here because it wasn't */
213 *numEntries
= (arraySize
- fixedPart
) / entrySize
;
214 *tdiEntitySet
= entitySet
;
216 DPRINT("TdiGetSetOfThings() => Success: %d things @ %08x\n",
217 (int)*numEntries
, (int)entitySet
);
219 return STATUS_SUCCESS
;
222 VOID
tdiFreeThingSet( PVOID things
) {
223 HeapFree( GetProcessHeap(), 0, things
);
226 NTSTATUS tdiGetMibForIfEntity
227 ( HANDLE tcpFile
, TDIEntityID
*ent
, IFEntrySafelySized
*entry
) {
228 TCP_REQUEST_QUERY_INFORMATION_EX req
= TCP_REQUEST_QUERY_INFORMATION_INIT
;
229 NTSTATUS status
= STATUS_SUCCESS
;
232 DPRINT("TdiGetMibForIfEntity(tcpFile %x,entityId %x)\n",
233 (int)tcpFile
, (int)ent
->tei_instance
);
235 req
.ID
.toi_class
= INFO_CLASS_PROTOCOL
;
236 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
237 req
.ID
.toi_id
= IF_MIB_STATS_ID
;
238 req
.ID
.toi_entity
= *ent
;
240 status
= DeviceIoControl( tcpFile
,
241 IOCTL_TCP_QUERY_INFORMATION_EX
,
251 DPRINT("IOCTL Failed\n");
252 return STATUS_UNSUCCESSFUL
;
255 DPRINT("TdiGetMibForIfEntity() => {\n"
256 " if_index ....................... %x\n"
257 " if_type ........................ %x\n"
258 " if_mtu ......................... %d\n"
259 " if_speed ....................... %x\n"
260 " if_physaddrlen ................. %d\n",
265 entry
->ent
.if_physaddrlen
);
266 DPRINT(" if_physaddr .................... %02x:%02x:%02x:%02x:%02x:%02x\n",
267 " if_descr ....................... %s\n",
268 entry
->ent
.if_physaddr
[0] & 0xff,
269 entry
->ent
.if_physaddr
[1] & 0xff,
270 entry
->ent
.if_physaddr
[2] & 0xff,
271 entry
->ent
.if_physaddr
[3] & 0xff,
272 entry
->ent
.if_physaddr
[4] & 0xff,
273 entry
->ent
.if_physaddr
[5] & 0xff,
274 entry
->ent
.if_descr
);
275 DPRINT("} status %08x\n",status
);
280 NTSTATUS
tdiGetEntityIDSet( HANDLE tcpFile
,
281 TDIEntityID
**entitySet
,
282 PDWORD numEntities
) {
283 NTSTATUS status
= tdiGetSetOfThings( tcpFile
,
292 if( NT_SUCCESS(status
) ) {
295 for( i
= 0; i
< *numEntities
; i
++ ) {
296 DPRINT("%-4d: %04x:%08x\n",
298 (*entitySet
)[i
].tei_entity
,
299 (*entitySet
)[i
].tei_instance
);
306 static BOOL
isInterface( TDIEntityID
*if_maybe
) {
308 if_maybe
->tei_entity
== IF_ENTITY
;
311 static BOOL
isLoopback( HANDLE tcpFile
, TDIEntityID
*loop_maybe
) {
312 IFEntrySafelySized entryInfo
;
315 status
= tdiGetMibForIfEntity( tcpFile
,
319 return NT_SUCCESS(status
) && (!entryInfo
.ent
.if_type
||
320 entryInfo
.ent
.if_type
== IFENT_SOFTWARE_LOOPBACK
);
323 NTSTATUS
tdiGetEntityType( HANDLE tcpFile
, TDIEntityID
*ent
, PULONG type
) {
324 TCP_REQUEST_QUERY_INFORMATION_EX req
= TCP_REQUEST_QUERY_INFORMATION_INIT
;
325 NTSTATUS status
= STATUS_SUCCESS
;
328 DPRINT("TdiGetEntityType(tcpFile %x,entityId %x)\n",
329 (DWORD
)tcpFile
, ent
->tei_instance
);
331 req
.ID
.toi_class
= INFO_CLASS_GENERIC
;
332 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
333 req
.ID
.toi_id
= ENTITY_TYPE_ID
;
334 req
.ID
.toi_entity
.tei_entity
= ent
->tei_entity
;
335 req
.ID
.toi_entity
.tei_instance
= ent
->tei_instance
;
337 status
= DeviceIoControl( tcpFile
,
338 IOCTL_TCP_QUERY_INFORMATION_EX
,
346 DPRINT("TdiGetEntityType() => %08x %08x\n", *type
, status
);
348 return (status
? STATUS_SUCCESS
: STATUS_UNSUCCESSFUL
);
351 static NTSTATUS
getInterfaceInfoSet( HANDLE tcpFile
,
353 PDWORD numInterfaces
) {
355 TDIEntityID
*entIDSet
= 0;
356 NTSTATUS status
= tdiGetEntityIDSet( tcpFile
, &entIDSet
, &numEntities
);
357 IFInfo
*infoSetInt
= 0;
358 BOOL interfaceInfoComplete
;
359 int curInterf
= 0, i
;
361 if (!NT_SUCCESS(status
)) {
362 DPRINT1("getInterfaceInfoSet: tdiGetEntityIDSet() failed: 0x%lx\n", status
);
366 infoSetInt
= HeapAlloc( GetProcessHeap(), 0,
367 sizeof(IFInfo
) * numEntities
);
370 for( i
= 0; i
< numEntities
; i
++ ) {
371 if( isInterface( &entIDSet
[i
] ) ) {
372 infoSetInt
[curInterf
].entity_id
= entIDSet
[i
];
373 status
= tdiGetMibForIfEntity
376 &infoSetInt
[curInterf
].if_info
);
377 DPRINT("tdiGetMibForIfEntity: %08x\n", status
);
378 if( NT_SUCCESS(status
) ) {
384 interfaceInfoComplete
= FALSE
;
385 status
= getNthIpEntity( tcpFile
, 0, &ip_ent
);
386 if( NT_SUCCESS(status
) )
387 status
= tdiGetIpAddrsForIpEntity
388 ( tcpFile
, &ip_ent
, &addrs
, &numAddrs
);
389 for( j
= 0; j
< numAddrs
&& NT_SUCCESS(status
); j
++ ) {
390 DPRINT("ADDR %d: index %d (target %d)\n", j
, addrs
[j
].iae_index
, infoSetInt
[curInterf
].if_info
.ent
.if_index
);
391 if( addrs
[j
].iae_index
==
392 infoSetInt
[curInterf
].if_info
.ent
.if_index
) {
393 memcpy( &infoSetInt
[curInterf
].ip_addr
,
395 sizeof( addrs
[j
] ) );
404 if (NT_SUCCESS(status
)) {
405 *infoSet
= infoSetInt
;
406 *numInterfaces
= curInterf
;
408 HeapFree(GetProcessHeap(), 0, infoSetInt
);
413 return STATUS_INSUFFICIENT_RESOURCES
;
417 static DWORD
getNumInterfacesInt(BOOL onlyNonLoopback
)
419 DWORD numEntities
, numInterfaces
= 0;
420 TDIEntityID
*entitySet
;
425 status
= openTcpFile( &tcpFile
);
427 if( !NT_SUCCESS(status
) ) {
428 DPRINT("getNumInterfaces: failed %08x\n", status
);
432 status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
434 if( !NT_SUCCESS(status
) ) {
435 DPRINT("getNumInterfaces: failed %08x\n", status
);
439 for( i
= 0; i
< numEntities
; i
++ ) {
440 if( isInterface( &entitySet
[i
] ) &&
442 (onlyNonLoopback
&& !isLoopback( tcpFile
, &entitySet
[i
] ))) )
446 DPRINT("getNumInterfaces: success: %d %d %08x\n",
447 onlyNonLoopback
, numInterfaces
, status
);
449 closeTcpFile( tcpFile
);
451 tdiFreeThingSet( entitySet
);
453 return numInterfaces
;
456 DWORD
getNumInterfaces(void)
458 return getNumInterfacesInt( FALSE
);
461 DWORD
getNumNonLoopbackInterfaces(void)
463 return getNumInterfacesInt( TRUE
);
466 DWORD
getNthInterfaceEntity( HANDLE tcpFile
, DWORD index
, TDIEntityID
*ent
) {
467 DWORD numEntities
= 0;
468 DWORD numInterfaces
= 0;
469 TDIEntityID
*entitySet
= 0;
470 NTSTATUS status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
473 if( !NT_SUCCESS(status
) )
476 for( i
= 0; i
< numEntities
; i
++ ) {
477 if( isInterface( &entitySet
[i
] ) ) {
478 if( numInterfaces
== index
) break;
479 else numInterfaces
++;
483 DPRINT("Index %d is entity #%d - %04x:%08x\n", index
, i
,
484 entitySet
[i
].tei_entity
, entitySet
[i
].tei_instance
);
486 if( numInterfaces
== index
&& i
< numEntities
) {
487 memcpy( ent
, &entitySet
[i
], sizeof(*ent
) );
488 tdiFreeThingSet( entitySet
);
489 return STATUS_SUCCESS
;
491 tdiFreeThingSet( entitySet
);
492 return STATUS_UNSUCCESSFUL
;
496 NTSTATUS
getInterfaceInfoByIndex( HANDLE tcpFile
, DWORD index
, IFInfo
*info
) {
499 NTSTATUS status
= getInterfaceInfoSet( tcpFile
, &ifInfo
, &numInterfaces
);
502 if( NT_SUCCESS(status
) )
503 for( i
= 0; i
< numInterfaces
; i
++ ) {
504 if( ifInfo
[i
].if_info
.ent
.if_index
== index
) {
505 memcpy( info
, &ifInfo
[i
], sizeof(*info
) );
510 if( NT_SUCCESS(status
) )
511 return i
< numInterfaces
? STATUS_SUCCESS
: STATUS_UNSUCCESSFUL
;
516 NTSTATUS
getInterfaceInfoByName( HANDLE tcpFile
, char *name
, IFInfo
*info
) {
520 NTSTATUS status
= getInterfaceInfoSet( tcpFile
, &ifInfo
, &numInterfaces
);
522 if( NT_SUCCESS(status
) )
523 for( i
= 0; i
< numInterfaces
; i
++ ) {
524 if( !strcmp(ifInfo
[i
].if_info
.ent
.if_descr
, name
) ) {
525 memcpy( info
, &ifInfo
[i
], sizeof(*info
) );
530 if( NT_SUCCESS(status
) )
531 return i
< numInterfaces
? STATUS_SUCCESS
: STATUS_UNSUCCESSFUL
;
536 /* Note that the result of this operation must be freed later */
538 const char *getInterfaceNameByIndex(DWORD index
)
542 char *interfaceName
= 0, *adapter_name
= 0;
543 NTSTATUS status
= openTcpFile( &tcpFile
);
545 if( NT_SUCCESS(status
) ) {
546 status
= getInterfaceInfoByIndex( tcpFile
, index
, &ifInfo
);
548 if( NT_SUCCESS(status
) ) {
549 adapter_name
= ifInfo
.if_info
.ent
.if_descr
;
551 interfaceName
= HeapAlloc( GetProcessHeap(), 0,
552 strlen(adapter_name
) + 1 );
553 strcpy( interfaceName
, adapter_name
);
555 closeTcpFile( tcpFile
);
559 return interfaceName
;
562 void consumeInterfaceName(const char *name
) {
563 HeapFree( GetProcessHeap(), 0, (char *)name
);
566 DWORD
getInterfaceIndexByName(const char *name
, PDWORD index
)
570 NTSTATUS status
= openTcpFile( &tcpFile
);
572 if( NT_SUCCESS(status
) ) {
573 status
= getInterfaceInfoByName( tcpFile
, (char *)name
, &ifInfo
);
575 if( NT_SUCCESS(status
) ) {
576 *index
= ifInfo
.if_info
.ent
.if_index
;
577 closeTcpFile( tcpFile
);
584 InterfaceIndexTable
*getInterfaceIndexTableInt( BOOL nonLoopbackOnly
) {
585 DWORD numInterfaces
, curInterface
= 0;
588 InterfaceIndexTable
*ret
= 0;
590 NTSTATUS status
= openTcpFile( &tcpFile
);
592 if( NT_SUCCESS(status
) ) {
593 status
= getInterfaceInfoSet( tcpFile
, &ifInfo
, &numInterfaces
);
595 DPRINT("InterfaceInfoSet: %08x, %04x:%08x\n",
597 ifInfo
->entity_id
.tei_entity
,
598 ifInfo
->entity_id
.tei_instance
);
600 if( NT_SUCCESS(status
) ) {
601 ret
= (InterfaceIndexTable
*)
603 sizeof(InterfaceIndexTable
) +
604 (numInterfaces
- 1) * sizeof(DWORD
));
607 ret
->numAllocated
= numInterfaces
;
608 DPRINT("NumInterfaces = %d\n", numInterfaces
);
610 for( i
= 0; i
< numInterfaces
; i
++ ) {
611 DPRINT("Examining interface %d\n", i
);
612 if( !nonLoopbackOnly
||
613 !isLoopback( tcpFile
, &ifInfo
[i
].entity_id
) ) {
614 DPRINT("Interface %d matches (%d)\n", i
, curInterface
);
615 ret
->indexes
[curInterface
++] =
616 ifInfo
[i
].if_info
.ent
.if_index
;
620 ret
->numIndexes
= curInterface
;
623 tdiFreeThingSet( ifInfo
);
625 closeTcpFile( tcpFile
);
631 InterfaceIndexTable
*getInterfaceIndexTable(void) {
632 return getInterfaceIndexTableInt( FALSE
);
635 InterfaceIndexTable
*getNonLoopbackInterfaceIndexTable(void) {
636 return getInterfaceIndexTableInt( TRUE
);
639 DWORD
getInterfaceIPAddrByName(const char *name
)
644 NTSTATUS
getIPAddrEntryForIf(HANDLE tcpFile
,
650 getInterfaceInfoByName( tcpFile
, name
, ifInfo
) :
651 getInterfaceInfoByIndex( tcpFile
, index
, ifInfo
);
653 if (!NT_SUCCESS(status
)) {
654 DPRINT1("getIPAddrEntryForIf returning %lx\n", status
);
660 DWORD
getAddrByIndexOrName( char *name
, DWORD index
, IPHLPAddrType addrType
) {
662 HANDLE tcpFile
= INVALID_HANDLE_VALUE
;
663 NTSTATUS status
= STATUS_SUCCESS
;
664 DWORD addrOut
= INADDR_ANY
;
666 status
= openTcpFile( &tcpFile
);
668 if( NT_SUCCESS(status
) ) {
669 status
= getIPAddrEntryForIf( tcpFile
, name
, index
, &ifInfo
);
670 if( NT_SUCCESS(status
) ) {
672 case IPAAddr
: addrOut
= ifInfo
.ip_addr
.iae_addr
; break;
673 case IPABcast
: addrOut
= ifInfo
.ip_addr
.iae_bcastaddr
; break;
674 case IPAMask
: addrOut
= ifInfo
.ip_addr
.iae_mask
; break;
675 case IFMtu
: addrOut
= ifInfo
.if_info
.ent
.if_mtu
; break;
676 case IFStatus
: addrOut
= ifInfo
.if_info
.ent
.if_operstatus
; break;
679 closeTcpFile( &tcpFile
);
685 DWORD
getInterfaceIPAddrByIndex(DWORD index
) {
686 return getAddrByIndexOrName( 0, index
, IPAAddr
);
689 DWORD
getInterfaceBCastAddrByName(const char *name
) {
690 return getAddrByIndexOrName( (char *)name
, 0, IPABcast
);
693 DWORD
getInterfaceBCastAddrByIndex(DWORD index
) {
694 return getAddrByIndexOrName( 0, index
, IPABcast
);
697 DWORD
getInterfaceMaskByName(const char *name
) {
698 return getAddrByIndexOrName( (char *)name
, 0, IPAMask
);
701 DWORD
getInterfaceMaskByIndex(DWORD index
) {
702 return getAddrByIndexOrName( 0, index
, IPAMask
);
705 void getInterfacePhysicalFromInfo( IFInfo
*info
,
706 PDWORD len
, PBYTE addr
, PDWORD type
) {
707 *len
= info
->if_info
.ent
.if_physaddrlen
;
708 memcpy( addr
, info
->if_info
.ent
.if_physaddr
, *len
);
709 *type
= info
->if_info
.ent
.if_type
;
712 DWORD
getInterfacePhysicalByName(const char *name
, PDWORD len
, PBYTE addr
,
717 NTSTATUS status
= openTcpFile( &tcpFile
);
719 if( NT_SUCCESS(status
) ) {
720 status
= getInterfaceInfoByName( tcpFile
, (char *)name
, &info
);
721 if( NT_SUCCESS(status
) )
722 getInterfacePhysicalFromInfo( &info
, len
, addr
, type
);
723 closeTcpFile( tcpFile
);
729 DWORD
getInterfacePhysicalByIndex(DWORD index
, PDWORD len
, PBYTE addr
,
734 NTSTATUS status
= openTcpFile( &tcpFile
);
736 if( NT_SUCCESS(status
) ) {
737 status
= getInterfaceInfoByIndex( tcpFile
, index
, &info
);
738 if( NT_SUCCESS(status
) )
739 getInterfacePhysicalFromInfo( &info
, len
, addr
, type
);
740 closeTcpFile( tcpFile
);
746 DWORD
getInterfaceMtuByName(const char *name
, PDWORD mtu
) {
747 *mtu
= getAddrByIndexOrName( (char *)name
, 0, IFMtu
);
748 return STATUS_SUCCESS
;
751 DWORD
getInterfaceMtuByIndex(DWORD index
, PDWORD mtu
) {
752 *mtu
= getAddrByIndexOrName( 0, index
, IFMtu
);
753 return STATUS_SUCCESS
;
756 DWORD
getInterfaceStatusByName(const char *name
, PDWORD status
) {
757 *status
= getAddrByIndexOrName( (char *)name
, 0, IFStatus
);
758 return STATUS_SUCCESS
;
761 DWORD
getInterfaceStatusByIndex(DWORD index
, PDWORD status
)
763 *status
= getAddrByIndexOrName( 0, index
, IFStatus
);
764 return STATUS_SUCCESS
;
767 DWORD
getInterfaceEntryByName(const char *name
, PMIB_IFROW entry
)
771 NTSTATUS status
= openTcpFile( &tcpFile
);
775 if( NT_SUCCESS(status
) ) {
776 status
= getInterfaceInfoByName( tcpFile
, (char *)name
, &info
);
778 if( NT_SUCCESS(status
) ) {
779 memcpy( &entry
->wszName
[MAX_INTERFACE_NAME_LEN
],
781 sizeof(info
.if_info
) );
784 DPRINT("entry->bDescr = %s\n", entry
->bDescr
);
786 closeTcpFile( tcpFile
);
792 DWORD
getInterfaceEntryByIndex(DWORD index
, PMIB_IFROW entry
)
796 NTSTATUS status
= openTcpFile( &tcpFile
);
800 if( NT_SUCCESS(status
) ) {
801 status
= getInterfaceInfoByIndex( tcpFile
, index
, &info
);
803 if( NT_SUCCESS(status
) ) {
804 memcpy( &entry
->wszName
[MAX_INTERFACE_NAME_LEN
],
806 sizeof(info
.if_info
) );
809 closeTcpFile( tcpFile
);
815 char *toIPAddressString(unsigned int addr
, char string
[16])
818 struct in_addr iAddr
;
821 /* extra-anal, just to make auditors happy */
822 strncpy(string
, inet_ntoa(iAddr
), 16);
828 NTSTATUS
addIPAddress( IPAddr Address
, IPMask Mask
, DWORD IfIndex
,
829 PULONG NteContext
, PULONG NteInstance
)
832 NTSTATUS status
= openTcpFile( &tcpFile
);
834 IO_STATUS_BLOCK Iosb
;
838 if( !NT_SUCCESS(status
) ) return status
;
840 Data
.NteContext
= IfIndex
;
841 Data
.NewAddress
= Address
;
842 Data
.NewNetmask
= Mask
;
844 status
= NtDeviceIoControlFile( tcpFile
,
849 IOCTL_SET_IP_ADDRESS
,
855 closeTcpFile( tcpFile
);
857 if( NT_SUCCESS(status
) ) {
858 *NteContext
= Iosb
.Information
;
859 *NteInstance
= Data
.NewAddress
;
862 if (!NT_SUCCESS(status
)) {
863 DPRINT1("addIPAddress for if %d returning 0x%lx\n", IfIndex
, status
);
867 case STATUS_SUCCESS
: return ERROR_SUCCESS
;
868 case STATUS_DEVICE_DOES_NOT_EXIST
: return ERROR_DEV_NOT_EXIST
;
869 default: return status
;
873 NTSTATUS
deleteIpAddress( ULONG NteContext
)
876 NTSTATUS status
= openTcpFile( &tcpFile
);
877 IO_STATUS_BLOCK Iosb
;
881 if( !NT_SUCCESS(status
) ) return status
;
883 status
= NtDeviceIoControlFile( tcpFile
,
888 IOCTL_DELETE_IP_ADDRESS
,
894 closeTcpFile( tcpFile
);
896 if (!NT_SUCCESS(status
)) {
897 DPRINT1("deleteIpAddress(%lu) returning 0x%lx\n", NteContext
, status
);
901 if( NT_SUCCESS(status
) )
902 return ERROR_SUCCESS
;
904 return ERROR_GEN_FAILURE
;