as.c revision 1.1 1 /* $NetBSD: as.c,v 1.1 2001/11/04 23:14:36 atatat Exp $ */
2
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <err.h>
50 #include <stdio.h>
51
52 #include "as.h"
53
54 #define DEFAULT_AS_SERVER "whois.radb.net"
55 #undef AS_DEBUG_FILE
56
57 struct aslookup {
58 FILE *as_f;
59 #ifdef AS_DEBUG_FILE
60 FILE *as_debug;
61 #endif /* AS_DEBUG_FILE */
62 };
63
64 void *
65 as_setup(server)
66 char *server;
67 {
68 struct aslookup *asn;
69 struct hostent *he = NULL;
70 struct servent *se;
71 struct sockaddr_in in;
72 FILE *f;
73 int s;
74
75 if (server == NULL)
76 server = DEFAULT_AS_SERVER;
77
78 (void)memset(&in, 0, sizeof(in));
79 in.sin_family = AF_INET;
80 in.sin_len = sizeof(in);
81 if ((se = getservbyname("whois", "tcp")) == NULL) {
82 warnx("warning: whois/tcp service not found");
83 in.sin_port = ntohs(43);
84 } else
85 in.sin_port = se->s_port;
86
87 if (inet_aton(server, &in.sin_addr) == 0 &&
88 ((he = gethostbyname(server)) == NULL ||
89 he->h_addr == NULL)) {
90 warnx("%s: %s", server, hstrerror(h_errno));
91 return (NULL);
92 }
93
94 if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
95 warn("socket");
96 return (NULL);
97 }
98
99 do {
100 if (he != NULL) {
101 memcpy(&in.sin_addr, he->h_addr, he->h_length);
102 he->h_addr_list++;
103 }
104 if (connect(s, (struct sockaddr *)&in, sizeof(in)) == 0)
105 break;
106 if (he == NULL || he->h_addr == NULL) {
107 close(s);
108 s = -1;
109 break;
110 }
111 } while (1);
112
113 if (s == -1) {
114 warn("connect");
115 return (NULL);
116 }
117
118 f = fdopen(s, "r+");
119 (void)fprintf(f, "!!\n");
120 (void)fflush(f);
121
122 asn = malloc(sizeof(struct aslookup));
123 if (asn == NULL)
124 (void)fclose(f);
125 else
126 asn->as_f = f;
127
128 #ifdef AS_DEBUG_FILE
129 asn->as_debug = fopen(AS_DEBUG_FILE, "w");
130 if (asn->as_debug) {
131 (void)fprintf(asn->as_debug, ">> !!\n");
132 (void)fflush(asn->as_debug);
133 }
134 #endif /* AS_DEBUG_FILE */
135
136 return (asn);
137 }
138
139 int
140 as_lookup(_asn, addr)
141 void *_asn;
142 struct in_addr *addr;
143 {
144 struct aslookup *asn = _asn;
145 char buf[1024];
146 int as, rc, dlen;
147
148 as = rc = dlen = 0;
149 (void)fprintf(asn->as_f, "!r%s/32,l\n", inet_ntoa(*addr));
150 (void)fflush(asn->as_f);
151
152 #ifdef AS_DEBUG_FILE
153 if (asn->as_debug) {
154 (void)fprintf(asn->as_debug, ">> !r%s/32,l\n",
155 inet_ntoa(*addr));
156 (void)fflush(asn->as_debug);
157 }
158 #endif /* AS_DEBUG_FILE */
159
160 while (fgets(buf, sizeof(buf), asn->as_f) != NULL) {
161 buf[sizeof(buf) - 1] = '\0';
162
163 #ifdef AS_DEBUG_FILE
164 if (asn->as_debug) {
165 (void)fprintf(asn->as_debug, "<< %s", buf);
166 (void)fflush(asn->as_debug);
167 }
168 #endif /* AS_DEBUG_FILE */
169
170 if (rc == 0) {
171 rc = buf[0];
172 switch (rc) {
173 case 'A':
174 /* A - followed by # bytes of answer */
175 sscanf(buf, "A%d\n", &dlen);
176 #ifdef AS_DEBUG_FILE
177 if (asn->as_debug) {
178 (void)fprintf(asn->as_debug,
179 "dlen: %d\n", dlen);
180 (void)fflush(asn->as_debug);
181 }
182 #endif /* AS_DEBUG_FILE */
183 break;
184 case 'C':
185 case 'D':
186 case 'E':
187 case 'F':
188 /* C - no data returned */
189 /* D - key not found */
190 /* E - multiple copies of key */
191 /* F - some other error */
192 break;
193 }
194 if (rc == 'A')
195 /* skip to next input line */
196 continue;
197 }
198
199 if (dlen == 0)
200 /* out of data, next char read is end code */
201 rc = buf[0];
202 if (rc != 'A')
203 /* either an error off the bat, or a done code */
204 break;
205
206 /* data received, thank you */
207 dlen -= strlen(buf);
208
209 /* origin line is the interesting bit */
210 if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) {
211 sscanf(buf + 7, " AS%d", &as);
212 #ifdef AS_DEBUG_FILE
213 if (asn->as_debug) {
214 (void)fprintf(asn->as_debug, "as: %d\n", as);
215 (void)fflush(asn->as_debug);
216 }
217 #endif /* AS_DEBUG_FILE */
218 }
219 }
220
221 return (as);
222 }
223
224 void
225 as_shutdown(_asn)
226 void *_asn;
227 {
228 struct aslookup *asn = _asn;
229
230 (void)fprintf(asn->as_f, "!q\n");
231 (void)fclose(asn->as_f);
232
233 #ifdef AS_DEBUG_FILE
234 if (asn->as_debug) {
235 (void)fprintf(asn->as_debug, ">> !q\n");
236 (void)fclose(asn->as_debug);
237 }
238 #endif /* AS_DEBUG_FILE */
239
240 free(asn);
241 }
242