diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2010-03-29 09:40:46 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2010-03-29 09:40:46 +0000 |
commit | 36aeef6edae1d4f8697a0608a0404a78427bf7be (patch) | |
tree | 12c767035121536f57826d46f7dbed9eb8da8fee /src/util/test_resolver_api.c | |
parent | 49ede7a464271d2fed12da3ec8bf6a1dde2b4e75 (diff) | |
download | gnunet-36aeef6edae1d4f8697a0608a0404a78427bf7be.tar.gz gnunet-36aeef6edae1d4f8697a0608a0404a78427bf7be.zip |
Added code to test gnunet's dns resolver service. The test code compares gnunet's results to the system's own forward and reverse name resolution. Using dns root server as a targets to get a static 1:1 mapping
Diffstat (limited to 'src/util/test_resolver_api.c')
-rw-r--r-- | src/util/test_resolver_api.c | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/util/test_resolver_api.c b/src/util/test_resolver_api.c index 194ed2f8f..b8a0eb29a 100644 --- a/src/util/test_resolver_api.c +++ b/src/util/test_resolver_api.c | |||
@@ -32,6 +32,11 @@ | |||
32 | 32 | ||
33 | #define VERBOSE GNUNET_NO | 33 | #define VERBOSE GNUNET_NO |
34 | 34 | ||
35 | // Using dns rootservers to check gnunet's resolver service | ||
36 | // a.root-servers.net <-> 198.41.0.4 is a fix 1:1 mapping that should not change over years | ||
37 | // For more information have a look at IANA's website http://www.root-servers.org/ | ||
38 | #define ROOTSERVER_NAME "a.root-servers.net" | ||
39 | #define ROOTSERVER_IP "198.41.0.4" | ||
35 | 40 | ||
36 | static void | 41 | static void |
37 | check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen) | 42 | check_hostname (void *cls, const struct sockaddr *sa, socklen_t salen) |
@@ -123,6 +128,57 @@ check_127 (void *cls, const struct sockaddr *sa, socklen_t salen) | |||
123 | } | 128 | } |
124 | 129 | ||
125 | static void | 130 | static void |
131 | check_rootserver_ip (void *cls, const struct sockaddr *sa, socklen_t salen) | ||
132 | { | ||
133 | int *ok = cls; | ||
134 | const struct sockaddr_in *sai = (const struct sockaddr_in *) sa; | ||
135 | |||
136 | if (sa == NULL) | ||
137 | return; | ||
138 | GNUNET_assert (sizeof (struct sockaddr_in) == salen); | ||
139 | |||
140 | if ( 0 == strcmp(inet_ntoa(sai->sin_addr),ROOTSERVER_IP)) | ||
141 | { | ||
142 | #if DEBUG_RESOLVER | ||
143 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct rootserver ip address.\n"); | ||
144 | #endif | ||
145 | (*ok) &= ~1; | ||
146 | } | ||
147 | else | ||
148 | { | ||
149 | #if DEBUG_RESOLVER | ||
150 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received incorrect rootserver ip address.\n"); | ||
151 | #endif | ||
152 | GNUNET_break (0); | ||
153 | } | ||
154 | } | ||
155 | |||
156 | static void | ||
157 | check_rootserver_name (void *cls, const char *hostname) | ||
158 | { | ||
159 | int *ok = cls; | ||
160 | if (hostname == NULL) | ||
161 | return; | ||
162 | |||
163 | if (0 == strcmp (hostname, ROOTSERVER_NAME)) | ||
164 | { | ||
165 | #if DEBUG_RESOLVER | ||
166 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
167 | "Received correct rootserver hostname `%s'.\n", hostname); | ||
168 | #endif | ||
169 | (*ok) &= ~2; | ||
170 | } | ||
171 | else | ||
172 | { | ||
173 | #if DEBUG_RESOLVER | ||
174 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
175 | "Received invalid rootserver hostname `%s'.\n", hostname); | ||
176 | #endif | ||
177 | GNUNET_break (0); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | static void | ||
126 | run (void *cls, | 182 | run (void *cls, |
127 | struct GNUNET_SCHEDULER_Handle *sched, | 183 | struct GNUNET_SCHEDULER_Handle *sched, |
128 | char *const *args, | 184 | char *const *args, |
@@ -152,6 +208,119 @@ run (void *cls, | |||
152 | GNUNET_RESOLVER_hostname_resolve (sched, | 208 | GNUNET_RESOLVER_hostname_resolve (sched, |
153 | cfg, | 209 | cfg, |
154 | AF_UNSPEC, timeout, &check_hostname, cls); | 210 | AF_UNSPEC, timeout, &check_hostname, cls); |
211 | // Testing non-local dns resolution | ||
212 | // DNS Rootserver to test: a.root-servers.net - 198.41.0.4 | ||
213 | |||
214 | char const * rootserver_name = ROOTSERVER_NAME; | ||
215 | |||
216 | struct hostent *rootserver; | ||
217 | |||
218 | rootserver = gethostbyname(rootserver_name); | ||
219 | if (rootserver == NULL) | ||
220 | { | ||
221 | // Error: resolving ip addresses does not work | ||
222 | #if DEBUG_RESOLVER | ||
223 | switch (h_errno) | ||
224 | { | ||
225 | |||
226 | case HOST_NOT_FOUND: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyname() could not lookup ip address: HOST_NOT_FOUND\n");break; | ||
227 | case NO_ADDRESS: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyname() could not lookup ip address: NO_ADDRESS\n");break; | ||
228 | case NO_RECOVERY: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyname() could not lookup ip address: NO_RECOVERY\n");break; | ||
229 | case TRY_AGAIN: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyname() could not lookup ip address: TRY_AGAIN\n");break; | ||
230 | } | ||
231 | #endif | ||
232 | GNUNET_break (0); | ||
233 | } | ||
234 | else | ||
235 | { | ||
236 | // Counting returned ip addresses | ||
237 | int count_ips =0 ; | ||
238 | while (rootserver->h_addr_list[count_ips]!=NULL) | ||
239 | { | ||
240 | count_ips++; | ||
241 | } | ||
242 | if ( count_ips > 1) | ||
243 | { | ||
244 | #if DEBUG_RESOLVER | ||
245 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ip range for root name server, but a root nameserver has only 1 ip\n"); | ||
246 | #endif | ||
247 | GNUNET_break (0); | ||
248 | } | ||
249 | |||
250 | // Comparing to resolved address to the address the root nameserver should have | ||
251 | if ( strcmp(inet_ntoa( *(struct in_addr *) rootserver->h_addr_list[0]),ROOTSERVER_IP) !=0) | ||
252 | { | ||
253 | #if DEBUG_RESOLVER | ||
254 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ip and ip for root name server differ\n"); | ||
255 | #endif | ||
256 | GNUNET_break (0); | ||
257 | } | ||
258 | |||
259 | #if DEBUG_RESOLVER | ||
260 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own forward name resolution is working\n"); | ||
261 | #endif | ||
262 | |||
263 | // Resolve the same using GNUNET | ||
264 | GNUNET_RESOLVER_ip_get (sched, cfg, ROOTSERVER_NAME, AF_INET, timeout, &check_rootserver_ip, cls); | ||
265 | |||
266 | // Success: forward lookups work as exptected | ||
267 | |||
268 | |||
269 | // Next step: reverse lookups | ||
270 | |||
271 | struct in_addr rootserver_addr; | ||
272 | rootserver->h_name=""; | ||
273 | if ( 1 != inet_pton(AF_INET, ROOTSERVER_IP, &rootserver_addr)) | ||
274 | { | ||
275 | #if DEBUG_RESOLVER | ||
276 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not transform root nameserver ip addressr\n"); | ||
277 | #endif | ||
278 | GNUNET_break (0); | ||
279 | } | ||
280 | |||
281 | rootserver = gethostbyaddr(&rootserver_addr, sizeof(rootserver_addr), AF_INET); | ||
282 | if (rootserver == NULL) | ||
283 | { | ||
284 | // Error: resolving ip addresses does not work | ||
285 | #if DEBUG_RESOLVER | ||
286 | switch (h_errno) | ||
287 | { | ||
288 | |||
289 | case HOST_NOT_FOUND: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: HOST_NOT_FOUND\n");break; | ||
290 | case NO_ADDRESS: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: NO_ADDRESS\n");break; | ||
291 | case NO_RECOVERY: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: NO_RECOVERY\n");break; | ||
292 | case TRY_AGAIN: GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "gethostbyaddr() could not lookup ip address: TRY_AGAIN\n");break; | ||
293 | } | ||
294 | #endif | ||
295 | GNUNET_break (0); | ||
296 | } | ||
297 | |||
298 | if ( 0 != strcmp( rootserver->h_name,ROOTSERVER_NAME)) | ||
299 | { | ||
300 | #if DEBUG_RESOLVER | ||
301 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received hostname and hostname for root name server differ\n"); | ||
302 | #endif | ||
303 | GNUNET_break (0); | ||
304 | } | ||
305 | |||
306 | #if DEBUG_RESOLVER | ||
307 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "System's own reverse name resolution is working\n"); | ||
308 | #endif | ||
309 | // Resolve the same using GNUNET | ||
310 | |||
311 | memset (&sa, 0, sizeof (sa)); | ||
312 | sa.sin_family = AF_INET; | ||
313 | inet_aton(ROOTSERVER_IP, &sa.sin_addr.s_addr); | ||
314 | |||
315 | GNUNET_RESOLVER_hostname_get (sched, | ||
316 | cfg, | ||
317 | (const struct sockaddr *) &sa, | ||
318 | sizeof (struct sockaddr), | ||
319 | GNUNET_YES, | ||
320 | timeout, &check_rootserver_name, cls); | ||
321 | |||
322 | // Success: reverse lookups work as exptected | ||
323 | } | ||
155 | } | 324 | } |
156 | 325 | ||
157 | static int | 326 | static int |