现在的位置: 首页 > 综合 > 正文

取ios本地ip及物理mac地址

2013年09月07日 ⁄ 综合 ⁄ 共 3786字 ⁄ 字号 评论关闭

两个方法,(网上整理得来,原作不详----heqin)

方法一:

在该文件中加入

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <unistd.h>  
  5. #include <sys/ioctl.h>  
  6. #include <sys/types.h>  
  7. #include <sys/socket.h>  
  8. #include <netinet/in.h>  
  9. #include <netdb.h>  
  10. #include <arpa/inet.h>  
  11. #include <sys/sockio.h>  
  12. #include <net/if.h>  
  13. #include <errno.h>  
  14. #include <net/if_dl.h>  
  15.   
  16. //#include "GetAddresses.h"  
  17.   
  18. #define min(a,b)    ((a) < (b) ? (a) : (b))  
  19. #define max(a,b)    ((a) > (b) ? (a) : (b))  
  20.   
  21. #define BUFFERSIZE  4000
  22. // added by heqin, 原来的代码中没有下面这一行去定义MAXADDRS
     
 #define MAXADDRS 32
  1. char *if_names[MAXADDRS];  
  2. char *ip_names[MAXADDRS];  
  3. char *hw_addrs[MAXADDRS];  
  4. unsigned long ip_addrs[MAXADDRS];  
  5.   
  6. static int   nextAddr = 0;  
  7.   
  8. void InitAddresses()  
  9. {  
  10.     int i;  
  11.     for (i=0; i<MAXADDRS; ++i)  
  12.     {  
  13.         if_names[i] = ip_names[i] = hw_addrs[i] = NULL;  
  14.         ip_addrs[i] = 0;  
  15.     }  
  16. }  
  17.   
  18. void FreeAddresses()  
  19. {  
  20.     int i;  
  21.     for (i=0; i<MAXADDRS; ++i)  
  22.     {  
  23.         if (if_names[i] != 0) free(if_names[i]);  
  24.         if (ip_names[i] != 0) free(ip_names[i]);  
  25.         if (hw_addrs[i] != 0) free(hw_addrs[i]);  
  26.         ip_addrs[i] = 0;  
  27.     }  
  28.     InitAddresses();  
  29. }  
  30.   
  31. void GetIPAddresses()  
  32. {  
  33.     int                 i, len, flags;  
  34.     char                buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;  
  35.     struct ifconf       ifc;  
  36.     struct ifreq        *ifr, ifrcopy;  
  37.     struct sockaddr_in  *sin;  
  38.       
  39.     char temp[80];  
  40.       
  41.     int sockfd;  
  42.       
  43.     for (i=0; i<MAXADDRS; ++i)  
  44.     {  
  45.         if_names[i] = ip_names[i] = NULL;  
  46.         ip_addrs[i] = 0;  
  47.     }  
  48.       
  49.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);  
  50.     if (sockfd < 0)  
  51.     {  
  52.         perror("socket failed");  
  53.         return;  
  54.     }  
  55.       
  56.     ifc.ifc_len = BUFFERSIZE;  
  57.     ifc.ifc_buf = buffer;  
  58.       
  59.     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)  
  60.     {  
  61.         perror("ioctl error");  
  62.         return;  
  63.     }  
  64.       
  65.     lastname[0] = 0;  
  66.       
  67.     for (ptr = buffer; ptr < buffer + ifc.ifc_len; )  
  68.     {  
  69.         ifr = (struct ifreq *)ptr;  
  70.         len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);  
  71.         ptr += sizeof(ifr->ifr_name) + len;  // for next one in buffer  
  72.           
  73.         if (ifr->ifr_addr.sa_family != AF_INET)  
  74.         {  
  75.             continue;   // ignore if not desired address family  
  76.         }  
  77.           
  78.         if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)  
  79.         {  
  80.             *cptr = 0;      // replace colon will null  
  81.         }  
  82.           
  83.         if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)  
  84.         {  
  85.             continue;   /* already processed this interface */  
  86.         }  
  87.           
  88.         memcpy(lastname, ifr->ifr_name, IFNAMSIZ);  
  89.           
  90.         ifrcopy = *ifr;  
  91.         ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);  
  92.         flags = ifrcopy.ifr_flags;  
  93.         if ((flags & IFF_UP) == 0)  
  94.         {  
  95.             continue;   // ignore if interface not up  
  96.         }  
  97.           
  98.         if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);  
  99.         if (if_names[nextAddr] == NULL)  
  100.         {  
  101.             return;  
  102.         }  
  103.         strcpy(if_names[nextAddr], ifr->ifr_name);  
  104.           
  105.         sin = (struct sockaddr_in *)&ifr->ifr_addr;  
  106.         strcpy(temp, inet_ntoa(sin->sin_addr));  
  107.           
  108.         ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);  
  109.         if (ip_names[nextAddr] == NULL)  
  110.         {  
  111.             return;  
  112.         }  
  113.         strcpy(ip_names[nextAddr], temp);  
  114.           
  115.         ip_addrs[nextAddr] = sin->sin_addr.s_addr;  
  116.           
  117.         ++nextAddr;  
  118.     }  
  119.       
  120.     close(sockfd);  
  121. }  
  122.   
  123. void GetHWAddresses()  
  124. {  
  125.     struct ifconf ifc;  
  126.     struct ifreq *ifr;  
  127.     int i, sockfd;  
  128.     char buffer[BUFFERSIZE], *cp, *cplim;  
  129.     char temp[80];  
  130.       
  131.     for (i=0; i<MAXADDRS; ++i)  
  132.     {  
  133.     

抱歉!评论已关闭.