Debian Codegraph @

perl/SNMP/SNMP.xs

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
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
1278 sv_bless(varlist_ref, gv_stashpv("SNMP::VarList",0));
1279 for(vars = (pdu?pdu->variables:NULL); vars; vars = vars->next_variable) {
1280 int local_getlabel_flag = getlabel_flag;
1281 varbind = newAV();
1282 varbind_ref = newRV_noinc((SV*)varbind);
1283 sv_bless(varbind_ref, gv_stashpv("SNMP::Varbind",0));
1284 av_push(varlist, varbind_ref);
1285 *str_buf = '.';
1286 *(str_buf+1) = '\0';
1287 out_len = 0;
1288 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp,
1289 &str_buf_len,
1290 &out_len, 0, &buf_over,
1291 vars->name,vars->name_length);
1292 str_buf[sizeof(str_buf)-1] = '\0';
1293 if (__is_leaf(tp)) {
1294 type = tp->type;
1295 } else {
1296 local_getlabel_flag |= NON_LEAF_NAME;
1297 type = __translate_asn_type(vars->type);
1298 }
1299 __get_label_iid(str_buf,&label,&iid,local_getlabel_flag);
1300 if (label) {
1301 av_store(varbind, VARBIND_TAG_F,
1302 newSVpv(label, strlen(label)));
1303 } else {
1304 av_store(varbind, VARBIND_TAG_F,
1305 newSVpv("", 0));
1306 }
1307 if (iid) {
1308 av_store(varbind, VARBIND_IID_F,
1309 newSVpv(iid, strlen(iid)));
1310 } else {
1311 av_store(varbind, VARBIND_IID_F,
1312 newSVpv("", 0));
1313 }
1314 __get_type_str(type, tmp_type_str);
1315 tmp_sv = newSVpv(tmp_type_str, strlen(tmp_type_str));
1316 av_store(varbind, VARBIND_TYPE_F, tmp_sv);
1317 len = __snprint_value(str_buf, sizeof(str_buf),
1318 vars, tp, type, sprintval_flag);
1319 tmp_sv = newSVpv((char*)str_buf, len);
1320 av_store(varbind, VARBIND_VAL_F, tmp_sv);
1321 } /* for */
1322
1323 /* Reset the library's behavior for numeric/symbolic OID's. */
1324 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS, old_numeric );
1325 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID, old_printfull);
1326 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, old_format);
1327
1328 } /* case SNMP_MSG_RESPONSE */
1329 break;
1330 default:;
1331 } /* switch pdu->command */
1332 break;
1333
1334 case NETSNMP_CALLBACK_OP_TIMED_OUT:
1335 varlist_ref = &sv_undef;
1336 sv_setpv(*err_str_svp, (char*)snmp_api_errstring(SNMPERR_TIMEOUT));
1337 sv_setiv(*err_num_svp, SNMPERR_TIMEOUT);
1338 break;
1339 default:;
1340 } /* switch op */
1341 if (cb_data != ss->callback_magic)
1342 sv_2mortal(cb);
1343 cb = __push_cb_args2(cb,
1344 (SvTRUE(varlist_ref) ? sv_2mortal(varlist_ref):varlist_ref),
1345 (SvTRUE(traplist_ref) ? sv_2mortal(traplist_ref):traplist_ref));
1346 __call_callback(cb, G_DISCARD);
1347
1348 FREETMPS;
1349 LEAVE;
1350 if (cb_data != ss->callback_magic)
1351 sv_2mortal(sess_ref);
1352 return 1;
1353 }
1354
1355 static SV *
1356 __push_cb_args2(sv,esv,tsv)
1357 SV *sv;
1358 SV *esv;
1359 SV *tsv;
1360 {
1361 dSP;
1362 if (SvTYPE(SvRV(sv)) != SVt_PVCV) sv = SvRV(sv);
1363
1364 PUSHMARK(sp);
1365 if (SvTYPE(sv) == SVt_PVAV) {
1366 AV *av = (AV *) sv;
1367 int n = av_len(av) + 1;
1368 SV **x = av_fetch(av, 0, 0);
1369 if (x) {
1370 int i = 1;
1371 sv = *x;
1372
1373 for (i = 1; i < n; i++) {
1374 x = av_fetch(av, i, 0);
1375 if (x) {
1376 SV *arg = *x;
1377 XPUSHs(sv_mortalcopy(arg));
1378 } else {
1379 XPUSHs(&sv_undef);
1380 }
1381 }
1382 } else {
1383 sv = &sv_undef;
1384 }
1385 }
1386 if (esv) XPUSHs(sv_mortalcopy(esv));
1387 if (tsv) XPUSHs(sv_mortalcopy(tsv));
1388 PUTBACK;
1389 return sv;
1390 }
1391
1392 static int
1393 __call_callback(sv, flags)
1394 SV *sv;
1395 int flags;
1396 {
1397 I32 myframe = TOPMARK;
1398 I32 count;
1399 ENTER;
1400 if (SvTYPE(sv) == SVt_PVCV)
1401 {
1402 count = perl_call_sv(sv, flags);
1403 }
1404 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV)
1405 {
1406 count = perl_call_sv(SvRV(sv), flags);
1407 }
1408 else
1409 {
1410
1411 SV **top = stack_base + myframe + 1;
1412 SV *obj = *top;
1413 if (SvPOK(sv) && SvROK(obj) && SvOBJECT(SvRV(obj)))
1414 {
1415 count = perl_call_method(SvPV(sv, na), flags);
1416 }
1417 else if (SvPOK(obj) && SvROK(sv) && SvOBJECT(SvRV(sv)))
1418 {
1419 /* We have obj method ...
1420 Used to be used instead of LangMethodCall()
1421 */
1422 *top = sv;
1423 count = perl_call_method(SvPV(obj, na), flags);
1424 }
1425 else
1426 {
1427 count = perl_call_sv(sv, flags);
1428 }
1429 }
1430 LEAVE;
1431 return count;
1432 }
1433
1434 /* Bulkwalk support routines */
1435
1436 /* Add a context pointer to the list of valid pointers. Place it in the first
1437 ** NULL slot in the array.
1438 */
1439 static int
1440 _context_add(walk_context *context)
1441 {
1442 int i, j, new_sz;
1443
1444 if ((i = _context_okay(context)) != 0) /* Already exists? Okay. */
1445 return i;
1446
1447 /* Initialize the array if necessary. */
1448 if (_valid_contexts == NULL) {
1449
1450 /* Create the _valid_contexts structure. */
1451 Newz(0, _valid_contexts, 1, struct valid_contexts);
1452 assert(_valid_contexts != NULL);
1453
1454 /* Populate the original valid contexts array. */
1455 Newz(0, _valid_contexts->valid, 4, walk_context *);
1456 assert(_valid_contexts->valid != NULL);
1457
1458 /* Computer number of slots in the array. */
1459 _valid_contexts->sz_valid = sizeof(*_valid_contexts->valid) /
1460 sizeof(walk_context *);
1461
1462 for (i = 0; i < _valid_contexts->sz_valid; i++)
1463 _valid_contexts->valid[i] = NULL;
1464
1465 DBPRT(3, (DBOUT "Created valid_context array 0x%p (%d slots)\n",
1466 _valid_contexts->valid, _valid_contexts->sz_valid));
1467 }
1468
1469 /* Search through the list, looking for NULL's -- unused slots. */
1470 for (i = 0; i < _valid_contexts->sz_valid; i++)
1471 if (_valid_contexts->valid[i] == NULL)
1472 break;
1473
1474 /* Did we walk off the end of the list? Need to grow the list. Double
1475 ** it for now.
1476 */
1477 if (i == _valid_contexts->sz_valid) {
1478 new_sz = _valid_contexts->sz_valid * 2;
1479
1480 Renew(_valid_contexts->valid, new_sz, walk_context *);
1481 assert(_valid_contexts->valid != NULL);
1482
1483 DBPRT(3, (DBOUT "Resized valid_context array 0x%p from %d to %d slots\n",
1484 _valid_contexts->valid, _valid_contexts->sz_valid, new_sz));
1485
1486 _valid_contexts->sz_valid = new_sz;
1487
1488 /* Initialize the new half of the resized array. */
1489 for (j = i; j < new_sz; j++)
1490 _valid_contexts->valid[j] = NULL;
1491 }
1492
1493 /* Store the context pointer in the array and return 0 (success). */
1494 _valid_contexts->valid[i] = context;
1495 DBPRT(3,(DBOUT "Add context 0x%p to valid context list\n", context));
1496 return 0;
1497 }
1498
1499 /* Remove a context pointer from the valid list. Replace the pointer with
1500 ** NULL in the valid pointer list.
1501 */
1502 static int
1503 _context_del(walk_context *context)
1504 {
1505 int i;
1506
1507 if (_valid_contexts == NULL) /* Make sure it was initialized. */
1508 return 1;
1509
1510 for (i = 0; i < _valid_contexts->sz_valid; i++) {
1511 if (_valid_contexts->valid[i] == context) {
1512 DBPRT(3,(DBOUT "Remove context 0x%p from valid context list\n", context));
1513 _valid_contexts->valid[i] = NULL; /* Remove it from the list. */
1514 return 0; /* Return successful status. */
1515 }
1516 }
1517 return 1;
1518 }
1519
1520 /* Check if a specific context pointer is in the valid list. Return true (1)
1521 ** if the context is still in the valid list, or 0 if not (or context is NULL).
1522 */
1523 static int
1524 _context_okay(walk_context *context)
1525 {
1526 int i;
1527
1528 if (_valid_contexts == NULL) /* Make sure it was initialized. */
1529 return 0;
1530
1531 if (context == NULL) /* Asked about a NULL context? Fail. */
1532 return 0;
1533
1534 for (i = 0; i < _valid_contexts->sz_valid; i++)
1535 if (_valid_contexts->valid[i] == context)
1536 return 1; /* Found it! */
1537
1538 return 0; /* No match -- return failure. */
1539 }
1540
1541 /* Check if the walk is completed, based upon the context. Also set the
1542 ** ignore flag on any completed variables -- this prevents them from being
1543 ** being sent in later packets.
1544 */
1545 static int
1546 _bulkwalk_done(walk_context *context)
1547 {
1548 int is_done = 1;
1549 int i;
1550 bulktbl *bt_entry; /* bulktbl requested OID entry */
1551
1552 /* Don't consider walk done until at least one packet has been exchanged. */
1553 if (context->pkts_exch == 0)
1554 return 0;
1555
1556 /* Fix up any requests that have completed. If the complete flag is set,
1557 ** or it is a non-repeater OID, set the ignore flag so that it will not
1558 ** be considered further. Assume we are done with the walk, and note
1559 ** otherwise if we aren't. Return 1 if all requests are complete, or 0
1560 ** if there's more to do.
1561 */
1562 for (i = 0; i < context->nreq_oids; i ++) {
1563 bt_entry = &context->req_oids[i];
1564
1565 if (bt_entry->complete || bt_entry->norepeat) {
1566
1567 /* This request is complete. Remove it from list of
1568 ** walks still in progress.
1569 */
1570 DBPRT(1, (DBOUT "Ignoring %s request oid %s\n",
1571 bt_entry->norepeat ? "nonrepeater" : "completed",
1572 __snprint_oid(bt_entry->req_oid, bt_entry->req_len)));
1573
1574 /* Ignore this OID in any further packets. */
1575 bt_entry->ignore = 1;
1576 }
1577
1578 /* If any OID is not being ignored, the walk is not done. Must loop
1579 ** through all requests to do the fixup -- no early return possible.
1580 */
1581 if (!bt_entry->ignore)
1582 is_done = 0;
1583 }
1584
1585 return is_done; /* Did the walk complete? */
1586 }
1587
1588 /* Callback registered with SNMP. Return 1 from this callback to cause the
1589 ** current request to be deleted from the retransmit queue.
1590 */
1591 static int
1592 _bulkwalk_async_cb(int op,
1593 SnmpSession *ss,
1594 int reqid,
1595 netsnmp_pdu *pdu,
1596 void *context_ptr)
1597 {
1598 walk_context *context;
1599 int done = 0;
1600 SV **err_str_svp;
1601 SV **err_num_svp;
1602
1603 /* Handle callback request for asynchronous bulkwalk. If the bulkwalk has
1604 ** not completed, and has not timed out, send the next request packet in
1605 ** the walk.
1606 **
1607 ** Return 0 to indicate success (caller ignores return value).
1608 */
1609
1610 DBPRT(2, (DBOUT "bulkwalk_async_cb(op %d, reqid 0x%08X, context 0x%p)\n",
1611 op, reqid, context_ptr));
1612
1613 context = (walk_context *)context_ptr;
1614
1615 /* Make certain this is a valid context pointer. This pdu may
1616 ** have been retransmitted after the bulkwalk was completed
1617 ** (and the context was destroyed). If so, just return.
1618 */
1619 if (!_context_okay(context)) {
1620 DBPRT(2,(DBOUT "Ignoring PDU for dead context 0x%p...\n", context));
1621 return 1;
1622 }
1623
1624 /* Is this a retransmission of a request we've already seen or some
1625 ** unexpected request id? If so, just ignore it.
1626 */
1627 if (reqid != context->exp_reqid) {
1628 DBPRT(2,
1629 (DBOUT "Got reqid 0x%08X, expected reqid 0x%08X. Ignoring...\n", reqid,
1630 context->exp_reqid));
1631 return 1;
1632 }
1633 /* Ignore any future packets for this reqid. */
1634 context->exp_reqid = -1;
1635
1636 err_str_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorStr", 8, 1);
1637 err_num_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorNum", 8, 1);
1638
1639 switch (op) {
1640 case NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE:
1641 {
1642 DBPRT(1,(DBOUT "Received message for reqid 0x%08X ...\n", reqid));
1643
1644 switch (pdu->command)
1645 {
1646 case SNMP_MSG_RESPONSE:
1647 {
1648 DBPRT(2, (DBOUT "Calling bulkwalk_recv_pdu(context 0x%p, pdu 0x%p)\n",
1649 context_ptr, pdu));
1650
1651 /* Handle the response PDU. If an error occurs or there were
1652 ** no variables in the response, consider the walk done. If
1653 ** the response was okay, check if we have any more to do after
1654 ** this response.
1655 */
1656 if (_bulkwalk_recv_pdu(context, pdu) <= 0)
1657 done = 1;
1658 else
1659 done = _bulkwalk_done(context); /* Also set req ignore flags */
1660 break;
1661 }
1662 default:
1663 {
1664 DBPRT(1,(DBOUT "unexpected pdu->command %d\n", pdu->command));
1665 done = 1; /* "This can't happen!", so bail out when it does. */
1666 break;
1667 }
1668 }
1669
1670 break;
1671 }
1672
1673 case NETSNMP_CALLBACK_OP_TIMED_OUT:
1674 {
1675 DBPRT(1,(DBOUT "\n*** Timeout for reqid 0x%08X\n\n", reqid));
1676
1677 sv_setpv(*err_str_svp, (char*)snmp_api_errstring(SNMPERR_TIMEOUT));
1678 sv_setiv(*err_num_svp, SNMPERR_TIMEOUT);
1679
1680 /* Timeout means something bad has happened. Return a not-okay
1681 ** result to the async callback.
1682 */
1683 _bulkwalk_finish(context, 0 /* NOT OKAY */);
1684 return 1;
1685 }
1686
1687 default:
1688 {
1689 DBPRT(1,(DBOUT "unexpected callback op %d\n", op));
1690 sv_setpv(*err_str_svp, (char*)snmp_api_errstring(SNMPERR_GENERR));
1691 sv_setiv(*err_num_svp, SNMPERR_GENERR);
1692 _bulkwalk_finish(context, 0 /* NOT OKAY */);
1693 return 1;
1694 }
1695 }
1696
1697 /* We have either timed out, or received and parsed in a response. Now,
1698 ** if we have more variables to test, call bulkwalk_send_pdu() to enqueue
1699 ** another async packet, and return.
1700 **
1701 ** If, however, the bulkwalk has completed (or an error has occurred that
1702 ** cuts the walk short), call bulkwalk_finish() to push the results onto
1703 ** the Perl call stack. Then explicitly call the Perl callback that was
1704 ** passed in by the user oh-so-long-ago.
1705 */
1706 if (!done) {
1707 DBPRT(1,(DBOUT "bulkwalk not complete -- send next pdu from callback\n"));
1708
1709 if (_bulkwalk_send_pdu(context) != NULL)
1710 return 1;
1711
1712 DBPRT(1,(DBOUT "send_pdu() failed!\n"));
1713 /* Fall through and return what we have so far. */
1714 }
1715
1716 /* Call the perl callback with the return values and we're done. */
1717 _bulkwalk_finish(context, 1 /* OKAY */);
1718
1719 return 1;
1720 }
1721
1722 static netsnmp_pdu *
1723 _bulkwalk_send_pdu(walk_context *context)
1724 {
1725 netsnmp_pdu *pdu = NULL;
1726 netsnmp_pdu *response = NULL;
1727 struct bulktbl *bt_entry;
1728 int nvars = 0;
1729 int reqid;
1730 int status;
1731 int i;
1732
1733 /* Send a pdu requesting any remaining variables in the context.
1734 **
1735 ** In synchronous mode, returns a pointer to the response packet.
1736 **
1737 ** In asynchronous mode, it returns the request ID, cast to a struct snmp *,
1738 ** not a valid SNMP response packet. The async code should not be trying
1739 ** to get variables out of this "response".
1740 **
1741 ** In either case, return a NULL pointer on error or failure.
1742 */
1743
1744 SV **sess_ptr_sv = hv_fetch((HV*)SvRV(context->sess_ref), "SessPtr", 7, 1);
1745 void *ss = (void *)SvIV((SV*)SvRV(*sess_ptr_sv));
1746 SV **err_str_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorStr", 8, 1);
1747 SV **err_num_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorNum", 8, 1);
1748 SV **err_ind_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorInd", 8, 1);
1749
1750 /* Create a new PDU and send the remaining set of requests to the agent. */
1751 pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
1752 if (pdu == NULL) {
1753 sv_setpv(*err_str_svp, "snmp_pdu_create(GETBULK) failed: ");
1754 sv_catpv(*err_str_svp, strerror(errno));
1755 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
1756 goto err;
1757 }
1758
1759 /* Request non-repeater variables only in the first packet exchange. */
1760 pdu->errstat = (context->pkts_exch == 0) ? context->non_reps : 0;
1761 pdu->errindex = context->max_reps;
1762
1763 for (i = 0; i < context->nreq_oids; i++) {
1764 bt_entry = &context->req_oids[i];
1765 if (bt_entry->ignore)
1766 continue;
1767
1768 assert(bt_entry->complete == 0);
1769
1770 if (!snmp_add_null_var(pdu, bt_entry->last_oid, bt_entry->last_len)) {
1771 sv_setpv(*err_str_svp, "snmp_add_null_var() failed");
1772 sv_setiv(*err_num_svp, SNMPERR_GENERR);
1773 sv_setiv(*err_ind_svp, i);
1774 goto err;
1775 }
1776
1777 nvars ++;
1778
1779 DBPRT(1, (DBOUT " Add %srepeater %s\n", bt_entry->norepeat ? "non" : "",
1780 __snprint_oid(bt_entry->last_oid, bt_entry->last_len)));
1781 }
1782
1783 /* Make sure variables are actually being requested in the packet. */
1784 assert (nvars != 0);
1785
1786 context->pkts_exch ++;
1787
1788 DBPRT(1, (DBOUT "Sending %ssynchronous request %d...\n",
1789 SvTRUE(context->perl_cb) ? "a" : "", context->pkts_exch));
1790
1791 /* We handle the asynchronous and synchronous requests differently here.
1792 ** For async, we simply enqueue the packet with a callback to handle the
1793 ** returned response, then return. Note that this we call the bulkwalk
1794 ** callback, and hand it the walk_context, not the Perl callback. The
1795 ** snmp_async_send() function returns the reqid on success, 0 on failure.
1796 */
1797 if (SvTRUE(context->perl_cb)) {
1798 if(api_mode == SNMP_API_SINGLE)
1799 {
1800 reqid = snmp_sess_async_send(ss, pdu, _bulkwalk_async_cb, (void *)context);
1801 } else {
1802 reqid = snmp_async_send(ss, pdu, _bulkwalk_async_cb, (void *)context);
1803 }
1804
1805 DBPRT(2,(DBOUT "bulkwalk_send_pdu(): snmp_async_send => 0x%08X\n", reqid));
1806
1807 if (reqid == 0) {
1808 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
1809 goto err;
1810 }
1811
1812 /* Make a note of the request we expect to be answered. */
1813 context->exp_reqid = reqid;
1814
1815 /* Callbacks take care of the rest. Let the caller know how many vars
1816 ** we sent in this request. Note that this is not a valid SNMP PDU,
1817 ** but that's because a response has not yet been received.
1818 */
1819 return (netsnmp_pdu *)(intptr_t)reqid;
1820 }
1821
1822 /* This code is for synchronous mode support.
1823 **
1824 ** Send the PDU and block awaiting the response. Return the response
1825 ** packet back to the caller. Note that snmp_sess_read() frees the pdu.
1826 */
1827 status = __send_sync_pdu(ss, pdu, &response, NO_RETRY_NOSUCH,
1828 *err_str_svp, *err_num_svp, *err_ind_svp);
1829
1830 pdu = NULL;
1831
1832 /* Check for a failed request. __send_sync_pdu() will set the appropriate
1833 ** values in the error string and number SV's.
1834 */
1835 if (status != STAT_SUCCESS) {
1836 DBPRT(1,(DBOUT "__send_sync_pdu() -> %d\n",(int)status));
1837 goto err;
1838 }
1839
1840 DBPRT(1, (DBOUT "%d packets exchanged, response 0x%p\n", context->pkts_exch,
1841 response));
1842 return response;
1843
1844
1845 err:
1846 if (pdu)
1847 snmp_free_pdu(pdu);
1848 return NULL;
1849 }
1850
1851 /* Handle an incoming GETBULK response PDU. This function just pulls the
1852 ** variables off of the PDU and builds up the arrays of returned values
1853 ** that are stored in the context.
1854 **
1855 ** Returns the number of variables found in this packet, or -1 on error.
1856 ** Note that the caller is expected to free the pdu.
1857 */
1858 static int
1859 _bulkwalk_recv_pdu(walk_context *context, netsnmp_pdu *pdu)
1860 {
1861 netsnmp_variable_list *vars;
1862 struct tree *tp;
1863 char type_str[MAX_TYPE_NAME_LEN];
1864 char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;
1865 size_t str_buf_len = sizeof(str_buf);
1866 size_t out_len = 0;
1867 int buf_over = 0;
1868 char *label;
1869 char *iid;
1870 bulktbl *expect = NULL;
1871 int old_numeric;
1872 int old_printfull;
1873 int old_format;
1874 int getlabel_flag;
1875 int type;
1876 int pix;
1877 int len;
1878 int i;
1879 AV *varbind;
1880 SV *rv;
1881 SV **sess_ptr_sv = hv_fetch((HV*)SvRV(context->sess_ref), "SessPtr", 7, 1);
1882 SV **err_str_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorStr", 8, 1);
1883 SV **err_num_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorNum", 8, 1);
1884 SV **err_ind_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorInd", 8, 1);
1885 int check = SvIV(*hv_fetch((HV*)SvRV(context->sess_ref), "NonIncreasing",13,1));
1886
1887 DBPRT(3, (DBOUT "bulkwalk: sess_ref = 0x%p, sess_ptr_sv = 0x%p\n",
1888 context->sess_ref, sess_ptr_sv));
1889
1890 /* Set up for numeric OID's, if necessary. Save the old values
1891 ** so that they can be restored when we finish -- these are
1892 ** library-wide globals, and have to be set/restored for each
1893 ** session.
1894 */
1895 old_numeric = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS);
1896 old_printfull = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID);
1897 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1898 if (context->getlabel_f & USE_NUMERIC_OIDS) {
1899 DBPRT(2,(DBOUT "Using numeric oid's\n"));
1900 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS, 1);
1901 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID, 1);
1902 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, NETSNMP_OID_OUTPUT_NUMERIC);
1903 }
1904
1905 /* Parse through the list of variables returned, adding each return to
1906 ** the appropriate array (as a VarBind). Also keep track of which
1907 ** repeated OID we're expecting to see, and check if that tree walk has
1908 ** been completed (i.e. we've walked past the root of our request). If
1909 ** so, mark the request complete so that we don't send it again in any
1910 ** subsequent request packets.
1911 */
1912 if (context->pkts_exch == 1)
1913 context->reqbase = context->req_oids; /* Request with non-repeaters */
1914 else
1915 context->reqbase = context->repbase; /* Request only repeater vars */
1916
1917 /* Note the first variable we expect to see. Should be reqbase. */
1918 expect = context->reqbase;
1919
1920 for (vars = pdu->variables, pix = 0;
1921 vars != NULL;
1922 vars = vars->next_variable, pix ++)
1923 {
1924
1925 /* If no outstanding requests remain, we're done. This works, but it
1926 ** causes the reported total variable count to be wrong (since the
1927 ** remaining vars on the last packet are not counted). In practice
1928 ** this is probably worth the win, but for debugging it's not.
1929 */
1930 if (context->req_remain == 0) {
1931 DBPRT(2,(DBOUT "No outstanding requests remain. Terminating processing.\n"));
1932 while (vars) {
1933 pix ++;
1934 vars = vars->next_variable;
1935 }
1936 break;
1937 }
1938
1939 /* Determine which OID we expect to see next. We assert that the OID's
1940 ** must be returned in the expected order. The first nreq_oids returns
1941 ** should match the req_oids array, after that, we must cycle through
1942 ** the repeaters in order. Non-repeaters are not included in later
1943 ** packets, so cannot have the "ignore" flag set.
1944 */
1945
1946 if (context->oid_saved < context->non_reps) {
1947 assert(context->pkts_exch == 1);
1948
1949 expect = context->reqbase ++;
1950 assert(expect->norepeat);
1951
1952 } else {
1953 /* Must be a repeater. Look for the first one that is not being
1954 ** ignored. Make sure we don't loop around to where we started.
1955 ** If we get here but everything is being ignored, there's a problem.
1956 **
1957 ** Note that we *do* accept completed but not ignored OID's -- these
1958 ** are OID's for trees that have been completed sometime in this
1959 ** response, but must be looked at to maintain ordering.
1960 */
1961 /* In previous version we started from 1st repeater any time when
1962 ** pix == 0. But if 1st repeater is ignored we can get wrong results,
1963 ** because it was not included in 2nd and later request. So we set
1964 ** expect to repbase-1 and then search for 1st non-ignored repeater.
1965 ** repbase-1 is nessessary because we're starting search in loop below
1966 ** from ++expect and it will be exactly repbase on 1st search pass.
1967 */
1968 if (pix == 0)
1969 expect = context->repbase - 1;
1970
1971 /* Find the repeater OID we expect to see. Ignore any
1972 ** OID's marked 'ignore' -- these have been completed
1973 ** and were not requested in this iteration.
1974 */
1975 for (i = 0; i < context->repeaters; i++) {
1976
1977 /* Loop around to first repeater if we hit the end. */
1978 if (++ expect == &context->req_oids[context->nreq_oids])
1979 expect = context->reqbase = context->repbase;
1980
1981 /* Stop if this OID is not being ignored. */
1982 if (!expect->ignore)
1983 break;
1984 }
1985 }
1986
1987 DBPRT(2, (DBOUT "Var %03d request %s\n", pix,
1988 __snprint_oid(expect->req_oid, expect->req_len)));
1989
1990 /* Did we receive an error condition for this variable?
1991 ** If it's a repeated variable, mark it as complete and
1992 ** fall through to the block below.
1993 */
1994 if ((vars->type == SNMP_ENDOFMIBVIEW) ||
1995 (vars->type == SNMP_NOSUCHOBJECT) ||
1996 (vars->type == SNMP_NOSUCHINSTANCE))
1997 {
1998 DBPRT(2,(DBOUT "error type %d\n", (int)vars->type));
1999
2000 /* ENDOFMIBVIEW should be okay for a repeater - just walked off the
2001 ** end of the tree. Mark the request as complete, and go on to the
2002 ** next one.
2003 */
2004 if ((context->oid_saved >= context->non_reps) &&
2005 (vars->type == SNMP_ENDOFMIBVIEW))
2006 {
2007 expect->complete = 1;
2008 DBPRT(2, (DBOUT "Ran out of tree for oid %s\n",
2009 __snprint_oid(vars->name,vars->name_length)));
2010
2011 context->req_remain --;
2012
2013 /* Go on to the next variable. */
2014 continue;
2015
2016 }
2017 sv_setpv(*err_str_svp,
2018 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
2019 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
2020 sv_setiv(*err_ind_svp, pix);
2021 goto err;
2022 }
2023
2024 /* If this is not the first packet, skip any duplicated OID values, if
2025 ** present. These should be the seed values copied from the last OID's
2026 ** of the previous packet. In practice we don't see this, but it is
2027 ** easy enough to do, and will avoid confusion for the caller from mis-
2028 ** behaving agents (badly misbehaving... ;^).
2029 */
2030 if (context->pkts_exch > 1) {
2031 if (__oid_cmp(vars->name, vars->name_length,
2032 expect->last_oid, expect->last_len) <= 0)
2033 {
2034 if (check)
2035 {
2036 DBPRT(2, (DBOUT "Error: OID not increasing: %s\n",
2037 __snprint_oid(vars->name,vars->name_length)));
2038 sv_setpv(*err_str_svp, (char*)snmp_api_errstring(SNMPERR_OID_NONINCREASING));
2039 sv_setiv(*err_num_svp, SNMPERR_OID_NONINCREASING);
2040 sv_setiv(*err_ind_svp, pix);
2041 goto err;
2042 }
2043
2044 DBPRT(2, (DBOUT "Ignoring repeat oid: %s\n",
2045 __snprint_oid(vars->name,vars->name_length)));
2046
2047 continue;
2048 }
2049 }
2050
2051 context->oid_total ++; /* Count each variable received. */
2052
2053 /* If this is a non-repeater, handle it. Otherwise, if it is a
2054 ** repeater, has the walk wandered off of the requested tree? If so,
2055 ** this request is complete, so mark it as such. Ignore any other
2056 ** variables in a completed request. In order to maintain the correct
2057 ** ordering of which variables we expect to see in this packet, we must
2058 ** not set the ignore flags immediately. It is done in bulkwalk_done().
2059 */
2060 if (context->oid_saved < context->non_reps) {
2061 DBPRT(2, (DBOUT " expected var %s (nonrepeater %d/%d)\n",
2062 __snprint_oid(expect->req_oid, expect->req_len),
2063 pix, context->non_reps));
2064 DBPRT(2, (DBOUT " received var %s\n",
2065 __snprint_oid(vars->name, vars->name_length)));
2066
2067 /* This non-repeater has now been seen, so mark the sub-tree as
2068 ** completed. Note that this may not be the same oid as requested,
2069 ** since non-repeaters act like GETNEXT requests, not GET's. <sigh>
2070 */
2071 context->req_oids[pix].complete = 1;
2072 context->req_remain --;
2073
2074 } else { /* Must be a repeater variable. */
2075
2076 DBPRT(2, (DBOUT " received oid %s\n",
2077 __snprint_oid(vars->name, vars->name_length)));
2078
2079 /* Are we already done with this tree? If so, just ignore this
2080 ** variable and move on to the next expected variable.
2081 */
2082 if (expect->complete) {
2083 DBPRT(2,(DBOUT " this branch is complete - ignoring.\n"));
2084 continue;
2085 }
2086
2087 /* If the base oid of this variable doesn't match the expected oid,
2088 ** assume that we've walked past the end of the subtree. Set this
2089 ** subtree to be completed, and go on to the next variable.
2090 */
2091 if ((vars->name_length < expect->req_len) ||
2092 (memcmp(vars->name, expect->req_oid, expect->req_len*sizeof(oid))))
2093 {
2094 DBPRT(2,(DBOUT " walked off branch - marking subtree as complete.\n"));
2095 expect->complete = 1;
2096 context->req_remain --;
2097 continue;
2098 }
2099
2100 /* Still interested in the tree -- we need to keep track of the
2101 ** last-seen value in case we need to send an additional request
2102 ** packet.
2103 */
2104 (void)memcpy(expect->last_oid, vars->name,
2105 vars->name_length * sizeof(oid));
2106 expect->last_len = vars->name_length;
2107
2108 }
2109
2110 /* Create a new Varbind and populate it with the parsed information
2111 ** returned by the agent. This Varbind is then pushed onto the arrays
2112 ** maintained for each request OID in the context. These varbinds are
2113 ** collected into a return array by bulkwalk_finish().
2114 */
2115 varbind = (AV*) newAV();
2116 if (varbind == NULL) {
2117 sv_setpv(*err_str_svp, "newAV() failed: ");
2118 sv_catpv(*err_str_svp, (char*)strerror(errno));
2119 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
2120 goto err;
2121 }
2122
2123 *str_buf = '.';
2124 *(str_buf+1) = '\0';
2125 out_len = 0;
2126 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp, &str_buf_len,
2127 &out_len, 0, &buf_over,
2128 vars->name,vars->name_length);
2129 str_buf[sizeof(str_buf)-1] = '\0';
2130
2131 getlabel_flag = context->getlabel_f;
2132
2133 if (__is_leaf(tp)) {
2134 type = tp->type;
2135 } else {
2136 getlabel_flag |= NON_LEAF_NAME;
2137 type = __translate_asn_type(vars->type);
2138 }
2139 if (__get_label_iid(str_buf, &label, &iid, getlabel_flag) == FAILURE) {
2140 label = str_buf;
2141 iid = label + strlen(label);
2142 }
2143
2144 DBPRT(2,(DBOUT " save var %s.%s = ", label, iid));
2145
2146 av_store(varbind, VARBIND_TAG_F, newSVpv(label, strlen(label)));
2147 av_store(varbind, VARBIND_IID_F, newSVpv(iid, strlen(iid)));
2148
2149 __get_type_str(type, type_str);
2150 av_store(varbind, VARBIND_TYPE_F, newSVpv(type_str, strlen(type_str)));
2151
2152 len = __snprint_value(str_buf, sizeof(str_buf) - 1,
2153 vars, tp, type, context->sprintval_f);
2154 av_store(varbind, VARBIND_VAL_F, newSVpv(str_buf, len));
2155
2156 str_buf[len] = '\0';
2157 DBPRT(3,(DBOUT "'%s' (%s)\n", str_buf, type_str));
2158
2159 #if 0
2160 /* huh? */
2161 /* If necessary, store a timestamp as the semi-documented 5th element. */
2162 if (sv_timestamp)
2163 av_store(varbind, VARBIND_TIME_F, SvREFCNT_inc(sv_timestamp));
2164 #endif
2165
2166 /* Push ref to the varbind onto the list of vars for OID. */
2167 rv = newRV_noinc((SV *)varbind);
2168 sv_bless(rv, gv_stashpv("SNMP::Varbind", 0));
2169 av_push(expect->vars, rv);
2170
2171 context->oid_saved ++; /* Count this as a saved variable. */
2172
2173 } /* next variable in response packet */
2174
2175 DBPRT(1, (DBOUT "-- pkt %d saw %d vars, total %d (%d saved)\n", context->pkts_exch,
2176 pix, context->oid_total, context->oid_saved));
2177
2178 /* We assert that all non-repeaters must be returned in
2179 ** the initial response (they are not repeated in additional
2180 ** packets, so would be dropped). If nonrepeaters still
2181 ** exist, consider it a fatal error.
2182 */
2183 if ((context->pkts_exch == 1) && (context->oid_saved < context->non_reps)) {
2184 /* Re-use space from the value string for error message. */
2185 sprintf(str_buf, "%d non-repeaters went unanswered", context->non_reps);
2186 sv_setpv(*err_str_svp, str_buf);
2187 sv_setiv(*err_num_svp, SNMPERR_GENERR);
2188 sv_setiv(*err_num_svp, context->oid_saved);
2189 goto err;
2190 }
2191
2192 /* Reset the library's behavior for numeric/symbolic OID's. */
2193 if (context->getlabel_f & USE_NUMERIC_OIDS) {
2194 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS, old_numeric);
2195 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID, old_printfull);
2196 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, old_format);
2197 }
2198
2199 return pix;
2200
2201 err:
2202 return -1;
2203
2204 }
2205
2206 /* Once the bulkwalk has completed, extend the stack and push references to
2207 ** each of the arrays of SNMP::Varbind's onto the stack. Return the number
2208 ** of arrays pushed on the stack. The caller should return to Perl, or call
2209 ** the Perl callback function.
2210 **
2211 ** Note that this function free()'s the walk_context and request bulktbl's.
2212 */
2213 static int
2214 _bulkwalk_finish(walk_context *context, int okay)
2215 {
2216 dSP;
2217 int npushed = 0;
2218 int i;
2219 int async = 0;
2220 bulktbl *bt_entry;
2221 AV *ary = NULL;
2222 SV *rv;
2223 SV *perl_cb;
2224
2225 SV **err_str_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorStr", 8, 1);
2226 SV **err_num_svp = hv_fetch((HV*)SvRV(context->sess_ref), "ErrorNum", 8, 1);
2227
2228 async = SvTRUE(context->perl_cb);
2229
2230 /* XXX
2231 _bulkwalk_finish() was originally intended to be called from XS code, and
2232 would extend the caller's stack with result. Later it was changed into
2233 an asynchronous version that calls perl code instead. These two branches
2234 differ significantly in how they treat perl stack. Due to these differences,
2235 often implicit (f.ex. dMARK calls POPMARK ), it would be a good idea
2236 to write two different procedures, _bulkwalk_finish_sync and _bulkwalk_finish_async
2237 for cleaner separation. */
2238
2239 if (async) PUSHMARK(sp);
2240
2241 {
2242
2243 #ifdef dITEMS
2244 dMARK;
2245 dITEMS;
2246 #else
2247 /* unfortunately this may pop a mark, which is not what we want */
2248 /* older perl versions don't declare dITEMS though and the
2249 following declars it but also uses dAXMARK instead of dMARK
2250 which is the bad popping version */
2251 dMARK;
2252
2253 /* err... This is essentially what the newer dITEMS does */
2254 I32 items = sp - mark;
2255 #endif
2256
2257
2258 /* Successfully completed the bulkwalk. For synchronous calls, push each
2259 ** of the request value arrays onto the stack, and return the number of
2260 ** items pushed onto the stack. For async, create a new array and push
2261 ** the references onto it. The array is then passed to the Perl callback.
2262 */
2263 if(!async)
2264 SP -= items;
2265
2266 DBPRT(1, (DBOUT "Bulwalk %s (saved %d/%d), ", okay ? "completed" : "had error",
2267 context->oid_saved, context->oid_total));
2268
2269 if (okay) {
2270 DBPRT(1, (DBOUT "%s %d varbind refs %s\n",
2271 async ? "pass ref to array of" : "return",
2272 context->nreq_oids,
2273 async ? "to callback" : "on stack to caller"));
2274
2275 /* Create the array to hold the responses for the asynchronous callback,
2276 ** or pre-extend the stack enough to hold responses for synch return.
2277 */
2278 if (async) {
2279 ary = (AV *)newAV();
2280 if (ary == NULL) {
2281 sv_setpv(*err_str_svp, "newAV(): ");
2282 sv_catpv(*err_str_svp, (char *)strerror(errno));
2283 sv_setiv(*err_num_svp, errno);
2284 }
2285
2286 /* NULL ary pointer is okay -- we'll handle it below... */
2287
2288 } else {
2289 EXTEND(sp, context->nreq_oids);
2290
2291 }
2292
2293 /* Push a reference to each array of varbinds onto the stack, in
2294 ** the order requested. Note that these arrays may be empty.
2295 */
2296 for (i = 0; i < context->nreq_oids; i++) {
2297 bt_entry = &context->req_oids[i];
2298
2299 DBPRT(2, (DBOUT " %sreq #%d (%s) => %d var%s\n",
2300 bt_entry->complete ? "" : "incomplete ", i,
2301 __snprint_oid(bt_entry->req_oid, bt_entry->req_len),
2302 (int)av_len(bt_entry->vars) + 1,
2303 (int)av_len(bt_entry->vars) > 0 ? "s" : ""));
2304
2305 if (async && ary == NULL) {
2306 DBPRT(2,(DBOUT " [dropped due to newAV() failure]\n"));
2307 continue;
2308 }
2309
2310 /* Get a reference to the varlist, and push it onto array or stack */
2311 rv = newRV_noinc((SV *)bt_entry->vars);
2312 sv_bless(rv, gv_stashpv("SNMP::VarList",0));
2313
2314 if (async)
2315 av_push(ary, rv);
2316 else
2317 PUSHs(sv_2mortal((SV *)rv));
2318
2319 npushed ++;
2320 }
2321
2322 } else { /* Not okay -- push a single undef on the stack if not async */
2323
2324 if (!async) {
2325 XPUSHs(&sv_undef);
2326 npushed = 1;
2327 } else {
2328 for (i = 0; i < context->nreq_oids; i++) {
2329 sv_2mortal((SV *) (context->req_oids[i].vars));
2330 }
2331 }
2332 }
2333
2334 /* XXX Future enhancement -- make statistics (pkts exchanged, vars
2335 ** saved vs. received, total time, etc) available to caller so they
2336 ** can adjust their request parameters and/or re-order requests.
2337 */
2338 if(!async)
2339 SP -= items;
2340
2341 PUTBACK;
2342
2343 if (async) {
2344 /* Asynchronous callback. Push the caller's arglist onto the stack,
2345 ** and follow it with the contents of the array (or undef if newAV()
2346 ** failed or the session had an error). Then mortalize the Perl
2347 ** callback pointer, and call the callback.
2348 */
2349 if (!okay || ary == NULL)
2350 rv = &sv_undef;
2351 else
2352 rv = newRV_noinc((SV *)ary);
2353
2354 sv_2mortal(perl_cb = context->perl_cb);
2355 perl_cb = __push_cb_args(perl_cb, (SvTRUE(rv) ? sv_2mortal(rv) : rv));
2356
2357 __call_callback(perl_cb, G_DISCARD);
2358 }
2359 sv_2mortal(context->sess_ref);
2360
2361 /* Free the allocated space for the request states and return number of
2362 ** variables found. Remove the context from the valid context list.
2363 */
2364 _context_del(context);
2365 DBPRT(2,(DBOUT "Free() context->req_oids\n"));
2366 Safefree(context->req_oids);
2367 DBPRT(2,(DBOUT "Free() context 0x%p\n", context));
2368 Safefree(context);
2369 return npushed;
2370 }}
2371
2372 /* End of bulkwalk support routines */
2373
2374 static char *
2375 __av_elem_pv(AV *av, I32 key, char *dflt)
2376 {
2377 SV **elem = av_fetch(av, key, 0);
2378
2379 return (elem && SvOK(*elem)) ? SvPV(*elem, na) : dflt;
2380 }
2381
2382 static int
2383 not_here(const char *s)
2384 {
2385 warn("%s not implemented on this architecture", s);
2386 return -1;
2387 }
2388
2389 #define TEST_CONSTANT(value, name, C) \
2390 if (strEQ(name, #C)) { \
2391 *value = C; \
2392 return 0; \
2393 }
2394 #define TEST_CONSTANT2(value, name, C, V) \
2395 if (strEQ(name, #C)) { \
2396 *value = V; \
2397 return 0; \
2398 }
2399
2400 static int constant(double *value, const char * const name, const int arg)
2401 {
2402 switch (*name) {
2403 case 'N':
2404 TEST_CONSTANT(value, name, NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE);
2405 TEST_CONSTANT(value, name, NETSNMP_CALLBACK_OP_TIMED_OUT);
2406 break;
2407 case 'S':
2408 TEST_CONSTANT(value, name, SNMPERR_BAD_ADDRESS);
2409 TEST_CONSTANT(value, name, SNMPERR_BAD_LOCPORT);
2410 TEST_CONSTANT(value, name, SNMPERR_BAD_SESSION);
2411 TEST_CONSTANT(value, name, SNMPERR_GENERR);
2412 TEST_CONSTANT(value, name, SNMPERR_TOO_LONG);
2413 TEST_CONSTANT(value, name, SNMP_DEFAULT_ADDRESS);
2414 TEST_CONSTANT(value, name, SNMP_DEFAULT_COMMUNITY_LEN);
2415 TEST_CONSTANT(value, name, SNMP_DEFAULT_ENTERPRISE_LENGTH);
2416 TEST_CONSTANT(value, name, SNMP_DEFAULT_ERRINDEX);
2417 TEST_CONSTANT(value, name, SNMP_DEFAULT_ERRSTAT);
2418 TEST_CONSTANT2(value, name, SNMP_DEFAULT_PEERNAME, 0);
2419 TEST_CONSTANT(value, name, SNMP_DEFAULT_REMPORT);
2420 TEST_CONSTANT(value, name, SNMP_DEFAULT_REQID);
2421 TEST_CONSTANT(value, name, SNMP_DEFAULT_RETRIES);
2422 TEST_CONSTANT(value, name, SNMP_DEFAULT_TIME);
2423 TEST_CONSTANT(value, name, SNMP_DEFAULT_TIMEOUT);
2424 TEST_CONSTANT2(value, name, SNMP_DEFAULT_VERSION,
2425 NETSNMP_DEFAULT_SNMP_VERSION);
2426 TEST_CONSTANT(value, name, SNMP_API_SINGLE);
2427 TEST_CONSTANT(value, name, SNMP_API_TRADITIONAL);
2428 break;
2429 case 'X':
2430 goto not_there;
2431 break;
2432 default:
2433 break;
2434 }
2435 return EINVAL;
2436
2437 not_there:
2438 not_here(name);
2439 return ENOENT;
2440 }
2441
2442 /*
2443 Since s_snmp_errno can't be trusted with Single Session, this calls either
2444 snmp_error or snmp_sess_error to populate ErrorStr,ErrorNum, and ErrorInd
2445 in SNMP::Session objects
2446 */
2447 void snmp_return_err(void *ss, SV *err_str, SV *err_num, SV *err_ind)
2448 {
2449 int err;
2450 int liberr;
2451 char *errstr;
2452 if(ss == NULL)
2453 return;
2454 if(api_mode == SNMP_API_SINGLE)
2455 {
2456 snmp_sess_error(ss, &err, &liberr, &errstr);
2457 } else {
2458 snmp_error(ss, &err, &liberr, &errstr);
2459 }
2460 sv_catpv(err_str, errstr);
2461 sv_setiv(err_num, liberr);
2462 sv_setiv(err_ind, err);
2463 netsnmp_free(errstr);
2464 }
2465
2466
2467 /*
2468 int snmp_api_mode( int mode )
2469 Returns or sets static int api_mode for reference by functions to determine
2470 whether to use Traditional (non-threadsafe) or Single-Session (threadsafe)
2471 SNMP API calls.
2472
2473 Call with (int)NULL to return the current mode, or with SNMP_API_TRADITIONAL
2474 or SNMP_API_SINGLE to set the current mode. (defined above)
2475
2476 pm side call defaults to (int)NULL
2477 */
2478 int snmp_api_mode( int mode )
2479 {
2480 if (mode == 0)
2481 return api_mode;
2482 api_mode = mode;
2483 return api_mode;
2484 }
2485
2486 MODULE = SNMP PACKAGE = SNMP PREFIX = snmp
2487
2488 void
2489 constant(name,arg)
2490 char * name
2491 int arg
2492 INIT:
2493 int status;
2494 double value;
2495 PPCODE:
2496 value = 0;
2497 status = constant(&value, name, arg);
2498 XPUSHs(sv_2mortal(newSVuv(status)));
2499 XPUSHs(sv_2mortal(newSVnv(value)));
2500
2501 long
2502 snmp_sys_uptime()
2503 CODE:
2504 RETVAL = get_uptime();
2505 OUTPUT:
2506 RETVAL
2507
2508 void
2509 init_snmp(appname)
2510 char *appname
2511 CODE:
2512 __libraries_init(appname);
2513
2514 #----------------------------------------------------------------------
2515 # Perl call defaults to (int)NULL when given no args, so it will return
2516 # the current api_mode values
2517 #----------------------------------------------------------------------
2518 int
2519 snmp_api_mode(mode=0)
2520 int mode
2521
2522 SnmpSession *
2523 snmp_new_session(version, community, peer, lport, retries, timeout)
2524 char * version
2525 char * community
2526 char * peer
2527 int lport
2528 int retries
2529 int timeout
2530 CODE:
2531 {
2532 SnmpSession session = {0};
2533 void *ss = NULL;
2534 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2535
2536 snmp_sess_init(&session);
2537
2538 __libraries_init("perl");
2539
2540 session.version = -1;
2541 #ifndef NETSNMP_DISABLE_SNMPV1
2542 if (!strcmp(version, "1")) {
2543 session.version = SNMP_VERSION_1;
2544 }
2545 #endif
2546 #ifndef NETSNMP_DISABLE_SNMPV2C
2547 if ((!strcmp(version, "2")) || (!strcmp(version, "2c"))) {
2548 session.version = SNMP_VERSION_2c;
2549 }
2550 #endif
2551 if (!strcmp(version, "3")) {
2552 session.version = SNMP_VERSION_3;
2553 }
2554 if (session.version == -1) {
2555 if (verbose)
2556 warn("error:snmp_new_session:Unsupported SNMP version (%s)\n", version);
2557 goto end;
2558 }
2559
2560 session.community_len = strlen((char *)community);
2561 session.community = (u_char *)community;
2562 session.peername = peer;
2563 session.local_port = lport;
2564 session.retries = retries; /* 5 */
2565 session.timeout = timeout; /* 1000000L */
2566 session.authenticator = NULL;
2567
2568 if(api_mode == SNMP_API_SINGLE)
2569 {
2570 ss = snmp_sess_open(&session);
2571 } else {
2572 ss = snmp_open(&session);
2573 }
2574
2575 if (ss == NULL) {
2576 if (verbose) warn("error:snmp_new_session: Couldn't open SNMP session");
2577 }
2578 end:
2579 RETVAL = ss;
2580 }
2581 OUTPUT:
2582 RETVAL
2583
2584 SnmpSession *
2585 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)
2586 int version
2587 char * peer
2588 int retries
2589 int timeout
2590 char * sec_name
2591 int sec_level
2592 char * sec_eng_id
2593 char * context_eng_id
2594 char * context
2595 char * auth_proto
2596 char * auth_pass
2597 char * priv_proto
2598 char * priv_pass
2599 int eng_boots
2600 int eng_time
2601 char * auth_master_key
2602 size_t auth_master_key_len
2603 char * priv_master_key
2604 size_t priv_master_key_len
2605 char * auth_localized_key
2606 size_t auth_localized_key_len
2607 char * priv_localized_key
2608 size_t priv_localized_key_len
2609 CODE:
2610 {
2611 SnmpSession session = {0};
2612 void *ss = NULL;
2613 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2614 int auth_type, priv_type;
2615
2616 snmp_sess_init(&session);
2617
2618 __libraries_init("perl");
2619
2620 if (version == 3) {
2621 session.version = SNMP_VERSION_3;
2622 } else {
2623 if (verbose)
2624 warn("error:snmp_new_v3_session:Unsupported SNMP version (%d)\n", version);
2625 goto end;
2626 }
2627
2628 session.peername = peer;
2629 session.retries = retries; /* 5 */
2630 session.timeout = timeout; /* 1000000L */
2631 session.authenticator = NULL;
2632 session.contextNameLen = strlen(context);
2633 session.contextName = context;
2634 session.securityNameLen = strlen(sec_name);
2635 session.securityName = sec_name;
2636 session.securityLevel = sec_level;
2637 session.securityModel = USM_SEC_MODEL_NUMBER;
2638 session.securityEngineIDLen =
2639 hex_to_binary2((u_char*)sec_eng_id, strlen(sec_eng_id),
2640 (char **) &session.securityEngineID);
2641 session.contextEngineIDLen =
2642 hex_to_binary2((u_char*)context_eng_id, strlen(context_eng_id),
2643 (char **) &session.contextEngineID);
2644 session.engineBoots = eng_boots;
2645 session.engineTime = eng_time;
2646 /* NETSNMP_USMAUTH_* */
2647 auth_type = usm_lookup_auth_type(auth_proto);
2648 if (auth_type >= 0) {
2649 const netsnmp_auth_alg_info *auth_alg_info =
2650 sc_find_auth_alg_bytype(auth_type);
2651 if (auth_alg_info) {
2652 session.securityAuthProto =
2653 snmp_duplicate_objid(auth_alg_info->alg_oid,
2654 auth_alg_info->oid_len);
2655 session.securityAuthProtoLen = auth_alg_info->oid_len;
2656 }
2657 }
2658 if (strcmp(auth_proto, "DEFAULT") == 0) {
2659 const oid *theoid =
2660 get_default_authtype(&session.securityAuthProtoLen);
2661 session.securityAuthProto =
2662 snmp_duplicate_objid(theoid, session.securityAuthProtoLen);
2663 }
2664 if (session.securityAuthProto == NULL) {
2665 if (verbose)
2666 warn("error:snmp_new_v3_session:Unsupported authentication protocol(%s)\n", auth_proto);
2667 goto end;
2668 }
2669 if (session.securityLevel >= SNMP_SEC_LEVEL_AUTHNOPRIV) {
2670 if (auth_localized_key_len) {
2671 session.securityAuthLocalKey =
2672 netsnmp_memdup(auth_localized_key,
2673 auth_localized_key_len);
2674 session.securityAuthLocalKeyLen = auth_localized_key_len;
2675 } else if (auth_master_key_len) {
2676 session.securityAuthKeyLen =
2677 SNMP_MIN(auth_master_key_len,
2678 sizeof(session.securityAuthKey));
2679 memcpy(session.securityAuthKey, auth_master_key,
2680 session.securityAuthKeyLen);
2681 } else {
2682 if (strlen(auth_pass) > 0) {
2683 session.securityAuthKeyLen = USM_AUTH_KU_LEN;
2684 if (generate_Ku(session.securityAuthProto,
2685 session.securityAuthProtoLen,
2686 (u_char *)auth_pass, strlen(auth_pass),
2687 session.securityAuthKey,
2688 &session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
2689 if (verbose)
2690 warn("error:snmp_new_v3_session:Error generating Ku from authentication password.\n");
2691 goto end;
2692 }
2693 }
2694 }
2695 }
2696 priv_type = usm_lookup_priv_type(priv_proto);
2697 if (priv_type >= 0) {
2698 const netsnmp_priv_alg_info *priv_alg_info =
2699 sc_get_priv_alg_bytype(priv_type);
2700 if (priv_alg_info) {
2701 session.securityPrivProto =
2702 snmp_duplicate_objid(priv_alg_info->alg_oid,
2703 priv_alg_info->oid_len);
2704 session.securityPrivProtoLen = priv_alg_info->oid_len;
2705 }
2706 }
2707 if (strcmp(priv_proto, "DEFAULT") == 0) {
2708 const oid *theoid =
2709 get_default_privtype(&session.securityPrivProtoLen);
2710 session.securityPrivProto =
2711 snmp_duplicate_objid(theoid, session.securityPrivProtoLen);
2712 }
2713 if (session.securityPrivProto == NULL) {
2714 if (verbose)
2715 warn("error:snmp_new_v3_session:Unsupported privacy protocol(%s)\n", priv_proto);
2716 goto end;
2717 }
2718 if (session.securityLevel >= SNMP_SEC_LEVEL_AUTHPRIV) {
2719 if (priv_localized_key_len) {
2720 session.securityPrivLocalKey =
2721 netsnmp_memdup(priv_localized_key,
2722 priv_localized_key_len);
2723 session.securityPrivLocalKeyLen = priv_localized_key_len;
2724 } else if (priv_master_key_len) {
2725 session.securityPrivKeyLen =
2726 SNMP_MIN(auth_master_key_len,
2727 sizeof(session.securityPrivKey));
2728 memcpy(session.securityPrivKey, priv_master_key,
2729 session.securityPrivKeyLen);
2730 } else {
2731 session.securityPrivKeyLen = USM_PRIV_KU_LEN;
2732 if (generate_Ku(session.securityAuthProto,
2733 session.securityAuthProtoLen,
2734 (u_char *)priv_pass, strlen(priv_pass),
2735 session.securityPrivKey,
2736 &session.securityPrivKeyLen) != SNMPERR_SUCCESS) {
2737 if (verbose)
2738 warn("error:snmp_new_v3_session:Error generating Ku from privacy pass phrase.\n");
2739 goto end;
2740 }
2741 }
2742 }
2743
2744 if(api_mode == SNMP_API_SINGLE)
2745 {
2746 ss = snmp_sess_open(&session);
2747 } else {
2748 ss = snmp_open(&session);
2749 }
2750
2751 if (ss == NULL) {
2752 if (verbose) warn("error:snmp_new_v3_session:Couldn't open SNMP session");
2753 }
2754 end:
2755 RETVAL = ss;
2756 netsnmp_free(session.securityPrivLocalKey);
2757 netsnmp_free(session.securityPrivProto);
2758 netsnmp_free(session.securityAuthLocalKey);
2759 netsnmp_free(session.securityAuthProto);
2760 netsnmp_free(session.contextEngineID);
2761 netsnmp_free(session.securityEngineID);
2762 }
2763 OUTPUT:
2764 RETVAL
2765
2766 SnmpSession *
2767 snmp_new_tunneled_session(version, peer, retries, timeout, sec_name, sec_level, context_eng_id, context, our_identity, their_identity, their_hostname, trust_cert)
2768 int version
2769 char * peer
2770 int retries
2771 int timeout
2772 char * sec_name
2773 int sec_level
2774 char * context_eng_id
2775 char * context
2776 char * our_identity
2777 char * their_identity
2778 char * their_hostname
2779 char * trust_cert
2780 CODE:
2781 {
2782 SnmpSession session = {0};
2783 SnmpSession *ss = NULL;
2784 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2785
2786 snmp_sess_init(&session);
2787
2788 __libraries_init("perl");
2789
2790 session.version = version;
2791
2792 session.peername = peer;
2793 session.retries = retries; /* 5 */
2794 session.timeout = timeout; /* 1000000L */
2795 session.contextNameLen = strlen(context);
2796 session.contextName = context;
2797 session.securityNameLen = strlen(sec_name);
2798 session.securityName = sec_name;
2799 session.securityLevel = sec_level;
2800 session.securityModel = NETSNMP_TSM_SECURITY_MODEL;
2801 session.contextEngineIDLen =
2802 hex_to_binary2((u_char*)context_eng_id, strlen(context_eng_id),
2803 (char **) &session.contextEngineID);
2804
2805 /* create the transport configuration store */
2806 if (!session.transport_configuration) {
2807 netsnmp_container_init_list();
2808 session.transport_configuration =
2809 netsnmp_container_find("transport_configuration:fifo");
2810 if (!session.transport_configuration) {
2811 fprintf(stderr, "failed to initialize the transport configuration container\n");
2812 RETVAL = NULL;
2813 return;
2814 }
2815
2816 session.transport_configuration->compare =
2817 netsnmp_transport_config_compare;
2818 }
2819
2820 if (our_identity && our_identity[0] != '\0')
2821 CONTAINER_INSERT(session.transport_configuration,
2822 netsnmp_transport_create_config("our_identity",
2823 our_identity));
2824
2825 if (their_identity && their_identity[0] != '\0')
2826 CONTAINER_INSERT(session.transport_configuration,
2827 netsnmp_transport_create_config("their_identity",
2828 their_identity));
2829
2830 if (their_hostname && their_hostname[0] != '\0')
2831 CONTAINER_INSERT(session.transport_configuration,
2832 netsnmp_transport_create_config("their_hostname",
2833 their_hostname));
2834
2835 if (trust_cert && trust_cert[0] != '\0')
2836 CONTAINER_INSERT(session.transport_configuration,
2837 netsnmp_transport_create_config("trust_cert",
2838 trust_cert));
2839
2840
2841 ss = snmp_open(&session);
2842
2843 if (ss == NULL) {
2844 if (verbose) warn("error:snmp_new_v3_session:Couldn't open SNMP session");
2845 }
2846
2847 RETVAL = ss;
2848 netsnmp_free(session.securityPrivLocalKey);
2849 netsnmp_free(session.securityPrivProto);
2850 netsnmp_free(session.securityAuthLocalKey);
2851 netsnmp_free(session.securityAuthProto);
2852 netsnmp_free(session.contextEngineID);
2853 netsnmp_free(session.securityEngineID);
2854 }
2855 OUTPUT:
2856 RETVAL
2857
2858 SnmpSession *
2859 snmp_update_session(sess_ref, version, community, peer, lport, retries, timeout)
2860 SV * sess_ref
2861 char * version
2862 char * community
2863 char * peer
2864 int lport
2865 int retries
2866 int timeout
2867 CODE:
2868 {
2869 SV **sess_ptr_sv;
2870 SnmpSession *ss;
2871 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2872
2873 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
2874 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
2875
2876 if (!ss) goto update_end;
2877
2878 ss->version = -1;
2879 #ifndef NETSNMP_DISABLE_SNMPV1
2880 if (!strcmp(version, "1")) {
2881 ss->version = SNMP_VERSION_1;
2882 }
2883 #endif
2884 #ifndef NETSNMP_DISABLE_SNMPV2C
2885 if (!strcmp(version, "2") || !strcmp(version, "2c")) {
2886 ss->version = SNMP_VERSION_2c;
2887 }
2888 #endif
2889 if (!strcmp(version, "3")) {
2890 ss->version = SNMP_VERSION_3;
2891 }
2892 if (ss->version == -1) {
2893 if (verbose)
2894 warn("snmp_update_session: Unsupported SNMP version (%s)\n", version);
2895 goto update_end;
2896 }
2897 /* WARNING LEAKAGE but I cant free lib memory under win32 */
2898 ss->community_len = strlen((char *)community);
2899 ss->community = (u_char *)netsnmp_strdup(community);
2900 ss->peername = netsnmp_strdup(peer);
2901 ss->local_port = lport;
2902 ss->retries = retries; /* 5 */
2903 ss->timeout = timeout; /* 1000000L */
2904 ss->authenticator = NULL;
2905
2906 update_end:
2907 RETVAL = ss;
2908 }
2909 OUTPUT:
2910 RETVAL
2911
2912 int
2913 snmp_add_mib_dir(mib_dir,force=0)
2914 char * mib_dir
2915 int force
2916 CODE:
2917 {
2918 int result = 0; /* Avoid use of uninitialized variable below. */
2919 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2920
2921 DBPRT(999, (DBOUT "force=%d\n", force));
2922
2923 if (mib_dir && *mib_dir) {
2924 result = add_mibdir(mib_dir);
2925 }
2926 if (result) {
2927 if (verbose) warn("snmp_add_mib_dir: Added mib dir %s\n", mib_dir);
2928 } else {
2929 if (verbose) warn("snmp_add_mib_dir: Failed to add %s\n", mib_dir);
2930 }
2931 RETVAL = (I32)result;
2932 }
2933 OUTPUT:
2934 RETVAL
2935
2936 void
2937 snmp_init_mib_internals()
2938 CODE:
2939 {
2940 int notused = 1; notused++;
2941 /* this function does nothing */
2942 /* it is kept only for backwards compatibility */
2943 }
2944
2945
2946 char *
2947 snmp_getenv(name)
2948 char *name;
2949 CODE:
2950 RETVAL = netsnmp_getenv(name);
2951 OUTPUT:
2952 RETVAL
2953
2954 int
2955 snmp_setenv(envname, envval, overwrite)
2956 char *envname;
2957 char *envval;
2958 int overwrite;
2959 CODE:
2960 RETVAL = netsnmp_setenv(envname, envval, overwrite);
2961 OUTPUT:
2962 RETVAL
2963
2964 int
2965 snmp_read_mib(mib_file, force=0)
2966 char * mib_file
2967 int force
2968 CODE:
2969 {
2970 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
2971
2972 DBPRT(999, (DBOUT "force=%d\n", force));
2973
2974 if ((mib_file == NULL) || (*mib_file == '\0')) {
2975 if (get_tree_head() == NULL) {
2976 if (verbose) warn("snmp_read_mib: initializing MIB\n");
2977 netsnmp_init_mib();
2978 if (get_tree_head()) {
2979 if (verbose) warn("done\n");
2980 } else {
2981 if (verbose) warn("failed\n");
2982 }
2983 }
2984 } else {
2985 if (verbose) warn("snmp_read_mib: reading MIB: %s\n", mib_file);
2986 if (strcmp("ALL",mib_file))
2987 read_mib(mib_file);
2988 else
2989 read_all_mibs();
2990 if (get_tree_head()) {
2991 if (verbose) warn("done\n");
2992 } else {
2993 if (verbose) warn("failed\n");
2994 }
2995 }
2996 RETVAL = (IV)get_tree_head();
2997 }
2998 OUTPUT:
2999 RETVAL
3000
3001
3002 int
3003 snmp_read_module(module)
3004 char * module
3005 CODE:
3006 {
3007 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3008
3009 if (!strcmp(module,"ALL")) {
3010 read_all_mibs();
3011 } else {
3012 netsnmp_read_module(module);
3013 }
3014 if (get_tree_head()) {
3015 if (verbose) warn("Read %s\n", module);
3016 } else {
3017 if (verbose) warn("Failed reading %s\n", module);
3018 }
3019 RETVAL = (IV)get_tree_head();
3020 }
3021 OUTPUT:
3022 RETVAL
3023
3024 void
3025 snmp_set(sess_ref, varlist_ref, perl_callback)
3026 SV * sess_ref
3027 SV * varlist_ref
3028 SV * perl_callback
3029 PPCODE:
3030 {
3031 #ifndef NETSNMP_NO_WRITE_SUPPORT
3032 AV *varlist;
3033 SV **varbind_ref;
3034 SV **varbind_val_f;
3035 AV *varbind;
3036 I32 varlist_len;
3037 I32 varlist_ind;
3038 void *ss;
3039 netsnmp_pdu *pdu, *response;
3040 struct tree *tp;
3041 oid *oid_arr;
3042 size_t oid_arr_len = MAX_OID_LEN;
3043 char *tag_pv;
3044 snmp_xs_cb_data *xs_cb_data;
3045 SV **sess_ptr_sv;
3046 SV **err_str_svp;
3047 SV **err_num_svp;
3048 SV **err_ind_svp;
3049 int status = 0;
3050 int type;
3051 int res;
3052 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3053 int use_enums;
3054 struct enum_list *ep;
3055 int best_guess;
3056
3057 New (0, oid_arr, MAX_OID_LEN, oid);
3058
3059 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
3060
3061 use_enums = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums",8,1));
3062 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3063 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3064 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3065 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3066 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3067 sv_setpv(*err_str_svp, "");
3068 sv_setiv(*err_num_svp, 0);
3069 sv_setiv(*err_ind_svp, 0);
3070 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
3071
3072 pdu = snmp_pdu_create(SNMP_MSG_SET);
3073
3074 varlist = (AV*) SvRV(varlist_ref);
3075 varlist_len = av_len(varlist);
3076 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
3077 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3078 if (SvROK(*varbind_ref)) {
3079 varbind = (AV*) SvRV(*varbind_ref);
3080 tag_pv = __av_elem_pv(varbind, VARBIND_TAG_F,NULL);
3081 tp=__tag2oid(tag_pv,
3082 __av_elem_pv(varbind, VARBIND_IID_F,NULL),
3083 oid_arr, &oid_arr_len, &type, best_guess);
3084
3085 if (oid_arr_len==0) {
3086 if (verbose)
3087 warn("error: set: unknown object ID (%s)",
3088 (tag_pv?tag_pv:"<null>"));
3089 sv_catpv(*err_str_svp,
3090 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
3091 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
3092 XPUSHs(&sv_undef); /* unknown OID */
3093 snmp_free_pdu(pdu);
3094 goto done;
3095 }
3096
3097
3098 if (type == TYPE_UNKNOWN) {
3099 type = __translate_appl_type(
3100 __av_elem_pv(varbind, VARBIND_TYPE_F, NULL));
3101 if (type == TYPE_UNKNOWN) {
3102 if (verbose)
3103 warn("error: set: no type found for object");
3104 sv_catpv(*err_str_svp,
3105 (char*)snmp_api_errstring(SNMPERR_VAR_TYPE));
3106 sv_setiv(*err_num_svp, SNMPERR_VAR_TYPE);
3107 XPUSHs(&sv_undef); /* unknown OID */
3108 snmp_free_pdu(pdu);
3109 goto done;
3110 }
3111 }
3112
3113 varbind_val_f = av_fetch(varbind, VARBIND_VAL_F, 0);
3114
3115 if (type==TYPE_INTEGER && use_enums && tp && tp->enums) {
3116 for(ep = tp->enums; ep; ep = ep->next) {
3117 if (varbind_val_f && SvOK(*varbind_val_f) &&
3118 !strcmp(ep->label, SvPV(*varbind_val_f,na))) {
3119 sv_setiv(*varbind_val_f, ep->value);
3120 break;
3121 }
3122 }
3123 }
3124
3125 res = __add_var_val_str(pdu, oid_arr, oid_arr_len,
3126 (varbind_val_f && SvOK(*varbind_val_f) ?
3127 SvPV(*varbind_val_f,na):NULL),
3128 (varbind_val_f && SvPOK(*varbind_val_f) ?
3129 SvCUR(*varbind_val_f):0), type);
3130
3131 if (verbose && res == FAILURE)
3132 warn("error: set: adding variable/value to PDU");
3133 } /* if var_ref is ok */
3134 } /* for all the vars */
3135
3136 if (SvTRUE(perl_callback)) {
3137 xs_cb_data =
3138 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3139 xs_cb_data->perl_cb = newSVsv(perl_callback);
3140 xs_cb_data->sess_ref = newRV_inc(SvRV(sess_ref));
3141
3142 if(api_mode == SNMP_API_SINGLE)
3143 {
3144 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
3145 (void*)xs_cb_data);
3146 } else {
3147 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
3148 (void*)xs_cb_data);
3149 }
3150 if (status != 0) {
3151 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
3152 } else {
3153 snmp_free_pdu(pdu);
3154 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
3155 XPUSHs(&sv_undef);
3156 }
3157 goto done;
3158 }
3159
3160 status = __send_sync_pdu(ss, pdu, &response,
3161 NO_RETRY_NOSUCH,
3162 *err_str_svp, *err_num_svp,
3163 *err_ind_svp);
3164
3165 if (response) snmp_free_pdu(response);
3166
3167 if (status) {
3168 XPUSHs(&sv_undef);
3169 } else {
3170 XPUSHs(sv_2mortal(newSVpv(ZERO_BUT_TRUE,0)));
3171 }
3172 } else {
3173
3174 /* BUG!!! need to return an error value */
3175 XPUSHs(&sv_undef); /* no mem or bad args */
3176 }
3177 done:
3178 Safefree(oid_arr);
3179 #else /* NETSNMP_NO_WRITE_SUPPORT */
3180 warn("error: Net-SNMP was compiled using --enable-read-only, set() can not be used.");
3181 #endif /* NETSNMP_NO_WRITE_SUPPORT */
3182 }
3183
3184 void
3185 snmp_catch(sess_ref, perl_callback)
3186 SV * sess_ref
3187 SV * perl_callback
3188 PPCODE:
3189 {
3190 void *sess_ptr;
3191 netsnmp_session *ss;
3192 SV **sess_ptr_sv;
3193 SV **err_str_svp;
3194 SV **err_num_svp;
3195 SV **err_ind_svp;
3196
3197 if (SvROK(sess_ref)) {
3198 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3199 sess_ptr = (void *)SvIV((SV*)SvRV(*sess_ptr_sv));
3200 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3201 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3202 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3203 sv_setpv(*err_str_svp, "");
3204 sv_setiv(*err_num_svp, 0);
3205 sv_setiv(*err_ind_svp, 0);
3206
3207 ss = api_mode == SNMP_API_SINGLE ? snmp_sess_session(sess_ptr) :
3208 sess_ptr;
3209 ss->callback = NULL;
3210 ss->callback_magic = NULL;
3211
3212 if (SvTRUE(perl_callback)) {
3213 snmp_xs_cb_data *xs_cb_data;
3214 xs_cb_data =
3215 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3216 xs_cb_data->perl_cb = newSVsv(perl_callback);
3217 xs_cb_data->sess_ref = newRV_inc(SvRV(sess_ref));
3218
3219 # it might be more efficient to pass the varbind_ref to
3220 # __snmp_xs_cb as part of perl_callback so it is not freed
3221 # and reconstructed for each call
3222 ss->callback = __snmp_xs_cb;
3223 ss->callback_magic = xs_cb_data;
3224 sv_2mortal(newSViv(1));
3225 goto done;
3226 }
3227 }
3228 sv_2mortal(newSViv(0));
3229 done:
3230 ;
3231 }
3232
3233 void
3234 snmp_get(sess_ref, retry_nosuch, varlist_ref, perl_callback)
3235 SV * sess_ref
3236 int retry_nosuch
3237 SV * varlist_ref
3238 SV * perl_callback
3239 PPCODE:
3240 {
3241 AV *varlist;
3242 SV **varbind_ref;
3243 AV *varbind;
3244 I32 varlist_len;
3245 I32 varlist_ind;
3246 void *ss;
3247 netsnmp_pdu *pdu, *response;
3248 netsnmp_variable_list *vars;
3249 struct tree *tp;
3250 int len;
3251 oid *oid_arr;
3252 size_t oid_arr_len = MAX_OID_LEN;
3253 SV *tmp_sv;
3254 int type;
3255 char tmp_type_str[MAX_TYPE_NAME_LEN];
3256 snmp_xs_cb_data *xs_cb_data;
3257 SV **sess_ptr_sv;
3258 SV **err_str_svp;
3259 SV **err_num_svp;
3260 SV **err_ind_svp;
3261 int status;
3262 char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;
3263 size_t str_buf_len = sizeof(str_buf);
3264 size_t out_len = 0;
3265 int buf_over = 0;
3266 char *label;
3267 char *iid;
3268 int getlabel_flag = NO_FLAGS;
3269 int sprintval_flag = USE_BASIC;
3270 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3271 int old_format;
3272 SV *sv_timestamp = NULL;
3273 int best_guess;
3274
3275 New (0, oid_arr, MAX_OID_LEN, oid);
3276
3277 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
3278
3279 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3280 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3281 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3282 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3283 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3284 sv_setpv(*err_str_svp, "");
3285 sv_setiv(*err_num_svp, 0);
3286 sv_setiv(*err_ind_svp, 0);
3287 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1)))
3288 getlabel_flag |= USE_LONG_NAMES;
3289 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1)))
3290 getlabel_flag |= USE_NUMERIC_OIDS;
3291 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums", 8, 1)))
3292 sprintval_flag = USE_ENUMS;
3293 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseSprintValue", 14, 1)))
3294 sprintval_flag = USE_SPRINT_VALUE;
3295 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
3296
3297 pdu = snmp_pdu_create(SNMP_MSG_GET);
3298
3299 varlist = (AV*) SvRV(varlist_ref);
3300 varlist_len = av_len(varlist);
3301 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
3302 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3303 if (SvROK(*varbind_ref)) {
3304 char *tag_pv;
3305 varbind = (AV*) SvRV(*varbind_ref);
3306
3307 tag_pv = __av_elem_pv(varbind, VARBIND_TAG_F, ".0");
3308 tp = __tag2oid(tag_pv,
3309 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
3310 oid_arr, &oid_arr_len, NULL, best_guess);
3311
3312 if (oid_arr_len) {
3313 snmp_add_null_var(pdu, oid_arr, oid_arr_len);
3314 } else {
3315 if (verbose)
3316 warn("error: get: unknown object ID (%s)",
3317 (tag_pv?tag_pv:"<null>"));
3318 sv_catpv(*err_str_svp,
3319 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
3320 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
3321 XPUSHs(&sv_undef); /* unknown OID */
3322 snmp_free_pdu(pdu);
3323 goto done;
3324 }
3325
3326 } /* if var_ref is ok */
3327 } /* for all the vars */
3328
3329 if (perl_callback && SvTRUE(perl_callback)) {
3330 xs_cb_data =
3331 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3332 xs_cb_data->perl_cb = newSVsv(perl_callback);
3333 xs_cb_data->sess_ref = newSVsv(sess_ref);
3334
3335 if(api_mode == SNMP_API_SINGLE)
3336 {
3337 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
3338 (void*)xs_cb_data);
3339 } else {
3340 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
3341 (void*)xs_cb_data);
3342 }
3343 if (status != 0) {
3344 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
3345 } else {
3346 snmp_free_pdu(pdu);
3347 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
3348 XPUSHs(&sv_undef);
3349 }
3350 goto done;
3351 }
3352
3353 status = __send_sync_pdu(ss, pdu, &response,
3354 retry_nosuch,
3355 *err_str_svp, *err_num_svp,
3356 *err_ind_svp);
3357
3358 /*
3359 ** Set up for numeric or full OID's, if necessary. Save the old
3360 ** output format so that it can be restored when we finish -- this
3361 ** is a library-wide global, and has to be set/restored for each
3362 ** session.
3363 */
3364 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
3365 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
3366
3367 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1))) {
3368 getlabel_flag |= USE_LONG_NAMES;
3369
3370 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3371 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3372 NETSNMP_OID_OUTPUT_FULL);
3373 }
3374 /* Setting UseNumeric forces UseLongNames on so check for UseNumeric
3375 after UseLongNames (above) to make sure the final outcome of
3376 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT is NETSNMP_OID_OUTPUT_NUMERIC */
3377 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1))) {
3378 getlabel_flag |= USE_LONG_NAMES;
3379 getlabel_flag |= USE_NUMERIC_OIDS;
3380
3381 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3382 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3383 NETSNMP_OID_OUTPUT_NUMERIC);
3384 }
3385
3386 if (SvIOK(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)) &&
3387 SvIV(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)))
3388 sv_timestamp = newSViv((IV)time(NULL));
3389
3390 for(vars = (response?response->variables:NULL), varlist_ind = 0;
3391 vars && (varlist_ind <= varlist_len);
3392 vars = vars->next_variable, varlist_ind++) {
3393 int local_getlabel_flag = getlabel_flag;
3394 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3395 if (SvROK(*varbind_ref)) {
3396 varbind = (AV*) SvRV(*varbind_ref);
3397
3398 *str_buf = '.';
3399 *(str_buf+1) = '\0';
3400 out_len = 0;
3401 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp,
3402 &str_buf_len,
3403 &out_len, 0,
3404 &buf_over,
3405 vars->name,
3406 vars->name_length);
3407 str_buf[sizeof(str_buf)-1] = '\0';
3408
3409 if (__is_leaf(tp)) {
3410 type = tp->type;
3411 } else {
3412 local_getlabel_flag |= NON_LEAF_NAME;
3413 type = __translate_asn_type(vars->type);
3414 }
3415 __get_label_iid(str_buf,&label,&iid,local_getlabel_flag);
3416 if (label) {
3417 av_store(varbind, VARBIND_TAG_F,
3418 newSVpv(label, strlen(label)));
3419 } else {
3420 av_store(varbind, VARBIND_TAG_F,
3421 newSVpv("", 0));
3422 }
3423 if (iid) {
3424 av_store(varbind, VARBIND_IID_F,
3425 newSVpv(iid, strlen(iid)));
3426 } else {
3427 av_store(varbind, VARBIND_IID_F,
3428 newSVpv("", 0));
3429 }
3430 __get_type_str(type, tmp_type_str);
3431 tmp_sv = newSVpv(tmp_type_str, strlen(tmp_type_str));
3432 av_store(varbind, VARBIND_TYPE_F, tmp_sv);
3433 len=__snprint_value(str_buf,sizeof(str_buf),
3434 vars,tp,type,sprintval_flag);
3435 tmp_sv = newSVpv(str_buf, len);
3436 av_store(varbind, VARBIND_VAL_F, tmp_sv);
3437 if (sv_timestamp)
3438 av_store(varbind, VARBIND_TYPE_F, sv_timestamp);
3439 XPUSHs(sv_mortalcopy(tmp_sv));
3440 } else {
3441 /* Return undef for this variable. */
3442 XPUSHs(&sv_undef);
3443 }
3444 }
3445
3446 /* Reset the library's behavior for numeric/symbolic OID's. */
3447 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3448 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3449 old_format);
3450
3451 if (response) snmp_free_pdu(response);
3452
3453 } else {
3454 XPUSHs(&sv_undef); /* no mem or bad args */
3455 }
3456 done:
3457 Safefree(oid_arr);
3458 }
3459
3460 void
3461 snmp_getnext(sess_ref, varlist_ref, perl_callback)
3462 SV * sess_ref
3463 SV * varlist_ref
3464 SV * perl_callback
3465 PPCODE:
3466 {
3467 AV *varlist;
3468 SV **varbind_ref;
3469 AV *varbind;
3470 I32 varlist_len;
3471 I32 varlist_ind;
3472 void *ss;
3473 netsnmp_pdu *pdu, *response;
3474 netsnmp_variable_list *vars;
3475 struct tree *tp;
3476 int len;
3477 oid *oid_arr;
3478 size_t oid_arr_len = MAX_OID_LEN;
3479 SV *tmp_sv;
3480 int type;
3481 char tmp_type_str[MAX_TYPE_NAME_LEN];
3482 snmp_xs_cb_data *xs_cb_data;
3483 SV **sess_ptr_sv;
3484 SV **err_str_svp;
3485 SV **err_num_svp;
3486 SV **err_ind_svp;
3487 int status;
3488 char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;
3489 size_t str_buf_len = sizeof(str_buf);
3490 char tmp_buf_prefix[STR_BUF_SIZE];
3491 char str_buf_prefix[STR_BUF_SIZE];
3492 size_t out_len = 0;
3493 int buf_over = 0;
3494 char *label;
3495 char *iid;
3496 int getlabel_flag = NO_FLAGS;
3497 int sprintval_flag = USE_BASIC;
3498 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3499 int old_format;
3500 SV *sv_timestamp = NULL;
3501 int best_guess;
3502 char *tmp_prefix_ptr;
3503 char *st;
3504
3505 New (0, oid_arr, MAX_OID_LEN, oid);
3506
3507 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
3508
3509 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3510 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3511 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3512 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3513 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3514 sv_setpv(*err_str_svp, "");
3515 sv_setiv(*err_num_svp, 0);
3516 sv_setiv(*err_ind_svp, 0);
3517 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1)))
3518 getlabel_flag |= USE_LONG_NAMES;
3519 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1)))
3520 getlabel_flag |= USE_NUMERIC_OIDS;
3521 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums", 8, 1)))
3522 sprintval_flag = USE_ENUMS;
3523 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseSprintValue", 14, 1)))
3524 sprintval_flag = USE_SPRINT_VALUE;
3525 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
3526
3527 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
3528
3529 varlist = (AV*) SvRV(varlist_ref);
3530 varlist_len = av_len(varlist);
3531 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
3532 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3533 if (SvROK(*varbind_ref)) {
3534 char *tag_pv;
3535 varbind = (AV*) SvRV(*varbind_ref);
3536
3537 /* If the varbind includes the module prefix, capture it for use later */
3538 strlcpy(tmp_buf_prefix, __av_elem_pv(varbind, VARBIND_TAG_F, ".0"), STR_BUF_SIZE);
3539 tmp_prefix_ptr = strstr(tmp_buf_prefix,"::");
3540 if (tmp_prefix_ptr) {
3541 tmp_prefix_ptr = strtok_r(tmp_buf_prefix, "::", &st);
3542 strlcpy(str_buf_prefix, tmp_prefix_ptr, STR_BUF_SIZE);
3543 }
3544 else {
3545 *str_buf_prefix = '\0';
3546 }
3547
3548 tag_pv = __av_elem_pv(varbind, VARBIND_TAG_F, ".0");
3549 tp = __tag2oid(tag_pv,
3550 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
3551 oid_arr, &oid_arr_len, NULL, best_guess);
3552
3553 if (oid_arr_len) {
3554 snmp_add_null_var(pdu, oid_arr, oid_arr_len);
3555 } else {
3556 if (verbose)
3557 warn("error: getnext: unknown object ID (%s)",
3558 (tag_pv?tag_pv:"<null>"));
3559 sv_catpv(*err_str_svp,
3560 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
3561 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
3562 XPUSHs(&sv_undef); /* unknown OID */
3563 snmp_free_pdu(pdu);
3564 goto done;
3565 }
3566
3567 } /* if var_ref is ok */
3568 } /* for all the vars */
3569
3570 if (SvTRUE(perl_callback)) {
3571 xs_cb_data =
3572 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3573 xs_cb_data->perl_cb = newSVsv(perl_callback);
3574 xs_cb_data->sess_ref = newSVsv(sess_ref);
3575
3576 if(api_mode == SNMP_API_SINGLE)
3577 {
3578 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
3579 (void*)xs_cb_data);
3580 } else {
3581 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
3582 (void*)xs_cb_data);
3583 }
3584 if (status != 0) {
3585 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
3586 } else {
3587 snmp_free_pdu(pdu);
3588 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
3589 XPUSHs(&sv_undef);
3590 }
3591 goto done;
3592 }
3593
3594 status = __send_sync_pdu(ss, pdu, &response,
3595 NO_RETRY_NOSUCH,
3596 *err_str_svp, *err_num_svp,
3597 *err_ind_svp);
3598
3599 /*
3600 ** Set up for numeric or full OID's, if necessary. Save the old
3601 ** output format so that it can be restored when we finish -- this
3602 ** is a library-wide global, and has to be set/restored for each
3603 ** session.
3604 */
3605 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
3606 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
3607
3608 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1))) {
3609 getlabel_flag |= USE_LONG_NAMES;
3610
3611 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3612 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3613 NETSNMP_OID_OUTPUT_FULL);
3614 }
3615 /* Setting UseNumeric forces UseLongNames on so check
3616 for UseNumeric after UseLongNames (above) to make
3617 sure the final outcome of
3618 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT is
3619 NETSNMP_OID_OUTPUT_NUMERIC */
3620 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1))) {
3621 getlabel_flag |= USE_LONG_NAMES;
3622 getlabel_flag |= USE_NUMERIC_OIDS;
3623
3624 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3625 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3626 NETSNMP_OID_OUTPUT_NUMERIC);
3627 }
3628
3629 if (SvIOK(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)) &&
3630 SvIV(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)))
3631 sv_timestamp = newSViv((IV)time(NULL));
3632
3633 for(vars = (response?response->variables:NULL), varlist_ind = 0;
3634 vars && (varlist_ind <= varlist_len);
3635 vars = vars->next_variable, varlist_ind++) {
3636 int local_getlabel_flag = getlabel_flag;
3637 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3638 if (SvROK(*varbind_ref)) {
3639 varbind = (AV*) SvRV(*varbind_ref);
3640
3641 *str_buf = '.';
3642 *(str_buf+1) = '\0';
3643 out_len = 0;
3644 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp,
3645 &str_buf_len,
3646 &out_len, 0,
3647 &buf_over,
3648 vars->name,
3649 vars->name_length);
3650 str_buf[sizeof(str_buf)-1] = '\0';
3651
3652 /* Prepend the module prefix to the next OID if needed */
3653 if (*str_buf_prefix) {
3654 strlcat(str_buf_prefix, "::", STR_BUF_SIZE);
3655 strlcat(str_buf_prefix, str_buf, STR_BUF_SIZE);
3656 strlcpy(str_buf, str_buf_prefix, STR_BUF_SIZE);
3657 }
3658
3659 if (__is_leaf(tp)) {
3660 type = tp->type;
3661 } else {
3662 local_getlabel_flag |= NON_LEAF_NAME;
3663 type = __translate_asn_type(vars->type);
3664 }
3665 __get_label_iid(str_buf,&label,&iid,local_getlabel_flag);
3666 if (label) {
3667 av_store(varbind, VARBIND_TAG_F,
3668 newSVpv(label, strlen(label)));
3669 } else {
3670 av_store(varbind, VARBIND_TAG_F,
3671 newSVpv("", 0));
3672 }
3673 if (iid) {
3674 av_store(varbind, VARBIND_IID_F,
3675 newSVpv(iid, strlen(iid)));
3676 } else {
3677 av_store(varbind, VARBIND_IID_F,
3678 newSVpv("", 0));
3679 }
3680 __get_type_str(type, tmp_type_str);
3681 tmp_sv = newSVpv(tmp_type_str, strlen(tmp_type_str));
3682 av_store(varbind, VARBIND_TYPE_F, tmp_sv);
3683 len=__snprint_value(str_buf,sizeof(str_buf),
3684 vars,tp,type,sprintval_flag);
3685 tmp_sv = newSVpv(str_buf, len);
3686 av_store(varbind, VARBIND_VAL_F, tmp_sv);
3687 if (sv_timestamp)
3688 av_store(varbind, VARBIND_TYPE_F, sv_timestamp);
3689 XPUSHs(sv_mortalcopy(tmp_sv));
3690 } else {
3691 /* Return undef for this variable. */
3692 XPUSHs(&sv_undef);
3693 }
3694 }
3695
3696 /* Reset the library's behavior for numeric/symbolic OID's. */
3697 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3698 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3699 old_format);
3700
3701 if (response) snmp_free_pdu(response);
3702
3703 } else {
3704 XPUSHs(&sv_undef); /* no mem or bad args */
3705 }
3706 done:
3707 Safefree(oid_arr);
3708 }
3709
3710 void
3711 snmp_getbulk(sess_ref, nonrepeaters, maxrepetitions, varlist_ref, perl_callback)
3712 SV * sess_ref
3713 int nonrepeaters
3714 int maxrepetitions
3715 SV * varlist_ref
3716 SV * perl_callback
3717 PPCODE:
3718 {
3719 AV *varlist;
3720 SV **varbind_ref;
3721 AV *varbind;
3722 I32 varlist_len;
3723 I32 varlist_ind;
3724 void *ss;
3725 netsnmp_pdu *pdu, *response;
3726 netsnmp_variable_list *vars;
3727 struct tree *tp;
3728 int len;
3729 oid *oid_arr;
3730 size_t oid_arr_len = MAX_OID_LEN;
3731 SV *tmp_sv;
3732 int type;
3733 char tmp_type_str[MAX_TYPE_NAME_LEN];
3734 snmp_xs_cb_data *xs_cb_data;
3735 SV **sess_ptr_sv;
3736 SV **err_str_svp;
3737 SV **err_num_svp;
3738 SV **err_ind_svp;
3739 int status;
3740 char str_buf[STR_BUF_SIZE], *str_bufp = str_buf;
3741 size_t str_buf_len = sizeof(str_buf);
3742 size_t out_len = 0;
3743 int buf_over = 0;
3744 char *label;
3745 char *iid;
3746 int getlabel_flag = NO_FLAGS;
3747 int sprintval_flag = USE_BASIC;
3748 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3749 int old_format;
3750 SV *rv;
3751 SV *sv_timestamp = NULL;
3752 int best_guess;
3753
3754 New (0, oid_arr, MAX_OID_LEN, oid);
3755
3756 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
3757
3758 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3759 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3760 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3761 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3762 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3763 sv_setpv(*err_str_svp, "");
3764 sv_setiv(*err_num_svp, 0);
3765 sv_setiv(*err_ind_svp, 0);
3766 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1)))
3767 getlabel_flag |= USE_LONG_NAMES;
3768 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1)))
3769 getlabel_flag |= USE_NUMERIC_OIDS;
3770 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums", 8, 1)))
3771 sprintval_flag = USE_ENUMS;
3772 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseSprintValue", 14, 1)))
3773 sprintval_flag = USE_SPRINT_VALUE;
3774 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
3775
3776 pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
3777
3778 pdu->errstat = nonrepeaters;
3779 pdu->errindex = maxrepetitions;
3780
3781 varlist = (AV*) SvRV(varlist_ref);
3782 varlist_len = av_len(varlist);
3783 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
3784 varbind_ref = av_fetch(varlist, varlist_ind, 0);
3785 if (SvROK(*varbind_ref)) {
3786 char *tag_pv;
3787 varbind = (AV*) SvRV(*varbind_ref);
3788 tag_pv = __av_elem_pv(varbind, VARBIND_TAG_F, "0");
3789 __tag2oid(tag_pv,
3790 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
3791 oid_arr, &oid_arr_len, NULL, best_guess);
3792
3793
3794 if (oid_arr_len) {
3795 snmp_add_null_var(pdu, oid_arr, oid_arr_len);
3796 } else {
3797 if (verbose)
3798 warn("error: getbulk: unknown object ID (%s)",
3799 (tag_pv?tag_pv:"<null>"));
3800 sv_catpv(*err_str_svp,
3801 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
3802 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
3803 XPUSHs(&sv_undef); /* unknown OID */
3804 snmp_free_pdu(pdu);
3805 goto done;
3806 }
3807
3808
3809 } /* if var_ref is ok */
3810 } /* for all the vars */
3811
3812 if (SvTRUE(perl_callback)) {
3813 xs_cb_data =
3814 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
3815 xs_cb_data->perl_cb = newSVsv(perl_callback);
3816 xs_cb_data->sess_ref = newSVsv(sess_ref);
3817
3818 if(api_mode == SNMP_API_SINGLE)
3819 {
3820 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
3821 (void*)xs_cb_data);
3822 } else {
3823 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
3824 (void*)xs_cb_data);
3825 }
3826 if (status != 0) {
3827 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
3828 } else {
3829 snmp_free_pdu(pdu);
3830 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
3831 XPUSHs(&sv_undef);
3832 }
3833 goto done;
3834 }
3835
3836 status = __send_sync_pdu(ss, pdu, &response,
3837 NO_RETRY_NOSUCH,
3838 *err_str_svp, *err_num_svp,
3839 *err_ind_svp);
3840
3841 if (SvIOK(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)) &&
3842 SvIV(*hv_fetch((HV*)SvRV(sess_ref),"TimeStamp", 9, 1)))
3843 sv_timestamp = newSViv((IV)time(NULL));
3844
3845 av_clear(varlist);
3846
3847 /*
3848 ** Set up for numeric or full OID's, if necessary. Save the old
3849 ** output format so that it can be restored when we finish -- this
3850 ** is a library-wide global, and has to be set/restored for each
3851 ** session.
3852 */
3853 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
3854 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
3855 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1))) {
3856 getlabel_flag |= USE_LONG_NAMES;
3857
3858 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3859 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3860 NETSNMP_OID_OUTPUT_FULL);
3861 }
3862 /* Setting UseNumeric forces UseLongNames on so check for UseNumeric
3863 after UseLongNames (above) to make sure the final outcome of
3864 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT is NETSNMP_OID_OUTPUT_NUMERIC */
3865 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1))) {
3866 getlabel_flag |= USE_LONG_NAMES;
3867 getlabel_flag |= USE_NUMERIC_OIDS;
3868
3869 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3870 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3871 NETSNMP_OID_OUTPUT_NUMERIC);
3872 }
3873
3874 if(response && response->variables) {
3875 for(vars = response->variables;
3876 vars;
3877 vars = vars->next_variable) {
3878
3879 int local_getlabel_flag = getlabel_flag;
3880 varbind = (AV*) newAV();
3881 *str_buf = '.';
3882 *(str_buf+1) = '\0';
3883 out_len = 0;
3884 buf_over = 0;
3885 str_bufp = str_buf;
3886 tp = netsnmp_sprint_realloc_objid_tree((u_char**)&str_bufp,
3887 &str_buf_len,
3888 &out_len, 0,
3889 &buf_over,
3890 vars->name,
3891 vars->name_length);
3892 str_buf[sizeof(str_buf)-1] = '\0';
3893 if (__is_leaf(tp)) {
3894 type = tp->type;
3895 } else {
3896 local_getlabel_flag |= NON_LEAF_NAME;
3897 type = __translate_asn_type(vars->type);
3898 }
3899 __get_label_iid(str_buf,&label,&iid,local_getlabel_flag);
3900 if (label) {
3901 av_store(varbind, VARBIND_TAG_F,
3902 newSVpv(label, strlen(label)));
3903 } else {
3904 av_store(varbind, VARBIND_TAG_F,
3905 newSVpv("", 0));
3906 }
3907 if (iid) {
3908 av_store(varbind, VARBIND_IID_F,
3909 newSVpv(iid, strlen(iid)));
3910 } else {
3911 av_store(varbind, VARBIND_IID_F,
3912 newSVpv("", 0));
3913 }
3914 __get_type_str(type, tmp_type_str);
3915 av_store(varbind, VARBIND_TYPE_F, newSVpv(tmp_type_str,
3916 strlen(tmp_type_str)));
3917
3918 len=__snprint_value(str_buf,sizeof(str_buf),
3919 vars,tp,type,sprintval_flag);
3920 tmp_sv = newSVpv(str_buf, len);
3921 av_store(varbind, VARBIND_VAL_F, tmp_sv);
3922 if (sv_timestamp)
3923 av_store(varbind, VARBIND_TYPE_F, SvREFCNT_inc(sv_timestamp));
3924
3925 rv = newRV_noinc((SV *)varbind);
3926 sv_bless(rv, gv_stashpv("SNMP::Varbind",0));
3927 av_push(varlist, rv);
3928
3929 XPUSHs(sv_mortalcopy(tmp_sv));
3930 }
3931 } else {
3932 XPUSHs(&sv_undef);
3933 }
3934
3935 /* Reset the library's behavior for numeric/symbolic OID's. */
3936 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
3937 NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
3938 old_format);
3939
3940 if (response) snmp_free_pdu(response);
3941
3942 } else {
3943 XPUSHs(&sv_undef); /* no mem or bad args */
3944 }
3945 done:
3946 Safefree(oid_arr);
3947 }
3948
3949 void
3950 snmp_bulkwalk(sess_ref, nonrepeaters, maxrepetitions, varlist_ref,perl_callback)
3951 SV * sess_ref
3952 int nonrepeaters
3953 int maxrepetitions
3954 SV * varlist_ref
3955 SV * perl_callback
3956 PPCODE:
3957 {
3958 AV *varlist;
3959 SV **varbind_ref;
3960 AV *varbind;
3961 I32 varlist_len;
3962 I32 varlist_ind;
3963 void *ss;
3964 netsnmp_pdu *pdu = NULL;
3965 oid oid_arr[MAX_OID_LEN];
3966 size_t oid_arr_len;
3967 SV **sess_ptr_sv;
3968 SV **err_str_svp;
3969 SV **err_num_svp;
3970 SV **err_ind_svp;
3971 char str_buf[STR_BUF_SIZE];
3972 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
3973 walk_context *context = NULL; /* Context for this bulkwalk */
3974 bulktbl *bt_entry; /* Current bulktbl/OID entry */
3975 int i; /* General purpose iterator */
3976 int npushed; /* Number of return arrays */
3977 int okay; /* Did bulkwalk complete okay */
3978 int best_guess;
3979
3980 if (!SvROK(sess_ref) || !SvROK(varlist_ref)) {
3981 if (verbose)
3982 warn("bulkwalk: Bad session or varlist reference!\n");
3983
3984 XSRETURN_UNDEF;
3985 }
3986
3987 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
3988 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
3989 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
3990 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
3991 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
3992 sv_setpv(*err_str_svp, "");
3993 sv_setiv(*err_num_svp, 0);
3994 sv_setiv(*err_ind_svp, 0);
3995 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
3996
3997 /* Create and initialize a new session context for this bulkwalk.
3998 ** This will be used to carry state between callbacks.
3999 */
4000 Newz(0x57616b6c /* "Walk" */, context, 1, walk_context);
4001 if (context == NULL) {
4002 sprintf(str_buf, "malloc(context) failed (%s)", strerror(errno));
4003 sv_setpv(*err_str_svp, str_buf);
4004 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
4005 goto err;
4006 }
4007
4008 /* Store the Perl callback and session reference in the context. */
4009 context->perl_cb = newSVsv(perl_callback);
4010 context->sess_ref = newSVsv(sess_ref);
4011
4012 DBPRT(3,(DBOUT "bulkwalk: sess_ref = 0x%p, sess_ptr_sv = 0x%p, ss = 0x%p\n",
4013 sess_ref, sess_ptr_sv, ss));
4014
4015 context->getlabel_f = NO_FLAGS; /* long/numeric name flags */
4016 context->sprintval_f = USE_BASIC; /* Don't do fancy printing */
4017 context->req_oids = NULL; /* List of oid's requested */
4018 context->repbase = NULL; /* Repeaters in req_oids[] */
4019 context->reqbase = NULL; /* Ptr to start of requests */
4020 context->nreq_oids = 0; /* Number of oid's in list */
4021 context->repeaters = 0; /* Repeater count (see below) */
4022 context->non_reps = nonrepeaters; /* Non-repeater var count */
4023 context->max_reps = maxrepetitions; /* Max repetition/var count */
4024 context->pkts_exch = 0; /* Packets exchanged in walk */
4025 context->oid_total = 0; /* OID's received during walk */
4026 context->oid_saved = 0; /* OID's saved as results */
4027
4028 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseLongNames", 12, 1)))
4029 context->getlabel_f |= USE_LONG_NAMES;
4030 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseNumeric", 10, 1)))
4031 context->getlabel_f |= USE_NUMERIC_OIDS;
4032 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums", 8, 1)))
4033 context->sprintval_f = USE_ENUMS;
4034 if (SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseSprintValue", 14, 1)))
4035 context->sprintval_f = USE_SPRINT_VALUE;
4036
4037 /* Set up an array of bulktbl's to hold the original list of
4038 ** requested OID's. This is used to populate the PDU's with
4039 ** oid values, to contain/sort the return values, and (through
4040 ** last_oid/last_len) to determine when the bulkwalk for each
4041 ** variable has completed.
4042 */
4043 varlist = (AV*) SvRV(varlist_ref);
4044 varlist_len = av_len(varlist) + 1; /* XXX av_len returns index of
4045 ** last element not #elements */
4046
4047 Newz(0, context->req_oids, varlist_len, bulktbl);
4048
4049 if (context->req_oids == NULL) {
4050 sprintf(str_buf, "Newz(req_oids) failed (%s)", strerror(errno));
4051 if (verbose)
4052 warn("%s", str_buf);
4053 sv_setpv(*err_str_svp, str_buf);
4054 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
4055 goto err;
4056 }
4057
4058 /* Walk through the varbind_list, parsing and copying each OID
4059 ** into a bulktbl slot in the req_oids array. Bail if there's
4060 ** some error. Create the initial packet to send out, which
4061 ** includes the non-repeaters.
4062 */
4063 DBPRT(1,(DBOUT "Building request table:\n"));
4064 for (varlist_ind = 0; varlist_ind < varlist_len; varlist_ind++) {
4065 /* Get a handle on this entry in the request table. */
4066 bt_entry = &context->req_oids[context->nreq_oids];
4067
4068 DBPRT(1,(DBOUT " request %d: ", (int) varlist_ind));
4069
4070 /* Get the request varbind from the varlist, parse it out to
4071 ** tag and index, and copy it to the req_oid[] array slots.
4072 */
4073 varbind_ref = av_fetch(varlist, varlist_ind, 0);
4074 if (!SvROK(*varbind_ref)) {
4075 sv_setpv(*err_str_svp, \
4076 (char*)snmp_api_errstring(SNMPERR_BAD_NAME));
4077 sv_setiv(*err_num_svp, SNMPERR_BAD_NAME);
4078 goto err;
4079 }
4080
4081 varbind = (AV*) SvRV(*varbind_ref);
4082 __tag2oid(__av_elem_pv(varbind, VARBIND_TAG_F, "0"),
4083 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
4084 oid_arr, &oid_arr_len, NULL, best_guess);
4085
4086 if ((oid_arr_len == 0) || (oid_arr_len > MAX_OID_LEN)) {
4087 if (verbose)
4088 warn("error: bulkwalk(): unknown object ID");
4089 sv_setpv(*err_str_svp, \
4090 (char*)snmp_api_errstring(SNMPERR_UNKNOWN_OBJID));
4091 sv_setiv(*err_num_svp, SNMPERR_UNKNOWN_OBJID);
4092 goto err;
4093 }
4094
4095 /* Copy the now-parsed OID into the first available slot
4096 ** in the req_oids[] array. Set both the req_oid (original
4097 ** request) and the last_oid (last requested/seen oid) to
4098 ** the initial value. We build packets using last_oid (see
4099 ** below), so initialize last_oid to the initial request.
4100 */
4101 Copy((void *)oid_arr, (void *)bt_entry->req_oid,
4102 oid_arr_len, oid);
4103 Copy((void *)oid_arr, (void *)bt_entry->last_oid,
4104 oid_arr_len, oid);
4105
4106 bt_entry->req_len = oid_arr_len;
4107 bt_entry->last_len = oid_arr_len;
4108
4109 /* Adjust offset to and count of repeaters. Note non-repeater
4110 ** OID's in the list, if appropriate.
4111 */
4112 if (varlist_ind >= context->non_reps) {
4113
4114 /* Store a pointer to the first repeater value. */
4115 if (context->repbase == NULL)
4116 context->repbase = bt_entry;
4117
4118 context->repeaters ++;
4119
4120 } else {
4121 bt_entry->norepeat = 1;
4122 DBPRT(1,(DBOUT "HERE 1\n"));
4123 DBPRT(1,(DBOUT "(nonrepeater) "));
4124 }
4125
4126 /* Initialize the array in which to hold the Varbinds to be
4127 ** returned for the OID or subtree.
4128 */
4129 if ((bt_entry->vars = (AV*) newAV()) == NULL) {
4130 sv_setpv(*err_str_svp, "newAV() failed: ");
4131 sv_catpv(*err_str_svp, strerror(errno));
4132 sv_setiv(*err_num_svp, SNMPERR_MALLOC);
4133 goto err;
4134 }
4135 DBPRT(1,(DBOUT "%s\n", __snprint_oid(oid_arr, oid_arr_len)));
4136 context->nreq_oids ++;
4137 }
4138
4139 /* Keep track of the number of outstanding requests. This lets us
4140 ** finish processing early if we're done with all requests.
4141 */
4142 context->req_remain = context->nreq_oids;
4143 DBPRT(1,(DBOUT "Total %d variable requests added\n", context->nreq_oids));
4144
4145 /* If no good variable requests were found, return an error. */
4146 if (context->nreq_oids == 0) {
4147 sv_setpv(*err_str_svp, "No variables found in varlist");
4148 sv_setiv(*err_num_svp, SNMPERR_NO_VARS);
4149 goto err;
4150 }
4151
4152 /* Note that this is a good context. This allows later callbacks
4153 ** to ignore re-sent PDU's that correspond to completed (and hence
4154 ** destroyed) bulkwalk contexts.
4155 */
4156 _context_add(context);
4157
4158 /* For asynchronous bulkwalk requests, all we have to do at this
4159 ** point is enqueue the asynchronous GETBULK request with our
4160 ** bulkwalk-specific callback and return. Remember that the
4161 ** bulkwalk_send_pdu() function returns the reqid cast to an
4162 ** snmp_pdu pointer, or NULL on failure. Return undef if the
4163 ** initial send fails; bulkwalk_send_pdu() takes care of setting
4164 ** the various error values.
4165 **
4166 ** From here, the callbacks do all the work, including sending
4167 ** requests for variables and handling responses. The caller's
4168 ** callback will be invoked as soon as the walk completes.
4169 */
4170 if (SvTRUE(perl_callback)) {
4171 DBPRT(1,(DBOUT "Starting asynchronous bulkwalk...\n"));
4172
4173 pdu = _bulkwalk_send_pdu(context);
4174
4175 if (pdu == NULL) {
4176 DBPRT(1,(DBOUT "Initial asynchronous send failed...\n"));
4177 XSRETURN_UNDEF;
4178 }
4179
4180 /* Sent okay... Return the request ID in 'pdu' as an SvIV. */
4181 DBPRT(1,(DBOUT "Okay, request id is %ld\n", (long)(intptr_t)pdu));
4182 /* XSRETURN_IV((intptr_t)pdu); */
4183 XPUSHs(sv_2mortal(newSViv((IV)pdu)));
4184 XSRETURN(1);
4185 }
4186
4187 /* For synchronous bulkwalk, we perform the basic send/receive
4188 ** iteration right here. Once the walk has been completed, the
4189 ** bulkwalk_finish() function will push the return values onto
4190 ** the Perl call stack, and we return.
4191 */
4192 DBPRT(1,(DBOUT "Starting synchronous bulkwalk...\n"));
4193
4194 while (!(okay = _bulkwalk_done(context))) {
4195
4196 /* Send a request for the next batch of variables. */
4197 DBPRT(1, (DBOUT "Building %s GETBULK bulkwalk PDU (%d)...\n",
4198 context->pkts_exch ? "next" : "first",
4199 context->pkts_exch));
4200 pdu = _bulkwalk_send_pdu(context);
4201
4202 /* If the request failed, consider the walk done. */
4203 if (pdu == NULL) {
4204 DBPRT(1,(DBOUT "bulkwalk_send_pdu() failed!\n"));
4205 break;
4206 }
4207
4208 /* Handle the variables in this response packet. Break out
4209 ** of the loop if an error occurs or no variables are found
4210 ** in the response.
4211 */
4212 if ((i = _bulkwalk_recv_pdu(context, pdu)) <= 0) {
4213 DBPRT(2,(DBOUT "bulkwalk_recv_pdu() returned %d (error/empty)\n", i));
4214 goto err;
4215 }
4216
4217 /* Free the returned pdu. Don't bother to do this for the async
4218 ** case, since the SNMP callback mechanism itself does the free
4219 ** for us.
4220 */
4221 snmp_free_pdu(pdu);
4222
4223 /* And loop. The call to bulkwalk_done() sets the ignore flags
4224 ** for any completed request subtrees. Next time around, they
4225 ** won't be added to the request sent to the agent.
4226 */
4227 continue;
4228 }
4229
4230 DBPRT(1, (DBOUT "Bulkwalk done... calling bulkwalk_finish(%s)...\n",
4231 okay ? "okay" : "error"));
4232 npushed = _bulkwalk_finish(context, okay);
4233
4234 DBPRT(2,(DBOUT "Returning %d values on the stack.\n", npushed));
4235 XSRETURN(npushed);
4236
4237 /* Handle error cases and clean up after ourselves. */
4238 err:
4239 if (context) {
4240 if (context->req_oids && context->nreq_oids) {
4241 bt_entry = context->req_oids;
4242 for (i = 0; i < context->nreq_oids; i++, bt_entry++)
4243 av_clear(bt_entry->vars);
4244 }
4245 if (context->req_oids)
4246 Safefree(context->req_oids);
4247 Safefree(context);
4248 }
4249 if (pdu)
4250 snmp_free_pdu(pdu);
4251
4252 XSRETURN_UNDEF;
4253 }
4254
4255
4256 void
4257 snmp_trapV1(sess_ref,enterprise,agent,generic,specific,uptime,varlist_ref)
4258 SV * sess_ref
4259 char * enterprise
4260 char * agent
4261 int generic
4262 int specific
4263 long uptime
4264 SV * varlist_ref
4265 PPCODE:
4266 {
4267 AV *varlist;
4268 SV **varbind_ref;
4269 SV **varbind_val_f;
4270 AV *varbind;
4271 I32 varlist_len;
4272 I32 varlist_ind;
4273 void *ss;
4274 netsnmp_pdu *pdu = NULL;
4275 struct tree *tp;
4276 oid *oid_arr;
4277 size_t oid_arr_len = MAX_OID_LEN;
4278 SV **sess_ptr_sv;
4279 SV **err_str_svp;
4280 SV **err_num_svp;
4281 SV **err_ind_svp;
4282 int type;
4283 int res;
4284 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
4285 int use_enums = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums",8,1));
4286 struct enum_list *ep;
4287 int best_guess;
4288
4289 New (0, oid_arr, MAX_OID_LEN, oid);
4290
4291 if (oid_arr && SvROK(sess_ref)) {
4292
4293 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4294 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4295 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
4296 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
4297 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
4298 sv_setpv(*err_str_svp, "");
4299 sv_setiv(*err_num_svp, 0);
4300 sv_setiv(*err_ind_svp, 0);
4301 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
4302
4303 pdu = snmp_pdu_create(SNMP_MSG_TRAP);
4304
4305 if (SvROK(varlist_ref)) {
4306 varlist = (AV*) SvRV(varlist_ref);
4307 varlist_len = av_len(varlist);
4308 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
4309 varbind_ref = av_fetch(varlist, varlist_ind, 0);
4310 if (SvROK(*varbind_ref)) {
4311 varbind = (AV*) SvRV(*varbind_ref);
4312
4313 tp=__tag2oid(__av_elem_pv(varbind, VARBIND_TAG_F, NULL),
4314 __av_elem_pv(varbind, VARBIND_IID_F, NULL),
4315 oid_arr, &oid_arr_len, &type, best_guess);
4316
4317 if (oid_arr_len == 0) {
4318 if (verbose)
4319 warn("error:trap: unable to determine oid for object");
4320 goto err;
4321 }
4322
4323 if (type == TYPE_UNKNOWN) {
4324 type = __translate_appl_type(
4325 __av_elem_pv(varbind, VARBIND_TYPE_F, NULL));
4326 if (type == TYPE_UNKNOWN) {
4327 if (verbose)
4328 warn("error:trap: no type found for object");
4329 goto err;
4330 }
4331 }
4332
4333 varbind_val_f = av_fetch(varbind, VARBIND_VAL_F, 0);
4334
4335 if (type==TYPE_INTEGER && use_enums && tp && tp->enums) {
4336 for(ep = tp->enums; ep; ep = ep->next) {
4337 if (varbind_val_f && SvOK(*varbind_val_f) &&
4338 !strcmp(ep->label, SvPV(*varbind_val_f,na))) {
4339 sv_setiv(*varbind_val_f, ep->value);
4340 break;
4341 }
4342 }
4343 }
4344
4345 res = __add_var_val_str(pdu, oid_arr, oid_arr_len,
4346 (varbind_val_f && SvOK(*varbind_val_f) ?
4347 SvPV(*varbind_val_f,na):NULL),
4348 (varbind_val_f && SvPOK(*varbind_val_f) ?
4349 SvCUR(*varbind_val_f):0),
4350 type);
4351
4352 if(res == FAILURE) {
4353 if(verbose) warn("error:trap: adding varbind");
4354 goto err;
4355 }
4356
4357 } /* if var_ref is ok */
4358 } /* for all the vars */
4359 }
4360
4361 pdu->enterprise = (oid *)netsnmp_malloc(MAX_OID_LEN * sizeof(oid));
4362 tp = __tag2oid(enterprise,NULL, pdu->enterprise,
4363 &pdu->enterprise_length, NULL, best_guess);
4364 if (pdu->enterprise_length == 0) {
4365 if (verbose) warn("error:trap:invalid enterprise id: %s", enterprise);
4366 goto err;
4367 }
4368 /* If agent is given then set the v1-TRAP specific
4369 agent-address field to that. Otherwise set it to
4370 our address. */
4371 if (agent && strlen(agent)) {
4372 if (0 > netsnmp_gethostbyname_v4(agent,
4373 (in_addr_t *)pdu->agent_addr)){
4374 if (verbose)
4375 warn("error:trap:invalid agent address: %s", agent);
4376 goto err;
4377 }
4378 } else {
4379 *((in_addr_t *)pdu->agent_addr) = get_myaddr();
4380 }
4381 pdu->trap_type = generic;
4382 pdu->specific_type = specific;
4383 pdu->time = uptime;
4384
4385 if(api_mode == SNMP_API_SINGLE)
4386 {
4387 if(snmp_sess_send(ss,pdu) == 0)
4388 snmp_free_pdu(pdu);
4389 } else {
4390 if (snmp_send(ss, pdu) == 0)
4391 snmp_free_pdu(pdu);
4392 }
4393 XPUSHs(sv_2mortal(newSVpv(ZERO_BUT_TRUE,0)));
4394 } else {
4395 err:
4396 XPUSHs(&sv_undef); /* no mem or bad args */
4397 if (pdu) snmp_free_pdu(pdu);
4398 }
4399 Safefree(oid_arr);
4400 }
4401
4402
4403 void
4404 snmp_trapV2(sess_ref,uptime,trap_oid,varlist_ref)
4405 SV * sess_ref
4406 char * uptime
4407 char * trap_oid
4408 SV * varlist_ref
4409 PPCODE:
4410 {
4411 AV *varlist;
4412 SV **varbind_ref;
4413 SV **varbind_val_f;
4414 AV *varbind;
4415 I32 varlist_len;
4416 I32 varlist_ind;
4417 void *ss;
4418 netsnmp_pdu *pdu = NULL;
4419 struct tree *tp;
4420 oid *oid_arr;
4421 size_t oid_arr_len = MAX_OID_LEN;
4422 SV **sess_ptr_sv;
4423 SV **err_str_svp;
4424 SV **err_num_svp;
4425 SV **err_ind_svp;
4426 int type;
4427 int res;
4428 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
4429 int use_enums = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums",8,1));
4430 struct enum_list *ep;
4431 int best_guess;
4432
4433 New (0, oid_arr, MAX_OID_LEN, oid);
4434
4435 if (oid_arr && SvROK(sess_ref)) {
4436
4437 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4438 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4439 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
4440 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
4441 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
4442 sv_setpv(*err_str_svp, "");
4443 sv_setiv(*err_num_svp, 0);
4444 sv_setiv(*err_ind_svp, 0);
4445 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
4446
4447 pdu = snmp_pdu_create(SNMP_MSG_TRAP2);
4448
4449 if (SvROK(varlist_ref)) {
4450 varlist = (AV*) SvRV(varlist_ref);
4451 varlist_len = av_len(varlist);
4452 } else {
4453 varlist = NULL;
4454 varlist_len = -1;
4455 }
4456 /************************************************/
4457 res = __add_var_val_str(pdu, sysUpTime, SYS_UPTIME_OID_LEN,
4458 uptime, strlen(uptime), TYPE_TIMETICKS);
4459
4460 if(res == FAILURE) {
4461 if(verbose) warn("error:trap v2: adding sysUpTime varbind");
4462 goto err;
4463 }
4464
4465 res = __add_var_val_str(pdu, snmpTrapOID, SNMP_TRAP_OID_LEN,
4466 trap_oid ,strlen(trap_oid) ,TYPE_OBJID);
4467
4468 if(res == FAILURE) {
4469 if(verbose) warn("error:trap v2: adding snmpTrapOID varbind");
4470 goto err;
4471 }
4472
4473
4474 /******************************************************/
4475
4476 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
4477 varbind_ref = av_fetch(varlist, varlist_ind, 0);
4478 if (SvROK(*varbind_ref)) {
4479 varbind = (AV*) SvRV(*varbind_ref);
4480
4481 tp=__tag2oid(__av_elem_pv(varbind, VARBIND_TAG_F,NULL),
4482 __av_elem_pv(varbind, VARBIND_IID_F,NULL),
4483 oid_arr, &oid_arr_len, &type, best_guess);
4484
4485 if (oid_arr_len == 0) {
4486 if (verbose)
4487 warn("error:trap v2: unable to determine oid for object");
4488 goto err;
4489 }
4490
4491 if (type == TYPE_UNKNOWN) {
4492 type = __translate_appl_type(
4493 __av_elem_pv(varbind, VARBIND_TYPE_F, NULL));
4494 if (type == TYPE_UNKNOWN) {
4495 if (verbose)
4496 warn("error:trap v2: no type found for object");
4497 goto err;
4498 }
4499 }
4500
4501 varbind_val_f = av_fetch(varbind, VARBIND_VAL_F, 0);
4502
4503 if (type==TYPE_INTEGER && use_enums && tp && tp->enums) {
4504 for(ep = tp->enums; ep; ep = ep->next) {
4505 if (varbind_val_f && SvOK(*varbind_val_f) &&
4506 !strcmp(ep->label, SvPV(*varbind_val_f,na))) {
4507 sv_setiv(*varbind_val_f, ep->value);
4508 break;
4509 }
4510 }
4511 }
4512
4513 res = __add_var_val_str(pdu, oid_arr, oid_arr_len,
4514 (varbind_val_f && SvOK(*varbind_val_f) ?
4515 SvPV(*varbind_val_f,na):NULL),
4516 (varbind_val_f && SvPOK(*varbind_val_f) ?
4517 SvCUR(*varbind_val_f):0),
4518 type);
4519
4520 if(res == FAILURE) {
4521 if(verbose) warn("error:trap v2: adding varbind");
4522 goto err;
4523 }
4524
4525 } /* if var_ref is ok */
4526 } /* for all the vars */
4527
4528 if(api_mode == SNMP_API_SINGLE)
4529 {
4530 if (snmp_sess_send(ss, pdu) == 0)
4531 snmp_free_pdu(pdu);
4532 } else {
4533 if (snmp_send(ss, pdu) == 0)
4534 snmp_free_pdu(pdu);
4535 }
4536
4537 XPUSHs(sv_2mortal(newSVpv(ZERO_BUT_TRUE,0)));
4538 } else {
4539 err:
4540 XPUSHs(&sv_undef); /* no mem or bad args */
4541 if (pdu) snmp_free_pdu(pdu);
4542 }
4543 Safefree(oid_arr);
4544 }
4545
4546
4547
4548 void
4549 snmp_inform(sess_ref,uptime,trap_oid,varlist_ref,perl_callback)
4550 SV * sess_ref
4551 char * uptime
4552 char * trap_oid
4553 SV * varlist_ref
4554 SV * perl_callback
4555 PPCODE:
4556 {
4557 AV *varlist;
4558 SV **varbind_ref;
4559 SV **varbind_val_f;
4560 AV *varbind;
4561 I32 varlist_len;
4562 I32 varlist_ind;
4563 void *ss;
4564 netsnmp_pdu *pdu = NULL;
4565 netsnmp_pdu *response;
4566 struct tree *tp;
4567 oid *oid_arr;
4568 size_t oid_arr_len = MAX_OID_LEN;
4569 snmp_xs_cb_data *xs_cb_data;
4570 SV **sess_ptr_sv;
4571 SV **err_str_svp;
4572 SV **err_num_svp;
4573 SV **err_ind_svp;
4574 int status = 0;
4575 int type;
4576 int res;
4577 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
4578 int use_enums = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"UseEnums",8,1));
4579 struct enum_list *ep;
4580 int best_guess;
4581
4582 New (0, oid_arr, MAX_OID_LEN, oid);
4583
4584 if (oid_arr && SvROK(sess_ref) && SvROK(varlist_ref)) {
4585
4586 sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4587 ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4588 err_str_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorStr", 8, 1);
4589 err_num_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorNum", 8, 1);
4590 err_ind_svp = hv_fetch((HV*)SvRV(sess_ref), "ErrorInd", 8, 1);
4591 sv_setpv(*err_str_svp, "");
4592 sv_setiv(*err_num_svp, 0);
4593 sv_setiv(*err_ind_svp, 0);
4594 best_guess = SvIV(*hv_fetch((HV*)SvRV(sess_ref),"BestGuess",9,1));
4595
4596 pdu = snmp_pdu_create(SNMP_MSG_INFORM);
4597
4598 varlist = (AV*) SvRV(varlist_ref);
4599 varlist_len = av_len(varlist);
4600 /************************************************/
4601 res = __add_var_val_str(pdu, sysUpTime, SYS_UPTIME_OID_LEN,
4602 uptime, strlen(uptime), TYPE_TIMETICKS);
4603
4604 if(res == FAILURE) {
4605 if(verbose) warn("error:inform: adding sysUpTime varbind");
4606 goto err;
4607 }
4608
4609 res = __add_var_val_str(pdu, snmpTrapOID, SNMP_TRAP_OID_LEN,
4610 trap_oid ,strlen(trap_oid) ,TYPE_OBJID);
4611
4612 if(res == FAILURE) {
4613 if(verbose) warn("error:inform: adding snmpTrapOID varbind");
4614 goto err;
4615 }
4616
4617
4618 /******************************************************/
4619
4620 for(varlist_ind = 0; varlist_ind <= varlist_len; varlist_ind++) {
4621 varbind_ref = av_fetch(varlist, varlist_ind, 0);
4622 if (SvROK(*varbind_ref)) {
4623 varbind = (AV*) SvRV(*varbind_ref);
4624
4625 tp=__tag2oid(__av_elem_pv(varbind, VARBIND_TAG_F,NULL),
4626 __av_elem_pv(varbind, VARBIND_IID_F,NULL),
4627 oid_arr, &oid_arr_len, &type, best_guess);
4628
4629 if (oid_arr_len == 0) {
4630 if (verbose)
4631 warn("error:inform: unable to determine oid for object");
4632 goto err;
4633 }
4634
4635 if (type == TYPE_UNKNOWN) {
4636 type = __translate_appl_type(
4637 __av_elem_pv(varbind, VARBIND_TYPE_F, NULL));
4638 if (type == TYPE_UNKNOWN) {
4639 if (verbose)
4640 warn("error:inform: no type found for object");
4641 goto err;
4642 }
4643 }
4644
4645 varbind_val_f = av_fetch(varbind, VARBIND_VAL_F, 0);
4646
4647 if (type==TYPE_INTEGER && use_enums && tp && tp->enums) {
4648 for(ep = tp->enums; ep; ep = ep->next) {
4649 if (varbind_val_f && SvOK(*varbind_val_f) &&
4650 !strcmp(ep->label, SvPV(*varbind_val_f,na))) {
4651 sv_setiv(*varbind_val_f, ep->value);
4652 break;
4653 }
4654 }
4655 }
4656
4657 res = __add_var_val_str(pdu, oid_arr, oid_arr_len,
4658 (varbind_val_f && SvOK(*varbind_val_f) ?
4659 SvPV(*varbind_val_f,na):NULL),
4660 (varbind_val_f && SvPOK(*varbind_val_f) ?
4661 SvCUR(*varbind_val_f):0),
4662 type);
4663
4664 if(res == FAILURE) {
4665 if(verbose) warn("error:inform: adding varbind");
4666 goto err;
4667 }
4668
4669 } /* if var_ref is ok */
4670 } /* for all the vars */
4671
4672
4673 if (SvTRUE(perl_callback)) {
4674 xs_cb_data =
4675 (snmp_xs_cb_data*)malloc(sizeof(snmp_xs_cb_data));
4676 xs_cb_data->perl_cb = newSVsv(perl_callback);
4677 xs_cb_data->sess_ref = newRV_inc(SvRV(sess_ref));
4678
4679 if(api_mode == SNMP_API_SINGLE)
4680 {
4681 status = snmp_sess_async_send(ss, pdu, __snmp_xs_cb,
4682 (void*)xs_cb_data);
4683 } else {
4684 status = snmp_async_send(ss, pdu, __snmp_xs_cb,
4685 (void*)xs_cb_data);
4686 }
4687 if (status != 0) {
4688 XPUSHs(sv_2mortal(newSViv(status))); /* push the reqid?? */
4689 } else {
4690 snmp_free_pdu(pdu);
4691 snmp_return_err(ss, *err_str_svp, *err_num_svp, *err_ind_svp);
4692 XPUSHs(&sv_undef);
4693 }
4694 goto done;
4695 }
4696
4697 status = __send_sync_pdu(ss, pdu, &response,
4698 NO_RETRY_NOSUCH,
4699 *err_str_svp, *err_num_svp,
4700 *err_ind_svp);
4701
4702 if (response) snmp_free_pdu(response);
4703
4704 if (status) {
4705 XPUSHs(&sv_undef);
4706 } else {
4707 XPUSHs(sv_2mortal(newSVpv(ZERO_BUT_TRUE,0)));
4708 }
4709 } else {
4710 err:
4711 XPUSHs(&sv_undef); /* no mem or bad args */
4712 if (pdu) snmp_free_pdu(pdu);
4713 }
4714 done:
4715 Safefree(oid_arr);
4716 }
4717
4718
4719
4720 char *
4721 snmp_get_sec_engine_id(sess_ref)
4722 SV * sess_ref
4723 CODE:
4724 {
4725 RETVAL = NULL;
4726 if (SvROK(sess_ref)) {
4727 SV **sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4728 SnmpSession *ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4729 if (ss->securityEngineIDLen > 0) {
4730 binary_to_hex(ss->securityEngineID,
4731 ss->securityEngineIDLen,
4732 &RETVAL);
4733 }
4734 }
4735 }
4736 OUTPUT:
4737 RETVAL
4738 CLEANUP:
4739 netsnmp_free(RETVAL);
4740
4741
4742 char *
4743 snmp_get_context_engine_id(sess_ref)
4744 SV * sess_ref
4745 CODE:
4746 {
4747 RETVAL = NULL;
4748 if (SvROK(sess_ref)) {
4749 SV **sess_ptr_sv = hv_fetch((HV*)SvRV(sess_ref), "SessPtr", 7, 1);
4750 SnmpSession *ss = (SnmpSession *)SvIV((SV*)SvRV(*sess_ptr_sv));
4751 if (ss->contextEngineIDLen > 0) {
4752 binary_to_hex(ss->contextEngineID,
4753 ss->contextEngineIDLen,
4754 &RETVAL);
4755 }
4756 }
4757 }
4758 OUTPUT:
4759 RETVAL
4760 CLEANUP:
4761 netsnmp_free(RETVAL);
4762
4763
4764 char *
4765 snmp_get_type(tag, best_guess)
4766 char * tag
4767 int best_guess
4768 CODE:
4769 {
4770 struct tree *tp = NULL;
4771 static char type_str[MAX_TYPE_NAME_LEN];
4772 char *ret = NULL;
4773
4774 if (tag && *tag) tp = __tag2oid(tag, NULL, NULL, NULL, NULL, best_guess);
4775 if (tp) __get_type_str(tp->type, ret = type_str);
4776 RETVAL = ret;
4777 }
4778 OUTPUT:
4779 RETVAL
4780
4781
4782 void
4783 snmp_dump_packet(flag)
4784 int flag
4785 CODE:
4786 {
4787 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
4788 NETSNMP_DS_LIB_DUMP_PACKET, flag);
4789 }
4790
4791
4792 char *
4793 snmp_map_enum(tag, val, iflag, best_guess)
4794 char * tag
4795 char * val
4796 int iflag
4797 int best_guess
4798 CODE:
4799 {
4800 struct tree *tp = NULL;
4801 struct enum_list *ep;
4802 static char str_buf[STR_BUF_SIZE];
4803 int ival;
4804
4805 RETVAL = NULL;
4806
4807 if (tag && *tag) tp = __tag2oid(tag, NULL, NULL, NULL, NULL, best_guess);
4808
4809 if (tp) {
4810 if (iflag) {
4811 ival = atoi(val);
4812 for(ep = tp->enums; ep; ep = ep->next) {
4813 if (ep->value == ival) {
4814 RETVAL = ep->label;
4815 break;
4816 }
4817 }
4818 } else {
4819 for(ep = tp->enums; ep; ep = ep->next) {
4820 if (strEQ(ep->label, val)) {
4821 sprintf(str_buf,"%d", ep->value);
4822 RETVAL = str_buf;
4823 break;
4824 }
4825 }
4826 }
4827 }
4828 }
4829 OUTPUT:
4830 RETVAL
4831
4832 #define SNMP_XLATE_MODE_OID2TAG 1
4833 #define SNMP_XLATE_MODE_TAG2OID 0
4834
4835 char *
4836 snmp_translate_obj(var,mode,use_long,auto_init,best_guess,include_module_name)
4837 char * var
4838 int mode
4839 int use_long
4840 int auto_init
4841 int best_guess
4842 int include_module_name
4843 CODE:
4844 {
4845 static char str_buf[STR_BUF_SIZE];
4846 char str_buf_temp[STR_BUF_SIZE];
4847 oid oid_arr[MAX_OID_LEN];
4848 size_t oid_arr_len = MAX_OID_LEN;
4849 char * label;
4850 char * iid;
4851 int status = FAILURE;
4852 int verbose = SvIV(perl_get_sv("SNMP::verbose", 0x01 | 0x04));
4853 struct tree *module_tree = NULL;
4854 char modbuf[256];
4855 int old_format; /* Current NETSNMP_DS_LIB_OID_OUTPUT_FORMAT */
4856
4857 str_buf[0] = '\0';
4858 str_buf_temp[0] = '\0';
4859
4860 if (auto_init)
4861 netsnmp_init_mib(); /* vestigial */
4862
4863 /* Save old output format and set to FULL so long_names works */
4864 old_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
4865 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, NETSNMP_OID_OUTPUT_FULL);
4866
4867 switch (mode) {
4868 case SNMP_XLATE_MODE_TAG2OID:
4869 if (!__tag2oid(var, NULL, oid_arr, &oid_arr_len, NULL, best_guess)) {
4870 if (verbose) warn("error:snmp_translate_obj:Unknown OID %s\n",var);
4871 } else {
4872 status = __snprint_num_objid(str_buf, sizeof(str_buf), oid_arr, oid_arr_len);
4873 }
4874 break;
4875 case SNMP_XLATE_MODE_OID2TAG:
4876 oid_arr_len = 0;
4877 __concat_oid_str(oid_arr, &oid_arr_len, var);
4878 snprint_objid(str_buf_temp, sizeof(str_buf_temp), oid_arr, oid_arr_len);
4879
4880 if (!use_long) {
4881 label = NULL; iid = NULL;
4882 if (((status=__get_label_iid(str_buf_temp,
4883 &label, &iid, NO_FLAGS)) == SUCCESS)
4884 && label) {
4885 strlcpy(str_buf_temp, label, sizeof(str_buf_temp));
4886 if (iid && *iid) {
4887 strlcat(str_buf_temp, ".", sizeof(str_buf_temp));
4888 strlcat(str_buf_temp, iid, sizeof(str_buf_temp));
4889 }
4890 }
4891 }
4892
4893 /* Prepend modulename:: if enabled */
4894 if (include_module_name) {
4895 module_tree = get_tree (oid_arr, oid_arr_len, get_tree_head());
4896 if (module_tree) {
4897 if (strcmp(module_name(module_tree->modid, modbuf), "#-1") ) {
4898 strcat(str_buf, modbuf);
4899 strcat(str_buf, "::");
4900 }
4901 else {
4902 strcat(str_buf, "UNKNOWN::");
4903 }
4904 }
4905 }
4906 strcat(str_buf, str_buf_temp);
4907
4908 break;
4909 default:
4910 if (verbose) warn("snmp_translate_obj:unknown translation mode: %d\n", mode);
4911 }
4912 if (*str_buf) {
4913 RETVAL = str_buf;
4914 } else {
4915 RETVAL = NULL;
4916 }
4917 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, old_format);
4918 }
4919 OUTPUT:
4920 RETVAL
4921
4922 void
4923 snmp_set_replace_newer(val)
4924 int val
4925 CODE:
4926 {
4927 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
4928 NETSNMP_DS_LIB_MIB_REPLACE, val);
4929 }
4930
4931 void
4932 snmp_set_save_descriptions(val)
4933 int val
4934 CODE:
4935 {
4936 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
4937 NETSNMP_DS_LIB_SAVE_MIB_DESCRS, val);
4938 }
4939
4940 void
4941 snmp_set_debugging(val)
4942 int val
4943 CODE:
4944 {
4945 snmp_set_do_debugging(val);
4946 }
4947
4948 void
4949 snmp_register_debug_tokens(tokens)
4950 char *tokens
4951 CODE:
4952 {
4953 debug_register_tokens(tokens);
4954 snmp_set_do_debugging(1);
4955 }
4956
4957 void
4958 snmp_debug_internals(val)
4959 int val
4960 CODE:
4961 {
4962 #ifdef DEBUGGING
4963 _debug_level = val;
4964 #else
4965 val++;
4966 #endif /* DEBUGGING */
4967 }
4968
4969
4970 void
4971 snmp_mib_toggle_options(options)
4972 char *options
4973 CODE:
4974 {
4975 snmp_mib_toggle_options(options);
4976 }
4977
4978 void
4979 snmp_sock_cleanup()
4980 CODE:
4981 {
4982 SOCK_CLEANUP;
4983 }
4984
4985 void
4986 snmp_mainloop_finish()
4987 CODE:
4988 {
4989 mainloop_finish = 1;
4990 }
4991
4992
4993 #-----------------------------------------------------------------------------
4994 # Note: ss=(SnmpSession*)NULL is so &SNMP::MainLoop() can still be called
4995 # without a sess handler argument, this way I'm not breaking anyone's old code
4996 #
4997 # see MainLoop() in SNMP.pm for more details
4998 #-----------------------------------------------------------------------------
4999 void
5000 snmp_main_loop(timeout_sec,timeout_usec,perl_callback,ss=(SnmpSession*)NULL)
5001 int timeout_sec
5002 int timeout_usec
5003 SV * perl_callback
5004 void * ss
5005 CODE:
5006 {
5007 int numfds, fd_count;
5008 fd_set fdset;
5009 struct timeval time_val, *tvp;
5010 struct timeval last_time, *ltvp;
5011 struct timeval ctimeout, *ctvp;
5012 struct timeval interval, *itvp;
5013 int block;
5014 SV *cb;
5015
5016 mainloop_finish = 0;
5017
5018 itvp = &interval;
5019 itvp->tv_sec = timeout_sec;
5020 itvp->tv_usec = timeout_usec;
5021 ctvp = &ctimeout;
5022 ctvp->tv_sec = -1;
5023 ctvp->tv_usec = 0;
5024 ltvp = &last_time;
5025 gettimeofday(ltvp,(struct timezone*)0);
5026 timersub(ltvp,itvp,ltvp);
5027 while (1) {
5028 numfds = 0;
5029 FD_ZERO(&fdset);
5030 block = 1;
5031 tvp = &time_val;
5032 timerclear(tvp);
5033 if(api_mode == SNMP_API_SINGLE)
5034 {
5035 snmp_sess_select_info(ss,&numfds, &fdset, tvp, &block);
5036 } else {
5037 snmp_select_info(&numfds, &fdset, tvp, &block);
5038 }
5039 __recalc_timeout(tvp,ctvp,ltvp,itvp,&block);
5040 # printf("pre-select: numfds = %ld, block = %ld\n", numfds, block);
5041 if (block == 1) tvp = NULL; /* block without timeout */
5042 fd_count = select(numfds, &fdset, 0, 0, tvp);
5043 #printf("post-select: fd_count = %ld,block = %ld\n",fd_count,block);
5044 if (fd_count > 0) {
5045 ENTER;
5046 SAVETMPS;
5047 if(api_mode == SNMP_API_SINGLE)
5048 {
5049 snmp_sess_read(ss, &fdset);
5050 } else {
5051 snmp_read(&fdset);
5052 }
5053 FREETMPS;
5054 LEAVE;
5055
5056 } else switch(fd_count) {
5057 case 0:
5058 SPAGAIN;
5059 ENTER;
5060 SAVETMPS;
5061 if(api_mode == SNMP_API_SINGLE)
5062 {
5063 snmp_sess_timeout( ss );
5064 } else {
5065 snmp_timeout();
5066 }
5067 if (!timerisset(ctvp)) {
5068 if (SvTRUE(perl_callback)) {
5069 /* sv_2mortal(perl_callback); */
5070 cb = __push_cb_args(perl_callback, NULL);
5071 __call_callback(cb, G_DISCARD);
5072 ctvp->tv_sec = -1;
5073
5074 } else {
5075 FREETMPS;
5076 LEAVE;
5077 goto done;
5078 }
5079 }
5080 FREETMPS;
5081 LEAVE;
5082 break;
5083 case -1:
5084 if (errno == EINTR) {
5085 continue;
5086 } else {
5087 /* snmp_set_detail(strerror(errno)); */
5088 /* snmp_errno = SNMPERR_GENERR; */
5089 }
5090 default:;
5091 }
5092
5093 /* A call to snmp_mainloop_finish() in the callback sets the
5094 ** mainloop_finish flag. Exit the loop after the callback returns.
5095 */
5096 if (mainloop_finish)
5097 goto done;
5098
5099 }
5100 done:
5101 return;
5102 }
5103
5104
5105 void
5106 snmp_get_select_info()
5107 PPCODE:
5108 {
5109 int numfds;
5110 fd_set fdset;
5111 struct timeval time_val, *tvp;
5112 int block;
5113 int i;
5114
5115 numfds = 0;
5116 block = 1;
5117 tvp = &time_val;
5118 FD_ZERO(&fdset);
5119 snmp_select_info(&numfds, &fdset, tvp, &block);
5120 XPUSHs(sv_2mortal(newSViv(block)));
5121 if(block){
5122 XPUSHs(sv_2mortal(newSViv(0)));
5123 XPUSHs(sv_2mortal(newSViv(0)));
5124 } else {
5125 XPUSHs(sv_2mortal(newSViv(tvp->tv_sec)));
5126 XPUSHs(sv_2mortal(newSViv(tvp->tv_usec)));
5127 }
5128 if ( numfds ) {
5129 for(i=0; i<numfds ; i++) {
5130 if(FD_ISSET(i, &fdset)){
5131 XPUSHs(sv_2mortal(newSViv(i)));
5132 }
5133 }
5134 } else {
5135 XPUSHs(&sv_undef); /* no mem or bad args */
5136 }
5137 }
5138
5139 void
5140 snmp_read_on_fd(fd)
5141 int fd
5142 CODE:
5143 {
5144 fd_set fdset;
5145
5146 FD_ZERO(&fdset);
5147 FD_SET(fd, &fdset);
5148
5149 snmp_read(&fdset);
5150 }
5151
5152 void
5153 snmp_check_timeout()
5154 CODE:
5155 {
5156 snmp_timeout();
5157 }
5158
5159 MODULE = SNMP PACKAGE = SNMP::MIB::NODE PREFIX = snmp_mib_node_
5160
5161 SV *
5162 snmp_mib_node_TIEHASH(cl,key,tp=0)
5163 char * cl
5164 char * key
5165 IV tp
5166 CODE:
5167 {
5168 __libraries_init("perl");
5169 if (!tp) tp = (IV)__tag2oid(key, NULL, NULL, NULL, NULL,0);
5170 if (tp) {
5171 RETVAL = sv_setref_iv(newSV(0), cl, tp);
5172 } else {
5173 RETVAL = &sv_undef;
5174 }
5175 }
5176 OUTPUT:
5177 RETVAL
5178
5179
5180 SV *
5181 snmp_mib_node_FETCH(tp_ref, key)
5182 SV * tp_ref
5183 char * key
5184 CODE:
5185 {
5186 char c = *key;
5187 char str_buf[STR_BUF_SIZE];
5188 SnmpMibNode *tp = NULL, *tptmp = NULL;
5189 struct index_list *ip;
5190 struct enum_list *ep;
5191 struct range_list *rp;
5192 struct varbind_list *vp;
5193 struct module *mp;
5194 SV *child_list_aref, *next_node_href, *mib_tied_href = NULL;
5195 SV **nn_hrefp;
5196 HV *mib_hv, *enum_hv, *range_hv;
5197 AV *index_av, *varbind_av, *ranges_av;
5198 MAGIC *mg = NULL;
5199 SV *ret = NULL;
5200
5201 if (SvROK(tp_ref)) tp = (SnmpMibNode*)SvIV((SV*)SvRV(tp_ref));
5202
5203 ret = newSV(0);
5204 if (tp)
5205 switch (c) {
5206 case 'a': /* access */
5207 if (strncmp("access", key, strlen(key)) == 0) {
5208 switch (tp->access) {
5209 case MIB_ACCESS_READONLY:
5210 sv_setpv(ret,"ReadOnly");
5211 break;
5212 case MIB_ACCESS_READWRITE:
5213 sv_setpv(ret,"ReadWrite");
5214 break;
5215 case MIB_ACCESS_WRITEONLY:
5216 sv_setpv(ret,"WriteOnly");
5217 break;
5218 case MIB_ACCESS_NOACCESS:
5219 sv_setpv(ret,"NoAccess");
5220 break;
5221 case MIB_ACCESS_NOTIFY:
5222 sv_setpv(ret,"Notify");
5223 break;
5224 case MIB_ACCESS_CREATE:
5225 sv_setpv(ret,"Create");
5226 break;
5227 default:
5228 break;
5229 }
5230 } else if (strncmp("augments", key, strlen(key)) == 0) {
5231 sv_setpv(ret,tp->augments);
5232 }
5233 break;
5234 case 'c': /* children */
5235 if (strncmp("children", key, strlen(key))) break;
5236 child_list_aref = newRV((SV*)newAV());
5237 for (tp = tp->child_list; tp; tp = tp->next_peer) {
5238 mib_hv = perl_get_hv("SNMP::MIB", FALSE);
5239 if (SvMAGICAL(mib_hv)) mg = mg_find((SV*)mib_hv, 'P');
5240 if (mg) mib_tied_href = (SV*)mg->mg_obj;
5241 next_node_href = newRV((SV*)newHV());
5242 __tp_sprint_num_objid(str_buf, sizeof(str_buf), tp);
5243 nn_hrefp = hv_fetch((HV*)SvRV(mib_tied_href),
5244 str_buf, strlen(str_buf), 1);
5245 if (!SvROK(*nn_hrefp)) {
5246 sv_setsv(*nn_hrefp, next_node_href);
5247 ENTER ;
5248 SAVETMPS ;
5249 PUSHMARK(sp) ;
5250 XPUSHs(SvRV(*nn_hrefp));
5251 XPUSHs(sv_2mortal(newSVpv("SNMP::MIB::NODE",0)));
5252 XPUSHs(sv_2mortal(newSVpv(str_buf,0)));
5253 XPUSHs(sv_2mortal(newSViv((IV)tp)));
5254 PUTBACK ;
5255 perl_call_pv("SNMP::_tie",G_VOID);
5256 /* pp_tie(ARGS); */
5257 SPAGAIN ;
5258 FREETMPS ;
5259 LEAVE ;
5260 } /* if SvROK */
5261 av_push((AV*)SvRV(child_list_aref), *nn_hrefp);
5262 } /* for child_list */
5263 sv_setsv(ret, child_list_aref);
5264 break;
5265 case 'v':
5266 if (strncmp("varbinds", key, strlen(key))) break;
5267 varbind_av = newAV();
5268 for (vp = tp->varbinds; vp; vp = vp->next) {
5269 av_push(varbind_av, newSVpv((vp->vblabel),strlen(vp->vblabel)));
5270 }
5271 sv_setsv(ret, newRV((SV*)varbind_av));
5272 break;
5273 case 'd': /* description */
5274 if (strncmp("description", key, strlen(key))) {
5275 if(!(strncmp("defaultValue",key,strlen(key)))) {
5276 /* We're looking at defaultValue */
5277 sv_setpv(ret, tp->defaultValue);
5278 break;
5279 } /* end if */
5280 } /* end if */
5281 /* we must be looking at description */
5282 sv_setpv(ret,tp->description);
5283 break;
5284 case 'i': /* indexes, implied */
5285 if (tp->augments) {
5286 clear_tree_flags(get_tree_head());
5287 tptmp = find_best_tree_node(tp->augments, get_tree_head(), NULL);
5288 if (tptmp == NULL) {
5289 tptmp = tp;
5290 }
5291 } else {
5292 tptmp = tp;
5293 }
5294 if (strcmp("implied", key) == 0) {
5295 /* only the last index can be implied */
5296 int isimplied = 0;
5297 if (tptmp && tptmp->indexes) {
5298 for(ip=tptmp->indexes; ip->next; ip = ip->next) {
5299 }
5300 isimplied = ip->isimplied;
5301 }
5302 sv_setiv(ret, isimplied);
5303 break;
5304 }
5305 if (strncmp("indexes", key, strlen(key))) break;
5306 index_av = newAV();
5307 if (tptmp)
5308 for(ip=tptmp->indexes; ip != NULL; ip = ip->next) {
5309 av_push(index_av,newSVpv((ip->ilabel),strlen(ip->ilabel)));
5310 }
5311 sv_setsv(ret, newRV((SV*)index_av));
5312 break;
5313 case 'l': /* label */
5314 if (strncmp("label", key, strlen(key))) break;
5315 sv_setpv(ret,tp->label);
5316 break;
5317 case 'm': /* moduleID */
5318 if (strncmp("moduleID", key, strlen(key))) break;
5319 mp = find_module(tp->modid);
5320 if (mp) sv_setpv(ret, mp->name);
5321 break;
5322 case 'n': /* nextNode */
5323 if (strncmp("nextNode", key, strlen(key))) break;
5324 tp = __get_next_mib_node(tp);
5325 if (tp == NULL) {
5326 sv_setsv(ret, &sv_undef);
5327 break;
5328 }
5329 mib_hv = perl_get_hv("SNMP::MIB", FALSE);
5330 if (SvMAGICAL(mib_hv)) mg = mg_find((SV*)mib_hv, 'P');
5331 if (mg) mib_tied_href = (SV*)mg->mg_obj;
5332 __tp_sprint_num_objid(str_buf, sizeof(str_buf), tp);
5333
5334 nn_hrefp = hv_fetch((HV*)SvRV(mib_tied_href),
5335 str_buf, strlen(str_buf), 1);
5336 /* if (!SvROK(*nn_hrefp)) { */ /* bug in ucd - 2 .0.0 nodes */
5337 next_node_href = newRV((SV*)newHV());
5338 sv_setsv(*nn_hrefp, next_node_href);
5339 ENTER ;
5340 SAVETMPS ;
5341 PUSHMARK(sp) ;
5342 XPUSHs(SvRV(*nn_hrefp));
5343 XPUSHs(sv_2mortal(newSVpv("SNMP::MIB::NODE",0)));
5344 XPUSHs(sv_2mortal(newSVpv(str_buf,0)));
5345 XPUSHs(sv_2mortal(newSViv((IV)tp)));
5346 PUTBACK ;
5347 perl_call_pv("SNMP::_tie",G_VOID);
5348 /* pp_tie(ARGS); */
5349 SPAGAIN ;
5350 FREETMPS ;
5351 LEAVE ;
5352 /* } */
5353 sv_setsv(ret, *nn_hrefp);
5354 break;
5355 case 'o': /* objectID */
5356 if (strncmp("objectID", key, strlen(key))) break;
5357 __tp_sprint_num_objid(str_buf, sizeof(str_buf), tp);
5358 sv_setpv(ret,str_buf);
5359 break;
5360 case 'p': /* parent */
5361 if (strncmp("parent", key, strlen(key))) break;
5362 tp = tp->parent;
5363 if (tp == NULL) {
5364 sv_setsv(ret, &sv_undef);
5365 break;
5366 }
5367 mib_hv = perl_get_hv("SNMP::MIB", FALSE);
5368 if (SvMAGICAL(mib_hv)) mg = mg_find((SV*)mib_hv, 'P');
5369 if (mg) mib_tied_href = (SV*)mg->mg_obj;
5370 __tp_sprint_num_objid(str_buf, sizeof(str_buf), tp);
5371 nn_hrefp = hv_fetch((HV*)SvRV(mib_tied_href),
5372 str_buf, strlen(str_buf), 1);
5373 if (!SvROK(*nn_hrefp)) {
5374 next_node_href = newRV((SV*)newHV());
5375 sv_setsv(*nn_hrefp, next_node_href);
5376 ENTER ;
5377 SAVETMPS ;
5378 PUSHMARK(sp) ;
5379 XPUSHs(SvRV(*nn_hrefp));
5380 XPUSHs(sv_2mortal(newSVpv("SNMP::MIB::NODE",0)));
5381 XPUSHs(sv_2mortal(newSVpv(str_buf,0)));
5382 XPUSHs(sv_2mortal(newSViv((IV)tp)));
5383 PUTBACK ;
5384 perl_call_pv("SNMP::_tie",G_VOID);
5385 /* pp_tie(ARGS); */
5386 SPAGAIN ;
5387 FREETMPS ;
5388 LEAVE ;
5389 }
5390 sv_setsv(ret, *nn_hrefp);
5391 break;
5392 case 'r': /* ranges */
5393 if (strncmp("reference", key, strlen(key)) == 0) {
5394 sv_setpv(ret,tp->reference);
5395 break;
5396 }
5397 if (strncmp("ranges", key, strlen(key))) break;
5398 ranges_av = newAV();
5399 for(rp=tp->ranges; rp ; rp = rp->next) {
5400 range_hv = newHV();
5401 (void)hv_store(range_hv, "low", strlen("low"), newSViv(rp->low), 0);
5402 (void)hv_store(range_hv, "high", strlen("high"), newSViv(rp->high), 0);
5403 av_push(ranges_av, newRV((SV*)range_hv));
5404 }
5405 sv_setsv(ret, newRV((SV*)ranges_av));
5406 break;
5407 case 's': /* subID */
5408 if (strncmp("subID", key, strlen(key))) {
5409 if (strncmp("status", key, strlen(key))) {
5410 if (strncmp("syntax", key, strlen(key))) break;
5411 if (tp->tc_index >= 0) {
5412 sv_setpv(ret, get_tc_descriptor(tp->tc_index));
5413 } else {
5414 __get_type_str(tp->type, str_buf);
5415 sv_setpv(ret, str_buf);
5416 }
5417 break;
5418 }
5419
5420 switch(tp->status) {
5421 case MIB_STATUS_MANDATORY:
5422 sv_setpv(ret,"Mandatory");
5423 break;
5424 case MIB_STATUS_OPTIONAL:
5425 sv_setpv(ret,"Optional");
5426 break;
5427 case MIB_STATUS_OBSOLETE:
5428 sv_setpv(ret,"Obsolete");
5429 break;
5430 case MIB_STATUS_DEPRECATED:
5431 sv_setpv(ret,"Deprecated");
5432 break;
5433 case MIB_STATUS_CURRENT:
5434 sv_setpv(ret,"Current");
5435 break;
5436 default:
5437 break;
5438 }
5439 } else {
5440 sv_setiv(ret,(I32)tp->subid);
5441 }
5442 break;
5443 case 't': /* type */
5444 if (strncmp("type", key, strlen(key))) {
5445 if (strncmp("textualConvention", key, strlen(key))) break;
5446 sv_setpv(ret, get_tc_descriptor(tp->tc_index));
5447 break;
5448 }
5449 __get_type_str(tp->type, str_buf);
5450 sv_setpv(ret, str_buf);
5451 break;
5452 case 'T': /* textual convention description */
5453 if (strncmp("TCDescription", key, strlen(key))) break;
5454 sv_setpv(ret, get_tc_description(tp->tc_index));
5455 break;
5456 case 'u': /* units */
5457 if (strncmp("units", key, strlen(key))) break;
5458 sv_setpv(ret,tp->units);
5459 break;
5460 case 'h': /* hint */
5461 if (strncmp("hint", key, strlen(key))) break;
5462 sv_setpv(ret,tp->hint);
5463 break;
5464 case 'e': /* enums */
5465 if (strncmp("enums", key, strlen(key))) break;
5466 enum_hv = newHV();
5467 for(ep=tp->enums; ep != NULL; ep = ep->next) {
5468 (void)hv_store(enum_hv, ep->label, strlen(ep->label),
5469 newSViv(ep->value), 0);
5470 }
5471 sv_setsv(ret, newRV((SV*)enum_hv));
5472 break;
5473 default:
5474 break;
5475 }
5476 RETVAL = ret;
5477 }
5478 OUTPUT:
5479 RETVAL
5480
5481 MODULE = SNMP PACKAGE = SnmpSessionPtr PREFIX = snmp_session_
5482
5483 void
5484 snmp_session_DESTROY(sess_ptr)
5485 void *sess_ptr
5486 CODE:
5487 {
5488 if(sess_ptr != NULL)
5489 {
5490 if(api_mode == SNMP_API_SINGLE)
5491 {
5492 snmp_sess_close( sess_ptr );
5493 } else {
5494 snmp_close( sess_ptr );
5495 }
5496 }
5497 }
5498