2009-09-29

Netlink and RTnetlink manpage dump

This, gentlemans [sic], is a dump.

#define NLMSG_ALIGNTO   4   


DESCRIPTION
<linux/netlink.h> defines several standard macros to access or create a netlink datagram. They are similar in spirit to the macros defined in cmsg(3)
for auxiliary data. The buffer passed to and from a netlink socket should only be accessed using these macros.

int NLMSG_ALIGN(size_t len);
Round the length of a netlink message up to align it properly.

#define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) )


int NLMSG_LENGTH(size_t len);
Given the payload length, len, this macro returns the aligned length to store in the nlmsg_len field of the nlmsghdr.

#define NLMSG_LENGTH(len) ((len)+NLMSG_ALIGN(NLMSG_HDRLEN))


int NLMSG_SPACE(size_t len);
Return the number of bytes that a netlink message with payload of len would occupy.

#define NLMSG_SPACE(len) NLMSG_ALIGN(NLMSG_LENGTH(len))


void *NLMSG_DATA(struct nlmsghdr *nlh);
Return a pointer to the payload associated with the passed nlmsghdr.

#define NLMSG_DATA(nlh) ((void*)(((char*)nlh) + NLMSG_LENGTH(0)))


struct nlmsghdr *NLMSG_NEXT(struct nlmsghdr *nlh, int len);
Get the next nlmsghdr in a multipart message. The caller must check if the current nlmsghdr didn't have the NLMSG_DONE set � this function
doesn't return NULL on end. The len argument is an lvalue containing the remaining length of the message buffer. This macro decrements it by
the length of the message header.

#define NLMSG_NEXT(nlh,len) ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \
(struct nlmsghdr*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))


int NLMSG_OK(struct nlmsghdr *nlh, int len);
Return true if the netlink message is not truncated and ok to parse.

#define NLMSG_OK(nlh,len) ((len) >= (int)sizeof(struct nlmsghdr) && \
(nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
(nlh)->nlmsg_len <= (len))



int NLMSG_PAYLOAD(struct nlmsghdr *nlh, int len);
Return the length of the payload associated with the nlmsghdr.

#define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - NLMSG_SPACE((len)))


#define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))




--------------------------------------------------------------------------------------------


rtnetlink_socket = socket(AF_NETLINK, int socket_type, NETLINK_ROUTE);

#define RTA_ALIGNTO 4


All rtnetlink(7) messages consist of a netlink(7) message header and appended attributes. The attributes should be only manipulated using the macros
provided here.


#define RTA_ALIGN(len) ( ((len)+RTA_ALIGNTO-1) & ~(RTA_ALIGNTO-1) )


unsigned int RTA_LENGTH(unsigned int length);
returns the length which is required for len bytes of data plus the header.
------------------Given the payload length, len, this macro returns the aligned length to store in the nlmsg_len field of the nlmsghdr.

#define RTA_LENGTH(len) (RTA_ALIGN(sizeof(struct rtattr)) + (len))


unsigned int RTA_SPACE(unsigned int length);
returns the amount of space which will be needed in a message with len bytes of data.

#define RTA_SPACE(len) RTA_ALIGN(RTA_LENGTH(len))


void *RTA_DATA(struct rtattr *rta);
returns a pointer to the start of this attribute's data.

#define RTA_DATA(rta) ((void*)(((char*)(rta)) + RTA_LENGTH(0)))


struct rtattr *RTA_NEXT(struct rtattr *rta, unsigned int rtabuflen);
gets the next attribute after rta. Calling this macro will update attrlen. You should use RTA_OK to check the validity of the
returned pointer.

#define RTA_NEXT(rta,attrlen) ((attrlen) -= RTA_ALIGN((rta)->rta_len), \
(struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))



int RTA_OK(struct rtattr *rta, int rtabuflen);
returns true if rta points to a valid routing attribute; attrlen is the running length of the attribute buffer. When not true
then you must assume there are no more attributes in the message, even if attrlen is non-zero.

#define RTA_OK(rta,len) ((len) >= (int)sizeof(struct rtattr) && \
(rta)->rta_len >= sizeof(struct rtattr) && \
(rta)->rta_len <= (len))




unsigned int RTA_PAYLOAD(struct rtattr *rta);
returns the length of this attribute's data.

#define RTA_PAYLOAD(rta) ((int)((rta)->rta_len) - RTA_LENGTH(0))








----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
--------------------COMPARISON----------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------


rtnetlink_socket = socket(AF_NETLINK, int socket_type, NETLINK_ROUTE);

#define NLMSG_ALIGNTO 4
#define RTA_ALIGNTO 4


All rtnetlink(7) messages consist of a netlink(7) message header and appended attributes. The attributes should be only manipulated using the macros
provided here.


DESCRIPTION
<linux/netlink.h> defines several standard macros to access or create a netlink datagram. They are similar in spirit to the macros defined in cmsg(3)
for auxiliary data. The buffer passed to and from a netlink socket should only be accessed using these macros.

