使用Linux的netfilter从结构iphdr *转换的源IP地址,等效字符串 [英] Convert source IP address from struct iphdr* to string equivalent using Linux netfilter
问题描述
我希望将源和放大器转换;使用网络过滤器为char *捕获数据包的目的地IP地址。
I want to convert the source & destination IP addresses from a packet captured using netfilter to char *.
在我的netfilter钩子函数,我有:
In my netfilter hook function, I have:
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
感谢。
推荐答案
内核的系列的printf()
功能具有IP-地址的特殊格式说明符(%PI4
对于IPv4的地址,%PI6
对于IPv6)。
The kernel's family of printf()
functions has a special format specifier for IP-addresses (%pI4
for IPv4-addresses, %pI6
for IPv6).
因此,与IPv4中,你可以使用类似:
So with IPv4, you could use something like:
char source[16];
snprintf(source, 16, "%pI4", &ip_header->saddr); // Mind the &!
或写信到动态分配的内存。
Or write to dynamically allocated memory.
如果您只是想打印的调试输出,您也可以使用的printk()
。对于%P
的许多其他功能,请参阅这文件。
If you simply want to print debug-output, you can also use printk()
. For the many other features of %p
, see this document.
这篇关于使用Linux的netfilter从结构iphdr *转换的源IP地址,等效字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!