By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

Converting SNMP OID into byte array

P: n/a
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.

Dec 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/Ne...impleSNMP.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[i]);
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

Jason James wrote:
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.
Jan 15 '07 #2

P: n/a
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...

On Mon, 14 Jan 2007, Eamon wrote:
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/Ne...impleSNMP.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[i]);
^^^^^^^ 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!
// END SNIPPET

Cheers

Jason James wrote:
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.

Jan 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.