|
Packit |
667938 |
### -*- mode: Perl -*-
|
|
Packit |
667938 |
######################################################################
|
|
Packit |
667938 |
### BER (Basic Encoding Rules) encoding and decoding.
|
|
Packit |
667938 |
######################################################################
|
|
Packit |
667938 |
### Copyright (c) 1995-2008, Simon Leinen.
|
|
Packit |
667938 |
###
|
|
Packit |
667938 |
### This program is free software; you can redistribute it under the
|
|
Packit |
667938 |
### "Artistic License 2.0" included in this distribution
|
|
Packit |
667938 |
### (file "Artistic").
|
|
Packit |
667938 |
######################################################################
|
|
Packit |
667938 |
### This module implements encoding and decoding of ASN.1-based data
|
|
Packit |
667938 |
### structures using the Basic Encoding Rules (BER). Only the subset
|
|
Packit |
667938 |
### necessary for SNMP is implemented.
|
|
Packit |
667938 |
######################################################################
|
|
Packit |
667938 |
### Created by: Simon Leinen <simon@switch.ch>
|
|
Packit |
667938 |
###
|
|
Packit |
667938 |
### Contributions and fixes by:
|
|
Packit |
667938 |
###
|
|
Packit |
667938 |
### Andrzej Tobola <san@iem.pw.edu.pl>: Added long String decode
|
|
Packit |
667938 |
### Tobias Oetiker <tobi@oetiker.ch>: Added 5 Byte Integer decode ...
|
|
Packit |
667938 |
### Dave Rand <dlr@Bungi.com>: Added SysUpTime decode
|
|
Packit |
667938 |
### Philippe Simonet <sip00@vg.swissptt.ch>: Support larger subids
|
|
Packit |
667938 |
### Yufang HU <yhu@casc.com>: Support even larger subids
|
|
Packit |
667938 |
### Mike Mitchell <Mike.Mitchell@sas.com>: New generalized encode_int()
|
|
Packit |
667938 |
### Mike Diehn <mdiehn@mindspring.net>: encode_ip_address()
|
|
Packit |
667938 |
### Rik Hoorelbeke <rik.hoorelbeke@pandora.be>: encode_oid() fix
|
|
Packit |
667938 |
### Brett T Warden <wardenb@eluminant.com>: pretty UInteger32
|
|
Packit |
667938 |
### Bert Driehuis <driehuis@playbeing.org>: Handle SNMPv2 exception codes
|
|
Packit |
667938 |
### Jakob Ilves (/IlvJa) <jakob.ilves@oracle.com>: PDU decoding
|
|
Packit |
667938 |
### Jan Kasprzak <kas@informatics.muni.cz>: Fix for PDU syntax check
|
|
Packit |
667938 |
### Milen Pavlov <milen@batmbg.com>: Recognize variant length for ints
|
|
Packit |
667938 |
######################################################################
|
|
Packit |
667938 |
|
|
Packit |
667938 |
package BER;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
require 5.002;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
use strict;
|
|
Packit |
667938 |
use vars qw(@ISA @EXPORT $VERSION $pretty_print_timeticks
|
|
Packit |
667938 |
%pretty_printer %default_printer $errmsg);
|
|
Packit |
667938 |
use Exporter;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$VERSION = '1.05';
|
|
Packit |
667938 |
|
|
Packit |
667938 |
@ISA = qw(Exporter);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
@EXPORT = qw(context_flag constructor_flag
|
|
Packit |
667938 |
encode_int encode_int_0 encode_null encode_oid
|
|
Packit |
667938 |
encode_sequence encode_tagged_sequence
|
|
Packit |
667938 |
encode_string encode_ip_address encode_timeticks
|
|
Packit |
667938 |
encode_uinteger32 encode_counter32 encode_counter64
|
|
Packit |
667938 |
encode_gauge32
|
|
Packit |
667938 |
decode_sequence decode_by_template
|
|
Packit |
667938 |
pretty_print pretty_print_timeticks
|
|
Packit |
667938 |
hex_string hex_string_of_type
|
|
Packit |
667938 |
encoded_oid_prefix_p errmsg
|
|
Packit |
667938 |
register_pretty_printer unregister_pretty_printer);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
### Variables
|
|
Packit |
667938 |
|
|
Packit |
667938 |
## Bind this to zero if you want to avoid that TimeTicks are converted
|
|
Packit |
667938 |
## into "human readable" strings containing days, hours, minutes and
|
|
Packit |
667938 |
## seconds.
|
|
Packit |
667938 |
##
|
|
Packit |
667938 |
## If the variable is zero, pretty_print will simply return an
|
|
Packit |
667938 |
## unsigned integer representing hundredths of seconds.
|
|
Packit |
667938 |
##
|
|
Packit |
667938 |
$pretty_print_timeticks = 1;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
### Prototypes
|
|
Packit |
667938 |
sub encode_header ($$);
|
|
Packit |
667938 |
sub encode_int_0 ();
|
|
Packit |
667938 |
sub encode_int ($);
|
|
Packit |
667938 |
sub encode_oid (@);
|
|
Packit |
667938 |
sub encode_null ();
|
|
Packit |
667938 |
sub encode_sequence (@);
|
|
Packit |
667938 |
sub encode_tagged_sequence ($@);
|
|
Packit |
667938 |
sub encode_string ($);
|
|
Packit |
667938 |
sub encode_ip_address ($);
|
|
Packit |
667938 |
sub encode_timeticks ($);
|
|
Packit |
667938 |
sub pretty_print ($);
|
|
Packit |
667938 |
sub pretty_using_decoder ($$);
|
|
Packit |
667938 |
sub pretty_string ($);
|
|
Packit |
667938 |
sub pretty_intlike ($);
|
|
Packit |
667938 |
sub pretty_unsignedlike ($);
|
|
Packit |
667938 |
sub pretty_oid ($);
|
|
Packit |
667938 |
sub pretty_uptime ($);
|
|
Packit |
667938 |
sub pretty_uptime_value ($);
|
|
Packit |
667938 |
sub pretty_ip_address ($);
|
|
Packit |
667938 |
sub pretty_generic_sequence ($);
|
|
Packit |
667938 |
sub register_pretty_printer ($);
|
|
Packit |
667938 |
sub unregister_pretty_printer ($);
|
|
Packit |
667938 |
sub hex_string ($);
|
|
Packit |
667938 |
sub hex_string_of_type ($$);
|
|
Packit |
667938 |
sub decode_oid ($);
|
|
Packit |
667938 |
sub decode_by_template;
|
|
Packit |
667938 |
sub decode_by_template_2;
|
|
Packit |
667938 |
sub decode_sequence ($);
|
|
Packit |
667938 |
sub decode_int ($);
|
|
Packit |
667938 |
sub decode_intlike ($);
|
|
Packit |
667938 |
sub decode_unsignedlike ($);
|
|
Packit |
667938 |
sub decode_intlike_s ($$);
|
|
Packit |
667938 |
sub decode_string ($);
|
|
Packit |
667938 |
sub decode_length ($@);
|
|
Packit |
667938 |
sub encoded_oid_prefix_p ($$);
|
|
Packit |
667938 |
sub decode_subid ($$$);
|
|
Packit |
667938 |
sub decode_generic_tlv ($);
|
|
Packit |
667938 |
sub error (@);
|
|
Packit |
667938 |
sub template_error ($$$);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub version () { $VERSION; }
|
|
Packit |
667938 |
|
|
Packit |
667938 |
### Flags for different types of tags
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub universal_flag { 0x00 }
|
|
Packit |
667938 |
sub application_flag { 0x40 }
|
|
Packit |
667938 |
sub context_flag { 0x80 }
|
|
Packit |
667938 |
sub private_flag { 0xc0 }
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub primitive_flag { 0x00 }
|
|
Packit |
667938 |
sub constructor_flag { 0x20 }
|
|
Packit |
667938 |
|
|
Packit |
667938 |
### Universal tags
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub boolean_tag { 0x01 }
|
|
Packit |
667938 |
sub int_tag { 0x02 }
|
|
Packit |
667938 |
sub bit_string_tag { 0x03 }
|
|
Packit |
667938 |
sub octet_string_tag { 0x04 }
|
|
Packit |
667938 |
sub null_tag { 0x05 }
|
|
Packit |
667938 |
sub object_id_tag { 0x06 }
|
|
Packit |
667938 |
sub sequence_tag { 0x10 }
|
|
Packit |
667938 |
sub set_tag { 0x11 }
|
|
Packit |
667938 |
sub uptime_tag { 0x43 }
|
|
Packit |
667938 |
|
|
Packit |
667938 |
### Flag for length octet announcing multi-byte length field
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub long_length { 0x80 }
|
|
Packit |
667938 |
|
|
Packit |
667938 |
### SNMP specific tags
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub snmp_ip_address_tag { 0x00 | application_flag () }
|
|
Packit |
667938 |
sub snmp_counter32_tag { 0x01 | application_flag () }
|
|
Packit |
667938 |
sub snmp_gauge32_tag { 0x02 | application_flag () }
|
|
Packit |
667938 |
sub snmp_timeticks_tag { 0x03 | application_flag () }
|
|
Packit |
667938 |
sub snmp_opaque_tag { 0x04 | application_flag () }
|
|
Packit |
667938 |
sub snmp_nsap_address_tag { 0x05 | application_flag () }
|
|
Packit |
667938 |
sub snmp_counter64_tag { 0x06 | application_flag () }
|
|
Packit |
667938 |
sub snmp_uinteger32_tag { 0x07 | application_flag () }
|
|
Packit |
667938 |
|
|
Packit |
667938 |
## Error codes (SNMPv2 and later)
|
|
Packit |
667938 |
##
|
|
Packit |
667938 |
sub snmp_nosuchobject { context_flag () | 0x00 }
|
|
Packit |
667938 |
sub snmp_nosuchinstance { context_flag () | 0x01 }
|
|
Packit |
667938 |
sub snmp_endofmibview { context_flag () | 0x02 }
|
|
Packit |
667938 |
|
|
Packit |
667938 |
### pretty-printer initialization code. Create a hash with
|
|
Packit |
667938 |
### the most common types of pretty-printer routines.
|
|
Packit |
667938 |
|
|
Packit |
667938 |
BEGIN {
|
|
Packit |
667938 |
$default_printer{int_tag()} = \&pretty_intlike;
|
|
Packit |
667938 |
$default_printer{snmp_counter32_tag()} = \&pretty_unsignedlike;
|
|
Packit |
667938 |
$default_printer{snmp_gauge32_tag()} = \&pretty_unsignedlike;
|
|
Packit |
667938 |
$default_printer{snmp_counter64_tag()} = \&pretty_unsignedlike;
|
|
Packit |
667938 |
$default_printer{snmp_uinteger32_tag()} = \&pretty_unsignedlike;
|
|
Packit |
667938 |
$default_printer{octet_string_tag()} = \&pretty_string;
|
|
Packit |
667938 |
$default_printer{object_id_tag()} = \&pretty_oid;
|
|
Packit |
667938 |
$default_printer{snmp_ip_address_tag()} = \&pretty_ip_address;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
%pretty_printer = %default_printer;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
#### Encoding
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_header ($$) {
|
|
Packit |
667938 |
my ($type,$length) = @_;
|
|
Packit |
667938 |
return pack ("C C", $type, $length) if $length < 128;
|
|
Packit |
667938 |
return pack ("C C C", $type, long_length | 1, $length) if $length < 256;
|
|
Packit |
667938 |
return pack ("C C n", $type, long_length | 2, $length) if $length < 65536;
|
|
Packit |
667938 |
return error ("Cannot encode length $length yet");
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_int_0 () {
|
|
Packit |
667938 |
return pack ("C C C", 2, 1, 0);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_int ($) {
|
|
Packit |
667938 |
return encode_intlike ($_[0], int_tag);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_uinteger32 ($) {
|
|
Packit |
667938 |
return encode_intlike ($_[0], snmp_uinteger32_tag);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_counter32 ($) {
|
|
Packit |
667938 |
return encode_intlike ($_[0], snmp_counter32_tag);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_counter64 ($) {
|
|
Packit |
667938 |
return encode_intlike ($_[0], snmp_counter64_tag);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_gauge32 ($) {
|
|
Packit |
667938 |
return encode_intlike ($_[0], snmp_gauge32_tag);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_intlike ($$) {
|
|
Packit |
667938 |
my ($int, $tag)=@_;
|
|
Packit |
667938 |
my ($sign, $val, @vals);
|
|
Packit |
667938 |
$sign = ($int >= 0) ? 0 : 0xff;
|
|
Packit |
667938 |
if (ref $int && $int->isa ("Math::BigInt")) {
|
|
Packit |
667938 |
for(;;) {
|
|
Packit |
667938 |
$val = $int->copy()->bmod (256);
|
|
Packit |
667938 |
unshift(@vals, $val);
|
|
Packit |
667938 |
return encode_header ($tag, $#vals + 1).pack ("C*", @vals)
|
|
Packit |
667938 |
if ($int >= -128 && $int < 128);
|
|
Packit |
667938 |
$int->bsub ($sign)->bdiv (256);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
for(;;) {
|
|
Packit |
667938 |
$val = $int & 0xff;
|
|
Packit |
667938 |
unshift(@vals, $val);
|
|
Packit |
667938 |
return encode_header ($tag, $#vals + 1).pack ("C*", @vals)
|
|
Packit |
667938 |
if ($int >= -128 && $int < 128);
|
|
Packit |
667938 |
$int -= $sign, $int = int($int / 256);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_oid (@) {
|
|
Packit |
667938 |
my @oid = @_;
|
|
Packit |
667938 |
my ($result,$subid);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$result = '';
|
|
Packit |
667938 |
## Ignore leading empty sub-ID. The favourite reason for
|
|
Packit |
667938 |
## those to occur is that people cut&paste numeric OIDs from
|
|
Packit |
667938 |
## CMU/UCD SNMP including the leading dot.
|
|
Packit |
667938 |
shift @oid if $oid[0] eq '';
|
|
Packit |
667938 |
|
|
Packit |
667938 |
return error ("Object ID too short: ", join('.',@oid))
|
|
Packit |
667938 |
if $#oid < 1;
|
|
Packit |
667938 |
## The first two subids in an Object ID are encoded as a single
|
|
Packit |
667938 |
## byte in BER, according to a funny convention. This poses
|
|
Packit |
667938 |
## restrictions on the ranges of those subids. In the past, I
|
|
Packit |
667938 |
## didn't check for those. But since so many people try to use
|
|
Packit |
667938 |
## OIDs in CMU/UCD SNMP's format and leave out the mib-2 or
|
|
Packit |
667938 |
## enterprises prefix, I introduced this check to catch those
|
|
Packit |
667938 |
## errors.
|
|
Packit |
667938 |
##
|
|
Packit |
667938 |
return error ("first subid too big in Object ID ", join('.',@oid))
|
|
Packit |
667938 |
if $oid[0] > 2;
|
|
Packit |
667938 |
$result = shift (@oid) * 40;
|
|
Packit |
667938 |
$result += shift @oid;
|
|
Packit |
667938 |
return error ("second subid too big in Object ID ", join('.',@oid))
|
|
Packit |
667938 |
if $result > 255;
|
|
Packit |
667938 |
$result = pack ("C", $result);
|
|
Packit |
667938 |
foreach $subid (@oid) {
|
|
Packit |
667938 |
if ( ($subid>=0) && ($subid<128) ){ #7 bits long subid
|
|
Packit |
667938 |
$result .= pack ("C", $subid);
|
|
Packit |
667938 |
} elsif ( ($subid>=128) && ($subid<16384) ){ #14 bits long subid
|
|
Packit |
667938 |
$result .= pack ("CC", 0x80 | $subid >> 7, $subid & 0x7f);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
elsif ( ($subid>=16384) && ($subid<2097152) ) {#21 bits long subid
|
|
Packit |
667938 |
$result .= pack ("CCC",
|
|
Packit |
667938 |
0x80 | (($subid>>14) & 0x7f),
|
|
Packit |
667938 |
0x80 | (($subid>>7) & 0x7f),
|
|
Packit |
667938 |
$subid & 0x7f);
|
|
Packit |
667938 |
} elsif ( ($subid>=2097152) && ($subid<268435456) ){ #28 bits long subid
|
|
Packit |
667938 |
$result .= pack ("CCCC",
|
|
Packit |
667938 |
0x80 | (($subid>>21) & 0x7f),
|
|
Packit |
667938 |
0x80 | (($subid>>14) & 0x7f),
|
|
Packit |
667938 |
0x80 | (($subid>>7) & 0x7f),
|
|
Packit |
667938 |
$subid & 0x7f);
|
|
Packit |
667938 |
} elsif ( ($subid>=268435456) && ($subid<4294967296) ){ #32 bits long subid
|
|
Packit |
667938 |
$result .= pack ("CCCCC",
|
|
Packit |
667938 |
0x80 | (($subid>>28) & 0x0f), #mask the bits beyond 32
|
|
Packit |
667938 |
0x80 | (($subid>>21) & 0x7f),
|
|
Packit |
667938 |
0x80 | (($subid>>14) & 0x7f),
|
|
Packit |
667938 |
0x80 | (($subid>>7) & 0x7f),
|
|
Packit |
667938 |
$subid & 0x7f);
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
return error ("Cannot encode subid $subid");
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
encode_header (object_id_tag, length $result).$result;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_null () { encode_header (null_tag, 0); }
|
|
Packit |
667938 |
sub encode_sequence (@) { encode_tagged_sequence (sequence_tag, @_); }
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_tagged_sequence ($@) {
|
|
Packit |
667938 |
my ($tag,$result);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$tag = shift @_;
|
|
Packit |
667938 |
$result = join '',@_;
|
|
Packit |
667938 |
return encode_header ($tag | constructor_flag, length $result).$result;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_string ($) {
|
|
Packit |
667938 |
my ($string)=@_;
|
|
Packit |
667938 |
return encode_header (octet_string_tag, length $string).$string;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_ip_address ($) {
|
|
Packit |
667938 |
my ($addr)=@_;
|
|
Packit |
667938 |
my @octets;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
if (length $addr == 4) {
|
|
Packit |
667938 |
## Four bytes... let's suppose that this is a binary IP address
|
|
Packit |
667938 |
## in network byte order.
|
|
Packit |
667938 |
return encode_header (snmp_ip_address_tag, length $addr).$addr;
|
|
Packit |
667938 |
} elsif (@octets = ($addr =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
|
|
Packit |
667938 |
return encode_ip_address (pack ("CCCC", @octets));
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
return error ("IP address must be four bytes long or a dotted-quad");
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub encode_timeticks ($) {
|
|
Packit |
667938 |
my ($tt) = @_;
|
|
Packit |
667938 |
return encode_intlike ($tt, snmp_timeticks_tag);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
#### Decoding
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub pretty_print ($) {
|
|
Packit |
667938 |
my ($packet) = @_;
|
|
Packit |
667938 |
return undef unless defined $packet;
|
|
Packit |
667938 |
my $result = ord (substr ($packet, 0, 1));
|
|
Packit |
667938 |
if (exists ($pretty_printer{$result})) {
|
|
Packit |
667938 |
my $c_ref = $pretty_printer{$result};
|
|
Packit |
667938 |
return &$c_ref ($packet);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
return ($pretty_print_timeticks
|
|
Packit |
667938 |
? pretty_uptime ($packet)
|
|
Packit |
667938 |
: pretty_unsignedlike ($packet))
|
|
Packit |
667938 |
if $result == uptime_tag;
|
|
Packit |
667938 |
return "(null)" if $result == null_tag;
|
|
Packit |
667938 |
return error ("Exception code: noSuchObject") if $result == snmp_nosuchobject;
|
|
Packit |
667938 |
return error ("Exception code: noSuchInstance") if $result == snmp_nosuchinstance;
|
|
Packit |
667938 |
return error ("Exception code: endOfMibView") if $result == snmp_endofmibview;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# IlvJa
|
|
Packit |
667938 |
# pretty print sequences and their contents.
|
|
Packit |
667938 |
|
|
Packit |
667938 |
my $ctx_cons_flags = context_flag | constructor_flag;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
if($result == (&constructor_flag | &sequence_tag) # sequence
|
|
Packit |
667938 |
|| $result == (0 | $ctx_cons_flags) #get_request
|
|
Packit |
667938 |
|| $result == (1 | $ctx_cons_flags) #getnext_request
|
|
Packit |
667938 |
|| $result == (2 | $ctx_cons_flags) #response
|
|
Packit |
667938 |
|| $result == (3 | $ctx_cons_flags) #set_request
|
|
Packit |
667938 |
|| $result == (4 | $ctx_cons_flags) #trap_request
|
|
Packit |
667938 |
|| $result == (5 | $ctx_cons_flags) #getbulk_request
|
|
Packit |
667938 |
|| $result == (6 | $ctx_cons_flags) #inform_request
|
|
Packit |
667938 |
|| $result == (7 | $ctx_cons_flags) #trap2_request
|
|
Packit |
667938 |
)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
my $pretty_result = pretty_generic_sequence($packet);
|
|
Packit |
667938 |
$pretty_result =~ s/^/ /gm; #Indent.
|
|
Packit |
667938 |
|
|
Packit |
667938 |
my $seq_type_desc =
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
(constructor_flag | sequence_tag) => "Sequence",
|
|
Packit |
667938 |
(0 | $ctx_cons_flags) => "GetRequest",
|
|
Packit |
667938 |
(1 | $ctx_cons_flags) => "GetNextRequest",
|
|
Packit |
667938 |
(2 | $ctx_cons_flags) => "Response",
|
|
Packit |
667938 |
(3 | $ctx_cons_flags) => "SetRequest",
|
|
Packit |
667938 |
(4 | $ctx_cons_flags) => "Trap",
|
|
Packit |
667938 |
(5 | $ctx_cons_flags) => "GetBulkRequest",
|
|
Packit |
667938 |
(6 | $ctx_cons_flags) => "InformRequest",
|
|
Packit |
667938 |
(7 | $ctx_cons_flags) => "SNMPv2-Trap",
|
|
Packit |
667938 |
(8 | $ctx_cons_flags) => "Report",
|
|
Packit |
667938 |
}->{($result)};
|
|
Packit |
667938 |
|
|
Packit |
667938 |
return $seq_type_desc . "{\n" . $pretty_result . "\n}";
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
return sprintf ("#<unprintable BER type 0x%x>", $result);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub pretty_using_decoder ($$) {
|
|
Packit |
667938 |
my ($decoder, $packet) = @_;
|
|
Packit |
667938 |
my ($decoded,$rest);
|
|
Packit |
667938 |
($decoded,$rest) = &$decoder ($packet);
|
|
Packit |
667938 |
return error ("Junk after object") unless $rest eq '';
|
|
Packit |
667938 |
return $decoded;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub pretty_string ($) {
|
|
Packit |
667938 |
pretty_using_decoder (\&decode_string, $_[0]);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub pretty_intlike ($) {
|
|
Packit |
667938 |
my $decoded = pretty_using_decoder (\&decode_intlike, $_[0]);
|
|
Packit |
667938 |
$decoded;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub pretty_unsignedlike ($) {
|
|
Packit |
667938 |
return pretty_using_decoder (\&decode_unsignedlike, $_[0]);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub pretty_oid ($) {
|
|
Packit |
667938 |
my ($oid) = shift;
|
|
Packit |
667938 |
my ($result,$subid,$next);
|
|
Packit |
667938 |
my (@oid);
|
|
Packit |
667938 |
$result = ord (substr ($oid, 0, 1));
|
|
Packit |
667938 |
return error ("Object ID expected") unless $result == object_id_tag;
|
|
Packit |
667938 |
($result, $oid) = decode_length ($oid, 1);
|
|
Packit |
667938 |
return error ("inconsistent length in OID") unless $result == length $oid;
|
|
Packit |
667938 |
@oid = ();
|
|
Packit |
667938 |
$subid = ord (substr ($oid, 0, 1));
|
|
Packit |
667938 |
push @oid, int ($subid / 40);
|
|
Packit |
667938 |
push @oid, $subid % 40;
|
|
Packit |
667938 |
$oid = substr ($oid, 1);
|
|
Packit |
667938 |
while ($oid ne '') {
|
|
Packit |
667938 |
$subid = ord (substr ($oid, 0, 1));
|
|
Packit |
667938 |
if ($subid < 128) {
|
|
Packit |
667938 |
$oid = substr ($oid, 1);
|
|
Packit |
667938 |
push @oid, $subid;
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
$next = $subid;
|
|
Packit |
667938 |
$subid = 0;
|
|
Packit |
667938 |
while ($next >= 128) {
|
|
Packit |
667938 |
$subid = ($subid << 7) + ($next & 0x7f);
|
|
Packit |
667938 |
$oid = substr ($oid, 1);
|
|
Packit |
667938 |
$next = ord (substr ($oid, 0, 1));
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
$subid = ($subid << 7) + $next;
|
|
Packit |
667938 |
$oid = substr ($oid, 1);
|
|
Packit |
667938 |
push @oid, $subid;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
join ('.', @oid);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub pretty_uptime ($) {
|
|
Packit |
667938 |
my ($packet,$uptime);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
($uptime,$packet) = &decode_unsignedlike (@_);
|
|
Packit |
667938 |
pretty_uptime_value ($uptime);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub pretty_uptime_value ($) {
|
|
Packit |
667938 |
my ($uptime) = @_;
|
|
Packit |
667938 |
my ($seconds,$minutes,$hours,$days,$result);
|
|
Packit |
667938 |
## We divide the uptime by hundred since we're not interested in
|
|
Packit |
667938 |
## sub-second precision.
|
|
Packit |
667938 |
$uptime = int ($uptime / 100);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$days = int ($uptime / (60 * 60 * 24));
|
|
Packit |
667938 |
$uptime %= (60 * 60 * 24);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$hours = int ($uptime / (60 * 60));
|
|
Packit |
667938 |
$uptime %= (60 * 60);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$minutes = int ($uptime / 60);
|
|
Packit |
667938 |
$seconds = $uptime % 60;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
if ($days == 0){
|
|
Packit |
667938 |
$result = sprintf ("%d:%02d:%02d", $hours, $minutes, $seconds);
|
|
Packit |
667938 |
} elsif ($days == 1) {
|
|
Packit |
667938 |
$result = sprintf ("%d day, %d:%02d:%02d",
|
|
Packit |
667938 |
$days, $hours, $minutes, $seconds);
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
$result = sprintf ("%d days, %d:%02d:%02d",
|
|
Packit |
667938 |
$days, $hours, $minutes, $seconds);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
return $result;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub pretty_ip_address ($) {
|
|
Packit |
667938 |
my $pdu = shift;
|
|
Packit |
667938 |
my ($length, $rest);
|
|
Packit |
667938 |
return error ("IP Address tag (".snmp_ip_address_tag.") expected")
|
|
Packit |
667938 |
unless ord (substr ($pdu, 0, 1)) == snmp_ip_address_tag;
|
|
Packit |
667938 |
($length,$pdu) = decode_length ($pdu, 1);
|
|
Packit |
667938 |
return error ("Length of IP address should be four")
|
|
Packit |
667938 |
unless $length == 4;
|
|
Packit |
667938 |
sprintf "%d.%d.%d.%d", unpack ("CCCC", $pdu);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# IlvJa
|
|
Packit |
667938 |
# Returns a string with the pretty prints of all
|
|
Packit |
667938 |
# the elements in the sequence.
|
|
Packit |
667938 |
sub pretty_generic_sequence ($) {
|
|
Packit |
667938 |
my ($pdu) = shift;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
my $rest;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
my $type = ord substr ($pdu, 0 ,1);
|
|
Packit |
667938 |
my $flags = context_flag | constructor_flag;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
return error (sprintf ("Tag 0x%x is not a valid sequence tag",$type))
|
|
Packit |
667938 |
unless ($type == (&constructor_flag | &sequence_tag) # sequence
|
|
Packit |
667938 |
|| $type == (0 | $flags) #get_request
|
|
Packit |
667938 |
|| $type == (1 | $flags) #getnext_request
|
|
Packit |
667938 |
|| $type == (2 | $flags) #response
|
|
Packit |
667938 |
|| $type == (3 | $flags) #set_request
|
|
Packit |
667938 |
|| $type == (4 | $flags) #trap_request
|
|
Packit |
667938 |
|| $type == (5 | $flags) #getbulk_request
|
|
Packit |
667938 |
|| $type == (6 | $flags) #inform_request
|
|
Packit |
667938 |
|| $type == (7 | $flags) #trap2_request
|
|
Packit |
667938 |
);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
my $curelem;
|
|
Packit |
667938 |
my $pretty_result; # Holds the pretty printed sequence.
|
|
Packit |
667938 |
my $pretty_elem; # Holds the pretty printed current elem.
|
|
Packit |
667938 |
my $first_elem = 'true';
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# Cut away the first Tag and Length from $packet and then
|
|
Packit |
667938 |
# init $rest with that.
|
|
Packit |
667938 |
(undef, $rest) = decode_length ($pdu, 1);
|
|
Packit |
667938 |
while($rest)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
($curelem,$rest) = decode_generic_tlv($rest);
|
|
Packit |
667938 |
$pretty_elem = pretty_print($curelem);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$pretty_result .= "\n" if not $first_elem;
|
|
Packit |
667938 |
$pretty_result .= $pretty_elem;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# The rest of the iterations are not related to the
|
|
Packit |
667938 |
# first element of the sequence so..
|
|
Packit |
667938 |
$first_elem = '' if $first_elem;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
return $pretty_result;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub hex_string ($) {
|
|
Packit |
667938 |
&hex_string_of_type ($_[0], octet_string_tag);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub hex_string_of_type ($$) {
|
|
Packit |
667938 |
my ($pdu, $wanted_type) = @_;
|
|
Packit |
667938 |
my ($length);
|
|
Packit |
667938 |
return error ("BER tag ".$wanted_type." expected")
|
|
Packit |
667938 |
unless ord (substr ($pdu, 0, 1)) == $wanted_type;
|
|
Packit |
667938 |
($length,$pdu) = decode_length ($pdu, 1);
|
|
Packit |
667938 |
hex_string_aux ($pdu);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub hex_string_aux ($) {
|
|
Packit |
667938 |
my ($binary_string) = @_;
|
|
Packit |
667938 |
my ($c, $result);
|
|
Packit |
667938 |
$result = '';
|
|
Packit |
667938 |
for $c (unpack "C*", $binary_string) {
|
|
Packit |
667938 |
$result .= sprintf "%02x", $c;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
$result;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_oid ($) {
|
|
Packit |
667938 |
my ($pdu) = @_;
|
|
Packit |
667938 |
my ($result,$pdu_rest);
|
|
Packit |
667938 |
my (@result);
|
|
Packit |
667938 |
$result = ord (substr ($pdu, 0, 1));
|
|
Packit |
667938 |
return error ("Object ID expected") unless $result == object_id_tag;
|
|
Packit |
667938 |
($result, $pdu_rest) = decode_length ($pdu, 1);
|
|
Packit |
667938 |
return error ("Short PDU")
|
|
Packit |
667938 |
if $result > length $pdu_rest;
|
|
Packit |
667938 |
@result = (substr ($pdu, 0, $result + (length ($pdu) - length ($pdu_rest))),
|
|
Packit |
667938 |
substr ($pdu_rest, $result));
|
|
Packit |
667938 |
@result;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# IlvJa
|
|
Packit |
667938 |
# This takes a PDU and returns a two element list consisting of
|
|
Packit |
667938 |
# the first element found in the PDU (whatever it is) and the
|
|
Packit |
667938 |
# rest of the PDU
|
|
Packit |
667938 |
sub decode_generic_tlv ($) {
|
|
Packit |
667938 |
my ($pdu) = @_;
|
|
Packit |
667938 |
my (@result);
|
|
Packit |
667938 |
my ($elemlength,$pdu_rest) = decode_length ($pdu, 1);
|
|
Packit |
667938 |
@result = (# Extract the first element.
|
|
Packit |
667938 |
substr ($pdu, 0, $elemlength + (length ($pdu)
|
|
Packit |
667938 |
- length ($pdu_rest)
|
|
Packit |
667938 |
)
|
|
Packit |
667938 |
),
|
|
Packit |
667938 |
#Extract the rest of the PDU.
|
|
Packit |
667938 |
substr ($pdu_rest, $elemlength)
|
|
Packit |
667938 |
);
|
|
Packit |
667938 |
@result;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_by_template {
|
|
Packit |
667938 |
my ($pdu) = shift;
|
|
Packit |
667938 |
local ($_) = shift;
|
|
Packit |
667938 |
return decode_by_template_2 ($pdu, $_, 0, 0, @_);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
my $template_debug = 0;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_by_template_2 {
|
|
Packit |
667938 |
my ($pdu, $template, $pdu_index, $template_index);
|
|
Packit |
667938 |
local ($_);
|
|
Packit |
667938 |
$pdu = shift;
|
|
Packit |
667938 |
$template = $_ = shift;
|
|
Packit |
667938 |
$pdu_index = shift;
|
|
Packit |
667938 |
$template_index = shift;
|
|
Packit |
667938 |
my (@results);
|
|
Packit |
667938 |
my ($length,$expected,$read,$rest);
|
|
Packit |
667938 |
return undef unless defined $pdu;
|
|
Packit |
667938 |
while (0 < length ($_)) {
|
|
Packit |
667938 |
if (substr ($_, 0, 1) eq '%') {
|
|
Packit |
667938 |
print STDERR "template $_ ", length $pdu," bytes remaining\n"
|
|
Packit |
667938 |
if $template_debug;
|
|
Packit |
667938 |
$_ = substr ($_,1);
|
|
Packit |
667938 |
++$template_index;
|
|
Packit |
667938 |
if (($expected) = /^(\d*|\*)\{(.*)/) {
|
|
Packit |
667938 |
## %{
|
|
Packit |
667938 |
$template_index += length ($expected) + 1;
|
|
Packit |
667938 |
print STDERR "%{\n" if $template_debug;
|
|
Packit |
667938 |
$_ = $2;
|
|
Packit |
667938 |
$expected = shift | constructor_flag if ($expected eq '*');
|
|
Packit |
667938 |
$expected = sequence_tag | constructor_flag
|
|
Packit |
667938 |
if $expected eq '';
|
|
Packit |
667938 |
return template_error ("Unexpected end of PDU",
|
|
Packit |
667938 |
$template, $template_index)
|
|
Packit |
667938 |
if !defined $pdu or $pdu eq '';
|
|
Packit |
667938 |
return template_error ("Expected sequence tag $expected, got ".
|
|
Packit |
667938 |
ord (substr ($pdu, 0, 1)),
|
|
Packit |
667938 |
$template,
|
|
Packit |
667938 |
$template_index)
|
|
Packit |
667938 |
unless (ord (substr ($pdu, 0, 1)) == $expected);
|
|
Packit |
667938 |
(($length,$pdu) = decode_length ($pdu, 1))
|
|
Packit |
667938 |
|| return template_error ("cannot read length",
|
|
Packit |
667938 |
$template, $template_index);
|
|
Packit |
667938 |
return template_error ("Expected length $length, got ".length $pdu ,
|
|
Packit |
667938 |
$template, $template_index)
|
|
Packit |
667938 |
unless length $pdu == $length;
|
|
Packit |
667938 |
} elsif (($expected,$rest) = /^(\*|)s(.*)/) {
|
|
Packit |
667938 |
## %s
|
|
Packit |
667938 |
$template_index += length ($expected) + 1;
|
|
Packit |
667938 |
($expected = shift) if $expected eq '*';
|
|
Packit |
667938 |
(($read,$pdu) = decode_string ($pdu))
|
|
Packit |
667938 |
|| return template_error ("cannot read string",
|
|
Packit |
667938 |
$template, $template_index);
|
|
Packit |
667938 |
print STDERR "%s => $read\n" if $template_debug;
|
|
Packit |
667938 |
if ($expected eq '') {
|
|
Packit |
667938 |
push @results, $read;
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
return template_error ("Expected $expected, read $read",
|
|
Packit |
667938 |
$template, $template_index)
|
|
Packit |
667938 |
unless $expected eq $read;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
$_ = $rest;
|
|
Packit |
667938 |
} elsif (($rest) = /^A(.*)/) {
|
|
Packit |
667938 |
## %A
|
|
Packit |
667938 |
$template_index += 1;
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
my ($tag, $length, $value);
|
|
Packit |
667938 |
$tag = ord (substr ($pdu, 0, 1));
|
|
Packit |
667938 |
return error ("Expected IP address, got tag ".$tag)
|
|
Packit |
667938 |
unless $tag == snmp_ip_address_tag;
|
|
Packit |
667938 |
($length, $pdu) = decode_length ($pdu, 1);
|
|
Packit |
667938 |
return error ("Inconsistent length of InetAddress encoding")
|
|
Packit |
667938 |
if $length > length $pdu;
|
|
Packit |
667938 |
return template_error ("IP address must be four bytes long",
|
|
Packit |
667938 |
$template, $template_index)
|
|
Packit |
667938 |
unless $length == 4;
|
|
Packit |
667938 |
$read = substr ($pdu, 0, $length);
|
|
Packit |
667938 |
$pdu = substr ($pdu, $length);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
print STDERR "%A => $read\n" if $template_debug;
|
|
Packit |
667938 |
push @results, $read;
|
|
Packit |
667938 |
$_ = $rest;
|
|
Packit |
667938 |
} elsif (/^O(.*)/) {
|
|
Packit |
667938 |
## %O
|
|
Packit |
667938 |
$template_index += 1;
|
|
Packit |
667938 |
$_ = $1;
|
|
Packit |
667938 |
(($read,$pdu) = decode_oid ($pdu))
|
|
Packit |
667938 |
|| return template_error ("cannot read OID",
|
|
Packit |
667938 |
$template, $template_index);
|
|
Packit |
667938 |
print STDERR "%O => ".pretty_oid ($read)."\n"
|
|
Packit |
667938 |
if $template_debug;
|
|
Packit |
667938 |
push @results, $read;
|
|
Packit |
667938 |
} elsif (($expected,$rest) = /^(\d*|\*|)i(.*)/) {
|
|
Packit |
667938 |
## %i
|
|
Packit |
667938 |
$template_index += length ($expected) + 1;
|
|
Packit |
667938 |
print STDERR "%i\n" if $template_debug;
|
|
Packit |
667938 |
$_ = $rest;
|
|
Packit |
667938 |
(($read,$pdu) = decode_int ($pdu))
|
|
Packit |
667938 |
|| return template_error ("cannot read int",
|
|
Packit |
667938 |
$template, $template_index);
|
|
Packit |
667938 |
if ($expected eq '') {
|
|
Packit |
667938 |
push @results, $read;
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
$expected = int (shift) if $expected eq '*';
|
|
Packit |
667938 |
return template_error (sprintf ("Expected %d (0x%x), got %d (0x%x)",
|
|
Packit |
667938 |
$expected, $expected, $read, $read),
|
|
Packit |
667938 |
$template, $template_index)
|
|
Packit |
667938 |
unless ($expected == $read)
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
} elsif (($rest) = /^u(.*)/) {
|
|
Packit |
667938 |
## %u
|
|
Packit |
667938 |
$template_index += 1;
|
|
Packit |
667938 |
print STDERR "%u\n" if $template_debug;
|
|
Packit |
667938 |
$_ = $rest;
|
|
Packit |
667938 |
(($read,$pdu) = decode_unsignedlike ($pdu))
|
|
Packit |
667938 |
|| return template_error ("cannot read uptime",
|
|
Packit |
667938 |
$template, $template_index);
|
|
Packit |
667938 |
push @results, $read;
|
|
Packit |
667938 |
} elsif (/^\@(.*)/) {
|
|
Packit |
667938 |
## %@
|
|
Packit |
667938 |
$template_index += 1;
|
|
Packit |
667938 |
print STDERR "%@\n" if $template_debug;
|
|
Packit |
667938 |
$_ = $1;
|
|
Packit |
667938 |
push @results, $pdu;
|
|
Packit |
667938 |
$pdu = '';
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
return template_error ("Unknown decoding directive in template: $_",
|
|
Packit |
667938 |
$template, $template_index);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
if (substr ($_, 0, 1) ne substr ($pdu, 0, 1)) {
|
|
Packit |
667938 |
return template_error ("Expected ".substr ($_, 0, 1).", got ".substr ($pdu, 0, 1),
|
|
Packit |
667938 |
$template, $template_index);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
$_ = substr ($_,1);
|
|
Packit |
667938 |
$pdu = substr ($pdu,1);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
return template_error ("PDU too long", $template, $template_index)
|
|
Packit |
667938 |
if length ($pdu) > 0;
|
|
Packit |
667938 |
return template_error ("PDU too short", $template, $template_index)
|
|
Packit |
667938 |
if length ($_) > 0;
|
|
Packit |
667938 |
@results;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_sequence ($) {
|
|
Packit |
667938 |
my ($pdu) = @_;
|
|
Packit |
667938 |
my ($result);
|
|
Packit |
667938 |
my (@result);
|
|
Packit |
667938 |
$result = ord (substr ($pdu, 0, 1));
|
|
Packit |
667938 |
return error ("Sequence expected")
|
|
Packit |
667938 |
unless $result == (sequence_tag | constructor_flag);
|
|
Packit |
667938 |
($result, $pdu) = decode_length ($pdu, 1);
|
|
Packit |
667938 |
return error ("Short PDU")
|
|
Packit |
667938 |
if $result > length $pdu;
|
|
Packit |
667938 |
@result = (substr ($pdu, 0, $result), substr ($pdu, $result));
|
|
Packit |
667938 |
@result;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_int ($) {
|
|
Packit |
667938 |
my ($pdu) = @_;
|
|
Packit |
667938 |
my $tag = ord (substr ($pdu, 0, 1));
|
|
Packit |
667938 |
return error ("Integer expected, found tag ".$tag)
|
|
Packit |
667938 |
unless $tag == int_tag;
|
|
Packit |
667938 |
decode_intlike ($pdu);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_intlike ($) {
|
|
Packit |
667938 |
decode_intlike_s ($_[0], 1);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_unsignedlike ($) {
|
|
Packit |
667938 |
decode_intlike_s ($_[0], 0);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
my $have_math_bigint_p = 0;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_intlike_s ($$) {
|
|
Packit |
667938 |
my ($pdu, $signedp) = @_;
|
|
Packit |
667938 |
my ($length,$result);
|
|
Packit |
667938 |
($length,$pdu) = decode_length ($pdu, 1);
|
|
Packit |
667938 |
my $ptr = 0;
|
|
Packit |
667938 |
$result = unpack ($signedp ? "c" : "C", substr ($pdu, $ptr++, 1));
|
|
Packit |
667938 |
if ($length > 5 || ($length == 5 && $result > 0)) {
|
|
Packit |
667938 |
require 'Math/BigInt.pm' unless $have_math_bigint_p++;
|
|
Packit |
667938 |
$result = new Math::BigInt ($result);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
while (--$length > 0) {
|
|
Packit |
667938 |
$result *= 256;
|
|
Packit |
667938 |
$result += unpack ("C", substr ($pdu, $ptr++, 1));
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
($result, substr ($pdu, $ptr));
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_string ($) {
|
|
Packit |
667938 |
my ($pdu) = shift;
|
|
Packit |
667938 |
my ($result);
|
|
Packit |
667938 |
$result = ord (substr ($pdu, 0, 1));
|
|
Packit |
667938 |
return error ("Expected octet string, got tag ".$result)
|
|
Packit |
667938 |
unless $result == octet_string_tag;
|
|
Packit |
667938 |
($result, $pdu) = decode_length ($pdu, 1);
|
|
Packit |
667938 |
return error ("Short PDU")
|
|
Packit |
667938 |
if $result > length $pdu;
|
|
Packit |
667938 |
return (substr ($pdu, 0, $result), substr ($pdu, $result));
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub decode_length ($@) {
|
|
Packit |
667938 |
my ($pdu) = shift;
|
|
Packit |
667938 |
my $index = shift || 0;
|
|
Packit |
667938 |
my ($result);
|
|
Packit |
667938 |
my (@result);
|
|
Packit |
667938 |
$result = ord (substr ($pdu, $index, 1));
|
|
Packit |
667938 |
if ($result & long_length) {
|
|
Packit |
667938 |
if ($result == (long_length | 1)) {
|
|
Packit |
667938 |
@result = (ord (substr ($pdu, $index+1, 1)), substr ($pdu, $index+2));
|
|
Packit |
667938 |
} elsif ($result == (long_length | 2)) {
|
|
Packit |
667938 |
@result = ((ord (substr ($pdu, $index+1, 1)) << 8)
|
|
Packit |
667938 |
+ ord (substr ($pdu, $index+2, 1)), substr ($pdu, $index+3));
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
return error ("Unsupported length");
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
@result = ($result, substr ($pdu, $index+1));
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
@result;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# This takes a hashref that specifies functions to call when
|
|
Packit |
667938 |
# the specified value type is being printed. It returns the
|
|
Packit |
667938 |
# number of functions that were registered.
|
|
Packit |
667938 |
sub register_pretty_printer($)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
my ($h_ref) = shift;
|
|
Packit |
667938 |
my ($type, $val, $cnt);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$cnt = 0;
|
|
Packit |
667938 |
while(($type, $val) = each %$h_ref) {
|
|
Packit |
667938 |
if (ref $val eq "CODE") {
|
|
Packit |
667938 |
$pretty_printer{$type} = $val;
|
|
Packit |
667938 |
$cnt++;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
return($cnt);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
# This takes a hashref that specifies functions to call when
|
|
Packit |
667938 |
# the specified value type is being printed. It removes the
|
|
Packit |
667938 |
# functions from the list for the types specified.
|
|
Packit |
667938 |
# It returns the number of functions that were unregistered.
|
|
Packit |
667938 |
sub unregister_pretty_printer($)
|
|
Packit |
667938 |
{
|
|
Packit |
667938 |
my ($h_ref) = shift;
|
|
Packit |
667938 |
my ($type, $val, $cnt);
|
|
Packit |
667938 |
|
|
Packit |
667938 |
$cnt = 0;
|
|
Packit |
667938 |
while(($type, $val) = each %$h_ref) {
|
|
Packit |
667938 |
if ((exists ($pretty_printer{$type}))
|
|
Packit |
667938 |
&& ($pretty_printer{$type} == $val)) {
|
|
Packit |
667938 |
if (exists($default_printer{$type})) {
|
|
Packit |
667938 |
$pretty_printer{$type} = $default_printer{$type};
|
|
Packit |
667938 |
} else {
|
|
Packit |
667938 |
delete $pretty_printer{$type};
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
$cnt++;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
return($cnt);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
#### OID prefix check
|
|
Packit |
667938 |
|
|
Packit |
667938 |
### encoded_oid_prefix_p OID1 OID2
|
|
Packit |
667938 |
###
|
|
Packit |
667938 |
### OID1 and OID2 should be BER-encoded OIDs.
|
|
Packit |
667938 |
### The function returns non-zero iff OID1 is a prefix of OID2.
|
|
Packit |
667938 |
### This can be used in the termination condition of a loop that walks
|
|
Packit |
667938 |
### a table using GetNext or GetBulk.
|
|
Packit |
667938 |
###
|
|
Packit |
667938 |
sub encoded_oid_prefix_p ($$) {
|
|
Packit |
667938 |
my ($oid1, $oid2) = @_;
|
|
Packit |
667938 |
my ($i1, $i2);
|
|
Packit |
667938 |
my ($l1, $l2);
|
|
Packit |
667938 |
my ($subid1, $subid2);
|
|
Packit |
667938 |
return error ("OID tag expected") unless ord (substr ($oid1, 0, 1)) == object_id_tag;
|
|
Packit |
667938 |
return error ("OID tag expected") unless ord (substr ($oid2, 0, 1)) == object_id_tag;
|
|
Packit |
667938 |
($l1,$oid1) = decode_length ($oid1, 1);
|
|
Packit |
667938 |
($l2,$oid2) = decode_length ($oid2, 1);
|
|
Packit |
667938 |
for ($i1 = 0, $i2 = 0;
|
|
Packit |
667938 |
$i1 < $l1 && $i2 < $l2;
|
|
Packit |
667938 |
++$i1, ++$i2) {
|
|
Packit |
667938 |
($subid1,$i1) = &decode_subid ($oid1, $i1, $l1);
|
|
Packit |
667938 |
($subid2,$i2) = &decode_subid ($oid2, $i2, $l2);
|
|
Packit |
667938 |
return 0 unless $subid1 == $subid2;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
return $i2 if $i1 == $l1;
|
|
Packit |
667938 |
return 0;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
### decode_subid OID INDEX
|
|
Packit |
667938 |
###
|
|
Packit |
667938 |
### Decodes a subid field from a BER-encoded object ID.
|
|
Packit |
667938 |
### Returns two values: the field, and the index of the last byte that
|
|
Packit |
667938 |
### was actually decoded.
|
|
Packit |
667938 |
###
|
|
Packit |
667938 |
sub decode_subid ($$$) {
|
|
Packit |
667938 |
my ($oid, $i, $l) = @_;
|
|
Packit |
667938 |
my $subid = 0;
|
|
Packit |
667938 |
my $next;
|
|
Packit |
667938 |
|
|
Packit |
667938 |
while (($next = ord (substr ($oid, $i, 1))) >= 128) {
|
|
Packit |
667938 |
$subid = ($subid << 7) + ($next & 0x7f);
|
|
Packit |
667938 |
++$i;
|
|
Packit |
667938 |
return error ("decoding object ID: short field")
|
|
Packit |
667938 |
unless $i < $l;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
return (($subid << 7) + $next, $i);
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub error (@) {
|
|
Packit |
667938 |
$errmsg = join ("",@_);
|
|
Packit |
667938 |
return undef;
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
sub template_error ($$$) {
|
|
Packit |
667938 |
my ($errmsg, $template, $index) = @_;
|
|
Packit |
667938 |
return error ($errmsg."\n ".$template."\n ".(' ' x $index)."^");
|
|
Packit |
667938 |
}
|
|
Packit |
667938 |
|
|
Packit |
667938 |
1;
|