小编典典

使用Linux netfilter将源IP地址从struct iphdr *转换为等效字符串

linux

我想将使用netfilter捕获的数据包中的源IP地址和目标IP地址转换为char *。

在我的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

谢谢。


阅读 805

收藏
2020-06-07

共1个答案

小编典典

内核的printf()功能家族具有用于IP地址的特殊格式说明符(%pI4用于IPv4地址,%pI6用于IPv6)。

因此,使用IPv4,您可以使用类似以下内容:

char source[16];
snprintf(source, 16, "%pI4", &ip_header->saddr); // Mind the &!

或写入动态分配的内存。

如果您只想打印调试输出,也可以使用printk()。有关的许多其他功能%p,请参阅本文档

2020-06-07