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