# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / mibII / sysORTable.c
1 #ifdef BUILD_SNMP_SYSOR_MIB
2 /*
3  *  Template MIB group implementation - sysORTable.c
4  *
5  */
6 #include <net-snmp/net-snmp-config.h>
7 #if HAVE_STDLIB_H
8 #include <stdlib.h>
9 #endif
10 #include <sys/types.h>
11 #if TIME_WITH_SYS_TIME
12 # ifdef WIN32
13 #  include <sys/timeb.h>
14 # else
15 #  include <sys/time.h>
16 # endif
17 # include <time.h>
18 #else
19 # if HAVE_SYS_TIME_H
20 #  include <sys/time.h>
21 # else
22 #  include <time.h>
23 # endif
24 #endif
25 #if HAVE_STRING_H
26 #include <string.h>
27 #else
28 #include <strings.h>
29 #endif
30
31 #if HAVE_WINSOCK_H
32 #include <winsock.h>
33 #endif
34 #if HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37
38 #if HAVE_DMALLOC_H
39 #include <dmalloc.h>
40 #endif
41
42 #include <net-snmp/net-snmp-includes.h>
43 #include <net-snmp/agent/net-snmp-agent-includes.h>
44 #include <net-snmp/agent/agent_callbacks.h>
45
46 #include "struct.h"
47 #include "util_funcs.h"
48 #include "sysORTable.h"
49 #include "snmpd.h"
50
51 #ifdef USING_AGENTX_SUBAGENT_MODULE
52 #include "agentx/subagent.h"
53 #include "agentx/client.h"
54 #endif
55
56
57 struct timeval  sysOR_lastchange;
58 static struct sysORTable *table = NULL;
59 static int      numEntries = 0;
60
61 /*
62  * define the structure we're going to ask the agent to register our
63  * information at 
64  */
65 struct variable1 sysORTable_variables[] = {
66     {SYSORTABLEID, ASN_OBJECT_ID, RONLY, var_sysORTable, 1, {2}},
67     {SYSORTABLEDESCR, ASN_OCTET_STR, RONLY, var_sysORTable, 1, {3}},
68     {SYSORTABLEUPTIME, ASN_TIMETICKS, RONLY, var_sysORTable, 1, {4}}
69 };
70
71 /*
72  * Define the OID pointer to the top of the mib tree that we're
73  * registering underneath 
74  */
75 oid             sysORTable_variables_oid[] = { SNMP_OID_MIB2, 1, 9, 1 };
76 #ifdef USING_MIBII_SYSTEM_MIB_MODULE
77 extern oid      system_module_oid[];
78 extern int      system_module_oid_len;
79 extern int      system_module_count;
80 #endif
81
82 void
83 init_sysORTable(void)
84 {
85     /*
86      * register ourselves with the agent to handle our mib tree 
87      */
88
89 #ifdef USING_AGENTX_SUBAGENT_MODULE
90     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) == MASTER_AGENT)
91         (void) register_mib_priority("mibII/sysORTable",
92                                      (struct variable *)
93                                      sysORTable_variables,
94                                      sizeof(struct variable1),
95                                      sizeof(sysORTable_variables) /
96                                      sizeof(struct variable1),
97                                      sysORTable_variables_oid,
98                                      sizeof(sysORTable_variables_oid) /
99                                      sizeof(oid), 1);
100     else
101 #endif
102         REGISTER_MIB("mibII/sysORTable", sysORTable_variables, variable1,
103                      sysORTable_variables_oid);
104
105 #ifdef USING_MIBII_SYSTEM_MIB_MODULE
106     if (++system_module_count == 3)
107         REGISTER_SYSOR_TABLE(system_module_oid, system_module_oid_len,
108                              "The MIB module for SNMPv2 entities");
109 #endif
110
111     gettimeofday(&sysOR_lastchange, NULL);
112 }
113
114         /*********************
115          *
116          *  System specific implementation functions
117          *
118          *********************/
119
120 u_char         *
121 var_sysORTable(struct variable *vp,
122                oid * name,
123                size_t * length,
124                int exact, size_t * var_len, WriteMethod ** write_method)
125 {
126     unsigned long   i = 0;
127     static unsigned long ret;
128     struct sysORTable *ptr = table;
129
130     if (header_simple_table
131         (vp, name, length, exact, var_len, write_method, numEntries))
132         return NULL;
133
134     for (i = 1; ptr != NULL && i < name[*length - 1]; ptr = ptr->next, i++) {
135         DEBUGMSGTL(("mibII/sysORTable", "sysORTable -- %lu != %lu\n",
136                     i, name[*length - 1]));
137     }
138     if (ptr == NULL) {
139         DEBUGMSGTL(("mibII/sysORTable", "sysORTable -- no match: %lu\n",
140                     i));
141         return NULL;
142     }
143     DEBUGMSGTL(("mibII/sysORTable", "sysORTable -- match: %lu\n", i));
144
145     switch (vp->magic) {
146     case SYSORTABLEID:
147         *var_len = ptr->OR_oidlen * sizeof(ptr->OR_oid[0]);
148         return (u_char *) ptr->OR_oid;
149
150     case SYSORTABLEDESCR:
151         *var_len = strlen(ptr->OR_descr);
152         return (u_char *) ptr->OR_descr;
153
154     case SYSORTABLEUPTIME:
155         ret = netsnmp_timeval_uptime(&ptr->OR_uptime);
156         return (u_char *) & ret;
157
158     default:
159         DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_sysORTable\n",
160                     vp->magic));
161     }
162     return NULL;
163 }
164
165
166 int
167 register_sysORTable_sess(oid * oidin,
168                          size_t oidlen,
169                          const char *descr, netsnmp_session * ss)
170 {
171     struct sysORTable **ptr = &table;
172     struct register_sysOR_parameters reg_sysOR_parms;
173
174     DEBUGMSGTL(("mibII/sysORTable", "sysORTable registering: "));
175     DEBUGMSGOID(("mibII/sysORTable", oidin, oidlen));
176     DEBUGMSG(("mibII/sysORTable", "\n"));
177
178     while (*ptr != NULL)
179         ptr = &((*ptr)->next);
180     *ptr = (struct sysORTable *) malloc(sizeof(struct sysORTable));
181     if (*ptr == NULL) {
182         return SYS_ORTABLE_REGISTRATION_FAILED;
183     }
184     (*ptr)->OR_descr = (char *) malloc(strlen(descr) + 1);
185     if ((*ptr)->OR_descr == NULL) {
186         free(*ptr);
187         return SYS_ORTABLE_REGISTRATION_FAILED;
188     }
189     strcpy((*ptr)->OR_descr, descr);
190     (*ptr)->OR_oidlen = oidlen;
191     (*ptr)->OR_oid = (oid *) malloc(sizeof(oid) * oidlen);
192     if ((*ptr)->OR_oid == NULL) {
193         free(*ptr);
194         free((*ptr)->OR_descr);
195         return SYS_ORTABLE_REGISTRATION_FAILED;
196     }
197     memcpy((*ptr)->OR_oid, oidin, sizeof(oid) * oidlen);
198     gettimeofday(&((*ptr)->OR_uptime), NULL);
199     gettimeofday(&(sysOR_lastchange), NULL);
200     (*ptr)->OR_sess = ss;
201     (*ptr)->next = NULL;
202     numEntries++;
203
204     reg_sysOR_parms.name = oidin;
205     reg_sysOR_parms.namelen = oidlen;
206     reg_sysOR_parms.descr = descr;
207     snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
208                         SNMPD_CALLBACK_REG_SYSOR, &reg_sysOR_parms);
209
210     return SYS_ORTABLE_REGISTERED_OK;
211 }
212
213 int
214 register_sysORTable(oid * oidin, size_t oidlen, const char *descr)
215 {
216     return register_sysORTable_sess(oidin, oidlen, descr, NULL);
217 }
218
219
220
221 int
222 unregister_sysORTable_sess(oid * oidin,
223                            size_t oidlen, netsnmp_session * ss)
224 {
225     struct sysORTable **ptr = &table, *prev = NULL;
226     int             found = SYS_ORTABLE_NO_SUCH_REGISTRATION;
227     struct register_sysOR_parameters reg_sysOR_parms;
228
229     DEBUGMSGTL(("mibII/sysORTable", "sysORTable unregistering: "));
230     DEBUGMSGOID(("mibII/sysORTable", oidin, oidlen));
231     DEBUGMSG(("mibII/sysORTable", "\n"));
232
233     while (*ptr != NULL) {
234         if (snmp_oid_compare
235             (oidin, oidlen, (*ptr)->OR_oid, (*ptr)->OR_oidlen) == 0) {
236             if ((*ptr)->OR_sess != ss)
237                 continue;       /* different session */
238             if (prev == NULL)
239                 table = (*ptr)->next;
240             else
241                 prev->next = (*ptr)->next;
242
243             free((*ptr)->OR_descr);
244             free((*ptr)->OR_oid);
245             free((*ptr));
246             numEntries--;
247             gettimeofday(&(sysOR_lastchange), NULL);
248             found = SYS_ORTABLE_UNREGISTERED_OK;
249             break;
250         }
251         prev = *ptr;
252         ptr = &((*ptr)->next);
253     }
254
255     reg_sysOR_parms.name = oidin;
256     reg_sysOR_parms.namelen = oidlen;
257     snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
258                         SNMPD_CALLBACK_UNREG_SYSOR, &reg_sysOR_parms);
259
260     return found;
261 }
262
263
264 int
265 unregister_sysORTable(oid * oidin, size_t oidlen)
266 {
267     return unregister_sysORTable_sess(oidin, oidlen, NULL);
268 }
269
270 void
271 unregister_sysORTable_by_session(netsnmp_session * ss)
272 {
273     struct sysORTable *ptr = table, *prev = NULL, *next;
274
275     while (ptr != NULL) {
276         next = ptr->next;
277         if (((ss->flags & SNMP_FLAGS_SUBSESSION) && ptr->OR_sess == ss) ||
278             (!(ss->flags & SNMP_FLAGS_SUBSESSION) && ptr->OR_sess &&
279              ptr->OR_sess->subsession == ss)) {
280             if (prev == NULL)
281                 table = next;
282             else
283                 prev->next = next;
284             free(ptr->OR_descr);
285             free(ptr->OR_oid);
286             free(ptr);
287             numEntries--;
288             gettimeofday(&(sysOR_lastchange), NULL);
289         } else
290             prev = ptr;
291         ptr = next;
292     }
293 }
294
295 #else
296 void
297 init_sysORTable(void)
298 {
299   return;
300 }
301 #endif /* BUILD_SNMP_SYSOR_MIB */