1 #ifdef BRCM_SNMP_NOT_USED
3 * header complex: More complex storage and data sorting for mib modules
6 #include <net-snmp/net-snmp-config.h>
21 #include <net-snmp/net-snmp-includes.h>
22 #include <net-snmp/agent/net-snmp-agent-includes.h>
23 #include "header_complex.h"
26 header_complex_generate_varoid(netsnmp_variable_list * var)
30 if (var->name == NULL) {
32 * assume cached value is correct
40 var->name = (oid *) malloc(sizeof(oid));
41 if (var->name == NULL)
42 return SNMPERR_GENERR;
43 var->name[0] = *(var->val.integer);
46 case ASN_PRIV_IMPLIED_OBJECT_ID:
47 var->name_length = var->val_len / sizeof(oid);
48 var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
49 if (var->name == NULL)
50 return SNMPERR_GENERR;
52 for (i = 0; i < (int) var->name_length; i++)
53 var->name[i] = var->val.objid[i];
57 var->name_length = var->val_len / sizeof(oid) + 1;
58 var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
59 if (var->name == NULL)
60 return SNMPERR_GENERR;
62 var->name[0] = var->name_length - 1;
63 for (i = 0; i < (int) var->name_length - 1; i++)
64 var->name[i + 1] = var->val.objid[i];
67 case ASN_PRIV_IMPLIED_OCTET_STR:
68 var->name_length = var->val_len;
69 var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
70 if (var->name == NULL)
71 return SNMPERR_GENERR;
73 for (i = 0; i < (int) var->val_len; i++)
74 var->name[i] = (oid) var->val.string[i];
79 var->name_length = var->val_len + 1;
80 var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
81 if (var->name == NULL)
82 return SNMPERR_GENERR;
84 var->name[0] = (oid) var->val_len;
85 for (i = 0; i < (int) var->val_len; i++)
86 var->name[i + 1] = (oid) var->val.string[i];
90 DEBUGMSGTL(("header_complex_generate_varoid",
91 "invalid asn type: %d\n", var->type));
92 return SNMPERR_GENERR;
95 if (var->name_length > MAX_OID_LEN) {
96 DEBUGMSGTL(("header_complex_generate_varoid",
97 "Something terribly wrong, namelen = %d\n",
99 return SNMPERR_GENERR;
102 return SNMPERR_SUCCESS;
106 * header_complex_parse_oid(): parses an index to the usmTable to
107 * break it down into a engineID component and a name component.
108 * The results are stored in the data pointer, as a varbindlist:
111 * returns 1 if an error is encountered, or 0 if successful.
114 header_complex_parse_oid(oid * oidIndex, size_t oidLen,
115 netsnmp_variable_list * data)
117 netsnmp_variable_list *var = data;
120 while (var && oidLen > 0) {
126 var->val.integer = (long *) calloc(1, sizeof(long));
127 if (var->val.string == NULL)
128 return SNMPERR_GENERR;
130 *var->val.integer = (long) *oidIndex++;
131 var->val_len = sizeof(long);
133 DEBUGMSGTL(("header_complex_parse_oid",
134 "Parsed int(%d): %d\n", var->type,
139 case ASN_PRIV_IMPLIED_OBJECT_ID:
140 if (var->type == ASN_PRIV_IMPLIED_OBJECT_ID) {
143 itmp = (long) *oidIndex++;
145 if (itmp > (int) oidLen)
146 return SNMPERR_GENERR;
150 break; /* zero length strings shouldn't malloc */
152 var->val_len = itmp * sizeof(oid);
153 var->val.objid = (oid *) calloc(1, var->val_len);
154 if (var->val.objid == NULL)
155 return SNMPERR_GENERR;
157 for (i = 0; i < itmp; i++)
158 var->val.objid[i] = (u_char) * oidIndex++;
161 DEBUGMSGTL(("header_complex_parse_oid", "Parsed oid: "));
162 DEBUGMSGOID(("header_complex_parse_oid", var->val.objid,
163 var->val_len / sizeof(oid)));
164 DEBUGMSG(("header_complex_parse_oid", "\n"));
169 case ASN_PRIV_IMPLIED_OCTET_STR:
170 if (var->type == ASN_PRIV_IMPLIED_OCTET_STR) {
173 itmp = (long) *oidIndex++;
175 if (itmp > (int) oidLen)
176 return SNMPERR_GENERR;
180 break; /* zero length strings shouldn't malloc */
183 * malloc by size+1 to allow a null to be appended.
186 var->val.string = (u_char *) calloc(1, itmp + 1);
187 if (var->val.string == NULL)
188 return SNMPERR_GENERR;
190 for (i = 0; i < itmp; i++)
191 var->val.string[i] = (u_char) * oidIndex++;
192 var->val.string[itmp] = '\0';
195 DEBUGMSGTL(("header_complex_parse_oid",
196 "Parsed str(%d): %s\n", var->type,
201 DEBUGMSGTL(("header_complex_parse_oid",
202 "invalid asn type: %d\n", var->type));
203 return SNMPERR_GENERR;
205 var = var->next_variable;
207 if (var != NULL || oidLen > 0)
208 return SNMPERR_GENERR;
209 return SNMPERR_SUCCESS;
214 header_complex_generate_oid(oid * name, /* out */
215 size_t * length, /* out */
218 netsnmp_variable_list * data)
222 netsnmp_variable_list *var;
225 memcpy(name, prefix, prefix_len * sizeof(oid));
226 oidptr = (name + (prefix_len));
227 *length = prefix_len;
233 for (var = data; var != NULL; var = var->next_variable) {
234 header_complex_generate_varoid(var);
235 memcpy(oidptr, var->name, sizeof(oid) * var->name_length);
236 oidptr = oidptr + var->name_length;
237 *length += var->name_length;
240 DEBUGMSGTL(("header_complex_generate_oid", "generated: "));
241 DEBUGMSGOID(("header_complex_generate_oid", name, *length));
242 DEBUGMSG(("header_complex_generate_oid", "\n"));
246 * finds the data in "datalist" stored at "index"
249 header_complex_get(struct header_complex_index *datalist,
250 netsnmp_variable_list * index)
252 oid searchfor[MAX_OID_LEN];
253 size_t searchfor_len;
255 header_complex_generate_oid(searchfor, /* out */
256 &searchfor_len, /* out */
258 return header_complex_get_from_oid(datalist, searchfor, searchfor_len);
262 header_complex_get_from_oid(struct header_complex_index *datalist,
263 oid * searchfor, size_t searchfor_len)
265 struct header_complex_index *nptr;
266 for (nptr = datalist; nptr != NULL; nptr = nptr->next) {
267 if (netsnmp_oid_equals(searchfor, searchfor_len,
268 nptr->name, nptr->namelen) == 0)
276 header_complex(struct header_complex_index *datalist,
280 int exact, size_t * var_len, WriteMethod ** write_method)
283 struct header_complex_index *nptr, *found = NULL;
284 oid indexOid[MAX_OID_LEN];
289 * set up some nice defaults for the user
292 *write_method = NULL;
294 *var_len = sizeof(long);
296 for (nptr = datalist; nptr != NULL && found == NULL; nptr = nptr->next) {
298 memcpy(indexOid, vp->name, vp->namelen * sizeof(oid));
299 memcpy(indexOid + vp->namelen, nptr->name,
300 nptr->namelen * sizeof(oid));
301 len = vp->namelen + nptr->namelen;
303 memcpy(indexOid, nptr->name, nptr->namelen * sizeof(oid));
306 result = snmp_oid_compare(name, *length, indexOid, len);
307 DEBUGMSGTL(("header_complex", "Checking: "));
308 DEBUGMSGOID(("header_complex", indexOid, len));
309 DEBUGMSG(("header_complex", "\n"));
318 * found an exact match. Need the next one for !exact
322 } else if (result == -1) {
329 memcpy(name, vp->name, vp->namelen * sizeof(oid));
330 memcpy(name + vp->namelen, found->name,
331 found->namelen * sizeof(oid));
332 *length = vp->namelen + found->namelen;
334 memcpy(name, found->name, found->namelen * sizeof(oid));
335 *length = found->namelen;
343 struct header_complex_index *
344 header_complex_add_data(struct header_complex_index **thedata,
345 netsnmp_variable_list * var, void *data)
347 oid newoid[MAX_OID_LEN];
349 struct header_complex_index *ret;
351 if (thedata == NULL || var == NULL || data == NULL)
354 header_complex_generate_oid(newoid, &newoid_len, NULL, 0, var);
356 header_complex_add_data_by_oid(thedata, newoid, newoid_len, data);
358 * free the variable list, but not the enclosed data! it's not ours!
360 snmp_free_varbind(var);
364 struct header_complex_index *
365 header_complex_add_data_by_oid(struct header_complex_index **thedata,
366 oid * newoid, size_t newoid_len, void *data)
368 struct header_complex_index *hciptrn, *hciptrp, *ourself;
370 if (thedata == NULL || newoid == NULL || data == NULL)
373 for (hciptrn = *thedata, hciptrp = NULL;
374 hciptrn != NULL; hciptrp = hciptrn, hciptrn = hciptrn->next)
376 * XXX: check for == and error (overlapping table entries)
379 (hciptrn->name, hciptrn->namelen, newoid, newoid_len)
384 * nptr should now point to the spot that we need to add ourselves
385 * in front of, and pptr should be our new 'prev'.
391 ourself = (struct header_complex_index *)
392 SNMP_MALLOC_STRUCT(header_complex_index);
395 * change our pointers
397 ourself->prev = hciptrp;
398 ourself->next = hciptrn;
401 ourself->next->prev = ourself;
404 ourself->prev->next = ourself;
406 ourself->data = data;
407 ourself->name = snmp_duplicate_objid(newoid, newoid_len);
408 ourself->namelen = newoid_len;
411 * rewind to the head of the list and return it (since the new head
412 * could be us, we need to notify the above routine who the head now is.
414 for (hciptrp = ourself; hciptrp->prev != NULL;
415 hciptrp = hciptrp->prev);
418 DEBUGMSGTL(("header_complex_add_data", "adding something...\n"));
424 * extracts an entry from the storage space (removing it from future
425 * accesses) and returns the data stored there
427 * Modifies "thetop" pointer as needed (and if present) if were
428 * extracting the first node.
432 header_complex_extract_entry(struct header_complex_index **thetop,
433 struct header_complex_index *thespot)
435 struct header_complex_index *hciptrp, *hciptrn;
436 void *retdata = thespot->data;
438 if (thespot == NULL) {
439 DEBUGMSGTL(("header_complex_extract_entry",
440 "Null pointer asked to be extracted\n"));
444 hciptrp = thespot->prev;
445 hciptrn = thespot->next;
448 hciptrp->next = hciptrn;
453 hciptrn->prev = hciptrp;
463 * wipe out a single entry
466 header_complex_free_entry(struct header_complex_index *theentry,
467 HeaderComplexCleaner * cleaner)
470 data = header_complex_extract_entry(NULL, theentry);
475 * completely wipe out all entries in our data store
478 header_complex_free_all(struct header_complex_index *thestuff,
479 HeaderComplexCleaner * cleaner)
481 struct header_complex_index *hciptr, *hciptrn;
483 for (hciptr = thestuff; hciptr != NULL; hciptr = hciptrn) {
484 hciptrn = hciptr->next; /* need to extract this before deleting it */
485 header_complex_free_entry(hciptr, cleaner);
489 struct header_complex_index *
490 header_complex_find_entry(struct header_complex_index *thestuff,
493 struct header_complex_index *hciptr;
495 for (hciptr = thestuff; hciptr != NULL && hciptr->data != theentry;
496 hciptr = hciptr->next);
503 header_complex_dump(struct header_complex_index *thestuff)
505 struct header_complex_index *hciptr;
506 oid oidsave[MAX_OID_LEN];
509 for (hciptr = thestuff; hciptr != NULL; hciptr = hciptr->next) {
510 DEBUGMSGTL(("header_complex_dump", "var: "));
511 header_complex_generate_oid(oidsave, &len, NULL, 0, hciptr->);
512 DEBUGMSGOID(("header_complex_dump", oidsave, len));
513 DEBUGMSG(("header_complex_dump", "\n"));
519 oid oidsave[MAX_OID_LEN];
520 int len = MAX_OID_LEN, len2;
521 netsnmp_variable_list *vars;
522 long ltmp = 4242, ltmp2 = 88, ltmp3 = 1, ltmp4 = 4200;
523 oid ourprefix[] = { 1, 2, 3, 4 };
524 oid testparse[] = { 4, 116, 101, 115, 116, 4200 };
527 char *string = "wes", *string2 = "dawn", *string3 = "test";
529 struct header_complex_index *thestorage = NULL;
531 debug_register_tokens("header_complex");
532 snmp_set_do_debugging(1);
536 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, (char *) <mp,
538 header_complex_add_data(&thestorage, vars, ourprefix);
541 len2 = strlen(string);
542 snmp_varlist_add_variable(&vars, NULL, 0, ASN_OCTET_STR, string, len2);
543 header_complex_add_data(&thestorage, vars, ourprefix);
546 len2 = sizeof(ltmp2);
547 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, (char *) <mp2,
549 header_complex_add_data(&thestorage, vars, ourprefix);
552 len2 = strlen(string2);
553 snmp_varlist_add_variable(&vars, NULL, 0, ASN_OCTET_STR, string2,
555 header_complex_add_data(&thestorage, vars, ourprefix);
558 len2 = sizeof(ltmp3);
559 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, (char *) <mp3,
561 header_complex_add_data(&thestorage, vars, ourprefix);
564 len2 = strlen(string3);
565 snmp_varlist_add_variable(&vars, NULL, 0, ASN_OCTET_STR, string3,
567 len2 = sizeof(ltmp4);
568 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, (char *) <mp4,
570 header_complex_add_data(&thestorage, vars, ourprefix);
572 header_complex_dump(thestorage);
575 snmp_varlist_add_variable(&vars, NULL, 0, ASN_OCTET_STR, NULL, 0);
576 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, NULL, 0);
578 header_complex_parse_oid(testparse,
579 sizeof(testparse) / sizeof(oid), vars);
580 DEBUGMSGTL(("header_complex_test", "parse returned %d...\n", ret));
585 #endif /* #if BRCM_SNMP_NOT_USED */