C语言跨平台获取ip、mac

C语言跨平台获取ip、mac

C语言获取ip、mac地址等信息一直没有一个跨平台的接口,
之前通过gethostnamegethostbyname尝试,却只能得到127.0.0.1 localhost的ip地址,
不得不自己封装一个,Windows、unix平台下分别实现

在Windows平台下使用IPHlpApi.h提供的GetAdaptersAddressesGetAdaptersInfo
在unix平台使用<sys/ioctl.h>提供的ioctl

两种平台做了统一的封装和对最后结果集的过滤,源码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <vector>

typedef struct ifconfig_s {
    char name[128];
    char ip[32];
    char mask[32];
    char mac[32];
} ifconfig_t;

#ifdef __unix__
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <arpa/inet.h>

int ifconfig(std::vector<ifconfig_t>& ifcs) {
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
        return -10;
    }

    struct ifconf ifc;
    char buf[1024];
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;

    int iRet = ioctl(sock, SIOCGIFCONF, &ifc);
    if (iRet != 0) {
        close(sock);
        return iRet;
    }

    int cnt = ifc.ifc_len / sizeof(struct ifreq);
    //printf("ifc.size=%d\n", cnt);
    if (cnt == 0) {
        close(sock);
        return -20;
    }

    struct ifreq ifr;
    ifcs.clear();
    ifconfig_t tmp;
    char macaddr[32] = {'\0'};
    for (int i = 0; i < cnt; ++i) {
        // name
        strcpy(ifr.ifr_name, ifc.ifc_req[i].ifr_name);
        //printf("name: %s\n", ifr.ifr_name);
        strncpy(tmp.name, ifr.ifr_name, sizeof(tmp.name));
        // flags
        //iRet = ioctl(sock, SIOCGIFFLAGS, &ifr);
        //short flags = ifr.ifr_flags;
        // addr
        iRet = ioctl(sock, SIOCGIFADDR, &ifr);
        struct sockaddr_in* addr = (struct sockaddr_in*)&ifr.ifr_addr;
        char* ip = inet_ntoa(addr->sin_addr);
        //printf("ip: %s\n", ip);
        strncpy(tmp.ip, ip, sizeof(tmp.ip));
        // netmask
        iRet = ioctl(sock, SIOCGIFNETMASK, &ifr);
        addr = (struct sockaddr_in*)&ifr.ifr_netmask;
        char* netmask = inet_ntoa(addr->sin_addr);
        //printf("netmask: %s\n", netmask);
        strncpy(tmp.mask, netmask, sizeof(tmp.mask));
        // broadaddr
        //iRet = ioctl(sock, SIOCGIFBRDADDR, &ifr);
        //addr = (struct sockaddr_in*)&ifr.ifr_broadaddr;
        //char* broadaddr = inet_ntoa(addr->sin_addr);
        //printf("broadaddr: %s\n", broadaddr);
        // hwaddr
        iRet = ioctl(sock, SIOCGIFHWADDR, &ifr);
        snprintf(macaddr, sizeof(macaddr), "%02x:%02x:%02x:%02x:%02x:%02x",
            (unsigned char)ifr.ifr_hwaddr.sa_data[0],
            (unsigned char)ifr.ifr_hwaddr.sa_data[1],
            (unsigned char)ifr.ifr_hwaddr.sa_data[2],
            (unsigned char)ifr.ifr_hwaddr.sa_data[3],
            (unsigned char)ifr.ifr_hwaddr.sa_data[4],
            (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
        //printf("mac: %s\n", macaddr);
        strncpy(tmp.mac, macaddr, sizeof(tmp.mac));
        //printf("\n");

        if (strcmp(tmp.ip, "0.0.0.0") == 0 ||
            strcmp(tmp.ip, "127.0.0.1") == 0 ||
            strcmp(tmp.mac, "00:00:00:00:00:00") == 0) {
            continue;
        }

        ifcs.push_back(tmp);
    }

    close(sock);
    return 0;
}
#elif defined(_WIN32)
#include <winsock2.h>
#include <windows.h>
#include <IphlpApi.h>
#ifdef _MSC_VER
#pragma comment(lib, "Iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#endif
int ifconfig(std::vector<ifconfig_t>& ifcs) {
    PIP_ADAPTER_ADDRESSES pAddrs = NULL;
    ULONG buflen = 0;
    GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);
    pAddrs = (PIP_ADAPTER_ADDRESSES)malloc(buflen);
    GetAdaptersAddresses(AF_INET, 0, NULL, pAddrs, &buflen);

    PIP_ADAPTER_INFO pInfos = NULL;
    buflen = 0;
    GetAdaptersInfo(pInfos, &buflen);
    pInfos = (PIP_ADAPTER_INFO)malloc(buflen);
    GetAdaptersInfo(pInfos, &buflen);

    ifconfig_t ifc;
    std::vector<ifconfig_t> tmp_ifcs;
    PIP_ADAPTER_ADDRESSES pAddr = pAddrs;
    char mac[32] = {'\0'};
    while (pAddr) {
        snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
            pAddr->PhysicalAddress[0],
            pAddr->PhysicalAddress[1],
            pAddr->PhysicalAddress[2],
            pAddr->PhysicalAddress[3],
            pAddr->PhysicalAddress[4],
            pAddr->PhysicalAddress[5]);

        memset(&ifc, 0, sizeof(ifc));
        strncpy(ifc.name, pAddr->AdapterName, sizeof(ifc.name));
        strncpy(ifc.ip, "0.0.0.0", sizeof(ifc.ip));
        strncpy(ifc.mask, "0.0.0.0", sizeof(ifc.ip));
        strncpy(ifc.mac, mac, sizeof(ifc.mac));
        tmp_ifcs.push_back(ifc);

        pAddr = pAddr->Next;
    }

    PIP_ADAPTER_INFO pInfo = pInfos;
    while (pInfo) {
        for (auto& item : tmp_ifcs) {
            if (strcmp(item.name, pInfo->AdapterName) == 0) {
                // description more friendly
                strncpy(item.name, pInfo->Description, sizeof(item.name));
                strncpy(item.ip, pInfo->IpAddressList.IpAddress.String, sizeof(item.ip));
                strncpy(item.mask, pInfo->IpAddressList.IpMask.String, sizeof(item.mask));
            }
        }

        pInfo = pInfo->Next;
    }

    free(pAddrs);
    free(pInfos);

    // filter
    ifcs.clear();
    for (auto& item : tmp_ifcs) {
        if (strcmp(item.ip, "0.0.0.0") == 0 ||
            strcmp(item.ip, "127.0.0.1") == 0 ||
            strcmp(item.mac, "00:00:00:00:00:00") == 0) {
            continue;
        }
        ifcs.push_back(item);
    }

    return 0;
}
#else
int ifconfig(std::vector<ifconfig_t>& ifcs) {
    return -10; // unimplemented
}
#endif

int main(int argc, char** argv) {
    std::vector<ifconfig_t> ifcs;
    ifconfig(ifcs);
    for (auto& item : ifcs) {
        printf("%s\nip: %s\nmask: %s\nmac: %s\n\n",
                item.name,
                item.ip,
                item.mask,
                item.mac);
    }
    return 0;
}


linux下输出如下:

eth0
ip: 10.1.10.111
mask: 255.255.255.0
mac: 08:00:27:13:46:0e

windows下输出如下:

Intel(R) Dual Band Wireless-AC 3165
ip: 10.1.10.134
mask: 255.255.255.0
mac: 74:e5:f9:59:18:66
ithewei CSDN认证博客专家 c/c++ Qt libhv
编程之路,其路漫漫,吾将上下而求索
https://github.com/ithewei
https://hewei.blog.csdn.net
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页