Remove libnl++ dependency on NETLINK_ROUTE.
One exception is for RTA_* definitions at NetlinkRequest. This class is
going to be refactored anyway, so let's focus on this later.
While we're here, also replace type assumptions with an actual
decltype lookup.
Bug: 162032964
Test: canhalctrl up test virtual vcan123
Change-Id: I3b509fa7b1870d4de7302fb5221a06613dfbb817
diff --git a/automotive/can/1.0/default/libnetdevice/can.cpp b/automotive/can/1.0/default/libnetdevice/can.cpp
index 0aa5afe..c6f1b04 100644
--- a/automotive/can/1.0/default/libnetdevice/can.cpp
+++ b/automotive/can/1.0/default/libnetdevice/can.cpp
@@ -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 {
diff --git a/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp b/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp
index 04381f2..aeb5005 100644
--- a/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp
+++ b/automotive/can/1.0/default/libnetdevice/libnetdevice.cpp
@@ -24,6 +24,7 @@
#include <libnl++/NetlinkSocket.h>
#include <linux/can.h>
+#include <linux/rtnetlink.h>
#include <net/if.h>
namespace android::netdevice {
diff --git a/automotive/can/1.0/default/libnetdevice/vlan.cpp b/automotive/can/1.0/default/libnetdevice/vlan.cpp
index bcc9345..e419154 100644
--- a/automotive/can/1.0/default/libnetdevice/vlan.cpp
+++ b/automotive/can/1.0/default/libnetdevice/vlan.cpp
@@ -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) {
diff --git a/automotive/can/1.0/default/libnl++/NetlinkRequest.cpp b/automotive/can/1.0/default/libnl++/NetlinkRequest.cpp
index e9463d1..b12489c 100644
--- a/automotive/can/1.0/default/libnl++/NetlinkRequest.cpp
+++ b/automotive/can/1.0/default/libnl++/NetlinkRequest.cpp
@@ -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 @@
}
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
diff --git a/automotive/can/1.0/default/libnl++/include/libnl++/NetlinkRequest.h b/automotive/can/1.0/default/libnl++/include/libnl++/NetlinkRequest.h
index 3b30a2a..29c3601 100644
--- a/automotive/can/1.0/default/libnl++/include/libnl++/NetlinkRequest.h
+++ b/automotive/can/1.0/default/libnl++/include/libnl++/NetlinkRequest.h
@@ -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 @@
/** 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 @@
* \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 @@
/** 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 @@
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);
}
};
diff --git a/automotive/can/1.0/default/libnl++/include/libnl++/types.h b/automotive/can/1.0/default/libnl++/include/libnl++/types.h
index d2f10ff..567590b 100644
--- a/automotive/can/1.0/default/libnl++/include/libnl++/types.h
+++ b/automotive/can/1.0/default/libnl++/include/libnl++/types.h
@@ -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