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"
46 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi
);
50 /* I'm a bit skittish about maintaining this info in memory, as I'd rather
51 * not add any mutex or critical section blockers to these functions. I've
52 * encountered far too many windows functions that contribute to deadlock
53 * by not announcing themselves. */
54 void interfaceMapInit(void)
56 /* For now, nothing */
59 void interfaceMapFree(void)
64 NTSTATUS tdiGetMibForIfEntity
65 ( HANDLE tcpFile
, TDIEntityID
*ent
, IFEntrySafelySized
*entry
) {
66 TCP_REQUEST_QUERY_INFORMATION_EX req
= TCP_REQUEST_QUERY_INFORMATION_INIT
;
67 NTSTATUS status
= STATUS_SUCCESS
;
70 WARN("TdiGetMibForIfEntity(tcpFile %x,entityId %x)\n",
71 (int)tcpFile
, (int)ent
->tei_instance
);
73 req
.ID
.toi_class
= INFO_CLASS_PROTOCOL
;
74 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
75 req
.ID
.toi_id
= IF_MIB_STATS_ID
;
76 req
.ID
.toi_entity
= *ent
;
78 status
= DeviceIoControl( tcpFile
,
79 IOCTL_TCP_QUERY_INFORMATION_EX
,
89 WARN("IOCTL Failed\n");
90 return STATUS_UNSUCCESSFUL
;
93 TRACE("TdiGetMibForIfEntity() => {\n"
94 " if_index ....................... %x\n"
95 " if_type ........................ %x\n"
96 " if_mtu ......................... %d\n"
97 " if_speed ....................... %x\n"
98 " if_physaddrlen ................. %d\n",
103 entry
->ent
.if_physaddrlen
);
104 TRACE(" if_physaddr .................... %02x:%02x:%02x:%02x:%02x:%02x\n"
105 " if_descr ....................... %s\n",
106 entry
->ent
.if_physaddr
[0] & 0xff,
107 entry
->ent
.if_physaddr
[1] & 0xff,
108 entry
->ent
.if_physaddr
[2] & 0xff,
109 entry
->ent
.if_physaddr
[3] & 0xff,
110 entry
->ent
.if_physaddr
[4] & 0xff,
111 entry
->ent
.if_physaddr
[5] & 0xff,
112 entry
->ent
.if_descr
);
113 TRACE("} status %08x\n",status
);
115 return STATUS_SUCCESS
;
118 BOOL
isInterface( TDIEntityID
*if_maybe
) {
120 if_maybe
->tei_entity
== IF_ENTITY
;
123 BOOL
isLoopback( HANDLE tcpFile
, TDIEntityID
*loop_maybe
) {
124 IFEntrySafelySized entryInfo
;
127 status
= tdiGetMibForIfEntity( tcpFile
,
131 return NT_SUCCESS(status
) &&
132 (entryInfo
.ent
.if_type
== IFENT_SOFTWARE_LOOPBACK
);
135 BOOL
hasArp( HANDLE tcpFile
, TDIEntityID
*arp_maybe
) {
136 TCP_REQUEST_QUERY_INFORMATION_EX req
= TCP_REQUEST_QUERY_INFORMATION_INIT
;
137 NTSTATUS status
= STATUS_SUCCESS
;
138 DWORD returnSize
, type
;
140 req
.ID
.toi_class
= INFO_CLASS_GENERIC
;
141 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
142 req
.ID
.toi_id
= ENTITY_TYPE_ID
;
143 req
.ID
.toi_entity
.tei_entity
= AT_ENTITY
;
144 req
.ID
.toi_entity
.tei_instance
= arp_maybe
->tei_instance
;
146 status
= DeviceIoControl( tcpFile
,
147 IOCTL_TCP_QUERY_INFORMATION_EX
,
154 if( !NT_SUCCESS(status
) ) return FALSE
;
156 return (type
& AT_ARP
);
159 static NTSTATUS
getInterfaceInfoSet( HANDLE tcpFile
,
161 PDWORD numInterfaces
) {
163 TDIEntityID
*entIDSet
= 0;
164 NTSTATUS status
= tdiGetEntityIDSet( tcpFile
, &entIDSet
, &numEntities
);
165 IFInfo
*infoSetInt
= 0;
166 int curInterf
= 0, i
;
168 if (!NT_SUCCESS(status
)) {
169 ERR("getInterfaceInfoSet: tdiGetEntityIDSet() failed: 0x%lx\n", status
);
173 infoSetInt
= HeapAlloc( GetProcessHeap(), 0,
174 sizeof(IFInfo
) * numEntities
);
177 for( i
= 0; i
< numEntities
; i
++ ) {
178 if( isInterface( &entIDSet
[i
] ) ) {
179 infoSetInt
[curInterf
].entity_id
= entIDSet
[i
];
180 status
= tdiGetMibForIfEntity
183 &infoSetInt
[curInterf
].if_info
);
184 TRACE("tdiGetMibForIfEntity: %08x\n", status
);
185 if( NT_SUCCESS(status
) ) {
191 status
= getNthIpEntity( tcpFile
, curInterf
, &ip_ent
);
192 if( NT_SUCCESS(status
) )
193 status
= tdiGetIpAddrsForIpEntity
194 ( tcpFile
, &ip_ent
, &addrs
, &numAddrs
);
195 for( j
= 0; j
< numAddrs
&& NT_SUCCESS(status
); j
++ ) {
196 TRACE("ADDR %d: index %d (target %d)\n", j
, addrs
[j
].iae_index
, infoSetInt
[curInterf
].if_info
.ent
.if_index
);
197 if( addrs
[j
].iae_index
==
198 infoSetInt
[curInterf
].if_info
.ent
.if_index
) {
199 memcpy( &infoSetInt
[curInterf
].ip_addr
,
201 sizeof( addrs
[j
] ) );
210 if (NT_SUCCESS(status
)) {
211 *infoSet
= infoSetInt
;
212 *numInterfaces
= curInterf
;
214 HeapFree(GetProcessHeap(), 0, infoSetInt
);
219 return STATUS_INSUFFICIENT_RESOURCES
;
223 static DWORD
getNumInterfacesInt(BOOL onlyNonLoopback
)
225 DWORD numEntities
, numInterfaces
= 0;
226 TDIEntityID
*entitySet
;
231 status
= openTcpFile( &tcpFile
);
233 if( !NT_SUCCESS(status
) ) {
234 WARN("getNumInterfaces: failed %08x\n", status
);
238 status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
240 if( !NT_SUCCESS(status
) ) {
241 WARN("getNumInterfaces: failed %08x\n", status
);
242 closeTcpFile( tcpFile
);
246 for( i
= 0; i
< numEntities
; i
++ ) {
247 if( isInterface( &entitySet
[i
] ) &&
249 (onlyNonLoopback
&& !isLoopback( tcpFile
, &entitySet
[i
] ))) )
253 TRACE("getNumInterfaces: success: %d %d %08x\n",
254 onlyNonLoopback
, numInterfaces
, status
);
256 closeTcpFile( tcpFile
);
258 tdiFreeThingSet( entitySet
);
260 return numInterfaces
;
263 DWORD
getNumInterfaces(void)
265 return getNumInterfacesInt( FALSE
);
268 DWORD
getNumNonLoopbackInterfaces(void)
270 return getNumInterfacesInt( TRUE
);
273 DWORD
getNthInterfaceEntity( HANDLE tcpFile
, DWORD index
, TDIEntityID
*ent
) {
274 DWORD numEntities
= 0;
275 DWORD numInterfaces
= 0;
276 TDIEntityID
*entitySet
= 0;
277 NTSTATUS status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
280 if( !NT_SUCCESS(status
) )
283 for( i
= 0; i
< numEntities
; i
++ ) {
284 if( isInterface( &entitySet
[i
] ) ) {
285 if( numInterfaces
== index
) break;
286 else numInterfaces
++;
290 TRACE("Index %d is entity #%d - %04x:%08x\n", index
, i
,
291 entitySet
[i
].tei_entity
, entitySet
[i
].tei_instance
);
293 if( numInterfaces
== index
&& i
< numEntities
) {
294 memcpy( ent
, &entitySet
[i
], sizeof(*ent
) );
295 tdiFreeThingSet( entitySet
);
296 return STATUS_SUCCESS
;
298 tdiFreeThingSet( entitySet
);
299 return STATUS_UNSUCCESSFUL
;
303 NTSTATUS
getInterfaceInfoByIndex( HANDLE tcpFile
, DWORD index
, IFInfo
*info
) {
306 NTSTATUS status
= getInterfaceInfoSet( tcpFile
, &ifInfo
, &numInterfaces
);
309 if( NT_SUCCESS(status
) )
310 for( i
= 0; i
< numInterfaces
; i
++ ) {
311 if( ifInfo
[i
].if_info
.ent
.if_index
== index
) {
312 memcpy( info
, &ifInfo
[i
], sizeof(*info
) );
317 if( NT_SUCCESS(status
) )
318 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
) )
330 for( i
= 0; i
< numInterfaces
; i
++ ) {
331 if( !strcmp((PCHAR
)ifInfo
[i
].if_info
.ent
.if_descr
, name
) ) {
332 memcpy( info
, &ifInfo
[i
], sizeof(*info
) );
337 if( NT_SUCCESS(status
) )
338 return i
< numInterfaces
? STATUS_SUCCESS
: STATUS_UNSUCCESSFUL
;
343 /* Note that the result of this operation must be freed later */
345 const char *getInterfaceNameByIndex(DWORD index
)
349 char *interfaceName
= 0, *adapter_name
= 0;
350 NTSTATUS status
= openTcpFile( &tcpFile
);
352 if( NT_SUCCESS(status
) ) {
353 status
= getInterfaceInfoByIndex( tcpFile
, index
, &ifInfo
);
355 if( NT_SUCCESS(status
) ) {
356 adapter_name
= (char *)ifInfo
.if_info
.ent
.if_descr
;
358 interfaceName
= HeapAlloc( GetProcessHeap(), 0,
359 strlen(adapter_name
) + 1 );
360 if (!interfaceName
) return NULL
;
362 strcpy( interfaceName
, adapter_name
);
365 closeTcpFile( tcpFile
);
368 return interfaceName
;
371 void consumeInterfaceName(const char *name
) {
372 HeapFree( GetProcessHeap(), 0, (char *)name
);
375 DWORD
getInterfaceIndexByName(const char *name
, PDWORD index
)
379 NTSTATUS status
= openTcpFile( &tcpFile
);
381 if( NT_SUCCESS(status
) ) {
382 status
= getInterfaceInfoByName( tcpFile
, (char *)name
, &ifInfo
);
384 if( NT_SUCCESS(status
) ) {
385 *index
= ifInfo
.if_info
.ent
.if_index
;
388 closeTcpFile( tcpFile
);
394 InterfaceIndexTable
*getInterfaceIndexTableInt( BOOL nonLoopbackOnly
) {
395 DWORD numInterfaces
, curInterface
= 0;
398 InterfaceIndexTable
*ret
= 0;
400 NTSTATUS status
= openTcpFile( &tcpFile
);
402 if( NT_SUCCESS(status
) ) {
403 status
= getInterfaceInfoSet( tcpFile
, &ifInfo
, &numInterfaces
);
405 TRACE("InterfaceInfoSet: %08x, %04x:%08x\n",
407 ifInfo
->entity_id
.tei_entity
,
408 ifInfo
->entity_id
.tei_instance
);
410 if( NT_SUCCESS(status
) ) {
411 ret
= (InterfaceIndexTable
*)
413 sizeof(InterfaceIndexTable
) +
414 (numInterfaces
- 1) * sizeof(DWORD
));
417 ret
->numAllocated
= numInterfaces
;
418 TRACE("NumInterfaces = %d\n", numInterfaces
);
420 for( i
= 0; i
< numInterfaces
; i
++ ) {
421 TRACE("Examining interface %d\n", i
);
422 if( !nonLoopbackOnly
||
423 !isLoopback( tcpFile
, &ifInfo
[i
].entity_id
) ) {
424 TRACE("Interface %d matches (%d)\n", i
, curInterface
);
425 ret
->indexes
[curInterface
++] =
426 ifInfo
[i
].if_info
.ent
.if_index
;
430 ret
->numIndexes
= curInterface
;
433 tdiFreeThingSet( ifInfo
);
435 closeTcpFile( tcpFile
);
441 InterfaceIndexTable
*getInterfaceIndexTable(void) {
442 return getInterfaceIndexTableInt( FALSE
);
445 InterfaceIndexTable
*getNonLoopbackInterfaceIndexTable(void) {
446 return getInterfaceIndexTableInt( TRUE
);
449 DWORD
getInterfaceIPAddrByName(const char *name
)
454 NTSTATUS
getIPAddrEntryForIf(HANDLE tcpFile
,
460 getInterfaceInfoByName( tcpFile
, name
, ifInfo
) :
461 getInterfaceInfoByIndex( tcpFile
, index
, ifInfo
);
463 if (!NT_SUCCESS(status
)) {
464 ERR("getIPAddrEntryForIf returning %lx\n", status
);
470 DWORD
getAddrByIndexOrName( char *name
, DWORD index
, IPHLPAddrType addrType
) {
473 NTSTATUS status
= STATUS_SUCCESS
;
474 DWORD addrOut
= INADDR_ANY
;
476 status
= openTcpFile( &tcpFile
);
478 if( NT_SUCCESS(status
) ) {
479 status
= getIPAddrEntryForIf( tcpFile
, name
, index
, &ifInfo
);
480 if( NT_SUCCESS(status
) ) {
482 case IPAAddr
: addrOut
= ifInfo
.ip_addr
.iae_addr
; break;
483 case IPABcast
: addrOut
= ifInfo
.ip_addr
.iae_bcastaddr
; break;
484 case IPAMask
: addrOut
= ifInfo
.ip_addr
.iae_mask
; break;
485 case IFMtu
: addrOut
= ifInfo
.if_info
.ent
.if_mtu
; break;
486 case IFStatus
: addrOut
= ifInfo
.if_info
.ent
.if_operstatus
; break;
489 closeTcpFile( tcpFile
);
495 DWORD
getInterfaceIPAddrByIndex(DWORD index
) {
496 return getAddrByIndexOrName( 0, index
, IPAAddr
);
499 DWORD
getInterfaceBCastAddrByName(const char *name
) {
500 return getAddrByIndexOrName( (char *)name
, 0, IPABcast
);
503 DWORD
getInterfaceBCastAddrByIndex(DWORD index
) {
504 return getAddrByIndexOrName( 0, index
, IPABcast
);
507 DWORD
getInterfaceMaskByName(const char *name
) {
508 return getAddrByIndexOrName( (char *)name
, 0, IPAMask
);
511 DWORD
getInterfaceMaskByIndex(DWORD index
) {
512 return getAddrByIndexOrName( 0, index
, IPAMask
);
515 void getInterfacePhysicalFromInfo( IFInfo
*info
,
516 PDWORD len
, PBYTE addr
, PDWORD type
) {
517 *len
= info
->if_info
.ent
.if_physaddrlen
;
518 memcpy( addr
, info
->if_info
.ent
.if_physaddr
, *len
);
519 *type
= info
->if_info
.ent
.if_type
;
522 DWORD
getInterfacePhysicalByName(const char *name
, PDWORD len
, PBYTE addr
,
527 NTSTATUS status
= openTcpFile( &tcpFile
);
529 if( NT_SUCCESS(status
) ) {
530 status
= getInterfaceInfoByName( tcpFile
, (char *)name
, &info
);
531 if( NT_SUCCESS(status
) )
532 getInterfacePhysicalFromInfo( &info
, len
, addr
, type
);
533 closeTcpFile( tcpFile
);
539 DWORD
getInterfacePhysicalByIndex(DWORD index
, PDWORD len
, PBYTE addr
,
544 NTSTATUS status
= openTcpFile( &tcpFile
);
546 if( NT_SUCCESS(status
) ) {
547 status
= getInterfaceInfoByIndex( tcpFile
, index
, &info
);
548 if( NT_SUCCESS(status
) )
549 getInterfacePhysicalFromInfo( &info
, len
, addr
, type
);
550 closeTcpFile( tcpFile
);
556 DWORD
getInterfaceMtuByName(const char *name
, PDWORD mtu
) {
557 *mtu
= getAddrByIndexOrName( (char *)name
, 0, IFMtu
);
558 return STATUS_SUCCESS
;
561 DWORD
getInterfaceMtuByIndex(DWORD index
, PDWORD mtu
) {
562 *mtu
= getAddrByIndexOrName( 0, index
, IFMtu
);
563 return STATUS_SUCCESS
;
566 DWORD
getInterfaceStatusByName(const char *name
, PDWORD status
) {
567 *status
= getAddrByIndexOrName( (char *)name
, 0, IFStatus
);
568 return STATUS_SUCCESS
;
571 DWORD
getInterfaceStatusByIndex(DWORD index
, PDWORD status
)
573 *status
= getAddrByIndexOrName( 0, index
, IFStatus
);
574 return STATUS_SUCCESS
;
577 DWORD
getInterfaceEntryByName(const char *name
, PMIB_IFROW entry
)
581 NTSTATUS status
= openTcpFile( &tcpFile
);
585 if( NT_SUCCESS(status
) ) {
586 status
= getInterfaceInfoByName( tcpFile
, (char *)name
, &info
);
588 if( NT_SUCCESS(status
) ) {
589 memcpy( &entry
->wszName
[MAX_INTERFACE_NAME_LEN
],
591 sizeof(info
.if_info
) );
594 TRACE("entry->bDescr = %s\n", entry
->bDescr
);
596 closeTcpFile( tcpFile
);
602 DWORD
getInterfaceEntryByIndex(DWORD index
, PMIB_IFROW entry
)
606 NTSTATUS status
= openTcpFile( &tcpFile
);
610 if( NT_SUCCESS(status
) ) {
611 status
= getInterfaceInfoByIndex( tcpFile
, index
, &info
);
613 if( NT_SUCCESS(status
) ) {
614 memcpy( &entry
->wszName
[MAX_INTERFACE_NAME_LEN
],
616 sizeof(info
.if_info
) );
619 closeTcpFile( tcpFile
);
625 char *toIPAddressString(unsigned int addr
, char string
[16])
627 struct in_addr iAddr
;
632 strncpy(string
, inet_ntoa(iAddr
), 16);
634 return inet_ntoa(iAddr
);
637 NTSTATUS
addIPAddress( IPAddr Address
, IPMask Mask
, DWORD IfIndex
,
638 PULONG NteContext
, PULONG NteInstance
)
641 NTSTATUS status
= openTcpFile( &tcpFile
);
643 IO_STATUS_BLOCK Iosb
;
647 if( !NT_SUCCESS(status
) ) return status
;
649 Data
.NteContext
= IfIndex
;
650 Data
.NewAddress
= Address
;
651 Data
.NewNetmask
= Mask
;
653 status
= NtDeviceIoControlFile( tcpFile
,
658 IOCTL_SET_IP_ADDRESS
,
664 closeTcpFile( tcpFile
);
666 if( NT_SUCCESS(status
) ) {
667 *NteContext
= Iosb
.Information
;
668 *NteInstance
= Data
.NewAddress
;
671 if (!NT_SUCCESS(status
)) {
672 ERR("addIPAddress for if %d returning 0x%lx\n", IfIndex
, status
);
679 NTSTATUS
deleteIpAddress( ULONG NteContext
)
682 NTSTATUS status
= openTcpFile( &tcpFile
);
683 IO_STATUS_BLOCK Iosb
;
687 if( !NT_SUCCESS(status
) ) return status
;
689 status
= NtDeviceIoControlFile( tcpFile
,
694 IOCTL_DELETE_IP_ADDRESS
,
700 closeTcpFile( tcpFile
);
702 if (!NT_SUCCESS(status
)) {
703 ERR("deleteIpAddress(%lu) returning 0x%lx\n", NteContext
, status
);