前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS 下三种 DNS 解析方式分析(LocalDns)

IOS 下三种 DNS 解析方式分析(LocalDns)

作者头像
molier
发布2022-11-02 14:03:55
3.3K0
发布2022-11-02 14:03:55
举报
文章被收录于专栏:Molier的小站

# 背景

最近在做 iOS 的 DNS 解析,顺便研究了下 iOS 端本地的 DNS 解析方式(localDNS), 也就是不依赖 Http 请求,而是用原始的 API 进行解析,虽然有 HttpDNS 但是考虑到成本、第三方服务稳定性的问题,LocalDNS 仍然是一个很重要的部分,在 iOS 系统下,localDNS 的解析方式有三种,下面主要对三种方式进行下利弊分析及简单的原理介绍。

# 方式一

这个也是我一开始在项目中使用的方式。

代码语言:javascript
复制
1:struct hostent	*gethostbyname(const char *);
2:struct hostent	*gethostbyname2(const char *, int);

两个函数作用完全一样,返回值一样,但是第一个只能用于 IPV4 的网络环境,而第二个则 IPV4 和 IPV6 都可使用,可以通过第二个参数传入当前的网络环境。

# 使用方式:
代码语言:javascript
复制
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:

代码语言:javascript
复制
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 的别名就是他自己。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。
  • 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。详细使用见上文
# 缺点:
  • 在进行网络切换的时候小概率卡死,自测十次有一两次左右。
  • 在本地的 LocalDns 被破坏的时候会必卡死 30 秒,然后返回 nil 。
  • 缓存是个玄学东西,他会对自己解析出来的 IP 进行缓存(可能是运营商缓存)缓存时间不确定,有可能我即使切换了无数个网络,但是从早到晚同一个域名总是解析出同样的 IP,
  • 网上说的比较多的问题
image.png
image.png

# 方式二

除了经常用到的 gethostbyname (3) 和 gethostbyaddr (3) 函数以外,Linux (以及其它 UNIX/UNIX-like 系统) 还提供了一套用于在底层处理 DNS 相关问题的函数 (这里所说的底层仅是相对 gethostbyname 和 gethostbyaddr 两个函数而言). 这套函数被称为地址解析函数 (resolver functions)。曾经尝试过这个方式...

代码语言:javascript
复制
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 服务器。

# 使用方式
代码语言:javascript
复制
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 内核的理解,这里放一篇参考资料

# 优点:
  • 在 LocalDns 被破坏掉的情况下能及时响应不会延迟。
  • 没有缓存,缓存由开发者控制
# 缺点
  • 在进行网络切换时候 3G/4G 切 wify 高概率出现卡死 这一个缺点是比较致命的,所以没有再继续使用。

# 方式三

苹果原生的 DNS 解析

代码语言:javascript
复制
Boolean CFHostStartInfoResolution (CFHostRef theHost, CFHostInfoType info, CFStreamError *error);
# 使用方法:
代码语言:javascript
复制
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);
# 参数解析:
代码语言:javascript
复制
/*
 *  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);
# 优点:
  • 在网络切换时候不会卡顿。
# 缺点:
  • 在本地 DNS 被破坏的情况下会出现卡死的现象 (卡 30s)

# 总结:

以上三个方法除了第二个方法会在网络切换时候卡死不可用之外,其他两个方法都是可选择的,关于那个本地 LocalDns 破坏会卡死的问题看来是无法避免,不过开发者可以自行通过 ping 等方式来判断 LocalDns 的正确性,在被破坏的情况下使用 httpDns 来进行解析即可。具体的 demo 可以到这里查看

DNS解析 LocalDNS

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-01-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # 背景
  • # 方式一
  • # 方式二
  • # 方式三
  • # 总结:
相关产品与服务
移动解析 HTTPDNS
移动解析 HTTPDNS 基于 HTTP 协议向腾讯云的 DNS 服务器发送域名解析请求,替代了基于 DNS 协议向运营商 Local DNS 发起解析请求的传统方式,可以避免 Local DNS 造成的域名劫持和跨网访问问题,解决移动互联网服务中域名解析异常带来的困扰。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档