You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
940 B
23 lines
940 B
#pragma once
|
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
template<class Request>
|
|
inline void addRouterAttribute(Request& r,
|
|
int type,
|
|
const void* data,
|
|
size_t size) {
|
|
// Calculate the offset into the character buffer where the RTA data lives
|
|
// We use offsetof on the buffer to get it. This avoids undefined behavior
|
|
// by casting the buffer (which is safe because it's char) instead of the
|
|
// Request struct.(which is undefined because of aliasing)
|
|
size_t offset = NLMSG_ALIGN(r.hdr.nlmsg_len) - offsetof(Request, buf);
|
|
auto attr = reinterpret_cast<struct rtattr*>(r.buf + offset);
|
|
attr->rta_type = type;
|
|
attr->rta_len = RTA_LENGTH(size);
|
|
memcpy(RTA_DATA(attr), data, size);
|
|
|
|
// Update the message length to include the router attribute.
|
|
r.hdr.nlmsg_len = NLMSG_ALIGN(r.hdr.nlmsg_len) + RTA_ALIGN(attr->rta_len);
|
|
}
|