Converting SNMP OID into byte array

J

Jason James

Guys,

can anyone confirm the process on converting the OID into an array of
bytes for sending to the SNMP device. The code I have seems to only
work for values in the OID that are less than 2^14 but some enterprise
IDs are now larger than that. Do I just contine the conversion on to
additional bytes needed to house the OID in full?

So and enterprise ID of 25000 would be sent as three bytes of the
following:

0x01
0x43
0x28

assuming that my maths is correct?

Many thanks in advance,

Jason.
 
E

Eamon

Sorry I'm only new to SNMP (not sure this'll help), but from this code
I was looking at
(http://www.java2s.com/Code/CSharp/Network/SimpleSNMP.htm), I think
what you're doing is right.

In the "get(string request, string host, string community, string
mibstring)" function of the SNMP class in the code from the link,
integer values over 128 need to be converted into multiple bytes:

// START SNIPPET
// Convert the string MIB into a byte array of integer values
// Unfortunately, values over 128 require multiple bytes
// which also increases the MIB length
for (i = 0; i < orgmiblen; i++)
{
temp = Convert.ToInt16(mibvals);
if (temp > 127)
{
mib[cnt] = Convert.ToByte(128 + (temp / 128));
mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
cnt += 2;
miblen++;
} else
{
mib[cnt] = Convert.ToByte(temp);
cnt++;
}
}
snmplen = 29 + comlen + miblen - 1; //Length of entire SNMP
packet
// END SNIPPET

Cheers
 
D

dperkins

HI,

The code below only works for sub-oid values less than 2^14.

A high level description of the encoding is
1) a sub-oid value is split into chunks of 7 bits.
2) each chunk is stored in an octet (a 8-bit byte)
3) for each octet except the last, the high order
bit is set on (that is either OR 0x80 or add 128
to the octet)


Given the above, see comments below...

Sorry I'm only new to SNMP (not sure this'll help), but from this code
I was looking at
(http://www.java2s.com/Code/CSharp/Network/SimpleSNMP.htm), I think
what you're doing is right.

In the "get(string request, string host, string community, string
mibstring)" function of the SNMP class in the code from the link,
integer values over 128 need to be converted into multiple bytes:

// START SNIPPET
// Convert the string MIB into a byte array of integer values
^^^^^^^^^^ I assume what you really mean
is the OID value
Note, an OID value is an ordered sequence of unsigned integers
(called sub-identifiers in SNMP-speak).
// Unfortunately, values over 128 require multiple bytes
// which also increases the MIB length
^^^^^^^^^^ length of the
encoded value.
for (i = 0; i < orgmiblen; i++)
I assume that you have already dealt with the issue that the
encoding of the first two sub-identifiers is different than
the rest.
{
temp = Convert.ToInt16(mibvals);

^^^^^^^ just wish the "proper"
terminology was used!
^^^^^^^^^^^^^^^This is not correct! In SNMP
sub-identifiers have values
upto 2^32. So, ToInt16 should
be something like ToInt32
I'd replace the code below!
if (temp > 127)
{
mib[cnt] = Convert.ToByte(128 + (temp / 128));
mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
cnt += 2;
miblen++;
} else
{
mib[cnt] = Convert.ToByte(temp);
cnt++;
}
I'm not a C# programmer, but here goes with the replacement code...
if (temp < (1 << 7))
{
mib[cnt++] = Convert.ToByte(temp);
}
else if (temp < (1 << 14))
{
mib[cnt++] = Convert.ToByte(128 + (temp >> 7));
mib[cnt++] = Convert.ToByte(temp & 0x07f);
}
else if (temp < (1 << 21))
{
mib[cnt++] = Convert.ToByte(128 + (temp >> 14));
mib[cnt++] = Convert.ToByte(128 + (temp >> 7) & 0x07f);
mib[cnt++] = Convert.ToByte(temp & 0x07f);
}
else if (temp < (1 << 28))
{
mib[cnt++] = Convert.ToByte(128 + (temp >> 21));
mib[cnt++] = Convert.ToByte(128 + (temp >> 14) & 0x07f);
mib[cnt++] = Convert.ToByte(128 + (temp >> 7) & 0x07f);
mib[cnt++] = Convert.ToByte(temp & 0x07f);
}
else
{
mib[cnt++] = Convert.ToByte(128 + (temp >> 28));
mib[cnt++] = Convert.ToByte(128 + (temp >> 21) & 0x07f);
mib[cnt++] = Convert.ToByte(128 + (temp >> 14) & 0x07f);
mib[cnt++] = Convert.ToByte(128 + (temp >> 7) & 0x07f);
mib[cnt++] = Convert.ToByte(temp & 0x07f);
}
}
snmplen = 29 + comlen + miblen - 1; //Length of entire SNMP packet
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this looks pretty bogus!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top