1 /* $NetBSD: as.c,v 1.3 2011/01/04 10:26:56 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Brown. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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. 30 */ 31 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> 37 #include <netdb.h> 38 #include <unistd.h> 39 #include <string.h> 40 #include <stdlib.h> 41 #include <errno.h> 42 #include <err.h> 43 #include <stdio.h> 44 45 #include "as.h" 46 47 #define DEFAULT_AS_SERVER "whois.radb.net" 48 #undef AS_DEBUG_FILE 49 50 struct aslookup { 51 FILE *as_f; 52 #ifdef AS_DEBUG_FILE 53 FILE *as_debug; 54 #endif /* AS_DEBUG_FILE */ 55 }; 56 57 void * 58 as_setup(server) 59 char *server; 60 { 61 struct aslookup *asn; 62 struct hostent *he = NULL; 63 struct servent *se; 64 struct sockaddr_in in; 65 FILE *f; 66 int s; 67 68 if (server == NULL) 69 server = DEFAULT_AS_SERVER; 70 71 (void)memset(&in, 0, sizeof(in)); 72 in.sin_family = AF_INET; 73 in.sin_len = sizeof(in); 74 if ((se = getservbyname("whois", "tcp")) == NULL) { 75 warnx("warning: whois/tcp service not found"); 76 in.sin_port = ntohs(43); 77 } else 78 in.sin_port = se->s_port; 79 80 if (inet_aton(server, &in.sin_addr) == 0 && 81 ((he = gethostbyname(server)) == NULL || 82 he->h_addr == NULL)) { 83 warnx("%s: %s", server, hstrerror(h_errno)); 84 return (NULL); 85 } 86 87 if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) { 88 warn("socket"); 89 return (NULL); 90 } 91 92 do { 93 if (he != NULL) { 94 memcpy(&in.sin_addr, he->h_addr, he->h_length); 95 he->h_addr_list++; 96 } 97 if (connect(s, (struct sockaddr *)&in, sizeof(in)) == 0) 98 break; 99 if (he == NULL || he->h_addr == NULL) { 100 close(s); 101 s = -1; 102 break; 103 } 104 } while (1); 105 106 if (s == -1) { 107 warn("connect"); 108 return (NULL); 109 } 110 111 f = fdopen(s, "r+"); 112 (void)fprintf(f, "!!\n"); 113 (void)fflush(f); 114 115 asn = malloc(sizeof(struct aslookup)); 116 if (asn == NULL) 117 (void)fclose(f); 118 else 119 asn->as_f = f; 120 121 #ifdef AS_DEBUG_FILE 122 if (asn) { 123 asn->as_debug = fopen(AS_DEBUG_FILE, "w"); 124 if (asn->as_debug) { 125 (void)fprintf(asn->as_debug, ">> !!\n"); 126 (void)fflush(asn->as_debug); 127 } 128 } 129 #endif /* AS_DEBUG_FILE */ 130 131 return (asn); 132 } 133 134 int 135 as_lookup(_asn, addr) 136 void *_asn; 137 struct in_addr *addr; 138 { 139 struct aslookup *asn = _asn; 140 char buf[1024]; 141 int as, rc, dlen; 142 143 as = rc = dlen = 0; 144 (void)fprintf(asn->as_f, "!r%s/32,l\n", inet_ntoa(*addr)); 145 (void)fflush(asn->as_f); 146 147 #ifdef AS_DEBUG_FILE 148 if (asn->as_debug) { 149 (void)fprintf(asn->as_debug, ">> !r%s/32,l\n", 150 inet_ntoa(*addr)); 151 (void)fflush(asn->as_debug); 152 } 153 #endif /* AS_DEBUG_FILE */ 154 155 while (fgets(buf, sizeof(buf), asn->as_f) != NULL) { 156 buf[sizeof(buf) - 1] = '\0'; 157 158 #ifdef AS_DEBUG_FILE 159 if (asn->as_debug) { 160 (void)fprintf(asn->as_debug, "<< %s", buf); 161 (void)fflush(asn->as_debug); 162 } 163 #endif /* AS_DEBUG_FILE */ 164 165 if (rc == 0) { 166 rc = buf[0]; 167 switch (rc) { 168 case 'A': 169 /* A - followed by # bytes of answer */ 170 sscanf(buf, "A%d\n", &dlen); 171 #ifdef AS_DEBUG_FILE 172 if (asn->as_debug) { 173 (void)fprintf(asn->as_debug, 174 "dlen: %d\n", dlen); 175 (void)fflush(asn->as_debug); 176 } 177 #endif /* AS_DEBUG_FILE */ 178 break; 179 case 'C': 180 case 'D': 181 case 'E': 182 case 'F': 183 /* C - no data returned */ 184 /* D - key not found */ 185 /* E - multiple copies of key */ 186 /* F - some other error */ 187 break; 188 } 189 if (rc == 'A') 190 /* skip to next input line */ 191 continue; 192 } 193 194 if (dlen == 0) 195 /* out of data, next char read is end code */ 196 rc = buf[0]; 197 if (rc != 'A') 198 /* either an error off the bat, or a done code */ 199 break; 200 201 /* data received, thank you */ 202 dlen -= strlen(buf); 203 204 /* origin line is the interesting bit */ 205 if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) { 206 sscanf(buf + 7, " AS%d", &as); 207 #ifdef AS_DEBUG_FILE 208 if (asn->as_debug) { 209 (void)fprintf(asn->as_debug, "as: %d\n", as); 210 (void)fflush(asn->as_debug); 211 } 212 #endif /* AS_DEBUG_FILE */ 213 } 214 } 215 216 return (as); 217 } 218 219 void 220 as_shutdown(_asn) 221 void *_asn; 222 { 223 struct aslookup *asn = _asn; 224 225 (void)fprintf(asn->as_f, "!q\n"); 226 (void)fclose(asn->as_f); 227 228 #ifdef AS_DEBUG_FILE 229 if (asn->as_debug) { 230 (void)fprintf(asn->as_debug, ">> !q\n"); 231 (void)fclose(asn->as_debug); 232 } 233 #endif /* AS_DEBUG_FILE */ 234 235 free(asn); 236 } 237