我想转换来源&从使用netfilter捕获的数据包到char *的目标IP地址。
在我的netfilter钩子函数中,我有:
sock_buff = skb; // argument 2 of hook function
// ip_header is struct iphdr*
ip_header = (struct iphdr *)skb_network_header(sock_buff);
// now how to convert ip_header->saddr & ip_header->daddr to char *
// ip_header->saddr & ip_header->daddr are of type __be32
感谢。
答案 0 :(得分:16)
内核的printf()
函数族具有IP地址的特殊格式说明符(IPv4地址为%pI4
,IPv6为%pI6
。
因此,使用IPv4,您可以使用以下内容:
char source[16];
snprintf(source, 16, "%pI4", &ip_header->saddr); // Mind the &!
或写入动态分配的内存。
如果您只想打印调试输出,也可以使用printk()
。有关%p
的许多其他功能,请参阅this document。
答案 1 :(得分:1)
在onActivityCreated()
中尝试in4_pton()
函数(定义:https://elixir.bootlin.com/linux/latest/source/net/core/utils.c#L118)
net/core/utils.c