Crates.io | rsmnl-derive |
lib.rs | rsmnl-derive |
version | 0.1.0 |
source | src |
created_at | 2021-09-01 05:47:03.629254 |
updated_at | 2021-09-01 05:47:03.629254 |
description | procedural macro for rsmnl |
homepage | |
repository | https://github.com/chamaken/rsmnl-derive |
max_upload_size | |
id | 445459 |
size | 43,528 |
I just want to write rsmnl-core examples shorter and more compact, and to try using Rust procedual macro. See examples in rsmnl-linux.
see examples/netfilter/nfct-daemon.rs and src/netfilter/nfnetlink_conntrack.rs.
define:
use { Msghdr, Attr, AttrTbl, Result };
#[repr(u16)]
#[derive(..., NlaType)
pub enum Parent {
None = 0,
One,
Two,
Three,
_MAX
}
will implements std::convert::TryFrom
Then define nla_type by macro attribute:
[#nla_type(u32, one)]
One,
putting value to nlh: Msghdr (e.g. Nlmsghdr) can be done by:
use mnl:: { AttrTbl, Msghdr };
Parent::put_one(&mut nlv, 1234u32)
create tb data from read Msghdr, specify its table name:
#[tbname="ParentTbl"]
pub enum Parent {
Then, value can be accessed via:
let tb = ParentTbl::from_nlmsg(header_offset, nlh)?;
let one: Option<u32> = tb.one()?;
let attr: Option<&Attr> = tb[Parent::One]?;
Two is nested which is defined:
#[repr(u16)]
#[derive(..., NlaType)
#[tbname="ChildTbl"]
pub enum Child {
None = 0,
[#nla_type(str, one)]
Ichi,
Ni,
San,
_MAX
}
In enum Parent, define:
[#nla_nest(ChildTbl, two)]
Two,
Then Two can be acquire and access:
let two = tb.two()?;
let ichi = two.ichi()?;