SNMP
SNMP is quite different from others protocols: it uses ASN.1 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 Rasn1 wiki 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:
PDUs,which is a CHOICE between all SNMP PDUs,
which is the model of a SNMP Get request,
which is Trap PDU for SNMPv1,
Bulk,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
depends on its name).
SNMP base class
SNMP class is a simple ASN.1 object defined like this:
which is equivalent to this ASN.1 definition:
All objects may be accessed to #[] accessor:
For convenience, these upper fields are accessible through accessors, as for others headers:
SNMP::PDUs class
In RFC, PDUs is defined as:
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:
As RASN1::Model may delegate some methods to its root object, we can simplify previous code:
SNMP::GetRequest class
GetRequest PDU is defined as:
So a GetRequest object has these accessors:
You may add some VarBind:
SNMP::GetNextRequest class
GetNextRequest is a subclass of GetRequest, with only a different PDU identifier.
Creating a GetNextRequest may be done this way:
SNMP::GetResponse class
GetResponse is a subclass of GetRequest, with only a different PDU identifier.
Creating a GetResponse may be done this way:
SNMP::SetRequest class
GetResponse is a subclass of GetRequest, with only a different PDU identifier.
Creating a GetResponse may be done this way:
SNMP::Trapv1 class
Trapv1 PDU is defined as:
So a GetRequest object has these accessors:
:generic_trap may take these values:
cold_startor0,warm_startor1,link_downor2,link_upor3,auth_failureor4,egp_neighbor_lossor5,specificor6.
SNMP::Bulk class
Create a Bulk PDU:
Bulk accessors:
SNMP::InformRequest class
Create a InformRequest PDU:
InformRequest is a subclass of GetRequest, so it has the same accessors.
SNMP::Trapv2 class
Create a Trapv2 PDU:
Trapv2 is a subclass of GetRequest, so it has the same accessors.
SNMP::Report class
Create a Report PDU:
Report is a subclass of GetRequest, so it has the same accessors.
Last updated