Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / traceroute / as.c
blob52ef87ec99ae46859c4906c01b27878745c94449
1 /* $NetBSD: as.c,v 1.1 2001/11/04 23:14:36 atatat Exp $ */
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
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.
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>
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>
45 #include "as.h"
47 #define DEFAULT_AS_SERVER "whois.radb.net"
48 #undef AS_DEBUG_FILE
50 struct aslookup {
51 FILE *as_f;
52 #ifdef AS_DEBUG_FILE
53 FILE *as_debug;
54 #endif /* AS_DEBUG_FILE */
57 void *
58 as_setup(server)
59 char *server;
61 struct aslookup *asn;
62 struct hostent *he = NULL;
63 struct servent *se;
64 struct sockaddr_in in;
65 FILE *f;
66 int s;
68 if (server == NULL)
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);
77 } else
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));
84 return (NULL);
87 if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
88 warn("socket");
89 return (NULL);
92 do {
93 if (he != NULL) {
94 memcpy(&in.sin_addr, he->h_addr, he->h_length);
95 he->h_addr_list++;
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;
104 } while (1);
106 if (s == -1) {
107 warn("connect");
108 return (NULL);
111 f = fdopen(s, "r+");
112 (void)fprintf(f, "!!\n");
113 (void)fflush(f);
115 asn = malloc(sizeof(struct aslookup));
116 if (asn == NULL)
117 (void)fclose(f);
118 else
119 asn->as_f = f;
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);
127 #endif /* AS_DEBUG_FILE */
129 return (asn);
133 as_lookup(_asn, addr)
134 void *_asn;
135 struct in_addr *addr;
137 struct aslookup *asn = _asn;
138 char buf[1024];
139 int as, rc, dlen;
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);
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);
151 #endif /* AS_DEBUG_FILE */
153 while (fgets(buf, sizeof(buf), asn->as_f) != NULL) {
154 buf[sizeof(buf) - 1] = '\0';
156 #ifdef AS_DEBUG_FILE
157 if (asn->as_debug) {
158 (void)fprintf(asn->as_debug, "<< %s", buf);
159 (void)fflush(asn->as_debug);
161 #endif /* AS_DEBUG_FILE */
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);
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;
187 if (rc == 'A')
188 /* skip to next input line */
189 continue;
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;
199 /* data received, thank you */
200 dlen -= strlen(buf);
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);
210 #endif /* AS_DEBUG_FILE */
214 return (as);
217 void
218 as_shutdown(_asn)
219 void *_asn;
221 struct aslookup *asn = _asn;
223 (void)fprintf(asn->as_f, "!q\n");
224 (void)fclose(asn->as_f);
226 #ifdef AS_DEBUG_FILE
227 if (asn->as_debug) {
228 (void)fprintf(asn->as_debug, ">> !q\n");
229 (void)fclose(asn->as_debug);
231 #endif /* AS_DEBUG_FILE */
233 free(asn);