得到iphone的IP地址源码 .

来源:互联网 发布:飞常准 航旅 知乎 编辑:程序博客网 时间:2024/06/06 03:36
 

定义了几个方法,获取iphone的IP地址。

IPAdress.h

01.#define MAXADDRS    32   02.extern char *if_names[MAXADDRS];  03.extern char *ip_names[MAXADDRS];  04.extern char *hw_addrs[MAXADDRS];  05.extern unsigned long ip_addrs[MAXADDRS];  06.// Function prototypes   07.void InitAddresses();  08.void FreeAddresses();  09.void GetIPAddresses();  10.void GetHWAddresses();  

IPAddress.c
01.#include <stdio.h>   02.#include <stdlib.h>   03.#include <string.h>   04.#include <unistd.h>   05.#include <sys/ioctl.h>   06.#include <sys/types.h>   07.#include <sys/socket.h>   08.#include <netinet/in.h>   09.#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.#include "GetAddresses.h"   16.#define min(a,b)    ((a) < (b) ? (a) : (b))   17.#define max(a,b)    ((a) > (b) ? (a) : (b))   18.#define BUFFERSIZE  4000   19.char *if_names[MAXADDRS];  20.char *ip_names[MAXADDRS];  21.char *hw_addrs[MAXADDRS];  22.unsigned long ip_addrs[MAXADDRS];  23.static int   nextAddr = 0;  24.void InitAddresses()  25.{  26.    int i;  27.    for (i=0; i<MAXADDRS; ++i)  28.    {  29.        if_names[i] = ip_names[i] = hw_addrs[i] = NULL;  30.        ip_addrs[i] = 0;  31.    }  32.}  33.void FreeAddresses()  34.{  35.    int i;  36.    for (i=0; i<MAXADDRS; ++i)  37.    {  38.        if (if_names[i] != 0) free(if_names[i]);  39.        if (ip_names[i] != 0) free(ip_names[i]);  40.        if (hw_addrs[i] != 0) free(hw_addrs[i]);  41.        ip_addrs[i] = 0;  42.    }  43.    InitAddresses();  44.}  45.void GetIPAddresses()  46.{  47.    int                 i, len, flags;  48.    char                buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;  49.    struct ifconf       ifc;  50.    struct ifreq        *ifr, ifrcopy;  51.    struct sockaddr_in  *sin;  52.    char temp[80];  53.    int sockfd;  54.    for (i=0; i<MAXADDRS; ++i)  55.    {  56.        if_names[i] = ip_names[i] = NULL;  57.        ip_addrs[i] = 0;  58.    }  59.    sockfd = socket(AF_INET, SOCK_DGRAM, 0);  60.    if (sockfd < 0)  61.    {  62.        perror("socket failed");  63.        return;  64.    }  65.      66.    ifc.ifc_len = BUFFERSIZE;  67.    ifc.ifc_buf = buffer;  68.      69.    if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)  70.    {  71.        perror("ioctl error");  72.        return;  73.    }  74.      75.    lastname[0] = 0;  76.      77.    for (ptr = buffer; ptr < buffer + ifc.ifc_len; )  78.    {  79.        ifr = (struct ifreq *)ptr;  80.        len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);  81.        ptr += sizeof(ifr->ifr_name) + len;  // for next one in buffer   82.      83.        if (ifr->ifr_addr.sa_family != AF_INET)  84.        {  85.            continue;   // ignore if not desired address family   86.        }  87.      88.        if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)  89.        {  90.            *cptr = 0;      // replace colon will null   91.        }  92.      93.        if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)  94.        {  95.            continue;   /* already processed this interface */  96.        }  97.      98.        memcpy(lastname, ifr->ifr_name, IFNAMSIZ);  99.      100.        ifrcopy = *ifr;  101.        ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);  102.        flags = ifrcopy.ifr_flags;  103.        if ((flags & IFF_UP) == 0)  104.        {  105.            continue;   // ignore if interface not up   106.        }  107.      108.        if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);  109.        if (if_names[nextAddr] == NULL)  110.        {  111.            return;  112.        }  113.        strcpy(if_names[nextAddr], ifr->ifr_name);  114.      115.        sin = (struct sockaddr_in *)&ifr->ifr_addr;  116.        strcpy(temp, inet_ntoa(sin->sin_addr));  117.      118.        ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);  119.        if (ip_names[nextAddr] == NULL)  120.        {  121.            return;  122.        }  123.        strcpy(ip_names[nextAddr], temp);  124.        ip_addrs[nextAddr] = sin->sin_addr.s_addr;  125.        ++nextAddr;  126.    }  127.      128.    close(sockfd);  129.}  130.void GetHWAddresses()  131.{  132.   struct ifconf ifc;  133.   struct ifreq *ifr;  134.   int i, sockfd;  135.   char buffer[BUFFERSIZE], *cp, *cplim;  136.   char temp[80];  137.   for (i=0; i<MAXADDRS; ++i)  138.   {  139.      hw_addrs[i] = NULL;  140.   }  141.   sockfd = socket(AF_INET, SOCK_DGRAM, 0);  142.   if (sockfd < 0)  143.   {  144.      perror("socket failed");  145.      return;  146.   }  147.   ifc.ifc_len = BUFFERSIZE;  148.   ifc.ifc_buf = buffer;  149.   if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)  150.   {  151.      perror("ioctl error");  152.      close(sockfd);  153.      return;  154.   }  155.   ifr = ifc.ifc_req;  156.   cplim = buffer + ifc.ifc_len;  157.   for (cp=buffer; cp < cplim; )  158.   {  159.      ifr = (struct ifreq *)cp;  160.      if (ifr->ifr_addr.sa_family == AF_LINK)  161.      {  162.         struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;  163.         int a,b,c,d,e,f;  164.         int i;  165.         strcpy(temp, (char *)ether_ntoa(LLADDR(sdl)));  166.         sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);  167.         sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);  168.         for (i=0; i<MAXADDRS; ++i)  169.         {  170.            if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))  171.            {  172.               if (hw_addrs[i] == NULL)  173.               {  174.                  hw_addrs[i] = (char *)malloc(strlen(temp)+1);  175.                  strcpy(hw_addrs[i], temp);  176.                  break;  177.               }  178.            }  179.        }  180.      }  181.      cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);  182.   }  183.   close(sockfd);  184.}  

