as.c (176428) | as.c (196475) |
---|---|
1/* $FreeBSD: head/contrib/traceroute/as.c 176428 2008-02-20 23:29:53Z rpaulo $ */ | 1/* $FreeBSD: head/contrib/traceroute/as.c 196475 2009-08-23 17:00:16Z ume $ */ |
2/* $NetBSD: as.c,v 1.1 2001/11/04 23:14:36 atatat Exp $ */ 3 4/* 5 * Copyright (c) 2001 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Andrew Brown. --- 48 unchanged lines hidden (view full) --- 58struct aslookup { 59 FILE *as_f; 60#ifdef AS_DEBUG_FILE 61 FILE *as_debug; 62#endif /* AS_DEBUG_FILE */ 63}; 64 65void * | 2/* $NetBSD: as.c,v 1.1 2001/11/04 23:14:36 atatat Exp $ */ 3 4/* 5 * Copyright (c) 2001 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Andrew Brown. --- 48 unchanged lines hidden (view full) --- 58struct aslookup { 59 FILE *as_f; 60#ifdef AS_DEBUG_FILE 61 FILE *as_debug; 62#endif /* AS_DEBUG_FILE */ 63}; 64 65void * |
66as_setup(server) 67 char *server; | 66as_setup(char *server) |
68{ 69 struct aslookup *asn; | 67{ 68 struct aslookup *asn; |
70 struct hostent *he = NULL; 71 struct servent *se; 72 struct sockaddr_in in; | 69 struct addrinfo hints, *res0, *res; |
73 FILE *f; | 70 FILE *f; |
74 int s; | 71 int s, error; |
75 76 if (server == NULL) | 72 73 if (server == NULL) |
74 server = getenv("RA_SERVER"); 75 if (server == NULL) |
|
77 server = DEFAULT_AS_SERVER; 78 | 76 server = DEFAULT_AS_SERVER; 77 |
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) { | 78 memset(&hints, 0, sizeof(hints)); 79 hints.ai_family = PF_UNSPEC; 80 hints.ai_socktype = SOCK_STREAM; 81 error = getaddrinfo(server, "whois", &hints, &res0); 82 if (error == EAI_SERVICE) { |
83 warnx("warning: whois/tcp service not found"); | 83 warnx("warning: whois/tcp service not found"); |
84 in.sin_port = ntohs(43); 85 } else 86 in.sin_port = se->s_port; 87 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)); 92 return (NULL); | 84 error = getaddrinfo(server, "43", &hints, &res0); |
93 } | 85 } |
94 95 if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) { 96 warn("socket"); | 86 if (error != 0) { 87 warnx("%s: %s", server, gai_strerror(error)); |
97 return (NULL); 98 } 99 | 88 return (NULL); 89 } 90 |
100 do { 101 if (he != NULL) { 102 memcpy(&in.sin_addr, he->h_addr, he->h_length); 103 he->h_addr_list++; 104 } 105 if (connect(s, (struct sockaddr *)&in, sizeof(in)) == 0) | 91 for (res = res0; res; res = res->ai_next) { 92 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 93 if (s < 0) 94 continue; 95 if (connect(s, res->ai_addr, res->ai_addrlen) >= 0) |
106 break; | 96 break; |
107 if (he == NULL || he->h_addr == NULL) { 108 close(s); 109 s = -1; 110 break; 111 } 112 } while (1); 113 114 if (s == -1) { | 97 close(s); 98 s = -1; 99 } 100 freeaddrinfo(res0); 101 if (s < 0) { |
115 warn("connect"); 116 return (NULL); 117 } 118 119 f = fdopen(s, "r+"); 120 (void)fprintf(f, "!!\n"); 121 (void)fflush(f); 122 --- 9 unchanged lines hidden (view full) --- 132 (void)fprintf(asn->as_debug, ">> !!\n"); 133 (void)fflush(asn->as_debug); 134 } 135#endif /* AS_DEBUG_FILE */ 136 137 return (asn); 138} 139 | 102 warn("connect"); 103 return (NULL); 104 } 105 106 f = fdopen(s, "r+"); 107 (void)fprintf(f, "!!\n"); 108 (void)fflush(f); 109 --- 9 unchanged lines hidden (view full) --- 119 (void)fprintf(asn->as_debug, ">> !!\n"); 120 (void)fflush(asn->as_debug); 121 } 122#endif /* AS_DEBUG_FILE */ 123 124 return (asn); 125} 126 |
140int 141as_lookup(_asn, addr) 142 void *_asn; 143 struct in_addr *addr; | 127unsigned int 128as_lookup(void *_asn, char *addr, sa_family_t family) |
144{ 145 struct aslookup *asn = _asn; 146 char buf[1024]; | 129{ 130 struct aslookup *asn = _asn; 131 char buf[1024]; |
147 int as, rc, dlen; | 132 unsigned int as; 133 int rc, dlen, plen; |
148 | 134 |
149 as = rc = dlen = 0; 150 (void)fprintf(asn->as_f, "!r%s/32,l\n", inet_ntoa(*addr)); | 135 as = 0; 136 rc = dlen = 0; 137 plen = (family == AF_INET6) ? 128 : 32; 138 (void)fprintf(asn->as_f, "!r%s/%d,l\n", addr, plen); |
151 (void)fflush(asn->as_f); 152 153#ifdef AS_DEBUG_FILE 154 if (asn->as_debug) { | 139 (void)fflush(asn->as_f); 140 141#ifdef AS_DEBUG_FILE 142 if (asn->as_debug) { |
155 (void)fprintf(asn->as_debug, ">> !r%s/32,l\n", 156 inet_ntoa(*addr)); | 143 (void)fprintf(asn->as_debug, ">> !r%s/%d,l\n", addr, plen); |
157 (void)fflush(asn->as_debug); 158 } 159#endif /* AS_DEBUG_FILE */ 160 161 while (fgets(buf, sizeof(buf), asn->as_f) != NULL) { 162 buf[sizeof(buf) - 1] = '\0'; 163 164#ifdef AS_DEBUG_FILE --- 12 unchanged lines hidden (view full) --- 177#ifdef AS_DEBUG_FILE 178 if (asn->as_debug) { 179 (void)fprintf(asn->as_debug, 180 "dlen: %d\n", dlen); 181 (void)fflush(asn->as_debug); 182 } 183#endif /* AS_DEBUG_FILE */ 184 break; | 144 (void)fflush(asn->as_debug); 145 } 146#endif /* AS_DEBUG_FILE */ 147 148 while (fgets(buf, sizeof(buf), asn->as_f) != NULL) { 149 buf[sizeof(buf) - 1] = '\0'; 150 151#ifdef AS_DEBUG_FILE --- 12 unchanged lines hidden (view full) --- 164#ifdef AS_DEBUG_FILE 165 if (asn->as_debug) { 166 (void)fprintf(asn->as_debug, 167 "dlen: %d\n", dlen); 168 (void)fflush(asn->as_debug); 169 } 170#endif /* AS_DEBUG_FILE */ 171 break; |
185 case 'C': | 172 case 'C': |
186 case 'D': 187 case 'E': 188 case 'F': 189 /* C - no data returned */ 190 /* D - key not found */ 191 /* E - multiple copies of key */ 192 /* F - some other error */ 193 break; --- 10 unchanged lines hidden (view full) --- 204 /* either an error off the bat, or a done code */ 205 break; 206 207 /* data received, thank you */ 208 dlen -= strlen(buf); 209 210 /* origin line is the interesting bit */ 211 if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) { | 173 case 'D': 174 case 'E': 175 case 'F': 176 /* C - no data returned */ 177 /* D - key not found */ 178 /* E - multiple copies of key */ 179 /* F - some other error */ 180 break; --- 10 unchanged lines hidden (view full) --- 191 /* either an error off the bat, or a done code */ 192 break; 193 194 /* data received, thank you */ 195 dlen -= strlen(buf); 196 197 /* origin line is the interesting bit */ 198 if (as == 0 && strncasecmp(buf, "origin:", 7) == 0) { |
212 sscanf(buf + 7, " AS%d", &as); | 199 sscanf(buf + 7, " AS%u", &as); |
213#ifdef AS_DEBUG_FILE 214 if (asn->as_debug) { 215 (void)fprintf(asn->as_debug, "as: %d\n", as); 216 (void)fflush(asn->as_debug); 217 } 218#endif /* AS_DEBUG_FILE */ 219 } 220 } 221 222 return (as); 223} 224 225void | 200#ifdef AS_DEBUG_FILE 201 if (asn->as_debug) { 202 (void)fprintf(asn->as_debug, "as: %d\n", as); 203 (void)fflush(asn->as_debug); 204 } 205#endif /* AS_DEBUG_FILE */ 206 } 207 } 208 209 return (as); 210} 211 212void |
226as_shutdown(_asn) 227 void *_asn; | 213as_shutdown(void *_asn) |
228{ 229 struct aslookup *asn = _asn; 230 231 (void)fprintf(asn->as_f, "!q\n"); 232 (void)fclose(asn->as_f); 233 234#ifdef AS_DEBUG_FILE 235 if (asn->as_debug) { 236 (void)fprintf(asn->as_debug, ">> !q\n"); 237 (void)fclose(asn->as_debug); 238 } 239#endif /* AS_DEBUG_FILE */ 240 241 free(asn); 242} | 214{ 215 struct aslookup *asn = _asn; 216 217 (void)fprintf(asn->as_f, "!q\n"); 218 (void)fclose(asn->as_f); 219 220#ifdef AS_DEBUG_FILE 221 if (asn->as_debug) { 222 (void)fprintf(asn->as_debug, ">> !q\n"); 223 (void)fclose(asn->as_debug); 224 } 225#endif /* AS_DEBUG_FILE */ 226 227 free(asn); 228} |