2 * Copyright (C) 2003,2006 Juan Lang
3 * Copyright (C) 2007 TransGaming Technologies Inc.
4 * Copyright (C) 2009 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
29 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
39 #ifdef HAVE_SYS_SOCKETVAR_H
40 #include <sys/socketvar.h>
42 #ifdef HAVE_SYS_TIMEOUT_H
43 #include <sys/timeout.h>
45 #ifdef HAVE_NETINET_IN_H
46 #include <netinet/in.h>
48 #ifdef HAVE_NETINET_IN_SYSTM_H
49 #include <netinet/in_systm.h>
51 #ifdef HAVE_ARPA_INET_H
52 #include <arpa/inet.h>
57 #ifdef HAVE_NET_IF_DL_H
58 #include <net/if_dl.h>
60 #ifdef HAVE_NET_IF_TYPES_H
61 #include <net/if_types.h>
63 #ifdef HAVE_NET_ROUTE_H
64 #include <net/route.h>
66 #ifdef HAVE_NET_IF_ARP_H
67 #include <net/if_arp.h>
69 #ifdef HAVE_NETINET_IF_ETHER_H
70 #include <netinet/if_ether.h>
72 #ifdef HAVE_NETINET_IF_INARP_H
73 #include <netinet/if_inarp.h>
75 #ifdef HAVE_NETINET_IP_H
76 #include <netinet/ip.h>
78 #ifdef HAVE_NETINET_TCP_H
79 #include <netinet/tcp.h>
81 #ifdef HAVE_NETINET_IP_VAR_H
82 #include <netinet/ip_var.h>
84 #ifdef HAVE_NETINET_TCP_FSM_H
85 #include <netinet/tcp_fsm.h>
87 #ifdef HAVE_NETINET_IN_PCB_H
88 #include <netinet/in_pcb.h>
90 #ifdef HAVE_NETINET_TCP_TIMER_H
91 #include <netinet/tcp_timer.h>
93 #ifdef HAVE_NETINET_TCP_VAR_H
94 #include <netinet/tcp_var.h>
96 #ifdef HAVE_NETINET_IP_ICMP_H
97 #include <netinet/ip_icmp.h>
99 #ifdef HAVE_NETINET_ICMP_VAR_H
100 #include <netinet/icmp_var.h>
102 #ifdef HAVE_NETINET_UDP_H
103 #include <netinet/udp.h>
105 #ifdef HAVE_NETINET_UDP_VAR_H
106 #include <netinet/udp_var.h>
108 #ifdef HAVE_SYS_PROTOSW_H
109 #include <sys/protosw.h>
111 #ifdef HAVE_SYS_SYSCTL_H
112 #include <sys/sysctl.h>
117 #ifdef HAVE_INET_MIB2_H
118 #include <inet/mib2.h>
120 #ifdef HAVE_STROPTS_H
123 #ifdef HAVE_SYS_TIHDR_H
124 #include <sys/tihdr.h>
129 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
132 #define ADVANCE(x, n) (x += ROUNDUP(((struct sockaddr *)n)->sa_len))
135 #include "ntstatus.h"
136 #define WIN32_NO_STATUS
137 #define NONAMELESSUNION
141 #include "wine/debug.h"
142 #include "wine/server.h"
144 #ifndef HAVE_NETINET_TCP_FSM_H
145 #define TCPS_ESTABLISHED 1
146 #define TCPS_SYN_SENT 2
147 #define TCPS_SYN_RECEIVED 3
148 #define TCPS_FIN_WAIT_1 4
149 #define TCPS_FIN_WAIT_2 5
150 #define TCPS_TIME_WAIT 6
151 #define TCPS_CLOSED 7
152 #define TCPS_CLOSE_WAIT 8
153 #define TCPS_LAST_ACK 9
154 #define TCPS_LISTEN 10
155 #define TCPS_CLOSING 11
158 #ifndef RTF_MULTICAST
159 #define RTF_MULTICAST 0 /* Not available on NetBSD/OpenBSD */
163 #define RTF_LLINFO 0 /* Not available on FreeBSD 8 and above */
166 WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi
);
169 static DWORD
kstat_get_ui32( kstat_t
*ksp
, const char *name
)
172 kstat_named_t
*data
= ksp
->ks_data
;
174 for (i
= 0; i
< ksp
->ks_ndata
; i
++)
175 if (!strcmp( data
[i
].name
, name
)) return data
[i
].value
.ui32
;
179 static ULONGLONG
kstat_get_ui64( kstat_t
*ksp
, const char *name
)
182 kstat_named_t
*data
= ksp
->ks_data
;
184 for (i
= 0; i
< ksp
->ks_ndata
; i
++)
185 if (!strcmp( data
[i
].name
, name
)) return data
[i
].value
.ui64
;
190 #if defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
191 static int open_streams_mib( const char *proto
)
197 struct T_optmgmt_req req_header
;
198 struct opthdr opt_header
;
201 if ((fd
= open( "/dev/arp", O_RDWR
)) == -1)
203 WARN( "could not open /dev/arp: %s\n", strerror(errno
) );
206 if (proto
) ioctl( fd
, I_PUSH
, proto
);
208 request
.req_header
.PRIM_type
= T_SVR4_OPTMGMT_REQ
;
209 request
.req_header
.OPT_length
= sizeof(request
.opt_header
);
210 request
.req_header
.OPT_offset
= FIELD_OFFSET( struct request
, opt_header
);
211 request
.req_header
.MGMT_flags
= T_CURRENT
;
212 request
.opt_header
.level
= MIB2_IP
;
213 request
.opt_header
.name
= 0;
214 request
.opt_header
.len
= 0;
216 buf
.len
= sizeof(request
);
217 buf
.buf
= (caddr_t
)&request
;
218 if (putmsg( fd
, &buf
, NULL
, 0 ) == -1)
220 WARN( "putmsg: %s\n", strerror(errno
) );
227 static void *read_mib_entry( int fd
, int level
, int name
, int *len
)
235 struct T_optmgmt_ack ack_header
;
236 struct opthdr opt_header
;
241 buf
.maxlen
= sizeof(reply
);
242 buf
.buf
= (caddr_t
)&reply
;
243 if ((ret
= getmsg( fd
, &buf
, NULL
, &flags
)) < 0) return NULL
;
244 if (!(ret
& MOREDATA
)) return NULL
;
245 if (reply
.ack_header
.PRIM_type
!= T_OPTMGMT_ACK
) return NULL
;
246 if (buf
.len
< sizeof(reply
.ack_header
)) return NULL
;
247 if (reply
.ack_header
.OPT_length
< sizeof(reply
.opt_header
)) return NULL
;
249 if (!(data
= HeapAlloc( GetProcessHeap(), 0, reply
.opt_header
.len
))) return NULL
;
250 buf
.maxlen
= reply
.opt_header
.len
;
251 buf
.buf
= (caddr_t
)data
;
253 if (getmsg( fd
, NULL
, &buf
, &flags
) >= 0 &&
254 reply
.opt_header
.level
== level
&&
255 reply
.opt_header
.name
== name
)
260 HeapFree( GetProcessHeap(), 0, data
);
263 #endif /* HAVE_SYS_TIHDR_H && T_OPTMGMT_ACK */
265 DWORD
getInterfaceStatsByName(const char *name
, PMIB_IFROW entry
)
267 DWORD ret
= ERROR_NOT_SUPPORTED
;
269 if (!name
|| !entry
) return ERROR_INVALID_PARAMETER
;
275 if ((fp
= fopen("/proc/net/dev", "r")))
279 int nameLen
= strlen(name
);
281 while ((ptr
= fgets(buf
, sizeof(buf
), fp
)))
283 while (*ptr
&& isspace(*ptr
)) ptr
++;
284 if (strncasecmp(ptr
, name
, nameLen
) == 0 && *(ptr
+ nameLen
) == ':')
287 sscanf( ptr
, "%u %u %u %u %u %u %u %u %u %u %u %u",
288 &entry
->dwInOctets
, &entry
->dwInUcastPkts
,
289 &entry
->dwInErrors
, &entry
->dwInDiscards
,
291 &entry
->dwInNUcastPkts
, &entry
->dwOutOctets
,
292 &entry
->dwOutUcastPkts
, &entry
->dwOutErrors
,
293 &entry
->dwOutDiscards
);
301 #elif defined(HAVE_LIBKSTAT)
306 if ((kc
= kstat_open()) &&
307 (ksp
= kstat_lookup( kc
, NULL
, -1, (char *)name
)) &&
308 kstat_read( kc
, ksp
, NULL
) != -1 &&
309 ksp
->ks_type
== KSTAT_TYPE_NAMED
)
311 entry
->dwMtu
= 1500; /* FIXME */
312 entry
->dwSpeed
= min( kstat_get_ui64( ksp
, "ifspeed" ), ~0u );
313 entry
->dwInOctets
= kstat_get_ui32( ksp
, "rbytes" );
314 entry
->dwInNUcastPkts
= kstat_get_ui32( ksp
, "multircv" );
315 entry
->dwInNUcastPkts
+= kstat_get_ui32( ksp
, "brdcstrcv" );
316 entry
->dwInUcastPkts
= kstat_get_ui32( ksp
, "ipackets" ) - entry
->dwInNUcastPkts
;
317 entry
->dwInDiscards
= kstat_get_ui32( ksp
, "norcvbuf" );
318 entry
->dwInErrors
= kstat_get_ui32( ksp
, "ierrors" );
319 entry
->dwInUnknownProtos
= kstat_get_ui32( ksp
, "unknowns" );
320 entry
->dwOutOctets
= kstat_get_ui32( ksp
, "obytes" );
321 entry
->dwOutNUcastPkts
= kstat_get_ui32( ksp
, "multixmt" );
322 entry
->dwOutNUcastPkts
+= kstat_get_ui32( ksp
, "brdcstxmt" );
323 entry
->dwOutUcastPkts
= kstat_get_ui32( ksp
, "opackets" ) - entry
->dwOutNUcastPkts
;
324 entry
->dwOutDiscards
= 0; /* FIXME */
325 entry
->dwOutErrors
= kstat_get_ui32( ksp
, "oerrors" );
326 entry
->dwOutQLen
= kstat_get_ui32( ksp
, "noxmtbuf" );
329 if (kc
) kstat_close( kc
);
331 #elif defined(HAVE_SYS_SYSCTL_H) && defined(NET_RT_IFLIST)
333 int mib
[] = {CTL_NET
, PF_ROUTE
, 0, AF_INET
, NET_RT_IFLIST
, if_nametoindex(name
)};
334 #define MIB_LEN (sizeof(mib) / sizeof(mib[0]))
337 char *buf
= NULL
, *end
;
338 struct if_msghdr
*ifm
;
339 struct if_data ifdata
;
341 if(sysctl(mib
, MIB_LEN
, NULL
, &needed
, NULL
, 0) == -1)
343 ERR ("failed to get size of iflist\n");
346 buf
= HeapAlloc (GetProcessHeap (), 0, needed
);
349 ret
= ERROR_OUTOFMEMORY
;
352 if(sysctl(mib
, MIB_LEN
, buf
, &needed
, NULL
, 0) == -1)
354 ERR ("failed to get iflist\n");
357 for ( end
= buf
+ needed
; buf
< end
; buf
+= ifm
->ifm_msglen
)
359 ifm
= (struct if_msghdr
*) buf
;
360 if(ifm
->ifm_type
== RTM_IFINFO
)
362 ifdata
= ifm
->ifm_data
;
363 entry
->dwMtu
= ifdata
.ifi_mtu
;
364 entry
->dwSpeed
= ifdata
.ifi_baudrate
;
365 entry
->dwInOctets
= ifdata
.ifi_ibytes
;
366 entry
->dwInErrors
= ifdata
.ifi_ierrors
;
367 entry
->dwInDiscards
= ifdata
.ifi_iqdrops
;
368 entry
->dwInUcastPkts
= ifdata
.ifi_ipackets
;
369 entry
->dwInNUcastPkts
= ifdata
.ifi_imcasts
;
370 entry
->dwOutOctets
= ifdata
.ifi_obytes
;
371 entry
->dwOutUcastPkts
= ifdata
.ifi_opackets
;
372 entry
->dwOutErrors
= ifdata
.ifi_oerrors
;
378 HeapFree (GetProcessHeap (), 0, buf
);
381 FIXME( "unimplemented\n" );
387 /******************************************************************
388 * GetIcmpStatistics (IPHLPAPI.@)
390 * Get the ICMP statistics for the local computer.
393 * stats [Out] buffer for ICMP statistics
397 * Failure: error code from winerror.h
399 DWORD WINAPI
GetIcmpStatistics(PMIB_ICMP stats
)
401 DWORD ret
= ERROR_NOT_SUPPORTED
;
403 if (!stats
) return ERROR_INVALID_PARAMETER
;
404 memset( stats
, 0, sizeof(MIB_ICMP
) );
410 if ((fp
= fopen("/proc/net/snmp", "r")))
412 static const char hdr
[] = "Icmp:";
415 while ((ptr
= fgets(buf
, sizeof(buf
), fp
)))
417 if (strncasecmp(buf
, hdr
, sizeof(hdr
) - 1)) continue;
418 /* last line was a header, get another */
419 if (!(ptr
= fgets(buf
, sizeof(buf
), fp
))) break;
420 if (!strncasecmp(buf
, hdr
, sizeof(hdr
) - 1))
423 sscanf( ptr
, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
424 &stats
->stats
.icmpInStats
.dwMsgs
,
425 &stats
->stats
.icmpInStats
.dwErrors
,
426 &stats
->stats
.icmpInStats
.dwDestUnreachs
,
427 &stats
->stats
.icmpInStats
.dwTimeExcds
,
428 &stats
->stats
.icmpInStats
.dwParmProbs
,
429 &stats
->stats
.icmpInStats
.dwSrcQuenchs
,
430 &stats
->stats
.icmpInStats
.dwRedirects
,
431 &stats
->stats
.icmpInStats
.dwEchoReps
,
432 &stats
->stats
.icmpInStats
.dwTimestamps
,
433 &stats
->stats
.icmpInStats
.dwTimestampReps
,
434 &stats
->stats
.icmpInStats
.dwAddrMasks
,
435 &stats
->stats
.icmpInStats
.dwAddrMaskReps
,
436 &stats
->stats
.icmpOutStats
.dwMsgs
,
437 &stats
->stats
.icmpOutStats
.dwErrors
,
438 &stats
->stats
.icmpOutStats
.dwDestUnreachs
,
439 &stats
->stats
.icmpOutStats
.dwTimeExcds
,
440 &stats
->stats
.icmpOutStats
.dwParmProbs
,
441 &stats
->stats
.icmpOutStats
.dwSrcQuenchs
,
442 &stats
->stats
.icmpOutStats
.dwRedirects
,
443 &stats
->stats
.icmpOutStats
.dwEchoReps
,
444 &stats
->stats
.icmpOutStats
.dwTimestamps
,
445 &stats
->stats
.icmpOutStats
.dwTimestampReps
,
446 &stats
->stats
.icmpOutStats
.dwAddrMasks
,
447 &stats
->stats
.icmpOutStats
.dwAddrMaskReps
);
455 #elif defined(HAVE_LIBKSTAT)
457 static char ip
[] = "ip", icmp
[] = "icmp";
461 if ((kc
= kstat_open()) &&
462 (ksp
= kstat_lookup( kc
, ip
, 0, icmp
)) &&
463 kstat_read( kc
, ksp
, NULL
) != -1 &&
464 ksp
->ks_type
== KSTAT_TYPE_NAMED
)
466 stats
->stats
.icmpInStats
.dwMsgs
= kstat_get_ui32( ksp
, "inMsgs" );
467 stats
->stats
.icmpInStats
.dwErrors
= kstat_get_ui32( ksp
, "inErrors" );
468 stats
->stats
.icmpInStats
.dwDestUnreachs
= kstat_get_ui32( ksp
, "inDestUnreachs" );
469 stats
->stats
.icmpInStats
.dwTimeExcds
= kstat_get_ui32( ksp
, "inTimeExcds" );
470 stats
->stats
.icmpInStats
.dwParmProbs
= kstat_get_ui32( ksp
, "inParmProbs" );
471 stats
->stats
.icmpInStats
.dwSrcQuenchs
= kstat_get_ui32( ksp
, "inSrcQuenchs" );
472 stats
->stats
.icmpInStats
.dwRedirects
= kstat_get_ui32( ksp
, "inRedirects" );
473 stats
->stats
.icmpInStats
.dwEchos
= kstat_get_ui32( ksp
, "inEchos" );
474 stats
->stats
.icmpInStats
.dwEchoReps
= kstat_get_ui32( ksp
, "inEchoReps" );
475 stats
->stats
.icmpInStats
.dwTimestamps
= kstat_get_ui32( ksp
, "inTimestamps" );
476 stats
->stats
.icmpInStats
.dwTimestampReps
= kstat_get_ui32( ksp
, "inTimestampReps" );
477 stats
->stats
.icmpInStats
.dwAddrMasks
= kstat_get_ui32( ksp
, "inAddrMasks" );
478 stats
->stats
.icmpInStats
.dwAddrMaskReps
= kstat_get_ui32( ksp
, "inAddrMaskReps" );
479 stats
->stats
.icmpOutStats
.dwMsgs
= kstat_get_ui32( ksp
, "outMsgs" );
480 stats
->stats
.icmpOutStats
.dwErrors
= kstat_get_ui32( ksp
, "outErrors" );
481 stats
->stats
.icmpOutStats
.dwDestUnreachs
= kstat_get_ui32( ksp
, "outDestUnreachs" );
482 stats
->stats
.icmpOutStats
.dwTimeExcds
= kstat_get_ui32( ksp
, "outTimeExcds" );
483 stats
->stats
.icmpOutStats
.dwParmProbs
= kstat_get_ui32( ksp
, "outParmProbs" );
484 stats
->stats
.icmpOutStats
.dwSrcQuenchs
= kstat_get_ui32( ksp
, "outSrcQuenchs" );
485 stats
->stats
.icmpOutStats
.dwRedirects
= kstat_get_ui32( ksp
, "outRedirects" );
486 stats
->stats
.icmpOutStats
.dwEchos
= kstat_get_ui32( ksp
, "outEchos" );
487 stats
->stats
.icmpOutStats
.dwEchoReps
= kstat_get_ui32( ksp
, "outEchoReps" );
488 stats
->stats
.icmpOutStats
.dwTimestamps
= kstat_get_ui32( ksp
, "outTimestamps" );
489 stats
->stats
.icmpOutStats
.dwTimestampReps
= kstat_get_ui32( ksp
, "outTimestampReps" );
490 stats
->stats
.icmpOutStats
.dwAddrMasks
= kstat_get_ui32( ksp
, "outAddrMasks" );
491 stats
->stats
.icmpOutStats
.dwAddrMaskReps
= kstat_get_ui32( ksp
, "outAddrMaskReps" );
494 if (kc
) kstat_close( kc
);
496 #elif defined(HAVE_SYS_SYSCTL_H) && defined(ICMPCTL_STATS)
498 int mib
[] = {CTL_NET
, PF_INET
, IPPROTO_ICMP
, ICMPCTL_STATS
};
499 #define MIB_LEN (sizeof(mib) / sizeof(mib[0]))
500 struct icmpstat icmp_stat
;
501 size_t needed
= sizeof(icmp_stat
);
504 if(sysctl(mib
, MIB_LEN
, &icmp_stat
, &needed
, NULL
, 0) != -1)
507 stats
->stats
.icmpInStats
.dwMsgs
= icmp_stat
.icps_badcode
+ icmp_stat
.icps_checksum
+ icmp_stat
.icps_tooshort
+ icmp_stat
.icps_badlen
;
508 for(i
= 0; i
<= ICMP_MAXTYPE
; i
++)
509 stats
->stats
.icmpInStats
.dwMsgs
+= icmp_stat
.icps_inhist
[i
];
511 stats
->stats
.icmpInStats
.dwErrors
= icmp_stat
.icps_badcode
+ icmp_stat
.icps_tooshort
+ icmp_stat
.icps_checksum
+ icmp_stat
.icps_badlen
;
513 stats
->stats
.icmpInStats
.dwDestUnreachs
= icmp_stat
.icps_inhist
[ICMP_UNREACH
];
514 stats
->stats
.icmpInStats
.dwTimeExcds
= icmp_stat
.icps_inhist
[ICMP_TIMXCEED
];
515 stats
->stats
.icmpInStats
.dwParmProbs
= icmp_stat
.icps_inhist
[ICMP_PARAMPROB
];
516 stats
->stats
.icmpInStats
.dwSrcQuenchs
= icmp_stat
.icps_inhist
[ICMP_SOURCEQUENCH
];
517 stats
->stats
.icmpInStats
.dwRedirects
= icmp_stat
.icps_inhist
[ICMP_REDIRECT
];
518 stats
->stats
.icmpInStats
.dwEchos
= icmp_stat
.icps_inhist
[ICMP_ECHO
];
519 stats
->stats
.icmpInStats
.dwEchoReps
= icmp_stat
.icps_inhist
[ICMP_ECHOREPLY
];
520 stats
->stats
.icmpInStats
.dwTimestamps
= icmp_stat
.icps_inhist
[ICMP_TSTAMP
];
521 stats
->stats
.icmpInStats
.dwTimestampReps
= icmp_stat
.icps_inhist
[ICMP_TSTAMPREPLY
];
522 stats
->stats
.icmpInStats
.dwAddrMasks
= icmp_stat
.icps_inhist
[ICMP_MASKREQ
];
523 stats
->stats
.icmpInStats
.dwAddrMaskReps
= icmp_stat
.icps_inhist
[ICMP_MASKREPLY
];
525 #ifdef HAVE_ICPS_OUTHIST
527 stats
->stats
.icmpOutStats
.dwMsgs
= icmp_stat
.icps_oldshort
+ icmp_stat
.icps_oldicmp
;
528 for(i
= 0; i
<= ICMP_MAXTYPE
; i
++)
529 stats
->stats
.icmpOutStats
.dwMsgs
+= icmp_stat
.icps_outhist
[i
];
531 stats
->stats
.icmpOutStats
.dwErrors
= icmp_stat
.icps_oldshort
+ icmp_stat
.icps_oldicmp
;
533 stats
->stats
.icmpOutStats
.dwDestUnreachs
= icmp_stat
.icps_outhist
[ICMP_UNREACH
];
534 stats
->stats
.icmpOutStats
.dwTimeExcds
= icmp_stat
.icps_outhist
[ICMP_TIMXCEED
];
535 stats
->stats
.icmpOutStats
.dwParmProbs
= icmp_stat
.icps_outhist
[ICMP_PARAMPROB
];
536 stats
->stats
.icmpOutStats
.dwSrcQuenchs
= icmp_stat
.icps_outhist
[ICMP_SOURCEQUENCH
];
537 stats
->stats
.icmpOutStats
.dwRedirects
= icmp_stat
.icps_outhist
[ICMP_REDIRECT
];
538 stats
->stats
.icmpOutStats
.dwEchos
= icmp_stat
.icps_outhist
[ICMP_ECHO
];
539 stats
->stats
.icmpOutStats
.dwEchoReps
= icmp_stat
.icps_outhist
[ICMP_ECHOREPLY
];
540 stats
->stats
.icmpOutStats
.dwTimestamps
= icmp_stat
.icps_outhist
[ICMP_TSTAMP
];
541 stats
->stats
.icmpOutStats
.dwTimestampReps
= icmp_stat
.icps_outhist
[ICMP_TSTAMPREPLY
];
542 stats
->stats
.icmpOutStats
.dwAddrMasks
= icmp_stat
.icps_outhist
[ICMP_MASKREQ
];
543 stats
->stats
.icmpOutStats
.dwAddrMaskReps
= icmp_stat
.icps_outhist
[ICMP_MASKREPLY
];
544 #endif /* ICPS_OUTHIST */
548 #else /* ICMPCTL_STATS */
549 FIXME( "unimplemented\n" );
555 /******************************************************************
556 * GetIpStatistics (IPHLPAPI.@)
558 * Get the IP statistics for the local computer.
561 * stats [Out] buffer for IP statistics
565 * Failure: error code from winerror.h
567 DWORD WINAPI
GetIpStatistics(PMIB_IPSTATS stats
)
569 DWORD ret
= ERROR_NOT_SUPPORTED
;
570 MIB_IPFORWARDTABLE
*fwd_table
;
572 if (!stats
) return ERROR_INVALID_PARAMETER
;
573 memset( stats
, 0, sizeof(*stats
) );
575 stats
->dwNumIf
= stats
->dwNumAddr
= getNumInterfaces();
576 if (!AllocateAndGetIpForwardTableFromStack( &fwd_table
, FALSE
, GetProcessHeap(), 0 ))
578 stats
->dwNumRoutes
= fwd_table
->dwNumEntries
;
579 HeapFree( GetProcessHeap(), 0, fwd_table
);
586 if ((fp
= fopen("/proc/net/snmp", "r")))
588 static const char hdr
[] = "Ip:";
591 while ((ptr
= fgets(buf
, sizeof(buf
), fp
)))
593 if (strncasecmp(buf
, hdr
, sizeof(hdr
) - 1)) continue;
594 /* last line was a header, get another */
595 if (!(ptr
= fgets(buf
, sizeof(buf
), fp
))) break;
596 if (!strncasecmp(buf
, hdr
, sizeof(hdr
) - 1))
599 sscanf( ptr
, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
600 &stats
->u
.dwForwarding
,
601 &stats
->dwDefaultTTL
,
602 &stats
->dwInReceives
,
603 &stats
->dwInHdrErrors
,
604 &stats
->dwInAddrErrors
,
605 &stats
->dwForwDatagrams
,
606 &stats
->dwInUnknownProtos
,
607 &stats
->dwInDiscards
,
608 &stats
->dwInDelivers
,
609 &stats
->dwOutRequests
,
610 &stats
->dwOutDiscards
,
611 &stats
->dwOutNoRoutes
,
612 &stats
->dwReasmTimeout
,
613 &stats
->dwReasmReqds
,
615 &stats
->dwReasmFails
,
618 &stats
->dwFragCreates
);
619 /* hmm, no routingDiscards */
627 #elif defined(HAVE_LIBKSTAT)
629 static char ip
[] = "ip";
633 if ((kc
= kstat_open()) &&
634 (ksp
= kstat_lookup( kc
, ip
, 0, ip
)) &&
635 kstat_read( kc
, ksp
, NULL
) != -1 &&
636 ksp
->ks_type
== KSTAT_TYPE_NAMED
)
638 stats
->u
.dwForwarding
= kstat_get_ui32( ksp
, "forwarding" );
639 stats
->dwDefaultTTL
= kstat_get_ui32( ksp
, "defaultTTL" );
640 stats
->dwInReceives
= kstat_get_ui32( ksp
, "inReceives" );
641 stats
->dwInHdrErrors
= kstat_get_ui32( ksp
, "inHdrErrors" );
642 stats
->dwInAddrErrors
= kstat_get_ui32( ksp
, "inAddrErrors" );
643 stats
->dwForwDatagrams
= kstat_get_ui32( ksp
, "forwDatagrams" );
644 stats
->dwInUnknownProtos
= kstat_get_ui32( ksp
, "inUnknownProtos" );
645 stats
->dwInDiscards
= kstat_get_ui32( ksp
, "inDiscards" );
646 stats
->dwInDelivers
= kstat_get_ui32( ksp
, "inDelivers" );
647 stats
->dwOutRequests
= kstat_get_ui32( ksp
, "outRequests" );
648 stats
->dwRoutingDiscards
= kstat_get_ui32( ksp
, "routingDiscards" );
649 stats
->dwOutDiscards
= kstat_get_ui32( ksp
, "outDiscards" );
650 stats
->dwOutNoRoutes
= kstat_get_ui32( ksp
, "outNoRoutes" );
651 stats
->dwReasmTimeout
= kstat_get_ui32( ksp
, "reasmTimeout" );
652 stats
->dwReasmReqds
= kstat_get_ui32( ksp
, "reasmReqds" );
653 stats
->dwReasmOks
= kstat_get_ui32( ksp
, "reasmOKs" );
654 stats
->dwReasmFails
= kstat_get_ui32( ksp
, "reasmFails" );
655 stats
->dwFragOks
= kstat_get_ui32( ksp
, "fragOKs" );
656 stats
->dwFragFails
= kstat_get_ui32( ksp
, "fragFails" );
657 stats
->dwFragCreates
= kstat_get_ui32( ksp
, "fragCreates" );
660 if (kc
) kstat_close( kc
);
662 #elif defined(HAVE_SYS_SYSCTL_H) && defined(IPCTL_STATS)
664 int mib
[] = {CTL_NET
, PF_INET
, IPPROTO_IP
, IPCTL_STATS
};
665 #define MIB_LEN (sizeof(mib) / sizeof(mib[0]))
666 int ip_ttl
, ip_forwarding
;
667 struct ipstat ip_stat
;
670 needed
= sizeof(ip_stat
);
671 if(sysctl(mib
, MIB_LEN
, &ip_stat
, &needed
, NULL
, 0) == -1)
673 ERR ("failed to get ipstat\n");
674 return ERROR_NOT_SUPPORTED
;
677 needed
= sizeof(ip_ttl
);
678 if (sysctlbyname ("net.inet.ip.ttl", &ip_ttl
, &needed
, NULL
, 0) == -1)
680 ERR ("failed to get ip Default TTL\n");
681 return ERROR_NOT_SUPPORTED
;
684 needed
= sizeof(ip_forwarding
);
685 if (sysctlbyname ("net.inet.ip.forwarding", &ip_forwarding
, &needed
, NULL
, 0) == -1)
687 ERR ("failed to get ip forwarding\n");
688 return ERROR_NOT_SUPPORTED
;
691 stats
->u
.dwForwarding
= ip_forwarding
;
692 stats
->dwDefaultTTL
= ip_ttl
;
693 stats
->dwInDelivers
= ip_stat
.ips_delivered
;
694 stats
->dwInHdrErrors
= ip_stat
.ips_badhlen
+ ip_stat
.ips_badsum
+ ip_stat
.ips_tooshort
+ ip_stat
.ips_badlen
;
695 stats
->dwInAddrErrors
= ip_stat
.ips_cantforward
;
696 stats
->dwInReceives
= ip_stat
.ips_total
;
697 stats
->dwForwDatagrams
= ip_stat
.ips_forward
;
698 stats
->dwInUnknownProtos
= ip_stat
.ips_noproto
;
699 stats
->dwInDiscards
= ip_stat
.ips_fragdropped
;
700 stats
->dwOutDiscards
= ip_stat
.ips_odropped
;
701 stats
->dwReasmOks
= ip_stat
.ips_reassembled
;
702 stats
->dwFragOks
= ip_stat
.ips_fragmented
;
703 stats
->dwFragFails
= ip_stat
.ips_cantfrag
;
704 stats
->dwReasmTimeout
= ip_stat
.ips_fragtimeout
;
705 stats
->dwOutNoRoutes
= ip_stat
.ips_noroute
;
706 stats
->dwOutRequests
= ip_stat
.ips_localout
;
707 stats
->dwReasmReqds
= ip_stat
.ips_fragments
;
711 FIXME( "unimplemented\n" );
717 /******************************************************************
718 * GetTcpStatistics (IPHLPAPI.@)
720 * Get the TCP statistics for the local computer.
723 * stats [Out] buffer for TCP statistics
727 * Failure: error code from winerror.h
729 DWORD WINAPI
GetTcpStatistics(PMIB_TCPSTATS stats
)
731 DWORD ret
= ERROR_NOT_SUPPORTED
;
733 if (!stats
) return ERROR_INVALID_PARAMETER
;
734 memset( stats
, 0, sizeof(*stats
) );
740 if ((fp
= fopen("/proc/net/snmp", "r")))
742 static const char hdr
[] = "Tcp:";
743 MIB_TCPTABLE
*tcp_table
;
746 while ((ptr
= fgets(buf
, sizeof(buf
), fp
)))
748 if (strncasecmp(buf
, hdr
, sizeof(hdr
) - 1)) continue;
749 /* last line was a header, get another */
750 if (!(ptr
= fgets(buf
, sizeof(buf
), fp
))) break;
751 if (!strncasecmp(buf
, hdr
, sizeof(hdr
) - 1))
754 sscanf( ptr
, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u",
755 &stats
->u
.dwRtoAlgorithm
,
759 &stats
->dwActiveOpens
,
760 &stats
->dwPassiveOpens
,
761 &stats
->dwAttemptFails
,
762 &stats
->dwEstabResets
,
766 &stats
->dwRetransSegs
,
772 if (!AllocateAndGetTcpTableFromStack( &tcp_table
, FALSE
, GetProcessHeap(), 0 ))
774 stats
->dwNumConns
= tcp_table
->dwNumEntries
;
775 HeapFree( GetProcessHeap(), 0, tcp_table
);
781 #elif defined(HAVE_LIBKSTAT)
783 static char tcp
[] = "tcp";
787 if ((kc
= kstat_open()) &&
788 (ksp
= kstat_lookup( kc
, tcp
, 0, tcp
)) &&
789 kstat_read( kc
, ksp
, NULL
) != -1 &&
790 ksp
->ks_type
== KSTAT_TYPE_NAMED
)
792 stats
->u
.dwRtoAlgorithm
= kstat_get_ui32( ksp
, "rtoAlgorithm" );
793 stats
->dwRtoMin
= kstat_get_ui32( ksp
, "rtoMin" );
794 stats
->dwRtoMax
= kstat_get_ui32( ksp
, "rtoMax" );
795 stats
->dwMaxConn
= kstat_get_ui32( ksp
, "maxConn" );
796 stats
->dwActiveOpens
= kstat_get_ui32( ksp
, "activeOpens" );
797 stats
->dwPassiveOpens
= kstat_get_ui32( ksp
, "passiveOpens" );
798 stats
->dwAttemptFails
= kstat_get_ui32( ksp
, "attemptFails" );
799 stats
->dwEstabResets
= kstat_get_ui32( ksp
, "estabResets" );
800 stats
->dwCurrEstab
= kstat_get_ui32( ksp
, "currEstab" );
801 stats
->dwInSegs
= kstat_get_ui32( ksp
, "inSegs" );
802 stats
->dwOutSegs
= kstat_get_ui32( ksp
, "outSegs" );
803 stats
->dwRetransSegs
= kstat_get_ui32( ksp
, "retransSegs" );
804 stats
->dwInErrs
= kstat_get_ui32( ksp
, "inErrs" );
805 stats
->dwOutRsts
= kstat_get_ui32( ksp
, "outRsts" );
806 stats
->dwNumConns
= kstat_get_ui32( ksp
, "connTableSize" );
809 if (kc
) kstat_close( kc
);
811 #elif defined(HAVE_SYS_SYSCTL_H) && defined(UDPCTL_STATS)
813 #ifndef TCPTV_MIN /* got removed in Mac OS X for some reason */
815 #define TCPTV_REXMTMAX 128
817 int mib
[] = {CTL_NET
, PF_INET
, IPPROTO_TCP
, TCPCTL_STATS
};
818 #define MIB_LEN (sizeof(mib) / sizeof(mib[0]))
820 struct tcpstat tcp_stat
;
821 size_t needed
= sizeof(tcp_stat
);
823 if(sysctl(mib
, MIB_LEN
, &tcp_stat
, &needed
, NULL
, 0) != -1)
825 stats
->u
.RtoAlgorithm
= MIB_TCP_RTO_VANJ
;
826 stats
->dwRtoMin
= TCPTV_MIN
;
827 stats
->dwRtoMax
= TCPTV_REXMTMAX
;
828 stats
->dwMaxConn
= -1;
829 stats
->dwActiveOpens
= tcp_stat
.tcps_connattempt
;
830 stats
->dwPassiveOpens
= tcp_stat
.tcps_accepts
;
831 stats
->dwAttemptFails
= tcp_stat
.tcps_conndrops
;
832 stats
->dwEstabResets
= tcp_stat
.tcps_drops
;
833 stats
->dwCurrEstab
= 0;
834 stats
->dwInSegs
= tcp_stat
.tcps_rcvtotal
;
835 stats
->dwOutSegs
= tcp_stat
.tcps_sndtotal
- tcp_stat
.tcps_sndrexmitpack
;
836 stats
->dwRetransSegs
= tcp_stat
.tcps_sndrexmitpack
;
837 stats
->dwInErrs
= tcp_stat
.tcps_rcvbadsum
+ tcp_stat
.tcps_rcvbadoff
+ tcp_stat
.tcps_rcvmemdrop
+ tcp_stat
.tcps_rcvshort
;
838 stats
->dwOutRsts
= tcp_stat
.tcps_sndctrl
- tcp_stat
.tcps_closed
;
839 stats
->dwNumConns
= tcp_stat
.tcps_connects
;
842 else ERR ("failed to get tcpstat\n");
845 FIXME( "unimplemented\n" );
851 /******************************************************************
852 * GetUdpStatistics (IPHLPAPI.@)
854 * Get the UDP statistics for the local computer.
857 * stats [Out] buffer for UDP statistics
861 * Failure: error code from winerror.h
863 DWORD WINAPI
GetUdpStatistics(PMIB_UDPSTATS stats
)
865 DWORD ret
= ERROR_NOT_SUPPORTED
;
867 if (!stats
) return ERROR_INVALID_PARAMETER
;
868 memset( stats
, 0, sizeof(*stats
) );
874 if ((fp
= fopen("/proc/net/snmp", "r")))
876 static const char hdr
[] = "Udp:";
879 while ((ptr
= fgets(buf
, sizeof(buf
), fp
)))
881 if (strncasecmp(buf
, hdr
, sizeof(hdr
) - 1)) continue;
882 /* last line was a header, get another */
883 if (!(ptr
= fgets(buf
, sizeof(buf
), fp
))) break;
884 if (!strncasecmp(buf
, hdr
, sizeof(hdr
) - 1))
887 sscanf( ptr
, "%u %u %u %u %u",
888 &stats
->dwInDatagrams
, &stats
->dwNoPorts
,
889 &stats
->dwInErrors
, &stats
->dwOutDatagrams
, &stats
->dwNumAddrs
);
897 #elif defined(HAVE_LIBKSTAT)
899 static char udp
[] = "udp";
902 MIB_UDPTABLE
*udp_table
;
904 if ((kc
= kstat_open()) &&
905 (ksp
= kstat_lookup( kc
, udp
, 0, udp
)) &&
906 kstat_read( kc
, ksp
, NULL
) != -1 &&
907 ksp
->ks_type
== KSTAT_TYPE_NAMED
)
909 stats
->dwInDatagrams
= kstat_get_ui32( ksp
, "inDatagrams" );
910 stats
->dwNoPorts
= 0; /* FIXME */
911 stats
->dwInErrors
= kstat_get_ui32( ksp
, "inErrors" );
912 stats
->dwOutDatagrams
= kstat_get_ui32( ksp
, "outDatagrams" );
913 if (!AllocateAndGetUdpTableFromStack( &udp_table
, FALSE
, GetProcessHeap(), 0 ))
915 stats
->dwNumAddrs
= udp_table
->dwNumEntries
;
916 HeapFree( GetProcessHeap(), 0, udp_table
);
920 if (kc
) kstat_close( kc
);
922 #elif defined(HAVE_SYS_SYSCTL_H) && defined(UDPCTL_STATS)
924 int mib
[] = {CTL_NET
, PF_INET
, IPPROTO_UDP
, UDPCTL_STATS
};
925 #define MIB_LEN (sizeof(mib) / sizeof(mib[0]))
926 struct udpstat udp_stat
;
927 MIB_UDPTABLE
*udp_table
;
928 size_t needed
= sizeof(udp_stat
);
930 if(sysctl(mib
, MIB_LEN
, &udp_stat
, &needed
, NULL
, 0) != -1)
932 stats
->dwInDatagrams
= udp_stat
.udps_ipackets
;
933 stats
->dwOutDatagrams
= udp_stat
.udps_opackets
;
934 stats
->dwNoPorts
= udp_stat
.udps_noport
;
935 stats
->dwInErrors
= udp_stat
.udps_hdrops
+ udp_stat
.udps_badsum
+ udp_stat
.udps_fullsock
+ udp_stat
.udps_badlen
;
936 if (!AllocateAndGetUdpTableFromStack( &udp_table
, FALSE
, GetProcessHeap(), 0 ))
938 stats
->dwNumAddrs
= udp_table
->dwNumEntries
;
939 HeapFree( GetProcessHeap(), 0, udp_table
);
943 else ERR ("failed to get udpstat\n");
946 FIXME( "unimplemented\n" );
952 static MIB_IPFORWARDTABLE
*append_ipforward_row( HANDLE heap
, DWORD flags
, MIB_IPFORWARDTABLE
*table
,
953 DWORD
*count
, const MIB_IPFORWARDROW
*row
)
955 if (table
->dwNumEntries
>= *count
)
957 MIB_IPFORWARDTABLE
*new_table
;
958 DWORD new_count
= table
->dwNumEntries
* 2;
960 if (!(new_table
= HeapReAlloc( heap
, flags
, table
,
961 FIELD_OFFSET(MIB_IPFORWARDTABLE
, table
[new_count
] ))))
963 HeapFree( heap
, 0, table
);
969 memcpy( &table
->table
[table
->dwNumEntries
++], row
, sizeof(*row
) );
973 static int compare_ipforward_rows(const void *a
, const void *b
)
975 const MIB_IPFORWARDROW
*rowA
= a
;
976 const MIB_IPFORWARDROW
*rowB
= b
;
979 if ((ret
= rowA
->dwForwardDest
- rowB
->dwForwardDest
) != 0) return ret
;
980 if ((ret
= rowA
->u2
.dwForwardProto
- rowB
->u2
.dwForwardProto
) != 0) return ret
;
981 if ((ret
= rowA
->dwForwardPolicy
- rowB
->dwForwardPolicy
) != 0) return ret
;
982 return rowA
->dwForwardNextHop
- rowB
->dwForwardNextHop
;
985 /******************************************************************
986 * AllocateAndGetIpForwardTableFromStack (IPHLPAPI.@)
988 * Get the route table.
989 * Like GetIpForwardTable(), but allocate the returned table from heap.
992 * ppIpForwardTable [Out] pointer into which the MIB_IPFORWARDTABLE is
993 * allocated and returned.
994 * bOrder [In] whether to sort the table
995 * heap [In] heap from which the table is allocated
996 * flags [In] flags to HeapAlloc
999 * ERROR_INVALID_PARAMETER if ppIfTable is NULL, other error codes
1000 * on failure, NO_ERROR on success.
1002 DWORD WINAPI
AllocateAndGetIpForwardTableFromStack(PMIB_IPFORWARDTABLE
*ppIpForwardTable
, BOOL bOrder
,
1003 HANDLE heap
, DWORD flags
)
1005 MIB_IPFORWARDTABLE
*table
;
1006 MIB_IPFORWARDROW row
;
1007 DWORD ret
= NO_ERROR
, count
= 16;
1009 TRACE("table %p, bOrder %d, heap %p, flags 0x%08x\n", ppIpForwardTable
, bOrder
, heap
, flags
);
1011 if (!ppIpForwardTable
) return ERROR_INVALID_PARAMETER
;
1013 if (!(table
= HeapAlloc( heap
, flags
, FIELD_OFFSET(MIB_IPFORWARDTABLE
, table
[count
] ))))
1014 return ERROR_OUTOFMEMORY
;
1016 table
->dwNumEntries
= 0;
1022 if ((fp
= fopen("/proc/net/route", "r")))
1024 char buf
[512], *ptr
;
1027 /* skip header line */
1028 ptr
= fgets(buf
, sizeof(buf
), fp
);
1029 while ((ptr
= fgets(buf
, sizeof(buf
), fp
)))
1031 memset( &row
, 0, sizeof(row
) );
1033 while (!isspace(*ptr
)) ptr
++;
1035 if (getInterfaceIndexByName(buf
, &row
.dwForwardIfIndex
) != NO_ERROR
)
1038 row
.dwForwardDest
= strtoul(ptr
, &ptr
, 16);
1039 row
.dwForwardNextHop
= strtoul(ptr
+ 1, &ptr
, 16);
1040 flags
= strtoul(ptr
+ 1, &ptr
, 16);
1042 if (!(flags
& RTF_UP
)) row
.u1
.ForwardType
= MIB_IPROUTE_TYPE_INVALID
;
1043 else if (flags
& RTF_GATEWAY
) row
.u1
.ForwardType
= MIB_IPROUTE_TYPE_INDIRECT
;
1044 else row
.u1
.ForwardType
= MIB_IPROUTE_TYPE_DIRECT
;
1046 strtoul(ptr
+ 1, &ptr
, 16); /* refcount, skip */
1047 strtoul(ptr
+ 1, &ptr
, 16); /* use, skip */
1048 row
.dwForwardMetric1
= strtoul(ptr
+ 1, &ptr
, 16);
1049 row
.dwForwardMask
= strtoul(ptr
+ 1, &ptr
, 16);
1050 /* FIXME: other protos might be appropriate, e.g. the default
1051 * route is typically set with MIB_IPPROTO_NETMGMT instead */
1052 row
.u2
.ForwardProto
= MIB_IPPROTO_LOCAL
;
1054 if (!(table
= append_ipforward_row( heap
, flags
, table
, &count
, &row
)))
1059 else ret
= ERROR_NOT_SUPPORTED
;
1061 #elif defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
1064 int fd
, len
, namelen
;
1065 mib2_ipRouteEntry_t
*entry
;
1068 if ((fd
= open_streams_mib( NULL
)) != -1)
1070 if ((data
= read_mib_entry( fd
, MIB2_IP
, MIB2_IP_ROUTE
, &len
)))
1072 for (entry
= data
; (char *)(entry
+ 1) <= (char *)data
+ len
; entry
++)
1074 row
.dwForwardDest
= entry
->ipRouteDest
;
1075 row
.dwForwardMask
= entry
->ipRouteMask
;
1076 row
.dwForwardPolicy
= 0;
1077 row
.dwForwardNextHop
= entry
->ipRouteNextHop
;
1078 row
.u1
.dwForwardType
= entry
->ipRouteType
;
1079 row
.u2
.dwForwardProto
= entry
->ipRouteProto
;
1080 row
.dwForwardAge
= entry
->ipRouteAge
;
1081 row
.dwForwardNextHopAS
= 0;
1082 row
.dwForwardMetric1
= entry
->ipRouteMetric1
;
1083 row
.dwForwardMetric2
= entry
->ipRouteMetric2
;
1084 row
.dwForwardMetric3
= entry
->ipRouteMetric3
;
1085 row
.dwForwardMetric4
= entry
->ipRouteMetric4
;
1086 row
.dwForwardMetric5
= entry
->ipRouteMetric5
;
1087 namelen
= min( sizeof(name
) - 1, entry
->ipRouteIfIndex
.o_length
);
1088 memcpy( name
, entry
->ipRouteIfIndex
.o_bytes
, namelen
);
1090 getInterfaceIndexByName( name
, &row
.dwForwardIfIndex
);
1091 if (!(table
= append_ipforward_row( heap
, flags
, table
, &count
, &row
))) break;
1093 HeapFree( GetProcessHeap(), 0, data
);
1097 else ret
= ERROR_NOT_SUPPORTED
;
1099 #elif defined(HAVE_SYS_SYSCTL_H) && defined(NET_RT_DUMP)
1101 int mib
[6] = {CTL_NET
, PF_ROUTE
, 0, PF_INET
, NET_RT_DUMP
, 0};
1103 char *buf
= NULL
, *lim
, *next
, *addrPtr
;
1104 struct rt_msghdr
*rtm
;
1106 if (sysctl (mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
1108 ERR ("sysctl 1 failed!\n");
1109 ret
= ERROR_NOT_SUPPORTED
;
1113 buf
= HeapAlloc (GetProcessHeap (), 0, needed
);
1116 ret
= ERROR_OUTOFMEMORY
;
1120 if (sysctl (mib
, 6, buf
, &needed
, NULL
, 0) < 0)
1122 ret
= ERROR_NOT_SUPPORTED
;
1127 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
)
1131 rtm
= (struct rt_msghdr
*)next
;
1133 if (rtm
->rtm_type
!= RTM_GET
)
1135 WARN ("Got unexpected message type 0x%x!\n",
1140 /* Ignore all entries except for gateway routes which aren't
1142 if (!(rtm
->rtm_flags
& RTF_GATEWAY
) ||
1143 (rtm
->rtm_flags
& RTF_MULTICAST
))
1146 memset( &row
, 0, sizeof(row
) );
1147 row
.dwForwardIfIndex
= rtm
->rtm_index
;
1148 row
.u1
.ForwardType
= MIB_IPROUTE_TYPE_INDIRECT
;
1149 row
.dwForwardMetric1
= rtm
->rtm_rmx
.rmx_hopcount
;
1150 row
.u2
.ForwardProto
= MIB_IPPROTO_LOCAL
;
1152 addrPtr
= (char *)(rtm
+ 1);
1154 for (i
= 1; i
; i
<<= 1)
1156 struct sockaddr
*sa
;
1159 if (!(i
& rtm
->rtm_addrs
))
1162 sa
= (struct sockaddr
*)addrPtr
;
1163 ADVANCE (addrPtr
, sa
);
1165 /* default routes are encoded by length-zero sockaddr */
1166 if (sa
->sa_len
== 0)
1168 else if (sa
->sa_family
!= AF_INET
)
1170 WARN ("Received unsupported sockaddr family 0x%x\n",
1176 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
1178 addr
= sin
->sin_addr
.s_addr
;
1183 case RTA_DST
: row
.dwForwardDest
= addr
; break;
1184 case RTA_GATEWAY
: row
.dwForwardNextHop
= addr
; break;
1185 case RTA_NETMASK
: row
.dwForwardMask
= addr
; break;
1187 WARN ("Unexpected address type 0x%x\n", i
);
1191 if (!(table
= append_ipforward_row( heap
, flags
, table
, &count
, &row
)))
1195 HeapFree( GetProcessHeap (), 0, buf
);
1198 FIXME( "not implemented\n" );
1199 ret
= ERROR_NOT_SUPPORTED
;
1202 if (!table
) return ERROR_OUTOFMEMORY
;
1205 if (bOrder
&& table
->dwNumEntries
)
1206 qsort( table
->table
, table
->dwNumEntries
, sizeof(row
), compare_ipforward_rows
);
1207 *ppIpForwardTable
= table
;
1209 else HeapFree( heap
, flags
, table
);
1210 TRACE( "returning ret %u table %p\n", ret
, table
);
1214 static MIB_IPNETTABLE
*append_ipnet_row( HANDLE heap
, DWORD flags
, MIB_IPNETTABLE
*table
,
1215 DWORD
*count
, const MIB_IPNETROW
*row
)
1217 if (table
->dwNumEntries
>= *count
)
1219 MIB_IPNETTABLE
*new_table
;
1220 DWORD new_count
= table
->dwNumEntries
* 2;
1222 if (!(new_table
= HeapReAlloc( heap
, flags
, table
,
1223 FIELD_OFFSET(MIB_IPNETTABLE
, table
[new_count
] ))))
1225 HeapFree( heap
, 0, table
);
1231 memcpy( &table
->table
[table
->dwNumEntries
++], row
, sizeof(*row
) );
1235 static int compare_ipnet_rows(const void *a
, const void *b
)
1237 const MIB_IPNETROW
*rowA
= a
;
1238 const MIB_IPNETROW
*rowB
= b
;
1240 return ntohl(rowA
->dwAddr
) - ntohl(rowB
->dwAddr
);
1244 /******************************************************************
1245 * AllocateAndGetIpNetTableFromStack (IPHLPAPI.@)
1247 * Get the IP-to-physical address mapping table.
1248 * Like GetIpNetTable(), but allocate the returned table from heap.
1251 * ppIpNetTable [Out] pointer into which the MIB_IPNETTABLE is
1252 * allocated and returned.
1253 * bOrder [In] whether to sort the table
1254 * heap [In] heap from which the table is allocated
1255 * flags [In] flags to HeapAlloc
1258 * ERROR_INVALID_PARAMETER if ppIpNetTable is NULL, other error codes
1259 * on failure, NO_ERROR on success.
1261 DWORD WINAPI
AllocateAndGetIpNetTableFromStack(PMIB_IPNETTABLE
*ppIpNetTable
, BOOL bOrder
,
1262 HANDLE heap
, DWORD flags
)
1264 MIB_IPNETTABLE
*table
;
1266 DWORD ret
= NO_ERROR
, count
= 16;
1268 TRACE("table %p, bOrder %d, heap %p, flags 0x%08x\n", ppIpNetTable
, bOrder
, heap
, flags
);
1270 if (!ppIpNetTable
) return ERROR_INVALID_PARAMETER
;
1272 if (!(table
= HeapAlloc( heap
, flags
, FIELD_OFFSET(MIB_IPNETTABLE
, table
[count
] ))))
1273 return ERROR_OUTOFMEMORY
;
1275 table
->dwNumEntries
= 0;
1281 if ((fp
= fopen("/proc/net/arp", "r")))
1283 char buf
[512], *ptr
;
1286 /* skip header line */
1287 ptr
= fgets(buf
, sizeof(buf
), fp
);
1288 while ((ptr
= fgets(buf
, sizeof(buf
), fp
)))
1290 memset( &row
, 0, sizeof(row
) );
1292 row
.dwAddr
= inet_addr(ptr
);
1293 while (*ptr
&& !isspace(*ptr
)) ptr
++;
1294 strtoul(ptr
+ 1, &ptr
, 16); /* hw type (skip) */
1295 flags
= strtoul(ptr
+ 1, &ptr
, 16);
1298 if (flags
& ATF_COM
) row
.u
.Type
= MIB_IPNET_TYPE_DYNAMIC
;
1302 if (flags
& ATF_PERM
) row
.u
.Type
= MIB_IPNET_TYPE_STATIC
;
1305 row
.u
.Type
= MIB_IPNET_TYPE_OTHER
;
1307 while (*ptr
&& isspace(*ptr
)) ptr
++;
1308 while (*ptr
&& !isspace(*ptr
))
1310 row
.bPhysAddr
[row
.dwPhysAddrLen
++] = strtoul(ptr
, &ptr
, 16);
1313 while (*ptr
&& isspace(*ptr
)) ptr
++;
1314 while (*ptr
&& !isspace(*ptr
)) ptr
++; /* mask (skip) */
1315 while (*ptr
&& isspace(*ptr
)) ptr
++;
1316 getInterfaceIndexByName(ptr
, &row
.dwIndex
);
1318 if (!(table
= append_ipnet_row( heap
, flags
, table
, &count
, &row
)))
1323 else ret
= ERROR_NOT_SUPPORTED
;
1325 #elif defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
1328 int fd
, len
, namelen
;
1329 mib2_ipNetToMediaEntry_t
*entry
;
1332 if ((fd
= open_streams_mib( NULL
)) != -1)
1334 if ((data
= read_mib_entry( fd
, MIB2_IP
, MIB2_IP_MEDIA
, &len
)))
1336 for (entry
= data
; (char *)(entry
+ 1) <= (char *)data
+ len
; entry
++)
1338 row
.dwPhysAddrLen
= min( entry
->ipNetToMediaPhysAddress
.o_length
, MAXLEN_PHYSADDR
);
1339 memcpy( row
.bPhysAddr
, entry
->ipNetToMediaPhysAddress
.o_bytes
, row
.dwPhysAddrLen
);
1340 row
.dwAddr
= entry
->ipNetToMediaNetAddress
;
1341 row
.u
.Type
= entry
->ipNetToMediaType
;
1342 namelen
= min( sizeof(name
) - 1, entry
->ipNetToMediaIfIndex
.o_length
);
1343 memcpy( name
, entry
->ipNetToMediaIfIndex
.o_bytes
, namelen
);
1345 getInterfaceIndexByName( name
, &row
.dwIndex
);
1346 if (!(table
= append_ipnet_row( heap
, flags
, table
, &count
, &row
))) break;
1348 HeapFree( GetProcessHeap(), 0, data
);
1352 else ret
= ERROR_NOT_SUPPORTED
;
1354 #elif defined(HAVE_SYS_SYSCTL_H) && defined(NET_RT_DUMP)
1356 int mib
[] = {CTL_NET
, PF_ROUTE
, 0, AF_INET
, NET_RT_FLAGS
, RTF_LLINFO
};
1357 #define MIB_LEN (sizeof(mib) / sizeof(mib[0]))
1359 char *buf
= NULL
, *lim
, *next
;
1360 struct rt_msghdr
*rtm
;
1361 struct sockaddr_inarp
*sinarp
;
1362 struct sockaddr_dl
*sdl
;
1364 if (sysctl (mib
, MIB_LEN
, NULL
, &needed
, NULL
, 0) == -1)
1366 ERR ("failed to get arp table\n");
1367 ret
= ERROR_NOT_SUPPORTED
;
1371 buf
= HeapAlloc (GetProcessHeap (), 0, needed
);
1374 ret
= ERROR_OUTOFMEMORY
;
1378 if (sysctl (mib
, MIB_LEN
, buf
, &needed
, NULL
, 0) == -1)
1380 ret
= ERROR_NOT_SUPPORTED
;
1388 rtm
= (struct rt_msghdr
*)next
;
1389 sinarp
=(struct sockaddr_inarp
*)(rtm
+ 1);
1390 sdl
= (struct sockaddr_dl
*)((char *)sinarp
+ ROUNDUP(sinarp
->sin_len
));
1391 if(sdl
->sdl_alen
) /* arp entry */
1393 memset( &row
, 0, sizeof(row
) );
1394 row
.dwAddr
= sinarp
->sin_addr
.s_addr
;
1395 row
.dwIndex
= sdl
->sdl_index
;
1396 row
.dwPhysAddrLen
= min( 8, sdl
->sdl_alen
);
1397 memcpy( row
.bPhysAddr
, &sdl
->sdl_data
[sdl
->sdl_nlen
], row
.dwPhysAddrLen
);
1398 if(rtm
->rtm_rmx
.rmx_expire
== 0) row
.u
.Type
= MIB_IPNET_TYPE_STATIC
;
1399 else if(sinarp
->sin_other
& SIN_PROXY
) row
.u
.Type
= MIB_IPNET_TYPE_OTHER
;
1400 else if(rtm
->rtm_rmx
.rmx_expire
!= 0) row
.u
.Type
= MIB_IPNET_TYPE_DYNAMIC
;
1401 else row
.u
.Type
= MIB_IPNET_TYPE_INVALID
;
1403 if (!(table
= append_ipnet_row( heap
, flags
, table
, &count
, &row
)))
1406 next
+= rtm
->rtm_msglen
;
1409 HeapFree( GetProcessHeap (), 0, buf
);
1412 FIXME( "not implemented\n" );
1413 ret
= ERROR_NOT_SUPPORTED
;
1416 if (!table
) return ERROR_OUTOFMEMORY
;
1419 if (bOrder
&& table
->dwNumEntries
)
1420 qsort( table
->table
, table
->dwNumEntries
, sizeof(row
), compare_ipnet_rows
);
1421 *ppIpNetTable
= table
;
1423 else HeapFree( heap
, flags
, table
);
1424 TRACE( "returning ret %u table %p\n", ret
, table
);
1429 static MIB_UDPTABLE
*append_udp_row( HANDLE heap
, DWORD flags
, MIB_UDPTABLE
*table
,
1430 DWORD
*count
, const MIB_UDPROW
*row
)
1432 if (table
->dwNumEntries
>= *count
)
1434 MIB_UDPTABLE
*new_table
;
1435 DWORD new_count
= table
->dwNumEntries
* 2;
1437 if (!(new_table
= HeapReAlloc( heap
, flags
, table
, FIELD_OFFSET(MIB_UDPTABLE
, table
[new_count
] ))))
1439 HeapFree( heap
, 0, table
);
1445 memcpy( &table
->table
[table
->dwNumEntries
++], row
, sizeof(*row
) );
1449 static int compare_udp_rows(const void *a
, const void *b
)
1451 const MIB_UDPROW
*rowA
= a
;
1452 const MIB_UDPROW
*rowB
= b
;
1455 if ((ret
= rowA
->dwLocalAddr
- rowB
->dwLocalAddr
) != 0) return ret
;
1456 return rowA
->dwLocalPort
- rowB
->dwLocalPort
;
1460 /******************************************************************
1461 * AllocateAndGetUdpTableFromStack (IPHLPAPI.@)
1463 * Get the UDP listener table.
1464 * Like GetUdpTable(), but allocate the returned table from heap.
1467 * ppUdpTable [Out] pointer into which the MIB_UDPTABLE is
1468 * allocated and returned.
1469 * bOrder [In] whether to sort the table
1470 * heap [In] heap from which the table is allocated
1471 * flags [In] flags to HeapAlloc
1474 * ERROR_INVALID_PARAMETER if ppUdpTable is NULL, whatever GetUdpTable()
1475 * returns otherwise.
1477 DWORD WINAPI
AllocateAndGetUdpTableFromStack(PMIB_UDPTABLE
*ppUdpTable
, BOOL bOrder
,
1478 HANDLE heap
, DWORD flags
)
1480 MIB_UDPTABLE
*table
;
1482 DWORD ret
= NO_ERROR
, count
= 16;
1484 TRACE("table %p, bOrder %d, heap %p, flags 0x%08x\n", ppUdpTable
, bOrder
, heap
, flags
);
1486 if (!ppUdpTable
) return ERROR_INVALID_PARAMETER
;
1488 if (!(table
= HeapAlloc( heap
, flags
, FIELD_OFFSET(MIB_UDPTABLE
, table
[count
] ))))
1489 return ERROR_OUTOFMEMORY
;
1491 table
->dwNumEntries
= 0;
1497 if ((fp
= fopen("/proc/net/udp", "r")))
1499 char buf
[512], *ptr
;
1502 /* skip header line */
1503 ptr
= fgets(buf
, sizeof(buf
), fp
);
1504 while ((ptr
= fgets(buf
, sizeof(buf
), fp
)))
1506 if (sscanf( ptr
, "%u: %x:%x", &dummy
, &row
.dwLocalAddr
, &row
.dwLocalPort
) != 3)
1508 row
.dwLocalPort
= htons( row
.dwLocalPort
);
1509 if (!(table
= append_udp_row( heap
, flags
, table
, &count
, &row
)))
1514 else ret
= ERROR_NOT_SUPPORTED
;
1516 #elif defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
1520 mib2_udpEntry_t
*entry
;
1522 if ((fd
= open_streams_mib( "udp" )) != -1)
1524 if ((data
= read_mib_entry( fd
, MIB2_UDP
, MIB2_UDP_ENTRY
, &len
)))
1526 for (entry
= data
; (char *)(entry
+ 1) <= (char *)data
+ len
; entry
++)
1528 row
.dwLocalAddr
= entry
->udpLocalAddress
;
1529 row
.dwLocalPort
= htons( entry
->udpLocalPort
);
1530 if (!(table
= append_udp_row( heap
, flags
, table
, &count
, &row
))) break;
1532 HeapFree( GetProcessHeap(), 0, data
);
1536 else ret
= ERROR_NOT_SUPPORTED
;
1538 #elif defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_STRUCT_XINPGEN)
1542 struct xinpgen
*pXIG
, *pOrigXIG
;
1544 if (sysctlbyname ("net.inet.udp.pcblist", NULL
, &Len
, NULL
, 0) < 0)
1546 ERR ("Failure to read net.inet.udp.pcblist via sysctlbyname!\n");
1547 ret
= ERROR_NOT_SUPPORTED
;
1551 Buf
= HeapAlloc (GetProcessHeap (), 0, Len
);
1554 ret
= ERROR_OUTOFMEMORY
;
1558 if (sysctlbyname ("net.inet.udp.pcblist", Buf
, &Len
, NULL
, 0) < 0)
1560 ERR ("Failure to read net.inet.udp.pcblist via sysctlbyname!\n");
1561 ret
= ERROR_NOT_SUPPORTED
;
1565 /* Might be nothing here; first entry is just a header it seems */
1566 if (Len
<= sizeof (struct xinpgen
)) goto done
;
1568 pOrigXIG
= (struct xinpgen
*)Buf
;
1571 for (pXIG
= (struct xinpgen
*)((char *)pXIG
+ pXIG
->xig_len
);
1572 pXIG
->xig_len
> sizeof (struct xinpgen
);
1573 pXIG
= (struct xinpgen
*)((char *)pXIG
+ pXIG
->xig_len
))
1575 struct inpcb
*pINData
;
1576 struct xsocket
*pSockData
;
1578 pINData
= &((struct xinpcb
*)pXIG
)->xi_inp
;
1579 pSockData
= &((struct xinpcb
*)pXIG
)->xi_socket
;
1581 /* Ignore sockets for other protocols */
1582 if (pSockData
->xso_protocol
!= IPPROTO_UDP
)
1585 /* Ignore PCBs that were freed while generating the data */
1586 if (pINData
->inp_gencnt
> pOrigXIG
->xig_gen
)
1589 /* we're only interested in IPv4 addresses */
1590 if (!(pINData
->inp_vflag
& INP_IPV4
) ||
1591 (pINData
->inp_vflag
& INP_IPV6
))
1594 /* If all 0's, skip it */
1595 if (!pINData
->inp_laddr
.s_addr
&&
1596 !pINData
->inp_lport
)
1599 /* Fill in structure details */
1600 row
.dwLocalAddr
= pINData
->inp_laddr
.s_addr
;
1601 row
.dwLocalPort
= pINData
->inp_lport
;
1602 if (!(table
= append_udp_row( heap
, flags
, table
, &count
, &row
))) break;
1606 HeapFree (GetProcessHeap (), 0, Buf
);
1609 FIXME( "not implemented\n" );
1610 ret
= ERROR_NOT_SUPPORTED
;
1613 if (!table
) return ERROR_OUTOFMEMORY
;
1616 if (bOrder
&& table
->dwNumEntries
)
1617 qsort( table
->table
, table
->dwNumEntries
, sizeof(row
), compare_udp_rows
);
1618 *ppUdpTable
= table
;
1620 else HeapFree( heap
, flags
, table
);
1621 TRACE( "returning ret %u table %p\n", ret
, table
);
1625 static DWORD
get_tcp_table_sizes( TCP_TABLE_CLASS
class, DWORD row_count
, DWORD
*row_size
)
1631 case TCP_TABLE_BASIC_LISTENER
:
1632 case TCP_TABLE_BASIC_CONNECTIONS
:
1633 case TCP_TABLE_BASIC_ALL
:
1635 table_size
= FIELD_OFFSET(MIB_TCPTABLE
, table
[row_count
]);
1636 if (row_size
) *row_size
= sizeof(MIB_TCPROW
);
1639 case TCP_TABLE_OWNER_PID_LISTENER
:
1640 case TCP_TABLE_OWNER_PID_CONNECTIONS
:
1641 case TCP_TABLE_OWNER_PID_ALL
:
1643 table_size
= FIELD_OFFSET(MIB_TCPTABLE_OWNER_PID
, table
[row_count
]);
1644 if (row_size
) *row_size
= sizeof(MIB_TCPROW_OWNER_PID
);
1648 ERR("unhandled class %u\n", class);
1654 static MIB_TCPTABLE
*append_tcp_row( TCP_TABLE_CLASS
class, HANDLE heap
, DWORD flags
,
1655 MIB_TCPTABLE
*table
, DWORD
*count
,
1656 const MIB_TCPROW_OWNER_PID
*row
, DWORD row_size
)
1658 if (table
->dwNumEntries
>= *count
)
1660 MIB_TCPTABLE
*new_table
;
1661 DWORD new_count
= table
->dwNumEntries
* 2, new_table_size
;
1663 new_table_size
= get_tcp_table_sizes( class, new_count
, NULL
);
1664 if (!(new_table
= HeapReAlloc( heap
, flags
, table
, new_table_size
)))
1666 HeapFree( heap
, 0, table
);
1672 memcpy( (char *)table
->table
+ (table
->dwNumEntries
* row_size
), row
, row_size
);
1673 table
->dwNumEntries
++;
1678 /* Why not a lookup table? Because the TCPS_* constants are different
1679 on different platforms */
1680 static inline MIB_TCP_STATE
TCPStateToMIBState (int state
)
1684 case TCPS_ESTABLISHED
: return MIB_TCP_STATE_ESTAB
;
1685 case TCPS_SYN_SENT
: return MIB_TCP_STATE_SYN_SENT
;
1686 case TCPS_SYN_RECEIVED
: return MIB_TCP_STATE_SYN_RCVD
;
1687 case TCPS_FIN_WAIT_1
: return MIB_TCP_STATE_FIN_WAIT1
;
1688 case TCPS_FIN_WAIT_2
: return MIB_TCP_STATE_FIN_WAIT2
;
1689 case TCPS_TIME_WAIT
: return MIB_TCP_STATE_TIME_WAIT
;
1690 case TCPS_CLOSE_WAIT
: return MIB_TCP_STATE_CLOSE_WAIT
;
1691 case TCPS_LAST_ACK
: return MIB_TCP_STATE_LAST_ACK
;
1692 case TCPS_LISTEN
: return MIB_TCP_STATE_LISTEN
;
1693 case TCPS_CLOSING
: return MIB_TCP_STATE_CLOSING
;
1695 case TCPS_CLOSED
: return MIB_TCP_STATE_CLOSED
;
1699 static int compare_tcp_rows(const void *a
, const void *b
)
1701 const MIB_TCPROW
*rowA
= a
;
1702 const MIB_TCPROW
*rowB
= b
;
1705 if ((ret
= ntohl (rowA
->dwLocalAddr
) - ntohl (rowB
->dwLocalAddr
)) != 0) return ret
;
1706 if ((ret
= ntohs ((unsigned short)rowA
->dwLocalPort
) -
1707 ntohs ((unsigned short)rowB
->dwLocalPort
)) != 0) return ret
;
1708 if ((ret
= ntohl (rowA
->dwRemoteAddr
) - ntohl (rowB
->dwRemoteAddr
)) != 0) return ret
;
1709 return ntohs ((unsigned short)rowA
->dwRemotePort
) - ntohs ((unsigned short)rowB
->dwRemotePort
);
1715 unsigned int unix_pid
;
1718 static struct pid_map
*get_pid_map( unsigned int *num_entries
)
1720 HANDLE snapshot
= NULL
;
1721 struct pid_map
*map
;
1722 unsigned int i
= 0, count
= 16, size
= count
* sizeof(*map
);
1725 if (!(map
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
1727 SERVER_START_REQ( create_snapshot
)
1729 req
->flags
= SNAP_PROCESS
;
1730 req
->attributes
= 0;
1731 if (!(ret
= wine_server_call( req
)))
1732 snapshot
= wine_server_ptr_handle( reply
->handle
);
1737 while (ret
== STATUS_SUCCESS
)
1739 SERVER_START_REQ( next_process
)
1741 req
->handle
= wine_server_obj_handle( snapshot
);
1742 req
->reset
= (i
== 0);
1743 if (!(ret
= wine_server_call( req
)))
1747 struct pid_map
*new_map
;
1749 size
= count
* sizeof(*new_map
);
1751 if (!(new_map
= HeapReAlloc( GetProcessHeap(), 0, map
, size
)))
1753 HeapFree( GetProcessHeap(), 0, map
);
1759 map
[i
].pid
= reply
->pid
;
1760 map
[i
].unix_pid
= reply
->unix_pid
;
1769 NtClose( snapshot
);
1773 static unsigned int find_owning_pid( struct pid_map
*map
, unsigned int num_entries
, int inode
)
1776 unsigned int i
, len_socket
;
1779 sprintf( socket
, "socket:[%d]", inode
);
1780 len_socket
= strlen( socket
);
1781 for (i
= 0; i
< num_entries
; i
++)
1784 struct dirent
*dirent
;
1787 sprintf( dir
, "/proc/%u/fd", map
[i
].unix_pid
);
1788 if ((dirfd
= opendir( dir
)))
1790 while ((dirent
= readdir( dirfd
)))
1792 char link
[sizeof(dirent
->d_name
) + 32], name
[32];
1795 sprintf( link
, "/proc/%u/fd/%s", map
[i
].unix_pid
, dirent
->d_name
);
1796 if ((len
= readlink( link
, name
, 32 )) > 0) name
[len
] = 0;
1797 if (len
== len_socket
&& !strcmp( socket
, name
))
1808 FIXME( "not implemented\n" );
1813 DWORD
build_tcp_table( TCP_TABLE_CLASS
class, void **tablep
, BOOL order
, HANDLE heap
, DWORD flags
,
1816 MIB_TCPTABLE
*table
;
1817 MIB_TCPROW_OWNER_PID row
;
1818 DWORD ret
= NO_ERROR
, count
= 16, table_size
, row_size
;
1820 if (!(table_size
= get_tcp_table_sizes( class, count
, &row_size
)))
1821 return ERROR_INVALID_PARAMETER
;
1823 if (!(table
= HeapAlloc( heap
, flags
, table_size
)))
1824 return ERROR_OUTOFMEMORY
;
1826 table
->dwNumEntries
= 0;
1832 if ((fp
= fopen("/proc/net/tcp", "r")))
1834 char buf
[512], *ptr
;
1835 struct pid_map
*map
= NULL
;
1836 unsigned int dummy
, num_entries
= 0;
1839 if (class == TCP_TABLE_OWNER_PID_ALL
) map
= get_pid_map( &num_entries
);
1841 /* skip header line */
1842 ptr
= fgets(buf
, sizeof(buf
), fp
);
1843 while ((ptr
= fgets(buf
, sizeof(buf
), fp
)))
1845 if (sscanf( ptr
, "%x: %x:%x %x:%x %x %*s %*s %*s %*s %*s %d", &dummy
,
1846 &row
.dwLocalAddr
, &row
.dwLocalPort
, &row
.dwRemoteAddr
,
1847 &row
.dwRemotePort
, &row
.dwState
, &inode
) != 7)
1849 row
.dwLocalPort
= htons( row
.dwLocalPort
);
1850 row
.dwRemotePort
= htons( row
.dwRemotePort
);
1851 row
.dwState
= TCPStateToMIBState( row
.dwState
);
1852 if (class == TCP_TABLE_OWNER_PID_ALL
)
1853 row
.dwOwningPid
= find_owning_pid( map
, num_entries
, inode
);
1855 if (!(table
= append_tcp_row( class, heap
, flags
, table
, &count
, &row
, row_size
)))
1858 HeapFree( GetProcessHeap(), 0, map
);
1861 else ret
= ERROR_NOT_SUPPORTED
;
1863 #elif defined(HAVE_SYS_TIHDR_H) && defined(T_OPTMGMT_ACK)
1867 mib2_tcpConnEntry_t
*entry
;
1869 if ((fd
= open_streams_mib( "tcp" )) != -1)
1871 if ((data
= read_mib_entry( fd
, MIB2_TCP
, MIB2_TCP_CONN
, &len
)))
1873 for (entry
= data
; (char *)(entry
+ 1) <= (char *)data
+ len
; entry
++)
1875 row
.dwLocalAddr
= entry
->tcpConnLocalAddress
;
1876 row
.dwLocalPort
= htons( entry
->tcpConnLocalPort
);
1877 row
.dwRemoteAddr
= entry
->tcpConnRemAddress
;
1878 row
.dwRemotePort
= htons( entry
->tcpConnRemPort
);
1879 row
.dwState
= entry
->tcpConnState
;
1880 if (!(table
= append_tcp_row( class, heap
, flags
, table
, &count
, &row
, row_size
)))
1883 HeapFree( GetProcessHeap(), 0, data
);
1887 else ret
= ERROR_NOT_SUPPORTED
;
1889 #elif defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_STRUCT_XINPGEN)
1893 struct xinpgen
*pXIG
, *pOrigXIG
;
1895 if (sysctlbyname ("net.inet.tcp.pcblist", NULL
, &Len
, NULL
, 0) < 0)
1897 ERR ("Failure to read net.inet.tcp.pcblist via sysctlbyname!\n");
1898 ret
= ERROR_NOT_SUPPORTED
;
1902 Buf
= HeapAlloc (GetProcessHeap (), 0, Len
);
1905 ret
= ERROR_OUTOFMEMORY
;
1909 if (sysctlbyname ("net.inet.tcp.pcblist", Buf
, &Len
, NULL
, 0) < 0)
1911 ERR ("Failure to read net.inet.tcp.pcblist via sysctlbyname!\n");
1912 ret
= ERROR_NOT_SUPPORTED
;
1916 /* Might be nothing here; first entry is just a header it seems */
1917 if (Len
<= sizeof (struct xinpgen
)) goto done
;
1919 pOrigXIG
= (struct xinpgen
*)Buf
;
1922 for (pXIG
= (struct xinpgen
*)((char *)pXIG
+ pXIG
->xig_len
);
1923 pXIG
->xig_len
> sizeof (struct xinpgen
);
1924 pXIG
= (struct xinpgen
*)((char *)pXIG
+ pXIG
->xig_len
))
1926 struct tcpcb
*pTCPData
= NULL
;
1927 struct inpcb
*pINData
;
1928 struct xsocket
*pSockData
;
1930 pTCPData
= &((struct xtcpcb
*)pXIG
)->xt_tp
;
1931 pINData
= &((struct xtcpcb
*)pXIG
)->xt_inp
;
1932 pSockData
= &((struct xtcpcb
*)pXIG
)->xt_socket
;
1934 /* Ignore sockets for other protocols */
1935 if (pSockData
->xso_protocol
!= IPPROTO_TCP
)
1938 /* Ignore PCBs that were freed while generating the data */
1939 if (pINData
->inp_gencnt
> pOrigXIG
->xig_gen
)
1942 /* we're only interested in IPv4 addresses */
1943 if (!(pINData
->inp_vflag
& INP_IPV4
) ||
1944 (pINData
->inp_vflag
& INP_IPV6
))
1947 /* If all 0's, skip it */
1948 if (!pINData
->inp_laddr
.s_addr
&&
1949 !pINData
->inp_lport
&&
1950 !pINData
->inp_faddr
.s_addr
&&
1951 !pINData
->inp_fport
)
1954 /* Fill in structure details */
1955 row
.dwLocalAddr
= pINData
->inp_laddr
.s_addr
;
1956 row
.dwLocalPort
= pINData
->inp_lport
;
1957 row
.dwRemoteAddr
= pINData
->inp_faddr
.s_addr
;
1958 row
.dwRemotePort
= pINData
->inp_fport
;
1959 row
.dwState
= TCPStateToMIBState (pTCPData
->t_state
);
1960 if (!(table
= append_tcp_row( class, heap
, flags
, table
, &count
, &row
, row_size
)))
1965 HeapFree (GetProcessHeap (), 0, Buf
);
1968 FIXME( "not implemented\n" );
1969 ret
= ERROR_NOT_SUPPORTED
;
1972 if (!table
) return ERROR_OUTOFMEMORY
;
1975 if (order
&& table
->dwNumEntries
)
1976 qsort( table
->table
, table
->dwNumEntries
, row_size
, compare_tcp_rows
);
1979 else HeapFree( heap
, flags
, table
);
1980 if (size
) *size
= get_tcp_table_sizes( class, count
, NULL
);
1981 TRACE( "returning ret %u table %p\n", ret
, table
);
1985 /******************************************************************
1986 * AllocateAndGetTcpTableFromStack (IPHLPAPI.@)
1988 * Get the TCP connection table.
1989 * Like GetTcpTable(), but allocate the returned table from heap.
1992 * ppTcpTable [Out] pointer into which the MIB_TCPTABLE is
1993 * allocated and returned.
1994 * bOrder [In] whether to sort the table
1995 * heap [In] heap from which the table is allocated
1996 * flags [In] flags to HeapAlloc
1999 * ERROR_INVALID_PARAMETER if ppTcpTable is NULL, whatever GetTcpTable()
2000 * returns otherwise.
2002 DWORD WINAPI
AllocateAndGetTcpTableFromStack( PMIB_TCPTABLE
*ppTcpTable
, BOOL bOrder
,
2003 HANDLE heap
, DWORD flags
)
2005 TRACE("table %p, bOrder %d, heap %p, flags 0x%08x\n", ppTcpTable
, bOrder
, heap
, flags
);
2007 if (!ppTcpTable
) return ERROR_INVALID_PARAMETER
;
2008 return build_tcp_table( TCP_TABLE_BASIC_ALL
, (void **)ppTcpTable
, bOrder
, heap
, flags
, NULL
);