Struct NetlinkHeader

Source
pub struct NetlinkHeader { /* private fields */ }
Expand description

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)             |
+----------------+----------------+----------------+----------------+
extern crate netlink_packet;
use netlink_packet::{NetlinkBuffer, NetlinkHeader, Parseable};
use netlink_packet::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);
}
extern crate netlink_packet;
use netlink_packet::{NetlinkBuffer, NetlinkHeader, NetlinkFlags, Emitable};
use netlink_packet::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]);
}

Implementations§

Source§

impl NetlinkHeader

Source

pub fn new( length: u32, message_type: u16, flags: NetlinkFlags, sequence_number: u32, port_number: u32, ) -> Self

Create a new header, initialized with the given values

Source

pub fn length(&self) -> u32

Get the length field

Source

pub fn length_mut(&mut self) -> &mut u32

Get a mutable reference to the length field

Source

pub fn set_length(&mut self, value: u32) -> &mut Self

Setter for the length field

Source

pub fn message_type(&self) -> u16

Get the message type field

Source

pub fn message_type_mut(&mut self) -> &mut u16

Get a mutable reference to the message type field

Source

pub fn set_message_type(&mut self, value: u16) -> &mut Self

Setter for the message_type field

Source

pub fn flags(&self) -> NetlinkFlags

Get the flags field

Source

pub fn flags_mut(&mut self) -> &mut NetlinkFlags

Get a mutable reference to the flags field

Source

pub fn set_flags(&mut self, value: NetlinkFlags) -> &mut Self

Setter for the flags field

Source

pub fn sequence_number(&self) -> u32

Get the sequence number field

Source

pub fn sequence_number_mut(&mut self) -> &mut u32

Get a mutable reference to the sequence number field

Source

pub fn set_sequence_number(&mut self, value: u32) -> &mut Self

Setter for the sequence number field

Source

pub fn port_number(&self) -> u32

Get the port number field

Source

pub fn port_number_mut(&mut self) -> &mut u32

Get a mutable reference to the port number field

Source

pub fn set_port_number(&mut self, value: u32) -> &mut Self

Setter for the port number field

Trait Implementations§

Source§

impl Clone for NetlinkHeader

Source§

fn clone(&self) -> NetlinkHeader

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NetlinkHeader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NetlinkHeader

Source§

fn default() -> NetlinkHeader

Returns the “default value” for a type. Read more
Source§

impl Emitable for NetlinkHeader

Source§

fn buffer_len(&self) -> usize

Return the length of the serialized data.
Source§

fn emit(&self, buffer: &mut [u8])

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

impl Hash for NetlinkHeader

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

fn parse(&self) -> Result<NetlinkHeader, DecodeError>

Deserialize the current type.
Source§

impl PartialEq for NetlinkHeader

Source§

fn eq(&self, other: &NetlinkHeader) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for NetlinkHeader

Source§

impl Eq for NetlinkHeader

Source§

impl StructuralPartialEq for NetlinkHeader

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.