C++ 上的设备网络

Devices networking on C++

我需要访问我设备上的网络接口。我有以下代码可以执行此操作,但我遗漏了那些处于活动状态但没有关联 IP 地址的代码。知道如何以编程方式从这些信息中收集信息吗?

贴出的代码有从接口获取信息的函数,列出所有接口的函数和测试的main。

谢谢

struct iface {
    std::string name;
    std::string address;
    std::string netmask;
    std::string broadcast;
    std::string hwaddr;
    int mtu;
};

void print_iface(iface s) {
    std::cout << s.name << ": <UP, RUNNING>" << std::endl;
    std::cout << "  mtu: " << s.mtu << std::endl;
    std::cout << "  hwaddr: " << s.hwaddr << " " << std::endl;
    std::cout << "  inet: " << s.address << " " << std::endl;
    std::cout << "  netmask: " << s.netmask << " " << std::endl;
    std::cout << "  broadcast: " << s.broadcast << std::endl;
}

std::tuple<char*, int> get_active_interfaces() {
    int socketfd;
    struct ifconf conf;
    char data[4096];

    socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    conf.ifc_len = sizeof(data);
    conf.ifc_buf = (caddr_t) data;
    if (ioctl(socketfd, SIOCGIFCONF, &conf) < 0) {
        fprintf(stderr, "ioctl error: %s", std::strerror(errno));
        return std::make_tuple(nullptr, 0);
    } else {
        return std::make_tuple(data, conf.ifc_len);
    }
}

std::string get_ifname(struct ifreq *ifr) {
    return ifr->ifr_name;
}

std::string get_ipv4_addr(struct ifreq *ifr) {
    std::string addr(45, 0);
    inet_ntop(ifr->ifr_addr.sa_family,
            &((struct sockaddr_in*) &ifr->ifr_addr)->sin_addr, &addr[0],
            addr.size());
    return addr;
}

#define MAC_TEMPLATE "%02X:%02X:%02X:%02X:%02X:%02X"
#define LOOPBACK_TEMPLATE "LOOPBAK INTERFACE"

std::string get_mac_addr(struct ifreq *ifr) {
    std::string addr;

    u_int8_t hd[6];
    struct ifreq ifl;
    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sock >= 0) {
        strcpy(ifl.ifr_name, ifr->ifr_name);
        if (ioctl(sock, SIOCGIFFLAGS, &ifl) == 0) {
            if (!(ifl.ifr_flags & IFF_LOOPBACK)) {
                if (ioctl(sock, SIOCGIFHWADDR, &ifl) == 0) {
                    memcpy(hd, ifl.ifr_hwaddr.sa_data, sizeof(hd));
                    size_t n = snprintf(nullptr, 0, MAC_TEMPLATE, hd[0], hd[1],
                            hd[2], hd[3], hd[4], hd[5]);
                    addr.resize(n + 1, 0);
                    sprintf(&addr[0], MAC_TEMPLATE, hd[0], hd[1], hd[2], hd[3],
                            hd[4], hd[5]);
                }
            } else {
                addr = LOOPBACK_TEMPLATE;
            }
        }
    }

    return addr;
}

std::string get_netmask(struct ifreq *ifr) {
    std::string addr(45, 0);

    struct ifreq ifl;
    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sock >= 0) {
        strcpy(ifl.ifr_name, ifr->ifr_name);
        if (ioctl(sock, SIOCGIFNETMASK, &ifl) == 0) {
            inet_ntop(ifl.ifr_addr.sa_family,
                    &((struct sockaddr_in*) &ifl.ifr_addr)->sin_addr, &addr[0],
                    addr.size());
        }
    }
    return addr;
}

std::string get_broadcast(struct ifreq *ifr) {
    std::string addr(45, 0);

    struct ifreq ifl;
    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sock >= 0) {
        strcpy(ifl.ifr_name, ifr->ifr_name);
        if (ioctl(sock, SIOCGIFFLAGS, &ifl) == 0) {
            if (!(ifl.ifr_flags & IFF_LOOPBACK)) {
                if (ioctl(sock, SIOCGIFBRDADDR, &ifl) == 0) {
                    inet_ntop(ifl.ifr_addr.sa_family,
                            &((struct sockaddr_in*) &ifl.ifr_addr)->sin_addr,
                            &addr[0], addr.size());
                }
            } else {
                addr = LOOPBACK_TEMPLATE;
            }
        }
    }
    return addr;
}

