Gusev Andrey
Gusev Andrey

Reputation: 456

Resolve NetBIOS name on iOS

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.

Upvotes: 13

Views: 2291

Answers (1)

Liam Kelly
Liam Kelly

Reputation: 3714

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).

Upvotes: 1

Related Questions