[英]inet_ntoa problem
I am declaring following variables 我在声明以下变量
unsigned long dstAddr;
unsigned long gateWay;
unsigned long mask;
These variables contains ipaddresses in network byte order. 这些变量包含网络字节顺序的ipaddress。 So when I am trying to print the dot notation using inet_ntoa function for mask variable sometimes it is printing strange values.
因此,当我尝试使用inet_ntoa函数的mask变量打印点表示法时,有时会打印出奇怪的值。 The below code is in a while loop .. which loops for n times.
下面的代码在while循环中..循环了n次。
printf("%s\t%s\t%s\t",inet_ntoa(dstAddr),inet_ntoa(gateWay),inet_ntoa(mask));
192.168.122.0 0.0.0.0 0.255.255.255
but it should be 但应该是
192.168.122.0 0.0.0.0 255.255.255.0
I printed the HEX values of the variables and it shows .. 我打印了变量的十六进制值,它显示..
007aa8c0 00000000 ffffff00
So is this because of inet_ntoa ?? 那是因为inet_ntoa吗?
Actually I am trying to get the values of the declared variables from 254 routing table in kernel via NETLINKS. 实际上,我正在尝试通过NETLINKS从内核中的254路由表中获取声明的变量的值。 I guess I should still use inet_ntoa function to convert the value into dot notation .. ??
我想我仍然应该使用inet_ntoa函数将值转换为点表示法。
唯一有意义的是,您对所有以网络字节顺序排列的地址的假设都是错误的。
Well, given that it works for your non-mask values (including the first which also has the high bit set), I'd be looking at what mask
actually contains. 好吧,鉴于它适用于您的非掩码值(包括第一个也设置了高位的值),我将查看
mask
实际包含的内容。
What is it when you print it out as a normal unsigned long? 以正常的无符号长打印出来是什么? My bet is that
mask
is actually not the correct value: 我敢打赌,
mask
实际上不是正确的值:
printf ("%08x\t%08x\t%08x\n", dstAddr, gateWay, mask);
(assuming you have four-byte longs). (假设您有四字节长)。
For example, this little program (compiled under Cygwin): 例如,这个小程序(在Cygwin下编译):
#include <stdio.h>
int main (void) {
unsigned long dstAddr, gateWay, mask;
dstAddr = 0x007aa8c0;
gateWay = 0x00000000;
mask = 0x00ffffff;
printf("%-15s %-15s %-15s\n",
inet_ntoa (dstAddr),
inet_ntoa (gateWay),
inet_ntoa (mask));
printf("%-15s ", inet_ntoa (dstAddr));
printf("%-15s ", inet_ntoa (gateWay));
printf("%-15s\n", inet_ntoa (mask));
printf ("%08x%8s%08x%8s%08x\n",
dstAddr, "",
gateWay, "",
mask);
return 0;
}
outputs: 输出:
192.168.122.0 192.168.122.0 192.168.122.0
192.168.122.0 0.0.0.0 255.255.255.0
007aa8c0 00000000 00ffffff
Note that I had to separate my calls to inet_ntoa
as it appears to use a static buffer. 请注意,由于它似乎使用静态缓冲区,因此必须分开对
inet_ntoa
调用。 When I was doing it all within a single printf
, it overwrote the contents of that buffer before any of them were printed, hence I only got the last one processed. 当我在单个
printf
所有操作时,它在打印任何缓冲区之前都会覆盖该缓冲区的内容,因此,我只处理了最后一个缓冲区。 I don't think that's happening in your case since you're getting different values. 我不认为这种情况正在发生,因为您获得了不同的价值观。
I also have the same problem with you. 我也跟你有同样的问题。 Use inet_ntop() and inet_pton() if you need it other way around.
如果您需要其他方式,请使用inet_ntop()和inet_pton()。 Do not use inet_ntoa(), inet_aton() and similar as they are deprecated and don't support ipv6.
不要使用inet_ntoa(),inet_aton()等类似的东西,因为它们已被弃用并且不支持ipv6。 see link: convert Ip address int to string
请参阅链接: 将IP地址int转换为字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.