Getting DNS server IP on IPhone

前端 未结 2 1864
孤城傲影
孤城傲影 2021-01-14 05:28

I\'m trying to get the my dns server IP using using libresolv framework , but all I get is \"0.0.0.0\"

-(void) getDns
{
    res_init();

    for         


        
相关标签:
2条回答
  • 2021-01-14 06:02

    You are not resolving anything, you are just printing the address stored in some variable. You actually need to call res_query or res_search. But on iOS, you're better off with using the CFHost* methods, like in this question. There's also sample code from Apple (search the MyResolveNameToAddress function).

    Update after understanding the question: According to this question you cannot access the /etc/resolv.conf file (permissions). But according to this question you might succeed by using the SystemConfigFramework but I've got no idea whether it works on iOS (the framework exists, but whether that information is exposed I don't know).

    0 讨论(0)
  • 2021-01-14 06:20

    Try below code to get DNS Server Address . Don't forget to link libresolv.lib

        #include <arpa/inet.h>
        #include <ifaddrs.h>
        #include <resolv.h>
        #include <dns.h>
    
        - (NSString *) getDNSServers
        {
        // dont forget to link libresolv.lib
        NSMutableString *addresses = [[NSMutableString alloc]initWithString:@"DNS Addresses \n"];
    
        res_state res = malloc(sizeof(struct __res_state));
    
        int result = res_ninit(res);
    
        if ( result == 0 )
        {
            for ( int i = 0; i < res->nscount; i++ )
            {
                NSString *s = [NSString stringWithUTF8String :  inet_ntoa(res->nsaddr_list[i].sin_addr)];
                [addresses appendFormat:@"%@\n",s];
                NSLog(@"%@",s);
            }
        }
        else
            [addresses appendString:@" res_init result != 0"];
    
        return addresses;
    }
    
    0 讨论(0)
提交回复
热议问题