Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform DNS query on iOS

Tags:

ios

iphone

ipad

i want to perform some DNS queries e.g. to get IP records against a specific domain name, i am looking for a preferred way or some useful snippet for this on iOS 3.2+ SDK. thanx in advance

part from other snippets i found this code

 Boolean result;
 CFHostRef hostRef;
 NSArray *addresses;
 NSString *hostname = @"apple.com";
 hostRef = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostname);
 if (hostRef) {
      result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
      if (result == TRUE) {
           addresses = (NSArray*)CFHostGetAddressing(hostRef, &result);
      }
 }
 if (result == TRUE) {
      [addresses enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
             NSString *strDNS = [NSString stringWithUTF8String:inet_ntoa(*((struct in_addr *)obj))];
           NSLog(@"Resolved %d->%@", idx, strDNS);
      }];

 } else {
      NSLog(@"Not resolved");
 }

but this is producing same IP for every host Resolved 0->220.120.64.1 any help??

like image 725
yasirmturk Avatar asked Feb 15 '11 06:02

yasirmturk


People also ask

Can you do an nslookup on an iPhone?

NSLookup for iPhone and iPad allows you to see the public DNS records of domains or sub domains. Then you may also query the NS and CNAME records. It's also possible to force it to get an authoritative response from main DNS server of the domain.

How do I run a DNS query?

Name Server lookup Access your command prompt. Use the command nslookup (this stands for Name Server Lookup) followed by the domain name or IP address you want to trace. Press enter. This command will simply query the Name Service for information about the specified IP address or domain name.

Does iPhone have DNS settings?

You can configure DNS Settings settings for users of an iPhone, iPad, Shared iPad, or Mac enrolled in a mobile device management (MDM) solution. Use the DNS Settings payload to specify apps that must use specific DNS settings.

How do I find the DNS on my iPhone?

On an Apple iPhone or iPad, you can view or change your DNS settings through iOS. Open the "Settings" app and tap "Wi-Fi." Find the network you want to configure and tap the "i" icon next to it. Tap "Configure DNS" to see and edit your DNS settings.


1 Answers

Figured out a change in this snippet makes it working

if (result == TRUE) {
        NSMutableArray *tempDNS = [[NSMutableArray alloc] init];
        for(int i = 0; i < CFArrayGetCount(addresses); i++){
            struct sockaddr_in* remoteAddr;
            CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex(addresses, i);
            remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData);

            if(remoteAddr != NULL){
                // Extract the ip address
                //const char *strIP41 = inet_ntoa(remoteAddr->sin_addr);
                NSString *strDNS =[NSString stringWithCString:inet_ntoa(remoteAddr->sin_addr) encoding:NSASCIIStringEncoding];
                NSLog(@"RESOLVED %d:<%@>", i, strDNS);
                [tempDNS addObject:strDNS];
            }
        }
}
like image 126
yasirmturk Avatar answered Sep 25 '22 19:09

yasirmturk