SNMP is quite different from others protocols: it uses data structures. So, to understand SNMP serialization/deserialization, you need to understand ASN.1.
ASN.1
ASN.1 is an interface description language for defining data structures that can be serialized and deserialized in a standard and cross-platform way. It is broadly used in telecommunications and computer networking, and also in cryptography (X.509 certificates are defined with ASN.1, for example).
ASN.1 is a notation independent of the way data are encoded. Encoding are specified in Encoding Rules.
The most used encoding rules are BER (Basic Encoding Rules) and DER (Distinguished Encoding Rules). Both are quite equivalent, but the latter is specified to guaranty uniqueness of encoding. But, in fact, there are just little differences between them.
ASN.1 provides basic objects, such as: integers, many kinds of strings, floats, booleans. It also provides some container objects (sequences and sets). They all are defined in Universal class. A protocol may defined others objects, which will be grouped in the Context class.For example, SNMP defines GetRequest-PDU object in this class. They also exist Private and Application classes.
Each basic object has a tag, used by the encoding rules. For example, Boolean has tag value 1, Integer has tag value 2. In context class, tags begin at 0xA0. For example, in SNMP context, 0xA0 tag is a GetRequest-PDU.
Others objects may be constructed from those basic ones using Sequences and Sets. Sequences are like ruby arrays. Sets are arrays limited to a unique object type.
Finally, a ASN.1 object is a tree, whose leafs are basic types, and non-leaf nodes are Sets or Sequences.
ASN.1 in PacketGen
In PacketGen, ASN.1 objetcs are handled using rasn1 gem. This gem defines basic ASN.1 objets and provides ways to decode and encode data in DER and BER encodings.
rasn1 gem
rasn1 gem provides a RASN1::Model class to define complex ASN.1 objects. See for a simple example.
SNMP
In PacketGen, SNMP header inherits from PacketGen::Header::ASN1Base, which inherits from RASN1::Model. Header::ASN1Base provides [[Header minimal API|Create Custom Protocol#Header minimal API]].
Some ASN1. objets are also defined in PacketGen::Header::SNMP namespace:
which is a CHOICE between all SNMP PDUs,
which is the model of a SNMP Get request,
which is Trap PDU for SNMPv1,
which is Trap PDU for SNMPv2,
which is a SEQUENCE OF (an array of) VarBind. This class is used in PDU classes,
which is an association between a name (as an OBJECT ID) and a value (its type
SNMP::PDUs class is defined as a subclass of RASN1::Model, and as a CHOICE.
Setting header type (or PDU type) may be done this way:
smp.data.root # access to root ASN.1 object in PDU class
# => RASN1::Types::CHOICE
snmp.data.root.chosen = 0 # Choose first CHOICE from PDUs: SNMP::GetRequest
snmp.data.root.chosen_value # => PacketGen::Header::SNMP::GetRequest
As RASN1::Model may delegate some methods to its root object, we can simplify previous code:
snmp.data.root # access to root ASN.1 object in PDU class
# => RASN1::Types::CHOICE
snmp.data.chosen = 0 # Choose first CHOICE from PDUs: SNMP::GetRequest
snmp.data.chosen_value # => PacketGen::Header::SNMP::GetRequest
# or even simpler
snmp.pdu # => PacketGen::Header::SNMP::GetRequest
SNMP::GetRequest class
GetRequest PDU is defined as:
GetRequest-PDU ::= [0] IMPLICIT PDU
PDU ::= SEQUENCE {
request-id INTEGER (-214783648..214783647),
error-status -- sometimes ignored
INTEGER {
noError(0),
tooBig(1),
noSuchName(2), -- for proxy compatibility
badValue(3), -- for proxy compatibility
readOnly(4), -- for proxy compatibility
genErr(5),
noAccess(6),
wrongType(7),
wrongLength(8),
wrongEncoding(9),
wrongValue(10),
noCreation(11),
inconsistentValue(12),
resourceUnavailable(13),
commitFailed(14),
undoFailed(15),
authorizationError(16),
notWritable(17),
inconsistentName(18)
},
error-index -- sometimes ignored
INTEGER (0..max-bindings),
variable-bindings -- values are sometimes ignored
VarBindList
}
# name is an OBJECT ID, and there is no value in a GetRequest (value set to NULL)
snmp.pdu[:varbindlist] << { name: '1.3.6.1.2.1.1.5.0' }
snmp.pdu[:varbindlist].value[0] # => PacketGen::Header::SNMP::VarBind
snmp.pdu[:varbindlist].value[0][:name] # => RASN1::Types::ObjectId
snmp.pdu[:varbindlist].value[0][:name].value # => '1.3.6.1.2.1.1.5.0'
SNMP::GetNextRequest class
GetNextRequest is a subclass of GetRequest, with only a different PDU identifier.