1    | /***************************************
2    |   $Revision: 1.5 $
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   | int
44   | WH_sock(int sock, char *hostname, int port, 
45   | 	char *query, int maxlines, int timeout)
46   | {
47   |   
48   |   FILE *sfi;
49   |   FILE *sfo;
50   |   struct sockaddr_in sin;
51   |   struct hostent *hp;
52   |   int ch;
53   |   int s;
54   |   
55   | #if 0
56   |   char log_str[256];
57   |   sprintf(log_str, "would perform query >%s< to %s:%d  \n"
58   | 	  "limits: line %d tmout %d and print on socket %d\n",
59   | 	  query,hostname,port, maxlines,timeout,sock );
60   |   log_inst_print(log_str);
61   | #endif
62   |   
63   |   if ( (hp = gethostbyname(hostname)) == NULL) {
64   |     return WH_BADHOST;
65   |   }
66   | 
67   |   s = socket(AF_INET, SOCK_STREAM, 0);
68   |   if (s < 0) {
69   |     return WH_SOCKET;
70   |   }
71   |   
72   |   bzero((caddr_t)&sin, sizeof (sin));
73   |   sin.sin_family = hp->h_addrtype;
74   |   if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
75   |     close(s);
76   |     return WH_BIND;
77   |   }
78   |   bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
79   |   sin.sin_port=htons(port);
80   | 
81   |   if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
82   |     close(s);
83   |     return WH_CONNECT;
84   |   }
85   | 
86   |   /*  SK_puts(sock, "% Connection established...\n"); */
87   |   
88   | #if 0
89   |   sfi = fdopen(s, "r");
90   |   sfo = fdopen(s, "w");
91   |   if (sfi == NULL || sfo == NULL) {
92   |     (void)close(s);
93   |     return WH_OPEN;
94   |   }
95   | 
96   |   fprintf(sfo, "%s\r\n", query);
97   |   fflush(sfo);
98   | 
99   |   while ((ch = getc(sfi)) != EOF) {
100  |     SK_putc(sock,ch);
101  |   }
102  | 
103  |   fclose(sfo);
104  |   fclose(sfi);
105  | #else
106  |   SK_puts(s, query);
107  |   SK_puts(s, "\r\n");
108  | 
109  |   while( (ch = SK_getc(s)) != EOF ) {
110  |     SK_putc(sock,ch);
111  |   }
112  | #endif
113  |   close(s);
114  | }
115  |