Debian Codegraph @

agent/mibgroup/header_complex.c

raw
1 /*
2 * header complex: More complex storage and data sorting for mib modules
3 */
4
5 #include <net-snmp/net-snmp-config.h>
6
7 #include <sys/types.h>
8 #ifdef HAVE_STDLIB_H
9 #include <stdlib.h>
10 #endif
11 #ifdef HAVE_STRING_H
12 #include <string.h>
13 #else
14 #include <strings.h>
15 #endif
16
17 #include <net-snmp/net-snmp-includes.h>
18 #include <net-snmp/agent/net-snmp-agent-includes.h>
19 #include "header_complex.h"
20
21 #include <net-snmp/net-snmp-features.h>
22
23 netsnmp_feature_child_of(header_complex_all, libnetsnmpmibs);
24
25 netsnmp_feature_child_of(header_complex_free_all, header_complex_all);
26 netsnmp_feature_child_of(header_complex_find_entry, header_complex_all);
27
28 int
29 header_complex_generate_varoid(netsnmp_variable_list * var)
30 {
31 int i;
32
33 if (var->name == NULL) {
34 /*
35 * assume cached value is correct
36 */
37 switch (var->type) {
38 case ASN_INTEGER:
39 case ASN_COUNTER:
40 case ASN_GAUGE:
41 case ASN_TIMETICKS:
42 var->name_length = 1;
43 var->name = (oid *) malloc(sizeof(oid));
44 if (var->name == NULL)
45 return SNMPERR_GENERR;
46 var->name[0] = *(var->val.integer);
47 break;
48
49 case ASN_PRIV_IMPLIED_OBJECT_ID:
50 var->name_length = var->val_len / sizeof(oid);
51 var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
52 if (var->name == NULL)
53 return SNMPERR_GENERR;
54
55 for (i = 0; i < (int) var->name_length; i++)
56 var->name[i] = var->val.objid[i];
57 break;
58
59 case ASN_OBJECT_ID:
60 var->name_length = var->val_len / sizeof(oid) + 1;
61 var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
62 if (var->name == NULL)
63 return SNMPERR_GENERR;
64
65 var->name[0] = var->name_length - 1;
66 for (i = 0; i < (int) var->name_length - 1; i++)
67 var->name[i + 1] = var->val.objid[i];
68 break;
69
70 case ASN_PRIV_IMPLIED_OCTET_STR:
71 var->name_length = var->val_len;
72 var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
73 if (var->name == NULL)
74 return SNMPERR_GENERR;
75
76 for (i = 0; i < (int) var->val_len; i++)
77 var->name[i] = (oid) var->val.string[i];
78 break;
79
80 case ASN_OPAQUE:
81 case ASN_OCTET_STR:
82 var->name_length = var->val_len + 1;
83 var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
84 if (var->name == NULL)
85 return SNMPERR_GENERR;
86
87 var->name[0] = (oid) var->val_len;
88 for (i = 0; i < (int) var->val_len; i++)
89 var->name[i + 1] = (oid) var->val.string[i];
90 break;
91
92 default:
93 DEBUGMSGTL(("header_complex_generate_varoid",
94 "invalid asn type: %d\n", var->type));
95 return SNMPERR_GENERR;
96 }
97 }
98 if (var->name_length > MAX_OID_LEN) {
99 DEBUGMSGTL(("header_complex_generate_varoid",
100 "Something terribly wrong, namelen = %d\n",
101 (int)var->name_length));
102 return SNMPERR_GENERR;
103 }
104
105 return SNMPERR_SUCCESS;
106 }
107
108 /*
109 * header_complex_parse_oid(): parses an index to the usmTable to
110 * break it down into a engineID component and a name component.
111 * The results are stored in the data pointer, as a varbindlist:
112 *
113 *
114 * returns 1 if an error is encountered, or 0 if successful.
115 */
116 int
117 header_complex_parse_oid(oid * oidIndex, size_t oidLen,
118 netsnmp_variable_list * data)
119 {
120 netsnmp_variable_list *var = data;
121 int i, itmp;
122
123 while (var && oidLen > 0) {
124 switch (var->type) {
125 case ASN_INTEGER:
126 case ASN_COUNTER:
127 case ASN_GAUGE:
128 case ASN_TIMETICKS:
129 var->val.integer = (long *) calloc(1, sizeof(long));
130 if (var->val.string == NULL)
131 return SNMPERR_GENERR;
132
133 *var->val.integer = (long) *oidIndex++;
134 var->val_len = sizeof(long);
135 oidLen--;
136 DEBUGMSGTL(("header_complex_parse_oid",
137 "Parsed int(%d): %ld\n", var->type,
138 *var->val.integer));
139 break;
140
141 case ASN_OBJECT_ID:
142 case ASN_PRIV_IMPLIED_OBJECT_ID:
143 if (var->type == ASN_PRIV_IMPLIED_OBJECT_ID) {
144 itmp = oidLen;
145 } else {
146 itmp = (long) *oidIndex++;
147 oidLen--;
148 if (itmp > (int) oidLen)
149 return SNMPERR_GENERR;
150 }
151
152 if (itmp == 0)
153 break; /* zero length strings shouldn't malloc */
154
155 var->val_len = itmp * sizeof(oid);
156 var->val.objid = (oid *) calloc(1, var->val_len);
157 if (var->val.objid == NULL)
158 return SNMPERR_GENERR;
159
160 for (i = 0; i < itmp; i++)
161 var->val.objid[i] = (u_char) * oidIndex++;
162 oidLen -= itmp;
163
164 DEBUGMSGTL(("header_complex_parse_oid", "Parsed oid: "));
165 DEBUGMSGOID(("header_complex_parse_oid", var->val.objid,
166 var->val_len / sizeof(oid)));
167 DEBUGMSG(("header_complex_parse_oid", "\n"));
168 break;
169
170 case ASN_OPAQUE:
171 case ASN_OCTET_STR:
172 case ASN_PRIV_IMPLIED_OCTET_STR:
173 if (var->type == ASN_PRIV_IMPLIED_OCTET_STR) {
174 itmp = oidLen;
175 } else {
176 itmp = (long) *oidIndex++;
177 oidLen--;
178 if (itmp > (int) oidLen)
179 return SNMPERR_GENERR;
180 }
181
182 if (itmp == 0)
183 break; /* zero length strings shouldn't malloc */
184
185 /*
186 * malloc by size+1 to allow a null to be appended.
187 */
188 var->val_len = itmp;
189 var->val.string = (u_char *) calloc(1, itmp + 1);
190 if (var->val.string == NULL)
191 return SNMPERR_GENERR;
192
193 for (i = 0; i < itmp; i++)
194 var->val.string[i] = (u_char) * oidIndex++;
195 var->val.string[itmp] = '\0';
196 oidLen -= itmp;
197
198 DEBUGMSGTL(("header_complex_parse_oid",
199 "Parsed str(%d): %s\n", var->type,
200 var->val.string));
201 break;
202
203 default:
204 DEBUGMSGTL(("header_complex_parse_oid",
205 "invalid asn type: %d\n", var->type));
206 return SNMPERR_GENERR;
207 }
208 var = var->next_variable;
209 }
210 if (var != NULL || oidLen > 0)
211 return SNMPERR_GENERR;
212 return SNMPERR_SUCCESS;
213 }
214
215
216 void
217 header_complex_generate_oid(oid * name, /* out */
218 size_t * length, /* out */
219 oid * prefix,
220 size_t prefix_len,
221 netsnmp_variable_list * data)
222 {
223
224 oid *oidptr;
225 netsnmp_variable_list *var;
226
227 if (prefix) {
228 memcpy(name, prefix, prefix_len * sizeof(oid));
229 oidptr = (name + (prefix_len));
230 *length = prefix_len;
231 } else {
232 oidptr = name;
233 *length = 0;
234 }
235
236 for (var = data; var != NULL; var = var->next_variable) {
237 header_complex_generate_varoid(var);
238 memcpy(oidptr, var->name, sizeof(oid) * var->name_length);
239 oidptr = oidptr + var->name_length;
240 *length += var->name_length;
241 }
242
243 DEBUGMSGTL(("header_complex_generate_oid", "generated: "));
244 DEBUGMSGOID(("header_complex_generate_oid", name, *length));
245 DEBUGMSG(("header_complex_generate_oid", "\n"));
246 }
247
248 /*
249 * finds the data in "datalist" stored at "index"
250 */
251 void *
252 header_complex_get(struct header_complex_index *datalist,
253 netsnmp_variable_list * index)
254 {
255 oid searchfor[MAX_OID_LEN];
256 size_t searchfor_len;
257
258 header_complex_generate_oid(searchfor, /* out */
259 &searchfor_len, /* out */
260 NULL, 0, index);
261 return header_complex_get_from_oid(datalist, searchfor, searchfor_len);
262 }
263
264 void *
265 header_complex_get_from_oid(struct header_complex_index *datalist,
266 oid * searchfor, size_t searchfor_len)
267 {
268 struct header_complex_index *nptr;
269 for (nptr = datalist; nptr != NULL; nptr = nptr->next) {
270 if (netsnmp_oid_equals(searchfor, searchfor_len,
271 nptr->name, nptr->namelen) == 0)
272 return nptr->data;
273 }
274 return NULL;
275 }
276
277
278 void *
279 header_complex(struct header_complex_index *datalist,
280 struct variable *vp,
281 oid * name,
282 size_t * length,
283 int exact, size_t * var_len, WriteMethod ** write_method)
284 {
285
286 struct header_complex_index *nptr, *found = NULL;
287 oid indexOid[MAX_OID_LEN];
288 size_t len;
289 int result;
290
291 /*
292 * set up some nice defaults for the user
293 */
294 if (write_method)
295 *write_method = NULL;
296 if (var_len)
297 *var_len = sizeof(long);
298
299 for (nptr = datalist; nptr != NULL && found == NULL; nptr = nptr->next) {
300 if (vp) {
301 memcpy(indexOid, vp->name, vp->namelen * sizeof(oid));
302 memcpy(indexOid + vp->namelen, nptr->name,
303 nptr->namelen * sizeof(oid));
304 len = vp->namelen + nptr->namelen;
305 } else {
306 memcpy(indexOid, nptr->name, nptr->namelen * sizeof(oid));
307 len = nptr->namelen;
308 }
309 result = snmp_oid_compare(name, *length, indexOid, len);
310 DEBUGMSGTL(("header_complex", "Checking: "));
311 DEBUGMSGOID(("header_complex", indexOid, len));
312 DEBUGMSG(("header_complex", "\n"));
313
314 if (exact) {
315 if (result == 0) {
316 found = nptr;
317 }
318 } else {
319 if (result == 0) {
320 /*
321 * found an exact match. Need the next one for !exact
322 */
323 if (nptr->next)
324 found = nptr->next;
325 } else if (result == -1) {
326 found = nptr;
327 }
328 }
329 }
330 if (found) {
331 if (vp) {
332 memcpy(name, vp->name, vp->namelen * sizeof(oid));
333 memcpy(name + vp->namelen, found->name,
334 found->namelen * sizeof(oid));
335 *length = vp->namelen + found->namelen;
336 } else {
337 memcpy(name, found->name, found->namelen * sizeof(oid));
338 *length = found->namelen;
339 }
340 return found->data;
341 }
342
343 return NULL;
344 }
345
346 struct header_complex_index *
347 header_complex_maybe_add_data(struct header_complex_index **thedata,
348 netsnmp_variable_list * var, void *data,
349 int dont_allow_duplicates)
350 {
351 oid newoid[MAX_OID_LEN];
352 size_t newoid_len;
353 struct header_complex_index *ret;
354
355 if (thedata == NULL || var == NULL || data == NULL)
356 return NULL;
357
358 header_complex_generate_oid(newoid, &newoid_len, NULL, 0, var);
359 ret =
360 header_complex_maybe_add_data_by_oid(thedata, newoid, newoid_len, data,
361 dont_allow_duplicates);
362 /*
363 * free the variable list, but not the enclosed data! it's not ours!
364 */
365 snmp_free_varbind(var);
366 return (ret);
367 }
368
369 struct header_complex_index *
370 header_complex_add_data(struct header_complex_index **thedata,
371 netsnmp_variable_list * var, void *data)
372 {
373 return
374 header_complex_maybe_add_data(thedata, var, data, 0);
375 }
376
377
378 struct header_complex_index *
379 _header_complex_add_between(struct header_complex_index **thedata,
380 struct header_complex_index *hciptrp,
381 struct header_complex_index *hciptrn,
382 oid * newoid, size_t newoid_len, void *data)
383 {
384 struct header_complex_index *ourself;
385
386 /*
387 * nptr should now point to the spot that we need to add ourselves
388 * in front of, and pptr should be our new 'prev'.
389 */
390
391 /*
392 * create ourselves
393 */
394 ourself = (struct header_complex_index *)
395 SNMP_MALLOC_STRUCT(header_complex_index);
396 if (ourself == NULL)
397 return NULL;
398
399 /*
400 * change our pointers
401 */
402 ourself->prev = hciptrp;
403 ourself->next = hciptrn;
404
405 if (ourself->next)
406 ourself->next->prev = ourself;
407
408 if (ourself->prev)
409 ourself->prev->next = ourself;
410
411 ourself->data = data;
412 ourself->name = snmp_duplicate_objid(newoid, newoid_len);
413 ourself->namelen = newoid_len;
414
415 /*
416 * rewind to the head of the list and return it (since the new head
417 * could be us, we need to notify the above routine who the head now is.
418 */
419 for (hciptrp = ourself; hciptrp->prev != NULL;
420 hciptrp = hciptrp->prev);
421
422 *thedata = hciptrp;
423 DEBUGMSGTL(("header_complex_add_data", "adding something...\n"));
424
425 return hciptrp;
426 }
427
428
429 struct header_complex_index *
430 header_complex_maybe_add_data_by_oid(struct header_complex_index **thedata,
431 oid * newoid, size_t newoid_len, void *data,
432 int dont_allow_duplicates)
433 {
434 struct header_complex_index *hciptrn, *hciptrp;
435 int rc;
436
437 if (thedata == NULL || newoid == NULL || data == NULL)
438 return NULL;
439
440 for (hciptrn = *thedata, hciptrp = NULL;
441 hciptrn != NULL; hciptrp = hciptrn, hciptrn = hciptrn->next) {
442 /*
443 * XXX: check for == and error (overlapping table entries)
444 * 8/2005 rks Ok, I added duplicate entry check, but only log
445 * warning and continue, because it seems that nobody
446 * that calls this fucntion does error checking!.
447 */
448 rc = snmp_oid_compare(hciptrn->name, hciptrn->namelen,
449 newoid, newoid_len);
450 if (rc > 0)
451 break;
452 else if (0 == rc) {
453 snmp_log(LOG_WARNING, "header_complex_add_data_by_oid with "
454 "duplicate index.\n");
455 if (dont_allow_duplicates)
456 return NULL;
457 }
458 }
459
460 return _header_complex_add_between(thedata, hciptrp, hciptrn,
461 newoid, newoid_len, data);
462 }
463
464 struct header_complex_index *
465 header_complex_add_data_by_oid(struct header_complex_index **thedata,
466 oid * newoid, size_t newoid_len, void *data)
467 {
468 return header_complex_maybe_add_data_by_oid(thedata, newoid, newoid_len,
469 data, 0);
470 }
471
472 /*
473 * extracts an entry from the storage space (removing it from future
474 * accesses) and returns the data stored there
475 *
476 * Modifies "thetop" pointer as needed (and if present) if were
477 * extracting the first node.
478 */
479
480 void *
481 header_complex_extract_entry(struct header_complex_index **thetop,
482 struct header_complex_index *thespot)
483 {
484 struct header_complex_index *hciptrp, *hciptrn;
485 void *retdata;
486
487 if (thespot == NULL) {
488 DEBUGMSGTL(("header_complex_extract_entry",
489 "Null pointer asked to be extracted\n"));
490 return NULL;
491 }
492
493 retdata = thespot->data;
494
495 hciptrp = thespot->prev;
496 hciptrn = thespot->next;
497
498 if (hciptrp)
499 hciptrp->next = hciptrn;
500 else if (thetop)
501 *thetop = hciptrn;
502
503 if (hciptrn)
504 hciptrn->prev = hciptrp;
505
506 if (thespot->name)
507 free(thespot->name);
508
509 free(thespot);
510 return retdata;
511 }
512
513 /*
514 * wipe out a single entry
515 */
516 void
517 header_complex_free_entry(struct header_complex_index *theentry,
518 HeaderComplexCleaner * cleaner)
519 {
520 void *data;
521 data = header_complex_extract_entry(NULL, theentry);
522 (*cleaner) (data);
523 }
524
525 /*
526 * completely wipe out all entries in our data store
527 */
528 #ifndef NETSNMP_FEATURE_REMOVE_HEADER_COMPLEX_FREE_ALL
529 void
530 header_complex_free_all(struct header_complex_index *thestuff,
531 HeaderComplexCleaner * cleaner)
532 {
533 struct header_complex_index *hciptr, *hciptrn;
534
535 for (hciptr = thestuff; hciptr != NULL; hciptr = hciptrn) {
536 hciptrn = hciptr->next; /* need to extract this before deleting it */
537 header_complex_free_entry(hciptr, cleaner);
538 }
539 }
540 #endif /* NETSNMP_FEATURE_REMOVE_HEADER_COMPLEX_FREE_ALL */
541
542 #ifndef NETSNMP_FEATURE_REMOVE_HEADER_COMPLEX_FIND_ENTRY
543 struct header_complex_index *
544 header_complex_find_entry(struct header_complex_index *thestuff,
545 void *theentry)
546 {
547 struct header_complex_index *hciptr;
548
549 for (hciptr = thestuff; hciptr != NULL && hciptr->data != theentry;
550 hciptr = hciptr->next);
551 return hciptr;
552 }
553 #endif /* NETSNMP_FEATURE_REMOVE_HEADER_COMPLEX_FIND_ENTRY */
554
555 #ifdef TESTING
556
557 void
558 header_complex_dump(struct header_complex_index *thestuff)
559 {
560 struct header_complex_index *hciptr;
561 oid oidsave[MAX_OID_LEN];
562 size_t len;
563
564 for (hciptr = thestuff; hciptr != NULL; hciptr = hciptr->next) {
565 DEBUGMSGTL(("header_complex_dump", "var: "));
566 header_complex_generate_oid(oidsave, &len, NULL, 0, hciptr->);
567 DEBUGMSGOID(("header_complex_dump", oidsave, len));
568 DEBUGMSG(("header_complex_dump", "\n"));
569 }
570 }
571
572 int main(void)
573 {
574 oid oidsave[MAX_OID_LEN];
575 int len = MAX_OID_LEN, len2;
576 netsnmp_variable_list *vars;
577 long ltmp = 4242, ltmp2 = 88, ltmp3 = 1, ltmp4 = 4200;
578 oid ourprefix[] = { 1, 2, 3, 4 };
579 oid testparse[] = { 4, 116, 101, 115, 116, 4200 };
580 int ret;
581
582 char *string = "wes", *string2 = "dawn", *string3 = "test";
583
584 struct header_complex_index *thestorage = NULL;
585
586 debug_register_tokens("header_complex");
587 snmp_set_do_debugging(1);
588
589 vars = NULL;
590 len2 = sizeof(ltmp);
591 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, (char *) &ltmp,
592 len2);
593 header_complex_add_data(&thestorage, vars, ourprefix);
594
595 vars = NULL;
596 len2 = strlen(string);
597 snmp_varlist_add_variable(&vars, NULL, 0, ASN_OCTET_STR, string, len2);
598 header_complex_add_data(&thestorage, vars, ourprefix);
599
600 vars = NULL;
601 len2 = sizeof(ltmp2);
602 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, (char *) &ltmp2,
603 len2);
604 header_complex_add_data(&thestorage, vars, ourprefix);
605
606 vars = NULL;
607 len2 = strlen(string2);
608 snmp_varlist_add_variable(&vars, NULL, 0, ASN_OCTET_STR, string2,
609 len2);
610 header_complex_add_data(&thestorage, vars, ourprefix);
611
612 vars = NULL;
613 len2 = sizeof(ltmp3);
614 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, (char *) &ltmp3,
615 len2);
616 header_complex_add_data(&thestorage, vars, ourprefix);
617
618 vars = NULL;
619 len2 = strlen(string3);
620 snmp_varlist_add_variable(&vars, NULL, 0, ASN_OCTET_STR, string3,
621 len2);
622 len2 = sizeof(ltmp4);
623 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, (char *) &ltmp4,
624 len2);
625 header_complex_add_data(&thestorage, vars, ourprefix);
626
627 header_complex_dump(thestorage);
628
629 vars = NULL;
630 snmp_varlist_add_variable(&vars, NULL, 0, ASN_OCTET_STR, NULL, 0);
631 snmp_varlist_add_variable(&vars, NULL, 0, ASN_INTEGER, NULL, 0);
632 ret =
633 header_complex_parse_oid(testparse,
634 sizeof(testparse) / sizeof(oid), vars);
635 DEBUGMSGTL(("header_complex_test", "parse returned %d...\n", ret));
636
637 }
638 #endif