Debian Codegraph @

perl/SNMP/SNMP.xs

raw ◀ ▶
2 Debian patches touch this file
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 SNMP.xs -- Perl 5 interface to the Net-SNMP toolkit
3
4 written by G. S. Marzot ([email protected])
5
6 Copyright (c) 1995-2006 G. S. Marzot. All rights reserved.
7 This program is free software; you can redistribute it and/or
8 modify it under the same terms as Perl itself.
9 */
10
11 #pragma GCC diagnostic push
12 #pragma GCC diagnostic ignored "-Wcast-qual"
13 #pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
14 #include "EXTERN.h"
15 #include "perl.h"
16 #include "XSUB.h"
17 #pragma GCC diagnostic pop
18
19 #include <net-snmp/net-snmp-config.h>
20 #include <net-snmp/net-snmp-includes.h>
21 #include <sys/types.h>
22 #include <arpa/inet.h>
23 #include <errno.h>
24 #ifndef MSVC_PERL
25 #include <signal.h>
26 #endif
27 #include <stdio.h>
28 #include <ctype.h>
29 #ifdef I_SYS_TIME
30 #include <sys/time.h>
31 #endif
32 #include <netdb.h>
33 #include <stdlib.h>
34 #ifndef MSVC_PERL
35 #include <unistd.h>
36 #endif
37
38 #ifdef HAVE_REGEX_H
39 #include <regex.h>
40 #endif
41
42 #ifndef __P
43 #define __P(x) x
44 #endif
45
46 #ifndef na
47 #define na PL_na
48 #endif
49
50 #ifndef sv_undef
51 #define sv_undef PL_sv_undef
52 #endif
53
54 #ifndef stack_base
55 #define stack_base PL_stack_base
56 #endif
57
58 #ifndef G_VOID
59 #define G_VOID G_DISCARD
60 #endif
61
62 #include "perlsnmp.h"
63
64 #define SUCCESS 1
65 #define FAILURE 0
66
67 #define ZERO_BUT_TRUE "0 but true"
68
69 #define SNMP_API_TRADITIONAL 0
70 #define SNMP_API_SINGLE 1
71
72 #define VARBIND_TAG_F 0
73 #define VARBIND_IID_F 1
74 #define VARBIND_VAL_F 2
75 #define VARBIND_TYPE_F 3
76
77 #define TYPE_UNKNOWN 0
78 #define MAX_TYPE_NAME_LEN 32
79 #define STR_BUF_SIZE (MAX_TYPE_NAME_LEN * MAX_OID_LEN)
80 #define ENG_ID_BUF_SIZE 32
81
82 #define SYS_UPTIME_OID_LEN 9
83 #define SNMP_TRAP_OID_LEN 11
84 #define NO_RETRY_NOSUCH 0
85 static oid sysUpTime[SYS_UPTIME_OID_LEN] = {1, 3, 6, 1, 2, 1, 1, 3, 0};
86 static oid snmpTrapOID[SNMP_TRAP_OID_LEN] = {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0};
87
88 /* Internal flag to determine snmp_main_loop() should return after callback */
89 static int mainloop_finish = 0;
90
91 /* Internal flag to determine which API we're using */
92 static int api_mode = SNMP_API_TRADITIONAL;
93
94 /* why does ucd-snmp redefine sockaddr_in ??? */
95 #define SIN_ADDR(snmp_addr) (((struct sockaddr_in *) &(snmp_addr))->sin_addr)
96
97 typedef netsnmp_session SnmpSession;
98 typedef struct tree SnmpMibNode;
99 typedef struct snmp_xs_cb_data {
100 SV* perl_cb;
101 SV* sess_ref;
102 } snmp_xs_cb_data;
103
104 static void __recalc_timeout _((struct timeval*,struct timeval*,
105 struct timeval*,struct timeval*, int* ));
106 static int __is_numeric_oid _((char*));
107 static int __is_leaf _((struct tree*));
108 static int __translate_appl_type _((char*));
109 static int __translate_asn_type _((int));
110 static int __snprint_value _((char *, size_t,
111 netsnmp_variable_list*, struct tree *,
112 int, int));
113 static int __snprint_num_objid _((char *, size_t, const oid *, int));
114 static int __scan_num_objid _((char *, oid *, size_t *));
115 static int __get_type_str _((int, char *));
116 static int __get_label_iid _((char *, char **, char **, int));
117 static int __oid_cmp _((oid *, size_t, oid *, size_t));
118 static int __tp_sprint_num_objid _((char*, size_t, const SnmpMibNode *));
119 static SnmpMibNode * __get_next_mib_node _((SnmpMibNode *));
120 static struct tree * __tag2oid _((char *, char *, oid *, size_t *, int *, int));
121 static int __concat_oid_str _((oid *, size_t *, char *));
122 static int __add_var_val_str _((netsnmp_pdu *, oid *, size_t, char *,
123 int, int));
124 static SV* __push_cb_args2 _((SV * sv, SV * esv, SV * tsv));
125 #define __push_cb_args(a,b) __push_cb_args2(a,b,NULL)
126 static int __call_callback _((SV * sv, int flags));
127 static char* __av_elem_pv _((AV * av, I32 key, char *dflt));
128
129 #define USE_NUMERIC_OIDS 0x08
130 #define NON_LEAF_NAME 0x04
131 #define USE_LONG_NAMES 0x02
132 #define FAIL_ON_NULL_IID 0x01
133 #define NO_FLAGS 0x00
134
135 /* Structures used by snmp_bulkwalk method to track requested OID's/subtrees. */
136 typedef struct bulktbl {
137 oid req_oid[MAX_OID_LEN]; /* The OID originally requested. */
138 oid last_oid[MAX_OID_LEN]; /* Last-seen OID under this branch. */
139 AV *vars; /* Array of Varbinds for this OID. */
140 size_t req_len; /* Length of requested OID. */
141 size_t last_len; /* Length of last-seen OID. */
142 char norepeat; /* Is this a non-repeater OID? */
143 char complete; /* Non-zero if this tree complete. */
144 char ignore; /* Ignore this OID, not requested. */
145 } bulktbl;
146
147 /* Context for bulkwalk() sessions. Used to store state across callbacks. */
148 typedef struct walk_context {
149 SV *sess_ref; /* Reference to Perl SNMP session object. */
150 SV *perl_cb; /* Pointer to Perl callback func or array. */
151 bulktbl *req_oids; /* Pointer to bulktbl[] for requested OIDs. */
152 bulktbl *repbase; /* Pointer to first repeater in req_oids[]. */
153 bulktbl *reqbase; /* Pointer to start of requests req_oids[]. */
154 int nreq_oids; /* Number of valid bulktbls in req_oids[]. */
155 int req_remain; /* Number of outstanding requests remaining */
156 int non_reps; /* Number of nonrepeater vars in req_oids[] */
157 int repeaters; /* Number of repeater vars in req_oids[]. */
158 int max_reps; /* Maximum repetitions of variable per PDU. */
159 int exp_reqid; /* Expect a response to this request only. */
160 int getlabel_f; /* Flag long/numeric names for get_label(). */
161 int sprintval_f; /* Flag enum/sprint values for sprintval(). */
162 int pkts_exch; /* Number of packet exchanges with agent. */
163 int oid_total; /* Total number of OIDs received this walk. */
164 int oid_saved; /* Total number of OIDs saved as results. */
165 } walk_context;
166
167 /* Prototypes for bulkwalk support functions. */
168 static netsnmp_pdu *_bulkwalk_send_pdu _((walk_context *context));
169 static int _bulkwalk_done _((walk_context *context));
170 static int _bulkwalk_recv_pdu _((walk_context *context, netsnmp_pdu *pdu));
171 static int _bulkwalk_finish _((walk_context *context, int okay));
172 static int _bulkwalk_async_cb _((int op, SnmpSession *ss, int reqid,
173 netsnmp_pdu *pdu, void *context_ptr));
174
175 /* Prototype for error handler */
176 void snmp_return_err(void *ss, SV *err_str, SV *err_num, SV *err_ind);
177
178 /* Structure to hold valid context sessions. */
179 struct valid_contexts {
180 walk_context **valid; /* Array of valid walk_context pointers. */
181 int sz_valid; /* Maximum size of valid contexts array. */
182 int num_valid; /* Count of valid contexts in the array. */
183 };
184 static struct valid_contexts *_valid_contexts = NULL;
185 static int _context_add _((walk_context *context));
186 static int _context_del _((walk_context *context));
187 static int _context_okay _((walk_context *context));
188
189 /* Wrapper around fprintf(stderr, ...) for clean and easy debug output. */
190 #ifdef DEBUGGING
191 static int _debug_level = 0;
192 #define DBOUT PerlIO_stderr(),
193 #define DBPRT(severity, otherargs) \
194 do { \
195 if (_debug_level && severity <= _debug_level) { \
196 (void)PerlIO_printf otherargs; \
197 } \
198 } while (/*CONSTCOND*/0)
199
200 char _debugx[1024]; /* Space to sprintf() into - used by sprint_objid(). */
201
202 /* wrapper around snprint_objid to snprint_objid to return the pointer
203 instead of length */
204
205 static char *
206 __snprint_oid(const oid *objid, size_t objidlen) {
207 snprint_objid(_debugx, sizeof(_debugx), objid, objidlen);
208 return _debugx;
209 }
210
211 #define DBDCL(x) x
212 #else /* DEBUGGING */
213 #define DBDCL(x)
214 #define DBOUT
215 /* Do nothing but in such a way that the compiler sees "otherargs". */
216 #define DBPRT(severity, otherargs) \
217 do { if (0) printf otherargs; } while(0)
218
219 static char *
220 __snprint_oid(const oid *objid, size_t objidlen)
221 {
222 return "(debugging is disabled)";
223 }
224
225 #endif /* DEBUGGING */
226
227 void
228 __libraries_init(char *appname)
229 {
230 static int have_inited = 0;
231
232 if (have_inited)
233 return;
234 have_inited = 1;
235
236 SOCK_STARTUP;
237
238 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
239 NETSNMP_DS_LIB_QUICK_PRINT, 1);
240 init_snmp(appname);
241
242 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS, 1);
243 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_SUFFIX_ONLY, 1);
244 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
245 NETSNMP_OID_OUTPUT_SUFFIX);
246 }
247
248 static void
249 __recalc_timeout (tvp, ctvp, ltvp, itvp, block)
250 struct timeval* tvp;
251 struct timeval* ctvp;
252 struct timeval* ltvp;
253 struct timeval* itvp;
254 int *block;
255 {
256 struct timeval now;
257
258 if (!timerisset(itvp)) return; /* interval zero means loop forever */
259 *block = 0;
260 gettimeofday(&now,(struct timezone *)0);
261
262 if (ctvp->tv_sec < 0) { /* first time or callback just fired */
263 timersub(&now,ltvp,ctvp);
264 timersub(ctvp,itvp,ctvp);
265 timersub(itvp,ctvp,ctvp);
266 timeradd(ltvp,itvp,ltvp);
267 } else {
268 timersub(&now,ltvp,ctvp);
269 timersub(itvp,ctvp,ctvp);
270 }
271
272 /* flag is set for callback but still hasnt fired so set to something
273 * small and we will service packets first if there are any ready
274 * (also guard against negative timeout - should never happen?)
275 */
276 if (!timerisset(ctvp) || ctvp->tv_sec < 0 || ctvp->tv_usec < 0) {
277 ctvp->tv_sec = 0;
278 ctvp->tv_usec = 10;
279 }
280
281 /* if snmp timeout > callback timeout or no more requests to process */
282 if (timercmp(tvp, ctvp, >) || !timerisset(tvp)) {
283 *tvp = *ctvp; /* use the smaller non-zero timeout */
284 timerclear(ctvp); /* used as a flag to let callback fire on timeout */
285 }
286 }
287
288 static int
289 __is_numeric_oid (oidstr)
290 char* oidstr;
291 {
292 if (!oidstr) return 0;
293 for (; *oidstr; oidstr++) {
294 if (isalpha((int)*oidstr)) return 0;
295 }
296 return(1);
297 }
298
299 static int
300 __is_leaf (tp)
301 struct tree* tp;
302 {
303 char buf[MAX_TYPE_NAME_LEN];
304 return (tp && __get_type_str(tp->type,buf));
305 }
306
307 static SnmpMibNode*
308 __get_next_mib_node (tp)
309 SnmpMibNode* tp;
310 {
311 /* printf("tp = %lX, parent = %lX, peer = %lX, child = %lX\n",
312 tp, tp->parent, tp->next_peer, tp->child_list); */
313 if (tp->child_list) return(tp->child_list);
314 if (tp->next_peer) return(tp->next_peer);
315 if (!tp->parent) return(NULL);
316 for (tp = tp->parent; !tp->next_peer; tp = tp->parent) {
317 if (!tp->parent) return(NULL);
318 }
319 return(tp->next_peer);
320 }
321
322 static int
323 __translate_appl_type(typestr)
324 char* typestr;
325 {
326 if (typestr == NULL || *typestr == '\0') return TYPE_UNKNOWN;
327
328 if (!strncasecmp(typestr,"INTEGER32",8))
329 return(TYPE_INTEGER32);
330 if (!strncasecmp(typestr,"INTEGER",3))
331 return(TYPE_INTEGER);
332 if (!strncasecmp(typestr,"UNSIGNED32",3))
333 return(TYPE_UNSIGNED32);
334 if (!strcasecmp(typestr,"COUNTER")) /* check all in case counter64 */
335 return(TYPE_COUNTER);
336 if (!strncasecmp(typestr,"GAUGE",3))
337 return(TYPE_GAUGE);
338 if (!strncasecmp(typestr,"IPADDR",3))
339 return(TYPE_IPADDR);
340 if (!strncasecmp(typestr,"OCTETSTR",3))
341 return(TYPE_OCTETSTR);
342 if (!strncasecmp(typestr,"TICKS",3))
343 return(TYPE_TIMETICKS);
344 if (!strncasecmp(typestr,"OPAQUE",3))
345 return(TYPE_OPAQUE);
346 if (!strncasecmp(typestr,"OBJECTID",3))
347 return(TYPE_OBJID);
348 if (!strncasecmp(typestr,"NETADDR",3))
349 return(TYPE_NETADDR);
350 if (!strncasecmp(typestr,"COUNTER64",3))
351 return(TYPE_COUNTER64);
352 if (!strncasecmp(typestr,"NULL",3))
353 return(TYPE_NULL);
354 if (!strncasecmp(typestr,"BITS",3))
355 return(TYPE_BITSTRING);
356 if (!strncasecmp(typestr,"ENDOFMIBVIEW",3))
357 return(SNMP_ENDOFMIBVIEW);
358 if (!strncasecmp(typestr,"NOSUCHOBJECT",7))
359 return(SNMP_NOSUCHOBJECT);
360 if (!strncasecmp(typestr,"NOSUCHINSTANCE",7))
361 return(SNMP_NOSUCHINSTANCE);
362 if (!strncasecmp(typestr,"UINTEGER",3))
363 return(TYPE_UINTEGER); /* historic - should not show up */
364 /* but it does? */
365 if (!strncasecmp(typestr, "NOTIF", 3))
366 return(TYPE_NOTIFTYPE);
367 if (!strncasecmp(typestr, "TRAP", 4))
368 return(TYPE_TRAPTYPE);
369 return(TYPE_UNKNOWN);
370 }
371
372 static int
373 __translate_asn_type(type)
374 int type;
375 {
376 switch (type) {
377 case ASN_INTEGER:
378 return(TYPE_INTEGER);
379 break;
380 case ASN_OCTET_STR:
381 return(TYPE_OCTETSTR);
382 break;
383 case ASN_OPAQUE:
384 return(TYPE_OPAQUE);
385 break;
386 case ASN_OBJECT_ID:
387 return(TYPE_OBJID);
388 break;
389 case ASN_TIMETICKS:
390 return(TYPE_TIMETICKS);
391 break;
392 case ASN_GAUGE:
393 return(TYPE_GAUGE);
394 break;
395 case ASN_COUNTER:
396 return(TYPE_COUNTER);
397 break;
398 case ASN_IPADDRESS:
399 return(TYPE_IPADDR);
400 break;
401 case ASN_BIT_STR:
402 return(TYPE_BITSTRING);
403 break;
404 case ASN_NULL:
405 return(TYPE_NULL);
406 break;
407 /* no translation for these exception type values */
408 case SNMP_ENDOFMIBVIEW:
409 case SNMP_NOSUCHOBJECT:
410 case SNMP_NOSUCHINSTANCE:
411 return(type);
412 break;
413 case ASN_UINTEGER:
414 return(TYPE_UINTEGER);
415 break;
416 case ASN_COUNTER64:
417 return(TYPE_COUNTER64);
418 break;
419 default:
420 warn("translate_asn_type: unhandled asn type (%d)\n",type);
421 return(TYPE_OTHER);
422 break;
423 }
424 }
425
426 #define USE_BASIC 0
427 #define USE_ENUMS 1
428 #define USE_SPRINT_VALUE 2
429 static int
430 #if defined(__has_attribute) && __has_attribute(nonnull)
431 __attribute__((nonnull(1)))
432 #endif
433 __snprint_value(char *buf, size_t buf_len, netsnmp_variable_list *var,
434 struct tree *tp, int type, int flag)
435 {
436 int len = 0;
437 u_char* ip;
438 struct enum_list *ep;
439
440
441 buf[0] = '\0';
442 if (flag == USE_SPRINT_VALUE) {
443 snprint_value(buf, buf_len, var->name, var->name_length, var);
444 len = strlen(buf);
445 } else {
446 switch (var->type) {
447 case ASN_INTEGER:
448 if (flag == USE_ENUMS) {
449 for(ep = tp->enums; ep; ep = ep->next) {
450 if (ep->value == *var->val.integer) {
451 strlcpy(buf, ep->label, buf_len);
452 len = strlen(buf);
453 break;
454 }
455 }
456 }
457 if (!len) {
458 snprintf(buf, buf_len, "%ld", *var->val.integer);
459 buf[buf_len-1] = '\0';
460 len = strlen(buf);
461 }
462 break;
463
464 case ASN_GAUGE:
465 case ASN_COUNTER:
466 case ASN_TIMETICKS:
467 case ASN_UINTEGER:
468 snprintf(buf, buf_len, "%lu", (unsigned long) *var->val.integer);
469 buf[buf_len-1] = '\0';
470 len = strlen(buf);
471 break;
472
473 case ASN_OCTET_STR:
474 case ASN_OPAQUE:
475 len = var->val_len;
476 if ( len > buf_len )
477 len = buf_len;
478 memcpy(buf, (char*)var->val.string, len);
479 break;
480
481 case ASN_IPADDRESS:
482 ip = (u_char*)var->val.string;
483 snprintf(buf, buf_len, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
484 buf[buf_len-1] = '\0';
485 len = strlen(buf);
486 break;
487
488 case ASN_NULL:
489 break;
490
491 case ASN_OBJECT_ID:
492 __snprint_num_objid(buf, buf_len, var->val.objid,
493 var->val_len / sizeof(oid));
494 len = strlen(buf);
495 break;
496
497 case SNMP_ENDOFMIBVIEW:
498 snprintf(buf, buf_len, "%s", "ENDOFMIBVIEW");
499 break;
500 case SNMP_NOSUCHOBJECT:
501 snprintf(buf, buf_len, "%s", "NOSUCHOBJECT");
502 break;
503 case SNMP_NOSUCHINSTANCE:
504 snprintf(buf, buf_len, "%s", "NOSUCHINSTANCE");
505 break;
506
507 case ASN_COUNTER64:
508 #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
509 case ASN_OPAQUE_COUNTER64:
510 case ASN_OPAQUE_U64:
511 #endif
512 printU64(buf,(struct counter64 *)var->val.counter64);
513 len = strlen(buf);
514 break;
515
516 #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
517 case ASN_OPAQUE_I64:
518 printI64(buf,(struct counter64 *)var->val.counter64);
519 len = strlen(buf);
520 break;
521 #endif
522
523 case ASN_BIT_STR:
524 snprint_bitstring(buf, buf_len, var, NULL, NULL, NULL);
525 len = strlen(buf);
526 break;
527 #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
528 case ASN_OPAQUE_FLOAT:
529 if (var->val.floatVal)
530 snprintf(buf, buf_len, "%f", *var->val.floatVal);
531 break;
532
533 case ASN_OPAQUE_DOUBLE:
534 if (var->val.doubleVal)
535 snprintf(buf, buf_len, "%f", *var->val.doubleVal);
536 break;
537 #endif
538
539 case ASN_NSAP:
540 default:
541 warn("snprint_value: asn type not handled %d\n",var->type);
542 }
543 }
544 return(len);
545 }
546
547 static int
548 __snprint_num_objid (buf, buf_len, objid, len)
549 char *buf;
550 size_t buf_len;
551 const oid *objid;
552 int len;
553 {
554 const char* const end = buf + buf_len;
555 int i;
556
557 buf[0] = '\0';
558 for (i=0; i < len; i++) {
559 snprintf(buf, end - buf, ".%" NETSNMP_PRIo "u", *objid++);
560 buf += strlen(buf);
561 }
562 return SUCCESS;
563 }
564
565 static int
566 __tp_sprint_num_objid (buf, buf_len, tp)
567 char *buf;
568 size_t buf_len;
569 const SnmpMibNode *tp;
570 {
571 oid newname[MAX_OID_LEN], *op;
572 /* code taken from get_node in snmp_client.c */
573 for (op = newname + MAX_OID_LEN - 1; op >= newname; op--) {
574 *op = tp->subid;
575 tp = tp->parent;
576 if (tp == NULL) break;
577 }
578 return __snprint_num_objid(buf, buf_len, op, newname + MAX_OID_LEN - op);
579 }
580
581 static int
582 __scan_num_objid (buf, objid, len)
583 char *buf;
584 oid *objid;
585 size_t *len;
586 {
587 char *cp;
588 *len = 0;
589 if (*buf == '.') buf++;
590 cp = buf;
591 while (*buf) {
592 if (*buf++ == '.') {
593 sscanf(cp, "%" NETSNMP_PRIo "u", objid++);
594 /* *objid++ = atoi(cp); */
595 (*len)++;
596 cp = buf;
597 } else {
598 if (isalpha((int)*buf)) {
599 return FAILURE;
600 }
601 }
602 }
603 sscanf(cp, "%" NETSNMP_PRIo "u", objid++);
604 /* *objid++ = atoi(cp); */
605 (*len)++;
606 return SUCCESS;
607 }
608
609 static int
610 __get_type_str (type, str)
611 int type;
612 char * str;
613 {
614 switch (type) {
615 case TYPE_OBJID:
616 strcpy(str, "OBJECTID");
617 break;
618 case TYPE_OCTETSTR:
619 strcpy(str, "OCTETSTR");
620 break;
621 case TYPE_INTEGER:
622 strcpy(str, "INTEGER");
623 break;
624 case TYPE_INTEGER32:
625 strcpy(str, "INTEGER32");
626 break;
627 case TYPE_UNSIGNED32:
628 strcpy(str, "UNSIGNED32");
629 break;
630 case TYPE_NETADDR:
631 strcpy(str, "NETADDR");
632 break;
633 case TYPE_IPADDR:
634 strcpy(str, "IPADDR");
635 break;
636 case TYPE_COUNTER:
637 strcpy(str, "COUNTER");
638 break;
639 case TYPE_GAUGE:
640 strcpy(str, "GAUGE");
641 break;
642 case TYPE_TIMETICKS:
643 strcpy(str, "TICKS");
644 break;
645 case TYPE_OPAQUE:
646 strcpy(str, "OPAQUE");
647 break;
648 case TYPE_COUNTER64:
649 strcpy(str, "COUNTER64");
650 break;
651 case TYPE_NULL:
652 strcpy(str, "NULL");
653 break;
654 case SNMP_ENDOFMIBVIEW:
655 strcpy(str, "ENDOFMIBVIEW");
656 break;
657 case SNMP_NOSUCHOBJECT:
658 strcpy(str, "NOSUCHOBJECT");
659 break;
660 case SNMP_NOSUCHINSTANCE:
661 strcpy(str, "NOSUCHINSTANCE");
662 break;
663 case TYPE_UINTEGER:
664 strcpy(str, "UINTEGER"); /* historic - should not show up */
665 /* but it does? */
666 break;
667 case TYPE_NOTIFTYPE:
668 strcpy(str, "NOTIF");
669 break;
670 case TYPE_BITSTRING:
671 strcpy(str, "BITS");
672 break;
673 case TYPE_TRAPTYPE:
674 strcpy(str, "TRAP");
675 break;
676 case TYPE_OTHER: /* not sure if this is a valid leaf type?? */
677 case TYPE_NSAPADDRESS:
678 default: /* unsupported types for now */
679 strcpy(str, "");
680 return(FAILURE);
681 }
682 return SUCCESS;
683 }
684
685 /* does a destructive disection of <label1>...<labeln>.<iid> returning
686 <labeln> and <iid> in seperate strings (note: will destructively
687 alter input string, 'name') */
688 static int
689 __get_label_iid (name, last_label, iid, flag)
690 char * name;
691 char ** last_label;
692 char ** iid;
693 int flag;
694 {
695 char *lcp;
696 char *icp;
697 int len = strlen(name);
698 int found_label = 0;
699
700 *last_label = *iid = NULL;
701
702 if (len == 0) return(FAILURE);
703
704 /* Handle case where numeric oid's have been requested. The input 'name'
705 ** in this case should be a numeric OID -- return failure if not.
706 */
707 if ((flag & USE_NUMERIC_OIDS)) {
708 if (!__is_numeric_oid(name))
709 return(FAILURE);
710
711 /* Walk backward through the string, looking for first two '.' chars */
712 lcp = &(name[len]);
713 icp = NULL;
714 while (lcp > name) {
715 if (*lcp == '.') {
716
717 /* If this is the first occurence of '.', note it in icp.
718 ** Otherwise, this must be the second occurrence, so break
719 ** out of the loop.
720 */
721 if (icp == NULL)
722 icp = lcp;
723 else
724 break;
725 }
726 lcp --;
727 }
728
729 /* Make sure we found at least a label and index. */
730 if (!icp)
731 return(FAILURE);
732
733 /* Push forward past leading '.' chars and separate the strings. */
734 lcp ++;
735 *icp ++ = '\0';
736
737 *last_label = (flag & USE_LONG_NAMES) ? name : lcp;
738 *iid = icp;
739
740 return(SUCCESS);
741 }
742
743 lcp = icp = &(name[len]);
744
745 while (lcp > name) {
746 if (*lcp == '.') {
747 if (found_label) {
748 lcp++;
749 break;
750 } else {
751 icp = lcp;
752 }
753 }
754 if (!found_label && isalpha((unsigned char)*lcp)) found_label = 1;
755 lcp--;
756 }
757
758 if (!found_label
759 || ((icp + 1 >= name + len || !isdigit((unsigned char)*(icp+1)))
760 && (flag & FAIL_ON_NULL_IID)))
761 return(FAILURE);
762
763 if (flag & NON_LEAF_NAME) { /* dont know where to start instance id */
764 /* put the whole thing in label */
765 icp = &(name[len]);
766 flag |= USE_LONG_NAMES;
767 /* special hack in case no mib loaded - object identifiers will
768 * start with .iso.<num>.<num>...., in which case it is preferable
769 * to make the label entirely numeric (i.e., convert "iso" => "1")
770 */
771 if (*lcp == '.' && lcp == name) {
772 if (!strncmp(".ccitt.",lcp,7)) {
773 name += 2;
774 *name = '.';
775 *(name+1) = '0';
776 } else if (!strncmp(".iso.",lcp,5)) {
777 name += 2;
778 *name = '.';
779 *(name+1) = '1';
780 } else if (!strncmp(".joint-iso-ccitt.",lcp,17)) {
781 name += 2;
782 *name = '.';
783 *(name+1) = '2';
784 }
785 }
786 } else if (*icp) {
787 *(icp++) = '\0';
788 }
789 *last_label = (flag & USE_LONG_NAMES ? name : lcp);
790
791 *iid = icp;
792
793 return(SUCCESS);
794 }
795
796
797 static int
798 __oid_cmp(oida_arr, oida_arr_len, oidb_arr, oidb_arr_len)
799 oid *oida_arr;
800 size_t oida_arr_len;
801 oid *oidb_arr;
802 size_t oidb_arr_len;
803 {
804 for (;oida_arr_len && oidb_arr_len;
805 oida_arr++, oida_arr_len--, oidb_arr++, oidb_arr_len--) {
806 if (*oida_arr == *oidb_arr) continue;
807 return(*oida_arr > *oidb_arr ? 1 : -1);
808 }
809 if (oida_arr_len == oidb_arr_len) return(0);
810 return(oida_arr_len > oidb_arr_len ? 1 : -1);
811 }
812
813 /* Convert a tag (string) to an OID array */
814 /* Tag can be either a symbolic name, or an OID string */
815 static struct tree *
816 __tag2oid(tag, iid, oid_arr, oid_arr_len, type, best_guess)
817 char * tag;
818 char * iid;
819 oid * oid_arr;
820 size_t * oid_arr_len;
821 int * type;
822 int best_guess;
823 {
824 struct tree *tp = NULL;
825 struct tree *rtp = NULL;
826 oid newname[MAX_OID_LEN], *op;
827 size_t newname_len = 0;
828
829 if (type) *type = TYPE_UNKNOWN;
830 if (oid_arr_len) *oid_arr_len = 0;
831 if (!tag) goto done;
832
833 /*********************************************************/
834 /* best_guess = 0 - same as no switches (read_objid) */
835 /* if multiple parts, or uses find_node */
836 /* if a single leaf */
837 /* best_guess = 1 - same as -Ib (get_wild_node) */
838 /* best_guess = 2 - same as -IR (get_node) */
839 /*********************************************************/
840
841 /* numeric scalar (1,2) */
842 /* single symbolic (1,2) */
843 /* single regex (1) */
844 /* partial full symbolic (2) */
845 /* full symbolic (2) */
846 /* module::single symbolic (2) */
847 /* module::partial full symbolic (2) */
848 if (best_guess == 1 || best_guess == 2) {
849 if (!__scan_num_objid(tag, newname, &newname_len)) { /* make sure it's not a numeric tag */
850 newname_len = MAX_OID_LEN;
851 if (best_guess == 2) { /* Random search -IR */
852 if (get_node(tag, newname, &newname_len)) {
853 rtp = tp = get_tree(newname, newname_len, get_tree_head());
854 }
855 }
856 else if (best_guess == 1) { /* Regex search -Ib */
857 clear_tree_flags(get_tree_head());
858 if (get_wild_node(tag, newname, &newname_len)) {
859 rtp = tp = get_tree(newname, newname_len, get_tree_head());
860 }
861 }
862 }
863 else {
864 rtp = tp = get_tree(newname, newname_len, get_tree_head());
865 }
866 if (type) *type = (tp ? tp->type : TYPE_UNKNOWN);
867 if ((oid_arr == NULL) || (oid_arr_len == NULL)) return rtp;
868 memcpy(oid_arr,(char*)newname,newname_len*sizeof(oid));
869 *oid_arr_len = newname_len;
870 }
871
872 /* if best_guess is off and multi part tag or module::tag */
873 /* numeric scalar */
874 /* module::single symbolic */
875 /* module::partial full symbolic */
876 /* FULL symbolic OID */
877 else if (strchr(tag,'.') || strchr(tag,':')) {
878 if (!__scan_num_objid(tag, newname, &newname_len)) { /* make sure it's not a numeric tag */
879 newname_len = MAX_OID_LEN;
880 if (read_objid(tag, newname, &newname_len)) { /* long name */
881 rtp = tp = get_tree(newname, newname_len, get_tree_head());
882 }
883 }
884 else {
885 rtp = tp = get_tree(newname, newname_len, get_tree_head());
886 }
887 if (type) *type = (tp ? tp->type : TYPE_UNKNOWN);
888 if ((oid_arr == NULL) || (oid_arr_len == NULL)) return rtp;
889 memcpy(oid_arr,(char*)newname,newname_len*sizeof(oid));
890 *oid_arr_len = newname_len;
891 }
892
893 /* else best_guess is off and it is a single leaf */
894 /* single symbolic */
895 else {
896 rtp = tp = find_node(tag, get_tree_head());
897 if (tp) {
898 if (type) *type = tp->type;
899 if ((oid_arr == NULL) || (oid_arr_len == NULL)) return rtp;
900 /* code taken from get_node in snmp_client.c */
901 for(op = newname + MAX_OID_LEN - 1; op >= newname; op--){
902 *op = tp->subid;
903 tp = tp->parent;
904 if (tp == NULL)
905 break;
906 }
907 *oid_arr_len = newname + MAX_OID_LEN - op;
908 memcpy(oid_arr, op, *oid_arr_len * sizeof(oid));
909 } else {
910 return(rtp); /* HACK: otherwise, concat_oid_str confuses things */
911 }
912 }
913 done:
914 if (iid && *iid && oid_arr_len) __concat_oid_str(oid_arr, oid_arr_len, iid);
915 return(rtp);
916 }
917
918 /* function: __concat_oid_str
919 *
920 * This function converts a dotted-decimal string, soid_str, to an array
921 * of oid types and concatenates them on doid_arr begining at the index
922 * specified by doid_arr_len.
923 *
924 * returns : SUCCESS, FAILURE
925 */
926 static int
927 __concat_oid_str(doid_arr, doid_arr_len, soid_str)
928 oid *doid_arr;
929 size_t *doid_arr_len;
930 char * soid_str;
931 {
932 char *soid_buf;
933 char *cp;
934 char *st;
935
936 if (!soid_str || !*soid_str) return SUCCESS;/* successfully added nothing */
937 if (*soid_str == '.') soid_str++;
938 soid_buf = netsnmp_strdup(soid_str);
939 if (!soid_buf)
940 return FAILURE;
941 cp = strtok_r(soid_buf,".",&st);
942 while (cp) {
943 sscanf(cp, "%" NETSNMP_PRIo "u", &(doid_arr[(*doid_arr_len)++]));
944 /* doid_arr[(*doid_arr_len)++] = atoi(cp); */
945 cp = strtok_r(NULL,".",&st);
946 }
947 netsnmp_free(soid_buf);
948 return(SUCCESS);
949 }
950
951 /*
952 * add a varbind to PDU
953 */
954 static int
955 __add_var_val_str(pdu, name, name_length, val, len, type)
956 netsnmp_pdu *pdu;
957 oid *name;
958 size_t name_length;
959 char * val;
960 int len;
961 int type;
962 {
963 netsnmp_variable_list *vars;
964 oid oidbuf[MAX_OID_LEN];
965 int ret = SUCCESS;
966
967 if (pdu->variables == NULL){
968 pdu->variables = vars
969 = netsnmp_calloc(1, sizeof(netsnmp_variable_list));
970 } else {
971 for(vars = pdu->variables;
972 vars->next_variable;
973 vars = vars->next_variable)
974 /*EXIT*/;
975 vars->next_variable = netsnmp_calloc(1, sizeof(netsnmp_variable_list));
976 vars = vars->next_variable;
977 }
978
979 vars->next_variable = NULL;
980 vars->name = netsnmp_memdup(name, name_length * sizeof(oid));
981 vars->name_length = name_length;
982 switch (type) {
983 case TYPE_INTEGER:
984 case TYPE_INTEGER32:
985 vars->type = ASN_INTEGER;
986 vars->val.integer = netsnmp_malloc(sizeof(long));
987 if (val)
988 *(vars->val.integer) = strtol(val,NULL,0);
989 else {
990 ret = FAILURE;
991 *(vars->val.integer) = 0;
992 }
993 vars->val_len = sizeof(long);
994 break;
995
996 case TYPE_GAUGE:
997 case TYPE_UNSIGNED32:
998 vars->type = ASN_GAUGE;
999 goto as_uint;
1000 case TYPE_COUNTER:
1001 vars->type = ASN_COUNTER;
1002 goto as_uint;
1003 case TYPE_TIMETICKS:
1004 vars->type = ASN_TIMETICKS;
1005 goto as_uint;
1006 case TYPE_UINTEGER:
1007 vars->type = ASN_UINTEGER;
1008 as_uint:
1009 vars->val.integer = netsnmp_malloc(sizeof(long));
1010 if (val)
1011 sscanf(val,"%lu",vars->val.integer);
1012 else {
1013 ret = FAILURE;
1014 *(vars->val.integer) = 0;
1015 }
1016 vars->val_len = sizeof(long);
1017 break;
1018
1019 case TYPE_OCTETSTR:
1020 vars->type = ASN_OCTET_STR;
1021 goto as_oct;
1022
1023 case TYPE_BITSTRING:
1024 vars->type = ASN_OCTET_STR;
1025 goto as_oct;
1026
1027 case TYPE_OPAQUE:
1028 vars->type = ASN_OCTET_STR;
1029 as_oct:
1030 vars->val.string = netsnmp_memdup(val && len ? val : "", len ? len : 1);
1031 vars->val_len = len;
1032 if (!val)
1033 ret = FAILURE;
1034 break;
1035
1036 case TYPE_IPADDR:
1037 vars->type = ASN_IPADDRESS;
1038 if (val) {
1039 const in_addr_t addr = inet_addr(val);
1040
1041 vars->val.integer = netsnmp_memdup(&addr, sizeof(addr));
1042 } else {
1043 ret = FAILURE;
1044 *(vars->val.integer) = 0;
1045 }
1046 vars->val_len = sizeof(in_addr_t);
1047 break;
1048
1049 case TYPE_OBJID:
1050 vars->type = ASN_OBJECT_ID;
1051 vars->val_len = MAX_OID_LEN;
1052 /* if (read_objid(val, oidbuf, &(vars->val_len))) { */
1053 /* tp = __tag2oid(val,NULL,oidbuf,&(vars->val_len),NULL,0); */
1054 if (!val || !snmp_parse_oid(val, oidbuf, &vars->val_len)) {
1055 vars->val.objid = NULL;
1056 ret = FAILURE;
1057 } else {
1058 vars->val_len *= sizeof(oid);
1059 vars->val.objid = netsnmp_memdup(oidbuf, vars->val_len);
1060 }
1061 break;
1062
1063 default:
1064 vars->type = ASN_NULL;
1065 vars->val_len = 0;
1066 vars->val.string = NULL;
1067 ret = FAILURE;
1068 }
1069
1070 return ret;
1071 }
1072
1073 /* takes ss and pdu as input and updates the 'response' argument */
1074 /* the input 'pdu' argument will be freed */
1075 static int
1076 __send_sync_pdu(void *ss, netsnmp_pdu *pdu, netsnmp_pdu **response,
1077 int retry_nosuch, SV *err_str_sv, SV *err_num_sv,
1078 SV *err_ind_sv)
1079 {
1080 int status;
1081 long command = pdu->command;
1082 *response = NULL;
1083 retry:
1084 if(api_mode == SNMP_API_SINGLE)
1085 {
1086 status = snmp_sess_synch_response(ss, pdu, response);
1087 } else {
1088 status = snmp_synch_response(ss, pdu, response);
1089 };
1090
1091 if ((*response == NULL) && (status == STAT_SUCCESS)) status = STAT_ERROR;
1092
1093 switch (status) {
1094 case STAT_SUCCESS:
1095 switch ((*response)->errstat) {
1096 case SNMP_ERR_NOERROR:
1097 break;
1098
1099 case SNMP_ERR_NOSUCHNAME:
1100 if (retry_nosuch && (pdu = snmp_fix_pdu(*response, command))) {
1101 if (*response) snmp_free_pdu(*response);
1102 goto retry;
1103 }
1104 /* FALLTHROUGH */
1105
1106 /* Pv1, SNMPsec, Pv2p, v2c, v2u, v2*, and SNMPv3 PDUs */
1107 case SNMP_ERR_TOOBIG:
1108 case SNMP_ERR_BADVALUE:
1109 case SNMP_ERR_READONLY:
1110 case SNMP_ERR_GENERR:
1111 /* in SNMPv2p, SNMPv2c, SNMPv2u, SNMPv2*, and SNMPv3 PDUs */
1112 case SNMP_ERR_NOACCESS:
1113 case SNMP_ERR_WRONGTYPE:
1114 case SNMP_ERR_WRONGLENGTH:
1115 case SNMP_ERR_WRONGENCODING:
1116 case SNMP_ERR_WRONGVALUE:
1117 case SNMP_ERR_NOCREATION:
1118 case SNMP_ERR_INCONSISTENTVALUE:
1119 case SNMP_ERR_RESOURCEUNAVAILABLE:
1120 case SNMP_ERR_COMMITFAILED:
1121 case SNMP_ERR_UNDOFAILED:
1122 case SNMP_ERR_AUTHORIZATIONERROR:
1123 case SNMP_ERR_NOTWRITABLE:
1124 /* in SNMPv2c, SNMPv2u, SNMPv2*, and SNMPv3 PDUs */
1125 case SNMP_ERR_INCONSISTENTNAME:
1126 default:
1127 sv_catpv(err_str_sv,
1128 (char*)snmp_errstring((*response)->errstat));
1129 sv_setiv(err_num_sv, (*response)->errstat);
1130 sv_setiv(err_ind_sv, (*response)->errindex);
1131 status = (*response)->errstat;
1132 break;
1133 }
1134 break;
1135
1136 case STAT_TIMEOUT:
1137 case STAT_ERROR:
1138 snmp_return_err(ss, err_str_sv, err_num_sv, err_ind_sv);
1139 break;
1140
1141 default:
1142 snmp_return_err(ss, err_str_sv, err_num_sv, err_ind_sv);
1143 sv_catpv(err_str_sv, "send_sync_pdu: unknown status");
1144 break;
1145 }
1146
1147 return(status);
1148 }
1149
1150 static int
1151 __snmp_xs_cb(int op, netsnmp_session *ss, int reqid, netsnmp_pdu *pdu,
1152 void *cb_data)
1153 {
1154 SV *varlist_ref;
1155 AV *varlist;
1156 SV *varbind_ref;
1157 AV *varbind;
1158 SV *traplist_ref = NULL;
1159 AV *traplist = NULL;
1160 netsnmp_variable_list *vars;
1161 struct tree *tp;
1162 int len;
1163 SV *tmp_sv;
1164 int type;
1165 char tmp_type_str[MAX_TYPE_NAME_LEN];
1166 char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;
1167 size_t str_buf_len = sizeof(str_buf);
1168 size_t out_len = 0;
1169 int buf_over = 0;
1170 char *label;
1171 char *iid;
1172 char *cp;
1173 int getlabel_flag = NO_FLAGS;
1174 int sprintval_flag = USE_BASIC;
1175 netsnmp_pdu *reply_pdu;
1176 int old_numeric, old_printfull, old_format;
1177 netsnmp_transport *transport = NULL;
1178
1179 SV* cb = ((struct snmp_xs_cb_data*)cb_data)->perl_cb;
1180 SV* sess_ref = ((struct snmp_xs_cb_data*)cb_data)->sess_ref;
1181 SV **err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
1182 SV **err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
1183 SV **err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
1184
1185 ENTER;
1186 SAVETMPS;
1187
1188 if (cb_data != ss->callback_magic)
1189 free(cb_data);
1190
1191 sv_setpv(*err_str_svp, (char*)snmp_errstring(pdu->errstat));
1192 sv_setiv(*err_num_svp, pdu->errstat);
1193 sv_setiv(*err_ind_svp, pdu->errindex);
1194
1195 varlist_ref = &sv_undef; /* Prevent unintialized use below. */
1196
1197 switch (op) {
1198 case NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE:
1199 traplist_ref = NULL;
1200 switch (pdu->command) {
1201 case SNMP_MSG_INFORM:
1202 /*
1203 * Ideally, we would use the return value from the callback to
1204 * decide what response, if any, we send, and what the error status
1205 * and error index should be.
1206 */
1207 reply_pdu = snmp_clone_pdu(pdu);
1208 if (reply_pdu) {
1209 reply_pdu->command = SNMP_MSG_RESPONSE;
1210 reply_pdu->reqid = pdu->reqid;
1211 reply_pdu->errstat = reply_pdu->errindex = 0;
1212 if (!snmp_send(ss, reply_pdu))
1213 snmp_free_pdu(reply_pdu);
1214 } else {
1215 warn("Couldn't clone PDU for inform response");
1216 }
1217 /* FALLTHRU */
1218 case SNMP_MSG_TRAP:
1219 case SNMP_MSG_TRAP2:
1220 traplist = newAV();
1221 traplist_ref = newRV_noinc((SV*)traplist);
1222 #if 0
1223 /* of dubious utility... */
1224 av_push(traplist, newSViv(pdu->command));
1225 #endif
1226 av_push(traplist, newSViv(pdu->reqid));
1227 if ((transport = snmp_sess_transport(snmp_sess_pointer(ss))) != NULL) {
1228 cp = transport->f_fmtaddr(transport, pdu->transport_data,
1229 pdu->transport_data_length);
1230 av_push(traplist, newSVpv(cp, strlen(cp)));
1231 netsnmp_free(cp);
1232 } else {
1233 /* This shouldn't ever happen; every session has a transport. */
1234 av_push(traplist, newSVpv("", 0));
1235 }
1236 av_push(traplist, newSVpv((char*) pdu->community, pdu->community_len));
1237 if (pdu->command == SNMP_MSG_TRAP) {
1238 /* SNMP v1 only trap fields */
1239 snprint_objid(str_buf, sizeof(str_buf), pdu->enterprise, pdu->enterprise_length);
1240 av_push(traplist, newSVpv(str_buf,strlen(str_buf)));
1241 cp = inet_ntoa(*((struct in_addr *) pdu->agent_addr));
1242 av_push(traplist, newSVpv(cp,strlen(cp)));
1243 av_push(traplist, newSViv(pdu->trap_type));
1244 av_push(traplist, newSViv(pdu->specific_type));
1245 /* perl didn't have perlSVuv until 5.6.0 */
1246 tmp_sv=newSViv(0);
1247 sv_setuv(tmp_sv, pdu->time);
1248 av_push(traplist, tmp_sv);
1249 }
1250 /* FALLTHRU */
1251 case SNMP_MSG_RESPONSE:
1252 {
1253 varlist = newAV();
1254 varlist_ref = newRV_noinc((SV*)varlist);
1255
1256 /*
1257 ** Set up for numeric OID's, if necessary. Save the old values
1258 ** so that they can be restored when we finish -- these are
1259 ** library-wide globals, and have to be set/restored for each
1260 ** session.
1261 */
1262 old_numeric = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS);
1263 old_printfull = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID);
1264 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1265 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1))) {
1266 getlabel_flag |= USE_LONG_NAMES;
1267 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID, 1);
1268 }
1269 /* Setting UseNumeric forces UseLongNames on so check for UseNumeric
1270 after UseLongNames (above) to make sure the final outcome of
1271 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT is NETSNMP_OID_OUTPUT_NUMERIC */
1272 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1))) {
1273 getlabel_flag |= USE_NUMERIC_OIDS;
1274 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS, 1);
1275 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, NETSNMP_OID_OUTPUT_NUMERIC);
1276 }
•1277 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums", 8, 1)))
1278 sprintval_flag = USE_ENUMS;
1279 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseSprintValue", 14, 1)))
1280 sprintval_flag = USE_SPRINT_VALUE;
1281
1282 sv_bless(varlist_ref, gv_stashpv("SNMP::VarList",0));
1283 for(vars = (pdu?pdu->variables:NULL); vars; vars = vars->next_variable) {
1284 int local_getlabel_flag = getlabel_flag;
1285 varbind = newAV();
1286 varbind_ref = newRV_noinc((SV*)varbind);
1287 sv_bless(varbind_ref, gv_stashpv("SNMP::Varbind",0));
1288 av_push(varlist, varbind_ref);
1289 *str_buf = '.';
1290 *(str_buf+1) = '\0';
1291 out_len = 0;
1292 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp,
1293 &str_buf_len,
1294 &out_len, 0, &buf_over,
1295 vars->name,vars->name_length);
1296 str_buf[sizeof(str_buf)-1] = '\0';
1297 if (__is_leaf(tp)) {
1298 type = tp->type;
1299 } else {
1300 local_getlabel_flag |= NON_LEAF_NAME;
1301 type = __translate_asn_type(vars->type);
1302 }
1303 __get_label_iid(str_buf,&label,&iid,local_getlabel_flag);
1304 if (label) {
1305 av_store(varbind, VARBIND_TAG_F,
1306 newSVpv(label, strlen(label)));
1307 } else {
1308 av_store(varbind, VARBIND_TAG_F,
1309 newSVpv("", 0));
1310 }
1311 if (iid) {
1312 av_store(varbind, VARBIND_IID_F,
1313 newSVpv(iid, strlen(iid)));
1314 } else {
1315 av_store(varbind, VARBIND_IID_F,
1316 newSVpv("", 0));
1317 }
1318 __get_type_str(type, tmp_type_str);
1319 tmp_sv = newSVpv(tmp_type_str, strlen(tmp_type_str));
1320 av_store(varbind, VARBIND_TYPE_F, tmp_sv);
1321 len = __snprint_value(str_buf, sizeof(str_buf),
1322 vars, tp, type, sprintval_flag);
1323 tmp_sv = newSVpv((char*)str_buf, len);
1324 av_store(varbind, VARBIND_VAL_F, tmp_sv);
1325 } /* for */
1326
1327 /* Reset the library's behavior for numeric/symbolic OID's. */
1328 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS, old_numeric );
1329 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID, old_printfull);
1330 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, old_format);
1331
1332 } /* case SNMP_MSG_RESPONSE */
1333 break;
1334 default:;
1335 } /* switch pdu->command */
1336 break;
1337
1338 case NETSNMP_CALLBACK_OP_TIMED_OUT:
1339 varlist_ref = &sv_undef;
1340 sv_setpv(*err_str_svp, (char*)snmp_api_errstring(SNMPERR_TIMEOUT));
1341 sv_setiv(*err_num_svp, SNMPERR_TIMEOUT);
1342 break;
1343 default:;
1344 } /* switch op */
1345 if (cb_data != ss->callback_magic)
1346 sv_2mortal(cb);
1347 cb = __push_cb_args2(cb,
1348 (SvTRUE(varlist_ref) ? sv_2mortal(varlist_ref):varlist_ref),
1349 (SvTRUE(traplist_ref) ? sv_2mortal(traplist_ref):traplist_ref));
1350 __call_callback(cb, G_DISCARD);
1351
1352 FREETMPS;
1353 LEAVE;
1354 if (cb_data != ss->callback_magic)
1355 sv_2mortal(sess_ref);
1356 return 1;
1357 }
1358
1359 static SV *
1360 __push_cb_args2(sv,esv,tsv)
1361 SV *sv;
1362 SV *esv;
1363 SV *tsv;
1364 {
1365 dSP;
1366 if (SvTYPE(SvRV(sv)) != SVt_PVCV) sv = SvRV(sv);
1367
1368 PUSHMARK(sp);
1369 if (SvTYPE(sv) == SVt_PVAV) {
1370 AV *av = (AV *) sv;
1371 int n = av_len(av) + 1;
1372 SV **x = av_fetch(av, 0, 0);
1373 if (x) {
1374 int i = 1;
1375 sv = *x;
1376
1377 for (i = 1; i < n; i++) {
1378 x = av_fetch(av, i, 0);
1379 if (x) {
1380 SV *arg = *x;
1381 XPUSHs(sv_mortalcopy(arg));
1382 } else {
1383 XPUSHs(&sv_undef);
1384 }
1385 }
1386 } else {
1387 sv = &sv_undef;
1388 }
1389 }
1390 if (esv) XPUSHs(sv_mortalcopy(esv));
1391 if (tsv) XPUSHs(sv_mortalcopy(tsv));
1392 PUTBACK;
1393 return sv;
1394 }
1395
1396 static int
1397 __call_callback(sv, flags)
1398 SV *sv;
1399 int flags;
1400 {
1401 I32 myframe = TOPMARK;
1402 I32 count;
1403 ENTER;
1404 if (SvTYPE(sv) == SVt_PVCV)
1405 {
1406 count = perl_call_sv(sv, flags);
1407 }
1408 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV)
1409 {
1410 count = perl_call_sv(SvRV(sv), flags);
1411 }
1412 else
1413 {
1414
1415 SV **top = stack_base + myframe + 1;
1416 SV *obj = *top;
1417 if (SvPOK(sv) && SvROK(obj) && SvOBJECT(SvRV(obj)))
1418 {
1419 count = perl_call_method(SvPV(sv, na), flags);
1420 }
1421 else if (SvPOK(obj) && SvROK(sv) && SvOBJECT(SvRV(sv)))
1422 {
1423 /* We have obj method ...
1424 Used to be used instead of LangMethodCall()
1425 */
1426 *top = sv;
1427 count = perl_call_method(SvPV(obj, na), flags);
1428 }
1429 else
1430 {
1431 count = perl_call_sv(sv, flags);
1432 }
1433 }
1434 LEAVE;
1435 return count;
1436 }
1437
1438 /* Bulkwalk support routines */
1439
1440 /* Add a context pointer to the list of valid pointers. Place it in the first
1441 ** NULL slot in the array.
1442 */
1443 static int
1444 _context_add(walk_context *context)
1445 {
1446 int i, j, new_sz;
1447
1448 if ((i = _context_okay(context)) != 0) /* Already exists? Okay. */
1449 return i;
1450
1451 /* Initialize the array if necessary. */
1452 if (_valid_contexts == NULL) {
1453
1454 /* Create the _valid_contexts structure. */
1455 Newz(0, _valid_contexts, 1, struct valid_contexts);
1456 assert(_valid_contexts != NULL);
1457
1458 /* Populate the original valid contexts array. */
1459 Newz(0, _valid_contexts->valid, 4, walk_context *);
1460 assert(_valid_contexts->valid != NULL);
1461
1462 /* Computer number of slots in the array. */
1463 _valid_contexts->sz_valid = sizeof(*_valid_contexts->valid) /
1464 sizeof(walk_context *);
1465
1466 for (i = 0; i < _valid_contexts->sz_valid; i++)
1467 _valid_contexts->valid[i] = NULL;
1468
1469 DBPRT(3, (DBOUT "Created valid_context array 0x%p (%d slots)\n",
1470 _valid_contexts->valid, _valid_contexts->sz_valid));
1471 }
1472
1473 /* Search through the list, looking for NULL's -- unused slots. */
1474 for (i = 0; i < _valid_contexts->sz_valid; i++)
1475 if (_valid_contexts->valid[i] == NULL)
1476 break;
1477
1478 /* Did we walk off the end of the list? Need to grow the list. Double
1479 ** it for now.
1480 */
1481 if (i == _valid_contexts->sz_valid) {
1482 new_sz = _valid_contexts->sz_valid * 2;
1483
1484 Renew(_valid_contexts->valid, new_sz, walk_context *);
1485 assert(_valid_contexts->valid != NULL);
1486
1487 DBPRT(3, (DBOUT "Resized valid_context array 0x%p from %d to %d slots\n",
1488 _valid_contexts->valid, _valid_contexts->sz_valid, new_sz));
1489
1490 _valid_contexts->sz_valid = new_sz;
1491
1492 /* Initialize the new half of the resized array. */
1493 for (j = i; j < new_sz; j++)
1494 _valid_contexts->valid[j] = NULL;
1495 }
1496
1497 /* Store the context pointer in the array and return 0 (success). */
1498 _valid_contexts->valid[i] = context;
1499 DBPRT(3,(DBOUT "Add context 0x%p to valid context list\n", context));
1500 return 0;
1501 }
1502
1503 /* Remove a context pointer from the valid list. Replace the pointer with
1504 ** NULL in the valid pointer list.
1505 */
1506 static int
1507 _context_del(walk_context *context)
1508 {
1509 int i;
1510
1511 if (_valid_contexts == NULL) /* Make sure it was initialized. */
1512 return 1;
1513
1514 for (i = 0; i < _valid_contexts->sz_valid; i++) {
1515 if (_valid_contexts->valid[i] == context) {
1516 DBPRT(3,(DBOUT "Remove context 0x%p from valid context list\n", context));
1517 _valid_contexts->valid[i] = NULL; /* Remove it from the list. */
1518 return 0; /* Return successful status. */
1519 }
1520 }
1521 return 1;
1522 }
1523
1524 /* Check if a specific context pointer is in the valid list. Return true (1)
1525 ** if the context is still in the valid list, or 0 if not (or context is NULL).
1526 */
1527 static int
1528 _context_okay(walk_context *context)
1529 {
1530 int i;
1531
1532 if (_valid_contexts == NULL) /* Make sure it was initialized. */
1533 return 0;
1534
1535 if (context == NULL) /* Asked about a NULL context? Fail. */
1536 return 0;
1537
1538 for (i = 0; i < _valid_contexts->sz_valid; i++)
1539 if (_valid_contexts->valid[i] == context)
1540 return 1; /* Found it! */
1541
1542 return 0; /* No match -- return failure. */
1543 }
1544
1545 /* Check if the walk is completed, based upon the context. Also set the
1546 ** ignore flag on any completed variables -- this prevents them from being
1547 ** being sent in later packets.
1548 */
1549 static int
1550 _bulkwalk_done(walk_context *context)
1551 {
1552 int is_done = 1;
1553 int i;
1554 bulktbl *bt_entry; /* bulktbl requested OID entry */
1555
1556 /* Don't consider walk done until at least one packet has been exchanged. */
1557 if (context->pkts_exch == 0)
1558 return 0;
1559
1560 /* Fix up any requests that have completed. If the complete flag is set,
1561 ** or it is a non-repeater OID, set the ignore flag so that it will not
1562 ** be considered further. Assume we are done with the walk, and note
1563 ** otherwise if we aren't. Return 1 if all requests are complete, or 0
1564 ** if there's more to do.
1565 */
1566 for (i = 0; i < context->nreq_oids; i ++) {
1567 bt_entry = &context->req_oids[i];
1568
1569 if (bt_entry->complete || bt_entry->norepeat) {
1570
1571 /* This request is complete. Remove it from list of
1572 ** walks still in progress.
1573 */
1574 DBPRT(1, (DBOUT "Ignoring %s request oid %s\n",
1575 bt_entry->norepeat ? "nonrepeater" : "completed",
1576 __snprint_oid(bt_entry->req_oid, bt_entry->req_len)));
1577
1578 /* Ignore this OID in any further packets. */
1579 bt_entry->ignore = 1;
1580 }
1581
1582 /* If any OID is not being ignored, the walk is not done. Must loop
1583 ** through all requests to do the fixup -- no early return possible.
1584 */
1585 if (!bt_entry->ignore)
1586 is_done = 0;
1587 }
1588
1589 return is_done; /* Did the walk complete? */
1590 }
1591
1592 /* Callback registered with SNMP. Return 1 from this callback to cause the
1593 ** current request to be deleted from the retransmit queue.
1594 */
1595 static int
1596 _bulkwalk_async_cb(int op,
1597 SnmpSession *ss,
1598 int reqid,
1599 netsnmp_pdu *pdu,
1600 void *context_ptr)
1601 {
1602 walk_context *context;
1603 int done = 0;
1604 SV **err_str_svp;
1605 SV **err_num_svp;
1606
1607 /* Handle callback request for asynchronous bulkwalk. If the bulkwalk has
1608 ** not completed, and has not timed out, send the next request packet in
1609 ** the walk.
1610 **
1611 ** Return 0 to indicate success (caller ignores return value).
1612 */
1613
1614 DBPRT(2, (DBOUT "bulkwalk_async_cb(op %d, reqid 0x%08X, context 0x%p)\n",
1615 op, reqid, context_ptr));
1616
1617 context = (walk_context *)context_ptr;
1618
1619 /* Make certain this is a valid context pointer. This pdu may
1620 ** have been retransmitted after the bulkwalk was completed
1621 ** (and the context was destroyed). If so, just return.
1622 */
1623 if (!_context_okay(context)) {
1624 DBPRT(2,(DBOUT "Ignoring PDU for dead context 0x%p...\n", context));
1625 return 1;
1626 }
1627
1628 /* Is this a retransmission of a request we've already seen or some
1629 ** unexpected request id? If so, just ignore it.
1630 */
1631 if (reqid != context->exp_reqid) {
1632 DBPRT(2,
1633 (DBOUT "Got reqid 0x%08X, expected reqid 0x%08X. Ignoring...\n", reqid,
1634 context->exp_reqid));
1635 return 1;
1636 }
1637 /* Ignore any future packets for this reqid. */
1638 context->exp_reqid = -1;
1639
1640 err_str_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorStr", 8, 1);
1641 err_num_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorNum", 8, 1);
1642
1643 switch (op) {
1644 case NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE:
1645 {
1646 DBPRT(1,(DBOUT "Received message for reqid 0x%08X ...\n", reqid));
1647
1648 switch (pdu->command)
1649 {
1650 case SNMP_MSG_RESPONSE:
1651 {
1652 DBPRT(2, (DBOUT "Calling bulkwalk_recv_pdu(context 0x%p, pdu 0x%p)\n",
1653 context_ptr, pdu));
1654
1655 /* Handle the response PDU. If an error occurs or there were
1656 ** no variables in the response, consider the walk done. If
1657 ** the response was okay, check if we have any more to do after
1658 ** this response.
1659 */
1660 if (_bulkwalk_recv_pdu(context, pdu) <= 0)
1661 done = 1;
1662 else
1663 done = _bulkwalk_done(context); /* Also set req ignore flags */
1664 break;
1665 }
1666 default:
1667 {
1668 DBPRT(1,(DBOUT "unexpected pdu->command %d\n", pdu->command));
1669 done = 1; /* "This can't happen!", so bail out when it does. */
1670 break;
1671 }
1672 }
1673
1674 break;
1675 }
1676
1677 case NETSNMP_CALLBACK_OP_TIMED_OUT:
1678 {
1679 DBPRT(1,(DBOUT "\n*** Timeout for reqid 0x%08X\n\n", reqid));
1680
1681 sv_setpv(*err_str_svp, (char*)snmp_api_errstring(SNMPERR_TIMEOUT));
1682 sv_setiv(*err_num_svp, SNMPERR_TIMEOUT);
1683
1684 /* Timeout means something bad has happened. Return a not-okay
1685 ** result to the async callback.
1686 */
1687 _bulkwalk_finish(context, 0 /* NOT OKAY */);
1688 return 1;
1689 }
1690
1691 default:
1692 {
1693 DBPRT(1,(DBOUT "unexpected callback op %d\n", op));
1694 sv_setpv(*err_str_svp, (char*)snmp_api_errstring(SNMPERR_GENERR));
1695 sv_setiv(*err_num_svp, SNMPERR_GENERR);
1696 _bulkwalk_finish(context, 0 /* NOT OKAY */);
1697 return 1;
1698 }
1699 }
1700
1701 /* We have either timed out, or received and parsed in a response. Now,
1702 ** if we have more variables to test, call bulkwalk_send_pdu() to enqueue
1703 ** another async packet, and return.
1704 **
1705 ** If, however, the bulkwalk has completed (or an error has occurred that
1706 ** cuts the walk short), call bulkwalk_finish() to push the results onto
1707 ** the Perl call stack. Then explicitly call the Perl callback that was
1708 ** passed in by the user oh-so-long-ago.
1709 */
1710 if (!done) {
1711 DBPRT(1,(DBOUT "bulkwalk not complete -- send next pdu from callback\n"));
1712
1713 if (_bulkwalk_send_pdu(context) != NULL)
1714 return 1;
1715
1716 DBPRT(1,(DBOUT "send_pdu() failed!\n"));
1717 /* Fall through and return what we have so far. */
1718 }
1719
1720 /* Call the perl callback with the return values and we're done. */
1721 _bulkwalk_finish(context, 1 /* OKAY */);
1722
1723 return 1;
1724 }
1725
1726 static netsnmp_pdu *
1727 _bulkwalk_send_pdu(walk_context *context)
1728 {
1729 netsnmp_pdu *pdu = NULL;
1730 netsnmp_pdu *response = NULL;
1731 struct bulktbl *bt_entry;
1732 int nvars = 0;
1733 int reqid;
1734 int status;
1735 int i;
1736
1737 /* Send a pdu requesting any remaining variables in the context.
1738 **
1739 ** In synchronous mode, returns a pointer to the response packet.
1740 **
1741 ** In asynchronous mode, it returns the request ID, cast to a struct snmp *,
1742 ** not a valid SNMP response packet. The async code should not be trying
1743 ** to get variables out of this "response".
1744 **
1745 ** In either case, return a NULL pointer on error or failure.
1746 */
1747
1748 SV **sess_ptr_sv = hv_fetch((HV*)SvRV(context->sess_ref), "SessPtr", 7, 1);
1749 void *ss = (void *)SvIV((SV*)SvRV(*sess_ptr_sv));
1750 SV **err_str_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorStr", 8, 1);
1751 SV **err_num_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorNum", 8, 1);
1752 SV **err_ind_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorInd", 8, 1);
1753
1754 /* Create a new PDU and send the remaining set of requests to the agent. */
1755 pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
1756 if (pdu == NULL) {
1757 sv_setpv(*err_str_svp, "snmp_pdu_create(GETBULK) failed: ");
1758 sv_catpv(*err_str_svp, strerror(errno));
1759 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
1760 goto err;
1761 }
1762
1763 /* Request non-repeater variables only in the first packet exchange. */
1764 pdu->errstat = (context->pkts_exch == 0) ? context->non_reps : 0;
1765 pdu->errindex = context->max_reps;
1766
1767 for (i = 0; i < context->nreq_oids; i++) {
1768 bt_entry = &context->req_oids[i];
1769 if (bt_entry->ignore)
1770 continue;
1771
1772 assert(bt_entry->complete == 0);
1773
1774 if (!snmp_add_null_var(pdu, bt_entry->last_oid, bt_entry->last_len)) {
1775 sv_setpv(*err_str_svp, "snmp_add_null_var() failed");
1776 sv_setiv(*err_num_svp, SNMPERR_GENERR);
1777 sv_setiv(*err_ind_svp, i);
1778 goto err;
1779 }
1780
1781 nvars ++;
1782
1783 DBPRT(1, (DBOUT " Add %srepeater %s\n", bt_entry->norepeat ? "non" : "",
1784 __snprint_oid(bt_entry->last_oid, bt_entry->last_len)));
1785 }
1786
1787 /* Make sure variables are actually being requested in the packet. */
1788 assert (nvars != 0);
1789
1790 context->pkts_exch ++;
1791
1792 DBPRT(1, (DBOUT "Sending %ssynchronous request %d...\n",
1793 SvTRUE(context->perl_cb) ? "a" : "", context->pkts_exch));
1794
1795 /* We handle the asynchronous and synchronous requests differently here.
1796 ** For async, we simply enqueue the packet with a callback to handle the
1797 ** returned response, then return. Note that this we call the bulkwalk
1798 ** callback, and hand it the walk_context, not the Perl callback. The
1799 ** snmp_async_send() function returns the reqid on success, 0 on failure.
1800 */
1801 if (SvTRUE(context->perl_cb)) {
1802 if(api_mode == SNMP_API_SINGLE)
1803 {
1804 reqid = snmp_sess_async_send(ss, pdu, _bulkwalk_async_cb, (void *)context);
1805 } else {
1806 reqid = snmp_async_send(ss, pdu, _bulkwalk_async_cb, (void *)context);
1807 }
1808
1809 DBPRT(2,(DBOUT "bulkwalk_send_pdu(): snmp_async_send => 0x%08X\n", reqid));
1810
1811 if (reqid == 0) {
1812 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
1813 goto err;
1814 }
1815
1816 /* Make a note of the request we expect to be answered. */
1817 context->exp_reqid = reqid;
1818
1819 /* Callbacks take care of the rest. Let the caller know how many vars
1820 ** we sent in this request. Note that this is not a valid SNMP PDU,
1821 ** but that's because a response has not yet been received.
1822 */
1823 return (netsnmp_pdu *)(intptr_t)reqid;
1824 }
1825
1826 /* This code is for synchronous mode support.
1827 **
1828 ** Send the PDU and block awaiting the response. Return the response
1829 ** packet back to the caller. Note that snmp_sess_read() frees the pdu.
1830 */
1831 status = __send_sync_pdu(ss, pdu, &response, NO_RETRY_NOSUCH,
1832 *err_str_svp, *err_num_svp, *err_ind_svp);
1833
1834 pdu = NULL;
1835
1836 /* Check for a failed request. __send_sync_pdu() will set the appropriate
1837 ** values in the error string and number SV's.
1838 */
1839 if (status != STAT_SUCCESS) {
1840 DBPRT(1,(DBOUT "__send_sync_pdu() -> %d\n",(int)status));
1841 goto err;
1842 }
1843
1844 DBPRT(1, (DBOUT "%d packets exchanged, response 0x%p\n", context->pkts_exch,
1845 response));
1846 return response;
1847
1848
1849 err:
1850 if (pdu)
1851 snmp_free_pdu(pdu);
1852 return NULL;
1853 }
1854
1855 /* Handle an incoming GETBULK response PDU. This function just pulls the
1856 ** variables off of the PDU and builds up the arrays of returned values
1857 ** that are stored in the context.
1858 **
1859 ** Returns the number of variables found in this packet, or -1 on error.
1860 ** Note that the caller is expected to free the pdu.
1861 */
1862 static int
1863 _bulkwalk_recv_pdu(walk_context *context, netsnmp_pdu *pdu)
1864 {
1865 netsnmp_variable_list *vars;
1866 struct tree *tp;
1867 char type_str[MAX_TYPE_NAME_LEN];
1868 char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;
1869 size_t str_buf_len = sizeof(str_buf);
1870 size_t out_len = 0;
1871 int buf_over = 0;
1872 char *label;
1873 char *iid;
1874 bulktbl *expect = NULL;
1875 int old_numeric;
1876 int old_printfull;
1877 int old_format;
1878 int getlabel_flag;
1879 int type;
1880 int pix;
1881 int len;
1882 int i;
1883 AV *varbind;
1884 SV *rv;
1885 SV **sess_ptr_sv = hv_fetch((HV*)SvRV(context->sess_ref), "SessPtr", 7, 1);
1886 SV **err_str_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorStr", 8, 1);
1887 SV **err_num_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorNum", 8, 1);
1888 SV **err_ind_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorInd", 8, 1);
1889 int check = SvIV(*hv_fetch((HV*)SvRV(context->sess_ref), "NonIncreasing",13,1));
1890
1891 DBPRT(3, (DBOUT "bulkwalk: sess_ref = 0x%p, sess_ptr_sv = 0x%p\n",
1892 context->sess_ref, sess_ptr_sv));
1893
1894 /* Set up for numeric OID's, if necessary. Save the old values
1895 ** so that they can be restored when we finish -- these are
1896 ** library-wide globals, and have to be set/restored for each
1897 ** session.
1898 */
1899 old_numeric = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS);
1900 old_printfull = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID);
1901 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1902 if (context->getlabel_f & USE_NUMERIC_OIDS) {
1903 DBPRT(2,(DBOUT "Using numeric oid's\n"));
1904 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS, 1);
1905 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID, 1);
1906 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, NETSNMP_OID_OUTPUT_NUMERIC);
1907 }
1908
1909 /* Parse through the list of variables returned, adding each return to
1910 ** the appropriate array (as a VarBind). Also keep track of which
1911 ** repeated OID we're expecting to see, and check if that tree walk has
1912 ** been completed (i.e. we've walked past the root of our request). If
1913 ** so, mark the request complete so that we don't send it again in any
1914 ** subsequent request packets.
1915 */
1916 if (context->pkts_exch == 1)
1917 context->reqbase = context->req_oids; /* Request with non-repeaters */
1918 else
1919 context->reqbase = context->repbase; /* Request only repeater vars */
1920
1921 /* Note the first variable we expect to see. Should be reqbase. */
1922 expect = context->reqbase;
1923
1924 for (vars = pdu->variables, pix = 0;
1925 vars != NULL;
1926 vars = vars->next_variable, pix ++)
1927 {
1928
1929 /* If no outstanding requests remain, we're done. This works, but it
1930 ** causes the reported total variable count to be wrong (since the
1931 ** remaining vars on the last packet are not counted). In practice
1932 ** this is probably worth the win, but for debugging it's not.
1933 */
1934 if (context->req_remain == 0) {
1935 DBPRT(2,(DBOUT "No outstanding requests remain. Terminating processing.\n"));
1936 while (vars) {
1937 pix ++;
1938 vars = vars->next_variable;
1939 }
1940 break;
1941 }
1942
1943 /* Determine which OID we expect to see next. We assert that the OID's
1944 ** must be returned in the expected order. The first nreq_oids returns
1945 ** should match the req_oids array, after that, we must cycle through
1946 ** the repeaters in order. Non-repeaters are not included in later
1947 ** packets, so cannot have the "ignore" flag set.
1948 */
1949
1950 if (context->oid_saved < context->non_reps) {
1951 assert(context->pkts_exch == 1);
1952
1953 expect = context->reqbase ++;
1954 assert(expect->norepeat);
1955
1956 } else {
1957 /* Must be a repeater. Look for the first one that is not being
1958 ** ignored. Make sure we don't loop around to where we started.
1959 ** If we get here but everything is being ignored, there's a problem.
1960 **
1961 ** Note that we *do* accept completed but not ignored OID's -- these
1962 ** are OID's for trees that have been completed sometime in this
1963 ** response, but must be looked at to maintain ordering.
1964 */
1965 /* In previous version we started from 1st repeater any time when
1966 ** pix == 0. But if 1st repeater is ignored we can get wrong results,
1967 ** because it was not included in 2nd and later request. So we set
1968 ** expect to repbase-1 and then search for 1st non-ignored repeater.
1969 ** repbase-1 is nessessary because we're starting search in loop below
1970 ** from ++expect and it will be exactly repbase on 1st search pass.
1971 */
1972 if (pix == 0)
1973 expect = context->repbase - 1;
1974
1975 /* Find the repeater OID we expect to see. Ignore any
1976 ** OID's marked 'ignore' -- these have been completed
1977 ** and were not requested in this iteration.
1978 */
1979 for (i = 0; i < context->repeaters; i++) {
1980
1981 /* Loop around to first repeater if we hit the end. */
1982 if (++ expect == &context->req_oids[context->nreq_oids])
1983 expect = context->reqbase = context->repbase;
1984
1985 /* Stop if this OID is not being ignored. */
1986 if (!expect->ignore)
1987 break;
1988 }
1989 }
1990
1991 DBPRT(2, (DBOUT "Var %03d request %s\n", pix,
1992 __snprint_oid(expect->req_oid, expect->req_len)));
1993
1994 /* Did we receive an error condition for this variable?
1995 ** If it's a repeated variable, mark it as complete and
1996 ** fall through to the block below.
1997 */
1998 if ((vars->type == SNMP_ENDOFMIBVIEW) ||
1999 (vars->type == SNMP_NOSUCHOBJECT) ||
2000 (vars->type == SNMP_NOSUCHINSTANCE))
2001 {
2002 DBPRT(2,(DBOUT "error type %d\n", (int)vars->type));
2003
2004 /* ENDOFMIBVIEW should be okay for a repeater - just walked off the
2005 ** end of the tree. Mark the request as complete, and go on to the
2006 ** next one.
2007 */
2008 if ((context->oid_saved >= context->non_reps) &&
2009 (vars->type == SNMP_ENDOFMIBVIEW))
2010 {
2011 expect->complete = 1;
2012 DBPRT(2, (DBOUT "Ran out of tree for oid %s\n",
2013 __snprint_oid(vars->name,vars->name_length)));
2014
2015 context->req_remain --;
2016
2017 /* Go on to the next variable. */
2018 continue;
2019
2020 }
2021 sv_setpv(*err_str_svp,
2022 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
2023 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
2024 sv_setiv(*err_ind_svp, pix);
2025 goto err;
2026 }
2027
2028 /* If this is not the first packet, skip any duplicated OID values, if
2029 ** present. These should be the seed values copied from the last OID's
2030 ** of the previous packet. In practice we don't see this, but it is
2031 ** easy enough to do, and will avoid confusion for the caller from mis-
2032 ** behaving agents (badly misbehaving... ;^).
2033 */
2034 if (context->pkts_exch > 1) {
2035 if (__oid_cmp(vars->name, vars->name_length,
2036 expect->last_oid, expect->last_len) <= 0)
2037 {
2038 if (check)
2039 {
2040 DBPRT(2, (DBOUT "Error: OID not increasing: %s\n",
2041 __snprint_oid(vars->name,vars->name_length)));
2042 sv_setpv(*err_str_svp, (char*)snmp_api_errstring(SNMPERR_OID_NONINCREASING));
2043 sv_setiv(*err_num_svp, SNMPERR_OID_NONINCREASING);
2044 sv_setiv(*err_ind_svp, pix);
2045 goto err;
2046 }
2047
2048 DBPRT(2, (DBOUT "Ignoring repeat oid: %s\n",
2049 __snprint_oid(vars->name,vars->name_length)));
2050
2051 continue;
2052 }
2053 }
2054
2055 context->oid_total ++; /* Count each variable received. */
2056
2057 /* If this is a non-repeater, handle it. Otherwise, if it is a
2058 ** repeater, has the walk wandered off of the requested tree? If so,
2059 ** this request is complete, so mark it as such. Ignore any other
2060 ** variables in a completed request. In order to maintain the correct
2061 ** ordering of which variables we expect to see in this packet, we must
2062 ** not set the ignore flags immediately. It is done in bulkwalk_done().
2063 */
2064 if (context->oid_saved < context->non_reps) {
2065 DBPRT(2, (DBOUT " expected var %s (nonrepeater %d/%d)\n",
2066 __snprint_oid(expect->req_oid, expect->req_len),
2067 pix, context->non_reps));
2068 DBPRT(2, (DBOUT " received var %s\n",
2069 __snprint_oid(vars->name, vars->name_length)));
2070
2071 /* This non-repeater has now been seen, so mark the sub-tree as
2072 ** completed. Note that this may not be the same oid as requested,
2073 ** since non-repeaters act like GETNEXT requests, not GET's. <sigh>
2074 */
2075 context->req_oids[pix].complete = 1;
2076 context->req_remain --;
2077
2078 } else { /* Must be a repeater variable. */
2079
2080 DBPRT(2, (DBOUT " received oid %s\n",
2081 __snprint_oid(vars->name, vars->name_length)));
2082
2083 /* Are we already done with this tree? If so, just ignore this
2084 ** variable and move on to the next expected variable.
2085 */
2086 if (expect->complete) {
2087 DBPRT(2,(DBOUT " this branch is complete - ignoring.\n"));
2088 continue;
2089 }
2090
2091 /* If the base oid of this variable doesn't match the expected oid,
2092 ** assume that we've walked past the end of the subtree. Set this
2093 ** subtree to be completed, and go on to the next variable.
2094 */
2095 if ((vars->name_length < expect->req_len) ||
2096 (memcmp(vars->name, expect->req_oid, expect->req_len*sizeof(oid))))
2097 {
2098 DBPRT(2,(DBOUT " walked off branch - marking subtree as complete.\n"));
2099 expect->complete = 1;
2100 context->req_remain --;
2101 continue;
2102 }
2103
2104 /* Still interested in the tree -- we need to keep track of the
2105 ** last-seen value in case we need to send an additional request
2106 ** packet.
2107 */
2108 (void)memcpy(expect->last_oid, vars->name,
2109 vars->name_length * sizeof(oid));
2110 expect->last_len = vars->name_length;
2111
2112 }
2113
2114 /* Create a new Varbind and populate it with the parsed information
2115 ** returned by the agent. This Varbind is then pushed onto the arrays
2116 ** maintained for each request OID in the context. These varbinds are
2117 ** collected into a return array by bulkwalk_finish().
2118 */
2119 varbind = (AV*) newAV();
2120 if (varbind == NULL) {
2121 sv_setpv(*err_str_svp, "newAV() failed: ");
2122 sv_catpv(*err_str_svp, (char*)strerror(errno));
2123 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
2124 goto err;
2125 }
2126
2127 *str_buf = '.';
2128 *(str_buf+1) = '\0';
2129 out_len = 0;
2130 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp, &str_buf_len,
2131 &out_len, 0, &buf_over,
2132 vars->name,vars->name_length);
2133 str_buf[sizeof(str_buf)-1] = '\0';
2134
2135 getlabel_flag = context->getlabel_f;
2136
2137 if (__is_leaf(tp)) {
2138 type = tp->type;
2139 } else {
2140 getlabel_flag |= NON_LEAF_NAME;
2141 type = __translate_asn_type(vars->type);
2142 }
2143 if (__get_label_iid(str_buf, &label, &iid, getlabel_flag) == FAILURE) {
2144 label = str_buf;
2145 iid = label + strlen(label);
2146 }
2147
2148 DBPRT(2,(DBOUT " save var %s.%s = ", label, iid));
2149
2150 av_store(varbind, VARBIND_TAG_F, newSVpv(label, strlen(label)));
2151 av_store(varbind, VARBIND_IID_F, newSVpv(iid, strlen(iid)));
2152
2153 __get_type_str(type, type_str);
2154 av_store(varbind, VARBIND_TYPE_F, newSVpv(type_str, strlen(type_str)));
2155
2156 len = __snprint_value(str_buf, sizeof(str_buf) - 1,
2157 vars, tp, type, context->sprintval_f);
2158 av_store(varbind, VARBIND_VAL_F, newSVpv(str_buf, len));
2159
2160 str_buf[len] = '\0';
2161 DBPRT(3,(DBOUT "'%s' (%s)\n", str_buf, type_str));
2162
2163 #if 0
2164 /* huh? */
2165 /* If necessary, store a timestamp as the semi-documented 5th element. */
2166 if (sv_timestamp)
2167 av_store(varbind, VARBIND_TIME_F, SvREFCNT_inc(sv_timestamp));
2168 #endif
2169
2170 /* Push ref to the varbind onto the list of vars for OID. */
2171 rv = newRV_noinc((SV *)varbind);
2172 sv_bless(rv, gv_stashpv("SNMP::Varbind", 0));
2173 av_push(expect->vars, rv);
2174
2175 context->oid_saved ++; /* Count this as a saved variable. */
2176
2177 } /* next variable in response packet */
2178
2179 DBPRT(1, (DBOUT "-- pkt %d saw %d vars, total %d (%d saved)\n", context->pkts_exch,
2180 pix, context->oid_total, context->oid_saved));
2181
2182 /* We assert that all non-repeaters must be returned in
2183 ** the initial response (they are not repeated in additional
2184 ** packets, so would be dropped). If nonrepeaters still
2185 ** exist, consider it a fatal error.
2186 */
2187 if ((context->pkts_exch == 1) && (context->oid_saved < context->non_reps)) {
2188 /* Re-use space from the value string for error message. */
2189 sprintf(str_buf, "%d non-repeaters went unanswered", context->non_reps);
2190 sv_setpv(*err_str_svp, str_buf);
2191 sv_setiv(*err_num_svp, SNMPERR_GENERR);
2192 sv_setiv(*err_num_svp, context->oid_saved);
2193 goto err;
2194 }
2195
2196 /* Reset the library's behavior for numeric/symbolic OID's. */
2197 if (context->getlabel_f & USE_NUMERIC_OIDS) {
2198 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS, old_numeric);
2199 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID, old_printfull);
2200 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, old_format);
2201 }
2202
2203 return pix;
2204
2205 err:
2206 return -1;
2207
2208 }
2209
2210 /* Once the bulkwalk has completed, extend the stack and push references to
2211 ** each of the arrays of SNMP::Varbind's onto the stack. Return the number
2212 ** of arrays pushed on the stack. The caller should return to Perl, or call
2213 ** the Perl callback function.
2214 **
2215 ** Note that this function free()'s the walk_context and request bulktbl's.
2216 */
2217 static int
2218 _bulkwalk_finish(walk_context *context, int okay)
2219 {
2220 dSP;
2221 int npushed = 0;
2222 int i;
2223 int async = 0;
2224 bulktbl *bt_entry;
2225 AV *ary = NULL;
2226 SV *rv;
2227 SV *perl_cb;
2228
2229 SV **err_str_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorStr", 8, 1);
2230 SV **err_num_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorNum", 8, 1);
2231
2232 async = SvTRUE(context->perl_cb);
2233
2234 /* XXX
2235 _bulkwalk_finish() was originally intended to be called from XS code, and
2236 would extend the caller's stack with result. Later it was changed into
2237 an asynchronous version that calls perl code instead. These two branches
2238 differ significantly in how they treat perl stack. Due to these differences,
2239 often implicit (f.ex. dMARK calls POPMARK ), it would be a good idea
2240 to write two different procedures, _bulkwalk_finish_sync and _bulkwalk_finish_async
2241 for cleaner separation. */
2242
2243 if (async) PUSHMARK(sp);
2244
2245 {
2246
2247 #ifdef dITEMS
2248 dMARK;
2249 dITEMS;
2250 #else
2251 /* unfortunately this may pop a mark, which is not what we want */
2252 /* older perl versions don't declare dITEMS though and the
2253 following declars it but also uses dAXMARK instead of dMARK
2254 which is the bad popping version */
2255 dMARK;
2256
2257 /* err... This is essentially what the newer dITEMS does */
2258 I32 items = sp - mark;
2259 #endif
2260
2261
2262 /* Successfully completed the bulkwalk. For synchronous calls, push each
2263 ** of the request value arrays onto the stack, and return the number of
2264 ** items pushed onto the stack. For async, create a new array and push
2265 ** the references onto it. The array is then passed to the Perl callback.
2266 */
2267 if(!async)
2268 SP -= items;
2269
2270 DBPRT(1, (DBOUT "Bulwalk %s (saved %d/%d), ", okay ? "completed" : "had error",
2271 context->oid_saved, context->oid_total));
2272
2273 if (okay) {
2274 DBPRT(1, (DBOUT "%s %d varbind refs %s\n",
2275 async ? "pass ref to array of" : "return",
2276 context->nreq_oids,
2277 async ? "to callback" : "on stack to caller"));
2278
2279 /* Create the array to hold the responses for the asynchronous callback,
2280 ** or pre-extend the stack enough to hold responses for synch return.
2281 */
2282 if (async) {
2283 ary = (AV *)newAV();
2284 if (ary == NULL) {
2285 sv_setpv(*err_str_svp, "newAV(): ");
2286 sv_catpv(*err_str_svp, (char *)strerror(errno));
2287 sv_setiv(*err_num_svp, errno);
2288 }
2289
2290 /* NULL ary pointer is okay -- we'll handle it below... */
2291
2292 } else {
2293 EXTEND(sp, context->nreq_oids);
2294
2295 }
2296
2297 /* Push a reference to each array of varbinds onto the stack, in
2298 ** the order requested. Note that these arrays may be empty.
2299 */
2300 for (i = 0; i < context->nreq_oids; i++) {
2301 bt_entry = &context->req_oids[i];
2302
2303 DBPRT(2, (DBOUT " %sreq #%d (%s) => %d var%s\n",
2304 bt_entry->complete ? "" : "incomplete ", i,
2305 __snprint_oid(bt_entry->req_oid, bt_entry->req_len),
2306 (int)av_len(bt_entry->vars) + 1,
2307 (int)av_len(bt_entry->vars) > 0 ? "s" : ""));
2308
2309 if (async && ary == NULL) {
2310 DBPRT(2,(DBOUT " [dropped due to newAV() failure]\n"));
2311 continue;
2312 }
2313
2314 /* Get a reference to the varlist, and push it onto array or stack */
2315 rv = newRV_noinc((SV *)bt_entry->vars);
2316 sv_bless(rv, gv_stashpv("SNMP::VarList",0));
2317
2318 if (async)
2319 av_push(ary, rv);
2320 else
2321 PUSHs(sv_2mortal((SV *)rv));
2322
2323 npushed ++;
2324 }
2325
2326 } else { /* Not okay -- push a single undef on the stack if not async */
2327
2328 if (!async) {
2329 XPUSHs(&sv_undef);
2330 npushed = 1;
2331 } else {
2332 for (i = 0; i < context->nreq_oids; i++) {
2333 sv_2mortal((SV *) (context->req_oids[i].vars));
2334 }
2335 }
2336 }
2337
2338 /* XXX Future enhancement -- make statistics (pkts exchanged, vars
2339 ** saved vs. received, total time, etc) available to caller so they
2340 ** can adjust their request parameters and/or re-order requests.
2341 */
2342 if(!async)
2343 SP -= items;
2344
2345 PUTBACK;
2346
2347 if (async) {
2348 /* Asynchronous callback. Push the caller's arglist onto the stack,
2349 ** and follow it with the contents of the array (or undef if newAV()
2350 ** failed or the session had an error). Then mortalize the Perl
2351 ** callback pointer, and call the callback.
2352 */
2353 if (!okay || ary == NULL)
2354 rv = &sv_undef;
2355 else
2356 rv = newRV_noinc((SV *)ary);
2357
2358 sv_2mortal(perl_cb = context->perl_cb);
2359 perl_cb = __push_cb_args(perl_cb, (SvTRUE(rv) ? sv_2mortal(rv) : rv));
2360
2361 __call_callback(perl_cb, G_DISCARD);
2362 }
2363 sv_2mortal(context->sess_ref);
2364
2365 /* Free the allocated space for the request states and return number of
2366 ** variables found. Remove the context from the valid context list.
2367 */
2368 _context_del(context);
2369 DBPRT(2,(DBOUT "Free() context->req_oids\n"));
2370 Safefree(context->req_oids);
2371 DBPRT(2,(DBOUT "Free() context 0x%p\n", context));
2372 Safefree(context);
2373 return npushed;
2374 }}
2375
2376 /* End of bulkwalk support routines */
2377
2378 static char *
2379 __av_elem_pv(AV *av, I32 key, char *dflt)
2380 {
2381 SV **elem = av_fetch(av, key, 0);
2382
2383 return (elem && SvOK(*elem)) ? SvPV(*elem, na) : dflt;
2384 }
2385
2386 static int
2387 not_here(const char *s)
2388 {
2389 warn("%s not implemented on this architecture", s);
2390 return -1;
2391 }
2392
2393 #define TEST_CONSTANT(value, name, C) \
2394 if (strEQ(name, #C)) { \
2395 *value = C; \
2396 return 0; \
2397 }
2398 #define TEST_CONSTANT2(value, name, C, V) \
2399 if (strEQ(name, #C)) { \
2400 *value = V; \
2401 return 0; \
2402 }
2403
2404 static int constant(double *value, const char * const name, const int arg)
2405 {
2406 switch (*name) {
2407 case 'N':
2408 TEST_CONSTANT(value, name, NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE);
2409 TEST_CONSTANT(value, name, NETSNMP_CALLBACK_OP_TIMED_OUT);
2410 break;
2411 case 'S':
2412 TEST_CONSTANT(value, name, SNMPERR_BAD_ADDRESS);
2413 TEST_CONSTANT(value, name, SNMPERR_BAD_LOCPORT);
2414 TEST_CONSTANT(value, name, SNMPERR_BAD_SESSION);
2415 TEST_CONSTANT(value, name, SNMPERR_GENERR);
2416 TEST_CONSTANT(value, name, SNMPERR_TOO_LONG);
2417 TEST_CONSTANT(value, name, SNMP_DEFAULT_ADDRESS);
2418 TEST_CONSTANT(value, name, SNMP_DEFAULT_COMMUNITY_LEN);
2419 TEST_CONSTANT(value, name, SNMP_DEFAULT_ENTERPRISE_LENGTH);
2420 TEST_CONSTANT(value, name, SNMP_DEFAULT_ERRINDEX);
2421 TEST_CONSTANT(value, name, SNMP_DEFAULT_ERRSTAT);
2422 TEST_CONSTANT2(value, name, SNMP_DEFAULT_PEERNAME, 0);
2423 TEST_CONSTANT(value, name, SNMP_DEFAULT_REMPORT);
2424 TEST_CONSTANT(value, name, SNMP_DEFAULT_REQID);
2425 TEST_CONSTANT(value, name, SNMP_DEFAULT_RETRIES);
2426 TEST_CONSTANT(value, name, SNMP_DEFAULT_TIME);
2427 TEST_CONSTANT(value, name, SNMP_DEFAULT_TIMEOUT);
2428 TEST_CONSTANT2(value, name, SNMP_DEFAULT_VERSION,
2429 NETSNMP_DEFAULT_SNMP_VERSION);
2430 TEST_CONSTANT(value, name, SNMP_API_SINGLE);
2431 TEST_CONSTANT(value, name, SNMP_API_TRADITIONAL);
2432 break;
2433 case 'X':
2434 goto not_there;
2435 break;
2436 default:
2437 break;
2438 }
2439 return EINVAL;
2440
2441 not_there:
2442 not_here(name);
2443 return ENOENT;
2444 }
2445
2446 /*
2447 Since s_snmp_errno can't be trusted with Single Session, this calls either
2448 snmp_error or snmp_sess_error to populate ErrorStr,ErrorNum, and ErrorInd
2449 in SNMP::Session objects
2450 */
2451 void snmp_return_err(void *ss, SV *err_str, SV *err_num, SV *err_ind)
2452 {
2453 int err;
2454 int liberr;
2455 char *errstr;
2456 if(ss == NULL)
2457 return;
2458 if(api_mode == SNMP_API_SINGLE)
2459 {
2460 snmp_sess_error(ss, &err, &liberr, &errstr);
2461 } else {
2462 snmp_error(ss, &err, &liberr, &errstr);
2463 }
2464 sv_catpv(err_str, errstr);
2465 sv_setiv(err_num, liberr);
2466 sv_setiv(err_ind, err);
2467 netsnmp_free(errstr);
2468 }
2469
2470
2471 /*
2472 int snmp_api_mode( int mode )
2473 Returns or sets static int api_mode for reference by functions to determine
2474 whether to use Traditional (non-threadsafe) or Single-Session (threadsafe)
2475 SNMP API calls.
2476
2477 Call with (int)NULL to return the current mode, or with SNMP_API_TRADITIONAL
2478 or SNMP_API_SINGLE to set the current mode. (defined above)
2479
2480 pm side call defaults to (int)NULL
2481 */
2482 int snmp_api_mode( int mode )
2483 {
2484 if (mode == 0)
2485 return api_mode;
2486 api_mode = mode;
2487 return api_mode;
2488 }
2489
2490 MODULE = SNMP PACKAGE = SNMP PREFIX = snmp
2491
2492 void
2493 constant(name,arg)
2494 char * name
2495 int arg
2496 INIT:
2497 int status;
2498 double value;
2499 PPCODE:
2500 value = 0;
2501 status = constant(&value, name, arg);
2502 XPUSHs(sv_2mortal(newSVuv(status)));
2503 XPUSHs(sv_2mortal(newSVnv(value)));
2504
2505 long
2506 snmp_sys_uptime()
2507 CODE:
2508 RETVAL = get_uptime();
2509 OUTPUT:
2510 RETVAL
2511
2512 void
2513 init_snmp(appname)
2514 char *appname
2515 CODE:
2516 __libraries_init(appname);
2517
2518 #----------------------------------------------------------------------
2519 # Perl call defaults to (int)NULL when given no args, so it will return
2520 # the current api_mode values
2521 #----------------------------------------------------------------------
2522 int
2523 snmp_api_mode(mode=0)
2524 int mode
2525
2526 SnmpSession *
2527 snmp_new_session(version, community, peer, lport, retries, timeout)
2528 char * version
2529 char * community
2530 char * peer
2531 int lport
2532 int retries
2533 int timeout
2534 CODE:
2535 {
2536 SnmpSession session = {0};
2537 void *ss = NULL;
2538 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2539
2540 snmp_sess_init(&session);
2541
2542 __libraries_init("perl");
2543
2544 session.version = -1;
2545 #ifndef NETSNMP_DISABLE_SNMPV1
2546 if (!strcmp(version, "1")) {
2547 session.version = SNMP_VERSION_1;
2548 }
2549 #endif
2550 #ifndef NETSNMP_DISABLE_SNMPV2C
2551 if ((!strcmp(version, "2")) || (!strcmp(version, "2c"))) {
2552 session.version = SNMP_VERSION_2c;
2553 }
2554 #endif
2555 if (!strcmp(version, "3")) {
2556 session.version = SNMP_VERSION_3;
2557 }
2558 if (session.version == -1) {
2559 if (verbose)
2560 warn("error:snmp_new_session:Unsupported SNMP version (%s)\n", version);
2561 goto end;
2562 }
2563
2564 session.community_len = strlen((char *)community);
2565 session.community = (u_char *)community;
2566 session.peername = peer;
2567 session.local_port = lport;
2568 session.retries = retries; /* 5 */
2569 session.timeout = timeout; /* 1000000L */
2570 session.authenticator = NULL;
2571
2572 if(api_mode == SNMP_API_SINGLE)
2573 {
2574 ss = snmp_sess_open(&session);
2575 } else {
2576 ss = snmp_open(&session);
2577 }
2578
2579 if (ss == NULL) {
2580 if (verbose) warn("error:snmp_new_session: Couldn't open SNMP session");
2581 }
2582 end:
2583 RETVAL = ss;
2584 }
2585 OUTPUT:
2586 RETVAL
2587
2588 SnmpSession *
2589 snmp_new_v3_session(version, peer, retries, timeout, sec_name, sec_level, sec_eng_id, context_eng_id, context, auth_proto, auth_pass, priv_proto, priv_pass, eng_boots, eng_time, auth_master_key, auth_master_key_len, priv_master_key, priv_master_key_len, auth_localized_key, auth_localized_key_len, priv_localized_key, priv_localized_key_len)
2590 int version
2591 char * peer
2592 int retries
2593 int timeout
2594 char * sec_name
2595 int sec_level
2596 char * sec_eng_id
2597 char * context_eng_id
2598 char * context
2599 char * auth_proto
2600 char * auth_pass
2601 char * priv_proto
2602 char * priv_pass
2603 int eng_boots
2604 int eng_time
2605 char * auth_master_key
2606 size_t auth_master_key_len
2607 char * priv_master_key
2608 size_t priv_master_key_len
2609 char * auth_localized_key
2610 size_t auth_localized_key_len
2611 char * priv_localized_key
2612 size_t priv_localized_key_len
2613 CODE:
2614 {
2615 SnmpSession session = {0};
2616 void *ss = NULL;
2617 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2618 int auth_type, priv_type;
2619
2620 snmp_sess_init(&session);
2621
2622 __libraries_init("perl");
2623
2624 if (version == 3) {
2625 session.version = SNMP_VERSION_3;
2626 } else {
2627 if (verbose)
2628 warn("error:snmp_new_v3_session:Unsupported SNMP version (%d)\n", version);
2629 goto end;
2630 }
2631
2632 session.peername = peer;
2633 session.retries = retries; /* 5 */
2634 session.timeout = timeout; /* 1000000L */
2635 session.authenticator = NULL;
2636 session.contextNameLen = strlen(context);
2637 session.contextName = context;
2638 session.securityNameLen = strlen(sec_name);
2639 session.securityName = sec_name;
2640 session.securityLevel = sec_level;
2641 session.securityModel = USM_SEC_MODEL_NUMBER;
2642 session.securityEngineIDLen =
2643 hex_to_binary2((u_char*)sec_eng_id, strlen(sec_eng_id),
2644 (char **) &session.securityEngineID);
2645 session.contextEngineIDLen =
2646 hex_to_binary2((u_char*)context_eng_id, strlen(context_eng_id),
2647 (char **) &session.contextEngineID);
2648 session.engineBoots = eng_boots;
2649 session.engineTime = eng_time;
2650 /* NETSNMP_USMAUTH_* */
2651 auth_type = usm_lookup_auth_type(auth_proto);
2652 if (auth_type >= 0) {
2653 const netsnmp_auth_alg_info *auth_alg_info =
2654 sc_find_auth_alg_bytype(auth_type);
2655 if (auth_alg_info) {
2656 session.securityAuthProto =
2657 snmp_duplicate_objid(auth_alg_info->alg_oid,
2658 auth_alg_info->oid_len);
2659 session.securityAuthProtoLen = auth_alg_info->oid_len;
2660 }
2661 }
2662 if (strcmp(auth_proto, "DEFAULT") == 0) {
2663 const oid *theoid =
2664 get_default_authtype(&session.securityAuthProtoLen);
2665 session.securityAuthProto =
2666 snmp_duplicate_objid(theoid, session.securityAuthProtoLen);
2667 }
2668 if (session.securityAuthProto == NULL) {
2669 if (verbose)
2670 warn("error:snmp_new_v3_session:Unsupported authentication protocol(%s)\n", auth_proto);
2671 goto end;
2672 }
2673 if (session.securityLevel >= SNMP_SEC_LEVEL_AUTHNOPRIV) {
2674 if (auth_localized_key_len) {
2675 session.securityAuthLocalKey =
2676 netsnmp_memdup(auth_localized_key,
2677 auth_localized_key_len);
2678 session.securityAuthLocalKeyLen = auth_localized_key_len;
2679 } else if (auth_master_key_len) {
2680 session.securityAuthKeyLen =
2681 SNMP_MIN(auth_master_key_len,
2682 sizeof(session.securityAuthKey));
2683 memcpy(session.securityAuthKey, auth_master_key,
2684 session.securityAuthKeyLen);
2685 } else {
2686 if (strlen(auth_pass) > 0) {
2687 session.securityAuthKeyLen = USM_AUTH_KU_LEN;
2688 if (generate_Ku(session.securityAuthProto,
2689 session.securityAuthProtoLen,
2690 (u_char *)auth_pass, strlen(auth_pass),
2691 session.securityAuthKey,
2692 &session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
2693 if (verbose)
2694 warn("error:snmp_new_v3_session:Error generating Ku from authentication password.\n");
2695 goto end;
2696 }
2697 }
2698 }
2699 }
2700 priv_type = usm_lookup_priv_type(priv_proto);
2701 if (priv_type >= 0) {
2702 const netsnmp_priv_alg_info *priv_alg_info =
2703 sc_get_priv_alg_bytype(priv_type);
2704 if (priv_alg_info) {
2705 session.securityPrivProto =
2706 snmp_duplicate_objid(priv_alg_info->alg_oid,
2707 priv_alg_info->oid_len);
2708 session.securityPrivProtoLen = priv_alg_info->oid_len;
2709 }
2710 }
2711 if (strcmp(priv_proto, "DEFAULT") == 0) {
2712 const oid *theoid =
2713 get_default_privtype(&session.securityPrivProtoLen);
2714 session.securityPrivProto =
2715 snmp_duplicate_objid(theoid, session.securityPrivProtoLen);
2716 }
2717 if (session.securityPrivProto == NULL) {
2718 if (verbose)
2719 warn("error:snmp_new_v3_session:Unsupported privacy protocol(%s)\n", priv_proto);
2720 goto end;
2721 }
2722 if (session.securityLevel >= SNMP_SEC_LEVEL_AUTHPRIV) {
2723 if (priv_localized_key_len) {
2724 session.securityPrivLocalKey =
2725 netsnmp_memdup(priv_localized_key,
2726 priv_localized_key_len);
2727 session.securityPrivLocalKeyLen = priv_localized_key_len;
2728 } else if (priv_master_key_len) {
2729 session.securityPrivKeyLen =
2730 SNMP_MIN(auth_master_key_len,
2731 sizeof(session.securityPrivKey));
2732 memcpy(session.securityPrivKey, priv_master_key,
2733 session.securityPrivKeyLen);
2734 } else {
2735 session.securityPrivKeyLen = USM_PRIV_KU_LEN;
2736 if (generate_Ku(session.securityAuthProto,
2737 session.securityAuthProtoLen,
2738 (u_char *)priv_pass, strlen(priv_pass),
2739 session.securityPrivKey,
2740 &session.securityPrivKeyLen) != SNMPERR_SUCCESS) {
2741 if (verbose)
2742 warn("error:snmp_new_v3_session:Error generating Ku from privacy pass phrase.\n");
2743 goto end;
2744 }
2745 }
2746 }
2747
2748 if(api_mode == SNMP_API_SINGLE)
2749 {
2750 ss = snmp_sess_open(&session);
2751 } else {
2752 ss = snmp_open(&session);
2753 }
2754
2755 if (ss == NULL) {
2756 if (verbose) warn("error:snmp_new_v3_session:Couldn't open SNMP session");
2757 }
2758 end:
2759 RETVAL = ss;
2760 netsnmp_free(session.securityPrivLocalKey);
2761 netsnmp_free(session.securityPrivProto);
2762 netsnmp_free(session.securityAuthLocalKey);
2763 netsnmp_free(session.securityAuthProto);
2764 netsnmp_free(session.contextEngineID);
2765 netsnmp_free(session.securityEngineID);
2766 }
2767 OUTPUT:
2768 RETVAL
2769
2770 SnmpSession *
2771 snmp_new_tunneled_session(version, peer, retries, timeout, sec_name, sec_level, context_eng_id, context, our_identity, their_identity, their_hostname, trust_cert)
2772 int version
2773 char * peer
2774 int retries
2775 int timeout
2776 char * sec_name
2777 int sec_level
2778 char * context_eng_id
2779 char * context
2780 char * our_identity
2781 char * their_identity
2782 char * their_hostname
2783 char * trust_cert
2784 CODE:
2785 {
2786 SnmpSession session = {0};
2787 SnmpSession *ss = NULL;
2788 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2789
2790 snmp_sess_init(&session);
2791
2792 __libraries_init("perl");
2793
2794 session.version = version;
2795
2796 session.peername = peer;
2797 session.retries = retries; /* 5 */
2798 session.timeout = timeout; /* 1000000L */
2799 session.contextNameLen = strlen(context);
2800 session.contextName = context;
2801 session.securityNameLen = strlen(sec_name);
2802 session.securityName = sec_name;
2803 session.securityLevel = sec_level;
2804 session.securityModel = NETSNMP_TSM_SECURITY_MODEL;
2805 session.contextEngineIDLen =
2806 hex_to_binary2((u_char*)context_eng_id, strlen(context_eng_id),
2807 (char **) &session.contextEngineID);
2808
2809 /* create the transport configuration store */
2810 if (!session.transport_configuration) {
2811 netsnmp_container_init_list();
2812 session.transport_configuration =
2813 netsnmp_container_find("transport_configuration:fifo");
2814 if (!session.transport_configuration) {
2815 fprintf(stderr, "failed to initialize the transport configuration container\n");
2816 RETVAL = NULL;
2817 return;
2818 }
2819
2820 session.transport_configuration->compare =
2821 netsnmp_transport_config_compare;
2822 }
2823
2824 if (our_identity && our_identity[0] != '\0')
2825 CONTAINER_INSERT(session.transport_configuration,
2826 netsnmp_transport_create_config("our_identity",
2827 our_identity));
2828
2829 if (their_identity && their_identity[0] != '\0')
2830 CONTAINER_INSERT(session.transport_configuration,
2831 netsnmp_transport_create_config("their_identity",
2832 their_identity));
2833
2834 if (their_hostname && their_hostname[0] != '\0')
2835 CONTAINER_INSERT(session.transport_configuration,
2836 netsnmp_transport_create_config("their_hostname",
2837 their_hostname));
2838
2839 if (trust_cert && trust_cert[0] != '\0')
2840 CONTAINER_INSERT(session.transport_configuration,
2841 netsnmp_transport_create_config("trust_cert",
2842 trust_cert));
2843
2844
2845 ss = snmp_open(&session);
2846
2847 if (ss == NULL) {
2848 if (verbose) warn("error:snmp_new_v3_session:Couldn't open SNMP session");
2849 }
2850
2851 RETVAL = ss;
2852 netsnmp_free(session.securityPrivLocalKey);
2853 netsnmp_free(session.securityPrivProto);
2854 netsnmp_free(session.securityAuthLocalKey);
2855 netsnmp_free(session.securityAuthProto);
2856 netsnmp_free(session.contextEngineID);
2857 netsnmp_free(session.securityEngineID);
2858 }
2859 OUTPUT:
2860 RETVAL
2861
2862 SnmpSession *
2863 snmp_update_session(sess_ref, version, community, peer, lport, retries, timeout)
2864 SV * sess_ref
2865 char * version
2866 char * community
2867 char * peer
2868 int lport
2869 int retries
2870 int timeout
2871 CODE:
2872 {
2873 SV **sess_ptr_sv;
2874 SnmpSession *ss;
2875 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2876
2877 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
2878 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
2879
2880 if (!ss) goto update_end;
2881
2882 ss->version = -1;
2883 #ifndef NETSNMP_DISABLE_SNMPV1
2884 if (!strcmp(version, "1")) {
2885 ss->version = SNMP_VERSION_1;
2886 }
2887 #endif
2888 #ifndef NETSNMP_DISABLE_SNMPV2C
2889 if (!strcmp(version, "2") || !strcmp(version, "2c")) {
2890 ss->version = SNMP_VERSION_2c;
2891 }
2892 #endif
2893 if (!strcmp(version, "3")) {
2894 ss->version = SNMP_VERSION_3;
2895 }
2896 if (ss->version == -1) {
2897 if (verbose)
2898 warn("snmp_update_session: Unsupported SNMP version (%s)\n", version);
2899 goto update_end;
2900 }
2901 /* WARNING LEAKAGE but I cant free lib memory under win32 */
2902 ss->community_len = strlen((char *)community);
2903 ss->community = (u_char *)netsnmp_strdup(community);
2904 ss->peername = netsnmp_strdup(peer);
2905 ss->local_port = lport;
2906 ss->retries = retries; /* 5 */
2907 ss->timeout = timeout; /* 1000000L */
2908 ss->authenticator = NULL;
2909
2910 update_end:
2911 RETVAL = ss;
2912 }
2913 OUTPUT:
2914 RETVAL
2915
2916 int
2917 snmp_add_mib_dir(mib_dir,force=0)
2918 char * mib_dir
2919 int force
2920 CODE:
2921 {
2922 int result = 0; /* Avoid use of uninitialized variable below. */
2923 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2924
2925 DBPRT(999, (DBOUT "force=%d\n", force));
2926
2927 if (mib_dir && *mib_dir) {
2928 result = add_mibdir(mib_dir);
2929 }
2930 if (result) {
2931 if (verbose) warn("snmp_add_mib_dir: Added mib dir %s\n", mib_dir);
2932 } else {
2933 if (verbose) warn("snmp_add_mib_dir: Failed to add %s\n", mib_dir);
2934 }
2935 RETVAL = (I32)result;
2936 }
2937 OUTPUT:
2938 RETVAL
2939
2940 void
2941 snmp_init_mib_internals()
2942 CODE:
2943 {
2944 int notused = 1; notused++;
2945 /* this function does nothing */
2946 /* it is kept only for backwards compatibility */
2947 }
2948
2949
2950 char *
2951 snmp_getenv(name)
2952 char *name;
2953 CODE:
2954 RETVAL = netsnmp_getenv(name);
2955 OUTPUT:
2956 RETVAL
2957
2958 int
2959 snmp_setenv(envname, envval, overwrite)
2960 char *envname;
2961 char *envval;
2962 int overwrite;
2963 CODE:
2964 RETVAL = netsnmp_setenv(envname, envval, overwrite);
2965 OUTPUT:
2966 RETVAL
2967
2968 int
2969 snmp_read_mib(mib_file, force=0)
2970 char * mib_file
2971 int force
2972 CODE:
2973 {
2974 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2975
2976 DBPRT(999, (DBOUT "force=%d\n", force));
2977
2978 if ((mib_file == NULL) || (*mib_file == '\0')) {
2979 if (get_tree_head() == NULL) {
2980 if (verbose) warn("snmp_read_mib: initializing MIB\n");
2981 netsnmp_init_mib();
2982 if (get_tree_head()) {
2983 if (verbose) warn("done\n");
2984 } else {
2985 if (verbose) warn("failed\n");
2986 }
2987 }
2988 } else {
2989 if (verbose) warn("snmp_read_mib: reading MIB: %s\n", mib_file);
2990 if (strcmp("ALL",mib_file))
2991 read_mib(mib_file);
2992 else
2993 read_all_mibs();
2994 if (get_tree_head()) {
2995 if (verbose) warn("done\n");
2996 } else {
2997 if (verbose) warn("failed\n");
2998 }
2999 }
3000 RETVAL = (IV)get_tree_head();
3001 }
3002 OUTPUT:
3003 RETVAL
3004
3005
3006 int
3007 snmp_read_module(module)
3008 char * module
3009 CODE:
3010 {
3011 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3012
3013 if (!strcmp(module,"ALL")) {
3014 read_all_mibs();
3015 } else {
3016 netsnmp_read_module(module);
3017 }
3018 if (get_tree_head()) {
3019 if (verbose) warn("Read %s\n", module);
3020 } else {
3021 if (verbose) warn("Failed reading %s\n", module);
3022 }
3023 RETVAL = (IV)get_tree_head();
3024 }
3025 OUTPUT:
3026 RETVAL
3027
3028 void
3029 snmp_set(sess_ref, varlist_ref, perl_callback)
3030 SV * sess_ref
3031 SV * varlist_ref
3032 SV * perl_callback
3033 PPCODE:
3034 {
3035 #ifndef NETSNMP_NO_WRITE_SUPPORT
3036 AV *varlist;
3037 SV **varbind_ref;
3038 SV **varbind_val_f;
3039 AV *varbind;
3040 I32 varlist_len;
3041 I32 varlist_ind;
3042 void *ss;
3043 netsnmp_pdu *pdu, *response;
3044 struct tree *tp;
3045 oid *oid_arr;
3046 size_t oid_arr_len = MAX_OID_LEN;
3047 char *tag_pv;
3048 snmp_xs_cb_data *xs_cb_data;
3049 SV **sess_ptr_sv;
3050 SV **err_str_svp;
3051 SV **err_num_svp;
3052 SV **err_ind_svp;
3053 int status = 0;
3054 int type;
3055 int res;
3056 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3057 int use_enums;
3058 struct enum_list *ep;
3059 int best_guess;
3060
3061 New (0, oid_arr, MAX_OID_LEN, oid);
3062
3063 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
3064
3065 use_enums = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums",8,1));
3066 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3067 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3068 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3069 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3070 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3071 sv_setpv(*err_str_svp, "");
3072 sv_setiv(*err_num_svp, 0);
3073 sv_setiv(*err_ind_svp, 0);
3074 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
3075
3076 pdu = snmp_pdu_create(SNMP_MSG_SET);
3077
3078 varlist = (AV*) SvRV(varlist_ref);
3079 varlist_len = av_len(varlist);
3080 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
3081 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3082 if (SvROK(*varbind_ref)) {
3083 varbind = (AV*) SvRV(*varbind_ref);
3084 tag_pv = __av_elem_pv(varbind, VARBIND_TAG_F,NULL);
3085 tp=__tag2oid(tag_pv,
3086 __av_elem_pv(varbind, VARBIND_IID_F,NULL),
3087 oid_arr, &oid_arr_len, &type, best_guess);
3088
3089 if (oid_arr_len==0) {
3090 if (verbose)
3091 warn("error: set: unknown object ID (%s)",
3092 (tag_pv?tag_pv:"<null>"));
3093 sv_catpv(*err_str_svp,
3094 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
3095 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
3096 XPUSHs(&sv_undef); /* unknown OID */
3097 snmp_free_pdu(pdu);
3098 goto done;
3099 }
3100
3101
3102 if (type == TYPE_UNKNOWN) {
3103 type = __translate_appl_type(
3104 __av_elem_pv(varbind, VARBIND_TYPE_F, NULL));
3105 if (type == TYPE_UNKNOWN) {
3106 if (verbose)
3107 warn("error: set: no type found for object");
3108 sv_catpv(*err_str_svp,
3109 (char*)snmp_api_errstring(SNMPERR_VAR_TYPE));
3110 sv_setiv(*err_num_svp, SNMPERR_VAR_TYPE);
3111 XPUSHs(&sv_undef); /* unknown OID */
3112 snmp_free_pdu(pdu);
3113 goto done;
3114 }
3115 }
3116
3117 varbind_val_f = av_fetch(varbind, VARBIND_VAL_F, 0);
3118
3119 if (type==TYPE_INTEGER && use_enums && tp && tp->enums) {
3120 for(ep = tp->enums; ep; ep = ep->next) {
3121 if (varbind_val_f && SvOK(*varbind_val_f) &&
3122 !strcmp(ep->label, SvPV(*varbind_val_f,na))) {
3123 sv_setiv(*varbind_val_f, ep->value);
3124 break;
3125 }
3126 }
3127 }
3128
3129 res = __add_var_val_str(pdu, oid_arr, oid_arr_len,
3130 (varbind_val_f && SvOK(*varbind_val_f) ?
3131 SvPV(*varbind_val_f,na):NULL),
3132 (varbind_val_f && SvPOK(*varbind_val_f) ?
3133 SvCUR(*varbind_val_f):0), type);
3134
3135 if (verbose && res == FAILURE)
3136 warn("error: set: adding variable/value to PDU");
3137 } /* if var_ref is ok */
3138 } /* for all the vars */
3139
3140 if (SvTRUE(perl_callback)) {
3141 xs_cb_data =
3142 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3143 xs_cb_data->perl_cb = newSVsv(perl_callback);
3144 xs_cb_data->sess_ref = newRV_inc(SvRV(sess_ref));
3145
3146 if(api_mode == SNMP_API_SINGLE)
3147 {
3148 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
3149 (void*)xs_cb_data);
3150 } else {
3151 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
3152 (void*)xs_cb_data);
3153 }
3154 if (status != 0) {
3155 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
3156 } else {
3157 snmp_free_pdu(pdu);
3158 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
3159 XPUSHs(&sv_undef);
3160 }
3161 goto done;
3162 }
3163
3164 status = __send_sync_pdu(ss, pdu, &response,
3165 NO_RETRY_NOSUCH,
3166 *err_str_svp, *err_num_svp,
3167 *err_ind_svp);
3168
3169 if (response) snmp_free_pdu(response);
3170
3171 if (status) {
3172 XPUSHs(&sv_undef);
3173 } else {
3174 XPUSHs(sv_2mortal(newSVpv(ZERO_BUT_TRUE,0)));
3175 }
3176 } else {
3177
3178 /* BUG!!! need to return an error value */
3179 XPUSHs(&sv_undef); /* no mem or bad args */
3180 }
3181 done:
3182 Safefree(oid_arr);
3183 #else /* NETSNMP_NO_WRITE_SUPPORT */
3184 warn("error: Net-SNMP was compiled using --enable-read-only, set() can not be used.");
3185 #endif /* NETSNMP_NO_WRITE_SUPPORT */
3186 }
3187
3188 void
3189 snmp_catch(sess_ref, perl_callback)
3190 SV * sess_ref
3191 SV * perl_callback
3192 PPCODE:
3193 {
3194 void *sess_ptr;
3195 netsnmp_session *ss;
3196 SV **sess_ptr_sv;
3197 SV **err_str_svp;
3198 SV **err_num_svp;
3199 SV **err_ind_svp;
3200
3201 if (SvROK(sess_ref)) {
3202 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3203 sess_ptr = (void *)SvIV((SV*)SvRV(*sess_ptr_sv));
3204 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3205 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3206 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3207 sv_setpv(*err_str_svp, "");
3208 sv_setiv(*err_num_svp, 0);
3209 sv_setiv(*err_ind_svp, 0);
3210
3211 ss = api_mode == SNMP_API_SINGLE ? snmp_sess_session(sess_ptr) :
3212 sess_ptr;
3213 ss->callback = NULL;
3214 ss->callback_magic = NULL;
3215
3216 if (SvTRUE(perl_callback)) {
3217 snmp_xs_cb_data *xs_cb_data;
3218 xs_cb_data =
3219 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3220 xs_cb_data->perl_cb = newSVsv(perl_callback);
3221 xs_cb_data->sess_ref = newRV_inc(SvRV(sess_ref));
3222
3223 # it might be more efficient to pass the varbind_ref to
3224 # __snmp_xs_cb as part of perl_callback so it is not freed
3225 # and reconstructed for each call
3226 ss->callback = __snmp_xs_cb;
3227 ss->callback_magic = xs_cb_data;
3228 sv_2mortal(newSViv(1));
3229 goto done;
3230 }
3231 }
3232 sv_2mortal(newSViv(0));
3233 done:
3234 ;
3235 }
3236
3237 void
3238 snmp_get(sess_ref, retry_nosuch, varlist_ref, perl_callback)
3239 SV * sess_ref
3240 int retry_nosuch
3241 SV * varlist_ref
3242 SV * perl_callback
3243 PPCODE:
3244 {
3245 AV *varlist;
3246 SV **varbind_ref;
3247 AV *varbind;
3248 I32 varlist_len;
3249 I32 varlist_ind;
3250 void *ss;
3251 netsnmp_pdu *pdu, *response;
3252 netsnmp_variable_list *vars;
3253 struct tree *tp;
3254 int len;
3255 oid *oid_arr;
3256 size_t oid_arr_len = MAX_OID_LEN;
3257 SV *tmp_sv;
3258 int type;
3259 char tmp_type_str[MAX_TYPE_NAME_LEN];
3260 snmp_xs_cb_data *xs_cb_data;
3261 SV **sess_ptr_sv;
3262 SV **err_str_svp;
3263 SV **err_num_svp;
3264 SV **err_ind_svp;
3265 int status;
3266 char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;
3267 size_t str_buf_len = sizeof(str_buf);
3268 size_t out_len = 0;
3269 int buf_over = 0;
3270 char *label;
3271 char *iid;
3272 int getlabel_flag = NO_FLAGS;
3273 int sprintval_flag = USE_BASIC;
3274 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3275 int old_format;
3276 SV *sv_timestamp = NULL;
3277 int best_guess;
3278
3279 New (0, oid_arr, MAX_OID_LEN, oid);
3280
3281 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
3282
3283 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3284 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3285 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3286 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3287 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3288 sv_setpv(*err_str_svp, "");
3289 sv_setiv(*err_num_svp, 0);
3290 sv_setiv(*err_ind_svp, 0);
3291 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1)))
3292 getlabel_flag |= USE_LONG_NAMES;
3293 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1)))
3294 getlabel_flag |= USE_NUMERIC_OIDS;
3295 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums", 8, 1)))
3296 sprintval_flag = USE_ENUMS;
3297 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseSprintValue", 14, 1)))
3298 sprintval_flag = USE_SPRINT_VALUE;
3299 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
3300
3301 pdu = snmp_pdu_create(SNMP_MSG_GET);
3302
3303 varlist = (AV*) SvRV(varlist_ref);
3304 varlist_len = av_len(varlist);
3305 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
3306 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3307 if (SvROK(*varbind_ref)) {
3308 char *tag_pv;
3309 varbind = (AV*) SvRV(*varbind_ref);
3310
3311 tag_pv = __av_elem_pv(varbind, VARBIND_TAG_F, ".0");
3312 tp = __tag2oid(tag_pv,
3313 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
3314 oid_arr, &oid_arr_len, NULL, best_guess);
3315
3316 if (oid_arr_len) {
3317 snmp_add_null_var(pdu, oid_arr, oid_arr_len);
3318 } else {
3319 if (verbose)
3320 warn("error: get: unknown object ID (%s)",
3321 (tag_pv?tag_pv:"<null>"));
3322 sv_catpv(*err_str_svp,
3323 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
3324 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
3325 XPUSHs(&sv_undef); /* unknown OID */
3326 snmp_free_pdu(pdu);
3327 goto done;
3328 }
3329
3330 } /* if var_ref is ok */
3331 } /* for all the vars */
3332
3333 if (perl_callback && SvTRUE(perl_callback)) {
3334 xs_cb_data =
3335 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3336 xs_cb_data->perl_cb = newSVsv(perl_callback);
3337 xs_cb_data->sess_ref = newSVsv(sess_ref);
3338
3339 if(api_mode == SNMP_API_SINGLE)
3340 {
3341 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
3342 (void*)xs_cb_data);
3343 } else {
3344 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
3345 (void*)xs_cb_data);
3346 }
3347 if (status != 0) {
3348 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
3349 } else {
3350 snmp_free_pdu(pdu);
3351 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
3352 XPUSHs(&sv_undef);
3353 }
3354 goto done;
3355 }
3356
3357 status = __send_sync_pdu(ss, pdu, &response,
3358 retry_nosuch,
3359 *err_str_svp, *err_num_svp,
3360 *err_ind_svp);
3361
3362 /*
3363 ** Set up for numeric or full OID's, if necessary. Save the old
3364 ** output format so that it can be restored when we finish -- this
3365 ** is a library-wide global, and has to be set/restored for each
3366 ** session.
3367 */
3368 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
3369 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
3370
3371 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1))) {
3372 getlabel_flag |= USE_LONG_NAMES;
3373
3374 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3375 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3376 NETSNMP_OID_OUTPUT_FULL);
3377 }
3378 /* Setting UseNumeric forces UseLongNames on so check for UseNumeric
3379 after UseLongNames (above) to make sure the final outcome of
3380 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT is NETSNMP_OID_OUTPUT_NUMERIC */
3381 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1))) {
3382 getlabel_flag |= USE_LONG_NAMES;
3383 getlabel_flag |= USE_NUMERIC_OIDS;
3384
3385 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3386 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3387 NETSNMP_OID_OUTPUT_NUMERIC);
3388 }
3389
3390 if (SvIOK(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)) &&
3391 SvIV(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)))
3392 sv_timestamp = newSViv((IV)time(NULL));
3393
3394 for(vars = (response?response->variables:NULL), varlist_ind = 0;
3395 vars && (varlist_ind <= varlist_len);
3396 vars = vars->next_variable, varlist_ind++) {
3397 int local_getlabel_flag = getlabel_flag;
3398 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3399 if (SvROK(*varbind_ref)) {
3400 varbind = (AV*) SvRV(*varbind_ref);
3401
3402 *str_buf = '.';
3403 *(str_buf+1) = '\0';
3404 out_len = 0;
3405 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp,
3406 &str_buf_len,
3407 &out_len, 0,
3408 &buf_over,
3409 vars->name,
3410 vars->name_length);
3411 str_buf[sizeof(str_buf)-1] = '\0';
3412
3413 if (__is_leaf(tp)) {
3414 type = tp->type;
3415 } else {
3416 local_getlabel_flag |= NON_LEAF_NAME;
3417 type = __translate_asn_type(vars->type);
3418 }
3419 __get_label_iid(str_buf,&label,&iid,local_getlabel_flag);
3420 if (label) {
3421 av_store(varbind, VARBIND_TAG_F,
3422 newSVpv(label, strlen(label)));
3423 } else {
3424 av_store(varbind, VARBIND_TAG_F,
3425 newSVpv("", 0));
3426 }
3427 if (iid) {
3428 av_store(varbind, VARBIND_IID_F,
3429 newSVpv(iid, strlen(iid)));
3430 } else {
3431 av_store(varbind, VARBIND_IID_F,
3432 newSVpv("", 0));
3433 }
3434 __get_type_str(type, tmp_type_str);
3435 tmp_sv = newSVpv(tmp_type_str, strlen(tmp_type_str));
3436 av_store(varbind, VARBIND_TYPE_F, tmp_sv);
3437 len=__snprint_value(str_buf,sizeof(str_buf),
3438 vars,tp,type,sprintval_flag);
3439 tmp_sv = newSVpv(str_buf, len);
3440 av_store(varbind, VARBIND_VAL_F, tmp_sv);
3441 if (sv_timestamp)
3442 av_store(varbind, VARBIND_TYPE_F, sv_timestamp);
3443 XPUSHs(sv_mortalcopy(tmp_sv));
3444 } else {
3445 /* Return undef for this variable. */
3446 XPUSHs(&sv_undef);
3447 }
3448 }
3449
3450 /* Reset the library's behavior for numeric/symbolic OID's. */
3451 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3452 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3453 old_format);
3454
3455 if (response) snmp_free_pdu(response);
3456
3457 } else {
3458 XPUSHs(&sv_undef); /* no mem or bad args */
3459 }
3460 done:
3461 Safefree(oid_arr);
3462 }
3463
3464 void
3465 snmp_getnext(sess_ref, varlist_ref, perl_callback)
3466 SV * sess_ref
3467 SV * varlist_ref
3468 SV * perl_callback
3469 PPCODE:
3470 {
3471 AV *varlist;
3472 SV **varbind_ref;
3473 AV *varbind;
3474 I32 varlist_len;
3475 I32 varlist_ind;
3476 void *ss;
3477 netsnmp_pdu *pdu, *response;
3478 netsnmp_variable_list *vars;
3479 struct tree *tp;
3480 int len;
3481 oid *oid_arr;
3482 size_t oid_arr_len = MAX_OID_LEN;
3483 SV *tmp_sv;
3484 int type;
3485 char tmp_type_str[MAX_TYPE_NAME_LEN];
3486 snmp_xs_cb_data *xs_cb_data;
3487 SV **sess_ptr_sv;
3488 SV **err_str_svp;
3489 SV **err_num_svp;
3490 SV **err_ind_svp;
3491 int status;
3492 char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;
3493 size_t str_buf_len = sizeof(str_buf);
3494 char tmp_buf_prefix[STR_BUF_SIZE];
3495 char str_buf_prefix[STR_BUF_SIZE];
3496 size_t out_len = 0;
3497 int buf_over = 0;
3498 char *label;
3499 char *iid;
3500 int getlabel_flag = NO_FLAGS;
3501 int sprintval_flag = USE_BASIC;
3502 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3503 int old_format;
3504 SV *sv_timestamp = NULL;
3505 int best_guess;
3506 char *tmp_prefix_ptr;
3507 char *st;
3508
3509 New (0, oid_arr, MAX_OID_LEN, oid);
3510
3511 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
3512
3513 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3514 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3515 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3516 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3517 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3518 sv_setpv(*err_str_svp, "");
3519 sv_setiv(*err_num_svp, 0);
3520 sv_setiv(*err_ind_svp, 0);
3521 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1)))
3522 getlabel_flag |= USE_LONG_NAMES;
3523 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1)))
3524 getlabel_flag |= USE_NUMERIC_OIDS;
3525 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums", 8, 1)))
3526 sprintval_flag = USE_ENUMS;
3527 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseSprintValue", 14, 1)))
3528 sprintval_flag = USE_SPRINT_VALUE;
3529 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
3530
3531 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
3532
3533 varlist = (AV*) SvRV(varlist_ref);
3534 varlist_len = av_len(varlist);
3535 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
3536 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3537 if (SvROK(*varbind_ref)) {
3538 char *tag_pv;
3539 varbind = (AV*) SvRV(*varbind_ref);
3540
3541 /* If the varbind includes the module prefix, capture it for use later */
3542 strlcpy(tmp_buf_prefix, __av_elem_pv(varbind, VARBIND_TAG_F, ".0"), STR_BUF_SIZE);
3543 tmp_prefix_ptr = strstr(tmp_buf_prefix,"::");
3544 if (tmp_prefix_ptr) {
3545 tmp_prefix_ptr = strtok_r(tmp_buf_prefix, "::", &st);
3546 strlcpy(str_buf_prefix, tmp_prefix_ptr, STR_BUF_SIZE);
3547 }
3548 else {
3549 *str_buf_prefix = '\0';
3550 }
3551
3552 tag_pv = __av_elem_pv(varbind, VARBIND_TAG_F, ".0");
3553 tp = __tag2oid(tag_pv,
3554 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
3555 oid_arr, &oid_arr_len, NULL, best_guess);
3556
3557 if (oid_arr_len) {
3558 snmp_add_null_var(pdu, oid_arr, oid_arr_len);
3559 } else {
3560 if (verbose)
3561 warn("error: getnext: unknown object ID (%s)",
3562 (tag_pv?tag_pv:"<null>"));
3563 sv_catpv(*err_str_svp,
3564 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
3565 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
3566 XPUSHs(&sv_undef); /* unknown OID */
3567 snmp_free_pdu(pdu);
3568 goto done;
3569 }
3570
3571 } /* if var_ref is ok */
3572 } /* for all the vars */
3573
3574 if (SvTRUE(perl_callback)) {
3575 xs_cb_data =
3576 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3577 xs_cb_data->perl_cb = newSVsv(perl_callback);
3578 xs_cb_data->sess_ref = newSVsv(sess_ref);
3579
3580 if(api_mode == SNMP_API_SINGLE)
3581 {
3582 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
3583 (void*)xs_cb_data);
3584 } else {
3585 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
3586 (void*)xs_cb_data);
3587 }
3588 if (status != 0) {
3589 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
3590 } else {
3591 snmp_free_pdu(pdu);
3592 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
3593 XPUSHs(&sv_undef);
3594 }
3595 goto done;
3596 }
3597
3598 status = __send_sync_pdu(ss, pdu, &response,
3599 NO_RETRY_NOSUCH,
3600 *err_str_svp, *err_num_svp,
3601 *err_ind_svp);
3602
3603 /*
3604 ** Set up for numeric or full OID's, if necessary. Save the old
3605 ** output format so that it can be restored when we finish -- this
3606 ** is a library-wide global, and has to be set/restored for each
3607 ** session.
3608 */
3609 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
3610 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
3611
3612 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1))) {
3613 getlabel_flag |= USE_LONG_NAMES;
3614
3615 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3616 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3617 NETSNMP_OID_OUTPUT_FULL);
3618 }
3619 /* Setting UseNumeric forces UseLongNames on so check
3620 for UseNumeric after UseLongNames (above) to make
3621 sure the final outcome of
3622 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT is
3623 NETSNMP_OID_OUTPUT_NUMERIC */
3624 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1))) {
3625 getlabel_flag |= USE_LONG_NAMES;
3626 getlabel_flag |= USE_NUMERIC_OIDS;
3627
3628 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3629 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3630 NETSNMP_OID_OUTPUT_NUMERIC);
3631 }
3632
3633 if (SvIOK(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)) &&
3634 SvIV(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)))
3635 sv_timestamp = newSViv((IV)time(NULL));
3636
3637 for(vars = (response?response->variables:NULL), varlist_ind = 0;
3638 vars && (varlist_ind <= varlist_len);
3639 vars = vars->next_variable, varlist_ind++) {
3640 int local_getlabel_flag = getlabel_flag;
3641 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3642 if (SvROK(*varbind_ref)) {
3643 varbind = (AV*) SvRV(*varbind_ref);
3644
3645 *str_buf = '.';
3646 *(str_buf+1) = '\0';
3647 out_len = 0;
3648 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp,
3649 &str_buf_len,
3650 &out_len, 0,
3651 &buf_over,
3652 vars->name,
3653 vars->name_length);
3654 str_buf[sizeof(str_buf)-1] = '\0';
3655
3656 /* Prepend the module prefix to the next OID if needed */
3657 if (*str_buf_prefix) {
3658 strlcat(str_buf_prefix, "::", STR_BUF_SIZE);
3659 strlcat(str_buf_prefix, str_buf, STR_BUF_SIZE);
3660 strlcpy(str_buf, str_buf_prefix, STR_BUF_SIZE);
3661 }
3662
3663 if (__is_leaf(tp)) {
3664 type = tp->type;
3665 } else {
3666 local_getlabel_flag |= NON_LEAF_NAME;
3667 type = __translate_asn_type(vars->type);
3668 }
3669 __get_label_iid(str_buf,&label,&iid,local_getlabel_flag);
3670 if (label) {
3671 av_store(varbind, VARBIND_TAG_F,
3672 newSVpv(label, strlen(label)));
3673 } else {
3674 av_store(varbind, VARBIND_TAG_F,
3675 newSVpv("", 0));
3676 }
3677 if (iid) {
3678 av_store(varbind, VARBIND_IID_F,
3679 newSVpv(iid, strlen(iid)));
3680 } else {
3681 av_store(varbind, VARBIND_IID_F,
3682 newSVpv("", 0));
3683 }
3684 __get_type_str(type, tmp_type_str);
3685 tmp_sv = newSVpv(tmp_type_str, strlen(tmp_type_str));
3686 av_store(varbind, VARBIND_TYPE_F, tmp_sv);
3687 len=__snprint_value(str_buf,sizeof(str_buf),
3688 vars,tp,type,sprintval_flag);
3689 tmp_sv = newSVpv(str_buf, len);
3690 av_store(varbind, VARBIND_VAL_F, tmp_sv);
3691 if (sv_timestamp)
3692 av_store(varbind, VARBIND_TYPE_F, sv_timestamp);
3693 XPUSHs(sv_mortalcopy(tmp_sv));
3694 } else {
3695 /* Return undef for this variable. */
3696 XPUSHs(&sv_undef);
3697 }
3698 }
3699
3700 /* Reset the library's behavior for numeric/symbolic OID's. */
3701 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3702 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3703 old_format);
3704
3705 if (response) snmp_free_pdu(response);
3706
3707 } else {
3708 XPUSHs(&sv_undef); /* no mem or bad args */
3709 }
3710 done:
3711 Safefree(oid_arr);
3712 }
3713
3714 void
3715 snmp_getbulk(sess_ref, nonrepeaters, maxrepetitions, varlist_ref, perl_callback)
3716 SV * sess_ref
3717 int nonrepeaters
3718 int maxrepetitions
3719 SV * varlist_ref
3720 SV * perl_callback
3721 PPCODE:
3722 {
3723 AV *varlist;
3724 SV **varbind_ref;
3725 AV *varbind;
3726 I32 varlist_len;
3727 I32 varlist_ind;
3728 void *ss;
3729 netsnmp_pdu *pdu, *response;
3730 netsnmp_variable_list *vars;
3731 struct tree *tp;
3732 int len;
3733 oid *oid_arr;
3734 size_t oid_arr_len = MAX_OID_LEN;
3735 SV *tmp_sv;
3736 int type;
3737 char tmp_type_str[MAX_TYPE_NAME_LEN];
3738 snmp_xs_cb_data *xs_cb_data;
3739 SV **sess_ptr_sv;
3740 SV **err_str_svp;
3741 SV **err_num_svp;
3742 SV **err_ind_svp;
3743 int status;
3744 char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;
3745 size_t str_buf_len = sizeof(str_buf);
3746 size_t out_len = 0;
3747 int buf_over = 0;
3748 char *label;
3749 char *iid;
3750 int getlabel_flag = NO_FLAGS;
3751 int sprintval_flag = USE_BASIC;
3752 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3753 int old_format;
3754 SV *rv;
3755 SV *sv_timestamp = NULL;
3756 int best_guess;
3757
3758 New (0, oid_arr, MAX_OID_LEN, oid);
3759
3760 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
3761
3762 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3763 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3764 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3765 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3766 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3767 sv_setpv(*err_str_svp, "");
3768 sv_setiv(*err_num_svp, 0);
3769 sv_setiv(*err_ind_svp, 0);
3770 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1)))
3771 getlabel_flag |= USE_LONG_NAMES;
3772 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1)))
3773 getlabel_flag |= USE_NUMERIC_OIDS;
3774 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums", 8, 1)))
3775 sprintval_flag = USE_ENUMS;
3776 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseSprintValue", 14, 1)))
3777 sprintval_flag = USE_SPRINT_VALUE;
3778 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
3779
3780 pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
3781
3782 pdu->errstat = nonrepeaters;
3783 pdu->errindex = maxrepetitions;
3784
3785 varlist = (AV*) SvRV(varlist_ref);
3786 varlist_len = av_len(varlist);
3787 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
3788 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3789 if (SvROK(*varbind_ref)) {
3790 char *tag_pv;
3791 varbind = (AV*) SvRV(*varbind_ref);
3792 tag_pv = __av_elem_pv(varbind, VARBIND_TAG_F, "0");
3793 __tag2oid(tag_pv,
3794 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
3795 oid_arr, &oid_arr_len, NULL, best_guess);
3796
3797
3798 if (oid_arr_len) {
3799 snmp_add_null_var(pdu, oid_arr, oid_arr_len);
3800 } else {
3801 if (verbose)
3802 warn("error: getbulk: unknown object ID (%s)",
3803 (tag_pv?tag_pv:"<null>"));
3804 sv_catpv(*err_str_svp,
3805 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
3806 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
3807 XPUSHs(&sv_undef); /* unknown OID */
3808 snmp_free_pdu(pdu);
3809 goto done;
3810 }
3811
3812
3813 } /* if var_ref is ok */
3814 } /* for all the vars */
3815
3816 if (SvTRUE(perl_callback)) {
3817 xs_cb_data =
3818 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3819 xs_cb_data->perl_cb = newSVsv(perl_callback);
3820 xs_cb_data->sess_ref = newSVsv(sess_ref);
3821
3822 if(api_mode == SNMP_API_SINGLE)
3823 {
3824 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
3825 (void*)xs_cb_data);
3826 } else {
3827 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
3828 (void*)xs_cb_data);
3829 }
3830 if (status != 0) {
3831 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
3832 } else {
3833 snmp_free_pdu(pdu);
3834 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
3835 XPUSHs(&sv_undef);
3836 }
3837 goto done;
3838 }
3839
3840 status = __send_sync_pdu(ss, pdu, &response,
3841 NO_RETRY_NOSUCH,
3842 *err_str_svp, *err_num_svp,
3843 *err_ind_svp);
3844
3845 if (SvIOK(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)) &&
3846 SvIV(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)))
3847 sv_timestamp = newSViv((IV)time(NULL));
3848
3849 av_clear(varlist);
3850
3851 /*
3852 ** Set up for numeric or full OID's, if necessary. Save the old
3853 ** output format so that it can be restored when we finish -- this
3854 ** is a library-wide global, and has to be set/restored for each
3855 ** session.
3856 */
3857 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
3858 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
3859 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1))) {
3860 getlabel_flag |= USE_LONG_NAMES;
3861
3862 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3863 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3864 NETSNMP_OID_OUTPUT_FULL);
3865 }
3866 /* Setting UseNumeric forces UseLongNames on so check for UseNumeric
3867 after UseLongNames (above) to make sure the final outcome of
3868 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT is NETSNMP_OID_OUTPUT_NUMERIC */
3869 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1))) {
3870 getlabel_flag |= USE_LONG_NAMES;
3871 getlabel_flag |= USE_NUMERIC_OIDS;
3872
3873 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3874 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3875 NETSNMP_OID_OUTPUT_NUMERIC);
3876 }
3877
3878 if(response && response->variables) {
3879 for(vars = response->variables;
3880 vars;
3881 vars = vars->next_variable) {
3882
3883 int local_getlabel_flag = getlabel_flag;
3884 varbind = (AV*) newAV();
3885 *str_buf = '.';
3886 *(str_buf+1) = '\0';
3887 out_len = 0;
3888 buf_over = 0;
3889 str_bufp = str_buf;
3890 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp,
3891 &str_buf_len,
3892 &out_len, 0,
3893 &buf_over,
3894 vars->name,
3895 vars->name_length);
3896 str_buf[sizeof(str_buf)-1] = '\0';
3897 if (__is_leaf(tp)) {
3898 type = tp->type;
3899 } else {
3900 local_getlabel_flag |= NON_LEAF_NAME;
3901 type = __translate_asn_type(vars->type);
3902 }
3903 __get_label_iid(str_buf,&label,&iid,local_getlabel_flag);
3904 if (label) {
3905 av_store(varbind, VARBIND_TAG_F,
3906 newSVpv(label, strlen(label)));
3907 } else {
3908 av_store(varbind, VARBIND_TAG_F,
3909 newSVpv("", 0));
3910 }
3911 if (iid) {
3912 av_store(varbind, VARBIND_IID_F,
3913 newSVpv(iid, strlen(iid)));
3914 } else {
3915 av_store(varbind, VARBIND_IID_F,
3916 newSVpv("", 0));
3917 }
3918 __get_type_str(type, tmp_type_str);
3919 av_store(varbind, VARBIND_TYPE_F, newSVpv(tmp_type_str,
3920 strlen(tmp_type_str)));
3921
3922 len=__snprint_value(str_buf,sizeof(str_buf),
3923 vars,tp,type,sprintval_flag);
3924 tmp_sv = newSVpv(str_buf, len);
3925 av_store(varbind, VARBIND_VAL_F, tmp_sv);
3926 if (sv_timestamp)
3927 av_store(varbind, VARBIND_TYPE_F, SvREFCNT_inc(sv_timestamp));
3928
3929 rv = newRV_noinc((SV *)varbind);
3930 sv_bless(rv, gv_stashpv("SNMP::Varbind",0));
3931 av_push(varlist, rv);
3932
3933 XPUSHs(sv_mortalcopy(tmp_sv));
3934 }
3935 } else {
3936 XPUSHs(&sv_undef);
3937 }
3938
3939 /* Reset the library's behavior for numeric/symbolic OID's. */
3940 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3941 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3942 old_format);
3943
3944 if (response) snmp_free_pdu(response);
3945
3946 } else {
3947 XPUSHs(&sv_undef); /* no mem or bad args */
3948 }
3949 done:
3950 Safefree(oid_arr);
3951 }
3952
3953 void
3954 snmp_bulkwalk(sess_ref, nonrepeaters, maxrepetitions, varlist_ref,perl_callback)
3955 SV * sess_ref
3956 int nonrepeaters
3957 int maxrepetitions
3958 SV * varlist_ref
3959 SV * perl_callback
3960 PPCODE:
3961 {
3962 AV *varlist;
3963 SV **varbind_ref;
3964 AV *varbind;
3965 I32 varlist_len;
3966 I32 varlist_ind;
3967 void *ss;
3968 netsnmp_pdu *pdu = NULL;
3969 oid oid_arr[MAX_OID_LEN];
3970 size_t oid_arr_len;
3971 SV **sess_ptr_sv;
3972 SV **err_str_svp;
3973 SV **err_num_svp;
3974 SV **err_ind_svp;
3975 char str_buf[STR_BUF_SIZE];
3976 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3977 walk_context *context = NULL; /* Context for this bulkwalk */
3978 bulktbl *bt_entry; /* Current bulktbl/OID entry */
3979 int i; /* General purpose iterator */
3980 int npushed; /* Number of return arrays */
3981 int okay; /* Did bulkwalk complete okay */
3982 int best_guess;
3983
3984 if (!SvROK(sess_ref) || !SvROK(varlist_ref)) {
3985 if (verbose)
3986 warn("bulkwalk: Bad session or varlist reference!\n");
3987
3988 XSRETURN_UNDEF;
3989 }
3990
3991 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3992 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3993 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3994 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3995 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3996 sv_setpv(*err_str_svp, "");
3997 sv_setiv(*err_num_svp, 0);
3998 sv_setiv(*err_ind_svp, 0);
3999 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
4000
4001 /* Create and initialize a new session context for this bulkwalk.
4002 ** This will be used to carry state between callbacks.
4003 */
4004 Newz(0x57616b6c /* "Walk" */, context, 1, walk_context);
4005 if (context == NULL) {
4006 sprintf(str_buf, "malloc(context) failed (%s)", strerror(errno));
4007 sv_setpv(*err_str_svp, str_buf);
4008 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
4009 goto err;
4010 }
4011
4012 /* Store the Perl callback and session reference in the context. */
4013 context->perl_cb = newSVsv(perl_callback);
4014 context->sess_ref = newSVsv(sess_ref);
4015
4016 DBPRT(3,(DBOUT "bulkwalk: sess_ref = 0x%p, sess_ptr_sv = 0x%p, ss = 0x%p\n",
4017 sess_ref, sess_ptr_sv, ss));
4018
4019 context->getlabel_f = NO_FLAGS; /* long/numeric name flags */
4020 context->sprintval_f = USE_BASIC; /* Don't do fancy printing */
4021 context->req_oids = NULL; /* List of oid's requested */
4022 context->repbase = NULL; /* Repeaters in req_oids[] */
4023 context->reqbase = NULL; /* Ptr to start of requests */
4024 context->nreq_oids = 0; /* Number of oid's in list */
4025 context->repeaters = 0; /* Repeater count (see below) */
4026 context->non_reps = nonrepeaters; /* Non-repeater var count */
4027 context->max_reps = maxrepetitions; /* Max repetition/var count */
4028 context->pkts_exch = 0; /* Packets exchanged in walk */
4029 context->oid_total = 0; /* OID's received during walk */
4030 context->oid_saved = 0; /* OID's saved as results */
4031
4032 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1)))
4033 context->getlabel_f |= USE_LONG_NAMES;
4034 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1)))
4035 context->getlabel_f |= USE_NUMERIC_OIDS;
4036 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums", 8, 1)))
4037 context->sprintval_f = USE_ENUMS;
4038 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseSprintValue", 14, 1)))
4039 context->sprintval_f = USE_SPRINT_VALUE;
4040
4041 /* Set up an array of bulktbl's to hold the original list of
4042 ** requested OID's. This is used to populate the PDU's with
4043 ** oid values, to contain/sort the return values, and (through
4044 ** last_oid/last_len) to determine when the bulkwalk for each
4045 ** variable has completed.
4046 */
4047 varlist = (AV*) SvRV(varlist_ref);
4048 varlist_len = av_len(varlist) + 1; /* XXX av_len returns index of
4049 ** last element not #elements */
4050
4051 Newz(0, context->req_oids, varlist_len, bulktbl);
4052
4053 if (context->req_oids == NULL) {
4054 sprintf(str_buf, "Newz(req_oids) failed (%s)", strerror(errno));
4055 if (verbose)
4056 warn("%s", str_buf);
4057 sv_setpv(*err_str_svp, str_buf);
4058 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
4059 goto err;
4060 }
4061
4062 /* Walk through the varbind_list, parsing and copying each OID
4063 ** into a bulktbl slot in the req_oids array. Bail if there's
4064 ** some error. Create the initial packet to send out, which
4065 ** includes the non-repeaters.
4066 */
4067 DBPRT(1,(DBOUT "Building request table:\n"));
4068 for (varlist_ind = 0; varlist_ind < varlist_len; varlist_ind++) {
4069 /* Get a handle on this entry in the request table. */
4070 bt_entry = &context->req_oids[context->nreq_oids];
4071
4072 DBPRT(1,(DBOUT " request %d: ", (int) varlist_ind));
4073
4074 /* Get the request varbind from the varlist, parse it out to
4075 ** tag and index, and copy it to the req_oid[] array slots.
4076 */
4077 varbind_ref = av_fetch(varlist, varlist_ind, 0);
4078 if (!SvROK(*varbind_ref)) {
4079 sv_setpv(*err_str_svp, \
4080 (char*)snmp_api_errstring(SNMPERR_BAD_NAME));
4081 sv_setiv(*err_num_svp, SNMPERR_BAD_NAME);
4082 goto err;
4083 }
4084
4085 varbind = (AV*) SvRV(*varbind_ref);
4086 __tag2oid(__av_elem_pv(varbind, VARBIND_TAG_F, "0"),
4087 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
4088 oid_arr, &oid_arr_len, NULL, best_guess);
4089
4090 if ((oid_arr_len == 0) || (oid_arr_len > MAX_OID_LEN)) {
4091 if (verbose)
4092 warn("error: bulkwalk(): unknown object ID");
4093 sv_setpv(*err_str_svp, \
4094 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
4095 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
4096 goto err;
4097 }
4098
4099 /* Copy the now-parsed OID into the first available slot
4100 ** in the req_oids[] array. Set both the req_oid (original
4101 ** request) and the last_oid (last requested/seen oid) to
4102 ** the initial value. We build packets using last_oid (see
4103 ** below), so initialize last_oid to the initial request.
4104 */
4105 Copy((void *)oid_arr, (void *)bt_entry->req_oid,
4106 oid_arr_len, oid);
4107 Copy((void *)oid_arr, (void *)bt_entry->last_oid,
4108 oid_arr_len, oid);
4109
4110 bt_entry->req_len = oid_arr_len;
4111 bt_entry->last_len = oid_arr_len;
4112
4113 /* Adjust offset to and count of repeaters. Note non-repeater
4114 ** OID's in the list, if appropriate.
4115 */
4116 if (varlist_ind >= context->non_reps) {
4117
4118 /* Store a pointer to the first repeater value. */
4119 if (context->repbase == NULL)
4120 context->repbase = bt_entry;
4121
4122 context->repeaters ++;
4123
4124 } else {
4125 bt_entry->norepeat = 1;
4126 DBPRT(1,(DBOUT "HERE 1\n"));
4127 DBPRT(1,(DBOUT "(nonrepeater) "));
4128 }
4129
4130 /* Initialize the array in which to hold the Varbinds to be
4131 ** returned for the OID or subtree.
4132 */
4133 if ((bt_entry->vars = (AV*) newAV()) == NULL) {
4134 sv_setpv(*err_str_svp, "newAV() failed: ");
4135 sv_catpv(*err_str_svp, strerror(errno));
4136 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
4137 goto err;
4138 }
4139 DBPRT(1,(DBOUT "%s\n", __snprint_oid(oid_arr, oid_arr_len)));
4140 context->nreq_oids ++;
4141 }
4142
4143 /* Keep track of the number of outstanding requests. This lets us
4144 ** finish processing early if we're done with all requests.
4145 */
4146 context->req_remain = context->nreq_oids;
4147 DBPRT(1,(DBOUT "Total %d variable requests added\n", context->nreq_oids));
4148
4149 /* If no good variable requests were found, return an error. */
4150 if (context->nreq_oids == 0) {
4151 sv_setpv(*err_str_svp, "No variables found in varlist");
4152 sv_setiv(*err_num_svp, SNMPERR_NO_VARS);
4153 goto err;
4154 }
4155
4156 /* Note that this is a good context. This allows later callbacks
4157 ** to ignore re-sent PDU's that correspond to completed (and hence
4158 ** destroyed) bulkwalk contexts.
4159 */
4160 _context_add(context);
4161
4162 /* For asynchronous bulkwalk requests, all we have to do at this
4163 ** point is enqueue the asynchronous GETBULK request with our
4164 ** bulkwalk-specific callback and return. Remember that the
4165 ** bulkwalk_send_pdu() function returns the reqid cast to an
4166 ** snmp_pdu pointer, or NULL on failure. Return undef if the
4167 ** initial send fails; bulkwalk_send_pdu() takes care of setting
4168 ** the various error values.
4169 **
4170 ** From here, the callbacks do all the work, including sending
4171 ** requests for variables and handling responses. The caller's
4172 ** callback will be invoked as soon as the walk completes.
4173 */
4174 if (SvTRUE(perl_callback)) {
4175 DBPRT(1,(DBOUT "Starting asynchronous bulkwalk...\n"));
4176
4177 pdu = _bulkwalk_send_pdu(context);
4178
4179 if (pdu == NULL) {
4180 DBPRT(1,(DBOUT "Initial asynchronous send failed...\n"));
4181 XSRETURN_UNDEF;
4182 }
4183
4184 /* Sent okay... Return the request ID in 'pdu' as an SvIV. */
4185 DBPRT(1,(DBOUT "Okay, request id is %ld\n", (long)(intptr_t)pdu));
4186 /* XSRETURN_IV((intptr_t)pdu); */
4187 XPUSHs(sv_2mortal(newSViv((IV)pdu)));
4188 XSRETURN(1);
4189 }
4190
4191 /* For synchronous bulkwalk, we perform the basic send/receive
4192 ** iteration right here. Once the walk has been completed, the
4193 ** bulkwalk_finish() function will push the return values onto
4194 ** the Perl call stack, and we return.
4195 */
4196 DBPRT(1,(DBOUT "Starting synchronous bulkwalk...\n"));
4197
4198 while (!(okay = _bulkwalk_done(context))) {
4199
4200 /* Send a request for the next batch of variables. */
4201 DBPRT(1, (DBOUT "Building %s GETBULK bulkwalk PDU (%d)...\n",
4202 context->pkts_exch ? "next" : "first",
4203 context->pkts_exch));
4204 pdu = _bulkwalk_send_pdu(context);
4205
4206 /* If the request failed, consider the walk done. */
4207 if (pdu == NULL) {
4208 DBPRT(1,(DBOUT "bulkwalk_send_pdu() failed!\n"));
4209 break;
4210 }
4211
4212 /* Handle the variables in this response packet. Break out
4213 ** of the loop if an error occurs or no variables are found
4214 ** in the response.
4215 */
4216 if ((i = _bulkwalk_recv_pdu(context, pdu)) <= 0) {
4217 DBPRT(2,(DBOUT "bulkwalk_recv_pdu() returned %d (error/empty)\n", i));
4218 goto err;
4219 }
4220
4221 /* Free the returned pdu. Don't bother to do this for the async
4222 ** case, since the SNMP callback mechanism itself does the free
4223 ** for us.
4224 */
4225 snmp_free_pdu(pdu);
4226
4227 /* And loop. The call to bulkwalk_done() sets the ignore flags
4228 ** for any completed request subtrees. Next time around, they
4229 ** won't be added to the request sent to the agent.
4230 */
4231 continue;
4232 }
4233
4234 DBPRT(1, (DBOUT "Bulkwalk done... calling bulkwalk_finish(%s)...\n",
4235 okay ? "okay" : "error"));
4236 npushed = _bulkwalk_finish(context, okay);
4237
4238 DBPRT(2,(DBOUT "Returning %d values on the stack.\n", npushed));
4239 XSRETURN(npushed);
4240
4241 /* Handle error cases and clean up after ourselves. */
4242 err:
4243 if (context) {
4244 if (context->req_oids && context->nreq_oids) {
4245 bt_entry = context->req_oids;
4246 for (i = 0; i < context->nreq_oids; i++, bt_entry++)
4247 av_clear(bt_entry->vars);
4248 }
4249 if (context->req_oids)
4250 Safefree(context->req_oids);
4251 Safefree(context);
4252 }
4253 if (pdu)
4254 snmp_free_pdu(pdu);
4255
4256 XSRETURN_UNDEF;
4257 }
4258
4259
4260 void
4261 snmp_trapV1(sess_ref,enterprise,agent,generic,specific,uptime,varlist_ref)
4262 SV * sess_ref
4263 char * enterprise
4264 char * agent
4265 int generic
4266 int specific
4267 long uptime
4268 SV * varlist_ref
4269 PPCODE:
4270 {
4271 AV *varlist;
4272 SV **varbind_ref;
4273 SV **varbind_val_f;
4274 AV *varbind;
4275 I32 varlist_len;
4276 I32 varlist_ind;
4277 void *ss;
4278 netsnmp_pdu *pdu = NULL;
4279 struct tree *tp;
4280 oid *oid_arr;
4281 size_t oid_arr_len = MAX_OID_LEN;
4282 SV **sess_ptr_sv;
4283 SV **err_str_svp;
4284 SV **err_num_svp;
4285 SV **err_ind_svp;
4286 int type;
4287 int res;
4288 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
4289 int use_enums = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums",8,1));
4290 struct enum_list *ep;
4291 int best_guess;
4292
4293 New (0, oid_arr, MAX_OID_LEN, oid);
4294
4295 if (oid_arr && SvROK(sess_ref)) {
4296
4297 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4298 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4299 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
4300 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
4301 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
4302 sv_setpv(*err_str_svp, "");
4303 sv_setiv(*err_num_svp, 0);
4304 sv_setiv(*err_ind_svp, 0);
4305 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
4306
4307 pdu = snmp_pdu_create(SNMP_MSG_TRAP);
4308
4309 if (SvROK(varlist_ref)) {
4310 varlist = (AV*) SvRV(varlist_ref);
4311 varlist_len = av_len(varlist);
4312 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
4313 varbind_ref = av_fetch(varlist, varlist_ind, 0);
4314 if (SvROK(*varbind_ref)) {
4315 varbind = (AV*) SvRV(*varbind_ref);
4316
4317 tp=__tag2oid(__av_elem_pv(varbind, VARBIND_TAG_F, NULL),
4318 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
4319 oid_arr, &oid_arr_len, &type, best_guess);
4320
4321 if (oid_arr_len == 0) {
4322 if (verbose)
4323 warn("error:trap: unable to determine oid for object");
4324 goto err;
4325 }
4326
4327 if (type == TYPE_UNKNOWN) {
4328 type = __translate_appl_type(
4329 __av_elem_pv(varbind, VARBIND_TYPE_F, NULL));
4330 if (type == TYPE_UNKNOWN) {
4331 if (verbose)
4332 warn("error:trap: no type found for object");
4333 goto err;
4334 }
4335 }
4336
4337 varbind_val_f = av_fetch(varbind, VARBIND_VAL_F, 0);
4338
4339 if (type==TYPE_INTEGER && use_enums && tp && tp->enums) {
4340 for(ep = tp->enums; ep; ep = ep->next) {
4341 if (varbind_val_f && SvOK(*varbind_val_f) &&
4342 !strcmp(ep->label, SvPV(*varbind_val_f,na))) {
4343 sv_setiv(*varbind_val_f, ep->value);
4344 break;
4345 }
4346 }
4347 }
4348
4349 res = __add_var_val_str(pdu, oid_arr, oid_arr_len,
4350 (varbind_val_f && SvOK(*varbind_val_f) ?
4351 SvPV(*varbind_val_f,na):NULL),
4352 (varbind_val_f && SvPOK(*varbind_val_f) ?
4353 SvCUR(*varbind_val_f):0),
4354 type);
4355
4356 if(res == FAILURE) {
4357 if(verbose) warn("error:trap: adding varbind");
4358 goto err;
4359 }
4360
4361 } /* if var_ref is ok */
4362 } /* for all the vars */
4363 }
4364
4365 pdu->enterprise = (oid *)netsnmp_malloc(MAX_OID_LEN * sizeof(oid));
4366 tp = __tag2oid(enterprise,NULL, pdu->enterprise,
4367 &pdu->enterprise_length, NULL, best_guess);
4368 if (pdu->enterprise_length == 0) {
4369 if (verbose) warn("error:trap:invalid enterprise id: %s", enterprise);
4370 goto err;
4371 }
4372 /* If agent is given then set the v1-TRAP specific
4373 agent-address field to that. Otherwise set it to
4374 our address. */
4375 if (agent && strlen(agent)) {
4376 if (0 > netsnmp_gethostbyname_v4(agent,
4377 (in_addr_t *)pdu->agent_addr)){
4378 if (verbose)
4379 warn("error:trap:invalid agent address: %s", agent);
4380 goto err;
4381 }
4382 } else {
4383 *((in_addr_t *)pdu->agent_addr) = get_myaddr();
4384 }
4385 pdu->trap_type = generic;
4386 pdu->specific_type = specific;
4387 pdu->time = uptime;
4388
4389 if(api_mode == SNMP_API_SINGLE)
4390 {
4391 if(snmp_sess_send(ss,pdu) == 0)
4392 snmp_free_pdu(pdu);
4393 } else {
4394 if (snmp_send(ss, pdu) == 0)
4395 snmp_free_pdu(pdu);
4396 }
4397 XPUSHs(sv_2mortal(newSVpv(ZERO_BUT_TRUE,0)));
4398 } else {
4399 err:
4400 XPUSHs(&sv_undef); /* no mem or bad args */
4401 if (pdu) snmp_free_pdu(pdu);
4402 }
4403 Safefree(oid_arr);
4404 }
4405
4406
4407 void
4408 snmp_trapV2(sess_ref,uptime,trap_oid,varlist_ref)
4409 SV * sess_ref
4410 char * uptime
4411 char * trap_oid
4412 SV * varlist_ref
4413 PPCODE:
4414 {
4415 AV *varlist;
4416 SV **varbind_ref;
4417 SV **varbind_val_f;
4418 AV *varbind;
4419 I32 varlist_len;
4420 I32 varlist_ind;
4421 void *ss;
4422 netsnmp_pdu *pdu = NULL;
4423 struct tree *tp;
4424 oid *oid_arr;
4425 size_t oid_arr_len = MAX_OID_LEN;
4426 SV **sess_ptr_sv;
4427 SV **err_str_svp;
4428 SV **err_num_svp;
4429 SV **err_ind_svp;
4430 int type;
4431 int res;
4432 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
4433 int use_enums = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums",8,1));
4434 struct enum_list *ep;
4435 int best_guess;
4436
4437 New (0, oid_arr, MAX_OID_LEN, oid);
4438
4439 if (oid_arr && SvROK(sess_ref)) {
4440
4441 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4442 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4443 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
4444 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
4445 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
4446 sv_setpv(*err_str_svp, "");
4447 sv_setiv(*err_num_svp, 0);
4448 sv_setiv(*err_ind_svp, 0);
4449 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
4450
4451 pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
4452
4453 if (SvROK(varlist_ref)) {
4454 varlist = (AV*) SvRV(varlist_ref);
4455 varlist_len = av_len(varlist);
4456 } else {
4457 varlist = NULL;
4458 varlist_len = -1;
4459 }
4460 /************************************************/
4461 res = __add_var_val_str(pdu, sysUpTime, SYS_UPTIME_OID_LEN,
4462 uptime, strlen(uptime), TYPE_TIMETICKS);
4463
4464 if(res == FAILURE) {
4465 if(verbose) warn("error:trap v2: adding sysUpTime varbind");
4466 goto err;
4467 }
4468
4469 res = __add_var_val_str(pdu, snmpTrapOID, SNMP_TRAP_OID_LEN,
4470 trap_oid ,strlen(trap_oid) ,TYPE_OBJID);
4471
4472 if(res == FAILURE) {
4473 if(verbose) warn("error:trap v2: adding snmpTrapOID varbind");
4474 goto err;
4475 }
4476
4477
4478 /******************************************************/
4479
4480 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
4481 varbind_ref = av_fetch(varlist, varlist_ind, 0);
4482 if (SvROK(*varbind_ref)) {
4483 varbind = (AV*) SvRV(*varbind_ref);
4484
4485 tp=__tag2oid(__av_elem_pv(varbind, VARBIND_TAG_F,NULL),
4486 __av_elem_pv(varbind, VARBIND_IID_F,NULL),
4487 oid_arr, &oid_arr_len, &type, best_guess);
4488
4489 if (oid_arr_len == 0) {
4490 if (verbose)
4491 warn("error:trap v2: unable to determine oid for object");
4492 goto err;
4493 }
4494
4495 if (type == TYPE_UNKNOWN) {
4496 type = __translate_appl_type(
4497 __av_elem_pv(varbind, VARBIND_TYPE_F, NULL));
4498 if (type == TYPE_UNKNOWN) {
4499 if (verbose)
4500 warn("error:trap v2: no type found for object");
4501 goto err;
4502 }
4503 }
4504
4505 varbind_val_f = av_fetch(varbind, VARBIND_VAL_F, 0);
4506
4507 if (type==TYPE_INTEGER && use_enums && tp && tp->enums) {
4508 for(ep = tp->enums; ep; ep = ep->next) {
4509 if (varbind_val_f && SvOK(*varbind_val_f) &&
4510 !strcmp(ep->label, SvPV(*varbind_val_f,na))) {
4511 sv_setiv(*varbind_val_f, ep->value);
4512 break;
4513 }
4514 }
4515 }
4516
4517 res = __add_var_val_str(pdu, oid_arr, oid_arr_len,
4518 (varbind_val_f && SvOK(*varbind_val_f) ?
4519 SvPV(*varbind_val_f,na):NULL),
4520 (varbind_val_f && SvPOK(*varbind_val_f) ?
4521 SvCUR(*varbind_val_f):0),
4522 type);
4523
4524 if(res == FAILURE) {
4525 if(verbose) warn("error:trap v2: adding varbind");
4526 goto err;
4527 }
4528
4529 } /* if var_ref is ok */
4530 } /* for all the vars */
4531
4532 if(api_mode == SNMP_API_SINGLE)
4533 {
4534 if (snmp_sess_send(ss, pdu) == 0)
4535 snmp_free_pdu(pdu);
4536 } else {
4537 if (snmp_send(ss, pdu) == 0)
4538 snmp_free_pdu(pdu);
4539 }
4540
4541 XPUSHs(sv_2mortal(newSVpv(ZERO_BUT_TRUE,0)));
4542 } else {
4543 err:
4544 XPUSHs(&sv_undef); /* no mem or bad args */
4545 if (pdu) snmp_free_pdu(pdu);
4546 }
4547 Safefree(oid_arr);
4548 }
4549
4550
4551
4552 void
4553 snmp_inform(sess_ref,uptime,trap_oid,varlist_ref,perl_callback)
4554 SV * sess_ref
4555 char * uptime
4556 char * trap_oid
4557 SV * varlist_ref
4558 SV * perl_callback
4559 PPCODE:
4560 {
4561 AV *varlist;
4562 SV **varbind_ref;
4563 SV **varbind_val_f;
4564 AV *varbind;
4565 I32 varlist_len;
4566 I32 varlist_ind;
4567 void *ss;
4568 netsnmp_pdu *pdu = NULL;
4569 netsnmp_pdu *response;
4570 struct tree *tp;
4571 oid *oid_arr;
4572 size_t oid_arr_len = MAX_OID_LEN;
4573 snmp_xs_cb_data *xs_cb_data;
4574 SV **sess_ptr_sv;
4575 SV **err_str_svp;
4576 SV **err_num_svp;
4577 SV **err_ind_svp;
4578 int status = 0;
4579 int type;
4580 int res;
4581 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
4582 int use_enums = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums",8,1));
4583 struct enum_list *ep;
4584 int best_guess;
4585
4586 New (0, oid_arr, MAX_OID_LEN, oid);
4587
4588 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
4589
4590 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4591 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4592 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
4593 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
4594 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
4595 sv_setpv(*err_str_svp, "");
4596 sv_setiv(*err_num_svp, 0);
4597 sv_setiv(*err_ind_svp, 0);
4598 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
4599
4600 pdu = snmp_pdu_create(SNMP_MSG_INFORM);
4601
4602 varlist = (AV*) SvRV(varlist_ref);
4603 varlist_len = av_len(varlist);
4604 /************************************************/
4605 res = __add_var_val_str(pdu, sysUpTime, SYS_UPTIME_OID_LEN,
4606 uptime, strlen(uptime), TYPE_TIMETICKS);
4607
4608 if(res == FAILURE) {
4609 if(verbose) warn("error:inform: adding sysUpTime varbind");
4610 goto err;
4611 }
4612
4613 res = __add_var_val_str(pdu, snmpTrapOID, SNMP_TRAP_OID_LEN,
4614 trap_oid ,strlen(trap_oid) ,TYPE_OBJID);
4615
4616 if(res == FAILURE) {
4617 if(verbose) warn("error:inform: adding snmpTrapOID varbind");
4618 goto err;
4619 }
4620
4621
4622 /******************************************************/
4623
4624 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
4625 varbind_ref = av_fetch(varlist, varlist_ind, 0);
4626 if (SvROK(*varbind_ref)) {
4627 varbind = (AV*) SvRV(*varbind_ref);
4628
4629 tp=__tag2oid(__av_elem_pv(varbind, VARBIND_TAG_F,NULL),
4630 __av_elem_pv(varbind, VARBIND_IID_F,NULL),
4631 oid_arr, &oid_arr_len, &type, best_guess);
4632
4633 if (oid_arr_len == 0) {
4634 if (verbose)
4635 warn("error:inform: unable to determine oid for object");
4636 goto err;
4637 }
4638
4639 if (type == TYPE_UNKNOWN) {
4640 type = __translate_appl_type(
4641 __av_elem_pv(varbind, VARBIND_TYPE_F, NULL));
4642 if (type == TYPE_UNKNOWN) {
4643 if (verbose)
4644 warn("error:inform: no type found for object");
4645 goto err;
4646 }
4647 }
4648
4649 varbind_val_f = av_fetch(varbind, VARBIND_VAL_F, 0);
4650
4651 if (type==TYPE_INTEGER && use_enums && tp && tp->enums) {
4652 for(ep = tp->enums; ep; ep = ep->next) {
4653 if (varbind_val_f && SvOK(*varbind_val_f) &&
4654 !strcmp(ep->label, SvPV(*varbind_val_f,na))) {
4655 sv_setiv(*varbind_val_f, ep->value);
4656 break;
4657 }
4658 }
4659 }
4660
4661 res = __add_var_val_str(pdu, oid_arr, oid_arr_len,
4662 (varbind_val_f && SvOK(*varbind_val_f) ?
4663 SvPV(*varbind_val_f,na):NULL),
4664 (varbind_val_f && SvPOK(*varbind_val_f) ?
4665 SvCUR(*varbind_val_f):0),
4666 type);
4667
4668 if(res == FAILURE) {
4669 if(verbose) warn("error:inform: adding varbind");
4670 goto err;
4671 }
4672
4673 } /* if var_ref is ok */
4674 } /* for all the vars */
4675
4676
4677 if (SvTRUE(perl_callback)) {
4678 xs_cb_data =
4679 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
4680 xs_cb_data->perl_cb = newSVsv(perl_callback);
4681 xs_cb_data->sess_ref = newRV_inc(SvRV(sess_ref));
4682
4683 if(api_mode == SNMP_API_SINGLE)
4684 {
4685 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
4686 (void*)xs_cb_data);
4687 } else {
4688 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
4689 (void*)xs_cb_data);
4690 }
4691 if (status != 0) {
4692 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
4693 } else {
4694 snmp_free_pdu(pdu);
4695 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
4696 XPUSHs(&sv_undef);
4697 }
4698 goto done;
4699 }
4700
4701 status = __send_sync_pdu(ss, pdu, &response,
4702 NO_RETRY_NOSUCH,
4703 *err_str_svp, *err_num_svp,
4704 *err_ind_svp);
4705
4706 if (response) snmp_free_pdu(response);
4707
4708 if (status) {
4709 XPUSHs(&sv_undef);
4710 } else {
4711 XPUSHs(sv_2mortal(newSVpv(ZERO_BUT_TRUE,0)));
4712 }
4713 } else {
4714 err:
4715 XPUSHs(&sv_undef); /* no mem or bad args */
4716 if (pdu) snmp_free_pdu(pdu);
4717 }
4718 done:
4719 Safefree(oid_arr);
4720 }
4721
4722
4723
4724 char *
•4725 snmp_get_sec_engine_id(sess_ref)
4726 SV * sess_ref
4727 CODE:
4728 {
4729 RETVAL = NULL;
4730 if (SvROK(sess_ref)) {
4731 SV **sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4732 SnmpSession *ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4733 if (ss->securityEngineIDLen > 0) {
4734 binary_to_hex(ss->securityEngineID,
4735 ss->securityEngineIDLen,
4736 &RETVAL);
4737 }
4738 }
4739 }
4740 OUTPUT:
4741 RETVAL
4742 CLEANUP:
4743 netsnmp_free(RETVAL);
4744
4745
4746 char *
4747 snmp_get_context_engine_id(sess_ref)
4748 SV * sess_ref
4749 CODE:
4750 {
4751 RETVAL = NULL;
4752 if (SvROK(sess_ref)) {
4753 SV **sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4754 SnmpSession *ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4755 if (ss->contextEngineIDLen > 0) {
4756 binary_to_hex(ss->contextEngineID,
4757 ss->contextEngineIDLen,
4758 &RETVAL);
4759 }
4760 }
4761 }
4762 OUTPUT:
4763 RETVAL
4764 CLEANUP:
4765 netsnmp_free(RETVAL);
4766
4767
4768 char *
4769 snmp_get_type(tag, best_guess)
4770 char * tag
4771 int best_guess
4772 CODE:
4773 {
4774 struct tree *tp = NULL;
4775 static char type_str[MAX_TYPE_NAME_LEN];
4776 char *ret = NULL;
4777
4778 if (tag && *tag) tp = __tag2oid(tag, NULL, NULL, NULL, NULL, best_guess);
4779 if (tp) __get_type_str(tp->type, ret = type_str);
4780 RETVAL = ret;
4781 }
4782 OUTPUT:
4783 RETVAL
4784
4785
4786 void
4787 snmp_dump_packet(flag)
4788 int flag
4789 CODE:
4790 {
4791 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
4792 NETSNMP_DS_LIB_DUMP_PACKET, flag);
4793 }
4794
4795
4796 char *
4797 snmp_map_enum(tag, val, iflag, best_guess)
4798 char * tag
4799 char * val
4800 int iflag
4801 int best_guess
4802 CODE:
4803 {
4804 struct tree *tp = NULL;
4805 struct enum_list *ep;
4806 static char str_buf[STR_BUF_SIZE];
4807 int ival;
4808
4809 RETVAL = NULL;
4810
4811 if (tag && *tag) tp = __tag2oid(tag, NULL, NULL, NULL, NULL, best_guess);
4812
4813 if (tp) {
4814 if (iflag) {
4815 ival = atoi(val);
4816 for(ep = tp->enums; ep; ep = ep->next) {
4817 if (ep->value == ival) {
4818 RETVAL = ep->label;
4819 break;
4820 }
4821 }
4822 } else {
4823 for(ep = tp->enums; ep; ep = ep->next) {
4824 if (strEQ(ep->label, val)) {
4825 sprintf(str_buf,"%d", ep->value);
4826 RETVAL = str_buf;
4827 break;
4828 }
4829 }
4830 }
4831 }
4832 }
4833 OUTPUT:
4834 RETVAL
4835
4836 #define SNMP_XLATE_MODE_OID2TAG 1
4837 #define SNMP_XLATE_MODE_TAG2OID 0
4838
4839 char *
4840 snmp_translate_obj(var,mode,use_long,auto_init,best_guess,include_module_name)
4841 char * var
4842 int mode
4843 int use_long
4844 int auto_init
4845 int best_guess
4846 int include_module_name
4847 CODE:
4848 {
4849 static char str_buf[STR_BUF_SIZE];
4850 char str_buf_temp[STR_BUF_SIZE];
4851 oid oid_arr[MAX_OID_LEN];
4852 size_t oid_arr_len = MAX_OID_LEN;
4853 char * label;
4854 char * iid;
4855 int status = FAILURE;
4856 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
4857 struct tree *module_tree = NULL;
4858 char modbuf[256];
4859 int old_format; /* Current NETSNMP_DS_LIB_OID_OUTPUT_FORMAT */
4860
4861 str_buf[0] = '\0';
4862 str_buf_temp[0] = '\0';
4863
4864 if (auto_init)
4865 netsnmp_init_mib(); /* vestigial */
4866
4867 /* Save old output format and set to FULL so long_names works */
4868 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
4869 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, NETSNMP_OID_OUTPUT_FULL);
4870
4871 switch (mode) {
4872 case SNMP_XLATE_MODE_TAG2OID:
4873 if (!__tag2oid(var, NULL, oid_arr, &oid_arr_len, NULL, best_guess)) {
4874 if (verbose) warn("error:snmp_translate_obj:Unknown OID %s\n",var);
4875 } else {
4876 status = __snprint_num_objid(str_buf, sizeof(str_buf), oid_arr, oid_arr_len);
4877 }
4878 break;
4879 case SNMP_XLATE_MODE_OID2TAG:
4880 oid_arr_len = 0;
4881 __concat_oid_str(oid_arr, &oid_arr_len, var);
4882 snprint_objid(str_buf_temp, sizeof(str_buf_temp), oid_arr, oid_arr_len);
4883
4884 if (!use_long) {
4885 label = NULL; iid = NULL;
4886 if (((status=__get_label_iid(str_buf_temp,
4887 &label, &iid, NO_FLAGS)) == SUCCESS)
4888 && label) {
4889 strlcpy(str_buf_temp, label, sizeof(str_buf_temp));
4890 if (iid && *iid) {
4891 strlcat(str_buf_temp, ".", sizeof(str_buf_temp));
4892 strlcat(str_buf_temp, iid, sizeof(str_buf_temp));
4893 }
4894 }
4895 }
4896
4897 /* Prepend modulename:: if enabled */
4898 if (include_module_name) {
4899 module_tree = get_tree (oid_arr, oid_arr_len, get_tree_head());
4900 if (module_tree) {
4901 if (strcmp(module_name(module_tree->modid, modbuf), "#-1") ) {
4902 strcat(str_buf, modbuf);
4903 strcat(str_buf, "::");
4904 }
4905 else {
4906 strcat(str_buf, "UNKNOWN::");
4907 }
4908 }
4909 }
4910 strcat(str_buf, str_buf_temp);
4911
4912 break;
4913 default:
4914 if (verbose) warn("snmp_translate_obj:unknown translation mode: %d\n", mode);
4915 }
4916 if (*str_buf) {
4917 RETVAL = str_buf;
4918 } else {
4919 RETVAL = NULL;
4920 }
4921 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, old_format);
4922 }
4923 OUTPUT:
4924 RETVAL
4925
4926 void
4927 snmp_set_replace_newer(val)
4928 int val
4929 CODE:
4930 {
4931 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
4932 NETSNMP_DS_LIB_MIB_REPLACE, val);
4933 }
4934
4935 void
4936 snmp_set_save_descriptions(val)
4937 int val
4938 CODE:
4939 {
4940 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
4941 NETSNMP_DS_LIB_SAVE_MIB_DESCRS, val);
4942 }
4943
4944 void
4945 snmp_set_debugging(val)
4946 int val
4947 CODE:
4948 {
4949 snmp_set_do_debugging(val);
4950 }
4951
4952 void
4953 snmp_register_debug_tokens(tokens)
4954 char *tokens
4955 CODE:
4956 {
4957 debug_register_tokens(tokens);
4958 snmp_set_do_debugging(1);
4959 }
4960
4961 void
4962 snmp_debug_internals(val)
4963 int val
4964 CODE:
4965 {
4966 #ifdef DEBUGGING
4967 _debug_level = val;
4968 #else
4969 val++;
4970 #endif /* DEBUGGING */
4971 }
4972
4973
4974 void
4975 snmp_mib_toggle_options(options)
4976 char *options
4977 CODE:
4978 {
4979 snmp_mib_toggle_options(options);
4980 }
4981
4982 void
4983 snmp_sock_cleanup()
4984 CODE:
4985 {
4986 SOCK_CLEANUP;
4987 }
4988
4989 void
4990 snmp_mainloop_finish()
4991 CODE:
4992 {
4993 mainloop_finish = 1;
4994 }
4995
4996
4997 #-----------------------------------------------------------------------------
4998 # Note: ss=(SnmpSession*)NULL is so &SNMP::MainLoop() can still be called
4999 # without a sess handler argument, this way I'm not breaking anyone's old code
5000 #
5001 # see MainLoop() in SNMP.pm for more details
5002 #-----------------------------------------------------------------------------
5003 void
5004 snmp_main_loop(timeout_sec,timeout_usec,perl_callback,ss=(SnmpSession*)NULL)
5005 int timeout_sec
5006 int timeout_usec
5007 SV * perl_callback
5008 void * ss
5009 CODE:
5010 {
5011 int numfds, fd_count;
5012 fd_set fdset;
5013 struct timeval time_val, *tvp;
5014 struct timeval last_time, *ltvp;
5015 struct timeval ctimeout, *ctvp;
5016 struct timeval interval, *itvp;
5017 int block;
5018 SV *cb;
5019
5020 mainloop_finish = 0;
5021
5022 itvp = &interval;
5023 itvp->tv_sec = timeout_sec;
5024 itvp->tv_usec = timeout_usec;
5025 ctvp = &ctimeout;
5026 ctvp->tv_sec = -1;
5027 ctvp->tv_usec = 0;
5028 ltvp = &last_time;
5029 gettimeofday(ltvp,(struct timezone*)0);
5030 timersub(ltvp,itvp,ltvp);
5031 while (1) {
5032 numfds = 0;
5033 FD_ZERO(&fdset);
5034 block = 1;
5035 tvp = &time_val;
5036 timerclear(tvp);
5037 if(api_mode == SNMP_API_SINGLE)
5038 {
5039 snmp_sess_select_info(ss,&numfds, &fdset, tvp, &block);
5040 } else {
5041 snmp_select_info(&numfds, &fdset, tvp, &block);
5042 }
5043 __recalc_timeout(tvp,ctvp,ltvp,itvp,&block);
5044 # printf("pre-select: numfds = %ld, block = %ld\n", numfds, block);
5045 if (block == 1) tvp = NULL; /* block without timeout */
5046 fd_count = select(numfds, &fdset, 0, 0, tvp);
5047 #printf("post-select: fd_count = %ld,block = %ld\n",fd_count,block);
5048 if (fd_count > 0) {
5049 ENTER;
5050 SAVETMPS;
5051 if(api_mode == SNMP_API_SINGLE)
5052 {
5053 snmp_sess_read(ss, &fdset);
5054 } else {
5055 snmp_read(&fdset);
5056 }
5057 FREETMPS;
5058 LEAVE;
5059
5060 } else switch(fd_count) {
5061 case 0:
5062 SPAGAIN;
5063 ENTER;
5064 SAVETMPS;
5065 if(api_mode == SNMP_API_SINGLE)
5066 {
5067 snmp_sess_timeout( ss );
5068 } else {
5069 snmp_timeout();
5070 }
5071 if (!timerisset(ctvp)) {
5072 if (SvTRUE(perl_callback)) {
5073 /* sv_2mortal(perl_callback); */
5074 cb = __push_cb_args(perl_callback, NULL);
5075 __call_callback(cb, G_DISCARD);
5076 ctvp->tv_sec = -1;
5077
5078 } else {
5079 FREETMPS;
5080 LEAVE;
5081 goto done;
5082 }
5083 }
5084 FREETMPS;
5085 LEAVE;
5086 break;
5087 case -1:
5088 if (errno == EINTR) {
5089 continue;
5090 } else {
5091 /* snmp_set_detail(strerror(errno)); */
5092 /* snmp_errno = SNMPERR_GENERR; */
5093 }
5094 default:;
5095 }
5096
5097 /* A call to snmp_mainloop_finish() in the callback sets the
5098 ** mainloop_finish flag. Exit the loop after the callback returns.
5099 */
5100 if (mainloop_finish)
5101 goto done;
5102
5103 }
5104 done:
5105 return;
5106 }
5107
5108
5109 void
5110 snmp_get_select_info()
5111 PPCODE:
5112 {
5113 int numfds;
5114 fd_set fdset;
5115 struct timeval time_val, *tvp;
5116 int block;
5117 int i;
5118
5119 numfds = 0;
5120 block = 1;
5121 tvp = &time_val;
5122 FD_ZERO(&fdset);
5123 snmp_select_info(&numfds, &fdset, tvp, &block);
5124 XPUSHs(sv_2mortal(newSViv(block)));
5125 if(block){
5126 XPUSHs(sv_2mortal(newSViv(0)));
5127 XPUSHs(sv_2mortal(newSViv(0)));
5128 } else {
5129 XPUSHs(sv_2mortal(newSViv(tvp->tv_sec)));
5130 XPUSHs(sv_2mortal(newSViv(tvp->tv_usec)));
5131 }
5132 if ( numfds ) {
5133 for(i=0; i<numfds ; i++) {
5134 if(FD_ISSET(i, &fdset)){
5135 XPUSHs(sv_2mortal(newSViv(i)));
5136 }
5137 }
5138 } else {
5139 XPUSHs(&sv_undef); /* no mem or bad args */
5140 }
5141 }
5142
5143 void
5144 snmp_read_on_fd(fd)
5145 int fd
5146 CODE:
5147 {
5148 fd_set fdset;
5149
5150 FD_ZERO(&fdset);
5151 FD_SET(fd, &fdset);
5152
5153 snmp_read(&fdset);
5154 }
5155
5156 void
5157 snmp_check_timeout()
5158 CODE:
5159 {
5160 snmp_timeout();
5161 }
5162
5163 MODULE = SNMP PACKAGE = SNMP::MIB::NODE PREFIX = snmp_mib_node_
5164
5165 SV *
5166 snmp_mib_node_TIEHASH(cl,key,tp=0)
5167 char * cl
5168 char * key
5169 IV tp
5170 CODE:
5171 {
5172 __libraries_init("perl");
5173 if (!tp) tp = (IV)__tag2oid(key, NULL, NULL, NULL, NULL,0);
5174 if (tp) {
5175 RETVAL = sv_setref_iv(newSV(0), cl, tp);
5176 } else {
5177 RETVAL = &sv_undef;
5178 }
5179 }
5180 OUTPUT:
5181 RETVAL
5182
5183
5184 SV *
5185 snmp_mib_node_FETCH(tp_ref, key)
5186 SV * tp_ref
5187 char * key
5188 CODE:
5189 {
5190 char c = *key;
5191 char str_buf[STR_BUF_SIZE];
5192 SnmpMibNode *tp = NULL, *tptmp = NULL;
5193 struct index_list *ip;
5194 struct enum_list *ep;
5195 struct range_list *rp;
5196 struct varbind_list *vp;
5197 struct module *mp;
5198 SV *child_list_aref, *next_node_href, *mib_tied_href = NULL;
5199 SV **nn_hrefp;
5200 HV *mib_hv, *enum_hv, *range_hv;
5201 AV *index_av, *varbind_av, *ranges_av;
5202 MAGIC *mg = NULL;
5203 SV *ret = NULL;
5204
5205 if (SvROK(tp_ref)) tp = (SnmpMibNode*)SvIV((SV*)SvRV(tp_ref));
5206
5207 ret = newSV(0);
5208 if (tp)
5209 switch (c) {
5210 case 'a': /* access */
5211 if (strncmp("access", key, strlen(key)) == 0) {
5212 switch (tp->access) {
5213 case MIB_ACCESS_READONLY:
5214 sv_setpv(ret,"ReadOnly");
5215 break;
5216 case MIB_ACCESS_READWRITE:
5217 sv_setpv(ret,"ReadWrite");
5218 break;
5219 case MIB_ACCESS_WRITEONLY:
5220 sv_setpv(ret,"WriteOnly");
5221 break;
5222 case MIB_ACCESS_NOACCESS:
5223 sv_setpv(ret,"NoAccess");
5224 break;
5225 case MIB_ACCESS_NOTIFY:
5226 sv_setpv(ret,"Notify");
5227 break;
5228 case MIB_ACCESS_CREATE:
5229 sv_setpv(ret,"Create");
5230 break;
5231 default:
5232 break;
5233 }
5234 } else if (strncmp("augments", key, strlen(key)) == 0) {
5235 sv_setpv(ret,tp->augments);
5236 }
5237 break;
5238 case 'c': /* children */
5239 if (strncmp("children", key, strlen(key))) break;
5240 child_list_aref = newRV((SV*)newAV());
5241 for (tp = tp->child_list; tp; tp = tp->next_peer) {
5242 mib_hv = perl_get_hv("SNMP::MIB", FALSE);
5243 if (SvMAGICAL(mib_hv)) mg = mg_find((SV*)mib_hv, 'P');
5244 if (mg) mib_tied_href = (SV*)mg->mg_obj;
5245 next_node_href = newRV((SV*)newHV());
5246 __tp_sprint_num_objid(str_buf, sizeof(str_buf), tp);
5247 nn_hrefp = hv_fetch((HV*)SvRV(mib_tied_href),
5248 str_buf, strlen(str_buf), 1);
5249 if (!SvROK(*nn_hrefp)) {
5250 sv_setsv(*nn_hrefp, next_node_href);
5251 ENTER ;
5252 SAVETMPS ;
5253 PUSHMARK(sp) ;
5254 XPUSHs(SvRV(*nn_hrefp));
5255 XPUSHs(sv_2mortal(newSVpv("SNMP::MIB::NODE",0)));
5256 XPUSHs(sv_2mortal(newSVpv(str_buf,0)));
5257 XPUSHs(sv_2mortal(newSViv((IV)tp)));
5258 PUTBACK ;
5259 perl_call_pv("SNMP::_tie",G_VOID);
5260 /* pp_tie(ARGS); */
5261 SPAGAIN ;
5262 FREETMPS ;
5263 LEAVE ;
5264 } /* if SvROK */
5265 av_push((AV*)SvRV(child_list_aref), *nn_hrefp);
5266 } /* for child_list */
5267 sv_setsv(ret, child_list_aref);
5268 break;
5269 case 'v':
5270 if (strncmp("varbinds", key, strlen(key))) break;
5271 varbind_av = newAV();
5272 for (vp = tp->varbinds; vp; vp = vp->next) {
5273 av_push(varbind_av, newSVpv((vp->vblabel),strlen(vp->vblabel)));
5274 }
5275 sv_setsv(ret, newRV((SV*)varbind_av));
5276 break;
5277 case 'd': /* description */
5278 if (strncmp("description", key, strlen(key))) {
5279 if(!(strncmp("defaultValue",key,strlen(key)))) {
5280 /* We're looking at defaultValue */
5281 sv_setpv(ret, tp->defaultValue);
5282 break;
5283 } /* end if */
5284 } /* end if */
5285 /* we must be looking at description */
5286 sv_setpv(ret,tp->description);
5287 break;
5288 case 'i': /* indexes, implied */
5289 if (tp->augments) {
5290 clear_tree_flags(get_tree_head());
5291 tptmp = find_best_tree_node(tp->augments, get_tree_head(), NULL);
5292 if (tptmp == NULL) {
5293 tptmp = tp;
5294 }
5295 } else {
5296 tptmp = tp;
5297 }
5298 if (strcmp("implied", key) == 0) {
5299 /* only the last index can be implied */
5300 int isimplied = 0;
5301 if (tptmp && tptmp->indexes) {
5302 for(ip=tptmp->indexes; ip->next; ip = ip->next) {
5303 }
5304 isimplied = ip->isimplied;
5305 }
5306 sv_setiv(ret, isimplied);
5307 break;
5308 }
5309 if (strncmp("indexes", key, strlen(key))) break;
5310 index_av = newAV();
5311 if (tptmp)
5312 for(ip=tptmp->indexes; ip != NULL; ip = ip->next) {
5313 av_push(index_av,newSVpv((ip->ilabel),strlen(ip->ilabel)));
5314 }
5315 sv_setsv(ret, newRV((SV*)index_av));
5316 break;
5317 case 'l': /* label */
5318 if (strncmp("label", key, strlen(key))) break;
5319 sv_setpv(ret,tp->label);
5320 break;
5321 case 'm': /* moduleID */
5322 if (strncmp("moduleID", key, strlen(key))) break;
5323 mp = find_module(tp->modid);
5324 if (mp) sv_setpv(ret, mp->name);
5325 break;
5326 case 'n': /* nextNode */
5327 if (strncmp("nextNode", key, strlen(key))) break;
5328 tp = __get_next_mib_node(tp);
5329 if (tp == NULL) {
5330 sv_setsv(ret, &sv_undef);
5331 break;
5332 }
5333 mib_hv = perl_get_hv("SNMP::MIB", FALSE);
5334 if (SvMAGICAL(mib_hv)) mg = mg_find((SV*)mib_hv, 'P');
5335 if (mg) mib_tied_href = (SV*)mg->mg_obj;
5336 __tp_sprint_num_objid(str_buf, sizeof(str_buf), tp);
5337
5338 nn_hrefp = hv_fetch((HV*)SvRV(mib_tied_href),
5339 str_buf, strlen(str_buf), 1);
5340 /* if (!SvROK(*nn_hrefp)) { */ /* bug in ucd - 2 .0.0 nodes */
5341 next_node_href = newRV((SV*)newHV());
5342 sv_setsv(*nn_hrefp, next_node_href);
5343 ENTER ;
5344 SAVETMPS ;
5345 PUSHMARK(sp) ;
5346 XPUSHs(SvRV(*nn_hrefp));
5347 XPUSHs(sv_2mortal(newSVpv("SNMP::MIB::NODE",0)));
5348 XPUSHs(sv_2mortal(newSVpv(str_buf,0)));
5349 XPUSHs(sv_2mortal(newSViv((IV)tp)));
5350 PUTBACK ;
5351 perl_call_pv("SNMP::_tie",G_VOID);
5352 /* pp_tie(ARGS); */
5353 SPAGAIN ;
5354 FREETMPS ;
5355 LEAVE ;
5356 /* } */
5357 sv_setsv(ret, *nn_hrefp);
5358 break;
5359 case 'o': /* objectID */
5360 if (strncmp("objectID", key, strlen(key))) break;
5361 __tp_sprint_num_objid(str_buf, sizeof(str_buf), tp);
5362 sv_setpv(ret,str_buf);
5363 break;
5364 case 'p': /* parent */
5365 if (strncmp("parent", key, strlen(key))) break;
5366 tp = tp->parent;
5367 if (tp == NULL) {
5368 sv_setsv(ret, &sv_undef);
5369 break;
5370 }
5371 mib_hv = perl_get_hv("SNMP::MIB", FALSE);
5372 if (SvMAGICAL(mib_hv)) mg = mg_find((SV*)mib_hv, 'P');
5373 if (mg) mib_tied_href = (SV*)mg->mg_obj;
5374 __tp_sprint_num_objid(str_buf, sizeof(str_buf), tp);
5375 nn_hrefp = hv_fetch((HV*)SvRV(mib_tied_href),
5376 str_buf, strlen(str_buf), 1);
5377 if (!SvROK(*nn_hrefp)) {
5378 next_node_href = newRV((SV*)newHV());
5379 sv_setsv(*nn_hrefp, next_node_href);
5380 ENTER ;
5381 SAVETMPS ;
5382 PUSHMARK(sp) ;
5383 XPUSHs(SvRV(*nn_hrefp));
5384 XPUSHs(sv_2mortal(newSVpv("SNMP::MIB::NODE",0)));
5385 XPUSHs(sv_2mortal(newSVpv(str_buf,0)));
5386 XPUSHs(sv_2mortal(newSViv((IV)tp)));
5387 PUTBACK ;
5388 perl_call_pv("SNMP::_tie",G_VOID);
5389 /* pp_tie(ARGS); */
5390 SPAGAIN ;
5391 FREETMPS ;
5392 LEAVE ;
5393 }
5394 sv_setsv(ret, *nn_hrefp);
5395 break;
5396 case 'r': /* ranges */
5397 if (strncmp("reference", key, strlen(key)) == 0) {
5398 sv_setpv(ret,tp->reference);
5399 break;
5400 }
5401 if (strncmp("ranges", key, strlen(key))) break;
5402 ranges_av = newAV();
5403 for(rp=tp->ranges; rp ; rp = rp->next) {
5404 range_hv = newHV();
5405 (void)hv_store(range_hv, "low", strlen("low"), newSViv(rp->low), 0);
5406 (void)hv_store(range_hv, "high", strlen("high"), newSViv(rp->high), 0);
5407 av_push(ranges_av, newRV((SV*)range_hv));
5408 }
5409 sv_setsv(ret, newRV((SV*)ranges_av));
5410 break;
5411 case 's': /* subID */
5412 if (strncmp("subID", key, strlen(key))) {
5413 if (strncmp("status", key, strlen(key))) {
5414 if (strncmp("syntax", key, strlen(key))) break;
5415 if (tp->tc_index >= 0) {
5416 sv_setpv(ret, get_tc_descriptor(tp->tc_index));
5417 } else {
5418 __get_type_str(tp->type, str_buf);
5419 sv_setpv(ret, str_buf);
5420 }
5421 break;
5422 }
5423
5424 switch(tp->status) {
5425 case MIB_STATUS_MANDATORY:
5426 sv_setpv(ret,"Mandatory");
5427 break;
5428 case MIB_STATUS_OPTIONAL:
5429 sv_setpv(ret,"Optional");
5430 break;
5431 case MIB_STATUS_OBSOLETE:
5432 sv_setpv(ret,"Obsolete");
5433 break;
5434 case MIB_STATUS_DEPRECATED:
5435 sv_setpv(ret,"Deprecated");
5436 break;
5437 case MIB_STATUS_CURRENT:
5438 sv_setpv(ret,"Current");
5439 break;
5440 default:
5441 break;
5442 }
5443 } else {
5444 sv_setiv(ret,(I32)tp->subid);
5445 }
5446 break;
5447 case 't': /* type */
5448 if (strncmp("type", key, strlen(key))) {
5449 if (strncmp("textualConvention", key, strlen(key))) break;
5450 sv_setpv(ret, get_tc_descriptor(tp->tc_index));
5451 break;
5452 }
5453 __get_type_str(tp->type, str_buf);
5454 sv_setpv(ret, str_buf);
5455 break;
5456 case 'T': /* textual convention description */
5457 if (strncmp("TCDescription", key, strlen(key))) break;
5458 sv_setpv(ret, get_tc_description(tp->tc_index));
5459 break;
5460 case 'u': /* units */
5461 if (strncmp("units", key, strlen(key))) break;
5462 sv_setpv(ret,tp->units);
5463 break;
5464 case 'h': /* hint */
5465 if (strncmp("hint", key, strlen(key))) break;
5466 sv_setpv(ret,tp->hint);
5467 break;
5468 case 'e': /* enums */
5469 if (strncmp("enums", key, strlen(key))) break;
5470 enum_hv = newHV();
5471 for(ep=tp->enums; ep != NULL; ep = ep->next) {
5472 (void)hv_store(enum_hv, ep->label, strlen(ep->label),
5473 newSViv(ep->value), 0);
5474 }
5475 sv_setsv(ret, newRV((SV*)enum_hv));
5476 break;
5477 default:
5478 break;
5479 }
5480 RETVAL = ret;
5481 }
5482 OUTPUT:
5483 RETVAL
5484
5485 MODULE = SNMP PACKAGE = SnmpSessionPtr PREFIX = snmp_session_
5486
5487 void
5488 snmp_session_DESTROY(sess_ptr)
5489 void *sess_ptr
5490 CODE:
5491 {
5492 if(sess_ptr != NULL)
5493 {
5494 if(api_mode == SNMP_API_SINGLE)
5495 {
5496 snmp_sess_close( sess_ptr );
5497 } else {
5498 snmp_close( sess_ptr );
5499 }
5500 }
5501 }
5502