1 #pragma once 2 3 #include <linux/rtnetlink.h> 4 5 template <class Request> addRouterAttribute(Request & r,int type,const void * data,size_t size)6inline void addRouterAttribute(Request& r, int type, const void* data, size_t size) { 7 // Calculate the offset into the character buffer where the RTA data lives 8 // We use offsetof on the buffer to get it. This avoids undefined behavior 9 // by casting the buffer (which is safe because it's char) instead of the 10 // Request struct.(which is undefined because of aliasing) 11 size_t offset = NLMSG_ALIGN(r.hdr.nlmsg_len) - offsetof(Request, buf); 12 auto attr = reinterpret_cast<struct rtattr*>(r.buf + offset); 13 attr->rta_type = type; 14 attr->rta_len = RTA_LENGTH(size); 15 memcpy(RTA_DATA(attr), data, size); 16 17 // Update the message length to include the router attribute. 18 r.hdr.nlmsg_len = NLMSG_ALIGN(r.hdr.nlmsg_len) + RTA_ALIGN(attr->rta_len); 19 } 20