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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
48 #define DEFAULT_AS_SERVER "whois.radb.net"
55 #endif /* AS_DEBUG_FILE */
59 as_setup(const char *server)
62 struct addrinfo hints, *res0, *res;
68 server = getenv("RA_SERVER");
70 server = DEFAULT_AS_SERVER;
72 memset(&hints, 0, sizeof(hints));
73 hints.ai_family = PF_UNSPEC;
74 hints.ai_socktype = SOCK_STREAM;
75 error = getaddrinfo(server, "whois", &hints, &res0);
76 if (error == EAI_SERVICE) {
77 warnx("warning: whois/tcp service not found");
78 error = getaddrinfo(server, "43", &hints, &res0);
81 warnx("%s: %s", server, gai_strerror(error));
85 for (res = res0; res; res = res->ai_next) {
86 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
89 if (connect(s, res->ai_addr, res->ai_addrlen) >= 0)
101 (void)fprintf(f, "!!\n");
104 asn = malloc(sizeof(struct aslookup));
111 asn->as_debug = fopen(AS_DEBUG_FILE, "w");
113 (void)fprintf(asn->as_debug, ">> !!\n");
114 (void)fflush(asn->as_debug);
116 #endif /* AS_DEBUG_FILE */
122 as_lookup(void *_asn, char *addr, sa_family_t family)
124 struct aslookup *asn = _asn;
131 plen = (family == AF_INET6) ? 128 : 32;
132 (void)fprintf(asn->as_f, "!r%s/%d,l\n", addr, plen);
133 (void)fflush(asn->as_f);
137 (void)fprintf(asn->as_debug, ">> !r%s/%d,l\n", addr, plen);
138 (void)fflush(asn->as_debug);
140 #endif /* AS_DEBUG_FILE */
142 while (fgets(buf, sizeof(buf), asn->as_f) != NULL) {
143 buf[sizeof(buf) - 1] = '\0';
147 (void)fprintf(asn->as_debug, "<< %s", buf);
148 (void)fflush(asn->as_debug);
150 #endif /* AS_DEBUG_FILE */
156 /* A - followed by # bytes of answer */
157 sscanf(buf, "A%d\n", &dlen);
160 (void)fprintf(asn->as_debug,
162 (void)fflush(asn->as_debug);
164 #endif /* AS_DEBUG_FILE */
170 /* C - no data returned */
171 /* D - key not found */
172 /* E - multiple copies of key */
173 /* F - some other error */
177 /* skip to next input line */
182 /* out of data, next char read is end code */
185 /* either an error off the bat, or a done code */
188 /* data received, thank you */
191 /* origin line is the interesting bit */
192 if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) {
193 sscanf(buf + 7, " AS%u", &as);
196 (void)fprintf(asn->as_debug, "as: %d\n", as);
197 (void)fflush(asn->as_debug);
199 #endif /* AS_DEBUG_FILE */
207 as_shutdown(void *_asn)
209 struct aslookup *asn = _asn;
211 (void)fprintf(asn->as_f, "!q\n");
212 (void)fclose(asn->as_f);
216 (void)fprintf(asn->as_debug, ">> !q\n");
217 (void)fclose(asn->as_debug);
219 #endif /* AS_DEBUG_FILE */