libmnl 1.0.5
|
Functions | |
size_t | mnl_nlmsg_size (size_t len) |
size_t | mnl_nlmsg_get_payload_len (const struct nlmsghdr *nlh) |
struct nlmsghdr * | mnl_nlmsg_put_header (void *buf) |
void * | mnl_nlmsg_put_extra_header (struct nlmsghdr *nlh, size_t size) |
void * | mnl_nlmsg_get_payload (const struct nlmsghdr *nlh) |
void * | mnl_nlmsg_get_payload_offset (const struct nlmsghdr *nlh, size_t offset) |
bool | mnl_nlmsg_ok (const struct nlmsghdr *nlh, int len) |
struct nlmsghdr * | mnl_nlmsg_next (const struct nlmsghdr *nlh, int *len) |
void * | mnl_nlmsg_get_payload_tail (const struct nlmsghdr *nlh) |
bool | mnl_nlmsg_seq_ok (const struct nlmsghdr *nlh, unsigned int seq) |
bool | mnl_nlmsg_portid_ok (const struct nlmsghdr *nlh, unsigned int portid) |
void | mnl_nlmsg_fprintf (FILE *fd, const void *data, size_t datalen, size_t extra_header_size) |
Netlink message:
|<----------------- 4 bytes ------------------->| |<----- 2 bytes ------>|<------- 2 bytes ------>| |-----------------------------------------------| | Message length (including header) | |-----------------------------------------------| | Message type | Message flags | |-----------------------------------------------| | Message sequence number | |-----------------------------------------------| | Netlink PortID | |-----------------------------------------------| | | . Payload . |_______________________________________________|
There is usually an extra header after the the Netlink header (at the beginning of the payload). This extra header is specific of the Netlink subsystem. After this extra header, it comes the sequence of attributes that are expressed in Type-Length-Value (TLV) format.
void mnl_nlmsg_fprintf | ( | FILE * | fd, |
const void * | data, | ||
size_t | datalen, | ||
size_t | extra_header_size | ||
) |
mnl_nlmsg_fprintf - print netlink message to file
fd | pointer to file type |
data | pointer to the buffer that contains messages to be printed |
datalen | length of data stored in the buffer |
extra_header_size | size of the extra header (if any) |
This function prints the netlink header to a file handle. It may be useful for debugging purposes. One example of the output is the following:
---------------- ------------------ | 0000000040 | | message length | | 00016 | R-A- | | type | flags | | 1289148991 | | sequence number| | 0000000000 | | port ID | ---------------- ------------------ | 00 00 00 00 | | extra header | | 00 00 00 00 | | extra header | | 01 00 00 00 | | extra header | | 01 00 00 00 | | extra header | |00008|--|00003| |len |flags| type| | 65 74 68 30 | | data | e t h 0 ---------------- ------------------
This example above shows the netlink message that is send to kernel-space to set up the link interface eth0. The netlink and attribute header data are displayed in base 10 whereas the extra header and the attribute payload are expressed in base 16. The possible flags in the netlink header are:
The lack of one flag is displayed with '-'. On the other hand, the possible attribute flags available are:
void * mnl_nlmsg_get_payload | ( | const struct nlmsghdr * | nlh | ) |
size_t mnl_nlmsg_get_payload_len | ( | const struct nlmsghdr * | nlh | ) |
void * mnl_nlmsg_get_payload_offset | ( | const struct nlmsghdr * | nlh, |
size_t | offset | ||
) |
mnl_nlmsg_get_payload_offset - get a pointer to the payload of the message
nlh | pointer to a netlink header |
offset | offset to the payload of the attributes TLV set |
This function returns a pointer to the payload of the netlink message plus a given offset.
void * mnl_nlmsg_get_payload_tail | ( | const struct nlmsghdr * | nlh | ) |
mnl_nlmsg_get_payload_tail - get the ending of the netlink message
nlh | pointer to netlink message |
This function returns a pointer to the netlink message tail. This is useful to build a message since we continue adding attributes at the end of the message.
struct nlmsghdr * mnl_nlmsg_next | ( | const struct nlmsghdr * | nlh, |
int * | len | ||
) |
mnl_nlmsg_next - get the next netlink message in a multipart message
nlh | current netlink message that we are handling |
len | length of the remaining bytes in the buffer (passed by reference). |
This function returns a pointer to the next netlink message that is part of a multi-part netlink message. Netlink can batch several messages into one buffer so that the receiver has to iterate over the whole set of Netlink messages.
You have to use mnl_nlmsg_ok() to check if the next Netlink message is valid.
bool mnl_nlmsg_ok | ( | const struct nlmsghdr * | nlh, |
int | len | ||
) |
mnl_nlmsg_ok - check a there is room for netlink message
nlh | netlink message that we want to check |
len | remaining bytes in a buffer that contains the netlink message |
This function is used to check that a buffer that contains a netlink message has enough room for the netlink message that it stores, ie. this function can be used to verify that a netlink message is not malformed nor truncated.
This function does not set errno in case of error since it is intended for iterations. Thus, it returns true on success and false on error.
The len parameter may become negative in malformed messages during message iteration, that is why we use a signed integer.
bool mnl_nlmsg_portid_ok | ( | const struct nlmsghdr * | nlh, |
unsigned int | portid | ||
) |
mnl_nlmsg_portid_ok - perform portID origin check
nlh | current netlink message that we are handling |
portid | netlink portid that we want to check |
This functions returns true if the origin is fulfilled, otherwise false is returned. We skip the tracking for netlink message whose portID is zero since it is reserved for event-based kernel notifications. On the other hand, if portid is set but the message PortID is not (i.e. this is an event message coming from kernel-space), then we also skip the tracking. This approach is good if we use the same socket to send commands to kernel-space (that we want to track) and to listen to events (that we do not track).
void * mnl_nlmsg_put_extra_header | ( | struct nlmsghdr * | nlh, |
size_t | size | ||
) |
mnl_nlmsg_put_extra_header - reserve and prepare room for an extra header
nlh | pointer to Netlink header |
size | size of the extra header that we want to put |
This function sets to zero the room that is required to put the extra header after the initial Netlink header. This function also increases the nlmsg_len field. You have to invoke mnl_nlmsg_put_header() before you call this function. This function returns a pointer to the extra header.
struct nlmsghdr * mnl_nlmsg_put_header | ( | void * | buf | ) |
mnl_nlmsg_put_header - reserve and prepare room for Netlink header
buf | memory already allocated to store the Netlink header |
This function sets to zero the room that is required to put the Netlink header in the memory buffer passed as parameter. This function also initializes the nlmsg_len field to the size of the Netlink header. This function returns a pointer to the Netlink header structure.
bool mnl_nlmsg_seq_ok | ( | const struct nlmsghdr * | nlh, |
unsigned int | seq | ||
) |
mnl_nlmsg_seq_ok - perform sequence tracking
nlh | current netlink message that we are handling |
seq | last sequence number used to send a message |
This functions returns true if the sequence tracking is fulfilled, otherwise false is returned. We skip the tracking for netlink messages whose sequence number is zero since it is usually reserved for event-based kernel notifications. On the other hand, if seq is set but the message sequence number is not set (i.e. this is an event message coming from kernel-space), then we also skip the tracking. This approach is good if we use the same socket to send commands to kernel-space (that we want to track) and to listen to events (that we do not track).