Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 位域与端点_C++_C - Fatal编程技术网

C++ 位域与端点

C++ 位域与端点,c++,c,C++,C,我已定义以下结构来表示IPv4标头(直到选项字段): 我现在还用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, 0x

我已定义以下结构来表示IPv4标头(直到选项字段):

我现在还用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对象

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

IPv4Header header = *((IPv4Header*)cIPHeaderSample);
我知道像ntohs和ntohl这样的函数,但它不知道如何正确使用它们:

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

// Output is:
// version: 0

有人能帮忙吗?

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


在C语言中,位字段非常混乱。大多数人(包括我)都会建议您避免使用它们。

您想看一看源代码,它来自FreeBSD。您的系统上应该有一个预先定义的iphdr结构,请使用它。如果没有必要,不要重新发明轮子

最简单的方法是从wireshark获取指向字节数组的指针,并将其转换为指向iphdr的指针。这样就可以使用正确的头结构

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

此外,htons接受16位并更改字节顺序,在32位变量上调用它只会把事情弄得一团糟。您需要对32位变量使用htonl。还要注意的是,对于一个字节,没有endianess这样的东西,需要多个字节才能有不同的endianess

最可移植的方法是一次一个字段,对于长度超过一个字节的类型使用
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);

更新:

我建议您使用
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
struct ipv4头文件
{
uint32_t源;
};
int main(int argc,字符**argv){
const uint8_t CiphederSample[]={
0x45、0x00、0x05、0x17
};
ipv4报头;
memcpy(&header.Source,ciphedersample,sizeof(uint8\u t)*4);
header.Source=ntohl(header.Source);

难道
:4
表示一个4位长的位字段-
uint32\u t
只是位字段的基本类型。+1表示不要重新发明轮子。查看tcpdump源代码并使用libpcap读取-您可以自己自动捕获,尽管您也可以使用tcpdump和重定向IO与您的用户进行通信进程。令人印象深刻:)我希望有一个更通用的解决方案,但如果这能奏效,我会很高兴。@StackedCrooked:你会注意到,对整个1、2或4字节字段的处理总是相同的模式-只是字段的位数奇怪,需要特殊处理。你可以(应该)为这些常见情况编写内联帮助器函数。
标题。版本
应为4位,而不是4字节-输出应仅为
4
(对于同名的“IPv4”)。谢谢,我在您发表评论时进行了更新,我最初看到了
uint32\u t
类型,并认为这是OP的错误。该示例现在应该是正确的。
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