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

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

Merge changes I971b8b81,I08d88256 into main

* changes:
  Implement netdevice::rename
  libnl++: fix std::string_view handling
parents 684414c9 460adf5a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -80,7 +80,7 @@ bool setBitrate(std::string_view ifname, uint32_t bitrate) {

    {
        auto linkinfo = req.addNested(IFLA_LINKINFO);
        req.addBuffer(IFLA_INFO_KIND, "can");
        req.add(IFLA_INFO_KIND, "can");
        {
            auto infodata = req.addNested(IFLA_INFO_DATA);
            /* For CAN FD, it would require to add IFLA_CAN_DATA_BITTIMING
+9 −0
Original line number Diff line number Diff line
@@ -160,6 +160,15 @@ bool add(std::string_view dev, std::string_view type);
 */
bool del(std::string_view dev);

/**
 * Rename interface.
 *
 * \param from the name of the interface to rename from
 * \param to the name of the interface to rename to
 * \return true in case of success, false otherwise
 */
bool rename(std::string_view from, std::string_view to);

/**
 * Fetches interface's hardware address.
 *
+19 −1
Original line number Diff line number Diff line
@@ -126,6 +126,10 @@ bool addAddr4(std::string_view ifname, std::string_view addr, uint8_t prefixlen)
    req->ifa_prefixlen = prefixlen;
    req->ifa_flags = IFA_F_SECONDARY;
    req->ifa_index = nametoindex(ifname);
    if (req->ifa_index == 0) {
        LOG(ERROR) << "Interface " << ifname << " doesn't exist";
        return false;
    }

    auto addrn = inetAddr(addr);
    req.add(IFLA_ADDRESS, addrn);
@@ -141,7 +145,7 @@ bool add(std::string_view dev, std::string_view type) {

    {
        auto linkinfo = req.addNested(IFLA_LINKINFO);
        req.addBuffer(IFLA_INFO_KIND, type);
        req.add(IFLA_INFO_KIND, type);
    }

    nl::Socket sock(NETLINK_ROUTE);
@@ -156,6 +160,20 @@ bool del(std::string_view dev) {
    return sock.send(req) && sock.receiveAck(req);
}

bool rename(std::string_view from, std::string_view to) {
    nl::MessageFactory<ifinfomsg> req(RTM_SETLINK);
    req.add(IFLA_IFNAME, to);

    req->ifi_index = nametoindex(from);
    if (req->ifi_index == 0) {
        LOG(ERROR) << "Interface " << from << " doesn't exist";
        return false;
    }

    nl::Socket sock(NETLINK_ROUTE);
    return sock.send(req) && sock.receiveAck(req);
}

std::optional<hwaddr_t> getHwAddr(std::string_view ifname) {
    auto ifr = ifreqs::fromName(ifname);
    if (!ifreqs::send(SIOCGIFHWADDR, ifr)) return std::nullopt;
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ bool add(std::string_view eth, std::string_view vlan, uint16_t id) {

    {
        auto linkinfo = req.addNested(IFLA_LINKINFO);
        req.addBuffer(IFLA_INFO_KIND, "vlan");
        req.add(IFLA_INFO_KIND, "vlan");

        {
            auto linkinfo = req.addNested(IFLA_INFO_DATA);
+8 −11
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <linux/netlink.h>

#include <string>
#include <type_traits>

namespace android::nl {

@@ -109,25 +110,21 @@ class MessageFactory : private MessageFactoryBase {
     */
    template <class A>
    void add(nlattrtype_t type, const A& attr) {
        addInternal(type, &attr, sizeof(attr));
        static_assert(std::is_pod_v<A>, "POD type required");
        add(type, &attr, sizeof(attr));
    }

    // It will always send the last null character, otherwise use addBuffer
    // variant instead
    template <>
    void add(nlattrtype_t type, const std::string& s) {
        addInternal(type, s.c_str(), s.size() + 1);
        add(type, s.c_str(), s.size());
    }

    void addBuffer(nlattrtype_t type, const std::string_view& s) {
        addInternal(type, s.data(), s.size());
    }
    void add(nlattrtype_t type, std::string_view s) { add(type, s.data(), s.size()); }

    /** Guard class to frame nested attributes. \see addNested(nlattrtype_t). */
    class [[nodiscard]] NestedGuard {
      public:
        NestedGuard(MessageFactory& req, nlattrtype_t type)
            : mReq(req), mAttr(req.addInternal(type)) {}
        NestedGuard(MessageFactory& req, nlattrtype_t type) : mReq(req), mAttr(req.add(type)) {}
        ~NestedGuard() { closeNested(&mReq.mMessage.header, mAttr); }

      private:
@@ -148,7 +145,7 @@ class MessageFactory : private MessageFactoryBase {
     *    MessageFactory<ifinfomsg> req(RTM_NEWLINK, NLM_F_REQUEST);
     *    {
     *        auto linkinfo = req.addNested(IFLA_LINKINFO);
     *        req.addBuffer(IFLA_INFO_KIND, "can");
     *        req.add(IFLA_INFO_KIND, "can");
     *        {
     *            auto infodata = req.addNested(IFLA_INFO_DATA);
     *            req.add(IFLA_CAN_BITTIMING, bitTimingStruct);
@@ -164,7 +161,7 @@ class MessageFactory : private MessageFactoryBase {
    Message mMessage = {};
    bool mIsGood = true;

    nlattr* addInternal(nlattrtype_t type, const void* data = nullptr, size_t len = 0) {
    nlattr* add(nlattrtype_t type, const void* data = nullptr, size_t len = 0) {
        if (!mIsGood) return nullptr;
        auto attr = MessageFactoryBase::add(&mMessage.header, sizeof(mMessage), type, data, len);
        if (attr == nullptr) mIsGood = false;