fix CNAME handling, caching, out-of-bounds accesses, etc. in gnunet-service-resolver
[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  * A cached DNS lookup result.
65  */
66 struct ResolveCache
67 {
68   /**
69    * This is a doubly linked list.
70    */
71   struct ResolveCache *next;
72
73   /**
74    * This is a doubly linked list.
75    */
76   struct ResolveCache *prev;
77
78   /**
79    * Which hostname is this cache for?
80    */
81   char *hostname;
82
83   /**
84    * head of a double linked list containing the lookup results
85    */
86   struct RecordListEntry *records_head;
87
88   /**
89    * tail of a double linked list containing the lookup results
90    */
91   struct RecordListEntry *records_tail;
92
93 };
94
95
96 /**
97  * Information about pending lookups.
98  */
99 struct ActiveLookup
100 {
101   /**
102    * Stored in a DLL.
103    */
104   struct ActiveLookup *next;
105
106   /**
107    * Stored in a DLL.
108    */
109   struct ActiveLookup *prev;
110
111   /**
112    * The client that queried the records contained in this cache entry.
113    */
114   struct GNUNET_SERVICE_Client *client;
115
116   /**
117    * handle for cancelling a request
118    */
119   struct GNUNET_DNSSTUB_RequestSocket *resolve_handle;
120
121   /**
122    * handle for the resolution timeout task
123    */
124   struct GNUNET_SCHEDULER_Task *timeout_task;
125
126   /**
127    * Which hostname are we resolving?
128    */
129   char *hostname;
130
131   /**
132    * type of queried DNS record
133    */
134   uint16_t record_type;
135
136   /**
137    * Unique request ID of a client if a query for this hostname/record_type
138    * is currently pending, undefined otherwise.
139    */
140   uint16_t request_id;
141
142   /**
143    * Unique DNS request ID of a client if a query for this hostname/record_type
144    * is currently pending, undefined otherwise.
145    */
146   uint16_t dns_id;
147
148 };
149
150
151 /**
152  * Start of the linked list of cached DNS lookup results.
153  */
154 static struct ResolveCache *cache_head;
155
156 /**
157  * Tail of the linked list of cached DNS lookup results.
158  */
159 static struct ResolveCache *cache_tail;
160
161 /**
162  * Start of the linked list of active DNS lookups.
163  */
164 static struct ActiveLookup *lookup_head;
165
166 /**
167  * Tail of the linked list of active DNS lookups.
168  */
169 static struct ActiveLookup *lookup_tail;
170
171 /**
172  * context of dnsstub library
173  */
174 static struct GNUNET_DNSSTUB_Context *dnsstub_ctx;
175
176 /**
177  * How many entries do we have in #cache_head DLL?
178  */
179 static unsigned int cache_size;
180
181 /**
182  * Remove @a entry from cache.
183  *
184  * @param rc entry to free
185  */
186 static void
187 free_cache_entry (struct ResolveCache *rc)
188 {
189   struct RecordListEntry *pos;
190
191   while (NULL != (pos = rc->records_head))
192   {
193     GNUNET_CONTAINER_DLL_remove (rc->records_head,
194                                  rc->records_tail,
195                                  pos);
196     GNUNET_DNSPARSER_free_record (pos->record);
197     GNUNET_free (pos->record);
198     GNUNET_free (pos);
199   }
200   GNUNET_free_non_null (rc->hostname);
201   GNUNET_CONTAINER_DLL_remove (cache_head,
202                                cache_tail,
203                                rc);
204   cache_size--;
205   GNUNET_free (rc);
206 }
207
208
209 /**
210  * Release resources associated with @a al
211  *
212  * @param al an active lookup
213  */
214 static void
215 free_active_lookup (struct ActiveLookup *al)
216 {
217   GNUNET_CONTAINER_DLL_remove (lookup_head,
218                                lookup_tail,
219                                al);
220   if (NULL != al->resolve_handle)
221   {
222     GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
223     al->resolve_handle = NULL;
224   }
225   if (NULL != al->timeout_task)
226   {
227     GNUNET_SCHEDULER_cancel (al->timeout_task);
228     al->timeout_task = NULL;
229   }
230   GNUNET_free_non_null (al->hostname);
231   GNUNET_free (al);
232 }
233
234
235
236 /**
237  * Find out if the configuration file line contains a string
238  * starting with "nameserver ", and if so, return a copy of
239  * the nameserver's IP.
240  *
241  * @param line line to parse
242  * @param line_len number of characters in @a line
243  * @return NULL if no nameserver is configured in this @a line
244  */
245 static char *
246 extract_dns_server (const char* line,
247                     size_t line_len)
248 {
249   if (0 == strncmp (line,
250                     "nameserver ",
251                     strlen ("nameserver ")))
252     return GNUNET_strndup (line + strlen ("nameserver "),
253                            line_len - strlen ("nameserver "));
254   return NULL;
255 }
256
257
258 /**
259  * Reads the list of nameservers from /etc/resolve.conf
260  *
261  * @param server_addrs[out] a list of null-terminated server address strings
262  * @return the number of server addresses in @server_addrs, -1 on error
263  */
264 static int
265 lookup_dns_servers (char ***server_addrs)
266 {
267   struct GNUNET_DISK_FileHandle *fh;
268   char buf[2048];
269   ssize_t bytes_read;
270   size_t read_offset;
271   unsigned int num_dns_servers;
272
273   fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
274                               GNUNET_DISK_OPEN_READ,
275                               GNUNET_DISK_PERM_NONE);
276   if (NULL == fh)
277   {
278     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
279                 "Could not open /etc/resolv.conf. "
280                 "DNS resolution will not be possible.\n");
281     return -1;
282   }
283   bytes_read = GNUNET_DISK_file_read (fh,
284                                       buf,
285                                       sizeof (buf));
286   *server_addrs = NULL;
287   read_offset = 0;
288   num_dns_servers = 0;
289   while (read_offset < bytes_read)
290   {
291     const char *newline;
292     size_t line_len;
293     char *dns_server;
294
295     newline = strchr (buf + read_offset,
296                       '\n');
297     if (NULL == newline)
298       break;
299     line_len = newline - buf - read_offset;
300     dns_server = extract_dns_server (buf + read_offset,
301                                      line_len);
302     if (NULL != dns_server)
303       GNUNET_array_append (*server_addrs,
304                            num_dns_servers,
305                            dns_server);
306     read_offset += line_len + 1;
307   }
308   GNUNET_DISK_file_close (fh);
309   return (int) num_dns_servers;
310 }
311
312
313 /**
314  * Compute name to use for DNS reverse lookups from @a ip.
315  *
316  * @param ip IP address to resolve, in binary format, network byte order
317  * @param af address family of @a ip, AF_INET or AF_INET6
318  */
319 static char *
320 make_reverse_hostname (const void *ip,
321                        int af)
322 {
323   char *buf = GNUNET_new_array (80,
324                                 char);
325   int pos = 0;
326
327   if (AF_INET == af)
328   {
329     struct in_addr *addr = (struct in_addr *)ip;
330     uint32_t ip_int = addr->s_addr;
331
332     for (int i = 3; i >= 0; i--)
333     {
334       int n = GNUNET_snprintf (buf + pos,
335                                80 - pos,
336                                "%u.",
337                                ((uint8_t *)&ip_int)[i]);
338       if (n < 0)
339       {
340         GNUNET_free (buf);
341         return NULL;
342       }
343       pos += n;
344     }
345     pos += GNUNET_snprintf (buf + pos,
346                             80 - pos,
347                             "in-addr.arpa");
348   }
349   else if (AF_INET6 == af)
350   {
351     struct in6_addr *addr = (struct in6_addr *)ip;
352     for (int i = 15; i >= 0; i--)
353     {
354       int n = GNUNET_snprintf (buf + pos,
355                                80 - pos,
356                                "%x.",
357                                addr->s6_addr[i] & 0xf);
358       if (n < 0)
359       {
360         GNUNET_free (buf);
361         return NULL;
362       }
363       pos += n;
364       n = GNUNET_snprintf (buf + pos,
365                            80 - pos,
366                            "%x.",
367                            addr->s6_addr[i] >> 4);
368       if (n < 0)
369       {
370         GNUNET_free (buf);
371         return NULL;
372       }
373       pos += n;
374     }
375     pos += GNUNET_snprintf (buf + pos,
376                             80 - pos,
377                             "ip6.arpa");
378   }
379   buf[pos] = '\0';
380   return buf;
381 }
382
383
384 /**
385  * Send DNS @a record back to our @a client.
386  *
387  * @param record information to transmit
388  * @param record_type requested record type from client
389  * @param request_id to which request are we responding
390  * @param client where to send @a record
391  * @return #GNUNET_YES if we sent a reply,
392  *         #GNUNET_NO if the record type is not understood or
393  *         does not match @a record_type
394  */
395 static int
396 send_reply (struct GNUNET_DNSPARSER_Record *record,
397             uint16_t record_type,
398             uint16_t request_id,
399             struct GNUNET_SERVICE_Client *client)
400 {
401   struct GNUNET_RESOLVER_ResponseMessage *msg;
402   struct GNUNET_MQ_Envelope *env;
403   const void *payload;
404   size_t payload_len;
405
406   switch (record->type)
407   {
408   case GNUNET_DNSPARSER_TYPE_CNAME:
409     if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
410       return GNUNET_NO;
411     payload = record->data.hostname;
412     payload_len = strlen (record->data.hostname) + 1;
413     break;
414   case GNUNET_DNSPARSER_TYPE_PTR:
415     if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
416       return GNUNET_NO;
417     payload = record->data.hostname;
418     payload_len = strlen (record->data.hostname) + 1;
419     break;
420   case GNUNET_DNSPARSER_TYPE_A:
421     if ( (GNUNET_DNSPARSER_TYPE_A != record_type) &&
422          (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
423       return GNUNET_NO;
424     payload = record->data.raw.data;
425     payload_len = record->data.raw.data_len;
426     break;
427   case GNUNET_DNSPARSER_TYPE_AAAA:
428     if ( (GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
429          (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
430       return GNUNET_NO;
431     payload = record->data.raw.data;
432     payload_len = record->data.raw.data_len;
433     break;
434   default:
435     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436                 "Cannot handle DNS response type %u: not supported here\n",
437                 record->type);
438     return GNUNET_NO;
439   }
440   env = GNUNET_MQ_msg_extra (msg,
441                              payload_len,
442                              GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
443   msg->id = request_id;
444   GNUNET_memcpy (&msg[1],
445                  payload,
446                  payload_len);
447   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
448                   env);
449   return GNUNET_YES;
450 }
451
452
453 /**
454  * Send message to @a client that we transmitted all
455  * responses for @a request_id
456  *
457  * @param request_id to which request are we responding
458  * @param client where to send @a record
459  */
460 static void
461 send_end_msg (uint16_t request_id,
462               struct GNUNET_SERVICE_Client *client)
463 {
464   struct GNUNET_RESOLVER_ResponseMessage *msg;
465   struct GNUNET_MQ_Envelope *env;
466
467   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468               "Sending END message\n");
469   env = GNUNET_MQ_msg (msg,
470                        GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
471   msg->id = request_id;
472   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
473                   env);
474 }
475
476
477 /**
478  * Remove expired entries from @a rc
479  *
480  * @param rc entry in resolver cache
481  * @return #GNUNET_YES if @a rc was completely expired
482  *         #GNUNET_NO if some entries are left
483  */
484 static int
485 remove_expired (struct ResolveCache *rc)
486 {
487   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
488   struct RecordListEntry *n;
489
490   for (struct RecordListEntry *pos = rc->records_head;
491        NULL != pos;
492        pos = n)
493   {
494     n = pos->next;
495     if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
496       GNUNET_CONTAINER_DLL_remove (rc->records_head,
497                                    rc->records_tail,
498                                    pos);
499   }
500   if (NULL == rc->records_head)
501   {
502     free_cache_entry (rc);
503     return GNUNET_YES;
504   }
505   return GNUNET_NO;
506 }
507
508
509 /**
510  * Process DNS request for @a hostname with request ID @a request_id
511  * from @a client demanding records of type @a record_type.
512  *
513  * @param hostname DNS name to resolve
514  * @param record_type desired record type
515  * @param request_id client's request ID
516  * @param client who should get the result?
517  */
518 static void
519 process_get (const char *hostname,
520              uint16_t record_type,
521              uint16_t request_id,
522              struct GNUNET_SERVICE_Client *client);
523
524
525 /**
526  * Get an IP address as a string (works for both IPv4 and IPv6).  Note
527  * that the resolution happens asynchronously and that the first call
528  * may not immediately result in the FQN (but instead in a
529  * human-readable IP address).
530  *
531  * @param hostname what hostname was to be resolved
532  * @param record_type what type of record was requested
533  * @param request_id unique identification of the client's request
534  * @param client handle to the client making the request (for sending the reply)
535  */
536 static int
537 try_cache (const char *hostname,
538            uint16_t record_type,
539            uint16_t request_id,
540            struct GNUNET_SERVICE_Client *client)
541 {
542   struct ResolveCache *pos;
543   struct ResolveCache *next;
544   int found;
545
546   next = cache_head;
547   for (pos = next; NULL != pos; pos = next)
548   {
549     next = pos->next;
550     if (GNUNET_YES == remove_expired (pos))
551       continue;
552     if (0 == strcmp (pos->hostname,
553                      hostname))
554       break;
555   }
556   if (NULL == pos)
557   {
558     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559                 "No cache entry for '%s'\n",
560                 hostname);
561     return GNUNET_NO;
562   }
563   if (cache_head != pos)
564   {
565     /* move result to head to achieve LRU for cache eviction */
566     GNUNET_CONTAINER_DLL_remove (cache_head,
567                                  cache_tail,
568                                  pos);
569     GNUNET_CONTAINER_DLL_insert (cache_head,
570                                  cache_tail,
571                                  pos);
572   }
573   found = GNUNET_NO;
574   for (struct RecordListEntry *rle = pos->records_head;
575        NULL != rle;
576        rle = rle->next)
577   {
578     const struct GNUNET_DNSPARSER_Record *record = rle->record;
579
580     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581                 "Found cache entry for '%s', record type '%u'\n",
582                 hostname,
583                 record_type);
584     if ( (GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
585          (GNUNET_DNSPARSER_TYPE_CNAME != record_type) &&
586          (GNUNET_NO == found) )
587     {
588       const char *hostname = record->data.hostname;
589
590       process_get (hostname,
591                    record_type,
592                    request_id,
593                    client);
594       return GNUNET_YES; /* counts as a cache "hit" */
595     }
596     found |= send_reply (rle->record,
597                          record_type,
598                          request_id,
599                          client);
600   }
601   if (GNUNET_NO == found)
602     return GNUNET_NO; /* had records, but none matched! */
603   send_end_msg (request_id,
604                 client);
605   return GNUNET_YES;
606 }
607
608
609 /**
610  * We got a result from DNS. Add it to the cache and
611  * see if we can make our client happy...
612  *
613  * @param cls the `struct ActiveLookup`
614  * @param dns the DNS response
615  * @param dns_len number of bytes in @a dns
616  */
617 static void
618 handle_resolve_result (void *cls,
619                        const struct GNUNET_TUN_DnsHeader *dns,
620                        size_t dns_len)
621 {
622   struct ActiveLookup *al = cls;
623   struct GNUNET_DNSPARSER_Packet *parsed;
624   struct ResolveCache *rc;
625
626   parsed = GNUNET_DNSPARSER_parse ((const char *)dns,
627                                    dns_len);
628   if (NULL == parsed)
629   {
630     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
631                 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
632                 al->hostname,
633                 al->dns_id);
634     return;
635   }
636   if (al->dns_id != ntohs (parsed->id))
637   {
638     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639                 "Request ID in DNS reply does not match\n");
640     GNUNET_DNSPARSER_free_packet (parsed);
641     return;
642   }
643   if (0 == parsed->num_answers)
644   {
645     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646                 "DNS reply (hostname %s, request ID %u) contains no answers\n",
647                 al->hostname,
648                 al->request_id);
649     GNUNET_DNSPARSER_free_packet (parsed);
650     send_end_msg (al->request_id,
651                   al->client);
652     free_active_lookup (al);
653     return;
654   }
655   /* LRU-based cache eviction: we remove from tail */
656   while (cache_size > MAX_CACHE)
657     free_cache_entry (cache_tail);
658
659   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660               "Got reply for hostname %s and request ID %u\n",
661               al->hostname,
662               al->request_id);
663   /* add to cache */
664   for (unsigned int i = 0; i != parsed->num_answers; i++)
665   {
666     struct GNUNET_DNSPARSER_Record *record = &parsed->answers[i];
667     struct RecordListEntry *rle;
668
669     for (rc = cache_head; NULL != rc; rc = rc->next)
670       if (0 == strcasecmp (rc->hostname,
671                            record->name))
672         break;
673     if (NULL == rc)
674     {
675       rc = GNUNET_new (struct ResolveCache);
676       rc->hostname = GNUNET_strdup (record->name);
677       GNUNET_CONTAINER_DLL_insert (cache_head,
678                                    cache_tail,
679                                    rc);
680       cache_size++;
681     }
682     /* TODO: ought to check first if we have this exact record
683        already in the cache! */
684     rle = GNUNET_new (struct RecordListEntry);
685     rle->record = GNUNET_DNSPARSER_duplicate_record (record);
686     GNUNET_CONTAINER_DLL_insert (rc->records_head,
687                                  rc->records_tail,
688                                  rle);
689   }
690
691   /* resume by trying again from cache */
692   if (GNUNET_NO ==
693       try_cache (al->hostname,
694                  al->record_type,
695                  al->request_id,
696                  al->client))
697     /* cache failed, tell client we could not get an answer */
698     send_end_msg (al->request_id,
699                   al->client);
700   free_active_lookup (al);
701   GNUNET_DNSPARSER_free_packet (parsed);
702 }
703
704
705 /**
706  * We encountered a timeout trying to perform a
707  * DNS lookup.
708  *
709  * @param cls a `struct ActiveLookup`
710  */
711 static void
712 handle_resolve_timeout (void *cls)
713 {
714   struct ActiveLookup *al = cls;
715
716   al->timeout_task = NULL;
717   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718               "DNS lookup timeout!\n");
719   send_end_msg (al->request_id,
720                 al->client);
721   free_active_lookup (al);
722 }
723
724
725 /**
726  * Initiate an active lookup, then cache the result and
727  * try to then complete the resolution.
728  *
729  * @param hostname DNS name to resolve
730  * @param record_type record type to locate
731  * @param request_id client request ID
732  * @param client handle to the client
733  */
734 static int
735 resolve_and_cache (const char* hostname,
736                    uint16_t record_type,
737                    uint16_t request_id,
738                    struct GNUNET_SERVICE_Client *client)
739 {
740   char *packet_buf;
741   size_t packet_size;
742   struct GNUNET_DNSPARSER_Query query;
743   struct GNUNET_DNSPARSER_Packet packet;
744   struct ActiveLookup *al;
745   uint16_t dns_id;
746
747   dns_id =(uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
748                                                UINT16_MAX);
749   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750               "resolve_and_cache\n");
751   query.name = (char *)hostname;
752   query.type = record_type;
753   query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
754   memset (&packet,
755           0,
756           sizeof (packet));
757   packet.num_queries = 1;
758   packet.queries = &query;
759   packet.id = htons (dns_id);
760   packet.flags.recursion_desired = 1;
761   if (GNUNET_OK !=
762       GNUNET_DNSPARSER_pack (&packet,
763                              UINT16_MAX,
764                              &packet_buf,
765                              &packet_size))
766   {
767     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
768                 "Failed to pack query for hostname `%s'\n",
769                 hostname);
770     return GNUNET_SYSERR;
771
772   }
773   al = GNUNET_new (struct ActiveLookup);
774   al->hostname = GNUNET_strdup (hostname);
775   al->record_type = record_type;
776   al->request_id = request_id;
777   al->dns_id = dns_id;
778   al->client = client;
779   al->timeout_task = GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT,
780                                                    &handle_resolve_timeout,
781                                                    al);
782   al->resolve_handle =
783     GNUNET_DNSSTUB_resolve (dnsstub_ctx,
784                             packet_buf,
785                             packet_size,
786                             &handle_resolve_result,
787                             al);
788   GNUNET_free (packet_buf);
789   GNUNET_CONTAINER_DLL_insert (lookup_head,
790                                lookup_tail,
791                                al);
792   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793               "Resolving %s, request_id = %u, dns_id = %u\n",
794               hostname,
795               (unsigned int) request_id,
796               (unsigned int) dns_id);
797   return GNUNET_OK;
798 }
799
800
801 /**
802  * Process DNS request for @a hostname with request ID @a request_id
803  * from @a client demanding records of type @a record_type.
804  *
805  * @param hostname DNS name to resolve
806  * @param record_type desired record type
807  * @param request_id client's request ID
808  * @param client who should get the result?
809  */
810 static void
811 process_get (const char *hostname,
812              uint16_t record_type,
813              uint16_t request_id,
814              struct GNUNET_SERVICE_Client *client)
815 {
816   if (GNUNET_NO ==
817       try_cache (hostname,
818                  record_type,
819                  request_id,
820                  client))
821   {
822     if (GNUNET_OK !=
823         resolve_and_cache (hostname,
824                            record_type,
825                            request_id,
826                            client))
827     {
828       send_end_msg (request_id,
829                     client);
830     }
831   }
832 }
833
834
835 /**
836  * Verify well-formedness of GET-message.
837  *
838  * @param cls closure, unused
839  * @param get the actual message
840  * @return #GNUNET_OK if @a get is well-formed
841  */
842 static int
843 check_get (void *cls,
844            const struct GNUNET_RESOLVER_GetMessage *get)
845 {
846   uint16_t size;
847   int direction;
848   int af;
849
850   (void) cls;
851   size = ntohs (get->header.size) - sizeof (*get);
852   direction = ntohl (get->direction);
853   if (GNUNET_NO == direction)
854   {
855     /* IP from hostname */
856     const char *hostname;
857
858     hostname = (const char *) &get[1];
859     if (hostname[size - 1] != '\0')
860     {
861       GNUNET_break (0);
862       return GNUNET_SYSERR;
863     }
864     return GNUNET_OK;
865   }
866   af = ntohl (get->af);
867   switch (af)
868   {
869   case AF_INET:
870     if (size != sizeof (struct in_addr))
871     {
872       GNUNET_break (0);
873       return GNUNET_SYSERR;
874     }
875     break;
876   case AF_INET6:
877     if (size != sizeof (struct in6_addr))
878     {
879       GNUNET_break (0);
880       return GNUNET_SYSERR;
881     }
882     break;
883   default:
884     GNUNET_break (0);
885     return GNUNET_SYSERR;
886   }
887   return GNUNET_OK;
888 }
889
890
891 /**
892  * Handle GET-message.
893  *
894  * @param cls identification of the client
895  * @param msg the actual message
896  */
897 static void
898 handle_get (void *cls,
899             const struct GNUNET_RESOLVER_GetMessage *msg)
900 {
901   struct GNUNET_SERVICE_Client *client = cls;
902   int direction;
903   int af;
904   uint16_t request_id;
905   char *hostname;
906
907   direction = ntohl (msg->direction);
908   af = ntohl (msg->af);
909   request_id = ntohs (msg->id);
910   if (GNUNET_NO == direction)
911   {
912     /* IP from hostname */
913     hostname = GNUNET_strdup ((const char *) &msg[1]);
914     switch (af)
915     {
916       case AF_UNSPEC:
917       {
918         process_get (hostname,
919                      GNUNET_DNSPARSER_TYPE_ALL,
920                      request_id,
921                      client);
922         break;
923       }
924       case AF_INET:
925       {
926         process_get (hostname,
927                      GNUNET_DNSPARSER_TYPE_A,
928                      request_id,
929                      client);
930         break;
931       }
932       case AF_INET6:
933       {
934         process_get (hostname,
935                      GNUNET_DNSPARSER_TYPE_AAAA,
936                      request_id,
937                      client);
938         break;
939       }
940       default:
941       {
942         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943                   "got invalid af: %d\n",
944                   af);
945         GNUNET_assert (0);
946       }
947     }
948   }
949   else
950   {
951     /* hostname from IP */
952     hostname = make_reverse_hostname (&msg[1],
953                                       af);
954     process_get (hostname,
955                  GNUNET_DNSPARSER_TYPE_PTR,
956                  request_id,
957                  client);
958   }
959   GNUNET_free_non_null (hostname);
960   GNUNET_SERVICE_client_continue (client);
961 }
962
963
964 /**
965  * Service is shutting down, clean up.
966  *
967  * @param cls NULL, unused
968  */
969 static void
970 shutdown_task (void *cls)
971 {
972   (void) cls;
973
974   while (NULL != lookup_head)
975     free_active_lookup (lookup_head);
976   while (NULL != cache_head)
977     free_cache_entry (cache_head);
978   GNUNET_DNSSTUB_stop (dnsstub_ctx);
979 }
980
981
982 /**
983  * Service is starting, initialize everything.
984  *
985  * @param cls NULL, unused
986  * @param cfg our configuration
987  * @param sh service handle
988  */
989 static void
990 init_cb (void *cls,
991          const struct GNUNET_CONFIGURATION_Handle *cfg,
992          struct GNUNET_SERVICE_Handle *sh)
993 {
994   char **dns_servers;
995   int num_dns_servers;
996
997   (void) cfg;
998   (void) sh;
999   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1000                                  cls);
1001   dnsstub_ctx = GNUNET_DNSSTUB_start (128);
1002   num_dns_servers = lookup_dns_servers (&dns_servers);
1003   if (0 >= num_dns_servers)
1004   {
1005     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1006                 _("No DNS server available. DNS resolution will not be possible.\n"));
1007   }
1008   for (int i = 0; i < num_dns_servers; i++)
1009   {
1010     int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1011     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012                 "Adding DNS server '%s': %s\n",
1013                 dns_servers[i],
1014                 GNUNET_OK == result ? "success" : "failure");
1015     GNUNET_free (dns_servers[i]);
1016   }
1017   GNUNET_free_non_null (dns_servers);
1018 }
1019
1020
1021 /**
1022  * Callback called when a client connects to the service.
1023  *
1024  * @param cls closure for the service, unused
1025  * @param c the new client that connected to the service
1026  * @param mq the message queue used to send messages to the client
1027  * @return @a c
1028  */
1029 static void *
1030 connect_cb (void *cls,
1031             struct GNUNET_SERVICE_Client *c,
1032             struct GNUNET_MQ_Handle *mq)
1033 {
1034   (void) cls;
1035   (void) mq;
1036
1037   return c;
1038 }
1039
1040
1041 /**
1042  * Callback called when a client disconnected from the service
1043  *
1044  * @param cls closure for the service
1045  * @param c the client that disconnected
1046  * @param internal_cls should be equal to @a c
1047  */
1048 static void
1049 disconnect_cb (void *cls,
1050                struct GNUNET_SERVICE_Client *c,
1051                void *internal_cls)
1052 {
1053   struct ActiveLookup *n;
1054   (void) cls;
1055
1056   GNUNET_assert (c == internal_cls);
1057   n = lookup_head;
1058   for (struct ActiveLookup *al = n;
1059        NULL != al;
1060        al = n)
1061   {
1062     n = al->next;
1063     if (al->client == c)
1064       free_active_lookup (al);
1065   }
1066 }
1067
1068
1069 /**
1070  * Define "main" method using service macro.
1071  */
1072 GNUNET_SERVICE_MAIN
1073 ("resolver",
1074  GNUNET_SERVICE_OPTION_NONE,
1075  &init_cb,
1076  &connect_cb,
1077  &disconnect_cb,
1078  NULL,
1079  GNUNET_MQ_hd_var_size (get,
1080                         GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1081                         struct GNUNET_RESOLVER_GetMessage,
1082                         NULL),
1083  GNUNET_MQ_handler_end ());
1084
1085
1086 #if defined(LINUX) && defined(__GLIBC__)
1087 #include <malloc.h>
1088
1089 /**
1090  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1091  */
1092 void __attribute__ ((constructor))
1093 GNUNET_RESOLVER_memory_init ()
1094 {
1095   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1096   mallopt (M_TOP_PAD, 1 * 1024);
1097   malloc_trim (0);
1098 }
1099 #endif
1100
1101
1102 /* end of gnunet-service-resolver.c */