Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the exactly ip address which used to connect in iOS?

I created a request using a domain name, e.g., http://www.google.com. But how could I get the exactly ip address which the framework used to connect to the server?

I knew that the gethostbyname method or the nslookup method could give us the address, but since the ip address is dynamic allocated, maybe the ip address I request that time is different with these methods returned.

So is there a way for me to get the real requested ip address? (I wanted to get the ip programmatically, rather than using tcpdump, etc.)

like image 208
feihu Avatar asked Jul 20 '15 04:07

feihu


People also ask

How do I find the IP address of my iOS device?

Find your IP address on an iOS deviceOn the Home screen, tap Settings. Tap Wi-Fi. Tap the information icon (blue i, in a circle) to the right of the network name (eduroam). Tap DHCP and the IP Address will be listed as the first line item below the heading.

Can you get exact address with IP?

What information does my IP address reveal? IP addresses do reveal your geolocation, but not your precise location like a home address does. IP addresses will also never reveal your name, phone number, or other precise personal information.

How do I find the exact location of an IP address?

If you want know how to find your IP address, visit the WhatIsMyIP.com home page to see your IP address as it is listed. This is your public IP; it will also tell you what your IP location is and who your ISP is.

How do you find the IP address of the device is connected to?

To see all of the devices connected to your network, type arp -a in a Command Prompt window. This will show you the allocated IP addresses and the MAC addresses of all connected devices.


2 Answers

When you say "which the framework used" here, I assume you mean NSURLSession, NSURLConnection, UIWebView, or WKWebView. Each of those is a slightly different situation, but in all of them, the answer is that it's not possible directly (but see below; it's possible indirectly). You have no access to the underlying sockets that any of them use. All of them use connection pooling, which complicates things slightly even if you could get "the socket." And in the case of UIWebView and WKWebView, a single request to www.google.com may generate several independent connections, each of which could potentially interact with a different IP address.

(I'm a bit fascinated about what you're trying to do. Due to load balancing, a single IP address doesn't mean a single server, so IP addresses are only marginally more identifying than CNAMEs. Mixing in reverse proxies....)

If you need this kind of access, you have to manage the socket yourself. That's generally possible with all of the systems except WKWebView. I'll assume that you know already (or can easily study) how to create a socket and perform HTTP using CFSocket and CFHTTPMessage. This is extensively covered in the CFNetwork Programming Guide. If you've created the socket, you can use CFSocketCopyPeerAddress to check what host you really connected to. That's the piece you wanted.

Given you are able to create this kind of socket and manage it yourself, you can hook that into the major URL loaders (except WKWebView) using an NSURLProtocol. See Drop-in Offline Caching for UIWebView (and NSURLProtocol) for a quick introduction and some sample code. You just need to take the request and make it yourself with CFSocket, giving you the chance to see the exact port you're connected to.

like image 186
Rob Napier Avatar answered Oct 07 '22 01:10

Rob Napier


Your browser or whatever other tool you use to make a HTTP request will look up the address at the time of doing the request. On any reasonable system, they will use the same method for looking up the address as gethostbyname does, except that there might be local caching in the browser (which you can usually turn off or clear).

The request might get a redirection response to make the same request to a different location. These are often used for load balancing, etc. The only straightforward way I can see that you would be able to find out the 'ultimate destination' behind these, would be to make the request programmatically and record the details of any redirections, Of course, it is the nature of load balancing that you might redirected to a slightly different server each time.

This has nothing to do with 'dynamic allocation of IP addresses' by the way. If the IP address of www.google.com changes, it is likely to be not because the same server has been allocated a different IP address, but because requests are being directed to a different machine.

like image 1
jwg Avatar answered Oct 07 '22 01:10

jwg