axo*_*xon 3 c pointers casting pcap
试图为数据包嗅探器编写处理程序.我遇到了铸造问题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);
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我试图printf("%s\n", test)获得SEGV.我确定我正在混淆指针,价值观和做某种愚蠢的演员.运行期间收到错误:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff787ec61 in __strlen_sse2 () from /lib/libc.so.6
Run Code Online (Sandbox Code Playgroud)
编译警告,我确信这指向了正确的方向,但我不确定它意味着什么以及如何解决它:
mypcap.c: In function ‘handle_sniffed’:
mypcap.c:61:15: warning: assignment makes pointer from integer without a cast [enabled by default]
Run Code Online (Sandbox Code Playgroud)
这指的是这条线 test2 = inet_ntoa(*test);
警告可能表示您没有正确的范围原型inet_ntoa()(因为您没有包含正确的标题).这意味着编译器假定它的返回类型为int.
你也传递test到printf()时你应该传递test2.
此外:
malloc()分配单个uint32_t;ntohl()因为inet_ntoa()期望它以网络字节顺序输入; 和inet_ntoa()已过期 - inet_ntop()应该在新代码中使用.尝试:
#include <arpa/inet.h>
if (*packet_ethertype == ETHERTYPE_IP) { /* IPv4 */
struct in_addr sin_addr;
char straddr[INET_ADDRSTRLEN];
memcpy(&sin_addr.s_addr, packet+26, 4);
if (inet_ntop(AF_INET, &sin_addr, straddr, sizeof straddr))
printf("%s\n", straddr);
else
perror("inet_ntop");
}
Run Code Online (Sandbox Code Playgroud)