Obtaining a server IP address from hostname - objective-c

When performing an NSURLRequest to a hostname, is it possible to obtain the IP address of the server that the response came from?
The NSURL method:
- (NSString *)host;
simply returns the hostname, and I see no way of obtaining the IP address from any of the other NSURL methods.
Perhaps there is a way of performing a host lookup before inititing the NSURLRequest?

You can use the system call gethostbyname() to resolve a hostname then use the returning structure to get the ip address. Have a look at 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);

I was asking a question regarding
"how to get IP from hostname in unix\linux?"
but found this question in a different context which is not for Unix I guess, let me correct if I am wrong
since this question already been asked so I am fearing to avoid asking the same question marked as duplicated by stack overflow team.
Quest: how to get IP from hostname in unix\linux?
Ans: the two commands over there
ping host_name
Ex:
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
nslookup host_name
Ex:
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

#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"]

Related

Get All of Mac's IP Addresses Using Objective-C

I'm trying to get all of my Mac's IP addresses (IPv4 + IPv6) but am not seeing the results I expect. I'm using code from this Stack Overflow post to get an array of all IP addresses, but there's an IP address missing.
I'm using another Mac to share it's internet connection and create a NAT64 network per Apple's documentation for testing/supporting IPv6 networks.
For example here's what the System Preferences → Network pane says my IP address is:
... but I see that I actually have two IPv6 addresses upon further inspection:
... but only one of these is returned:
"169.254.38.213",
"2001:2:0:aab1:d0ef:646d:f22a:5d83",
"127.0.0.1"
... when using this:
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <net/if.h>
#interface AppDelegate ()
#define IP_ADDR_IPv4 #"ipv4"
#define IP_ADDR_IPv6 #"ipv6"
#end
#implementation AppDelegate
- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
NSMutableArray *ipAddressesArray = [[NSMutableArray alloc] init];
// retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if (!getifaddrs(&interfaces))
{
// Loop through linked list of interfaces
struct ifaddrs *interface;
for (interface=interfaces; interface; interface=interface->ifa_next)
{
if (!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */)
{
continue; // deeply nested code harder to read
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if (addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6))
{
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
NSString *type;
if (addr->sin_family == AF_INET)
{
if (inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN))
{
type = IP_ADDR_IPv4;
}
}
else
{
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
if (inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN))
{
type = IP_ADDR_IPv6;
}
}
if (type)
{
NSString *key = [NSString stringWithFormat:#"%#/%#", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
for (id key in addresses)
{
if (![[addresses valueForKey:key] hasPrefix:#"fe80"])
{
[ipAddressesArray addObject:[addresses valueForKey:key]];
}
}
// Free memory
freeifaddrs(interfaces);
}
NSLog(#"%#", ipAddressesArray);
}
Any idea what is going on here? Does it matter? I'm trying to execute some other code conditionally based on IP address matching. It'd be one thing if the only IPv6 address returned was the one displayed to the user in System Preferences when first opening the Network pane, but instead the only IPv6 address returned is the "hidden" one that you have to dig into the Advanced... section to find. Thank you in advance.
The answer here is that I am an idiot. As suggested by Rob Napier in their comment on my question, I was essentially filtering out the missing IP addresses:
NSString *key = [NSString stringWithFormat:#"%#/%#", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
Each interface can have more than one IP address, but I since I was using the interface type as the unique key in my addresses dictionary, only one IP address per interface was present in the dictionary. I fixed this by returning an array of dictionaries rather than a single dictionary:
if (![[NSString stringWithFormat:#"%s", addrBuf] hasPrefix:#"fe80"])
{
NSDictionary *addressDict = [NSDictionary dictionaryWithObjectsAndKeys :
[NSString stringWithFormat:#"%#/%#", name, type], #"Interface",
[NSString stringWithFormat:#"%s", addrBuf], #"Address",
nil];
[addresses addObject:addressDict];
}
return addresses;

Calling CFRelease for a CFHostRef will sometimes crash

This is a secondary question that arose out of a post I made earlier today. I have the method below, which works fine for what I need, but sometimes crashes when I call CFRelease on the hostRef variable. I think it may have to do with the resource being used elsewhere when I'm trying to release it, but as far as I can tell, I'm synchronously resolving the host and I'm not accessing it from another thread.
I tried calling CFHostCancelInfoResolution before CFRelease, but that didn't change the frequency of the crashes. I thought I would post this here to see if there are some assumptions or misconceptions I have that aren't true.
+ (NSArray *) addressesForHostname: (NSString *)hostname {
CFMutableArrayRef ipAddresses = nil;
DLog(#"Getting addresses for host name %#", hostname);
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)(hostname));
CFStreamError error;
BOOL didResolve = CFHostStartInfoResolution(hostRef, kCFHostAddresses, &error); // synchronously get the host.
if (didResolve) {
CFArrayRef responseObjects = CFHostGetAddressing(hostRef, NULL);
long numberOfResponses = CFArrayGetCount(responseObjects);
ipAddresses = CFArrayCreateMutable(kCFAllocatorDefault, numberOfResponses, &kCFTypeArrayCallBacks);
for ( int i = 0 ; i < numberOfResponses; ++i ) {
char * ipAddress = NULL;
CFDataRef responseObject = CFArrayGetValueAtIndex(responseObjects, i);
struct sockaddr * currentAddress = (struct sockaddr *) CFDataGetBytePtr(responseObject); // Unwrap the CFData wrapper aound the sockaddr struct
switch (currentAddress->sa_family) {
case AF_INET: { // Internetworking AKA IPV4
DLog(#"Extracting IPV4 address");
struct sockaddr_in * socketAddress = (struct sockaddr_in *) currentAddress;
ipAddress = malloc(sizeof(INET_ADDRSTRLEN));
inet_ntop(AF_INET,
&(socketAddress->sin_addr),
ipAddress,
INET_ADDRSTRLEN);
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
CFArrayInsertValueAtIndex(ipAddresses, i, ipAddressString);
break;
}
case AF_INET6: { // IPV6
DLog(#"Extracting IPV6 address");
struct sockaddr_in6 * socketAddress = (struct sockaddr_in6 *) currentAddress;
ipAddress = malloc(sizeof(INET6_ADDRSTRLEN));
inet_ntop(AF_INET6,
&(socketAddress->sin6_addr),
ipAddress,
INET6_ADDRSTRLEN);
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
CFArrayInsertValueAtIndex(ipAddresses, i, ipAddressString);
break;
}
default:
DLog(#"Unsupported addressing protocol encountered. Gracefully ignoring and continuing.");
break;
}
if(ipAddress != NULL) {
free(ipAddress);
}
}
CFRelease(responseObjects);
}
CFRelease(hostRef);
return (__bridge_transfer NSArray *) ipAddresses;
}
removing CFRelease(responseObjects); shall fix it
You must follow the Create Rule for Core Foundation objects. If you received the object by calling a function with the words Create or Copy in their names (or if you call CFRetain explicitly), then you must release (CFRelease) the object when you're done with it. If you did not receive the object this way, then you must not release the object.
There are several mistakes in your code. First, the one you're finding, which is around responseObjects. You fetch this object using:
CFDataRef responseObject = CFArrayGetValueAtIndex(responseObjects, i);
The function does not have Create or Copy in its name. You must not call CFRelease on it.
However, you also call this:
ipAddresses = CFArrayCreateMutable(kCFAllocatorDefault, numberOfResponses, &kCFTypeArrayCallBacks);
and in a couple of places:
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
You must call CFRelease on these objects before they go out of scope or you will leak them.
I think CFStreamError error; may cause this issue.
Try to declare error to null?
// synchronously get the host.
BOOL didResolve = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);

how to resolve hostname from ip address in iOS Objective-C

I'm looking for a way to resolve the hostname of a device in my LAN from its ip address on this LAN.
I wrote a program in C which works perfectly on Linux using gethostbyaddr() function.
When I tried that on OS X or iOS it doesn't work.
It seems that there is a problem with gethostbyaddr() in OS X and iOS.
Anyway, if you have another idea to get hostname of remote machine from it's IP in iOS, it'll make my day.
This is the code I used:
First test:
192.168.0.101 is the ip address of the machine that we are querying for hostname.
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct hostent *he;
struct in_addr ipv4addr;
inet_pton(AF_INET, "192.168.0.101", &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
printf("Host name: %s\n", he->h_name);
This code works well on linux, but it doesn't on OS X nor iOS.
Second test:
+ (NSArray *)hostnamesForAddress:(NSString *)address {
// Get the host reference for the given address.
struct addrinfo hints;
struct addrinfo *result = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
if (errorStatus != 0) return nil;
CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
if (addressRef == nil) return nil;
freeaddrinfo(result);
CFHostRef hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, addressRef);
if (hostRef == nil) return nil;
CFRelease(addressRef);
BOOL isSuccess = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL);
if (!isSuccess) return nil;
// Get the hostnames for the host reference.
CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
NSMutableArray *hostnames = [NSMutableArray array];
for (int currentIndex = 0; currentIndex < [(NSArray *)hostnamesRef count]; currentIndex++) {
[hostnames addObject:[(NSArray *)hostnamesRef objectAtIndex:currentIndex]];
}
return hostnames;
}
This code stucks at CFHostStartInfoResolution returning nil at this point.
Thx in advance.
It's actually a one-liner.
[[NSHost hostWithAddress:#"173.194.34.24"] name]

CFSocketSetAddress fails if not host computer IP?

I am making a simple WOL application.
So far I can successfully create a socket, however when setting the address using CFSocketSetAddress I can only use the network ip of my computer (WiFi ip = 192.168.0.5) or local ip (127.0.0.1).
For WOL I would like to send the data to the broadcast address (255.255.255.255). If this is entered I am returned with the error 'address could not be set'.
Am I miss-understanding the use of CFSocketSetAddress, and the address is supposed to be the hosts IP, or the destination IP? In either case, what do I need to do, so that my destination ip is the broadcast address?
Below is some of my code:
/*************************************************************************/
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT); //port
inet_aton(IP, &addr.sin_addr);//IP is the host network ip: 192.168.0.5
NSData *address = [NSData dataWithBytes: &addr length: sizeof(addr)];
if (CFSocketSetAddress(WOLsocket, (CFDataRef)address) != kCFSocketSuccess){
NSLog(#"Address could not be set!");
}
I solved my problem by using a different method (Got native socket), and then instead of using CFSocketSetAddress, I passed the address in the second argument of CFSocketSendData.
I don't have a link/reference for the changes, as it was some code stored on my HDD from days of heavy googling.
Would like to say thanks to David Gelhar Who pointed me in the right direction from my previous question.
for anyone else who might need this this is my code;
//Gets native & sets options
/*************************************************************************/
int desc = -1;
desc = CFSocketGetNative(WOLsocket);
int yes = 1;
if (setsockopt (desc, SOL_SOCKET, SO_BROADCAST, (char *)&yes, sizeof (yes)) < 0) {
NSLog(#"Set Socket options failed");
return EXIT_FAILURE;
}
//sets address socket - doesn't bind this is done in CFSocketSendData
/*************************************************************************/
unsigned long bcast = 0xffffffff;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT); //port
addr.sin_addr.s_addr = bcast;
NSData *address = [NSData dataWithBytes: &addr length: sizeof(addr)];
//Sends Data
/*************************************************************************/
char ethadd []= "helloworld";
CFDataRef Data = CFDataCreate(NULL, (const UInt8*)ethadd, sizeof(ethadd));
if (CFSocketSendData(WOLsocket, address, Data, 0) < 0){
NSLog(#"Data could not be sent!");
return EXIT_FAILURE;
}
else NSLog(#"Data Sent");
}
For my app, the problem is that it was sandboxed, but it wasn't given network permissions.
If you're using App Sandbox, you need to check one of these:

How to validate an IP address with regular expression in Objective-C?

How to validate an IP address in Objective-C?
Here's a category using the modern inet_pton which will return YES for a valid IPv4 or IPv6 string.
#include <arpa/inet.h>
#implementation NSString (IPValidation)
- (BOOL)isValidIPAddress
{
const char *utf8 = [self UTF8String];
int success;
struct in_addr dst;
success = inet_pton(AF_INET, utf8, &dst);
if (success != 1) {
struct in6_addr dst6;
success = inet_pton(AF_INET6, utf8, &dst6);
}
return success == 1;
}
#end
Here's an alternative approach that might also help. Let's assume you have an NSString* that contains your IP address, called ipAddressStr, of the format a.b.c.d:
int ipQuads[4];
const char *ipAddress = [ipAddressStr cStringUsingEncoding:NSUTF8StringEncoding];
sscanf(ipAddress, "%d.%d.%d.%d", &ipQuads[0], &ipQuads[1], &ipQuads[2], &ipQuads[3]);
#try {
for (int quad = 0; quad < 4; quad++) {
if ((ipQuads[quad] < 0) || (ipQuads[quad] > 255)) {
NSException *ipException = [NSException
exceptionWithName:#"IPNotFormattedCorrectly"
reason:#"IP range is invalid"
userInfo:nil];
#throw ipException;
}
}
}
#catch (NSException *exc) {
NSLog(#"ERROR: %#", [exc reason]);
}
You could modify the if conditional block to follow RFC 1918 guidelines, if you need that level of validation.
Swift edition:
func isIPAddressValid(ip: String) -> Bool {
guard let utf8Str = (ip as NSString).utf8String else {
return false
}
let utf8:UnsafePointer<Int8> = UnsafePointer(utf8Str)
var success: Int32
var dst: in_addr = in_addr()
success = inet_pton(AF_INET, utf8, &dst)
if (success != 1) {
var dst6: in6_addr? = in6_addr()
success = inet_pton(AF_INET6, utf8, &dst6);
}
return success == 1
}
A trick you can do is test the return of the inet_aton BSD call like this:
#include <arpa/inet.h>
- (BOOL)isIp:(NSString*)string{
struct in_addr pin;
int success = inet_aton([string UTF8String],&pin);
if (success == 1) return TRUE;
return FALSE;
}
Be aware however that this validates the string if it contains a ip address in any format, it is not restricted to the dotted format.