Fixed bug where reachability did not work when initialized with an IP address in the base URL

This commit is contained in:
Blake Watters
2011-02-01 19:30:07 -05:00
parent 99559f73a8
commit bff66e60cc
4 changed files with 66 additions and 2 deletions

View File

@@ -8,6 +8,8 @@
#import "RKReachabilityObserver.h"
#import <UIKit/UIKit.h>
#include <netdb.h>
#include <arpa/inet.h>
// Constants
NSString* const RKReachabilityStateChangedNotification = @"RKReachabilityStateChangedNotification";
@@ -43,8 +45,32 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach
@implementation RKReachabilityObserver
+ (RKReachabilityObserver*)reachabilityObserverWithHostName:(NSString*)hostName {
RKReachabilityObserver* observer = nil;
SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
RKReachabilityObserver* observer = nil;
SCNetworkReachabilityRef reachabilityRef;
// Try to determine if we have an IP address or a hostname
struct sockaddr_in sa;
char* hostNameOrIPAddress = (char*) [hostName UTF8String];
int result = inet_pton(AF_INET, hostNameOrIPAddress, &(sa.sin_addr));
if (result != 0) {
// IP Address
struct sockaddr_in remote_saddr;
bzero(&remote_saddr, sizeof(struct sockaddr_in));
remote_saddr.sin_len = sizeof(struct sockaddr_in);
remote_saddr.sin_family = AF_INET;
// inet_aton(hostNameOrIPAddress, &(remote_saddr.sin_addr));
remote_saddr.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
reachabilityRef = SCNetworkReachabilityCreateWithAddress(CFAllocatorGetDefault(), (struct sockaddr*)&remote_saddr);
// We can immediately determine reachability to an IP address
hasNetworkAvailabilityBeenDetermined = YES;
} else {
// Hostname
reachabilityRef = SCNetworkReachabilityCreateWithName(CFAllocatorGetDefault(), hostNameOrIPAddress);
}
if (nil != reachabilityRef) {
observer = [[[self alloc] initWithReachabilityRef:reachabilityRef] autorelease];