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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>> 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.


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.
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 can be stored as {1,3,6,1,2,1}, the max oid number is 255. So it is easy to encode numbers > 127.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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

2021 has to be encoded using two octects, how does this work?
nitelordAuthor Commented:
Answered how to store bytes of data and cleared up the endian issue. Thanx
nitelordAuthor Commented:
Seems I owe nietod 450 pts, how do I rectify this.
I don't understand.  Why do you owe me 450 Pts?
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.
nitelordAuthor Commented:
and there was light :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.