little-dude / netlink

netlink libraries for rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

can't get the messages's data

jm-observer opened this issue · comments

when i try to receive the messages form Kernel, it return the right size, but didn't to write the data to buf. and i change netlink-sys's version("0.8.3") to "0.7", it does work.

use std::process;
use std::str::from_utf8;
use netlink_sys::{protocols::NETLINK_KOBJECT_UEVENT, Socket, SocketAddr};
fn main() {
    let mut socket = Socket::new(NETLINK_KOBJECT_UEVENT).unwrap();
    let sa = SocketAddr::new(process::id(), 1);
    let mut buf = vec![0; 1024 * 8];
    socket.bind(&sa).unwrap();
    loop {
        let n = socket.recv(&mut buf, 0).unwrap();
        let msg = from_utf8(&buf[..n]).unwrap();
        println!("msg: {:?}", msg);
    }
}

the output:

msg: "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

Same here. The buf sent to recv is never modified, seems the correct length is returned though.

The API was changed to take a bytes::BufMut (as mutable reference ofc); the bytes::BufMut implementation for Vec<u8> appends at the end, so you should only reserve but not actually fill the vector. Or use bytes::BytesMut instead of Vec<u8>.
The returned "length" should only be used with MSG_TRUNC in the flags to handle "actual lengths" of the data (to reserve the buffer with the appropiate size); apart from that use the length of the filled bytes::BufMut object.

ok, when change the code as follows, it can now receive the message correctly. Thanks!
let mut buf = BytesMut::with_capacity(1024 * 8);
or
let mut buf = Vec::with_capacity(1024 * 8);