Linux 获取本机IP地址和MAC地址(一)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MAXINTERFACES 16
#define MAC_ADDR_LEN 18
#define ADDR_LEN 16

void err_quit(char *msg);
char *getIP(char *addr);
char *getMAC(char *mac_addr);

int main(int argc, char *argv[])
{
    char addr[ADDR_LEN];
    char mac_addr[MAC_ADDR_LEN];

    printf("\n****************************************\n");

    getIP(addr);

    printf("\n****************************************\n");

    getMAC(mac_addr);

    printf("\n****************************************\n");

    return 0;
}

void err_quit(char *msg)
{
    perror(msg);
    exit(1);
}

char *getIP(char *addr)
{
    int sock_fd;
    struct ifreq buf[MAXINTERFACES];
    struct ifconf ifc;
    int interface_num;

    if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        err_quit("Create socket failed");

    ifc.ifc_len = sizeof(buf);
    ifc.ifc_req = buf;
    if(ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc) < 0)
        err_quit("Get a list of interface addresses failed");

    interface_num = ifc.ifc_len / sizeof(struct ifreq);
    printf("The number of interfaces is %d\n", interface_num);

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

        if(ioctl(sock_fd, SIOCGIFFLAGS, (char *)&buf[interface_num]) < 0)
            err_quit("Get the active flag word of the device");

        if(buf[interface_num].ifr_flags & IFF_PROMISC)
            printf("Interface is in promiscuous mode\n");

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

        if(ioctl(sock_fd, SIOCGIFADDR, (char *)&buf[interface_num]) < 0)
            err_quit("Get interface address failed");

        addr = inet_ntoa(((struct sockaddr_in*)(&buf[interface_num].ifr_addr))->sin_addr);
        printf("IP address is %s\n", addr);   
    }

  return addr;
}

char *getMAC(char *mac_addr)
{
    int sock_fd;
    struct ifreq buf[MAXINTERFACES];
    struct ifconf ifc;
    int interface_num;

    if((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        err_quit("Create socket failed");

    ifc.ifc_len = sizeof(buf);
    ifc.ifc_req = buf;
    if(ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc) < 0)
        err_quit("Get a list of interface addresses failed");

    interface_num = ifc.ifc_len / sizeof(struct ifreq);
    printf("The number of interfaces is %d\n", interface_num);

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

        if(ioctl(sock_fd, SIOCGIFFLAGS, (char *)&buf[interface_num]) < 0)
            err_quit("Get the active flag word of the device");

        if(buf[interface_num].ifr_flags & IFF_PROMISC)
            printf("Interface is in promiscuous mode\n");

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

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

        snprintf(mac_addr, MAC_ADDR_LEN, "%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]);

        printf("Mac address is %s\n", mac_addr);
    }

    return mac_addr; 
}

 
 
总结:struct ifreq 和 struct ifconf 以及ioctl各个命名说明见下篇文章,unix网络编程卷一的第17章 ioctl操作也有详细的命令描述。

使用下面代码获取的IP地址是127.0.0.1

char * getip_byhostname(char *addr)
{
        char name[NAME_LEN];
        struct hostent *host;
        char **addr_list;

        if(gethostname(name, NAME_LEN) == -1)
                err_quit("Get host name failed");

        printf("The host name is %s\n", name);

        host = gethostbyname(name);

        for(addr_list = host->h_addr_list; *addr_list != NULL; addr_list++) {
              if(inet_ntop(host->h_addrtype, *addr_list, addr, ADDR_LEN) == NULL)
                        err_quit("Get host address failed");
        }

        printf("The host address is %s\n", addr);

        return addr;
}



 

 

本文转载自:http://blog.chinaunix.net/uid-30441-id-2133830.html

你可能感兴趣的:(linux)