GNUnet 0.22.2
gnunet-service-resolver.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2007-2016 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_protocols.h"
30#include "../../lib/util/resolver.h"
31
32
36#define DNS_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37
41#define MAX_CACHE 1024
42
47{
52
57
62};
63
64
69{
74
79
83 char *hostname;
84
89
94};
95
96
101{
106
111
116
121
126
130 char *hostname;
131
137
141 uint16_t record_type;
142
148
153 uint16_t dns_id;
154};
155
156
161
166
171
176
181
186
191
195static char *my_domain;
196
200static unsigned int cache_size;
201
202
208static void
210{
211 struct RecordListEntry *pos;
212
213 while (NULL != (pos = rc->records_head))
214 {
217 GNUNET_free (pos->record);
218 GNUNET_free (pos);
219 }
220 GNUNET_free (rc->hostname);
222 cache_size--;
223 GNUNET_free (rc);
224}
225
226
232static void
234{
235 struct RecordListEntry *pos;
236
237 while (NULL != (pos = rc->records_head))
238 {
241 GNUNET_free (pos->record);
242 GNUNET_free (pos);
243 }
244 GNUNET_free (rc->hostname);
246 cache_size--;
247 GNUNET_free (rc);
248}
249
250
256static void
258{
260 if (NULL != al->resolve_handle)
261 {
263 al->resolve_handle = NULL;
264 }
265 if (NULL != al->timeout_task)
266 {
268 al->timeout_task = NULL;
269 }
270 GNUNET_free (al->hostname);
271 GNUNET_free (al);
272}
273
274
284static char *
285extract_dns_server (const char *line, size_t line_len)
286{
287 if (0 == strncmp (line, "nameserver ", strlen ("nameserver ")))
288 return GNUNET_strndup (line + strlen ("nameserver "),
289 line_len - strlen ("nameserver "));
290 return NULL;
291}
292
293
303static char *
304extract_search_domain (const char *line, size_t line_len)
305{
306 if (0 == strncmp (line, "search ", strlen ("search ")))
307 return GNUNET_strndup (line + strlen ("search "),
308 line_len - strlen ("search "));
309 return NULL;
310}
311
312
319static int
320lookup_dns_servers (char ***server_addrs)
321{
322 struct GNUNET_DISK_FileHandle *fh;
324 off_t bytes_read;
325 const char *buf;
326 size_t read_offset;
327 unsigned int num_dns_servers;
328
329 fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
332 if (NULL == fh)
333 {
335 "Could not open /etc/resolv.conf. "
336 "DNS resolution will not be possible.\n");
337 return -1;
338 }
339 if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read))
340 {
342 "Could not determine size of /etc/resolv.conf. "
343 "DNS resolution will not be possible.\n");
345 return -1;
346 }
347 if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
348 {
350 "/etc/resolv.conf file too large to mmap. "
351 "DNS resolution will not be possible.\n");
353 return -1;
354 }
355 buf = GNUNET_DISK_file_map (fh,
356 &mh,
358 (size_t) bytes_read);
359 *server_addrs = NULL;
360 read_offset = 0;
361 num_dns_servers = 0;
362 while (read_offset < (size_t) bytes_read)
363 {
364 const char *newline;
365 size_t line_len;
366 char *dns_server;
367
368 newline = strchr (buf + read_offset, '\n');
369 if (NULL == newline)
370 break;
371 line_len = newline - buf - read_offset;
372 dns_server = extract_dns_server (buf + read_offset, line_len);
373 if (NULL != dns_server)
374 {
375 GNUNET_array_append (*server_addrs, num_dns_servers, dns_server);
376 }
377 else if (NULL == my_domain)
378 {
379 my_domain = extract_search_domain (buf + read_offset, line_len);
380 }
381 read_offset += line_len + 1;
382 }
385 return (int) num_dns_servers;
386}
387
388
395static char *
396make_reverse_hostname (const void *ip, int af)
397{
398 char *buf = GNUNET_new_array (80, char);
399 int pos = 0;
400
401 if (AF_INET == af)
402 {
403 struct in_addr *addr = (struct in_addr *) ip;
404 uint32_t ip_int = addr->s_addr;
405
406 for (int i = 3; i >= 0; i--)
407 {
408 int n =
409 GNUNET_snprintf (buf + pos, 80 - pos, "%u.", ((uint8_t *) &ip_int)[i]);
410 if (n < 0)
411 {
412 GNUNET_free (buf);
413 return NULL;
414 }
415 pos += n;
416 }
417 pos += GNUNET_snprintf (buf + pos, 80 - pos, "in-addr.arpa");
418 }
419 else if (AF_INET6 == af)
420 {
421 struct in6_addr *addr = (struct in6_addr *) ip;
422 for (int i = 15; i >= 0; i--)
423 {
424 int n =
425 GNUNET_snprintf (buf + pos, 80 - pos, "%x.", addr->s6_addr[i] & 0xf);
426 if (n < 0)
427 {
428 GNUNET_free (buf);
429 return NULL;
430 }
431 pos += n;
432 n = GNUNET_snprintf (buf + pos, 80 - pos, "%x.", addr->s6_addr[i] >> 4);
433 if (n < 0)
434 {
435 GNUNET_free (buf);
436 return NULL;
437 }
438 pos += n;
439 }
440 pos += GNUNET_snprintf (buf + pos, 80 - pos, "ip6.arpa");
441 }
442 buf[pos] = '\0';
443 return buf;
444}
445
446
458static int
460 uint16_t record_type,
461 uint32_t client_request_id,
462 struct GNUNET_SERVICE_Client *client)
463{
465 struct GNUNET_MQ_Envelope *env;
466 const void *payload;
467 size_t payload_len;
468
469 switch (record->type)
470 {
472 if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
473 return GNUNET_NO;
474 payload = record->data.hostname;
475 payload_len = strlen (record->data.hostname) + 1;
476 break;
477
479 if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
480 return GNUNET_NO;
481 payload = record->data.hostname;
482 payload_len = strlen (record->data.hostname) + 1;
483 break;
484
486 if ((GNUNET_DNSPARSER_TYPE_A != record_type) &&
487 (GNUNET_DNSPARSER_TYPE_ALL != record_type))
488 return GNUNET_NO;
489 payload = record->data.raw.data;
490 payload_len = record->data.raw.data_len;
491 break;
492
494 if ((GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
495 (GNUNET_DNSPARSER_TYPE_ALL != record_type))
496 return GNUNET_NO;
497 payload = record->data.raw.data;
498 payload_len = record->data.raw.data_len;
499 break;
500
501 default:
503 "Cannot handle DNS response type %u: not supported here\n",
504 record->type);
505 return GNUNET_NO;
506 }
508 payload_len,
510 msg->client_id = client_request_id;
511 GNUNET_memcpy (&msg[1], payload, payload_len);
513 return GNUNET_YES;
514}
515
516
524static void
525send_end_msg (uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
526{
528 struct GNUNET_MQ_Envelope *env;
529
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending END message\n");
532 msg->client_id = client_request_id;
534}
535
536
544static int
546{
548 struct RecordListEntry *n;
549
550 for (struct RecordListEntry *pos = rc->records_head; NULL != pos; pos = n)
551 {
552 n = pos->next;
553 if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
554 {
556 GNUNET_DNSPARSER_free_record (pos->record);
557 GNUNET_free (pos->record);
558 GNUNET_free (pos);
559 }
560 }
561 if (NULL == rc->records_head)
562 {
563 free_cache_entry (rc);
564 return GNUNET_YES;
565 }
566 return GNUNET_NO;
567}
568
569
579static void
580process_get (const char *hostname,
581 uint16_t record_type,
582 uint32_t client_request_id,
583 struct GNUNET_SERVICE_Client *client);
584
585
597static int
598try_cache (const char *hostname,
599 uint16_t record_type,
600 uint32_t client_request_id,
601 struct GNUNET_SERVICE_Client *client)
602{
603 struct ResolveCache *pos;
604 struct ResolveCache *next;
605 int found;
606 int in_hosts;
607
608 in_hosts = GNUNET_NO;
609 for (pos = hosts_head; NULL != pos; pos = pos->next)
610 if (0 == strcmp (pos->hostname, hostname))
611 {
612 in_hosts = GNUNET_YES;
613 break;
614 }
615 if (NULL == pos)
616 {
618 for (pos = next; NULL != pos; pos = next)
619 {
620 next = pos->next;
621 if (GNUNET_YES == remove_expired (pos))
622 continue;
623 if (0 == strcmp (pos->hostname, hostname))
624 break;
625 }
626 }
627 if (NULL == pos)
628 {
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No cache entry for '%s'\n", hostname);
630 return GNUNET_NO;
631 }
632 if ((GNUNET_NO == in_hosts) && (cache_head != pos))
633 {
634 /* move result to head to achieve LRU for cache eviction */
637 }
638 found = GNUNET_NO;
639 for (struct RecordListEntry *rle = pos->records_head; NULL != rle;
640 rle = rle->next)
641 {
642 const struct GNUNET_DNSPARSER_Record *record = rle->record;
643
645 "Checking cache entry for '%s', record is for '%s'\n",
646 hostname,
647 record->name);
648 if ((GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
649 (GNUNET_DNSPARSER_TYPE_CNAME != record_type) && (GNUNET_NO == found))
650 {
651 const char *hostname_tmp = record->data.hostname;
652
653 process_get (hostname_tmp, record_type, client_request_id, client);
654 return GNUNET_YES; /* counts as a cache "hit" */
655 }
656 if (0 == strcmp (record->name, hostname))
657 found |= send_reply (rle->record, record_type, client_request_id, client);
658 }
659 if (GNUNET_NO == found)
660 return GNUNET_NO; /* had records, but none matched! */
661 send_end_msg (client_request_id, client);
662 return GNUNET_YES;
663}
664
665
677static int
678pack (const char *hostname,
679 uint16_t type,
680 uint16_t dns_id,
681 char **packet_buf,
682 size_t *packet_size)
683{
684 struct GNUNET_DNSPARSER_Query query;
685 struct GNUNET_DNSPARSER_Packet packet;
686
687 query.name = (char *) hostname;
688 query.type = type;
690 memset (&packet, 0, sizeof(packet));
691 packet.num_queries = 1;
692 packet.queries = &query;
693 packet.id = htons (dns_id);
694 packet.flags.recursion_desired = 1;
695 if (GNUNET_OK !=
696 GNUNET_DNSPARSER_pack (&packet, UINT16_MAX, packet_buf, packet_size))
697 {
699 "Failed to pack query for hostname `%s'\n",
700 hostname);
701 packet_buf = NULL;
702 return GNUNET_SYSERR;
703 }
704 return GNUNET_OK;
705}
706
707
708static void
709cache_answers (const char *name,
711 unsigned int num_records)
712{
713 struct ResolveCache *rc;
715 struct RecordListEntry *rle;
716
717 for (unsigned int i = 0; i != num_records; i++)
718 {
719 record = &records[i];
720
721 for (rc = cache_head; NULL != rc; rc = rc->next)
722 if (0 == strcasecmp (rc->hostname, name))
723 break;
724 if (NULL == rc)
725 {
726 rc = GNUNET_new (struct ResolveCache);
729 "Caching record for name %s under %s\n",
730 record->name, name);
732 cache_size++;
733 }
734 /* TODO: ought to check first if we have this exact record
735 already in the cache! */
736 rle = GNUNET_new (struct RecordListEntry);
739 }
740}
741
742
751static void
753 const struct GNUNET_TUN_DnsHeader *dns,
754 size_t dns_len)
755{
756 struct ActiveLookup *al = cls;
757 struct GNUNET_DNSPARSER_Packet *parsed;
758
759 parsed = GNUNET_DNSPARSER_parse ((const char *) dns, dns_len);
760 if (NULL == parsed)
761 {
763 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
764 al->hostname,
765 al->dns_id);
766 return;
767 }
768 if (al->dns_id != ntohs (parsed->id))
769 {
771 "Request ID in DNS reply does not match\n");
773 return;
774 }
775 if (0 == parsed->num_answers + parsed->num_authority_records
776 + parsed->num_additional_records)
777 {
779 "DNS reply (hostname %s, request ID %u) contains no answers\n",
780 al->hostname,
781 (unsigned int) al->client_request_id);
782 /* resume by trying again from cache */
783 if (GNUNET_NO == try_cache (al->hostname,
784 al->record_type,
786 al->client))
787 /* cache failed, tell client we could not get an answer */
788 {
790 }
793 return;
794 }
795 /* LRU-based cache eviction: we remove from tail */
796 while (cache_size > MAX_CACHE)
798
800 "Got reply for hostname %s and request ID %u\n",
801 al->hostname,
802 (unsigned int) al->client_request_id);
803 /* add to cache */
804 cache_answers (al->hostname, parsed->answers, parsed->num_answers);
806 parsed->authority_records,
807 parsed->num_authority_records);
809 parsed->additional_records,
810 parsed->num_additional_records);
811
812 /* see if we need to do the 2nd request for AAAA records */
814 (GNUNET_NO == al->did_aaaa))
815 {
816 char *packet_buf;
817 size_t packet_size;
818 uint16_t dns_id;
819
821 UINT16_MAX);
822 if (GNUNET_OK == pack (al->hostname,
824 dns_id,
825 &packet_buf,
826 &packet_size))
827 {
828 al->did_aaaa = GNUNET_YES;
829 al->dns_id = dns_id;
832 packet_buf,
833 packet_size,
835 al);
836 GNUNET_free (packet_buf);
838 return;
839 }
840 }
841
842 /* resume by trying again from cache */
843 if (GNUNET_NO == try_cache (al->hostname,
844 al->record_type,
846 al->client))
847 /* cache failed, tell client we could not get an answer */
848 {
850 }
853}
854
855
862static void
864{
865 struct ActiveLookup *al = cls;
866
867 al->timeout_task = NULL;
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DNS lookup timeout!\n");
871}
872
873
884static int
886 uint16_t record_type,
887 uint32_t client_request_id,
889{
890 char *packet_buf;
891 size_t packet_size;
892 struct ActiveLookup *al;
893 uint16_t dns_id;
894 uint16_t type;
895
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "resolve_and_cache `%s'\n", hostname);
898 UINT16_MAX);
899
902 else
904 if (GNUNET_OK != pack (hostname, type, dns_id, &packet_buf, &packet_size))
905 {
907 "Failed to pack query for hostname `%s'\n",
908 hostname);
909 return GNUNET_SYSERR;
910 }
911
912 al = GNUNET_new (struct ActiveLookup);
916 al->dns_id = dns_id;
917 al->client = client;
918 al->timeout_task =
921 packet_buf,
922 packet_size,
924 al);
925 GNUNET_free (packet_buf);
928 "Resolving %s, client_request_id = %u, dns_id = %u\n",
929 hostname,
930 (unsigned int) client_request_id,
931 (unsigned int) dns_id);
932 return GNUNET_OK;
933}
934
935
945static void
947 uint16_t record_type,
948 uint32_t client_request_id,
950{
951 char fqdn[255];
952
954 return;
955 if ((NULL != my_domain) && (NULL == strchr (hostname, (unsigned char) '.')) &&
956 (strlen (hostname) + strlen (my_domain) <= 253))
957 {
958 GNUNET_snprintf (fqdn, sizeof(fqdn), "%s.%s", hostname, my_domain);
959 }
960 else if (strlen (hostname) < 255)
961 {
962 GNUNET_snprintf (fqdn, sizeof(fqdn), "%s", hostname);
963 }
964 else
965 {
966 GNUNET_break (0);
968 return;
969 }
971 {
972 if (GNUNET_OK !=
974 {
976 }
977 }
978}
979
980
988static int
989check_get (void *cls, const struct GNUNET_RESOLVER_GetMessage *get)
990{
991 uint16_t size;
992 int direction;
993 int af;
994
995 (void) cls;
996 size = ntohs (get->header.size) - sizeof(*get);
997 direction = ntohl (get->direction);
998 if (GNUNET_NO == direction)
999 {
1001 return GNUNET_OK;
1002 }
1003 af = ntohl (get->af);
1004 switch (af)
1005 {
1006 case AF_INET:
1007 if (size != sizeof(struct in_addr))
1008 {
1009 GNUNET_break (0);
1010 return GNUNET_SYSERR;
1011 }
1012 break;
1013
1014 case AF_INET6:
1015 if (size != sizeof(struct in6_addr))
1016 {
1017 GNUNET_break (0);
1018 return GNUNET_SYSERR;
1019 }
1020 break;
1021
1022 default:
1023 GNUNET_break (0);
1024 return GNUNET_SYSERR;
1025 }
1026 return GNUNET_OK;
1027}
1028
1029
1036static void
1038{
1039 struct GNUNET_SERVICE_Client *client = cls;
1040 int direction;
1041 int af;
1042 uint32_t client_request_id;
1043 char *hostname;
1044
1045 direction = ntohl (msg->direction);
1046 af = ntohl (msg->af);
1047 client_request_id = msg->client_id;
1049 if (GNUNET_NO == direction)
1050 {
1051 /* IP from hostname */
1052 hostname = GNUNET_strdup ((const char *) &msg[1]);
1054 "Client asks to resolve `%s'\n",
1055 hostname);
1056 switch (af)
1057 {
1058 case AF_UNSPEC: {
1059 process_get (hostname,
1061 client_request_id,
1062 client);
1063 break;
1064 }
1065
1066 case AF_INET: {
1067 process_get (hostname,
1069 client_request_id,
1070 client);
1071 break;
1072 }
1073
1074 case AF_INET6: {
1075 process_get (hostname,
1077 client_request_id,
1078 client);
1079 break;
1080 }
1081
1082 default: {
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got invalid af: %d\n", af);
1084 GNUNET_assert (0);
1085 }
1086 }
1087 }
1088 else
1089 {
1090 /* hostname from IP */
1091 hostname = make_reverse_hostname (&msg[1], af);
1092 process_get (hostname,
1094 client_request_id,
1095 client);
1096 }
1097 GNUNET_free (hostname);
1098}
1099
1100
1106static void
1107shutdown_task (void *cls)
1108{
1109 (void) cls;
1110
1111 while (NULL != lookup_head)
1113 while (NULL != cache_head)
1115 while (NULL != hosts_head)
1119}
1120
1121
1131static void
1132add_host (const char *hostname,
1133 uint16_t rec_type,
1134 const void *data,
1135 size_t data_size)
1136{
1137 struct ResolveCache *rc;
1138 struct RecordListEntry *rle;
1139 struct GNUNET_DNSPARSER_Record *rec;
1140
1141 rec = GNUNET_malloc (sizeof(struct GNUNET_DNSPARSER_Record));
1143 rec->type = rec_type;
1145 rec->name = GNUNET_strdup (hostname);
1147 rec->data.raw.data_len = data_size;
1148 rle = GNUNET_new (struct RecordListEntry);
1149 rle->record = rec;
1150 rc = GNUNET_new (struct ResolveCache);
1154}
1155
1156
1163static void
1164extract_hosts (const char *line, size_t line_len)
1165{
1166 const char *c;
1167 struct in_addr v4;
1168 struct in6_addr v6;
1169 char *tbuf;
1170 char *tok;
1171
1172 /* ignore everything after '#' */
1173 c = memrchr (line, (unsigned char) '#', line_len);
1174 if (NULL != c)
1175 line_len = c - line;
1176 /* ignore leading whitespace */
1177 while ((0 < line_len) && isspace ((unsigned char) *line))
1178 {
1179 line++;
1180 line_len--;
1181 }
1182 tbuf = GNUNET_strndup (line, line_len);
1183 tok = strtok (tbuf, " \t");
1184 if (NULL == tok)
1185 {
1186 GNUNET_free (tbuf);
1187 return;
1188 }
1189 if (1 == inet_pton (AF_INET, tok, &v4))
1190 {
1191 while (NULL != (tok = strtok (NULL, " \t")))
1192 add_host (tok, GNUNET_DNSPARSER_TYPE_A, &v4, sizeof(struct in_addr));
1193 }
1194 else if (1 == inet_pton (AF_INET6, tok, &v6))
1195 {
1196 while (NULL != (tok = strtok (NULL, " \t")))
1197 add_host (tok, GNUNET_DNSPARSER_TYPE_AAAA, &v6, sizeof(struct in6_addr));
1198 }
1199 GNUNET_free (tbuf);
1200}
1201
1202
1206static void
1208{
1209 struct GNUNET_DISK_FileHandle *fh;
1210 struct GNUNET_DISK_MapHandle *mh;
1211 off_t bytes_read;
1212 const char *buf;
1213 size_t read_offset;
1214
1215 fh = GNUNET_DISK_file_open ("/etc/hosts",
1218 if (NULL == fh)
1219 {
1220 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to open /etc/hosts");
1221 return;
1222 }
1223 if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read))
1224 {
1226 "Could not determine size of /etc/hosts. "
1227 "DNS resolution will not be possible.\n");
1229 return;
1230 }
1231 if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
1232 {
1234 "/etc/hosts file too large to mmap. "
1235 "DNS resolution will not be possible.\n");
1237 return;
1238 }
1239 buf = GNUNET_DISK_file_map (fh,
1240 &mh,
1242 (size_t) bytes_read);
1243 read_offset = 0;
1244 while (read_offset < (size_t) bytes_read)
1245 {
1246 const char *newline;
1247 size_t line_len;
1248
1249 newline = strchr (buf + read_offset, '\n');
1250 if (NULL == newline)
1251 break;
1252 line_len = newline - buf - read_offset;
1253 extract_hosts (buf + read_offset, line_len);
1254 read_offset += line_len + 1;
1255 }
1258}
1259
1260
1268static void
1269init_cb (void *cls,
1270 const struct GNUNET_CONFIGURATION_Handle *cfg,
1271 struct GNUNET_SERVICE_Handle *sh)
1272{
1273 char **dns_servers;
1274 int num_dns_servers;
1275
1276 (void) cfg;
1277 (void) sh;
1278 load_etc_hosts ();
1281 dns_servers = NULL;
1282 num_dns_servers = lookup_dns_servers (&dns_servers);
1283 if (0 >= num_dns_servers)
1284 {
1285 GNUNET_log (
1287 _ ("No DNS server available. DNS resolution will not be possible.\n"));
1288 return;
1289 }
1290 for (int i = 0; i < num_dns_servers; i++)
1291 {
1292 int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1294 "Adding DNS server '%s': %s\n",
1295 dns_servers[i],
1296 GNUNET_OK == result ? "success" : "failure");
1297 GNUNET_free (dns_servers[i]);
1298 }
1299 GNUNET_free (dns_servers);
1300}
1301
1302
1311static void *
1312connect_cb (void *cls,
1313 struct GNUNET_SERVICE_Client *c,
1314 struct GNUNET_MQ_Handle *mq)
1315{
1316 (void) cls;
1317 (void) mq;
1318
1319 return c;
1320}
1321
1322
1330static void
1331disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
1332{
1333 struct ActiveLookup *n;
1334
1335 (void) cls;
1336
1337 GNUNET_assert (c == internal_cls);
1338 n = lookup_head;
1339 for (struct ActiveLookup *al = n; NULL != al; al = n)
1340 {
1341 n = al->next;
1342 if (al->client == c)
1343 free_active_lookup (al);
1344 }
1345}
1346
1347
1353 "resolver",
1355 &init_cb,
1356 &connect_cb,
1358 NULL,
1362 NULL),
1364
1365
1366#if defined(__linux__) && defined(__GLIBC__)
1367#include <malloc.h>
1368
1369void __attribute__ ((constructor))
1370GNUNET_RESOLVER_memory_init (void);
1371
1375void __attribute__ ((constructor))
1376GNUNET_RESOLVER_memory_init (void)
1377{
1378 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1379 mallopt (M_TOP_PAD, 1 * 1024);
1380 malloc_trim (0);
1381}
1382
1383
1384#endif
1385
1386
1387/* end of gnunet-service-resolver.c */
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define memrchr(s, c, n)
Definition: compat.h:49
enum GNUNET_ABD_AlgoDirectionFlags direction
API enum, filled and passed for collect/verify.
Definition: gnunet-abd.c:173
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static char * line
Desired phone line (string to be converted to a hash).
static char * data
The data to insert into the dht.
static int get
Get DID Documement for DID Flag.
Definition: gnunet-did.c:62
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
static char * name
Name (label) of the records to list.
static uint32_t type
Type string converted to DNS type value.
static size_t data_size
Number of bytes in data.
static int result
Global testing status.
static unsigned long long payload
How much data are we currently storing in the database?
#define MAX_CACHE
Maximum number of hostnames we cache results for.
static struct ResolveCache * cache_head
Start of the linked list of cached DNS lookup results.
static void free_cache_entry(struct ResolveCache *rc)
Remove entry from cache.
static void cache_answers(const char *name, struct GNUNET_DNSPARSER_Record *records, unsigned int num_records)
static void send_end_msg(uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Send message to client that we transmitted all responses for client_request_id.
static void disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
Callback called when a client disconnected from the service.
static char * extract_dns_server(const char *line, size_t line_len)
Find out if the configuration file line contains a string starting with "nameserver ",...
static int send_reply(struct GNUNET_DNSPARSER_Record *record, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Send DNS record back to our client.
static int remove_expired(struct ResolveCache *rc)
Remove expired entries from rc.
static int pack(const char *hostname, uint16_t type, uint16_t dns_id, char **packet_buf, size_t *packet_size)
Create DNS query for hostname of type type with DNS request ID dns_id.
static struct ResolveCache * hosts_head
Head of the linked list of DNS lookup results from /etc/hosts.
static void init_cb(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_SERVICE_Handle *sh)
Service is starting, initialize everything.
static struct ActiveLookup * lookup_head
Start of the linked list of active DNS lookups.
static char * my_domain
My domain, to be appended to the hostname to get a FQDN.
static void shutdown_task(void *cls)
Service is shutting down, clean up.
static char * make_reverse_hostname(const void *ip, int af)
Compute name to use for DNS reverse lookups from ip.
static void * connect_cb(void *cls, struct GNUNET_SERVICE_Client *c, struct GNUNET_MQ_Handle *mq)
Callback called when a client connects to the service.
static int resolve_and_cache(const char *hostname, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Initiate an active lookup, then cache the result and try to then complete the resolution.
static void add_host(const char *hostname, uint16_t rec_type, const void *data, size_t data_size)
Add information about a host from /etc/hosts to our cache.
static unsigned int cache_size
How many entries do we have in cache_head DLL?
#define DNS_TIMEOUT
How long do we wait for DNS answers?
static void handle_resolve_timeout(void *cls)
We encountered a timeout trying to perform a DNS lookup.
static void load_etc_hosts(void)
Reads the list of hosts from /etc/hosts.
static struct ResolveCache * hosts_tail
Tail of the linked list of DNS lookup results from /etc/hosts.
static void free_hosts_entry(struct ResolveCache *rc)
Remove entry from cache.
static struct ActiveLookup * lookup_tail
Tail of the linked list of active DNS lookups.
static char * extract_search_domain(const char *line, size_t line_len)
Find out if the configuration file line contains a string starting with "search ",...
static int check_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *get)
Verify well-formedness of GET-message.
static void free_active_lookup(struct ActiveLookup *al)
Release resources associated with al.
static void extract_hosts(const char *line, size_t line_len)
Extract host information from a line in /etc/hosts.
static int lookup_dns_servers(char ***server_addrs)
Reads the list of nameservers from /etc/resolve.conf.
static void handle_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *msg)
Handle GET-message.
GNUNET_SERVICE_MAIN(GNUNET_OS_project_data_gnunet(), "resolver", GNUNET_SERVICE_OPTION_NONE, &init_cb, &connect_cb, &disconnect_cb, NULL, GNUNET_MQ_hd_var_size(get, GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST, struct GNUNET_RESOLVER_GetMessage, NULL), GNUNET_MQ_handler_end())
Define "main" method using service macro.
static void process_get(const char *hostname, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Process DNS request for hostname with request ID request_id from client demanding records of type rec...
static void handle_resolve_result(void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t dns_len)
We got a result from DNS.
static int try_cache(const char *hostname, uint16_t record_type, uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
Get an IP address as a string (works for both IPv4 and IPv6).
static struct ResolveCache * cache_tail
Tail of the linked list of cached DNS lookup results.
static struct GNUNET_DNSSTUB_Context * dnsstub_ctx
context of dnsstub library
static unsigned int records
Number of records we found.
struct GNUNET_PQ_ResultSpec __attribute__
Constants for network protocols.
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
@ GNUNET_CRYPTO_QUALITY_NONCE
Randomness for IVs etc.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1215
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1289
void * GNUNET_DISK_file_map(const struct GNUNET_DISK_FileHandle *h, struct GNUNET_DISK_MapHandle **m, enum GNUNET_DISK_MapType access, size_t len)
Map a file into memory.
Definition: disk.c:1361
enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition: disk.c:193
enum GNUNET_GenericReturnValue GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
Definition: disk.c:1392
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
@ GNUNET_DISK_MAP_TYPE_READ
Read-only memory map.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
void GNUNET_DNSPARSER_free_packet(struct GNUNET_DNSPARSER_Packet *p)
Free memory taken by a packet.
Definition: dnsparser.c:950
#define GNUNET_DNSPARSER_TYPE_ALL
void GNUNET_DNSPARSER_free_record(struct GNUNET_DNSPARSER_Record *r)
Free the given DNS record.
Definition: dnsparser.c:184
#define GNUNET_DNSPARSER_TYPE_A
#define GNUNET_DNSPARSER_TYPE_PTR
#define GNUNET_DNSPARSER_TYPE_CNAME
#define GNUNET_DNSPARSER_TYPE_AAAA
int GNUNET_DNSPARSER_pack(const struct GNUNET_DNSPARSER_Packet *p, uint16_t max, char **buf, size_t *buf_length)
Given a DNS packet p, generate the corresponding UDP payload.
Definition: dnsparser.c:1400
struct GNUNET_DNSPARSER_Packet * GNUNET_DNSPARSER_parse(const char *udp_payload, size_t udp_payload_length)
Parse a UDP payload of a DNS packet in to a nice struct for further processing and manipulation.
Definition: dnsparser.c:729
struct GNUNET_DNSPARSER_Record * GNUNET_DNSPARSER_duplicate_record(const struct GNUNET_DNSPARSER_Record *r)
Duplicate (deep-copy) the given DNS record.
Definition: dnsparser.c:810
struct GNUNET_DNSSTUB_Context * GNUNET_DNSSTUB_start(unsigned int num_sockets)
Start a DNS stub resolver.
Definition: dnsstub.c:586
int GNUNET_DNSSTUB_add_dns_ip(struct GNUNET_DNSSTUB_Context *ctx, const char *dns_ip)
Add nameserver for use by the DNSSTUB.
Definition: dnsstub.c:613
void GNUNET_DNSSTUB_stop(struct GNUNET_DNSSTUB_Context *ctx)
Cleanup DNSSTUB resolver.
Definition: dnsstub.c:705
struct GNUNET_DNSSTUB_RequestSocket * GNUNET_DNSSTUB_resolve(struct GNUNET_DNSSTUB_Context *ctx, const void *request, size_t request_len, GNUNET_DNSSTUB_ResultCallback rc, void *rc_cls)
Perform DNS resolution using our default IP from init.
Definition: dnsstub.c:526
void GNUNET_DNSSTUB_resolve_cancel(struct GNUNET_DNSSTUB_RequestSocket *rs)
Cancel DNS resolution.
Definition: dnsstub.c:562
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:305
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_check_zero_termination(m)
Insert code for a "check_" function that verifies that a given variable-length message received over ...
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:61
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:76
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
#define GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST
Request DNS resolution.
#define GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE
Response to a DNS resolution request.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1339
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:980
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1277
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2418
struct GNUNET_MQ_Handle * GNUNET_SERVICE_client_get_mq(struct GNUNET_SERVICE_Client *c)
Obtain the message queue of c.
Definition: service.c:2500
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2389
@ GNUNET_SERVICE_OPTION_NONE
Use defaults.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define GNUNET_TUN_DNS_CLASS_INTERNET
A few common DNS classes (ok, only one is common, but I list a couple more to make it clear what we'r...
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
#define SIZE_MAX
Definition: platform.h:208
Information about pending lookups.
char * hostname
Which hostname are we resolving?
struct GNUNET_SERVICE_Client * client
The client that queried the records contained in this cache entry.
struct GNUNET_SCHEDULER_Task * timeout_task
handle for the resolution timeout task
uint16_t record_type
type of queried DNS record
uint16_t dns_id
Unique DNS request ID of a client if a query for this hostname/record_type is currently pending,...
struct ActiveLookup * next
Stored in a DLL.
uint32_t client_request_id
Unique request ID of a client if a query for this hostname/record_type is currently pending,...
int did_aaaa
If record_type is GNUNET_DNSPARSER_TYPE_ALL, did we go again for the AAAA records yet?
struct ActiveLookup * prev
Stored in a DLL.
struct GNUNET_DNSSTUB_RequestSocket * resolve_handle
handle for cancelling a request
Handle used to access files (and pipes).
Handle for a memory-mapping operation.
Definition: disk.c:1342
Easy-to-process, parsed version of a DNS packet.
struct GNUNET_DNSPARSER_Query * queries
Array of all queries in the packet, must contain "num_queries" entries.
unsigned int num_answers
Number of answers in the packet, should be 0 for queries.
struct GNUNET_TUN_DnsFlags flags
Bitfield of DNS flags.
struct GNUNET_DNSPARSER_Record * answers
Array of all answers in the packet, must contain "num_answers" entries.
unsigned int num_additional_records
Number of additional records in the packet, should be 0 for queries.
struct GNUNET_DNSPARSER_Record * additional_records
Array of all additional answers in the packet, must contain "num_additional_records" entries.
struct GNUNET_DNSPARSER_Record * authority_records
Array of all authority records in the packet, must contain "num_authority_records" entries.
unsigned int num_authority_records
Number of authoritative answers in the packet, should be 0 for queries.
unsigned int num_queries
Number of queries in the packet.
uint16_t id
DNS ID (to match replies to requests).
uint16_t dns_traffic_class
See GNUNET_TUN_DNS_CLASS_*.
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
char * name
Name of the record that the query is for (0-terminated).
void * data
Binary record data.
size_t data_len
Number of bytes in data.
A DNS response record.
uint16_t dns_traffic_class
See GNUNET_TUN_DNS_CLASS_*.
char * hostname
For NS, CNAME and PTR records, this is the uncompressed 0-terminated hostname.
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
struct GNUNET_TIME_Absolute expiration_time
When does the record expire?
char * name
Name of the record that the query is for (0-terminated).
struct GNUNET_DNSPARSER_RawRecord raw
Raw data for all other types.
union GNUNET_DNSPARSER_Record::@20 data
Payload of the record (which one of these is valid depends on the 'type').
Handle to the stub resolver.
Definition: dnsstub.c:125
UDP socket we are using for sending DNS requests to the Internet.
Definition: dnsstub.c:46
Handle to a message queue.
Definition: mq.c:87
Request for the resolver.
Definition: resolver.h:44
Entry in list of pending tasks.
Definition: scheduler.c:136
Handle to a client that is connected to a service.
Definition: service.c:249
Handle to a service.
Definition: service.c:116
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
unsigned int recursion_desired
Set to 1 if recursion is desired (client -> server)
Entry in list of cached DNS records for a hostname.
struct RecordListEntry * next
This is a doubly linked list.
struct GNUNET_DNSPARSER_Record * record
Cached data.
struct RecordListEntry * prev
This is a doubly linked list.
A cached DNS lookup result.
struct RecordListEntry * records_tail
tail of a double linked list containing the lookup results
struct ResolveCache * next
This is a doubly linked list.
struct RecordListEntry * records_head
head of a double linked list containing the lookup results
char * hostname
Which hostname is this cache for?
struct ResolveCache * prev
This is a doubly linked list.