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.7 by root, Thu Apr 9 07:10:23 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 BENCHMARK
8
7#define ASN_BOOLEAN 0x01 9#define ASN_BOOLEAN 0x01
8#define ASN_INTEGER 0x02 10#define ASN_INTEGER32 0x02
9#define ASN_BIT_STR 0x03
10#define ASN_OCTET_STR 0x04 11#define ASN_OCTET_STRING 0x04
11#define ASN_NULL 0x05 12#define ASN_NULL 0x05
12#define ASN_OBJECT_ID 0x06 13#define ASN_OBJECT_IDENTIFIER 0x06
13#define ASN_SEQUENCE 0x10 14#define ASN_SEQUENCE 0x30
14#define ASN_SET 0x11
15
16#define ASN_UNIVERSAL 0x00 15#define ASN_IPADDRESS 0x40
17#define ASN_APPLICATION 0x40
18#define ASN_CONTEXT 0x80 16#define ASN_COUNTER32 0x41
17#define ASN_UNSIGNED32 0x42
18#define ASN_TIMETICKS 0x43
19#define ASN_PRIVATE 0xc0 19#define ASN_OPAQUE 0x44
20
21#define ASN_PRIMITIVE 0x00
22#define ASN_CONSTRUCTOR 0x20
23
24#define ASN_LONG_LEN 0x80 20#define ASN_COUNTER64 0x46
25#define ASN_EXTENSION_ID 0x1f
26#define ASN_BIT8 0x80
27 21
28//#define BENCHMARK 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 "";
224 }
225
226 U8 *end = cur + length;
227 U32 w = getb ();
228
229 static char oid[MAX_OID_STRLEN]; // must be static
230 char *app = oid;
231
232 *app = '.'; app += ! ! leading_dot;
233 app = write_uv (app, (U8)w / 40);
234 *app++ = '.';
235 app = write_uv (app, (U8)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
245 return newSVpvn (oid, app - oid);
246}
247
248static AV *av_type;
249
250static SV *
251process_sv (int *found)
252{
253 int type = get8 ();
254
255 *found = type;
256
257 SV *res;
258
259 switch (type)
260 {
261 case ASN_OBJECT_IDENTIFIER:
262 res = process_object_identifier_sv ();
263 break;
264
265 case ASN_INTEGER32:
266 res = process_integer32_sv ();
267 break;
268
269 case ASN_UNSIGNED32:
270 case ASN_COUNTER32:
271 case ASN_TIMETICKS:
272 res = process_unsigned32_sv ();
273 break;
274
275 case ASN_SEQUENCE:
276 res = newSVuv (process_length ());
277 break;
278
279 case ASN_OCTET_STRING:
280 case ASN_OPAQUE:
281 res = process_octet_string_sv ();
282 break;
283
284 default:
285 {
286 if (type > AvFILLp (av_type) || !SvTYPE (AvARRAY (av_type)[type]) == SVt_PVCV)
287 {
288 error ("Unknown ASN.1 type");
289 return &PL_sv_undef;
290 }
291
292 dSP;
293 PUSHMARK (SP);
294 EXTEND (SP, 2);
295 PUSHs (msg);
296 PUSHs (sv_2mortal (newSViv (type)));
297 PUTBACK;
298 int count = call_sv (AvARRAY (av_type)[type], G_SCALAR);
299 SPAGAIN;
300 res = count ? SvREFCNT_inc (TOPs) : &PL_sv_undef;
301 }
302 }
303
304 return errflag ? &PL_sv_undef : res;
305}
306
139MODULE = Net::SNMP::XS PACKAGE = Net::SNMP::XS 307MODULE = Net::SNMP::XS PACKAGE = Net::SNMP::XS
308
309PROTOTYPES: ENABLE
310
311BOOT:
312 av_type = newAV ();
313
314void
315set_type (int type, SV *cv)
316 CODE:
317 av_store (av_type, type, SvREFCNT_inc (x_get_cv (cv)));
140 318
141void 319void
142set_msg (SV *msg_, SV *buf_) 320set_msg (SV *msg_, SV *buf_)
143 CODE: 321 CODE:
144 errflag = 0; 322 errflag = 0;
323 leading_dot = -1;
145 msg = SvREFCNT_inc (msg_); 324 msg = SvREFCNT_inc (msg_);
146 buf = SvPVbyte (buf_, len); 325 buf = SvPVbyte (buf_, len);
147 cur = buf; 326 cur = buf;
148 rem = len; 327 rem = len;
149#ifdef BENCHMARK 328#ifdef BENCHMARK
150 t1 = tstamp (); 329 t1 = tstamp ();
151#endif 330#endif
152 331
153void 332void
154clr_msg () 333clr_msg ()
155 CODE: 334 CODE:
199 RETVAL 378 RETVAL
200 379
201U32 380U32
202_process_length (SV *self, ...) 381_process_length (SV *self, ...)
203 ALIAS: 382 ALIAS:
204 _process_sequence = 1 383 _process_sequence = 0
205 CODE: 384 CODE:
206 RETVAL = process_length (); 385 RETVAL = process_length ();
207 OUTPUT: 386 OUTPUT:
208 RETVAL 387 RETVAL
209 388
210I32 389SV *
211_process_integer32 (SV *self, ...) 390_process_integer32 (SV *self, ...)
391 CODE:
392 RETVAL = process_integer32_sv ();
393 OUTPUT:
394 RETVAL
395
396SV *
397_process_counter (SV *self, ...)
212 ALIAS: 398 ALIAS:
213 _process_counter = 0
214 _process_gauge = 0 399 _process_gauge = 0
400 _process_timeticks = 0
215 CODE: 401 CODE:
216{ 402 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: 403 OUTPUT:
244 RETVAL 404 RETVAL
245 405
246SV * 406SV *
247_process_object_identifier (SV *self, ...) 407_process_object_identifier (SV *self, ...)
248 CODE: 408 CODE:
249{ 409 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: 410 OUTPUT:
272 RETVAL 411 RETVAL
273 412
274SV * 413SV *
275_process_octet_string (SV *self, ...) 414_process_octet_string (SV *self, ...)
276 ALIAS: 415 ALIAS:
277 _process_opaque = 0 416 _process_opaque = 0
278 CODE: 417 CODE:
279{ 418 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: 419 OUTPUT:
292 RETVAL 420 RETVAL
293 421
294SV * 422SV *
295_process_ipaddress (SV *self, ...) 423_process_ipaddress (SV *self, ...)
306 RETVAL = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]); 434 RETVAL = newSVpvf ("%d.%d.%d.%d", data [0], data [1], data [2], data [3]);
307} 435}
308 OUTPUT: 436 OUTPUT:
309 RETVAL 437 RETVAL
310 438
439SV *
440process (SV *self, SV *expected = &PL_sv_undef, SV *found = 0)
441 CODE:
442{
443 int type;
444
445 RETVAL = process_sv (&type);
446
447 if (found)
448 sv_setiv (found, type);
449
450 if (SvOK (expected) && type != SvIV (expected))
451 error ("Expected a different type than found");
452}
453 OUTPUT:
454 RETVAL
455
456MODULE = Net::SNMP::XS PACKAGE = Net::SNMP::PDU
457
458SV *
459_process_var_bind_list (SV *self)
460 CODE:
461{
462 if (get8 () != ASN_SEQUENCE)
463 error ("SEQUENCE expected at beginning of VarBindList");
464 int seqlen = process_length ();
465 U8 *end = cur + seqlen;
466
467 HV *list = newHV ();
468 AV *names = newAV ();
469 HV *types = newHV ();
470
471 hv_store ((HV *)SvRV (self), "_var_bind_list" , sizeof ("_var_bind_list" ) - 1, newRV_noinc ((SV *)list ), 0);
472 hv_store ((HV *)SvRV (self), "_var_bind_names", sizeof ("_var_bind_names") - 1, newRV_noinc ((SV *)names), 0);
473 hv_store ((HV *)SvRV (self), "_var_bind_types", sizeof ("_var_bind_types") - 1, newRV_noinc ((SV *)types), 0);
474
475 while (cur < end && !errflag)
476 {
477 // SEQUENCE ObjectName ObjectSyntax
478 if (get8 () != ASN_SEQUENCE)
479 error ("SEQUENCE expected at beginning of VarBind");
480 process_length ();
481
482 if (get8 () != ASN_OBJECT_IDENTIFIER)
483 error ("OBJECT IDENTIFIER expected at beginning of VarBind");
484 int type, oidlen;
485 SV *oid = process_object_identifier_sv ();
486 SV *val = process_sv (&type);
487
488 hv_store_ent (types, oid, newSViv (type), 0);
489 hv_store_ent (list , oid, val, 0);
490 av_push (names, oid);
491 }
492
493 //return $this->_report_pdu_error if ($this->{_pdu_type} == REPORT);
494
495 RETVAL = newRV_inc ((SV *)list);
496}
497 OUTPUT:
498 RETVAL
499
500

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines