utils/whoisRIP/whoisRIP.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- index
- rindex
- bzero
- bcopy
- bcopy
- usage
- closesocket
- termhandler
- occurs
- occurs
- main
1 /*
2 * RIPE version marten@ripe.net
3 * many changes & networkupdate by david@ripe.net
4 * cosmetics by steven@dante.org.uk -- gcc stopped complaining mostly,
5 * code is still messy, though.
6 *
7 * 1.15 94/09/07
8 *
9 * 1.2 9705/02
10 * "-v" option added; ambrose@ripe.net
11 * "whois.ripe.net" replaced by "bsdbase.ripe.net"; ambrose@ripe.net
12 * "bsdbase.ripe.net" replaced by "joshua.ripe.net"; marek@ripe.net
13 * "joshua.ripe.net" replaced by "whois.ripe.net"; roman@ripe.net 981105
14 *
15 * Copyright (c) 1980 Regents of the University of California.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the University of
29 * California, Berkeley and its contributors.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 */
46
47 #if defined(sun) && defined(solaris)
48 #define SYSV
49 #endif
50
51 #ifndef lint
52 char copyright[] =
53 "@(#)Copyright (c) 1980 Regents of the University of California.\n\
54 All rights reserved.\n";
55 #endif /* not lint */
56
57 #ifndef lint
58 char sccsid[] =
59 "@(#)whois.c 5.11 (Berkeley) 3/2/91 - RIPE 1.15 94/09/07 marten@ripe.net";
60 #endif /* not lint */
61
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <netdb.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <ctype.h>
69 #include <string.h>
70 #include <errno.h>
71 #include <unistd.h>
72 #include <pwd.h>
73 #include <signal.h>
74
75 #ifdef __STDC__
76 extern FILE *fdopen(int fildes, const char *type);
77 #endif /* __STDC__ */
78
79 #if defined(SYSV) || defined(__STDC__)
80
81 #define index(s,c) strchr((const char*)(s),(int)(c))
/* [<][>][^][v][top][bottom][index][help] */
82 #define rindex(s,c) strrchr((const char*)(s),(int)(c))
/* [<][>][^][v][top][bottom][index][help] */
83 #define bzero(s,n) memset((void*)s,0,(size_t)n)
/* [<][>][^][v][top][bottom][index][help] */
84
85 #ifdef HASMEMMOVE
86 # define bcopy(s,d,n) memmove((void*)(d),(void*)(s),(size_t)(n))
/* [<][>][^][v][top][bottom][index][help] */
87 #else
88 # define bcopy(s,d,n) memcpy((void*)(d),(void*)(s),(size_t)(n))
/* [<][>][^][v][top][bottom][index][help] */
89 #endif /* HASMEMMOVE */
90
91 #endif /* SYSV || __STDC__ */
92
93 #ifdef GLIBC
94 typedef __u_short u_short;
95 typedef __caddr_t caddr_t;
96 #endif /* GLIBC */
97
98 #include <sys/param.h>
99 #define NICHOST "rpsl.ripe.net"
100
101 void usage()
/* [<][>][^][v][top][bottom][index][help] */
102 {
103 (void)fprintf(stderr, "\nUsage: whois [-h hostname] [-p port] keys ...");
104 (void)fprintf(stderr, "\n\nWhere:\n\n");
105 (void)fprintf(stderr, "-h hostname search alternate server\n");
106 (void)fprintf(stderr, "-p port port to connect to\n");
107 (void)fprintf(stderr, "\n");
108
109 exit(1);
110 }
111
112 int s;
113
114 void closesocket(s, child)
/* [<][>][^][v][top][bottom][index][help] */
115 int s, child;
116 {
117 /* printf("close connection child=%i\n", child); */
118
119 close(s);
120
121 exit(0);
122
123 }
124
125 void termhandler(sig, code, scp, addr)
/* [<][>][^][v][top][bottom][index][help] */
126 int sig, code;
127 struct sigcontext *scp;
128 char *addr;
129 {
130 closesocket(s,1);
131 }
132
133
134 /* #ifdef RIPE */
135 #if defined(__STDC__) || defined(SYSV)
136 #define occurs(str,pat) ((int) strstr((str),(pat)))
/* [<][>][^][v][top][bottom][index][help] */
137 #else /* !__STDC__ && !SYSV */
138 int occurs(str, pat)
/* [<][>][^][v][top][bottom][index][help] */
139 char *str, *pat;
140 {
141 register char *point = str;
142
143 while ((point=index(point, *pat)))
144 {
145 if (strncmp(point, pat, strlen(pat)) == 0)
146 return(1);
147 point++;
148 }
149 return(0);
150 }
151 #endif
152 /* #endif */
153
154 int main(argc, argv)
/* [<][>][^][v][top][bottom][index][help] */
155 int argc;
156 char **argv;
157 {
158 FILE *sfi;
159 FILE *sfo;
160 int ch;
161 struct sockaddr_in sin;
162 struct hostent *hp;
163 struct servent *sp;
164 char *host, *whoishost, *realhost;
165 int optport=0, port=0;
166 int verb=0, opthost=0;
167 int optcount;
168 char query[1024]="";
169 char *string;
170
171
172 host = NICHOST;
173
174 if (argc<=1)
175 usage();
176
177 for (optcount=1; optcount<argc; optcount++) {
178 if( argv[optcount][0] == '-' ) {
179 switch (argv[optcount][1]) {
180 case 'h':
181 opthost = 1;
182 if ((!argv[optcount+1]) || (argv[optcount+1][0] == '-'))
183 usage();
184 host = argv[optcount+1];
185
186 argv[optcount+1] = argv[optcount]; /* set both to point to the same address */
187 *argv[optcount] = 0; /* and set to "" */
188 optcount++;
189 continue;
190 case 'p':
191 if ((!argv[optcount+1]) || (argv[optcount+1][0] == '-'))
192 usage();
193 port = htons((u_short)atoi(argv[optcount+1]));
194 optport = 1;
195 *argv[optcount] = *argv[optcount+1]=0;
196 optcount++;
197 continue;
198 }
199 }
200 strcat(query, argv[optcount]);
201 strcat(query, " ");
202 }
203
204
205 if (verb) fprintf(stderr, "Trying: %s\n\n", host);
206 hp = gethostbyname(host);
207
208 if (hp == NULL) {
209 fprintf(stderr,"whois: %s\n", host);
210 if (verb) fprintf(stderr, "Now I give up ...\n");
211 perror("Unknown host");
212 exit(1);
213
214 }
215
216
217 realhost = hp->h_name;
218
219 s = socket(hp->h_addrtype, SOCK_STREAM, 0);
220 if (s < 0) {
221 perror("whois: socket");
222 exit(1);
223 }
224 bzero((caddr_t)&sin, sizeof (sin));
225 sin.sin_family = hp->h_addrtype;
226 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
227 perror("whois: bind");
228 exit(1);
229 }
230 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
231
232 if (optport) {
233 sin.sin_port=port;
234 }
235 else {
236
237 sp=getservbyname("whois", "tcp");
238
239 if (sp == NULL) {
240 (void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
241 exit(1);
242 }
243
244 sin.sin_port = sp->s_port;
245
246 }
247
248 /* printf("%i\n", sin.sin_port); */
249
250 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
251 perror("whois: connect");
252 exit(1);
253 }
254
255 sfi = fdopen(s, "r");
256 sfo = fdopen(s, "w");
257 if (sfi == NULL || sfo == NULL) {
258 perror("whois: fdopen");
259 (void)close(s);
260 exit(1);
261 }
262
263 signal(SIGTERM, termhandler);
264
265 /* we can only send the -V when we are sure that we are dealing with
266 a RIPE whois server :-( */
267
268 /* Not a very nice piece of code. The server types change...
269 There should be a string of (assumed) RIPE servers in the
270 Makefile, and the string should be processed here. */
271
272 whoishost=(char *)calloc(strlen(realhost)+1, sizeof(char));
273 strcpy(whoishost, realhost);
274 for (string=whoishost;(*string=(char)tolower(*string));string++);
275
276 if (strstr(whoishost, "ripe.net") ||
277 strstr(whoishost, "ra.net") ||
278 strstr(whoishost, "apnic.net") ||
279 strstr(whoishost, "mci.net") ||
280 strstr(whoishost, "isi.edu") ||
281 strstr(whoishost, "garr.it") ||
282 strstr(whoishost, "ans.net") )
283 (void)fprintf(sfo, "-VwR1.0 ");
284
285 (void)fprintf(sfo, "%s", query);
286 /*if (*argv) (void)fputs(*argv, sfo);*/
287 (void)fputs("\r\n", sfo);
288 (void)fflush(sfo);
289
290 while ((ch = getc(sfi)) != EOF)
291 putchar(ch);
292
293 closesocket(s, 1);
294
295 exit(0);
296
297 }
298