为什么我的IP地址被颠倒了?给定- 12.0.0.100,在C中接收为100.0.0.1。

时间:2022-09-16 13:26:20

I am testing RADIUS-DISCONNECT options.

我正在测试RADIUS-DISCONNECT选项。

Server sends disconnect request to disconnect (Cmd: cat packet.txt | radclient -c 10 -i 40 -x 12.0.0.1:3799 disconnect "Secret")

服务器发送断开连接请求断开(Cmd: cat数据包。txt | radclient - c10 - 40 -x 12.0.1:37 99断开“秘密”

My RAIDUS server is configured as 12.0.0.100. RADIUS client IP is configured as 12.0.0.1.

我的RAIDUS服务器配置为12.0.0.100。RADIUS客户机IP配置为12.0.0.1。

Sending packets from server captured the same using packet capture command. Packet capture says source IP as: Source: 12.0.0.100 (12.0.0.100).

从服务器发送数据包捕获相同的使用包捕获命令。包捕获说源IP为:源:12.0.0.100(12.0.0.100)。

But on reception, when printing my server IP, it is 100.0.0.12 because of which, I am unable to track or validate the server details. I am not aware if it is due to little endian, big endian issues.

但是在接收时,当打印我的服务器IP时,它是100.0.0.12,因为我无法跟踪或验证服务器的详细信息。我不知道这是不是因为小恩人,大的恩人问题。

GDB prints on server IP: {0xc, 0x0, 0x0, 0x64, 0x0 <repeats 12 times>} But it actually has to be {0x64, 0x0, 0x0, 0xc, 0x0 <repeats 12 times>}

GDB在服务器IP上打印:{0xc, 0x0, 0x0, 0x64, 0x0 },但它实际上必须是{0x64, 0x0, 0x0, 0xc, 0x0 }

If it is due to little endian or big endian issues, can anyone tell me how to resolve the issue.

如果是由于小的endian或大的endian问题,谁能告诉我如何解决这个问题。

Please someone kindly help.

请人请帮助。

1 个解决方案

#1


1  

The wire format for IP addresses in the vast majority of protocols is big endian, whereas your appear to want the IP address in little endian format.

在绝大多数协议中,IP地址的有线格式是大的endian,而你的IP地址则以小的endian格式显示。

Macros htonl (host to network long) and ntohl (network to host long) are provided to convert the endianness from big endian, to whatever the host uses (big, little or mixed).

宏的htonl(主机到网络长)和ntohl(网络主机长)被提供来转换从大的endian的发现,到任何主机使用的(大的,小的或混合的)。

uint8_t  *packet;
uint32_t ip_net;
uint32_t ip_host;

memcpy(&ip_net, packet, sizeof(ip_net));  /* Assuming packet is a buffer of at least 4 bytes */
ip_host = ntohl(ip_net);

You may see instances in other code of pointers into packet buffers being cast to a uint32_t, and that being passed into the byte order conversion functions. This may work on some architectures (x86), but can result in unaligned memory accesses on others (Sparc).

您可能会在其他代码的指针中看到被转换为uint32_t的包缓冲区的实例,并将其传递到字节顺序转换函数中。这可能在某些架构(x86)上起作用,但可能会导致未对齐的内存访问(Sparc)。

It's generally best practice to memcpy the from the buffer being processed to an intermediary variable, as memcpy will handle the alignment issues, and stack variables are guaranteed to be appropriately aligned.

通常最好的做法是将缓冲区被处理的缓冲区作为中介变量,因为memcpy将处理对齐问题,而栈变量则保证适当地对齐。

There's a quick summary of these issues and the approaches available here

这里有一个关于这些问题的快速摘要和可用的方法。

#1


1  

The wire format for IP addresses in the vast majority of protocols is big endian, whereas your appear to want the IP address in little endian format.

在绝大多数协议中,IP地址的有线格式是大的endian,而你的IP地址则以小的endian格式显示。

Macros htonl (host to network long) and ntohl (network to host long) are provided to convert the endianness from big endian, to whatever the host uses (big, little or mixed).

宏的htonl(主机到网络长)和ntohl(网络主机长)被提供来转换从大的endian的发现,到任何主机使用的(大的,小的或混合的)。

uint8_t  *packet;
uint32_t ip_net;
uint32_t ip_host;

memcpy(&ip_net, packet, sizeof(ip_net));  /* Assuming packet is a buffer of at least 4 bytes */
ip_host = ntohl(ip_net);

You may see instances in other code of pointers into packet buffers being cast to a uint32_t, and that being passed into the byte order conversion functions. This may work on some architectures (x86), but can result in unaligned memory accesses on others (Sparc).

您可能会在其他代码的指针中看到被转换为uint32_t的包缓冲区的实例,并将其传递到字节顺序转换函数中。这可能在某些架构(x86)上起作用,但可能会导致未对齐的内存访问(Sparc)。

It's generally best practice to memcpy the from the buffer being processed to an intermediary variable, as memcpy will handle the alignment issues, and stack variables are guaranteed to be appropriately aligned.

通常最好的做法是将缓冲区被处理的缓冲区作为中介变量,因为memcpy将处理对齐问题,而栈变量则保证适当地对齐。

There's a quick summary of these issues and the approaches available here

这里有一个关于这些问题的快速摘要和可用的方法。