diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-06-04 11:37:37 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-06-04 11:38:24 +0200 |
commit | b3b3ef19da629d3f5e1e2806b0e02e57c4589c10 (patch) | |
tree | 2ba5630cca228cb7401c2b6aca9402f6cad43864 /src/util/gnunet-service-resolver.c | |
parent | c18bc6d46612fe7c642abec2e6503e3c79a5f45b (diff) | |
download | gnunet-b3b3ef19da629d3f5e1e2806b0e02e57c4589c10.tar.gz gnunet-b3b3ef19da629d3f5e1e2806b0e02e57c4589c10.zip |
fix cast
Diffstat (limited to 'src/util/gnunet-service-resolver.c')
-rw-r--r-- | src/util/gnunet-service-resolver.c | 603 |
1 files changed, 223 insertions, 380 deletions
diff --git a/src/util/gnunet-service-resolver.c b/src/util/gnunet-service-resolver.c index 336e35f94..29ecf3e89 100644 --- a/src/util/gnunet-service-resolver.c +++ b/src/util/gnunet-service-resolver.c | |||
@@ -59,7 +59,6 @@ struct RecordListEntry | |||
59 | * Cached data. | 59 | * Cached data. |
60 | */ | 60 | */ |
61 | struct GNUNET_DNSPARSER_Record *record; | 61 | struct GNUNET_DNSPARSER_Record *record; |
62 | |||
63 | }; | 62 | }; |
64 | 63 | ||
65 | 64 | ||
@@ -92,7 +91,6 @@ struct ResolveCache | |||
92 | * tail of a double linked list containing the lookup results | 91 | * tail of a double linked list containing the lookup results |
93 | */ | 92 | */ |
94 | struct RecordListEntry *records_tail; | 93 | struct RecordListEntry *records_tail; |
95 | |||
96 | }; | 94 | }; |
97 | 95 | ||
98 | 96 | ||
@@ -153,7 +151,6 @@ struct ActiveLookup | |||
153 | * is currently pending, undefined otherwise. | 151 | * is currently pending, undefined otherwise. |
154 | */ | 152 | */ |
155 | uint16_t dns_id; | 153 | uint16_t dns_id; |
156 | |||
157 | }; | 154 | }; |
158 | 155 | ||
159 | 156 | ||
@@ -215,17 +212,13 @@ free_cache_entry (struct ResolveCache *rc) | |||
215 | 212 | ||
216 | while (NULL != (pos = rc->records_head)) | 213 | while (NULL != (pos = rc->records_head)) |
217 | { | 214 | { |
218 | GNUNET_CONTAINER_DLL_remove (rc->records_head, | 215 | GNUNET_CONTAINER_DLL_remove (rc->records_head, rc->records_tail, pos); |
219 | rc->records_tail, | ||
220 | pos); | ||
221 | GNUNET_DNSPARSER_free_record (pos->record); | 216 | GNUNET_DNSPARSER_free_record (pos->record); |
222 | GNUNET_free (pos->record); | 217 | GNUNET_free (pos->record); |
223 | GNUNET_free (pos); | 218 | GNUNET_free (pos); |
224 | } | 219 | } |
225 | GNUNET_free_non_null (rc->hostname); | 220 | GNUNET_free_non_null (rc->hostname); |
226 | GNUNET_CONTAINER_DLL_remove (cache_head, | 221 | GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, rc); |
227 | cache_tail, | ||
228 | rc); | ||
229 | cache_size--; | 222 | cache_size--; |
230 | GNUNET_free (rc); | 223 | GNUNET_free (rc); |
231 | } | 224 | } |
@@ -243,17 +236,13 @@ free_hosts_entry (struct ResolveCache *rc) | |||
243 | 236 | ||
244 | while (NULL != (pos = rc->records_head)) | 237 | while (NULL != (pos = rc->records_head)) |
245 | { | 238 | { |
246 | GNUNET_CONTAINER_DLL_remove (rc->records_head, | 239 | GNUNET_CONTAINER_DLL_remove (rc->records_head, rc->records_tail, pos); |
247 | rc->records_tail, | ||
248 | pos); | ||
249 | GNUNET_DNSPARSER_free_record (pos->record); | 240 | GNUNET_DNSPARSER_free_record (pos->record); |
250 | GNUNET_free (pos->record); | 241 | GNUNET_free (pos->record); |
251 | GNUNET_free (pos); | 242 | GNUNET_free (pos); |
252 | } | 243 | } |
253 | GNUNET_free_non_null (rc->hostname); | 244 | GNUNET_free_non_null (rc->hostname); |
254 | GNUNET_CONTAINER_DLL_remove (hosts_head, | 245 | GNUNET_CONTAINER_DLL_remove (hosts_head, hosts_tail, rc); |
255 | hosts_tail, | ||
256 | rc); | ||
257 | cache_size--; | 246 | cache_size--; |
258 | GNUNET_free (rc); | 247 | GNUNET_free (rc); |
259 | } | 248 | } |
@@ -267,9 +256,7 @@ free_hosts_entry (struct ResolveCache *rc) | |||
267 | static void | 256 | static void |
268 | free_active_lookup (struct ActiveLookup *al) | 257 | free_active_lookup (struct ActiveLookup *al) |
269 | { | 258 | { |
270 | GNUNET_CONTAINER_DLL_remove (lookup_head, | 259 | GNUNET_CONTAINER_DLL_remove (lookup_head, lookup_tail, al); |
271 | lookup_tail, | ||
272 | al); | ||
273 | if (NULL != al->resolve_handle) | 260 | if (NULL != al->resolve_handle) |
274 | { | 261 | { |
275 | GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle); | 262 | GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle); |
@@ -285,7 +272,6 @@ free_active_lookup (struct ActiveLookup *al) | |||
285 | } | 272 | } |
286 | 273 | ||
287 | 274 | ||
288 | |||
289 | /** | 275 | /** |
290 | * Find out if the configuration file line contains a string | 276 | * Find out if the configuration file line contains a string |
291 | * starting with "nameserver ", and if so, return a copy of | 277 | * starting with "nameserver ", and if so, return a copy of |
@@ -296,12 +282,9 @@ free_active_lookup (struct ActiveLookup *al) | |||
296 | * @return NULL if no nameserver is configured in this @a line | 282 | * @return NULL if no nameserver is configured in this @a line |
297 | */ | 283 | */ |
298 | static char * | 284 | static char * |
299 | extract_dns_server (const char* line, | 285 | extract_dns_server (const char *line, size_t line_len) |
300 | size_t line_len) | ||
301 | { | 286 | { |
302 | if (0 == strncmp (line, | 287 | if (0 == strncmp (line, "nameserver ", strlen ("nameserver "))) |
303 | "nameserver ", | ||
304 | strlen ("nameserver "))) | ||
305 | return GNUNET_strndup (line + strlen ("nameserver "), | 288 | return GNUNET_strndup (line + strlen ("nameserver "), |
306 | line_len - strlen ("nameserver ")); | 289 | line_len - strlen ("nameserver ")); |
307 | return NULL; | 290 | return NULL; |
@@ -318,12 +301,9 @@ extract_dns_server (const char* line, | |||
318 | * @return NULL if no nameserver is configured in this @a line | 301 | * @return NULL if no nameserver is configured in this @a line |
319 | */ | 302 | */ |
320 | static char * | 303 | static char * |
321 | extract_search_domain (const char* line, | 304 | extract_search_domain (const char *line, size_t line_len) |
322 | size_t line_len) | ||
323 | { | 305 | { |
324 | if (0 == strncmp (line, | 306 | if (0 == strncmp (line, "search ", strlen ("search "))) |
325 | "search ", | ||
326 | strlen ("search "))) | ||
327 | return GNUNET_strndup (line + strlen ("search "), | 307 | return GNUNET_strndup (line + strlen ("search "), |
328 | line_len - strlen ("search ")); | 308 | line_len - strlen ("search ")); |
329 | return NULL; | 309 | return NULL; |
@@ -347,37 +327,35 @@ lookup_dns_servers (char ***server_addrs) | |||
347 | unsigned int num_dns_servers; | 327 | unsigned int num_dns_servers; |
348 | 328 | ||
349 | fh = GNUNET_DISK_file_open ("/etc/resolv.conf", | 329 | fh = GNUNET_DISK_file_open ("/etc/resolv.conf", |
350 | GNUNET_DISK_OPEN_READ, | 330 | GNUNET_DISK_OPEN_READ, |
351 | GNUNET_DISK_PERM_NONE); | 331 | GNUNET_DISK_PERM_NONE); |
352 | if (NULL == fh) | 332 | if (NULL == fh) |
353 | { | 333 | { |
354 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 334 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
355 | "Could not open /etc/resolv.conf. " | 335 | "Could not open /etc/resolv.conf. " |
356 | "DNS resolution will not be possible.\n"); | 336 | "DNS resolution will not be possible.\n"); |
357 | return -1; | 337 | return -1; |
358 | } | 338 | } |
359 | if (GNUNET_OK != | 339 | if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read)) |
360 | GNUNET_DISK_file_handle_size (fh, | ||
361 | &bytes_read)) | ||
362 | { | 340 | { |
363 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 341 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
364 | "Could not determine size of /etc/resolv.conf. " | 342 | "Could not determine size of /etc/resolv.conf. " |
365 | "DNS resolution will not be possible.\n"); | 343 | "DNS resolution will not be possible.\n"); |
366 | GNUNET_DISK_file_close (fh); | 344 | GNUNET_DISK_file_close (fh); |
367 | return -1; | 345 | return -1; |
368 | } | 346 | } |
369 | if ((size_t) bytes_read > SIZE_MAX) | 347 | if (bytes_read > (off_t) SIZE_MAX) |
370 | { | 348 | { |
371 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 349 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
372 | "/etc/resolv.conf file too large to mmap. " | 350 | "/etc/resolv.conf file too large to mmap. " |
373 | "DNS resolution will not be possible.\n"); | 351 | "DNS resolution will not be possible.\n"); |
374 | GNUNET_DISK_file_close (fh); | 352 | GNUNET_DISK_file_close (fh); |
375 | return -1; | 353 | return -1; |
376 | } | 354 | } |
377 | buf = GNUNET_DISK_file_map (fh, | 355 | buf = GNUNET_DISK_file_map (fh, |
378 | &mh, | 356 | &mh, |
379 | GNUNET_DISK_MAP_TYPE_READ, | 357 | GNUNET_DISK_MAP_TYPE_READ, |
380 | (size_t) bytes_read); | 358 | (size_t) bytes_read); |
381 | *server_addrs = NULL; | 359 | *server_addrs = NULL; |
382 | read_offset = 0; | 360 | read_offset = 0; |
383 | num_dns_servers = 0; | 361 | num_dns_servers = 0; |
@@ -387,23 +365,18 @@ lookup_dns_servers (char ***server_addrs) | |||
387 | size_t line_len; | 365 | size_t line_len; |
388 | char *dns_server; | 366 | char *dns_server; |
389 | 367 | ||
390 | newline = strchr (buf + read_offset, | 368 | newline = strchr (buf + read_offset, '\n'); |
391 | '\n'); | ||
392 | if (NULL == newline) | 369 | if (NULL == newline) |
393 | break; | 370 | break; |
394 | line_len = newline - buf - read_offset; | 371 | line_len = newline - buf - read_offset; |
395 | dns_server = extract_dns_server (buf + read_offset, | 372 | dns_server = extract_dns_server (buf + read_offset, line_len); |
396 | line_len); | ||
397 | if (NULL != dns_server) | 373 | if (NULL != dns_server) |
398 | { | 374 | { |
399 | GNUNET_array_append (*server_addrs, | 375 | GNUNET_array_append (*server_addrs, num_dns_servers, dns_server); |
400 | num_dns_servers, | ||
401 | dns_server); | ||
402 | } | 376 | } |
403 | else if (NULL == my_domain) | 377 | else if (NULL == my_domain) |
404 | { | 378 | { |
405 | my_domain = extract_search_domain (buf + read_offset, | 379 | my_domain = extract_search_domain (buf + read_offset, line_len); |
406 | line_len); | ||
407 | } | 380 | } |
408 | read_offset += line_len + 1; | 381 | read_offset += line_len + 1; |
409 | } | 382 | } |
@@ -420,64 +393,51 @@ lookup_dns_servers (char ***server_addrs) | |||
420 | * @param af address family of @a ip, AF_INET or AF_INET6 | 393 | * @param af address family of @a ip, AF_INET or AF_INET6 |
421 | */ | 394 | */ |
422 | static char * | 395 | static char * |
423 | make_reverse_hostname (const void *ip, | 396 | make_reverse_hostname (const void *ip, int af) |
424 | int af) | ||
425 | { | 397 | { |
426 | char *buf = GNUNET_new_array (80, | 398 | char *buf = GNUNET_new_array (80, char); |
427 | char); | ||
428 | int pos = 0; | 399 | int pos = 0; |
429 | 400 | ||
430 | if (AF_INET == af) | 401 | if (AF_INET == af) |
431 | { | 402 | { |
432 | struct in_addr *addr = (struct in_addr *)ip; | 403 | struct in_addr *addr = (struct in_addr *) ip; |
433 | uint32_t ip_int = addr->s_addr; | 404 | uint32_t ip_int = addr->s_addr; |
434 | 405 | ||
435 | for (int i = 3; i >= 0; i--) | 406 | for (int i = 3; i >= 0; i--) |
436 | { | 407 | { |
437 | int n = GNUNET_snprintf (buf + pos, | 408 | int n = |
438 | 80 - pos, | 409 | GNUNET_snprintf (buf + pos, 80 - pos, "%u.", ((uint8_t *) &ip_int)[i]); |
439 | "%u.", | ||
440 | ((uint8_t *)&ip_int)[i]); | ||
441 | if (n < 0) | 410 | if (n < 0) |
442 | { | 411 | { |
443 | GNUNET_free (buf); | 412 | GNUNET_free (buf); |
444 | return NULL; | 413 | return NULL; |
445 | } | 414 | } |
446 | pos += n; | 415 | pos += n; |
447 | } | 416 | } |
448 | pos += GNUNET_snprintf (buf + pos, | 417 | pos += GNUNET_snprintf (buf + pos, 80 - pos, "in-addr.arpa"); |
449 | 80 - pos, | ||
450 | "in-addr.arpa"); | ||
451 | } | 418 | } |
452 | else if (AF_INET6 == af) | 419 | else if (AF_INET6 == af) |
453 | { | 420 | { |
454 | struct in6_addr *addr = (struct in6_addr *)ip; | 421 | struct in6_addr *addr = (struct in6_addr *) ip; |
455 | for (int i = 15; i >= 0; i--) | 422 | for (int i = 15; i >= 0; i--) |
456 | { | 423 | { |
457 | int n = GNUNET_snprintf (buf + pos, | 424 | int n = |
458 | 80 - pos, | 425 | GNUNET_snprintf (buf + pos, 80 - pos, "%x.", addr->s6_addr[i] & 0xf); |
459 | "%x.", | ||
460 | addr->s6_addr[i] & 0xf); | ||
461 | if (n < 0) | 426 | if (n < 0) |
462 | { | 427 | { |
463 | GNUNET_free (buf); | 428 | GNUNET_free (buf); |
464 | return NULL; | 429 | return NULL; |
465 | } | 430 | } |
466 | pos += n; | 431 | pos += n; |
467 | n = GNUNET_snprintf (buf + pos, | 432 | n = GNUNET_snprintf (buf + pos, 80 - pos, "%x.", addr->s6_addr[i] >> 4); |
468 | 80 - pos, | ||
469 | "%x.", | ||
470 | addr->s6_addr[i] >> 4); | ||
471 | if (n < 0) | 433 | if (n < 0) |
472 | { | 434 | { |
473 | GNUNET_free (buf); | 435 | GNUNET_free (buf); |
474 | return NULL; | 436 | return NULL; |
475 | } | 437 | } |
476 | pos += n; | 438 | pos += n; |
477 | } | 439 | } |
478 | pos += GNUNET_snprintf (buf + pos, | 440 | pos += GNUNET_snprintf (buf + pos, 80 - pos, "ip6.arpa"); |
479 | 80 - pos, | ||
480 | "ip6.arpa"); | ||
481 | } | 441 | } |
482 | buf[pos] = '\0'; | 442 | buf[pos] = '\0'; |
483 | return buf; | 443 | return buf; |
@@ -498,8 +458,8 @@ make_reverse_hostname (const void *ip, | |||
498 | static int | 458 | static int |
499 | send_reply (struct GNUNET_DNSPARSER_Record *record, | 459 | send_reply (struct GNUNET_DNSPARSER_Record *record, |
500 | uint16_t record_type, | 460 | uint16_t record_type, |
501 | uint32_t client_request_id, | 461 | uint32_t client_request_id, |
502 | struct GNUNET_SERVICE_Client *client) | 462 | struct GNUNET_SERVICE_Client *client) |
503 | { | 463 | { |
504 | struct GNUNET_RESOLVER_ResponseMessage *msg; | 464 | struct GNUNET_RESOLVER_ResponseMessage *msg; |
505 | struct GNUNET_MQ_Envelope *env; | 465 | struct GNUNET_MQ_Envelope *env; |
@@ -521,15 +481,15 @@ send_reply (struct GNUNET_DNSPARSER_Record *record, | |||
521 | payload_len = strlen (record->data.hostname) + 1; | 481 | payload_len = strlen (record->data.hostname) + 1; |
522 | break; | 482 | break; |
523 | case GNUNET_DNSPARSER_TYPE_A: | 483 | case GNUNET_DNSPARSER_TYPE_A: |
524 | if ( (GNUNET_DNSPARSER_TYPE_A != record_type) && | 484 | if ((GNUNET_DNSPARSER_TYPE_A != record_type) && |
525 | (GNUNET_DNSPARSER_TYPE_ALL != record_type) ) | 485 | (GNUNET_DNSPARSER_TYPE_ALL != record_type)) |
526 | return GNUNET_NO; | 486 | return GNUNET_NO; |
527 | payload = record->data.raw.data; | 487 | payload = record->data.raw.data; |
528 | payload_len = record->data.raw.data_len; | 488 | payload_len = record->data.raw.data_len; |
529 | break; | 489 | break; |
530 | case GNUNET_DNSPARSER_TYPE_AAAA: | 490 | case GNUNET_DNSPARSER_TYPE_AAAA: |
531 | if ( (GNUNET_DNSPARSER_TYPE_AAAA != record_type) && | 491 | if ((GNUNET_DNSPARSER_TYPE_AAAA != record_type) && |
532 | (GNUNET_DNSPARSER_TYPE_ALL != record_type) ) | 492 | (GNUNET_DNSPARSER_TYPE_ALL != record_type)) |
533 | return GNUNET_NO; | 493 | return GNUNET_NO; |
534 | payload = record->data.raw.data; | 494 | payload = record->data.raw.data; |
535 | payload_len = record->data.raw.data_len; | 495 | payload_len = record->data.raw.data_len; |
@@ -541,14 +501,11 @@ send_reply (struct GNUNET_DNSPARSER_Record *record, | |||
541 | return GNUNET_NO; | 501 | return GNUNET_NO; |
542 | } | 502 | } |
543 | env = GNUNET_MQ_msg_extra (msg, | 503 | env = GNUNET_MQ_msg_extra (msg, |
544 | payload_len, | 504 | payload_len, |
545 | GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); | 505 | GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); |
546 | msg->client_id = client_request_id; | 506 | msg->client_id = client_request_id; |
547 | GNUNET_memcpy (&msg[1], | 507 | GNUNET_memcpy (&msg[1], payload, payload_len); |
548 | payload, | 508 | GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env); |
549 | payload_len); | ||
550 | GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), | ||
551 | env); | ||
552 | return GNUNET_YES; | 509 | return GNUNET_YES; |
553 | } | 510 | } |
554 | 511 | ||
@@ -561,19 +518,15 @@ send_reply (struct GNUNET_DNSPARSER_Record *record, | |||
561 | * @param client where to send @a record | 518 | * @param client where to send @a record |
562 | */ | 519 | */ |
563 | static void | 520 | static void |
564 | send_end_msg (uint32_t client_request_id, | 521 | send_end_msg (uint32_t client_request_id, struct GNUNET_SERVICE_Client *client) |
565 | struct GNUNET_SERVICE_Client *client) | ||
566 | { | 522 | { |
567 | struct GNUNET_RESOLVER_ResponseMessage *msg; | 523 | struct GNUNET_RESOLVER_ResponseMessage *msg; |
568 | struct GNUNET_MQ_Envelope *env; | 524 | struct GNUNET_MQ_Envelope *env; |
569 | 525 | ||
570 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 526 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending END message\n"); |
571 | "Sending END message\n"); | 527 | env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); |
572 | env = GNUNET_MQ_msg (msg, | ||
573 | GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); | ||
574 | msg->client_id = client_request_id; | 528 | msg->client_id = client_request_id; |
575 | GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), | 529 | GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env); |
576 | env); | ||
577 | } | 530 | } |
578 | 531 | ||
579 | 532 | ||
@@ -590,16 +543,12 @@ remove_expired (struct ResolveCache *rc) | |||
590 | struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); | 543 | struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); |
591 | struct RecordListEntry *n; | 544 | struct RecordListEntry *n; |
592 | 545 | ||
593 | for (struct RecordListEntry *pos = rc->records_head; | 546 | for (struct RecordListEntry *pos = rc->records_head; NULL != pos; pos = n) |
594 | NULL != pos; | ||
595 | pos = n) | ||
596 | { | 547 | { |
597 | n = pos->next; | 548 | n = pos->next; |
598 | if (now.abs_value_us > pos->record->expiration_time.abs_value_us) | 549 | if (now.abs_value_us > pos->record->expiration_time.abs_value_us) |
599 | { | 550 | { |
600 | GNUNET_CONTAINER_DLL_remove (rc->records_head, | 551 | GNUNET_CONTAINER_DLL_remove (rc->records_head, rc->records_tail, pos); |
601 | rc->records_tail, | ||
602 | pos); | ||
603 | GNUNET_DNSPARSER_free_record (pos->record); | 552 | GNUNET_DNSPARSER_free_record (pos->record); |
604 | GNUNET_free (pos->record); | 553 | GNUNET_free (pos->record); |
605 | GNUNET_free (pos); | 554 | GNUNET_free (pos); |
@@ -625,9 +574,9 @@ remove_expired (struct ResolveCache *rc) | |||
625 | */ | 574 | */ |
626 | static void | 575 | static void |
627 | process_get (const char *hostname, | 576 | process_get (const char *hostname, |
628 | uint16_t record_type, | 577 | uint16_t record_type, |
629 | uint32_t client_request_id, | 578 | uint32_t client_request_id, |
630 | struct GNUNET_SERVICE_Client *client); | 579 | struct GNUNET_SERVICE_Client *client); |
631 | 580 | ||
632 | 581 | ||
633 | /** | 582 | /** |
@@ -644,8 +593,8 @@ process_get (const char *hostname, | |||
644 | static int | 593 | static int |
645 | try_cache (const char *hostname, | 594 | try_cache (const char *hostname, |
646 | uint16_t record_type, | 595 | uint16_t record_type, |
647 | uint32_t client_request_id, | 596 | uint32_t client_request_id, |
648 | struct GNUNET_SERVICE_Client *client) | 597 | struct GNUNET_SERVICE_Client *client) |
649 | { | 598 | { |
650 | struct ResolveCache *pos; | 599 | struct ResolveCache *pos; |
651 | struct ResolveCache *next; | 600 | struct ResolveCache *next; |
@@ -654,8 +603,7 @@ try_cache (const char *hostname, | |||
654 | 603 | ||
655 | in_hosts = GNUNET_NO; | 604 | in_hosts = GNUNET_NO; |
656 | for (pos = hosts_head; NULL != pos; pos = pos->next) | 605 | for (pos = hosts_head; NULL != pos; pos = pos->next) |
657 | if (0 == strcmp (pos->hostname, | 606 | if (0 == strcmp (pos->hostname, hostname)) |
658 | hostname)) | ||
659 | { | 607 | { |
660 | in_hosts = GNUNET_YES; | 608 | in_hosts = GNUNET_YES; |
661 | break; | 609 | break; |
@@ -667,33 +615,24 @@ try_cache (const char *hostname, | |||
667 | { | 615 | { |
668 | next = pos->next; | 616 | next = pos->next; |
669 | if (GNUNET_YES == remove_expired (pos)) | 617 | if (GNUNET_YES == remove_expired (pos)) |
670 | continue; | 618 | continue; |
671 | if (0 == strcmp (pos->hostname, | 619 | if (0 == strcmp (pos->hostname, hostname)) |
672 | hostname)) | 620 | break; |
673 | break; | ||
674 | } | 621 | } |
675 | } | 622 | } |
676 | if (NULL == pos) | 623 | if (NULL == pos) |
677 | { | 624 | { |
678 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 625 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cache entry for '%s'\n", hostname); |
679 | "No cache entry for '%s'\n", | ||
680 | hostname); | ||
681 | return GNUNET_NO; | 626 | return GNUNET_NO; |
682 | } | 627 | } |
683 | if ( (GNUNET_NO == in_hosts) && | 628 | if ((GNUNET_NO == in_hosts) && (cache_head != pos)) |
684 | (cache_head != pos) ) | ||
685 | { | 629 | { |
686 | /* move result to head to achieve LRU for cache eviction */ | 630 | /* move result to head to achieve LRU for cache eviction */ |
687 | GNUNET_CONTAINER_DLL_remove (cache_head, | 631 | GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, pos); |
688 | cache_tail, | 632 | GNUNET_CONTAINER_DLL_insert (cache_head, cache_tail, pos); |
689 | pos); | ||
690 | GNUNET_CONTAINER_DLL_insert (cache_head, | ||
691 | cache_tail, | ||
692 | pos); | ||
693 | } | 633 | } |
694 | found = GNUNET_NO; | 634 | found = GNUNET_NO; |
695 | for (struct RecordListEntry *rle = pos->records_head; | 635 | for (struct RecordListEntry *rle = pos->records_head; NULL != rle; |
696 | NULL != rle; | ||
697 | rle = rle->next) | 636 | rle = rle->next) |
698 | { | 637 | { |
699 | const struct GNUNET_DNSPARSER_Record *record = rle->record; | 638 | const struct GNUNET_DNSPARSER_Record *record = rle->record; |
@@ -702,27 +641,19 @@ try_cache (const char *hostname, | |||
702 | "Found cache entry for '%s', record type '%u'\n", | 641 | "Found cache entry for '%s', record type '%u'\n", |
703 | hostname, | 642 | hostname, |
704 | record_type); | 643 | record_type); |
705 | if ( (GNUNET_DNSPARSER_TYPE_CNAME == record->type) && | 644 | if ((GNUNET_DNSPARSER_TYPE_CNAME == record->type) && |
706 | (GNUNET_DNSPARSER_TYPE_CNAME != record_type) && | 645 | (GNUNET_DNSPARSER_TYPE_CNAME != record_type) && (GNUNET_NO == found)) |
707 | (GNUNET_NO == found) ) | ||
708 | { | 646 | { |
709 | const char *hostname = record->data.hostname; | 647 | const char *hostname = record->data.hostname; |
710 | 648 | ||
711 | process_get (hostname, | 649 | process_get (hostname, record_type, client_request_id, client); |
712 | record_type, | ||
713 | client_request_id, | ||
714 | client); | ||
715 | return GNUNET_YES; /* counts as a cache "hit" */ | 650 | return GNUNET_YES; /* counts as a cache "hit" */ |
716 | } | 651 | } |
717 | found |= send_reply (rle->record, | 652 | found |= send_reply (rle->record, record_type, client_request_id, client); |
718 | record_type, | ||
719 | client_request_id, | ||
720 | client); | ||
721 | } | 653 | } |
722 | if (GNUNET_NO == found) | 654 | if (GNUNET_NO == found) |
723 | return GNUNET_NO; /* had records, but none matched! */ | 655 | return GNUNET_NO; /* had records, but none matched! */ |
724 | send_end_msg (client_request_id, | 656 | send_end_msg (client_request_id, client); |
725 | client); | ||
726 | return GNUNET_YES; | 657 | return GNUNET_YES; |
727 | } | 658 | } |
728 | 659 | ||
@@ -748,24 +679,19 @@ pack (const char *hostname, | |||
748 | struct GNUNET_DNSPARSER_Query query; | 679 | struct GNUNET_DNSPARSER_Query query; |
749 | struct GNUNET_DNSPARSER_Packet packet; | 680 | struct GNUNET_DNSPARSER_Packet packet; |
750 | 681 | ||
751 | query.name = (char *)hostname; | 682 | query.name = (char *) hostname; |
752 | query.type = type; | 683 | query.type = type; |
753 | query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET; | 684 | query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET; |
754 | memset (&packet, | 685 | memset (&packet, 0, sizeof (packet)); |
755 | 0, | ||
756 | sizeof (packet)); | ||
757 | packet.num_queries = 1; | 686 | packet.num_queries = 1; |
758 | packet.queries = &query; | 687 | packet.queries = &query; |
759 | packet.id = htons (dns_id); | 688 | packet.id = htons (dns_id); |
760 | packet.flags.recursion_desired = 1; | 689 | packet.flags.recursion_desired = 1; |
761 | if (GNUNET_OK != | 690 | if (GNUNET_OK != |
762 | GNUNET_DNSPARSER_pack (&packet, | 691 | GNUNET_DNSPARSER_pack (&packet, UINT16_MAX, packet_buf, packet_size)) |
763 | UINT16_MAX, | ||
764 | packet_buf, | ||
765 | packet_size)) | ||
766 | { | 692 | { |
767 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 693 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
768 | "Failed to pack query for hostname `%s'\n", | 694 | "Failed to pack query for hostname `%s'\n", |
769 | hostname); | 695 | hostname); |
770 | packet_buf = NULL; | 696 | packet_buf = NULL; |
771 | return GNUNET_SYSERR; | 697 | return GNUNET_SYSERR; |
@@ -774,9 +700,9 @@ pack (const char *hostname, | |||
774 | } | 700 | } |
775 | 701 | ||
776 | static void | 702 | static void |
777 | cache_answers(const char* name, | 703 | cache_answers (const char *name, |
778 | struct GNUNET_DNSPARSER_Record *records, | 704 | struct GNUNET_DNSPARSER_Record *records, |
779 | unsigned int num_records) | 705 | unsigned int num_records) |
780 | { | 706 | { |
781 | struct ResolveCache *rc; | 707 | struct ResolveCache *rc; |
782 | struct GNUNET_DNSPARSER_Record *record; | 708 | struct GNUNET_DNSPARSER_Record *record; |
@@ -787,25 +713,20 @@ cache_answers(const char* name, | |||
787 | record = &records[i]; | 713 | record = &records[i]; |
788 | 714 | ||
789 | for (rc = cache_head; NULL != rc; rc = rc->next) | 715 | for (rc = cache_head; NULL != rc; rc = rc->next) |
790 | if (0 == strcasecmp (rc->hostname, | 716 | if (0 == strcasecmp (rc->hostname, name)) |
791 | name)) | ||
792 | break; | 717 | break; |
793 | if (NULL == rc) | 718 | if (NULL == rc) |
794 | { | 719 | { |
795 | rc = GNUNET_new (struct ResolveCache); | 720 | rc = GNUNET_new (struct ResolveCache); |
796 | rc->hostname = GNUNET_strdup (name); | 721 | rc->hostname = GNUNET_strdup (name); |
797 | GNUNET_CONTAINER_DLL_insert (cache_head, | 722 | GNUNET_CONTAINER_DLL_insert (cache_head, cache_tail, rc); |
798 | cache_tail, | ||
799 | rc); | ||
800 | cache_size++; | 723 | cache_size++; |
801 | } | 724 | } |
802 | /* TODO: ought to check first if we have this exact record | 725 | /* TODO: ought to check first if we have this exact record |
803 | already in the cache! */ | 726 | already in the cache! */ |
804 | rle = GNUNET_new (struct RecordListEntry); | 727 | rle = GNUNET_new (struct RecordListEntry); |
805 | rle->record = GNUNET_DNSPARSER_duplicate_record (record); | 728 | rle->record = GNUNET_DNSPARSER_duplicate_record (record); |
806 | GNUNET_CONTAINER_DLL_insert (rc->records_head, | 729 | GNUNET_CONTAINER_DLL_insert (rc->records_head, rc->records_tail, rle); |
807 | rc->records_tail, | ||
808 | rle); | ||
809 | } | 730 | } |
810 | } | 731 | } |
811 | 732 | ||
@@ -825,8 +746,7 @@ handle_resolve_result (void *cls, | |||
825 | struct ActiveLookup *al = cls; | 746 | struct ActiveLookup *al = cls; |
826 | struct GNUNET_DNSPARSER_Packet *parsed; | 747 | struct GNUNET_DNSPARSER_Packet *parsed; |
827 | 748 | ||
828 | parsed = GNUNET_DNSPARSER_parse ((const char *)dns, | 749 | parsed = GNUNET_DNSPARSER_parse ((const char *) dns, dns_len); |
829 | dns_len); | ||
830 | if (NULL == parsed) | 750 | if (NULL == parsed) |
831 | { | 751 | { |
832 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 752 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
@@ -842,22 +762,21 @@ handle_resolve_result (void *cls, | |||
842 | GNUNET_DNSPARSER_free_packet (parsed); | 762 | GNUNET_DNSPARSER_free_packet (parsed); |
843 | return; | 763 | return; |
844 | } | 764 | } |
845 | if (0 == parsed->num_answers + parsed->num_authority_records + parsed->num_additional_records) | 765 | if (0 == parsed->num_answers + parsed->num_authority_records + |
766 | parsed->num_additional_records) | ||
846 | { | 767 | { |
847 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 768 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
848 | "DNS reply (hostname %s, request ID %u) contains no answers\n", | 769 | "DNS reply (hostname %s, request ID %u) contains no answers\n", |
849 | al->hostname, | 770 | al->hostname, |
850 | (unsigned int) al->client_request_id); | 771 | (unsigned int) al->client_request_id); |
851 | /* resume by trying again from cache */ | 772 | /* resume by trying again from cache */ |
852 | if (GNUNET_NO == | 773 | if (GNUNET_NO == try_cache (al->hostname, |
853 | try_cache (al->hostname, | 774 | al->record_type, |
854 | al->record_type, | 775 | al->client_request_id, |
855 | al->client_request_id, | 776 | al->client)) |
856 | al->client)) | 777 | /* cache failed, tell client we could not get an answer */ |
857 | /* cache failed, tell client we could not get an answer */ | ||
858 | { | 778 | { |
859 | send_end_msg (al->client_request_id, | 779 | send_end_msg (al->client_request_id, al->client); |
860 | al->client); | ||
861 | } | 780 | } |
862 | GNUNET_DNSPARSER_free_packet (parsed); | 781 | GNUNET_DNSPARSER_free_packet (parsed); |
863 | free_active_lookup (al); | 782 | free_active_lookup (al); |
@@ -872,16 +791,17 @@ handle_resolve_result (void *cls, | |||
872 | al->hostname, | 791 | al->hostname, |
873 | (unsigned int) al->client_request_id); | 792 | (unsigned int) al->client_request_id); |
874 | /* add to cache */ | 793 | /* add to cache */ |
875 | cache_answers(al->hostname, | 794 | cache_answers (al->hostname, parsed->answers, parsed->num_answers); |
876 | parsed->answers, parsed->num_answers); | 795 | cache_answers (al->hostname, |
877 | cache_answers(al->hostname, | 796 | parsed->authority_records, |
878 | parsed->authority_records, parsed->num_authority_records); | 797 | parsed->num_authority_records); |
879 | cache_answers(al->hostname, | 798 | cache_answers (al->hostname, |
880 | parsed->additional_records, parsed->num_additional_records); | 799 | parsed->additional_records, |
800 | parsed->num_additional_records); | ||
881 | 801 | ||
882 | /* see if we need to do the 2nd request for AAAA records */ | 802 | /* see if we need to do the 2nd request for AAAA records */ |
883 | if ( (GNUNET_DNSPARSER_TYPE_ALL == al->record_type) && | 803 | if ((GNUNET_DNSPARSER_TYPE_ALL == al->record_type) && |
884 | (GNUNET_NO == al->did_aaaa) ) | 804 | (GNUNET_NO == al->did_aaaa)) |
885 | { | 805 | { |
886 | char *packet_buf; | 806 | char *packet_buf; |
887 | size_t packet_size; | 807 | size_t packet_size; |
@@ -889,22 +809,20 @@ handle_resolve_result (void *cls, | |||
889 | 809 | ||
890 | dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, | 810 | dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, |
891 | UINT16_MAX); | 811 | UINT16_MAX); |
892 | if (GNUNET_OK == | 812 | if (GNUNET_OK == pack (al->hostname, |
893 | pack (al->hostname, | 813 | GNUNET_DNSPARSER_TYPE_AAAA, |
894 | GNUNET_DNSPARSER_TYPE_AAAA, | 814 | dns_id, |
895 | dns_id, | 815 | &packet_buf, |
896 | &packet_buf, | 816 | &packet_size)) |
897 | &packet_size)) | ||
898 | { | 817 | { |
899 | al->did_aaaa = GNUNET_YES; | 818 | al->did_aaaa = GNUNET_YES; |
900 | al->dns_id = dns_id; | 819 | al->dns_id = dns_id; |
901 | GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle); | 820 | GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle); |
902 | al->resolve_handle = | 821 | al->resolve_handle = GNUNET_DNSSTUB_resolve (dnsstub_ctx, |
903 | GNUNET_DNSSTUB_resolve (dnsstub_ctx, | 822 | packet_buf, |
904 | packet_buf, | 823 | packet_size, |
905 | packet_size, | 824 | &handle_resolve_result, |
906 | &handle_resolve_result, | 825 | al); |
907 | al); | ||
908 | GNUNET_free (packet_buf); | 826 | GNUNET_free (packet_buf); |
909 | GNUNET_DNSPARSER_free_packet (parsed); | 827 | GNUNET_DNSPARSER_free_packet (parsed); |
910 | return; | 828 | return; |
@@ -912,15 +830,13 @@ handle_resolve_result (void *cls, | |||
912 | } | 830 | } |
913 | 831 | ||
914 | /* resume by trying again from cache */ | 832 | /* resume by trying again from cache */ |
915 | if (GNUNET_NO == | 833 | if (GNUNET_NO == try_cache (al->hostname, |
916 | try_cache (al->hostname, | 834 | al->record_type, |
917 | al->record_type, | 835 | al->client_request_id, |
918 | al->client_request_id, | 836 | al->client)) |
919 | al->client)) | 837 | /* cache failed, tell client we could not get an answer */ |
920 | /* cache failed, tell client we could not get an answer */ | ||
921 | { | 838 | { |
922 | send_end_msg (al->client_request_id, | 839 | send_end_msg (al->client_request_id, al->client); |
923 | al->client); | ||
924 | } | 840 | } |
925 | free_active_lookup (al); | 841 | free_active_lookup (al); |
926 | GNUNET_DNSPARSER_free_packet (parsed); | 842 | GNUNET_DNSPARSER_free_packet (parsed); |
@@ -939,10 +855,8 @@ handle_resolve_timeout (void *cls) | |||
939 | struct ActiveLookup *al = cls; | 855 | struct ActiveLookup *al = cls; |
940 | 856 | ||
941 | al->timeout_task = NULL; | 857 | al->timeout_task = NULL; |
942 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 858 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DNS lookup timeout!\n"); |
943 | "DNS lookup timeout!\n"); | 859 | send_end_msg (al->client_request_id, al->client); |
944 | send_end_msg (al->client_request_id, | ||
945 | al->client); | ||
946 | free_active_lookup (al); | 860 | free_active_lookup (al); |
947 | } | 861 | } |
948 | 862 | ||
@@ -958,7 +872,7 @@ handle_resolve_timeout (void *cls) | |||
958 | * @return #GNUNET_OK if the DNS query is now pending | 872 | * @return #GNUNET_OK if the DNS query is now pending |
959 | */ | 873 | */ |
960 | static int | 874 | static int |
961 | resolve_and_cache (const char* hostname, | 875 | resolve_and_cache (const char *hostname, |
962 | uint16_t record_type, | 876 | uint16_t record_type, |
963 | uint32_t client_request_id, | 877 | uint32_t client_request_id, |
964 | struct GNUNET_SERVICE_Client *client) | 878 | struct GNUNET_SERVICE_Client *client) |
@@ -969,9 +883,7 @@ resolve_and_cache (const char* hostname, | |||
969 | uint16_t dns_id; | 883 | uint16_t dns_id; |
970 | uint16_t type; | 884 | uint16_t type; |
971 | 885 | ||
972 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 886 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "resolve_and_cache `%s'\n", hostname); |
973 | "resolve_and_cache `%s'\n", | ||
974 | hostname); | ||
975 | dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, | 887 | dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, |
976 | UINT16_MAX); | 888 | UINT16_MAX); |
977 | 889 | ||
@@ -979,12 +891,7 @@ resolve_and_cache (const char* hostname, | |||
979 | type = GNUNET_DNSPARSER_TYPE_A; | 891 | type = GNUNET_DNSPARSER_TYPE_A; |
980 | else | 892 | else |
981 | type = record_type; | 893 | type = record_type; |
982 | if (GNUNET_OK != | 894 | if (GNUNET_OK != pack (hostname, type, dns_id, &packet_buf, &packet_size)) |
983 | pack (hostname, | ||
984 | type, | ||
985 | dns_id, | ||
986 | &packet_buf, | ||
987 | &packet_size)) | ||
988 | { | 895 | { |
989 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 896 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
990 | "Failed to pack query for hostname `%s'\n", | 897 | "Failed to pack query for hostname `%s'\n", |
@@ -998,19 +905,15 @@ resolve_and_cache (const char* hostname, | |||
998 | al->client_request_id = client_request_id; | 905 | al->client_request_id = client_request_id; |
999 | al->dns_id = dns_id; | 906 | al->dns_id = dns_id; |
1000 | al->client = client; | 907 | al->client = client; |
1001 | al->timeout_task = GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT, | 908 | al->timeout_task = |
1002 | &handle_resolve_timeout, | 909 | GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT, &handle_resolve_timeout, al); |
1003 | al); | 910 | al->resolve_handle = GNUNET_DNSSTUB_resolve (dnsstub_ctx, |
1004 | al->resolve_handle = | 911 | packet_buf, |
1005 | GNUNET_DNSSTUB_resolve (dnsstub_ctx, | 912 | packet_size, |
1006 | packet_buf, | 913 | &handle_resolve_result, |
1007 | packet_size, | 914 | al); |
1008 | &handle_resolve_result, | ||
1009 | al); | ||
1010 | GNUNET_free (packet_buf); | 915 | GNUNET_free (packet_buf); |
1011 | GNUNET_CONTAINER_DLL_insert (lookup_head, | 916 | GNUNET_CONTAINER_DLL_insert (lookup_head, lookup_tail, al); |
1012 | lookup_tail, | ||
1013 | al); | ||
1014 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 917 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1015 | "Resolving %s, client_request_id = %u, dns_id = %u\n", | 918 | "Resolving %s, client_request_id = %u, dns_id = %u\n", |
1016 | hostname, | 919 | hostname, |
@@ -1037,29 +940,16 @@ process_get (const char *hostname, | |||
1037 | { | 940 | { |
1038 | char fqdn[255]; | 941 | char fqdn[255]; |
1039 | 942 | ||
1040 | if (GNUNET_NO != | 943 | if (GNUNET_NO != try_cache (hostname, record_type, client_request_id, client)) |
1041 | try_cache (hostname, | ||
1042 | record_type, | ||
1043 | client_request_id, | ||
1044 | client)) | ||
1045 | return; | 944 | return; |
1046 | if ( (NULL != my_domain) && | 945 | if ((NULL != my_domain) && (NULL == strchr (hostname, (unsigned char) '.')) && |
1047 | (NULL == strchr (hostname, | 946 | (strlen (hostname) + strlen (my_domain) <= 253)) |
1048 | (unsigned char) '.')) && | ||
1049 | (strlen (hostname) + strlen (my_domain) <= 253) ) | ||
1050 | { | 947 | { |
1051 | GNUNET_snprintf (fqdn, | 948 | GNUNET_snprintf (fqdn, sizeof (fqdn), "%s.%s", hostname, my_domain); |
1052 | sizeof (fqdn), | ||
1053 | "%s.%s", | ||
1054 | hostname, | ||
1055 | my_domain); | ||
1056 | } | 949 | } |
1057 | else if (strlen (hostname) < 255) | 950 | else if (strlen (hostname) < 255) |
1058 | { | 951 | { |
1059 | GNUNET_snprintf (fqdn, | 952 | GNUNET_snprintf (fqdn, sizeof (fqdn), "%s", hostname); |
1060 | sizeof (fqdn), | ||
1061 | "%s", | ||
1062 | hostname); | ||
1063 | } | 953 | } |
1064 | else | 954 | else |
1065 | { | 955 | { |
@@ -1067,20 +957,12 @@ process_get (const char *hostname, | |||
1067 | GNUNET_SERVICE_client_drop (client); | 957 | GNUNET_SERVICE_client_drop (client); |
1068 | return; | 958 | return; |
1069 | } | 959 | } |
1070 | if (GNUNET_NO == | 960 | if (GNUNET_NO == try_cache (fqdn, record_type, client_request_id, client)) |
1071 | try_cache (fqdn, | ||
1072 | record_type, | ||
1073 | client_request_id, | ||
1074 | client)) | ||
1075 | { | 961 | { |
1076 | if (GNUNET_OK != | 962 | if (GNUNET_OK != |
1077 | resolve_and_cache (fqdn, | 963 | resolve_and_cache (fqdn, record_type, client_request_id, client)) |
1078 | record_type, | ||
1079 | client_request_id, | ||
1080 | client)) | ||
1081 | { | 964 | { |
1082 | send_end_msg (client_request_id, | 965 | send_end_msg (client_request_id, client); |
1083 | client); | ||
1084 | } | 966 | } |
1085 | } | 967 | } |
1086 | } | 968 | } |
@@ -1094,8 +976,7 @@ process_get (const char *hostname, | |||
1094 | * @return #GNUNET_OK if @a get is well-formed | 976 | * @return #GNUNET_OK if @a get is well-formed |
1095 | */ | 977 | */ |
1096 | static int | 978 | static int |
1097 | check_get (void *cls, | 979 | check_get (void *cls, const struct GNUNET_RESOLVER_GetMessage *get) |
1098 | const struct GNUNET_RESOLVER_GetMessage *get) | ||
1099 | { | 980 | { |
1100 | uint16_t size; | 981 | uint16_t size; |
1101 | int direction; | 982 | int direction; |
@@ -1112,23 +993,23 @@ check_get (void *cls, | |||
1112 | af = ntohl (get->af); | 993 | af = ntohl (get->af); |
1113 | switch (af) | 994 | switch (af) |
1114 | { | 995 | { |
1115 | case AF_INET: | 996 | case AF_INET: |
1116 | if (size != sizeof (struct in_addr)) | 997 | if (size != sizeof (struct in_addr)) |
1117 | { | 998 | { |
1118 | GNUNET_break (0); | ||
1119 | return GNUNET_SYSERR; | ||
1120 | } | ||
1121 | break; | ||
1122 | case AF_INET6: | ||
1123 | if (size != sizeof (struct in6_addr)) | ||
1124 | { | ||
1125 | GNUNET_break (0); | ||
1126 | return GNUNET_SYSERR; | ||
1127 | } | ||
1128 | break; | ||
1129 | default: | ||
1130 | GNUNET_break (0); | 999 | GNUNET_break (0); |
1131 | return GNUNET_SYSERR; | 1000 | return GNUNET_SYSERR; |
1001 | } | ||
1002 | break; | ||
1003 | case AF_INET6: | ||
1004 | if (size != sizeof (struct in6_addr)) | ||
1005 | { | ||
1006 | GNUNET_break (0); | ||
1007 | return GNUNET_SYSERR; | ||
1008 | } | ||
1009 | break; | ||
1010 | default: | ||
1011 | GNUNET_break (0); | ||
1012 | return GNUNET_SYSERR; | ||
1132 | } | 1013 | } |
1133 | return GNUNET_OK; | 1014 | return GNUNET_OK; |
1134 | } | 1015 | } |
@@ -1141,8 +1022,7 @@ check_get (void *cls, | |||
1141 | * @param msg the actual message | 1022 | * @param msg the actual message |
1142 | */ | 1023 | */ |
1143 | static void | 1024 | static void |
1144 | handle_get (void *cls, | 1025 | handle_get (void *cls, const struct GNUNET_RESOLVER_GetMessage *msg) |
1145 | const struct GNUNET_RESOLVER_GetMessage *msg) | ||
1146 | { | 1026 | { |
1147 | struct GNUNET_SERVICE_Client *client = cls; | 1027 | struct GNUNET_SERVICE_Client *client = cls; |
1148 | int direction; | 1028 | int direction; |
@@ -1159,48 +1039,41 @@ handle_get (void *cls, | |||
1159 | /* IP from hostname */ | 1039 | /* IP from hostname */ |
1160 | hostname = GNUNET_strdup ((const char *) &msg[1]); | 1040 | hostname = GNUNET_strdup ((const char *) &msg[1]); |
1161 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1041 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1162 | "Client asks to resolve `%s'\n", | 1042 | "Client asks to resolve `%s'\n", |
1163 | hostname); | 1043 | hostname); |
1164 | switch (af) | 1044 | switch (af) |
1165 | { | 1045 | { |
1166 | case AF_UNSPEC: | 1046 | case AF_UNSPEC: { |
1167 | { | 1047 | process_get (hostname, |
1168 | process_get (hostname, | 1048 | GNUNET_DNSPARSER_TYPE_ALL, |
1169 | GNUNET_DNSPARSER_TYPE_ALL, | 1049 | client_request_id, |
1170 | client_request_id, | 1050 | client); |
1171 | client); | 1051 | break; |
1172 | break; | 1052 | } |
1173 | } | 1053 | case AF_INET: { |
1174 | case AF_INET: | 1054 | process_get (hostname, |
1175 | { | 1055 | GNUNET_DNSPARSER_TYPE_A, |
1176 | process_get (hostname, | 1056 | client_request_id, |
1177 | GNUNET_DNSPARSER_TYPE_A, | 1057 | client); |
1178 | client_request_id, | 1058 | break; |
1179 | client); | 1059 | } |
1180 | break; | 1060 | case AF_INET6: { |
1181 | } | 1061 | process_get (hostname, |
1182 | case AF_INET6: | 1062 | GNUNET_DNSPARSER_TYPE_AAAA, |
1183 | { | 1063 | client_request_id, |
1184 | process_get (hostname, | 1064 | client); |
1185 | GNUNET_DNSPARSER_TYPE_AAAA, | 1065 | break; |
1186 | client_request_id, | 1066 | } |
1187 | client); | 1067 | default: { |
1188 | break; | 1068 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got invalid af: %d\n", af); |
1189 | } | 1069 | GNUNET_assert (0); |
1190 | default: | 1070 | } |
1191 | { | ||
1192 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1193 | "got invalid af: %d\n", | ||
1194 | af); | ||
1195 | GNUNET_assert (0); | ||
1196 | } | ||
1197 | } | 1071 | } |
1198 | } | 1072 | } |
1199 | else | 1073 | else |
1200 | { | 1074 | { |
1201 | /* hostname from IP */ | 1075 | /* hostname from IP */ |
1202 | hostname = make_reverse_hostname (&msg[1], | 1076 | hostname = make_reverse_hostname (&msg[1], af); |
1203 | af); | ||
1204 | process_get (hostname, | 1077 | process_get (hostname, |
1205 | GNUNET_DNSPARSER_TYPE_PTR, | 1078 | GNUNET_DNSPARSER_TYPE_PTR, |
1206 | client_request_id, | 1079 | client_request_id, |
@@ -1255,19 +1128,14 @@ add_host (const char *hostname, | |||
1255 | rec->type = rec_type; | 1128 | rec->type = rec_type; |
1256 | rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET; | 1129 | rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET; |
1257 | rec->name = GNUNET_strdup (hostname); | 1130 | rec->name = GNUNET_strdup (hostname); |
1258 | rec->data.raw.data = GNUNET_memdup (data, | 1131 | rec->data.raw.data = GNUNET_memdup (data, data_size); |
1259 | data_size); | ||
1260 | rec->data.raw.data_len = data_size; | 1132 | rec->data.raw.data_len = data_size; |
1261 | rle = GNUNET_new (struct RecordListEntry); | 1133 | rle = GNUNET_new (struct RecordListEntry); |
1262 | rle->record = rec; | 1134 | rle->record = rec; |
1263 | rc = GNUNET_new (struct ResolveCache); | 1135 | rc = GNUNET_new (struct ResolveCache); |
1264 | rc->hostname = GNUNET_strdup (hostname); | 1136 | rc->hostname = GNUNET_strdup (hostname); |
1265 | GNUNET_CONTAINER_DLL_insert (rc->records_head, | 1137 | GNUNET_CONTAINER_DLL_insert (rc->records_head, rc->records_tail, rle); |
1266 | rc->records_tail, | 1138 | GNUNET_CONTAINER_DLL_insert (hosts_head, hosts_tail, rc); |
1267 | rle); | ||
1268 | GNUNET_CONTAINER_DLL_insert (hosts_head, | ||
1269 | hosts_tail, | ||
1270 | rc); | ||
1271 | } | 1139 | } |
1272 | 1140 | ||
1273 | 1141 | ||
@@ -1278,8 +1146,7 @@ add_host (const char *hostname, | |||
1278 | * @param line_len number of bytes in @a line | 1146 | * @param line_len number of bytes in @a line |
1279 | */ | 1147 | */ |
1280 | static void | 1148 | static void |
1281 | extract_hosts (const char *line, | 1149 | extract_hosts (const char *line, size_t line_len) |
1282 | size_t line_len) | ||
1283 | { | 1150 | { |
1284 | const char *c; | 1151 | const char *c; |
1285 | struct in_addr v4; | 1152 | struct in_addr v4; |
@@ -1288,45 +1155,31 @@ extract_hosts (const char *line, | |||
1288 | char *tok; | 1155 | char *tok; |
1289 | 1156 | ||
1290 | /* ignore everything after '#' */ | 1157 | /* ignore everything after '#' */ |
1291 | c = memrchr (line, | 1158 | c = memrchr (line, (unsigned char) '#', line_len); |
1292 | (unsigned char) '#', | ||
1293 | line_len); | ||
1294 | if (NULL != c) | 1159 | if (NULL != c) |
1295 | line_len = c - line; | 1160 | line_len = c - line; |
1296 | /* ignore leading whitespace */ | 1161 | /* ignore leading whitespace */ |
1297 | while ( (0 < line_len) && | 1162 | while ((0 < line_len) && isspace ((unsigned char) *line)) |
1298 | isspace ((unsigned char) *line) ) | ||
1299 | { | 1163 | { |
1300 | line++; | 1164 | line++; |
1301 | line_len--; | 1165 | line_len--; |
1302 | } | 1166 | } |
1303 | tbuf = GNUNET_strndup (line, | 1167 | tbuf = GNUNET_strndup (line, line_len); |
1304 | line_len); | ||
1305 | tok = strtok (tbuf, " \t"); | 1168 | tok = strtok (tbuf, " \t"); |
1306 | if (NULL == tok) | 1169 | if (NULL == tok) |
1307 | { | 1170 | { |
1308 | GNUNET_free (tbuf); | 1171 | GNUNET_free (tbuf); |
1309 | return; | 1172 | return; |
1310 | } | 1173 | } |
1311 | if (1 == inet_pton (AF_INET, | 1174 | if (1 == inet_pton (AF_INET, tok, &v4)) |
1312 | tok, | ||
1313 | &v4)) | ||
1314 | { | 1175 | { |
1315 | while (NULL != (tok = strtok (NULL, " \t"))) | 1176 | while (NULL != (tok = strtok (NULL, " \t"))) |
1316 | add_host (tok, | 1177 | add_host (tok, GNUNET_DNSPARSER_TYPE_A, &v4, sizeof (struct in_addr)); |
1317 | GNUNET_DNSPARSER_TYPE_A, | ||
1318 | &v4, | ||
1319 | sizeof (struct in_addr)); | ||
1320 | } | 1178 | } |
1321 | else if (1 == inet_pton (AF_INET6, | 1179 | else if (1 == inet_pton (AF_INET6, tok, &v6)) |
1322 | tok, | ||
1323 | &v6)) | ||
1324 | { | 1180 | { |
1325 | while (NULL != (tok = strtok (NULL, " \t"))) | 1181 | while (NULL != (tok = strtok (NULL, " \t"))) |
1326 | add_host (tok, | 1182 | add_host (tok, GNUNET_DNSPARSER_TYPE_AAAA, &v6, sizeof (struct in6_addr)); |
1327 | GNUNET_DNSPARSER_TYPE_AAAA, | ||
1328 | &v6, | ||
1329 | sizeof (struct in6_addr)); | ||
1330 | } | 1183 | } |
1331 | GNUNET_free (tbuf); | 1184 | GNUNET_free (tbuf); |
1332 | } | 1185 | } |
@@ -1349,13 +1202,10 @@ load_etc_hosts (void) | |||
1349 | GNUNET_DISK_PERM_NONE); | 1202 | GNUNET_DISK_PERM_NONE); |
1350 | if (NULL == fh) | 1203 | if (NULL == fh) |
1351 | { | 1204 | { |
1352 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 1205 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to open /etc/hosts"); |
1353 | "Failed to open /etc/hosts"); | ||
1354 | return; | 1206 | return; |
1355 | } | 1207 | } |
1356 | if (GNUNET_OK != | 1208 | if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read)) |
1357 | GNUNET_DISK_file_handle_size (fh, | ||
1358 | &bytes_read)) | ||
1359 | { | 1209 | { |
1360 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 1210 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
1361 | "Could not determin size of /etc/hosts. " | 1211 | "Could not determin size of /etc/hosts. " |
@@ -1363,7 +1213,7 @@ load_etc_hosts (void) | |||
1363 | GNUNET_DISK_file_close (fh); | 1213 | GNUNET_DISK_file_close (fh); |
1364 | return; | 1214 | return; |
1365 | } | 1215 | } |
1366 | if ((size_t) bytes_read > SIZE_MAX) | 1216 | if (bytes_read > (off_t) SIZE_MAX) |
1367 | { | 1217 | { |
1368 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 1218 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
1369 | "/etc/hosts file too large to mmap. " | 1219 | "/etc/hosts file too large to mmap. " |
@@ -1381,13 +1231,11 @@ load_etc_hosts (void) | |||
1381 | const char *newline; | 1231 | const char *newline; |
1382 | size_t line_len; | 1232 | size_t line_len; |
1383 | 1233 | ||
1384 | newline = strchr (buf + read_offset, | 1234 | newline = strchr (buf + read_offset, '\n'); |
1385 | '\n'); | ||
1386 | if (NULL == newline) | 1235 | if (NULL == newline) |
1387 | break; | 1236 | break; |
1388 | line_len = newline - buf - read_offset; | 1237 | line_len = newline - buf - read_offset; |
1389 | extract_hosts (buf + read_offset, | 1238 | extract_hosts (buf + read_offset, line_len); |
1390 | line_len); | ||
1391 | read_offset += line_len + 1; | 1239 | read_offset += line_len + 1; |
1392 | } | 1240 | } |
1393 | GNUNET_DISK_file_unmap (mh); | 1241 | GNUNET_DISK_file_unmap (mh); |
@@ -1413,15 +1261,15 @@ init_cb (void *cls, | |||
1413 | (void) cfg; | 1261 | (void) cfg; |
1414 | (void) sh; | 1262 | (void) sh; |
1415 | load_etc_hosts (); | 1263 | load_etc_hosts (); |
1416 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | 1264 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, cls); |
1417 | cls); | ||
1418 | dnsstub_ctx = GNUNET_DNSSTUB_start (128); | 1265 | dnsstub_ctx = GNUNET_DNSSTUB_start (128); |
1419 | dns_servers = NULL; | 1266 | dns_servers = NULL; |
1420 | num_dns_servers = lookup_dns_servers (&dns_servers); | 1267 | num_dns_servers = lookup_dns_servers (&dns_servers); |
1421 | if (0 >= num_dns_servers) | 1268 | if (0 >= num_dns_servers) |
1422 | { | 1269 | { |
1423 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 1270 | GNUNET_log ( |
1424 | _("No DNS server available. DNS resolution will not be possible.\n")); | 1271 | GNUNET_ERROR_TYPE_ERROR, |
1272 | _ ("No DNS server available. DNS resolution will not be possible.\n")); | ||
1425 | return; | 1273 | return; |
1426 | } | 1274 | } |
1427 | for (int i = 0; i < num_dns_servers; i++) | 1275 | for (int i = 0; i < num_dns_servers; i++) |
@@ -1465,18 +1313,14 @@ connect_cb (void *cls, | |||
1465 | * @param internal_cls should be equal to @a c | 1313 | * @param internal_cls should be equal to @a c |
1466 | */ | 1314 | */ |
1467 | static void | 1315 | static void |
1468 | disconnect_cb (void *cls, | 1316 | disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls) |
1469 | struct GNUNET_SERVICE_Client *c, | ||
1470 | void *internal_cls) | ||
1471 | { | 1317 | { |
1472 | struct ActiveLookup *n; | 1318 | struct ActiveLookup *n; |
1473 | (void) cls; | 1319 | (void) cls; |
1474 | 1320 | ||
1475 | GNUNET_assert (c == internal_cls); | 1321 | GNUNET_assert (c == internal_cls); |
1476 | n = lookup_head; | 1322 | n = lookup_head; |
1477 | for (struct ActiveLookup *al = n; | 1323 | for (struct ActiveLookup *al = n; NULL != al; al = n) |
1478 | NULL != al; | ||
1479 | al = n) | ||
1480 | { | 1324 | { |
1481 | n = al->next; | 1325 | n = al->next; |
1482 | if (al->client == c) | 1326 | if (al->client == c) |
@@ -1488,18 +1332,18 @@ disconnect_cb (void *cls, | |||
1488 | /** | 1332 | /** |
1489 | * Define "main" method using service macro. | 1333 | * Define "main" method using service macro. |
1490 | */ | 1334 | */ |
1491 | GNUNET_SERVICE_MAIN | 1335 | GNUNET_SERVICE_MAIN ( |
1492 | ("resolver", | 1336 | "resolver", |
1493 | GNUNET_SERVICE_OPTION_NONE, | 1337 | GNUNET_SERVICE_OPTION_NONE, |
1494 | &init_cb, | 1338 | &init_cb, |
1495 | &connect_cb, | 1339 | &connect_cb, |
1496 | &disconnect_cb, | 1340 | &disconnect_cb, |
1497 | NULL, | 1341 | NULL, |
1498 | GNUNET_MQ_hd_var_size (get, | 1342 | GNUNET_MQ_hd_var_size (get, |
1499 | GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, | 1343 | GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, |
1500 | struct GNUNET_RESOLVER_GetMessage, | 1344 | struct GNUNET_RESOLVER_GetMessage, |
1501 | NULL), | 1345 | NULL), |
1502 | GNUNET_MQ_handler_end ()); | 1346 | GNUNET_MQ_handler_end ()); |
1503 | 1347 | ||
1504 | 1348 | ||
1505 | #if defined(LINUX) && defined(__GLIBC__) | 1349 | #if defined(LINUX) && defined(__GLIBC__) |
@@ -1508,8 +1352,7 @@ GNUNET_SERVICE_MAIN | |||
1508 | /** | 1352 | /** |
1509 | * MINIMIZE heap size (way below 128k) since this process doesn't need much. | 1353 | * MINIMIZE heap size (way below 128k) since this process doesn't need much. |
1510 | */ | 1354 | */ |
1511 | void __attribute__ ((constructor)) | 1355 | void __attribute__ ((constructor)) GNUNET_RESOLVER_memory_init () |
1512 | GNUNET_RESOLVER_memory_init () | ||
1513 | { | 1356 | { |
1514 | mallopt (M_TRIM_THRESHOLD, 4 * 1024); | 1357 | mallopt (M_TRIM_THRESHOLD, 4 * 1024); |
1515 | mallopt (M_TOP_PAD, 1 * 1024); | 1358 | mallopt (M_TOP_PAD, 1 * 1024); |