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
);
407 closeTcpFile( tcpFile
);
411 for( i
= 0; i
< numEntities
; i
++ ) {
412 if( isIpEntity( tcpFile
, &entitySet
[i
] ) ) {
414 memset( &isnmp
, 0, sizeof( isnmp
) );
415 status
= tdiGetMibForIpEntity( tcpFile
, &entitySet
[i
], &isnmp
);
416 if( !NT_SUCCESS(status
) ) {
417 tdiFreeThingSet( entitySet
);
418 closeTcpFile( tcpFile
);
421 numRoutes
+= isnmp
.ipsi_numroutes
;
425 TRACE("numRoutes: %d\n", (int)numRoutes
);
427 closeTcpFile( tcpFile
);
432 VOID
HexDump( PCHAR Data
, DWORD Len
) {
435 for( i
= 0; i
< Len
; i
++ ) {
437 if( i
) fprintf(stderr
,"\n");
438 fprintf(stderr
,"%08x:", i
);
440 fprintf( stderr
, " %02x", Data
[i
] & 0xff );
442 fprintf(stderr
,"\n");
445 RouteTable
*getRouteTable(void)
447 RouteTable
*out_route_table
;
448 DWORD numRoutes
= getNumRoutes(), routesAdded
= 0;
452 NTSTATUS status
= openTcpFile( &tcpFile
);
455 if( !NT_SUCCESS(status
) )
458 TRACE("GETTING ROUTE TABLE\n");
460 out_route_table
= HeapAlloc( GetProcessHeap(), 0,
462 (sizeof(RouteEntry
) * (numRoutes
- 1)) );
464 out_route_table
->numRoutes
= numRoutes
;
466 for( i
= 0; routesAdded
< numRoutes
; i
++ ) {
468 IPRouteEntry
*route_set
;
470 getNthIpEntity( tcpFile
, i
, &ent
);
471 tdiGetMibForIpEntity( tcpFile
, &ent
, &snmpInfo
);
473 TRACE( "%d routes in instance %d\n", snmpInfo
.ipsi_numroutes
, i
);
475 tdiGetRoutesForIpEntity( tcpFile
, &ent
, &route_set
, &numRoutes
);
478 closeTcpFile( tcpFile
);
479 HeapFree( GetProcessHeap(), 0, out_route_table
);
483 TRACE("Route set returned\n");
486 sizeof( IPRouteEntry
) *
487 snmpInfo
.ipsi_numroutes
);
490 for( j
= 0; j
< snmpInfo
.ipsi_numroutes
; j
++ ) {
491 int routeNum
= j
+ routesAdded
;
492 out_route_table
->routes
[routeNum
].dest
=
493 route_set
[j
].ire_dest
;
494 out_route_table
->routes
[routeNum
].mask
=
495 route_set
[j
].ire_mask
;
496 out_route_table
->routes
[routeNum
].gateway
=
498 out_route_table
->routes
[routeNum
].ifIndex
=
499 route_set
[j
].ire_index
;
500 out_route_table
->routes
[routeNum
].metric
=
501 route_set
[j
].ire_metric1
;
504 if( route_set
) tdiFreeThingSet( route_set
);
506 routesAdded
+= snmpInfo
.ipsi_numroutes
;
509 closeTcpFile( tcpFile
);
511 TRACE("Return: %08x, %08x\n", status
, out_route_table
);
513 return out_route_table
;
516 DWORD
getNumArpEntries(void)
519 TDIEntityID
*entitySet
= NULL
;
521 int i
, totalNumber
= 0;
523 PMIB_IPNETROW IpArpTable
= NULL
;
528 status
= openTcpFile( &tcpFile
);
530 if( !NT_SUCCESS(status
) ) {
531 TRACE("failure: %08x\n", (int)status
);
535 status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
537 for( i
= 0; i
< numEntities
; i
++ ) {
538 if( isInterface( &entitySet
[i
] ) &&
539 hasArp( tcpFile
, &entitySet
[i
] ) ) {
541 status
= tdiGetSetOfThings( tcpFile
,
544 IP_MIB_ARPTABLE_ENTRY_ID
,
546 entitySet
[i
].tei_instance
,
548 sizeof(MIB_IPNETROW
),
549 (PVOID
*)&IpArpTable
,
552 if( status
== STATUS_SUCCESS
) totalNumber
+= returnSize
;
553 if( IpArpTable
) tdiFreeThingSet( IpArpTable
);
557 closeTcpFile( tcpFile
);
558 if( IpArpTable
) tdiFreeThingSet( IpArpTable
);
559 if( entitySet
) tdiFreeThingSet( entitySet
);
563 PMIB_IPNETTABLE
getArpTable(void)
565 DWORD numEntities
, returnSize
;
566 TDIEntityID
*entitySet
;
568 int i
, row
= 0, totalNumber
;
570 PMIB_IPNETTABLE IpArpTable
= NULL
;
571 PMIB_IPNETROW AdapterArpTable
= NULL
;
575 totalNumber
= getNumArpEntries();
577 status
= openTcpFile( &tcpFile
);
579 if( !NT_SUCCESS(status
) ) {
580 TRACE("failure: %08x\n", (int)status
);
584 IpArpTable
= HeapAlloc
585 ( GetProcessHeap(), 0,
586 sizeof(DWORD
) + (sizeof(MIB_IPNETROW
) * totalNumber
) );
588 status
= tdiGetEntityIDSet( tcpFile
, &entitySet
, &numEntities
);
590 for( i
= 0; i
< numEntities
; i
++ ) {
591 if( isIpEntity( tcpFile
, &entitySet
[i
] ) &&
592 hasArp( tcpFile
, &entitySet
[i
] ) ) {
594 status
= tdiGetSetOfThings( tcpFile
,
597 IP_MIB_ARPTABLE_ENTRY_ID
,
599 entitySet
[i
].tei_instance
,
601 sizeof(MIB_IPNETROW
),
602 (PVOID
*)&AdapterArpTable
,
605 if( status
== STATUS_SUCCESS
) {
606 for( row
= 0; row
< returnSize
; row
++ )
607 IpArpTable
->table
[row
] = AdapterArpTable
[row
];
610 if( AdapterArpTable
) tdiFreeThingSet( AdapterArpTable
);
614 closeTcpFile( tcpFile
);
616 tdiFreeThingSet( entitySet
);
617 IpArpTable
->dwNumEntries
= row
;
622 DWORD
getNumUdpEntries(void)
624 return getNumWithOneHeader("/proc/net/udp");
627 PMIB_UDPTABLE
getUdpTable(void)
629 DWORD numEntries
= getNumUdpEntries();
632 ret
= (PMIB_UDPTABLE
)calloc(1, sizeof(MIB_UDPTABLE
) +
633 (numEntries
- 1) * sizeof(MIB_UDPROW
));
637 /* get from /proc/net/udp, no error if can't */
638 fp
= fopen("/proc/net/udp", "r");
640 char buf
[512] = { 0 }, *ptr
;
642 /* skip header line */
643 ptr
= fgets(buf
, sizeof(buf
), fp
);
644 while (ptr
&& ret
->dwNumEntries
< numEntries
) {
645 ptr
= fgets(buf
, sizeof(buf
), fp
);
650 strtoul(ptr
, &endPtr
, 16); /* skip */
655 ret
->table
[ret
->dwNumEntries
].dwLocalAddr
= strtoul(ptr
, &endPtr
,
661 ret
->table
[ret
->dwNumEntries
].dwLocalPort
= strtoul(ptr
, &endPtr
,
674 DWORD
getNumTcpEntries(void)
676 return getNumWithOneHeader("/proc/net/tcp");
679 PMIB_TCPTABLE
getTcpTable(void)