1 /* $NetBSD: as.c,v 1.1 2001/11/04 23:14:36 atatat Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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>
47 #define DEFAULT_AS_SERVER "whois.radb.net"
54 #endif /* AS_DEBUG_FILE */
58 as_setup(const char *server)
61 struct addrinfo hints, *res0, *res;
67 server = getenv("RA_SERVER");
69 server = DEFAULT_AS_SERVER;
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);
80 warnx("%s: %s", server, gai_strerror(error));
84 for (res = res0; res; res = res->ai_next) {
85 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
88 if (connect(s, res->ai_addr, res->ai_addrlen) >= 0)
100 (void)fprintf(f, "!!\n");
103 asn = malloc(sizeof(struct aslookup));
110 asn->as_debug = fopen(AS_DEBUG_FILE, "w");
112 (void)fprintf(asn->as_debug, ">> !!\n");
113 (void)fflush(asn->as_debug);
115 #endif /* AS_DEBUG_FILE */
121 as_lookup(void *_asn, char *addr, sa_family_t family)
123 struct aslookup *asn = _asn;
130 plen = (family == AF_INET6) ? 128 : 32;
131 (void)fprintf(asn->as_f, "!r%s/%d,l\n", addr, plen);
132 (void)fflush(asn->as_f);
136 (void)fprintf(asn->as_debug, ">> !r%s/%d,l\n", addr, plen);
137 (void)fflush(asn->as_debug);
139 #endif /* AS_DEBUG_FILE */
141 while (fgets(buf, sizeof(buf), asn->as_f) != NULL) {
142 buf[sizeof(buf) - 1] = '\0';
146 (void)fprintf(asn->as_debug, "<< %s", buf);
147 (void)fflush(asn->as_debug);
149 #endif /* AS_DEBUG_FILE */
155 /* A - followed by # bytes of answer */
156 sscanf(buf, "A%d\n", &dlen);
159 (void)fprintf(asn->as_debug,
161 (void)fflush(asn->as_debug);
163 #endif /* AS_DEBUG_FILE */
169 /* C - no data returned */
170 /* D - key not found */
171 /* E - multiple copies of key */
172 /* F - some other error */
176 /* skip to next input line */
181 /* out of data, next char read is end code */
184 /* either an error off the bat, or a done code */
187 /* data received, thank you */
190 /* origin line is the interesting bit */
191 if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) {
192 sscanf(buf + 7, " AS%u", &as);
195 (void)fprintf(asn->as_debug, "as: %d\n", as);
196 (void)fflush(asn->as_debug);
198 #endif /* AS_DEBUG_FILE */
206 as_shutdown(void *_asn)
208 struct aslookup *asn = _asn;
210 (void)fprintf(asn->as_f, "!q\n");
211 (void)fclose(asn->as_f);
215 (void)fprintf(asn->as_debug, ">> !q\n");
216 (void)fclose(asn->as_debug);
218 #endif /* AS_DEBUG_FILE */