Struct rtnetlink::NetlinkHeader[][src]

pub struct NetlinkHeader { /* fields omitted */ }

A Netlink header representation.

A netlink header has the following structure:

0                8                16              24               32
+----------------+----------------+----------------+----------------+
|                 packet length (including header)                  |
+----------------+----------------+----------------+----------------+
|          message type           |              flags              |
+----------------+----------------+----------------+----------------+
|                           sequence number                         |
+----------------+----------------+----------------+----------------+
|                   port number (formerly known as PID)             |
+----------------+----------------+----------------+----------------+

Example: parsing a netlink header

extern crate rtnetlink;
use rtnetlink::{NetlinkBuffer, NetlinkHeader, Parseable};
use rtnetlink::constants::{RTM_GETLINK, NLM_F_ROOT, NLM_F_REQUEST, NLM_F_MATCH};

// a packet captured with tcpdump that was sent when running `ip link show`
static PKT: [u8; 40] = [
    0x28, 0x00, 0x00, 0x00, // length = 40
    0x12, 0x00, // message type = 18 (RTM_GETLINK)
    0x01, 0x03, // flags = Request + Specify Tree Root + Return All Matching
    0x34, 0x0e, 0xf9, 0x5a, // sequence number = 1526271540
    0x00, 0x00, 0x00, 0x00, // port id = 0
    // payload
    0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x08, 0x00, 0x1d, 0x00, 0x01, 0x00, 0x00, 0x00];

fn main() {
    let pkt: NetlinkHeader = NetlinkBuffer::new_checked(&PKT[..]).unwrap().parse().unwrap();
    assert_eq!(pkt.length(), 40);
    assert_eq!(pkt.message_type(), RTM_GETLINK);
    assert_eq!(pkt.sequence_number(), 1_526_271_540);
    assert_eq!(pkt.port_number(), 0);
    assert_eq!(u16::from(pkt.flags()), NLM_F_ROOT | NLM_F_REQUEST | NLM_F_MATCH);
}

Example: emitting a netlink header

extern crate rtnetlink;
use rtnetlink::{NetlinkBuffer, NetlinkHeader, NetlinkFlags, Emitable};
use rtnetlink::constants::{RTM_GETLINK, NLM_F_ROOT, NLM_F_REQUEST, NLM_F_MATCH};

// a packet captured with tcpdump that was sent when running `ip link show`
static PKT: [u8; 40] = [
    0x28, 0x00, 0x00, 0x00, // length = 40
    0x12, 0x00, // message type = 18 (RTM_GETLINK)
    0x01, 0x03, // flags = Request + Specify Tree Root + Return All Matching
    0x34, 0x0e, 0xf9, 0x5a, // sequence number = 1526271540
    0x00, 0x00, 0x00, 0x00, // port id = 0
    // payload
    0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x08, 0x00, 0x1d, 0x00, 0x01, 0x00, 0x00, 0x00];

fn main() {
    let flags = NetlinkFlags::from(NLM_F_ROOT | NLM_F_REQUEST | NLM_F_MATCH);
    let pkt = NetlinkHeader::new(40, RTM_GETLINK, flags, 0x5af9_0e34, 0);
    assert_eq!(pkt.buffer_len(), 16);
    let mut buf = vec![0; 16];
    pkt.emit(&mut buf[..]);
    assert_eq!(&buf[..], &PKT[..16]);
}

Methods

impl NetlinkHeader
[src]

Create a new header, initialized with the given values

Get the length field

Get a mutable reference to the length field

Setter for the length field

Get the message type field

Get a mutable reference to the message type field

Setter for the message_type field

Get the flags field

Get a mutable reference to the flags field

Setter for the flags field

Get the sequence number field

Get a mutable reference to the sequence number field

Setter for the sequence number field

Get the port number field

Get a mutable reference to the port number field

Setter for the port number field

Trait Implementations

impl Debug for NetlinkHeader
[src]

Formats the value using the given formatter. Read more

impl PartialEq for NetlinkHeader
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Eq for NetlinkHeader
[src]

impl Clone for NetlinkHeader
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Copy for NetlinkHeader
[src]

impl Hash for NetlinkHeader
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl Default for NetlinkHeader
[src]

Returns the "default value" for a type. Read more

impl Emitable for NetlinkHeader
[src]

Return the length of the serialized data.

Serialize this types and write the serialized data into the given buffer. Read more

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NetlinkHeader> for NetlinkBuffer<&'a T>
[src]

Deserialize the current type.

Auto Trait Implementations