473,698 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting SNMP OID into byte array

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
2 6978
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
13918
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do something like: char chars = "Hello!"; byte bytes = (byte) chars;
2
2673
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is there any way. Regards, Govind.
2
407
by: Tomas Deman | last post by:
Hi, I need a fast method for converting an int array to a byte array. At the moment, I'm using this: public static byte Int2ByteArray(int array) { byte lbytRetval = new byte; int lintIdxHi; int lintIdxLo; for (int i = 0; i < array.GetLength(0); i++)
3
2338
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes need to be converted to an enum. The next 4 bytes to a 32-bit int. And so on. I'm basically populating a struct with the data. How does one do this in C#? Thanks.
4
5393
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use "System.Text.Encoding.ASCII.GetString(bytearray)" of .Net library, we found that the char (delimiters) specified above are replaced with different char.
8
4562
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in XML, and embedded the array as some child element in an xml file, i.e.: <Mask>bHQAAH4AAABCTX4AAAAAAAAAPgAAACgAAAAQAAAAEAAAAAEAAQAAAAAAQAAAAAAAAAAAAA AA AAAAAAAAAAAAAAAA////AP//AAD//wAA//8AAP//AAD/7wAA//cAALtzAABVeQAAVUAAAFVA...
0
1667
by: Milan Goellner | last post by:
greetings, i am completely new to c# and i'm looking to write a littel util to extract snmp information from cisco hardware for starters. i have found some sample code helping me along my way but i'm now looking at a piece of code which extract the usefull stuff from the responds and cuts away the useless. this code i don't understand, could someone please tell me how it works, short and very simple :) ?
1
1837
by: Peter Krikelis | last post by:
Hi, I am trying to build a SNMP application using VB .NET. I am using the udpClient class to send and receive data. The SNMP header when viewed in hex looks like :: 7E 05 13 C1 ........ 7E My problem is the following. Udpclient's send method requires a byte array. So I tried
8
4194
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim bytParam() As Byte 'load the byte array here...
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.