免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: Godbach
打印 上一主题 下一主题

关于构造Http Response包的问题 [复制链接]

论坛徽章:
0
41 [报告]
发表于 2008-09-10 23:00 |只看该作者
我用的是2.6.26,skb_copy_expand代码有点不一样
/**
*        skb_copy_expand        -        copy and expand sk_buff
*        @skb: buffer to copy
*        @newheadroom: new free bytes at head
*        @newtailroom: new free bytes at tail
*        @gfp_mask: allocation priority
*
*        Make a copy of both an &sk_buff and its data and while doing so
*        allocate additional space.
*
*        This is used when the caller wishes to modify the data and needs a
*        private copy of the data to alter as well as more space for new fields.
*        Returns %NULL on failure or the pointer to the buffer
*        on success. The returned buffer has a reference count of 1.
*
*        You must pass %GFP_ATOMIC as the allocation priority if this function
*        is called from an interrupt.
*/
struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
                                int newheadroom, int newtailroom,
                                gfp_t gfp_mask)
{
        /*
         *        Allocate the copy buffer
         */
        struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
                                      gfp_mask);
        int oldheadroom = skb_headroom(skb);
        int head_copy_len, head_copy_off;
        int off;

        if (!n)
                return NULL;

        skb_reserve(n, newheadroom);

        /* Set the tail pointer and length */
        skb_put(n, skb->len);

        head_copy_len = oldheadroom;
        head_copy_off = 0;
        if (newheadroom <= head_copy_len)
                head_copy_len = newheadroom;
        else
                head_copy_off = newheadroom - head_copy_len;

        /* Copy the linear header and data. */
        if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
                          skb->len + head_copy_len))
                BUG();

        copy_skb_header(n, skb);

        off                  = newheadroom - oldheadroom;
        n->csum_start       += off;
#ifdef NET_SKBUFF_DATA_USES_OFFSET
        n->transport_header += off;
        n->network_header   += off;
        n->mac_header            += off;
#endif

        return n;
}

论坛徽章:
0
42 [报告]
发表于 2008-09-10 23:12 |只看该作者
原帖由 Godbach 于 2008-9-5 10:55 发表
基本上搞清楚了这个函数,当tailroom的长度小于需要追加数据时,函数skb_copy_expand将重新申请buffer,其大小为原先skb的headroom大小+ skb->len+ 需要追加数据的长度,相当于把原先的tailroom扩大到等于追加数 ...


从代码来看,skb_copy_expand上来就重新分配了一个struct sk_buff *,
长度等于原来的长度加上newheadroom再加上newtailroom,是不是有点浪费?
难道不应该是skb->len + newheadroom + newtailroom - skb_headroom(skb)- 原来的newtailroom 吗?

如果头部保持不变的话,那么newheadroom取值应该等于skb_headroom(skb)而不是0?

[ 本帖最后由 flyfrogs 于 2008-9-10 23:13 编辑 ]

论坛徽章:
0
43 [报告]
发表于 2008-09-10 23:15 |只看该作者
如果不是想在尾部填充数据,而是想在中间插入数据,
那么skb_copy_expand是不是就不适用了?
那用什么呢?

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
44 [报告]
发表于 2008-09-11 09:29 |只看该作者
原帖由 flyfrogs 于 2008-9-10 23:00 发表
我用的是2.6.26,skb_copy_expand代码有点不一样
/**
*        skb_copy_expand        -        copy and expand sk_buff
*        @skb: buffer to copy
*        @newheadroom: new free bytes at head
*        @newtailroom: new free bytes ...


看来和2.4的不大一样。那么可能她这个函数完全就是用来扩展一个原skb中tail-head的大小也不够用的情形吧。

其实这样也好,单独调整headroom和tailroom的函数本来就有。这样可以让每个函数的功能更加单一。

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
45 [报告]
发表于 2008-09-11 09:31 |只看该作者
原帖由 flyfrogs 于 2008-9-10 23:12 发表


从代码来看,skb_copy_expand上来就重新分配了一个struct sk_buff *,
长度等于原来的长度加上newheadroom再加上newtailroom,是不是有点浪费?
难道不应该是skb->len + newheadroom + newtailroom - skb ...


他这样做是为了保持函数的通用性。你可以看一下,真正调用这个函数的代码里,分别传给newheadroom和newtailroom是什么值

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
46 [报告]
发表于 2008-09-11 09:33 |只看该作者
原帖由 flyfrogs 于 2008-9-10 23:15 发表
如果不是想在尾部填充数据,而是想在中间插入数据,
那么skb_copy_expand是不是就不适用了?
那用什么呢?



只要你data的空间足够大,完全可以将你新的数据插入到中间某个位置,原先从该位置开始的数据往后调整新数据的长度应该就可以了

论坛徽章:
0
47 [报告]
发表于 2008-09-11 18:49 |只看该作者
我构造新的sk_buff *代码如下:
static struct sk_buff *create_skb(struct sk_buff *skb, int extra_len)
{
        int headroom;
        struct sk_buff *new_skb;

        headroom = skb_headroom(skb);
        new_skb = skb_copy_expand(skb, headroom, extra_len, GFP_ATOMIC);
        if(new_skb != NULL)
        {
                skb_put(new_skb, extra_len);
                new_skb->dev = skb->dev;
                new_skb->tstamp = skb->tstamp;
                new_skb->csum = skb->csum;
                new_skb->csum_start = skb->csum_start;
                new_skb->csum_offset = skb->csum_offset;
                new_skb->local_df = skb->local_df;
                new_skb->pkt_type = skb->pkt_type;
                new_skb->ip_summed = skb->ip_summed;
                new_skb->priority = skb->priority;
                new_skb->protocol = skb->protocol;
                new_skb->nf_bridge = skb->nf_bridge;
                new_skb->iif = skb->iif;
                new_skb->dst = skb->dst;
                new_skb->sp = skb->sp;
                MY_IPHDR(new_skb)->tot_len = htons(my_ntohs(MY_IPHDR(skb)->tot_len) + extra_len);
        }
        return new_skb;
}
然后填充TCP数据区域的数据,计算IP和TCP校验和,然后使用dev_queue_xmit发送,
但是很可惜,没发出去,我在目标机上使用commview抓包没见到这个包。

怎么回事?

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
48 [报告]
发表于 2008-09-11 19:25 |只看该作者
这个问题要检查的方面比较多, 要么就加上printk打印相关信息吧,看看你的程序是走到哪一行出问题了。我一般是通过这样来查找错误的。

我也出现做这样的问题,后来发现原因是skb->len和iph->tot_len不一致造成的。 你可以见检查一下是否有这个问题。

如果发不出去数据包,肯定是skb中相关参数和具体数据有不对照的关系。 只是数据包的内容不对,校验和不对,数据包是可以发出去,也可以抓到包,顶多校验和不对。

另外,既然,你的数据包是基于原先的skb进行修改的,我觉得可以把原先的skb全部拷贝过去,然后从你需要修改的地方进行修改。

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
49 [报告]
发表于 2008-09-11 19:27 |只看该作者
skb->len好像是主机字节序,不用转换的

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
50 [报告]
发表于 2008-09-11 19:29 |只看该作者
确定了一下skb->len = ntohs(iph->tot_len)
因此应该是主机字节序,不用转换。
                MY_IPHDR(new_skb)->tot_len = htons(my_ntohs(MY_IPHDR(skb)->tot_len) + extra_len);
这行代码修改一下,看看啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP