File: ctrl-list.rs

package info (click to toggle)
rust-neli 0.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 456 kB
  • sloc: makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,624 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::error::Error;

use neli::{
    attr::Attribute,
    consts::{genl::*, nl::*, socket::*},
    genl::{Genlmsghdr, GenlmsghdrBuilder},
    nl::NlPayload,
    router::synchronous::NlRouter,
    types::{Buffer, GenlBuffer},
    utils::Groups,
};

const GENL_VERSION: u8 = 2;

// This example attempts to mimic the "genl ctrl list" command. For simplicity, it only outputs
// the name and identifier of each generic netlink family.

fn main() -> Result<(), Box<dyn Error>> {
    env_logger::init();

    let (socket, _) = NlRouter::connect(NlFamily::Generic, None, Groups::empty())?;
    let recv = socket.send::<_, _, NlTypeWrapper, Genlmsghdr<CtrlCmd, CtrlAttr>>(
        GenlId::Ctrl,
        NlmF::DUMP,
        NlPayload::Payload(
            GenlmsghdrBuilder::default()
                .cmd(CtrlCmd::Getfamily)
                .version(GENL_VERSION)
                .attrs(GenlBuffer::<u16, Buffer>::new())
                .build()?,
        ),
    )?;

    for response_result in recv {
        let response = response_result?;

        if let Some(p) = response.get_payload() {
            let handle = p.attrs().get_attr_handle();
            for attr in handle.iter() {
                match attr.nla_type().nla_type() {
                    CtrlAttr::FamilyName => {
                        println!("{}", attr.get_payload_as_with_len::<String>()?);
                    }
                    CtrlAttr::FamilyId => {
                        println!("\tID: 0x{:x}", attr.get_payload_as::<u16>()?);
                    }
                    _ => (),
                }
            }
        }
    }

    Ok(())
}