SNMP Variable Types

Here is a quick explanation of SNMP variable types as implemented in SnmpSharpNet.

First from the high level, all variable type classes are derived from the AsnType class. AsnType is an abstract class that forces derived classes to expose encode() and decode() methods and Type property.

Why is this important? encode() and decode() methods are used to encode values into a binary stream that can be transmitted to SNMP peers and decode() will decode those same binary encoded SNMP values into data you can work with in your .NET application.

Type property represents a one byte code identifying value type. As mentioned in introduction to SNMP, each value is encoded using TLV (Type, Length, Value) representation. This one byte value represents the "T" value of the TLV encoding.

Type property can be very handy when you are trying to evaluate Variable Binding value entries as they are stored as AsnType and to properly get access to the values or access methods specific to a SNMP value type you will need to cast the AsnType value to a specific class. For example, you can do the following:

AsnType val = new OctetString("This is my test string");
if (val.Type == SnmpConstants.SMI_STRING)
{
	OctetString ostr = (OctetString)val;
	Console.WriteLine("Character 2 is {0}", ostr[1]);
}
else
{
	Console.WriteLine("AsnType is not an OctetString class instance.");
}

Have a look at the constants defined in SnmpConstants class starting with SMI_ for the available data types you can check this way.

Type property can be used to retrieve text name of the variable type specific class:

AsnType val = new OctetString("This is my test string");
Console.WriteLine("Variable [{0}] is type {1}", val.ToString(), SnmpConstants.GetTypeName(val.Type));