Chinaunix首页 | 论坛 | 博客
  • 博客访问: 336317
  • 博文数量: 90
  • 博客积分: 1585
  • 博客等级: 上尉
  • 技术积分: 964
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-26 18:33
文章分类

全部博文(90)

文章存档

2011年(10)

2010年(80)

我的朋友

分类: C/C++

2010-05-26 09:54:08

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

#define  ETH_NAME "eth0"

char * get_interface(char *interface, int type)
{
        int inet_sock;
        char *value= NULL;
        struct ifreq ifr;
        memset(&ifr, 0, sizeof(ifr));

        inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
       
        unsigned char *ptr;
        strcpy(ifr.ifr_name, interface);
       
        memset(&ifr.ifr_hwaddr, 0, sizeof(struct sockaddr));
        switch (type)
        {
                case 1: //ip

                        if (ioctl(inet_sock, SIOCGIFADDR, &ifr)< 0)
                        {
                                perror("ioctl");
                        }
                        value = inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr);
                        printf("IP: %s \n", value);   
                        break;
                case 2: //ipmask

                        if (ioctl(inet_sock, SIOCGIFNETMASK, &ifr)< 0)
                        {
                                perror("ioctl");
                        }
                        value = inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr);
                        printf("IPMask: %s \n", value);
                        break;
                case 3: //mac

                        if (ioctl(inet_sock, SIOCGIFHWADDR, &ifr)< 0)
                        {
                                perror("ioctl");
                        }
                       
                        ptr = &ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
                       
                        printf("Mac%02X: %02X:%02X:%02X:%02X:%02X\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
                        //printf ("ptr+3: %02x\n", ptr[3]);                       
                        break;
                default:
                        break;
        }       
        return value;
}

int main()
{
        char *ip, *ipmask, *mac;
        ip = get_interface(ETH_NAME, 1);
        ipmask = get_interface(ETH_NAME, 2);
        mac = get_interface(ETH_NAME, 3);
}
阅读(1098) | 评论(0) | 转发(0) |
0

上一篇:configure.ac

下一篇:C/C++题目

给主人留下些什么吧!~~