Lines 1-80
Link Here
|
1 |
--- ioctl_stat.c.orig Tue Feb 1 08:11:24 2000 |
|
|
2 |
+++ ioctl_stat.c Mon Oct 18 22:32:49 2004 |
3 |
@@ -30,6 +30,12 @@ |
4 |
#include <fcntl.h> /* open */ |
5 |
#include <sys/ioctl.h> /* ioctl */ |
6 |
#include <errno.h> |
7 |
+#ifdef __FreeBSD__ |
8 |
+#include <sys/sysctl.h> |
9 |
+#include <sys/socket.h> |
10 |
+#include <net/if.h> |
11 |
+#include <net/if_mib.h> |
12 |
+#endif |
13 |
|
14 |
#ifndef STREAMS /* Linux, FreeBSD, NetBSD, Ultrix */ |
15 |
# include <sys/socket.h> /* socket */ |
16 |
@@ -63,6 +69,16 @@ |
17 |
struct ifreq ifr; |
18 |
struct ifpppstatsreq req; |
19 |
|
20 |
+#ifdef __FreeBSD__ |
21 |
+ static int if_ix = -1; |
22 |
+ struct ifmibdata ifmd; |
23 |
+ size_t ifmd_sz = sizeof(ifmd); |
24 |
+ int nr_ifs; |
25 |
+ size_t nr_ifs_sz = sizeof(nr_ifs); |
26 |
+ int name[6]; |
27 |
+ int i; |
28 |
+#endif |
29 |
+ |
30 |
if (!ifd->s) getsocket(ifd); |
31 |
|
32 |
memset(&ifr, 0, sizeof(ifr)); |
33 |
@@ -84,18 +100,36 @@ |
34 |
#define ifr_name ifr__name |
35 |
#endif |
36 |
|
37 |
- strncpy(req.ifr_name, ifd->device, sizeof(req.ifr_name)); |
38 |
- if (ioctl(ifd->s, SIOCGPPPSTATS, &req) != 0) |
39 |
- { |
40 |
- /* shouldn't fail if SIOCGIFFLAGS worked... */ |
41 |
- ifd->in_bytes = 0UL; |
42 |
- ifd->out_bytes = 0UL; |
43 |
- return; |
44 |
+#ifdef __FreeBSD__ |
45 |
+ name[0] = CTL_NET; |
46 |
+ name[1] = PF_LINK; |
47 |
+ name[2] = NETLINK_GENERIC; |
48 |
+ name[3] = IFMIB_IFDATA; |
49 |
+ name[5] = IFDATA_GENERAL; |
50 |
+ |
51 |
+ if (if_ix < 0) { |
52 |
+ if (sysctlbyname("net.link.generic.system.ifcount", (void *) &nr_ifs, &nr_ifs_sz, (void *) 0, 0) < 0) { |
53 |
+ return; |
54 |
+ } |
55 |
+ for (i = 1; i <= nr_ifs; i++) { |
56 |
+ name[4] = i; /* row of the ifmib table */ |
57 |
+ |
58 |
+ if (sysctl(name, 6, (void *) &ifmd, &ifmd_sz, (void *) 0, 0) < 0) { |
59 |
+ continue; |
60 |
+ } |
61 |
+ if (strncmp(ifmd.ifmd_name, ifr.ifr_name, strlen(ifr.ifr_name)) == 0) { |
62 |
+ if_ix = i; |
63 |
+ break; |
64 |
+ } |
65 |
+ } |
66 |
} |
67 |
- |
68 |
- ifd->in_bytes = (unsigned long)req.stats.p.ppp_ibytes; |
69 |
- ifd->out_bytes = (unsigned long)req.stats.p.ppp_obytes; |
70 |
- |
71 |
+ name[4] = if_ix; |
72 |
+ if (sysctl(name, 6, (void *) &ifmd, &ifmd_sz, (void *) 0, 0) >= 0) { |
73 |
+ ifd->in_bytes = ifmd.ifmd_data.ifi_ibytes; |
74 |
+ ifd->out_bytes = ifmd.ifmd_data.ifi_obytes; |
75 |
+ } |
76 |
+#endif |
77 |
+ |
78 |
return; |
79 |
} |
80 |
|