Variable Bindings

Variable Bindings represent a set of Oid/Value pairs.

As described in other pages, each value available through SNMP is identified by a unique object identifier. To enable mapping of object identifiers to their values a special sequence is required. That sequence is variable binding.

Most requests involve multiple OIDs so a protocol data unit needs to have a way to encode multiple variable bindings. This is accomplished by encoding all variable bindings into an outer sequence TLV (where the value is multiple variable bindings).

Here is a graphical representation of a variable binding collection:

Individual Variable Bindings are stored in the Vb class. Multiple Variable Bindings are controlled by the Variable Binding collection represented by the VbCollection class.

Standard request (Get, GetNext or GetBulk) contains one or more Variable Bindings with the Object Identifier of the requested value and value of Null (as in SNMP Null not c# null). Agent will replace Null value with the actual value of the object identifier identified value in the reply.

One way to create Variable Binding(s) for a request is to construct individual variable binding entries and to add them to the Variable Binding collection:

Create a variable binding and add the Object identifier in string format:

Vb vb = new Vb("");

Create a variable binding and add the Object identifier in Oid format:

Oid oid = new Oid("");
Vb vb = new Vb(oid);

Variable Binding on its own is not very useful. SNMP requests, replies and notifications are collections of zero or more Variable Bindings. To make use of Variable Binding that you created it has to be added to a Variable Binding collection:

VbCollection vbcol = new VbCollection();

or VarBind collection that is part of a Pdu class:

Pdu pdu = new Pdu();

Variable Bing Vb class provides two data access properites: Vb.Oid of type Oid and Vb.Value of type AsnType.

Oid property is the Oid assigned to the Variable Binding and using this property you can access all methods and properties available in the Oid class.

Value property is returned SMI value cast to AsnType. AsnType is an abstract class that all SNMP value types implemented in Snmp#Net library are derived from. One of the properties AsnType class has is AsnType.Type which allows you to query the type of data and related Snmp#Net class that is represented by the Value property. Type code constants are defined as part of the SnmpConstants class and begin with SMI_ (for example Counter32 is defined as SnmpConstants.SMI_COUNTER32). Example:

Vb v = new Vb(new Oid(""), new Integer32(78));
if (v.Value.Type == SnmpConstants.SMI_INTEGER)
 Integer32 val = (Integer32)v.Value;
 Console.WriteLine("Integer value: {0}", val.Value);
  Console.WriteLine("Unknown data type: {0}", v.Value.Type);
// Prints: Integer value: 78

Variable Binding Collection

Variable Binding collection is managed using Snmp#Net VbCollection class.

Internally VbCollection class manages a list of Vb class instances, each with its own Oid/Value pair. During encoding of the Pdu VbCollection belongs to, individual variable bindings are encoded in the order they were added.

Methods available in the VbCollection class used to manage individual variable binds are:

VbCollection.Add(…) methods add new Vb instances to the end of the list of managed variable bindings. Available formats are:

VbCollection vbCol = new VbCollection();
Oid oidinst = new Oid("");
// Add a Vb with Oid and value Null (value is auto-added)
OctetString vbVal = new OctetString("New string value");
// Add a Vb with Oid and specific Value
vbCol.Add(oidinst, vbVal);
Vb vb = new Vb(oidinst, vbVal);
// Add pre-initialized Vb class instance
VbCollection vbCol2 = new VbCollection();
vbCol2.Add(new Oid(""));
// Add contents of another VbCollection

If you would like to change ordering of Vb instances within the VbCollection, you can use VbCollection.Insert(…) method:

Vb vb = new Vb(new Oid(""), new OctetString("New string value"));
// Add new Vb instance to the beginning of the Vb collection
vbCol.Insert(0, vb);

You can remove Vb instances from the collection using VbCollection.RemoveAt(…) method:

// Loop through all Vb's in collection until collection is emptied
while( vbCol.Count > 0 ) {
  Vb vb = vbCol[0];

You can access Vb’s stored in the VbCollection using multiple methods. First is using indexed access:

for(int i=0;i<vbCol.Count;i++)

You can access individual Vb’s using their Oid value (if you know it):

Oid vbOid = new Oid("");
Pdu pdu = new Pdu(PduType.Get);
// perform get operation, check that there were no errors, etc.
// assuming response Pdu is stored in the pdu variable
if( pdu.VbList.ContainsOid(vbOid) ) {
  Console.WriteLine("Value: {0}", pdu.VbList[vbOid].Value.ToString());

You can also use string representation of the Vb Oid:

String oidString = "";
Pdu pdu = new Pdu(PduType.Get);
// perform get operation, check that there were no errors, etc.
// assuming response Pdu is stored in the pdu variable
Vb vbResult = pdu.VbList[oidString];
if( vbResult != null ) {
  Console.WriteLine("Value: {0}", pdu.VbList[oidString].Value.ToString());

There is also IEnumerator interface that is implemented:

foreach( Vb vb in pdu.VbList ) {
  Console.WriteLine("{0}: {1}", vb.Oid.ToString(), vb.Value.ToString());

Or you can retrieve the list of Vb Oid values and then process them one at the time:

Oid[] oidList = pdu.VbList.OidArray();
foreach( Oid o in oidList ) {
  Vb v = pdu.VbList[o];
  if( v != null )
    Console.WriteLine("{0}: {1}", o.ToString(), v.Value.ToString());

Retrieving the list of Vb Oid’s from a VarBind collection is an inefficient process because each Oid instance stored in the class is duplicated to maintain class values integrity. Because of the duplication additional memory and CPU time is used that is not strictly speaking necessary. Other access methods described here do not have this down side.

Finally, when you are done with the VbCollection and would like to re-use it for additional requests, call the VbCollection.Clear() method to remove all the Vb’s and prepare it for a new operation.

Check out Snmp#Net documentation site for the list of other methods.