]>
git.saurik.com Git - apple/network_cmds.git/blob - traceroute.tproj/as.c
1 /* $FreeBSD: src/contrib/traceroute/as.c,v 1.1 2008/02/20 23:29:52 rpaulo Exp $ */
2 /* $NetBSD: as.c,v 1.1 2001/11/04 23:14:36 atatat Exp $ */
5 * Copyright (c) 2001 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/cdefs.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
55 #define DEFAULT_AS_SERVER "whois.radb.net"
62 #endif /* AS_DEBUG_FILE */
70 struct hostent
*he
= NULL
;
72 struct sockaddr_in in
;
77 server
= DEFAULT_AS_SERVER
;
79 (void)memset(&in
, 0, sizeof(in
));
80 in
.sin_family
= AF_INET
;
81 in
.sin_len
= sizeof(in
);
82 if ((se
= getservbyname("whois", "tcp")) == NULL
) {
83 warnx("warning: whois/tcp service not found");
84 in
.sin_port
= ntohs(43);
86 in
.sin_port
= se
->s_port
;
88 if (inet_aton(server
, &in
.sin_addr
) == 0 &&
89 ((he
= gethostbyname(server
)) == NULL
||
90 he
->h_addr
== NULL
)) {
91 warnx("%s: %s", server
, hstrerror(h_errno
));
95 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1) {
102 memcpy(&in
.sin_addr
, he
->h_addr
, he
->h_length
);
105 if (connect(s
, (struct sockaddr
*)&in
, sizeof(in
)) == 0)
107 if (he
== NULL
|| he
->h_addr
== NULL
) {
120 (void)fprintf(f
, "!!\n");
123 asn
= malloc(sizeof(struct aslookup
));
130 asn
->as_debug
= fopen(AS_DEBUG_FILE
, "w");
132 (void)fprintf(asn
->as_debug
, ">> !!\n");
133 (void)fflush(asn
->as_debug
);
135 #endif /* AS_DEBUG_FILE */
141 as_lookup(_asn
, addr
)
143 struct in_addr
*addr
;
145 struct aslookup
*asn
= _asn
;
150 (void)fprintf(asn
->as_f
, "!r%s/32,l\n", inet_ntoa(*addr
));
151 (void)fflush(asn
->as_f
);
155 (void)fprintf(asn
->as_debug
, ">> !r%s/32,l\n",
157 (void)fflush(asn
->as_debug
);
159 #endif /* AS_DEBUG_FILE */
161 while (fgets(buf
, sizeof(buf
), asn
->as_f
) != NULL
) {
162 buf
[sizeof(buf
) - 1] = '\0';
166 (void)fprintf(asn
->as_debug
, "<< %s", buf
);
167 (void)fflush(asn
->as_debug
);
169 #endif /* AS_DEBUG_FILE */
175 /* A - followed by # bytes of answer */
176 sscanf(buf
, "A%d\n", &dlen
);
179 (void)fprintf(asn
->as_debug
,
181 (void)fflush(asn
->as_debug
);
183 #endif /* AS_DEBUG_FILE */
189 /* C - no data returned */
190 /* D - key not found */
191 /* E - multiple copies of key */
192 /* F - some other error */
196 /* skip to next input line */
201 /* out of data, next char read is end code */
204 /* either an error off the bat, or a done code */
207 /* data received, thank you */
210 /* origin line is the interesting bit */
211 if (as
== 0 && strncasecmp(buf
, "origin:", 7) == 0) {
212 sscanf(buf
+ 7, " AS%d", &as
);
215 (void)fprintf(asn
->as_debug
, "as: %d\n", as
);
216 (void)fflush(asn
->as_debug
);
218 #endif /* AS_DEBUG_FILE */
229 struct aslookup
*asn
= _asn
;
231 (void)fprintf(asn
->as_f
, "!q\n");
232 (void)fclose(asn
->as_f
);
236 (void)fprintf(asn
->as_debug
, ">> !q\n");
237 (void)fclose(asn
->as_debug
);
239 #endif /* AS_DEBUG_FILE */