0

I have a module that receives data in two parts. First an unsigned integer indicating the length of data that will be followed next. Then the data itself. I do this as follows in an infinite loop

unsigned int z;
struct kvec vec;
struct msghdr msg;
while(1) {
    memset(&vec, 0, sizeof(vec));
    memset(&msg, 0, sizeof(msg));
    vec.iov_base = &z;
    vec.iov_len = sizeof(unsigned int);

    ret = kernel_recvmsg(client_socket, &msg, &vec, 1, vec.iov_len, 0);
    if(!z)
        break;
    data = kmalloc(z, GFP_KERNEL);
    if(!data)
        break;
    memset(&vec, 0, sizeof(vec));
    memset(&msg, 0, sizeof(msg));
    vec.iov_base = data;
    vec.iov_len = z;
    ret = kernel_recvmsg(client_socket, &msg, &vec, 1, vec.iov_len, 0);
    ...
    ...
    kfree(data);
}

Edit : I am getting wrong data because of incomplete receives. My ret < vec.iov_len. Is there a way to workaround this?

Thank you.

1 Answers1

0

I fixed it by changing the flags. These links will help

http://www.beej.us/guide/bgnet/output/html/multipage/recvman.html

http://elixir.free-electrons.com/linux/latest/source/include/linux/socket.h#L277

In the second link you could find all available flags and a description of a subset of those flags. I had earlier used flag field as 0 which meant no option in particular, by changing it to MSG_WAITALL, the command waits for entire iov.len sized message before returning. Incomplete receives were earlier corrupting my data.