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

#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
#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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值