12

Is there any way on iOS to resolve NetBIOS name using IP address?

One could resolve NetBIOS name on a Mac using terminal command:

smbutil -v status -ae

The smbutil is opensourced so you can find it here: http://www.apple.com/opensource/ -> http://opensource.apple.com//source/smb/

And the same on GitHub: https://github.com/unofficial-opensource-apple/smb

But the util extensively uses private and Mac OS-specific frameworks so Xcode couldn't even compile the source for iOS.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Gusev Andrey
  • 446
  • 5
  • 23
  • Have you tried isolating the Apple smb code to that needed only for name resolution? That looks to be substantially less code, just that needed to support `nbns_resolvename`: https://github.com/unofficial-opensource-apple/smb/search?utf8=%E2%9C%93&q=nbns_resolvename – Jeremy W. Sherman Feb 13 '17 at 14:15

1 Answers1

0

I think you just need to do a reverse DNS lookup, gethostbyaddr, because it looks like DNS, gethostbyname, is the only thing being used to go from NetBios name to IP.

Looking at the source from Apple, the smbutil application in the 'status' mode first calls 'nb_resolvehost' which just uses 'gethostbyname' to get the IP address from NetBios name. 'gethostbyname' uses DNS under the hood:

nb_resolvehost_in(const char *name, struct sockaddr **dest)
{
    struct hostent* h;
    struct sockaddr_in *sinp;
    int len;

    h = gethostbyname(name);
    if (!h) {
        warnx("can't get server address `%s': ", name);
        herror(NULL);
        return ENETDOWN;
    }
    if (h->h_addrtype != AF_INET) {
        warnx("address for `%s' is not in the AF_INET family", name);
        return EAFNOSUPPORT;
    }
    if (h->h_length != 4) {
        warnx("address for `%s' has invalid length", name);
        return EAFNOSUPPORT;
    }
    len = sizeof(struct sockaddr_in);
    sinp = malloc(len);
    if (sinp == NULL)
        return ENOMEM;
    bzero(sinp, len);
    sinp->sin_len = len;
    sinp->sin_family = h->h_addrtype;
    memcpy(&sinp->sin_addr.s_addr, h->h_addr, 4);
    sinp->sin_port = htons(SMB_TCP_PORT);
    *dest = (struct sockaddr*)sinp;
    return 0;
}

After this call the 'smbutil status' then sends some sort of SMB query using the function 'nbns_getnodestatus' to the previously given IP address on the SMB port. From this query the command gets Workgroup and Servername (not sure which server this is referring to).

Liam Kelly
  • 3,524
  • 1
  • 17
  • 41
  • Can you please take look at this [thread](https://developer.apple.com/forums/thread/679455)? Do you have any idea how to resolve this issue of getting host name from network scan of connected wi-fi? Other apps on App Store like Fing, Net Analyzer are able to get host name. – Chintan Shah May 06 '21 at 11:26
  • @ChintanShah if you are already enumerating IP addresses, you can just reverse DNS the addresses as seen [here](https://stackoverflow.com/questions/23258464/getnameinfo-reverse-dns-lookup-ip-address-to-hostname-c-c?). A general warning though, enumerating the network can get your app flagged as malware, might want to look into multicast instead. – Liam Kelly May 10 '21 at 19:21