diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-01-22 22:43:05 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-01-22 22:43:05 +0000 |
commit | a78f04d49051a5d5742ef605fb5d3670e9938c57 (patch) | |
tree | bc4a002a534b4c0fb8bba09dbd46f93cea5a38c6 /networking | |
parent | 00f85b4aa3a51486c7e1558653705e3ffec80d5a (diff) | |
download | busybox-w32-a78f04d49051a5d5742ef605fb5d3670e9938c57.tar.gz busybox-w32-a78f04d49051a5d5742ef605fb5d3670e9938c57.tar.bz2 busybox-w32-a78f04d49051a5d5742ef605fb5d3670e9938c57.zip |
nslookup: full circle. Here we started IPv6 work. Use "new API"
and thus save a few bytes.
git-svn-id: svn://busybox.net/trunk/busybox@17477 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'networking')
-rw-r--r-- | networking/nslookup.c | 117 |
1 files changed, 88 insertions, 29 deletions
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 | |||