2 This file is part of GNUnet.
3 Copyright (C) 2007-2016 GNUnet e.V.
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.
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.
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/>.
20 * @file util/gnunet-service-resolver.c
21 * @brief code to do DNS resolution
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_statistics_service.h"
32 * How long do we wait for DNS answers?
34 #define DNS_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37 * Maximum number of hostnames we cache results for.
39 #define MAX_CACHE 1024
42 * Entry in list of cached DNS records for a hostname.
44 struct RecordListEntry
47 * This is a doubly linked list.
49 struct RecordListEntry *next;
52 * This is a doubly linked list.
54 struct RecordListEntry *prev;
59 struct GNUNET_DNSPARSER_Record *record;
64 * A cached DNS lookup result.
69 * This is a doubly linked list.
71 struct ResolveCache *next;
74 * This is a doubly linked list.
76 struct ResolveCache *prev;
79 * Which hostname is this cache for?
84 * head of a double linked list containing the lookup results
86 struct RecordListEntry *records_head;
89 * tail of a double linked list containing the lookup results
91 struct RecordListEntry *records_tail;
97 * Information about pending lookups.
104 struct ActiveLookup *next;
109 struct ActiveLookup *prev;
112 * The client that queried the records contained in this cache entry.
114 struct GNUNET_SERVICE_Client *client;
117 * handle for cancelling a request
119 struct GNUNET_DNSSTUB_RequestSocket *resolve_handle;
122 * handle for the resolution timeout task
124 struct GNUNET_SCHEDULER_Task *timeout_task;
127 * Which hostname are we resolving?
132 * If @a record_type is #GNUNET_DNSPARSER_TYPE_ALL, did we go again
133 * for the AAAA records yet?
138 * type of queried DNS record
140 uint16_t record_type;
143 * Unique request ID of a client if a query for this hostname/record_type
144 * is currently pending, undefined otherwise.
146 uint32_t client_request_id;
149 * Unique DNS request ID of a client if a query for this hostname/record_type
150 * is currently pending, undefined otherwise.
158 * Start of the linked list of cached DNS lookup results.
160 static struct ResolveCache *cache_head;
163 * Tail of the linked list of cached DNS lookup results.
165 static struct ResolveCache *cache_tail;
168 * Start of the linked list of active DNS lookups.
170 static struct ActiveLookup *lookup_head;
173 * Tail of the linked list of active DNS lookups.
175 static struct ActiveLookup *lookup_tail;
178 * context of dnsstub library
180 static struct GNUNET_DNSSTUB_Context *dnsstub_ctx;
183 * My domain, to be appended to the hostname to get a FQDN.
185 static char *my_domain;
188 * How many entries do we have in #cache_head DLL?
190 static unsigned int cache_size;
194 * Remove @a entry from cache.
196 * @param rc entry to free
199 free_cache_entry (struct ResolveCache *rc)
201 struct RecordListEntry *pos;
203 while (NULL != (pos = rc->records_head))
205 GNUNET_CONTAINER_DLL_remove (rc->records_head,
208 GNUNET_DNSPARSER_free_record (pos->record);
209 GNUNET_free (pos->record);
212 GNUNET_free_non_null (rc->hostname);
213 GNUNET_CONTAINER_DLL_remove (cache_head,
222 * Release resources associated with @a al
224 * @param al an active lookup
227 free_active_lookup (struct ActiveLookup *al)
229 GNUNET_CONTAINER_DLL_remove (lookup_head,
232 if (NULL != al->resolve_handle)
234 GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
235 al->resolve_handle = NULL;
237 if (NULL != al->timeout_task)
239 GNUNET_SCHEDULER_cancel (al->timeout_task);
240 al->timeout_task = NULL;
242 GNUNET_free_non_null (al->hostname);
249 * Find out if the configuration file line contains a string
250 * starting with "nameserver ", and if so, return a copy of
251 * the nameserver's IP.
253 * @param line line to parse
254 * @param line_len number of characters in @a line
255 * @return NULL if no nameserver is configured in this @a line
258 extract_dns_server (const char* line,
261 if (0 == strncmp (line,
263 strlen ("nameserver ")))
264 return GNUNET_strndup (line + strlen ("nameserver "),
265 line_len - strlen ("nameserver "));
271 * Find out if the configuration file line contains a string
272 * starting with "search ", and if so, return a copy of
273 * the machine's search domain.
275 * @param line line to parse
276 * @param line_len number of characters in @a line
277 * @return NULL if no nameserver is configured in this @a line
280 extract_search_domain (const char* line,
283 if (0 == strncmp (line,
286 return GNUNET_strndup (line + strlen ("search "),
287 line_len - strlen ("search "));
293 * Reads the list of nameservers from /etc/resolve.conf
295 * @param server_addrs[out] a list of null-terminated server address strings
296 * @return the number of server addresses in @server_addrs, -1 on error
299 lookup_dns_servers (char ***server_addrs)
301 struct GNUNET_DISK_FileHandle *fh;
305 unsigned int num_dns_servers;
307 fh = GNUNET_DISK_file_open ("/etc/resolv.conf",
308 GNUNET_DISK_OPEN_READ,
309 GNUNET_DISK_PERM_NONE);
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313 "Could not open /etc/resolv.conf. "
314 "DNS resolution will not be possible.\n");
317 bytes_read = GNUNET_DISK_file_read (fh,
320 *server_addrs = NULL;
323 while (read_offset < bytes_read)
329 newline = strchr (buf + read_offset,
333 line_len = newline - buf - read_offset;
334 dns_server = extract_dns_server (buf + read_offset,
336 if (NULL != dns_server)
338 GNUNET_array_append (*server_addrs,
342 else if (NULL == my_domain)
344 my_domain = extract_search_domain (buf + read_offset,
347 read_offset += line_len + 1;
349 GNUNET_DISK_file_close (fh);
350 return (int) num_dns_servers;
355 * Compute name to use for DNS reverse lookups from @a ip.
357 * @param ip IP address to resolve, in binary format, network byte order
358 * @param af address family of @a ip, AF_INET or AF_INET6
361 make_reverse_hostname (const void *ip,
364 char *buf = GNUNET_new_array (80,
370 struct in_addr *addr = (struct in_addr *)ip;
371 uint32_t ip_int = addr->s_addr;
373 for (int i = 3; i >= 0; i--)
375 int n = GNUNET_snprintf (buf + pos,
378 ((uint8_t *)&ip_int)[i]);
386 pos += GNUNET_snprintf (buf + pos,
390 else if (AF_INET6 == af)
392 struct in6_addr *addr = (struct in6_addr *)ip;
393 for (int i = 15; i >= 0; i--)
395 int n = GNUNET_snprintf (buf + pos,
398 addr->s6_addr[i] & 0xf);
405 n = GNUNET_snprintf (buf + pos,
408 addr->s6_addr[i] >> 4);
416 pos += GNUNET_snprintf (buf + pos,
426 * Send DNS @a record back to our @a client.
428 * @param record information to transmit
429 * @param record_type requested record type from client
430 * @param client_request_id to which request are we responding
431 * @param client where to send @a record
432 * @return #GNUNET_YES if we sent a reply,
433 * #GNUNET_NO if the record type is not understood or
434 * does not match @a record_type
437 send_reply (struct GNUNET_DNSPARSER_Record *record,
438 uint16_t record_type,
439 uint32_t client_request_id,
440 struct GNUNET_SERVICE_Client *client)
442 struct GNUNET_RESOLVER_ResponseMessage *msg;
443 struct GNUNET_MQ_Envelope *env;
447 switch (record->type)
449 case GNUNET_DNSPARSER_TYPE_CNAME:
450 if (GNUNET_DNSPARSER_TYPE_CNAME != record_type)
452 payload = record->data.hostname;
453 payload_len = strlen (record->data.hostname) + 1;
455 case GNUNET_DNSPARSER_TYPE_PTR:
456 if (GNUNET_DNSPARSER_TYPE_PTR != record_type)
458 payload = record->data.hostname;
459 payload_len = strlen (record->data.hostname) + 1;
461 case GNUNET_DNSPARSER_TYPE_A:
462 if ( (GNUNET_DNSPARSER_TYPE_A != record_type) &&
463 (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
465 payload = record->data.raw.data;
466 payload_len = record->data.raw.data_len;
468 case GNUNET_DNSPARSER_TYPE_AAAA:
469 if ( (GNUNET_DNSPARSER_TYPE_AAAA != record_type) &&
470 (GNUNET_DNSPARSER_TYPE_ALL != record_type) )
472 payload = record->data.raw.data;
473 payload_len = record->data.raw.data_len;
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Cannot handle DNS response type %u: not supported here\n",
481 env = GNUNET_MQ_msg_extra (msg,
483 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
484 msg->client_id = client_request_id;
485 GNUNET_memcpy (&msg[1],
488 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
495 * Send message to @a client that we transmitted all
496 * responses for @a client_request_id
498 * @param client_request_id to which request are we responding
499 * @param client where to send @a record
502 send_end_msg (uint32_t client_request_id,
503 struct GNUNET_SERVICE_Client *client)
505 struct GNUNET_RESOLVER_ResponseMessage *msg;
506 struct GNUNET_MQ_Envelope *env;
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Sending END message\n");
510 env = GNUNET_MQ_msg (msg,
511 GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE);
512 msg->client_id = client_request_id;
513 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
519 * Remove expired entries from @a rc
521 * @param rc entry in resolver cache
522 * @return #GNUNET_YES if @a rc was completely expired
523 * #GNUNET_NO if some entries are left
526 remove_expired (struct ResolveCache *rc)
528 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
529 struct RecordListEntry *n;
531 for (struct RecordListEntry *pos = rc->records_head;
536 if (now.abs_value_us > pos->record->expiration_time.abs_value_us)
537 GNUNET_CONTAINER_DLL_remove (rc->records_head,
541 if (NULL == rc->records_head)
543 free_cache_entry (rc);
551 * Process DNS request for @a hostname with request ID @a request_id
552 * from @a client demanding records of type @a record_type.
554 * @param hostname DNS name to resolve
555 * @param record_type desired record type
556 * @param client_request_id client's request ID
557 * @param client who should get the result?
560 process_get (const char *hostname,
561 uint16_t record_type,
562 uint32_t client_request_id,
563 struct GNUNET_SERVICE_Client *client);
567 * Get an IP address as a string (works for both IPv4 and IPv6). Note
568 * that the resolution happens asynchronously and that the first call
569 * may not immediately result in the FQN (but instead in a
570 * human-readable IP address).
572 * @param hostname what hostname was to be resolved
573 * @param record_type what type of record was requested
574 * @param client_request_id unique identification of the client's request
575 * @param client handle to the client making the request (for sending the reply)
578 try_cache (const char *hostname,
579 uint16_t record_type,
580 uint32_t client_request_id,
581 struct GNUNET_SERVICE_Client *client)
583 struct ResolveCache *pos;
584 struct ResolveCache *next;
588 for (pos = next; NULL != pos; pos = next)
591 if (GNUNET_YES == remove_expired (pos))
593 if (0 == strcmp (pos->hostname,
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600 "No cache entry for '%s'\n",
604 if (cache_head != pos)
606 /* move result to head to achieve LRU for cache eviction */
607 GNUNET_CONTAINER_DLL_remove (cache_head,
610 GNUNET_CONTAINER_DLL_insert (cache_head,
615 for (struct RecordListEntry *rle = pos->records_head;
619 const struct GNUNET_DNSPARSER_Record *record = rle->record;
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "Found cache entry for '%s', record type '%u'\n",
625 if ( (GNUNET_DNSPARSER_TYPE_CNAME == record->type) &&
626 (GNUNET_DNSPARSER_TYPE_CNAME != record_type) &&
627 (GNUNET_NO == found) )
629 const char *hostname = record->data.hostname;
631 process_get (hostname,
635 return GNUNET_YES; /* counts as a cache "hit" */
637 found |= send_reply (rle->record,
642 if (GNUNET_NO == found)
643 return GNUNET_NO; /* had records, but none matched! */
644 send_end_msg (client_request_id,
651 * Create DNS query for @a hostname of type @a type
652 * with DNS request ID @a dns_id.
654 * @param hostname DNS name to query
655 * @param type requested DNS record type
656 * @param dns_id what should be the DNS request ID
657 * @param packet_buf[out] where to write the request packet
658 * @param packet_size[out] set to size of @a packet_buf on success
659 * @return #GNUNET_OK on success
662 pack (const char *hostname,
668 struct GNUNET_DNSPARSER_Query query;
669 struct GNUNET_DNSPARSER_Packet packet;
671 query.name = (char *)hostname;
673 query.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
677 packet.num_queries = 1;
678 packet.queries = &query;
679 packet.id = htons (dns_id);
680 packet.flags.recursion_desired = 1;
682 GNUNET_DNSPARSER_pack (&packet,
687 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
688 "Failed to pack query for hostname `%s'\n",
691 return GNUNET_SYSERR;
698 * We got a result from DNS. Add it to the cache and
699 * see if we can make our client happy...
701 * @param cls the `struct ActiveLookup`
702 * @param dns the DNS response
703 * @param dns_len number of bytes in @a dns
706 handle_resolve_result (void *cls,
707 const struct GNUNET_TUN_DnsHeader *dns,
710 struct ActiveLookup *al = cls;
711 struct GNUNET_DNSPARSER_Packet *parsed;
712 struct ResolveCache *rc;
714 parsed = GNUNET_DNSPARSER_parse ((const char *)dns,
718 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
719 "Failed to parse DNS reply (hostname %s, request ID %u)\n",
724 if (al->dns_id != ntohs (parsed->id))
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Request ID in DNS reply does not match\n");
728 GNUNET_DNSPARSER_free_packet (parsed);
731 if (0 == parsed->num_answers + parsed->num_authority_records + parsed->num_additional_records)
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
734 "DNS reply (hostname %s, request ID %u) contains no answers\n",
736 (unsigned int) al->client_request_id);
737 GNUNET_DNSPARSER_free_packet (parsed);
738 send_end_msg (al->client_request_id,
740 free_active_lookup (al);
743 /* LRU-based cache eviction: we remove from tail */
744 while (cache_size > MAX_CACHE)
745 free_cache_entry (cache_tail);
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "Got reply for hostname %s and request ID %u\n",
750 (unsigned int) al->client_request_id);
752 for (unsigned int i = 0; i != parsed->num_answers; i++)
754 struct GNUNET_DNSPARSER_Record *record = &parsed->answers[i];
755 struct RecordListEntry *rle;
757 for (rc = cache_head; NULL != rc; rc = rc->next)
758 if (0 == strcasecmp (rc->hostname,
763 rc = GNUNET_new (struct ResolveCache);
764 rc->hostname = GNUNET_strdup (record->name);
765 GNUNET_CONTAINER_DLL_insert (cache_head,
770 /* TODO: ought to check first if we have this exact record
771 already in the cache! */
772 rle = GNUNET_new (struct RecordListEntry);
773 rle->record = GNUNET_DNSPARSER_duplicate_record (record);
774 GNUNET_CONTAINER_DLL_insert (rc->records_head,
778 for (unsigned int i = 0; i != parsed->num_authority_records; i++)
780 struct GNUNET_DNSPARSER_Record *record = &parsed->authority_records[i];
781 struct RecordListEntry *rle;
783 for (rc = cache_head; NULL != rc; rc = rc->next)
784 if (0 == strcasecmp (rc->hostname,
789 rc = GNUNET_new (struct ResolveCache);
790 rc->hostname = GNUNET_strdup (record->name);
791 GNUNET_CONTAINER_DLL_insert (cache_head,
796 /* TODO: ought to check first if we have this exact record
797 already in the cache! */
798 rle = GNUNET_new (struct RecordListEntry);
799 rle->record = GNUNET_DNSPARSER_duplicate_record (record);
800 GNUNET_CONTAINER_DLL_insert (rc->records_head,
804 for (unsigned int i = 0; i != parsed->num_additional_records; i++)
806 struct GNUNET_DNSPARSER_Record *record = &parsed->additional_records[i];
807 struct RecordListEntry *rle;
809 for (rc = cache_head; NULL != rc; rc = rc->next)
810 if (0 == strcasecmp (rc->hostname,
815 rc = GNUNET_new (struct ResolveCache);
816 rc->hostname = GNUNET_strdup (record->name);
817 GNUNET_CONTAINER_DLL_insert (cache_head,
822 /* TODO: ought to check first if we have this exact record
823 already in the cache! */
824 rle = GNUNET_new (struct RecordListEntry);
825 rle->record = GNUNET_DNSPARSER_duplicate_record (record);
826 GNUNET_CONTAINER_DLL_insert (rc->records_head,
830 /* see if we need to do the 2nd request for AAAA records */
831 if ( (GNUNET_DNSPARSER_TYPE_ALL == al->record_type) &&
832 (GNUNET_NO == al->did_aaaa) )
838 dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
842 GNUNET_DNSPARSER_TYPE_AAAA,
847 al->did_aaaa = GNUNET_YES;
849 GNUNET_DNSSTUB_resolve_cancel (al->resolve_handle);
851 GNUNET_DNSSTUB_resolve (dnsstub_ctx,
854 &handle_resolve_result,
860 /* resume by trying again from cache */
862 try_cache (al->hostname,
864 al->client_request_id,
866 /* cache failed, tell client we could not get an answer */
868 send_end_msg (al->client_request_id,
871 free_active_lookup (al);
872 GNUNET_DNSPARSER_free_packet (parsed);
877 * We encountered a timeout trying to perform a
880 * @param cls a `struct ActiveLookup`
883 handle_resolve_timeout (void *cls)
885 struct ActiveLookup *al = cls;
887 al->timeout_task = NULL;
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "DNS lookup timeout!\n");
890 send_end_msg (al->client_request_id,
892 free_active_lookup (al);
897 * Initiate an active lookup, then cache the result and
898 * try to then complete the resolution.
900 * @param hostname DNS name to resolve
901 * @param record_type record type to locate
902 * @param client_request_id client request ID
903 * @param client handle to the client
904 * @return #GNUNET_OK if the DNS query is now pending
907 resolve_and_cache (const char* hostname,
908 uint16_t record_type,
909 uint32_t client_request_id,
910 struct GNUNET_SERVICE_Client *client)
914 struct ActiveLookup *al;
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
919 "resolve_and_cache `%s'\n",
921 dns_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
924 if (GNUNET_DNSPARSER_TYPE_ALL == record_type)
925 type = GNUNET_DNSPARSER_TYPE_A;
935 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
936 "Failed to pack query for hostname `%s'\n",
938 return GNUNET_SYSERR;
941 al = GNUNET_new (struct ActiveLookup);
942 al->hostname = GNUNET_strdup (hostname);
943 al->record_type = record_type;
944 al->client_request_id = client_request_id;
947 al->timeout_task = GNUNET_SCHEDULER_add_delayed (DNS_TIMEOUT,
948 &handle_resolve_timeout,
951 GNUNET_DNSSTUB_resolve (dnsstub_ctx,
954 &handle_resolve_result,
956 GNUNET_free (packet_buf);
957 GNUNET_CONTAINER_DLL_insert (lookup_head,
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "Resolving %s, client_request_id = %u, dns_id = %u\n",
963 (unsigned int) client_request_id,
964 (unsigned int) dns_id);
970 * Process DNS request for @a hostname with request ID @a client_request_id
971 * from @a client demanding records of type @a record_type.
973 * @param hostname DNS name to resolve
974 * @param record_type desired record type
975 * @param client_request_id client's request ID
976 * @param client who should get the result?
979 process_get (const char *hostname,
980 uint16_t record_type,
981 uint32_t client_request_id,
982 struct GNUNET_SERVICE_Client *client)
986 if ( (NULL != my_domain) &&
987 (NULL == strchr (hostname,
988 (unsigned char) '.')) &&
989 (strlen (hostname) + strlen (my_domain) <= 253) )
991 GNUNET_snprintf (fqdn,
997 else if (strlen (hostname) < 255)
999 GNUNET_snprintf (fqdn,
1007 GNUNET_SERVICE_client_drop (client);
1017 resolve_and_cache (fqdn,
1022 send_end_msg (client_request_id,
1030 * Verify well-formedness of GET-message.
1032 * @param cls closure, unused
1033 * @param get the actual message
1034 * @return #GNUNET_OK if @a get is well-formed
1037 check_get (void *cls,
1038 const struct GNUNET_RESOLVER_GetMessage *get)
1045 size = ntohs (get->header.size) - sizeof (*get);
1046 direction = ntohl (get->direction);
1047 if (GNUNET_NO == direction)
1049 /* IP from hostname */
1050 const char *hostname;
1052 hostname = (const char *) &get[1];
1053 if (hostname[size - 1] != '\0')
1056 return GNUNET_SYSERR;
1060 af = ntohl (get->af);
1064 if (size != sizeof (struct in_addr))
1067 return GNUNET_SYSERR;
1071 if (size != sizeof (struct in6_addr))
1074 return GNUNET_SYSERR;
1079 return GNUNET_SYSERR;
1086 * Handle GET-message.
1088 * @param cls identification of the client
1089 * @param msg the actual message
1092 handle_get (void *cls,
1093 const struct GNUNET_RESOLVER_GetMessage *msg)
1095 struct GNUNET_SERVICE_Client *client = cls;
1098 uint32_t client_request_id;
1101 direction = ntohl (msg->direction);
1102 af = ntohl (msg->af);
1103 client_request_id = msg->client_id;
1104 GNUNET_SERVICE_client_continue (client);
1105 if (GNUNET_NO == direction)
1107 /* IP from hostname */
1108 hostname = GNUNET_strdup ((const char *) &msg[1]);
1113 process_get (hostname,
1114 GNUNET_DNSPARSER_TYPE_ALL,
1121 process_get (hostname,
1122 GNUNET_DNSPARSER_TYPE_A,
1129 process_get (hostname,
1130 GNUNET_DNSPARSER_TYPE_AAAA,
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "got invalid af: %d\n",
1146 /* hostname from IP */
1147 hostname = make_reverse_hostname (&msg[1],
1149 process_get (hostname,
1150 GNUNET_DNSPARSER_TYPE_PTR,
1154 GNUNET_free_non_null (hostname);
1159 * Service is shutting down, clean up.
1161 * @param cls NULL, unused
1164 shutdown_task (void *cls)
1168 while (NULL != lookup_head)
1169 free_active_lookup (lookup_head);
1170 while (NULL != cache_head)
1171 free_cache_entry (cache_head);
1172 GNUNET_DNSSTUB_stop (dnsstub_ctx);
1177 * Service is starting, initialize everything.
1179 * @param cls NULL, unused
1180 * @param cfg our configuration
1181 * @param sh service handle
1185 const struct GNUNET_CONFIGURATION_Handle *cfg,
1186 struct GNUNET_SERVICE_Handle *sh)
1189 int num_dns_servers;
1193 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1195 dnsstub_ctx = GNUNET_DNSSTUB_start (128);
1197 num_dns_servers = lookup_dns_servers (&dns_servers);
1198 if (0 >= num_dns_servers)
1200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1201 _("No DNS server available. DNS resolution will not be possible.\n"));
1204 for (int i = 0; i < num_dns_servers; i++)
1206 int result = GNUNET_DNSSTUB_add_dns_ip (dnsstub_ctx, dns_servers[i]);
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Adding DNS server '%s': %s\n",
1210 GNUNET_OK == result ? "success" : "failure");
1211 GNUNET_free (dns_servers[i]);
1213 GNUNET_free_non_null (dns_servers);
1218 * Callback called when a client connects to the service.
1220 * @param cls closure for the service, unused
1221 * @param c the new client that connected to the service
1222 * @param mq the message queue used to send messages to the client
1226 connect_cb (void *cls,
1227 struct GNUNET_SERVICE_Client *c,
1228 struct GNUNET_MQ_Handle *mq)
1238 * Callback called when a client disconnected from the service
1240 * @param cls closure for the service
1241 * @param c the client that disconnected
1242 * @param internal_cls should be equal to @a c
1245 disconnect_cb (void *cls,
1246 struct GNUNET_SERVICE_Client *c,
1249 struct ActiveLookup *n;
1252 GNUNET_assert (c == internal_cls);
1254 for (struct ActiveLookup *al = n;
1259 if (al->client == c)
1260 free_active_lookup (al);
1266 * Define "main" method using service macro.
1270 GNUNET_SERVICE_OPTION_NONE,
1275 GNUNET_MQ_hd_var_size (get,
1276 GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST,
1277 struct GNUNET_RESOLVER_GetMessage,
1279 GNUNET_MQ_handler_end ());
1282 #if defined(LINUX) && defined(__GLIBC__)
1286 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1288 void __attribute__ ((constructor))
1289 GNUNET_RESOLVER_memory_init ()
1291 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1292 mallopt (M_TOP_PAD, 1 * 1024);
1298 /* end of gnunet-service-resolver.c */