modules/wh/wh_queries.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. WH_connect
  2. WH_sock

   1 /***************************************
   2   $Revision: 1.7 $
   3 
   4   Whois query (wh) - connects to a whois server and returns result
   5 
   6   Status: NOT REVIEWED, TESTED
   7   
   8   Design and implementation by: Marek Bukowy
   9   
  10   Note:  still not final. Probably SK calls should be moved to the
  11          calling routine
  12   
  13   ******************/ /******************
  14   Copyright (c) 1999                              RIPE NCC
  15  
  16   All Rights Reserved
  17   
  18   Permission to use, copy, modify, and distribute this software and its
  19   documentation for any purpose and without fee is hereby granted,
  20   provided that the above copyright notice appear in all copies and that
  21   both that copyright notice and this permission notice appear in
  22   supporting documentation, and that the name of the author not be
  23   used in advertising or publicity pertaining to distribution of the
  24   software without specific, written prior permission.
  25   
  26   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  27   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  28   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  29   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  30   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  31   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32   ***************************************/
  33 
  34 #include <sys/types.h>
  35 #include <sys/socket.h>
  36 #include <netinet/in.h>
  37 #include <netdb.h>
  38 #include <stdio.h>
  39 
  40 #include <erroutines.h>
  41 #include "socket.h"
  42 
  43 /*+ opens a connection to hostname and queries it,
  44   sets sock to the socket number it got for this connection.
  45   
  46   All reading and possibly other queries must be performed by 
  47   the caller.
  48 +*/
  49 er_ret_t
  50 WH_connect(int *sock, char *hostname, int port)
     /* [<][>][^][v][top][bottom][index][help] */
  51 {
  52   
  53   struct sockaddr_in sin;
  54   struct hostent *hp;
  55   int s;
  56   
  57 #if 0
  58   char log_str[256];
  59   sprintf(log_str, "would perform query >%s< to %s:%d  \n"
  60           "limits: line %d tmout %d and print on socket %d\n",
  61           query,hostname,port, maxlines,timeout,sock );
  62   log_inst_print(log_str);
  63 #endif
  64   
  65   if ( (hp = gethostbyname(hostname)) == NULL) {
  66     return WH_BADHOST;
  67   }
  68 
  69   s = socket(AF_INET, SOCK_STREAM, 0);
  70   if (s < 0) {
  71     return WH_SOCKET;
  72   }
  73   
  74   bzero((caddr_t)&sin, sizeof (sin));
  75   sin.sin_family = hp->h_addrtype;
  76   if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  77     close(s);
  78     return WH_BIND;
  79   }
  80   bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
  81   sin.sin_port=htons(port);
  82 
  83   if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  84     close(s);
  85     return WH_CONNECT;
  86   }
  87 
  88   /*  SK_puts(sock, "% Connection established...\n"); */
  89 
  90   *sock = s;
  91 
  92   return WH_OK;
  93 }
  94 
  95 
  96 
  97 /*+ opens a whois connection to hostname, queries it and
  98   prints result on sock +*/
  99 er_ret_t
 100 WH_sock(int sock, char *hostname, int port, 
     /* [<][>][^][v][top][bottom][index][help] */
 101         char *query, int maxlines, int timeout)
 102 {
 103   int s; 
 104   int ch;
 105   er_ret_t err;
 106   
 107  if( (err = WH_connect(&s, hostname, port)) != WH_OK ) {
 108    return err;
 109  } 
 110  else {
 111 #if 1
 112   {
 113     FILE *sfi;
 114     FILE *sfo;
 115     
 116     sfi = fdopen(s, "r");
 117     sfo = fdopen(s, "w");
 118     if (sfi == NULL || sfo == NULL) {
 119       (void)close(s);
 120       return WH_OPEN;
 121     }
 122     
 123     fprintf(sfo, "%s\r\n", query);
 124     fflush(sfo);
 125     
 126     while ((ch = getc(sfi)) != EOF) {
 127       SK_putc(sock,ch);
 128     }
 129     
 130     fclose(sfo);
 131     fclose(sfi);
 132   }
 133 #else
 134   SK_puts(s, query);
 135   SK_puts(s, "\r\n");
 136   
 137   while( (ch = SK_getc(s)) != EOF ) {
 138     SK_putc(sock,ch);
 139   }
 140 #endif
 141   close(s);
 142   
 143   
 144   
 145   return WH_OK;
 146  } /* if OK */
 147 }
 148 

/* [<][>][^][v][top][bottom][index][help] */