Line 0
Link Here
|
|
|
1 |
--- src/agent/subagents/freebsd/net.cpp.orig 2014-10-02 16:54:48.000000000 +0400 |
2 |
+++ src/agent/subagents/freebsd/net.cpp 2014-10-02 17:19:53.000000000 +0400 |
3 |
@@ -32,7 +32,6 @@ |
4 |
#include <net/if_media.h> |
5 |
#include <net/if_dl.h> |
6 |
#include <net/if_types.h> |
7 |
-#include <net/if_var.h> |
8 |
#include <net/route.h> |
9 |
#include <net/iso88025.h> |
10 |
#include <netinet/if_ether.h> |
11 |
@@ -40,8 +39,7 @@ |
12 |
#include <ifaddrs.h> |
13 |
#include <arpa/inet.h> |
14 |
#include <net/ethernet.h> |
15 |
-#include <kvm.h> |
16 |
-#include <nlist.h> |
17 |
+#include <ifaddrs.h> |
18 |
|
19 |
#include "net.h" |
20 |
|
21 |
@@ -60,14 +58,6 @@ |
22 |
int index; |
23 |
} IFLIST; |
24 |
|
25 |
-struct nlist nl[] = { |
26 |
-#define N_IFNET 0 |
27 |
- { (char *)"_ifnet" }, |
28 |
- { NULL }, |
29 |
-}; |
30 |
- |
31 |
-kvm_t *kvmd = NULL; |
32 |
- |
33 |
LONG H_NetIpForwarding(const TCHAR *pszParam, const TCHAR *pArg, TCHAR *pValue) |
34 |
{ |
35 |
int nVer = CAST_FROM_POINTER(pArg, int); |
36 |
@@ -568,94 +558,66 @@ |
37 |
return nRet; |
38 |
} |
39 |
|
40 |
-LONG H_NetIfInfoFromKVM(const TCHAR *pszParam, const TCHAR *pArg, TCHAR *pValue) |
41 |
+LONG H_NetIfInfo(const TCHAR *pszParam, const TCHAR *pArg, TCHAR *pValue) |
42 |
{ |
43 |
- int nRet = SYSINFO_RC_SUCCESS; |
44 |
char szArg[512]; |
45 |
- u_long ifnetaddr; |
46 |
- struct ifnet ifnet; |
47 |
- struct ifnethead ifnethead; |
48 |
-#if __FreeBSD__ < 5 |
49 |
- char szTName[IFNAMSIZ]; |
50 |
-#endif |
51 |
- char szName[IFNAMSIZ]; |
52 |
+ struct ifaddrs *ifap, *ifa; |
53 |
+ u_int index; |
54 |
|
55 |
AgentGetParameterArgA(pszParam, 1, szArg, sizeof(szArg)); |
56 |
|
57 |
- if (szArg[0] != 0) |
58 |
+ if (szArg[0] == 0) |
59 |
+ return SYSINFO_RC_SUCCESS; |
60 |
+ |
61 |
+ if (szArg[0] >= '0' && szArg[0] <= '9') |
62 |
+ index = atoi(szArg); |
63 |
+ else |
64 |
+ index = 0; |
65 |
+ |
66 |
+ if (getifaddrs(&ifap) != 0) |
67 |
{ |
68 |
- if (szArg[0] >= '0' && szArg[0] <= '9') |
69 |
- { |
70 |
- // index |
71 |
- if (if_indextoname(atoi(szArg), szArg) != szArg) |
72 |
- { |
73 |
- // not found |
74 |
- nRet = SYSINFO_RC_ERROR; |
75 |
- } |
76 |
- } |
77 |
+ return SYSINFO_RC_ERROR; |
78 |
+ } |
79 |
|
80 |
- if (nRet == SYSINFO_RC_SUCCESS) |
81 |
- { |
82 |
- nRet = SYSINFO_RC_ERROR; |
83 |
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) { |
84 |
+ if (ifa->ifa_addr->sa_family != AF_LINK) |
85 |
+ continue; |
86 |
+ if (index) { |
87 |
+ if (index != if_nametoindex(ifa->ifa_name)) |
88 |
+ continue; |
89 |
+ } else if (strcmp(ifa->ifa_name, szArg) != 0) |
90 |
+ continue; |
91 |
|
92 |
- if (kvmd == NULL) { |
93 |
- kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); |
94 |
- if (kvmd == NULL) |
95 |
- return SYSINFO_RC_ERROR; |
96 |
- if (kvm_nlist(kvmd, nl) < 0) |
97 |
- return SYSINFO_RC_ERROR; |
98 |
- if (nl[0].n_type == 0) |
99 |
- return SYSINFO_RC_ERROR; |
100 |
- } |
101 |
- ifnetaddr = nl[N_IFNET].n_value; |
102 |
- if (kvm_read(kvmd, ifnetaddr, &ifnethead, sizeof(ifnethead)) != sizeof(ifnethead)) |
103 |
- return SYSINFO_RC_ERROR; |
104 |
- ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead); |
105 |
- while (ifnetaddr) { |
106 |
- if (kvm_read(kvmd, ifnetaddr, &ifnet, sizeof(ifnet)) != sizeof(ifnet)) |
107 |
- return SYSINFO_RC_ERROR; |
108 |
- ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link); |
109 |
-#if __FreeBSD__ >= 5 |
110 |
- strlcpy(szName, ifnet.if_xname, sizeof(szName)); |
111 |
-#else |
112 |
- if (kvm_read(kvmd, ifnet.if_name, szTName, sizeof(szTName)) != sizeof(szTName)) |
113 |
- return SYSINFO_RC_ERROR; |
114 |
- szTName[sizeof(szTName) - 1] = '\0'; |
115 |
- snprintf(szName, sizeof(szName), "%s%d", szTName, ifnet.if_unit); |
116 |
-#endif |
117 |
- if (strcmp(szName, szArg) == 0) { |
118 |
- nRet = SYSINFO_RC_SUCCESS; |
119 |
- switch((long)pArg) |
120 |
- { |
121 |
- case IF_INFO_BYTES_IN: |
122 |
- ret_uint(pValue, ifnet.if_ibytes); |
123 |
- break; |
124 |
- case IF_INFO_BYTES_OUT: |
125 |
- ret_uint(pValue, ifnet.if_obytes); |
126 |
- break; |
127 |
- case IF_INFO_IN_ERRORS: |
128 |
- ret_uint(pValue, ifnet.if_ierrors); |
129 |
- break; |
130 |
- case IF_INFO_OUT_ERRORS: |
131 |
- ret_uint(pValue, ifnet.if_oerrors); |
132 |
- break; |
133 |
- case IF_INFO_PACKETS_IN: |
134 |
- ret_uint(pValue, ifnet.if_ipackets); |
135 |
- break; |
136 |
- case IF_INFO_PACKETS_OUT: |
137 |
- ret_uint(pValue, ifnet.if_opackets); |
138 |
- break; |
139 |
- default: |
140 |
- nRet = SYSINFO_RC_UNSUPPORTED; |
141 |
- break; |
142 |
- } |
143 |
- break; |
144 |
- } |
145 |
- else |
146 |
- continue; |
147 |
- } |
148 |
+#define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s) |
149 |
+ |
150 |
+ switch((long)pArg) |
151 |
+ { |
152 |
+ case IF_INFO_BYTES_IN: |
153 |
+ ret_uint(pValue, IFA_STAT(ibytes)); |
154 |
+ break; |
155 |
+ case IF_INFO_BYTES_OUT: |
156 |
+ ret_uint(pValue, IFA_STAT(obytes)); |
157 |
+ break; |
158 |
+ case IF_INFO_IN_ERRORS: |
159 |
+ ret_uint(pValue, IFA_STAT(ierrors)); |
160 |
+ break; |
161 |
+ case IF_INFO_OUT_ERRORS: |
162 |
+ ret_uint(pValue, IFA_STAT(oerrors)); |
163 |
+ break; |
164 |
+ case IF_INFO_PACKETS_IN: |
165 |
+ ret_uint(pValue, IFA_STAT(ipackets)); |
166 |
+ break; |
167 |
+ case IF_INFO_PACKETS_OUT: |
168 |
+ ret_uint(pValue, IFA_STAT(opackets)); |
169 |
+ break; |
170 |
+ default: |
171 |
+ freeifaddrs(ifap); |
172 |
+ return SYSINFO_RC_UNSUPPORTED; |
173 |
} |
174 |
+ freeifaddrs(ifap); |
175 |
+ return SYSINFO_RC_SUCCESS; |
176 |
} |
177 |
|
178 |
- return nRet; |
179 |
+ freeifaddrs(ifap); |
180 |
+ return SYSINFO_RC_ERROR; |
181 |
} |