How to process MAC/Ethernet Addresses using EthernetAddress class

Ethernet/MAC address in SNMP requests and responses is a 6 byte OctetString value. Because there is no dedicated Ethernet address data type, library has no way to determine what is an Ethernet address field instead a generic OctetString that happens to be 6 bytes in length.

For this reason, if you wish to use the EthernetAddress class, you will need to do more work yourself instead of having it done by the library.

Basic steps are simple, you will need which variable will contain the Ethernet address, retrieve the variable and validate the OID for the value is correct (you want to make sure correct data type is passed to the EthernetAddress class), create an instance of the EthernetAddress class with the received value and voila, all done 🙂

Here is a quick example:

Oid atPhysAddress = new Oid("1.3.6.1.2.1.3.1.1.2");
foreach( Vb v in pkt.Pdu.VbList ) {
  if (atPhysAddress.IsRootOf(v.Oid))
  {
    EthernetAddress ethAddr = new EthernetAddress((OctetString)v.Value);
    Console.WriteLine(String.Format(" {0} ({1}): {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), ethAddr.ToString()));
  }
  else
  {
    Console.WriteLine(String.Format(" {0} ({1}): {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString()));
  }

The above example starts right after you’ve received the response packet and validated that there are no error (using Pdu.ErrorStatus). Just enumerate the values in the VbList and look for the OIDs that belong to the root that will contain Ethernet addresses. When found, handle EthernetAddress values appropriately.