[comp.sources.misc] v06i012: reverse hostname lookup program

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (01/24/89)

Posting-number: Volume 6, Issue 12
Submitted-by: koreth@ssyx.UCSC.EDU (Steven Grimm)
Archive-name: hname

This is primarily useful for sites running the Berkeley Internet Name Daemon
or some other form of Internet name server; it looks up a host name given
the host's internet address.  It looks up each of the arguments, or prompts
for addresses if no arguments are given.  Example:

% hname 128.114.133.1
128.114.133.1 = SSYX.UCSC.EDU

This program isn't robust or complex or anything, but it works.  Note that
your gethostbyaddr() routine must use the nameserver for this to work.
---
These are my opinions, which you can probably ignore if you want to.
Steven Grimm		Moderator, comp.{sources,binaries}.atari.st
koreth@ssyx.ucsc.edu	uunet!ucbvax!ucscc!ssyx!koreth

#-------------- cut here -------------
#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  hname.c
#
sed 's/^X//' << 'SHAR_EOF' > hname.c &&
X#include <ctype.h>
X#include <stdio.h>
X#include <sys/types.h>
X#include <netdb.h>
X#include <sys/socket.h>
X
Xmain(argc, argv)
Xchar **argv;
X{
X	int i;
X	if (argc == 1)
X	{
X		char string[80];
X
X		while (! feof(stdin))
X		{
X			printf("> ");
X			fflush(stdout);
X			lookup(gets(string));
X		}
X	}
X	else
X		for (i=1; i<argc; i++)
X			lookup(argv[i]);
X}
X
Xlookup(string)
Xchar *string;
X{
X	struct hostent *hp, *gethostbyaddr();
X	long		addr, inet_addr();
X
X	if (! string || ! isdigit(string[0]))
X		return;
X	addr = inet_addr(string);
X	hp = gethostbyaddr(&addr, sizeof(addr), AF_INET);
X	if (hp == (struct hostent *)NULL)
X		printf("%s not found\n", string);
X	else
X		printf("%s = %s\n", string, hp->h_name);
X}
X
SHAR_EOF
chmod 0600 hname.c || echo "restore of hname.c fails"
exit 0