获取iphone ip 非私有方法 iphone xcode

 
 
  1. /*  
  2.  *  IPAdress.h  
  3.  *  
  4.  *  
  5.  */  
  6.    
  7. #define MAXADDRS    32  
  8.    
  9. extern char *if_names[MAXADDRS];  
  10. extern char *ip_names[MAXADDRS];  
  11. extern char *hw_addrs[MAXADDRS];  
  12. extern unsigned long ip_addrs[MAXADDRS];  
  13.    
  14. // Function prototypes  
  15.    
  16. void InitAddresses();  
  17. void FreeAddresses();  
  18. void GetIPAddresses();  
  19. void GetHWAddresses();  
  20.    
  21.    
  22. /*  
  23.  *  IPAddress.c  
  24.  *  
  25.  */  
  26.    
  27. #include "IPAddress.h"  
  28.    
  29. #include <stdio.h> 
  30. #include <stdlib.h> 
  31. #include <string.h> 
  32. #include <unistd.h> 
  33. #include <sys/ioctl.h> 
  34. #include <sys/types.h> 
  35. #include <sys/socket.h> 
  36. #include <netinet/in.h> 
  37. #include <netdb.h> 
  38. #include <arpa/inet.h> 
  39. #include <sys/sockio.h> 
  40. #include <net/if.h> 
  41. #include <errno.h> 
  42. #include <net/if_dl.h> 
  43.     
  44. #define    min(a,b)    ((a) < (b) ? (a) : (b))  
  45. #define    max(a,b)    ((a) > (b) ? (a) : (b))  
  46.    
  47. #define BUFFERSIZE    4000  
  48.    
  49. char *if_names[MAXADDRS];  
  50. char *ip_names[MAXADDRS];  
  51. char *hw_addrs[MAXADDRS];  
  52. unsigned long ip_addrs[MAXADDRS];  
  53.    
  54. static int   nextAddr = 0;  
  55.    
  56. void InitAddresses()  
  57. {  
  58.     int i;  
  59.     for (i=0; i<MAXADDRS; ++i)  
  60.     {  
  61.         if_names[i] = ip_names[i] = hw_addrs[i] = NULL;  
  62.         ip_addrs[i] = 0;  
  63.     }  
  64. }  
  65.    
  66. void FreeAddresses()  
  67. {  
  68.     int i;  
  69.     for (i=0; i<MAXADDRS; ++i)  
  70.     {  
  71.         if (if_names[i] != 0) free(if_names[i]);  
  72.         if (ip_names[i] != 0) free(ip_names[i]);  
  73.         if (hw_addrs[i] != 0) free(hw_addrs[i]);  
  74.         ip_addrs[i] = 0;  
  75.     }  
  76.     InitAddresses();  
  77. }  
  78.    
  79. void GetIPAddresses()  
  80. {  
  81.     int                 i, len, flags;  
  82.     char                buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;  
  83.     struct ifconf       ifc;  
  84.     struct ifreq        *ifr, ifrcopy;  
  85.     struct sockaddr_in    *sin;  
  86.       
  87.     char temp[80];  
  88.       
  89.     int sockfd;  
  90.       
  91.     for (i=0; i<MAXADDRS; ++i)  
  92.     {  
  93.         if_names[i] = ip_names[i] = NULL;  
  94.         ip_addrs[i] = 0;  
  95.     }  
  96.       
  97.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);  
  98.     if (sockfd < 0)  
  99.     {  
  100.         perror("socket failed");  
  101.         return;  
  102.     }  
  103.       
  104.     ifc.ifc_len = BUFFERSIZE;  
  105.     ifc.ifc_buf = buffer;  
  106.       
  107.     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)  
  108.     {  
  109.         perror("ioctl error");  
  110.         return;  
  111.     }  
  112.       
  113.     lastname[0] = 0;  
  114.       
  115.     for (ptr = buffer; ptr < buffer + ifc.ifc_len; )  
  116.     {  
  117.         ifr = (struct ifreq *)ptr;  
  118.         len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);  
  119.         ptr += sizeof(ifr->ifr_name) + len;    // for next one in buffer  
  120.           
  121.         if (ifr->ifr_addr.sa_family != AF_INET)  
  122.         {  
  123.             continue;    // ignore if not desired address family  
  124.         }  
  125.           
  126.         if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)  
  127.         {  
  128.             *cptr = 0;        // replace colon will null  
  129.         }  
  130.           
  131.         if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)  
  132.         {  
  133.             continue;    /* already processed this interface */  
  134.         }  
  135.           
  136.         memcpy(lastname, ifr->ifr_name, IFNAMSIZ);  
  137.           
  138.         ifrcopy = *ifr;  
  139.         ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);  
  140.         flags = ifrcopy.ifr_flags;  
  141.         if ((flags & IFF_UP) == 0)  
  142.         {  
  143.             continue;    // ignore if interface not up  
  144.         }  
  145.           
  146.         if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);  
  147.         if (if_names[nextAddr] == NULL)  
  148.         {  
  149.             return;  
  150.         }  
  151.         strcpy(if_names[nextAddr], ifr->ifr_name);  
  152.           
  153.         sin = (struct sockaddr_in *)&ifr->ifr_addr;  
  154.         strcpy(temp, inet_ntoa(sin->sin_addr));  
  155.           
  156.         ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);  
  157.         if (ip_names[nextAddr] == NULL)  
  158.         {  
  159.             return;  
  160.         }  
  161.         strcpy(ip_names[nextAddr], temp);  
  162.           
  163.         ip_addrs[nextAddr] = sin->sin_addr.s_addr;  
  164.           
  165.         ++nextAddr;  
  166.     }  
  167.       
  168.     close(sockfd);  
  169. }  
  170.    
  171. void GetHWAddresses()  
  172. {  
  173.     struct ifconf ifc;  
  174.     struct ifreq *ifr;  
  175.     int i, sockfd;  
  176.     char buffer[BUFFERSIZE], *cp, *cplim;  
  177.     char temp[80];  
  178.       
  179.     for (i=0; i<MAXADDRS; ++i)  
  180.     {  
  181.         hw_addrs[i] = NULL;  
  182.     }  
  183.       
  184.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);  
  185.     if (sockfd < 0)  
  186.     {  
  187.         perror("socket failed");  
  188.         return;  
  189.     }  
  190.       
  191.     ifc.ifc_len = BUFFERSIZE;  
  192.     ifc.ifc_buf = buffer;  
  193.       
  194.     if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)  
  195.     {  
  196.         perror("ioctl error");  
  197.         close(sockfd);  
  198.         return;  
  199.     }  
  200.       
  201.     ifr = ifc.ifc_req;  
  202.       
  203.     cplim = buffer + ifc.ifc_len;  
  204.       
  205.     for (cp=buffer; cp < cplim; )  
  206.     {  
  207.         ifr = (struct ifreq *)cp;  
  208.         if (ifr->ifr_addr.sa_family == AF_LINK)  
  209.         {  
  210.             struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;  
  211.             int a,b,c,d,e,f;  
  212.             int i;  
  213.               
  214.             strcpy(temp, (char *)ether_ntoa(LLADDR(sdl)));  
  215.             sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);  
  216.             sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);  
  217.               
  218.             for (i=0; i<MAXADDRS; ++i)  
  219.             {  
  220.                 if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name,if_names[i]) == 0))  
  221.                 {  
  222.                     if (hw_addrs[i] == NULL)  
  223.                     {  
  224.                         hw_addrs[i] = (char *)malloc(strlen(temp)+1);  
  225.                         strcpy(hw_addrs[i], temp);  
  226.                         break;  
  227.                     }  
  228.                 }  
  229.             }  
  230.         }  
  231.         cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);  
  232.     }  
  233.       
  234.     close(sockfd);  
  235. }  
  236. test:  
  237.  
  238. #import "IPAdress.h"  
  239.  
  240. - (NSString *)deviceIPAdress {  
  241.     InitAddresses();  
  242.     GetIPAddresses();  
  243.     GetHWAddresses();  
  244.     return [NSString stringWithFormat:@"%s", ip_names[1]];  
  245. }  
  246.    
  247. - (void)viewDidLoad {  
  248.     [super viewDidLoad];  
  249.    
  250.     NSString* ip_iphone = [self deviceIPAdress];  
  251.    NSLog(@"ip:%@",ip_iphone);  

小结:获取iPhone本机IP地址非调用私有API方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值