ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-SNMP-XS/XS.xs
(Generate patch)

Comparing Net-SNMP-XS/XS.xs (file contents):
Revision 1.2 by root, Wed Apr 8 10:39:32 2009 UTC vs.
Revision 1.5 by root, Thu Apr 9 04:49:16 2009 UTC

2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5// C99 required 5// C99 required
6 6
7#define ASN_BOOLEAN 0x01 7#define ASN_BOOLEAN 0x01
8#define ASN_INTEGER 0x02 8#define ASN_INTEGER32 0x02
9#define ASN_BIT_STR 0x03
10#define ASN_OCTET_STR 0x04 9#define ASN_OCTET_STRING 0x04
11#define ASN_NULL 0x05 10#define ASN_NULL 0x05
12#define ASN_OBJECT_ID 0x06 11#define ASN_OBJECT_IDENTIFIER 0x06
13#define ASN_SEQUENCE 0x10 12#define ASN_SEQUENCE 0x30
14#define ASN_SET 0x11
15
16#define ASN_UNIVERSAL 0x00 13#define ASN_IPADDRESS 0x40
17#define ASN_APPLICATION 0x40
18#define ASN_CONTEXT 0x80 14#define ASN_COUNTER32 0x41
15#define ASN_UNSIGNED32 0x42
16#define ASN_TIMETICKS 0x43
19#define ASN_PRIVATE 0xc0 17#define ASN_OPAQUE 0x44
20
21#define ASN_PRIMITIVE 0x00
22#define ASN_CONSTRUCTOR 0x20
23
24#define ASN_LONG_LEN 0x80 18#define ASN_COUNTER64 0x46
25#define ASN_EXTENSION_ID 0x1f
26#define ASN_BIT8 0x80
27 19
28//#define BENCHMARK 20#define BENCHMARK
21
22#define MAX_OID_STRLEN 4096
29 23
30static SV *msg; 24static SV *msg;
31static int errflag; 25static int errflag, leading_dot;
32static U8 *buf, *cur; 26static U8 *buf, *cur;
33static STRLEN len, rem; 27static STRLEN len, rem;
28
29static SV *
30x_get_cv (SV *cb_sv)
31{
32 HV *st;
33 GV *gvp;
34 CV *cv = sv_2cv (cb_sv, &st, &gvp, 0);
35
36 if (!cv)
37 croak ("CODE reference expected");
38
39 return (SV *)cv;
40}
34 41
35static void 42static void
36error (const char *msg) 43error (const char *msg)
37{ 44{
38 errflag = 1; 45 errflag = 1;
134 } 141 }
135 142
136 return res; 143 return res;
137} 144}
138 145
146static U32
147process_integer32 (void)
148{
149 U32 length = process_length ();
150
151 if (length <= 0)
152 {
153 error ("INTEGER32 length equal to zero");
154 return 0;
155 }
156
157 U8 *data = getn (length, 0);
158
159 if (!data)
160 return 0;
161
162 if (length > 5 || (length > 4 && data [0]))
163 {
164 error ("INTEGER32 length too long");
165 return 0;
166 }
167
168 U32 res = data [0] & 0x80 ? 0xffffffff : 0;
169
170 while (length--)
171 res = (res << 8) | *data++;
172
173 return res;
174}
175
176static SV *
177process_integer32_sv (void)
178{
179 return newSViv ((I32)process_integer32 ());
180}
181
182static SV *
183process_unsigned32_sv (void)
184{
185 return newSVuv ((U32)process_integer32 ());
186}
187
188static SV *
189process_octet_string_sv (void)
190{
191 U32 length = process_length ();
192
193 U8 *data = getn (length, 0);
194 if (!data)
195 {
196 error ("OCTET STRING too long");
197 return &PL_sv_undef;
198 }
199
200 return newSVpvn (data, length);
201}
202
203static char *
204write_uv (char *buf, U32 u)
205{
206 // the one-digit case is absolutely predominant
207 if (u < 10)
208 *buf++ = u + '0';
209 else
210 buf += sprintf (buf, "%u", (unsigned int)u);
211
212 return buf;
213}
214
215static SV *
216process_object_identifier_sv (void)
217{
218 U32 length = process_length ();
219
220 if (length <= 0)
221 {
222 error ("OBJECT IDENTIFIER length equal to zero");
223 return &PL_sv_undef;
224 }
225
226 U8 *end = cur + length;
227 U32 w = getb ();
228
229 static char oid[MAX_OID_STRLEN];
230 char *app = oid;
231
232 *app = '.'; app += ! ! leading_dot;
233 app = write_uv (app, w / 40);
234 *app++ = '.';
235 app = write_uv (app, w % 40);
236
237 // we assume an oid component is never > 64 bytes
238 while (cur < end && oid + sizeof (oid) - app > 64)
239 {
240 w = getb ();
241 *app++ = '.';
242 app = write_uv (app, w);
243 }
244#if 0
245 buf += snprintf (buf, oid + sizeof (oid) - buf, "%d.%d", (int)w / 40, (int)w % 40);
246
247 while (cur < end)
248 {
249 w = getb ();
250 buf += snprintf (buf, oid + sizeof (oid) - buf, ".%u", (unsigned int)w);
251 }
252#endif
253
254 return newSVpvn (oid, app - oid);
255}
256
257static AV *av_type;
258
139MODULE = Net::SNMP::XS PACKAGE = Net::SNMP::XS 259MODULE = Net::SNMP::XS PACKAGE = Net::SNMP::XS
260
261PROTOTYPES: ENABLE
262
263BOOT:
264 av_type = newAV ();
265
266void
267set_type (int type, SV *cv)
268 CODE:
269 av_store (av_type, type, SvREFCNT_inc (x_get_cv (cv)));
140 270
141void 271void
142set_msg (SV *msg_, SV *buf_) 272set_msg (SV *msg_, SV *buf_)
143 CODE: 273 CODE:
144 errflag = 0; 274 errflag = 0;
275 leading_dot = -1;
145 msg = SvREFCNT_inc (msg_); 276 msg = SvREFCNT_inc (msg_);
146 buf = SvPVbyte (buf_, len); 277 buf = SvPVbyte (buf_, len);
147 cur = buf; 278 cur = buf;
148 rem = len; 279 rem = len;
149#ifdef BENCHMARK 280#ifdef BENCHMARK
150 t1 = tstamp (); 281 t1 = tstamp ();
151#endif 282#endif
152 283
153void 284void
154clr_msg () 285clr_msg ()
155 CODE: 286 CODE:
199 RETVAL 330 RETVAL
200 331
201U32 332U32
202_process_length (SV *self, ...) 333_process_length (SV *self, ...)
203 ALIAS: 334 ALIAS:
204 _process_sequence = 1 335 _process_sequence = 0
205 CODE: 336 CODE:
206 RETVAL = process_length (); 337 RETVAL = process_length ();
207 OUTPUT: 338 OUTPUT:
208 RETVAL 339 RETVAL
209 340
210I32 341SV *
211_process_integer32 (SV *self, ...) 342_process_integer32 (SV *self, ...)
343 CODE:
344 RETVAL = process_integer32_sv ();
345 OUTPUT:
346 RETVAL
347
348SV *
349_process_counter (SV *self, ...)
212 ALIAS: 350 ALIAS:
213 _process_counter = 0
214 _process_gauge = 0 351 _process_gauge = 0
352 _process_timeticks = 0
215 CODE: 353 CODE:
216{ 354 RETVAL = process_unsigned32_sv ();
217 U32 length = process_length ();
218
219 if (length <= 0)
220 {
221 error ("INTEGER32 length equal to zero");
222 XSRETURN_UNDEF;
223 }
224
225 U8 *data = getn (length, 0);
226
227 if (!data)
228 XSRETURN_UNDEF;
229
230 if (length > 5 || (length > 4 && data [0]))
231 {
232 error ("INTEGER32 length too long");
233 XSRETURN_UNDEF;
234 }
235
236 U32 res = data [0] & 0x80 ? 0xffffffff : 0;
237
238 while (length--)
239 res = (res << 8) | *data++;
240
241 RETVAL = res;
242}
243 OUTPUT: 355 OUTPUT:
244 RETVAL 356 RETVAL
245 357
246SV * 358SV *
247_process_object_identifier (SV *self, ...) 359_process_object_identifier (SV *self, ...)
248 CODE: 360 CODE:
249{ 361 RETVAL = process_object_identifier_sv ();
250 U32 length = process_length ();
251
252 if (length <= 0)
253 {
254 error ("OBJECT IDENTIFIER length equal to zero");
255 XSRETURN_UNDEF;
256 }
257
258 U8 *end = cur + length;
259 U32 w = getb ();
260
261 //TODO: leading_dots
262
263 RETVAL = newSVpvf (".%d.%d", (int)w / 40, (int)w % 40);
264
265 while (cur < end)
266 {
267 w = getb ();
268 sv_catpvf (RETVAL, ".%u", (unsigned int)w);
269 }
270}
271 OUTPUT: 362 OUTPUT:
272 RETVAL 363 RETVAL
273 364
274SV * 365SV *
275_process_octet_string (SV *self, ...) 366_process_octet_string (SV *self, ...)
276 ALIAS: 367 ALIAS:
277 _process_opaque = 0 368 _process_opaque = 0
278 CODE: 369 CODE:
279{ 370 RETVAL = process_octet_string_sv ();
280 U32 length = process_length ();
281
282 U8 *data = getn (length, 0);
283 if (!data)
284 {
285 error ("OCTET STRING too long");
286 XSRETURN_UNDEF;
287 }
288
289 RETVAL = newSVpvn (data, length);
290}
291 OUTPUT: 371 OUTPUT:
292 RETVAL 372 RETVAL
293 373
294SV * 374SV *
295_process_ipaddress (SV *self, ...) 375_process_ipaddress (SV *self, ...)
306 RETVAL = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]); 386 RETVAL = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
307} 387}
308 OUTPUT: 388 OUTPUT:
309 RETVAL 389 RETVAL
310 390
391SV *
392process (SV *self, SV *expected = 0, SV *found = 0)
393 PPCODE:
394{
395 U8 type = get8 ();
396
397 if (expected && SvOK (expected) && type != SvIV (expected))
398 {
399 error ("Expected a different type than found");
400 XSRETURN_UNDEF;
401 }
402
403 if (type > AvFILLp (av_type) || !SvTYPE (AvARRAY (av_type)[type]) == SVt_PVCV)
404 {
405 sv_dump (AvARRAY (av_type)[type]);//D
406 error ("Unknown ASN.1 type");
407 XSRETURN_UNDEF;
408 }
409
410 if (found)
411 sv_setiv (found, type);
412
413 //TODO: switch based on type, to avoid calling overhead
414 SV *res;
415
416 switch (type)
417 {
418 case ASN_OBJECT_IDENTIFIER:
419 res = sv_2mortal (process_object_identifier_sv ());
420 break;
421
422 case ASN_INTEGER32:
423 res = sv_2mortal (process_integer32_sv ());
424 break;
425
426 case ASN_UNSIGNED32:
427 case ASN_COUNTER32:
428 case ASN_TIMETICKS:
429 res = sv_2mortal (process_unsigned32_sv ());
430 break;
431
432 case ASN_SEQUENCE:
433 res = sv_2mortal (newSVuv (process_length ()));
434 break;
435
436 case ASN_OCTET_STRING:
437 case ASN_OPAQUE:
438 res = sv_2mortal (process_octet_string_sv ());
439 break;
440
441 default:
442 {
443 dSP;
444 PUSHMARK (SP);
445 EXTEND (SP, 2);
446 PUSHs (self);
447 PUSHs (expected);
448 PUTBACK;
449 int count = call_sv (AvARRAY (av_type)[type], G_SCALAR);
450 SPAGAIN;
451 res = count ? TOPs : &PL_sv_undef;
452 }
453 }
454
455 XPUSHs (errflag ? &PL_sv_undef : res);
456}
457
458#if 0
459
460MODULE = Net::SNMP::XS PACKAGE = Net::SNMP::PDU
461
462SV *
463_process_var_bind_list (SV *self)
464 CODE:
465{
466 # VarBindList::=SEQUENCE
467 if (!defined($value = $this->process(SEQUENCE))) {
468 return $this->_error;
469 }
470
471 # Using the length of the VarBindList SEQUENCE,
472 # calculate the end index.
473
474 my $end = $this->index + $value;
475
476 $this->{_var_bind_list} = {};
477 $this->{_var_bind_names} = [];
478 $this->{_var_bind_types} = {};
479
480 my ($oid, $type);
481
482 while ($this->index < $end) {
483
484 # VarBind::=SEQUENCE
485 if (!defined($this->process(SEQUENCE))) {
486 return $this->_error;
487 }
488 # name::=ObjectName
489 if (!defined($oid = $this->process(OBJECT_IDENTIFIER))) {
490 return $this->_error;
491 }
492 # value::=ObjectSyntax
493 if (!defined($value = $this->process(undef, $type))) {
494 return $this->_error;
495 }
496
497 # Create a hash consisting of the OBJECT IDENTIFIER as a
498 # key and the ObjectSyntax as the value. If there is a
499 # duplicate OBJECT IDENTIFIER in the VarBindList, we pad
500 # that OBJECT IDENTIFIER with spaces to make a unique
501 # key in the hash.
502
503 while (exists($this->{_var_bind_list}->{$oid})) {
504 $oid .= ' '; # Pad with spaces
505 }
506
507 DEBUG_INFO('{ %s => %s: %s }', $oid, asn1_itoa($type), $value);
508 $this->{_var_bind_list}->{$oid} = $value;
509 $this->{_var_bind_types}->{$oid} = $type;
510
511 # Create an array with the ObjectName OBJECT IDENTIFIERs
512 # so that the order in which the VarBinds where encoded
513 # in the PDU can be retrieved later.
514
515 push(@{$this->{_var_bind_names}}, $oid);
516
517 }
518
519 # Return an error based on the contents of the VarBindList
520 # if we received a Report-PDU.
521
522 return $this->_report_pdu_error if ($this->{_pdu_type} == REPORT);
523
524 # Return the var_bind_list hash
525 $this->{_var_bind_list};
526}
527
528#endif
529

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines