GNUnet  0.10.x
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 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
30 #include "resolver.h"
31 
32 
36 #define DNS_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
37 
41 #define MAX_CACHE 1024
42 
51 
56 
61 };
62 
63 
67 struct ResolveCache {
71  struct ResolveCache *next;
72 
76  struct ResolveCache *prev;
77 
81  char *hostname;
82 
87 
92 };
93 
94 
98 struct ActiveLookup {
103 
108 
113 
118 
123 
127  char *hostname;
128 
133  int did_aaaa;
134 
138  uint16_t record_type;
139 
145 
150  uint16_t dns_id;
151 };
152 
153 
157 static struct ResolveCache *cache_head;
158 
162 static struct ResolveCache *cache_tail;
163 
167 static struct ResolveCache *hosts_head;
168 
172 static struct ResolveCache *hosts_tail;
173 
177 static struct ActiveLookup *lookup_head;
178 
182 static struct ActiveLookup *lookup_tail;
183 
188 
192 static char *my_domain;
193 
197 static unsigned int cache_size;
198 
199 
205 static void
207 {
208  struct RecordListEntry *pos;
209 
210  while (NULL != (pos = rc->records_head))
211  {
214  GNUNET_free(pos->record);
215  GNUNET_free(pos);
216  }
218  GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, rc);
219  cache_size--;
220  GNUNET_free(rc);
221 }
222 
223 
229 static void
231 {
232  struct RecordListEntry *pos;
233 
234  while (NULL != (pos = rc->records_head))
235  {
238  GNUNET_free(pos->record);
239  GNUNET_free(pos);
240  }
242  GNUNET_CONTAINER_DLL_remove(hosts_head, hosts_tail, rc);
243  cache_size--;
244  GNUNET_free(rc);
245 }
246 
247 
253 static void
255 {
256  GNUNET_CONTAINER_DLL_remove(lookup_head, lookup_tail, al);
257  if (NULL != al->resolve_handle)
258  {
260  al->resolve_handle = NULL;
261  }
262  if (NULL != al->timeout_task)
263  {
265  al->timeout_task = NULL;
266  }
268  GNUNET_free(al);
269 }
270 
271 
281 static char *
282 extract_dns_server(const char *line, size_t line_len)
283 {
284  if (0 == strncmp(line, "nameserver ", strlen("nameserver ")))
285  return GNUNET_strndup(line + strlen("nameserver "),
286  line_len - strlen("nameserver "));
287  return NULL;
288 }
289 
290 
300 static char *
301 extract_search_domain(const char *line, size_t line_len)
302 {
303  if (0 == strncmp(line, "search ", strlen("search ")))
304  return GNUNET_strndup(line + strlen("search "),
305  line_len - strlen("search "));
306  return NULL;
307 }
308 
309 
316 static int
317 lookup_dns_servers(char ***server_addrs)
318 {
319  struct GNUNET_DISK_FileHandle *fh;
320  struct GNUNET_DISK_MapHandle *mh;
321  off_t bytes_read;
322  const char *buf;
323  size_t read_offset;
324  unsigned int num_dns_servers;
325 
326  fh = GNUNET_DISK_file_open("/etc/resolv.conf",
329  if (NULL == fh)
330  {
332  "Could not open /etc/resolv.conf. "
333  "DNS resolution will not be possible.\n");
334  return -1;
335  }
336  if (GNUNET_OK != GNUNET_DISK_file_handle_size(fh, &bytes_read))
337  {
339  "Could not determine size of /etc/resolv.conf. "
340  "DNS resolution will not be possible.\n");
342  return -1;
343  }
344  if (((unsigned long long)bytes_read) > (unsigned long long)SIZE_MAX)
345  {
347  "/etc/resolv.conf file too large to mmap. "
348  "DNS resolution will not be possible.\n");
350  return -1;
351  }
352  buf = GNUNET_DISK_file_map(fh,
353  &mh,
355  (size_t)bytes_read);
356  *server_addrs = NULL;
357  read_offset = 0;
358  num_dns_servers = 0;
359  while (read_offset < (size_t)bytes_read)
360  {
361  const char *newline;
362  size_t line_len;
363  char *dns_server;
364 
365  newline = strchr(buf + read_offset, '\n');
366  if (NULL == newline)
367  break;
368  line_len = newline - buf - read_offset;
369  dns_server = extract_dns_server(buf + read_offset, line_len);
370  if (NULL != dns_server)
371  {
372  GNUNET_array_append(*server_addrs, num_dns_servers, dns_server);
373  }
374  else if (NULL == my_domain)
375  {
376  my_domain = extract_search_domain(buf + read_offset, line_len);
377  }
378  read_offset += line_len + 1;
379  }
382  return (int)num_dns_servers;
383 }
384 
385 
392 static char *
393 make_reverse_hostname(const void *ip, int af)
394 {
395  char *buf = GNUNET_new_array(80, char);
396  int pos = 0;
397 
398  if (AF_INET == af)
399  {
400  struct in_addr *addr = (struct in_addr *)ip;
401  uint32_t ip_int = addr->s_addr;
402 
403  for (int i = 3; i >= 0; i--)
404  {
405  int n =
406  GNUNET_snprintf(buf + pos, 80 - pos, "%u.", ((uint8_t *)&ip_int)[i]);
407  if (n < 0)
408  {
409  GNUNET_free(buf);
410  return NULL;
411  }
412  pos += n;
413  }
414  pos += GNUNET_snprintf(buf + pos, 80 - pos, "in-addr.arpa");
415  }
416  else if (AF_INET6 == af)
417  {
418  struct in6_addr *addr = (struct in6_addr *)ip;
419  for (int i = 15; i >= 0; i--)
420  {
421  int n =
422  GNUNET_snprintf(buf + pos, 80 - pos, "%x.", addr->s6_addr[i] & 0xf);
423  if (n < 0)
424  {
425  GNUNET_free(buf);
426  return NULL;
427  }
428  pos += n;
429  n = GNUNET_snprintf(buf + pos, 80 - pos, "%x.", addr->s6_addr[i] >> 4);
430  if (n < 0)
431  {
432  GNUNET_free(buf);
433  return NULL;
434  }
435  pos += n;
436  }
437  pos += GNUNET_snprintf(buf + pos, 80 - pos, "ip6.arpa");
438  }
439  buf[pos] = '\0';
440  return buf;
441 }
442 
443 
455 static int
457  uint16_t record_type,
458  uint32_t client_request_id,
459  struct GNUNET_SERVICE_Client *client)
460 {
462  struct GNUNET_MQ_Envelope *env;
463  const void *payload;
464  size_t payload_len;
465 
466  switch (record->type)
467  {
469  if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
470  return GNUNET_NO;
471  payload = record->data.hostname;
472  payload_len = strlen(record->data.hostname) + 1;
473  break;
474 
476  if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
477  return GNUNET_NO;
478  payload = record->data.hostname;
479  payload_len = strlen(record->data.hostname) + 1;
480  break;
481 
483  if ((GNUNET_DNSPARSER_TYPE_A != record_type) &&
484  (GNUNET_DNSPARSER_TYPE_ALL != record_type))
485  return GNUNET_NO;
486  payload = record->data.raw.data;
487  payload_len = record->data.raw.data_len;
488  break;
489 
491  if ((GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
492  (GNUNET_DNSPARSER_TYPE_ALL != record_type))
493  return GNUNET_NO;
494  payload = record->data.raw.data;
495  payload_len = record->data.raw.data_len;
496  break;
497 
498  default:
500  "Cannot handle DNS response type %u: not supported here\n",
501  record->type);
502  return GNUNET_NO;
503  }
504  env = GNUNET_MQ_msg_extra(msg,
505  payload_len,
507  msg->client_id = client_request_id;
508  GNUNET_memcpy(&msg[1], payload, payload_len);
510  return GNUNET_YES;
511 }
512 
513 
521 static void
522 send_end_msg(uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
523 {
525  struct GNUNET_MQ_Envelope *env;
526 
527  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending END message\n");
529  msg->client_id = client_request_id;
531 }
532 
533 
541 static int
543 {
545  struct RecordListEntry *n;
546 
547  for (struct RecordListEntry *pos = rc->records_head; NULL != pos; pos = n)
548  {
549  n = pos->next;
550  if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
551  {
553  GNUNET_DNSPARSER_free_record(pos->record);
554  GNUNET_free(pos->record);
555  GNUNET_free(pos);
556  }
557  }
558  if (NULL == rc->records_head)
559  {
560  free_cache_entry(rc);
561  return GNUNET_YES;
562  }
563  return GNUNET_NO;
564 }
565 
566 
576 static void
577 process_get(const char *hostname,
578  uint16_t record_type,
579  uint32_t client_request_id,
580  struct GNUNET_SERVICE_Client *client);
581 
582 
594 static int
595 try_cache(const char *hostname,
596  uint16_t record_type,
597  uint32_t client_request_id,
598  struct GNUNET_SERVICE_Client *client)
599 {
600  struct ResolveCache *pos;
601  struct ResolveCache *next;
602  int found;
603  int in_hosts;
604 
605  in_hosts = GNUNET_NO;
606  for (pos = hosts_head; NULL != pos; pos = pos->next)
607  if (0 == strcmp(pos->hostname, hostname))
608  {
609  in_hosts = GNUNET_YES;
610  break;
611  }
612  if (NULL == pos)
613  {
614  next = cache_head;
615  for (pos = next; NULL != pos; pos = next)
616  {
617  next = pos->next;
618  if (GNUNET_YES == remove_expired(pos))
619  continue;
620  if (0 == strcmp(pos->hostname, hostname))
621  break;
622  }
623  }
624  if (NULL == pos)
625  {
626  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "No cache entry for '%s'\n", hostname);
627  return GNUNET_NO;
628  }
629  if ((GNUNET_NO == in_hosts) && (cache_head != pos))
630  {
631  /* move result to head to achieve LRU for cache eviction */
632  GNUNET_CONTAINER_DLL_remove(cache_head, cache_tail, pos);
633  GNUNET_CONTAINER_DLL_insert(cache_head, cache_tail, pos);
634  }
635  found = GNUNET_NO;
636  for (struct RecordListEntry *rle = pos->records_head; NULL != rle;
637  rle = rle->next)
638  {
639  const struct GNUNET_DNSPARSER_Record *record = rle->record;
640 
642  "Found cache entry for '%s', record type '%u'\n",
643  hostname,
644  record_type);
645  if ((GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
646  (GNUNET_DNSPARSER_TYPE_CNAME != record_type) && (GNUNET_NO == found))
647  {
648  const char *hostname = record->data.hostname;
649 
650  process_get(hostname, record_type, client_request_id, client);
651  return GNUNET_YES; /* counts as a cache "hit" */
652  }
653  found |= send_reply(rle->record, record_type, client_request_id, client);
654  }
655  if (GNUNET_NO == found)
656  return GNUNET_NO; /* had records, but none matched! */
657  send_end_msg(client_request_id, client);
658  return GNUNET_YES;
659 }
660 
661 
673 static int
674 pack(const char *hostname,
675  uint16_t type,
676  uint16_t dns_id,
677  char **packet_buf,
678  size_t *packet_size)
679 {
680  struct GNUNET_DNSPARSER_Query query;
681  struct GNUNET_DNSPARSER_Packet packet;
682 
683  query.name = (char *)hostname;
684  query.type = type;
686  memset(&packet, 0, sizeof(packet));
687  packet.num_queries = 1;
688  packet.queries = &query;
689  packet.id = htons(dns_id);
690  packet.flags.recursion_desired = 1;
691  if (GNUNET_OK !=
692  GNUNET_DNSPARSER_pack(&packet, UINT16_MAX, packet_buf, packet_size))
693  {
695  "Failed to pack query for hostname `%s'\n",
696  hostname);
697  packet_buf = NULL;
698  return GNUNET_SYSERR;
699  }
700  return GNUNET_OK;
701 }
702 
703 static void
704 cache_answers(const char *name,
706  unsigned int num_records)
707 {
708  struct ResolveCache *rc;
710  struct RecordListEntry *rle;
711 
712  for (unsigned int i = 0; i != num_records; i++)
713  {
714  record = &records[i];
715 
716  for (rc = cache_head; NULL != rc; rc = rc->next)
717  if (0 == strcasecmp(rc->hostname, name))
718  break;
719  if (NULL == rc)
720  {
721  rc = GNUNET_new(struct ResolveCache);
722  rc->hostname = GNUNET_strdup(name);
723  GNUNET_CONTAINER_DLL_insert(cache_head, cache_tail, rc);
724  cache_size++;
725  }
726  /* TODO: ought to check first if we have this exact record
727  already in the cache! */
728  rle = GNUNET_new(struct RecordListEntry);
731  }
732 }
733 
742 static void
744  const struct GNUNET_TUN_DnsHeader *dns,
745  size_t dns_len)
746 {
747  struct ActiveLookup *al = cls;
748  struct GNUNET_DNSPARSER_Packet *parsed;
749 
750  parsed = GNUNET_DNSPARSER_parse((const char *)dns, dns_len);
751  if (NULL == parsed)
752  {
754  "Failed to parse DNS reply (hostname %s, request ID %u)\n",
755  al->hostname,
756  al->dns_id);
757  return;
758  }
759  if (al->dns_id != ntohs(parsed->id))
760  {
762  "Request ID in DNS reply does not match\n");
764  return;
765  }
766  if (0 == parsed->num_answers + parsed->num_authority_records +
767  parsed->num_additional_records)
768  {
770  "DNS reply (hostname %s, request ID %u) contains no answers\n",
771  al->hostname,
772  (unsigned int)al->client_request_id);
773  /* resume by trying again from cache */
774  if (GNUNET_NO == try_cache(al->hostname,
775  al->record_type,
776  al->client_request_id,
777  al->client))
778  /* cache failed, tell client we could not get an answer */
779  {
781  }
783  free_active_lookup(al);
784  return;
785  }
786  /* LRU-based cache eviction: we remove from tail */
787  while (cache_size > MAX_CACHE)
788  free_cache_entry(cache_tail);
789 
791  "Got reply for hostname %s and request ID %u\n",
792  al->hostname,
793  (unsigned int)al->client_request_id);
794  /* add to cache */
795  cache_answers(al->hostname, parsed->answers, parsed->num_answers);
797  parsed->authority_records,
798  parsed->num_authority_records);
800  parsed->additional_records,
801  parsed->num_additional_records);
802 
803  /* see if we need to do the 2nd request for AAAA records */
804  if ((GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
805  (GNUNET_NO == al->did_aaaa))
806  {
807  char *packet_buf;
808  size_t packet_size;
809  uint16_t dns_id;
810 
812  UINT16_MAX);
813  if (GNUNET_OK == pack(al->hostname,
815  dns_id,
816  &packet_buf,
817  &packet_size))
818  {
819  al->did_aaaa = GNUNET_YES;
820  al->dns_id = dns_id;
822  al->resolve_handle = GNUNET_DNSSTUB_resolve(dnsstub_ctx,
823  packet_buf,
824  packet_size,
826  al);
827  GNUNET_free(packet_buf);
829  return;
830  }
831  }
832 
833  /* resume by trying again from cache */
834  if (GNUNET_NO == try_cache(al->hostname,
835  al->record_type,
836  al->client_request_id,
837  al->client))
838  /* cache failed, tell client we could not get an answer */
839  {
841  }
842  free_active_lookup(al);
844 }
845 
846 
853 static void
855 {
856  struct ActiveLookup *al = cls;
857 
858  al->timeout_task = NULL;
859  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DNS lookup timeout!\n");
861  free_active_lookup(al);
862 }
863 
864 
875 static int
877  uint16_t record_type,
878  uint32_t client_request_id,
879  struct GNUNET_SERVICE_Client *client)
880 {
881  char *packet_buf;
882  size_t packet_size;
883  struct ActiveLookup *al;
884  uint16_t dns_id;
885  uint16_t type;
886 
887  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "resolve_and_cache `%s'\n", hostname);
889  UINT16_MAX);
890 
891  if (GNUNET_DNSPARSER_TYPE_ALL == record_type)
893  else
894  type = record_type;
895  if (GNUNET_OK != pack(hostname, type, dns_id, &packet_buf, &packet_size))
896  {
898  "Failed to pack query for hostname `%s'\n",
899  hostname);
900  return GNUNET_SYSERR;
901  }
902 
903  al = GNUNET_new(struct ActiveLookup);
904  al->hostname = GNUNET_strdup(hostname);
905  al->record_type = record_type;
907  al->dns_id = dns_id;
908  al->client = client;
909  al->timeout_task =
911  al->resolve_handle = GNUNET_DNSSTUB_resolve(dnsstub_ctx,
912  packet_buf,
913  packet_size,
915  al);
916  GNUNET_free(packet_buf);
917  GNUNET_CONTAINER_DLL_insert(lookup_head, lookup_tail, al);
919  "Resolving %s, client_request_id = %u, dns_id = %u\n",
920  hostname,
921  (unsigned int)client_request_id,
922  (unsigned int)dns_id);
923  return GNUNET_OK;
924 }
925 
926 
936 static void
937 process_get(const char *hostname,
938  uint16_t record_type,
939  uint32_t client_request_id,
940  struct GNUNET_SERVICE_Client *client)
941 {
942  char fqdn[255];
943 
944  if (GNUNET_NO != try_cache(hostname, record_type, client_request_id, client))
945  return;
946  if ((NULL != my_domain) && (NULL == strchr(hostname, (unsigned char)'.')) &&
947  (strlen(hostname) + strlen(my_domain) <= 253))
948  {
949  GNUNET_snprintf(fqdn, sizeof(fqdn), "%s.%s", hostname, my_domain);
950  }
951  else if (strlen(hostname) < 255)
952  {
953  GNUNET_snprintf(fqdn, sizeof(fqdn), "%s", hostname);
954  }
955  else
956  {
957  GNUNET_break(0);
959  return;
960  }
961  if (GNUNET_NO == try_cache(fqdn, record_type, client_request_id, client))
962  {
963  if (GNUNET_OK !=
964  resolve_and_cache(fqdn, record_type, client_request_id, client))
965  {
966  send_end_msg(client_request_id, client);
967  }
968  }
969 }
970 
971 
979 static int
980 check_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *get)
981 {
982  uint16_t size;
983  int direction;
984  int af;
985 
986  (void)cls;
987  size = ntohs(get->header.size) - sizeof(*get);
988  direction = ntohl(get->direction);
989  if (GNUNET_NO == direction)
990  {
992  return GNUNET_OK;
993  }
994  af = ntohl(get->af);
995  switch (af)
996  {
997  case AF_INET:
998  if (size != sizeof(struct in_addr))
999  {
1000  GNUNET_break(0);
1001  return GNUNET_SYSERR;
1002  }
1003  break;
1004 
1005  case AF_INET6:
1006  if (size != sizeof(struct in6_addr))
1007  {
1008  GNUNET_break(0);
1009  return GNUNET_SYSERR;
1010  }
1011  break;
1012 
1013  default:
1014  GNUNET_break(0);
1015  return GNUNET_SYSERR;
1016  }
1017  return GNUNET_OK;
1018 }
1019 
1020 
1027 static void
1028 handle_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *msg)
1029 {
1030  struct GNUNET_SERVICE_Client *client = cls;
1031  int direction;
1032  int af;
1033  uint32_t client_request_id;
1034  char *hostname;
1035 
1036  direction = ntohl(msg->direction);
1037  af = ntohl(msg->af);
1038  client_request_id = msg->client_id;
1040  if (GNUNET_NO == direction)
1041  {
1042  /* IP from hostname */
1043  hostname = GNUNET_strdup((const char *)&msg[1]);
1045  "Client asks to resolve `%s'\n",
1046  hostname);
1047  switch (af)
1048  {
1049  case AF_UNSPEC: {
1050  process_get(hostname,
1052  client_request_id,
1053  client);
1054  break;
1055  }
1056 
1057  case AF_INET: {
1058  process_get(hostname,
1060  client_request_id,
1061  client);
1062  break;
1063  }
1064 
1065  case AF_INET6: {
1066  process_get(hostname,
1068  client_request_id,
1069  client);
1070  break;
1071  }
1072 
1073  default: {
1074  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got invalid af: %d\n", af);
1075  GNUNET_assert(0);
1076  }
1077  }
1078  }
1079  else
1080  {
1081  /* hostname from IP */
1082  hostname = make_reverse_hostname(&msg[1], af);
1083  process_get(hostname,
1085  client_request_id,
1086  client);
1087  }
1088  GNUNET_free_non_null(hostname);
1089 }
1090 
1091 
1097 static void
1098 shutdown_task(void *cls)
1099 {
1100  (void)cls;
1101 
1102  while (NULL != lookup_head)
1103  free_active_lookup(lookup_head);
1104  while (NULL != cache_head)
1105  free_cache_entry(cache_head);
1106  while (NULL != hosts_head)
1107  free_hosts_entry(hosts_head);
1108  GNUNET_DNSSTUB_stop(dnsstub_ctx);
1110 }
1111 
1112 
1122 static void
1123 add_host(const char *hostname,
1124  uint16_t rec_type,
1125  const void *data,
1126  size_t data_size)
1127 {
1128  struct ResolveCache *rc;
1129  struct RecordListEntry *rle;
1130  struct GNUNET_DNSPARSER_Record *rec;
1131 
1132  rec = GNUNET_malloc(sizeof(struct GNUNET_DNSPARSER_Record));
1134  rec->type = rec_type;
1136  rec->name = GNUNET_strdup(hostname);
1137  rec->data.raw.data = GNUNET_memdup(data, data_size);
1138  rec->data.raw.data_len = data_size;
1139  rle = GNUNET_new(struct RecordListEntry);
1140  rle->record = rec;
1141  rc = GNUNET_new(struct ResolveCache);
1142  rc->hostname = GNUNET_strdup(hostname);
1144  GNUNET_CONTAINER_DLL_insert(hosts_head, hosts_tail, rc);
1145 }
1146 
1147 
1154 static void
1155 extract_hosts(const char *line, size_t line_len)
1156 {
1157  const char *c;
1158  struct in_addr v4;
1159  struct in6_addr v6;
1160  char *tbuf;
1161  char *tok;
1162 
1163  /* ignore everything after '#' */
1164  c = memrchr(line, (unsigned char)'#', line_len);
1165  if (NULL != c)
1166  line_len = c - line;
1167  /* ignore leading whitespace */
1168  while ((0 < line_len) && isspace((unsigned char)*line))
1169  {
1170  line++;
1171  line_len--;
1172  }
1173  tbuf = GNUNET_strndup(line, line_len);
1174  tok = strtok(tbuf, " \t");
1175  if (NULL == tok)
1176  {
1177  GNUNET_free(tbuf);
1178  return;
1179  }
1180  if (1 == inet_pton(AF_INET, tok, &v4))
1181  {
1182  while (NULL != (tok = strtok(NULL, " \t")))
1183  add_host(tok, GNUNET_DNSPARSER_TYPE_A, &v4, sizeof(struct in_addr));
1184  }
1185  else if (1 == inet_pton(AF_INET6, tok, &v6))
1186  {
1187  while (NULL != (tok = strtok(NULL, " \t")))
1188  add_host(tok, GNUNET_DNSPARSER_TYPE_AAAA, &v6, sizeof(struct in6_addr));
1189  }
1190  GNUNET_free(tbuf);
1191 }
1192 
1193 
1197 static void
1199 {
1200  struct GNUNET_DISK_FileHandle *fh;
1201  struct GNUNET_DISK_MapHandle *mh;
1202  off_t bytes_read;
1203  const char *buf;
1204  size_t read_offset;
1205 
1206  fh = GNUNET_DISK_file_open("/etc/hosts",
1209  if (NULL == fh)
1210  {
1211  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Failed to open /etc/hosts");
1212  return;
1213  }
1214  if (GNUNET_OK != GNUNET_DISK_file_handle_size(fh, &bytes_read))
1215  {
1217  "Could not determin size of /etc/hosts. "
1218  "DNS resolution will not be possible.\n");
1220  return;
1221  }
1222  if (((unsigned long long)bytes_read) > (unsigned long long)SIZE_MAX)
1223  {
1225  "/etc/hosts file too large to mmap. "
1226  "DNS resolution will not be possible.\n");
1228  return;
1229  }
1230  buf = GNUNET_DISK_file_map(fh,
1231  &mh,
1233  (size_t)bytes_read);
1234  read_offset = 0;
1235  while (read_offset < (size_t)bytes_read)
1236  {
1237  const char *newline;
1238  size_t line_len;
1239 
1240  newline = strchr(buf + read_offset, '\n');
1241  if (NULL == newline)
1242  break;
1243  line_len = newline - buf - read_offset;
1244  extract_hosts(buf + read_offset, line_len);
1245  read_offset += line_len + 1;
1246  }
1249 }
1250 
1251 
1259 static void
1260 init_cb(void *cls,
1261  const struct GNUNET_CONFIGURATION_Handle *cfg,
1262  struct GNUNET_SERVICE_Handle *sh)
1263 {
1264  char **dns_servers;
1265  int num_dns_servers;
1266 
1267  (void)cfg;
1268  (void)sh;
1269  load_etc_hosts();
1271  dnsstub_ctx = GNUNET_DNSSTUB_start(128);
1272  dns_servers = NULL;
1273  num_dns_servers = lookup_dns_servers(&dns_servers);
1274  if (0 >= num_dns_servers)
1275  {
1276  GNUNET_log(
1278  _("No DNS server available. DNS resolution will not be possible.\n"));
1279  return;
1280  }
1281  for (int i = 0; i < num_dns_servers; i++)
1282  {
1283  int result = GNUNET_DNSSTUB_add_dns_ip(dnsstub_ctx, dns_servers[i]);
1285  "Adding DNS server '%s': %s\n",
1286  dns_servers[i],
1287  GNUNET_OK == result ? "success" : "failure");
1288  GNUNET_free(dns_servers[i]);
1289  }
1290  GNUNET_free_non_null(dns_servers);
1291 }
1292 
1293 
1302 static void *
1303 connect_cb(void *cls,
1304  struct GNUNET_SERVICE_Client *c,
1305  struct GNUNET_MQ_Handle *mq)
1306 {
1307  (void)cls;
1308  (void)mq;
1309 
1310  return c;
1311 }
1312 
1313 
1321 static void
1322 disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
1323 {
1324  struct ActiveLookup *n;
1325 
1326  (void)cls;
1327 
1328  GNUNET_assert(c == internal_cls);
1329  n = lookup_head;
1330  for (struct ActiveLookup *al = n; NULL != al; al = n)
1331  {
1332  n = al->next;
1333  if (al->client == c)
1334  free_active_lookup(al);
1335  }
1336 }
1337 
1338 
1343  "resolver",
1345  &init_cb,
1346  &connect_cb,
1347  &disconnect_cb,
1348  NULL,
1352  NULL),
1354 
1355 
1356 #if defined(LINUX) && defined(__GLIBC__)
1357 #include <malloc.h>
1358 
1362 void __attribute__ ((constructor)) GNUNET_RESOLVER_memory_init()
1363 {
1364  mallopt(M_TRIM_THRESHOLD, 4 * 1024);
1365  mallopt(M_TOP_PAD, 1 * 1024);
1366  malloc_trim(0);
1367 }
1368 #endif
1369 
1370 
1371 /* end of gnunet-service-resolver.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static void load_etc_hosts(void)
Reads the list of hosts from /etc/hosts.
static char * make_reverse_hostname(const void *ip, int af)
Compute name to use for DNS reverse lookups from ip.
struct GNUNET_DNSPARSER_Record * answers
Array of all answers in the packet, must contain "num_answers" entries.
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
Open the file for reading.
static unsigned int cache_size
How many entries do we have in cache_head DLL?
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.
int32_t direction
GNUNET_YES to get hostname from IP, GNUNET_NO to get IP from hostname.
Definition: resolver.h:53
#define memrchr(s, c, n)
Definition: compat.h:48
uint16_t type
See GNUNET_DNSPARSER_TYPE_*.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1339
static void init_cb(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_SERVICE_Handle *sh)
Service is starting, initialize everything.
static struct ResolveCache * hosts_tail
Tail of the linked list of DNS lookup results from /etc/hosts.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
Handle to a service.
Definition: service.c:114
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.
struct RecordListEntry * records_head
head of a double linked list containing the lookup results
struct GNUNET_SERVICE_Client * client
The client that queried the records contained in this cache entry.
#define GNUNET_DNSPARSER_TYPE_CNAME
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
struct GNUNET_MQ_Handle * GNUNET_SERVICE_client_get_mq(struct GNUNET_SERVICE_Client *c)
Obtain the message queue of c.
Definition: service.c:2424
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
struct RecordListEntry * prev
This is a doubly linked list.
struct GNUNET_SCHEDULER_Task * timeout_task
handle for the resolution timeout task
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_DNSSTUB_stop(struct GNUNET_DNSSTUB_Context *ctx)
Cleanup DNSSTUB resolver.
Definition: dnsstub.c:687
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:1256
static struct GNUNET_CADET_Handle * mh
Cadet handle.
Definition: gnunet-cadet.c:92
void GNUNET_DNSPARSER_free_record(struct GNUNET_DNSPARSER_Record *r)
Free the given DNS record.
Definition: dnsparser.c:169
static struct GNUNET_DNSSTUB_Context * dnsstub_ctx
context of dnsstub library
static void shutdown_task(void *cls)
Service is shutting down, clean up.
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 "...
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:1433
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:588
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.
uint16_t id
DNS ID (to match replies to requests).
GNUNET_SERVICE_MAIN("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.
Nobody is allowed to do anything to the file.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
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.
void GNUNET_DNSPARSER_free_packet(struct GNUNET_DNSPARSER_Packet *p)
Free memory taken by a packet.
Definition: dnsparser.c:853
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct ResolveCache * next
This is a doubly linked list.
static void handle_resolve_result(void *cls, const struct GNUNET_TUN_DnsHeader *dns, size_t dns_len)
We got a result from DNS.
struct GNUNET_DNSPARSER_Record * additional_records
Array of all additional answers in the packet, must contain "num_additional_records" entries...
struct RecordListEntry * next
This is a doubly linked list.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
uint32_t client_id
identifies the request this message responds to.
Definition: resolver.h:81
unsigned int num_answers
Number of answers in the packet, should be 0 for queries.
uint64_t abs_value_us
The actual value.
#define SIZE_MAX
Definition: platform.h:215
#define DNS_TIMEOUT
How long do we wait for DNS answers?
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
A cached DNS lookup result.
struct RecordListEntry * records_tail
tail of a double linked list containing the lookup results
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_DNSPARSER_TYPE_PTR
static unsigned int records
Number of records we found.
uint16_t dns_traffic_class
See GNUNET_TUN_DNS_CLASS_*.
Handle to a client that is connected to a service.
Definition: service.c:246
struct ActiveLookup * prev
Stored in a DLL.
struct GNUNET_DNSPARSER_Record * record
Cached data.
uint16_t dns_id
Unique DNS request ID of a client if a query for this hostname/record_type is currently pending...
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).
#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:52
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:1237
uint32_t client_request_id
Unique request ID of a client if a query for this hostname/record_type is currently pending...
#define GNUNET_MQ_check_zero_termination(m)
Insert code for a "check_" function that verifies that a given variable-length message received over ...
static struct ActiveLookup * lookup_tail
Tail of the linked list of active DNS lookups.
char * name
Name of the record that the query is for (0-terminated).
char * hostname
For NS, CNAME and PTR records, this is the uncompressed 0-terminated hostname.
static char * line
Desired phone line (string to be converted to a hash).
uint16_t record_type
type of queried DNS record
unsigned int num_additional_records
Number of additional records in the packet, should be 0 for queries.
static void disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
Callback called when a client disconnected from the service.
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:656
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
struct GNUNET_DNSSTUB_Context * GNUNET_DNSSTUB_start(unsigned int num_sockets)
Start a DNS stub resolver.
Definition: dnsstub.c:561
static struct ResolveCache * hosts_head
Head of the linked list of DNS lookup results from /etc/hosts.
static struct SolverHandle * sh
static int remove_expired(struct ResolveCache *rc)
Remove expired entries from rc.
Read-only memory map.
static void free_hosts_entry(struct ResolveCache *rc)
Remove entry from cache.
int did_aaaa
If record_type is GNUNET_DNSPARSER_TYPE_ALL, did we go again for the AAAA records yet...
Randomness for IVs etc.
static char buf[2048]
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
static struct ResolveCache * cache_tail
Tail of the linked list of cached DNS lookup results.
unsigned int recursion_desired
Set to 1 if recursion is desired (client -> server)
static int result
Global testing status.
#define GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE
Response to a DNS resolution request.
static void handle_resolve_timeout(void *cls)
We encountered a timeout trying to perform a DNS lookup.
static int fh
Handle to the unique file.
A DNS response record.
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 ", and if so, return a copy of the machine&#39;s search domain.
Information about pending lookups.
struct GNUNET_DNSPARSER_Record * authority_records
Array of all authority records in the packet, must contain "num_authority_records" entries...
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.
struct GNUNET_DNSPARSER_Record * GNUNET_DNSPARSER_duplicate_record(const struct GNUNET_DNSPARSER_Record *r)
Duplicate (deep-copy) the given DNS record.
Definition: dnsparser.c:737
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2315
Handle to the stub resolver.
Definition: dnsstub.c:121
struct GNUNET_DNSPARSER_Query * queries
Array of all queries in the packet, must contain "num_queries" entries.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
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 cache_answers(const char *name, struct GNUNET_DNSPARSER_Record *records, unsigned int num_records)
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:501
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static struct ResolveCache * cache_head
Start of the linked list of cached DNS lookup results.
static unsigned int size
Size of the "table".
Definition: peer.c:66
size_t data_len
Number of bytes in data.
int GNUNET_DISK_file_unmap(struct GNUNET_DISK_MapHandle *h)
Unmap a file.
Definition: disk.c:1469
static int check_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *get)
Verify well-formedness of GET-message.
void * data
Binary record data.
const char * name
struct ResolveCache * prev
This is a doubly linked list.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
unsigned int num_authority_records
Number of authoritative answers in the packet, should be 0 for queries.
static unsigned long long payload
How much data are we currently storing in the database?
#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&#39;r...
static void handle_get(void *cls, const struct GNUNET_RESOLVER_GetMessage *msg)
Handle GET-message.
static struct ActiveLookup * lookup_head
Start of the linked list of active DNS lookups.
Handle to a message queue.
Definition: mq.c:84
struct GNUNET_TIME_Absolute expiration_time
When does the record expire?
int32_t af
Address family to use (AF_INET, AF_INET6 or AF_UNSPEC).
Definition: resolver.h:58
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
#define GNUNET_array_append(arr, size, element)
Append an element to a list (growing the list by one).
static void free_cache_entry(struct ResolveCache *rc)
Remove entry from cache.
enum RadiotapType __attribute__
static char * hostname
Our hostname; we give this to all the peers we start.
#define GNUNET_DNSPARSER_TYPE_AAAA
configuration data
Definition: configuration.c:83
void GNUNET_DNSSTUB_resolve_cancel(struct GNUNET_DNSSTUB_RequestSocket *rs)
Cancel DNS resolution.
Definition: dnsstub.c:537
#define GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST
Request DNS resolution.
uint32_t client_id
identifies the request and is contained in the response message.
Definition: resolver.h:64
Easy-to-process, parsed version of a DNS packet.
struct GNUNET_DNSSTUB_RequestSocket * resolve_handle
handle for cancelling a request
int GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
Definition: disk.c:203
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:131
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 int lookup_dns_servers(char ***server_addrs)
Reads the list of nameservers from /etc/resolve.conf.
uint16_t dns_traffic_class
See GNUNET_TUN_DNS_CLASS_*.
char * name
Name of the record that the query is for (0-terminated).
unsigned int num_queries
Number of queries in the packet.
char * hostname
Which hostname is this cache for?
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:77
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:351
static void extract_hosts(const char *line, size_t line_len)
Extract host information from a line in /etc/hosts.
union GNUNET_DNSPARSER_Record::@27 data
Payload of the record (which one of these is valid depends on the &#39;type&#39;).
struct GNUNET_TUN_DnsFlags flags
Bitfield of DNS flags.
Request for the resolver.
Definition: resolver.h:43
UDP socket we are using for sending DNS requests to the Internet.
Definition: dnsstub.c:44
struct ActiveLookup * next
Stored in a DLL.
uint32_t data
The data value.
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:1262
static void free_active_lookup(struct ActiveLookup *al)
Release resources associated with al.
static size_t data_size
Number of bytes in data.
Entry in list of cached DNS records for a hostname.
#define GNUNET_DNSPARSER_TYPE_ALL
struct GNUNET_DNSPARSER_RawRecord raw
Raw data for all other types.
#define GNUNET_DNSPARSER_TYPE_A
Handle used to access files (and pipes).
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2234
char * hostname
Which hostname are we resolving?
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define MAX_CACHE
Maximum number of hostnames we cache results for.
Handle for a memory-mapping operation.
Definition: disk.c:1406
#define GNUNET_free(ptr)
Wrapper around free.
static char * my_domain
My domain, to be appended to the hostname to get a FQDN.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956