IP Address to Hostname (gethostbyaddr)

IP Address to Hostname (gethostbyaddr)

Post by Mike Wils » Sat, 08 Feb 1997 04:00:00



        I am attempting to figure out a host name (www.flash.net/mike.flash.net
etc...) knowing the IP address (111.111.111.111 etc...) however I've had little
luck in doing so. I've been trying to decipher the man pages on this but have
yet to get it to work.

Anyone have hints on this?

--
---
Mike Wilson,                          817-332-8883
CTO, Head of Systems Administration   http://www.flash.net/~mwilson

 
 
 

IP Address to Hostname (gethostbyaddr)

Post by Andrew Giert » Sun, 09 Feb 1997 04:00:00


 Mike> I am attempting to figure out a host name
 Mike> (www.flash.net/mike.flash.net etc...) knowing the IP address
 Mike> (111.111.111.111 etc...) however I've had little luck in doing
 Mike> so. I've been trying to decipher the man pages on this but have
 Mike> yet to get it to work.

Is this any help?

/* byaddr.c */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>

main(int argc, char **argv)
{
    struct in_addr addr;
    struct hostent *host;
    char **argp = argv;

    if (argc < 2)
    {
        fprintf(stderr,"Usage: %s addresses...\n",argv[0]);
        return 1;
    }

    while (*++argp)
    {
        addr.s_addr = inet_addr(*argp);
        host = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);

        if (!host)
            printf("host %s not found\n",*argp);
        else
            printf("host %s found: name='%s'\n", *argp, host->h_name);
    }

    return 0;

Quote:}

--
Andrew.