在旧的原始套接字教程上使用 ntop() 而不是 ntoa()
我按照此链接上的教程创建原始套接字数据包嗅探器:
http:// /www.security-freak.net/raw-sockets/sniffer_eth_ip.c
该代码使用 ntoa() 函数获取源/目标 IP 地址的点表示法版本,但是据我了解,该函数已被弃用,因此这些行会导致问题。
ip_header = (struct iphdr*)(packet + sizeof(struct ethhdr));
/* print the Source and Destination IP address */
printf("Dest IP address: %d\n", inet_ntoa(ip_header->daddr));
printf("Source IP address: %d\n", inet_ntoa(ip_header->saddr));
printf("TTL = %d\n", ip_header->ttl);
在我的系统(Ubuntu 11.04)上,我只能在 arpa/inet.h 中找到 inet_ntoa() 函数,但教程甚至没有使用该标头。当我包含 arpa/inet.h 时,出现此编译错误:
sniffer.c:154:4: error: incompatible type for argument 1 of ‘inet_ntoa’
/usr/include/arpa/inet.h:54:14: note: expected ‘struct in_addr’ but argument is of type ‘__be32’
所以我知道我需要使用 struct in_addr 但我不熟悉这种类型“__be32”。有人可以帮忙吗?
编辑 - 实际上让这个可以进行一些相当复杂的转换,但是有更好的方法吗?
printf("Dest IP address: %s\n", inet_ntoa(*(struct in_addr*)&ip_header->daddr));
printf("Source IP address: %s\n", inet_ntoa(*(struct in_addr*)&ip_header->saddr));
I am following the tutorial at this link to create a raw socket packet sniffer:
http://www.security-freak.net/raw-sockets/sniffer_eth_ip.c
The code uses the ntoa() function to get the dot-notation version of the source/destination IP Addresses, but from what I understand that function is deprecated and so these lines are causing problems.
ip_header = (struct iphdr*)(packet + sizeof(struct ethhdr));
/* print the Source and Destination IP address */
printf("Dest IP address: %d\n", inet_ntoa(ip_header->daddr));
printf("Source IP address: %d\n", inet_ntoa(ip_header->saddr));
printf("TTL = %d\n", ip_header->ttl);
On my system (Ubuntu 11.04) I could only find the inet_ntoa() function in arpa/inet.h but the tutorial doesn't even use that header. When I included arpa/inet.h I get this compilation error:
sniffer.c:154:4: error: incompatible type for argument 1 of ‘inet_ntoa’
/usr/include/arpa/inet.h:54:14: note: expected ‘struct in_addr’ but argument is of type ‘__be32’
So I understand I need to use struct in_addr but I'm not familiar with this type '__be32'. Can anyone assist?
EDIT - Actually got this to work doing some fairly complicated casting, but is there a better way?
printf("Dest IP address: %s\n", inet_ntoa(*(struct in_addr*)&ip_header->daddr));
printf("Source IP address: %s\n", inet_ntoa(*(struct in_addr*)&ip_header->saddr));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
inet_ntoa 并未被正式弃用,但由于它使用静态缓冲区,因此风格有点旧。通常我建议使用 getnameinfo 将二进制地址转换为点字符,但在这种情况下 inet_ntop 效果会更好:
请注意,如果需要保留字符串供以后使用。
inet_ntoa
is not officially deprecated, but a bit old style since it uses a static buffer. Normally I recommend usinggetnameinfo
to convert at binary address to dotted char, but in this caseinet_ntop
would work better:Note that you should use separate buffers if you need to keep the strings for later.
它应该可以在大多数 Linux 发行版(x86 和 x64 版本)下运行,其中包括:
但不确定 100% 是否所有这些都是强制性的。
It should work under most Linux distributions (both x86 and x64 versions) with these includes:
Not sure 100% if all of them are mandatory, though.