位字段和字节序问题

8
我已经定义了以下结构体来表示IPv4头部(直到选项字段):

我已经定义了以下结构体来表示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

有人能帮忙吗?

4个回答

8

最便携的方法是逐个字段地进行操作,对于长度超过一个字节的类型,可以使用 memcpy()。对于长度为一个字节的字段,您不需要担心字节序问题:

uint16_t temp_u16;
uint32_t temp_u32;
struct IPv4Header header;

header.Version = cIPHeaderSample[0] >> 4;

header.InternetHeaderLength = cIPHeaderSample[0] & 0x0f;

header.TypeOfServer = cIPHeaderSample[1];

memcpy(&temp_u16, &cIPHeaderSample[2], 2);
header.TotalLength = ntohs(temp_u16);

memcpy(&temp_u16, &cIPHeaderSample[4], 2);
header.Identification = ntohs(temp_u16);

header.Flags = cIPHeaderSample[6] >> 5;

memcpy(&temp_u16, &cIPHeaderSample[6], 2);
header.FragmentOffset = ntohs(temp_u16) & 0x1fff;

header.TTL = cIPHeaderSample[8];

header.Protocol = cIPHeaderSample[9];

memcpy(&temp_u16, &cIPHeaderSample[10], 2);
header.HeaderChecksum = ntohs(temp_u16);

memcpy(&temp_u32, &cIPHeaderSample[12], 4);
header.SourceAddress = ntohl(temp_u32);

memcpy(&temp_u32, &cIPHeaderSample[16], 4);
header.DestinationAddress = ntohl(temp_u32);

很棒 :) 我希望有一个更通用的解决方案,但如果这个可以工作,我会很高兴。 - StackedCrooked
@StackedCrooked:你会注意到,处理整个1、2或4字节字段的方式总是相同的模式——只有那些位数奇怪的字段需要特殊处理。你可以(应该)为这些常见情况编写内联辅助函数。 - caf

4
ntohlntohs 不适用于 1 字节字段。它们分别用于 32 位和 16 位字段。如果需要,您可能需要从一个强制转换或 memcpy 开始,然后交换 16 位和 32 位字段的字节。如果您发现版本没有经过任何字节交换就无法使用该方法,则存在位字段问题。
在 C 中,位字段是一大混乱。大多数人(包括我)会建议您避免使用它们。

3
你想查看来自FreeBSD的ip.h源代码。你的系统上应该有一个预定义的iphdr结构体,使用它即可。如果没有必要,不要重复造轮子。

让这个工作最简单的方法是将wireshark的字节数组指针转换为iphdr指针。这样可以使用正确的头文件结构。

struct iphdr* hrd;
hdr = (iphdr*) cIPHeaderSample;
unsigned int version = hdr->version;

此外,htons接受16位并更改字节顺序,将其用于32位变量只会使事情混乱。对于32位变量,您需要使用htonl。还要注意,对于一个字节来说,并不存在字节序,需要多个字节才会有不同的字节序。

2
":4" 表示一个 4 位长的位域 - "uint32_t" 只是位域的基本类型。 - caf
不要重复造轮子。看看tcpdump源代码并学习如何使用libpcap——你可能可以自动捕获数据,尽管你也可以使用tcpdump和重定向IO与你的进程通信。 - user257111

1

更新:

我建议您使用memcpy来避免位域和struct对齐的问题,因为这可能会变得混乱。下面的解决方案适用于简单的示例,并且可以轻松扩展:

struct IPv4Header
{
    uint32_t Source;
};

int main(int argc, char **argv) {
    const uint8_t cIPHeaderSample[] = {
        0x45, 0x00, 0x05, 0x17
    };

    IPv4Header header;
    memcpy(&header.Source, cIPHeaderSample, sizeof(uint8_t) * 4);
    header.Source= ntohl(header.Source);
    cout << hex << header.Source<< endl;
}

Output: 
45000517

header.Version 应该是 4 ,而不是 4 字节 - 输出应该只是 4(代表“IPv4”)。 - caf
谢谢,我在你评论的时候更新了,我最初看到uint32_t类型并认为这是OP的错误。现在示例应该是正确的了。 - Justin Ardini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接