如何用呢, 我们在MyAppAppDelegate.h中如下
01.#import <UIKit/UIKit.h>   02.@interface MyAppAppDelegate : NSObject <UIApplicationDelegate> {  03.    NSString *localIP;  04.}  05.@property (nonatomic, retain) NSString *localIP;  06.- (NSString *)deviceIPAdress;  07.@end  

MyAppDelegate.m
01.- (void)applicationDidFinishLaunching:(UIApplication *)application {  02.    self.localIP = [self deviceIPAdress];  03.        ...  04.}  05.- (NSString *)deviceIPAdress {  06.    InitAddresses();  07.    GetIPAddresses();  08.    GetHWAddresses();  09.      10.        /*  11.    int i; 12.    NSString *deviceIP; 13.    for (i=0; i<MAXADDRS; ++i) 14.    { 15.        static unsigned long localHost = 0x7F000001;        // 127.0.0.1 16.        unsigned long theAddr; 17.         18.        theAddr = ip_addrs[i]; 19.         20.        if (theAddr == 0) break; 21.        if (theAddr == localHost) continue; 22.         23.        NSLog(@"%s %s %s/n", if_names[i], hw_addrs[i], ip_names[i]); 24.    } 25.    deviceIP = [NSString stringWithFormat:@"%s", ip_names[i]]; 26.    */  27.      28.        //this will get you the right IP from your device in format like 198.111.222.444. If you use the for loop above you will se that ip_names array will also contain localhost IP 127.0.0.1 that's why I don't use it. Eventualy this was code from mac that's why it uses arrays for ip_names as macs can have multiple IPs   29.    return [NSString stringWithFormat:@"%s", ip_names[1]];  30.}