Debian Codegraph @

agent/mibgroup/host/hr_network.c

raw
1 /*
2 * Host Resources MIB - network device group implementation - hr_network.c
3 *
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-features.h>
8 #include <net-snmp/net-snmp-includes.h>
9 #include <net-snmp/agent/net-snmp-agent-includes.h>
10 #include <net-snmp/data_access/interface.h>
11 #ifdef HAVE_STRING_H
12 #include <string.h>
13 #else
14 #include <strings.h>
15 #endif
16
17 #ifdef hpux11
18 #include <sys/mib.h>
19 #include <netinet/mib_kern.h>
20 #endif
21
22 #include "host_res.h"
23 #include "mibII/interfaces.h"
24 #include "hr_network.h"
25
26 #if !defined( solaris2 )
27 netsnmp_feature_require(interface_legacy);
28 #endif /* !solaris2 */
29
30 /*********************
31 *
32 * Kernel & interface information,
33 * and internal forward declarations
34 *
35 *********************/
36
37 void Init_HR_Network(void);
38 int Get_Next_HR_Network(void);
39 void Save_HR_Network_Info(void);
40
41 const char *describe_networkIF(int);
42 int network_status(int);
43 int network_errors(int);
44 int header_hrnet(struct variable *, oid *, size_t *, int,
45 size_t *, WriteMethod **);
46
47 #define HRN_MONOTONICALLY_INCREASING
48
49 /*********************
50 *
51 * Initialisation & common implementation functions
52 *
53 *********************/
54
55 #define HRNET_IFINDEX 1
56
57 struct variable4 hrnet_variables[] = {
58 {HRNET_IFINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
59 var_hrnet, 2, {1, 1}}
60 };
61 oid hrnet_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 4 };
62
63
64 void
65 init_hr_network(void)
66 {
67 init_device[HRDEV_NETWORK] = Init_HR_Network;
68 next_device[HRDEV_NETWORK] = Get_Next_HR_Network;
69 save_device[HRDEV_NETWORK] = Save_HR_Network_Info;
70 #ifdef HRN_MONOTONICALLY_INCREASING
71 dev_idx_inc[HRDEV_NETWORK] = 1;
72 #endif
73
74 device_descr[HRDEV_NETWORK] = describe_networkIF;
75 device_status[HRDEV_NETWORK] = network_status;
76 device_errors[HRDEV_NETWORK] = network_errors;
77
78 REGISTER_MIB("host/hr_network", hrnet_variables, variable4,
79 hrnet_variables_oid);
80 }
81
82 /*
83 * header_hrnet(...
84 * Arguments:
85 * vp IN - pointer to variable entry that points here
86 * name IN/OUT - IN/name requested, OUT/name found
87 * length IN/OUT - length of IN/OUT oid's
88 * exact IN - TRUE if an exact match was requested
89 * var_len OUT - length of variable or 0 if function returned
90 * write_method
91 *
92 */
93
94 int
95 header_hrnet(struct variable *vp,
96 oid * name,
97 size_t * length,
98 int exact, size_t * var_len, WriteMethod ** write_method)
99 {
100 #define HRNET_ENTRY_NAME_LENGTH 11
101 oid newname[MAX_OID_LEN];
102 int net_idx;
103 int result;
104 int LowIndex = -1;
105
106 DEBUGMSGTL(("host/hr_network", "var_hrnet: "));
107 DEBUGMSGOID(("host/hr_network", name, *length));
108 DEBUGMSG(("host/hr_network", " %d\n", exact));
109
110 memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
111 /*
112 * Find "next" net entry
113 */
114
115 Init_HR_Network();
116 for (;;) {
117 net_idx = Get_Next_HR_Network();
118 if (net_idx == -1)
119 break;
120 newname[HRNET_ENTRY_NAME_LENGTH] = net_idx;
121 result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
122 if (exact && (result == 0)) {
123 LowIndex = net_idx;
124 break;
125 }
126 if (!exact && (result < 0) &&
127 (LowIndex == -1 || net_idx < LowIndex)) {
128 LowIndex = net_idx;
129 #ifdef HRN_MONOTONICALLY_INCREASING
130 break;
131 #endif
132 }
133 }
134
135 if (LowIndex == -1) {
136 DEBUGMSGTL(("host/hr_network", "... index out of range\n"));
137 return (MATCH_FAILED);
138 }
139
140 newname[HRNET_ENTRY_NAME_LENGTH] = LowIndex;
141 memcpy((char *) name, (char *) newname,
142 (vp->namelen + 1) * sizeof(oid));
143 *length = vp->namelen + 1;
144 *write_method = (WriteMethod*)0;
145 *var_len = sizeof(long); /* default to 'long' results */
146
147 DEBUGMSGTL(("host/hr_network", "... get net stats "));
148 DEBUGMSGOID(("host/hr_network", name, *length));
149 DEBUGMSG(("host/hr_network", "\n"));
150 return LowIndex;
151 }
152
153
154 /*********************
155 *
156 * System specific implementation functions
157 *
158 *********************/
159
160
161 u_char *
162 var_hrnet(struct variable * vp,
163 oid * name,
164 size_t * length,
165 int exact, size_t * var_len, WriteMethod ** write_method)
166 {
167 int net_idx;
168
169 net_idx = header_hrnet(vp, name, length, exact, var_len, write_method);
170 if (net_idx == MATCH_FAILED)
171 return NULL;
172
173
174 switch (vp->magic) {
175 case HRNET_IFINDEX:
176 long_return = net_idx & ((1 << HRDEV_TYPE_SHIFT) - 1);
177 return (u_char *) & long_return;
178 default:
179 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrnet\n",
180 vp->magic));
181 }
182 return NULL;
183 }
184
185
186 /*********************
187 *
188 * Internal implementation functions
189 *
190 *********************/
191
192
193 #if defined( USING_IF_MIB_IFTABLE_IFTABLE_DATA_ACCESS_MODULE )
194 static char HRN_name[16];
195 static netsnmp_interface_entry *HRN_ifnet;
196 #define M_Interface_Scan_Next(a, b, c, d) Interface_Scan_NextInt(a, b, c, d)
197 #elif defined(hpux11)
198 static char HRN_name[MAX_PHYSADDR_LEN];
199 static nmapi_phystat HRN_ifnet;
200 #define M_Interface_Scan_Next(a, b, c, d) Interface_Scan_NextInt(a, b, c)
201 #elif defined darwin
202 static char HRN_name[IFNAMSIZ];
203 static struct if_msghdr HRN_ifnet;
204 #define M_Interface_Scan_Next(a, b, c, d) Interface_Scan_NextInt(a, b, c, d)
205 #else /* hpux11 */
206 static char HRN_name[16];
207 #ifndef WIN32
208 static struct ifnet HRN_ifnet;
209 #endif /* WIN32 */
210 #define M_Interface_Scan_Next(a, b, c, d) Interface_Scan_NextInt(a, b, c, d)
211 #endif
212
213 #ifdef hpux11
214 static char HRN_savedName[MAX_PHYSADDR_LEN];
215 #else
216 static char HRN_savedName[16];
217 #endif
218 static u_short HRN_savedFlags;
219 static int HRN_savedErrors;
220
221
222 void
223 Init_HR_Network(void)
224 {
225 #if !defined( solaris2 )
226 Interface_Scan_Init();
227 #endif
228 }
229
230 int
231 Get_Next_HR_Network(void)
232 {
233 int HRN_index;
234 #if !(defined(solaris2) || defined(darwin) || defined(WIN32))
235 if (M_Interface_Scan_Next(&HRN_index, HRN_name, &HRN_ifnet, NULL) == 0)
236 HRN_index = -1;
237 #else
238 HRN_index = -1;
239 #endif
240 if (-1 == HRN_index)
241 return HRN_index;
242
243 /*
244 * If the index is greater than the shift registry space,
245 * this will overrun into the next device type block,
246 * potentially resulting in duplicate index values
247 * which may cause the agent to crash.
248 * To avoid this, we silently drop interfaces greater
249 * than the shift registry size can handle.
250 */
251 if (HRN_index > (1 << HRDEV_TYPE_SHIFT ))
252 return -1;
253
254 return (HRDEV_NETWORK << HRDEV_TYPE_SHIFT) + HRN_index;
255 }
256
257 void
258 Save_HR_Network_Info(void)
259 {
260 strlcpy(HRN_savedName, HRN_name, sizeof(HRN_savedName));
261 #if defined( USING_IF_MIB_IFTABLE_IFTABLE_DATA_ACCESS_MODULE )
262 HRN_savedFlags = HRN_ifnet->os_flags;
263 HRN_savedErrors = HRN_ifnet->stats.ierrors + HRN_ifnet->stats.oerrors;
264 #elif defined( hpux11 )
265 HRN_savedFlags = HRN_ifnet.if_entry.ifOper;
266 HRN_savedErrors = HRN_ifnet.if_entry.ifInErrors +
267 HRN_ifnet.if_entry.ifOutErrors;
268 #elif defined(__APPLE__) /* or darwin? */
269 HRN_savedFlags = HRN_ifnet.ifm_flags;
270 HRN_savedErrors = HRN_ifnet.ifm_data.ifi_ierrors +
271 HRN_ifnet.ifm_data.ifi_oerrors;
272 #elif !defined(WIN32)
273 HRN_savedFlags = HRN_ifnet.if_flags;
274 HRN_savedErrors = HRN_ifnet.if_ierrors + HRN_ifnet.if_oerrors;
275 #endif
276 }
277
278
279 const char *
280 describe_networkIF(int idx)
281 {
282 static char string[1024];
283
284 snprintf(string, sizeof(string)-1, "network interface %s", HRN_savedName);
285 string[ sizeof(string)-1 ] = 0;
286 return string;
287 }
288
289
290 int
291 network_status(int idx)
292 {
293 #ifndef WIN32
294 #ifdef hpux11
295 if (HRN_savedFlags == LINK_UP)
296 #else
297 if (HRN_savedFlags & IFF_UP)
298 #endif
299 return 2; /* running */
300 else
301 return 5; /* down */
302 #else
303 /* To do: implement network_status() for Windows. */
304 return 2; /* running */
305 #endif /* WIN32 */
306 }
307
308 int
309 network_errors(int idx)
310 {
311 return HRN_savedErrors;
312 }