Home | History | Annotate | Line # | Download | only in traceroute
as.c revision 1.1
      1  1.1  atatat /*	$NetBSD: as.c,v 1.1 2001/11/04 23:14:36 atatat 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1  atatat  *    must display the following acknowledgement:
     20  1.1  atatat  *        This product includes software developed by the NetBSD
     21  1.1  atatat  *        Foundation, Inc. and its contributors.
     22  1.1  atatat  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  atatat  *    contributors may be used to endorse or promote products derived
     24  1.1  atatat  *    from this software without specific prior written permission.
     25  1.1  atatat  *
     26  1.1  atatat  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  atatat  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  atatat  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  atatat  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  atatat  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  atatat  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  atatat  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  atatat  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  atatat  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  atatat  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  atatat  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  atatat  */
     38  1.1  atatat 
     39  1.1  atatat #include <sys/cdefs.h>
     40  1.1  atatat #include <sys/types.h>
     41  1.1  atatat #include <sys/socket.h>
     42  1.1  atatat #include <netinet/in.h>
     43  1.1  atatat #include <arpa/inet.h>
     44  1.1  atatat #include <netdb.h>
     45  1.1  atatat #include <unistd.h>
     46  1.1  atatat #include <string.h>
     47  1.1  atatat #include <stdlib.h>
     48  1.1  atatat #include <errno.h>
     49  1.1  atatat #include <err.h>
     50  1.1  atatat #include <stdio.h>
     51  1.1  atatat 
     52  1.1  atatat #include "as.h"
     53  1.1  atatat 
     54  1.1  atatat #define DEFAULT_AS_SERVER "whois.radb.net"
     55  1.1  atatat #undef AS_DEBUG_FILE
     56  1.1  atatat 
     57  1.1  atatat struct aslookup {
     58  1.1  atatat 	FILE *as_f;
     59  1.1  atatat #ifdef AS_DEBUG_FILE
     60  1.1  atatat 	FILE *as_debug;
     61  1.1  atatat #endif /* AS_DEBUG_FILE */
     62  1.1  atatat };
     63  1.1  atatat 
     64  1.1  atatat void *
     65  1.1  atatat as_setup(server)
     66  1.1  atatat 	char *server;
     67  1.1  atatat {
     68  1.1  atatat 	struct aslookup *asn;
     69  1.1  atatat 	struct hostent *he = NULL;
     70  1.1  atatat 	struct servent *se;
     71  1.1  atatat 	struct sockaddr_in in;
     72  1.1  atatat 	FILE *f;
     73  1.1  atatat 	int s;
     74  1.1  atatat 
     75  1.1  atatat 	if (server == NULL)
     76  1.1  atatat 		server = DEFAULT_AS_SERVER;
     77  1.1  atatat 
     78  1.1  atatat 	(void)memset(&in, 0, sizeof(in));
     79  1.1  atatat 	in.sin_family = AF_INET;
     80  1.1  atatat 	in.sin_len = sizeof(in);
     81  1.1  atatat 	if ((se = getservbyname("whois", "tcp")) == NULL) {
     82  1.1  atatat 		warnx("warning: whois/tcp service not found");
     83  1.1  atatat 		in.sin_port = ntohs(43);
     84  1.1  atatat 	} else
     85  1.1  atatat 		in.sin_port = se->s_port;
     86  1.1  atatat 
     87  1.1  atatat 	if (inet_aton(server, &in.sin_addr) == 0 &&
     88  1.1  atatat 	    ((he = gethostbyname(server)) == NULL ||
     89  1.1  atatat 	    he->h_addr == NULL)) {
     90  1.1  atatat 		warnx("%s: %s", server, hstrerror(h_errno));
     91  1.1  atatat 		return (NULL);
     92  1.1  atatat 	}
     93  1.1  atatat 
     94  1.1  atatat 	if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
     95  1.1  atatat 		warn("socket");
     96  1.1  atatat 		return (NULL);
     97  1.1  atatat 	}
     98  1.1  atatat 
     99  1.1  atatat 	do {
    100  1.1  atatat 		if (he != NULL) {
    101  1.1  atatat 			memcpy(&in.sin_addr, he->h_addr, he->h_length);
    102  1.1  atatat 			he->h_addr_list++;
    103  1.1  atatat 		}
    104  1.1  atatat 		if (connect(s, (struct sockaddr *)&in, sizeof(in)) == 0)
    105  1.1  atatat 			break;
    106  1.1  atatat 		if (he == NULL || he->h_addr == NULL) {
    107  1.1  atatat 			close(s);
    108  1.1  atatat 			s = -1;
    109  1.1  atatat 			break;
    110  1.1  atatat 		}
    111  1.1  atatat 	} while (1);
    112  1.1  atatat 
    113  1.1  atatat 	if (s == -1) {
    114  1.1  atatat 		warn("connect");
    115  1.1  atatat 		return (NULL);
    116  1.1  atatat 	}
    117  1.1  atatat 
    118  1.1  atatat 	f = fdopen(s, "r+");
    119  1.1  atatat 	(void)fprintf(f, "!!\n");
    120  1.1  atatat 	(void)fflush(f);
    121  1.1  atatat 
    122  1.1  atatat 	asn = malloc(sizeof(struct aslookup));
    123  1.1  atatat 	if (asn == NULL)
    124  1.1  atatat 		(void)fclose(f);
    125  1.1  atatat 	else
    126  1.1  atatat 		asn->as_f = f;
    127  1.1  atatat 
    128  1.1  atatat #ifdef AS_DEBUG_FILE
    129  1.1  atatat 	asn->as_debug = fopen(AS_DEBUG_FILE, "w");
    130  1.1  atatat 	if (asn->as_debug) {
    131  1.1  atatat 		(void)fprintf(asn->as_debug, ">> !!\n");
    132  1.1  atatat 		(void)fflush(asn->as_debug);
    133  1.1  atatat 	}
    134  1.1  atatat #endif /* AS_DEBUG_FILE */
    135  1.1  atatat 
    136  1.1  atatat 	return (asn);
    137  1.1  atatat }
    138  1.1  atatat 
    139  1.1  atatat int
    140  1.1  atatat as_lookup(_asn, addr)
    141  1.1  atatat 	void *_asn;
    142  1.1  atatat 	struct in_addr *addr;
    143  1.1  atatat {
    144  1.1  atatat 	struct aslookup *asn = _asn;
    145  1.1  atatat 	char buf[1024];
    146  1.1  atatat 	int as, rc, dlen;
    147  1.1  atatat 
    148  1.1  atatat 	as = rc = dlen = 0;
    149  1.1  atatat 	(void)fprintf(asn->as_f, "!r%s/32,l\n", inet_ntoa(*addr));
    150  1.1  atatat 	(void)fflush(asn->as_f);
    151  1.1  atatat 
    152  1.1  atatat #ifdef AS_DEBUG_FILE
    153  1.1  atatat 	if (asn->as_debug) {
    154  1.1  atatat 		(void)fprintf(asn->as_debug, ">> !r%s/32,l\n",
    155  1.1  atatat 		     inet_ntoa(*addr));
    156  1.1  atatat 		(void)fflush(asn->as_debug);
    157  1.1  atatat 	}
    158  1.1  atatat #endif /* AS_DEBUG_FILE */
    159  1.1  atatat 
    160  1.1  atatat 	while (fgets(buf, sizeof(buf), asn->as_f) != NULL) {
    161  1.1  atatat 		buf[sizeof(buf) - 1] = '\0';
    162  1.1  atatat 
    163  1.1  atatat #ifdef AS_DEBUG_FILE
    164  1.1  atatat 		if (asn->as_debug) {
    165  1.1  atatat 			(void)fprintf(asn->as_debug, "<< %s", buf);
    166  1.1  atatat 			(void)fflush(asn->as_debug);
    167  1.1  atatat 		}
    168  1.1  atatat #endif /* AS_DEBUG_FILE */
    169  1.1  atatat 
    170  1.1  atatat 		if (rc == 0) {
    171  1.1  atatat 			rc = buf[0];
    172  1.1  atatat 			switch (rc) {
    173  1.1  atatat 			    case 'A':
    174  1.1  atatat 				/* A - followed by # bytes of answer */
    175  1.1  atatat 				sscanf(buf, "A%d\n", &dlen);
    176  1.1  atatat #ifdef AS_DEBUG_FILE
    177  1.1  atatat 				if (asn->as_debug) {
    178  1.1  atatat 					(void)fprintf(asn->as_debug,
    179  1.1  atatat 					     "dlen: %d\n", dlen);
    180  1.1  atatat 					(void)fflush(asn->as_debug);
    181  1.1  atatat 				}
    182  1.1  atatat #endif /* AS_DEBUG_FILE */
    183  1.1  atatat 				break;
    184  1.1  atatat 			    case 'C':
    185  1.1  atatat 			    case 'D':
    186  1.1  atatat 			    case 'E':
    187  1.1  atatat 			    case 'F':
    188  1.1  atatat 				/* C - no data returned */
    189  1.1  atatat 				/* D - key not found */
    190  1.1  atatat 				/* E - multiple copies of key */
    191  1.1  atatat 				/* F - some other error */
    192  1.1  atatat 				break;
    193  1.1  atatat 			}
    194  1.1  atatat 			if (rc == 'A')
    195  1.1  atatat 				/* skip to next input line */
    196  1.1  atatat 				continue;
    197  1.1  atatat 		}
    198  1.1  atatat 
    199  1.1  atatat 		if (dlen == 0)
    200  1.1  atatat 			/* out of data, next char read is end code */
    201  1.1  atatat 			rc = buf[0];
    202  1.1  atatat 		if (rc != 'A')
    203  1.1  atatat 			/* either an error off the bat, or a done code */
    204  1.1  atatat 			break;
    205  1.1  atatat 
    206  1.1  atatat 		/* data received, thank you */
    207  1.1  atatat 		dlen -= strlen(buf);
    208  1.1  atatat 
    209  1.1  atatat 		/* origin line is the interesting bit */
    210  1.1  atatat 		if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) {
    211  1.1  atatat 			sscanf(buf + 7, " AS%d", &as);
    212  1.1  atatat #ifdef AS_DEBUG_FILE
    213  1.1  atatat 			if (asn->as_debug) {
    214  1.1  atatat 				(void)fprintf(asn->as_debug, "as: %d\n", as);
    215  1.1  atatat 				(void)fflush(asn->as_debug);
    216  1.1  atatat 			}
    217  1.1  atatat #endif /* AS_DEBUG_FILE */
    218  1.1  atatat 		}
    219  1.1  atatat 	}
    220  1.1  atatat 
    221  1.1  atatat 	return (as);
    222  1.1  atatat }
    223  1.1  atatat 
    224  1.1  atatat void
    225  1.1  atatat as_shutdown(_asn)
    226  1.1  atatat 	void *_asn;
    227  1.1  atatat {
    228  1.1  atatat 	struct aslookup *asn = _asn;
    229  1.1  atatat 
    230  1.1  atatat 	(void)fprintf(asn->as_f, "!q\n");
    231  1.1  atatat 	(void)fclose(asn->as_f);
    232  1.1  atatat 
    233  1.1  atatat #ifdef AS_DEBUG_FILE
    234  1.1  atatat 	if (asn->as_debug) {
    235  1.1  atatat 		(void)fprintf(asn->as_debug, ">> !q\n");
    236  1.1  atatat 		(void)fclose(asn->as_debug);
    237  1.1  atatat 	}
    238  1.1  atatat #endif /* AS_DEBUG_FILE */
    239  1.1  atatat 
    240  1.1  atatat 	free(asn);
    241  1.1  atatat }
    242