2014-02-13 86 views
0

我做小iPhone應用程序,我需要獲取IP範圍,並通過目標C遍歷它。我可以獲得本地IP,網絡掩碼。此外,我在SO解決方案上找到了如何獲得廣播地址。但我怎樣才能得到網絡地址?通過了解網絡地址本地網絡中的第一個地址和廣播地址本地網絡中的最後一個地址我想迭代該範圍內的所有IP。只需調用它並查看響應。我怎樣才能做到這一點?獲取IP範圍與目標C

解決從https://stackoverflow.com/a/21077257

#include <net/ethernet.h> 
#include <arpa/inet.h> 

NSString *localIPAddress = @"192.168.1.10"; 
NSString *netmaskAddress = @"255.255.192.0"; 

// Strings to in_addr: 
struct in_addr localAddr; 
struct in_addr netmaskAddr; 
inet_aton([localIPAddress UTF8String], &localAddr); 
inet_aton([netmaskAddress UTF8String], &netmaskAddr); 

// The broadcast address calculation: 
localAddr.s_addr |= ~(netmaskAddr.s_addr); 

// in_addr to string: 
NSString *broadCastAddress = [NSString stringWithUTF8String:inet_ntoa(localAddr)]; 

更新獲得廣播:從netmaskAddress我可以得到網絡中主機的數量。我只需要與IP一起工作。現在的問題是如何從給定的IP獲得下一個IP?比如我有

NSString *ip = "192.168.1.5" 

我怎樣才能得到「192.168.1.6」與目標C?

+0

你要什麼迭代?你能解釋你的用例嗎? – Moonwalkr

+0

我必須在網絡中找到其他設備。所以我必須從本地網絡呼叫所有IP。 – poul

+0

你看過iOS多對等連接嗎?也許它解決了你的用例:http://nshipster.com/multipeer-connectivity/ https://developer.apple.com/library/ios/documentation/MultipeerConnectivity/Reference/MultipeerConnectivityFramework/Introduction/Introduction.html – rist

回答

1

我不建議在這種類型的IP(in_addr)上進行位操作,因爲如果你看看裏面的字節是反向排序的。

下面的代碼是打印設備的IP,網絡IP,網絡掩碼和廣播地址和所有IP的網絡範圍。這可能會有所幫助。請享用!

NetworkInformation *network = [[NetworkInformation alloc] init]; 
NSLog(@"%@", network); 
for(NSString *ip in network.ipsInRange){ 
    NSLog(@"ip: %@", ip); 
} 

NetworkInformation.h

#import <Foundation/Foundation.h> 

@interface NetworkInformation : NSObject 
@property (nonatomic, retain) NSString *deviceIP; 
@property (nonatomic, retain) NSString *netmask; 
@property (nonatomic, retain) NSString *address; 
@property (nonatomic, retain) NSString *broadcast; 
@property (nonatomic, retain) NSArray *ipsInRange; 
- (void)updateData; 
- (NSString *)description; 
@end 

NetworkInformation.m

#import "NetworkInformation.h" 
#import <arpa/inet.h> 
#import <ifaddrs.h> 

@implementation NetworkInformation 

- (id)init { 
    self = [super init]; 
    if (self) { 
     [self updateData]; 
    } 
    return self; 
} 

-(void)updateData { 
    [self updateDataFromWifiNetwork]; 
    self.address = [self getNetworkAddress]; 
    self.ipsInRange = [self getIPsInRange]; 
} 

- (void)updateDataFromWifiNetwork { 
    self.deviceIP = nil; 
    self.netmask = nil; 
    self.broadcast = nil; 
    struct ifaddrs *interfaces = NULL; 
    struct ifaddrs *temp_addr = NULL; 
    int success = 0; 
    success = getifaddrs(&interfaces); 
    if (success == 0){ 
     temp_addr = interfaces; 
     while(temp_addr != NULL){ 
      if(temp_addr->ifa_addr->sa_family == AF_INET){ 
       if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]){ 
        self.deviceIP = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; 
        self.netmask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)]; 
        self.broadcast = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)]; 
       } 
      } 
      temp_addr = temp_addr->ifa_next; 
     } 
    } 
    freeifaddrs(interfaces); 
    if(!self.deviceIP || !self.netmask){ 
     NSLog(@"error in updateDataFromWifiNetwork, device ip: %@ netmask: %@", self.deviceIP, self.netmask); 
    } 
} 

-(NSString*)getNetworkAddress { 
    if(!self.deviceIP || !self.netmask){ 
     return nil; 
    } 
    unsigned int address = [self convertSymbolicIpToNumeric:self.deviceIP]; 
    address &= [self convertSymbolicIpToNumeric:self.netmask]; 
    return [self convertNumericIpToSymbolic:address]; 
} 

-(NSArray*)getIPsInRange { 
    unsigned int address = [self convertSymbolicIpToNumeric:self.address]; 
    unsigned int netmask = [self convertSymbolicIpToNumeric:self.netmask]; 
    NSMutableArray *result = [[NSMutableArray alloc] init]; 
    int numberOfBits; 
    for (numberOfBits = 0; numberOfBits < 32; numberOfBits++) { 
     if ((netmask << numberOfBits) == 0){ 
      break; 
     } 
    } 
    int numberOfIPs = 0; 
    for (int n = 0; n < (32 - numberOfBits); n++) { 
     numberOfIPs = numberOfIPs << 1; 
     numberOfIPs = numberOfIPs | 0x01; 
    } 
    for (int i = 1; i < (numberOfIPs) && i < numberOfIPs; i++) { 
     unsigned int ourIP = address + i; 
     NSString *ip = [self convertNumericIpToSymbolic:ourIP]; 
     [result addObject:ip]; 
    } 
    return result; 
} 

-(NSString*)convertNumericIpToSymbolic:(unsigned int)numericIP { 
    NSMutableString *sb = [NSMutableString string]; 
    for (int shift = 24; shift > 0; shift -= 8) { 
     [sb appendString:[NSString stringWithFormat:@"%d", (numericIP >> shift) & 0xff]]; 
     [sb appendString:@"."]; 
    } 
    [sb appendString:[NSString stringWithFormat:@"%d", (numericIP & 0xff)]]; 
    return sb; 
} 

-(unsigned int)convertSymbolicIpToNumeric:(NSString*)symbolicIP { 
    NSArray *st = [symbolicIP componentsSeparatedByString: @"."]; 
    if (st.count != 4){ 
     NSLog(@"error in convertSymbolicIpToNumeric, splited string count: %lu", st.count); 
     return 0; 
    } 
    int i = 24; 
    int ipNumeric = 0; 
    for (int n = 0; n < st.count; n++) { 
     int value = [(NSString*)st[n] intValue]; 
     if (value != (value & 0xff)) { 
      NSLog(@"error in convertSymbolicIpToNumeric, invalid IP address: %@", symbolicIP); 
      return 0; 
     } 
     ipNumeric += value << i; 
     i -= 8; 
    } 
    return ipNumeric; 
} 

-(NSString*)description { 
    return [NSString stringWithFormat: @"\nip:%@\nnetmask:%@\nnetwork:%@\nbroadcast:%@", self.deviceIP, self.netmask, self.address, self.broadcast]; 
} 

@end