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

Commit 460adf5a authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk
Browse files

Implement netdevice::rename

Bug: 372814636
Test: m
Change-Id: I971b8b81caf2fea2db6551d2727c57c3a9f8f7f9
parent b141ef6f
Loading
Loading
Loading
Loading
+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.
 *
+18 −0
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);
@@ -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;