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 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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;
123 * Handle to a peer that advertised that it is willing to serve
124 * as a DNS exit. We try to keep a few channels open and a few
132 struct CadetExit *next;
137 struct CadetExit *prev;
140 * Channel we use for DNS requests over CADET, NULL if we did
141 * not initialze a channel to this peer yet.
143 struct GNUNET_CADET_Channel *cadet_channel;
146 * At what time did the peer's advertisement expire?
148 struct GNUNET_TIME_Absolute expiration;
151 * Head of DLL of requests waiting for a response.
153 struct RequestContext *receive_queue_head;
156 * Tail of DLL of requests waiting for a response.
158 struct RequestContext *receive_queue_tail;
161 * Identity of the peer that is providing the exit for us.
163 struct GNUNET_PeerIdentity peer;
166 * How many DNS requests did we transmit via this channel?
168 unsigned int num_transmitted;
171 * How many DNS requests were answered via this channel?
173 unsigned int num_answered;
176 * Size of the window, 0 if we are busy.
178 /* unsigned */ int idle;
184 * State we keep for a request that is going out via CADET.
186 struct RequestContext
189 * We keep these in a DLL.
191 struct RequestContext *next;
194 * We keep these in a DLL.
196 struct RequestContext *prev;
199 * Exit that was chosen for this request.
201 struct CadetExit *exit;
204 * Handle for interaction with DNS service.
206 struct GNUNET_DNS_RequestHandle *rh;
209 * Envelope with the request we are transmitting.
211 struct GNUNET_MQ_Envelope *env;
214 * Task used to abort this operation with timeout.
216 struct GNUNET_SCHEDULER_Task *timeout_task;
219 * Length of the request message that follows this struct.
224 * ID of the original DNS request (used to match the reply).
231 * Head of DLL of cadet exits. Cadet exits with an open channel are
232 * always at the beginning (so we do not have to traverse the entire
233 * list to find them).
235 static struct CadetExit *exit_head;
238 * Tail of DLL of cadet exits.
240 static struct CadetExit *exit_tail;
243 * The handle to the configuration used throughout the process
245 static const struct GNUNET_CONFIGURATION_Handle *cfg;
248 * The handle to the VPN
250 static struct GNUNET_VPN_Handle *vpn_handle;
253 * The handle to the CADET service
255 static struct GNUNET_CADET_Handle *cadet_handle;
260 static struct GNUNET_STATISTICS_Handle *stats;
263 * The handle to DNS post-resolution modifications.
265 static struct GNUNET_DNS_Handle *dns_post_handle;
268 * The handle to DNS pre-resolution modifications.
270 static struct GNUNET_DNS_Handle *dns_pre_handle;
273 * Handle to access the DHT.
275 static struct GNUNET_DHT_Handle *dht;
278 * Our DHT GET operation to find DNS exits.
280 static struct GNUNET_DHT_GetHandle *dht_get;
283 * Are we doing IPv4-pt?
288 * Are we doing IPv6-pt?
293 * Are we channeling DNS queries?
295 static int dns_channel;
298 * Number of DNS exit peers we currently have in the cadet channel.
299 * Used to see if using the cadet channel makes any sense right now,
300 * as well as to decide if we should open new channels.
302 static unsigned int dns_exit_available;
306 * We are short on cadet exits, try to open another one.
309 try_open_exit (void);
313 * Compute the weight of the given exit. The higher the weight,
314 * the more likely it will be that the channel will be chosen.
315 * A weigt of zero means that we should close the channel as it
316 * is so bad, that we should not use it.
318 * @param exit exit to calculate the weight for
319 * @return weight of the channel
322 get_channel_weight (struct CadetExit *exit)
325 uint32_t drop_percent;
326 uint32_t good_percent;
328 GNUNET_assert (exit->num_transmitted >= exit->num_answered);
329 dropped = exit->num_transmitted - exit->num_answered;
330 if (exit->num_transmitted > 0)
331 drop_percent = (uint32_t) ((100LL * dropped) / exit->num_transmitted);
333 drop_percent = 50; /* no data */
334 if ((exit->num_transmitted > 20) &&
336 return 0; /* statistically significant, and > 25% loss, die */
337 good_percent = 100 - drop_percent;
338 GNUNET_assert (0 != good_percent);
339 if (UINT32_MAX / good_percent / good_percent < exit->num_transmitted)
340 return UINT32_MAX; /* formula below would overflow */
341 return 1 + good_percent * good_percent * exit->num_transmitted;
346 * Choose a cadet exit for a DNS request. We try to use a channel
347 * that is reliable and currently available. All existing
348 * channels are given a base weight of 1, plus a score relating
349 * to the total number of queries answered in relation to the
350 * total number of queries we sent to that channel. That
351 * score is doubled if the channel is currently idle.
353 * @return NULL if no exit is known, otherwise the
354 * exit that we should use to queue a message with
356 static struct CadetExit *
359 struct CadetExit *pos;
360 uint64_t total_transmitted;
361 uint64_t selected_offset;
362 uint32_t channel_weight;
364 total_transmitted = 0;
365 for (pos = exit_head; NULL != pos; pos = pos->next)
367 if (NULL == pos->cadet_channel)
369 channel_weight = get_channel_weight (pos);
370 total_transmitted += channel_weight;
371 /* double weight for idle channels */
373 total_transmitted += channel_weight;
375 if (0 == total_transmitted)
377 /* no channels available, or only a very bad one... */
380 selected_offset = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
382 total_transmitted = 0;
383 for (pos = exit_head; NULL != pos; pos = pos->next)
385 if (NULL == pos->cadet_channel)
387 channel_weight = get_channel_weight (pos);
388 total_transmitted += channel_weight;
389 /* double weight for idle channels */
391 total_transmitted += channel_weight;
392 if (total_transmitted > selected_offset)
401 * We're done modifying all records in the response. Submit the reply
402 * and free the resources of the rc.
404 * @param rc context to process
407 finish_request (struct ReplyContext *rc)
413 GNUNET_DNSPARSER_pack (rc->dns,
418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
419 _ ("Failed to pack DNS request. Dropping.\n"));
420 GNUNET_DNS_request_drop (rc->rh);
424 GNUNET_STATISTICS_update (stats,
425 gettext_noop ("# DNS requests mapped to VPN"),
427 GNUNET_DNS_request_answer (rc->rh,
432 GNUNET_DNSPARSER_free_packet (rc->dns);
438 * Process the next record of the given request context.
439 * When done, submit the reply and free the resources of
442 * @param rc context to process
445 submit_request (struct ReplyContext *rc);
449 * Callback invoked from the VPN service once a redirection is
450 * available. Provides the IP address that can now be used to
451 * reach the requested destination. We substitute the active
452 * record and then continue with 'submit_request' to look at
455 * @param cls our `struct ReplyContext`
456 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
457 * will match 'result_af' from the request
458 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
459 * that the VPN allocated for the redirection;
460 * traffic to this IP will now be redirected to the
461 * specified target peer; NULL on error
464 vpn_allocation_callback (void *cls,
468 struct ReplyContext *rc = cls;
473 GNUNET_DNS_request_drop (rc->rh);
474 GNUNET_DNSPARSER_free_packet (rc->dns);
478 GNUNET_STATISTICS_update (stats,
479 gettext_noop ("# DNS records modified"),
482 switch (rc->rec->type)
484 case GNUNET_DNSPARSER_TYPE_A:
485 GNUNET_assert (AF_INET == af);
486 GNUNET_memcpy (rc->rec->data.raw.data,
488 sizeof(struct in_addr));
491 case GNUNET_DNSPARSER_TYPE_AAAA:
492 GNUNET_assert (AF_INET6 == af);
493 GNUNET_memcpy (rc->rec->data.raw.data,
495 sizeof(struct in6_addr));
508 * Modify the given DNS record by asking VPN to create a channel
509 * to the given address. When done, continue with submitting
510 * other records from the request context ('submit_request' is
513 * @param rc context to process
514 * @param rec record to modify
517 modify_address (struct ReplyContext *rc,
518 struct GNUNET_DNSPARSER_Record *rec)
524 case GNUNET_DNSPARSER_TYPE_A:
526 GNUNET_assert (rec->data.raw.data_len == sizeof(struct in_addr));
529 case GNUNET_DNSPARSER_TYPE_AAAA:
531 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 (
545 &vpn_allocation_callback,
551 * Process the next record of the given request context.
552 * When done, submit the reply and free the resources of
555 * @param rc context to process
558 submit_request (struct ReplyContext *rc)
560 struct GNUNET_DNSPARSER_Record *ra;
569 ra = rc->dns->answers;
570 ra_len = rc->dns->num_answers;
573 case AUTHORITY_RECORDS:
574 ra = rc->dns->authority_records;
575 ra_len = rc->dns->num_authority_records;
578 case ADDITIONAL_RECORDS:
579 ra = rc->dns->additional_records;
580 ra_len = rc->dns->num_additional_records;
590 for (i = rc->offset; i < ra_len; i++)
594 case GNUNET_DNSPARSER_TYPE_A:
604 case GNUNET_DNSPARSER_TYPE_AAAA:
621 * Test if any of the given records need protocol-translation work.
623 * @param ra array of records
624 * @param ra_len number of entries in @a ra
625 * @return #GNUNET_YES if any of the given records require protocol-translation
628 work_test (const struct GNUNET_DNSPARSER_Record *ra,
633 for (i = 0; i < ra_len; i++)
637 case GNUNET_DNSPARSER_TYPE_A:
642 case GNUNET_DNSPARSER_TYPE_AAAA:
653 * This function is called AFTER we got an IP address for a
654 * DNS request. Now, the PT daemon has the chance to substitute
655 * the IP address with one from the VPN range to channel requests
656 * destined for this IP address via VPN and CADET.
659 * @param rh request handle to user for reply
660 * @param request_length number of bytes in request
661 * @param request udp payload of the DNS request
664 dns_post_request_handler (void *cls,
665 struct GNUNET_DNS_RequestHandle *rh,
666 size_t request_length,
669 struct GNUNET_DNSPARSER_Packet *dns;
670 struct ReplyContext *rc;
673 GNUNET_STATISTICS_update (stats,
674 gettext_noop ("# DNS replies intercepted"),
676 dns = GNUNET_DNSPARSER_parse (request,
680 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
681 _ ("Failed to parse DNS request. Dropping.\n"));
682 GNUNET_DNS_request_drop (rh);
686 work |= work_test (dns->answers,
688 work |= work_test (dns->authority_records,
689 dns->num_authority_records);
690 work |= work_test (dns->additional_records,
691 dns->num_additional_records);
694 GNUNET_DNS_request_forward (rh);
695 GNUNET_DNSPARSER_free_packet (dns);
698 rc = GNUNET_new (struct ReplyContext);
708 * Task run if the time to answer a DNS request via CADET is over.
710 * @param cls the `struct RequestContext` to abort
713 timeout_request (void *cls)
715 struct RequestContext *rc = cls;
716 struct CadetExit *exit = rc->exit;
718 GNUNET_STATISTICS_update (stats,
719 gettext_noop ("# DNS requests dropped (timeout)"),
722 GNUNET_DNS_request_drop (rc->rh);
724 if ((0 == get_channel_weight (exit)) &&
725 (NULL == exit->receive_queue_head))
727 /* this straw broke the camel's back: this channel now has
728 such a low score that it will not be used; close it! */
729 GNUNET_CADET_channel_destroy (exit->cadet_channel);
730 exit->cadet_channel = NULL;
731 GNUNET_CONTAINER_DLL_remove (exit_head,
734 GNUNET_CONTAINER_DLL_insert_tail (exit_head,
737 /* go back to semi-innocent: mark as not great, but
738 avoid a prohibitively negative score (see
739 #get_channel_weight(), which checks for a certain
740 minimum number of transmissions before making
742 exit->num_transmitted = 5;
743 exit->num_answered = 0;
744 dns_exit_available--;
745 /* now try to open an alternative exit */
752 * This function is called *before* the DNS request has been
753 * given to a "local" DNS resolver. Channeling for DNS requests
754 * was enabled, so we now need to send the request via some CADET
755 * channel to a DNS EXIT for resolution.
758 * @param rh request handle to user for reply
759 * @param request_length number of bytes in request
760 * @param request udp payload of the DNS request
763 dns_pre_request_handler (void *cls,
764 struct GNUNET_DNS_RequestHandle *rh,
765 size_t request_length,
768 struct RequestContext *rc;
769 struct GNUNET_MQ_Envelope *env;
770 struct GNUNET_MessageHeader *hdr;
771 struct GNUNET_TUN_DnsHeader dns;
772 struct CadetExit *exit;
774 GNUNET_STATISTICS_update (stats,
775 gettext_noop ("# DNS requests intercepted"),
777 if (0 == dns_exit_available)
779 GNUNET_STATISTICS_update (stats,
781 "# DNS requests dropped (DNS cadet channel down)"),
783 GNUNET_DNS_request_drop (rh);
786 if (request_length < sizeof(dns))
788 GNUNET_STATISTICS_update (stats,
790 "# DNS requests dropped (malformed)"),
792 GNUNET_DNS_request_drop (rh);
795 exit = choose_exit ();
796 GNUNET_assert (NULL != exit);
797 GNUNET_assert (NULL != exit->cadet_channel);
799 env = GNUNET_MQ_msg_extra (hdr,
801 GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET);
802 GNUNET_memcpy (&hdr[1],
805 rc = GNUNET_new (struct RequestContext);
808 rc->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
816 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
817 exit->receive_queue_tail,
821 exit->num_transmitted++;
822 GNUNET_MQ_send (GNUNET_CADET_get_mq (exit->cadet_channel),
823 GNUNET_MQ_env_copy (env));
827 GNUNET_NETWORK_STRUCT_BEGIN
830 * Message with a DNS response.
832 struct DnsResponseMessage
835 * GNUnet header, of type #GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET
837 struct GNUNET_MessageHeader header;
842 struct GNUNET_TUN_DnsHeader dns;
844 /* Followed by more DNS payload */
847 GNUNET_NETWORK_STRUCT_END
850 * Process a request via cadet to perform a DNS query.
852 * @param cls the `struct CadetExit` which got the message
853 * @param msg the actual message
854 * @return #GNUNET_OK to keep the connection open,
855 * #GNUNET_SYSERR to close it (signal serious error)
858 check_dns_response (void *cls,
859 const struct DnsResponseMessage *msg)
861 return GNUNET_OK; /* all OK */
866 * Process a request via cadet to perform a DNS query.
868 * @param cls the `struct CadetExit` which got the message
869 * @param msg the actual message
872 handle_dns_response (void *cls,
873 const struct DnsResponseMessage *msg)
875 struct CadetExit *exit = cls;
877 struct RequestContext *rc;
879 mlen = ntohs (msg->header.size) - sizeof(*msg);
880 for (rc = exit->receive_queue_head; NULL != rc; rc = rc->next)
882 if (msg->dns.id == rc->dns_id)
884 GNUNET_STATISTICS_update (stats,
885 gettext_noop ("# DNS replies received"),
888 GNUNET_DNS_request_answer (rc->rh,
889 mlen + sizeof(struct GNUNET_TUN_DnsHeader),
890 (const void*) &msg->dns);
891 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
892 exit->receive_queue_tail,
894 GNUNET_SCHEDULER_cancel (rc->timeout_task);
895 GNUNET_MQ_discard (rc->env);
897 exit->num_answered++;
901 GNUNET_STATISTICS_update (stats,
902 gettext_noop ("# DNS replies dropped (too late?)"),
908 * Abort all pending DNS requests with the given cadet exit.
910 * @param exit cadet exit to abort requests for
913 abort_all_requests (struct CadetExit *exit)
915 struct RequestContext *rc;
917 while (NULL != (rc = exit->receive_queue_head))
919 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
920 exit->receive_queue_tail,
922 GNUNET_DNS_request_drop (rc->rh);
923 GNUNET_SCHEDULER_cancel (rc->timeout_task);
924 GNUNET_MQ_discard (rc->env);
931 * Function scheduled as very last function, cleans up after us
933 * @param cls closure, NULL
938 struct CadetExit *exit;
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941 "Protocol translation daemon is shutting down now\n");
942 if (NULL != vpn_handle)
944 GNUNET_VPN_disconnect (vpn_handle);
947 while (NULL != (exit = exit_head))
949 GNUNET_CONTAINER_DLL_remove (exit_head,
952 if (NULL != exit->cadet_channel)
954 GNUNET_CADET_channel_destroy (exit->cadet_channel);
955 exit->cadet_channel = NULL;
957 abort_all_requests (exit);
960 if (NULL != cadet_handle)
962 GNUNET_CADET_disconnect (cadet_handle);
965 if (NULL != dns_post_handle)
967 GNUNET_DNS_disconnect (dns_post_handle);
968 dns_post_handle = NULL;
970 if (NULL != dns_pre_handle)
972 GNUNET_DNS_disconnect (dns_pre_handle);
973 dns_pre_handle = NULL;
977 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
982 GNUNET_DHT_get_stop (dht_get);
987 GNUNET_DHT_disconnect (dht);
994 * Function called whenever a channel is destroyed. Should clean up
995 * the associated state and attempt to build a new one.
997 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
999 * @param cls closure (the `struct CadetExit` set from #GNUNET_CADET_connect)
1000 * @param channel connection to the other end (henceforth invalid)
1001 * @param channel_ctx place where local state associated
1002 * with the channel is stored
1005 cadet_channel_end_cb (void *cls,
1006 const struct GNUNET_CADET_Channel *channel)
1008 struct CadetExit *exit = cls;
1009 struct CadetExit *alt;
1010 struct RequestContext *rc;
1012 exit->cadet_channel = NULL;
1013 dns_exit_available--;
1014 /* open alternative channels */
1015 /* our channel is now closed, move our requests to an alternative
1017 alt = choose_exit ();
1018 while (NULL != (rc = exit->receive_queue_head))
1020 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
1021 exit->receive_queue_tail,
1024 GNUNET_CONTAINER_DLL_insert (alt->receive_queue_head,
1025 alt->receive_queue_tail,
1027 GNUNET_MQ_send (GNUNET_CADET_get_mq (alt->cadet_channel),
1028 GNUNET_MQ_env_copy (rc->env));
1035 * Function called whenever a channel has excess capacity.
1037 * @param cls the `struct CadetExit`
1038 * @param channel connection to the other end
1039 * @param window_size how much capacity do we have
1042 channel_idle_notify_cb (void *cls,
1043 const struct GNUNET_CADET_Channel *channel,
1046 struct CadetExit *pos = cls;
1048 pos->idle = window_size;
1053 * We are short on cadet exits, try to open another one.
1058 struct CadetExit *pos;
1059 uint32_t candidate_count;
1060 uint32_t candidate_selected;
1061 struct GNUNET_HashCode port;
1063 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER,
1064 strlen (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER),
1066 candidate_count = 0;
1067 for (pos = exit_head; NULL != pos; pos = pos->next)
1068 if (NULL == pos->cadet_channel)
1070 if (0 == candidate_count)
1072 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1073 "No DNS exits available yet.\n");
1076 candidate_selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1078 candidate_count = 0;
1079 for (pos = exit_head; NULL != pos; pos = pos->next)
1080 if (NULL == pos->cadet_channel)
1083 if (candidate_selected < candidate_count)
1085 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
1086 GNUNET_MQ_hd_var_size (dns_response,
1087 GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET,
1088 struct DnsResponseMessage,
1090 GNUNET_MQ_handler_end ()
1094 /* move to the head of the DLL */
1096 = GNUNET_CADET_channel_create (cadet_handle,
1100 &channel_idle_notify_cb,
1101 &cadet_channel_end_cb,
1103 if (NULL == pos->cadet_channel)
1108 GNUNET_CONTAINER_DLL_remove (exit_head,
1111 GNUNET_CONTAINER_DLL_insert (exit_head,
1114 dns_exit_available++;
1118 GNUNET_assert (NULL == exit_head);
1123 * Function called whenever we find an advertisement for a
1124 * DNS exit in the DHT. If we don't have a cadet channel,
1125 * we should build one; otherwise, we should save the
1126 * advertisement for later use.
1128 * @param cls closure
1129 * @param exp when will this value expire
1130 * @param key key of the result
1131 * @param get_path peers on reply path (or NULL if not recorded)
1132 * [0] = datastore's first neighbor, [length - 1] = local peer
1133 * @param get_path_length number of entries in @a get_path
1134 * @param put_path peers on the PUT path (or NULL if not recorded)
1135 * [0] = origin, [length - 1] = datastore
1136 * @param put_path_length number of entries in @a put_path
1137 * @param type type of the result
1138 * @param size number of bytes in @a data
1139 * @param data pointer to the result data
1142 handle_dht_result (void *cls,
1143 struct GNUNET_TIME_Absolute exp,
1144 const struct GNUNET_HashCode *key,
1145 const struct GNUNET_PeerIdentity *get_path,
1146 unsigned int get_path_length,
1147 const struct GNUNET_PeerIdentity *put_path,
1148 unsigned int put_path_length,
1149 enum GNUNET_BLOCK_Type type,
1150 size_t size, const void *data)
1152 const struct GNUNET_DNS_Advertisement *ad;
1153 struct CadetExit *exit;
1155 if (sizeof(struct GNUNET_DNS_Advertisement) != size)
1161 for (exit = exit_head; NULL != exit; exit = exit->next)
1162 if (0 == GNUNET_memcmp (&ad->peer,
1167 exit = GNUNET_new (struct CadetExit);
1168 exit->peer = ad->peer;
1169 /* channel is closed, so insert at the end */
1170 GNUNET_CONTAINER_DLL_insert_tail (exit_head,
1174 exit->expiration = GNUNET_TIME_absolute_max (exit->expiration,
1175 GNUNET_TIME_absolute_ntoh (
1176 ad->expiration_time));
1177 if (dns_exit_available < MAX_OPEN_TUNNELS)
1183 * @brief Main function that will be run by the scheduler.
1185 * @param cls closure
1186 * @param args remaining command-line arguments
1187 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1188 * @param cfg_ configuration
1191 run (void *cls, char *const *args GNUNET_UNUSED,
1192 const char *cfgfile GNUNET_UNUSED,
1193 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1195 struct GNUNET_HashCode dns_key;
1198 stats = GNUNET_STATISTICS_create ("pt",
1200 ipv4_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1203 ipv6_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1206 dns_channel = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1209 if (! (ipv4_pt || ipv6_pt || dns_channel))
1211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1212 _ ("No useful service enabled. Exiting.\n"));
1213 GNUNET_SCHEDULER_shutdown ();
1216 GNUNET_SCHEDULER_add_shutdown (&cleanup, cls);
1217 if (ipv4_pt || ipv6_pt)
1220 = GNUNET_DNS_connect (cfg,
1221 GNUNET_DNS_FLAG_POST_RESOLUTION,
1222 &dns_post_request_handler,
1224 if (NULL == dns_post_handle)
1226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1227 _ ("Failed to connect to %s service. Exiting.\n"),
1229 GNUNET_SCHEDULER_shutdown ();
1232 vpn_handle = GNUNET_VPN_connect (cfg);
1233 if (NULL == vpn_handle)
1235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1236 _ ("Failed to connect to %s service. Exiting.\n"),
1238 GNUNET_SCHEDULER_shutdown ();
1245 = GNUNET_DNS_connect (cfg,
1246 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1247 &dns_pre_request_handler,
1249 if (NULL == dns_pre_handle)
1251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1252 _ ("Failed to connect to %s service. Exiting.\n"),
1254 GNUNET_SCHEDULER_shutdown ();
1257 cadet_handle = GNUNET_CADET_connect (cfg);
1258 if (NULL == cadet_handle)
1260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1261 _ ("Failed to connect to %s service. Exiting.\n"),
1263 GNUNET_SCHEDULER_shutdown ();
1266 dht = GNUNET_DHT_connect (cfg, 1);
1269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1270 _ ("Failed to connect to %s service. Exiting.\n"),
1272 GNUNET_SCHEDULER_shutdown ();
1275 GNUNET_CRYPTO_hash ("dns",
1278 dht_get = GNUNET_DHT_get_start (dht,
1279 GNUNET_BLOCK_TYPE_DNS,
1282 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1293 * @param argc number of arguments from the command line
1294 * @param argv command line arguments
1295 * @return 0 ok, 1 on error
1301 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1302 GNUNET_GETOPT_OPTION_END
1306 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
1312 GNUNET_PROGRAM_run (argc,
1316 "Daemon to run to perform IP protocol translation to GNUnet"),
1322 GNUNET_free ((void*) argv);
1327 /* end of gnunet-daemon-pt.c */