diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-22 22:43:05 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-01-22 22:43:05 +0000 |
commit | 448f0241e06e7df2003b7f8afcf01e7406762b4e (patch) | |
tree | bc4a002a534b4c0fb8bba09dbd46f93cea5a38c6 | |
parent | fdcd7c4237a99682724ffbf4e40ab50a40197c56 (diff) | |
download | busybox-w32-448f0241e06e7df2003b7f8afcf01e7406762b4e.tar.gz busybox-w32-448f0241e06e7df2003b7f8afcf01e7406762b4e.tar.bz2 busybox-w32-448f0241e06e7df2003b7f8afcf01e7406762b4e.zip |
nslookup: full circle. Here we started IPv6 work. Use "new API"
and thus save a few bytes.
-rw-r--r-- | include/libbb.h | 39 | ||||
-rw-r--r-- | libbb/xconnect.c | 24 | ||||
-rw-r--r-- | networking/nslookup.c | 117 |
3 files changed, 132 insertions, 48 deletions
diff --git a/include/libbb.h b/include/libbb.h index 57531e491..e41993796 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -278,12 +278,12 @@ extern off_t xlseek(int fd, off_t offset, int whence); | |||
278 | extern off_t fdlength(int fd); | 278 | extern off_t fdlength(int fd); |
279 | 279 | ||
280 | 280 | ||
281 | extern int xsocket(int domain, int type, int protocol); | 281 | int xsocket(int domain, int type, int protocol); |
282 | extern void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen); | 282 | void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen); |
283 | extern void xlisten(int s, int backlog); | 283 | void xlisten(int s, int backlog); |
284 | extern void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen); | 284 | void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen); |
285 | extern int setsockopt_reuseaddr(int fd); | 285 | int setsockopt_reuseaddr(int fd); |
286 | extern int setsockopt_broadcast(int fd); | 286 | int setsockopt_broadcast(int fd); |
287 | /* NB: returns port in host byte order */ | 287 | /* NB: returns port in host byte order */ |
288 | unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port); | 288 | unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port); |
289 | typedef struct len_and_sockaddr { | 289 | typedef struct len_and_sockaddr { |
@@ -303,34 +303,39 @@ int xsocket_stream(len_and_sockaddr **lsap); | |||
303 | * numeric IP ("N.N.N.N") or numeric IPv6 address, | 303 | * numeric IP ("N.N.N.N") or numeric IPv6 address, |
304 | * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). | 304 | * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). |
305 | * If there is no suffix, port argument is used */ | 305 | * If there is no suffix, port argument is used */ |
306 | extern int create_and_bind_stream_or_die(const char *bindaddr, int port); | 306 | int create_and_bind_stream_or_die(const char *bindaddr, int port); |
307 | /* Create client TCP socket connected to peer:port. Peer cannot be NULL. | 307 | /* Create client TCP socket connected to peer:port. Peer cannot be NULL. |
308 | * Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname, | 308 | * Peer can be numeric IP ("N.N.N.N"), numeric IPv6 address or hostname, |
309 | * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). | 309 | * and can have ":PORT" suffix (for IPv6 use "[X:X:...:X]:PORT"). |
310 | * If there is no suffix, port argument is used */ | 310 | * If there is no suffix, port argument is used */ |
311 | extern int create_and_connect_stream_or_die(const char *peer, int port); | 311 | int create_and_connect_stream_or_die(const char *peer, int port); |
312 | /* Connect to peer identified by lsa */ | 312 | /* Connect to peer identified by lsa */ |
313 | extern int xconnect_stream(const len_and_sockaddr *lsa); | 313 | int xconnect_stream(const len_and_sockaddr *lsa); |
314 | /* Return malloc'ed len_and_sockaddr with socket address of host:port | 314 | /* Return malloc'ed len_and_sockaddr with socket address of host:port |
315 | * Currently will return IPv4 or IPv6 sockaddrs only | 315 | * Currently will return IPv4 or IPv6 sockaddrs only |
316 | * (depending on host), but in theory nothing prevents e.g. | 316 | * (depending on host), but in theory nothing prevents e.g. |
317 | * UNIX socket address being returned, IPX sockaddr etc... */ | 317 | * UNIX socket address being returned, IPX sockaddr etc... */ |
318 | extern len_and_sockaddr* host2sockaddr(const char *host, int port); | 318 | len_and_sockaddr* host2sockaddr(const char *host, int port); |
319 | /* Assign sin[6]_port member if the socket is of corresponding type, | 319 | /* Assign sin[6]_port member if the socket is of corresponding type, |
320 | * otherwise no-op. Useful for ftp. | 320 | * otherwise no-op. Useful for ftp. |
321 | * NB: does NOT do htons() internally, just direct assignment. */ | 321 | * NB: does NOT do htons() internally, just direct assignment. */ |
322 | extern void set_nport(len_and_sockaddr *lsa, unsigned port); | 322 | void set_nport(len_and_sockaddr *lsa, unsigned port); |
323 | /* Retrieve sin[6]_port or return -1 for non-INET[6] lsa's */ | 323 | /* Retrieve sin[6]_port or return -1 for non-INET[6] lsa's */ |
324 | extern int get_nport(len_and_sockaddr *lsa); | 324 | int get_nport(len_and_sockaddr *lsa); |
325 | extern char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen); | 325 | /* Reverse DNS */ |
326 | extern char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen); | 326 | char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen); |
327 | /* This one deosn't fall back to dotted IP and do not append :PORTNUM */ | ||
328 | char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t salen); | ||
329 | /* inet_[ap]ton on steroids */ | ||
330 | char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen); | ||
331 | char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen); | ||
327 | // "old" (ipv4 only) API | 332 | // "old" (ipv4 only) API |
328 | //void bb_lookup_host(struct sockaddr_in *s_in, const char *host); | 333 | //void bb_lookup_host(struct sockaddr_in *s_in, const char *host); |
329 | //extern int xconnect_tcp_v4(struct sockaddr_in *s_addr); | 334 | //int xconnect_tcp_v4(struct sockaddr_in *s_addr); |
330 | // users: traceroute.c hostname.c ifconfig.c ping.c | 335 | // users: traceroute.c hostname.c ifconfig.c ping.c |
331 | extern struct hostent *xgethostbyname(const char *name); | 336 | struct hostent *xgethostbyname(const char *name); |
332 | // ping6 is the only user - convert to new API | 337 | // ping6 is the only user - convert to new API |
333 | extern struct hostent *xgethostbyname2(const char *name, int af); | 338 | struct hostent *xgethostbyname2(const char *name, int af); |
334 | 339 | ||
335 | 340 | ||
336 | extern char *xstrdup(const char *s); | 341 | extern char *xstrdup(const char *s); |
diff --git a/libbb/xconnect.c b/libbb/xconnect.c index 0addda157..188837e36 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c | |||
@@ -245,25 +245,36 @@ int xconnect_stream(const len_and_sockaddr *lsa) | |||
245 | return fd; | 245 | return fd; |
246 | } | 246 | } |
247 | 247 | ||
248 | /* We hijack this constant to mean something else */ | ||
249 | /* It doesn't hurt because we will add this bit anyway */ | ||
250 | #define IGNORE_PORT NI_NUMERICSERV | ||
248 | static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags) | 251 | static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags) |
249 | { | 252 | { |
250 | char host[128]; | 253 | char host[128]; |
251 | char serv[16]; | 254 | char serv[16]; |
252 | int rc = getnameinfo(sa, salen, | 255 | int rc = getnameinfo(sa, salen, |
253 | host, sizeof(host), | 256 | host, sizeof(host), |
257 | /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */ | ||
254 | serv, sizeof(serv), | 258 | serv, sizeof(serv), |
255 | /* do not resolve port# into service _name_ */ | 259 | /* do not resolve port# into service _name_ */ |
256 | flags | NI_NUMERICSERV | 260 | flags | NI_NUMERICSERV |
257 | ); | 261 | ); |
258 | if (rc) | 262 | if (rc) |
259 | return NULL; | 263 | return NULL; |
264 | if (flags & IGNORE_PORT) | ||
265 | return xstrdup(host); | ||
260 | #if ENABLE_FEATURE_IPV6 | 266 | #if ENABLE_FEATURE_IPV6 |
261 | if (sa->sa_family == AF_INET6) | 267 | if (sa->sa_family == AF_INET6) { |
262 | return xasprintf("[%s]:%s", host, serv); | 268 | if (strchr(host, ':')) /* heh, it's not a resolved hostname */ |
269 | return xasprintf("[%s]:%s", host, serv); | ||
270 | /*return xasprintf("%s:%s", host, serv);*/ | ||
271 | /* - fall through instead */ | ||
272 | } | ||
263 | #endif | 273 | #endif |
264 | /* For now we don't support anything else, so it has to be INET */ | 274 | /* For now we don't support anything else, so it has to be INET */ |
265 | /*if (sa->sa_family == AF_INET)*/ | 275 | /*if (sa->sa_family == AF_INET)*/ |
266 | return xasprintf("%s:%s", host, serv); | 276 | return xasprintf("%s:%s", host, serv); |
277 | /*return xstrdup(host);*/ | ||
267 | } | 278 | } |
268 | 279 | ||
269 | char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen) | 280 | char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen) |
@@ -271,7 +282,16 @@ char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen) | |||
271 | return sockaddr2str(sa, salen, 0); | 282 | return sockaddr2str(sa, salen, 0); |
272 | } | 283 | } |
273 | 284 | ||
285 | char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t salen) | ||
286 | { | ||
287 | return sockaddr2str(sa, salen, NI_NAMEREQD | IGNORE_PORT); | ||
288 | } | ||
274 | char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen) | 289 | char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen) |
275 | { | 290 | { |
276 | return sockaddr2str(sa, salen, NI_NUMERICHOST); | 291 | return sockaddr2str(sa, salen, NI_NUMERICHOST); |
277 | } | 292 | } |
293 | |||
294 | char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen) | ||
295 | { | ||
296 | return sockaddr2str(sa, salen, NI_NUMERICHOST | IGNORE_PORT); | ||
297 | } | ||
diff --git a/networking/nslookup.c b/networking/nslookup.c index 5a08844f0..af0816215 100644 --- a/networking/nslookup.c +++ b/networking/nslookup.c | |||
@@ -47,7 +47,7 @@ | |||
47 | * ns3.kernel.org internet address = 204.152.191.36 | 47 | * ns3.kernel.org internet address = 204.152.191.36 |
48 | */ | 48 | */ |
49 | 49 | ||
50 | static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen) | 50 | /*static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen) |
51 | { | 51 | { |
52 | if (buflen <= 0) return -1; | 52 | if (buflen <= 0) return -1; |
53 | buf[0] = '\0'; | 53 | buf[0] = '\0'; |
@@ -61,9 +61,11 @@ static int sockaddr_to_dotted(struct sockaddr *saddr, char *buf, int buflen) | |||
61 | } | 61 | } |
62 | return -1; | 62 | return -1; |
63 | } | 63 | } |
64 | */ | ||
64 | 65 | ||
65 | static int print_host(const char *hostname, const char *header) | 66 | static int print_host(const char *hostname, const char *header) |
66 | { | 67 | { |
68 | #if 0 | ||
67 | char str[128]; /* IPv6 address will fit, hostnames hopefully too */ | 69 | char str[128]; /* IPv6 address will fit, hostnames hopefully too */ |
68 | struct addrinfo *result = NULL; | 70 | struct addrinfo *result = NULL; |
69 | int rc; | 71 | int rc; |
@@ -76,6 +78,7 @@ static int print_host(const char *hostname, const char *header) | |||
76 | hint.ai_socktype = SOCK_STREAM; | 78 | hint.ai_socktype = SOCK_STREAM; |
77 | // hint.ai_flags = AI_CANONNAME; | 79 | // hint.ai_flags = AI_CANONNAME; |
78 | rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result); | 80 | rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result); |
81 | |||
79 | if (!rc) { | 82 | if (!rc) { |
80 | struct addrinfo *cur = result; | 83 | struct addrinfo *cur = result; |
81 | // printf("%s\n", cur->ai_canonname); ? | 84 | // printf("%s\n", cur->ai_canonname); ? |
@@ -93,6 +96,75 @@ static int print_host(const char *hostname, const char *header) | |||
93 | } | 96 | } |
94 | freeaddrinfo(result); | 97 | freeaddrinfo(result); |
95 | return (rc != 0); | 98 | return (rc != 0); |
99 | |||
100 | #else | ||
101 | /* We can't use host2sockaddr() - we want to get ALL addresses, | ||
102 | * not just one */ | ||
103 | |||
104 | struct addrinfo *result = NULL; | ||
105 | int rc; | ||
106 | struct addrinfo hint; | ||
107 | |||
108 | memset(&hint, 0 , sizeof(hint)); | ||
109 | /* hint.ai_family = AF_UNSPEC; - zero anyway */ | ||
110 | /* Needed. Or else we will get each address thrice (or more) | ||
111 | * for each possible socket type (tcp,udp,raw...): */ | ||
112 | hint.ai_socktype = SOCK_STREAM; | ||
113 | // hint.ai_flags = AI_CANONNAME; | ||
114 | rc = getaddrinfo(hostname, NULL /*service*/, &hint, &result); | ||
115 | |||
116 | if (!rc) { | ||
117 | struct addrinfo *cur = result; | ||
118 | unsigned cnt = 0; | ||
119 | |||
120 | printf("%-10s %s\n", header, hostname); | ||
121 | // printf("%s\n", cur->ai_canonname); ? | ||
122 | while (cur) { | ||
123 | char *dotted, *revhost; | ||
124 | dotted = xmalloc_sockaddr2dotted_noport(cur->ai_addr, cur->ai_addrlen); | ||
125 | revhost = xmalloc_sockaddr2hostonly_noport(cur->ai_addr, cur->ai_addrlen); | ||
126 | |||
127 | printf("Address %u: %s%c", ++cnt, dotted, revhost ? ' ' : '\n'); | ||
128 | if (revhost) { | ||
129 | puts(revhost); | ||
130 | if (ENABLE_FEATURE_CLEAN_UP) | ||
131 | free(revhost); | ||
132 | } | ||
133 | if (ENABLE_FEATURE_CLEAN_UP) | ||
134 | free(dotted); | ||
135 | cur = cur->ai_next; | ||
136 | } | ||
137 | } else { | ||
138 | #if ENABLE_VERBOSE_RESOLUTION_ERRORS | ||
139 | bb_error_msg("getaddrinfo('%s') failed: %s", hostname, gai_strerror(rc)); | ||
140 | #else | ||
141 | bb_error_msg("can't resolve '%s'", hostname); | ||
142 | #endif | ||
143 | } | ||
144 | if (ENABLE_FEATURE_CLEAN_UP) | ||
145 | freeaddrinfo(result); | ||
146 | return (rc != 0); | ||
147 | #endif | ||
148 | } | ||
149 | |||
150 | |||
151 | /* lookup the default nameserver and display it */ | ||
152 | static void server_print(void) | ||
153 | { | ||
154 | char *server; | ||
155 | |||
156 | server = xmalloc_sockaddr2dotted_noport((struct sockaddr*)&_res.nsaddr_list[0], | ||
157 | sizeof(struct sockaddr_in)); | ||
158 | /* I honestly don't know what to do if DNS server has _IPv6 address_. | ||
159 | * Probably it is listed in | ||
160 | * _res._u._ext_.nsaddrs[MAXNS] (of type "struct sockaddr_in6*" each) | ||
161 | * but how to find out whether resolver uses | ||
162 | * _res.nsaddr_list[] or _res._u._ext_.nsaddrs[], or both? | ||
163 | * Looks like classic design from hell, BIND-grade. Hard to surpass. */ | ||
164 | print_host(server, "Server:"); | ||
165 | if (ENABLE_FEATURE_CLEAN_UP) | ||
166 | free(server); | ||
167 | puts(""); | ||
96 | } | 168 | } |
97 | 169 | ||
98 | 170 | ||
@@ -109,40 +181,27 @@ static void set_default_dns(char *server) | |||
109 | } | 181 | } |
110 | 182 | ||
111 | 183 | ||
112 | /* lookup the default nameserver and display it */ | ||
113 | static void server_print(void) | ||
114 | { | ||
115 | char str[INET6_ADDRSTRLEN]; | ||
116 | |||
117 | sockaddr_to_dotted((struct sockaddr*)&_res.nsaddr_list[0], str, sizeof(str)); | ||
118 | print_host(str, "Server:"); | ||
119 | puts(""); | ||
120 | } | ||
121 | |||
122 | |||
123 | int nslookup_main(int argc, char **argv) | 184 | int nslookup_main(int argc, char **argv) |
124 | { | 185 | { |
125 | /* | 186 | /* We allow 1 or 2 arguments. |
126 | * initialize DNS structure _res used in printing the default | 187 | * The first is the name to be looked up and the second is an |
127 | * name server and in the explicit name server option feature. | 188 | * optional DNS server with which to do the lookup. |
128 | */ | 189 | * More than 3 arguments is an error to follow the pattern of the |
129 | 190 | * standard nslookup */ | |
130 | res_init(); | ||
131 | |||
132 | /* | ||
133 | * We allow 1 or 2 arguments. | ||
134 | * The first is the name to be looked up and the second is an | ||
135 | * optional DNS server with which to do the lookup. | ||
136 | * More than 3 arguments is an error to follow the pattern of the | ||
137 | * standard nslookup | ||
138 | */ | ||
139 | 191 | ||
140 | if (argc < 2 || *argv[1] == '-' || argc > 3) | 192 | if (argc < 2 || *argv[1] == '-' || argc > 3) |
141 | bb_show_usage(); | 193 | bb_show_usage(); |
142 | else if(argc == 3) | 194 | |
195 | /* initialize DNS structure _res used in printing the default | ||
196 | * name server and in the explicit name server option feature. */ | ||
197 | res_init(); | ||
198 | /* rfc2133 says this enables IPv6 lookups */ | ||
199 | /* (but it also says "may be enabled in /etc/resolv.conf|) */ | ||
200 | /*_res.options |= RES_USE_INET6;*/ | ||
201 | |||
202 | if(argc == 3) | ||
143 | set_default_dns(argv[2]); | 203 | set_default_dns(argv[2]); |
144 | 204 | ||
145 | server_print(); | 205 | server_print(); |
146 | return print_host(argv[1], "Name: "); | 206 | return print_host(argv[1], "Name:"); |
147 | } | 207 | } |
148 | |||