fix rest; uncrustify
[oweals/gnunet.git] / src / util / gnunet-service-resolver.c
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
21 /**
22  * @file util/gnunet-service-resolver.c
23  * @brief code to do DNS resolution
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_statistics_service.h"
30 #include "resolver.h"
31
32
33 /**
34  * How long do we wait for DNS answers?
35  */
36 #define DNS_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37
38 /**
39  * Maximum number of hostnames we cache results for.
40  */
41 #define MAX_CACHE 1024
42
43 /**
44  * Entry in list of cached DNS records for a hostname.
45  */
46 struct RecordListEntry
47 {
48   /**
49    * This is a doubly linked list.
50    */
51   struct RecordListEntry *next;
52
53   /**
54    * This is a doubly linked list.
55    */
56   struct RecordListEntry *prev;
57
58   /**
59    * Cached data.
60    */
61   struct GNUNET_DNSPARSER_Record *record;
62 };
63
64
65 /**
66  * A cached DNS lookup result.
67  */
68 struct ResolveCache
69 {
70   /**
71    * This is a doubly linked list.
72    */
73   struct ResolveCache *next;
74
75   /**
76    * This is a doubly linked list.
77    */
78   struct ResolveCache *prev;
79
80   /**
81    * Which hostname is this cache for?
82    */
83   char *hostname;
84
85   /**
86    * head of a double linked list containing the lookup results
87    */
88   struct RecordListEntry *records_head;
89
90   /**
91    * tail of a double linked list containing the lookup results
92    */
93   struct RecordListEntry *records_tail;
94 };
95
96
97 /**
98  * Information about pending lookups.
99  */
100 struct ActiveLookup
101 {
102   /**
103    * Stored in a DLL.
104    */
105   struct ActiveLookup *next;
106
107   /**
108    * Stored in a DLL.
109    */
110   struct ActiveLookup *prev;
111
112   /**
113    * The client that queried the records contained in this cache entry.
114    */
115   struct GNUNET_SERVICE_Client *client;
116
117   /**
118    * handle for cancelling a request
119    */
120   struct GNUNET_DNSSTUB_RequestSocket *resolve_handle;
121
122   /**
123    * handle for the resolution timeout task
124    */
125   struct GNUNET_SCHEDULER_Task *timeout_task;
126
127   /**
128    * Which hostname are we resolving?
129    */
130   char *hostname;
131
132   /**
133    * If @a record_type is #GNUNET_DNSPARSER_TYPE_ALL, did we go again
134    * for the AAAA records yet?
135    */
136   int did_aaaa;
137
138   /**
139    * type of queried DNS record
140    */
141   uint16_t record_type;
142
143   /**
144    * Unique request ID of a client if a query for this hostname/record_type
145    * is currently pending, undefined otherwise.
146    */
147   uint32_t client_request_id;
148
149   /**
150    * Unique DNS request ID of a client if a query for this hostname/record_type
151    * is currently pending, undefined otherwise.
152    */
153   uint16_t dns_id;
154 };
155
156
157 /**
158  * Start of the linked list of cached DNS lookup results.
159  */
160 static struct ResolveCache *cache_head;
161
162 /**
163  * Tail of the linked list of cached DNS lookup results.
164  */
165 static struct ResolveCache *cache_tail;
166
167 /**
168  * Head of the linked list of DNS lookup results from /etc/hosts.
169  */
170 static struct ResolveCache *hosts_head;
171
172 /**
173  * Tail of the linked list of DNS lookup results from /etc/hosts.
174  */
175 static struct ResolveCache *hosts_tail;
176
177 /**
178  * Start of the linked list of active DNS lookups.
179  */
180 static struct ActiveLookup *lookup_head;
181
182 /**
183  * Tail of the linked list of active DNS lookups.
184  */
185 static struct ActiveLookup *lookup_tail;
186
187 /**
188  * context of dnsstub library
189  */
190 static struct GNUNET_DNSSTUB_Context *dnsstub_ctx;
191
192 /**
193  * My domain, to be appended to the hostname to get a FQDN.
194  */
195 static char *my_domain;
196
197 /**
198  * How many entries do we have in #cache_head DLL?
199  */
200 static unsigned int cache_size;
201
202
203 /**
204  * Remove @a entry from cache.
205  *
206  * @param rc entry to free
207  */
208 static void
209 free_cache_entry (struct ResolveCache *rc)
210 {
211   struct RecordListEntry *pos;
212
213   while (NULL != (pos = rc->records_head))
214   {
215     GNUNET_CONTAINER_DLL_remove (rc->records_head, rc->records_tail, pos);
216     GNUNET_DNSPARSER_free_record (pos->record);
217     GNUNET_free (pos->record);
218     GNUNET_free (pos);
219   }
220   GNUNET_free_non_null (rc->hostname);
221   GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, rc);
222   cache_size--;
223   GNUNET_free (rc);
224 }
225
226
227 /**
228  * Remove @a entry from cache.
229  *
230  * @param rc entry to free
231  */
232 static void
233 free_hosts_entry (struct ResolveCache *rc)
234 {
235   struct RecordListEntry *pos;
236
237   while (NULL != (pos = rc->records_head))
238   {
239     GNUNET_CONTAINER_DLL_remove (rc->records_head, rc->records_tail, pos);
240     GNUNET_DNSPARSER_free_record (pos->record);
241     GNUNET_free (pos->record);
242     GNUNET_free (pos);
243   }
244   GNUNET_free_non_null (rc->hostname);
245   GNUNET_CONTAINER_DLL_remove (hosts_head, hosts_tail, rc);
246   cache_size--;
247   GNUNET_free (rc);
248 }
249
250
251 /**
252  * Release resources associated with @a al
253  *
254  * @param al an active lookup
255  */
256 static void
257 free_active_lookup (struct ActiveLookup *al)
258 {
259   GNUNET_CONTAINER_DLL_remove (lookup_head, lookup_tail, al);
260   if (NULL != al->resolve_handle)
261   {
262     GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
263     al->resolve_handle = NULL;
264   }
265   if (NULL != al->timeout_task)
266   {
267     GNUNET_SCHEDULER_cancel (al->timeout_task);
268     al->timeout_task = NULL;
269   }
270   GNUNET_free_non_null (al->hostname);
271   GNUNET_free (al);
272 }
273
274
275 /**
276  * Find out if the configuration file line contains a string
277  * starting with "nameserver ", and if so, return a copy of
278  * the nameserver's IP.
279  *
280  * @param line line to parse
281  * @param line_len number of characters in @a line
282  * @return NULL if no nameserver is configured in this @a line
283  */
284 static char *
285 extract_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
294 /**
295  * Find out if the configuration file line contains a string
296  * starting with "search ", and if so, return a copy of
297  * the machine's search domain.
298  *
299  * @param line line to parse
300  * @param line_len number of characters in @a line
301  * @return NULL if no nameserver is configured in this @a line
302  */
303 static char *
304 extract_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
313 /**
314  * Reads the list of nameservers from /etc/resolve.conf
315  *
316  * @param server_addrs[out] a list of null-terminated server address strings
317  * @return the number of server addresses in @server_addrs, -1 on error
318  */
319 static int
320 lookup_dns_servers (char ***server_addrs)
321 {
322   struct GNUNET_DISK_FileHandle *fh;
323   struct GNUNET_DISK_MapHandle *mh;
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",
330                               GNUNET_DISK_OPEN_READ,
331                               GNUNET_DISK_PERM_NONE);
332   if (NULL == fh)
333   {
334     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
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   {
341     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
342                 "Could not determine size of /etc/resolv.conf. "
343                 "DNS resolution will not be possible.\n");
344     GNUNET_DISK_file_close (fh);
345     return -1;
346   }
347   if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
348   {
349     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350                 "/etc/resolv.conf file too large to mmap. "
351                 "DNS resolution will not be possible.\n");
352     GNUNET_DISK_file_close (fh);
353     return -1;
354   }
355   buf = GNUNET_DISK_file_map (fh,
356                               &mh,
357                               GNUNET_DISK_MAP_TYPE_READ,
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   }
383   GNUNET_DISK_file_unmap (mh);
384   GNUNET_DISK_file_close (fh);
385   return (int) num_dns_servers;
386 }
387
388
389 /**
390  * Compute name to use for DNS reverse lookups from @a ip.
391  *
392  * @param ip IP address to resolve, in binary format, network byte order
393  * @param af address family of @a ip, AF_INET or AF_INET6
394  */
395 static char *
396 make_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
447 /**
448  * Send DNS @a record back to our @a client.
449  *
450  * @param record information to transmit
451  * @param record_type requested record type from client
452  * @param client_request_id to which request are we responding
453  * @param client where to send @a record
454  * @return #GNUNET_YES if we sent a reply,
455  *         #GNUNET_NO if the record type is not understood or
456  *         does not match @a record_type
457  */
458 static int
459 send_reply (struct GNUNET_DNSPARSER_Record *record,
460             uint16_t record_type,
461             uint32_t client_request_id,
462             struct GNUNET_SERVICE_Client *client)
463 {
464   struct GNUNET_RESOLVER_ResponseMessage *msg;
465   struct GNUNET_MQ_Envelope *env;
466   const void *payload;
467   size_t payload_len;
468
469   switch (record->type)
470   {
471   case GNUNET_DNSPARSER_TYPE_CNAME:
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
478   case GNUNET_DNSPARSER_TYPE_PTR:
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
485   case GNUNET_DNSPARSER_TYPE_A:
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
493   case GNUNET_DNSPARSER_TYPE_AAAA:
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:
502     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503                 "Cannot handle DNS response type %u: not supported here\n",
504                 record->type);
505     return GNUNET_NO;
506   }
507   env = GNUNET_MQ_msg_extra (msg,
508                              payload_len,
509                              GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
510   msg->client_id = client_request_id;
511   GNUNET_memcpy (&msg[1], payload, payload_len);
512   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
513   return GNUNET_YES;
514 }
515
516
517 /**
518  * Send message to @a client that we transmitted all
519  * responses for @a client_request_id
520  *
521  * @param client_request_id to which request are we responding
522  * @param client where to send @a record
523  */
524 static void
525 send_end_msg (uint32_t client_request_id, struct GNUNET_SERVICE_Client *client)
526 {
527   struct GNUNET_RESOLVER_ResponseMessage *msg;
528   struct GNUNET_MQ_Envelope *env;
529
530   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending END message\n");
531   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
532   msg->client_id = client_request_id;
533   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env);
534 }
535
536
537 /**
538  * Remove expired entries from @a rc
539  *
540  * @param rc entry in resolver cache
541  * @return #GNUNET_YES if @a rc was completely expired
542  *         #GNUNET_NO if some entries are left
543  */
544 static int
545 remove_expired (struct ResolveCache *rc)
546 {
547   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
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     {
555       GNUNET_CONTAINER_DLL_remove (rc->records_head, rc->records_tail, pos);
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
570 /**
571  * Process DNS request for @a hostname with request ID @a request_id
572  * from @a client demanding records of type @a record_type.
573  *
574  * @param hostname DNS name to resolve
575  * @param record_type desired record type
576  * @param client_request_id client's request ID
577  * @param client who should get the result?
578  */
579 static void
580 process_get (const char *hostname,
581              uint16_t record_type,
582              uint32_t client_request_id,
583              struct GNUNET_SERVICE_Client *client);
584
585
586 /**
587  * Get an IP address as a string (works for both IPv4 and IPv6).  Note
588  * that the resolution happens asynchronously and that the first call
589  * may not immediately result in the FQN (but instead in a
590  * human-readable IP address).
591  *
592  * @param hostname what hostname was to be resolved
593  * @param record_type what type of record was requested
594  * @param client_request_id unique identification of the client's request
595  * @param client handle to the client making the request (for sending the reply)
596  */
597 static int
598 try_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   {
617     next = cache_head;
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 */
635     GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, pos);
636     GNUNET_CONTAINER_DLL_insert (cache_head, cache_tail, pos);
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
644     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
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 = record->data.hostname;
652
653       process_get (hostname, 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
666 /**
667  * Create DNS query for @a hostname of type @a type
668  * with DNS request ID @a dns_id.
669  *
670  * @param hostname DNS name to query
671  * @param type requested DNS record type
672  * @param dns_id what should be the DNS request ID
673  * @param packet_buf[out] where to write the request packet
674  * @param packet_size[out] set to size of @a packet_buf on success
675  * @return #GNUNET_OK on success
676  */
677 static int
678 pack (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;
689   query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
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   {
698     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
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 static void
708 cache_answers (const char *name,
709                struct GNUNET_DNSPARSER_Record *records,
710                unsigned int num_records)
711 {
712   struct ResolveCache *rc;
713   struct GNUNET_DNSPARSER_Record *record;
714   struct RecordListEntry *rle;
715
716   for (unsigned int i = 0; i != num_records; i++)
717   {
718     record = &records[i];
719
720     for (rc = cache_head; NULL != rc; rc = rc->next)
721       if (0 == strcasecmp (rc->hostname, name))
722         break;
723     if (NULL == rc)
724     {
725       rc = GNUNET_new (struct ResolveCache);
726       rc->hostname = GNUNET_strdup (name);
727       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728                   "Caching record for name %s under %s\n",
729                   record->name, name);
730       GNUNET_CONTAINER_DLL_insert (cache_head, cache_tail, rc);
731       cache_size++;
732     }
733     /* TODO: ought to check first if we have this exact record
734        already in the cache! */
735     rle = GNUNET_new (struct RecordListEntry);
736     rle->record = GNUNET_DNSPARSER_duplicate_record (record);
737     GNUNET_CONTAINER_DLL_insert (rc->records_head, rc->records_tail, rle);
738   }
739 }
740
741 /**
742  * We got a result from DNS. Add it to the cache and
743  * see if we can make our client happy...
744  *
745  * @param cls the `struct ActiveLookup`
746  * @param dns the DNS response
747  * @param dns_len number of bytes in @a dns
748  */
749 static void
750 handle_resolve_result (void *cls,
751                        const struct GNUNET_TUN_DnsHeader *dns,
752                        size_t dns_len)
753 {
754   struct ActiveLookup *al = cls;
755   struct GNUNET_DNSPARSER_Packet *parsed;
756
757   parsed = GNUNET_DNSPARSER_parse ((const char *) dns, dns_len);
758   if (NULL == parsed)
759   {
760     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
761                 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
762                 al->hostname,
763                 al->dns_id);
764     return;
765   }
766   if (al->dns_id != ntohs (parsed->id))
767   {
768     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769                 "Request ID in DNS reply does not match\n");
770     GNUNET_DNSPARSER_free_packet (parsed);
771     return;
772   }
773   if (0 == parsed->num_answers + parsed->num_authority_records
774       + parsed->num_additional_records)
775   {
776     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777                 "DNS reply (hostname %s, request ID %u) contains no answers\n",
778                 al->hostname,
779                 (unsigned int) al->client_request_id);
780     /* resume by trying again from cache */
781     if (GNUNET_NO == try_cache (al->hostname,
782                                 al->record_type,
783                                 al->client_request_id,
784                                 al->client))
785     /* cache failed, tell client we could not get an answer */
786     {
787       send_end_msg (al->client_request_id, al->client);
788     }
789     GNUNET_DNSPARSER_free_packet (parsed);
790     free_active_lookup (al);
791     return;
792   }
793   /* LRU-based cache eviction: we remove from tail */
794   while (cache_size > MAX_CACHE)
795     free_cache_entry (cache_tail);
796
797   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798               "Got reply for hostname %s and request ID %u\n",
799               al->hostname,
800               (unsigned int) al->client_request_id);
801   /* add to cache */
802   cache_answers (al->hostname, parsed->answers, parsed->num_answers);
803   cache_answers (al->hostname,
804                  parsed->authority_records,
805                  parsed->num_authority_records);
806   cache_answers (al->hostname,
807                  parsed->additional_records,
808                  parsed->num_additional_records);
809
810   /* see if we need to do the 2nd request for AAAA records */
811   if ((GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
812       (GNUNET_NO == al->did_aaaa))
813   {
814     char *packet_buf;
815     size_t packet_size;
816     uint16_t dns_id;
817
818     dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
819                                                   UINT16_MAX);
820     if (GNUNET_OK == pack (al->hostname,
821                            GNUNET_DNSPARSER_TYPE_AAAA,
822                            dns_id,
823                            &packet_buf,
824                            &packet_size))
825     {
826       al->did_aaaa = GNUNET_YES;
827       al->dns_id = dns_id;
828       GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
829       al->resolve_handle = GNUNET_DNSSTUB_resolve (dnsstub_ctx,
830                                                    packet_buf,
831                                                    packet_size,
832                                                    &handle_resolve_result,
833                                                    al);
834       GNUNET_free (packet_buf);
835       GNUNET_DNSPARSER_free_packet (parsed);
836       return;
837     }
838   }
839
840   /* resume by trying again from cache */
841   if (GNUNET_NO == try_cache (al->hostname,
842                               al->record_type,
843                               al->client_request_id,
844                               al->client))
845   /* cache failed, tell client we could not get an answer */
846   {
847     send_end_msg (al->client_request_id, al->client);
848   }
849   free_active_lookup (al);
850   GNUNET_DNSPARSER_free_packet (parsed);
851 }
852
853
854 /**
855  * We encountered a timeout trying to perform a
856  * DNS lookup.
857  *
858  * @param cls a `struct ActiveLookup`
859  */
860 static void
861 handle_resolve_timeout (void *cls)
862 {
863   struct ActiveLookup *al = cls;
864
865   al->timeout_task = NULL;
866   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DNS lookup timeout!\n");
867   send_end_msg (al->client_request_id, al->client);
868   free_active_lookup (al);
869 }
870
871
872 /**
873  * Initiate an active lookup, then cache the result and
874  * try to then complete the resolution.
875  *
876  * @param hostname DNS name to resolve
877  * @param record_type record type to locate
878  * @param client_request_id client request ID
879  * @param client handle to the client
880  * @return #GNUNET_OK if the DNS query is now pending
881  */
882 static int
883 resolve_and_cache (const char *hostname,
884                    uint16_t record_type,
885                    uint32_t client_request_id,
886                    struct GNUNET_SERVICE_Client *client)
887 {
888   char *packet_buf;
889   size_t packet_size;
890   struct ActiveLookup *al;
891   uint16_t dns_id;
892   uint16_t type;
893
894   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "resolve_and_cache `%s'\n", hostname);
895   dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
896                                                 UINT16_MAX);
897
898   if (GNUNET_DNSPARSER_TYPE_ALL == record_type)
899     type = GNUNET_DNSPARSER_TYPE_A;
900   else
901     type = record_type;
902   if (GNUNET_OK != pack (hostname, type, dns_id, &packet_buf, &packet_size))
903   {
904     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
905                 "Failed to pack query for hostname `%s'\n",
906                 hostname);
907     return GNUNET_SYSERR;
908   }
909
910   al = GNUNET_new (struct ActiveLookup);
911   al->hostname = GNUNET_strdup (hostname);
912   al->record_type = record_type;
913   al->client_request_id = client_request_id;
914   al->dns_id = dns_id;
915   al->client = client;
916   al->timeout_task =
917     GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT, &handle_resolve_timeout, al);
918   al->resolve_handle = GNUNET_DNSSTUB_resolve (dnsstub_ctx,
919                                                packet_buf,
920                                                packet_size,
921                                                &handle_resolve_result,
922                                                al);
923   GNUNET_free (packet_buf);
924   GNUNET_CONTAINER_DLL_insert (lookup_head, lookup_tail, al);
925   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926               "Resolving %s, client_request_id = %u, dns_id = %u\n",
927               hostname,
928               (unsigned int) client_request_id,
929               (unsigned int) dns_id);
930   return GNUNET_OK;
931 }
932
933
934 /**
935  * Process DNS request for @a hostname with request ID @a client_request_id
936  * from @a client demanding records of type @a record_type.
937  *
938  * @param hostname DNS name to resolve
939  * @param record_type desired record type
940  * @param client_request_id client's request ID
941  * @param client who should get the result?
942  */
943 static void
944 process_get (const char *hostname,
945              uint16_t record_type,
946              uint32_t client_request_id,
947              struct GNUNET_SERVICE_Client *client)
948 {
949   char fqdn[255];
950
951   if (GNUNET_NO != try_cache (hostname, record_type, client_request_id, client))
952     return;
953   if ((NULL != my_domain) && (NULL == strchr (hostname, (unsigned char) '.')) &&
954       (strlen (hostname) + strlen (my_domain) <= 253))
955   {
956     GNUNET_snprintf (fqdn, sizeof(fqdn), "%s.%s", hostname, my_domain);
957   }
958   else if (strlen (hostname) < 255)
959   {
960     GNUNET_snprintf (fqdn, sizeof(fqdn), "%s", hostname);
961   }
962   else
963   {
964     GNUNET_break (0);
965     GNUNET_SERVICE_client_drop (client);
966     return;
967   }
968   if (GNUNET_NO == try_cache (fqdn, record_type, client_request_id, client))
969   {
970     if (GNUNET_OK !=
971         resolve_and_cache (fqdn, record_type, client_request_id, client))
972     {
973       send_end_msg (client_request_id, client);
974     }
975   }
976 }
977
978
979 /**
980  * Verify well-formedness of GET-message.
981  *
982  * @param cls closure, unused
983  * @param get the actual message
984  * @return #GNUNET_OK if @a get is well-formed
985  */
986 static int
987 check_get (void *cls, const struct GNUNET_RESOLVER_GetMessage *get)
988 {
989   uint16_t size;
990   int direction;
991   int af;
992
993   (void) cls;
994   size = ntohs (get->header.size) - sizeof(*get);
995   direction = ntohl (get->direction);
996   if (GNUNET_NO == direction)
997   {
998     GNUNET_MQ_check_zero_termination (get);
999     return GNUNET_OK;
1000   }
1001   af = ntohl (get->af);
1002   switch (af)
1003   {
1004   case AF_INET:
1005     if (size != sizeof(struct in_addr))
1006     {
1007       GNUNET_break (0);
1008       return GNUNET_SYSERR;
1009     }
1010     break;
1011
1012   case AF_INET6:
1013     if (size != sizeof(struct in6_addr))
1014     {
1015       GNUNET_break (0);
1016       return GNUNET_SYSERR;
1017     }
1018     break;
1019
1020   default:
1021     GNUNET_break (0);
1022     return GNUNET_SYSERR;
1023   }
1024   return GNUNET_OK;
1025 }
1026
1027
1028 /**
1029  * Handle GET-message.
1030  *
1031  * @param cls identification of the client
1032  * @param msg the actual message
1033  */
1034 static void
1035 handle_get (void *cls, const struct GNUNET_RESOLVER_GetMessage *msg)
1036 {
1037   struct GNUNET_SERVICE_Client *client = cls;
1038   int direction;
1039   int af;
1040   uint32_t client_request_id;
1041   char *hostname;
1042
1043   direction = ntohl (msg->direction);
1044   af = ntohl (msg->af);
1045   client_request_id = msg->client_id;
1046   GNUNET_SERVICE_client_continue (client);
1047   if (GNUNET_NO == direction)
1048   {
1049     /* IP from hostname */
1050     hostname = GNUNET_strdup ((const char *) &msg[1]);
1051     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052                 "Client asks to resolve `%s'\n",
1053                 hostname);
1054     switch (af)
1055     {
1056     case AF_UNSPEC: {
1057         process_get (hostname,
1058                      GNUNET_DNSPARSER_TYPE_ALL,
1059                      client_request_id,
1060                      client);
1061         break;
1062       }
1063
1064     case AF_INET: {
1065         process_get (hostname,
1066                      GNUNET_DNSPARSER_TYPE_A,
1067                      client_request_id,
1068                      client);
1069         break;
1070       }
1071
1072     case AF_INET6: {
1073         process_get (hostname,
1074                      GNUNET_DNSPARSER_TYPE_AAAA,
1075                      client_request_id,
1076                      client);
1077         break;
1078       }
1079
1080     default: {
1081         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got invalid af: %d\n", af);
1082         GNUNET_assert (0);
1083       }
1084     }
1085   }
1086   else
1087   {
1088     /* hostname from IP */
1089     hostname = make_reverse_hostname (&msg[1], af);
1090     process_get (hostname,
1091                  GNUNET_DNSPARSER_TYPE_PTR,
1092                  client_request_id,
1093                  client);
1094   }
1095   GNUNET_free_non_null (hostname);
1096 }
1097
1098
1099 /**
1100  * Service is shutting down, clean up.
1101  *
1102  * @param cls NULL, unused
1103  */
1104 static void
1105 shutdown_task (void *cls)
1106 {
1107   (void) cls;
1108
1109   while (NULL != lookup_head)
1110     free_active_lookup (lookup_head);
1111   while (NULL != cache_head)
1112     free_cache_entry (cache_head);
1113   while (NULL != hosts_head)
1114     free_hosts_entry (hosts_head);
1115   GNUNET_DNSSTUB_stop (dnsstub_ctx);
1116   GNUNET_free_non_null (my_domain);
1117 }
1118
1119
1120 /**
1121  * Add information about a host from /etc/hosts
1122  * to our cache.
1123  *
1124  * @param hostname the name of the host
1125  * @param rec_type DNS record type to use
1126  * @param data payload
1127  * @param data_size number of bytes in @a data
1128  */
1129 static void
1130 add_host (const char *hostname,
1131           uint16_t rec_type,
1132           const void *data,
1133           size_t data_size)
1134 {
1135   struct ResolveCache *rc;
1136   struct RecordListEntry *rle;
1137   struct GNUNET_DNSPARSER_Record *rec;
1138
1139   rec = GNUNET_malloc (sizeof(struct GNUNET_DNSPARSER_Record));
1140   rec->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS;
1141   rec->type = rec_type;
1142   rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1143   rec->name = GNUNET_strdup (hostname);
1144   rec->data.raw.data = GNUNET_memdup (data, data_size);
1145   rec->data.raw.data_len = data_size;
1146   rle = GNUNET_new (struct RecordListEntry);
1147   rle->record = rec;
1148   rc = GNUNET_new (struct ResolveCache);
1149   rc->hostname = GNUNET_strdup (hostname);
1150   GNUNET_CONTAINER_DLL_insert (rc->records_head, rc->records_tail, rle);
1151   GNUNET_CONTAINER_DLL_insert (hosts_head, hosts_tail, rc);
1152 }
1153
1154
1155 /**
1156  * Extract host information from a line in /etc/hosts
1157  *
1158  * @param line the line to parse
1159  * @param line_len number of bytes in @a line
1160  */
1161 static void
1162 extract_hosts (const char *line, size_t line_len)
1163 {
1164   const char *c;
1165   struct in_addr v4;
1166   struct in6_addr v6;
1167   char *tbuf;
1168   char *tok;
1169
1170   /* ignore everything after '#' */
1171   c = memrchr (line, (unsigned char) '#', line_len);
1172   if (NULL != c)
1173     line_len = c - line;
1174   /* ignore leading whitespace */
1175   while ((0 < line_len) && isspace ((unsigned char) *line))
1176   {
1177     line++;
1178     line_len--;
1179   }
1180   tbuf = GNUNET_strndup (line, line_len);
1181   tok = strtok (tbuf, " \t");
1182   if (NULL == tok)
1183   {
1184     GNUNET_free (tbuf);
1185     return;
1186   }
1187   if (1 == inet_pton (AF_INET, tok, &v4))
1188   {
1189     while (NULL != (tok = strtok (NULL, " \t")))
1190       add_host (tok, GNUNET_DNSPARSER_TYPE_A, &v4, sizeof(struct in_addr));
1191   }
1192   else if (1 == inet_pton (AF_INET6, tok, &v6))
1193   {
1194     while (NULL != (tok = strtok (NULL, " \t")))
1195       add_host (tok, GNUNET_DNSPARSER_TYPE_AAAA, &v6, sizeof(struct in6_addr));
1196   }
1197   GNUNET_free (tbuf);
1198 }
1199
1200
1201 /**
1202  * Reads the list of hosts from /etc/hosts.
1203  */
1204 static void
1205 load_etc_hosts (void)
1206 {
1207   struct GNUNET_DISK_FileHandle *fh;
1208   struct GNUNET_DISK_MapHandle *mh;
1209   off_t bytes_read;
1210   const char *buf;
1211   size_t read_offset;
1212
1213   fh = GNUNET_DISK_file_open ("/etc/hosts",
1214                               GNUNET_DISK_OPEN_READ,
1215                               GNUNET_DISK_PERM_NONE);
1216   if (NULL == fh)
1217   {
1218     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to open /etc/hosts");
1219     return;
1220   }
1221   if (GNUNET_OK != GNUNET_DISK_file_handle_size (fh, &bytes_read))
1222   {
1223     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1224                 "Could not determin size of /etc/hosts. "
1225                 "DNS resolution will not be possible.\n");
1226     GNUNET_DISK_file_close (fh);
1227     return;
1228   }
1229   if (((unsigned long long) bytes_read) > (unsigned long long) SIZE_MAX)
1230   {
1231     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1232                 "/etc/hosts file too large to mmap. "
1233                 "DNS resolution will not be possible.\n");
1234     GNUNET_DISK_file_close (fh);
1235     return;
1236   }
1237   buf = GNUNET_DISK_file_map (fh,
1238                               &mh,
1239                               GNUNET_DISK_MAP_TYPE_READ,
1240                               (size_t) bytes_read);
1241   read_offset = 0;
1242   while (read_offset < (size_t) bytes_read)
1243   {
1244     const char *newline;
1245     size_t line_len;
1246
1247     newline = strchr (buf + read_offset, '\n');
1248     if (NULL == newline)
1249       break;
1250     line_len = newline - buf - read_offset;
1251     extract_hosts (buf + read_offset, line_len);
1252     read_offset += line_len + 1;
1253   }
1254   GNUNET_DISK_file_unmap (mh);
1255   GNUNET_DISK_file_close (fh);
1256 }
1257
1258
1259 /**
1260  * Service is starting, initialize everything.
1261  *
1262  * @param cls NULL, unused
1263  * @param cfg our configuration
1264  * @param sh service handle
1265  */
1266 static void
1267 init_cb (void *cls,
1268          const struct GNUNET_CONFIGURATION_Handle *cfg,
1269          struct GNUNET_SERVICE_Handle *sh)
1270 {
1271   char **dns_servers;
1272   int num_dns_servers;
1273
1274   (void) cfg;
1275   (void) sh;
1276   load_etc_hosts ();
1277   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, cls);
1278   dnsstub_ctx = GNUNET_DNSSTUB_start (128);
1279   dns_servers = NULL;
1280   num_dns_servers = lookup_dns_servers (&dns_servers);
1281   if (0 >= num_dns_servers)
1282   {
1283     GNUNET_log (
1284       GNUNET_ERROR_TYPE_ERROR,
1285       _ ("No DNS server available. DNS resolution will not be possible.\n"));
1286     return;
1287   }
1288   for (int i = 0; i < num_dns_servers; i++)
1289   {
1290     int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1291     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1292                 "Adding DNS server '%s': %s\n",
1293                 dns_servers[i],
1294                 GNUNET_OK == result ? "success" : "failure");
1295     GNUNET_free (dns_servers[i]);
1296   }
1297   GNUNET_free_non_null (dns_servers);
1298 }
1299
1300
1301 /**
1302  * Callback called when a client connects to the service.
1303  *
1304  * @param cls closure for the service, unused
1305  * @param c the new client that connected to the service
1306  * @param mq the message queue used to send messages to the client
1307  * @return @a c
1308  */
1309 static void *
1310 connect_cb (void *cls,
1311             struct GNUNET_SERVICE_Client *c,
1312             struct GNUNET_MQ_Handle *mq)
1313 {
1314   (void) cls;
1315   (void) mq;
1316
1317   return c;
1318 }
1319
1320
1321 /**
1322  * Callback called when a client disconnected from the service
1323  *
1324  * @param cls closure for the service
1325  * @param c the client that disconnected
1326  * @param internal_cls should be equal to @a c
1327  */
1328 static void
1329 disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
1330 {
1331   struct ActiveLookup *n;
1332
1333   (void) cls;
1334
1335   GNUNET_assert (c == internal_cls);
1336   n = lookup_head;
1337   for (struct ActiveLookup *al = n; NULL != al; al = n)
1338   {
1339     n = al->next;
1340     if (al->client == c)
1341       free_active_lookup (al);
1342   }
1343 }
1344
1345
1346 /**
1347  * Define "main" method using service macro.
1348  */
1349 GNUNET_SERVICE_MAIN (
1350   "resolver",
1351   GNUNET_SERVICE_OPTION_NONE,
1352   &init_cb,
1353   &connect_cb,
1354   &disconnect_cb,
1355   NULL,
1356   GNUNET_MQ_hd_var_size (get,
1357                          GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1358                          struct GNUNET_RESOLVER_GetMessage,
1359                          NULL),
1360   GNUNET_MQ_handler_end ());
1361
1362
1363 #if defined(LINUX) && defined(__GLIBC__)
1364 #include <malloc.h>
1365
1366 /**
1367  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1368  */
1369 void __attribute__ ((constructor)) GNUNET_RESOLVER_memory_init ()
1370 {
1371   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1372   mallopt (M_TOP_PAD, 1 * 1024);
1373   malloc_trim (0);
1374 }
1375 #endif
1376
1377
1378 /* end of gnunet-service-resolver.c */