使用原始数据包数据和 inet_ntoa() 进行转换
尝试为数据包嗅探器编写处理程序。我在转换和 inet_ntoa()
方面遇到问题。代码如下:
uint32_t *iphdr_srcaddr = malloc(sizeof(uint32_t));
if (*packet_ethertype == ETHERTYPE_IP) { /* IPv4 */
// copy packet data to vars
memcpy(iphdr_srcaddr, packet+26, 4);
// change to host-byte-order
*iphdr_srcaddr = ntohl(*iphdr_srcaddr);
struct in_addr *test;
test = (struct in_addr*) iphdr_srcaddr;
printf("uint32_t: %u\n", *iphdr_srcaddr); // Gives the correct long integer for the address
printf("struct in_addr: %u\n", test->s_addr); // Gives the correct long integer through the cast
char *test2;
test2 = inet_ntoa(*test);
}
现在,如果我尝试 printf("%s\n", test)
我会得到 SEGV。我确信我混淆了指针、值并进行了某种愚蠢的转换。在下面的运行过程中收到错误:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff787ec61 in __strlen_sse2 () from /lib/libc.so.6
还有编译警告,我确信这为我指明了正确的方向,但我不确定它的含义以及如何修复它:
mypcap.c: In function ‘handle_sniffed’:
mypcap.c:61:15: warning: assignment makes pointer from integer without a cast [enabled by default]
这指的是行 test2 = inet_ntoa(*测试);
Trying to write a handler for a packet sniffer. I'm having issues with casting and inet_ntoa()
. Code is as follows:
uint32_t *iphdr_srcaddr = malloc(sizeof(uint32_t));
if (*packet_ethertype == ETHERTYPE_IP) { /* IPv4 */
// copy packet data to vars
memcpy(iphdr_srcaddr, packet+26, 4);
// change to host-byte-order
*iphdr_srcaddr = ntohl(*iphdr_srcaddr);
struct in_addr *test;
test = (struct in_addr*) iphdr_srcaddr;
printf("uint32_t: %u\n", *iphdr_srcaddr); // Gives the correct long integer for the address
printf("struct in_addr: %u\n", test->s_addr); // Gives the correct long integer through the cast
char *test2;
test2 = inet_ntoa(*test);
}
Now if I try to printf("%s\n", test)
I get SEGV. I'm sure I'm mixing up pointers, values and doing some sort of stupid casting. Error received during run below:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff787ec61 in __strlen_sse2 () from /lib/libc.so.6
Compilation warning as well, I'm sure this is pointing me in the correct direction, but I'm not sure what to it means and how I can fix it:
mypcap.c: In function ‘handle_sniffed’:
mypcap.c:61:15: warning: assignment makes pointer from integer without a cast [enabled by default]
This refers to the line test2 = inet_ntoa(*test);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该警告可能表明您在
inet_ntoa()
范围内没有正确的原型(因为您没有包含正确的标头)。这意味着编译器假定它的返回类型为 int。当您应该传递
test2
时,您还将test
传递给printf()
。另外:
malloc()
来分配单个uint32_t
;ntohl()
,因为inet_ntoa()
期望其输入为网络字节顺序;并且inet_ntoa()
已过时 - 应在新代码中使用inet_ntop()
。尝试:
The warning probably indicates that you don't have a correct prototype in scope for
inet_ntoa()
(because you haven't included the right header). This means that the compiler assumes it has a return type ofint
.You're also passing
test
toprintf()
when you should be passingtest2
.In addition:
malloc()
to allocate a singleuint32_t
;ntohl()
becauseinet_ntoa()
expects its input in network byte order; andinet_ntoa()
is out of date -inet_ntop()
should be used in new code.Try: