home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / TERMNET / FWHOIS-1.00 / FWHOIS.C next >
Encoding:
C/C++ Source or Header  |  1995-04-20  |  4.6 KB  |  161 lines

  1. /* fwhois.c by Ang3ldust (Chris Cappuccio) <ccappuc@santafe.edu>
  2.  * Networking code taken and modified from net.c in the finger source code
  3.  */
  4.  
  5. /*
  6.  * Copyright (c) 1989 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * This code is derived from software contributed to Berkeley by
  10.  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions
  14.  * are met:
  15.  * 1. Redistributions of source code must retain the above copyright
  16.  *    notice, this list of conditions and the following disclaimer.
  17.  * 2. Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  * 3. All advertising materials mentioning features or use of this software
  21.  *    must display the following acknowledgement:
  22.  *    This product includes software developed by the University of
  23.  *    California, Berkeley and its contributors.
  24.  * 4. Neither the name of the University nor the names of its contributors
  25.  *    may be used to endorse or promote products derived from this software
  26.  *    without specific prior written permission.
  27.  *
  28.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38.  * SUCH DAMAGE.
  39.  */
  40.  
  41. #ifndef lint
  42. char copyright[] =
  43. "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
  44.  All rights reserved.\n";
  45. #endif /* not lint */
  46.  
  47. #ifndef lint
  48. static char sccsid[] = "@(#)fwhois.c    1.00 (DQ) 7/26/93";
  49. #endif /* not lint */
  50.  
  51. #include <sys/param.h>
  52. #include <sys/file.h>
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <sys/types.h>
  56. #include <sys/socket.h>
  57. #include <netinet/in.h>
  58. #include <netdb.h>
  59. #include <stdio.h>
  60. #include <ctype.h>
  61.  
  62. main(argc, argv)
  63. char **argv;
  64. int argc;
  65. {
  66.     char *stuff, *eleet;
  67.     
  68.     if (argc == 2) {
  69.         eleet = argv[1];
  70.         /* If no host specified, use whois.internic.net */
  71.         if(!index(argv[1],'@')) {
  72.             stuff = strcat(eleet,"@whois.internic.net");
  73.             eleet = stuff;
  74.             }
  75.         }
  76.     /* If the user entered an invalid argument, here they go */
  77.     if (argc != 2) {
  78.         printf("usage: fwhois user[@<whois.server>]\n");
  79.         exit(1);
  80.         }
  81.     netfinger(eleet);
  82. }
  83.  
  84. netfinger(name)
  85.     char *name;
  86. {
  87.     register FILE *fp;
  88.     register int c, lastc;
  89.     struct in_addr defaddr;
  90.     struct hostent *hp, def;
  91.     struct servent *sp;
  92.     struct sockaddr_in sin;
  93.     int s;
  94.     char *alist[1], *host, *rindex();
  95.     u_long inet_addr();
  96.  
  97.     if (!(host = rindex(name, '@')))
  98.         return;
  99.     *host++ = NULL;
  100.     if (!(hp = gethostbyname(host))) {
  101.         defaddr.s_addr = inet_addr(host);
  102.         if (defaddr.s_addr == -1) {
  103.             (void)fprintf(stderr,
  104.                 "fwhois: unknown host: %s\n",host);
  105.             return;
  106.         }
  107.         def.h_name = host;
  108.         def.h_addr_list = alist;
  109.         def.h_addr = (char *)&defaddr;
  110.         def.h_length = sizeof(struct in_addr);
  111.         def.h_addrtype = AF_INET;
  112.         def.h_aliases = 0;
  113.         hp = &def;
  114.     }
  115.     if (!(sp = getservbyname("whois", "tcp"))) {
  116.         (void)fprintf(stderr, "fwhois: tcp/whois: unknown service\n");
  117.         return;
  118.     }
  119.     sin.sin_family = hp->h_addrtype;
  120.     bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
  121.     sin.sin_port = sp->s_port;
  122.     if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
  123.         perror("fwhois: socket");
  124.         return;
  125.     }
  126.  
  127.     /* have network connection; identify the host connected with */
  128.     (void)printf("[%s]\n", hp->h_name);
  129.     if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  130.         perror("fwhois: connect");
  131.         (void)close(s);
  132.         return;
  133.     }
  134.  
  135.     /* send the name followed by <CR><LF> */
  136.     (void)write(s, name, strlen(name));
  137.     (void)write(s, "\r\n", 2);
  138.  
  139.     if (fp = fdopen(s, "r"))
  140.         while ((c = getc(fp)) != EOF) {
  141.             c &= 0x7f;
  142.             if (c == 0x0d) {
  143.                 c = '\n';
  144.                 lastc = '\r';
  145.             } else {
  146.                 if (!isprint(c) && !isspace(c))
  147.                     c |= 0x40;
  148.                 if (lastc != '\r' || c != '\n')
  149.                     lastc = c;
  150.                 else {
  151.                     lastc = '\n';
  152.                     continue;
  153.                 }
  154.             }
  155.             putchar(c);
  156.         }
  157.     if (lastc != '\n')
  158.         putchar('\n');
  159.     (void)fclose(fp);
  160. }
  161.