e416d4842b8cc4eac1abed4e2426be47f1f80a7d
[reactos.git] / dll / win32 / iphlpapi / ipstats_reactos.c
1 /* Copyright (C) 2003 Art Yerkes
2 * A reimplementation of ifenum.c by Juan Lang
3 *
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.
8 *
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.
13 *
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
17 *
18 * This file is implemented on the IOCTL_TCP_QUERY_INFORMATION_EX ioctl on
19 * tcpip.sys
20 */
21
22 #include "iphlpapi_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
25
26 #ifndef TCPS_ESTABLISHED
27 # define TCPS_ESTABLISHED TCP_ESTABLISHED
28 #endif
29 #ifndef TCPS_SYN_SENT
30 # define TCPS_SYN_SENT TCP_SYN_SENT
31 #endif
32 #ifndef TCPS_SYN_RECEIVED
33 # define TCPS_SYN_RECEIVED TCP_SYN_RECV
34 #endif
35 #ifndef TCPS_FIN_WAIT_1
36 # define TCPS_FIN_WAIT_1 TCP_FIN_WAIT1
37 #endif
38 #ifndef TCPS_FIN_WAIT_2
39 # define TCPS_FIN_WAIT_2 TCP_FIN_WAIT2
40 #endif
41 #ifndef TCPS_TIME_WAIT
42 # define TCPS_TIME_WAIT TCP_TIME_WAIT
43 #endif
44 #ifndef TCPS_CLOSED
45 # define TCPS_CLOSED TCP_CLOSE
46 #endif
47 #ifndef TCPS_CLOSE_WAIT
48 # define TCPS_CLOSE_WAIT TCP_CLOSE_WAIT
49 #endif
50 #ifndef TCPS_LAST_ACK
51 # define TCPS_LAST_ACK TCP_LAST_ACK
52 #endif
53 #ifndef TCPS_LISTEN
54 # define TCPS_LISTEN TCP_LISTEN
55 #endif
56 #ifndef TCPS_CLOSING
57 # define TCPS_CLOSING TCP_CLOSING
58 #endif
59
60 BOOL isIpEntity( HANDLE tcpFile, TDIEntityID *ent ) {
61 return (ent->tei_entity == CL_NL_ENTITY ||
62 ent->tei_entity == CO_NL_ENTITY);
63 }
64
65 NTSTATUS getNthIpEntity( HANDLE tcpFile, DWORD index, TDIEntityID *ent ) {
66 DWORD numEntities = 0;
67 DWORD numRoutes = 0;
68 TDIEntityID *entitySet = 0;
69 NTSTATUS status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
70 int i;
71
72 if( !NT_SUCCESS(status) )
73 return status;
74
75 for( i = 0; i < numEntities; i++ ) {
76 if( isIpEntity( tcpFile, &entitySet[i] ) ) {
77 TRACE("Entity %d is an IP Entity\n", i);
78 if( numRoutes == index ) break;
79 else numRoutes++;
80 }
81 }
82
83 if( numRoutes == index && i < numEntities ) {
84 TRACE("Index %d is entity #%d - %04x:%08x\n", index, i,
85 entitySet[i].tei_entity, entitySet[i].tei_instance );
86 memcpy( ent, &entitySet[i], sizeof(*ent) );
87 tdiFreeThingSet( entitySet );
88 return STATUS_SUCCESS;
89 } else {
90 tdiFreeThingSet( entitySet );
91 return STATUS_UNSUCCESSFUL;
92 }
93 }
94
95 NTSTATUS tdiGetMibForIpEntity
96 ( HANDLE tcpFile, TDIEntityID *ent, IPSNMPInfo *entry ) {
97 TCP_REQUEST_QUERY_INFORMATION_EX req = TCP_REQUEST_QUERY_INFORMATION_INIT;
98 NTSTATUS status = STATUS_SUCCESS;
99 DWORD returnSize;
100
101 memset( entry, 0, sizeof( *entry ) );
102
103 TRACE("TdiGetMibForIpEntity(tcpFile %x,entityId %x)\n",
104 (DWORD)tcpFile, ent->tei_instance);
105
106 req.ID.toi_class = INFO_CLASS_PROTOCOL;
107 req.ID.toi_type = INFO_TYPE_PROVIDER;
108 req.ID.toi_id = IP_MIB_STATS_ID;
109 req.ID.toi_entity = *ent;
110
111 status = DeviceIoControl( tcpFile,
112 IOCTL_TCP_QUERY_INFORMATION_EX,
113 &req,
114 sizeof(req),
115 entry,
116 sizeof(*entry),
117 &returnSize,
118 NULL );
119
120 TRACE("TdiGetMibForIpEntity() => {\n"
121 " ipsi_forwarding ............ %d\n"
122 " ipsi_defaultttl ............ %d\n"
123 " ipsi_inreceives ............ %d\n"
124 " ipsi_indelivers ............ %d\n"
125 " ipsi_outrequests ........... %d\n"
126 " ipsi_routingdiscards ....... %d\n"
127 " ipsi_outdiscards ........... %d\n"
128 " ipsi_outnoroutes ........... %d\n"
129 " ipsi_numif ................. %d\n"
130 " ipsi_numaddr ............... %d\n"
131 " ipsi_numroutes ............. %d\n"
132 "} status %08x\n",
133 entry->ipsi_forwarding,
134 entry->ipsi_defaultttl,
135 entry->ipsi_inreceives,
136 entry->ipsi_indelivers,
137 entry->ipsi_outrequests,
138 entry->ipsi_routingdiscards,
139 entry->ipsi_outdiscards,
140 entry->ipsi_outnoroutes,
141 entry->ipsi_numif,
142 entry->ipsi_numaddr,
143 entry->ipsi_numroutes,
144 status);
145
146 return status;
147 }
148
149 NTSTATUS tdiGetRoutesForIpEntity
150 ( HANDLE tcpFile, TDIEntityID *ent, IPRouteEntry **routes, PDWORD numRoutes ) {
151 NTSTATUS status = STATUS_SUCCESS;
152
153 TRACE("TdiGetRoutesForIpEntity(tcpFile %x,entityId %x)\n",
154 (DWORD)tcpFile, ent->tei_instance);
155
156 status = tdiGetSetOfThings( tcpFile,
157 INFO_CLASS_PROTOCOL,
158 INFO_TYPE_PROVIDER,
159 IP_MIB_ARPTABLE_ENTRY_ID,
160 CL_NL_ENTITY,
161 ent->tei_instance,
162 0,
163 sizeof(IPRouteEntry),
164 (PVOID *)routes,
165 numRoutes);
166
167 return status;
168 }
169
170 NTSTATUS tdiGetIpAddrsForIpEntity
171 ( HANDLE tcpFile, TDIEntityID *ent, IPAddrEntry **addrs, PDWORD numAddrs ) {
172 NTSTATUS status;
173
174 TRACE("TdiGetIpAddrsForIpEntity(tcpFile %x,entityId %x)\n",
175 (DWORD)tcpFile, ent->tei_instance);
176
177 status = tdiGetSetOfThings( tcpFile,
178 INFO_CLASS_PROTOCOL,
179 INFO_TYPE_PROVIDER,
180 IP_MIB_ADDRTABLE_ENTRY_ID,
181 CL_NL_ENTITY,
182 ent->tei_instance,
183 0,
184 sizeof(IPAddrEntry),
185 (PVOID *)addrs,
186 numAddrs );
187
188 return status;
189 }
190
191 DWORD getInterfaceStatsByName(const char *name, PMIB_IFROW entry)
192 {
193 if (!name)
194 return ERROR_INVALID_PARAMETER;
195 if (!entry)
196 return ERROR_INVALID_PARAMETER;
197
198 return NO_ERROR;
199 }
200
201 DWORD getInterfaceStatsByIndex(DWORD index, PMIB_IFROW entry)
202 {
203 return ERROR_INVALID_PARAMETER;
204 }
205
206 DWORD getICMPStats(MIB_ICMP *stats)
207 {
208 FILE *fp;
209
210 if (!stats)
211 return ERROR_INVALID_PARAMETER;
212
213 memset(stats, 0, sizeof(MIB_ICMP));
214 /* get most of these stats from /proc/net/snmp, no error if can't */
215 fp = fopen("/proc/net/snmp", "r");
216 if (fp) {
217 const char hdr[] = "Icmp:";
218 char buf[512] = { 0 }, *ptr;
219
220 do {
221 ptr = fgets(buf, sizeof(buf), fp);
222 } while (ptr && strncasecmp(buf, hdr, sizeof(hdr) - 1));
223 if (ptr) {
224 /* last line was a header, get another */
225 ptr = fgets(buf, sizeof(buf), fp);
226 if (ptr && strncasecmp(buf, hdr, sizeof(hdr) - 1) == 0) {
227 char *endPtr;
228
229 ptr += sizeof(hdr);
230 if (ptr && *ptr) {
231 stats->stats.icmpInStats.dwMsgs = strtoul(ptr, &endPtr, 10);
232 ptr = endPtr;
233 }
234 if (ptr && *ptr) {
235 stats->stats.icmpInStats.dwErrors = strtoul(ptr, &endPtr, 10);
236 ptr = endPtr;
237 }
238 if (ptr && *ptr) {
239 stats->stats.icmpInStats.dwDestUnreachs = strtoul(ptr, &endPtr, 10);
240 ptr = endPtr;
241 }
242 if (ptr && *ptr) {
243 stats->stats.icmpInStats.dwTimeExcds = strtoul(ptr, &endPtr, 10);
244 ptr = endPtr;
245 }
246 if (ptr && *ptr) {
247 stats->stats.icmpInStats.dwParmProbs = strtoul(ptr, &endPtr, 10);
248 ptr = endPtr;
249 }
250 if (ptr && *ptr) {
251 stats->stats.icmpInStats.dwSrcQuenchs = strtoul(ptr, &endPtr, 10);
252 ptr = endPtr;
253 }
254 if (ptr && *ptr) {
255 stats->stats.icmpInStats.dwRedirects = strtoul(ptr, &endPtr, 10);
256 ptr = endPtr;
257 }
258 if (ptr && *ptr) {
259 stats->stats.icmpInStats.dwEchoReps = strtoul(ptr, &endPtr, 10);
260 ptr = endPtr;
261 }
262 if (ptr && *ptr) {
263 stats->stats.icmpInStats.dwTimestamps = strtoul(ptr, &endPtr, 10);
264 ptr = endPtr;
265 }
266 if (ptr && *ptr) {
267 stats->stats.icmpInStats.dwTimestampReps = strtoul(ptr, &endPtr, 10);
268 ptr = endPtr;
269 }
270 if (ptr && *ptr) {
271 stats->stats.icmpInStats.dwAddrMasks = strtoul(ptr, &endPtr, 10);
272 ptr = endPtr;
273 }
274 if (ptr && *ptr) {
275 stats->stats.icmpInStats.dwAddrMaskReps = strtoul(ptr, &endPtr, 10);
276 ptr = endPtr;
277 }
278 if (ptr && *ptr) {
279 stats->stats.icmpOutStats.dwMsgs = strtoul(ptr, &endPtr, 10);
280 ptr = endPtr;
281 }
282 if (ptr && *ptr) {
283 stats->stats.icmpOutStats.dwErrors = strtoul(ptr, &endPtr, 10);
284 ptr = endPtr;
285 }
286 if (ptr && *ptr) {
287 stats->stats.icmpOutStats.dwDestUnreachs = strtoul(ptr, &endPtr, 10);
288 ptr = endPtr;
289 }
290 if (ptr && *ptr) {
291 stats->stats.icmpOutStats.dwTimeExcds = strtoul(ptr, &endPtr, 10);
292 ptr = endPtr;
293 }
294 if (ptr && *ptr) {
295 stats->stats.icmpOutStats.dwParmProbs = strtoul(ptr, &endPtr, 10);
296 ptr = endPtr;
297 }
298 if (ptr && *ptr) {
299 stats->stats.icmpOutStats.dwSrcQuenchs = strtoul(ptr, &endPtr, 10);
300 ptr = endPtr;
301 }
302 if (ptr && *ptr) {
303 stats->stats.icmpOutStats.dwRedirects = strtoul(ptr, &endPtr, 10);
304 ptr = endPtr;
305 }
306 if (ptr && *ptr) {
307 stats->stats.icmpOutStats.dwEchoReps = strtoul(ptr, &endPtr, 10);
308 ptr = endPtr;
309 }
310 if (ptr && *ptr) {
311 stats->stats.icmpOutStats.dwTimestamps = strtoul(ptr, &endPtr, 10);
312 ptr = endPtr;
313 }
314 if (ptr && *ptr) {
315 stats->stats.icmpOutStats.dwTimestampReps = strtoul(ptr, &endPtr, 10);
316 ptr = endPtr;
317 }
318 if (ptr && *ptr) {
319 stats->stats.icmpOutStats.dwAddrMasks = strtoul(ptr, &endPtr, 10);
320 ptr = endPtr;
321 }
322 if (ptr && *ptr) {
323 stats->stats.icmpOutStats.dwAddrMaskReps = strtoul(ptr, &endPtr, 10);
324 ptr = endPtr;
325 }
326 }
327 }
328 fclose(fp);
329 }
330 return NO_ERROR;
331 }
332
333 DWORD getIPStats(PMIB_IPSTATS stats, DWORD family)
334 {
335 if (!stats)
336 return ERROR_INVALID_PARAMETER;
337 return NO_ERROR;
338 }
339
340 DWORD getTCPStats(MIB_TCPSTATS *stats, DWORD family)
341 {
342 if (!stats)
343 return ERROR_INVALID_PARAMETER;
344 return NO_ERROR;
345 }
346
347 DWORD getUDPStats(MIB_UDPSTATS *stats, DWORD family)
348 {
349 if (!stats)
350 return ERROR_INVALID_PARAMETER;
351 return NO_ERROR;
352 }
353
354 static DWORD getNumWithOneHeader(const char *filename)
355 {
356 return 0;
357 }
358
359 DWORD getNumRoutes(void)
360 {
361 DWORD numEntities, numRoutes = 0;
362 TDIEntityID *entitySet;
363 HANDLE tcpFile;
364 int i;
365 NTSTATUS status;
366
367 TRACE("called.\n");
368
369 status = openTcpFile( &tcpFile, FILE_READ_DATA );
370
371 if( !NT_SUCCESS(status) ) {
372 TRACE("failure: %08x\n", (int)status );
373 return 0;
374 }
375
376 status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
377
378 if( !NT_SUCCESS(status) ) {
379 TRACE("failure: %08x\n", (int)status );
380 closeTcpFile( tcpFile );
381 return 0;
382 }
383
384 for( i = 0; i < numEntities; i++ ) {
385 if( isIpEntity( tcpFile, &entitySet[i] ) ) {
386 IPSNMPInfo isnmp;
387 memset( &isnmp, 0, sizeof( isnmp ) );
388 status = tdiGetMibForIpEntity( tcpFile, &entitySet[i], &isnmp );
389 if( !NT_SUCCESS(status) ) {
390 tdiFreeThingSet( entitySet );
391 closeTcpFile( tcpFile );
392 return status;
393 }
394 numRoutes += isnmp.ipsi_numroutes;
395 }
396 }
397
398 TRACE("numRoutes: %d\n", (int)numRoutes);
399
400 closeTcpFile( tcpFile );
401
402 return numRoutes;
403 }
404
405 VOID HexDump( PCHAR Data, DWORD Len ) {
406 int i;
407
408 for( i = 0; i < Len; i++ ) {
409 if( !(i & 0xf) ) {
410 if( i ) fprintf(stderr,"\n");
411 fprintf(stderr,"%08x:", i);
412 }
413 fprintf( stderr, " %02x", Data[i] & 0xff );
414 }
415 fprintf(stderr,"\n");
416 }
417
418 RouteTable *getRouteTable(void)
419 {
420 RouteTable *out_route_table;
421 DWORD numRoutes = getNumRoutes(), routesAdded = 0;
422 TDIEntityID ent;
423 HANDLE tcpFile;
424 NTSTATUS status = openTcpFile( &tcpFile, FILE_READ_DATA );
425 int i;
426
427 if( !NT_SUCCESS(status) )
428 return 0;
429
430 TRACE("GETTING ROUTE TABLE\n");
431
432 out_route_table = HeapAlloc( GetProcessHeap(), 0,
433 sizeof(RouteTable) +
434 (sizeof(RouteEntry) * (numRoutes - 1)) );
435 if (!out_route_table) {
436 closeTcpFile(tcpFile);
437 return NULL;
438 }
439
440 out_route_table->numRoutes = numRoutes;
441
442 for( i = 0; routesAdded < out_route_table->numRoutes; i++ ) {
443 int j;
444 IPRouteEntry *route_set;
445
446 getNthIpEntity( tcpFile, i, &ent );
447
448 tdiGetRoutesForIpEntity( tcpFile, &ent, &route_set, &numRoutes );
449
450 if( !route_set ) {
451 closeTcpFile( tcpFile );
452 HeapFree( GetProcessHeap(), 0, out_route_table );
453 return 0;
454 }
455
456 TRACE( "%d routes in instance %d\n", numRoutes, i );
457 #if 0
458 HexDump( route_set,
459 sizeof( IPRouteEntry ) *
460 snmpInfo.ipsi_numroutes );
461 #endif
462
463 for( j = 0; j < numRoutes; j++ ) {
464 int routeNum = j + routesAdded;
465 out_route_table->routes[routeNum].dest =
466 route_set[j].ire_dest;
467 out_route_table->routes[routeNum].mask =
468 route_set[j].ire_mask;
469 out_route_table->routes[routeNum].gateway =
470 route_set[j].ire_gw;
471 out_route_table->routes[routeNum].ifIndex =
472 route_set[j].ire_index;
473 out_route_table->routes[routeNum].metric =
474 route_set[j].ire_metric1;
475 }
476
477 if( route_set ) tdiFreeThingSet( route_set );
478
479 routesAdded += numRoutes;
480 }
481
482 closeTcpFile( tcpFile );
483
484 TRACE("Return: %08x, %08x\n", status, out_route_table);
485
486 return out_route_table;
487 }
488
489 DWORD getNumArpEntries(void)
490 {
491 DWORD numEntities;
492 TDIEntityID *entitySet = NULL;
493 HANDLE tcpFile;
494 int i, totalNumber = 0;
495 NTSTATUS status;
496 PMIB_IPNETROW IpArpTable = NULL;
497 DWORD returnSize;
498
499 TRACE("called.\n");
500
501 status = openTcpFile( &tcpFile, FILE_READ_DATA );
502
503 if( !NT_SUCCESS(status) ) {
504 TRACE("failure: %08x\n", (int)status );
505 return 0;
506 }
507
508 status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
509
510 for( i = 0; i < numEntities; i++ ) {
511 if( isInterface( &entitySet[i] ) &&
512 hasArp( tcpFile, &entitySet[i] ) ) {
513
514 status = tdiGetSetOfThings( tcpFile,
515 INFO_CLASS_PROTOCOL,
516 INFO_TYPE_PROVIDER,
517 IP_MIB_ARPTABLE_ENTRY_ID,
518 AT_ENTITY,
519 entitySet[i].tei_instance,
520 0,
521 sizeof(MIB_IPNETROW),
522 (PVOID *)&IpArpTable,
523 &returnSize );
524
525 if( status == STATUS_SUCCESS ) totalNumber += returnSize;
526 if( IpArpTable ) {
527 tdiFreeThingSet( IpArpTable );
528 IpArpTable = NULL;
529 }
530 }
531 }
532
533 closeTcpFile( tcpFile );
534 if( IpArpTable ) tdiFreeThingSet( IpArpTable );
535 if( entitySet ) tdiFreeThingSet( entitySet );
536 return totalNumber;
537 }
538
539 PMIB_IPNETTABLE getArpTable(void)
540 {
541 DWORD numEntities, returnSize;
542 TDIEntityID *entitySet;
543 HANDLE tcpFile;
544 int i, totalNumber, TmpIdx, CurrIdx = 0;
545 NTSTATUS status;
546 PMIB_IPNETTABLE IpArpTable = NULL;
547 PMIB_IPNETROW AdapterArpTable = NULL;
548
549 TRACE("called.\n");
550
551 totalNumber = getNumArpEntries();
552
553 status = openTcpFile( &tcpFile, FILE_READ_DATA );
554
555 if( !NT_SUCCESS(status) ) {
556 TRACE("failure: %08x\n", (int)status );
557 return 0;
558 }
559
560 IpArpTable = HeapAlloc
561 ( GetProcessHeap(), 0,
562 sizeof(DWORD) + (sizeof(MIB_IPNETROW) * totalNumber) );
563 if (!IpArpTable) {
564 closeTcpFile(tcpFile);
565 return NULL;
566 }
567
568 status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
569
570 for( i = 0; i < numEntities; i++ ) {
571 if( isInterface( &entitySet[i] ) &&
572 hasArp( tcpFile, &entitySet[i] ) ) {
573
574 status = tdiGetSetOfThings( tcpFile,
575 INFO_CLASS_PROTOCOL,
576 INFO_TYPE_PROVIDER,
577 IP_MIB_ARPTABLE_ENTRY_ID,
578 AT_ENTITY,
579 entitySet[i].tei_instance,
580 0,
581 sizeof(MIB_IPNETROW),
582 (PVOID *)&AdapterArpTable,
583 &returnSize );
584
585 if( status == STATUS_SUCCESS ) {
586 for( TmpIdx = 0; TmpIdx < returnSize; TmpIdx++, CurrIdx++ )
587 IpArpTable->table[CurrIdx] = AdapterArpTable[TmpIdx];
588 tdiFreeThingSet( AdapterArpTable );
589 }
590 }
591 }
592
593 closeTcpFile( tcpFile );
594
595 tdiFreeThingSet( entitySet );
596 IpArpTable->dwNumEntries = CurrIdx;
597
598 return IpArpTable;
599 }
600
601 DWORD getNumUdpEntries(void)
602 {
603 return getNumWithOneHeader("/proc/net/udp");
604 }
605
606 PMIB_UDPTABLE getUdpTable(void)
607 {
608 DWORD numEntries = getNumUdpEntries();
609 PMIB_UDPTABLE ret;
610
611 ret = (PMIB_UDPTABLE)calloc(1, sizeof(MIB_UDPTABLE) +
612 (numEntries - 1) * sizeof(MIB_UDPROW));
613 if (ret) {
614 FILE *fp;
615
616 /* get from /proc/net/udp, no error if can't */
617 fp = fopen("/proc/net/udp", "r");
618 if (fp) {
619 char buf[512] = { 0 }, *ptr;
620
621 /* skip header line */
622 ptr = fgets(buf, sizeof(buf), fp);
623 while (ptr && ret->dwNumEntries < numEntries) {
624 ptr = fgets(buf, sizeof(buf), fp);
625 if (ptr) {
626 char *endPtr;
627
628 if (ptr && *ptr) {
629 strtoul(ptr, &endPtr, 16); /* skip */
630 ptr = endPtr;
631 }
632 if (ptr && *ptr) {
633 ptr++;
634 ret->table[ret->dwNumEntries].dwLocalAddr = strtoul(ptr, &endPtr,
635 16);
636 ptr = endPtr;
637 }
638 if (ptr && *ptr) {
639 ptr++;
640 ret->table[ret->dwNumEntries].dwLocalPort = strtoul(ptr, &endPtr,
641 16);
642 ptr = endPtr;
643 }
644 ret->dwNumEntries++;
645 }
646 }
647 fclose(fp);
648 }
649 }
650 return ret;
651 }
652
653 DWORD getNumTcpEntries(void)
654 {
655 return getNumWithOneHeader("/proc/net/tcp");
656 }
657
658 PMIB_TCPTABLE getTcpTable(void)
659 {
660 return 0;
661 }