This crate contains building blocks for the route netlink protocol.
Messages
This crate provides two representations of most netlink packets:
-
Buffer types:
NetlinkBuffer
,LinkBuffer
,NlaBuffer
, etc. These types wrappers around actual byte buffers, and provide safe accessors to the various fields of the packet they represent. These types are useful if you manipulate byte streams, but everytime data is accessed, it must be parsed or encoded. -
Message and Nla types:
NetlinkMessage
,LinkMessage
,LinkNla
,AddressNla
etc. These are higher level representations of netlink packets and are the prefered way to build packets.
Using buffer types to parse messages
It is possible to go from on representation to another. Actually, the buffer types are used to
parse byte buffers into messages types, using the Parseable
trait. In
the list of implementors, we can see for instance:
impl<'buffer, T: AsRef<[u8]> + 'buffer> Parseable<NetlinkMessage> for NetlinkBuffer<&'buffer T>
That means a NetlinkBuffer
is parseable into a NetlinkMessage
:
extern crate rtnetlink;
use ;
use ;
// a packet captured with tcpdump that was sent when running `ip link show`
static PKT: = ;
This prints:
NetlinkMessage {
header: NetlinkHeader {
length: 40,
message_type: 18,
flags: NetlinkFlags(769),
sequence_number: 1526271540,
port_number: 0
},
message: GetLink(
LinkMessage {
header: LinkHeader {
address_family: 17,
index: 0,
link_layer_type: Netrom,
flags: LinkFlags(0),
change_mask: LinkFlags(0)
},
nlas: [ExtMask(1)]
}
),
finalized: true
}
Emitting messages
TODO
Should I use Buffer types or Message type (NetlinkBuffer
or NetlinkMessage
)?
Message types are much more convenient to work with. They are full blown rust types, and easier to manipulate. I use buffer types mostly as temporary intermediate representations between bytes and message types.
However, if you have to treat large quantities of packets and are only interested in a few of them, buffer types may be useful, because they allow to perform quick check on the messages without actually parsing them.
TODO
Tokio integration
TODO