summaryrefslogtreecommitdiff
path: root/src/util/resolver_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/resolver_api.c')
-rw-r--r--src/util/resolver_api.c1294
1 files changed, 651 insertions, 643 deletions
diff --git a/src/util/resolver_api.c b/src/util/resolver_api.c
index 78ce222e6..6c56786b0 100644
--- a/src/util/resolver_api.c
+++ b/src/util/resolver_api.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file util/resolver_api.c 22 * @file util/resolver_api.c
@@ -29,9 +29,9 @@
29#include "gnunet_resolver_service.h" 29#include "gnunet_resolver_service.h"
30#include "resolver.h" 30#include "resolver.h"
31 31
32#define LOG(kind,...) GNUNET_log_from (kind, "util-resolver-api", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from(kind, "util-resolver-api", __VA_ARGS__)
33 33
34#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-resolver-api", syscall) 34#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror(kind, "util-resolver-api", syscall)
35 35
36/** 36/**
37 * Maximum supported length for a hostname 37 * Maximum supported length for a hostname
@@ -97,9 +97,7 @@ static struct GNUNET_SCHEDULER_Task *s_task;
97 * the request prior to the timeout or successful execution. Also 97 * the request prior to the timeout or successful execution. Also
98 * used to track our internal state for the request. 98 * used to track our internal state for the request.
99 */ 99 */
100struct GNUNET_RESOLVER_RequestHandle 100struct GNUNET_RESOLVER_RequestHandle {
101{
102
103 /** 101 /**
104 * Next entry in DLL of requests. 102 * Next entry in DLL of requests.
105 */ 103 */
@@ -186,61 +184,61 @@ struct GNUNET_RESOLVER_RequestHandle
186 * #GNUNET_SYSERR otherwise. 184 * #GNUNET_SYSERR otherwise.
187 */ 185 */
188static int 186static int
189check_config () 187check_config()
190{ 188{
191 char *hostname; 189 char *hostname;
192 struct sockaddr_in v4; 190 struct sockaddr_in v4;
193 struct sockaddr_in6 v6; 191 struct sockaddr_in6 v6;
194 192
195 if (GNUNET_OK == 193 if (GNUNET_OK ==
196 GNUNET_CONFIGURATION_have_value (resolver_cfg, 194 GNUNET_CONFIGURATION_have_value(resolver_cfg,
197 "resolver", 195 "resolver",
198 "UNIXPATH")) 196 "UNIXPATH"))
199 return GNUNET_OK; 197 return GNUNET_OK;
200 memset (&v4, 0, sizeof (v4)); 198 memset(&v4, 0, sizeof(v4));
201 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK); 199 v4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
202 v4.sin_family = AF_INET; 200 v4.sin_family = AF_INET;
203#if HAVE_SOCKADDR_IN_SIN_LEN 201#if HAVE_SOCKADDR_IN_SIN_LEN
204 v4.sin_len = sizeof (v4); 202 v4.sin_len = sizeof(v4);
205#endif 203#endif
206 memset (&v6, 0, sizeof (v6)); 204 memset(&v6, 0, sizeof(v6));
207 v6.sin6_family = AF_INET6; 205 v6.sin6_family = AF_INET6;
208#if HAVE_SOCKADDR_IN_SIN_LEN 206#if HAVE_SOCKADDR_IN_SIN_LEN
209 v6.sin6_len = sizeof (v6); 207 v6.sin6_len = sizeof(v6);
210#endif 208#endif
211 if (GNUNET_OK != 209 if (GNUNET_OK !=
212 GNUNET_CONFIGURATION_get_value_string (resolver_cfg, 210 GNUNET_CONFIGURATION_get_value_string(resolver_cfg,
213 "resolver", 211 "resolver",
214 "HOSTNAME", 212 "HOSTNAME",
215 &hostname)) 213 &hostname))
216 { 214 {
217 LOG (GNUNET_ERROR_TYPE_INFO, 215 LOG(GNUNET_ERROR_TYPE_INFO,
218 _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"), 216 _("Missing `%s' for `%s' in configuration, DNS resolution will be unavailable.\n"),
219 "HOSTNAME", 217 "HOSTNAME",
220 "resolver"); 218 "resolver");
221 return GNUNET_SYSERR; 219 return GNUNET_SYSERR;
222 } 220 }
223 if ( (1 == inet_pton (AF_INET, hostname, &v4)) || 221 if ((1 == inet_pton(AF_INET, hostname, &v4)) ||
224 (1 == inet_pton (AF_INET6, hostname, &v6)) ) 222 (1 == inet_pton(AF_INET6, hostname, &v6)))
225 {
226 GNUNET_free (hostname);
227 return GNUNET_OK;
228 }
229 for (unsigned int i = 0;
230 NULL != loopback[i];
231 i++)
232 if (0 == strcasecmp (loopback[i],
233 hostname))
234 { 223 {
235 GNUNET_free (hostname); 224 GNUNET_free(hostname);
236 return GNUNET_OK; 225 return GNUNET_OK;
237 } 226 }
238 LOG (GNUNET_ERROR_TYPE_INFO, 227 for (unsigned int i = 0;
239 _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"), 228 NULL != loopback[i];
240 "localhost", 229 i++)
241 "HOSTNAME", 230 if (0 == strcasecmp(loopback[i],
242 "resolver"); 231 hostname))
243 GNUNET_free (hostname); 232 {
233 GNUNET_free(hostname);
234 return GNUNET_OK;
235 }
236 LOG(GNUNET_ERROR_TYPE_INFO,
237 _("Missing `%s' or numeric IP address for `%s' of `%s' in configuration, DNS resolution will be unavailable.\n"),
238 "localhost",
239 "HOSTNAME",
240 "resolver");
241 GNUNET_free(hostname);
244 return GNUNET_SYSERR; 242 return GNUNET_SYSERR;
245} 243}
246 244
@@ -251,9 +249,9 @@ check_config ()
251 * @param cfg configuration to use 249 * @param cfg configuration to use
252 */ 250 */
253void 251void
254GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) 252GNUNET_RESOLVER_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
255{ 253{
256 GNUNET_assert (NULL != cfg); 254 GNUNET_assert(NULL != cfg);
257 backoff = GNUNET_TIME_UNIT_MILLISECONDS; 255 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
258 resolver_cfg = cfg; 256 resolver_cfg = cfg;
259} 257}
@@ -263,35 +261,35 @@ GNUNET_RESOLVER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
263 * Destroy the connection to the resolver service. 261 * Destroy the connection to the resolver service.
264 */ 262 */
265void 263void
266GNUNET_RESOLVER_disconnect () 264GNUNET_RESOLVER_disconnect()
267{ 265{
268 struct GNUNET_RESOLVER_RequestHandle *rh; 266 struct GNUNET_RESOLVER_RequestHandle *rh;
269 267
270 while (NULL != (rh = req_head)) 268 while (NULL != (rh = req_head))
271 { 269 {
272 GNUNET_assert (GNUNET_SYSERR == rh->was_transmitted); 270 GNUNET_assert(GNUNET_SYSERR == rh->was_transmitted);
273 GNUNET_CONTAINER_DLL_remove (req_head, 271 GNUNET_CONTAINER_DLL_remove(req_head,
274 req_tail, 272 req_tail,
275 rh); 273 rh);
276 GNUNET_free (rh); 274 GNUNET_free(rh);
277 } 275 }
278 if (NULL != mq) 276 if (NULL != mq)
279 { 277 {
280 LOG (GNUNET_ERROR_TYPE_DEBUG, 278 LOG(GNUNET_ERROR_TYPE_DEBUG,
281 "Disconnecting from DNS service\n"); 279 "Disconnecting from DNS service\n");
282 GNUNET_MQ_destroy (mq); 280 GNUNET_MQ_destroy(mq);
283 mq = NULL; 281 mq = NULL;
284 } 282 }
285 if (NULL != r_task) 283 if (NULL != r_task)
286 { 284 {
287 GNUNET_SCHEDULER_cancel (r_task); 285 GNUNET_SCHEDULER_cancel(r_task);
288 r_task = NULL; 286 r_task = NULL;
289 } 287 }
290 if (NULL != s_task) 288 if (NULL != s_task)
291 { 289 {
292 GNUNET_SCHEDULER_cancel (s_task); 290 GNUNET_SCHEDULER_cancel(s_task);
293 s_task = NULL; 291 s_task = NULL;
294 } 292 }
295} 293}
296 294
297 295
@@ -299,11 +297,11 @@ GNUNET_RESOLVER_disconnect ()
299 * Task executed on system shutdown. 297 * Task executed on system shutdown.
300 */ 298 */
301static void 299static void
302shutdown_task (void *cls) 300shutdown_task(void *cls)
303{ 301{
304 (void) cls; 302 (void)cls;
305 s_task = NULL; 303 s_task = NULL;
306 GNUNET_RESOLVER_disconnect (); 304 GNUNET_RESOLVER_disconnect();
307 backoff = GNUNET_TIME_UNIT_MILLISECONDS; 305 backoff = GNUNET_TIME_UNIT_MILLISECONDS;
308} 306}
309 307
@@ -312,7 +310,7 @@ shutdown_task (void *cls)
312 * Consider disconnecting if we have no further requests pending. 310 * Consider disconnecting if we have no further requests pending.
313 */ 311 */
314static void 312static void
315check_disconnect () 313check_disconnect()
316{ 314{
317 for (struct GNUNET_RESOLVER_RequestHandle *rh = req_head; 315 for (struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
318 NULL != rh; 316 NULL != rh;
@@ -320,15 +318,15 @@ check_disconnect ()
320 if (GNUNET_SYSERR != rh->was_transmitted) 318 if (GNUNET_SYSERR != rh->was_transmitted)
321 return; 319 return;
322 if (NULL != r_task) 320 if (NULL != r_task)
323 { 321 {
324 GNUNET_SCHEDULER_cancel (r_task); 322 GNUNET_SCHEDULER_cancel(r_task);
325 r_task = NULL; 323 r_task = NULL;
326 } 324 }
327 if (NULL != s_task) 325 if (NULL != s_task)
328 return; 326 return;
329 s_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, 327 s_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MILLISECONDS,
330 &shutdown_task, 328 &shutdown_task,
331 NULL); 329 NULL);
332} 330}
333 331
334 332
@@ -341,47 +339,49 @@ check_disconnect ()
341 * @return address as a string, NULL on error 339 * @return address as a string, NULL on error
342 */ 340 */
343static char * 341static char *
344no_resolve (int af, 342no_resolve(int af,
345 const void *ip, 343 const void *ip,
346 socklen_t ip_len) 344 socklen_t ip_len)
347{ 345{
348 char buf[INET6_ADDRSTRLEN]; 346 char buf[INET6_ADDRSTRLEN];
349 347
350 switch (af) 348 switch (af)
351 {
352 case AF_INET:
353 if (ip_len != sizeof (struct in_addr))
354 return NULL;
355 if (NULL ==
356 inet_ntop (AF_INET,
357 ip,
358 buf,
359 sizeof (buf)))
360 { 349 {
361 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 350 case AF_INET:
362 "inet_ntop"); 351 if (ip_len != sizeof(struct in_addr))
363 return NULL; 352 return NULL;
364 } 353 if (NULL ==
365 break; 354 inet_ntop(AF_INET,
366 case AF_INET6: 355 ip,
367 if (ip_len != sizeof (struct in6_addr)) 356 buf,
368 return NULL; 357 sizeof(buf)))
369 if (NULL == 358 {
370 inet_ntop (AF_INET6, 359 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
371 ip, 360 "inet_ntop");
372 buf, 361 return NULL;
373 sizeof (buf))) 362 }
374 { 363 break;
375 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, 364
376 "inet_ntop"); 365 case AF_INET6:
366 if (ip_len != sizeof(struct in6_addr))
367 return NULL;
368 if (NULL ==
369 inet_ntop(AF_INET6,
370 ip,
371 buf,
372 sizeof(buf)))
373 {
374 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING,
375 "inet_ntop");
376 return NULL;
377 }
378 break;
379
380 default:
381 GNUNET_break(0);
377 return NULL; 382 return NULL;
378 } 383 }
379 break; 384 return GNUNET_strdup(buf);
380 default:
381 GNUNET_break (0);
382 return NULL;
383 }
384 return GNUNET_strdup (buf);
385} 385}
386 386
387 387
@@ -389,7 +389,7 @@ no_resolve (int af,
389 * Adjust exponential back-off and reconnect to the service. 389 * Adjust exponential back-off and reconnect to the service.
390 */ 390 */
391static void 391static void
392reconnect (void); 392reconnect(void);
393 393
394 394
395/** 395/**
@@ -401,16 +401,16 @@ reconnect (void);
401 * @param error error code 401 * @param error error code
402 */ 402 */
403static void 403static void
404mq_error_handler (void *cls, 404mq_error_handler(void *cls,
405 enum GNUNET_MQ_Error error) 405 enum GNUNET_MQ_Error error)
406{ 406{
407 (void) cls; 407 (void)cls;
408 GNUNET_MQ_destroy (mq); 408 GNUNET_MQ_destroy(mq);
409 mq = NULL; 409 mq = NULL;
410 LOG (GNUNET_ERROR_TYPE_DEBUG, 410 LOG(GNUNET_ERROR_TYPE_DEBUG,
411 "MQ error %d, reconnecting\n", 411 "MQ error %d, reconnecting\n",
412 error); 412 error);
413 reconnect (); 413 reconnect();
414} 414}
415 415
416 416
@@ -418,44 +418,44 @@ mq_error_handler (void *cls,
418 * Process pending requests to the resolver. 418 * Process pending requests to the resolver.
419 */ 419 */
420static void 420static void
421process_requests () 421process_requests()
422{ 422{
423 struct GNUNET_RESOLVER_GetMessage *msg; 423 struct GNUNET_RESOLVER_GetMessage *msg;
424 struct GNUNET_MQ_Envelope *env; 424 struct GNUNET_MQ_Envelope *env;
425 struct GNUNET_RESOLVER_RequestHandle *rh = req_head; 425 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
426 426
427 if (NULL == mq) 427 if (NULL == mq)
428 { 428 {
429 reconnect (); 429 reconnect();
430 return; 430 return;
431 } 431 }
432 if (NULL == rh) 432 if (NULL == rh)
433 { 433 {
434 /* nothing to do, release socket really soon if there is nothing 434 /* nothing to do, release socket really soon if there is nothing
435 * else happening... */ 435 * else happening... */
436 if (NULL == s_task) 436 if (NULL == s_task)
437 s_task = 437 s_task =
438 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, 438 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MILLISECONDS,
439 &shutdown_task, 439 &shutdown_task,
440 NULL); 440 NULL);
441 return; 441 return;
442 } 442 }
443 if (GNUNET_NO != rh->was_transmitted) 443 if (GNUNET_NO != rh->was_transmitted)
444 return; /* waiting for reply */ 444 return; /* waiting for reply */
445 env = GNUNET_MQ_msg_extra (msg, 445 env = GNUNET_MQ_msg_extra(msg,
446 rh->data_len, 446 rh->data_len,
447 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST); 447 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
448 msg->direction = htonl (rh->direction); 448 msg->direction = htonl(rh->direction);
449 msg->af = htonl (rh->af); 449 msg->af = htonl(rh->af);
450 msg->client_id = rh->id; 450 msg->client_id = rh->id;
451 GNUNET_memcpy (&msg[1], 451 GNUNET_memcpy(&msg[1],
452 &rh[1], 452 &rh[1],
453 rh->data_len); 453 rh->data_len);
454 LOG (GNUNET_ERROR_TYPE_DEBUG, 454 LOG(GNUNET_ERROR_TYPE_DEBUG,
455 "Transmitting DNS resolution request (ID %u) to DNS service\n", 455 "Transmitting DNS resolution request (ID %u) to DNS service\n",
456 rh->id); 456 rh->id);
457 GNUNET_MQ_send (mq, 457 GNUNET_MQ_send(mq,
458 env); 458 env);
459 rh->was_transmitted = GNUNET_YES; 459 rh->was_transmitted = GNUNET_YES;
460} 460}
461 461
@@ -467,11 +467,11 @@ process_requests ()
467 * @param msg message with the hostname 467 * @param msg message with the hostname
468 */ 468 */
469static int 469static int
470check_response (void *cls, 470check_response(void *cls,
471 const struct GNUNET_RESOLVER_ResponseMessage *msg) 471 const struct GNUNET_RESOLVER_ResponseMessage *msg)
472{ 472{
473 (void) cls; 473 (void)cls;
474 (void) msg; 474 (void)msg;
475 475
476 /* implemented in #handle_response() for now */ 476 /* implemented in #handle_response() for now */
477 return GNUNET_OK; 477 return GNUNET_OK;
@@ -487,8 +487,8 @@ check_response (void *cls,
487 * @param msg message with the response 487 * @param msg message with the response
488 */ 488 */
489static void 489static void
490handle_response (void *cls, 490handle_response(void *cls,
491 const struct GNUNET_RESOLVER_ResponseMessage *msg) 491 const struct GNUNET_RESOLVER_ResponseMessage *msg)
492{ 492{
493 struct GNUNET_RESOLVER_RequestHandle *rh = req_head; 493 struct GNUNET_RESOLVER_RequestHandle *rh = req_head;
494 uint16_t size; 494 uint16_t size;
@@ -496,141 +496,141 @@ handle_response (void *cls,
496 uint32_t client_request_id = msg->client_id; 496 uint32_t client_request_id = msg->client_id;
497 497
498 for (; rh != NULL; rh = rh->next) 498 for (; rh != NULL; rh = rh->next)
499 { 499 {
500 if (rh->id == client_request_id) 500 if (rh->id == client_request_id)
501 break; 501 break;
502 } 502 }
503 503
504 (void) cls; 504 (void)cls;
505 if (NULL == rh) 505 if (NULL == rh)
506 {
507 /* Resolver service sent extra replies to query (after terminator)? Bad! */
508 GNUNET_break (0);
509 GNUNET_MQ_destroy (mq);
510 mq = NULL;
511 reconnect ();
512 return;
513 }
514 size = ntohs (msg->header.size);
515 if (size == sizeof (struct GNUNET_RESOLVER_ResponseMessage))
516 {
517 LOG (GNUNET_ERROR_TYPE_DEBUG,
518 "Received empty response from DNS service\n");
519 /* message contains not data, just header; end of replies */
520 /* check if request was canceled */
521 if (GNUNET_SYSERR != rh->was_transmitted)
522 { 506 {
523 /* no reverse lookup was successful, return IP as string */ 507 /* Resolver service sent extra replies to query (after terminator)? Bad! */
524 if (NULL != rh->name_callback) 508 GNUNET_break(0);
525 { 509 GNUNET_MQ_destroy(mq);
526 if (GNUNET_NO == rh->received_response) 510 mq = NULL;
527 { 511 reconnect();
528 nret = no_resolve (rh->af, 512 return;
529 &rh[1],
530 rh->data_len);
531 rh->name_callback (rh->cls, nret);
532 GNUNET_free (nret);
533 }
534 /* finally, make termination call */
535 if (GNUNET_SYSERR != rh->was_transmitted)
536 rh->name_callback (rh->cls,
537 NULL);
538 }
539 if ( (NULL != rh->addr_callback) &&
540 (GNUNET_SYSERR != rh->was_transmitted) )
541 rh->addr_callback (rh->cls,
542 NULL,
543 0);
544 } 513 }
545 rh->was_transmitted = GNUNET_NO; 514 size = ntohs(msg->header.size);
546 GNUNET_RESOLVER_request_cancel (rh); 515 if (size == sizeof(struct GNUNET_RESOLVER_ResponseMessage))
547 process_requests ();
548 return;
549 }
550 /* return reverse lookup results to caller */
551 if (NULL != rh->name_callback)
552 {
553 const char *hostname;
554
555 hostname = (const char *) &msg[1];
556 if (hostname[size - sizeof (struct GNUNET_RESOLVER_ResponseMessage) - 1] != '\0')
557 { 516 {
558 GNUNET_break (0); 517 LOG(GNUNET_ERROR_TYPE_DEBUG,
518 "Received empty response from DNS service\n");
519 /* message contains not data, just header; end of replies */
520 /* check if request was canceled */
559 if (GNUNET_SYSERR != rh->was_transmitted) 521 if (GNUNET_SYSERR != rh->was_transmitted)
560 rh->name_callback (rh->cls, 522 {
561 NULL); 523 /* no reverse lookup was successful, return IP as string */
524 if (NULL != rh->name_callback)
525 {
526 if (GNUNET_NO == rh->received_response)
527 {
528 nret = no_resolve(rh->af,
529 &rh[1],
530 rh->data_len);
531 rh->name_callback(rh->cls, nret);
532 GNUNET_free(nret);
533 }
534 /* finally, make termination call */
535 if (GNUNET_SYSERR != rh->was_transmitted)
536 rh->name_callback(rh->cls,
537 NULL);
538 }
539 if ((NULL != rh->addr_callback) &&
540 (GNUNET_SYSERR != rh->was_transmitted))
541 rh->addr_callback(rh->cls,
542 NULL,
543 0);
544 }
562 rh->was_transmitted = GNUNET_NO; 545 rh->was_transmitted = GNUNET_NO;
563 GNUNET_RESOLVER_request_cancel (rh); 546 GNUNET_RESOLVER_request_cancel(rh);
564 GNUNET_MQ_destroy (mq); 547 process_requests();
565 mq = NULL;
566 reconnect ();
567 return; 548 return;
568 } 549 }
569 LOG (GNUNET_ERROR_TYPE_DEBUG, 550 /* return reverse lookup results to caller */
570 "Resolver returns `%s' for IP `%s'.\n", 551 if (NULL != rh->name_callback)
571 hostname, 552 {
572 GNUNET_a2s ((const void *) &rh[1], 553 const char *hostname;
554
555 hostname = (const char *)&msg[1];
556 if (hostname[size - sizeof(struct GNUNET_RESOLVER_ResponseMessage) - 1] != '\0')
557 {
558 GNUNET_break(0);
559 if (GNUNET_SYSERR != rh->was_transmitted)
560 rh->name_callback(rh->cls,
561 NULL);
562 rh->was_transmitted = GNUNET_NO;
563 GNUNET_RESOLVER_request_cancel(rh);
564 GNUNET_MQ_destroy(mq);
565 mq = NULL;
566 reconnect();
567 return;
568 }
569 LOG(GNUNET_ERROR_TYPE_DEBUG,
570 "Resolver returns `%s' for IP `%s'.\n",
571 hostname,
572 GNUNET_a2s((const void *)&rh[1],
573 rh->data_len)); 573 rh->data_len));
574 if (rh->was_transmitted != GNUNET_SYSERR) 574 if (rh->was_transmitted != GNUNET_SYSERR)
575 rh->name_callback (rh->cls, 575 rh->name_callback(rh->cls,
576 hostname); 576 hostname);
577 rh->received_response = GNUNET_YES; 577 rh->received_response = GNUNET_YES;
578 } 578 }
579 /* return lookup results to caller */ 579 /* return lookup results to caller */
580 if (NULL != rh->addr_callback) 580 if (NULL != rh->addr_callback)
581 {
582 struct sockaddr_in v4;
583 struct sockaddr_in6 v6;
584 const struct sockaddr *sa;
585 socklen_t salen;
586 const void *ip;
587 size_t ip_len;
588
589 ip = &msg[1];
590 ip_len = size - sizeof (struct GNUNET_RESOLVER_ResponseMessage);
591 if (ip_len == sizeof (struct in_addr))
592 { 581 {
593 memset (&v4, 0, sizeof (v4)); 582 struct sockaddr_in v4;
594 v4.sin_family = AF_INET; 583 struct sockaddr_in6 v6;
595 v4.sin_addr = *(struct in_addr*) ip; 584 const struct sockaddr *sa;
585 socklen_t salen;
586 const void *ip;
587 size_t ip_len;
588
589 ip = &msg[1];
590 ip_len = size - sizeof(struct GNUNET_RESOLVER_ResponseMessage);
591 if (ip_len == sizeof(struct in_addr))
592 {
593 memset(&v4, 0, sizeof(v4));
594 v4.sin_family = AF_INET;
595 v4.sin_addr = *(struct in_addr*)ip;
596#if HAVE_SOCKADDR_IN_SIN_LEN 596#if HAVE_SOCKADDR_IN_SIN_LEN
597 v4.sin_len = sizeof (v4); 597 v4.sin_len = sizeof(v4);
598#endif 598#endif
599 salen = sizeof (v4); 599 salen = sizeof(v4);
600 sa = (const struct sockaddr *) &v4; 600 sa = (const struct sockaddr *)&v4;
601 } 601 }
602 else if (ip_len == sizeof (struct in6_addr)) 602 else if (ip_len == sizeof(struct in6_addr))
603 { 603 {
604 memset (&v6, 0, sizeof (v6)); 604 memset(&v6, 0, sizeof(v6));
605 v6.sin6_family = AF_INET6; 605 v6.sin6_family = AF_INET6;
606 v6.sin6_addr = *(struct in6_addr*) ip; 606 v6.sin6_addr = *(struct in6_addr*)ip;
607#if HAVE_SOCKADDR_IN_SIN_LEN 607#if HAVE_SOCKADDR_IN_SIN_LEN
608 v6.sin6_len = sizeof (v6); 608 v6.sin6_len = sizeof(v6);
609#endif 609#endif
610 salen = sizeof (v6); 610 salen = sizeof(v6);
611 sa = (const struct sockaddr *) &v6; 611 sa = (const struct sockaddr *)&v6;
612 } 612 }
613 else 613 else
614 { 614 {
615 GNUNET_break (0); 615 GNUNET_break(0);
616 if (GNUNET_SYSERR != rh->was_transmitted)
617 rh->addr_callback(rh->cls,
618 NULL,
619 0);
620 rh->was_transmitted = GNUNET_NO;
621 GNUNET_RESOLVER_request_cancel(rh);
622 GNUNET_MQ_destroy(mq);
623 mq = NULL;
624 reconnect();
625 return;
626 }
627 LOG(GNUNET_ERROR_TYPE_DEBUG,
628 "Received IP from DNS service\n");
616 if (GNUNET_SYSERR != rh->was_transmitted) 629 if (GNUNET_SYSERR != rh->was_transmitted)
617 rh->addr_callback (rh->cls, 630 rh->addr_callback(rh->cls,
618 NULL, 631 sa,
619 0); 632 salen);
620 rh->was_transmitted = GNUNET_NO;
621 GNUNET_RESOLVER_request_cancel (rh);
622 GNUNET_MQ_destroy (mq);
623 mq = NULL;
624 reconnect ();
625 return;
626 } 633 }
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 "Received IP from DNS service\n");
629 if (GNUNET_SYSERR != rh->was_transmitted)
630 rh->addr_callback (rh->cls,
631 sa,
632 salen);
633 }
634} 634}
635 635
636 636
@@ -642,7 +642,7 @@ handle_response (void *cls,
642 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request 642 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
643 */ 643 */
644static void 644static void
645numeric_resolution (void *cls) 645numeric_resolution(void *cls)
646{ 646{
647 struct GNUNET_RESOLVER_RequestHandle *rh = cls; 647 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
648 struct sockaddr_in v4; 648 struct sockaddr_in v4;
@@ -650,63 +650,63 @@ numeric_resolution (void *cls)
650 const char *hostname; 650 const char *hostname;
651 651
652 rh->task = NULL; 652 rh->task = NULL;
653 memset (&v4, 0, sizeof (v4)); 653 memset(&v4, 0, sizeof(v4));
654 v4.sin_family = AF_INET; 654 v4.sin_family = AF_INET;
655#if HAVE_SOCKADDR_IN_SIN_LEN 655#if HAVE_SOCKADDR_IN_SIN_LEN
656 v4.sin_len = sizeof (v4); 656 v4.sin_len = sizeof(v4);
657#endif 657#endif
658 memset (&v6, 0, sizeof (v6)); 658 memset(&v6, 0, sizeof(v6));
659 v6.sin6_family = AF_INET6; 659 v6.sin6_family = AF_INET6;
660#if HAVE_SOCKADDR_IN_SIN_LEN 660#if HAVE_SOCKADDR_IN_SIN_LEN
661 v6.sin6_len = sizeof (v6); 661 v6.sin6_len = sizeof(v6);
662#endif 662#endif
663 hostname = (const char *) &rh[1]; 663 hostname = (const char *)&rh[1];
664 if ( ( (rh->af == AF_UNSPEC) || 664 if (((rh->af == AF_UNSPEC) ||
665 (rh->af == AF_INET) ) && 665 (rh->af == AF_INET)) &&
666 (1 == inet_pton (AF_INET, 666 (1 == inet_pton(AF_INET,
667 hostname, 667 hostname,
668 &v4.sin_addr)) ) 668 &v4.sin_addr)))
669 {
670 rh->addr_callback (rh->cls,
671 (const struct sockaddr *) &v4,
672 sizeof (v4));
673 if ( (rh->af == AF_UNSPEC) &&
674 (GNUNET_SYSERR != rh->was_transmitted) &&
675 (1 == inet_pton (AF_INET6,
676 hostname,
677 &v6.sin6_addr)) )
678 { 669 {
679 /* this can happen on some systems IF "hostname" is "localhost" */ 670 rh->addr_callback(rh->cls,
680 rh->addr_callback (rh->cls, 671 (const struct sockaddr *)&v4,
681 (const struct sockaddr *) &v6, 672 sizeof(v4));
682 sizeof (v6)); 673 if ((rh->af == AF_UNSPEC) &&
674 (GNUNET_SYSERR != rh->was_transmitted) &&
675 (1 == inet_pton(AF_INET6,
676 hostname,
677 &v6.sin6_addr)))
678 {
679 /* this can happen on some systems IF "hostname" is "localhost" */
680 rh->addr_callback(rh->cls,
681 (const struct sockaddr *)&v6,
682 sizeof(v6));
683 }
684 if (GNUNET_SYSERR != rh->was_transmitted)
685 rh->addr_callback(rh->cls,
686 NULL,
687 0);
688 GNUNET_free(rh);
689 return;
690 }
691 if (((rh->af == AF_UNSPEC) ||
692 (rh->af == AF_INET6)) &&
693 (1 == inet_pton(AF_INET6,
694 hostname,
695 &v6.sin6_addr)))
696 {
697 rh->addr_callback(rh->cls,
698 (const struct sockaddr *)&v6,
699 sizeof(v6));
700 if (GNUNET_SYSERR != rh->was_transmitted)
701 rh->addr_callback(rh->cls,
702 NULL,
703 0);
704 GNUNET_free(rh);
705 return;
683 } 706 }
684 if (GNUNET_SYSERR != rh->was_transmitted)
685 rh->addr_callback (rh->cls,
686 NULL,
687 0);
688 GNUNET_free (rh);
689 return;
690 }
691 if ( ( (rh->af == AF_UNSPEC) ||
692 (rh->af == AF_INET6) ) &&
693 (1 == inet_pton (AF_INET6,
694 hostname,
695 &v6.sin6_addr) ) )
696 {
697 rh->addr_callback (rh->cls,
698 (const struct sockaddr *) &v6,
699 sizeof (v6));
700 if (GNUNET_SYSERR != rh->was_transmitted)
701 rh->addr_callback (rh->cls,
702 NULL,
703 0);
704 GNUNET_free (rh);
705 return;
706 }
707 /* why are we here? this task should not have been scheduled! */ 707 /* why are we here? this task should not have been scheduled! */
708 GNUNET_assert (0); 708 GNUNET_assert(0);
709 GNUNET_free (rh); 709 GNUNET_free(rh);
710} 710}
711 711
712 712
@@ -718,58 +718,61 @@ numeric_resolution (void *cls)
718 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request 718 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
719 */ 719 */
720static void 720static void
721loopback_resolution (void *cls) 721loopback_resolution(void *cls)
722{ 722{
723 struct GNUNET_RESOLVER_RequestHandle *rh = cls; 723 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
724 struct sockaddr_in v4; 724 struct sockaddr_in v4;
725 struct sockaddr_in6 v6; 725 struct sockaddr_in6 v6;
726 726
727 rh->task = NULL; 727 rh->task = NULL;
728 memset (&v4, 0, sizeof (v4)); 728 memset(&v4, 0, sizeof(v4));
729 v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK); 729 v4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
730 v4.sin_family = AF_INET; 730 v4.sin_family = AF_INET;
731#if HAVE_SOCKADDR_IN_SIN_LEN 731#if HAVE_SOCKADDR_IN_SIN_LEN
732 v4.sin_len = sizeof (v4); 732 v4.sin_len = sizeof(v4);
733#endif 733#endif
734 memset (&v6, 0, sizeof (v6)); 734 memset(&v6, 0, sizeof(v6));
735 v6.sin6_family = AF_INET6; 735 v6.sin6_family = AF_INET6;
736#if HAVE_SOCKADDR_IN_SIN_LEN 736#if HAVE_SOCKADDR_IN_SIN_LEN
737 v6.sin6_len = sizeof (v6); 737 v6.sin6_len = sizeof(v6);
738#endif 738#endif
739 v6.sin6_addr = in6addr_loopback; 739 v6.sin6_addr = in6addr_loopback;
740 switch (rh->af) 740 switch (rh->af)
741 { 741 {
742 case AF_INET: 742 case AF_INET:
743 rh->addr_callback (rh->cls, 743 rh->addr_callback(rh->cls,
744 (const struct sockaddr *) &v4, 744 (const struct sockaddr *)&v4,
745 sizeof (v4)); 745 sizeof(v4));
746 break; 746 break;
747 case AF_INET6: 747
748 rh->addr_callback (rh->cls, 748 case AF_INET6:
749 (const struct sockaddr *) &v6, 749 rh->addr_callback(rh->cls,
750 sizeof (v6)); 750 (const struct sockaddr *)&v6,
751 break; 751 sizeof(v6));
752 case AF_UNSPEC: 752 break;
753 rh->addr_callback (rh->cls, 753
754 (const struct sockaddr *) &v6, 754 case AF_UNSPEC:
755 sizeof (v6)); 755 rh->addr_callback(rh->cls,
756 rh->addr_callback (rh->cls, 756 (const struct sockaddr *)&v6,
757 (const struct sockaddr *) &v4, 757 sizeof(v6));
758 sizeof (v4)); 758 rh->addr_callback(rh->cls,
759 759 (const struct sockaddr *)&v4,
760 break; 760 sizeof(v4));
761 default: 761
762 GNUNET_break (0); 762 break;
763 break; 763
764 } 764 default:
765 GNUNET_break(0);
766 break;
767 }
765 if (GNUNET_SYSERR != rh->was_transmitted) 768 if (GNUNET_SYSERR != rh->was_transmitted)
766 rh->addr_callback (rh->cls, 769 rh->addr_callback(rh->cls,
767 NULL, 770 NULL,
768 0); 771 0);
769 LOG (GNUNET_ERROR_TYPE_DEBUG, 772 LOG(GNUNET_ERROR_TYPE_DEBUG,
770 "Finished resolving hostname `%s'.\n", 773 "Finished resolving hostname `%s'.\n",
771 (const char *) &rh[1]); 774 (const char *)&rh[1]);
772 GNUNET_free (rh); 775 GNUNET_free(rh);
773} 776}
774 777
775 778
@@ -779,35 +782,35 @@ loopback_resolution (void *cls)
779 * @param cls NULL 782 * @param cls NULL
780 */ 783 */
781static void 784static void
782reconnect_task (void *cls) 785reconnect_task(void *cls)
783{ 786{
784 struct GNUNET_MQ_MessageHandler handlers[] = { 787 struct GNUNET_MQ_MessageHandler handlers[] = {
785 GNUNET_MQ_hd_var_size (response, 788 GNUNET_MQ_hd_var_size(response,
786 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE, 789 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE,
787 struct GNUNET_RESOLVER_ResponseMessage, 790 struct GNUNET_RESOLVER_ResponseMessage,
788 NULL), 791 NULL),
789 GNUNET_MQ_handler_end () 792 GNUNET_MQ_handler_end()
790 }; 793 };
791 794
792 (void) cls; 795 (void)cls;
793 r_task = NULL; 796 r_task = NULL;
794 if (NULL == req_head) 797 if (NULL == req_head)
795 return; /* no work pending */ 798 return; /* no work pending */
796 LOG (GNUNET_ERROR_TYPE_DEBUG, 799 LOG(GNUNET_ERROR_TYPE_DEBUG,
797 "Trying to connect to DNS service\n"); 800 "Trying to connect to DNS service\n");
798 mq = GNUNET_CLIENT_connect (resolver_cfg, 801 mq = GNUNET_CLIENT_connect(resolver_cfg,
799 "resolver", 802 "resolver",
800 handlers, 803 handlers,
801 &mq_error_handler, 804 &mq_error_handler,
802 NULL); 805 NULL);
803 if (NULL == mq) 806 if (NULL == mq)
804 { 807 {
805 LOG (GNUNET_ERROR_TYPE_DEBUG, 808 LOG(GNUNET_ERROR_TYPE_DEBUG,
806 "Failed to connect, will try again later\n"); 809 "Failed to connect, will try again later\n");
807 reconnect (); 810 reconnect();
808 return; 811 return;
809 } 812 }
810 process_requests (); 813 process_requests();
811} 814}
812 815
813 816
@@ -815,46 +818,49 @@ reconnect_task (void *cls)
815 * Adjust exponential back-off and reconnect to the service. 818 * Adjust exponential back-off and reconnect to the service.
816 */ 819 */
817static void 820static void
818reconnect () 821reconnect()
819{ 822{
820 struct GNUNET_RESOLVER_RequestHandle *rh; 823 struct GNUNET_RESOLVER_RequestHandle *rh;
821 824
822 if (NULL != r_task) 825 if (NULL != r_task)
823 return; 826 return;
824 GNUNET_assert (NULL == mq); 827 GNUNET_assert(NULL == mq);
825 if (NULL != (rh = req_head)) 828 if (NULL != (rh = req_head))
826 {
827 switch (rh->was_transmitted)
828 { 829 {
829 case GNUNET_NO: 830 switch (rh->was_transmitted)
830 /* nothing more to do */ 831 {
831 break; 832 case GNUNET_NO:
832 case GNUNET_YES: 833 /* nothing more to do */
833 /* disconnected, transmit again! */ 834 break;
834 rh->was_transmitted = GNUNET_NO; 835
835 break; 836 case GNUNET_YES:
836 case GNUNET_SYSERR: 837 /* disconnected, transmit again! */
837 /* request was cancelled, remove entirely */ 838 rh->was_transmitted = GNUNET_NO;
838 GNUNET_CONTAINER_DLL_remove (req_head, 839 break;
839 req_tail, 840
840 rh); 841 case GNUNET_SYSERR:
841 GNUNET_free (rh); 842 /* request was cancelled, remove entirely */
842 check_disconnect (); 843 GNUNET_CONTAINER_DLL_remove(req_head,
843 break; 844 req_tail,
844 default: 845 rh);
845 GNUNET_assert (0); 846 GNUNET_free(rh);
846 break; 847 check_disconnect();
848 break;
849
850 default:
851 GNUNET_assert(0);
852 break;
853 }
847 } 854 }
848 } 855 LOG(GNUNET_ERROR_TYPE_DEBUG,
849 LOG (GNUNET_ERROR_TYPE_DEBUG, 856 "Will try to connect to DNS service in %s\n",
850 "Will try to connect to DNS service in %s\n", 857 GNUNET_STRINGS_relative_time_to_string(backoff,
851 GNUNET_STRINGS_relative_time_to_string (backoff, 858 GNUNET_YES));
852 GNUNET_YES)); 859 GNUNET_assert(NULL != resolver_cfg);
853 GNUNET_assert (NULL != resolver_cfg); 860 r_task = GNUNET_SCHEDULER_add_delayed(backoff,
854 r_task = GNUNET_SCHEDULER_add_delayed (backoff, 861 &reconnect_task,
855 &reconnect_task, 862 NULL);
856 NULL); 863 backoff = GNUNET_TIME_STD_BACKOFF(backoff);
857 backoff = GNUNET_TIME_STD_BACKOFF (backoff);
858} 864}
859 865
860 866
@@ -864,52 +870,52 @@ reconnect ()
864 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *` 870 * @param cls the `struct GNUNET_RESOLVER_RequestHandle *`
865 */ 871 */
866static void 872static void
867handle_lookup_timeout (void *cls) 873handle_lookup_timeout(void *cls)
868{ 874{
869 struct GNUNET_RESOLVER_RequestHandle *rh = cls; 875 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
870 876
871 rh->task = NULL; 877 rh->task = NULL;
872 if (GNUNET_NO == rh->direction) 878 if (GNUNET_NO == rh->direction)
873 { 879 {
874 LOG (GNUNET_ERROR_TYPE_INFO, 880 LOG(GNUNET_ERROR_TYPE_INFO,
875 _("Timeout trying to resolve hostname `%s'.\n"), 881 _("Timeout trying to resolve hostname `%s'.\n"),
876 (const char *) &rh[1]); 882 (const char *)&rh[1]);
877 if (NULL != rh->addr_callback) 883 if (NULL != rh->addr_callback)
878 rh->addr_callback (rh->cls, 884 rh->addr_callback(rh->cls,
879 NULL, 885 NULL,
880 0); 886 0);
881 } 887 }
882 else 888 else
883 { 889 {
884#if !defined(GNUNET_CULL_LOGGING) 890#if !defined(GNUNET_CULL_LOGGING)
885 char buf[INET6_ADDRSTRLEN]; 891 char buf[INET6_ADDRSTRLEN];
886 892
887 LOG (GNUNET_ERROR_TYPE_INFO, 893 LOG(GNUNET_ERROR_TYPE_INFO,
888 _("Timeout trying to resolve IP address `%s'.\n"), 894 _("Timeout trying to resolve IP address `%s'.\n"),
889 inet_ntop (rh->af, 895 inet_ntop(rh->af,
890 (const void *) &rh[1], 896 (const void *)&rh[1],
891 buf, 897 buf,
892 sizeof(buf))); 898 sizeof(buf)));
893#endif 899#endif
894 if (GNUNET_NO == rh->received_response) 900 if (GNUNET_NO == rh->received_response)
895 { 901 {
896 char *nret; 902 char *nret;
897 903
898 nret = no_resolve (rh->af, 904 nret = no_resolve(rh->af,
899 &rh[1], 905 &rh[1],
900 rh->data_len); 906 rh->data_len);
907 if (NULL != rh->name_callback)
908 rh->name_callback(rh->cls, nret);
909 GNUNET_free(nret);
910 }
911 /* finally, make termination call */
901 if (NULL != rh->name_callback) 912 if (NULL != rh->name_callback)
902 rh->name_callback (rh->cls, nret); 913 rh->name_callback(rh->cls,
903 GNUNET_free (nret); 914 NULL);
904 } 915 }
905 /* finally, make termination call */
906 if (NULL != rh->name_callback)
907 rh->name_callback (rh->cls,
908 NULL);
909 }
910 rh->was_transmitted = GNUNET_NO; 916 rh->was_transmitted = GNUNET_NO;
911 GNUNET_RESOLVER_request_cancel (rh); 917 GNUNET_RESOLVER_request_cancel(rh);
912 process_requests (); 918 process_requests();
913} 919}
914 920
915 921
@@ -924,83 +930,83 @@ handle_lookup_timeout (void *cls)
924 * @return handle that can be used to cancel the request, NULL on error 930 * @return handle that can be used to cancel the request, NULL on error
925 */ 931 */
926struct GNUNET_RESOLVER_RequestHandle * 932struct GNUNET_RESOLVER_RequestHandle *
927GNUNET_RESOLVER_ip_get (const char *hostname, 933GNUNET_RESOLVER_ip_get(const char *hostname,
928 int af, 934 int af,
929 struct GNUNET_TIME_Relative timeout, 935 struct GNUNET_TIME_Relative timeout,
930 GNUNET_RESOLVER_AddressCallback callback, 936 GNUNET_RESOLVER_AddressCallback callback,
931 void *callback_cls) 937 void *callback_cls)
932{ 938{
933 struct GNUNET_RESOLVER_RequestHandle *rh; 939 struct GNUNET_RESOLVER_RequestHandle *rh;
934 size_t slen; 940 size_t slen;
935 struct in_addr v4; 941 struct in_addr v4;
936 struct in6_addr v6; 942 struct in6_addr v6;
937 943
938 slen = strlen (hostname) + 1; 944 slen = strlen(hostname) + 1;
939 if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >= 945 if (slen + sizeof(struct GNUNET_RESOLVER_GetMessage) >=
940 GNUNET_MAX_MESSAGE_SIZE) 946 GNUNET_MAX_MESSAGE_SIZE)
941 { 947 {
942 GNUNET_break (0); 948 GNUNET_break(0);
943 return NULL; 949 return NULL;
944 } 950 }
945 LOG (GNUNET_ERROR_TYPE_DEBUG, 951 LOG(GNUNET_ERROR_TYPE_DEBUG,
946 "Trying to resolve hostname `%s'.\n", 952 "Trying to resolve hostname `%s'.\n",
947 hostname); 953 hostname);
948 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + slen); 954 rh = GNUNET_malloc(sizeof(struct GNUNET_RESOLVER_RequestHandle) + slen);
949 rh->af = af; 955 rh->af = af;
950 rh->id = ++last_request_id; 956 rh->id = ++last_request_id;
951 rh->addr_callback = callback; 957 rh->addr_callback = callback;
952 rh->cls = callback_cls; 958 rh->cls = callback_cls;
953 GNUNET_memcpy (&rh[1], 959 GNUNET_memcpy(&rh[1],
954 hostname, 960 hostname,
955 slen); 961 slen);
956 rh->data_len = slen; 962 rh->data_len = slen;
957 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout); 963 rh->timeout = GNUNET_TIME_relative_to_absolute(timeout);
958 rh->direction = GNUNET_NO; 964 rh->direction = GNUNET_NO;
959 /* first, check if this is a numeric address */ 965 /* first, check if this is a numeric address */
960 if ( ( (1 == inet_pton (AF_INET, 966 if (((1 == inet_pton(AF_INET,
961 hostname, 967 hostname,
962 &v4)) && 968 &v4)) &&
963 ( (af == AF_INET) || 969 ((af == AF_INET) ||
964 (af == AF_UNSPEC) ) ) || 970 (af == AF_UNSPEC))) ||
965 ( (1 == inet_pton (AF_INET6, 971 ((1 == inet_pton(AF_INET6,
966 hostname, 972 hostname,
967 &v6)) && 973 &v6)) &&
968 ( (af == AF_INET6) || 974 ((af == AF_INET6) ||
969 (af == AF_UNSPEC)) ) ) 975 (af == AF_UNSPEC))))
970 { 976 {
971 rh->task = GNUNET_SCHEDULER_add_now (&numeric_resolution, 977 rh->task = GNUNET_SCHEDULER_add_now(&numeric_resolution,
972 rh); 978 rh);
973 return rh; 979 return rh;
974 } 980 }
975 /* then, check if this is a loopback address */ 981 /* then, check if this is a loopback address */
976 for (unsigned int i = 0; 982 for (unsigned int i = 0;
977 NULL != loopback[i]; 983 NULL != loopback[i];
978 i++) 984 i++)
979 if (0 == strcasecmp (loopback[i], 985 if (0 == strcasecmp(loopback[i],
980 hostname)) 986 hostname))
987 {
988 rh->task = GNUNET_SCHEDULER_add_now(&loopback_resolution,
989 rh);
990 return rh;
991 }
992 if (GNUNET_OK != check_config())
981 { 993 {
982 rh->task = GNUNET_SCHEDULER_add_now (&loopback_resolution, 994 GNUNET_free(rh);
983 rh); 995 return NULL;
984 return rh;
985 } 996 }
986 if (GNUNET_OK != check_config ()) 997 rh->task = GNUNET_SCHEDULER_add_delayed(timeout,
987 { 998 &handle_lookup_timeout,
988 GNUNET_free (rh); 999 rh);
989 return NULL; 1000 GNUNET_CONTAINER_DLL_insert_tail(req_head,
990 } 1001 req_tail,
991 rh->task = GNUNET_SCHEDULER_add_delayed (timeout, 1002 rh);
992 &handle_lookup_timeout,
993 rh);
994 GNUNET_CONTAINER_DLL_insert_tail (req_head,
995 req_tail,
996 rh);
997 rh->was_queued = GNUNET_YES; 1003 rh->was_queued = GNUNET_YES;
998 if (NULL != s_task) 1004 if (NULL != s_task)
999 { 1005 {
1000 GNUNET_SCHEDULER_cancel (s_task); 1006 GNUNET_SCHEDULER_cancel(s_task);
1001 s_task = NULL; 1007 s_task = NULL;
1002 } 1008 }
1003 process_requests (); 1009 process_requests();
1004 return rh; 1010 return rh;
1005} 1011}
1006 1012
@@ -1014,32 +1020,32 @@ GNUNET_RESOLVER_ip_get (const char *hostname,
1014 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request 1020 * @param cls `struct GNUNET_RESOLVER_RequestHandle` for the request
1015 */ 1021 */
1016static void 1022static void
1017numeric_reverse (void *cls) 1023numeric_reverse(void *cls)
1018{ 1024{
1019 struct GNUNET_RESOLVER_RequestHandle *rh = cls; 1025 struct GNUNET_RESOLVER_RequestHandle *rh = cls;
1020 char *result; 1026 char *result;
1021 1027
1022 rh->task = NULL; 1028 rh->task = NULL;
1023 result = no_resolve (rh->af, 1029 result = no_resolve(rh->af,
1024 &rh[1], 1030 &rh[1],
1025 rh->data_len); 1031 rh->data_len);
1026 LOG (GNUNET_ERROR_TYPE_DEBUG, 1032 LOG(GNUNET_ERROR_TYPE_DEBUG,
1027 "Resolver returns `%s'.\n", 1033 "Resolver returns `%s'.\n",
1028 result); 1034 result);
1029 if (NULL != result) 1035 if (NULL != result)
1030 { 1036 {
1031 rh->name_callback (rh->cls, 1037 rh->name_callback(rh->cls,
1032 result); 1038 result);
1033 GNUNET_free (result); 1039 GNUNET_free(result);
1034 } 1040 }
1035 rh->name_callback (rh->cls, 1041 rh->name_callback(rh->cls,
1036 NULL); 1042 NULL);
1037 if (NULL != rh->task) 1043 if (NULL != rh->task)
1038 { 1044 {
1039 GNUNET_SCHEDULER_cancel (rh->task); 1045 GNUNET_SCHEDULER_cancel(rh->task);
1040 rh->task = NULL; 1046 rh->task = NULL;
1041 } 1047 }
1042 GNUNET_free (rh); 1048 GNUNET_free(rh);
1043} 1049}
1044 1050
1045 1051
@@ -1056,71 +1062,73 @@ numeric_reverse (void *cls)
1056 * @return handle that can be used to cancel the request 1062 * @return handle that can be used to cancel the request
1057 */ 1063 */
1058struct GNUNET_RESOLVER_RequestHandle * 1064struct GNUNET_RESOLVER_RequestHandle *
1059GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa, 1065GNUNET_RESOLVER_hostname_get(const struct sockaddr *sa,
1060 socklen_t salen, 1066 socklen_t salen,
1061 int do_resolve, 1067 int do_resolve,
1062 struct GNUNET_TIME_Relative timeout, 1068 struct GNUNET_TIME_Relative timeout,
1063 GNUNET_RESOLVER_HostnameCallback callback, 1069 GNUNET_RESOLVER_HostnameCallback callback,
1064 void *cls) 1070 void *cls)
1065{ 1071{
1066 struct GNUNET_RESOLVER_RequestHandle *rh; 1072 struct GNUNET_RESOLVER_RequestHandle *rh;
1067 size_t ip_len; 1073 size_t ip_len;
1068 const void *ip; 1074 const void *ip;
1069 1075
1070 if (GNUNET_OK != check_config ()) 1076 if (GNUNET_OK != check_config())
1071 { 1077 {
1072 LOG (GNUNET_ERROR_TYPE_ERROR, 1078 LOG(GNUNET_ERROR_TYPE_ERROR,
1073 _("Resolver not configured correctly.\n")); 1079 _("Resolver not configured correctly.\n"));
1074 return NULL; 1080 return NULL;
1075 } 1081 }
1076 1082
1077 switch (sa->sa_family) 1083 switch (sa->sa_family)
1078 { 1084 {
1079 case AF_INET: 1085 case AF_INET:
1080 GNUNET_assert (salen == sizeof (struct sockaddr_in)); 1086 GNUNET_assert(salen == sizeof(struct sockaddr_in));
1081 ip_len = sizeof (struct in_addr); 1087 ip_len = sizeof(struct in_addr);
1082 ip = &((const struct sockaddr_in*)sa)->sin_addr; 1088 ip = &((const struct sockaddr_in*)sa)->sin_addr;
1083 break; 1089 break;
1084 case AF_INET6: 1090
1085 GNUNET_assert (salen == sizeof (struct sockaddr_in6)); 1091 case AF_INET6:
1086 ip_len = sizeof (struct in6_addr); 1092 GNUNET_assert(salen == sizeof(struct sockaddr_in6));
1087 ip = &((const struct sockaddr_in6*)sa)->sin6_addr; 1093 ip_len = sizeof(struct in6_addr);
1088 break; 1094 ip = &((const struct sockaddr_in6*)sa)->sin6_addr;
1089 default: 1095 break;
1090 GNUNET_break (0); 1096
1091 return NULL; 1097 default:
1092 } 1098 GNUNET_break(0);
1093 rh = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_RequestHandle) + salen); 1099 return NULL;
1100 }
1101 rh = GNUNET_malloc(sizeof(struct GNUNET_RESOLVER_RequestHandle) + salen);
1094 rh->name_callback = callback; 1102 rh->name_callback = callback;
1095 rh->cls = cls; 1103 rh->cls = cls;
1096 rh->af = sa->sa_family; 1104 rh->af = sa->sa_family;
1097 rh->id = ++last_request_id; 1105 rh->id = ++last_request_id;
1098 rh->timeout = GNUNET_TIME_relative_to_absolute (timeout); 1106 rh->timeout = GNUNET_TIME_relative_to_absolute(timeout);
1099 GNUNET_memcpy (&rh[1], 1107 GNUNET_memcpy(&rh[1],
1100 ip, 1108 ip,
1101 ip_len); 1109 ip_len);
1102 rh->data_len = ip_len; 1110 rh->data_len = ip_len;
1103 rh->direction = GNUNET_YES; 1111 rh->direction = GNUNET_YES;
1104 rh->received_response = GNUNET_NO; 1112 rh->received_response = GNUNET_NO;
1105 if (GNUNET_NO == do_resolve) 1113 if (GNUNET_NO == do_resolve)
1106 { 1114 {
1107 rh->task = GNUNET_SCHEDULER_add_now (&numeric_reverse, 1115 rh->task = GNUNET_SCHEDULER_add_now(&numeric_reverse,
1108 rh); 1116 rh);
1109 return rh; 1117 return rh;
1110 } 1118 }
1111 rh->task = GNUNET_SCHEDULER_add_delayed (timeout, 1119 rh->task = GNUNET_SCHEDULER_add_delayed(timeout,
1112 &handle_lookup_timeout, 1120 &handle_lookup_timeout,
1113 rh); 1121 rh);
1114 GNUNET_CONTAINER_DLL_insert_tail (req_head, 1122 GNUNET_CONTAINER_DLL_insert_tail(req_head,
1115 req_tail, 1123 req_tail,
1116 rh); 1124 rh);
1117 rh->was_queued = GNUNET_YES; 1125 rh->was_queued = GNUNET_YES;
1118 if (NULL != s_task) 1126 if (NULL != s_task)
1119 { 1127 {
1120 GNUNET_SCHEDULER_cancel (s_task); 1128 GNUNET_SCHEDULER_cancel(s_task);
1121 s_task = NULL; 1129 s_task = NULL;
1122 } 1130 }
1123 process_requests (); 1131 process_requests();
1124 return rh; 1132 return rh;
1125} 1133}
1126 1134
@@ -1131,78 +1139,78 @@ GNUNET_RESOLVER_hostname_get (const struct sockaddr *sa,
1131 * @return fqdn 1139 * @return fqdn
1132 */ 1140 */
1133char * 1141char *
1134GNUNET_RESOLVER_local_fqdn_get () 1142GNUNET_RESOLVER_local_fqdn_get()
1135{ 1143{
1136 char hostname[GNUNET_OS_get_hostname_max_length () + 1]; 1144 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
1137 1145
1138 if (0 != gethostname (hostname, 1146 if (0 != gethostname(hostname,
1139 sizeof (hostname) - 1)) 1147 sizeof(hostname) - 1))
1140 { 1148 {
1141 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 1149 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1142 "gethostname"); 1150 "gethostname");
1143 return NULL; 1151 return NULL;
1144 } 1152 }
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, 1153 LOG(GNUNET_ERROR_TYPE_DEBUG,
1146 "Resolving our FQDN `%s'\n", 1154 "Resolving our FQDN `%s'\n",
1147 hostname); 1155 hostname);
1148#if HAVE_GETADDRINFO 1156#if HAVE_GETADDRINFO
1149 { 1157 {
1150 struct addrinfo *ai; 1158 struct addrinfo *ai;
1151 int ret; 1159 int ret;
1152 char *rval; 1160 char *rval;
1153 1161
1154 if (0 != (ret = getaddrinfo (hostname, 1162 if (0 != (ret = getaddrinfo(hostname,
1155 NULL, 1163 NULL,
1156 NULL, 1164 NULL,
1157 &ai))) 1165 &ai)))
1158 { 1166 {
1159 LOG (GNUNET_ERROR_TYPE_ERROR, 1167 LOG(GNUNET_ERROR_TYPE_ERROR,
1160 _("Could not resolve our FQDN: %s\n"), 1168 _("Could not resolve our FQDN: %s\n"),
1161 gai_strerror (ret)); 1169 gai_strerror(ret));
1162 return NULL; 1170 return NULL;
1163 } 1171 }
1164 if (NULL != ai->ai_canonname) 1172 if (NULL != ai->ai_canonname)
1165 rval = GNUNET_strdup (ai->ai_canonname); 1173 rval = GNUNET_strdup(ai->ai_canonname);
1166 else 1174 else
1167 rval = GNUNET_strdup (hostname); 1175 rval = GNUNET_strdup(hostname);
1168 freeaddrinfo (ai); 1176 freeaddrinfo(ai);
1169 return rval; 1177 return rval;
1170 } 1178 }
1171#elif HAVE_GETHOSTBYNAME2 1179#elif HAVE_GETHOSTBYNAME2
1172 { 1180 {
1173 struct hostent *host; 1181 struct hostent *host;
1174 1182
1175 host = gethostbyname2 (hostname, 1183 host = gethostbyname2(hostname,
1176 AF_INET); 1184 AF_INET);
1177 if (NULL == host) 1185 if (NULL == host)
1178 host = gethostbyname2 (hostname, 1186 host = gethostbyname2(hostname,
1179 AF_INET6); 1187 AF_INET6);
1180 if (NULL == host) 1188 if (NULL == host)
1181 { 1189 {
1182 LOG (GNUNET_ERROR_TYPE_ERROR, 1190 LOG(GNUNET_ERROR_TYPE_ERROR,
1183 _("Could not resolve our FQDN: %s\n"), 1191 _("Could not resolve our FQDN: %s\n"),
1184 hstrerror (h_errno)); 1192 hstrerror(h_errno));
1185 return NULL; 1193 return NULL;
1186 } 1194 }
1187 return GNUNET_strdup (host->h_name); 1195 return GNUNET_strdup(host->h_name);
1188 } 1196 }
1189#elif HAVE_GETHOSTBYNAME 1197#elif HAVE_GETHOSTBYNAME
1190 { 1198 {
1191 struct hostent *host; 1199 struct hostent *host;
1192 1200
1193 host = gethostbyname (hostname); 1201 host = gethostbyname(hostname);
1194 if (NULL == host) 1202 if (NULL == host)
1195 { 1203 {
1196 LOG (GNUNET_ERROR_TYPE_ERROR, 1204 LOG(GNUNET_ERROR_TYPE_ERROR,
1197 _("Could not resolve our FQDN: %s\n"), 1205 _("Could not resolve our FQDN: %s\n"),
1198 hstrerror (h_errno)); 1206 hstrerror(h_errno));
1199 return NULL; 1207 return NULL;
1200 } 1208 }
1201 return GNUNET_strdup (host->h_name); 1209 return GNUNET_strdup(host->h_name);
1202 } 1210 }
1203#else 1211#else
1204 /* fallback: just hope name is already FQDN */ 1212 /* fallback: just hope name is already FQDN */
1205 return GNUNET_strdup (hostname); 1213 return GNUNET_strdup(hostname);
1206#endif 1214#endif
1207} 1215}
1208 1216
@@ -1217,27 +1225,27 @@ GNUNET_RESOLVER_local_fqdn_get ()
1217 * @return handle that can be used to cancel the request, NULL on error 1225 * @return handle that can be used to cancel the request, NULL on error
1218 */ 1226 */
1219struct GNUNET_RESOLVER_RequestHandle * 1227struct GNUNET_RESOLVER_RequestHandle *
1220GNUNET_RESOLVER_hostname_resolve (int af, 1228GNUNET_RESOLVER_hostname_resolve(int af,
1221 struct GNUNET_TIME_Relative timeout, 1229 struct GNUNET_TIME_Relative timeout,
1222 GNUNET_RESOLVER_AddressCallback callback, 1230 GNUNET_RESOLVER_AddressCallback callback,
1223 void *cls) 1231 void *cls)
1224{ 1232{
1225 char hostname[GNUNET_OS_get_hostname_max_length () + 1]; 1233 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
1226 1234
1227 if (0 != gethostname (hostname, sizeof (hostname) - 1)) 1235 if (0 != gethostname(hostname, sizeof(hostname) - 1))
1228 { 1236 {
1229 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 1237 LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1230 "gethostname"); 1238 "gethostname");
1231 return NULL; 1239 return NULL;
1232 } 1240 }
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, 1241 LOG(GNUNET_ERROR_TYPE_DEBUG,
1234 "Resolving our hostname `%s'\n", 1242 "Resolving our hostname `%s'\n",
1235 hostname); 1243 hostname);
1236 return GNUNET_RESOLVER_ip_get (hostname, 1244 return GNUNET_RESOLVER_ip_get(hostname,
1237 af, 1245 af,
1238 timeout, 1246 timeout,
1239 callback, 1247 callback,
1240 cls); 1248 cls);
1241} 1249}
1242 1250
1243 1251
@@ -1250,30 +1258,30 @@ GNUNET_RESOLVER_hostname_resolve (int af,
1250 * @param rh handle of request to cancel 1258 * @param rh handle of request to cancel
1251 */ 1259 */
1252void 1260void
1253GNUNET_RESOLVER_request_cancel (struct GNUNET_RESOLVER_RequestHandle *rh) 1261GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
1254{ 1262{
1255 if (GNUNET_NO == rh->direction) 1263 if (GNUNET_NO == rh->direction)
1256 LOG (GNUNET_ERROR_TYPE_DEBUG, 1264 LOG(GNUNET_ERROR_TYPE_DEBUG,
1257 "Asked to cancel request to resolve hostname `%s'.\n", 1265 "Asked to cancel request to resolve hostname `%s'.\n",
1258 (const char *) &rh[1]); 1266 (const char *)&rh[1]);
1259 if (NULL != rh->task) 1267 if (NULL != rh->task)
1260 { 1268 {
1261 GNUNET_SCHEDULER_cancel (rh->task); 1269 GNUNET_SCHEDULER_cancel(rh->task);
1262 rh->task = NULL; 1270 rh->task = NULL;
1263 } 1271 }
1264 if (GNUNET_NO == rh->was_transmitted) 1272 if (GNUNET_NO == rh->was_transmitted)
1265 { 1273 {
1266 if (GNUNET_YES == rh->was_queued) 1274 if (GNUNET_YES == rh->was_queued)
1267 GNUNET_CONTAINER_DLL_remove (req_head, 1275 GNUNET_CONTAINER_DLL_remove(req_head,
1268 req_tail, 1276 req_tail,
1269 rh); 1277 rh);
1270 GNUNET_free (rh); 1278 GNUNET_free(rh);
1271 check_disconnect (); 1279 check_disconnect();
1272 return; 1280 return;
1273 } 1281 }
1274 GNUNET_assert (GNUNET_YES == rh->was_transmitted); 1282 GNUNET_assert(GNUNET_YES == rh->was_transmitted);
1275 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */ 1283 rh->was_transmitted = GNUNET_SYSERR; /* mark as cancelled */
1276 check_disconnect (); 1284 check_disconnect();
1277} 1285}
1278 1286
1279 1287