fix macro test
[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   const char *str = &get[1];
1127   direction = ntohl (get->direction);
1128   if (GNUNET_NO == direction)
1129   {
1130     GNUNET_MQ_check_zero_termination (get);
1131     return GNUNET_OK;
1132   }
1133   af = ntohl (get->af);
1134   switch (af)
1135   {
1136   case AF_INET:
1137     if (size != sizeof (struct in_addr))
1138     {
1139       GNUNET_break (0);
1140       return GNUNET_SYSERR;
1141     }
1142     break;
1143   case AF_INET6:
1144     if (size != sizeof (struct in6_addr))
1145     {
1146       GNUNET_break (0);
1147       return GNUNET_SYSERR;
1148     }
1149     break;
1150   default:
1151     GNUNET_break (0);
1152     return GNUNET_SYSERR;
1153   }
1154   return GNUNET_OK;
1155 }
1156
1157
1158 /**
1159  * Handle GET-message.
1160  *
1161  * @param cls identification of the client
1162  * @param msg the actual message
1163  */
1164 static void
1165 handle_get (void *cls,
1166             const struct GNUNET_RESOLVER_GetMessage *msg)
1167 {
1168   struct GNUNET_SERVICE_Client *client = cls;
1169   int direction;
1170   int af;
1171   uint32_t client_request_id;
1172   char *hostname;
1173
1174   direction = ntohl (msg->direction);
1175   af = ntohl (msg->af);
1176   client_request_id = msg->client_id;
1177   GNUNET_SERVICE_client_continue (client);
1178   if (GNUNET_NO == direction)
1179   {
1180     /* IP from hostname */
1181     hostname = GNUNET_strdup ((const char *) &msg[1]);
1182     switch (af)
1183     {
1184       case AF_UNSPEC:
1185       {
1186         process_get (hostname,
1187                      GNUNET_DNSPARSER_TYPE_ALL,
1188                      client_request_id,
1189                      client);
1190         break;
1191       }
1192       case AF_INET:
1193       {
1194         process_get (hostname,
1195                      GNUNET_DNSPARSER_TYPE_A,
1196                      client_request_id,
1197                      client);
1198         break;
1199       }
1200       case AF_INET6:
1201       {
1202         process_get (hostname,
1203                      GNUNET_DNSPARSER_TYPE_AAAA,
1204                      client_request_id,
1205                      client);
1206         break;
1207       }
1208       default:
1209       {
1210         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211                     "got invalid af: %d\n",
1212                     af);
1213         GNUNET_assert (0);
1214       }
1215     }
1216   }
1217   else
1218   {
1219     /* hostname from IP */
1220     hostname = make_reverse_hostname (&msg[1],
1221                                       af);
1222     process_get (hostname,
1223                  GNUNET_DNSPARSER_TYPE_PTR,
1224                  client_request_id,
1225                  client);
1226   }
1227   GNUNET_free_non_null (hostname);
1228 }
1229
1230
1231 /**
1232  * Service is shutting down, clean up.
1233  *
1234  * @param cls NULL, unused
1235  */
1236 static void
1237 shutdown_task (void *cls)
1238 {
1239   (void) cls;
1240
1241   while (NULL != lookup_head)
1242     free_active_lookup (lookup_head);
1243   while (NULL != cache_head)
1244     free_cache_entry (cache_head);
1245   while (NULL != hosts_head)
1246     free_hosts_entry (hosts_head);
1247   GNUNET_DNSSTUB_stop (dnsstub_ctx);
1248   GNUNET_free (my_domain);
1249 }
1250
1251
1252 /**
1253  * Add information about a host from /etc/hosts
1254  * to our cache.
1255  *
1256  * @param hostname the name of the host
1257  * @param rec_type DNS record type to use
1258  * @param data payload
1259  * @param data_size number of bytes in @a data
1260  */
1261 static void
1262 add_host (const char *hostname,
1263           uint16_t rec_type,
1264           const void *data,
1265           size_t data_size)
1266 {
1267   struct ResolveCache *rc;
1268   struct RecordListEntry *rle;
1269   struct GNUNET_DNSPARSER_Record *rec;
1270
1271   rec = GNUNET_malloc (sizeof (struct GNUNET_DNSPARSER_Record));
1272   rec->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS;
1273   rec->type = rec_type;
1274   rec->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
1275   rec->name = GNUNET_strdup (hostname);
1276   rec->data.raw.data = GNUNET_memdup (data,
1277                                       data_size);
1278   rec->data.raw.data_len = data_size;
1279   rle = GNUNET_new (struct RecordListEntry);
1280   rle->record = rec;
1281   rc = GNUNET_new (struct ResolveCache);
1282   rc->hostname = GNUNET_strdup (hostname);
1283   GNUNET_CONTAINER_DLL_insert (rc->records_head,
1284                                rc->records_tail,
1285                                rle);
1286   GNUNET_CONTAINER_DLL_insert (hosts_head,
1287                                hosts_tail,
1288                                rc);
1289 }
1290
1291
1292 /**
1293  * Extract host information from a line in /etc/hosts
1294  *
1295  * @param line the line to parse
1296  * @param line_len number of bytes in @a line
1297  */
1298 static void
1299 extract_hosts (const char *line,
1300                size_t line_len)
1301 {
1302   const char *c;
1303   struct in_addr v4;
1304   struct in6_addr v6;
1305   char *tbuf;
1306   char *tok;
1307
1308   /* ignore everything after '#' */
1309   c = memrchr (line,
1310                (unsigned char) '#',
1311                line_len);
1312   if (NULL != c)
1313     line_len = c - line;
1314   /* ignore leading whitespace */
1315   while ( (0 < line_len) &&
1316           isspace ((unsigned char) *line) )
1317   {
1318     line++;
1319     line_len--;
1320   }
1321   tbuf = GNUNET_strndup (line,
1322                          line_len);
1323   tok = strtok (tbuf, " \t");
1324   if (NULL == tok)
1325   {
1326     GNUNET_free (tbuf);
1327     return;
1328   }
1329   if (1 == inet_pton (AF_INET,
1330                       tok,
1331                       &v4))
1332   {
1333     while (NULL != (tok = strtok (NULL, " \t")))
1334       add_host (tok,
1335                 GNUNET_DNSPARSER_TYPE_A,
1336                 &v4,
1337                 sizeof (struct in_addr));
1338   }
1339   else if (1 == inet_pton (AF_INET6,
1340                            tok,
1341                            &v6))
1342   {
1343     while (NULL != (tok = strtok (NULL, " \t")))
1344       add_host (tok,
1345                 GNUNET_DNSPARSER_TYPE_AAAA,
1346                 &v6,
1347                 sizeof (struct in6_addr));
1348   }
1349   GNUNET_free (tbuf);
1350 }
1351
1352
1353 /**
1354  * Reads the list of hosts from /etc/hosts.
1355  */
1356 static void
1357 load_etc_hosts (void)
1358 {
1359   struct GNUNET_DISK_FileHandle *fh;
1360   struct GNUNET_DISK_MapHandle *mh;
1361   off_t bytes_read;
1362   const char *buf;
1363   size_t read_offset;
1364
1365   fh = GNUNET_DISK_file_open ("/etc/hosts",
1366                               GNUNET_DISK_OPEN_READ,
1367                               GNUNET_DISK_PERM_NONE);
1368   if (NULL == fh)
1369   {
1370     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1371                 "Failed to open /etc/hosts");
1372     return;
1373   }
1374   if (GNUNET_OK !=
1375       GNUNET_DISK_file_handle_size (fh,
1376                                     &bytes_read))
1377   {
1378     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1379                 "Could not determin size of /etc/hosts. "
1380                 "DNS resolution will not be possible.\n");
1381     GNUNET_DISK_file_close (fh);
1382     return;
1383   }
1384   if ((size_t) bytes_read > SIZE_MAX)
1385   {
1386     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1387                 "/etc/hosts file too large to mmap. "
1388                 "DNS resolution will not be possible.\n");
1389     GNUNET_DISK_file_close (fh);
1390     return;
1391   }
1392   buf = GNUNET_DISK_file_map (fh,
1393                               &mh,
1394                               GNUNET_DISK_MAP_TYPE_READ,
1395                               (size_t) bytes_read);
1396   read_offset = 0;
1397   while (read_offset < (size_t) bytes_read)
1398   {
1399     const char *newline;
1400     size_t line_len;
1401
1402     newline = strchr (buf + read_offset,
1403                       '\n');
1404     if (NULL == newline)
1405       break;
1406     line_len = newline - buf - read_offset;
1407     extract_hosts (buf + read_offset,
1408                    line_len);
1409     read_offset += line_len + 1;
1410   }
1411   GNUNET_DISK_file_unmap (mh);
1412   GNUNET_DISK_file_close (fh);
1413 }
1414
1415
1416 /**
1417  * Service is starting, initialize everything.
1418  *
1419  * @param cls NULL, unused
1420  * @param cfg our configuration
1421  * @param sh service handle
1422  */
1423 static void
1424 init_cb (void *cls,
1425          const struct GNUNET_CONFIGURATION_Handle *cfg,
1426          struct GNUNET_SERVICE_Handle *sh)
1427 {
1428   char **dns_servers;
1429   int num_dns_servers;
1430
1431   (void) cfg;
1432   (void) sh;
1433   load_etc_hosts ();
1434   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1435                                  cls);
1436   dnsstub_ctx = GNUNET_DNSSTUB_start (128);
1437   dns_servers = NULL;
1438   num_dns_servers = lookup_dns_servers (&dns_servers);
1439   if (0 >= num_dns_servers)
1440   {
1441     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1442                 _("No DNS server available. DNS resolution will not be possible.\n"));
1443     return;
1444   }
1445   for (int i = 0; i < num_dns_servers; i++)
1446   {
1447     int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1448     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449                 "Adding DNS server '%s': %s\n",
1450                 dns_servers[i],
1451                 GNUNET_OK == result ? "success" : "failure");
1452     GNUNET_free (dns_servers[i]);
1453   }
1454   GNUNET_free_non_null (dns_servers);
1455 }
1456
1457
1458 /**
1459  * Callback called when a client connects to the service.
1460  *
1461  * @param cls closure for the service, unused
1462  * @param c the new client that connected to the service
1463  * @param mq the message queue used to send messages to the client
1464  * @return @a c
1465  */
1466 static void *
1467 connect_cb (void *cls,
1468             struct GNUNET_SERVICE_Client *c,
1469             struct GNUNET_MQ_Handle *mq)
1470 {
1471   (void) cls;
1472   (void) mq;
1473
1474   return c;
1475 }
1476
1477
1478 /**
1479  * Callback called when a client disconnected from the service
1480  *
1481  * @param cls closure for the service
1482  * @param c the client that disconnected
1483  * @param internal_cls should be equal to @a c
1484  */
1485 static void
1486 disconnect_cb (void *cls,
1487                struct GNUNET_SERVICE_Client *c,
1488                void *internal_cls)
1489 {
1490   struct ActiveLookup *n;
1491   (void) cls;
1492
1493   GNUNET_assert (c == internal_cls);
1494   n = lookup_head;
1495   for (struct ActiveLookup *al = n;
1496        NULL != al;
1497        al = n)
1498   {
1499     n = al->next;
1500     if (al->client == c)
1501       free_active_lookup (al);
1502   }
1503 }
1504
1505
1506 /**
1507  * Define "main" method using service macro.
1508  */
1509 GNUNET_SERVICE_MAIN
1510 ("resolver",
1511  GNUNET_SERVICE_OPTION_NONE,
1512  &init_cb,
1513  &connect_cb,
1514  &disconnect_cb,
1515  NULL,
1516  GNUNET_MQ_hd_var_size (get,
1517                         GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1518                         struct GNUNET_RESOLVER_GetMessage,
1519                         NULL),
1520  GNUNET_MQ_handler_end ());
1521
1522
1523 #if defined(LINUX) && defined(__GLIBC__)
1524 #include <malloc.h>
1525
1526 /**
1527  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1528  */
1529 void __attribute__ ((constructor))
1530 GNUNET_RESOLVER_memory_init ()
1531 {
1532   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1533   mallopt (M_TOP_PAD, 1 * 1024);
1534   malloc_trim (0);
1535 }
1536 #endif
1537
1538
1539 /* end of gnunet-service-resolver.c */