Skip to main content

Attribute

Struct Attribute 

Source
pub struct Attribute {
    pub len: u16,
    pub type_: u16,
}
Expand description

Represents a netlink attribute with type-length-value (TLV) format

Netlink attributes use a TLV format where each attribute consists of:

  • A length field (including header and payload)
  • A type field (which may include flags like NESTED and NET_BYTEORDER)
  • A variable-length payload

Fields§

§len: u16§type_: u16

Implementations§

Source§

impl Attribute

Source

pub const NESTED: i32

Flag bit indicating a nested attribute

Source

pub const NET_BYTEORDER: i32

Flag bit indicating the attribute value is in network byte order

Source

pub const TYPE_MASK: i32

Mask to extract the attribute type without flag bits

Source

pub const ALIGNTO: usize = 4

Alignment size for netlink attributes (4 bytes)

Source

pub const HDRLEN: usize

Size of attribute header, aligned to ALIGNTO boundary

Source

pub const data_type_len: [usize; 5]

Size lookup table for standard data types

Source

pub const fn new() -> Self

Creates a new empty attribute

§Returns

A zeroed attribute structure

Source

pub const fn align(len: usize) -> usize

Aligns a length to the attribute alignment boundary

This ensures proper memory alignment for netlink attributes.

§Parameters
  • len - The length to align
§Returns

The length aligned to ALIGNTO boundary

Source

pub fn is_empty(&self) -> bool

Checks if the attribute is empty (all bytes are zero)

Used to determine if the attribute has been initialized or reset.

§Returns

true if all bytes in the attribute are zero, false otherwise

Source

pub fn reset(&mut self)

Clears the attribute by zeroing its memory

Resets all bytes of the attribute to zero, effectively clearing it. Does nothing if the attribute is already empty.

Source

pub fn for_each(&mut self, nlh: &mut Header, offset: usize)

Iterates through all attributes in a netlink message

Provides access to each attribute within the message payload.

§Parameters
  • nlh - Netlink message header containing the attributes
  • offset - Offset in bytes from the start of the payload to the first attribute
Source

pub fn for_each_nested(&mut self) -> *mut Self

Iterates through attributes in a nested attribute

Provides access to each attribute within a nested attribute.

§Returns

Pointer to the first attribute within the nested attribute

§Panics

In debug mode, panics if this attribute is not a nested attribute

Source

pub fn for_each_payload(&mut self, payload_size: usize) -> *mut Self

Iterates through attributes in a raw payload buffer

Used when processing a raw buffer containing netlink attributes.

§Parameters
  • payload_size - Size in bytes of the payload buffer
§Returns

Pointer to the first attribute in the payload

Source

pub fn get_type(&self) -> u16

Gets the attribute type value without flag bits

§Returns

The attribute type with the NESTED and NET_BYTEORDER flag bits masked out

Source

pub fn len(&self) -> u16

Gets the total length of the netlink attribute

§Returns

The total length of the attribute in bytes, including header and payload

Source

pub fn get_payload_len(&self) -> u16

Gets the length of the attribute payload (value part)

§Returns

The length of the attribute payload in bytes

Source

pub fn get_payload(&self) -> *mut ()

Gets pointer to the attribute payload

§Returns

A mutable pointer to the attribute’s payload (value part)

Source

pub fn is_ok(&self, len: i32) -> bool

Checks if there is room for an attribute in a buffer

Verifies that a buffer containing an attribute has enough space for the attribute, ensuring it is neither malformed nor truncated.

§Parameters
  • len - The length of the buffer in bytes
§Returns

true if the attribute fits within the buffer, false otherwise

Source

pub fn next(&self) -> *mut Self

Gets the next attribute in a sequence

§Returns

A pointer to the next attribute in a sequence, accounting for alignment

Source

pub fn type_valid(&self, max: u16) -> i32

Verifies if the attribute type is within the supported range

§Parameters
  • max - The maximum valid attribute type value
§Returns

0 on error, 1 on success

Source

pub fn __validate(&self, type_: DataType, exp_len: usize) -> i32

Internal validation function for netlink attributes

§Parameters
  • type_ - The expected data type of the attribute
  • exp_len - The expected length of the attribute payload
§Returns

0 on success, negative error code on failure

Source

pub fn validate(&self, type_: DataType) -> i32

Validates a netlink attribute against its expected data type

Ensures that integer attributes have sufficient space allocated.

§Parameters
  • type_ - The expected data type of the attribute
§Returns

0 on success, negative error code on failure

§Panics

In debug mode, panics if the attribute has zero length

Source

pub fn validate2(&self, type_: DataType, exp_len: usize) -> i32

Extended validation for attributes with variable size

Allows more precise validation for attributes with variable data sizes.

§Parameters
  • type_ - The expected data type of the attribute
  • exp_len - The expected length of the attribute payload
§Returns

0 on success, negative error code on failure

Source

pub fn parse( nlh: &mut Header, offset: u32, cb: attr_callback_t, data: *mut u8, ) -> i32

Parses attributes in a netlink message

Iterates over the sequence of attributes in the message, calling the provided callback function for each attribute.

§Parameters
  • nlh - Netlink message header containing attributes to parse
  • offset - Offset in bytes from the start of the payload to the first attribute
  • cb - Callback function to call for each attribute
  • data - User data to pass to the callback function
§Returns

Status code from the last callback, or error code

Source

