127.0.0.1 is the address of the loopback device. You want the address of en0
or whatever, which is the device connected to the wifi/mobile network. (It's possible to enumerate all the device addresses using getifaddrs()
; see this SO question).
For IPv4 address the value can be represented using an 32-bit unsigned int
so the range change is as simple as:
BOOL inRange = ipAddress >= rangeStart && ipAddress <= endRange;
See this SO question.
EDIT The OP seems to be stuck on the conversion of an IP address represented using dot-notation within a string (i.e. "2.88.0.0") and the unsigned int
that I refer to. For this I refer him to the inet_aton()
function, which is used to perform that conversion. You will get many hits on the internet if you search for that function and how to use it.
EDIT 2 OK, I'll add a more complete answer as the OP is still having issues:
The key piece of information I think you are missing WRT using inet_aton()
is byte order. There is a concept of network byte order, which is big-endian given the founders of the internet used Dec PDP 11's to develop on. Most computers these days are little endian. So you need to add htonl()
into the mix, which is the function to convert from network to host byte order (where the l
means long, which is in-fact 32-bit).
So:
#import <Foundation/Foundation.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
@interface NetworkStuff : NSObject
- (uint32_t)convertIpAddress:(NSString *)ipAddress;
- (BOOL)ipAddress:(NSString *)ipAddress isBetweenIpAddress:(NSString *)rangeStart
andIpAddress:(NSString *)rangeEnd;
@end
@implementation NetworkStuff
- (uint32_t)convertIpAddress:(NSString *)ipAddress {
struct sockaddr_in sin;
inet_aton([ipAddress UTF8String], &sin.sin_addr);
return ntohl(sin.sin_addr.s_addr);
}
- (BOOL)ipAddress:(NSString *)ipAddress isBetweenIpAddress:(NSString *)rangeStart
andIpAddress:(NSString *)rangeEnd {
uint32_t ip = [self convertIpAddress:ipAddress];
uint32_t start = [self convertIpAddress:rangeStart];
uint32_t end = [self convertIpAddress:rangeEnd];
return ip >= start && ip <= end;
}
@end
#define BOOLSTR(b) (b ? @"YES" : @"NO")
int main()
{
@autoreleasepool{
NetworkStuff *networkStuff = [NetworkStuff new];
NSLog(@"%@", BOOLSTR([networkStuff ipAddress:@"2.90.1.2" isBetweenIpAddress:@"2.88.0.0" andIpAddress:@"2.91.255.255"]));
NSLog(@"%@", BOOLSTR([networkStuff ipAddress:@"2.92.1.2" isBetweenIpAddress:@"2.88.0.0" andIpAddress:@"2.91.255.255"]));
}
return 0;
}
$ clang -DDEBUG=1 -g -fobjc-arc -o iprange iprange.m -framework Foundation
$ ./iprange
2014-02-10 09:42:29.530 iprange[14693:707] YES
2014-02-10 09:42:29.532 iprange[14693:707] NO