文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>获取本机的ip地址以及mac地址

获取本机的ip地址以及mac地址

时间:2010-05-23  来源:julysee

 

#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>


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);
                        break;
                case 2: //ipmask

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

                        if (ioctl(inet_sock, SIOCGIFHWADDR, &ifr)< 0)
                        {
                                perror("ioctl");
                        }
                        
                        ptr = &ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
                        
                        printf("\nrequest 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 *mac;
      mac = get_interface("eth0",3);
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载