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_: u16Implementations§
Source§impl Attribute
impl Attribute
Sourcepub const NET_BYTEORDER: i32
pub const NET_BYTEORDER: i32
Flag bit indicating the attribute value is in network byte order
Sourcepub const data_type_len: [usize; 5]
pub const data_type_len: [usize; 5]
Size lookup table for standard data types
Sourcepub fn is_empty(&self) -> bool
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
Sourcepub fn reset(&mut self)
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.
Sourcepub fn for_each(&mut self, nlh: &mut Header, offset: usize)
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 attributesoffset- Offset in bytes from the start of the payload to the first attribute
Sourcepub fn for_each_nested(&mut self) -> *mut Self
pub fn for_each_nested(&mut self) -> *mut Self
Sourcepub fn for_each_payload(&mut self, payload_size: usize) -> *mut Self
pub fn for_each_payload(&mut self, payload_size: usize) -> *mut Self
Sourcepub fn get_type(&self) -> u16
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
Sourcepub fn len(&self) -> u16
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
Sourcepub fn get_payload_len(&self) -> u16
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
Sourcepub fn get_payload(&self) -> *mut ()
pub fn get_payload(&self) -> *mut ()
Gets pointer to the attribute payload
§Returns
A mutable pointer to the attribute’s payload (value part)
Sourcepub fn is_ok(&self, len: i32) -> bool
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
Sourcepub fn next(&self) -> *mut Self
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
Sourcepub fn type_valid(&self, max: u16) -> i32
pub fn type_valid(&self, max: u16) -> i32
Sourcepub fn __validate(&self, type_: DataType, exp_len: usize) -> i32
pub fn __validate(&self, type_: DataType, exp_len: usize) -> i32
Sourcepub fn validate(&self, type_: DataType) -> i32
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
Sourcepub fn parse(
nlh: &mut Header,
offset: u32,
cb: attr_callback_t,
data: *mut u8,
) -> i32
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 parseoffset- Offset in bytes from the start of the payload to the first attributecb- Callback function to call for each attributedata- User data to pass to the callback function
§Returns
Status code from the last callback, or error code
Sourcepub fn parse_nested(&mut self, cb: attr_callback_t, data: *mut u8) -> i32
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 attributedata- User data to pass to the callback function
§Returns
Status code from the last callback, or error code
Sourcepub fn parse_payload(
&mut self,
payload_len: usize,
cb: attr_callback_t,
data: *mut u8,
) -> i32
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 bytescb- Callback function to call for each attributedata- User data to pass to the callback function
§Returns
Status code from the last callback, or error code
Sourcepub fn get_u8(&self) -> u8
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
Sourcepub fn get_u16(&self) -> u16
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
Sourcepub fn get_u32(&self) -> u32
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
Sourcepub fn get_u64(&self) -> u64
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
Sourcepub fn get_uint(&self) -> u64
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
Sourcepub fn get_str(&self) -> *const i8
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
Sourcepub fn put(nlh: &mut Header, type_: u16, len: usize, data: *const u8)
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 totype_- Type of the attributelen- Length of the attribute payload in bytesdata- Pointer to the attribute payload data
Sourcepub fn put_u8(nlh: &mut Header, type_: u16, data: u8)
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 totype_- Type of the attributedata- The 8-bit unsigned integer value to add
Sourcepub fn put_u16(nlh: &mut Header, type_: u16, data: u16)
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 totype_- Type of the attributedata- The 16-bit unsigned integer value to add
Sourcepub fn put_u32(nlh: &mut Header, type_: u16, data: u32)
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 totype_- Type of the attributedata- The 32-bit unsigned integer value to add
Sourcepub fn put_u64(nlh: &mut Header, type_: u16, data: u64)
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 totype_- Type of the attributedata- The 64-bit unsigned integer value to add
Sourcepub fn put_str(nlh: &mut Header, type_: u16, data: *const i8)
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 totype_- Type of the attributedata- Pointer to the null-terminated string to add
Sourcepub fn put_strz(nlh: &mut Header, type_: u16, data: *const i8)
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 totype_- Type of the attributedata- Pointer to the string to add (null terminator will be included)
Sourcepub fn nest_start(nlh: &mut Header, type_: u16) -> *mut Self
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 totype_- Type of the attribute with the NESTED flag set
§Returns
Pointer to the nested attribute, to be used with nest_end or nest_cancel
Sourcepub fn nest_end(nlh: &mut Header, start: *mut Self)
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 attributestart- Pointer to the nested attribute as returned bynest_start
Sourcepub fn nest_cancel(nlh: &mut Header, start: *mut Self)
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 attributestart- Pointer to the nested attribute as returned bynest_start