我已经定义了以下结构体来表示IPv4头部(直到选项字段):
我的问题是:如何使用数组数据创建IPv4Header对象?
由于不兼容的字节序,这种方法行不通:
我已经定义了以下结构体来表示IPv4头部(直到选项字段):
struct IPv4Header
{
// First row in diagram
u_int32 Version:4;
u_int32 InternetHeaderLength:4; // Header length is expressed in units of 32 bits.
u_int32 TypeOfService:8;
u_int32 TotalLength:16;
// Second row in diagram
u_int32 Identification:16;
u_int32 Flags:3;
u_int32 FragmentOffset:13;
// Third row in diagram
u_int32 TTL:8;
u_int32 Protocol:8;
u_int32 HeaderChecksum:16;
// Fourth row in diagram
u_int32 SourceAddress:32;
// Fifth row in diagram
u_int32 DestinationAddress:32;
};
我现在使用Wireshark捕获了一个IP帧。作为数组文字,它看起来像这样:
// Captured with Wireshark
const u_int8 cIPHeaderSample[] = {
0x45, 0x00, 0x05, 0x17,
0xA7, 0xE0, 0x40, 0x00,
0x2E, 0x06, 0x1B, 0xEA,
0x51, 0x58, 0x25, 0x02,
0x0A, 0x04, 0x03, 0xB9
};
我的问题是:如何使用数组数据创建IPv4Header对象?
由于不兼容的字节序,这种方法行不通:
IPv4Header header = *((IPv4Header*)cIPHeaderSample);
我知道ntohs和ntohl这些函数,但是不知道如何正确使用它们:
u_int8 version = ntohs(cIPHeaderSample[0]);
printf("version: %x \n", version);
// Output is:
// version: 0
有人能帮忙吗?