2 This file is part of GNUnet.
3 Copyright (C) 2010, 2012, 2017 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file pt/gnunet-daemon-pt.c
22 * @brief tool to manipulate DNS and VPN services to perform protocol translation (IPvX over GNUnet)
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_dns_service.h"
28 #include "gnunet_dnsparser_lib.h"
29 #include "gnunet_cadet_service.h"
30 #include "gnunet_tun_lib.h"
31 #include "gnunet_dht_service.h"
32 #include "gnunet_vpn_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_applications.h"
35 #include "block_dns.h"
39 * After how long do we time out if we could not get an IP from VPN or CADET?
41 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
44 * How many bytes of payload do we allow at most for a DNS reply?
45 * Given that this is pretty much limited to loopback, we can be
46 * pretty high (Linux loopback defaults to 16k, most local UDP packets
47 * should survive up to 9k (NFS), so 8k should be pretty safe in
50 #define MAX_DNS_SIZE (8 * 1024)
53 * How many channels do we open at most at the same time?
55 #define MAX_OPEN_TUNNELS 4
59 * Which group of DNS records are we currently processing?
69 * DNS authority records
71 AUTHORITY_RECORDS = 1,
74 * DNS additional records
76 ADDITIONAL_RECORDS = 2,
79 * We're done processing.
86 * Information tracked per DNS reply that we are processing.
91 * Handle to submit the final result.
93 struct GNUNET_DNS_RequestHandle *rh;
96 * DNS packet that is being modified.
98 struct GNUNET_DNSPARSER_Packet *dns;
101 * Active redirection request with the VPN.
103 struct GNUNET_VPN_RedirectionRequest *rr;
106 * Record for which we have an active redirection request.
108 struct GNUNET_DNSPARSER_Record *rec;
111 * Offset in the current record group that is being modified.
116 * Group that is being modified
118 enum RequestGroup group;
124 * Handle to a peer that advertised that it is willing to serve
125 * as a DNS exit. We try to keep a few channels open and a few
134 struct CadetExit *next;
139 struct CadetExit *prev;
142 * Channel we use for DNS requests over CADET, NULL if we did
143 * not initialze a channel to this peer yet.
145 struct GNUNET_CADET_Channel *cadet_channel;
148 * At what time did the peer's advertisement expire?
150 struct GNUNET_TIME_Absolute expiration;
153 * Head of DLL of requests waiting for a response.
155 struct RequestContext *receive_queue_head;
158 * Tail of DLL of requests waiting for a response.
160 struct RequestContext *receive_queue_tail;
163 * Identity of the peer that is providing the exit for us.
165 struct GNUNET_PeerIdentity peer;
168 * How many DNS requests did we transmit via this channel?
170 unsigned int num_transmitted;
173 * How many DNS requests were answered via this channel?
175 unsigned int num_answered;
178 * Size of the window, 0 if we are busy.
180 /* unsigned */ int idle;
187 * State we keep for a request that is going out via CADET.
189 struct RequestContext
192 * We keep these in a DLL.
194 struct RequestContext *next;
197 * We keep these in a DLL.
199 struct RequestContext *prev;
202 * Exit that was chosen for this request.
204 struct CadetExit *exit;
207 * Handle for interaction with DNS service.
209 struct GNUNET_DNS_RequestHandle *rh;
212 * Envelope with the request we are transmitting.
214 struct GNUNET_MQ_Envelope *env;
217 * Task used to abort this operation with timeout.
219 struct GNUNET_SCHEDULER_Task *timeout_task;
222 * Length of the request message that follows this struct.
227 * ID of the original DNS request (used to match the reply).
235 * Head of DLL of cadet exits. Cadet exits with an open channel are
236 * always at the beginning (so we do not have to traverse the entire
237 * list to find them).
239 static struct CadetExit *exit_head;
242 * Tail of DLL of cadet exits.
244 static struct CadetExit *exit_tail;
247 * The handle to the configuration used throughout the process
249 static const struct GNUNET_CONFIGURATION_Handle *cfg;
252 * The handle to the VPN
254 static struct GNUNET_VPN_Handle *vpn_handle;
257 * The handle to the CADET service
259 static struct GNUNET_CADET_Handle *cadet_handle;
264 static struct GNUNET_STATISTICS_Handle *stats;
267 * The handle to DNS post-resolution modifications.
269 static struct GNUNET_DNS_Handle *dns_post_handle;
272 * The handle to DNS pre-resolution modifications.
274 static struct GNUNET_DNS_Handle *dns_pre_handle;
277 * Handle to access the DHT.
279 static struct GNUNET_DHT_Handle *dht;
282 * Our DHT GET operation to find DNS exits.
284 static struct GNUNET_DHT_GetHandle *dht_get;
287 * Are we doing IPv4-pt?
292 * Are we doing IPv6-pt?
297 * Are we channeling DNS queries?
299 static int dns_channel;
302 * Number of DNS exit peers we currently have in the cadet channel.
303 * Used to see if using the cadet channel makes any sense right now,
304 * as well as to decide if we should open new channels.
306 static unsigned int dns_exit_available;
310 * We are short on cadet exits, try to open another one.
313 try_open_exit (void);
317 * Compute the weight of the given exit. The higher the weight,
318 * the more likely it will be that the channel will be chosen.
319 * A weigt of zero means that we should close the channel as it
320 * is so bad, that we should not use it.
322 * @param exit exit to calculate the weight for
323 * @return weight of the channel
326 get_channel_weight (struct CadetExit *exit)
329 uint32_t drop_percent;
330 uint32_t good_percent;
332 GNUNET_assert (exit->num_transmitted >= exit->num_answered);
333 dropped = exit->num_transmitted - exit->num_answered;
334 if (exit->num_transmitted > 0)
335 drop_percent = (uint32_t) ((100LL * dropped) / exit->num_transmitted);
337 drop_percent = 50; /* no data */
338 if ( (exit->num_transmitted > 20) &&
339 (drop_percent > 25) )
340 return 0; /* statistically significant, and > 25% loss, die */
341 good_percent = 100 - drop_percent;
342 GNUNET_assert (0 != good_percent);
343 if ( UINT32_MAX / good_percent / good_percent < exit->num_transmitted)
344 return UINT32_MAX; /* formula below would overflow */
345 return 1 + good_percent * good_percent * exit->num_transmitted;
350 * Choose a cadet exit for a DNS request. We try to use a channel
351 * that is reliable and currently available. All existing
352 * channels are given a base weight of 1, plus a score relating
353 * to the total number of queries answered in relation to the
354 * total number of queries we sent to that channel. That
355 * score is doubled if the channel is currently idle.
357 * @return NULL if no exit is known, otherwise the
358 * exit that we should use to queue a message with
360 static struct CadetExit *
363 struct CadetExit *pos;
364 uint64_t total_transmitted;
365 uint64_t selected_offset;
366 uint32_t channel_weight;
368 total_transmitted = 0;
369 for (pos = exit_head; NULL != pos; pos = pos->next)
371 if (NULL == pos->cadet_channel)
373 channel_weight = get_channel_weight (pos);
374 total_transmitted += channel_weight;
375 /* double weight for idle channels */
377 total_transmitted += channel_weight;
379 if (0 == total_transmitted)
381 /* no channels available, or only a very bad one... */
384 selected_offset = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
386 total_transmitted = 0;
387 for (pos = exit_head; NULL != pos; pos = pos->next)
389 if (NULL == pos->cadet_channel)
391 channel_weight = get_channel_weight (pos);
392 total_transmitted += channel_weight;
393 /* double weight for idle channels */
395 total_transmitted += channel_weight;
396 if (total_transmitted > selected_offset)
405 * We're done modifying all records in the response. Submit the reply
406 * and free the resources of the rc.
408 * @param rc context to process
411 finish_request (struct ReplyContext *rc)
417 GNUNET_DNSPARSER_pack (rc->dns,
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
423 _("Failed to pack DNS request. Dropping.\n"));
424 GNUNET_DNS_request_drop (rc->rh);
428 GNUNET_STATISTICS_update (stats,
429 gettext_noop ("# DNS requests mapped to VPN"),
431 GNUNET_DNS_request_answer (rc->rh,
436 GNUNET_DNSPARSER_free_packet (rc->dns);
442 * Process the next record of the given request context.
443 * When done, submit the reply and free the resources of
446 * @param rc context to process
449 submit_request (struct ReplyContext *rc);
453 * Callback invoked from the VPN service once a redirection is
454 * available. Provides the IP address that can now be used to
455 * reach the requested destination. We substitute the active
456 * record and then continue with 'submit_request' to look at
459 * @param cls our `struct ReplyContext`
460 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
461 * will match 'result_af' from the request
462 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
463 * that the VPN allocated for the redirection;
464 * traffic to this IP will now be redirected to the
465 * specified target peer; NULL on error
468 vpn_allocation_callback (void *cls,
472 struct ReplyContext *rc = cls;
477 GNUNET_DNS_request_drop (rc->rh);
478 GNUNET_DNSPARSER_free_packet (rc->dns);
482 GNUNET_STATISTICS_update (stats,
483 gettext_noop ("# DNS records modified"),
486 switch (rc->rec->type)
488 case GNUNET_DNSPARSER_TYPE_A:
489 GNUNET_assert (AF_INET == af);
490 GNUNET_memcpy (rc->rec->data.raw.data,
492 sizeof (struct in_addr));
494 case GNUNET_DNSPARSER_TYPE_AAAA:
495 GNUNET_assert (AF_INET6 == af);
496 GNUNET_memcpy (rc->rec->data.raw.data,
498 sizeof (struct in6_addr));
510 * Modify the given DNS record by asking VPN to create a channel
511 * to the given address. When done, continue with submitting
512 * other records from the request context ('submit_request' is
515 * @param rc context to process
516 * @param rec record to modify
519 modify_address (struct ReplyContext *rc,
520 struct GNUNET_DNSPARSER_Record *rec)
526 case GNUNET_DNSPARSER_TYPE_A:
528 GNUNET_assert (rec->data.raw.data_len == sizeof (struct in_addr));
530 case GNUNET_DNSPARSER_TYPE_AAAA:
532 GNUNET_assert (rec->data.raw.data_len == sizeof (struct in6_addr));
539 rc->rr = GNUNET_VPN_redirect_to_ip (vpn_handle,
543 GNUNET_TIME_relative_to_absolute (TIMEOUT),
544 &vpn_allocation_callback,
550 * Process the next record of the given request context.
551 * When done, submit the reply and free the resources of
554 * @param rc context to process
557 submit_request (struct ReplyContext *rc)
559 struct GNUNET_DNSPARSER_Record *ra;
568 ra = rc->dns->answers;
569 ra_len = rc->dns->num_answers;
571 case AUTHORITY_RECORDS:
572 ra = rc->dns->authority_records;
573 ra_len = rc->dns->num_authority_records;
575 case ADDITIONAL_RECORDS:
576 ra = rc->dns->additional_records;
577 ra_len = rc->dns->num_additional_records;
585 for (i=rc->offset;i<ra_len;i++)
589 case GNUNET_DNSPARSER_TYPE_A:
598 case GNUNET_DNSPARSER_TYPE_AAAA:
615 * Test if any of the given records need protocol-translation work.
617 * @param ra array of records
618 * @param ra_len number of entries in @a ra
619 * @return #GNUNET_YES if any of the given records require protocol-translation
622 work_test (const struct GNUNET_DNSPARSER_Record *ra,
627 for (i=0;i<ra_len;i++)
631 case GNUNET_DNSPARSER_TYPE_A:
635 case GNUNET_DNSPARSER_TYPE_AAAA:
646 * This function is called AFTER we got an IP address for a
647 * DNS request. Now, the PT daemon has the chance to substitute
648 * the IP address with one from the VPN range to channel requests
649 * destined for this IP address via VPN and CADET.
652 * @param rh request handle to user for reply
653 * @param request_length number of bytes in request
654 * @param request udp payload of the DNS request
657 dns_post_request_handler (void *cls,
658 struct GNUNET_DNS_RequestHandle *rh,
659 size_t request_length,
662 struct GNUNET_DNSPARSER_Packet *dns;
663 struct ReplyContext *rc;
666 GNUNET_STATISTICS_update (stats,
667 gettext_noop ("# DNS replies intercepted"),
669 dns = GNUNET_DNSPARSER_parse (request,
673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
674 _("Failed to parse DNS request. Dropping.\n"));
675 GNUNET_DNS_request_drop (rh);
679 work |= work_test (dns->answers,
681 work |= work_test (dns->authority_records,
682 dns->num_authority_records);
683 work |= work_test (dns->additional_records,
684 dns->num_additional_records);
687 GNUNET_DNS_request_forward (rh);
688 GNUNET_DNSPARSER_free_packet (dns);
691 rc = GNUNET_new (struct ReplyContext);
701 * Task run if the time to answer a DNS request via CADET is over.
703 * @param cls the `struct RequestContext` to abort
706 timeout_request (void *cls)
708 struct RequestContext *rc = cls;
709 struct CadetExit *exit = rc->exit;
711 GNUNET_STATISTICS_update (stats,
712 gettext_noop ("# DNS requests dropped (timeout)"),
715 GNUNET_DNS_request_drop (rc->rh);
717 if ( (0 == get_channel_weight (exit)) &&
718 (NULL == exit->receive_queue_head) )
720 /* this straw broke the camel's back: this channel now has
721 such a low score that it will not be used; close it! */
722 GNUNET_CADET_channel_destroy (exit->cadet_channel);
723 exit->cadet_channel = NULL;
724 GNUNET_CONTAINER_DLL_remove (exit_head,
727 GNUNET_CONTAINER_DLL_insert_tail (exit_head,
730 /* go back to semi-innocent: mark as not great, but
731 avoid a prohibitively negative score (see
732 #get_channel_weight(), which checks for a certain
733 minimum number of transmissions before making
735 exit->num_transmitted = 5;
736 exit->num_answered = 0;
737 dns_exit_available--;
738 /* now try to open an alternative exit */
745 * This function is called *before* the DNS request has been
746 * given to a "local" DNS resolver. Channeling for DNS requests
747 * was enabled, so we now need to send the request via some CADET
748 * channel to a DNS EXIT for resolution.
751 * @param rh request handle to user for reply
752 * @param request_length number of bytes in request
753 * @param request udp payload of the DNS request
756 dns_pre_request_handler (void *cls,
757 struct GNUNET_DNS_RequestHandle *rh,
758 size_t request_length,
761 struct RequestContext *rc;
762 struct GNUNET_MQ_Envelope *env;
763 struct GNUNET_MessageHeader *hdr;
764 struct GNUNET_TUN_DnsHeader dns;
765 struct CadetExit *exit;
767 GNUNET_STATISTICS_update (stats,
768 gettext_noop ("# DNS requests intercepted"),
770 if (0 == dns_exit_available)
772 GNUNET_STATISTICS_update (stats,
773 gettext_noop ("# DNS requests dropped (DNS cadet channel down)"),
775 GNUNET_DNS_request_drop (rh);
778 if (request_length < sizeof (dns))
780 GNUNET_STATISTICS_update (stats,
781 gettext_noop ("# DNS requests dropped (malformed)"),
783 GNUNET_DNS_request_drop (rh);
786 exit = choose_exit ();
787 GNUNET_assert (NULL != exit);
788 GNUNET_assert (NULL != exit->cadet_channel);
790 env = GNUNET_MQ_msg_extra (hdr,
792 GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET);
793 GNUNET_memcpy (&hdr[1],
796 rc = GNUNET_new (struct RequestContext);
799 rc->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
807 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
808 exit->receive_queue_tail,
812 exit->num_transmitted++;
813 GNUNET_MQ_send (GNUNET_CADET_get_mq (exit->cadet_channel),
814 GNUNET_MQ_env_copy (env));
818 GNUNET_NETWORK_STRUCT_BEGIN
821 * Message with a DNS response.
823 struct DnsResponseMessage
826 * GNUnet header, of type #GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET
828 struct GNUNET_MessageHeader header;
833 struct GNUNET_TUN_DnsHeader dns;
835 /* Followed by more DNS payload */
838 GNUNET_NETWORK_STRUCT_END
841 * Process a request via cadet to perform a DNS query.
843 * @param cls the `struct CadetExit` which got the message
844 * @param msg the actual message
845 * @return #GNUNET_OK to keep the connection open,
846 * #GNUNET_SYSERR to close it (signal serious error)
849 check_dns_response (void *cls,
850 const struct DnsResponseMessage *msg)
852 return GNUNET_OK; /* all OK */
857 * Process a request via cadet to perform a DNS query.
859 * @param cls the `struct CadetExit` which got the message
860 * @param msg the actual message
863 handle_dns_response (void *cls,
864 const struct DnsResponseMessage *msg)
866 struct CadetExit *exit = cls;
867 struct GNUNET_TUN_DnsHeader dns;
869 struct RequestContext *rc;
871 mlen = ntohs (msg->header.size) - sizeof (*msg);
872 for (rc = exit->receive_queue_head; NULL != rc; rc = rc->next)
874 if (dns.id == rc->dns_id)
876 GNUNET_STATISTICS_update (stats,
877 gettext_noop ("# DNS replies received"),
880 GNUNET_DNS_request_answer (rc->rh,
881 mlen + sizeof (struct GNUNET_TUN_DnsHeader),
882 (const void*) &msg->dns);
883 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
884 exit->receive_queue_tail,
886 GNUNET_SCHEDULER_cancel (rc->timeout_task);
887 GNUNET_MQ_discard (rc->env);
889 exit->num_answered++;
893 GNUNET_STATISTICS_update (stats,
894 gettext_noop ("# DNS replies dropped (too late?)"),
900 * Abort all pending DNS requests with the given cadet exit.
902 * @param exit cadet exit to abort requests for
905 abort_all_requests (struct CadetExit *exit)
907 struct RequestContext *rc;
909 while (NULL != (rc = exit->receive_queue_head))
911 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
912 exit->receive_queue_tail,
914 GNUNET_DNS_request_drop (rc->rh);
915 GNUNET_SCHEDULER_cancel (rc->timeout_task);
916 GNUNET_MQ_discard (rc->env);
923 * Function scheduled as very last function, cleans up after us
925 * @param cls closure, NULL
930 struct CadetExit *exit;
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
933 "Protocol translation daemon is shutting down now\n");
934 if (NULL != vpn_handle)
936 GNUNET_VPN_disconnect (vpn_handle);
939 while (NULL != (exit = exit_head))
941 GNUNET_CONTAINER_DLL_remove (exit_head,
944 if (NULL != exit->cadet_channel)
946 GNUNET_CADET_channel_destroy (exit->cadet_channel);
947 exit->cadet_channel = NULL;
949 abort_all_requests (exit);
952 if (NULL != cadet_handle)
954 GNUNET_CADET_disconnect (cadet_handle);
957 if (NULL != dns_post_handle)
959 GNUNET_DNS_disconnect (dns_post_handle);
960 dns_post_handle = NULL;
962 if (NULL != dns_pre_handle)
964 GNUNET_DNS_disconnect (dns_pre_handle);
965 dns_pre_handle = NULL;
969 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
974 GNUNET_DHT_get_stop (dht_get);
979 GNUNET_DHT_disconnect (dht);
986 * Function called whenever a channel is destroyed. Should clean up
987 * the associated state and attempt to build a new one.
989 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
991 * @param cls closure (the `struct CadetExit` set from #GNUNET_CADET_connect)
992 * @param channel connection to the other end (henceforth invalid)
993 * @param channel_ctx place where local state associated
994 * with the channel is stored
997 cadet_channel_end_cb (void *cls,
998 const struct GNUNET_CADET_Channel *channel)
1000 struct CadetExit *exit = cls;
1001 struct CadetExit *alt;
1002 struct RequestContext *rc;
1004 exit->cadet_channel = NULL;
1005 dns_exit_available--;
1006 /* open alternative channels */
1007 /* our channel is now closed, move our requests to an alternative
1009 alt = choose_exit ();
1010 while (NULL != (rc = exit->receive_queue_head))
1012 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
1013 exit->receive_queue_tail,
1016 GNUNET_CONTAINER_DLL_insert (alt->receive_queue_head,
1017 alt->receive_queue_tail,
1019 GNUNET_MQ_send (GNUNET_CADET_get_mq (exit->cadet_channel),
1020 GNUNET_MQ_env_copy (rc->env));
1027 * Function called whenever a channel has excess capacity.
1029 * @param cls the `struct CadetExit`
1030 * @param channel connection to the other end
1031 * @param window_size how much capacity do we have
1034 channel_idle_notify_cb (void *cls,
1035 const struct GNUNET_CADET_Channel *channel,
1038 struct CadetExit *pos = cls;
1040 pos->idle = window_size;
1045 * We are short on cadet exits, try to open another one.
1050 struct CadetExit *pos;
1051 uint32_t candidate_count;
1052 uint32_t candidate_selected;
1053 struct GNUNET_HashCode port;
1055 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER,
1056 strlen (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER),
1058 candidate_count = 0;
1059 for (pos = exit_head; NULL != pos; pos = pos->next)
1060 if (NULL == pos->cadet_channel)
1062 if (0 == candidate_count)
1064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1065 "No DNS exits available yet.\n");
1068 candidate_selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1070 candidate_count = 0;
1071 for (pos = exit_head; NULL != pos; pos = pos->next)
1072 if (NULL == pos->cadet_channel)
1075 if (candidate_selected < candidate_count)
1077 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
1078 GNUNET_MQ_hd_var_size (dns_response,
1079 GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET,
1080 struct DnsResponseMessage,
1082 GNUNET_MQ_handler_end ()
1086 /* move to the head of the DLL */
1088 = GNUNET_CADET_channel_creatE (cadet_handle,
1092 GNUNET_CADET_OPTION_DEFAULT,
1093 &channel_idle_notify_cb,
1094 &cadet_channel_end_cb,
1096 if (NULL == pos->cadet_channel)
1101 GNUNET_CONTAINER_DLL_remove (exit_head,
1104 GNUNET_CONTAINER_DLL_insert (exit_head,
1107 dns_exit_available++;
1111 GNUNET_assert (NULL == exit_head);
1116 * Function called whenever we find an advertisement for a
1117 * DNS exit in the DHT. If we don't have a cadet channel,
1118 * we should build one; otherwise, we should save the
1119 * advertisement for later use.
1121 * @param cls closure
1122 * @param exp when will this value expire
1123 * @param key key of the result
1124 * @param get_path peers on reply path (or NULL if not recorded)
1125 * [0] = datastore's first neighbor, [length - 1] = local peer
1126 * @param get_path_length number of entries in @a get_path
1127 * @param put_path peers on the PUT path (or NULL if not recorded)
1128 * [0] = origin, [length - 1] = datastore
1129 * @param put_path_length number of entries in @a put_path
1130 * @param type type of the result
1131 * @param size number of bytes in @a data
1132 * @param data pointer to the result data
1135 handle_dht_result (void *cls,
1136 struct GNUNET_TIME_Absolute exp,
1137 const struct GNUNET_HashCode *key,
1138 const struct GNUNET_PeerIdentity *get_path,
1139 unsigned int get_path_length,
1140 const struct GNUNET_PeerIdentity *put_path,
1141 unsigned int put_path_length,
1142 enum GNUNET_BLOCK_Type type,
1143 size_t size, const void *data)
1145 const struct GNUNET_DNS_Advertisement *ad;
1146 struct CadetExit *exit;
1148 if (sizeof (struct GNUNET_DNS_Advertisement) != size)
1154 for (exit = exit_head; NULL != exit; exit = exit->next)
1155 if (0 == memcmp (&ad->peer,
1157 sizeof (struct GNUNET_PeerIdentity)))
1161 exit = GNUNET_new (struct CadetExit);
1162 exit->peer = ad->peer;
1163 /* channel is closed, so insert at the end */
1164 GNUNET_CONTAINER_DLL_insert_tail (exit_head,
1168 exit->expiration = GNUNET_TIME_absolute_max (exit->expiration,
1169 GNUNET_TIME_absolute_ntoh (ad->expiration_time));
1170 if (dns_exit_available < MAX_OPEN_TUNNELS)
1176 * @brief Main function that will be run by the scheduler.
1178 * @param cls closure
1179 * @param args remaining command-line arguments
1180 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1181 * @param cfg_ configuration
1184 run (void *cls, char *const *args GNUNET_UNUSED,
1185 const char *cfgfile GNUNET_UNUSED,
1186 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1188 struct GNUNET_HashCode dns_key;
1191 stats = GNUNET_STATISTICS_create ("pt",
1193 ipv4_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1196 ipv6_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1199 dns_channel = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1202 if (! (ipv4_pt || ipv6_pt || dns_channel))
1204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1205 _("No useful service enabled. Exiting.\n"));
1206 GNUNET_SCHEDULER_shutdown ();
1209 GNUNET_SCHEDULER_add_shutdown (&cleanup, cls);
1210 if (ipv4_pt || ipv6_pt)
1213 = GNUNET_DNS_connect (cfg,
1214 GNUNET_DNS_FLAG_POST_RESOLUTION,
1215 &dns_post_request_handler,
1217 if (NULL == dns_post_handle)
1219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1220 _("Failed to connect to %s service. Exiting.\n"),
1222 GNUNET_SCHEDULER_shutdown ();
1225 vpn_handle = GNUNET_VPN_connect (cfg);
1226 if (NULL == vpn_handle)
1228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1229 _("Failed to connect to %s service. Exiting.\n"),
1231 GNUNET_SCHEDULER_shutdown ();
1238 = GNUNET_DNS_connect (cfg,
1239 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1240 &dns_pre_request_handler,
1242 if (NULL == dns_pre_handle)
1244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1245 _("Failed to connect to %s service. Exiting.\n"),
1247 GNUNET_SCHEDULER_shutdown ();
1250 cadet_handle = GNUNET_CADET_connecT (cfg);
1251 if (NULL == cadet_handle)
1253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1254 _("Failed to connect to %s service. Exiting.\n"),
1256 GNUNET_SCHEDULER_shutdown ();
1259 dht = GNUNET_DHT_connect (cfg, 1);
1262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1263 _("Failed to connect to %s service. Exiting.\n"),
1265 GNUNET_SCHEDULER_shutdown ();
1268 GNUNET_CRYPTO_hash ("dns",
1271 dht_get = GNUNET_DHT_get_start (dht,
1272 GNUNET_BLOCK_TYPE_DNS,
1275 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1286 * @param argc number of arguments from the command line
1287 * @param argv command line arguments
1288 * @return 0 ok, 1 on error
1294 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1295 GNUNET_GETOPT_OPTION_END
1299 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
1305 GNUNET_PROGRAM_run (argc,
1308 gettext_noop ("Daemon to run to perform IP protocol translation to GNUnet"),
1314 GNUNET_free ((void*) argv);
1319 /* end of gnunet-daemon-pt.c */