netpkt/snmp.rs#
Source: lib/netpkt/snmp.rs
snmp -- SNMP messages of version 1 (RFC 1157) and version 2c (RFC 1901, RFC 3416): the
community, the PDUs GetRequest, GetNextRequest, GetBulkRequest, SetRequest, Response, the
Trap of version 1, SNMPv2-Trap, InformRequest and Report with their variable bindings; the
error statuses and their mapping to version 1 (RFC 3584); the objects every notification names
Version V1 (0) | V2c (1); other numbers are no message here (SNMPv3: snmpv3.rs)
Kind Get | GetNext | Response | Set | TrapV1 | GetBulk | Inform | TrapV2 |
Report: tag() (0xA0 ..), of_tag(), name(), in_v1()
VarBind { oid, value } VarBind::null(oid): a name to ask for
Pdu { kind, request_id, error_status, error_index, varbinds } a GetBulkRequest keeps
non-repeaters and max-repetitions in the two error fields
(Pdu::bulk, non_repeaters(), max_repetitions())
TrapV1 { enterprise, agent_addr, generic, specific, timestamp, varbinds }
Body Pdu(Pdu) | Trap(TrapV1)
Message { version, community, body }: parse(b) / to_bytes() / pdu() / kind()
E_* (0 .. 18), error_name(status), v1_error(status) RFC 3416 3; RFC 3584 4.4
G_* (0 .. 6), generic_name(g), trap_oid(generic, enterprise, specific) RFC 3584 3.1
SYS_UPTIME_0, SNMP_TRAP_OID_0, SNMP_TRAP_ENTERPRISE_0, SNMP_TRAPS, COLD_START, WARM_START,
LINK_DOWN, LINK_UP, AUTH_FAILURE, IF_INDEX, IF_ADMIN_STATUS, IF_OPER_STATUS (RFC 3418, 2863)
PORT (161, the agent), PORT_TRAP (162, the notification receiver)
parse refuses what an SNMP entity counts as an ASN.1 parse error (snmpInASNParseErrs): BER that
is not well-formed (ber.rs), elements missing or in the wrong order, bytes after the message, a
PDU tag the message's version does not have (GetBulk, Inform, SNMPv2-Trap and Report in
version 1, the v1 Trap in 2c), and in version 1 the types it has not (Counter64, the
exceptions). A version other than 0 and 1 is Bad::Version (snmpInBadVersions). Error
status and index are read as they are; what they mean is the receiver's business.
/// the agent's UDP port
PORT: u16 = 161
/// the port notifications go to
PORT_TRAP: u16 = 162
enum Versionthe version of a community-based messageV1V2c
impl Version
fn number(self) -> i64the value of the version field (0 and 1)fn of_number(n: i64) -> Option<Version>fn word(self) -> &'static str"1", "2c" as the tools take it after -vfn parse(s: &str) -> Option<Version>enum Kindthe kind of a PDU (its context tag)GetGetNextResponseSetTrapV1GetBulkInformTrapV2Report
impl Kind
fn tag(self) -> u8fn of_tag(t: u8) -> Option<Kind>fn name(self) -> &'static strits name in RFC 3416 (version 1 calls the Response GetResponse)fn in_v1(self) -> boolversion 1 has it (RFC 1157: get, get-next, get-response, set, trap)fn in_v2c(self) -> boolversion 2c has it (every one but the v1 Trap)struct VarBinda variable binding: a name and its valueoid: Oidvalue: Value
impl VarBind
fn new(oid: Oid, value: Value) -> VarBindfn null(oid: Oid) -> VarBinda name with NULL as its value: what a request asks forstruct Pdua PDU with request id, error status and index (GetBulk: non-repeaters and max-repetitions)kind: Kindrequest_id: i32error_status: i32error_index: i32varbinds: Vec<VarBind>
impl Pdu
fn new(kind: Kind, request_id: i32, varbinds: Vec<VarBind>) -> Pdua request (or notification) without an errorfn bulk(request_id: i32, non_repeaters: i32, max_repetitions: i32, varbinds: Vec<VarBind>) -> Pdua GetBulkRequestfn non_repeaters(&self) -> usizeGetBulk: the first N bindings are asked once (RFC 3416 4.2.3: negative counts as 0)fn max_repetitions(&self) -> usizeGetBulk: how often the others repeatstruct TrapV1the Trap of version 1 (RFC 1157 4.1.6)enterprise: Oid-- the type of object that sends it (sysObjectID)agent_addr: Ipv4generic: i32specific: i32timestamp: u32-- sysUpTime when it was sentvarbinds: Vec<VarBind>
enum Bodywhat a message carriesPdu(Pdu)Trap(TrapV1)
struct Messagea community-based messageversion: Versioncommunity: Vec<u8>body: Body
enum Badwhy bytes are no SNMP message of version 1 or 2cBer(ber::Bad)-- not well-formed BER, or an element missing or out of placeVersion(i64)-- a version this module does not speak (SNMPv3 is 3)Pdu(u8)-- a PDU tag that is none, or one the message's version does not haveV1Type(u8)-- a type version 1 does not have (Counter64, an exception)
impl Bad
fn text(self) -> Stringwhat is wrong, for a dissector
impl Message
fn new(version: Version, community: &str, pdu: Pdu) -> Messagea message of one PDUfn parse(b: &[u8]) -> Result<Message, Bad>bytes as a messagefn to_bytes(&self) -> Vec<u8>fn pdu(&self) -> Option<&Pdu>the PDU (None for a v1 Trap)fn kind(&self) -> Kindfn community_text(&self) -> Stringthe community as text (bytes that are no UTF-8 replaced)fn varbinds(&self) -> &[VarBind]the variable bindings of the PDU or the trap
errors
E_NO_ERROR: i32 = 0
E_TOO_BIG: i32 = 1
E_NO_SUCH_NAME: i32 = 2
E_BAD_VALUE: i32 = 3
E_READ_ONLY: i32 = 4
E_GEN_ERR: i32 = 5
E_NO_ACCESS: i32 = 6
E_WRONG_TYPE: i32 = 7
E_WRONG_LENGTH: i32 = 8
E_WRONG_ENCODING: i32 = 9
E_WRONG_VALUE: i32 = 10
E_NO_CREATION: i32 = 11
E_INCONSISTENT_VALUE: i32 = 12
E_RESOURCE_UNAVAILABLE: i32 = 13
E_COMMIT_FAILED: i32 = 14
E_UNDO_FAILED: i32 = 15
E_AUTHORIZATION_ERROR: i32 = 16
E_NOT_WRITABLE: i32 = 17
E_INCONSISTENT_NAME: i32 = 18
fn error_name(e: i32) -> &'static strthe name of an error status (RFC 3416 3)fn v1_error(e: i32) -> i32the status a version-1 response carries for an error of RFC 3416 (RFC 3584 4.4)
notifications
G_COLD_START: i32 = 0
G_WARM_START: i32 = 1
G_LINK_DOWN: i32 = 2
G_LINK_UP: i32 = 3
G_AUTH_FAILURE: i32 = 4
G_EGP_NEIGHBOR_LOSS: i32 = 5
G_ENTERPRISE: i32 = 6
fn generic_name(g: i32) -> &'static strthe name of a generic trap of version 1
/// sysUpTime.0, the first binding of every SNMPv2 notification
SYS_UPTIME_0: [u32; 9] = [1, 3, 6, 1, 2, 1, 1, 3, 0]
/// snmpTrapOID.0, the second one: which notification it is
SNMP_TRAP_OID_0: [u32; 11] = [1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0]
/// snmpTrapEnterprise.0 (RFC 3584 3.1)
SNMP_TRAP_ENTERPRISE_0: [u32; 11] = [1, 3, 6, 1, 6, 3, 1, 1, 4, 3, 0]
/// snmpTraps: the generic notifications are its children (coldStart .1 .. egpNeighborLoss .6)
SNMP_TRAPS: [u32; 9] = [1, 3, 6, 1, 6, 3, 1, 1, 5]
COLD_START: [u32; 10] = [1, 3, 6, 1, 6, 3, 1, 1, 5, 1]
WARM_START: [u32; 10] = [1, 3, 6, 1, 6, 3, 1, 1, 5, 2]
LINK_DOWN: [u32; 10] = [1, 3, 6, 1, 6, 3, 1, 1, 5, 3]
LINK_UP: [u32; 10] = [1, 3, 6, 1, 6, 3, 1, 1, 5, 4]
AUTH_FAILURE: [u32; 10] = [1, 3, 6, 1, 6, 3, 1, 1, 5, 5]
/// the columns of ifTable a linkDown or linkUp names (IF-MIB, RFC 2863)
IF_INDEX: [u32; 10] = [1, 3, 6, 1, 2, 1, 2, 2, 1, 1]
IF_ADMIN_STATUS: [u32; 10] = [1, 3, 6, 1, 2, 1, 2, 2, 1, 7]
IF_OPER_STATUS: [u32; 10] = [1, 3, 6, 1, 2, 1, 2, 2, 1, 8]
fn trap_oid(generic: i32, enterprise: &Oid, specific: i32) -> Oidthe SNMPv2 notification of a version-1 trap (RFC 3584 3.1): snmpTraps.(generic + 1) for the generic ones, else enterprise.0.specificfn generic_of(trap: &Oid) -> Option<i32>the generic trap of an SNMPv2 notification (RFC 3584 3.2): Some(0 .. 5) for the generic ones, None for an enterprise-specific onefn find<'a>(vbs: &'a [VarBind], oid: &[u32]) -> Option<&'a Value>the value of the binding namedoid(the first one)fn notification(vbs: &[VarBind]) -> Option<&Oid>the notification an SNMPv2-Trap or InformRequest carries (snmpTrapOID.0, the second binding)