1 /* $NetBSD: as.c,v 1.1 2001/11/04 23:14:36 atatat Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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>
47 #define DEFAULT_AS_SERVER "whois.radb.net"
54 #endif /* AS_DEBUG_FILE */
62 struct hostent
*he
= NULL
;
64 struct sockaddr_in in
;
69 server
= DEFAULT_AS_SERVER
;
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);
78 in
.sin_port
= se
->s_port
;
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
));
87 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) == -1) {
94 memcpy(&in
.sin_addr
, he
->h_addr
, he
->h_length
);
97 if (connect(s
, (struct sockaddr
*)&in
, sizeof(in
)) == 0)
99 if (he
== NULL
|| he
->h_addr
== NULL
) {
112 (void)fprintf(f
, "!!\n");
115 asn
= malloc(sizeof(struct aslookup
));
122 asn
->as_debug
= fopen(AS_DEBUG_FILE
, "w");
124 (void)fprintf(asn
->as_debug
, ">> !!\n");
125 (void)fflush(asn
->as_debug
);
127 #endif /* AS_DEBUG_FILE */
133 as_lookup(_asn
, addr
)
135 struct in_addr
*addr
;
137 struct aslookup
*asn
= _asn
;
142 (void)fprintf(asn
->as_f
, "!r%s/32,l\n", inet_ntoa(*addr
));
143 (void)fflush(asn
->as_f
);
147 (void)fprintf(asn
->as_debug
, ">> !r%s/32,l\n",
149 (void)fflush(asn
->as_debug
);
151 #endif /* AS_DEBUG_FILE */
153 while (fgets(buf
, sizeof(buf
), asn
->as_f
) != NULL
) {
154 buf
[sizeof(buf
) - 1] = '\0';
158 (void)fprintf(asn
->as_debug
, "<< %s", buf
);
159 (void)fflush(asn
->as_debug
);
161 #endif /* AS_DEBUG_FILE */
167 /* A - followed by # bytes of answer */
168 sscanf(buf
, "A%d\n", &dlen
);
171 (void)fprintf(asn
->as_debug
,
173 (void)fflush(asn
->as_debug
);
175 #endif /* AS_DEBUG_FILE */
181 /* C - no data returned */
182 /* D - key not found */
183 /* E - multiple copies of key */
184 /* F - some other error */
188 /* skip to next input line */
193 /* out of data, next char read is end code */
196 /* either an error off the bat, or a done code */
199 /* data received, thank you */
202 /* origin line is the interesting bit */
203 if (as
== 0 && strncasecmp(buf
, "origin:", 7) == 0) {
204 sscanf(buf
+ 7, " AS%d", &as
);
207 (void)fprintf(asn
->as_debug
, "as: %d\n", as
);
208 (void)fflush(asn
->as_debug
);
210 #endif /* AS_DEBUG_FILE */
221 struct aslookup
*asn
= _asn
;
223 (void)fprintf(asn
->as_f
, "!q\n");
224 (void)fclose(asn
->as_f
);
228 (void)fprintf(asn
->as_debug
, ">> !q\n");
229 (void)fclose(asn
->as_debug
);
231 #endif /* AS_DEBUG_FILE */