and added files
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / default_store.c
1 /*
2  * default_store.h: storage space for defaults 
3  */
4
5 #include <net-snmp/net-snmp-config.h>
6 #include <sys/types.h>
7 #if HAVE_STDLIB_H
8 #include <stdlib.h>
9 #endif
10 #if HAVE_NETINET_IN_H
11 #include <netinet/in.h>
12 #endif
13 #if HAVE_STDLIB_H
14 #include <stdlib.h>
15 #endif
16 #if HAVE_STRING_H
17 #include <string.h>
18 #else
19 #include <strings.h>
20 #endif
21 #if HAVE_WINSOCK_H
22 #include <winsock.h>
23 #endif
24
25 #if HAVE_DMALLOC_H
26 #include <dmalloc.h>
27 #endif
28
29 #include <net-snmp/types.h>
30 #include <net-snmp/output_api.h>
31 #include <net-snmp/config_api.h>
32 #include <net-snmp/library/default_store.h>    /* for "internal" definitions */
33 #include <net-snmp/utilities.h>
34
35 #include <net-snmp/library/snmp_api.h>
36
37 #ifndef BUILD_ILMI
38 static const char * stores [NETSNMP_DS_MAX_IDS] = { "LIB", "APP", "TOK" };
39
40 typedef struct netsnmp_ds_read_config_s {
41   u_char          type;
42   char           *token;
43   char           *ftype;
44   int             storeid;
45   int             which;
46   struct netsnmp_ds_read_config_s *next;
47 } netsnmp_ds_read_config;
48
49 static netsnmp_ds_read_config *netsnmp_ds_configs = NULL;
50
51 static int   netsnmp_ds_integers[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS];
52 static char  netsnmp_ds_booleans[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS/8];
53 static char *netsnmp_ds_strings[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS];
54 static void *netsnmp_ds_voids[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS];
55
56 /*
57  * Prototype definitions 
58  */
59 void            netsnmp_ds_handle_config(const char *token, char *line);
60
61 int
62 netsnmp_ds_set_boolean(int storeid, int which, int value)
63 {
64     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
65         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
66         return SNMPERR_GENERR;
67     }
68
69     DEBUGMSGTL(("netsnmp_ds_set_boolean", "Setting %s:%d = %d/%s\n",
70                 stores[storeid], which, value, ((value) ? "True" : "False")));
71
72     if (value > 0) {
73         netsnmp_ds_booleans[storeid][which/8] |= (1 << (which % 8));
74     } else {
75         netsnmp_ds_booleans[storeid][which/8] &= (0xff7f >> (7 - (which % 8)));
76     }
77
78     return SNMPERR_SUCCESS;
79 }
80
81 int
82 netsnmp_ds_toggle_boolean(int storeid, int which)
83 {
84     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
85         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
86         return SNMPERR_GENERR;
87     }
88
89     if ((netsnmp_ds_booleans[storeid][which/8] & (1 << (which % 8))) == 0) {
90         netsnmp_ds_booleans[storeid][which/8] |= (1 << (which % 8));
91     } else {
92         netsnmp_ds_booleans[storeid][which/8] &= (0xff7f >> (7 - (which % 8)));
93     }
94
95     DEBUGMSGTL(("netsnmp_ds_toggle_boolean", "Setting %s:%d = %d/%s\n",
96                 stores[storeid], which, netsnmp_ds_booleans[storeid][which/8],
97                 ((netsnmp_ds_booleans[storeid][which/8]) ? "True" : "False")));
98
99     return SNMPERR_SUCCESS;
100 }
101
102 int
103 netsnmp_ds_get_boolean(int storeid, int which)
104 {
105     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
106         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
107         return SNMPERR_GENERR;
108     }
109
110     return (netsnmp_ds_booleans[storeid][which/8] & (1 << (which % 8))) ? 1:0;
111 }
112
113 int
114 netsnmp_ds_set_int(int storeid, int which, int value)
115 {
116     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
117         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
118         return SNMPERR_GENERR;
119     }
120
121     DEBUGMSGTL(("netsnmp_ds_set_int", "Setting %s:%d = %d\n",
122                 stores[storeid], which, value));
123
124     netsnmp_ds_integers[storeid][which] = value;
125     return SNMPERR_SUCCESS;
126 }
127
128 int
129 netsnmp_ds_get_int(int storeid, int which)
130 {
131     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
132         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
133         return SNMPERR_GENERR;
134     }
135
136     return netsnmp_ds_integers[storeid][which];
137 }
138
139 int
140 netsnmp_ds_set_string(int storeid, int which, const char *value)
141 {
142     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
143         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
144         return SNMPERR_GENERR;
145     }
146
147     DEBUGMSGTL(("netsnmp_ds_set_string", "Setting %s:%d = \"%s\"\n",
148                 stores[storeid], which, (value ? value : "(null)")));
149
150     /*
151      * is some silly person is calling us with our own pointer?
152      */
153     if (netsnmp_ds_strings[storeid][which] == value)
154         return SNMPERR_SUCCESS;
155     
156     if (netsnmp_ds_strings[storeid][which] != NULL) {
157         free(netsnmp_ds_strings[storeid][which]);
158         netsnmp_ds_strings[storeid][which] = NULL;
159     }
160
161     if (value) {
162         netsnmp_ds_strings[storeid][which] = strdup(value);
163     } else {
164         netsnmp_ds_strings[storeid][which] = NULL;
165     }
166
167     return SNMPERR_SUCCESS;
168 }
169
170 char *
171 netsnmp_ds_get_string(int storeid, int which)
172 {
173     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
174         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
175         return NULL;
176     }
177
178     return netsnmp_ds_strings[storeid][which];
179 }
180
181 int
182 netsnmp_ds_set_void(int storeid, int which, void *value)
183 {
184     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
185         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
186         return SNMPERR_GENERR;
187     }
188
189     DEBUGMSGTL(("netsnmp_ds_set_void", "Setting %s:%d = %x\n",
190                 stores[storeid], which, value));
191
192     netsnmp_ds_voids[storeid][which] = value;
193
194     return SNMPERR_SUCCESS;
195 }
196
197 void *
198 netsnmp_ds_get_void(int storeid, int which)
199 {
200     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
201         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
202         return NULL;
203     }
204
205     return netsnmp_ds_voids[storeid][which];
206 }
207
208 void
209 netsnmp_ds_handle_config(const char *token, char *line)
210 {
211     netsnmp_ds_read_config *drsp;
212     char            buf[SNMP_MAXBUF];
213     char           *value, *endptr;
214     int             itmp;
215
216     DEBUGMSGTL(("netsnmp_ds_handle_config", "handling %s\n", token));
217
218     for (drsp = netsnmp_ds_configs;
219          drsp != NULL && strcasecmp(token, drsp->token) != 0;
220          drsp = drsp->next);
221
222     if (drsp != NULL) {
223         DEBUGMSGTL(("netsnmp_ds_handle_config",
224                     "setting: token=%s, type=%d, id=%s, which=%d\n",
225                     drsp->token, drsp->type, stores[drsp->storeid],
226                     drsp->which));
227
228         switch (drsp->type) {
229         case ASN_BOOLEAN:
230             value = strtok(line, " \t\n");
231             if (strcasecmp(value, "yes") == 0 || 
232                 strcasecmp(value, "true") == 0) {
233                 itmp = 1;
234             } else if (strcasecmp(value, "no") == 0 ||
235                        strcasecmp(value, "false") == 0) {
236                 itmp = 0;
237             } else {
238                 itmp = strtol(value, &endptr, 10);
239                 if (*endptr != 0 || itmp < 0 || itmp > 1) {
240                     config_perror("Should be yes|no|true|false|0|1");
241                 }
242             }
243             netsnmp_ds_set_boolean(drsp->storeid, drsp->which, itmp);
244             DEBUGMSGTL(("netsnmp_ds_handle_config", "bool: %d\n", itmp));
245             break;
246
247         case ASN_INTEGER:
248             value = strtok(line, " \t\n");
249             itmp = strtol(value, &endptr, 10);
250             if (*endptr != 0) {
251                 config_perror("Bad integer value");
252             } else {
253                 netsnmp_ds_set_int(drsp->storeid, drsp->which, itmp);
254             }
255             DEBUGMSGTL(("netsnmp_ds_handle_config", "int: %d\n", itmp));
256             break;
257
258         case ASN_OCTET_STR:
259             if (*line == '"') {
260                 copy_nword(line, buf, sizeof(buf));
261                 netsnmp_ds_set_string(drsp->storeid, drsp->which, buf);
262             } else {
263                 netsnmp_ds_set_string(drsp->storeid, drsp->which, line);
264             }
265             DEBUGMSGTL(("netsnmp_ds_handle_config", "string: %s\n", line));
266             break;
267
268         default:
269             snmp_log(LOG_ERR, "netsnmp_ds_handle_config: type %d (0x%02x)\n",
270                      drsp->type, drsp->type);
271             break;
272         }
273     } else {
274         snmp_log(LOG_ERR, "netsnmp_ds_handle_config: no registration for %s\n",
275                  token);
276     }
277 }
278
279
280 int
281 netsnmp_ds_register_config(u_char type, const char *ftype, const char *token,
282                            int storeid, int which)
283 {
284     netsnmp_ds_read_config *drsp;
285
286     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS    || 
287         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS || token == NULL) {
288         return SNMPERR_GENERR;
289     }
290
291     if (netsnmp_ds_configs == NULL) {
292         netsnmp_ds_configs = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
293         drsp = netsnmp_ds_configs;
294     } else {
295         for (drsp = netsnmp_ds_configs; drsp->next != NULL; drsp = drsp->next);
296         drsp->next = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
297         drsp = drsp->next;
298     }
299
300     drsp->type    = type;
301     drsp->ftype   = strdup(ftype);
302     drsp->token   = strdup(token);
303     drsp->storeid = storeid;
304     drsp->which   = which;
305
306     switch (type) {
307     case ASN_BOOLEAN:
308         register_config_handler(ftype, token, netsnmp_ds_handle_config, NULL,
309                                 "(1|yes|true|0|no|false)");
310         break;
311
312     case ASN_INTEGER:
313         register_config_handler(ftype, token, netsnmp_ds_handle_config, NULL,
314                                 "integerValue");
315         break;
316
317     case ASN_OCTET_STR:
318         register_config_handler(ftype, token, netsnmp_ds_handle_config, NULL,
319                                 "string");
320         break;
321
322     }
323     return SNMPERR_SUCCESS;
324 }
325
326 #ifdef BRCM_SNMP_SUPPORT
327 int
328 netsnmp_ds_register_premib(u_char type, const char *ftype, const char *token,
329                            int storeid, int which)
330 {
331     netsnmp_ds_read_config *drsp;
332
333     if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS    || 
334         which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS || token == NULL) {
335         return SNMPERR_GENERR;
336     }
337
338     if (netsnmp_ds_configs == NULL) {
339         netsnmp_ds_configs = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
340         drsp = netsnmp_ds_configs;
341     } else {
342         for (drsp = netsnmp_ds_configs; drsp->next != NULL; drsp = drsp->next);
343         drsp->next = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
344         drsp = drsp->next;
345     }
346
347     drsp->type    = type;
348     drsp->ftype   = strdup(ftype);
349     drsp->token   = strdup(token);
350     drsp->storeid = storeid;
351     drsp->which   = which;
352
353     switch (type) {
354     case ASN_BOOLEAN:
355         register_prenetsnmp_mib_handler(ftype, token, netsnmp_ds_handle_config,
356                                         NULL, "(1|yes|true|0|no|false)");
357         break;
358
359     case ASN_INTEGER:
360         register_prenetsnmp_mib_handler(ftype, token, netsnmp_ds_handle_config,
361                                         NULL, "integerValue");
362         break;
363
364     case ASN_OCTET_STR:
365         register_prenetsnmp_mib_handler(ftype, token, netsnmp_ds_handle_config,
366                                         NULL, "string");
367         break;
368
369     }
370     return SNMPERR_SUCCESS;
371 }
372 #endif /* BRCM_SNMP_SUPPORT */
373
374 void
375 netsnmp_ds_shutdown()
376 {
377     netsnmp_ds_read_config *drsp;
378     int             i, j;
379
380     for (drsp = netsnmp_ds_configs; drsp; drsp = netsnmp_ds_configs) {
381         netsnmp_ds_configs = drsp->next;
382
383         unregister_config_handler(drsp->ftype, drsp->token);
384         if (drsp->ftype != NULL) {
385             free(drsp->ftype);
386         }
387         if (drsp->token != NULL) {
388             free(drsp->token);
389         }
390         free(drsp);
391     }
392
393     for (i = 0; i < NETSNMP_DS_MAX_IDS; i++) {
394         for (j = 0; j < NETSNMP_DS_MAX_SUBIDS; j++) {
395             if (netsnmp_ds_strings[i][j] != NULL) {
396                 free(netsnmp_ds_strings[i][j]);
397                 netsnmp_ds_strings[i][j] = NULL;
398             }
399         }
400     }
401 }
402
403 #else /* BUILD_ILMI */
404
405
406 /*
407  * Prototype definitions 
408  */
409 void            netsnmp_ds_handle_config(const char *token, char *line);
410
411 int
412 netsnmp_ds_set_boolean(int storeid, int which, int value)
413 {
414     return SNMPERR_SUCCESS;
415 }
416
417 int
418 netsnmp_ds_toggle_boolean(int storeid, int which)
419 {
420     return SNMPERR_SUCCESS;
421 }
422
423 int
424 netsnmp_ds_get_boolean(int storeid, int which)
425 {
426     if (which == NETSNMP_DS_LIB_REVERSE_ENCODE)
427        return 1;
428     return SNMPERR_SUCCESS;
429 }
430
431 int
432 netsnmp_ds_set_int(int storeid, int which, int value)
433 {
434     return SNMPERR_SUCCESS;
435 }
436
437 int
438 netsnmp_ds_get_int(int storeid, int which)
439 {
440     return SNMPERR_SUCCESS;
441 }
442
443 int
444 netsnmp_ds_set_string(int storeid, int which, const char *value)
445 {
446     return SNMPERR_SUCCESS;
447 }
448
449 char *
450 netsnmp_ds_get_string(int storeid, int which)
451 {
452   return NULL;
453 }
454
455 int
456 netsnmp_ds_set_void(int storeid, int which, void *value)
457 {
458     return SNMPERR_SUCCESS;
459 }
460
461 void *
462 netsnmp_ds_get_void(int storeid, int which)
463 {
464   return NULL;
465 }
466
467 void
468 netsnmp_ds_handle_config(const char *token, char *line)
469 {
470   return;
471 }
472
473 int
474 netsnmp_ds_register_config(u_char type, const char *ftype, const char *token,
475                            int storeid, int which)
476 {
477     return SNMPERR_SUCCESS;
478 }
479
480 int
481 netsnmp_ds_register_premib(u_char type, const char *ftype, const char *token,
482                            int storeid, int which)
483 {
484     return SNMPERR_SUCCESS;
485 }
486
487 void
488 netsnmp_ds_shutdown()
489 {
490   return;
491 }
492
493 #endif