--- Convert-BER-XS/XS.pm 2020/02/06 23:15:44 1.62 +++ Convert-BER-XS/XS.pm 2021/03/03 05:30:23 1.63 @@ -6,16 +6,17 @@ use Convert::BER::XS ':all'; + # decode a binary BER data structure using the SNMP profile my $ber = ber_decode $buf, $Convert::BER::XS::SNMP_PROFILE or die "unable to decode SNMP message"; # The above results in a data structure consisting of # (class, tag, flags, data) - # tuples. Below is such a message, SNMPv1 trap + # tuples. Below is such a message, an SNMPv1 trap # with a Cisco mac change notification. - # Did you know that Cisco is in the news almost + # (Did you know that Cisco is in the news almost # every week because of some backdoor password - # or other extremely stupid security bug? + # or other extremely stupid security bug?) [ ASN_UNIVERSAL, ASN_SEQUENCE, 1, [ @@ -38,17 +39,18 @@ ] ], ... - # let's dump it, for debugging + # let's dump the above structure, for debugging ber_dump $ber, $Convert::BER::XS::SNMP_PROFILE; - # let's decode it a bit with some helper functions - + # let's decode it a bit with some helper functions. + # first check whether it starts with a sequence my $msg = ber_is_seq $ber or die "SNMP message does not start with a sequence"; + # then check if its some kind of integer ber_is $msg->[0], ASN_UNIVERSAL, ASN_INTEGER, 0 - or die "SNMP message does not start with snmp version\n"; + or die "SNMP message does not start with snmp version"; # message is SNMP v1 or v2c? if ($msg->[0][BER_DATA] == 0 || $msg->[0][BER_DATA] == 1) { @@ -66,7 +68,6 @@ ... and so on # finally, let's encode it again and hope it results in the same bit pattern - my $buf = ber_encode $ber, $Convert::BER::XS::SNMP_PROFILE; =head1 DESCRIPTION @@ -175,7 +176,7 @@ "constructed") or not (is "primitive"). Tags are simple integers, and ASN.1 defines a somewhat weird assortment -of those - for example, you have one integers and 16(!) different +of those - for example, you have one integer but 16(!) different string types, but there is no Unsigned32 type for example. Different applications work around this in different ways, for example, SNMP defines application-specific Gauge32, Counter32 and Unsigned32, which are mapped @@ -468,6 +469,7 @@ $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS); $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT); +$SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT); $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT); $SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT); @@ -565,8 +567,6 @@ "($value)" } -$SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT); - sub _ber_dump { my ($ber, $profile, $indent) = @_; @@ -650,6 +650,9 @@ A profile with mappings for SNMP-specific application tags added. This is useful when de-/encoding SNMP data. +The L section, below, shows how this profile is being +constructed. + Example: $ber = ber_decode $data, $Convert::BER::XS::SNMP_PROFILE;