1 /* $NetBSD: as.c,v 1.4 2011/05/10 01:52:49 christos 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
);
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
));
112 asn
->as_debug
= fopen(AS_DEBUG_FILE
, "w");
114 (void)fprintf(asn
->as_debug
, ">> !!\n");
115 (void)fflush(asn
->as_debug
);
118 #endif /* AS_DEBUG_FILE */
124 as_lookup(void *_asn
, char *addr
, sa_family_t family
)
126 struct aslookup
*asn
= _asn
;
133 plen
= (family
== AF_INET6
) ? 128 : 32;
134 (void)fprintf(asn
->as_f
, "!r%s/%d,l\n", addr
, plen
);
135 (void)fflush(asn
->as_f
);
139 (void)fprintf(asn
->as_debug
, ">> !r%s/%d,l\n", addr
, plen
);
140 (void)fflush(asn
->as_debug
);
142 #endif /* AS_DEBUG_FILE */
144 while (fgets(buf
, sizeof(buf
), asn
->as_f
) != NULL
) {
145 buf
[sizeof(buf
) - 1] = '\0';
149 (void)fprintf(asn
->as_debug
, "<< %s", buf
);
150 (void)fflush(asn
->as_debug
);
152 #endif /* AS_DEBUG_FILE */
158 /* A - followed by # bytes of answer */
159 sscanf(buf
, "A%d\n", &dlen
);
162 (void)fprintf(asn
->as_debug
,
164 (void)fflush(asn
->as_debug
);
166 #endif /* AS_DEBUG_FILE */
172 /* C - no data returned */
173 /* D - key not found */
174 /* E - multiple copies of key */
175 /* F - some other error */
179 /* skip to next input line */
184 /* out of data, next char read is end code */
187 /* either an error off the bat, or a done code */
190 /* data received, thank you */
193 /* origin line is the interesting bit */
194 if (as
== 0 && strncasecmp(buf
, "origin:", 7) == 0) {
195 sscanf(buf
+ 7, " AS%u", &as
);
198 (void)fprintf(asn
->as_debug
, "as: %d\n", as
);
199 (void)fflush(asn
->as_debug
);
201 #endif /* AS_DEBUG_FILE */
209 as_shutdown(void *_asn
)
211 struct aslookup
*asn
= _asn
;
213 (void)fprintf(asn
->as_f
, "!q\n");
214 (void)fclose(asn
->as_f
);
218 (void)fprintf(asn
->as_debug
, ">> !q\n");
219 (void)fclose(asn
->as_debug
);
221 #endif /* AS_DEBUG_FILE */