5

One task of my program is to scan local wi-fi network for any devices/computers in same network. I found solution to get all working devices IPs, but did not managed to get names of them. I did not find any clue to solve this problem. Any suggestions?

Nikolay Shubenkov
  • 3,133
  • 1
  • 29
  • 31
  • How did you get the list of all working devices? what was the code used for scanning. I tried whole lot of code from the internet but couldnt acheive this @Nikolay Shubenkov – Alejandro Vargas Oct 29 '14 at 12:14
  • Detect your IP. I your IP is, for example 192.168.2.44, then ping all IPs with addresses from 192.168.2.1 to 192.168.2.255. if you need code, create question on StackOverflow, post link to that question and I will post project to github and put a link to that project in answer to your question. – Nikolay Shubenkov Oct 30 '14 at 11:09
  • Here is the [link](http://stackoverflow.com/questions/29545847/cfhoststartinforesolution-fails-get-device-name-from-ip-address-on-lan) to that question. @Nikolay Shubenkov – Alejandro Vargas Jun 11 '15 at 09:18
  • @NikolayShubenkov you got succeed for get computer name from IP address ? – Darshan Kunjadiya Sep 16 '16 at 10:47
  • Unfortunately no. At least now. This is quite old question and I can not recheck this as project with code just do not compiles. – Nikolay Shubenkov Sep 16 '16 at 11:46
  • Had any one get succeed to fetch hostname? – Mehsam Saeed Nov 26 '18 at 14:24

2 Answers2

4

In order to perform a reverse DNS lookup, you need to call the CFHostGetNames function, like this:

+ (NSArray *)hostnamesForIPv4Address:(NSString *)address
{
    struct addrinfo *result = NULL;
    struct addrinfo hints;

    memset(&hints, 0, sizeof(hints));
    hints.ai_flags = AI_NUMERICHOST;
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;

    int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
    if (errorStatus != 0) {
        return nil;
    }

    CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
    if (addressRef == nil) {
        return nil;
    }
    freeaddrinfo(result);

    CFHostRef hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, addressRef);
    if (hostRef == nil) {
        return nil;
    }
    CFRelease(addressRef);

    BOOL succeeded = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL);
    if (!succeeded) {
        return nil;
    }

    NSMutableArray *hostnames = [NSMutableArray array];

    CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
    for (int currentIndex = 0; currentIndex < [(__bridge NSArray *)hostnamesRef count]; currentIndex++) {
        [hostnames addObject:[(__bridge NSArray *)hostnamesRef objectAtIndex:currentIndex]];
    }

    return hostnames;
}
neilco
  • 7,964
  • 2
  • 36
  • 41
  • 1
    i tried your code but i am always getting null value from that function. I am trying to get computer name in iOS app.Can you please suggest me right way to found computer name. Thanks – Darshan Kunjadiya Sep 16 '16 at 10:40
  • above function fail to get hostname for some network at this point and return null . BOOL succeeded = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL); if (!succeeded) { return nil; } . any idea why – Mehsam Saeed Nov 26 '18 at 14:13
0

BOOL succeeded = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL); Now I encounter that always failed at this line, and I tried to use getnameinfo function, it is still can't get the hostname

carya.liu
  • 51
  • 1
  • 8