2020-05-20

DNS解析三种实现

需要包含的头文件
#import 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
1.使用res_query
int res_query(char *domain_name, int class, int type, char *answer_buffer, int answer_buffer_length)

代码实现

CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    unsigned char auResult[512];
    int nBytesRead = 0;
    
    nBytesRead = res_query("www.baidu.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(@"res_query ip :%@ , time cost: %0.3fs", ipList,end - start);
    }

缺点:在网络从2/3G和WI-FI之间切换的时候,该方法经常不能正常工作,或者需要等待较长的时间

2.使用gethostbyname
struct hostent *gethostbyname(const char *hostName);

代码实现

    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    
    char   *ptr, **pptr;
    struct hostent *hptr;
    char   str[32];
    ptr = "www.baidu.com";
    NSMutableArray * ips = [NSMutableArray array];
    
    if((hptr = gethostbyname(ptr)) == NULL)
    {
        printf("no ips %s", ptr);
        return;
    }
    
    printf("official hostname:%s\n",hptr->h_name);
    for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
        printf(" alias hostname:%s\n",*pptr);
    
    switch(hptr->h_addrtype)
    {
        case AF_INET:
        case AF_INET6:
            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?:@""];
            }
//                printf(" address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
//            printf(" first address: %s\n",inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));
            break;
        default:
//            printf("unknown address type\n");
            break;
    }
    CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
    NSLog(@"gethostbyname ip : %@ , time cost: %0.3fs", ips,end - start);

缺点:切换网络的时候,出现失败的情况比res_query的方法好多了,但偶尔也还是会出现

3.苹果自带的CFHostStartInfoResolution
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.baidu.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(@"CFHostStartInfoResolution ip : %@ ,time cost: %0.3fs", ipsArr,end - start);
    CFRelease(hostNameRef);
    CFRelease(hostRef);

你可能感兴趣的:(2020-05-20)