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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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"
41 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi
);
45 /* I'm a bit skittish about maintaining this info in memory, as I'd rather
46 * not add any mutex or critical section blockers to these functions. I've
47 * encountered far too many windows functions that contribute to deadlock
48 * by not announcing themselves. */
49 void interfaceMapInit(void)
51 /* For now, nothing */
54 void interfaceMapFree(void)
59 NTSTATUS tdiGetMibForIfEntity
60 ( HANDLE tcpFile
, TDIEntityID
*ent
, IFEntrySafelySized
*entry
) {
61 TCP_REQUEST_QUERY_INFORMATION_EX req
= TCP_REQUEST_QUERY_INFORMATION_INIT
;
62 NTSTATUS status
= STATUS_SUCCESS
;
65 WARN("TdiGetMibForIfEntity(tcpFile %x,entityId %x)\n",
66 (int)tcpFile
, (int)ent
->tei_instance
);
68 req
.ID
.toi_class
= INFO_CLASS_PROTOCOL
;
69 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
70 req
.ID
.toi_id
= IF_MIB_STATS_ID
;
71 req
.ID
.toi_entity
= *ent
;
73 status
= DeviceIoControl( tcpFile
,
74 IOCTL_TCP_QUERY_INFORMATION_EX
,
84 WARN("IOCTL Failed\n");
85 return STATUS_UNSUCCESSFUL
;
88 TRACE("TdiGetMibForIfEntity() => {\n"
89 " if_index ....................... %x\n"
90 " if_type ........................ %x\n"
91 " if_mtu ......................... %d\n"
92 " if_speed ....................... %x\n"
93 " if_physaddrlen ................. %d\n",
98 entry
->ent
.if_physaddrlen
);
99 TRACE(" if_physaddr .................... %02x:%02x:%02x:%02x:%02x:%02x\n"
100 " if_descr ....................... %s\n",
101 entry
->ent
.if_physaddr
[0] & 0xff,
102 entry
->ent
.if_physaddr
[1] & 0xff,
103 entry
->ent
.if_physaddr
[2] & 0xff,
104 entry
->ent
.if_physaddr
[3] & 0xff,
105 entry
->ent
.if_physaddr
[4] & 0xff,
106 entry
->ent
.if_physaddr
[5] & 0xff,
107 entry
->ent
.if_descr
);
108 TRACE("} status %08x\n",status
);
110 return STATUS_SUCCESS
;
113 BOOL
isInterface( TDIEntityID
*if_maybe
) {
115 if_maybe
->tei_entity
== IF_ENTITY
;
118 BOOL
isLoopback( HANDLE tcpFile
, TDIEntityID
*loop_maybe
) {
119 IFEntrySafelySized entryInfo
;
122 status
= tdiGetMibForIfEntity( tcpFile
,
126 return NT_SUCCESS(status
) &&
127 (entryInfo
.ent
.if_type
== IFENT_SOFTWARE_LOOPBACK
);
130 BOOL
hasArp( HANDLE tcpFile
, TDIEntityID
*arp_maybe
) {
131 TCP_REQUEST_QUERY_INFORMATION_EX req
= TCP_REQUEST_QUERY_INFORMATION_INIT
;
132 NTSTATUS status
= STATUS_SUCCESS
;
133 DWORD returnSize
, type
;
135 req
.ID
.toi_class
= INFO_CLASS_GENERIC
;
136 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
137 req
.ID
.toi_id
= ENTITY_TYPE_ID
;
138 req
.ID
.toi_entity
.tei_entity
= AT_ENTITY
;
139 req
.ID
.toi_entity
.tei_instance
= arp_maybe
->tei_instance
;
141 status
= DeviceIoControl( tcpFile
,
142 IOCTL_TCP_QUERY_INFORMATION_EX
,
149 if( !NT_SUCCESS(status
) ) return FALSE
;
151 return (type
& AT_ARP
);
154 static NTSTATUS
getInterfaceInfoSet( HANDLE tcpFile
,
156 PDWORD numInterfaces
) {
158 TDIEntityID
*entIDSet
= 0;
159 NTSTATUS status
= tdiGetEntityIDSet( tcpFile
, &entIDSet
, &numEntities
);
160 IFInfo
*infoSetInt
= 0;
161 int curInterf
= 0, i
;
163 if (!NT_SUCCESS(status
)) {
164 ERR("getInterfaceInfoSet: tdiGetEntityIDSet() failed: 0x%lx\n", status
);
168 infoSetInt
= HeapAlloc( GetProcessHeap(), 0,
169 sizeof(IFInfo
) * numEntities
);
172 for( i
= 0; i
< numEntities
; i
++ ) {
173 if( isInterface( &entIDSet
[i
] ) ) {
174 infoSetInt
[curInterf
].entity_id
= entIDSet
[i
];
175 status
= tdiGetMibForIfEntity
178 &infoSetInt
[curInterf
].if_info
);
179 TRACE("tdiGetMibForIfEntity: %08x\n", status
);
180 if( NT_SUCCESS(status
) ) {
186 status
= getNthIpEntity( tcpFile
, curInterf
, &ip_ent
);
187 if( NT_SUCCESS(status
) )
188 status
= tdiGetIpAddrsForIpEntity
189 ( tcpFile
, &ip_ent
, &addrs
, &numAddrs
);
190 for( j
= 0; j
< numAddrs
&& NT_SUCCESS(status
); j
++ ) {
191 TRACE("ADDR %d: index %d (target %d)\n", j
, addrs
[j
].iae_index
, infoSetInt
[curInterf
].if_info
.ent
.if_index
);
192 if( addrs
[j
].iae_index
==
193 infoSetInt
[curInterf
].if_info
.ent
.if_index
) {
194 memcpy( &infoSetInt
[curInterf
].ip_addr
,
196 sizeof( addrs
[j
] ) );
205 tdiFreeThingSet(entIDSet
);
207 if (NT_SUCCESS(status
)) {
208 *infoSet
= infoSetInt
;
209 *numInterfaces
= curInterf
;
211 HeapFree(GetProcessHeap(), 0, infoSetInt
);
216 return STATUS_INSUFFICIENT_RESOURCES
;
220 static DWORD
getNumInterfacesInt(BOOL onlyNonLoopback
)
222 DWORD numEntities
, numInterfaces
= 0;
223 TDIEntityID
*entitySet
;
228 status
= openTcpFile( &tcpFile
);
230 if( !NT_SUCCESS(status
) ) {
231 WARN("getNumInterfaces: failed %08x\n", status
);
235 status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
237 if( !NT_SUCCESS(status
) ) {
238 WARN("getNumInterfaces: failed %08x\n", status
);
239 closeTcpFile( tcpFile
);
243 for( i
= 0; i
< numEntities
; i
++ ) {
244 if( isInterface( &entitySet
[i
] ) &&
246 (onlyNonLoopback
&& !isLoopback( tcpFile
, &entitySet
[i
] ))) )
250 TRACE("getNumInterfaces: success: %d %d %08x\n",
251 onlyNonLoopback
, numInterfaces
, status
);
253 closeTcpFile( tcpFile
);
255 tdiFreeThingSet( entitySet
);
257 return numInterfaces
;
260 DWORD
getNumInterfaces(void)
262 return getNumInterfacesInt( FALSE
);
265 DWORD
getNumNonLoopbackInterfaces(void)
267 return getNumInterfacesInt( TRUE
);
270 DWORD
getNthInterfaceEntity( HANDLE tcpFile
, DWORD index
, TDIEntityID
*ent
) {
271 DWORD numEntities
= 0;
272 DWORD numInterfaces
= 0;
273 TDIEntityID
*entitySet
= 0;
274 NTSTATUS status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
277 if( !NT_SUCCESS(status
) )
280 for( i
= 0; i
< numEntities
; i
++ ) {
281 if( isInterface( &entitySet
[i
] ) ) {
282 if( numInterfaces
== index
) break;
283 else numInterfaces
++;
287 TRACE("Index %d is entity #%d - %04x:%08x\n", index
, i
,
288 entitySet
[i
].tei_entity
, entitySet
[i
].tei_instance
);
290 if( numInterfaces
== index
&& i
< numEntities
) {
291 memcpy( ent
, &entitySet
[i
], sizeof(*ent
) );
292 tdiFreeThingSet( entitySet
);
293 return STATUS_SUCCESS
;
295 tdiFreeThingSet( entitySet
);
296 return STATUS_UNSUCCESSFUL
;
300 NTSTATUS
getInterfaceInfoByIndex( HANDLE tcpFile
, DWORD index
, IFInfo
*info
) {
303 NTSTATUS status
= getInterfaceInfoSet( tcpFile
, &ifInfo
, &numInterfaces
);
306 if( NT_SUCCESS(status
) )
308 for( i
= 0; i
< numInterfaces
; i
++ ) {
309 if( ifInfo
[i
].if_info
.ent
.if_index
== index
) {
310 memcpy( info
, &ifInfo
[i
], sizeof(*info
) );
315 HeapFree(GetProcessHeap(), 0, ifInfo
);
317 return i
< numInterfaces
? STATUS_SUCCESS
: STATUS_UNSUCCESSFUL
;
323 NTSTATUS
getInterfaceInfoByName( HANDLE tcpFile
, char *name
, IFInfo
*info
) {
327 NTSTATUS status
= getInterfaceInfoSet( tcpFile
, &ifInfo
, &numInterfaces
);
329 if( NT_SUCCESS(status
) )
331 for( i
= 0; i
< numInterfaces
; i
++ ) {
332 if( !strcmp((PCHAR
)ifInfo
[i
].if_info
.ent
.if_descr
, name
) ) {
333 memcpy( info
, &ifInfo
[i
], sizeof(*info
) );
338 HeapFree(GetProcessHeap(), 0,ifInfo
);
340 return i
< numInterfaces
? STATUS_SUCCESS
: STATUS_UNSUCCESSFUL
;
346 /* Note that the result of this operation must be freed later */
348 const char *getInterfaceNameByIndex(DWORD index
)
352 char *interfaceName
= 0, *adapter_name
= 0;
353 NTSTATUS status
= openTcpFile( &tcpFile
);
355 if( NT_SUCCESS(status
) ) {
356 status
= getInterfaceInfoByIndex( tcpFile
, index
, &ifInfo
);
358 if( NT_SUCCESS(status
) ) {
359 adapter_name
= (char *)ifInfo
.if_info
.ent
.if_descr
;
361 interfaceName
= HeapAlloc( GetProcessHeap(), 0,
362 strlen(adapter_name
) + 1 );
363 if (!interfaceName
) return NULL
;
365 strcpy( interfaceName
, adapter_name
);
368 closeTcpFile( tcpFile
);
371 return interfaceName
;
374 void consumeInterfaceName(const char *name
) {
375 HeapFree( GetProcessHeap(), 0, (char *)name
);
378 DWORD
getInterfaceIndexByName(const char *name
, PDWORD index
)
382 NTSTATUS status
= openTcpFile( &tcpFile
);
384 if( NT_SUCCESS(status
) ) {
385 status
= getInterfaceInfoByName( tcpFile
, (char *)name
, &ifInfo
);
387 if( NT_SUCCESS(status
) ) {
388 *index
= ifInfo
.if_info
.ent
.if_index
;
391 closeTcpFile( tcpFile
);
397 InterfaceIndexTable
*getInterfaceIndexTableInt( BOOL nonLoopbackOnly
) {
398 DWORD numInterfaces
, curInterface
= 0;
401 InterfaceIndexTable
*ret
= 0;
403 NTSTATUS status
= openTcpFile( &tcpFile
);
405 if( NT_SUCCESS(status
) ) {
406 status
= getInterfaceInfoSet( tcpFile
, &ifInfo
, &numInterfaces
);
408 TRACE("InterfaceInfoSet: %08x, %04x:%08x\n",
410 ifInfo
->entity_id
.tei_entity
,
411 ifInfo
->entity_id
.tei_instance
);
413 if( NT_SUCCESS(status
) ) {
414 ret
= (InterfaceIndexTable
*)
416 sizeof(InterfaceIndexTable
) +
417 (numInterfaces
- 1) * sizeof(DWORD
));
420 ret
->numAllocated
= numInterfaces
;
421 TRACE("NumInterfaces = %d\n", numInterfaces
);
423 for( i
= 0; i
< numInterfaces
; i
++ ) {
424 TRACE("Examining interface %d\n", i
);
425 if( !nonLoopbackOnly
||
426 !isLoopback( tcpFile
, &ifInfo
[i
].entity_id
) ) {
427 TRACE("Interface %d matches (%d)\n", i
, curInterface
);
428 ret
->indexes
[curInterface
++] =
429 ifInfo
[i
].if_info
.ent
.if_index
;
433 ret
->numIndexes
= curInterface
;
436 tdiFreeThingSet( ifInfo
);
438 closeTcpFile( tcpFile
);
444 InterfaceIndexTable
*getInterfaceIndexTable(void) {
445 return getInterfaceIndexTableInt( FALSE
);
448 InterfaceIndexTable
*getNonLoopbackInterfaceIndexTable(void) {
449 return getInterfaceIndexTableInt( TRUE
);
452 DWORD
getInterfaceIPAddrByName(const char *name
)
457 NTSTATUS
getIPAddrEntryForIf(HANDLE tcpFile
,
463 getInterfaceInfoByName( tcpFile
, name
, ifInfo
) :
464 getInterfaceInfoByIndex( tcpFile
, index
, ifInfo
);
466 if (!NT_SUCCESS(status
)) {
467 ERR("getIPAddrEntryForIf returning %lx\n", status
);
473 DWORD
getAddrByIndexOrName( char *name
, DWORD index
, IPHLPAddrType addrType
) {
476 NTSTATUS status
= STATUS_SUCCESS
;
477 DWORD addrOut
= INADDR_ANY
;
479 status
= openTcpFile( &tcpFile
);
481 if( NT_SUCCESS(status
) ) {
482 status
= getIPAddrEntryForIf( tcpFile
, name
, index
, &ifInfo
);
483 if( NT_SUCCESS(status
) ) {
485 case IPAAddr
: addrOut
= ifInfo
.ip_addr
.iae_addr
; break;
486 case IPABcast
: addrOut
= ifInfo
.ip_addr
.iae_bcastaddr
; break;
487 case IPAMask
: addrOut
= ifInfo
.ip_addr
.iae_mask
; break;
488 case IFMtu
: addrOut
= ifInfo
.if_info
.ent
.if_mtu
; break;
489 case IFStatus
: addrOut
= ifInfo
.if_info
.ent
.if_operstatus
; break;
492 closeTcpFile( tcpFile
);
498 DWORD
getInterfaceIPAddrByIndex(DWORD index
) {
499 return getAddrByIndexOrName( 0, index
, IPAAddr
);
502 DWORD
getInterfaceBCastAddrByName(const char *name
) {
503 return getAddrByIndexOrName( (char *)name
, 0, IPABcast
);
506 DWORD
getInterfaceBCastAddrByIndex(DWORD index
) {
507 return getAddrByIndexOrName( 0, index
, IPABcast
);
510 DWORD
getInterfaceMaskByName(const char *name
) {
511 return getAddrByIndexOrName( (char *)name
, 0, IPAMask
);
514 DWORD
getInterfaceMaskByIndex(DWORD index
) {
515 return getAddrByIndexOrName( 0, index
, IPAMask
);
518 void getInterfacePhysicalFromInfo( IFInfo
*info
,
519 PDWORD len
, PBYTE addr
, PDWORD type
) {
520 *len
= info
->if_info
.ent
.if_physaddrlen
;
521 memcpy( addr
, info
->if_info
.ent
.if_physaddr
, *len
);
522 *type
= info
->if_info
.ent
.if_type
;
525 DWORD
getInterfacePhysicalByName(const char *name
, PDWORD len
, PBYTE addr
,
530 NTSTATUS status
= openTcpFile( &tcpFile
);
532 if( NT_SUCCESS(status
) ) {
533 status
= getInterfaceInfoByName( tcpFile
, (char *)name
, &info
);
534 if( NT_SUCCESS(status
) )
535 getInterfacePhysicalFromInfo( &info
, len
, addr
, type
);
536 closeTcpFile( tcpFile
);
542 DWORD
getInterfacePhysicalByIndex(DWORD index
, PDWORD len
, PBYTE addr
,
547 NTSTATUS status
= openTcpFile( &tcpFile
);
549 if( NT_SUCCESS(status
) ) {
550 status
= getInterfaceInfoByIndex( tcpFile
, index
, &info
);
551 if( NT_SUCCESS(status
) )
552 getInterfacePhysicalFromInfo( &info
, len
, addr
, type
);
553 closeTcpFile( tcpFile
);
559 DWORD
getInterfaceMtuByName(const char *name
, PDWORD mtu
) {
560 *mtu
= getAddrByIndexOrName( (char *)name
, 0, IFMtu
);
561 return STATUS_SUCCESS
;
564 DWORD
getInterfaceMtuByIndex(DWORD index
, PDWORD mtu
) {
565 *mtu
= getAddrByIndexOrName( 0, index
, IFMtu
);
566 return STATUS_SUCCESS
;
569 DWORD
getInterfaceStatusByName(const char *name
, PDWORD status
) {
570 *status
= getAddrByIndexOrName( (char *)name
, 0, IFStatus
);
571 return STATUS_SUCCESS
;
574 DWORD
getInterfaceStatusByIndex(DWORD index
, PDWORD status
)
576 *status
= getAddrByIndexOrName( 0, index
, IFStatus
);
577 return STATUS_SUCCESS
;
580 DWORD
getInterfaceEntryByName(const char *name
, PMIB_IFROW entry
)
584 NTSTATUS status
= openTcpFile( &tcpFile
);
588 if( NT_SUCCESS(status
) ) {
589 status
= getInterfaceInfoByName( tcpFile
, (char *)name
, &info
);
591 if( NT_SUCCESS(status
) ) {
592 memcpy( &entry
->wszName
[MAX_INTERFACE_NAME_LEN
],
594 sizeof(info
.if_info
) );
597 TRACE("entry->bDescr = %s\n", entry
->bDescr
);
599 closeTcpFile( tcpFile
);
605 DWORD
getInterfaceEntryByIndex(DWORD index
, PMIB_IFROW entry
)
609 NTSTATUS status
= openTcpFile( &tcpFile
);
613 if( NT_SUCCESS(status
) ) {
614 status
= getInterfaceInfoByIndex( tcpFile
, index
, &info
);
616 if( NT_SUCCESS(status
) ) {
617 memcpy( &entry
->wszName
[MAX_INTERFACE_NAME_LEN
],
619 sizeof(info
.if_info
) );
622 closeTcpFile( tcpFile
);
628 char *toIPAddressString(unsigned int addr
, char string
[16])
630 struct in_addr iAddr
;
635 strncpy(string
, inet_ntoa(iAddr
), 16);
637 return inet_ntoa(iAddr
);
640 NTSTATUS
addIPAddress( IPAddr Address
, IPMask Mask
, DWORD IfIndex
,
641 PULONG NteContext
, PULONG NteInstance
)
644 NTSTATUS status
= openTcpFile( &tcpFile
);
646 IO_STATUS_BLOCK Iosb
;
650 if( !NT_SUCCESS(status
) ) return status
;
652 Data
.NteContext
= IfIndex
;
653 Data
.NewAddress
= Address
;
654 Data
.NewNetmask
= Mask
;
656 status
= NtDeviceIoControlFile( tcpFile
,
661 IOCTL_SET_IP_ADDRESS
,
667 closeTcpFile( tcpFile
);
669 if( NT_SUCCESS(status
) ) {
670 *NteContext
= Iosb
.Information
;
671 *NteInstance
= Data
.NewAddress
;
674 if (!NT_SUCCESS(status
)) {
675 ERR("addIPAddress for if %d returning 0x%lx\n", IfIndex
, status
);
682 NTSTATUS
deleteIpAddress( ULONG NteContext
)
685 NTSTATUS status
= openTcpFile( &tcpFile
);
686 IO_STATUS_BLOCK Iosb
;
690 if( !NT_SUCCESS(status
) ) return status
;
692 status
= NtDeviceIoControlFile( tcpFile
,
697 IOCTL_DELETE_IP_ADDRESS
,
703 closeTcpFile( tcpFile
);
705 if (!NT_SUCCESS(status
)) {
706 ERR("deleteIpAddress(%lu) returning 0x%lx\n", NteContext
, status
);