Mercurial > hg > anonet-resdb
comparison contrib/whoisd/as.c @ 1146:da853b90f62b draft
Added mkz to anonet and as many of the needed files as I could think of.
That as.c is the part of NetBSD traceroute that does AS lookups.
author | epoch <epoch@hacking.allowed.org> |
---|---|
date | Fri, 01 Nov 2013 09:12:45 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1145:b19ced4435be | 1146:da853b90f62b |
---|---|
1 /* $NetBSD: as.c,v 1.2 2008/04/28 20:24:17 martin 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 asn->as_debug = fopen(AS_DEBUG_FILE, "w"); | |
123 if (asn->as_debug) { | |
124 (void)fprintf(asn->as_debug, ">> !!\n"); | |
125 (void)fflush(asn->as_debug); | |
126 } | |
127 #endif /* AS_DEBUG_FILE */ | |
128 | |
129 return (asn); | |
130 } | |
131 | |
132 int | |
133 as_lookup(_asn, addr) | |
134 void *_asn; | |
135 struct in_addr *addr; | |
136 { | |
137 struct aslookup *asn = _asn; | |
138 char buf[1024]; | |
139 int as, rc, dlen; | |
140 | |
141 as = rc = dlen = 0; | |
142 (void)fprintf(asn->as_f, "!r%s/32,l\n", inet_ntoa(*addr)); | |
143 (void)fflush(asn->as_f); | |
144 | |
145 #ifdef AS_DEBUG_FILE | |
146 if (asn->as_debug) { | |
147 (void)fprintf(asn->as_debug, ">> !r%s/32,l\n", | |
148 inet_ntoa(*addr)); | |
149 (void)fflush(asn->as_debug); | |
150 } | |
151 #endif /* AS_DEBUG_FILE */ | |
152 | |
153 while (fgets(buf, sizeof(buf), asn->as_f) != NULL) { | |
154 buf[sizeof(buf) - 1] = '\0'; | |
155 | |
156 #ifdef AS_DEBUG_FILE | |
157 if (asn->as_debug) { | |
158 (void)fprintf(asn->as_debug, "<< %s", buf); | |
159 (void)fflush(asn->as_debug); | |
160 } | |
161 #endif /* AS_DEBUG_FILE */ | |
162 | |
163 if (rc == 0) { | |
164 rc = buf[0]; | |
165 switch (rc) { | |
166 case 'A': | |
167 /* A - followed by # bytes of answer */ | |
168 sscanf(buf, "A%d\n", &dlen); | |
169 #ifdef AS_DEBUG_FILE | |
170 if (asn->as_debug) { | |
171 (void)fprintf(asn->as_debug, | |
172 "dlen: %d\n", dlen); | |
173 (void)fflush(asn->as_debug); | |
174 } | |
175 #endif /* AS_DEBUG_FILE */ | |
176 break; | |
177 case 'C': | |
178 case 'D': | |
179 case 'E': | |
180 case 'F': | |
181 /* C - no data returned */ | |
182 /* D - key not found */ | |
183 /* E - multiple copies of key */ | |
184 /* F - some other error */ | |
185 break; | |
186 } | |
187 if (rc == 'A') | |
188 /* skip to next input line */ | |
189 continue; | |
190 } | |
191 | |
192 if (dlen == 0) | |
193 /* out of data, next char read is end code */ | |
194 rc = buf[0]; | |
195 if (rc != 'A') | |
196 /* either an error off the bat, or a done code */ | |
197 break; | |
198 | |
199 /* data received, thank you */ | |
200 dlen -= strlen(buf); | |
201 | |
202 /* origin line is the interesting bit */ | |
203 if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) { | |
204 sscanf(buf + 7, " AS%d", &as); | |
205 #ifdef AS_DEBUG_FILE | |
206 if (asn->as_debug) { | |
207 (void)fprintf(asn->as_debug, "as: %d\n", as); | |
208 (void)fflush(asn->as_debug); | |
209 } | |
210 #endif /* AS_DEBUG_FILE */ | |
211 } | |
212 } | |
213 | |
214 return (as); | |
215 } | |
216 | |
217 void | |
218 as_shutdown(_asn) | |
219 void *_asn; | |
220 { | |
221 struct aslookup *asn = _asn; | |
222 | |
223 (void)fprintf(asn->as_f, "!q\n"); | |
224 (void)fclose(asn->as_f); | |
225 | |
226 #ifdef AS_DEBUG_FILE | |
227 if (asn->as_debug) { | |
228 (void)fprintf(asn->as_debug, ">> !q\n"); | |
229 (void)fclose(asn->as_debug); | |
230 } | |
231 #endif /* AS_DEBUG_FILE */ | |
232 | |
233 free(asn); | |
234 } |