Path: wupost!unlinfo.unl.edu!moe.ksu.ksu.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ira.uka.de!math.fu-berlin.de!mailgzrz!mailgzrz.tu-berlin.de!nickel
From: nickel@cs.tu-berlin.de (Juergen Nickelsen)
Newsgroups: alt.sources
Subject: nslook -- simple Domain Name Service lookup
Message-ID: <NICKEL.91Sep10194519@desaster.cs.tu-berlin.de>
Date: 10 Sep 91 17:45:19 GMT
Sender: news@mailgzrz.tu-berlin.de
Reply-To: nickel@cs.tu-berlin.de
Distribution: world
Organization: STONE Project, Technical University of Berlin, Germany
Lines: 381
Nntp-Posting-Host: desaster.cs.tu-berlin.de

From the README file:

nslook is a simple form of nslookup. It only gives you what you want
to see: The IP address of a host, or the hostname of an IP address.

Usage: nslook [-l] hostname	   
			to give you an IP address from a name
       nslook [-l] -a IP-address
			to give you a name from an IP address

The flag "-l" gives a long output.

If you have any comments, suggestions, complaints, improvements,
please send them to me: Juergen Nickelsen <nickel@cs.tu-berlin.de>.


#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	README
#	nslook.1
#	Makefile
#	nslook.c
# This archive created: Tue Sep 10 18:32:30 1991
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'README'
then
	echo shar: "will not over-write existing file 'README'"
else
cat << \SHAR_EOF > 'README'
To build nslook, type "make" on BSD Systems or "make sysv" on System V.
It is tested on ISC 386/ix (sysv), BSD 4.3 and SunOS 4.1.1.

I don't know anything about other systems, but:
If ld complains about missing gethostbyname, gethostbyaddr, and
h_errno, you probably have to link with libresolv.a or some other
library containing these, if you have it.

nslook is a simple form of nslookup. It only gives you what you want
to see: The IP address of a host, or the hostname of an IP address.

Usage: nslook [-l] hostname	   
			to give you an IP address from a name
       nslook [-l] -a IP-address
			to give you a name from an IP address

The flag "-l" gives a long output.

Some examples of use:

coma: /u/nickel) nslook ucbvax.berkeley.edu
128.32.133.1
coma: /u/nickel) nslook -l ucbvax.berkeley.edu
Official name: ucbvax.berkeley.edu
Address:       128.32.133.1
Address:       128.32.130.12
Address:       128.32.149.36
Address:       128.32.137.3
coma: /u/nickel) nslook -a 128.32.133.1
ucbvax.Berkeley.EDU
coma: /u/nickel) nslook -al 128.32.133.1
Official name: ucbvax.Berkeley.EDU
Address:       128.32.133.1
coma: /u/nickel) nslook -l ftp.informatik.uni-dortmund.de
Official name: walhalla.informatik.uni-dortmund.de
Alias:         ftp.informatik.uni-dortmund.de
Address:       129.217.64.63

If you have any comments, suggestions, complaints, improvements,
please send them to me: Juergen Nickelsen <nickel@cs.tu-berlin.de>.

Copyrights? what Copyrights?
SHAR_EOF
fi
if test -f 'nslook.1'
then
	echo shar: "will not over-write existing file 'nslook.1'"
else
cat << \SHAR_EOF > 'nslook.1'
..TH NSLOOK 1 "Sep 10, 1991"
..UC 4
..SH NAME
nslook \- name server lookup
..SH SYNOPSIS
..B nslook
[
..B \-l
]
..I hostname
..br
..B nslook
[
..B \-l
]
..B \-a
..I IP-address
..br
..SH DESCRIPTION
..B Nslook 
provides a simple way to retrieve an IP address or a host name
from the Domain Name Service. The command
..PP
..ti +10n
\fC nslook \fP
..I hostname
..PP
prints the IP address of
..I hostname
on standard output (and nothing else). The command
..PP
..ti +10n
\fC nslook \-a \fP
..I IP-address
..PP
prints the official name of the Internet host with the specified
..I IP-address
on standard output.
..PP
If the
..B \-l
flag is given, 
..B nslook
gives a long format display, showing the hosts official name, zero or
more aliases, and one or more IP addresses.
..PP
..SH DIAGNOSTICS
The diagnostics of 
..B nslook
correspond to the errors signalled by
gethostbyname(3) and gethostbyaddr(3).
..TP
\fC unknown host \fP
..TP
\fC temporary error -- try again later \fP
This usually indicates that the local server did not receive
a response from an authoritative server.
A retry at some later time may succeed.
..TP
\fC unrecoverable error \fP
Some unexpected server failure was encountered.
..TP
\fC no IP address \fP
The requested name is valid but does not have an IP address; 
this is not a temporary error.  
This means that the name is known to the name server but there is no address
associated with this name.
Another type of request to the name server using this domain name
will result in an answer;
for example, a mail-forwarder may be registered for this domain.
..PP
If one of these errors occurs, 
..B nslook
returns an exit code of 1.
..SH "SEE ALSO"
nslookup(1), gethostbyaddr(3), gethostbyname(3)
..SH BUGS
Normally
..B nslook
does not list alias hostnames since it only uses the information given 
by gethostbyname(3) or gethostbyaddr(3).
..PP
If 
..B nslook
tries to find the address of a non-existent host in the local domain, 
the error ``no IP address'' is signalled. This is due to the return code 
of gethostbyname(3).
..PP
..SH AUTHOR
Juergen Nickelsen  <nickel@cs.tu-berlin.de>
SHAR_EOF
fi
if test -f 'Makefile'
then
	echo shar: "will not over-write existing file 'Makefile'"