pub fn parse_nested(&mut self, cb: attr_callback_t, data: *mut u8) -> i32

Parses attributes inside a nested attribute

Processes attributes contained within a nested attribute, calling the provided callback for each one.

§Parameters
  • cb - Callback function to call for each attribute
  • data - User data to pass to the callback function
§Returns

Status code from the last callback, or error code

Source

pub fn parse_payload( &mut self, payload_len: usize, cb: attr_callback_t, data: *mut u8, ) -> i32

Parses attributes in a raw payload buffer

Processes attributes contained within a raw buffer, calling the provided callback for each one.

§Parameters
  • payload_len - Length of the payload in bytes
  • cb - Callback function to call for each attribute
  • data - User data to pass to the callback function
§Returns

Status code from the last callback, or error code

Source

pub fn get_u8(&self) -> u8

Gets the 8-bit unsigned integer value from an attribute

§Returns

The 8-bit unsigned integer value contained in the attribute

Source

pub fn get_u16(&self) -> u16

Gets the 16-bit unsigned integer value from an attribute

Reads the value in an alignment-safe manner.

§Returns

The 16-bit unsigned integer value contained in the attribute

Source

pub fn get_u32(&self) -> u32

Gets the 32-bit unsigned integer value from an attribute

§Returns

The 32-bit unsigned integer value contained in the attribute

Source

pub fn get_u64(&self) -> u64

Gets the 64-bit unsigned integer value from an attribute

Reads the value in an alignment-safe manner.

§Returns

The 64-bit unsigned integer value contained in the attribute

Source

pub fn get_uint(&self) -> u64

Gets a variable-sized integer from an attribute

Handles attributes containing 8-bit, 16-bit, 32-bit, or 64-bit integers.

§Returns

The integer value as a u64, regardless of the actual size

Source

pub fn get_str(&self) -> *const i8

Gets a pointer to the string value in an attribute

§Returns

A pointer to the C-style string contained in the attribute

Source

pub fn put(nlh: &mut Header, type_: u16, len: usize, data: *const u8)

Adds an attribute to a netlink message

Updates the length field of the message by adding the size of the new attribute.

§Parameters
  • nlh - Netlink message header to add the attribute to
  • type_ - Type of the attribute
  • len - Length of the attribute payload in bytes
  • data - Pointer to the attribute payload data
Source

pub fn put_u8(nlh: &mut Header, type_: u16, data: u8)

Adds an 8-bit unsigned integer attribute to a netlink message

§Parameters
  • nlh - Netlink message header to add the attribute to
  • type_ - Type of the attribute
  • data - The 8-bit unsigned integer value to add
Source

pub fn put_u16(nlh: &mut Header, type_: u16, data: u16)

Adds a 16-bit unsigned integer attribute to a netlink message

§Parameters
  • nlh - Netlink message header to add the attribute to
  • type_ - Type of the attribute
  • data - The 16-bit unsigned integer value to add
Source

pub fn put_u32(nlh: &mut Header, type_: u16, data: u32)

Adds a 32-bit unsigned integer attribute to a netlink message

§Parameters
  • nlh - Netlink message header to add the attribute to
  • type_ - Type of the attribute
  • data - The 32-bit unsigned integer value to add
Source

pub fn put_u64(nlh: &mut Header, type_: u16, data: u64)

Adds a 64-bit unsigned integer attribute to a netlink message

§Parameters
  • nlh - Netlink message header to add the attribute to
  • type_ - Type of the attribute
  • data - The 64-bit unsigned integer value to add
Source

pub fn put_str(nlh: &mut Header, type_: u16, data: *const i8)

Adds a string attribute to a netlink message

§Parameters
  • nlh - Netlink message header to add the attribute to
  • type_ - Type of the attribute
  • data - Pointer to the null-terminated string to add
Source

pub fn put_strz(nlh: &mut Header, type_: u16, data: *const i8)

Adds a null-terminated string attribute to a netlink message

§Parameters
  • nlh - Netlink message header to add the attribute to
  • type_ - Type of the attribute
  • data - Pointer to the string to add (null terminator will be included)
Source

pub fn nest_start(nlh: &mut Header, type_: u16) -> *mut Self

Starts a nested attribute in a netlink message

Returns a pointer to the nested attribute header for later use.

§Parameters
  • nlh - Netlink message header to add the nested attribute to
  • type_ - Type of the attribute with the NESTED flag set
§Returns

Pointer to the nested attribute, to be used with nest_end or nest_cancel

Source

pub fn nest_end(nlh: &mut Header, start: *mut Self)

Finalizes a nested attribute by updating its length

§Parameters
  • nlh - Netlink message header containing the nested attribute
  • start - Pointer to the nested attribute as returned by nest_start
Source

pub fn nest_cancel(nlh: &mut Header, start: *mut Self)

Cancels a nested attribute by rolling back the message length

§Parameters
  • nlh - Netlink message header containing the nested attribute
  • start - Pointer to the nested attribute as returned by nest_start

Trait Implementations§

Source§

impl Clone for Attribute

Source§

fn clone(&self) -> Attribute

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Attribute

Source§

impl Debug for Attribute

Source§

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

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

impl Eq for Attribute

Source§

impl Hash for Attribute

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 Ord for Attribute

Source§

fn cmp(&self, other: &Attribute) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Attribute

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PartialOrd for Attribute

Source§

fn partial_cmp(&self, other: &Attribute) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Attribute

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.