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