【发布时间】:2022-01-08 13:00:00
【问题描述】:
我有以下代码,它只捕获数据包并打印 IP 源和目标:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
static struct nf_hook_ops nfho;
unsigned int hook_func(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) {
struct iphdr *ip_header = (struct iphdr *)skb_network_header(skb);
unsigned int src_ip = (unsigned int)ip_header->saddr;
unsigned int dest_ip = (unsigned int)ip_header->daddr;
printk(KERN_INFO "IPs: %u \t to \t %u \n", src_ip, dest_ip);
return NF_ACCEPT;
}
int init_module() { /* Fill in our hook structure */
nfho.hook = hook_func; /* Handler function */
nfho.hooknum = NF_INET_PRE_ROUTING; /* First hook for IPv4 */
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST; /* Make our function first */
nf_register_hook(&nfho);
return 0;
}
void cleanup_module() {
nf_unregister_hook(&nfho);
}
但是,我不知道如何正确打印它(如 IP X.X.X.X),因为它显示以下信息:
IP:16777343 到 16842879
IP:4198316624 到 67108884
IP:16842879 到 16777343
有人可以帮助我吗? 谢谢!
【问题讨论】:
标签: c linux-kernel ip netfilter