Obtains the current IP address of an iOS device.

Source: Internet
Author: User

 

The first method is to use the system api, as shown below:

# Include <stdio. h>

# Include <stdlib. h>

# Include <string. h>

# Include <unistd. h>

# Include <sys/ioctl. h>

# Include <sys/types. h>

# Include <sys/socket. h>

# Include <netinet/in. h>

# Include <netdb. h>

# Include <arpa/inet. h>

# Include <sys/sockio. h>

# Include <net/if. h>

# Include <errno. h>

# Include <net/if_dl.h>

 

// # Include "GetAddresses. h"

 

# Define min (a, B) (a) <(B )? (A): (B ))

# Define max (a, B) (a)> (B )? (A): (B ))

 

# Define BUFFERSIZE 4000

 

Char * if_names [MAXADDRS];

Char * ip_names [MAXADDRS];

Char * hw_addrs [MAXADDRS];

Unsigned long ip_addrs [MAXADDRS];

 

Static int nextAddr = 0;

 

Void InitAddresses ()

{

Int I;

For (I = 0; I <MAXADDRS; ++ I)

{

If_names [I] = ip_names [I] = hw_addrs [I] = NULL;

Ip_addrs [I] = 0;

}

}

 

Void FreeAddresses ()

{

Int I;

For (I = 0; I <MAXADDRS; ++ I)

{

If (if_names [I]! = 0) free (if_names [I]);

If (ip_names [I]! = 0) free (ip_names [I]);

If (hw_addrs [I]! = 0) free (hw_addrs [I]);

Ip_addrs [I] = 0;

}

InitAddresses ();

}

 

Void GetIPAddresses ()

{

Int I, len, flags;

Char buffer [BUFFERSIZE], * ptr, lastname [IFNAMSIZ], * cptr;

Struct ifconf ifc;

Struct ifreq * ifr, ifrcopy;

Struct sockaddr_in * sin;

Char temp [80];

Int sockfd;

For (I = 0; I <MAXADDRS; ++ I)

{

If_names [I] = ip_names [I] = NULL;

Ip_addrs [I] = 0;

}

Sockfd = socket (AF_INET, SOCK_DGRAM, 0 );

If (sockfd <0)

{

Perror ("socket failed ");

Return;

}

Ifc. ifc_len = BUFFERSIZE;

Ifc. ifc_buf = buffer;

If (ioctl (sockfd, SIOCGIFCONF, & ifc) <0)

{

Perror ("ioctl error ");

Return;

}

Lastname [0] = 0;

For (ptr = buffer; ptr <buffer + ifc. ifc_len ;)

{

Ifr = (struct ifreq *) ptr;

Len = max (sizeof (struct sockaddr), ifr-> ifr_addr.sa_len );

Ptr + = sizeof (ifr-> ifr_name) + len; // for next one in buffer

If (ifr-> ifr_addr.sa_family! = AF_INET)

{

Continue; // ignore if not desired address family

}

If (cptr = (char *) strchr (ifr-> ifr_name ,':'))! = NULL)

{

* Cptr = 0; // replace colon will null

}

If (strncmp (lastname, ifr-> ifr_name, IFNAMSIZ) = 0)

{

Continue;/* already processed this interface */

}

Memcpy (lastname, ifr-> ifr_name, IFNAMSIZ );

Ifrcopy = * ifr;

Ioctl (sockfd, SIOCGIFFLAGS, & ifrcopy );

Flags = ifrcopy. ifr_flags;

If (flags & IFF_UP) = 0)

{

Continue; // ignore if interface not up

}

If_names [nextAddr] = (char *) malloc (strlen (ifr-> ifr_name) + 1 );

If (if_names [nextAddr] = NULL)

{

Return;

}

Strcpy (if_names [nextAddr], ifr-> ifr_name );

Sin = (struct sockaddr_in *) & ifr-> ifr_addr;

Strcpy (temp, inet_ntoa (sin-> sin_addr ));

Ip_names [nextAddr] = (char *) malloc (strlen (temp) + 1 );

If (ip_names [nextAddr] = NULL)

{

Return;

}

Strcpy (ip_names [nextAddr], temp );

Ip_addrs [nextAddr] = sin-> sin_addr.s_addr;

++ NextAddr;

}

Close (sockfd );

}

 

Void GetHWAddresses ()

{

Struct ifconf ifc;

Struct ifreq * ifr;

Int I, sockfd;

Char buffer [BUFFERSIZE], * cp, * cplim;

Char temp [80];

For (I = 0; I <MAXADDRS; ++ I)

{

Hw_addrs [I] = NULL;

}

Sockfd = socket (AF_INET, SOCK_DGRAM, 0 );

If (sockfd <0)

{

Perror ("socket failed ");

Return;

}

Ifc. ifc_len = BUFFERSIZE;

Ifc. ifc_buf = buffer;

If (ioctl (sockfd, SIOCGIFCONF, (char *) & ifc) <0)

{

Perror ("ioctl error ");

Close (sockfd );

Return;

}

Ifr = ifc. ifc_req;

Cplim = buffer + ifc. ifc_len;

For (cp = buffer; cp <cplim ;)

{

Ifr = (struct ifreq *) cp;

If (ifr-> ifr_addr.sa_family = AF_LINK)

{

Struct sockaddr_dl * sdl = (struct sockaddr_dl *) & ifr-> ifr_addr;

Int a, B, c, d, e, f;

Int I;

Strcpy (temp, (char *) ether_ntoa (LLADDR (sdl )));

Sscanf (temp, "% x: % x", & a, & B, & c, & d, & e, & f );

Sprintf (temp, "% 02X: % 02X: % 02X: % 02X: % 02X: % 02X", a, B, c, d, e, f );

For (I = 0; I <MAXADDRS; ++ I)

{

If (if_names [I]! = NULL) & (strcmp (ifr-> ifr_name, if_names [I]) = 0 ))

{

If (hw_addrs [I] = NULL)

{

Hw_addrs [I] = (char *) malloc (strlen (temp) + 1 );

Strcpy (hw_addrs [I], temp );

Break;

}

} Www.2cto.com

}

}

Cp + = sizeof (ifr-> ifr_name) + max (sizeof (ifr-> ifr_addr), ifr-> ifr_addr.sa_len );

}

Close (sockfd );

}

 

-(NSString *) deviceIPAdress {

InitAddresses ();

GetIPAddresses ();

GetHWAddresses ();

 

Return [NSString stringWithFormat: @ "% s", ip_names [1];

}

 

The obtained ip address is the Intranet ip address in the intranet.

The second method is to obtain the public ip address.

-(Void) getCurrentIP

{

NSURL * url = [NSURL URLWithString: @ "http://automation.whatismyip.com/n09230945.asp"];

_ Block ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];

[Request setCompletionBlock: ^ {

NSString * responseString = [request responseString];

If (responseString ){

NSString * ip = [NSString stringWithFormat: @ "% @", responseString];

NSLog (@ "responseString = % @", ip );

};

}];

[Request setFailedBlock: ^ {

}];

}

The Network open source library ASIHTTPRequest is used.

Http://automation.whatismyip.com/n09230945.aspthis HTTP url can get your current IP address.

Author: Ecstasy talent

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.