In linux/netlink. h, some macros are defined to facilitate message processing. These macros include:
# Define NLMSG_ALIGNTO 4 # define NLMSG_ALIGN (len) + NLMSG_ALIGNTO-1 )&~ (NLMSG_ALIGNTO-1 )) |
The macro NLMSG_ALIGN (len) is used to obtain the minimum value not smaller than len and the byte align.
# Define NLMSG_LENGTH (len) + NLMSG_ALIGN (sizeof (struct nlmsghdr ))) |
The macro NLMSG_LENGTH (len) is used to calculate the actual message length when the data part length is len. It is generally used to allocate message cache.
# Define nlmsg_space (LEN) nlmsg_align (nlmsg_length (LEN )) |
The macro NLMSG_SPACE (len) returns the minimum value not less than NLMSG_LENGTH (len) and the byte alignment. It is also used to allocate message cache.
# Define nlmsg_data (NLH) (void *) (char *) NLH) + nlmsg_length (0 ))) |
The macro NLMSG_DATA (nlh) is used to obtain the first address of the Data part of the message. This macro is used to set and read the data part of the message.
# Define nlmsg_next (NLH, Len) (LEN)-= nlmsg_align (NLH)-> nlmsg_len),/(struct nlmsghdr *) (char *) (NLH )) + nlmsg_align (NLH)-> nlmsg_len ))) |
Macro NLMSG_NEXT (nlh, len) is used to obtain the first address of the next message, and len is also reduced to the total length of the remaining message, this macro is generally used when a message is divided into several parts for sending or receiving.
# Define nlmsg_ OK (NLH, Len) (LEN)> = (INT) sizeof (struct nlmsghdr) &/(NLH)-> nlmsg_len> = sizeof (struct nlmsghdr) &/(NLH)-> nlmsg_len <= (LEN )) |
The macro NLMSG_ OK (nlh, len) is used to determine whether the message has the length of len.
# Define nlmsg_payload (NLH, Len) (NLH)-> nlmsg_len-nlmsg_space (LEN ))) |
Macro NLMSG_PAYLOAD (nlh, len) is used to return the length of payload.
Function close is used to close the opened netlink socket.