attempt fix #5578
[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 /**
67  * A cached DNS lookup result.
68  */
69 struct ResolveCache
70 {
71   /**
72    * This is a doubly linked list.
73    */
74   struct ResolveCache *next;
75
76   /**
77    * This is a doubly linked list.
78    */
79   struct ResolveCache *prev;
80
81   /**
82    * Which hostname is this cache for?
83    */
84   char *hostname;
85
86   /**
87    * head of a double linked list containing the lookup results
88    */
89   struct RecordListEntry *records_head;
90
91   /**
92    * tail of a double linked list containing the lookup results
93    */
94   struct RecordListEntry *records_tail;
95
96 };
97
98
99 /**
100  * Information about pending lookups.
101  */
102 struct ActiveLookup
103 {
104   /**
105    * Stored in a DLL.
106    */
107   struct ActiveLookup *next;
108
109   /**
110    * Stored in a DLL.
111    */
112   struct ActiveLookup *prev;
113
114   /**
115    * The client that queried the records contained in this cache entry.
116    */
117   struct GNUNET_SERVICE_Client *client;
118
119   /**
120    * handle for cancelling a request
121    */
122   struct GNUNET_DNSSTUB_RequestSocket *resolve_handle;
123
124   /**
125    * handle for the resolution timeout task
126    */
127   struct GNUNET_SCHEDULER_Task *timeout_task;
128
129   /**
130    * Which hostname are we resolving?
131    */
132   char *hostname;
133
134   /**
135    * If @a record_type is #GNUNET_DNSPARSER_TYPE_ALL, did we go again
136    * for the AAAA records yet?
137    */
138   int did_aaaa;
139
140   /**
141    * type of queried DNS record
142    */
143   uint16_t record_type;
144
145   /**
146    * Unique request ID of a client if a query for this hostname/record_type
147    * is currently pending, undefined otherwise.
148    */
149   uint32_t client_request_id;
150
151   /**
152    * Unique DNS request ID of a client if a query for this hostname/record_type
153    * is currently pending, undefined otherwise.
154    */
155   uint16_t dns_id;
156
157 };
158
159
160 /**
161  * Start of the linked list of cached DNS lookup results.
162  */
163 static struct ResolveCache *cache_head;
164
165 /**
166  * Tail of the linked list of cached DNS lookup results.
167  */
168 static struct ResolveCache *cache_tail;
169
170 /**
171  * Head of the linked list of DNS lookup results from /etc/hosts.
172  */
173 static struct ResolveCache *hosts_head;
174
175 /**
176  * Tail of the linked list of DNS lookup results from /etc/hosts.
177  */
178 static struct ResolveCache *hosts_tail;
179
180 /**
181  * Start of the linked list of active DNS lookups.
182  */
183 static struct ActiveLookup *lookup_head;
184
185 /**
186  * Tail of the linked list of active DNS lookups.
187  */
188 static struct ActiveLookup *lookup_tail;
189
190 /**
191  * context of dnsstub library
192  */
193 static struct GNUNET_DNSSTUB_Context *dnsstub_ctx;
194
195 /**
196  * My domain, to be appended to the hostname to get a FQDN.
197  */
198 static char *my_domain;
199
200 /**
201  * How many entries do we have in #cache_head DLL?
202  */
203 static unsigned int cache_size;
204
205
206 /**
207  * Remove @a entry from cache.
208  *
209  * @param rc entry to free
210  */
211 static void
212 free_cache_entry (struct ResolveCache *rc)
213 {
214   struct RecordListEntry *pos;
215
216   while (NULL != (pos = rc->records_head))
217   {
218     GNUNET_CONTAINER_DLL_remove (rc->records_head,
219                                  rc->records_tail,
220                                  pos);
221     GNUNET_DNSPARSER_free_record (pos->record);
222     GNUNET_free (pos->record);
223     GNUNET_free (pos);
224   }
225   GNUNET_free_non_null (rc->hostname);
226   GNUNET_CONTAINER_DLL_remove (cache_head,
227                                cache_tail,
228                                rc);
229   cache_size--;
230   GNUNET_free (rc);
231 }
232
233
234 /**
235  * Remove @a entry from cache.
236  *
237  * @param rc entry to free
238  */
239 static void
240 free_hosts_entry (struct ResolveCache *rc)
241 {
242   struct RecordListEntry *pos;
243
244   while (NULL != (pos = rc->records_head))
245   {
246     GNUNET_CONTAINER_DLL_remove (rc->records_head,
247                                  rc->records_tail,
248                                  pos);
249     GNUNET_DNSPARSER_free_record (pos->record);
250     GNUNET_free (pos->record);
251     GNUNET_free (pos);
252   }
253   GNUNET_free_non_null (rc->hostname);
254   GNUNET_CONTAINER_DLL_remove (hosts_head,
255                                hosts_tail,
256                                rc);
257   cache_size--;
258   GNUNET_free (rc);
259 }
260
261
262 /**
263  * Release resources associated with @a al
264  *
265  * @param al an active lookup
266  */
267 static void
268 free_active_lookup (struct ActiveLookup *al)
269 {
270   GNUNET_CONTAINER_DLL_remove (lookup_head,
271                                lookup_tail,
272                                al);
273   if (NULL != al->resolve_handle)
274   {
275     GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
276     al->resolve_handle = NULL;
277   }
278   if (NULL != al->timeout_task)
279   {
280     GNUNET_SCHEDULER_cancel (al->timeout_task);
281     al->timeout_task = NULL;
282   }
283   GNUNET_free_non_null (al->hostname);
284   GNUNET_free (al);
285 }
286
287
288
289 /**
290  * Find out if the configuration file line contains a string
291  * starting with "nameserver ", and if so, return a copy of
292  * the nameserver's IP.
293  *
294  * @param line line to parse
295  * @param line_len number of characters in @a line
296  * @return NULL if no nameserver is configured in this @a line
297  */
298 static char *
299 extract_dns_server (const char* line,
300                     size_t line_len)
301 {
302   if (0 == strncmp (line,
303                     "nameserver ",
304                     strlen ("nameserver ")))
305     return GNUNET_strndup (line + strlen ("nameserver "),
306                            line_len - strlen ("nameserver "));
307   return NULL;
308 }
309
310
311 /**
312  * Find out if the configuration file line contains a string
313  * starting with "search ", and if so, return a copy of
314  * the machine's search domain.
315  *
316  * @param line line to parse
317  * @param line_len number of characters in @a line
318  * @return NULL if no nameserver is configured in this @a line
319  */
320 static char *
321 extract_search_domain (const char* line,
322                        size_t line_len)
323 {
324   if (0 == strncmp (line,
325                     "search ",
326                     strlen ("search ")))
327     return GNUNET_strndup (line + strlen ("search "),
328                            line_len - strlen ("search "));
329   return NULL;
330 }
331
332
333 /**
334  * Reads the list of nameservers from /etc/resolve.conf
335  *
336  * @param server_addrs[out] a list of null-terminated server address strings
337  * @return the number of server addresses in @server_addrs, -1 on error
338  */
339 static int
340 lookup_dns_servers (char ***server_addrs)
341 {
342   struct GNUNET_DISK_FileHandle *fh;
343   struct GNUNET_DISK_MapHandle *mh;
344   off_t bytes_read;
345   const char *buf;
346   size_t read_offset;
347   unsigned int num_dns_servers;
348
349   fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
350                               GNUNET_DISK_OPEN_READ,
351                               GNUNET_DISK_PERM_NONE);
352   if (NULL == fh)
353   {
354     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355                 "Could not open /etc/resolv.conf. "
356                 "DNS resolution will not be possible.\n");
357     return -1;
358   }
359   if (GNUNET_OK !=
360       GNUNET_DISK_file_handle_size (fh,
361                                     &bytes_read))
362   {
363     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364                 "Could not determine size of /etc/resolv.conf. "
365                 "DNS resolution will not be possible.\n");
366     GNUNET_DISK_file_close (fh);
367     return -1;
368   }
369   if ((size_t) bytes_read > SIZE_MAX)
370   {
371     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372                 "/etc/resolv.conf file too large to mmap. "
373                 "DNS resolution will not be possible.\n");
374     GNUNET_DISK_file_close (fh);
375     return -1;
376   }
377   buf = GNUNET_DISK_file_map (fh,
378                               &mh,
379                               GNUNET_DISK_MAP_TYPE_READ,
380                               (size_t) bytes_read);
381   *server_addrs = NULL;
382   read_offset = 0;
383   num_dns_servers = 0;
384   while (read_offset < (size_t) bytes_read)
385   {
386     const char *newline;
387     size_t line_len;
388     char *dns_server;
389
390     newline = strchr (buf + read_offset,
391                       '\n');
392     if (NULL == newline)
393       break;
394     line_len = newline - buf - read_offset;
395     dns_server = extract_dns_server (buf + read_offset,
396                                      line_len);
397     if (NULL != dns_server)
398     {
399       GNUNET_array_append (*server_addrs,
400                            num_dns_servers,
401                            dns_server);
402     }
403     else if (NULL == my_domain)
404     {
405       my_domain = extract_search_domain (buf + read_offset,
406                                          line_len);
407     }
408     read_offset += line_len + 1;
409   }
410   GNUNET_DISK_file_unmap (mh);
411   GNUNET_DISK_file_close (fh);
412   return (int) num_dns_servers;
413 }
414
415
416 /**
417  * Compute name to use for DNS reverse lookups from @a ip.
418  *
419  * @param ip IP address to resolve, in binary format, network byte order
420  * @param af address family of @a ip, AF_INET or AF_INET6
421  */
422 static char *
423 make_reverse_hostname (const void *ip,
424                        int af)
425 {
426   char *buf = GNUNET_new_array (80,
427                                 char);
428   int pos = 0;
429
430   if (AF_INET == af)
431   {
432     struct in_addr *addr = (struct in_addr *)ip;
433     uint32_t ip_int = addr->s_addr;
434
435     for (int i = 3; i >= 0; i--)
436     {
437       int n = GNUNET_snprintf (buf + pos,
438                                80 - pos,
439                                "%u.",
440                                ((uint8_t *)&ip_int)[i]);
441       if (n < 0)
442       {
443         GNUNET_free (buf);
444         return NULL;
445       }
446       pos += n;
447     }
448     pos += GNUNET_snprintf (buf + pos,
449                             80 - pos,
450                             "in-addr.arpa");
451   }
452   else if (AF_INET6 == af)
453   {
454     struct in6_addr *addr = (struct in6_addr *)ip;
455     for (int i = 15; i >= 0; i--)
456     {
457       int n = GNUNET_snprintf (buf + pos,
458                                80 - pos,
459                                "%x.",
460                                addr->s6_addr[i] & 0xf);
461       if (n < 0)
462       {
463         GNUNET_free (buf);
464         return NULL;
465       }
466       pos += n;
467       n = GNUNET_snprintf (buf + pos,
468                            80 - pos,
469                            "%x.",
470                            addr->s6_addr[i] >> 4);
471       if (n < 0)
472       {
473         GNUNET_free (buf);
474         return NULL;
475       }
476       pos += n;
477     }
478     pos += GNUNET_snprintf (buf + pos,
479                             80 - pos,
480                             "ip6.arpa");
481   }
482   buf[pos] = '\0';
483   return buf;
484 }
485
486
487 /**
488  * Send DNS @a record back to our @a client.
489  *
490  * @param record information to transmit
491  * @param record_type requested record type from client
492  * @param client_request_id to which request are we responding
493  * @param client where to send @a record
494  * @return #GNUNET_YES if we sent a reply,
495  *         #GNUNET_NO if the record type is not understood or
496  *         does not match @a record_type
497  */
498 static int
499 send_reply (struct GNUNET_DNSPARSER_Record *record,
500             uint16_t record_type,
501             uint32_t client_request_id,
502             struct GNUNET_SERVICE_Client *client)
503 {
504   struct GNUNET_RESOLVER_ResponseMessage *msg;
505   struct GNUNET_MQ_Envelope *env;
506   const void *payload;
507   size_t payload_len;
508
509   switch (record->type)
510   {
511   case GNUNET_DNSPARSER_TYPE_CNAME:
512     if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
513       return GNUNET_NO;
514     payload = record->data.hostname;
515     payload_len = strlen (record->data.hostname) + 1;
516     break;
517   case GNUNET_DNSPARSER_TYPE_PTR:
518     if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
519       return GNUNET_NO;
520     payload = record->data.hostname;
521     payload_len = strlen (record->data.hostname) + 1;
522     break;
523   case GNUNET_DNSPARSER_TYPE_A:
524     if ( (GNUNET_DNSPARSER_TYPE_A != record_type) &&
525          (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
526       return GNUNET_NO;
527     payload = record->data.raw.data;
528     payload_len = record->data.raw.data_len;
529     break;
530   case GNUNET_DNSPARSER_TYPE_AAAA:
531     if ( (GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
532          (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
533       return GNUNET_NO;
534     payload = record->data.raw.data;
535     payload_len = record->data.raw.data_len;
536     break;
537   default:
538     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539                 "Cannot handle DNS response type %u: not supported here\n",
540                 record->type);
541     return GNUNET_NO;
542   }
543   env = GNUNET_MQ_msg_extra (msg,
544                              payload_len,
545                              GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
546   msg->client_id = client_request_id;
547   GNUNET_memcpy (&msg[1],
548                  payload,
549                  payload_len);
550   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
551                   env);
552   return GNUNET_YES;
553 }
554
555
556 /**
557  * Send message to @a client that we transmitted all
558  * responses for @a client_request_id
559  *
560  * @param client_request_id to which request are we responding
561  * @param client where to send @a record
562  */
563 static void
564 send_end_msg (uint32_t client_request_id,
565               struct GNUNET_SERVICE_Client *client)
566 {
567   struct GNUNET_RESOLVER_ResponseMessage *msg;
568   struct GNUNET_MQ_Envelope *env;
569
570   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571               "Sending END message\n");
572   env = GNUNET_MQ_msg (msg,
573                        GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
574   msg->client_id = client_request_id;
575   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
576                   env);
577 }
578
579
580 /**
581  * Remove expired entries from @a rc
582  *
583  * @param rc entry in resolver cache
584  * @return #GNUNET_YES if @a rc was completely expired
585  *         #GNUNET_NO if some entries are left
586  */
587 static int
588 remove_expired (struct ResolveCache *rc)
589 {
590   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
591   struct RecordListEntry *n;
592
593   for (struct RecordListEntry *pos = rc->records_head;
594        NULL != pos;
595        pos = n)
596   {
597     n = pos->next;
598     if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
599     {
600       GNUNET_CONTAINER_DLL_remove (rc->records_head,
601                                    rc->records_tail,
602                                    pos);
603       GNUNET_DNSPARSER_free_record (pos->record);
604       GNUNET_free (pos->record);
605       GNUNET_free (pos);
606     }
607   }
608   if (NULL == rc->records_head)
609   {
610     free_cache_entry (rc);
611     return GNUNET_YES;
612   }
613   return GNUNET_NO;
614 }
615
616
617 /**
618  * Process DNS request for @a hostname with request ID @a request_id
619  * from @a client demanding records of type @a record_type.
620  *
621  * @param hostname DNS name to resolve
622  * @param record_type desired record type
623  * @param client_request_id client's request ID
624  * @param client who should get the result?
625  */
626 static void
627 process_get (const char *hostname,
628              uint16_t record_type,
629              uint32_t client_request_id,
630              struct GNUNET_SERVICE_Client *client);
631
632
633 /**
634  * Get an IP address as a string (works for both IPv4 and IPv6).  Note
635  * that the resolution happens asynchronously and that the first call
636  * may not immediately result in the FQN (but instead in a
637  * human-readable IP address).
638  *
639  * @param hostname what hostname was to be resolved
640  * @param record_type what type of record was requested
641  * @param client_request_id unique identification of the client's request
642  * @param client handle to the client making the request (for sending the reply)
643  */
644 static int
645 try_cache (const char *hostname,
646            uint16_t record_type,
647            uint32_t client_request_id,
648            struct GNUNET_SERVICE_Client *client)
649 {
650   struct ResolveCache *pos;
651   struct ResolveCache *next;
652   int found;
653   int in_hosts;
654
655   in_hosts = GNUNET_NO;
656   for (pos = hosts_head; NULL != pos; pos = pos->next)
657     if (0 == strcmp (pos->hostname,
658                      hostname))
659     {
660       in_hosts = GNUNET_YES;
661       break;
662     }
663   if (NULL == pos)
664   {
665     next = cache_head;
666     for (pos = next; NULL != pos; pos = next)
667     {
668       next = pos->next;
669       if (GNUNET_YES == remove_expired (pos))
670         continue;
671       if (0 == strcmp (pos->hostname,
672                        hostname))
673         break;
674     }
675   }
676   if (NULL == pos)
677   {
678     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679                 "No cache entry for '%s'\n",
680                 hostname);
681     return GNUNET_NO;
682   }
683   if ( (GNUNET_NO == in_hosts) &&
684        (cache_head != pos) )
685   {
686     /* move result to head to achieve LRU for cache eviction */
687     GNUNET_CONTAINER_DLL_remove (cache_head,
688                                  cache_tail,
689                                  pos);
690     GNUNET_CONTAINER_DLL_insert (cache_head,
691                                  cache_tail,
692                                  pos);
693   }
694   found = GNUNET_NO;
695   for (struct RecordListEntry *rle = pos->records_head;
696        NULL != rle;
697        rle = rle->next)
698   {
699     const struct GNUNET_DNSPARSER_Record *record = rle->record;
700
701     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702                 "Found cache entry for '%s', record type '%u'\n",
703                 hostname,
704                 record_type);
705     if ( (GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
706          (GNUNET_DNSPARSER_TYPE_CNAME != record_type) &&
707          (GNUNET_NO == found) )
708     {
709       const char *hostname = record->data.hostname;
710
711       process_get (hostname,
712                    record_type,
713                    client_request_id,
714                    client);
715       return GNUNET_YES; /* counts as a cache "hit" */
716     }
717     found |= send_reply (rle->record,
718                          record_type,
719                          client_request_id,
720                          client);
721   }
722   if (GNUNET_NO == found)
723     return GNUNET_NO; /* had records, but none matched! */
724   send_end_msg (client_request_id,
725                 client);
726   return GNUNET_YES;
727 }
728
729
730 /**
731  * Create DNS query for @a hostname of type @a type
732  * with DNS request ID @a dns_id.
733  *
734  * @param hostname DNS name to query
735  * @param type requested DNS record type
736  * @param dns_id what should be the DNS request ID
737  * @param packet_buf[out] where to write the request packet
738  * @param packet_size[out] set to size of @a packet_buf on success
739  * @return #GNUNET_OK on success
740  */
741 static int
742 pack (const char *hostname,
743       uint16_t type,
744       uint16_t dns_id,
745       char **packet_buf,
746       size_t *packet_size)
747 {
748   struct GNUNET_DNSPARSER_Query query;
749   struct GNUNET_DNSPARSER_Packet packet;
750
751   query.name = (char *)hostname;
752   query.type = type;
753   query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
754   memset (&packet,
755           0,
756           sizeof (packet));
757   packet.num_queries = 1;
758   packet.queries = &query;
759   packet.id = htons (dns_id);
760   packet.flags.recursion_desired = 1;
761   if (GNUNET_OK !=
762       GNUNET_DNSPARSER_pack (&packet,
763                              UINT16_MAX,
764                              packet_buf,
765                              packet_size))
766   {
767     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
768                 "Failed to pack query for hostname `%s'\n",
769                 hostname);
770     packet_buf = NULL;
771     return GNUNET_SYSERR;
772   }
773   return GNUNET_OK;
774 }
775
776
777 /**
778  * We got a result from DNS. Add it to the cache and
779  * see if we can make our client happy...
780  *
781  * @param cls the `struct ActiveLookup`
782  * @param dns the DNS response
783  * @param dns_len number of bytes in @a dns
784  */
785 static void
786 handle_resolve_result (void *cls,
787                        const struct GNUNET_TUN_DnsHeader *dns,
788                        size_t dns_len)
789 {
790   struct ActiveLookup *al = cls;
791   struct GNUNET_DNSPARSER_Packet *parsed;
792   struct ResolveCache *rc;
793
794   parsed = GNUNET_DNSPARSER_parse ((const char *)dns,
795                                    dns_len);
796   if (NULL == parsed)
797   {
798     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
799                 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
800                 al->hostname,
801                 al->dns_id);
802     return;
803   }
804   if (al->dns_id != ntohs (parsed->id))
805   {
806     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807                 "Request ID in DNS reply does not match\n");
808     GNUNET_DNSPARSER_free_packet (parsed);
809     return;
810   }
811   if (0 == parsed->num_answers + parsed->num_authority_records + parsed->num_additional_records)
812   {
813     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814                 "DNS reply (hostname %s, request ID %u) contains no answers\n",
815                 al->hostname,
816                 (unsigned int) al->client_request_id);
817     /* resume by trying again from cache */
818     if (GNUNET_NO ==
819       try_cache (al->hostname,
820                  al->record_type,
821                  al->client_request_id,
822                  al->client))
823     /* cache failed, tell client we could not get an answer */
824     {
825       send_end_msg (al->client_request_id,
826                     al->client);
827     }
828     GNUNET_DNSPARSER_free_packet (parsed);
829     free_active_lookup (al);
830     return;
831   }
832   /* LRU-based cache eviction: we remove from tail */
833   while (cache_size > MAX_CACHE)
834     free_cache_entry (cache_tail);
835
836   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837               "Got reply for hostname %s and request ID %u\n",
838               al->hostname,
839               (unsigned int) al->client_request_id);
840   /* add to cache */
841   for (unsigned int i = 0; i != parsed->num_answers; i++)
842   {
843     struct GNUNET_DNSPARSER_Record *record = &parsed->answers[i];
844     struct RecordListEntry *rle;
845
846     for (rc = cache_head; NULL != rc; rc = rc->next)
847       if (0 == strcasecmp (rc->hostname,
848                            record->name))
849         break;
850     if (NULL == rc)
851     {
852       rc = GNUNET_new (struct ResolveCache);
853       rc->hostname = GNUNET_strdup (record->name);
854       GNUNET_CONTAINER_DLL_insert (cache_head,
855                                    cache_tail,
856                                    rc);
857       cache_size++;
858     }
859     /* TODO: ought to check first if we have this exact record
860        already in the cache! */
861     rle = GNUNET_new (struct RecordListEntry);
862     rle->record = GNUNET_DNSPARSER_duplicate_record (record);
863     GNUNET_CONTAINER_DLL_insert (rc->records_head,
864                                  rc->records_tail,
865                                  rle);
866   }
867   for (unsigned int i = 0; i != parsed->num_authority_records; i++)
868   {
869     struct GNUNET_DNSPARSER_Record *record = &parsed->authority_records[i];
870     struct RecordListEntry *rle;
871
872     for (rc = cache_head; NULL != rc; rc = rc->next)
873       if (0 == strcasecmp (rc->hostname,
874                            record->name))
875         break;
876     if (NULL == rc)
877     {
878       rc = GNUNET_new (struct ResolveCache);
879       rc->hostname = GNUNET_strdup (record->name);
880       GNUNET_CONTAINER_DLL_insert (cache_head,
881                                    cache_tail,
882                                    rc);
883       cache_size++;
884     }
885     /* TODO: ought to check first if we have this exact record
886        already in the cache! */
887     rle = GNUNET_new (struct RecordListEntry);
888     rle->record = GNUNET_DNSPARSER_duplicate_record (record);
889     GNUNET_CONTAINER_DLL_insert (rc->records_head,
890                                  rc->records_tail,
891                                  rle);
892   }
893   for (unsigned int i = 0; i != parsed->num_additional_records; i++)
894   {
895     struct GNUNET_DNSPARSER_Record *record = &parsed->additional_records[i];
896     struct RecordListEntry *rle;
897
898     for (rc = cache_head; NULL != rc; rc = rc->next)
899       if (0 == strcasecmp (rc->hostname,
900                            record->name))
901         break;
902     if (NULL == rc)
903     {
904       rc = GNUNET_new (struct ResolveCache);
905       rc->hostname = GNUNET_strdup (record->name);
906       GNUNET_CONTAINER_DLL_insert (cache_head,
907                                    cache_tail,
908                                    rc);
909       cache_size++;
910     }
911     /* TODO: ought to check first if we have this exact record
912        already in the cache! */
913     rle = GNUNET_new (struct RecordListEntry);
914     rle->record = GNUNET_DNSPARSER_duplicate_record (record);
915     GNUNET_CONTAINER_DLL_insert (rc->records_head,
916                                  rc->records_tail,
917                                  rle);
918   }
919   /* see if we need to do the 2nd request for AAAA records */
920   if ( (GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
921        (GNUNET_NO == al->did_aaaa) )
922   {
923     char *packet_buf;
924     size_t packet_size;
925     uint16_t dns_id;
926
927     dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
928                                                   UINT16_MAX);
929     if (GNUNET_OK ==
930         pack (al->hostname,
931               GNUNET_DNSPARSER_TYPE_AAAA,
932               dns_id,
933               &packet_buf,
934               &packet_size))
935     {
936       al->did_aaaa = GNUNET_YES;
937       al->dns_id = dns_id;
938       GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
939       al->resolve_handle =
940         GNUNET_DNSSTUB_resolve (dnsstub_ctx,
941                                 packet_buf,
942                                 packet_size,
943                                 &handle_resolve_result,
944                                 al);
945       GNUNET_free (packet_buf);
946       GNUNET_DNSPARSER_free_packet (parsed);
947       return;
948     }
949   }
950
951   /* resume by trying again from cache */
952   if (GNUNET_NO ==
953       try_cache (al->hostname,
954                  al->record_type,
955                  al->client_request_id,
956                  al->client))
957     /* cache failed, tell client we could not get an answer */
958   {
959     send_end_msg (al->client_request_id,
960                   al->client);
961   }
962   free_active_lookup (al);
963   GNUNET_DNSPARSER_free_packet (parsed);
964 }
965
966
967 /**
968  * We encountered a timeout trying to perform a
969  * DNS lookup.
970  *
971  * @param cls a `struct ActiveLookup`
972  */
973 static void
974 handle_resolve_timeout (void *cls)
975 {
976   struct ActiveLookup *al = cls;
977
978   al->timeout_task = NULL;
979   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980               "DNS lookup timeout!\n");
981   send_end_msg (al->client_request_id,
982                 al->client);
983   free_active_lookup (al);
984 }
985
986
987 /**
988  * Initiate an active lookup, then cache the result and
989  * try to then complete the resolution.
990  *
991  * @param hostname DNS name to resolve
992  * @param record_type record type to locate
993  * @param client_request_id client request ID
994  * @param client handle to the client
995  * @return #GNUNET_OK if the DNS query is now pending
996  */
997 static int
998 resolve_and_cache (const char* hostname,
999                    uint16_t record_type,
1000                    uint32_t client_request_id,
1001                    struct GNUNET_SERVICE_Client *client)
1002 {
1003   char *packet_buf;
1004   size_t packet_size;
1005   struct ActiveLookup *al;
1006   uint16_t dns_id;
1007   uint16_t type;
1008
1009   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010               "resolve_and_cache `%s'\n",
1011               hostname);
1012   dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1013                                                 UINT16_MAX);
1014
1015   if (GNUNET_DNSPARSER_TYPE_ALL == record_type)
1016     type = GNUNET_DNSPARSER_TYPE_A;
1017   else
1018     type = record_type;
1019   if (GNUNET_OK !=
1020       pack (hostname,
1021             type,
1022             dns_id,
1023             &packet_buf,
1024             &packet_size))
1025   {
1026     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1027                 "Failed to pack query for hostname `%s'\n",
1028                 hostname);
1029     return GNUNET_SYSERR;
1030   }
1031
1032   al = GNUNET_new (struct ActiveLookup);
1033   al->hostname = GNUNET_strdup (hostname);
1034   al->record_type = record_type;
1035   al->client_request_id = client_request_id;
1036   al->dns_id = dns_id;
1037   al->client = client;
1038   al->timeout_task = GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT,
1039                                                    &handle_resolve_timeout,
1040                                                    al);
1041   al->resolve_handle =
1042     GNUNET_DNSSTUB_resolve (dnsstub_ctx,
1043                             packet_buf,
1044                             packet_size,
1045                             &handle_resolve_result,
1046                             al);
1047   GNUNET_free (packet_buf);
1048   GNUNET_CONTAINER_DLL_insert (lookup_head,
1049                                lookup_tail,
1050                                al);
1051   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052               "Resolving %s, client_request_id = %u, dns_id = %u\n",
1053               hostname,
1054               (unsigned int) client_request_id,
1055               (unsigned int) dns_id);
1056   return GNUNET_OK;
1057 }
1058
1059
1060 /**
1061  * Process DNS request for @a hostname with request ID @a client_request_id
1062  * from @a client demanding records of type @a record_type.
1063  *
1064  * @param hostname DNS name to resolve
1065  * @param record_type desired record type
1066  * @param client_request_id client's request ID
1067  * @param client who should get the result?
1068  */
1069 static void
1070 process_get (const char *hostname,
1071              uint16_t record_type,
1072              uint32_t client_request_id,
1073              struct GNUNET_SERVICE_Client *client)
1074 {
1075   char fqdn[255];
1076
1077   if (  (NULL != my_domain) &&
1078         (NULL == strchr (hostname,
1079                          (unsigned char) '.')) &&
1080         (strlen (hostname) + strlen (my_domain) <= 253) )
1081   {
1082     GNUNET_snprintf (fqdn,
1083                      sizeof (fqdn),
1084                      "%s.%s",
1085                      hostname,
1086                      my_domain);
1087   }
1088   else if (strlen (hostname) < 255)
1089   {
1090     GNUNET_snprintf (fqdn,
1091                      sizeof (fqdn),
1092                      "%s",
1093                      hostname);
1094   }
1095   else
1096   {
1097     GNUNET_break (0);
1098     GNUNET_SERVICE_client_drop (client);
1099     return;
1100   }
1101   if (GNUNET_NO ==
1102       try_cache (fqdn,
1103                  record_type,
1104                  client_request_id,
1105                  client))
1106   {
1107     if (GNUNET_OK !=
1108         resolve_and_cache (fqdn,
1109                            record_type,
1110                            client_request_id,
1111                            client))
1112     {
1113       send_end_msg (client_request_id,
1114                     client);
1115     }
1116   }
1117 }
1118
1119
1120 /**
1121  * Verify well-formedness of GET-message.
1122  *
1123  * @param cls closure, unused
1124  * @param get the actual message
1125  * @return #GNUNET_OK if @a get is well-formed
1126  */
1127 static int
1128 check_get (void *cls,
1129            const struct GNUNET_RESOLVER_GetMessage *get)
1130 {
1131   uint16_t size;
1132   int direction;
1133   int af;
1134
1135   (void) cls;
1136   size = ntohs (get->header.size) - sizeof (*get);
1137   direction = ntohl (get->direction);
1138   if (GNUNET_NO == direction)
1139   {
1140     GNUNET_MQ_check_zero_termination (get);
1141     return GNUNET_OK;
1142   }
1143   af = ntohl (get->af);
1144   switch (af)
1145   {
1146   case AF_INET:
1147     if (size != sizeof (struct in_addr))
1148     {
1149       GNUNET_break (0);
1150       return GNUNET_SYSERR;
1151     }
1152     break;
1153   case AF_INET6:
1154     if (size != sizeof (struct in6_addr))
1155     {
1156       GNUNET_break (0);
1157       return GNUNET_SYSERR;
1158     }
1159     break;
1160   default:
1161     GNUNET_break (0);
1162     return GNUNET_SYSERR;
1163   }
1164   return GNUNET_OK;
1165 }
1166
1167
1168 /**
1169  * Handle GET-message.
1170  *
1171  * @param cls identification of the client
1172  * @param msg the actual message
1173  */
1174 static void
1175 handle_get (void *cls,
1176             const struct GNUNET_RESOLVER_GetMessage *msg)
1177 {
1178   struct GNUNET_SERVICE_Client *client = cls;
1179   int direction;
1180   int af;
1181   uint32_t client_request_id;
1182   char *hostname;
1183
1184   direction = ntohl (msg->direction);
1185   af = ntohl (msg->af);
1186   client_request_id = msg->client_id;
1187   GNUNET_SERVICE_client_continue (client);
1188   if (GNUNET_NO == direction)
1189   {
1190     /* IP from hostname */
1191     hostname = GNUNET_strdup ((const char *) &msg[1]);
1192     switch (af)
1193     {
1194       case AF_UNSPEC:
1195       {
1196         process_get (hostname,
1197                      GNUNET_DNSPARSER_TYPE_ALL,
1198                      client_request_id,
1199                      client);
1200         break;
1201       }
1202       case AF_INET:
1203       {
1204         process_get (hostname,
1205                      GNUNET_DNSPARSER_TYPE_A,
1206                      client_request_id,
1207                      client);
1208         break;
1209       }
1210       case AF_INET6:
1211       {
1212         process_get (hostname,
1213                      GNUNET_DNSPARSER_TYPE_AAAA,
1214                      client_request_id,
1215                      client);
1216         break;
1217       }
1218       default:
1219       {
1220         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1221                     "got invalid af: %d\n",
1222                     af);
1223         GNUNET_assert (0);
1224       }
1225     }
1226   }
1227   else
1228   {
1229     /* hostname from IP */
1230     hostname = make_reverse_hostname (&msg[1],
1231                                       af);
1232     process_get (hostname,
1233                  GNUNET_DNSPARSER_TYPE_PTR,
1234                  client_request_id,
1235                  client);
1236   }
1237   GNUNET_free_non_null (hostname);
1238 }
1239
1240
1241 /**
1242  * Service is shutting down, clean up.
1243  *
1244  * @param cls NULL, unused
1245  */
1246 static void
1247 shutdown_task (void *cls)
1248 {
1249   (void) cls;
1250
1251   while (NULL != lookup_head)
1252     free_active_lookup (lookup_head);
1253   while (NULL != cache_head)
1254     free_cache_entry (cache_head);
1255   while (NULL != hosts_head)
1256     free_hosts_entry (hosts_head);
1257   GNUNET_DNSSTUB_stop (dnsstub_ctx);
1258   GNUNET_free_non_null (my_domain);
1259 }
1260
1261
1262 /**
1263  * Add information about a host from /etc/hosts
1264  * to our cache.
1265  *
1266  * @param hostname the name of the host
1267  * @param rec_type DNS record type to use
1268  * @param data payload
1269  * @param data_size number of bytes in @a data
1270  */
1271 static void
1272 add_host (const char *hostname,
1273           uint16_t rec_type,
1274           const void *data,
1275           size_t data_size)
1276 {
1277   struct ResolveCache *rc;
1278   struct RecordListEntry *rle;
1279   struct GNUNET_DNSPARSER_Record *rec;
1280
1281   rec = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Record));
1282   rec->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS;
1283   rec->type = rec_type;
1284   rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1285   rec->name = GNUNET_strdup (hostname);
1286   rec->data.raw.data = GNUNET_memdup (data,
1287                                       data_size);
1288   rec->data.raw.data_len = data_size;
1289   rle = GNUNET_new (struct RecordListEntry);
1290   rle->record = rec;
1291   rc = GNUNET_new (struct ResolveCache);
1292   rc->hostname = GNUNET_strdup (hostname);
1293   GNUNET_CONTAINER_DLL_insert (rc->records_head,
1294                                rc->records_tail,
1295                                rle);
1296   GNUNET_CONTAINER_DLL_insert (hosts_head,
1297                                hosts_tail,
1298                                rc);
1299 }
1300
1301
1302 /**
1303  * Extract host information from a line in /etc/hosts
1304  *
1305  * @param line the line to parse
1306  * @param line_len number of bytes in @a line
1307  */
1308 static void
1309 extract_hosts (const char *line,
1310                size_t line_len)
1311 {
1312   const char *c;
1313   struct in_addr v4;
1314   struct in6_addr v6;
1315   char *tbuf;
1316   char *tok;
1317
1318   /* ignore everything after '#' */
1319   c = memrchr (line,
1320                (unsigned char) '#',
1321                line_len);
1322   if (NULL != c)
1323     line_len = c - line;
1324   /* ignore leading whitespace */
1325   while ( (0 < line_len) &&
1326           isspace ((unsigned char) *line) )
1327   {
1328     line++;
1329     line_len--;
1330   }
1331   tbuf = GNUNET_strndup (line,
1332                          line_len);
1333   tok = strtok (tbuf, " \t");
1334   if (NULL == tok)
1335   {
1336     GNUNET_free (tbuf);
1337     return;
1338   }
1339   if (1 == inet_pton (AF_INET,
1340                       tok,
1341                       &v4))
1342   {
1343     while (NULL != (tok = strtok (NULL, " \t")))
1344       add_host (tok,
1345                 GNUNET_DNSPARSER_TYPE_A,
1346                 &v4,
1347                 sizeof (struct in_addr));
1348   }
1349   else if (1 == inet_pton (AF_INET6,
1350                            tok,
1351                            &v6))
1352   {
1353     while (NULL != (tok = strtok (NULL, " \t")))
1354       add_host (tok,
1355                 GNUNET_DNSPARSER_TYPE_AAAA,
1356                 &v6,
1357                 sizeof (struct in6_addr));
1358   }
1359   GNUNET_free (tbuf);
1360 }
1361
1362
1363 /**
1364  * Reads the list of hosts from /etc/hosts.
1365  */
1366 static void
1367 load_etc_hosts (void)
1368 {
1369   struct GNUNET_DISK_FileHandle *fh;
1370   struct GNUNET_DISK_MapHandle *mh;
1371   off_t bytes_read;
1372   const char *buf;
1373   size_t read_offset;
1374
1375   fh = GNUNET_DISK_file_open ("/etc/hosts",
1376                               GNUNET_DISK_OPEN_READ,
1377                               GNUNET_DISK_PERM_NONE);
1378   if (NULL == fh)
1379   {
1380     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1381                 "Failed to open /etc/hosts");
1382     return;
1383   }
1384   if (GNUNET_OK !=
1385       GNUNET_DISK_file_handle_size (fh,
1386                                     &bytes_read))
1387   {
1388     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1389                 "Could not determin size of /etc/hosts. "
1390                 "DNS resolution will not be possible.\n");
1391     GNUNET_DISK_file_close (fh);
1392     return;
1393   }
1394   if ((size_t) bytes_read > SIZE_MAX)
1395   {
1396     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1397                 "/etc/hosts file too large to mmap. "
1398                 "DNS resolution will not be possible.\n");
1399     GNUNET_DISK_file_close (fh);
1400     return;
1401   }
1402   buf = GNUNET_DISK_file_map (fh,
1403                               &mh,
1404                               GNUNET_DISK_MAP_TYPE_READ,
1405                               (size_t) bytes_read);
1406   read_offset = 0;
1407   while (read_offset < (size_t) bytes_read)
1408   {
1409     const char *newline;
1410     size_t line_len;
1411
1412     newline = strchr (buf + read_offset,
1413                       '\n');
1414     if (NULL == newline)
1415       break;
1416     line_len = newline - buf - read_offset;
1417     extract_hosts (buf + read_offset,
1418                    line_len);
1419     read_offset += line_len + 1;
1420   }
1421   GNUNET_DISK_file_unmap (mh);
1422   GNUNET_DISK_file_close (fh);
1423 }
1424
1425
1426 /**
1427  * Service is starting, initialize everything.
1428  *
1429  * @param cls NULL, unused
1430  * @param cfg our configuration
1431  * @param sh service handle
1432  */
1433 static void
1434 init_cb (void *cls,
1435          const struct GNUNET_CONFIGURATION_Handle *cfg,
1436          struct GNUNET_SERVICE_Handle *sh)
1437 {
1438   char **dns_servers;
1439   int num_dns_servers;
1440
1441   (void) cfg;
1442   (void) sh;
1443   load_etc_hosts ();
1444   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1445                                  cls);
1446   dnsstub_ctx = GNUNET_DNSSTUB_start (128);
1447   dns_servers = NULL;
1448   num_dns_servers = lookup_dns_servers (&dns_servers);
1449   if (0 >= num_dns_servers)
1450   {
1451     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1452                 _("No DNS server available. DNS resolution will not be possible.\n"));
1453     return;
1454   }
1455   for (int i = 0; i < num_dns_servers; i++)
1456   {
1457     int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1458     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459                 "Adding DNS server '%s': %s\n",
1460                 dns_servers[i],
1461                 GNUNET_OK == result ? "success" : "failure");
1462     GNUNET_free (dns_servers[i]);
1463   }
1464   GNUNET_free_non_null (dns_servers);
1465 }
1466
1467
1468 /**
1469  * Callback called when a client connects to the service.
1470  *
1471  * @param cls closure for the service, unused
1472  * @param c the new client that connected to the service
1473  * @param mq the message queue used to send messages to the client
1474  * @return @a c
1475  */
1476 static void *
1477 connect_cb (void *cls,
1478             struct GNUNET_SERVICE_Client *c,
1479             struct GNUNET_MQ_Handle *mq)
1480 {
1481   (void) cls;
1482   (void) mq;
1483
1484   return c;
1485 }
1486
1487
1488 /**
1489  * Callback called when a client disconnected from the service
1490  *
1491  * @param cls closure for the service
1492  * @param c the client that disconnected
1493  * @param internal_cls should be equal to @a c
1494  */
1495 static void
1496 disconnect_cb (void *cls,
1497                struct GNUNET_SERVICE_Client *c,
1498                void *internal_cls)
1499 {
1500   struct ActiveLookup *n;
1501   (void) cls;
1502
1503   GNUNET_assert (c == internal_cls);
1504   n = lookup_head;
1505   for (struct ActiveLookup *al = n;
1506        NULL != al;
1507        al = n)
1508   {
1509     n = al->next;
1510     if (al->client == c)
1511       free_active_lookup (al);
1512   }
1513 }
1514
1515
1516 /**
1517  * Define "main" method using service macro.
1518  */
1519 GNUNET_SERVICE_MAIN
1520 ("resolver",
1521  GNUNET_SERVICE_OPTION_NONE,
1522  &init_cb,
1523  &connect_cb,
1524  &disconnect_cb,
1525  NULL,
1526  GNUNET_MQ_hd_var_size (get,
1527                         GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1528                         struct GNUNET_RESOLVER_GetMessage,
1529                         NULL),
1530  GNUNET_MQ_handler_end ());
1531
1532
1533 #if defined(LINUX) && defined(__GLIBC__)
1534 #include <malloc.h>
1535
1536 /**
1537  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1538  */
1539 void __attribute__ ((constructor))
1540 GNUNET_RESOLVER_memory_init ()
1541 {
1542   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1543   mallopt (M_TOP_PAD, 1 * 1024);
1544   malloc_trim (0);
1545 }
1546 #endif
1547
1548
1549 /* end of gnunet-service-resolver.c */