General information
Title:
Structure overlay question
Paul,
I am relatively new to the Windows NT world and I am sure these is a simple explaination for this, but I haven't found it yet. I have a buffer and want to parse it by using a structure; for example:
#include
#include
typedef struct {
unsigned short port; // 2 bytes, right?
struct in_addr ip_addr; // 4 bytes
} address;
void main () {
char buff[20];
address *addrPtr;
unsigned short thePort;
unsigned long theAddr;
memset (buff, '\0', 20);
buff[0] = (unsigned char)0x9A;
buff[1] = (unsigned char)0x10;
buff[2] = (unsigned char)0xCF; // 2nd octet
buff[3] = (unsigned char)0xAD; // 1st octet
buff[4] = (unsigned char)0xD5; // 4th
buff[5] = (unsigned char)0x74; // 3rd
buff[6] = (unsigned char)0x0B; // shouldn't see
buff[7] = (unsigned char)0x16; // shouldn't see
addrPtr = (address *) buff; // overlay the structure
thePort = addrPtr->port;
theAddr = addrPtr->ip_addr.s_addr;
printf ("Port = %hd \n", thePort);
printf ("Address = %d.%d.%d.%d \n", addrPtr->ip_addr.s_net, addrPtr->ip_addr.s_host, addrPtr->ip_addr.s_lh, addrPtr->ip_addr.s_impno);
}
When I run this, I am able to get the Port number OK, but the ip_addr portion is always 2 bytes off. Using the debugger shows the following in memory:
9A 10 CF AD D5 74 0B 16
^
The addrPtr->ip_addr.s_addr points here to 'D5'; why doesn't it point to 'CF'???
I am using MS Visual C++ 5.0 on NT 4.0 (Service Pack 3)
Thanks for the help.
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only