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
22 #include "iphlpapi_private.h"
26 #ifndef TCPS_ESTABLISHED
27 # define TCPS_ESTABLISHED TCP_ESTABLISHED
30 # define TCPS_SYN_SENT TCP_SYN_SENT
32 #ifndef TCPS_SYN_RECEIVED
33 # define TCPS_SYN_RECEIVED TCP_SYN_RECV
35 #ifndef TCPS_FIN_WAIT_1
36 # define TCPS_FIN_WAIT_1 TCP_FIN_WAIT1
38 #ifndef TCPS_FIN_WAIT_2
39 # define TCPS_FIN_WAIT_2 TCP_FIN_WAIT2
41 #ifndef TCPS_TIME_WAIT
42 # define TCPS_TIME_WAIT TCP_TIME_WAIT
45 # define TCPS_CLOSED TCP_CLOSE
47 #ifndef TCPS_CLOSE_WAIT
48 # define TCPS_CLOSE_WAIT TCP_CLOSE_WAIT
51 # define TCPS_LAST_ACK TCP_LAST_ACK
54 # define TCPS_LISTEN TCP_LISTEN
57 # define TCPS_CLOSING TCP_CLOSING
60 BOOL
isIpEntity( HANDLE tcpFile
, TDIEntityID
*ent
) {
61 DWORD entityType
, returnedLen
;
63 TCP_REQUEST_QUERY_INFORMATION_EX req
;
65 req
.ID
.toi_class
= INFO_CLASS_GENERIC
;
66 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
67 req
.ID
.toi_id
= ENTITY_TYPE_ID
;
68 req
.ID
.toi_entity
= *ent
;
73 IOCTL_TCP_QUERY_INFORMATION_EX
,
81 DPRINT("Ent: %04x:d -> %04x\n",
82 ent
->tei_entity
, ent
->tei_instance
, entityType
);
84 return NT_SUCCESS(status
) && entityType
== CL_NL_IP
;
87 NTSTATUS
getNthIpEntity( HANDLE tcpFile
, DWORD index
, TDIEntityID
*ent
) {
88 DWORD numEntities
= 0;
90 TDIEntityID
*entitySet
= 0;
91 NTSTATUS status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
94 if( !NT_SUCCESS(status
) )
97 for( i
= 0; i
< numEntities
; i
++ ) {
98 if( isIpEntity( tcpFile
, &entitySet
[i
] ) ) {
99 DPRINT("Entity %d is an IP Entity\n", i
);
100 if( numRoutes
== index
) break;
105 if( numRoutes
== index
&& i
< numEntities
) {
106 DPRINT("Index %d is entity #%d - %04x:%08x\n", index
, i
,
107 entitySet
[i
].tei_entity
, entitySet
[i
].tei_instance
);
108 memcpy( ent
, &entitySet
[i
], sizeof(*ent
) );
109 tdiFreeThingSet( entitySet
);
110 return STATUS_SUCCESS
;
112 tdiFreeThingSet( entitySet
);
113 return STATUS_UNSUCCESSFUL
;
117 NTSTATUS tdiGetMibForIpEntity
118 ( HANDLE tcpFile
, TDIEntityID
*ent
, IPSNMPInfo
*entry
) {
119 TCP_REQUEST_QUERY_INFORMATION_EX req
= TCP_REQUEST_QUERY_INFORMATION_INIT
;
120 NTSTATUS status
= STATUS_SUCCESS
;
123 memset( entry
, 0, sizeof( *entry
) );
125 DPRINT("TdiGetMibForIpEntity(tcpFile %x,entityId %x)\n",
126 (DWORD
)tcpFile
, ent
->tei_instance
);
128 req
.ID
.toi_class
= INFO_CLASS_PROTOCOL
;
129 req
.ID
.toi_type
= INFO_TYPE_PROVIDER
;
130 req
.ID
.toi_id
= IP_MIB_STATS_ID
;
131 req
.ID
.toi_entity
= *ent
;
133 status
= DeviceIoControl( tcpFile
,
134 IOCTL_TCP_QUERY_INFORMATION_EX
,
142 DPRINT("TdiGetMibForIpEntity() => {\n"
143 " ipsi_forwarding ............ %d\n"
144 " ipsi_defaultttl ............ %d\n"
145 " ipsi_inreceives ............ %d\n"
146 " ipsi_indelivers ............ %d\n"
147 " ipsi_outrequests ........... %d\n"
148 " ipsi_routingdiscards ....... %d\n"
149 " ipsi_outdiscards ........... %d\n"
150 " ipsi_outnoroutes ........... %d\n"
151 " ipsi_numif ................. %d\n"
152 " ipsi_numaddr ............... %d\n"
153 " ipsi_numroutes ............. %d\n"
155 entry
->ipsi_forwarding
,
156 entry
->ipsi_defaultttl
,
157 entry
->ipsi_inreceives
,
158 entry
->ipsi_indelivers
,
159 entry
->ipsi_outrequests
,
160 entry
->ipsi_routingdiscards
,
161 entry
->ipsi_outdiscards
,
162 entry
->ipsi_outnoroutes
,
165 entry
->ipsi_numroutes
,
171 NTSTATUS tdiGetRoutesForIpEntity
172 ( HANDLE tcpFile
, TDIEntityID
*ent
, IPRouteEntry
**routes
, PDWORD numRoutes
) {
173 NTSTATUS status
= STATUS_SUCCESS
;
175 DPRINT("TdiGetRoutesForIpEntity(tcpFile %x,entityId %x)\n",
176 (DWORD
)tcpFile
, ent
->tei_instance
);
178 status
= tdiGetSetOfThings( tcpFile
,
181 IP_MIB_ROUTETABLE_ENTRY_ID
,
184 sizeof(IPRouteEntry
),
191 NTSTATUS tdiGetIpAddrsForIpEntity
192 ( HANDLE tcpFile
, TDIEntityID
*ent
, IPAddrEntry
**addrs
, PDWORD numAddrs
) {
195 DPRINT("TdiGetIpAddrsForIpEntity(tcpFile %x,entityId %x)\n",
196 (DWORD
)tcpFile
, ent
->tei_instance
);
198 status
= tdiGetSetOfThings( tcpFile
,
201 IP_MIB_ADDRTABLE_ENTRY_ID
,
211 DWORD
getInterfaceStatsByName(const char *name
, PMIB_IFROW entry
)
214 return ERROR_INVALID_PARAMETER
;
216 return ERROR_INVALID_PARAMETER
;
221 DWORD
getInterfaceStatsByIndex(DWORD index
, PMIB_IFROW entry
)
223 return ERROR_INVALID_PARAMETER
;
226 DWORD
getICMPStats(MIB_ICMP
*stats
)
231 return ERROR_INVALID_PARAMETER
;
233 memset(stats
, 0, sizeof(MIB_ICMP
));
234 /* get most of these stats from /proc/net/snmp, no error if can't */
235 fp
= fopen("/proc/net/snmp", "r");
237 const char hdr
[] = "Icmp:";
238 char buf
[512] = { 0 }, *ptr
;
241 ptr
= fgets(buf
, sizeof(buf
), fp
);
242 } while (ptr
&& strncasecmp(buf
, hdr
, sizeof(hdr
) - 1));
244 /* last line was a header, get another */
245 ptr
= fgets(buf
, sizeof(buf
), fp
);
246 if (ptr
&& strncasecmp(buf
, hdr
, sizeof(hdr
) - 1) == 0) {
251 stats
->stats
.icmpInStats
.dwMsgs
= strtoul(ptr
, &endPtr
, 10);
255 stats
->stats
.icmpInStats
.dwErrors
= strtoul(ptr
, &endPtr
, 10);
259 stats
->stats
.icmpInStats
.dwDestUnreachs
= strtoul(ptr
, &endPtr
, 10);
263 stats
->stats
.icmpInStats
.dwTimeExcds
= strtoul(ptr
, &endPtr
, 10);
267 stats
->stats
.icmpInStats
.dwParmProbs
= strtoul(ptr
, &endPtr
, 10);
271 stats
->stats
.icmpInStats
.dwSrcQuenchs
= strtoul(ptr
, &endPtr
, 10);
275 stats
->stats
.icmpInStats
.dwRedirects
= strtoul(ptr
, &endPtr
, 10);
279 stats
->stats
.icmpInStats
.dwEchoReps
= strtoul(ptr
, &endPtr
, 10);
283 stats
->stats
.icmpInStats
.dwTimestamps
= strtoul(ptr
, &endPtr
, 10);
287 stats
->stats
.icmpInStats
.dwTimestampReps
= strtoul(ptr
, &endPtr
, 10);
291 stats
->stats
.icmpInStats
.dwAddrMasks
= strtoul(ptr
, &endPtr
, 10);
295 stats
->stats
.icmpInStats
.dwAddrMaskReps
= strtoul(ptr
, &endPtr
, 10);
299 stats
->stats
.icmpOutStats
.dwMsgs
= strtoul(ptr
, &endPtr
, 10);
303 stats
->stats
.icmpOutStats
.dwErrors
= strtoul(ptr
, &endPtr
, 10);
307 stats
->stats
.icmpOutStats
.dwDestUnreachs
= strtoul(ptr
, &endPtr
, 10);
311 stats
->stats
.icmpOutStats
.dwTimeExcds
= strtoul(ptr
, &endPtr
, 10);
315 stats
->stats
.icmpOutStats
.dwParmProbs
= strtoul(ptr
, &endPtr
, 10);
319 stats
->stats
.icmpOutStats
.dwSrcQuenchs
= strtoul(ptr
, &endPtr
, 10);
323 stats
->stats
.icmpOutStats
.dwRedirects
= strtoul(ptr
, &endPtr
, 10);
327 stats
->stats
.icmpOutStats
.dwEchoReps
= strtoul(ptr
, &endPtr
, 10);
331 stats
->stats
.icmpOutStats
.dwTimestamps
= strtoul(ptr
, &endPtr
, 10);
335 stats
->stats
.icmpOutStats
.dwTimestampReps
= strtoul(ptr
, &endPtr
, 10);
339 stats
->stats
.icmpOutStats
.dwAddrMasks
= strtoul(ptr
, &endPtr
, 10);
343 stats
->stats
.icmpOutStats
.dwAddrMaskReps
= strtoul(ptr
, &endPtr
, 10);
353 DWORD
getIPStats(PMIB_IPSTATS stats
, DWORD family
)
356 return ERROR_INVALID_PARAMETER
;
360 DWORD
getTCPStats(MIB_TCPSTATS
*stats
, DWORD family
)
363 return ERROR_INVALID_PARAMETER
;
367 DWORD
getUDPStats(MIB_UDPSTATS
*stats
, DWORD family
)
370 return ERROR_INVALID_PARAMETER
;
374 static DWORD
getNumWithOneHeader(const char *filename
)
379 DWORD
getNumRoutes(void)
381 DWORD numEntities
, numRoutes
= 0;
382 TDIEntityID
*entitySet
;
389 status
= openTcpFile( &tcpFile
);
391 if( !NT_SUCCESS(status
) ) {
392 TRACE("failure: %08x\n", (int)status
);
396 status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
398 if( !NT_SUCCESS(status
) ) {
399 TRACE("failure: %08x\n", (int)status
);
403 for( i
= 0; i
< numEntities
; i
++ ) {
404 if( isIpEntity( tcpFile
, &entitySet
[i
] ) ) {
406 memset( &isnmp
, 0, sizeof( isnmp
) );
407 status
= tdiGetMibForIpEntity( tcpFile
, &entitySet
[i
], &isnmp
);
408 if( !NT_SUCCESS(status
) ) {
409 tdiFreeThingSet( entitySet
);
412 numRoutes
+= isnmp
.ipsi_numroutes
;
416 TRACE("numRoutes: %d\n", (int)numRoutes
);
418 closeTcpFile( tcpFile
);
423 VOID
HexDump( PCHAR Data
, DWORD Len
) {
426 for( i
= 0; i
< Len
; i
++ ) {
428 if( i
) fprintf(stderr
,"\n");
429 fprintf(stderr
,"%08x:", i
);
431 fprintf( stderr
, " %02x", Data
[i
] & 0xff );
433 fprintf(stderr
,"\n");
436 RouteTable
*getRouteTable(void)
438 RouteTable
*out_route_table
;
439 DWORD numRoutes
= getNumRoutes(), routesAdded
= 0;
443 NTSTATUS status
= openTcpFile( &tcpFile
);
446 if( !NT_SUCCESS(status
) )
449 out_route_table
= HeapAlloc( GetProcessHeap(), 0,
451 (sizeof(RouteEntry
) * (numRoutes
- 1)) );
453 out_route_table
->numRoutes
= numRoutes
;
455 for( i
= 0; routesAdded
< numRoutes
; i
++ ) {
457 IPRouteEntry
*route_set
;
459 getNthIpEntity( tcpFile
, i
, &ent
);
460 tdiGetMibForIpEntity( tcpFile
, &ent
, &snmpInfo
);
462 DPRINT( "%d routes in instance %d\n", snmpInfo
.ipsi_numroutes
, i
);
465 closeTcpFile( tcpFile
);
466 HeapFree( GetProcessHeap(), 0, out_route_table
);
470 tdiGetRoutesForIpEntity( tcpFile
, &ent
, &route_set
, &numRoutes
);
472 DPRINT("Route set returned\n");
475 sizeof( IPRouteEntry
) *
476 snmpInfo
.ipsi_numroutes
);
479 for( j
= 0; j
< snmpInfo
.ipsi_numroutes
; j
++ ) {
480 int routeNum
= j
+ routesAdded
;
481 out_route_table
->routes
[routeNum
].dest
=
482 route_set
[j
].ire_dest
;
483 out_route_table
->routes
[routeNum
].mask
=
484 route_set
[j
].ire_mask
;
485 out_route_table
->routes
[routeNum
].gateway
=
487 out_route_table
->routes
[routeNum
].ifIndex
=
488 route_set
[j
].ire_index
;
489 out_route_table
->routes
[routeNum
].metric
=
490 route_set
[j
].ire_metric
;
493 if( route_set
) tdiFreeThingSet( route_set
);
495 routesAdded
+= snmpInfo
.ipsi_numroutes
;
498 return out_route_table
;
501 DWORD
getNumArpEntries(void)
503 return getNumWithOneHeader("/proc/net/arp");
506 PMIB_IPNETTABLE
getArpTable(void)
511 DWORD
getNumUdpEntries(void)
513 return getNumWithOneHeader("/proc/net/udp");
516 PMIB_UDPTABLE
getUdpTable(void)
518 DWORD numEntries
= getNumUdpEntries();
521 ret
= (PMIB_UDPTABLE
)calloc(1, sizeof(MIB_UDPTABLE
) +
522 (numEntries
- 1) * sizeof(MIB_UDPROW
));
526 /* get from /proc/net/udp, no error if can't */
527 fp
= fopen("/proc/net/udp", "r");
529 char buf
[512] = { 0 }, *ptr
;
531 /* skip header line */
532 ptr
= fgets(buf
, sizeof(buf
), fp
);
533 while (ptr
&& ret
->dwNumEntries
< numEntries
) {
534 ptr
= fgets(buf
, sizeof(buf
), fp
);
539 strtoul(ptr
, &endPtr
, 16); /* skip */
544 ret
->table
[ret
->dwNumEntries
].dwLocalAddr
= strtoul(ptr
, &endPtr
,
550 ret
->table
[ret
->dwNumEntries
].dwLocalPort
= strtoul(ptr
, &endPtr
,
563 DWORD
getNumTcpEntries(void)
565 return getNumWithOneHeader("/proc/net/tcp");
568 PMIB_TCPTABLE
getTcpTable(void)