BER − Basic Encoding Rules (BER) of Abstract Syntax Notation One (ASN.1)
use BER;
$encoded = encode_sequence (encode_int (123), encode_string
("foo"));
($i, $s) = decode_by_template ($encoded,
"%{%i%s");
# $i will now be 123, $s the string "foo".
This is a simple library to encode and decode data using the Basic Encoding Rules (BER) of Abstract Syntax Notation One (ASN.1). It does not claim to be a complete implementation of the standard, but implements enough of the BER standard to encode and decode SNMP messages.
If non-zero (the default), "pretty_print" will convert TimeTicks to "human readable" strings containing days, hours, minutes and seconds.
If the variable is zero, "pretty_print" will simply return an unsigned integer representing hundredths of seconds. If you prefer this, bind $pretty_print_timeticks to zero.
When they encounter errors, the routines in this module will generally return "undef") and leave an informative error message in $errmsg).
This is functionally identical to encode_int(0).
$encoded = encode_oid (1,3,6,1,...);
This is used e.g. in binding lists for variables that don’t have a value (yet)
$encoded =
encode_sequence (encoded1, encoded2, ...);
$encoded = encode_tagged_sequence (tag, encoded1, encoded2,
...);
Take already encoded values, and extend them to an encoded sequence. "encoded_sequence" uses the generic sequence tag, while with "encode_tagged_sequence" you can specify your own tag.
This can either be passed as a four-octet sequence in network byte order, or as a text string in dotted-quad notation, e.g. "192.0.2.234".
The integer should count hundredths of a second since the epoch defined by "sysUpTime.0".
This function can be extended by registering pretty-printing methods for specific type codes. Most BER type codes used in SNMP already have such methods pre-registered by default. See "register_pretty_printer" for how new methods can be added.
($var1, ...) = decode_by_template ($pdu, $template, ...);
The template can
contain various %X directives. Some directives consume
additional arguments following the template itself. Most
directives will cause values to be returned. The values are
returned as a sequence in the order of the directives that
generated them.
%{ − decode sequence.
This doesn’t assign any return value, just checks and skips the tag/length fields of the sequence. By default, the tag should be the generic sequence tag, but a tag can also be specified in the directive. The directive can either specify the tag as a prefix, e.g. "%99{" will require a sequence tag of 99, or if the directive is given as "%*{", the tag will be taken from the next argument.
%s − decode string
%i − decode integer
%u − decode unsigned integer
%O − decode Object ID (OID)
%A − decode IPv4 address
%@ − assigns the remaining undecoded part of the PDU
to the next return
value.
($first, $rest) = decode_sequence ($pdu);
Checks whether the PDU has a sequence type tag and a plausible length field. Splits the initial element off the list, and returns both this and the remainder of the PDU.
This function takes a hashref that specifies functions to call when the specified value type is being printed. It returns the number of functions that were registered.
Created by: Simon Leinen <[email protected]>
Contributions
and fixes by:
Andrzej Tobola <[email protected]>: Added long String
decode
Tobias Oetiker <[email protected]>: Added 5 Byte Integer
decode ...
Dave Rand <[email protected]>: Added "SysUpTime"
decode
Philippe Simonet <[email protected]>: Support
larger subids
Yufang HU <[email protected]>: Support even larger subids
Mike Mitchell <[email protected]>: New generalized
encode_int()
Mike Diehn <[email protected]>:
encode_ip_address()
Rik Hoorelbeke <[email protected]>:
encode_oid() fix
Brett T Warden <[email protected]>: pretty
"UInteger32"
Bert Driehuis <[email protected]>: Handle SNMPv2
exception codes
Jakob Ilves (/IlvJa) <[email protected]>: PDU
decoding
Jan Kasprzak <[email protected]>: Fix for PDU
syntax check
Milen Pavlov <[email protected]>: Recognize variant
length for ints
Copyright (c) 1995−2009, Simon Leinen.
This program is free software; you can redistribute it under the "Artistic License 2.0" included in this distribution (file "Artistic").