Skip to content

Commit

Permalink
Added support for reachability to IP addresses (ipv4 and ipv6)
Browse files Browse the repository at this point in the history
  • Loading branch information
kstenerud committed Dec 12, 2013
1 parent 61524ef commit e1873aa
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions KSReachability/KSReachability/KSReachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#import "KSReachability.h"
#import <netdb.h>
#import <arpa/inet.h>


// ----------------------------------------------------------------------
Expand Down Expand Up @@ -104,18 +105,27 @@ + (KSReachability*) reachabilityToLocalNetwork
- (id) initWithHost:(NSString*) hostname
{
hostname = [self extractHostName:hostname];
if([hostname length] == 0)
{
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
const char* name = [hostname UTF8String];

struct sockaddr_in6 address;
bzero(&address, sizeof(address));
address.sin6_len = sizeof(address);
address.sin6_family = AF_INET;

return [self initWithAddress:(const struct sockaddr*)&address];
if([hostname length] > 0)
{
if(inet_pton(address.sin6_family, name, &address.sin6_addr) != 1)
{
address.sin6_family = AF_INET6;
if(inet_pton(address.sin6_family, name, &address.sin6_addr) != 1)
{
return [self initWithReachabilityRef:SCNetworkReachabilityCreateWithName(NULL, name)
hostname:hostname];
}
}
}

return [self initWithReachabilityRef:SCNetworkReachabilityCreateWithName(NULL, [hostname UTF8String])
hostname:hostname];
return [self initWithAddress:(const struct sockaddr*)&address];
}

- (id) initWithAddress:(const struct sockaddr*) address
Expand Down

0 comments on commit e1873aa

Please sign in to comment.