if(printk_ratelimit())
{
#define NIPQUAD(addr) \
((unsigned char *)&addr)[0],
\ ((unsigned char *)&addr)[1],
\ ((unsigned char *)&addr)[2],
\ ((unsigned char *)&addr)[3]
if((skb ->protocol) == htons(ETH_P_IP ))
{
struct iphdr *nh;
struct tcphdr *th;
nh = ip_hdr(skb);
//printk("src: %u.%u.%u.%u, dst: %u.%u.%u.%u\n", NIPQUAD(nh->saddr), NIPQUAD(nh->daddr));
if(nh->protocol == IPPROTO_TCP)
{
th = tcp_hdr(skb);
printk(" src: %u.%u.%u.%u, sport: %d dst: %u.%u.%u.%u dport: %d \n", NIPQUAD(nh->saddr), th->source, NIPQUAD(nh->daddr), th->dest);
}
}
}
最近编写内核代码,有发现一个新的内核打印ip地址的方法:
if (printk_ratelimit()) {
printk("%s fun: IP: %pI4\n", __func__, &iph->saddr);
}