2 /* $NetBSD: as.c,v 1.1 2001/11/04 23:14:36 atatat Exp $ */
5 * Copyright (c) 2001 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/cdefs.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
55 #define DEFAULT_AS_SERVER "whois.radb.net"
62 #endif /* AS_DEBUG_FILE */
66 as_setup(char *server)
69 struct addrinfo hints, *res0, *res;
74 server = getenv("RA_SERVER");
76 server = DEFAULT_AS_SERVER;
78 memset(&hints, 0, sizeof(hints));
79 hints.ai_family = PF_UNSPEC;
80 hints.ai_socktype = SOCK_STREAM;
81 error = getaddrinfo(server, "whois", &hints, &res0);
82 if (error == EAI_SERVICE) {
83 warnx("warning: whois/tcp service not found");
84 error = getaddrinfo(server, "43", &hints, &res0);
87 warnx("%s: %s", server, gai_strerror(error));
91 for (res = res0; res; res = res->ai_next) {
92 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
95 if (connect(s, res->ai_addr, res->ai_addrlen) >= 0)
107 (void)fprintf(f, "!!\n");
110 asn = malloc(sizeof(struct aslookup));
117 asn->as_debug = fopen(AS_DEBUG_FILE, "w");
119 (void)fprintf(asn->as_debug, ">> !!\n");
120 (void)fflush(asn->as_debug);
122 #endif /* AS_DEBUG_FILE */
128 as_lookup(void *_asn, char *addr, sa_family_t family)
130 struct aslookup *asn = _asn;
137 plen = (family == AF_INET6) ? 128 : 32;
138 (void)fprintf(asn->as_f, "!r%s/%d,l\n", addr, plen);
139 (void)fflush(asn->as_f);
143 (void)fprintf(asn->as_debug, ">> !r%s/%d,l\n", addr, plen);
144 (void)fflush(asn->as_debug);
146 #endif /* AS_DEBUG_FILE */
148 while (fgets(buf, sizeof(buf), asn->as_f) != NULL) {
149 buf[sizeof(buf) - 1] = '\0';
153 (void)fprintf(asn->as_debug, "<< %s", buf);
154 (void)fflush(asn->as_debug);
156 #endif /* AS_DEBUG_FILE */
162 /* A - followed by # bytes of answer */
163 sscanf(buf, "A%d\n", &dlen);
166 (void)fprintf(asn->as_debug,
168 (void)fflush(asn->as_debug);
170 #endif /* AS_DEBUG_FILE */
176 /* C - no data returned */
177 /* D - key not found */
178 /* E - multiple copies of key */
179 /* F - some other error */
183 /* skip to next input line */
188 /* out of data, next char read is end code */
191 /* either an error off the bat, or a done code */
194 /* data received, thank you */
197 /* origin line is the interesting bit */
198 if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) {
199 sscanf(buf + 7, " AS%u", &as);
202 (void)fprintf(asn->as_debug, "as: %d\n", as);
203 (void)fflush(asn->as_debug);
205 #endif /* AS_DEBUG_FILE */
213 as_shutdown(void *_asn)
215 struct aslookup *asn = _asn;
217 (void)fprintf(asn->as_f, "!q\n");
218 (void)fclose(asn->as_f);
222 (void)fprintf(asn->as_debug, ">> !q\n");
223 (void)fclose(asn->as_debug);
225 #endif /* AS_DEBUG_FILE */