Skip to content

Commit 4251bb1

Browse files
committed
Merge pull request #28 from longbai/ios8.2
dns 函数修正
2 parents dfa371b + d9e06ed commit 4251bb1

File tree

9 files changed

+57
-24
lines changed

9 files changed

+57
-24
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#Changelog
22

3+
## 7.0.9 (2015-)
4+
5+
### 修正
6+
* dns解析函数
7+
38
## 7.0.8 (2015-01-25)
49

510
### 增加

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
DF0D23CC19DCE6C400D6B68F /* QNResponseInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNResponseInfo.h; sourceTree = "<group>"; };
145145
DF0D23CE19DCE6E500D6B68F /* QNResponseInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNResponseInfo.m; sourceTree = "<group>"; };
146146
DF293C9019DB85CB00799011 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = usr/lib/libz.dylib; sourceTree = SDKROOT; };
147-
DF293C9319DB860200799011 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.1.sdk/usr/lib/libz.dylib; sourceTree = DEVELOPER_DIR; };
147+
DF293C9319DB860200799011 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/libz.dylib; sourceTree = DEVELOPER_DIR; };
148148
DF293C9619DB865800799011 /* QNCrc32Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNCrc32Test.m; sourceTree = "<group>"; };
149149
DF293C9C19DBC2AE00799011 /* QNUserAgent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNUserAgent.h; sourceTree = "<group>"; };
150150
DF293C9D19DBC2AE00799011 /* QNUserAgent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNUserAgent.m; sourceTree = "<group>"; };

QiniuSDK/Common/QNVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
/**
1212
* sdk 版本
1313
*/
14-
static const NSString *kQiniuVersion = @"7.0.8";
14+
static const NSString *kQiniuVersion = @"7.0.9";

QiniuSDK/Http/QNDns.m

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,52 @@
1111

1212
#import "QNDns.h"
1313

14+
static NSArray *getAddresses(CFHostRef hostRef) {
15+
Boolean lookup = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);
16+
if (!lookup) {
17+
return nil;
18+
}
19+
CFArrayRef addresses = CFHostGetAddressing(hostRef, &lookup);
20+
if (!lookup) {
21+
return nil;
22+
}
23+
24+
char buf[32];
25+
__block NSMutableArray *ret = [[NSMutableArray alloc] init];
26+
27+
// Iterate through the records to extract the address information
28+
struct sockaddr_in *remoteAddr;
29+
for (int i = 0; i < CFArrayGetCount(addresses); i++) {
30+
CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex(addresses, i);
31+
remoteAddr = (struct sockaddr_in *)CFDataGetBytePtr(saData);
32+
33+
if (remoteAddr != NULL) {
34+
const char *p = inet_ntop(AF_INET, &(remoteAddr->sin_addr), buf, 32);
35+
NSString *ip = [NSString stringWithUTF8String:p];
36+
[ret addObject:ip];
37+
NSLog(@"Resolved %u->%@", i, ip);
38+
}
39+
}
40+
return ret;
41+
}
42+
1443
@implementation QNDns
1544

1645
+ (NSArray *)getAddresses:(NSString *)hostName {
17-
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)hostName);
46+
// Convert the hostname into a StringRef
47+
CFStringRef hostNameRef = CFStringCreateWithCString(kCFAllocatorDefault, [hostName UTF8String], kCFStringEncodingASCII);
1848

19-
Boolean lookup = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);
20-
NSArray *addresses = (__bridge NSArray *)CFHostGetAddressing(hostRef, &lookup);
21-
__block NSMutableArray *ret = [[NSMutableArray alloc] init];
22-
[addresses enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
23-
struct in_addr *data = (__bridge struct in_addr *)obj;
24-
char buf[32];
25-
const char *p = inet_ntop(AF_INET, (void *)data, buf, 32);
26-
NSString *ip = [NSString stringWithUTF8String:p];
27-
[ret addObject:ip];
28-
// NSLog(@"Resolved %lu->%@", (unsigned long)idx, ip);
29-
}];
49+
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, hostNameRef);
50+
NSArray *ret = getAddresses(hostRef);
51+
52+
CFRelease(hostRef);
53+
CFRelease(hostNameRef);
3054
return ret;
3155
}
3256