int get_mtu(struct ifreq *ifr) {
    int mtu = 0;

    struct ifreq ifl;
    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sock >= 0) {
        strcpy(ifl.ifr_name, ifr->ifr_name);
        if (ioctl(sock, SIOCGIFMTU, &ifl) == 0) {
            mtu = ifl.ifr_mtu;
        }
    }
    return mtu;
}

/**
 *  Return a list of interface (network layer) addresses.
 *  Accept or return only AF_INET socket addresses are
 *  IP-specific and perhaps should rather be documented in ip(7).
 *  [https://www.man7.org/linux/man-pages/man7/ip.7.html]
 *  This currently means only addresses of the AF_INET (IPv4)
 *  family for compatibility.
 *  The names of interfaces with no addresses or that don't have the
 *  IFF_RUNNING flag set can be found via /proc/net/dev.
 */
std::vector<iface> get_ifce_list() {
    std::vector<iface> ifaces;
    int socketfd;

    printf("Opening socket...");
    socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (socketfd >= 0) {
        printf(" OK\n");
        printf("Discovering interfaces...\n");
        auto data = get_active_interfaces();

        for (struct ifreq *ifr = (struct ifreq*) std::get<0>(data);
                (char*) ifr < std::get<0>(data) + std::get<1>(data); ifr++) {
            switch (ifr->ifr_addr.sa_family) {
            case AF_INET: {
                iface ifc;
                ifc.name = get_ifname(ifr);
                ifc.address = get_ipv4_addr(ifr);
                ifc.netmask = get_netmask(ifr);
                ifc.broadcast = get_broadcast(ifr);
                ifc.hwaddr = get_mac_addr(ifr);
                ifc.mtu = get_mtu(ifr);

                ifaces.emplace_back(ifc);
        
                break;
            }
#if USE_IPV6
                case AF_INET6:

                    break;
    #endif
            }
        }
        close(socketfd);
    } else {
        printf(" Failed!\n");
    }

    return ifaces;
}

// This is the main function

int main() {
    std::time_t start = std::time(nullptr), stop;
    std::vector<iface> _ifaces = get_ifce_list();
    for (auto &_iface : _ifaces) {
        print_iface(_iface);
    }
    stop = std::time(nullptr);

    std::cout << "End of code. ran for: " << stop - start << " seconds. "
            << std::endl;
    return 0;
}

此代码在几个方面存在问题:

std::tuple<char*, int> get_active_interfaces() {
    int socketfd;
    struct ifconf conf;
    char data[4096];

    socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    conf.ifc_len = sizeof(data);
    conf.ifc_buf = (caddr_t) data;
    if (ioctl(socketfd, SIOCGIFCONF, &conf) < 0) {
        fprintf(stderr, "ioctl error: %s", std::strerror(errno));
        return std::make_tuple(nullptr, 0);
    } else {
        return std::make_tuple(data, conf.ifc_len);
    }
}

基本问题是你return的元组包含一个指向data的指针,这是一个局部变量。在函数外部使用该指针都是错误的。

IIUC,你想做的是遍历 ifc_req,它被定义为不超过 ifc_len 字节长的接口列表。

根据我的 Ubuntu 手册页,最通用的方法是首先使用 ifc_req == NULL 进行一次调用,在这种情况下,系统将使用所需的大小填充 ifc_len .计算元素的数量并使用

从空闲存储中分配
size_t N = ifc_len / sizeof(ifc_req[0]);
conf.ifc_req = new ifreq(N);

我会将函数重新定义为return std::tuple<struct ifreq *, size_t>,并将second的含义更改为元素数,而不是字节数。这样,调用者就可以围绕一个结构数组进行组织,并且您已经完成了对字节和强制转换的处理。由于您使用的是 C++,因此您可以非常巧妙地使用 std::for_each 遍历数组。