Getting server IP address from host name

When executing NSURLRequest for the host name, is it possible to get the IP address of the server to which the response is received?

NSURL Method:

 - (NSString *)host; 

just returns the hostname, and I see no way to get the IP address from any other NSURL method.

Perhaps there is a way to search for a node before starting NSURLRequest ?

+4
source share
3 answers

You can use the gethostbyname () system call to resolve the host name, and then use the return structure to get the IP address. See inet_ntop () for this last part.

EXAMPLE CODE

 struct hostent *hostentry; hostentry = gethostbyname("google.com"); char * ipbuf; ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0])); printf("%s",ipbuf); 
+11
source

I asked a question regarding

"how to get the IP address on behalf of the host in unix \ linux?"

but found this question in a different context that is not for Unix, I think let me fix it if I am wrong

since this question has already been asked, so I'm afraid not to ask the same question that is duplicated by the team.

Quest: how to get the IP address on behalf of the host in unix \ linux?

Ans: two teams there

  • ping host_name

Example:

 ping -s google.co.in PING google.co.in: 56 data bytes 64 bytes from dfw06s48-in-f3.1e100.net (216.58.194.99): icmp_seq=0. time=2.477 ms 64 bytes from dfw06s48-in-f3.1e100.net (216.58.194.99): icmp_seq=1. time=1.415 ms 64 bytes from dfw06s48-in-f3.1e100.net (216.58.194.99): icmp_seq=2. time=1.712 ms 
  1. nslookup host_name

Example:

 nslookup google.co.in Server: 155.179.59.249 Address: 155.179.59.249#53 Non-authoritative answer: Name: google.co.in Address: 216.58.194.99 
+2
source
 #import <arpa/inet.h> - (BOOL)resolveHost:(NSString *)hostname { Boolean result; CFHostRef hostRef; CFArrayRef addresses; NSString *ipAddress = nil; hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)hostname); CFStreamError *error = NULL; if (hostRef) { result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, error); if (result) { addresses = CFHostGetAddressing(hostRef, &result); } } if (result) { CFIndex index = 0; CFDataRef ref = (CFDataRef) CFArrayGetValueAtIndex(addresses, index); int port=0; struct sockaddr *addressGeneric; NSData *myData = (__bridge NSData *)ref; addressGeneric = (struct sockaddr *)[myData bytes]; switch (addressGeneric->sa_family) { case AF_INET: { struct sockaddr_in *ip4; char dest[INET_ADDRSTRLEN]; ip4 = (struct sockaddr_in *)[myData bytes]; port = ntohs(ip4->sin_port); ipAddress = [NSString stringWithFormat:@"%s", inet_ntop(AF_INET, &ip4->sin_addr, dest, sizeof dest)]; } break; case AF_INET6: { struct sockaddr_in6 *ip6; char dest[INET6_ADDRSTRLEN]; ip6 = (struct sockaddr_in6 *)[myData bytes]; port = ntohs(ip6->sin6_port); ipAddress = [NSString stringWithFormat:@"%s", inet_ntop(AF_INET6, &ip6->sin6_addr, dest, sizeof dest)]; } break; default: ipAddress = nil; break; } } NSLog(@"%@", ipAddress); if (ipAddress) { return YES; } else { return NO; } } [self resolveHost:@"google.com"] 
0
source

Source: https://habr.com/ru/post/1399778/


All Articles