我有一個結構,我已經寫了,它應該代表整個UDP數據包,以及以太網頭和所有。在這裏它是:結構中的字段跳過字節
#pragma pack(1)
struct UDPPacket {
// an array to hold the destination mac address of the packet
unsigned char dstmac[6];
// an array to hold the source mac address of the packet
unsigned char srcmac[6];
// two bytes to hold the packet type, this is almost always IP (08 00)
WORD ethtype;
// each of the subfields of this take up 4 bits. ver, the first half,
// is the ip version, which should usually be 4 for ipv4, and the second
// is the length of the header divided by 4, which is almost always 5
struct {
unsigned ver : 4;
unsigned len : 4;
} verlen;
// this isn't used in ipv4 and is 0
BYTE tos;
// the total length of the header + data
WORD iplen;
// the id of this datagram for reassembling fragmented packets
WORD id;
// the first subfield occupies 3 bits and is the flags of this packet, which is usually 0
// the second subfield is the fragmentation offset for large datagrams that have been split up for sending, usually 0
struct {
unsigned flags : 3;
unsigned fragmentation : 13;
} flagfrag;
// time to live; usually 35 or 128
BYTE ttl;
// the protocol with which this packet is being transported
// 1 = ICMP, 2 = IGMP, 6 = TCP, 17 = UDP
BYTE protocol;
// the ip checksum of this packet
WORD ipchecksum;
// the source ip of this packet
DWORD src;
// the destination ip of this packet
DWORD dest;
// the port from which this packet is coming
WORD srcport;
// the port this packet is headed to
WORD destport;
// the length of the udp header + data, not including the ip header
// so it's usually basically iplen - 20
WORD udplen;
// the udp checksum of this packet
WORD udpchecksum;
// a char pointer to the data of the packet
unsigned char data[10000];
};
#pragma pack()
當然,這是一個真正的UDP包的表示,該字節必須在相同的偏移,因爲它們將在一個信息包,和指針於這種類型的結構的將是投到unsigned char*
s發送。
我的問題是,當我嘗試在UDPPacket.verlen
之後分配任何內容時,它會跳過大約5個字節並從那裏開始。例如,當我指定iplen
字段時,不是將字節設置爲偏移量16和17,而是將它們指定爲類似於23和24的字符(我不能完全說明,因爲我的手機中沒有可用的程序)。
有沒有一個明顯的原因,我錯過了,或者我只是做錯了什麼?
順便說一下,我在x64 Windows 7上使用Visual Studio 2008,但是爲x86編譯。 – Hock 2010-08-18 14:39:30