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