JFLib

netpkt/ber.rs#

Source: lib/netpkt/ber.rs

ber -- the Basic Encoding Rules of ASN.1 (X.690) as SNMP uses them (RFC 3417 section 8): the
universal types INTEGER, OCTET STRING, NULL, OBJECT IDENTIFIER and SEQUENCE, the application
types of the SMI (RFC 2578: IpAddress, Counter32, Gauge32, TimeTicks, Opaque, Counter64) and
the exceptions of SNMPv2 (RFC 3416: noSuchObject, noSuchInstance, endOfMibView)

  Oid                   an OBJECT IDENTIFIER as its arcs: parse("1.3.6.1.2.1.1.5.0") (a
                        leading dot is allowed), Display (dotted), Ord (the lexicographic
                        order of SNMP: a prefix before what it starts), starts_with, child,
                        encode() / decode(contents)
  Value                 what a variable binding holds: Int, Str, Null, Oid, Ip, Counter32,
                        Gauge32, TimeTicks, Opaque, Counter64, NoSuchObject, NoSuchInstance,
                        EndOfMibView; tag(), type_name(), is_exception()
  Tlv                   one element: its tag, where it starts, the length of its header and
                        its contents (offsets in the buffer the Reader walks)
  Reader                walks a buffer: peek(), tlv(), int(), uint(tag, max), octets(), null(),
                        oid(), value(), seq(tag) (a Reader over the contents), done()
  Writer                builds a message: int, uint, octets, null, oid, value, begin(tag) /
                        end() around the elements of a SEQUENCE or a PDU (the length goes in at
                        end()); into_vec()
  tlv(tag, contents)    one element as bytes
  Bad                   why bytes are not BER as SNMP wants it

Strict about lengths: every length must fit in what is left of its buffer and of the element
around it.  The indefinite form (0x80) is refused, a long form of more than four octets too
(RFC 3417 allows more length octets than needed, so 0x81 0x05 is read as 5); a tag of more than
one octet (low bits 31) is refused.  An INTEGER has 1 to 8 content octets, an unsigned value 9
when the first is 0x00; Counter32, Gauge32 and TimeTicks fit 32 bits (RFC 2578: 0 ..
4294967295), an Integer32 in a variable binding fits i32; NULL and the exceptions have no
contents; an IpAddress has four octets.  An OBJECT IDENTIFIER has at least one content octet,
each subidentifier in its shortest form (no leading 0x80, X.690 8.19.2), at most 2^32 - 1 and
ended (its last octet below 0x80), at most 128 subidentifiers (RFC 2578 3.5).  A simple type in
its constructed form (0x24 for an OCTET STRING) is no tag SNMP knows.  Writers always use the
shortest form of lengths and integers.
T_INTEGER: u8 = 0x02
T_OCTETS: u8 = 0x04
T_NULL: u8 = 0x05
T_OID: u8 = 0x06
T_SEQUENCE: u8 = 0x30
/// application types of the SMI (RFC 2578 7.1)
T_IPADDRESS: u8 = 0x40
T_COUNTER32: u8 = 0x41
T_GAUGE32: u8 = 0x42
T_TIMETICKS: u8 = 0x43
T_OPAQUE: u8 = 0x44
T_COUNTER64: u8 = 0x46
/// the exceptions of a variable binding in a Response (RFC 3416 3)
T_NO_SUCH_OBJECT: u8 = 0x80
T_NO_SUCH_INSTANCE: u8 = 0x81
T_END_OF_MIB_VIEW: u8 = 0x82
/// the most subidentifiers an OBJECT IDENTIFIER of the SMI has
OID_MAX: usize = 128
  • enum Bad why bytes are no BER element as SNMP wants it
    • Truncated -- the buffer ends inside a header or before the contents it announces
    • Indefinite -- the indefinite length form (forbidden by RFC 3417)
    • Length -- a long length form of more than four octets
    • Tag(u8) -- a tag of more than one octet, or one that does not belong here
    • IntSize -- an INTEGER without contents or with more octets than fit
    • Range -- a value outside its type's range (a negative counter, more than 32 bits)
    • Size -- NULL or an exception with contents, an IpAddress that is not four octets
    • Oid -- an OBJECT IDENTIFIER that is empty, not in its shortest form, too big or unended
    • Trailing -- bytes after the last element of a SEQUENCE or the message

impl Bad

  • fn text(self) -> &'static str what is wrong, for a dissector

  • struct Oid(pub Vec<u32>) an OBJECT IDENTIFIER: its arcs ("1.3.6.1.2.1.1.5.0")

impl Oid

  • fn new(arcs: &[u32]) -> Oid

  • fn parse(s: &str) -> Option<Oid> dotted decimal, a leading dot allowed; at least two arcs, the first 0, 1 or 2, the second below 40 under 0 and 1

  • fn arcs(&self) -> &[u32]

  • fn len(&self) -> usize

  • fn is_empty(&self) -> bool

  • fn starts_with(&self, prefix: &[u32]) -> bool this OID begins with prefix (or is it)

  • fn child(&self, more: &[u32]) -> Oid this OID with more arcs after it

  • fn encode(&self) -> Vec<u8> the contents octets of its encoding (X.690 8.19): the first two arcs as 40 X + Y, then each arc in base 128 with the high bit on all octets but its last (one arc: X.0)

  • fn decode(c: &[u8]) -> Result<Oid, Bad> the OID of contents octets

  • enum Value the value of a variable binding

    • Int(i64) -- INTEGER (Integer32 in a variable binding)
    • Str(Vec<u8>) -- OCTET STRING
    • Null
    • Oid(Oid)
    • Ip(Ipv4)
    • Counter32(u32)
    • Gauge32(u32)
    • TimeTicks(u32)
    • Opaque(Vec<u8>)
    • Counter64(u64)
    • NoSuchObject
    • NoSuchInstance
    • EndOfMibView

