• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

packing a SNMP PDU

I am trying to write a small SNMP manager to query agents for data. I have translated a complete PDU into its binary representation and it compares fine to a sniffed packet. My question is how do I create a machine independent (if push comes to shove, Linux specific)buffer of binary data in C++. I do not know the internal representstion of struct so would it work? If so how?

While on the point, could someone give me a good explanation of how to encode numbers >127 in an oid.

Thanx
Adam
0
nitelord
Asked:
nitelord
  • 4
  • 4
1 Solution
 
nietodCommented:
>> My question is how do I create a
>> machine independent (if push comes to
>> shove, Linux specific)buffer of binary data in C++
You can't store the data in a struct, because some compilers will place padding between data members of the struct and some won't (Or some will place different amounts of padding.)  

The only portable format will be a byte array.  i.e. a character array (or unsigned character array.  You can copy the data to the array at specific locations, like to store a 2 byte integer followed by a 4 byte intger you might do

int short X1 = 1; // 2 byte
int X2 =2   // 4 byte.
char Data[6]; // Data buffer.

memcpy(Data+0,&X1,2);
memcpy(Data+2,&X2,4);

Now this handles the "position problem", but does not handle byte ordering.  Some machines place the least significant byte (LSB) of a multi-byte number at a lower address than the most significant byte (MSB), and some place it at a higher address.  Because of this there is no way to make multi-byte numbers completely portable.  You will have to choose which form to use and then on machines that don't use that form you will have to correct the value to the form for that machine.

Let me know if you have any questions.
0
 
nietodCommented:
I guess I should show you how to convert between little-endiand and big endian (the issue I just mentioned about the byte ordering.)

Little endian is the system used by Intel x86 computers (among others) in it the end (high address) of the number has less significance (its little) compared to the start (low address) of the number.  So A number like 4660 decimal, which is 0x1234 in hex, would be stored in two bytes in memory with the 0x34 in the low byte and the 0x12 in the high byte.

On the big endian system, used by Motorola 68000 computers and others, the bytes are exactly reversed, so the 0x12 woudl be in the low byte and the 0x34 in the high byte.  

You can convert between the two by simply swapping bytes.  For this you can treat an number as a character array to get access to it on the byte level.  For example, you can convert a 4 byte integer as follows.

int X = 123456;  // Number to be converted.
char *CnvPtr = (char *) &X; // Used to treat number as a byte array.
char SavByt; // Saves bytes during switching.

SavByt = CnvPtr[0]; // save low byte.
CnvPtr[0] = CnvPtr[3]; // Put highest byte in lowest.
CnvPtr[3] = SavByt; // Put lowest byte in highest.

SavByt = CnvPtr[1]; // save 2nd lowes byte.
CnvPtr[1] = CnvPtr[2]; // Put 2nd highest byte in 2nd lowest.
CnvPtr[2] = SavByt; // Put 2nd lowest byte in 2nd highest.

Note that this conversion works in both directions.  i.e each time you do it it it switches from little endian to big endian.
0
 
seasky_tigerCommented:
While on the point, could someone give me a good explanation of how to encode numbers >127 in an oid.

oid name is just a unsigned char arrary,
 such as 1.3.6.1.2.1.1 can be stored as {1,3,6,1,2,1}, the max oid number is 255. So it is easy to encode numbers > 127.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nitelordAuthor Commented:
Sorry, the question maybe wrong. Here goes. 1.3.6.1.4.1.2021.2 ..... encodes, using BER, to 2b.03.06.01.04.01 in hex.

2021 has to be encoded using two octects, how does this work?
0
 
nitelordAuthor Commented:
Answered how to store bytes of data and cleared up the endian issue. Thanx
0
 
nitelordAuthor Commented:
Seems I owe nietod 450 pts, how do I rectify this.
0
 
nietodCommented:
I don't understand.  Why do you owe me 450 Pts?
0
 
nietodCommented:
I got it.  It was a 500 pt question, now it is only 50, difference is 450.

Once an answer has been accepted, the points are "awarded" to the expert (multiplied by the grades's value, 4 for A, 3 for B etc.)  Then the question becomes a PAQ (previously asked question).  The question may still be looked at, but clients/experts that did not participate in the question must pay for the PAQ.  They must pay 1/10th of the orignal value.  This question was 500 pts, so as a PAQ it is worth 50.
0
 
nitelordAuthor Commented:
and there was light :-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now