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 Badwhy bytes are no BER element as SNMP wants itTruncated-- the buffer ends inside a header or before the contents it announcesIndefinite-- the indefinite length form (forbidden by RFC 3417)Length-- a long length form of more than four octetsTag(u8)-- a tag of more than one octet, or one that does not belong hereIntSize-- an INTEGER without contents or with more octets than fitRange-- 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 octetsOid-- an OBJECT IDENTIFIER that is empty, not in its shortest form, too big or unendedTrailing-- bytes after the last element of a SEQUENCE or the message
impl Bad
fn text(self) -> &'static strwhat is wrong, for a dissectorstruct Oid(pub Vec<u32>)an OBJECT IDENTIFIER: its arcs ("1.3.6.1.2.1.1.5.0")
impl Oid
fn new(arcs: &[u32]) -> Oidfn 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 1fn arcs(&self) -> &[u32]fn len(&self) -> usizefn is_empty(&self) -> boolfn starts_with(&self, prefix: &[u32]) -> boolthis OID begins withprefix(or is it)fn child(&self, more: &[u32]) -> Oidthis OID with more arcs after itfn 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 octetsenum Valuethe value of a variable bindingInt(i64)-- INTEGER (Integer32 in a variable binding)Str(Vec<u8>)-- OCTET STRINGNullOid(Oid)Ip(Ipv4)Counter32(u32)Gauge32(u32)TimeTicks(u32)Opaque(Vec<u8>)Counter64(u64)NoSuchObjectNoSuchInstanceEndOfMibView
impl Value
fn text(s: &str) -> Valuea text as an OCTET STRINGfn tag(&self) -> u8fn type_name(&self) -> &'static strthe name of its type as the SMI and RFC 3416 write itfn is_exception(&self) -> boolone of the three exceptions of a Responsefn contents(&self) -> Vec<u8>the contents octets of its encodingfn read(t: &Tlv, b: &[u8]) -> Result<Value, Bad>the value of an element read (the contents oftinb)fn type_name(tag: u8) -> &'static strthe name of a type of a variable binding (by its tag)fn int_bytes(v: i64) -> Vec<u8>the shortest two's complement ofv(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 valuefn uint_of(c: &[u8], max: u64) -> Result<u64, Bad>the contents of an unsigned type (at mostmax; 9 octets only with a leading 0x00)struct Tlvone element: tag, offset of its first octet, header length, contents lengthtag: u8off: usizehdr: usizelen: usize
impl Tlv
fn body(&self) -> usizewhere its contents startfn end(&self) -> usizewhere it endsfn contents<'a>(&self, b: &'a [u8]) -> &'a [u8]its contents in the buffer it was read fromfn constructed(&self) -> boola constructed element (a SEQUENCE or a PDU)fn header(b: &[u8], off: usize, end: usize) -> Result<Tlv, Bad>the header of the element atoffofb, which must end byendstruct 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 bufferfn buf(&self) -> &'a [u8]the buffer it reads (offsets of Tlv are in it)fn pos(&self) -> usizefn is_empty(&self) -> boolnothing is leftfn done(&self) -> Result<(), Bad>Err(Trailing) while bytes are leftfn peek(&self) -> Result<Tlv, Bad>the header of the next element without taking itfn tlv(&mut self) -> Result<Tlv, Bad>the next elementfn expect(&mut self, tag: u8) -> Result<Tlv, Bad>the next element, which must have tagtagfn seq(&mut self, tag: u8) -> Result<Reader<'a>, Bad>a constructed element of tagtag: a reader over its contentsfn 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 INTEGERfn 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 tagtagup tomaxfn octets(&mut self) -> Result<&'a [u8], Bad>an OCTET STRINGfn 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 ofnin their shortest formfn tlv(tag: u8, contents: &[u8]) -> Vec<u8>one element as bytesstruct Writerbuilds BER: elements one after the other, constructed ones between begin and end
impl Writer
fn new() -> Writerfn 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 alreadyfn 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 tagfn into_vec(mut self) -> Vec<u8>the bytes (every element begin() opened is closed first)fn len(&self) -> usizefn is_empty(&self) -> boolfn printable(b: &[u8]) -> Option<String>an OCTET STRING as text when every octet is printable ASCII (tab and line ends allowed)