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