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 * This file is implemented on the IOCTL_TCP_QUERY_INFORMATION_EX ioctl on
25 #include "iphlpapi_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi
);
31 #ifndef TCPS_ESTABLISHED
32 # define TCPS_ESTABLISHED TCP_ESTABLISHED
35 # define TCPS_SYN_SENT TCP_SYN_SENT
37 #ifndef TCPS_SYN_RECEIVED
38 # define TCPS_SYN_RECEIVED TCP_SYN_RECV
40 #ifndef TCPS_FIN_WAIT_1
41 # define TCPS_FIN_WAIT_1 TCP_FIN_WAIT1
43 #ifndef TCPS_FIN_WAIT_2
44 # define TCPS_FIN_WAIT_2 TCP_FIN_WAIT2
46 #ifndef TCPS_TIME_WAIT
47 # define TCPS_TIME_WAIT TCP_TIME_WAIT
50 # define TCPS_CLOSED TCP_CLOSE
52 #ifndef TCPS_CLOSE_WAIT
53 # define TCPS_CLOSE_WAIT TCP_CLOSE_WAIT
56 # define TCPS_LAST_ACK TCP_LAST_ACK
59 # define TCPS_LISTEN TCP_LISTEN
62 # define TCPS_CLOSING TCP_CLOSING
65 BOOL
isIpEntity( HANDLE tcpFile
, TDIEntityID
*ent
) {
66 DWORD entityType
, returnedLen
;
68 TCP_REQUEST_QUERY_INFORMATION_EX req
;
70 req
.ID
.toi_class
= INFO_CLASS_GENERIC
;
71 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
72 req
.ID
.toi_id
= ENTITY_TYPE_ID
;
73 req
.ID
.toi_entity
= *ent
;
78 IOCTL_TCP_QUERY_INFORMATION_EX
,
86 TRACE("Ent: %04x:d -> %04x\n",
87 ent
->tei_entity
, ent
->tei_instance
, entityType
);
89 return NT_SUCCESS(status
) && entityType
== CL_NL_IP
;
92 NTSTATUS
getNthIpEntity( HANDLE tcpFile
, DWORD index
, TDIEntityID
*ent
) {
93 DWORD numEntities
= 0;
95 TDIEntityID
*entitySet
= 0;
96 NTSTATUS status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
99 if( !NT_SUCCESS(status
) )
102 for( i
= 0; i
< numEntities
; i
++ ) {
103 if( isIpEntity( tcpFile
, &entitySet
[i
] ) ) {
104 TRACE("Entity %d is an IP Entity\n", i
);
105 if( numRoutes
== index
) break;
110 if( numRoutes
== index
&& i
< numEntities
) {
111 TRACE("Index %d is entity #%d - %04x:%08x\n", index
, i
,
112 entitySet
[i
].tei_entity
, entitySet
[i
].tei_instance
);
113 memcpy( ent
, &entitySet
[i
], sizeof(*ent
) );
114 tdiFreeThingSet( entitySet
);
115 return STATUS_SUCCESS
;
117 tdiFreeThingSet( entitySet
);
118 return STATUS_UNSUCCESSFUL
;
122 NTSTATUS tdiGetMibForIpEntity
123 ( HANDLE tcpFile
, TDIEntityID
*ent
, IPSNMPInfo
*entry
) {
124 TCP_REQUEST_QUERY_INFORMATION_EX req
= TCP_REQUEST_QUERY_INFORMATION_INIT
;
125 NTSTATUS status
= STATUS_SUCCESS
;
128 memset( entry
, 0, sizeof( *entry
) );
130 TRACE("TdiGetMibForIpEntity(tcpFile %x,entityId %x)\n",
131 (DWORD
)tcpFile
, ent
->tei_instance
);
133 req
.ID
.toi_class
= INFO_CLASS_PROTOCOL
;
134 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
135 req
.ID
.toi_id
= IP_MIB_STATS_ID
;
136 req
.ID
.toi_entity
= *ent
;
138 status
= DeviceIoControl( tcpFile
,
139 IOCTL_TCP_QUERY_INFORMATION_EX
,
147 TRACE("TdiGetMibForIpEntity() => {\n"
148 " ipsi_forwarding ............ %d\n"
149 " ipsi_defaultttl ............ %d\n"
150 " ipsi_inreceives ............ %d\n"
151 " ipsi_indelivers ............ %d\n"
152 " ipsi_outrequests ........... %d\n"
153 " ipsi_routingdiscards ....... %d\n"
154 " ipsi_outdiscards ........... %d\n"
155 " ipsi_outnoroutes ........... %d\n"
156 " ipsi_numif ................. %d\n"
157 " ipsi_numaddr ............... %d\n"
158 " ipsi_numroutes ............. %d\n"
160 entry
->ipsi_forwarding
,
161 entry
->ipsi_defaultttl
,
162 entry
->ipsi_inreceives
,
163 entry
->ipsi_indelivers
,
164 entry
->ipsi_outrequests
,
165 entry
->ipsi_routingdiscards
,
166 entry
->ipsi_outdiscards
,
167 entry
->ipsi_outnoroutes
,
170 entry
->ipsi_numroutes
,
176 NTSTATUS tdiGetRoutesForIpEntity
177 ( HANDLE tcpFile
, TDIEntityID
*ent
, IPRouteEntry
**routes
, PDWORD numRoutes
) {
178 NTSTATUS status
= STATUS_SUCCESS
;
180 TRACE("TdiGetRoutesForIpEntity(tcpFile %x,entityId %x)\n",
181 (DWORD
)tcpFile
, ent
->tei_instance
);
183 status
= tdiGetSetOfThings( tcpFile
,
186 IP_MIB_ROUTETABLE_ENTRY_ID
,
190 sizeof(IPRouteEntry
),
197 NTSTATUS tdiGetIpAddrsForIpEntity
198 ( HANDLE tcpFile
, TDIEntityID
*ent
, IPAddrEntry
**addrs
, PDWORD numAddrs
) {
201 TRACE("TdiGetIpAddrsForIpEntity(tcpFile %x,entityId %x)\n",
202 (DWORD
)tcpFile
, ent
->tei_instance
);
204 status
= tdiGetSetOfThings( tcpFile
,
207 IP_MIB_ADDRTABLE_ENTRY_ID
,
218 DWORD
getInterfaceStatsByName(const char *name
, PMIB_IFROW entry
)
221 return ERROR_INVALID_PARAMETER
;
223 return ERROR_INVALID_PARAMETER
;
228 DWORD
getInterfaceStatsByIndex(DWORD index
, PMIB_IFROW entry
)
230 return ERROR_INVALID_PARAMETER
;
233 DWORD
getICMPStats(MIB_ICMP
*stats
)
238 return ERROR_INVALID_PARAMETER
;
240 memset(stats
, 0, sizeof(MIB_ICMP
));
241 /* get most of these stats from /proc/net/snmp, no error if can't */
242 fp
= fopen("/proc/net/snmp", "r");
244 const char hdr
[] = "Icmp:";
245 char buf
[512] = { 0 }, *ptr
;
248 ptr
= fgets(buf
, sizeof(buf
), fp
);
249 } while (ptr
&& strncasecmp(buf
, hdr
, sizeof(hdr
) - 1));
251 /* last line was a header, get another */
252 ptr
= fgets(buf
, sizeof(buf
), fp
);
253 if (ptr
&& strncasecmp(buf
, hdr
, sizeof(hdr
) - 1) == 0) {
258 stats
->stats
.icmpInStats
.dwMsgs
= strtoul(ptr
, &endPtr
, 10);
262 stats
->stats
.icmpInStats
.dwErrors
= strtoul(ptr
, &endPtr
, 10);
266 stats
->stats
.icmpInStats
.dwDestUnreachs
= strtoul(ptr
, &endPtr
, 10);
270 stats
->stats
.icmpInStats
.dwTimeExcds
= strtoul(ptr
, &endPtr
, 10);
274 stats
->stats
.icmpInStats
.dwParmProbs
= strtoul(ptr
, &endPtr
, 10);
278 stats
->stats
.icmpInStats
.dwSrcQuenchs
= strtoul(ptr
, &endPtr
, 10);
282 stats
->stats
.icmpInStats
.dwRedirects
= strtoul(ptr
, &endPtr
, 10);
286 stats
->stats
.icmpInStats
.dwEchoReps
= strtoul(ptr
, &endPtr
, 10);
290 stats
->stats
.icmpInStats
.dwTimestamps
= strtoul(ptr
, &endPtr
, 10);
294 stats
->stats
.icmpInStats
.dwTimestampReps
= strtoul(ptr
, &endPtr
, 10);
298 stats
->stats
.icmpInStats
.dwAddrMasks
= strtoul(ptr
, &endPtr
, 10);
302 stats
->stats
.icmpInStats
.dwAddrMaskReps
= strtoul(ptr
, &endPtr
, 10);
306 stats
->stats
.icmpOutStats
.dwMsgs
= strtoul(ptr
, &endPtr
, 10);
310 stats
->stats
.icmpOutStats
.dwErrors
= strtoul(ptr
, &endPtr
, 10);
314 stats
->stats
.icmpOutStats
.dwDestUnreachs
= strtoul(ptr
, &endPtr
, 10);
318 stats
->stats
.icmpOutStats
.dwTimeExcds
= strtoul(ptr
, &endPtr
, 10);
322 stats
->stats
.icmpOutStats
.dwParmProbs
= strtoul(ptr
, &endPtr
, 10);
326 stats
->stats
.icmpOutStats
.dwSrcQuenchs
= strtoul(ptr
, &endPtr
, 10);
330 stats
->stats
.icmpOutStats
.dwRedirects
= strtoul(ptr
, &endPtr
, 10);
334 stats
->stats
.icmpOutStats
.dwEchoReps
= strtoul(ptr
, &endPtr
, 10);
338 stats
->stats
.icmpOutStats
.dwTimestamps
= strtoul(ptr
, &endPtr
, 10);
342 stats
->stats
.icmpOutStats
.dwTimestampReps
= strtoul(ptr
, &endPtr
, 10);
346 stats
->stats
.icmpOutStats
.dwAddrMasks
= strtoul(ptr
, &endPtr
, 10);
350 stats
->stats
.icmpOutStats
.dwAddrMaskReps
= strtoul(ptr
, &endPtr
, 10);
360 DWORD
getIPStats(PMIB_IPSTATS stats
, DWORD family
)
363 return ERROR_INVALID_PARAMETER
;
367 DWORD
getTCPStats(MIB_TCPSTATS
*stats
, DWORD family
)
370 return ERROR_INVALID_PARAMETER
;
374 DWORD
getUDPStats(MIB_UDPSTATS
*stats
, DWORD family
)
377 return ERROR_INVALID_PARAMETER
;
381 static DWORD
getNumWithOneHeader(const char *filename
)
386 DWORD
getNumRoutes(void)
388 DWORD numEntities
, numRoutes
= 0;
389 TDIEntityID
*entitySet
;
396 status
= openTcpFile( &tcpFile
);
398 if( !NT_SUCCESS(status
) ) {
399 TRACE("failure: %08x\n", (int)status
);
403 status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
405 if( !NT_SUCCESS(status
) ) {
406 TRACE("failure: %08x\n", (int)status
);
410 for( i
= 0; i
< numEntities
; i
++ ) {
411 if( isIpEntity( tcpFile
, &entitySet
[i
] ) ) {
413 memset( &isnmp
, 0, sizeof( isnmp
) );
414 status
= tdiGetMibForIpEntity( tcpFile
, &entitySet
[i
], &isnmp
);
415 if( !NT_SUCCESS(status
) ) {
416 tdiFreeThingSet( entitySet
);
419 numRoutes
+= isnmp
.ipsi_numroutes
;
423 TRACE("numRoutes: %d\n", (int)numRoutes
);
425 closeTcpFile( tcpFile
);
430 VOID
HexDump( PCHAR Data
, DWORD Len
) {
433 for( i
= 0; i
< Len
; i
++ ) {
435 if( i
) fprintf(stderr
,"\n");
436 fprintf(stderr
,"%08x:", i
);
438 fprintf( stderr
, " %02x", Data
[i
] & 0xff );
440 fprintf(stderr
,"\n");
443 RouteTable
*getRouteTable(void)
445 RouteTable
*out_route_table
;
446 DWORD numRoutes
= getNumRoutes(), routesAdded
= 0;
450 NTSTATUS status
= openTcpFile( &tcpFile
);
453 if( !NT_SUCCESS(status
) )
456 TRACE("GETTING ROUTE TABLE\n");
458 out_route_table
= HeapAlloc( GetProcessHeap(), 0,
460 (sizeof(RouteEntry
) * (numRoutes
- 1)) );
462 out_route_table
->numRoutes
= numRoutes
;
464 for( i
= 0; routesAdded
< numRoutes
; i
++ ) {
466 IPRouteEntry
*route_set
;
468 getNthIpEntity( tcpFile
, i
, &ent
);
469 tdiGetMibForIpEntity( tcpFile
, &ent
, &snmpInfo
);
471 TRACE( "%d routes in instance %d\n", snmpInfo
.ipsi_numroutes
, i
);
473 tdiGetRoutesForIpEntity( tcpFile
, &ent
, &route_set
, &numRoutes
);
476 closeTcpFile( tcpFile
);
477 HeapFree( GetProcessHeap(), 0, out_route_table
);
481 TRACE("Route set returned\n");
484 sizeof( IPRouteEntry
) *
485 snmpInfo
.ipsi_numroutes
);
488 for( j
= 0; j
< snmpInfo
.ipsi_numroutes
; j
++ ) {
489 int routeNum
= j
+ routesAdded
;
490 out_route_table
->routes
[routeNum
].dest
=
491 route_set
[j
].ire_dest
;
492 out_route_table
->routes
[routeNum
].mask
=
493 route_set
[j
].ire_mask
;
494 out_route_table
->routes
[routeNum
].gateway
=
496 out_route_table
->routes
[routeNum
].ifIndex
=
497 route_set
[j
].ire_index
;
498 out_route_table
->routes
[routeNum
].metric
=
499 route_set
[j
].ire_metric1
;
502 if( route_set
) tdiFreeThingSet( route_set
);
504 routesAdded
+= snmpInfo
.ipsi_numroutes
;
507 TRACE("Return: %08x, %08x\n", status
, out_route_table
);
509 return out_route_table
;
512 DWORD
getNumArpEntries(void)
515 TDIEntityID
*entitySet
= NULL
;
517 int i
, totalNumber
= 0;
519 PMIB_IPNETROW IpArpTable
= NULL
;
524 status
= openTcpFile( &tcpFile
);
526 if( !NT_SUCCESS(status
) ) {
527 TRACE("failure: %08x\n", (int)status
);
531 status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
533 for( i
= 0; i
< numEntities
; i
++ ) {
534 if( isInterface( &entitySet
[i
] ) &&
535 hasArp( tcpFile
, &entitySet
[i
] ) ) {
537 status
= tdiGetSetOfThings( tcpFile
,
540 IP_MIB_ARPTABLE_ENTRY_ID
,
542 entitySet
[i
].tei_instance
,
544 sizeof(MIB_IPNETROW
),
545 (PVOID
*)&IpArpTable
,
548 if( status
== STATUS_SUCCESS
) totalNumber
+= returnSize
;
549 if( IpArpTable
) tdiFreeThingSet( IpArpTable
);
553 closeTcpFile( tcpFile
);
554 if( IpArpTable
) tdiFreeThingSet( IpArpTable
);
555 if( entitySet
) tdiFreeThingSet( entitySet
);
559 PMIB_IPNETTABLE
getArpTable(void)
561 DWORD numEntities
, returnSize
;
562 TDIEntityID
*entitySet
;
564 int i
, row
= 0, totalNumber
;
566 PMIB_IPNETTABLE IpArpTable
= NULL
;
567 PMIB_IPNETROW AdapterArpTable
= NULL
;
571 totalNumber
= getNumArpEntries();
573 status
= openTcpFile( &tcpFile
);
575 if( !NT_SUCCESS(status
) ) {
576 TRACE("failure: %08x\n", (int)status
);
580 IpArpTable
= HeapAlloc
581 ( GetProcessHeap(), 0,
582 sizeof(DWORD
) + (sizeof(MIB_IPNETROW
) * totalNumber
) );
584 status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
586 for( i
= 0; i
< numEntities
; i
++ ) {
587 if( isIpEntity( tcpFile
, &entitySet
[i
] ) &&
588 hasArp( tcpFile
, &entitySet
[i
] ) ) {
590 status
= tdiGetSetOfThings( tcpFile
,
593 IP_MIB_ARPTABLE_ENTRY_ID
,
595 entitySet
[i
].tei_instance
,
597 sizeof(MIB_IPNETROW
),
598 (PVOID
*)&AdapterArpTable
,
601 if( status
== STATUS_SUCCESS
) {
602 for( row
= 0; row
< returnSize
; row
++ )
603 IpArpTable
->table
[row
] = AdapterArpTable
[row
];
606 if( AdapterArpTable
) tdiFreeThingSet( AdapterArpTable
);
610 tdiFreeThingSet( entitySet
);
611 IpArpTable
->dwNumEntries
= row
;
616 DWORD
getNumUdpEntries(void)
618 return getNumWithOneHeader("/proc/net/udp");
621 PMIB_UDPTABLE
getUdpTable(void)
623 DWORD numEntries
= getNumUdpEntries();
626 ret
= (PMIB_UDPTABLE
)calloc(1, sizeof(MIB_UDPTABLE
) +
627 (numEntries
- 1) * sizeof(MIB_UDPROW
));
631 /* get from /proc/net/udp, no error if can't */
632 fp
= fopen("/proc/net/udp", "r");
634 char buf
[512] = { 0 }, *ptr
;
636 /* skip header line */
637 ptr
= fgets(buf
, sizeof(buf
), fp
);
638 while (ptr
&& ret
->dwNumEntries
< numEntries
) {
639 ptr
= fgets(buf
, sizeof(buf
), fp
);
644 strtoul(ptr
, &endPtr
, 16); /* skip */
649 ret
->table
[ret
->dwNumEntries
].dwLocalAddr
= strtoul(ptr
, &endPtr
,
655 ret
->table
[ret
->dwNumEntries
].dwLocalPort
= strtoul(ptr
, &endPtr
,
668 DWORD
getNumTcpEntries(void)
670 return getNumWithOneHeader("/proc/net/tcp");
673 PMIB_TCPTABLE
getTcpTable(void)