Merge "Remove libnl++ dependency on NETLINK_ROUTE."

This commit is contained in:
Tomasz Wasilczyk 2020-07-30 22:13:25 +00:00 committed by Android (Google) Code Review
commit 531af086fb
6 changed files with 28 additions and 23 deletions

View file

@ -27,6 +27,7 @@
#include <linux/can/error.h>
#include <linux/can/netlink.h>
#include <linux/can/raw.h>
#include <linux/rtnetlink.h>
namespace android::netdevice::can {

View file

@ -24,6 +24,7 @@
#include <libnl++/NetlinkSocket.h>
#include <linux/can.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
namespace android::netdevice {

View file

@ -22,6 +22,8 @@
#include <libnl++/NetlinkRequest.h>
#include <libnl++/NetlinkSocket.h>
#include <linux/rtnetlink.h>
namespace android::netdevice::vlan {
bool add(const std::string& eth, const std::string& vlan, uint16_t id) {

View file

@ -18,14 +18,17 @@
#include <android-base/logging.h>
// for RTA_ macros missing from NLA_ definitions
#include <linux/rtnetlink.h>
namespace android::nl::impl {
static struct rtattr* nlmsg_tail(struct nlmsghdr* n) {
return reinterpret_cast<struct rtattr*>( //
static struct nlattr* nlmsg_tail(struct nlmsghdr* n) {
return reinterpret_cast<struct nlattr*>( //
reinterpret_cast<uintptr_t>(n) + NLMSG_ALIGN(n->nlmsg_len));
}
struct rtattr* addattr_l(struct nlmsghdr* n, size_t maxLen, rtattrtype_t type, const void* data,
struct nlattr* addattr_l(struct nlmsghdr* n, size_t maxLen, nlattrtype_t type, const void* data,
size_t dataLen) {
size_t newLen = NLMSG_ALIGN(n->nlmsg_len) + RTA_SPACE(dataLen);
if (newLen > maxLen) {
@ -34,21 +37,21 @@ struct rtattr* addattr_l(struct nlmsghdr* n, size_t maxLen, rtattrtype_t type, c
}
auto attr = nlmsg_tail(n);
attr->rta_len = RTA_SPACE(dataLen);
attr->rta_type = type;
attr->nla_len = RTA_SPACE(dataLen);
attr->nla_type = type;
if (dataLen > 0) memcpy(RTA_DATA(attr), data, dataLen);
n->nlmsg_len = newLen;
return attr;
}
struct rtattr* addattr_nest(struct nlmsghdr* n, size_t maxLen, rtattrtype_t type) {
struct nlattr* addattr_nest(struct nlmsghdr* n, size_t maxLen, nlattrtype_t type) {
return addattr_l(n, maxLen, type, nullptr, 0);
}
void addattr_nest_end(struct nlmsghdr* n, struct rtattr* nest) {
void addattr_nest_end(struct nlmsghdr* n, struct nlattr* nest) {
size_t nestLen = reinterpret_cast<uintptr_t>(nlmsg_tail(n)) - reinterpret_cast<uintptr_t>(nest);
nest->rta_len = nestLen;
nest->nla_len = nestLen;
}
} // namespace android::nl::impl

View file

@ -19,7 +19,7 @@
#include <android-base/macros.h>
#include <libnl++/types.h>
#include <linux/rtnetlink.h>
#include <linux/netlink.h>
#include <string>
@ -28,11 +28,10 @@ namespace android::nl {
/** Implementation details, do not use outside NetlinkRequest template. */
namespace impl {
// TODO(twasilczyk): use nlattr instead of rtattr
struct rtattr* addattr_l(struct nlmsghdr* n, size_t maxLen, rtattrtype_t type, const void* data,
struct nlattr* addattr_l(struct nlmsghdr* n, size_t maxLen, nlattrtype_t type, const void* data,
size_t dataLen);
struct rtattr* addattr_nest(struct nlmsghdr* n, size_t maxLen, rtattrtype_t type);
void addattr_nest_end(struct nlmsghdr* n, struct rtattr* nest);
struct nlattr* addattr_nest(struct nlmsghdr* n, size_t maxLen, nlattrtype_t type);
void addattr_nest_end(struct nlmsghdr* n, struct nlattr* nest);
} // namespace impl
@ -82,14 +81,14 @@ struct NetlinkRequest {
* \param attr attribute data
*/
template <class A>
void addattr(rtattrtype_t type, const A& attr) {
void addattr(nlattrtype_t type, const A& attr) {
if (!mIsGood) return;
auto ap = impl::addattr_l(&mRequest.nlmsg, sizeof(mRequest), type, &attr, sizeof(attr));
if (ap == nullptr) mIsGood = false;
}
template <>
void addattr(rtattrtype_t type, const std::string& s) {
void addattr(nlattrtype_t type, const std::string& s) {
if (!mIsGood) return;
auto ap = impl::addattr_l(&mRequest.nlmsg, sizeof(mRequest), type, s.c_str(), s.size() + 1);
if (ap == nullptr) mIsGood = false;
@ -97,12 +96,12 @@ struct NetlinkRequest {
/** Guard class to frame nested attributes. See nest(int). */
struct Nest {
Nest(NetlinkRequest& req, rtattrtype_t type) : mReq(req), mAttr(req.nestStart(type)) {}
Nest(NetlinkRequest& req, nlattrtype_t type) : mReq(req), mAttr(req.nestStart(type)) {}
~Nest() { mReq.nestEnd(mAttr); }
private:
NetlinkRequest& mReq;
struct rtattr* mAttr;
struct nlattr* mAttr;
DISALLOW_COPY_AND_ASSIGN(Nest);
};
@ -142,14 +141,14 @@ struct NetlinkRequest {
bool mIsGood = true;
RequestData mRequest = {};
struct rtattr* nestStart(rtattrtype_t type) {
struct nlattr* nestStart(nlattrtype_t type) {
if (!mIsGood) return nullptr;
auto attr = impl::addattr_nest(&mRequest.nlmsg, sizeof(mRequest), type);
if (attr == nullptr) mIsGood = false;
return attr;
}
void nestEnd(struct rtattr* nest) {
void nestEnd(struct nlattr* nest) {
if (mIsGood && nest != nullptr) impl::addattr_nest_end(&mRequest.nlmsg, nest);
}
};

View file

@ -16,12 +16,11 @@
#pragma once
#include <linux/types.h>
#include <linux/netlink.h>
namespace android::nl {
typedef __u16 nlmsgtype_t; // nlmsghdr::nlmsg_type
typedef __u16 nlattrtype_t; // nlattr::nla_type
typedef unsigned short rtattrtype_t; // rtattr::rta_type
typedef decltype(nlmsghdr::nlmsg_type) nlmsgtype_t;
typedef decltype(nlattr::nla_type) nlattrtype_t;
} // namespace android::nl