- 论坛徽章:
- 0
|
原帖由 loveskyer 于 2007-5-13 13:38 发表于 8楼
struct tcphdr *thead = (struct tcphdr *)(sk->data + (sk->nh.iph->ihl * 4));
这句关键的地方没看懂!可以解释一下啊吗?
这句就是用来跳过ip头得到tcp头吧。
正好这两天做的东西里有个HTTP重定向的部分,是直接写在ip_nat_fn函数里的,也现个丑贴下代码好了:
- static unsigned int
- ip_nat_fn(unsigned int hooknum,
- struct sk_buff **pskb,
- const struct net_device *in,
- const struct net_device *out,
- int (*okfn)(struct sk_buff *))
- {
- .............
- IP_NF_ASSERT(info);
- if(((*pskb)->nh.iph->protocol ==IPPROTO_TCP))
- {
- struct tcphdr *thead=(struct tcphdr *)((*pskb)->data +((*pskb)->nh.iph->ihl * 4));
- __u16 desport=ntohs(thead->dest);
- struct ip_nat_multi_range mr;
- switch (ctinfo) {
- case IP_CT_NEW:
- case IP_CT_ESTABLISHED:
-
- if (desport == 80)
- {
- __u32 lanip=0;
- struct in_device *in_dev;
- rcu_read_lock();
- in_dev = __in_dev_get(in);
- if (in_dev){
- // __u8 ip[4]={NIPQUAD((*pskb)->nh.iph->saddr)};//get src address number
- struct in_ifaddr *ifa;
- //获取网关地址,这段是从inet_select_addr函数里抄的一段,从入口设备获取网关地址
- for (ifa = (in_dev)->ifa_list; ifa && !(ifa->ifa_flags&IFA_F_SECONDARY); ifa = ifa->ifa_next)
- {
- if (ifa->ifa_scope > RT_SCOPE_UNIVERSE)
- continue;
- lanip=ifa->ifa_local;
- break;
- }
- //比较目的地址是否为网关,如果不是重定向回网关
- if(!inet_ifa_match((*pskb)->nh.iph->daddr,ifa))
- {
- unsigned int ret;
- mr.rangesize = 1;
- mr.range[0].flags = IP_NAT_RANGE_MAP_IPS;
- mr.range[0].min_ip = mr.range[0].max_ip = lanip;
- if((ret=ip_nat_setup_info(ct, &mr, hooknum))!=NF_ACCEPT)
- return ret;
- DEBUGP("http redirect change to %u,%u,%u,%u \n",NIPQUAD(lanip));
- }
- }
- rcu_read_unlock();
- }
-
- break;
- }
- }
- return do_bindings(ct, ctinfo, info, hooknum, pskb);
- }
复制代码 |
|