|
6 | 6 | // Copyright © 2016 Branch Metrics. All rights reserved. |
7 | 7 | // |
8 | 8 |
|
9 | | - |
10 | | -@import Foundation; |
11 | | -@import UIKit; |
12 | | -@import Darwin.sys.sysctl; |
13 | 9 | #import "BNCDeviceInfo.h" |
14 | 10 | #import "BNCPreferenceHelper.h" |
15 | 11 | #import "BNCSystemObserver.h" |
16 | 12 | #import "BNCXcode7Support.h" |
17 | 13 | #import "BNCLog.h" |
18 | 14 |
|
| 15 | +@import UIKit; |
| 16 | +#import <sys/sysctl.h> // @import not available in Xcode 7 |
| 17 | +#import <net/if.h> |
| 18 | +#import <ifaddrs.h> |
| 19 | +#import <arpa/inet.h> |
| 20 | +#import <netinet/in.h> |
| 21 | + |
| 22 | +#pragma mark - BRNNetworkInfo |
| 23 | + |
| 24 | +typedef NS_ENUM(NSInteger, BNCNetworkAddressType) { |
| 25 | + BNCNetworkAddressTypeUnknown = 0, |
| 26 | + BNCNetworkAddressTypeIPv4, |
| 27 | + BNCNetworkAddressTypeIPv6 |
| 28 | +}; |
| 29 | + |
| 30 | +@interface BNCNetworkInterface : NSObject |
| 31 | + |
| 32 | ++ (NSArray<BNCNetworkInterface*>*) currentInterfaces; |
| 33 | + |
| 34 | +@property (nonatomic, strong) NSString *interfaceName; |
| 35 | +@property (nonatomic, assign) BNCNetworkAddressType addressType; |
| 36 | +@property (nonatomic, strong) NSString *address; |
| 37 | +@end |
| 38 | + |
| 39 | +@implementation BNCNetworkInterface |
| 40 | + |
| 41 | ++ (NSArray<BNCNetworkInterface*>*) currentInterfaces { |
| 42 | + |
| 43 | + struct ifaddrs *interfaces = NULL; |
| 44 | + NSMutableArray *currentInterfaces = [NSMutableArray arrayWithCapacity:8]; |
| 45 | + |
| 46 | + // Retrieve the current interfaces - returns 0 on success |
| 47 | + |
| 48 | + if (getifaddrs(&interfaces) != 0) { |
| 49 | + int e = errno; |
| 50 | + BNCLogError(@"Can't read ip address: (%d): %s.", e, strerror(e)); |
| 51 | + goto exit; |
| 52 | + } |
| 53 | + |
| 54 | + // Loop through linked list of interfaces -- |
| 55 | + |
| 56 | + struct ifaddrs *interface = NULL; |
| 57 | + for(interface=interfaces; interface; interface=interface->ifa_next) { |
| 58 | + // BNCLogDebugSDK(@"Found %s: %x.", interface->ifa_name, interface->ifa_flags); |
| 59 | + |
| 60 | + // Check the state: IFF_RUNNING, IFF_UP, IFF_LOOPBACK, etc. |
| 61 | + if (!(interface->ifa_flags & IFF_RUNNING) || |
| 62 | + (interface->ifa_flags & IFF_LOOPBACK)) |
| 63 | + continue; |
| 64 | + |
| 65 | + const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr; |
| 66 | + if (!addr) continue; |
| 67 | + |
| 68 | + BNCNetworkAddressType type = BNCNetworkAddressTypeUnknown; |
| 69 | + char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ]; |
| 70 | + |
| 71 | + if (addr->sin_family == AF_INET) { |
| 72 | + if (inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) |
| 73 | + type = BNCNetworkAddressTypeIPv4; |
| 74 | + } |
| 75 | + else |
| 76 | + if (addr->sin_family == AF_INET6) { |
| 77 | + const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr; |
| 78 | + if (inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) |
| 79 | + type = BNCNetworkAddressTypeIPv6; |
| 80 | + } |
| 81 | + else { |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + NSString *name = [NSString stringWithUTF8String:interface->ifa_name]; |
| 86 | + if (name && type != BNCNetworkAddressTypeUnknown) { |
| 87 | + BNCNetworkInterface *interface = [BNCNetworkInterface new]; |
| 88 | + interface.interfaceName = name; |
| 89 | + interface.addressType = type; |
| 90 | + interface.address = [NSString stringWithUTF8String:addrBuf]; |
| 91 | + [currentInterfaces addObject:interface]; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +exit: |
| 96 | + if (interfaces) freeifaddrs(interfaces); |
| 97 | + return currentInterfaces; |
| 98 | +} |
| 99 | + |
| 100 | +- (NSString*) description { |
| 101 | + return [NSString stringWithFormat:@"<%@ %p %@ %@>", |
| 102 | + NSStringFromClass(self.class), |
| 103 | + self, |
| 104 | + self.interfaceName, |
| 105 | + self.address |
| 106 | + ]; |
| 107 | +} |
| 108 | + |
| 109 | +@end |
| 110 | + |
| 111 | +#pragma mark - BNCDeviceInfo |
19 | 112 |
|
20 | 113 | @interface BNCDeviceInfo() |
21 | 114 | @end |
22 | 115 |
|
23 | 116 |
|
24 | 117 | @implementation BNCDeviceInfo { |
25 | | - NSString * volatile _vendorId; |
| 118 | + NSString *_vendorId; |
| 119 | + NSString *_localIPAddress; |
26 | 120 | } |
27 | 121 |
|
28 | 122 | + (BNCDeviceInfo *)getInstance { |
@@ -84,6 +178,17 @@ - (NSString *)vendorId { |
84 | 178 | } |
85 | 179 | } |
86 | 180 |
|
| 181 | +- (NSString*) localIPAddress { // For 'local_ip' server field. |
| 182 | + @synchronized (self) { |
| 183 | + NSArray<BNCNetworkInterface*>*interfaces = [BNCNetworkInterface currentInterfaces]; |
| 184 | + for (BNCNetworkInterface *interface in interfaces) { |
| 185 | + if (interface.addressType == BNCNetworkAddressTypeIPv4) |
| 186 | + return interface.address; |
| 187 | + } |
| 188 | + return nil; |
| 189 | + } |
| 190 | +} |
| 191 | + |
87 | 192 | + (NSString*) bnc_country { |
88 | 193 |
|
89 | 194 | NSString *country = nil; |
|
0 commit comments