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

I created a query using a domain name, for example, http://www.google.com . But how could I get exactly the ip address that was used to connect to the server?

I knew that the gethostbyname method or nslookup method could give us an address, but since the ip address is dynamically allocated, maybe I am asking for an ip address that the time is different from these methods.

So, is there a way to get the real requested ip address? (I wanted to get ip programmatically and not use tcpdump, etc.)

+6
source share
3 answers

When you say "where the wireframe is used," I assume that you mean NSURLSession , NSURLConnection , UIWebView or WKWebView . Each of them is a slightly different situation, but in all of them the answer is that this is not possible directly (but see below, this is possible indirectly). You do not have access to the underlying sockets they use. They all use a connection pool, which complicates the situation, even if you can get a "socket". In the case of UIWebView and WKWebView single request to www.google.com can generate several independent connections, each of which can potentially interact with a different IP address.

(I am a bit delighted with what you are trying to do. Due to load balancing, one IP address does not mean one server, so IP addresses are only slightly more identifiable than CNAME. Proxies ....)

If you need such access, you need to manage the socket yourself. This is possible on all systems except WKWebView . I assume that you already know (or can easily learn) how to create a socket and execute HTTP using CFSocket and CFHTTPMessage . This is described in detail in the CFNetwork Programming Guide . If you created a socket, you can use CFSocketCopyPeerAddress to check which host you are really connected to. This is what you wanted.

Given that you can create this socket and manage it yourself, you can connect it to the main URL loaders (except WKWebView ) using NSURLProtocol . See Implementing Offline Caching for UIWebView (and NSURLProtocol) for a quick introduction and some code examples. You just need to take the request and make it using CFSocket , giving you the opportunity to see the exact port you are connected to.

+1
source

Your browser or any other tool that you use to create the HTTP request will look up the address during the execution of the request. On any reasonable system, they will use the same method to find the address as gethostbyname does, except that the browser may have local caching (which you can usually disable or clear).

The request may receive a redirect response to make the same request elsewhere. They are often used for load balancing, etc. The only simple way to understand that you could find out the “final destination” behind them would be to make a request programmatically and record the details of any redirects. Of course, this is the nature of load balancing, which you could redirect to a slightly different server each time.

This has nothing to do with the dynamic allocation of IP addresses. If the IP address of www.google.com changes, this is most likely not because the same server was assigned a different IP address, but because requests are sent to another machine.

+1
source

If you use NSURLSession to get the URLSessionStreamTask , you can capture the underlying streams using -captureStreams . This will call the delegate method -URLSession:streamTask:didBecomeInputStream:outputStream: which will give you NSInputStream and NSOutputStream . NSInputStream is free for the CFReadStreamRef bridge and uses CFReadStreamCopyProperty() , and the constant kCFStreamPropertySocketNativeHandle provides you with your own socket descriptor, which you can pass to getpeername() . For Upload-, Download- and DataTasks, this does not exist.

0
source

All Articles