else
cat << \SHAR_EOF > 'Makefile'
CC = gcc
# CFLAGS = -g
CFLAGS = -O
SYSTYPE = bsd
# SYSTYPE = sysv

all : $(SYSTYPE)

bsd : ; make nslook DEFINES=-DBSD CC=$(CC) CFLAGS=$(CFLAGS)

sysv : ; make nslook DEFINES=-DSYSV CC=$(CC) CFLAGS=$(CFLAGS) LIBS=-linet

nslook : nslook.c Makefile
	$(CC) $(CFLAGS) $(DEFINES) -o nslook nslook.c $(LIBS)
SHAR_EOF
fi
if test -f 'nslook.c'
then
	echo shar: "will not over-write existing file 'nslook.c'"
else
cat << \SHAR_EOF > 'nslook.c'
#include <sys/types.h>
#include <sys/socket.h>
#ifdef SYSV
#include <sys/param.h>
#include <netinet/in.h>
#endif
#include <netdb.h>
#include <stdio.h>

char *progname ;
extern struct hostent *gethostbyname(), *gethostbyaddr() ;
extern int errno ;

main(argc, argv)
int argc ;
char **argv ;
{
    extern int optind ;		/* from getopt */
    int opt ;			/* option character */
    int aflg = 0 ;		/* address is given if non-zero, else name */
    int lflg = 0 ;		/* long output format */
    int errflg = 0 ;		/* usage error encountered */
    char *cp, *strrchr() ;

    progname = argv[0] ;
    if (cp = strrchr(progname, '/')) progname = cp + 1 ;
    while ((opt = getopt(argc, argv, "la")) != -1) {
	switch (opt) {
	  case 'l':
	    lflg = 1 ;
	    break ;
	  case 'a':
	    aflg = 1 ;
	    break ;
	  case '?':
	    errflg++ ;
	}
    }
    if (errflg || optind != argc - 1) {
	usage() ;
	exit(1) ;
    }

    if (aflg) {
	from_address(argv[optind], lflg) ;
    } else {
	from_name(argv[optind], lflg) ;
    }

    return 0 ;
}


from_address(astring, lflg)
char *astring ;
int lflg ;
{
    unsigned long addr ;	/* address in host order */
    unsigned long norder ;	/* address in network order */
    struct hostent *host ;	/* structure returned by gethostbyaddr() */
    int octet[4] ;		/* the four bytes of the address */

    if (sscanf(astring, "%d.%d.%d.%d", 
	       octet, octet+1, octet+2, octet+3) != 4 ||
	octet[0] > 255 || octet [0] < 0 ||
	octet[1] > 255 || octet [1] < 0 ||
	octet[2] > 255 || octet [2] < 0 ||
	octet[3] > 255 || octet [3] < 0) {
	usage() ;
	exit(1) ;
    }

    norder = (octet[0] << 24) | (octet[1] << 16) | (octet[2] << 8) | octet[3] ;
    addr = ntohl(norder) ;

    if ((host = gethostbyaddr(&addr, 4, AF_INET)) == NULL) {
	herror(progname) ;
	exit(1) ;
    }

    if (lflg) {
	print_long(host) ;
    } else {
	puts(host->h_name) ;
    }
}

from_name(name, lflg)
char *name ;
int lflg ;
{
    struct hostent *host ;

    if ((host = gethostbyname(name)) == NULL) {
	herror(progname) ;
	exit(1) ;
    }

    if (lflg) {
	print_long(host) ;
    } else {
	paddress(*host->h_addr_list) ;
    }
}

print_long(host)
struct hostent *host ;
{
    int i ;
    unsigned long horder ;

    printf("Official name: %s\n", host->h_name) ;
    while (*host->h_aliases) {
	printf("Alias:         %s\n", *host->h_aliases++) ;
    }
    while (*host->h_addr_list) {
	printf("Address:       ") ;
	paddress(*host->h_addr_list++) ;
    }
}

paddress(paddr)
unsigned long *paddr ;
{
    unsigned long horder ;

    horder = ntohl(*paddr) ;
    printf("%u.%u.%u.%u\n", horder >> 24, (horder >> 16) & 0xff, 
	    (horder >> 8) & 0xff, horder & 0xff) ;

}
    

usage()
{
    fprintf(stderr, "Usage: %s [-l] hostname\n       %s [-l] -a IP-address\n",
	    progname, progname) ;
}

herror(s)
char *s ;
{
#ifdef BSD
    extern int h_errno ;
#endif

    if (s && *s) {
	fputs(s, stderr) ;
	fputs(": ", stderr) ;
    }

    switch (h_errno) {
      case HOST_NOT_FOUND:
	fputs("unknown host\n", stderr) ;
	break ;
      case TRY_AGAIN:
	fputs("temporary error -- try again later\n", stderr) ;
	break ;
      case NO_RECOVERY:
	fputs("unrecoverable error\n", stderr) ;
	break ;
#ifdef BSD
      case NO_DATA:
#else
#ifdef SYSV
      case NO_ADDRESS:
#else
you must define SYSV or BSD
#endif /* SYSV */
#endif /* BSD  */
	fputs("no IP address\n", stderr) ;
	break ;
      default:
	fprintf(stderr, "unknown error number %d\n", h_errno) ;
    }
}
SHAR_EOF
fi
exit 0
#	End of shell archive
--
Juergen Nickelsen                               nickel@cs.tu-berlin.de
