Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 531af086 authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk Committed by Android (Google) Code Review
Browse files

Merge "Remove libnl++ dependency on NETLINK_ROUTE."

parents 53529262 390c7110
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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 {

+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include <libnl++/NetlinkSocket.h>

#include <linux/can.h>
#include <linux/rtnetlink.h>
#include <net/if.h>

namespace android::netdevice {
+2 −0
Original line number Diff line number Diff line
@@ -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) {
+11 −8
Original line number Diff line number Diff line
@@ -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
+10 −11
Original line number Diff line number Diff line
@@ -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);
    }
};
Loading