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