int NLMSG_ALIGN(size_t len);
Round the length of a netlink message up to align it properly.

#define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) )
#define RTA_ALIGN(len) ( ((len)+RTA_ALIGNTO-1) & ~(RTA_ALIGNTO-1) )


int NLMSG_LENGTH(size_t len);
Given the payload length, len, this macro returns the aligned length to store in the nlmsg_len field of the nlmsghdr.

#define NLMSG_LENGTH(len) ((len)+NLMSG_ALIGN(NLMSG_HDRLEN))
#define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))

unsigned int RTA_LENGTH(unsigned int length);
returns the length which is required for len bytes of data plus the header.

#define RTA_LENGTH(len) ((len)+RTA_ALIGN(sizeof(struct rtattr)))


int NLMSG_SPACE(size_t len);
Return the number of bytes that a netlink message with payload of len would occupy.

#define NLMSG_SPACE(len) NLMSG_ALIGN(NLMSG_LENGTH(len))

unsigned int RTA_SPACE(unsigned int length);
returns the amount of space which will be needed in a message with len bytes of data.

#define RTA_SPACE(len) RTA_ALIGN(RTA_LENGTH(len))


void *NLMSG_DATA(struct nlmsghdr *nlh);
Return a pointer to the payload associated with the passed nlmsghdr.

#define NLMSG_DATA(nlh) ((void*)(((char*)nlh) + NLMSG_LENGTH(0)))

void *RTA_DATA(struct rtattr *rta);
returns a pointer to the start of this attribute's data.

#define RTA_DATA(rta) ((void*)(((char*)(rta)) + RTA_LENGTH(0)))


struct nlmsghdr *NLMSG_NEXT(struct nlmsghdr *nlh, int len);
Get the next nlmsghdr in a multipart message. The caller must check if the current nlmsghdr didn't have the NLMSG_DONE set � this function
doesn't return NULL on end. The len argument is an lvalue containing the remaining length of the message buffer. This macro decrements it by
the length of the message header.

#define NLMSG_NEXT(nlh,len) ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \
(struct nlmsghdr*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))



struct rtattr *RTA_NEXT(struct rtattr *rta, unsigned int rtabuflen);
gets the next attribute after rta. Calling this macro will update attrlen. You should use RTA_OK to check the validity of the
returned pointer.

#define RTA_NEXT(rta,attrlen) ((attrlen) -= RTA_ALIGN((rta)->rta_len), \
(struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))



int NLMSG_OK(struct nlmsghdr *nlh, int len);
Return true if the netlink message is not truncated and ok to parse.

#define NLMSG_OK(nlh,len) ((len) >= (int)sizeof(struct nlmsghdr) && \
(nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
(nlh)->nlmsg_len <= (len))


int RTA_OK(struct rtattr *rta, int rtabuflen);
returns true if rta points to a valid routing attribute; attrlen is the running length of the attribute buffer. When not true
then you must assume there are no more attributes in the message, even if attrlen is non-zero.

#define RTA_OK(rta,len) ((len) >= (int)sizeof(struct rtattr) && \
(rta)->rta_len >= sizeof(struct rtattr) && \
(rta)->rta_len <= (len))




int NLMSG_PAYLOAD(struct nlmsghdr *nlh, int len);
Return the length of the payload associated with the nlmsghdr.

#define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - NLMSG_SPACE((len)))

unsigned int RTA_PAYLOAD(struct rtattr *rta);
returns the length of this attribute's data.

#define RTA_PAYLOAD(rta) ((int)((rta)->rta_len) - RTA_LENGTH(0))




#define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - NLMSG_SPACE((len)))
#define NLMSG_SPACE(len) NLMSG_ALIGN(NLMSG_LENGTH(len))
#define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - NLMSG_ALIGN(NLMSG_LENGTH(len)))
#define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) )
#define NLMSG_LENGTH(len) ((len)+NLMSG_ALIGN(NLMSG_HDRLEN))
#define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
#define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - NLMSG_ALIGN(NLMSG_LENGTH(len)))
Assumindo len=0
#define NLMSG_PAYLOAD(nlh,len) ((nlh)->nlmsg_len - struct nlmsghdr - qqcoisa)

#define RTA_PAYLOAD(rta) ((int)((rta)->rta_len) - RTA_LENGTH(0))
#define RTA_LENGTH(len) ((len)+RTA_ALIGN(sizeof(struct rtattr)))

#define RTA_PAYLOAD(rta) ((int)((rta)->rta_len) - RTA_ALIGN(sizeof(struct rtattr)))


It makes sense to me. It may make sense to someone else. Ignore if the first three lines make no sense to you. Thank you.

Thanks to c2html.

3 comments:

Sintra said...

Tou seriamente a pensar em apagar este post.

Claymore said...

LOL
Macros rulam :P

Pedro Francisco said...

Especialmente qd pco documentadas :p