Debian Codegraph @

perl/agent/agent.xs

raw
1 /* -*- C -*- */
2
3 #pragma GCC diagnostic push
4 #pragma GCC diagnostic ignored "-Wcast-qual"
5 #pragma GCC diagnostic ignored "-Wdeclaration-after-statement"
6 #include "EXTERN.h"
7 #include "perl.h"
8 #include "XSUB.h"
9 #pragma GCC diagnostic pop
10
11 #include <netdb.h>
12 #include <sys/socket.h>
13
14 #include <net-snmp/net-snmp-config.h>
15 #include <net-snmp/net-snmp-includes.h>
16 #include <net-snmp/agent/net-snmp-agent-includes.h>
17
18 #ifndef sv_undef
19 #define sv_undef PL_sv_undef
20 #endif
21
22 typedef netsnmp_handler_registration *NetSNMP__agent__netsnmp_handler_registration;
23
24 /*
25 * needs to be in sync with the definitions in snmplib/snmpUDPDomain.c
26 * and snmplib/snmpTCPDomain.c
27 */
28 typedef netsnmp_indexed_addr_pair netsnmp_udp_addr_pair;
29
30 typedef struct handler_cb_data_s {
31 SV *perl_cb;
32 } handler_cb_data;
33
34 typedef struct netsnmp_oid_s {
35 oid *name;
36 size_t len;
37 oid namebuf[ MAX_OID_LEN ];
38 } netsnmp_oid;
39
40 #define TEST_CONSTANT(value, name, C) \
41 if (strEQ(name, #C)) { \
42 *value = C; \
43 return 0; \
44 }
45
46 static int constant_MODE_G(double *value, const char *name, const int len)
47 {
48 switch (len >= 8 ? name[8] : -1) {
49 case '\0':
50 TEST_CONSTANT(value, name, MODE_GET);
51 break;
52 case 'B':
53 TEST_CONSTANT(value, name, MODE_GETBULK);
54 break;
55 case 'N':
56 TEST_CONSTANT(value, name, MODE_GETNEXT);
57 break;
58 }
59 return EINVAL;
60 }
61
62 static int constant_MODE_SET_R(double *value, const char *name, const int len)
63 {
64 switch (len >= 16 ? name[16] : -1) {
65 #ifndef NETSNMP_NO_WRITE_SUPPORT
66 case '1':
67 TEST_CONSTANT(value, name, MODE_SET_RESERVE1);
68 break;
69 case '2':
70 TEST_CONSTANT(value, name, MODE_SET_RESERVE2);
71 break;
72 #endif /* NETSNMP_NO_WRITE_SUPPORT */
73 }
74 return EINVAL;
75 }
76
77 static int constant_SNMP_ERR(double *value, char *name, int len)
78 {
79 switch (len >= 9 ? name[9] : -1) {
80 case 'A':
81 TEST_CONSTANT(value, name, SNMP_ERR_AUTHORIZATIONERROR);
82 break;
83 case 'B':
84 TEST_CONSTANT(value, name, SNMP_ERR_BADVALUE);
85 break;
86 case 'C':
87 TEST_CONSTANT(value, name, SNMP_ERR_COMMITFAILED);
88 break;
89 case 'G':
90 TEST_CONSTANT(value, name, SNMP_ERR_GENERR);
91 break;
92 case 'I':
93 TEST_CONSTANT(value, name, SNMP_ERR_INCONSISTENTVALUE);
94 break;
95 case 'N':
96 TEST_CONSTANT(value, name, SNMP_ERR_NOACCESS);
97 TEST_CONSTANT(value, name, SNMP_ERR_NOCREATION);
98 TEST_CONSTANT(value, name, SNMP_ERR_NOERROR);
99 TEST_CONSTANT(value, name, SNMP_ERR_NOSUCHNAME);
100 TEST_CONSTANT(value, name, SNMP_ERR_NOTWRITABLE);
101 break;
102 case 'R':
103 TEST_CONSTANT(value, name, SNMP_ERR_READONLY);
104 TEST_CONSTANT(value, name, SNMP_ERR_RESOURCEUNAVAILABLE);
105 break;
106 case 'T':
107 TEST_CONSTANT(value, name, SNMP_ERR_TOOBIG);
108 break;
109 case 'U':
110 TEST_CONSTANT(value, name, SNMP_ERR_UNDOFAILED);
111 break;
112 case 'W':
113 TEST_CONSTANT(value, name, SNMP_ERR_WRONGENCODING);
114 TEST_CONSTANT(value, name, SNMP_ERR_WRONGLENGTH);
115 TEST_CONSTANT(value, name, SNMP_ERR_WRONGTYPE);
116 TEST_CONSTANT(value, name, SNMP_ERR_WRONGVALUE);
117 break;
118 }
119 return EINVAL;
120 }
121
122 static int constant_MODE_S(double *value, char *name, int len)
123 {
124 if (!strnEQ(name + 6, "ET_", 3))
125 return EINVAL;
126
127 switch (len >= 9 ? name[9] : -1) {
128 #ifndef NETSNMP_NO_WRITE_SUPPORT
129 case 'A':
130 TEST_CONSTANT(value, name, MODE_SET_ACTION);
131 break;
132 case 'B':
133 TEST_CONSTANT(value, name, MODE_SET_BEGIN);
134 break;
135 case 'C':
136 TEST_CONSTANT(value, name, MODE_SET_COMMIT);
137 break;
138 case 'F':
139 TEST_CONSTANT(value, name, MODE_SET_FREE);
140 break;
141 case 'U':
142 TEST_CONSTANT(value, name, MODE_SET_UNDO);
143 break;
144 #endif /* NETSNMP_NO_WRITE_SUPPORT */
145 case 'R':
146 return constant_MODE_SET_R(value, name, len);
147 }
148 return EINVAL;
149 }
150
151 static int constant(double *value, char *name, int len)
152 {
153 switch (len >= 5 ? name[5] : -1) {
154 case 'G':
155 if (strnEQ(name + 0,"MODE_", 5))
156 return constant_MODE_G(value, name, len);
157 break;
158 case 'S':
159 if (strnEQ(name + 0,"MODE_", 5))
160 return constant_MODE_S(value, name, len);
161 break;
162 case 'E':
163 if (strnEQ(name + 0,"SNMP_ERR_", 9))
164 return constant_SNMP_ERR(value, name, len);
165 break;
166 }
167 return EINVAL;
168 }
169
170 int
171 handler_wrapper(netsnmp_mib_handler *handler,
172 netsnmp_handler_registration *reginfo,
173 netsnmp_agent_request_info *reqinfo,
174 netsnmp_request_info *requests)
175 {
176 handler_cb_data *cb_data = (handler_cb_data *) handler->myvoid;
177 SV *cb;
178
179 if (cb_data && (cb = cb_data->perl_cb)) {
180 SV *arg;
181 SV *rarg;
182 dSP;
183 ENTER;
184 SAVETMPS;
185 PUSHMARK(sp);
186 rarg = newSViv(0);
187 arg = newSVrv(rarg, "NetSNMP::agent::netsnmp_mib_handler");
188 sv_setiv(arg, (IV) handler);
189 XPUSHs(sv_2mortal(rarg));
190 rarg = newSViv(0);
191 arg = newSVrv(rarg, "NetSNMP::agent::netsnmp_handler_registrationPtr");
192 sv_setiv(arg, (IV) reginfo);
193 XPUSHs(sv_2mortal(rarg));
194 rarg = newSViv(0);
195 arg = newSVrv(rarg, "NetSNMP::agent::netsnmp_agent_request_info");
196 sv_setiv(arg, (IV) reqinfo);
197 XPUSHs(sv_2mortal(rarg));
198 rarg = newSViv(0);
199 arg = newSVrv(rarg, "NetSNMP::agent::netsnmp_request_infoPtr");
200 sv_setiv(arg, (IV) requests);
201 XPUSHs(sv_2mortal(rarg));
202 PUTBACK;
203 if (SvTYPE(cb) == SVt_PVCV) {
204 perl_call_sv(cb, G_DISCARD);
205
206 } else if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) {
207 perl_call_sv(SvRV(cb), G_DISCARD);
208 }
209 SPAGAIN;
210 PUTBACK;
211 FREETMPS;
212 LEAVE;
213 }
214 return SNMP_ERR_NOERROR;
215 }
216
217 MODULE = NetSNMP::agent PACKAGE = NetSNMP::agent
218
219 void
220 constant(sv)
221 PREINIT:
222 STRLEN len;
223 INPUT:
224 SV * sv
225 char * s = SvPV(sv, len);
226 INIT:
227 int status;
228 double value;
229 PPCODE:
230 value = 0;
231 status = constant(&value, s, len);
232 XPUSHs(sv_2mortal(newSVuv(status)));
233 XPUSHs(sv_2mortal(newSVnv(value)));
234
235 int
236 __agent_check_and_process(block = 1)
237 int block;
238 CODE:
239 RETVAL = agent_check_and_process(block);
240 OUTPUT:
241 RETVAL
242
243 int
244 _uptime()
245 CODE:
246 RETVAL = netsnmp_get_agent_uptime();
247 OUTPUT:
248 RETVAL
249
250 void
251 init_mib()
252 CODE:
253 {
254 netsnmp_init_mib();
255 }
256
257 int
258 init_agent(name)
259 const char *name;
260 CODE:
261 SOCK_STARTUP;
262 RETVAL = init_agent(name);
263 OUTPUT:
264 RETVAL
265
266 void
267 init_snmp(name)
268 const char *name;
269
270 int
271 init_master_agent()
272
273 void
274 snmp_enable_stderrlog()
275
276 MODULE = NetSNMP::agent PACKAGE = NetSNMP::agent PREFIX = na_
277
278 void
279 na_shutdown(me)
280 SV *me;
281 CODE:
282 {
283 if (0)
284 printf("me = %p\n", me);
285 snmp_shutdown("perl");
286 }
287
288 void
289 na_errlog(me,value)
290 SV *me;
291 SV *value;
292 PREINIT:
293 STRLEN stringlen;
294 char * stringptr;
295 CODE:
296 {
297 if (0)
298 printf("me = %p\n", me);
299 stringptr = SvPV(value, stringlen);
300 snmp_log(LOG_ERR, "%s", stringptr );
301 }
302
303
304
305 MODULE = NetSNMP::agent PACKAGE = NetSNMP::agent::netsnmp_handler_registration PREFIX = nsahr_
306
307 NetSNMP::agent::netsnmp_handler_registration
308 nsahr_new(name, regoid, perlcallback)
309 char *name;
310 char *regoid;
311 SV *perlcallback;
312 PREINIT:
313 oid myoid[MAX_OID_LEN];
314 size_t myoid_len = MAX_OID_LEN;
315 handler_cb_data *cb_data;
316 int gotit=1;
317 CODE:
318 if (!snmp_parse_oid(regoid, myoid, &myoid_len)) {
319 if (!read_objid(regoid, myoid, &myoid_len)) {
320 snmp_log(LOG_ERR, "couldn't parse %s (reg name: %s)\n",
321 regoid, name);
322 RETVAL = NULL;
323 gotit = 0;
324 }
325 }
326 if (gotit) {
327 cb_data = (handler_cb_data *) malloc(sizeof(handler_cb_data));
328 RETVAL = netsnmp_create_handler_registration(name, handler_wrapper,
329 myoid, myoid_len,
330 HANDLER_CAN_RWRITE);
331 cb_data->perl_cb = newSVsv(perlcallback);
332 RETVAL->handler->myvoid = cb_data;
333 }
334 OUTPUT:
335 RETVAL
336
337 void
338 nsahr_DESTROY(reginfo)
339 netsnmp_handler_registration *reginfo
340 PREINIT:
341 handler_cb_data *cb_data;
342 CODE:
343 if (reginfo && reginfo->handler && reginfo->handler->myvoid) {
344 cb_data = (handler_cb_data *) (reginfo->handler->myvoid);
345 SvREFCNT_dec(cb_data->perl_cb);
346 free(cb_data);
347 }
348 netsnmp_handler_registration_free(reginfo);
349
350 int
351 nsahr_register(me)
352 SV *me;
353 PREINIT:
354 netsnmp_handler_registration *reginfo;
355 handler_cb_data *cb_data = NULL;
356 CODE:
357 {
358 reginfo = (netsnmp_handler_registration *) SvIV(SvRV(me));
359 if (reginfo && reginfo->handler && reginfo->handler->myvoid)
360 cb_data = (handler_cb_data *) (reginfo->handler->myvoid);
361 RETVAL = netsnmp_register_handler(reginfo);
362 if (!RETVAL) {
363 /* the agent now has a "reference" to this reg pointer */
364 SvREFCNT_inc(me);
365 } else {
366 /*
367 * The reginfo was freed by netsnmp_register_handler,
368 * don't touch it in nsahr_DESTROY!
369 */
370 sv_setiv(SvRV(me), 0);
371 if (cb_data) {
372 /* And just free the callback. */
373 SvREFCNT_dec(cb_data->perl_cb);
374 free(cb_data);
375 }
376 }
377 }
378 OUTPUT:
379 RETVAL
380
381
382 MODULE = NetSNMP::agent PACKAGE = NetSNMP::agent::netsnmp_handler_registrationPtr PREFIX = nsahr_
383
384 void
385 nsahr_getRootOID(me)
386 SV *me;
387 PREINIT:
388 int i;
389 netsnmp_oid *o;
390 netsnmp_handler_registration *reginfo;
391 SV *arg, *rarg;
392 PPCODE:
393 {
394 dSP;
395 PUSHMARK(SP);
396 reginfo = (netsnmp_handler_registration *) SvIV(SvRV(me));
397
398 o = malloc(sizeof(netsnmp_oid));
399 o->name = o->namebuf;
400 o->len = reginfo->rootoid_len;
401 memcpy(o->name, reginfo->rootoid,
402 reginfo->rootoid_len * sizeof(oid));
403
404 rarg = newSViv((int) 0);
405 arg = newSVrv(rarg, "netsnmp_oidPtr");
406 sv_setiv(arg, (IV) o);
407
408 XPUSHs(sv_2mortal(rarg));
409
410 PUTBACK;
411 i = perl_call_pv("NetSNMP::OID::newwithptr", G_SCALAR);
412 SPAGAIN;
413 if (i != 1) {
414 snmp_log(LOG_ERR, "unhandled OID error.\n");
415 /* ack XXX */
416 }
417 ST(0) = POPs;
418 XSRETURN(1);
419 }
420
421 MODULE = NetSNMP::agent PACKAGE = NetSNMP::agent::netsnmp_request_infoPtr PREFIX = nari_
422
423 void
424 getOID(me)
425 SV *me;
426 PREINIT:
427 int i;
428 netsnmp_oid *o;
429 netsnmp_request_info *request;
430 SV *arg, *rarg;
431 PPCODE:
432 {
433 dSP;
434 PUSHMARK(SP);
435 request = (netsnmp_request_info *) SvIV(SvRV(me));
436
437 o = malloc(sizeof(netsnmp_oid));
438 o->name = o->namebuf;
439 o->len = request->requestvb->name_length;
440 memcpy(o->name, request->requestvb->name,
441 request->requestvb->name_length * sizeof(oid));
442
443 rarg = newSViv((int) 0);
444 arg = newSVrv(rarg, "netsnmp_oidPtr");
445 sv_setiv(arg, (IV) o);
446
447 XPUSHs(sv_2mortal(rarg));
448
449 PUTBACK;
450 i = perl_call_pv("NetSNMP::OID::newwithptr", G_SCALAR);
451 SPAGAIN;
452 if (i != 1) {
453 snmp_log(LOG_ERR, "unhandled OID error.\n");
454 /* ack XXX */
455 }
456 ST(0) = POPs;
457 XSRETURN(1);
458 }
459
460 netsnmp_oid *
461 nari_getOIDptr(me)
462 SV *me;
463 PREINIT:
464 netsnmp_request_info *request;
465 CODE:
466 request = (netsnmp_request_info *) SvIV(SvRV(me));
467 RETVAL = malloc(sizeof(netsnmp_oid));
468 RETVAL->name = RETVAL->namebuf;
469 RETVAL->len = request->requestvb->name_length;
470 memcpy(RETVAL->name, request->requestvb->name,
471 request->requestvb->name_length * sizeof(oid));
472 OUTPUT:
473 RETVAL
474
475 int
476 nari_getType(me)
477 SV *me;
478 PREINIT:
479 netsnmp_request_info *request;
480 CODE:
481 request = (netsnmp_request_info *) SvIV(SvRV(me));
482
483 RETVAL = request->requestvb->type ;
484 OUTPUT:
485 RETVAL
486
487 void
488 nari_setType(me, newvalue)
489 SV *me;
490 int newvalue;
491 PREINIT:
492 netsnmp_request_info *request;
493 CODE:
494 request = (netsnmp_request_info *) SvIV(SvRV(me));
495 request->requestvb->type=newvalue;
496
497 SV *
498 nari_getValue(me)
499 SV *me;
500 PREINIT:
501 char *outbuf = NULL;
502 size_t ob_len = 0, oo_len = 0;
503 netsnmp_request_info *request;
504 CODE:
505 request = (netsnmp_request_info *) SvIV(SvRV(me));
506 sprint_realloc_by_type((u_char **) &outbuf, &ob_len, &oo_len, 1,
507 request->requestvb, 0, 0, 0);
508 RETVAL = newSVpv(outbuf, 0);
509 netsnmp_free(outbuf);
510 OUTPUT:
511 RETVAL
512
513 int
514 nari_getDelegated(me)
515 SV *me;
516 PREINIT:
517 netsnmp_request_info *request;
518 CODE:
519 request = (netsnmp_request_info *) SvIV(SvRV(me));
520 RETVAL = request->delegated;
521 OUTPUT:
522 RETVAL
523
524 void
525 nari_setDelegated(me, newdelegated)
526 SV *me;
527 int newdelegated;
528 PREINIT:
529 netsnmp_request_info *request;
530 CODE:
531 request = (netsnmp_request_info *) SvIV(SvRV(me));
532 request->delegated = newdelegated;
533
534 int
535 nari_getProcessed(me)
536 SV *me;
537 PREINIT:
538 netsnmp_request_info *request;
539 CODE:
540 request = (netsnmp_request_info *) SvIV(SvRV(me));
541 RETVAL = request->processed;
542 OUTPUT:
543 RETVAL
544
545 void
546 nari_setProcessed(me, newprocessed)
547 SV *me;
548 int newprocessed;
549 PREINIT:
550 netsnmp_request_info *request;
551 CODE:
552 request = (netsnmp_request_info *) SvIV(SvRV(me));
553 request->processed = newprocessed;
554
555 int
556 nari_getStatus(me)
557 SV *me;
558 PREINIT:
559 netsnmp_request_info *request;
560 CODE:
561 request = (netsnmp_request_info *) SvIV(SvRV(me));
562 RETVAL = request->status;
563 OUTPUT:
564 RETVAL
565
566 void
567 nari_setStatus(me, newstatus)
568 SV *me;
569 int newstatus;
570 PREINIT:
571 netsnmp_request_info *request;
572 CODE:
573 request = (netsnmp_request_info *) SvIV(SvRV(me));
574 request->status = newstatus;
575
576 int
577 nari_getRepeat(me)
578 SV *me;
579 PREINIT:
580 netsnmp_request_info *request;
581 CODE:
582 request = (netsnmp_request_info *) SvIV(SvRV(me));
583 RETVAL = request->repeat;
584 OUTPUT:
585 RETVAL
586
587 void
588 nari_setRepeat(me, newrepeat)
589 SV *me;
590 int newrepeat;
591 PREINIT:
592 netsnmp_request_info *request;
593 CODE:
594 request = (netsnmp_request_info *) SvIV(SvRV(me));
595 request->repeat = newrepeat;
596
597 int
598 nari_setValue(me, type, value)
599 SV *me;
600 int type;
601 SV *value;
602 PREINIT:
603 netsnmp_request_info *request;
604 u_long utmp;
605 long ltmp;
606 double dtmp;
607 uint64_t ulltmp;
608 struct counter64 c64;
609 oid myoid[MAX_OID_LEN];
610 size_t myoid_len;
611 STRLEN stringlen;
612 char * stringptr;
613 CODE:
614 request = (netsnmp_request_info *) SvIV(SvRV(me));
615 switch(type) {
616 case SNMP_NOSUCHINSTANCE :
617 snmp_set_var_typed_value(request->requestvb,SNMP_NOSUCHINSTANCE,0,0) ;
618 RETVAL = 1;
619 break ;
620 case SNMP_NOSUCHOBJECT :
621 snmp_set_var_typed_value(request->requestvb,SNMP_NOSUCHOBJECT,0,0) ;
622 RETVAL = 1;
623 break ;
624 case SNMP_ENDOFMIBVIEW :
625 snmp_set_var_typed_value(request->requestvb,SNMP_ENDOFMIBVIEW,0,0) ;
626 RETVAL = 1;
627 break ;
628 case ASN_INTEGER:
629 /* We want an integer here */
630 if ((SvTYPE(value) == SVt_IV) || (SvTYPE(value) == SVt_PVMG) ||
631 SvIOK(value)) {
632 /* Good - got a real one (or a blessed object that we hope will turn out OK) */
633 ltmp = SvIV(value);
634 snmp_set_var_typed_value(request->requestvb, (u_char)type,
635 (u_char *) &ltmp, sizeof(ltmp));
636 RETVAL = 1;
637 break;
638 }
639 else if (SvTYPE(value) == SVt_NV || SvNOK(value)) {
640 /* Might be ok - got a double that might be an actual integer */
641 dtmp = SvNVX(value);
642 ltmp = SvIV(value);
643 if (dtmp != ltmp) {
644 snmp_log(LOG_ERR, "Could not convert double to integer in setValue: '%f'", dtmp);
645 RETVAL = 0;
646 break;
647 }
648 snmp_set_var_typed_value(request->requestvb, (u_char)type,
649 (u_char *) &ltmp, sizeof(ltmp));
650 RETVAL = 1;
651 break;
652 }
653 else if (SvPOK(value)) {
654 /* Might be OK - got a string, so try to convert it, allowing base 10, octal, and hex forms */
655 stringptr = SvPV(value, stringlen);
656 ltmp = strtol( stringptr, NULL, 0 );
657 if (errno == EINVAL) {
658 snmp_log(LOG_ERR, "Could not convert string to number in setValue: '%s'", stringptr);
659 RETVAL = 0;
660 break;
661 }
662
663 snmp_set_var_typed_value(request->requestvb, (u_char)type,
664 (u_char *) &ltmp, sizeof(ltmp));
665 RETVAL = 1;
666 break;
667 }
668 else {
669 snmp_log(LOG_ERR, "Non-integer value passed to setValue with ASN_INTEGER: type was %lu flags %#lx\n",
670 (unsigned long)SvTYPE(value), (unsigned long)SvFLAGS(value));
671 RETVAL = 0;
672 break;
673 }
674
675
676 case ASN_UNSIGNED:
677 case ASN_COUNTER:
678 case ASN_TIMETICKS:
679 /* We want an integer here */
680 if ((SvTYPE(value) == SVt_IV) || (SvTYPE(value) == SVt_PVMG) ||
681 SvIOK(value)) {
682 /* Good - got a real one (or a blessed scalar which we have to hope will turn out OK) */
683 utmp = SvIV(value);
684 snmp_set_var_typed_value(request->requestvb, (u_char)type,
685 (u_char *) &utmp, sizeof(utmp));
686 RETVAL = 1;
687 break;
688 }
689 else if (SvTYPE(value) == SVt_NV || SvNOK(value)) {
690 /* Might be ok - got a double that might be an actual unsigned */
691 dtmp = SvNVX(value);
692 utmp = SvIV(value);
693 if (dtmp != utmp) {
694 snmp_log(LOG_ERR, "Could not convert double to unsigned in setValue: '%f'", dtmp);
695 RETVAL = 0;
696 break;
697 }
698 snmp_set_var_typed_value(request->requestvb, (u_char)type,
699 (u_char *) &utmp, sizeof(utmp));
700 RETVAL = 1;
701 break;
702 }
703 else if (SvPOK(value)) {
704 /* Might be OK - got a string, so try to convert it, allowing base 10, octal, and hex forms */
705 stringptr = SvPV(value, stringlen);
706 utmp = strtoul( stringptr, NULL, 0 );
707 if (errno == EINVAL) {
708 snmp_log(LOG_ERR, "Could not convert string to number in setValue: '%s'", stringptr);
709 RETVAL = 0;
710 break;
711 }
712
713 snmp_set_var_typed_value(request->requestvb, (u_char)type,
714 (u_char *) &utmp, sizeof(utmp));
715 RETVAL = 1;
716 break;
717 }
718 else {
719 snmp_log(LOG_ERR, "Non-unsigned-integer value passed to setValue with ASN_UNSIGNED/ASN_COUNTER/ASN_TIMETICKS: type was %lu flags %#lx\n",
720 (unsigned long)SvTYPE(value), (unsigned long)SvFLAGS(value));
721 RETVAL = 0;
722 break;
723 }
724
725 case ASN_COUNTER64:
726 /* We want an integer here */
727 if ((SvTYPE(value) == SVt_IV) || (SvTYPE(value) == SVt_PVMG)) {
728 /* Good - got a real one (or a blessed scalar which we have to hope will turn out OK) */
729 ulltmp = SvIV(value);
730 RETVAL = 1;
731 }
732 else if (SvTYPE(value) == SVt_NV || SvNOK(value)) {
733 /* Might be ok - got a double that might be an actual unsigned */
734 dtmp = SvNVX(value);
735 ulltmp = SvIV(value);
736 if (dtmp != ulltmp) {
737 snmp_log(LOG_ERR, "Could not convert double to unsigned in setValue: '%f'", dtmp);
738 RETVAL = 0;
739 break;
740 }
741 snmp_set_var_typed_value(request->requestvb, (u_char)type,
742 (u_char *) &ulltmp, sizeof(ulltmp));
743 RETVAL = 1;
744 break;
745 }
746 else if (SvPOK(value)) {
747 /* Might be OK - got a string, so try to convert it, allowing base 10, octal, and hex forms */
748 stringptr = SvPV(value, stringlen);
749 errno = 0;
750 ulltmp = strtoull( stringptr, NULL, 0 );
751 if (errno != 0) {
752 snmp_log(LOG_ERR, "Could not convert string to number in setValue: '%s'", stringptr);
753 RETVAL = 0;
754 } else
755
756 RETVAL = 1;
757 }
758 else {
759 snmp_log(LOG_ERR, "Non-unsigned-integer value passed to setValue with ASN_COUNTER64: type was %lu flags %#lx\n",
760 (unsigned long)SvTYPE(value), (unsigned long)SvFLAGS(value));
761 RETVAL = 0;
762 }
763 if (RETVAL) {
764 c64.high = (uint32_t)(ulltmp >> 32);
765 c64.low = (uint32_t)(ulltmp >> 0);
766 snmp_set_var_typed_value(request->requestvb, (u_char)type,
767 (u_char *) &c64, sizeof(struct counter64));
768 }
769 break;
770
771 case ASN_OCTET_STR:
772 case ASN_BIT_STR:
773 case ASN_OPAQUE:
774 /* Check that we have been passed something with a string value (or a blessed scalar) */
775 if (!SvPOK(value) && (SvTYPE(value) != SVt_PVMG)) {
776 snmp_log(LOG_ERR, "Non-string value passed to setValue with ASN_OCTET_STR/ASN_BIT_STR: type was %lu flags %#lx\n",
777 (unsigned long)SvTYPE(value), (unsigned long)SvFLAGS(value));
778 RETVAL = 0;
779 break;
780 }
781
782 /* Find length of string (strlen will *not* do, as these are binary strings) */
783 stringptr = SvPV(value, stringlen);
784
785 snmp_set_var_typed_value(request->requestvb, (u_char)type,
786 (u_char *) stringptr,
787 stringlen);
788 RETVAL = 1;
789 break;
790
791 case ASN_IPADDRESS:
792 /* IP addresses are passed as *binary* strings.
793 * In the case of IPv4 addresses, these are 4 bytes long.
794 * NOTE: the use of binary strings rather than dotted-quad or FQDNs was
795 * introduced here by Andrew Findlay's patch of March 17th 2003,
796 * and is effectively a change to the previous implied API which assumed
797 * the value was a (valid) hostname.
798 * Responsibility for decoding and resolving has been passed up to the Perl script.
799 */
800
801 /* Check that we have been passed something with a string value (or a blessed scalar) */
802 if (!SvPOK(value) && (SvTYPE(value) != SVt_PVMG)) {
803 snmp_log(LOG_ERR, "Non-string value passed to setValue with ASN_IPADDRESS: type was %lu flags %#lx\n",
804 (unsigned long)SvTYPE(value), (unsigned long)SvFLAGS(value));
805 RETVAL = 0;
806 break;
807 }
808
809 /* Find length of string (strlen will *not* do, as these are binary strings) */
810 stringptr = SvPV(value, stringlen);
811
812 # snmp_log(LOG_ERR, "IP address returned with length %d: %u.%u.%u.%u\n", stringlen, stringptr[0],
813 # stringptr[1], stringptr[2], stringptr[3] );
814
815 # Sanity check on address length
816 if ((stringlen != 4) && (stringlen != 16)) {
817 snmp_log(LOG_ERR, "IP address of %" NETSNMP_PRIz "d bytes passed to setValue with ASN_IPADDRESS\n", stringlen);
818 RETVAL = 0;
819 break;
820 }
821
822 snmp_set_var_typed_value(request->requestvb, (u_char)type,
823 stringptr, stringlen);
824 RETVAL = 1;
825 break;
826
827 case ASN_OBJECT_ID:
828 /* Check that we have been passed something with a string value (or a blessed scalar) */
829 if (!SvPOK(value) && (SvTYPE(value) != SVt_PVMG)) {
830 snmp_log(LOG_ERR, "Non-string value passed to setValue with ASN_OBJECT_ID: type was %lu flags %#lx\n",
831 (unsigned long)SvTYPE(value), (unsigned long)SvFLAGS(value));
832 RETVAL = 0;
833 break;
834 }
835
836 /* Extract the string */
837 stringptr = SvPV(value, stringlen);
838
839 /* snmp_log(LOG_ERR, "setValue returning OID '%s'\n", stringptr); */
840
841 myoid_len = MAX_OID_LEN;
842 if (!snmp_parse_oid(stringptr, myoid, &myoid_len)) {
843 snmp_log(LOG_ERR, "couldn't parse %s in setValue\n", stringptr);
844 RETVAL = 0;
845 break;
846 } else {
847 /* snmp_log(LOG_ERR, "setValue returning OID length %d\n", myoid_len); */
848
849 request = (netsnmp_request_info *) SvIV(SvRV(me));
850 snmp_set_var_typed_value(request->requestvb, (u_char)type,
851 (u_char *) myoid, (myoid_len * sizeof(myoid[0])) );
852 }
853
854 RETVAL = 1;
855 break;
856
857 default:
858 snmp_log(LOG_ERR, "unknown var value type: %d\n",
859 type);
860 RETVAL = 0;
861 break;
862 }
863
864 OUTPUT:
865 RETVAL
866
867 void
868 nari_setOID(me, value)
869 SV *me;
870 char *value;
871 PREINIT:
872 oid myoid[MAX_OID_LEN];
873 size_t myoid_len = MAX_OID_LEN;
874 netsnmp_request_info *request;
875 CODE:
876 myoid_len = MAX_OID_LEN;
877 if (!snmp_parse_oid(value, myoid, &myoid_len)) {
878 snmp_log(LOG_ERR, "couldn't parse %s in setOID\n", value);
879 } else {
880 request = (netsnmp_request_info *) SvIV(SvRV(me));
881 snmp_set_var_objid(request->requestvb, myoid, myoid_len);
882 }
883
884 void
885 nari_setError(me, rinfo, ecode)
886 SV *me;
887 SV *rinfo;
888 int ecode;
889 PREINIT:
890 netsnmp_request_info *request;
891 netsnmp_agent_request_info *reqinfo;
892 CODE:
893 request = (netsnmp_request_info *) SvIV(SvRV(me));
894 reqinfo = (netsnmp_agent_request_info *) SvIV(SvRV(rinfo));
895 netsnmp_set_request_error(reqinfo, request, ecode);
896
897 SV *
898 nari_next(me)
899 SV *me;
900 PREINIT:
901 netsnmp_request_info *request;
902 SV *arg, *rarg;
903 CODE:
904 {
905 request = (netsnmp_request_info *) SvIV(SvRV(me));
906 if (request && request->next) {
907 request = request->next;
908 rarg = newSViv(0);
909 arg = newSVrv(rarg, "NetSNMP::agent::netsnmp_request_infoPtr");
910 sv_setiv(arg, (IV) request);
911 RETVAL = rarg;
912 } else {
913 RETVAL = &sv_undef;
914 }
915 }
916 OUTPUT:
917 RETVAL
918
919 MODULE = NetSNMP::agent PACKAGE = NetSNMP::agent::netsnmp_agent_request_info PREFIX = narqi_
920
921
922 SV *
923 narqi_getSourceIp(me)
924 SV *me;
925 PREINIT:
926 netsnmp_agent_request_info *reqinfo;
927 netsnmp_udp_addr_pair *addr_pair;
928 struct sockaddr_in *from;
929 SV *rarg;
930
931 CODE:
932 reqinfo = (netsnmp_agent_request_info *) SvIV(SvRV(me));
933
934 /* XXX: transport-specific: UDP/IPv4 only! */
935 addr_pair = (netsnmp_udp_addr_pair *) (reqinfo->asp->pdu->transport_data);
936 from = (struct sockaddr_in *) &(addr_pair->remote_addr);
937 rarg = newSVpv((const char *)(&from->sin_addr.s_addr), sizeof(from->sin_addr.s_addr));
938 RETVAL = rarg;
939 OUTPUT:
940 RETVAL
941
942
943 SV *
944 narqi_getDestIp(me)
945 SV *me;
946 PREINIT:
947 netsnmp_agent_request_info *reqinfo;
948 netsnmp_udp_addr_pair *addr_pair;
949 struct in_addr *to;
950 SV *rarg;
951
952 CODE:
953 reqinfo = (netsnmp_agent_request_info *) SvIV(SvRV(me));
954
955 /* XXX: transport-specific: UDP/IPv4 only! */
956 addr_pair = (netsnmp_udp_addr_pair *) (reqinfo->asp->pdu->transport_data);
957 to = (struct in_addr *) &(addr_pair->local_addr);
958 rarg = newSVpv((const char *)(&to->s_addr), sizeof(to->s_addr));
959 RETVAL = rarg;
960 OUTPUT:
961 RETVAL
962
963 int
964 narqi_getMode(me)
965 SV *me;
966 PREINIT:
967 netsnmp_agent_request_info *reqinfo;
968 CODE:
969 reqinfo = (netsnmp_agent_request_info *) SvIV(SvRV(me));
970 RETVAL = reqinfo->mode;
971 OUTPUT:
972 RETVAL
973
974 void
975 narqi_setMode(me, newvalue)
976 SV *me;
977 int newvalue;
978 PREINIT:
979 netsnmp_agent_request_info *reqinfo;
980 CODE:
981 reqinfo = (netsnmp_agent_request_info *) SvIV(SvRV(me));
982 reqinfo->mode = newvalue;
983
984
985 MODULE = NetSNMP::agent PACKAGE = NetSNMP::agent
986