impl Value

  • fn text(s: &str) -> Value a text as an OCTET STRING

  • fn tag(&self) -> u8

  • fn type_name(&self) -> &'static str the name of its type as the SMI and RFC 3416 write it

  • fn is_exception(&self) -> bool one of the three exceptions of a Response

  • fn contents(&self) -> Vec<u8> the contents octets of its encoding

  • fn read(t: &Tlv, b: &[u8]) -> Result<Value, Bad> the value of an element read (the contents of t in b)

  • fn type_name(tag: u8) -> &'static str the name of a type of a variable binding (by its tag)

  • fn int_bytes(v: i64) -> Vec<u8> the shortest two's complement of v (one to eight octets)

  • fn uint_bytes(v: u64) -> Vec<u8> the shortest encoding of an unsigned value as an INTEGER's contents (a 0x00 before a first octet with its high bit set)

  • fn int_of(c: &[u8]) -> Result<i64, Bad> the contents of an INTEGER as a signed value

  • fn uint_of(c: &[u8], max: u64) -> Result<u64, Bad> the contents of an unsigned type (at most max; 9 octets only with a leading 0x00)

  • struct Tlv one element: tag, offset of its first octet, header length, contents length

    • tag: u8
    • off: usize
    • hdr: usize
    • len: usize

impl Tlv

  • fn body(&self) -> usize where its contents start

  • fn end(&self) -> usize where it ends

  • fn contents<'a>(&self, b: &'a [u8]) -> &'a [u8] its contents in the buffer it was read from

  • fn constructed(&self) -> bool a constructed element (a SEQUENCE or a PDU)

  • fn header(b: &[u8], off: usize, end: usize) -> Result<Tlv, Bad> the header of the element at off of b, which must end by end

  • struct Reader<'a> a walk over the elements of a buffer (or of one constructed element's contents)

impl<'a> Reader<'a>

  • fn new(b: &'a [u8]) -> Reader<'a> over the whole buffer

  • fn buf(&self) -> &'a [u8] the buffer it reads (offsets of Tlv are in it)

  • fn pos(&self) -> usize

  • fn is_empty(&self) -> bool nothing is left

  • fn done(&self) -> Result<(), Bad> Err(Trailing) while bytes are left

  • fn peek(&self) -> Result<Tlv, Bad> the header of the next element without taking it

  • fn tlv(&mut self) -> Result<Tlv, Bad> the next element

  • fn expect(&mut self, tag: u8) -> Result<Tlv, Bad> the next element, which must have tag tag

  • fn seq(&mut self, tag: u8) -> Result<Reader<'a>, Bad> a constructed element of tag tag: a reader over its contents

  • fn inside(&self, t: &Tlv) -> Reader<'a> the contents of a constructed element as a reader (any tag)

  • fn int(&mut self) -> Result<i64, Bad> an INTEGER

  • fn int32(&mut self) -> Result<i32, Bad> an INTEGER that fits i32 (request ids, error status and index)

  • fn uint(&mut self, tag: u8, max: u64) -> Result<u64, Bad> an unsigned value of tag tag up to max

  • fn octets(&mut self) -> Result<&'a [u8], Bad> an OCTET STRING

  • fn null(&mut self) -> Result<(), Bad>

  • fn oid(&mut self) -> Result<Oid, Bad>

  • fn value(&mut self) -> Result<Value, Bad> a value of a variable binding (any of its types)

  • fn length_bytes(n: usize) -> Vec<u8> the length octets of n in their shortest form

  • fn tlv(tag: u8, contents: &[u8]) -> Vec<u8> one element as bytes

  • struct Writer builds BER: elements one after the other, constructed ones between begin and end

impl Writer

  • fn new() -> Writer

  • fn int(&mut self, v: i64)

  • fn uint(&mut self, tag: u8, v: u64) an unsigned value under its application tag (Counter32, Gauge32, TimeTicks, Counter64)

  • fn octets(&mut self, b: &[u8])

  • fn null(&mut self)

  • fn oid(&mut self, o: &Oid)

  • fn value(&mut self, v: &Value)

  • fn raw(&mut self, b: &[u8]) bytes that are BER already

  • fn begin(&mut self, tag: u8) opens a constructed element (SEQUENCE 0x30, a PDU 0xA0 ...)

  • fn end(&mut self) closes the element begin() opened last: its length goes in after its tag

  • fn into_vec(mut self) -> Vec<u8> the bytes (every element begin() opened is closed first)

  • fn len(&self) -> usize

  • fn is_empty(&self) -> bool

  • fn printable(b: &[u8]) -> Option<String> an OCTET STRING as text when every octet is printable ASCII (tab and line ends allowed)

Source: docs/API.md in the JFLib repository.