- 論壇徽章:
- 6
|
回復(fù) 8# 雷鋒不謝
void printpacket(struct sk_buff*skb)
{
struct ethhdr *eth;
struct iphdr *iph;
struct tcphdr *tcph;//假定該packet為tcp包
eth=eth_hdr(skb);
iph=ip_hdr(skb);
tcph=tcp_hdr(skb);
int i;
printk("mac dst:");
for(i=0;i<6;i++)
printk("%02x ",eth->h_dest);
printk("\n");
printk("mac src:");
for(i=0;i<6;i++)
printk("%02x ",eth->h_source);
printk("\n");
printk("ip src:%pI4\n",&iph->saddr);//這是專門打印ip的格式,注意,參數(shù)為指針。
printk("ip dst:%pI4\n",&iph->daddr);//這是專門打印ip的格式,注意,參數(shù)為指針。
//printk("ip src :%u.%u.%u.%u",NIPQUAD(iph->saddr));//用NIPQUAD也是可以滴。。
printk("tcp srcport:%d\n",ntohs(tcph->source));
printk("tcp dstport:%d\n",ntohs(tcph->dest));
}
1. 你為什么假定為tcp。考觽判斷就那么難?
if (iph->protocol != IPPROTO_TCP)
return NF_ACCEPT;
2. 取tcphdr的方法有問題
tcp_hdr(skb)的方法只能在傳輸層調(diào)用,那個時候已經(jīng)設(shè)置好了傳輸層的頭(比如ip_local_deliver_finish函數(shù)中調(diào)用set_reset_transport_header),可以直接調(diào)用了。
而你現(xiàn)在的hook在ip層,需要使用如下方法取得tcp頭
struct tcphdr _tcph, *th;
th = skb_header_pointer(skb, ip_hdrlen(skb),
sizeof(_tcph), &_tcph); |
|