上次提到過由于電信的問題需要自己手動去解析dns,這里介紹的是如何攔截
每一個請求做解析,但是沒有說具體的解析方法,下面簡單的記錄一下:
- res_query方法
int res_query(char *domain_name, int class, int type, char *answer_buffer, int answer_buffer_length)
這是比較常見的系統調用,使用該方法的時候需要在Xcode中添加libresolv.dylib,然后包含resolv.h頭文件即可,具體代碼如下:
unsigned char res[512];
int nBytesRead = 0;
//調用系統方法
nBytesRead = res_query("www.baidu.com", ns_c_in, ns_t_a, res, sizeof(res));
ns_msg handle;
ns_initparse(res, nBytesRead, &handle);
NSMutableArray *ipList = nil;
int msg_count = ns_msg_count(handle, ns_s_an);
if (msg_count > 0) {
ipList = [[NSMutableArray alloc] initWithCapacity:msg_count];
for(int rrnum = 0; rrnum < msg_count; rrnum++) {
ns_rr rr;
//解析結果
if(ns_parserr(&handle, ns_s_an, rrnum, &rr) == 0) {
char ip1[16];
strcpy(ip1, inet_ntoa(*(struct in_addr *)ns_rr_rdata(rr)));
NSString *ipString = [[NSString alloc] initWithCString:ip1 encoding:NSASCIIStringEncoding];
if (![ipString isEqualToString:@""]) {
//將提取到的IP地址放到數組中
[ipList addObject:ipString];
}
}
}
}
然而該方法有一個問題,在網絡從2/3G和WI-FI之間切換的時候,該方法經常不能正常工作,或者需要等待較長的時間,
- gethostbyname
struct hostent *gethostbyname(const char *hostName);
具體代碼如下:
struct hostent *host = gethostbyname("www.google.com.hk");
struct in_addr **list = (struct in_addr **)host->h_addr_list;
//獲取IP地址
NSString *ip= [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding];
NSLog(@"ip address is : %@",ip);
該方法在碰到切換網絡的時候,出現失敗的情況比上面的方法好多了,但偶爾也還是會出現,是時候采用蘋果自己的方法了。
- CFHostStartInfoResolution
Boolean CFHostStartInfoResolution (CFHostRef theHost, CFHostInfoType info, CFStreamError *error);
具體實現方法如下:
Boolean result,bResolved;
CFHostRef hostRef;
CFArrayRef addresses = NULL;
CFStringRef hostNameRef = CFStringCreateWithCString(kCFAllocatorDefault, "www.google.com.hk", kCFStringEncodingASCII);
hostRef = CFHostCreateWithName(kCFAllocatorDefault, hostNameRef);
if (hostRef) {
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);
if (result == TRUE) {
addresses = CFHostGetAddressing(hostRef, &result);
}
}
bResolved = result == TRUE ? true : false;
if(bResolved)
{
struct sockaddr_in* remoteAddr;
for(int i = 0; i < CFArrayGetCount(addresses); i++)
{
CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex(addresses, i);
remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData);
if(remoteAddr != NULL)
{
//獲取IP地址
char ip[16];
strcpy(ip, inet_ntoa(remoteAddr->sin_addr));
}
}
}
CFRelease(hostNameRef);
CFRelease(hostRef);
具體的demo可以到這里看看