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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <netdb.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <err.h>
43 #include <stdio.h>
44
45 #include "as.h"
46
47 #define DEFAULT_AS_SERVER "whois.radb.net"
48 #undef AS_DEBUG_FILE
49
50 struct aslookup {
51 FILE *as_f;
52 #ifdef AS_DEBUG_FILE
53 FILE *as_debug;
54 #endif /* AS_DEBUG_FILE */
55 };
56
57 void *
as_setup(const char * server)58 as_setup(const char *server)
59 {
60 struct aslookup *asn;
61 struct addrinfo hints, *res0, *res;
62 FILE *f;
63 int s, error;
64
65 s = -1;
66 if (server == NULL)
67 server = getenv("RA_SERVER");
68 if (server == NULL)
69 server = DEFAULT_AS_SERVER;
70
71 memset(&hints, 0, sizeof(hints));
72 hints.ai_family = PF_UNSPEC;
73 hints.ai_socktype = SOCK_STREAM;
74 error = getaddrinfo(server, "whois", &hints, &res0);
75 if (error == EAI_SERVICE) {
76 warnx("warning: whois/tcp service not found");
77 error = getaddrinfo(server, "43", &hints, &res0);
78 }
79 if (error != 0) {
80 warnx("%s: %s", server, gai_strerror(error));
81 return (NULL);
82 }
83
84 for (res = res0; res; res = res->ai_next) {
85 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
86 if (s < 0)
87 continue;
88 if (connect(s, res->ai_addr, res->ai_addrlen) >= 0)
89 break;
90 close(s);
91 s = -1;
92 }
93 freeaddrinfo(res0);
94 if (s < 0) {
95 warn("connect");
96 return (NULL);
97 }
98
99 f = fdopen(s, "r+");
100 (void)fprintf(f, "!!\n");
101 (void)fflush(f);
102
103 asn = malloc(sizeof(struct aslookup));
104 if (asn == NULL)
105 (void)fclose(f);
106 else
107 asn->as_f = f;
108
109 #ifdef AS_DEBUG_FILE
110 asn->as_debug = fopen(AS_DEBUG_FILE, "w");
111 if (asn->as_debug) {
112 (void)fprintf(asn->as_debug, ">> !!\n");
113 (void)fflush(asn->as_debug);
114 }
115 #endif /* AS_DEBUG_FILE */
116
117 return (asn);
118 }
119
120 unsigned int
as_lookup(void * _asn,char * addr,sa_family_t family,int * status)121 as_lookup(void *_asn, char *addr, sa_family_t family, int *status)
122 {
123 struct aslookup *asn = _asn;
124 char buf[1024];
125 unsigned int as;
126 int rc, dlen, plen;
127
128 as = 0;
129 rc = dlen = 0;
130 plen = (family == AF_INET6) ? 128 : 32;
131 *status = fprintf(asn->as_f, "!r%s/%d,l\n", addr, plen);
132 if (*status < 0) {
133 *status = errno;
134 return 0;
135 }
136 *status = fflush(asn->as_f);
137 if (*status == EOF) {
138 *status = errno;
139 return 0;
140 }
141 *status = 0;
142
143 #ifdef AS_DEBUG_FILE
144 if (asn->as_debug) {
145 (void)fprintf(asn->as_debug, ">> !r%s/%d,l\n", addr, plen);
146 (void)fflush(asn->as_debug);
147 }
148 #endif /* AS_DEBUG_FILE */
149
150 while (1) {
151 if (fgets(buf, sizeof(buf), asn->as_f) == NULL) {
152 if(feof(asn->as_f) || ferror(asn->as_f)) {
153 *status = EIO;
154 return 0;
155 }
156 break;
157 }
158 buf[sizeof(buf) - 1] = '\0';
159
160 #ifdef AS_DEBUG_FILE
161 if (asn->as_debug) {
162 (void)fprintf(asn->as_debug, "<< %s", buf);
163 (void)fflush(asn->as_debug);
164 }
165 #endif /* AS_DEBUG_FILE */
166
167 if (rc == 0) {
168 rc = buf[0];
169 switch (rc) {
170 case 'A':
171 /* A - followed by # bytes of answer */
172 sscanf(buf, "A%d\n", &dlen);
173 #ifdef AS_DEBUG_FILE
174 if (asn->as_debug) {
175 (void)fprintf(asn->as_debug,
176 "dlen: %d\n", dlen);
177 (void)fflush(asn->as_debug);
178 }
179 #endif /* AS_DEBUG_FILE */
180 break;
181 case 'C':
182 case 'D':
183 case 'E':
184 case 'F':
185 /* C - no data returned */
186 /* D - key not found */
187 /* E - multiple copies of key */
188 /* F - some other error */
189 break;
190 }
191 if (rc == 'A')
192 /* skip to next input line */
193 continue;
194 }
195
196 if (dlen == 0)
197 /* out of data, next char read is end code */
198 rc = buf[0];
199 if (rc != 'A')
200 /* either an error off the bat, or a done code */
201 break;
202
203 /* data received, thank you */
204 dlen -= strlen(buf);
205
206 /* origin line is the interesting bit */
207 if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) {
208 sscanf(buf + 7, " AS%u", &as);
209 #ifdef AS_DEBUG_FILE
210 if (asn->as_debug) {
211 (void)fprintf(asn->as_debug, "as: %d\n", as);
212 (void)fflush(asn->as_debug);
213 }
214 #endif /* AS_DEBUG_FILE */
215 }
216 }
217
218 return (as);
219 }
220
221 void
as_shutdown(void * _asn)222 as_shutdown(void *_asn)
223 {
224 struct aslookup *asn = _asn;
225
226 (void)fprintf(asn->as_f, "!q\n");
227 (void)fclose(asn->as_f);
228
229 #ifdef AS_DEBUG_FILE
230 if (asn->as_debug) {
231 (void)fprintf(asn->as_debug, ">> !q\n");
232 (void)fclose(asn->as_debug);
233 }
234 #endif /* AS_DEBUG_FILE */
235
236 free(asn);
237 }
238