이더넷 헤더와 함께 전체 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*
로 전송하십시오.
내 문제는 UDPPacket.verlen
뒤에 아무 것도 지정하지 않으면 약 5 바이트 건너 뛰고 거기서 시작된다는 것입니다. 예를 들어 오프셋 16과 17에서 바이트를 설정하는 대신 iplen
feild를 할당하면 23과 24와 같이 할당됩니다 (정확하게 내 전화에서 내 프로그램을 사용할 수 없기 때문에 정확하게 말할 수 없습니다).).
누락 된 이유가 있습니까? 아니면 잘못되었습니다.
그리고 Visual Studio 2008을 x64 Windows 7에서 사용하고 있지만 x86 용으로 컴파일하고 있습니다. – Hock