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;
868 struct RequestContext *rc;
870 mlen = ntohs (msg->header.size) - sizeof (*msg);
871 for (rc = exit->receive_queue_head; NULL != rc; rc = rc->next)
873 if (msg->dns.id == rc->dns_id)
875 GNUNET_STATISTICS_update (stats,
876 gettext_noop ("# DNS replies received"),
879 GNUNET_DNS_request_answer (rc->rh,
880 mlen + sizeof (struct GNUNET_TUN_DnsHeader),
881 (const void*) &msg->dns);
882 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
883 exit->receive_queue_tail,
885 GNUNET_SCHEDULER_cancel (rc->timeout_task);
886 GNUNET_MQ_discard (rc->env);
888 exit->num_answered++;
892 GNUNET_STATISTICS_update (stats,
893 gettext_noop ("# DNS replies dropped (too late?)"),
899 * Abort all pending DNS requests with the given cadet exit.
901 * @param exit cadet exit to abort requests for
904 abort_all_requests (struct CadetExit *exit)
906 struct RequestContext *rc;
908 while (NULL != (rc = exit->receive_queue_head))
910 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
911 exit->receive_queue_tail,
913 GNUNET_DNS_request_drop (rc->rh);
914 GNUNET_SCHEDULER_cancel (rc->timeout_task);
915 GNUNET_MQ_discard (rc->env);
922 * Function scheduled as very last function, cleans up after us
924 * @param cls closure, NULL
929 struct CadetExit *exit;
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Protocol translation daemon is shutting down now\n");
933 if (NULL != vpn_handle)
935 GNUNET_VPN_disconnect (vpn_handle);
938 while (NULL != (exit = exit_head))
940 GNUNET_CONTAINER_DLL_remove (exit_head,
943 if (NULL != exit->cadet_channel)
945 GNUNET_CADET_channel_destroy (exit->cadet_channel);
946 exit->cadet_channel = NULL;
948 abort_all_requests (exit);
951 if (NULL != cadet_handle)
953 GNUNET_CADET_disconnect (cadet_handle);
956 if (NULL != dns_post_handle)
958 GNUNET_DNS_disconnect (dns_post_handle);
959 dns_post_handle = NULL;
961 if (NULL != dns_pre_handle)
963 GNUNET_DNS_disconnect (dns_pre_handle);
964 dns_pre_handle = NULL;
968 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
973 GNUNET_DHT_get_stop (dht_get);
978 GNUNET_DHT_disconnect (dht);
985 * Function called whenever a channel is destroyed. Should clean up
986 * the associated state and attempt to build a new one.
988 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
990 * @param cls closure (the `struct CadetExit` set from #GNUNET_CADET_connect)
991 * @param channel connection to the other end (henceforth invalid)
992 * @param channel_ctx place where local state associated
993 * with the channel is stored
996 cadet_channel_end_cb (void *cls,
997 const struct GNUNET_CADET_Channel *channel)
999 struct CadetExit *exit = cls;
1000 struct CadetExit *alt;
1001 struct RequestContext *rc;
1003 exit->cadet_channel = NULL;
1004 dns_exit_available--;
1005 /* open alternative channels */
1006 /* our channel is now closed, move our requests to an alternative
1008 alt = choose_exit ();
1009 while (NULL != (rc = exit->receive_queue_head))
1011 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
1012 exit->receive_queue_tail,
1015 GNUNET_CONTAINER_DLL_insert (alt->receive_queue_head,
1016 alt->receive_queue_tail,
1018 GNUNET_MQ_send (GNUNET_CADET_get_mq (alt->cadet_channel),
1019 GNUNET_MQ_env_copy (rc->env));
1026 * Function called whenever a channel has excess capacity.
1028 * @param cls the `struct CadetExit`
1029 * @param channel connection to the other end
1030 * @param window_size how much capacity do we have
1033 channel_idle_notify_cb (void *cls,
1034 const struct GNUNET_CADET_Channel *channel,
1037 struct CadetExit *pos = cls;
1039 pos->idle = window_size;
1044 * We are short on cadet exits, try to open another one.
1049 struct CadetExit *pos;
1050 uint32_t candidate_count;
1051 uint32_t candidate_selected;
1052 struct GNUNET_HashCode port;
1054 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER,
1055 strlen (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER),
1057 candidate_count = 0;
1058 for (pos = exit_head; NULL != pos; pos = pos->next)
1059 if (NULL == pos->cadet_channel)
1061 if (0 == candidate_count)
1063 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1064 "No DNS exits available yet.\n");
1067 candidate_selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1069 candidate_count = 0;
1070 for (pos = exit_head; NULL != pos; pos = pos->next)
1071 if (NULL == pos->cadet_channel)
1074 if (candidate_selected < candidate_count)
1076 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
1077 GNUNET_MQ_hd_var_size (dns_response,
1078 GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET,
1079 struct DnsResponseMessage,
1081 GNUNET_MQ_handler_end ()
1085 /* move to the head of the DLL */
1087 = GNUNET_CADET_channel_create (cadet_handle,
1091 GNUNET_CADET_OPTION_DEFAULT,
1092 &channel_idle_notify_cb,
1093 &cadet_channel_end_cb,
1095 if (NULL == pos->cadet_channel)
1100 GNUNET_CONTAINER_DLL_remove (exit_head,
1103 GNUNET_CONTAINER_DLL_insert (exit_head,
1106 dns_exit_available++;
1110 GNUNET_assert (NULL == exit_head);
1115 * Function called whenever we find an advertisement for a
1116 * DNS exit in the DHT. If we don't have a cadet channel,
1117 * we should build one; otherwise, we should save the
1118 * advertisement for later use.
1120 * @param cls closure
1121 * @param exp when will this value expire
1122 * @param key key of the result
1123 * @param get_path peers on reply path (or NULL if not recorded)
1124 * [0] = datastore's first neighbor, [length - 1] = local peer
1125 * @param get_path_length number of entries in @a get_path
1126 * @param put_path peers on the PUT path (or NULL if not recorded)
1127 * [0] = origin, [length - 1] = datastore
1128 * @param put_path_length number of entries in @a put_path
1129 * @param type type of the result
1130 * @param size number of bytes in @a data
1131 * @param data pointer to the result data
1134 handle_dht_result (void *cls,
1135 struct GNUNET_TIME_Absolute exp,
1136 const struct GNUNET_HashCode *key,
1137 const struct GNUNET_PeerIdentity *get_path,
1138 unsigned int get_path_length,
1139 const struct GNUNET_PeerIdentity *put_path,
1140 unsigned int put_path_length,
1141 enum GNUNET_BLOCK_Type type,
1142 size_t size, const void *data)
1144 const struct GNUNET_DNS_Advertisement *ad;
1145 struct CadetExit *exit;
1147 if (sizeof (struct GNUNET_DNS_Advertisement) != size)
1153 for (exit = exit_head; NULL != exit; exit = exit->next)
1154 if (0 == memcmp (&ad->peer,
1156 sizeof (struct GNUNET_PeerIdentity)))
1160 exit = GNUNET_new (struct CadetExit);
1161 exit->peer = ad->peer;
1162 /* channel is closed, so insert at the end */
1163 GNUNET_CONTAINER_DLL_insert_tail (exit_head,
1167 exit->expiration = GNUNET_TIME_absolute_max (exit->expiration,
1168 GNUNET_TIME_absolute_ntoh (ad->expiration_time));
1169 if (dns_exit_available < MAX_OPEN_TUNNELS)
1175 * @brief Main function that will be run by the scheduler.
1177 * @param cls closure
1178 * @param args remaining command-line arguments
1179 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1180 * @param cfg_ configuration
1183 run (void *cls, char *const *args GNUNET_UNUSED,
1184 const char *cfgfile GNUNET_UNUSED,
1185 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1187 struct GNUNET_HashCode dns_key;
1190 stats = GNUNET_STATISTICS_create ("pt",
1192 ipv4_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1195 ipv6_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1198 dns_channel = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1201 if (! (ipv4_pt || ipv6_pt || dns_channel))
1203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1204 _("No useful service enabled. Exiting.\n"));
1205 GNUNET_SCHEDULER_shutdown ();
1208 GNUNET_SCHEDULER_add_shutdown (&cleanup, cls);
1209 if (ipv4_pt || ipv6_pt)
1212 = GNUNET_DNS_connect (cfg,
1213 GNUNET_DNS_FLAG_POST_RESOLUTION,
1214 &dns_post_request_handler,
1216 if (NULL == dns_post_handle)
1218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1219 _("Failed to connect to %s service. Exiting.\n"),
1221 GNUNET_SCHEDULER_shutdown ();
1224 vpn_handle = GNUNET_VPN_connect (cfg);
1225 if (NULL == vpn_handle)
1227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1228 _("Failed to connect to %s service. Exiting.\n"),
1230 GNUNET_SCHEDULER_shutdown ();
1237 = GNUNET_DNS_connect (cfg,
1238 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1239 &dns_pre_request_handler,
1241 if (NULL == dns_pre_handle)
1243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1244 _("Failed to connect to %s service. Exiting.\n"),
1246 GNUNET_SCHEDULER_shutdown ();
1249 cadet_handle = GNUNET_CADET_connect (cfg);
1250 if (NULL == cadet_handle)
1252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1253 _("Failed to connect to %s service. Exiting.\n"),
1255 GNUNET_SCHEDULER_shutdown ();
1258 dht = GNUNET_DHT_connect (cfg, 1);
1261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1262 _("Failed to connect to %s service. Exiting.\n"),
1264 GNUNET_SCHEDULER_shutdown ();
1267 GNUNET_CRYPTO_hash ("dns",
1270 dht_get = GNUNET_DHT_get_start (dht,
1271 GNUNET_BLOCK_TYPE_DNS,
1274 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1285 * @param argc number of arguments from the command line
1286 * @param argv command line arguments
1287 * @return 0 ok, 1 on error
1293 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1294 GNUNET_GETOPT_OPTION_END
1298 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
1304 GNUNET_PROGRAM_run (argc,
1307 gettext_noop ("Daemon to run to perform IP protocol translation to GNUnet"),
1313 GNUNET_free ((void*) argv);
1318 /* end of gnunet-daemon-pt.c */