home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3948 < prev    next >
Encoding:
Text File  |  1991-08-31  |  3.2 KB  |  151 lines

  1. Path: wupost!zaphod.mps.ohio-state.edu!rpi!dali.cs.montana.edu!milton!nntp.uoregon.edu!cs.uoregon.edu!mystix.cs.uoregon.edu!bart
  2. From: bart@cs.uoregon.edu (Bart Massey)
  3. Newsgroups: alt.sources
  4. Subject: DPOTW1: ipaddrname
  5. Keywords: IP gethostbyname gethostbyaddr name address lookup
  6. Message-ID: <1991Aug31.020123.26380@cs.uoregon.edu>
  7. Date: 31 Aug 91 02:01:23 GMT
  8. Sender: bart@cs.uoregon.edu (Bart Massey)
  9. Followup-To: alt.sources.d
  10. Organization: Department of Computer Science, University of Oregon
  11. Lines: 137
  12. Originator: bart@mystix.cs.uoregon.edu
  13.  
  14. Dumb Package Of The Month #1: ipaddrname
  15.  
  16. Here's a dumb little name/address lookup program I whipped up a
  17. while back which I find myself using an inordinate amount.
  18. Basically, it just calls gethostbyname(3) or gethostbyaddr(3)
  19. and prints the result -- useful for debugging DNS, NIS, and
  20. /etc/hosts problems.  Have fun!
  21.  
  22.                         Bart Massey
  23.                         bart@cs.uoregon.edu
  24.  
  25. #! /bin/sh
  26. # This is a "shar" archive.  To extract, clip off any leading or
  27. # trailing garbage and execute using the Bourne Shell.
  28. echo "creating Makefile"
  29. sed 's/^X//' >Makefile <<'X'
  30. BIN = /local/bin
  31. MAN = /local/man/man1
  32. CFLAGS = -O
  33.  
  34. all: ipaddr ipname
  35.  
  36. ipaddr: ipaddrname.c
  37.     $(CC) $(CFLAGS) -s -o ipaddr ipaddrname.c
  38.  
  39. ipname: ipaddr
  40.     -rm -f ipname
  41.     ln -s ipaddr ipname
  42.  
  43. install:
  44.     -rm -f $(BIN)/ipaddr $(BIN)/ipname
  45.     cp ipaddr ipname $(BIN)
  46.     cp ipaddr.man $(MAN)/ipaddr.1
  47.  
  48. clean:
  49.     -rm -f *.o ipaddr ipname
  50. X
  51. echo "creating ipaddr.man"
  52. sed 's/^X//' >ipaddr.man <<'X'
  53. ..TH IPADDR 1
  54. ..SH NAME
  55. ipaddr, ipname \- quick hostname and IP address lookup
  56. ..SH SYNOPSIS
  57. ..B ipaddr
  58. ..I name
  59. ..br
  60. ..B ipname
  61. ..I address
  62. ..SH DESCRIPTION
  63. ..B ipaddr
  64. and
  65. ..B ipname
  66. call
  67. ..B "gethostbyname(3)"
  68. and
  69. ..B "gethostbyaddr(3),"
  70. respectively, with their argument,
  71. and report the results in a human-readable format.
  72. ..SH SEE ALSO
  73. gethostbyname(3), gethostbyaddr(3)
  74. ..SH DIAGNOSTICS
  75. Pretty much directly as returned by the library calls.
  76. ..SH BUGS
  77. Probably zillions.
  78. X
  79. echo "creating ipaddrname.c"
  80. sed 's/^X//' >ipaddrname.c <<'X'
  81. /*
  82.  * ipaddrname -- host name/address lookup
  83.  * Bart 9/91
  84.  */
  85.  
  86. #include <sys/types.h>
  87. #include <sys/socket.h>
  88. #include <netinet/in.h>
  89. #ifndef hpux
  90. #include <arpa/inet.h>
  91. #endif
  92. #include <netdb.h>
  93.  
  94. #include <stdio.h>
  95. #ifdef hpux
  96. #include <string.h>
  97. #define index(X,Y) strchr(X,Y)
  98. #define rindex(X,Y) strrchr(X,Y)
  99. #else
  100. #include <strings.h>
  101. #endif
  102.  
  103. static char *pgmname;
  104.  
  105. static int isipname( s )
  106. char *s;
  107. {
  108.     pgmname = rindex( s, '/' );
  109.     if( pgmname )
  110.         pgmname++;
  111.     else
  112.         pgmname = s;
  113.     if( !strcmp( pgmname, "ipname" ) )
  114.         return 1;
  115.     if( !strcmp( pgmname, "ipaddr" ) )
  116.         return 0;
  117.     fprintf( stderr, "%s: invoke as ipname or ipaddr\n", pgmname );
  118.     exit( 1 );
  119. }
  120.  
  121. main( argc, argv )
  122. int argc;
  123. char **argv;
  124. {
  125.     struct hostent *hp;
  126.     unsigned long addr;
  127.     int i;
  128.     char **ap;
  129.  
  130.     if( isipname( argv[0] ) ) {
  131.         addr = inet_addr( argv[1] );
  132.         hp = gethostbyaddr( (char *) &addr, sizeof( addr ), AF_INET );
  133.     } else
  134.         hp = gethostbyname( argv[1] );
  135.     if( !hp ) {
  136.         fprintf( stderr, "%s: gethostbyname failed\n", pgmname );
  137.         return 1;
  138.     }
  139.     printf( "%s", hp->h_name );
  140.     for( ap = hp->h_aliases; *ap; ap++ )
  141.         printf( " %s", *ap );
  142.     printf( ": " );
  143.     for( i = 0; hp->h_addr_list[i]; i++ )
  144.         printf( "%s ",
  145.            inet_ntoa( * (struct in_addr *) hp->h_addr_list[i] ) );
  146.     printf( "\n" );
  147.     return 0;
  148. }
  149. X
  150. exit 0
  151.