1 | /*************************************** 2 | $Revision: 1.10 $ 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 "sk.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) 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 | { 66 | int error; 67 | struct hostent result; 68 | char aliasbuf[8192]; /* Stevens, UNIX net. prog., p.304 */ 69 | 70 | #ifdef _LINUX 71 | if(gethostbyname_r(hostname, &result, aliasbuf, 72 | sizeof(aliasbuf), &hp, &error)<0) { 73 | #else /* default is Solaris implementation */ 74 | if( (hp=gethostbyname_r(hostname, &result, aliasbuf, 75 | sizeof(aliasbuf), &error)) == NULL) { 76 | #endif 77 | return WH_BADHOST; 78 | } 79 | } 80 | 81 | s = socket(AF_INET, SOCK_STREAM, 0); 82 | if (s < 0) { 83 | return WH_SOCKET; 84 | } 85 | 86 | bzero((caddr_t)&sin, sizeof (sin)); 87 | sin.sin_family = hp->h_addrtype; 88 | if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 89 | close(s); 90 | return WH_BIND; 91 | } 92 | bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); 93 | sin.sin_port=htons(port); 94 | 95 | if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 96 | close(s); 97 | return WH_CONNECT; 98 | } 99 | 100 | /* SK_puts(sock, "% Connection established...\n"); */ 101 | 102 | *sock = s; 103 | 104 | return WH_OK; 105 | } 106 | 107 | 108 | 109 | /*+ opens a whois connection to hostname, queries it and 110 | prints result on sock +*/ 111 | er_ret_t 112 | WH_sock(int sock, char *hostname, int port, 113 | char *query, int maxlines, int timeout) 114 | { 115 | int s; 116 | int ch; 117 | er_ret_t err; 118 | 119 | if( (err = WH_connect(&s, hostname, port)) != WH_OK ) { 120 | return err; 121 | } 122 | else { 123 | #if 1 124 | { 125 | FILE *sfi; 126 | FILE *sfo; 127 | 128 | sfi = fdopen(s, "r"); 129 | sfo = fdopen(s, "w"); 130 | if (sfi == NULL || sfo == NULL) { 131 | (void)close(s); 132 | return WH_OPEN; 133 | } 134 | 135 | fprintf(sfo, "%s\r\n", query); 136 | fflush(sfo); 137 | 138 | while ((ch = getc(sfi)) != EOF) { 139 | SK_putc(sock,ch); 140 | } 141 | 142 | fclose(sfo); 143 | fclose(sfi); 144 | } 145 | #else 146 | SK_puts(s, query); 147 | SK_puts(s, "\r\n"); 148 | 149 | while( (ch = SK_getc(s)) != EOF ) { 150 | SK_putc(sock,ch); 151 | } 152 | #endif 153 | close(s); 154 | 155 | 156 | 157 | return WH_OK; 158 | } /* if OK */ 159 | } 160 |