Author |
Message |
Pests Novice
Joined: Feb 24 2003 Posts: 84 Offline
|
Posted: Wed Jun 30, 2004 12:34 am Post subject: Reading packets |
 |
|
|
|
Whats the best way to read packets in C/C++? I was doing it catids way but then I saw a post by ekted that said it was wrong. What would be the correct way? |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
|
Back to top |
|
 |
Pests Novice
Joined: Feb 24 2003 Posts: 84 Offline
|
Posted: Wed Jun 30, 2004 2:30 am Post subject: |
 |
|
|
|
Thank you very much. I figured this would be the best way to do it. Can someone explain to me the #pragma pack() however? |
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Wed Jun 30, 2004 4:21 am Post subject: |
 |
|
|
|
Wow I'm confused well only by slightly lol..  _________________ Performance is often the art of cheating carefully. - James Gosling |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:26 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Wed Jun 30, 2004 6:15 am Post subject: |
 |
|
|
|
variables get aligned by default in memory so if you have a 2 byte variable and then a 4 byte variable they end up taking 8 bytes because you get 2 bytes for first variable then 2 bytes filler to preserve alignment for next variable which is 4 bytes. What pragma pack does is you can specify the alginment so pragma pack 1 means alaign it to 1 byte... dont waste any bytes...
at least that's the way I interprete it... correct me if i'm wrong. |
|
Back to top |
|
 |
Smong Server Help Squatter

Joined: 1043048991 Posts: 0x91E Offline
|
Posted: Wed Jun 30, 2004 9:02 am Post subject: |
 |
|
|
|
In order to do:
SomePacket *pkt = (SomePacket *)buffer;
Wouldn't you have to typedef struct SomePacket? |
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
Posted: Wed Jun 30, 2004 1:07 pm Post subject: |
 |
|
|
|
nope |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Wed Jun 30, 2004 1:08 pm Post subject: |
 |
|
|
|
Not in C++  _________________ This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him. |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Wed Jun 30, 2004 5:30 pm Post subject: |
 |
|
|
|
You are correct Bak, except that it's the compiler that makes the alignment choices and sets the default. When optimizing for speed, it is preferred to keep all data members of 32 bits or less inside 32-bit boundaries, otherwise you cause extra memory accesses at the micro-code level.
BYTE DWORD would usually be implemented BYTE space space space DWORD so that DWORD starts on a new 32-bit boundary. If you use pack(1), you get BYTE DWORD with no space. What is the difference in the compiled code? Not much.
i = pkt.byte;
j = pkt.dword; |
With packing off, it would look something like:
mov eax, BYTE PTR [00440000]
mov ebx, DWORD PTR [00440004] |
With packing on, it would look something like:
mov eax, BYTE PTR [00440000]
mov ebx, DWORD PTR [00440001] |
That access to address 0x00440001 requires a read of 2 rows of memory (00440000, and 00440004) to get the bytes necessary to make up the resulting 32-bit value. |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Wed Jun 30, 2004 7:28 pm Post subject: |
 |
|
|
|
Woah I never actually understood why structs were packed 4-bytes.
So If you have a struct completely composed of bytes, it's perfectly speedy to pack it 1? |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Wed Jun 30, 2004 8:26 pm Post subject: |
 |
|
|
|
That will happen by default. A structure with 4 byte members will be 4 bytes in size. But in general you shouldn't go out of your way to pack stuff unless you do it to match some structure or format (eg network packet, or file format). |
|
Back to top |
|
 |
|