… | |
… | |
8 | |
8 | |
9 | my $ber = ber_decode $buf, $Convert::BER::XS::SNMP_PROFILE |
9 | my $ber = ber_decode $buf, $Convert::BER::XS::SNMP_PROFILE |
10 | or die "unable to decode SNMP message"; |
10 | or die "unable to decode SNMP message"; |
11 | |
11 | |
12 | # The above results in a data structure consisting of |
12 | # The above results in a data structure consisting of |
13 | # (class, tag, # constructed, data) |
13 | # (class, tag, flags, data) |
14 | # tuples. Below is such a message, SNMPv1 trap |
14 | # tuples. Below is such a message, SNMPv1 trap |
15 | # with a Cisco mac change notification. |
15 | # with a Cisco mac change notification. |
16 | # Did you know that Cisco is in the news almost |
16 | # Did you know that Cisco is in the news almost |
17 | # every week because of some backdoor password |
17 | # every week because of some backdoor password |
18 | # or other extremely stupid security bug? |
18 | # or other extremely stupid security bug? |
… | |
… | |
134 | |
134 | |
135 | Constants only relevant to SNMP. These are the tag values used by SNMP in |
135 | Constants only relevant to SNMP. These are the tag values used by SNMP in |
136 | the C<ASN_APPLICATION> namespace and have the exact numerical value as in |
136 | the C<ASN_APPLICATION> namespace and have the exact numerical value as in |
137 | BER/RFC 2578. |
137 | BER/RFC 2578. |
138 | |
138 | |
139 | SNMP_IPADDRESS SNMP_COUNTER32 SNMP_UNSIGNED32 SNMP_TIMETICKS SNMP_OPAQUE SNMP_COUNTER64 |
139 | SNMP_IPADDRESS SNMP_COUNTER32 SNMP_UNSIGNED32 SNMP_GAUGE32 |
|
|
140 | SNMP_TIMETICKS SNMP_OPAQUE SNMP_COUNTER64 |
140 | |
141 | |
141 | =item C<:decode> |
142 | =item C<:decode> |
142 | |
143 | |
143 | C<ber_decode> and the match helper functions: |
144 | C<ber_decode> and the match helper functions: |
144 | |
145 | |
|
|
146 | ber_decode ber-decode_prefix |
145 | ber_decode ber_is ber_is_seq ber_is_int ber_is_oid |
147 | ber_is ber_is_seq ber_is_int ber_is_oid |
146 | |
148 | |
147 | =item C<:encode> |
149 | =item C<:encode> |
148 | |
150 | |
149 | C<ber_encode> and the construction helper functions: |
151 | C<ber_encode> and the construction helper functions: |
150 | |
152 | |
151 | ber_encode ber_int |
153 | ber_encode |
|
|
154 | ber_int |
152 | |
155 | |
153 | =back |
156 | =back |
154 | |
157 | |
155 | =head2 ASN.1/BER/DER/... BASICS |
158 | =head2 ASN.1/BER/DER/... BASICS |
156 | |
159 | |
… | |
… | |
252 | |
255 | |
253 | =head2 DECODING AND ENCODING |
256 | =head2 DECODING AND ENCODING |
254 | |
257 | |
255 | =over |
258 | =over |
256 | |
259 | |
257 | =item $tuple = ber_decoded $bindata[, $profile] |
260 | =item $tuple = ber_decode $bindata[, $profile] |
258 | |
261 | |
259 | Decodes binary BER data in C<$bindata> and returns the resulting BER |
262 | Decodes binary BER data in C<$bindata> and returns the resulting BER |
260 | tuple. Croaks on any decoding error, so the returned C<$tuple> is always |
263 | tuple. Croaks on any decoding error, so the returned C<$tuple> is always |
261 | valid. |
264 | valid. |
262 | |
265 | |
… | |
… | |
275 | |
278 | |
276 | Example: as above, but use the provided SNMP profile. |
279 | Example: as above, but use the provided SNMP profile. |
277 | |
280 | |
278 | $tuple = ber_encode $data, $Convert::BER::XS::SNMP_PROFILE; |
281 | $tuple = ber_encode $data, $Convert::BER::XS::SNMP_PROFILE; |
279 | |
282 | |
|
|
283 | =item ($tuple, $bytes) = ber_decode_prefix $bindata[, $profile] |
|
|
284 | |
|
|
285 | Works like C<ber_decode>, except it doesn't croak when there is data after |
|
|
286 | the BER data, but instead returns the decoded value and the number of |
|
|
287 | bytes it decoded. |
|
|
288 | |
|
|
289 | This is useful when you have BER data at the start of a buffer and other |
|
|
290 | data after, and you need to find the length. |
|
|
291 | |
|
|
292 | Also, since BER is self-delimited, this can be used to decode multiple BER |
|
|
293 | values joined together. |
|
|
294 | |
280 | =item $bindata = ber_encode $tuple[, $profile] |
295 | =item $bindata = ber_encode $tuple[, $profile] |
281 | |
296 | |
282 | Encodes the BER tuple into a BER/DER data structure. AS with |
297 | Encodes the BER tuple into a BER/DER data structure. AS with |
283 | Cyber_decode>, an optional profile can be given. |
298 | Cyber_decode>, an optional profile can be given. |
284 | |
299 | |
… | |
… | |
303 | a ease-of-use exception, they usually also accept C<undef> instead of a |
318 | a ease-of-use exception, they usually also accept C<undef> instead of a |
304 | tuple reference, in which case they silently fail to match. |
319 | tuple reference, in which case they silently fail to match. |
305 | |
320 | |
306 | =over |
321 | =over |
307 | |
322 | |
308 | =item $bool = ber_is $tuple, $class, $tag, $constructed, $data |
323 | =item $bool = ber_is $tuple, $class, $tag, $flags, $data |
309 | |
324 | |
310 | This takes a BER C<$tuple> and matches its elements against the provided |
325 | This takes a BER C<$tuple> and matches its elements against the provided |
311 | values, all of which are optional - values that are either missing or |
326 | values, all of which are optional - values that are either missing or |
312 | C<undef> will be ignored, the others will be matched exactly (e.g. as if |
327 | C<undef> will be ignored, the others will be matched exactly (e.g. as if |
313 | you used C<==> or C<eq> (for C<$data>)). |
328 | you used C<==> or C<eq> (for C<$data>)). |
… | |
… | |
393 | use Exporter qw(import); |
408 | use Exporter qw(import); |
394 | |
409 | |
395 | our $VERSION; |
410 | our $VERSION; |
396 | |
411 | |
397 | BEGIN { |
412 | BEGIN { |
398 | $VERSION = 0.8; |
413 | $VERSION = 0.9; |
399 | XSLoader::load __PACKAGE__, $VERSION; |
414 | XSLoader::load __PACKAGE__, $VERSION; |
400 | } |
415 | } |
401 | |
416 | |
402 | our %EXPORT_TAGS = ( |
417 | our %EXPORT_TAGS = ( |
403 | const_index => [qw( |
418 | const_index => [qw( |
… | |
… | |
417 | BER_TYPE_BYTES BER_TYPE_UTF8 BER_TYPE_UCS2 BER_TYPE_UCS4 BER_TYPE_INT |
432 | BER_TYPE_BYTES BER_TYPE_UTF8 BER_TYPE_UCS2 BER_TYPE_UCS4 BER_TYPE_INT |
418 | BER_TYPE_OID BER_TYPE_RELOID BER_TYPE_NULL BER_TYPE_BOOL BER_TYPE_REAL |
433 | BER_TYPE_OID BER_TYPE_RELOID BER_TYPE_NULL BER_TYPE_BOOL BER_TYPE_REAL |
419 | BER_TYPE_IPADDRESS BER_TYPE_CROAK |
434 | BER_TYPE_IPADDRESS BER_TYPE_CROAK |
420 | )], |
435 | )], |
421 | const_snmp => [qw( |
436 | const_snmp => [qw( |
422 | SNMP_IPADDRESS SNMP_COUNTER32 SNMP_UNSIGNED32 SNMP_TIMETICKS SNMP_OPAQUE SNMP_COUNTER64 |
437 | SNMP_IPADDRESS SNMP_COUNTER32 SNMP_GAUGE32 SNMP_UNSIGNED32 |
|
|
438 | SNMP_TIMETICKS SNMP_OPAQUE SNMP_COUNTER64 |
423 | )], |
439 | )], |
424 | decode => [qw( |
440 | decode => [qw( |
425 | ber_decode |
441 | ber_decode ber_decode_prefix |
426 | ber_is ber_is_seq ber_is_int ber_is_oid |
442 | ber_is ber_is_seq ber_is_int ber_is_oid |
427 | )], |
443 | )], |
428 | encode => [qw( |
444 | encode => [qw( |
429 | ber_encode |
445 | ber_encode |
430 | ber_int |
446 | ber_int |
… | |
… | |
582 | C<BER_TYPE_BYTES>. When you don't want that but instead prefer a hard |
598 | C<BER_TYPE_BYTES>. When you don't want that but instead prefer a hard |
583 | error for some types, then C<BER_TYPE_CROAK> is for you. |
599 | error for some types, then C<BER_TYPE_CROAK> is for you. |
584 | |
600 | |
585 | =back |
601 | =back |
586 | |
602 | |
|
|
603 | =head2 Example Profile |
|
|
604 | |
|
|
605 | The following creates a profile suitable for SNMP - it's exactly identical |
|
|
606 | to the C<$Convert::BER::XS::SNMP_PROFILE> profile. |
|
|
607 | |
|
|
608 | our $SNMP_PROFILE = new Convert::BER::XS::Profile; |
|
|
609 | |
|
|
610 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); |
|
|
611 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); |
|
|
612 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); |
|
|
613 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); |
|
|
614 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_OPAQUE , BER_TYPE_IPADDRESS); |
|
|
615 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT); |
|
|
616 | |
587 | =cut |
617 | =cut |
588 | |
618 | |
589 | our $DEFAULT_PROFILE = new Convert::BER::XS::Profile; |
619 | our $DEFAULT_PROFILE = new Convert::BER::XS::Profile; |
590 | our $SNMP_PROFILE = new Convert::BER::XS::Profile; |
620 | |
|
|
621 | $DEFAULT_PROFILE->_set_default; |
591 | |
622 | |
592 | # additional SNMP application types |
623 | # additional SNMP application types |
|
|
624 | our $SNMP_PROFILE = new Convert::BER::XS::Profile; |
|
|
625 | |
593 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); |
626 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); |
594 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); |
627 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); |
595 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); |
628 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); |
596 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); |
629 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); |
597 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_OPAQUE , BER_TYPE_IPADDRESS); |
630 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_OPAQUE , BER_TYPE_IPADDRESS); |
598 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT); |
631 | $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT); |
599 | |
632 | |
600 | $DEFAULT_PROFILE->_set_default; |
|
|
601 | |
|
|
602 | 1; |
633 | 1; |
603 | |
634 | |
604 | =head2 LIMITATIONS/NOTES |
635 | =head2 LIMITATIONS/NOTES |
605 | |
636 | |
606 | This module can only en-/decode 64 bit signed and unsigned integers, and |
637 | This module can only en-/decode 64 bit signed and unsigned integers, and |
… | |
… | |
619 | Constructed strings are decoded just fine, but there should be a way to |
650 | Constructed strings are decoded just fine, but there should be a way to |
620 | join them for convenience. |
651 | join them for convenience. |
621 | |
652 | |
622 | REAL values are not supported and will currently croak. |
653 | REAL values are not supported and will currently croak. |
623 | |
654 | |
|
|
655 | The encoder and decoder tend to accept more formats than should be |
|
|
656 | strictly supported. |
|
|
657 | |
624 | This module has undergone little to no testing so far. |
658 | This module has undergone little to no testing so far. |
625 | |
659 | |
626 | =head2 ITHREADS SUPPORT |
660 | =head2 ITHREADS SUPPORT |
627 | |
661 | |
628 | This module is unlikely to work when the (officially discouraged) ithreads |
662 | This module is unlikely to work when the (officially discouraged) ithreads |