3357
+ (NSString *)getAddressesString:(NSString *)hostName {
3458
NSArray *result = [QNDns getAddresses:hostName];
35-
if (result.count == 0) {
59+
if (result == nil || result.count == 0) {
3660
return @"";
3761
}
3862
return [result componentsJoinedByString:@";"];

QiniuSDK/Http/QNHttpManager.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#import "QNHttpManager.h"
1313
#import "QNUserAgent.h"
1414
#import "QNResponseInfo.h"
15-
#import "QNDns.h"
1615

1716
@interface QNHttpManager ()
1817
@property (nonatomic) AFHTTPRequestOperationManager *httpManager;

QiniuSDK/Http/QNResponseInfo.m

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99

1010
#import "QNResponseInfo.h"
11-
#import "QNDns.h"
1211

1312
const int kQNFileError = -4;
1413
const int kQNInvalidArgument = -3;
@@ -57,9 +56,6 @@ - (instancetype)initWithStatus:(int)status
5756
_error = error;
5857
_host = host;
5958
_duration = duration;
60-
if (error.code != -1003) {
61-
_serverIp = [QNDns getAddressesString:host];
62-
}
6359
}
6460
return self;
6561
}
@@ -85,7 +81,6 @@ - (instancetype)init:(int)status
8581
_host = [host copy];
8682
_duration = duration;
8783
if (status != 200) {
88-
_serverIp = [QNDns getAddressesString:host];
8984
if (body == nil) {
9085
_error = [[NSError alloc] initWithDomain:domain code:_statusCode userInfo:nil];
9186
}
@@ -112,7 +107,7 @@ - (instancetype)init:(int)status
112107
}
113108

114109
- (NSString *)description {
115-
return [NSString stringWithFormat:@"<%@: %p, status: %d, requestId: %@, xlog: %@, xvia: %@, host: %@ duration:%f s serverIp:%@ error: %@>", NSStringFromClass([self class]), self, _statusCode, _reqId, _xlog, _xvia, _host, _duration, _serverIp, _error];
110+
return [NSString stringWithFormat:@"<%@: %p, status: %d, requestId: %@, xlog: %@, xvia: %@, host: %@ duration:%f s error: %@>", NSStringFromClass([self class]), self, _statusCode, _reqId, _xlog, _xvia, _host, _duration, _error];
116111
}
117112

118113
- (BOOL)isCancelled {

QiniuSDK/Http/QNSessionManager.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#import "QNSessionManager.h"
1313
#import "QNUserAgent.h"
1414
#import "QNResponseInfo.h"
15-
#import "QNDns.h"
1615
#import "QNAsyncRun.h"
1716

1817
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1090)

QiniuSDKTests/QNDnsTest.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ - (void)testQiniu {
2121
NSString* ip = [[QNDns getAddresses:host] objectAtIndex:0];
2222
XCTAssert(ip != nil, @"Pass");
2323
NSLog(@"dns result %@", ip);
24-
24+
}
25+
26+
- (void)testNoHost {
2527
NSString* nohost = @"nodns.qiniu.com";
2628
NSArray* noip = [QNDns getAddresses:nohost];
2729
XCTAssert(noip.count == 0, @"Pass");

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,18 @@ pod "Qiniu", "~> 7.0"
4242
4343
## 测试
4444
45+
### 所有测试
46+
4547
``` bash
4648
$ xctool -workspace QiniuSDK.xcworkspace -scheme "QiniuSDK Mac" -sdk macosx -configuration Release test -test-sdk macosx
4749
```
50+
### 指定测试
51+
52+
可以在单元测试上修改,熟悉SDK
53+
54+
``` bash
55+
$ xctool -workspace QiniuSDK.xcworkspace -scheme "QiniuSDK Mac" -sdk macosx -configuration Debug test -test-sdk macosx -only "QiniuSDK MacTests:QNResumeUploadTest/test500k"
56+
```
4857

4958
## 常见问题
5059

0 commit comments

Comments
 (0)