免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5320 | 回复: 2
打印 上一主题 下一主题

内核模块使用struct kvec 结构时一个疑惑 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-09-10 11:03 |只看该作者 |倒序浏览
在内核模块里使用struct kvec用来TCP通信,问题如下

......
struct kvec iov;
iov.iov_base = buff;
iov.iov_base = len
log_message(0, "buff addr: %x\n", iov.iov_base);
rc = recv_message(socket, &iov, 1);
log_message(0, "iov_base: %x\n", iov.iov_base);
......

打印结果为:
fca82000
fca82018

也就是iov.iov_base的地址经过我的接收函数后就变了,导致我在接收函数里想检查结果也不行,不只是和原因呢?

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
2 [报告]
发表于 2010-09-14 08:57 |只看该作者
回复 1# Killdog1982


    你接收了多少字节?18字节?
不知道是不是接收自节后,他的地址会随之偏移。具体没有研究过,不过希望你能贴出所有的代码。

论坛徽章:
0
3 [报告]
发表于 2010-09-14 12:11 |只看该作者
代码有点长...

static size_t
recv_message(struct socket * sock, char * first_buff, struct kvec * iov, int iov_num)
{
    struct msghdr msg;
    int total_recv = 0, i = 0, header_done = 0;
        size_t rc = 0, desired = 0;

    if (!sock)
        return -ENOTCONN;
   
        memset(&msg, 0, sizeof(msg));
        msg.msg_flags = MSG_DONTWAIT;
        total_recv = 0;
        i = 0;
        desired = MSG_HDR_SIZE;
       
        while (1)
        {
                rc = kernel_recvmsg(sock, &msg, iov, 1, desired, MSG_DONTWAIT);
                if (rc == -EAGAIN)
                {
                        i++;
                        if (i >= MAX_RETRY_TIMES)
                        {
                                log_message(0, "EAGAIN received, total_recv: %d\n", total_recv);
                                break;
                        }
                        msleep_interruptible(1 << i);
                        continue;
                }
       
                if (rc > 0)
                {
                        total_recv += rc;
                        desired -= rc;

                        if (header_done && !desired)
                                break;
                        else
                        {
                                if (total_recv == MSG_HDR_SIZE)
                                {
                                        clinet_info * info = (client_info*)first_buff;
                                        desired = ntohl(info->ulength) - MSG_SIZE;
                                        header_done = 1;
                                        if (desired == 0)
                                                break;
                                }
                        }
                }
        }

    return total_recv;
}


上面是我改过之后的,又传一个first_buff过来才可以,不然还是不行
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP