Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Reading packets

 
Post new topic   Reply to topic Printable version
 View previous topic  MERVbot Plugins Post :: Post Rabbit Bot  View next topic  
Author Message
Pests
Novice


Joined: Feb 24 2003
Posts: 84
Offline

PostPosted: Wed Jun 30, 2004 12:34 am    Post subject: Reading packets Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Wed Jun 30, 2004 2:13 am    Post subject: Reply to topic Reply with quote

Are you talking about handling incoming packets in code? If so, then you should define a packed structure, and pick out the fields (members) from it...

Code: Show/Hide
pragma pack(1)

struct SomePacket
   {
   unsigned char type;
   unsigned short pid;
   unsigned long time;
   // blah blah
   };

#pragma pack()

// some packet handling function

void func (unsigned char *buffer, int len)
{
SomePacket *pkt = (SomePacket *)buffer;

// test acceptible length(s)

if (len ==    // or "len >=" for variable size packets

// pick out what you want

pkt->type;

pkt->pid;

pkt->time;

// ...
}


When I use the term packet here, I really mean message, but in UDP it's one in the same.
_________________
4,691 irradiated haggis!
Back to top
View users profile Send private message Add User to Ignore List
Pests
Novice


Joined: Feb 24 2003
Posts: 84
Offline

PostPosted: Wed Jun 30, 2004 2:30 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
50% Packetloss
Server Help Squatter


Age:40
Gender:Gender:Male
Joined: Sep 09 2003
Posts: 561
Location: Santa Clarita, California
Offline

PostPosted: Wed Jun 30, 2004 3:06 am    Post subject: Reply to topic Reply with quote

http://webdocs.caspur.it/ibm/web/cset++-3.6.6/language/ref/rnpgpack.htm
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address
CypherJF
I gargle nitroglycerin


Gender:Gender:Male
Joined: Aug 14 2003
Posts: 2582
Location: USA
Offline

PostPosted: Wed Jun 30, 2004 4:21 am    Post subject: Reply to topic Reply with quote

Wow I'm confused icon_smile.gif well only by slightly lol.. icon_smile.gif
_________________
Performance is often the art of cheating carefully. - James Gosling
Back to top
View users profile Send private message Add User to Ignore List
Bak
?ls -s
0 in


Age:26
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Wed Jun 30, 2004 6:15 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Wed Jun 30, 2004 9:02 am    Post subject: Reply to topic Reply with quote

In order to do:
SomePacket *pkt = (SomePacket *)buffer;

Wouldn't you have to typedef struct SomePacket?
Back to top
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
50% Packetloss
Server Help Squatter


Age:40
Gender:Gender:Male
Joined: Sep 09 2003
Posts: 561
Location: Santa Clarita, California
Offline

PostPosted: Wed Jun 30, 2004 1:07 pm    Post subject: Reply to topic Reply with quote

nope
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Wed Jun 30, 2004 1:08 pm    Post subject: Reply to topic Reply with quote

Not in C++ icon_razz.gif
_________________
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
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Wed Jun 30, 2004 5:30 pm    Post subject: Reply to topic Reply with quote

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.

Code: Show/Hide
i = pkt.byte;
j = pkt.dword;


With packing off, it would look something like:

Code: Show/Hide
mov eax, BYTE PTR [00440000]
mov ebx, DWORD PTR [00440004]


With packing on, it would look something like:

Code: Show/Hide
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
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Wed Jun 30, 2004 7:28 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Wed Jun 30, 2004 8:26 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Bot Questions All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 214 page(s) served in previous 5 minutes.

phpBB Created this page in 0.478151 seconds : 36 queries executed (94.1%): GZIP compression disabled