代码之家  ›  专栏  ›  技术社区  ›  StackedCrooked

位域和端点

c c++
  •  7
  • StackedCrooked  · 技术社区  · 14 年前

    我定义了以下结构来表示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;
    };
    

    // 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对象?

    这不起作用,因为不兼容的endianness:

    IPv4Header header = *((IPv4Header*)cIPHeaderSample);
    

    我知道像ntohs和ntohl这样的函数,但它不知道如何正确使用它们:

    u_int8 version = ntohs(cIPHeaderSample[0]);
    printf("version: %x \n", version);
    
    // Output is:
    // version: 0
    

    有人能帮忙吗?

    4 回复  |  直到 14 年前
        1
  •  7
  •   caf    14 年前

    最便携的方法是使用 memcpy() 对于超过一个字节的类型。您不必担心字节长度字段的endianness:

    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);
    
        2
  •  4
  •   nmichaels    14 年前

    ntohl ntohs 不要对1字节字段进行操作。它们分别用于32位和16位字段。您可能希望从cast或memcpy开始,然后根据需要对16位和32位字段进行字节交换。如果您发现没有任何字节交换的版本无法通过这种方法,那么就有位字段问题。

    位字段在C中是一个大烂摊子。大多数人(包括我)都会建议你避开它们。

        3
  •  3
  •   Paul Rubel    14 年前

    你想看一看 ip.h ,那个是FreeBSD的。系统上应该有一个预先定义的iphdr结构,使用它。如果不需要的话,就不要重新发明轮子。

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

    而且,htons接收16位并更改字节顺序,在32位变量上调用它只会把事情搞得一团糟。对于32位变量需要htonl。还要注意的是,对于一个字节来说,没有endianess,需要多个字节才能有不同的endianess。

        4
  •  1
  •   Justin Ardini    14 年前

    我建议你用 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