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