• Get MAC address using POSIX APIs


    #include <stdio.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <sys/ioctl.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <net/if.h>

    #define MAXINTERFACES 16

    void get_ip()
    {
        int sock_fd;
        struct ifreq buf[MAXINTERFACES];
        struct ifconf ifc;
        int interface_num;
        char *addr;//[ADDR_LEN];

        if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
            printf("Create socket failed");
            return;
        }
        ifc.ifc_len = sizeof(buf);
        ifc.ifc_req = buf;
        if(ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc) < 0)
        {
            printf("Get a list of interface addresses failed");
            return;
        }
        
        interface_num = ifc.ifc_len / sizeof(struct ifreq);
        printf("The number of interfaces is %d ", interface_num);

        while(interface_num--) 
        {
            printf("Net device: %s ", buf[interface_num].ifr_name);

            if(ioctl(sock_fd, SIOCGIFFLAGS, (char *)&buf[interface_num]) < 0)
            {
                printf("Get the active flag word of the device");
                continue;
            }
            if(buf[interface_num].ifr_flags & IFF_PROMISC)
                printf("Interface is in promiscuous mode ");

            if(buf[interface_num].ifr_flags & IFF_UP)
                printf("Interface is running ");
            else
                printf("Interface is not running ");

            if(ioctl(sock_fd, SIOCGIFADDR, (char *)&buf[interface_num]) < 0)
            {
                printf("Get interface address failed");
                continue;
            }
            addr = inet_ntoa(((struct sockaddr_in*)(&buf[interface_num].ifr_addr))->sin_addr);
            printf("IP address is %s ", addr);  
        }

    close(sock_fd);
        return;
    }

    void get_mac()
    {
        int sock_fd;
        struct ifreq buf[MAXINTERFACES];
        struct ifconf ifc;
        int interface_num;
        
        if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
            printf("Create socket failed");
            return;
        }
        ifc.ifc_len = sizeof(buf);
        ifc.ifc_req = buf;
        if(ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc) < 0)
        {
            printf("Get a list of interface addresses failed");
            return;
        }
        interface_num = ifc.ifc_len / sizeof(struct ifreq);
        printf("The number of interfaces is %d ", interface_num);

        while(interface_num--) 
        {
            printf("Net device: %s ", buf[interface_num].ifr_name);

            if(ioctl(sock_fd, SIOCGIFFLAGS, (char *)&buf[interface_num]) < 0)
            {
                printf("Get the active flag word of the device");
                continue;
            }
            if(buf[interface_num].ifr_flags & IFF_PROMISC)
                printf("Interface is in promiscuous mode ");

            if(buf[interface_num].ifr_flags & IFF_UP)
                printf("Interface is running ");
            else
                printf("Interface is not running ");

            if(ioctl(sock_fd, SIOCGIFHWADDR, (char *)&buf[interface_num]) < 0)
            {
                printf("Get the hardware address of a device failed");
                continue;
            }

            printf("Mac address is: %02X:%02X:%02X:%02X:%02X:%02X ",
                (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[0],
                (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[1],
                (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[2],
                (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[3],
                (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[4],
                (unsigned char)buf[interface_num].ifr_hwaddr.sa_data[5]);
        }

       close(sock_fd);
    }

  • 相关阅读:
    HyperV应用指南之4虚拟机管理[转]
    Windows Server 2003文件夹不能共享的解决办法【转】
    彻底了解DVD:从入门到精通(二)[转]
    HyperV应用指南之2--安装HyperV Server 2008 R2并配置远程管理[转]
    HyperV应用指南之HyperV应用基础[转]
    IIS7.5由于权限不足而无法读取配置文件的解决办法
    C# 十六进制字符串与数值类型之间转换(转)
    分享一个Winform下的分页控件[转]
    mysql的replace函数替换字符串功能简介
    聊聊.net程序设计——浅谈使用VS2010建模拓展(下)[转]
  • 原文地址:https://www.cnblogs.com/len3d/p/4225417.html
Copyright © 2020-2023  润新知