Skip to content

Commit 8075b8b

Browse files
committed
fixed dns
1 parent 944e5bb commit 8075b8b

File tree

6 files changed

+50
-21
lines changed

6 files changed

+50
-21
lines changed

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 & 5 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
}
@@ -112,7 +108,7 @@ - (instancetype)init:(int)status
112108
}
113109

114110
- (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];
111+
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];
116112
}
117113

118114
- (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)