最近在作iOS的DNS解析,順便研究了下iOS端本地的DNS解析方式(localDNS),也就是不依賴Http請求,而是用原始的API進行解析,雖然有HttpDNS可是考慮到成本、第三方服務穩定性的問題,LocalDNS仍然是一個很重要的部分,在iOS系統下,localDNS的解析方式有三種,下面主要對三種方式進行下利弊分析及簡單的原理介紹。html
這個也是我一開始在項目中使用的方式。git
1:struct hostent *gethostbyname(const char *);
2:struct hostent *gethostbyname2(const char *, int);
複製代碼
兩個函數做用徹底同樣,返回值同樣,可是第一個只能用於IPV4的網絡環境,而第二個則IPV4和IPV6均可使用,能夠經過第二個參數傳入當前的網絡環境。github
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
char *ptr, **pptr;
struct hostent *hptr;
char str[32];
ptr = "www.meitu.com";
NSMutableArray * ips = [NSMutableArray array];
if((hptr = gethostbyname(ptr)) == NULL)
{
return;
}
for(pptr=hptr->h_addr_list; *pptr!=NULL; pptr++) {
NSString * ipStr = [NSString stringWithCString:inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)) encoding:NSUTF8StringEncoding];
[ips addObject:ipStr?:@""];
}
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"22222 === ip === %@ === time cost: %0.3fs", ips,end - start);
複製代碼
使用gethostbyname方法後會獲得一個struct,也就是上文的struct hostent *hptr:數組
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses from name server */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define h_addr h_addr_list[0] /* address, for backward compatibility */
#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
};
複製代碼
hostent->h_name 表示的是主機的規範名。例如www.baidu.com的規範名實際上是www.a.shifen.com。緩存
hostent->h_aliases 表示的是主機的別名www.baidu.com的別名就是他本身。有的時候,有的主機可能有好幾個別名,這些,其實都是爲了易於用戶記憶而爲本身的網站多取的名字。bash
hostent->h_addrtype
表示的是主機ip地址的類型,究竟是ipv4(AF_INET),仍是pv6(AF_INET6)服務器
hostent->h_length
表示的是主機ip地址的長度網絡
hostent->h_addr_lisst 表示的是主機的ip地址,注意,這個是以網絡字節序存儲的。不要直接用printf帶%s參數來打這個東西,會有問題的哇。因此到真正須要打印出這個IP的話,須要調用const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
,來把它轉成char。詳細使用見上文async
在進行網絡切換的時候小几率卡死,自測十次有一兩次左右。函數
在本地的LocalDns被破壞的時候會必卡死30秒,而後返回nil 。
緩存是個玄學東西,他會對本身解析出來的IP進行緩存(多是運營商緩存)緩存時間不肯定,有可能我即便切換了無數個網絡,可是從早到晚同一個域名老是解析出一樣的IP,
網上說的比較多的問題
除了常常用到的gethostbyname(3)和gethostbyaddr(3)函數之外, Linux(以及其它UNIX/UNIX-like系統)還提供了一套用於在底層處理DNS相關問題的函數(這裏所說的底層僅是相對gethostbyname和gethostbyaddr兩個函數而言). 這套函數被稱爲地址解析函數(resolver functions)。曾經嘗試過這個方式...
int res_query __P((const char *, int, int, u_char *, int));
函數原型爲:
int res_query(const char *dname, int class, int type, unsigned char *answer, int anslen)
複製代碼
這個方式須要在項目中添加libresolv.tbd
庫,由於要依賴於庫中的函數去解析。res_query
用來發出一個指定類(由參數class指定)和類型(由參數type指定)的DNS詢問. dname是要查詢的主機名. 返回信息被存儲在answser指向的內存區域中. 信息的長度不能大於anslen個字節. 這個函數會建立一個DNS查詢報文並把它發送到指定的DNS服務器。
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
unsigned char auResult[512];
int nBytesRead = 0;
nBytesRead = res_query("www.meitu.com", ns_c_in, ns_t_a, auResult, sizeof(auResult));
ns_msg handle;
ns_initparse(auResult, 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];
}
}
}
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"11111 === ip === %@ === time cost: %0.3fs", ipList,end - start);
}
複製代碼
因爲該邏輯是Linux底層提供的代碼,蘋果用宏作了一次封裝,具體的函數含義還須要對Linux內核的理解,這裏放一篇參考資料
蘋果原生的DNS解析
Boolean CFHostStartInfoResolution (CFHostRef theHost, CFHostInfoType info, CFStreamError *error);
複製代碼
Boolean result,bResolved;
CFHostRef hostRef;
CFArrayRef addresses = NULL;
NSMutableArray * ipsArr = [[NSMutableArray alloc] init];
CFStringRef hostNameRef = CFStringCreateWithCString(kCFAllocatorDefault, "www.meitu.com", kCFStringEncodingASCII);
hostRef = CFHostCreateWithName(kCFAllocatorDefault, hostNameRef);
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
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));
NSString * ipStr = [NSString stringWithCString:ip encoding:NSUTF8StringEncoding];
[ipsArr addObject:ipStr];
}
}
}
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"33333 === ip === %@ === time cost: %0.3fs", ipsArr,end - start);
CFRelease(hostNameRef);
CFRelease(hostRef);
複製代碼
/*
* CFHostStartInfoResolution()
*
* Discussion:
* Performs a lookup for the given host. It will search for the
* requested information if there is no other active request.
* Previously cached information of the given type will be released.
*
* Mac OS X threading:
* Thread safe
*
* Parameters:
*
* theHost: //須要被解決的CFHostRef的對象
* The CFHostRef which should be resolved. Must be non-NULL. If
* this reference is not a valid CFHostRef, the behavior is
* undefined.
*
* info: 返回值的類型 數組/Data/string..
* The enum representing the type of information to be retrieved.
* If the value is not a valid type, the behavior is undefined.
*
* error: 錯誤
* A reference to a CFStreamError structure which will be filled
* with any error information should an error occur. May be set
* to NULL if error information is not wanted.
*
* Result: 解析結果成功仍是失敗
* Returns TRUE on success and FALSE on failure. In asynchronous
* mode, this function will return immediately. In synchronous
* mode, it will block until the resolve has completed or until the
* resolve is cancelled.
*
*/
CFN_EXPORT __nullable CFArrayRef
CFHostGetAddressing(CFHostRef theHost, Boolean * __nullable hasBeenResolved) CF_AVAILABLE(10_3, 2_0);
複製代碼
以上三個方法除了第二個方法會在網絡切換時候卡死不可用以外,其餘兩個方法都是可選擇的,關於那個本地LocalDns破壞會卡死的問題看來是沒法避免,不過開發者能夠自行經過ping等方式來判斷LocalDns的正確性,在被破壞的狀況下使用httpDns來進行解析便可。具體的Demo能夠到這裏查看 個人簡書同步跟新