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/>.
19 * @file pt/gnunet-daemon-pt.c
20 * @brief tool to manipulate DNS and VPN services to perform protocol translation (IPvX over GNUnet)
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
25 #include "gnunet_dns_service.h"
26 #include "gnunet_dnsparser_lib.h"
27 #include "gnunet_cadet_service.h"
28 #include "gnunet_tun_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_vpn_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_applications.h"
33 #include "block_dns.h"
37 * After how long do we time out if we could not get an IP from VPN or CADET?
39 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
42 * How many bytes of payload do we allow at most for a DNS reply?
43 * Given that this is pretty much limited to loopback, we can be
44 * pretty high (Linux loopback defaults to 16k, most local UDP packets
45 * should survive up to 9k (NFS), so 8k should be pretty safe in
48 #define MAX_DNS_SIZE (8 * 1024)
51 * How many channels do we open at most at the same time?
53 #define MAX_OPEN_TUNNELS 4
57 * Which group of DNS records are we currently processing?
67 * DNS authority records
69 AUTHORITY_RECORDS = 1,
72 * DNS additional records
74 ADDITIONAL_RECORDS = 2,
77 * We're done processing.
84 * Information tracked per DNS reply that we are processing.
89 * Handle to submit the final result.
91 struct GNUNET_DNS_RequestHandle *rh;
94 * DNS packet that is being modified.
96 struct GNUNET_DNSPARSER_Packet *dns;
99 * Active redirection request with the VPN.
101 struct GNUNET_VPN_RedirectionRequest *rr;
104 * Record for which we have an active redirection request.
106 struct GNUNET_DNSPARSER_Record *rec;
109 * Offset in the current record group that is being modified.
114 * Group that is being modified
116 enum RequestGroup group;
122 * Handle to a peer that advertised that it is willing to serve
123 * 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;
185 * State we keep for a request that is going out via CADET.
187 struct RequestContext
190 * We keep these in a DLL.
192 struct RequestContext *next;
195 * We keep these in a DLL.
197 struct RequestContext *prev;
200 * Exit that was chosen for this request.
202 struct CadetExit *exit;
205 * Handle for interaction with DNS service.
207 struct GNUNET_DNS_RequestHandle *rh;
210 * Envelope with the request we are transmitting.
212 struct GNUNET_MQ_Envelope *env;
215 * Task used to abort this operation with timeout.
217 struct GNUNET_SCHEDULER_Task *timeout_task;
220 * Length of the request message that follows this struct.
225 * ID of the original DNS request (used to match the reply).
233 * Head of DLL of cadet exits. Cadet exits with an open channel are
234 * always at the beginning (so we do not have to traverse the entire
235 * list to find them).
237 static struct CadetExit *exit_head;
240 * Tail of DLL of cadet exits.
242 static struct CadetExit *exit_tail;
245 * The handle to the configuration used throughout the process
247 static const struct GNUNET_CONFIGURATION_Handle *cfg;
250 * The handle to the VPN
252 static struct GNUNET_VPN_Handle *vpn_handle;
255 * The handle to the CADET service
257 static struct GNUNET_CADET_Handle *cadet_handle;
262 static struct GNUNET_STATISTICS_Handle *stats;
265 * The handle to DNS post-resolution modifications.
267 static struct GNUNET_DNS_Handle *dns_post_handle;
270 * The handle to DNS pre-resolution modifications.
272 static struct GNUNET_DNS_Handle *dns_pre_handle;
275 * Handle to access the DHT.
277 static struct GNUNET_DHT_Handle *dht;
280 * Our DHT GET operation to find DNS exits.
282 static struct GNUNET_DHT_GetHandle *dht_get;
285 * Are we doing IPv4-pt?
290 * Are we doing IPv6-pt?
295 * Are we channeling DNS queries?
297 static int dns_channel;
300 * Number of DNS exit peers we currently have in the cadet channel.
301 * Used to see if using the cadet channel makes any sense right now,
302 * as well as to decide if we should open new channels.
304 static unsigned int dns_exit_available;
308 * We are short on cadet exits, try to open another one.
311 try_open_exit (void);
315 * Compute the weight of the given exit. The higher the weight,
316 * the more likely it will be that the channel will be chosen.
317 * A weigt of zero means that we should close the channel as it
318 * is so bad, that we should not use it.
320 * @param exit exit to calculate the weight for
321 * @return weight of the channel
324 get_channel_weight (struct CadetExit *exit)
327 uint32_t drop_percent;
328 uint32_t good_percent;
330 GNUNET_assert (exit->num_transmitted >= exit->num_answered);
331 dropped = exit->num_transmitted - exit->num_answered;
332 if (exit->num_transmitted > 0)
333 drop_percent = (uint32_t) ((100LL * dropped) / exit->num_transmitted);
335 drop_percent = 50; /* no data */
336 if ( (exit->num_transmitted > 20) &&
337 (drop_percent > 25) )
338 return 0; /* statistically significant, and > 25% loss, die */
339 good_percent = 100 - drop_percent;
340 GNUNET_assert (0 != good_percent);
341 if ( UINT32_MAX / good_percent / good_percent < exit->num_transmitted)
342 return UINT32_MAX; /* formula below would overflow */
343 return 1 + good_percent * good_percent * exit->num_transmitted;
348 * Choose a cadet exit for a DNS request. We try to use a channel
349 * that is reliable and currently available. All existing
350 * channels are given a base weight of 1, plus a score relating
351 * to the total number of queries answered in relation to the
352 * total number of queries we sent to that channel. That
353 * score is doubled if the channel is currently idle.
355 * @return NULL if no exit is known, otherwise the
356 * exit that we should use to queue a message with
358 static struct CadetExit *
361 struct CadetExit *pos;
362 uint64_t total_transmitted;
363 uint64_t selected_offset;
364 uint32_t channel_weight;
366 total_transmitted = 0;
367 for (pos = exit_head; NULL != pos; pos = pos->next)
369 if (NULL == pos->cadet_channel)
371 channel_weight = get_channel_weight (pos);
372 total_transmitted += channel_weight;
373 /* double weight for idle channels */
375 total_transmitted += channel_weight;
377 if (0 == total_transmitted)
379 /* no channels available, or only a very bad one... */
382 selected_offset = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
384 total_transmitted = 0;
385 for (pos = exit_head; NULL != pos; pos = pos->next)
387 if (NULL == pos->cadet_channel)
389 channel_weight = get_channel_weight (pos);
390 total_transmitted += channel_weight;
391 /* double weight for idle channels */
393 total_transmitted += channel_weight;
394 if (total_transmitted > selected_offset)
403 * We're done modifying all records in the response. Submit the reply
404 * and free the resources of the rc.
406 * @param rc context to process
409 finish_request (struct ReplyContext *rc)
415 GNUNET_DNSPARSER_pack (rc->dns,
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
421 _("Failed to pack DNS request. Dropping.\n"));
422 GNUNET_DNS_request_drop (rc->rh);
426 GNUNET_STATISTICS_update (stats,
427 gettext_noop ("# DNS requests mapped to VPN"),
429 GNUNET_DNS_request_answer (rc->rh,
434 GNUNET_DNSPARSER_free_packet (rc->dns);
440 * Process the next record of the given request context.
441 * When done, submit the reply and free the resources of
444 * @param rc context to process
447 submit_request (struct ReplyContext *rc);
451 * Callback invoked from the VPN service once a redirection is
452 * available. Provides the IP address that can now be used to
453 * reach the requested destination. We substitute the active
454 * record and then continue with 'submit_request' to look at
457 * @param cls our `struct ReplyContext`
458 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
459 * will match 'result_af' from the request
460 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
461 * that the VPN allocated for the redirection;
462 * traffic to this IP will now be redirected to the
463 * specified target peer; NULL on error
466 vpn_allocation_callback (void *cls,
470 struct ReplyContext *rc = cls;
475 GNUNET_DNS_request_drop (rc->rh);
476 GNUNET_DNSPARSER_free_packet (rc->dns);
480 GNUNET_STATISTICS_update (stats,
481 gettext_noop ("# DNS records modified"),
484 switch (rc->rec->type)
486 case GNUNET_DNSPARSER_TYPE_A:
487 GNUNET_assert (AF_INET == af);
488 GNUNET_memcpy (rc->rec->data.raw.data,
490 sizeof (struct in_addr));
492 case GNUNET_DNSPARSER_TYPE_AAAA:
493 GNUNET_assert (AF_INET6 == af);
494 GNUNET_memcpy (rc->rec->data.raw.data,
496 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));
528 case GNUNET_DNSPARSER_TYPE_AAAA:
530 GNUNET_assert (rec->data.raw.data_len == sizeof (struct in6_addr));
537 rc->rr = GNUNET_VPN_redirect_to_ip (vpn_handle,
541 GNUNET_TIME_relative_to_absolute (TIMEOUT),
542 &vpn_allocation_callback,
548 * Process the next record of the given request context.
549 * When done, submit the reply and free the resources of
552 * @param rc context to process
555 submit_request (struct ReplyContext *rc)
557 struct GNUNET_DNSPARSER_Record *ra;
566 ra = rc->dns->answers;
567 ra_len = rc->dns->num_answers;
569 case AUTHORITY_RECORDS:
570 ra = rc->dns->authority_records;
571 ra_len = rc->dns->num_authority_records;
573 case ADDITIONAL_RECORDS:
574 ra = rc->dns->additional_records;
575 ra_len = rc->dns->num_additional_records;
583 for (i=rc->offset;i<ra_len;i++)
587 case GNUNET_DNSPARSER_TYPE_A:
596 case GNUNET_DNSPARSER_TYPE_AAAA:
613 * Test if any of the given records need protocol-translation work.
615 * @param ra array of records
616 * @param ra_len number of entries in @a ra
617 * @return #GNUNET_YES if any of the given records require protocol-translation
620 work_test (const struct GNUNET_DNSPARSER_Record *ra,
625 for (i=0;i<ra_len;i++)
629 case GNUNET_DNSPARSER_TYPE_A:
633 case GNUNET_DNSPARSER_TYPE_AAAA:
644 * This function is called AFTER we got an IP address for a
645 * DNS request. Now, the PT daemon has the chance to substitute
646 * the IP address with one from the VPN range to channel requests
647 * destined for this IP address via VPN and CADET.
650 * @param rh request handle to user for reply
651 * @param request_length number of bytes in request
652 * @param request udp payload of the DNS request
655 dns_post_request_handler (void *cls,
656 struct GNUNET_DNS_RequestHandle *rh,
657 size_t request_length,
660 struct GNUNET_DNSPARSER_Packet *dns;
661 struct ReplyContext *rc;
664 GNUNET_STATISTICS_update (stats,
665 gettext_noop ("# DNS replies intercepted"),
667 dns = GNUNET_DNSPARSER_parse (request,
671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
672 _("Failed to parse DNS request. Dropping.\n"));
673 GNUNET_DNS_request_drop (rh);
677 work |= work_test (dns->answers,
679 work |= work_test (dns->authority_records,
680 dns->num_authority_records);
681 work |= work_test (dns->additional_records,
682 dns->num_additional_records);
685 GNUNET_DNS_request_forward (rh);
686 GNUNET_DNSPARSER_free_packet (dns);
689 rc = GNUNET_new (struct ReplyContext);
699 * Task run if the time to answer a DNS request via CADET is over.
701 * @param cls the `struct RequestContext` to abort
704 timeout_request (void *cls)
706 struct RequestContext *rc = cls;
707 struct CadetExit *exit = rc->exit;
709 GNUNET_STATISTICS_update (stats,
710 gettext_noop ("# DNS requests dropped (timeout)"),
713 GNUNET_DNS_request_drop (rc->rh);
715 if ( (0 == get_channel_weight (exit)) &&
716 (NULL == exit->receive_queue_head) )
718 /* this straw broke the camel's back: this channel now has
719 such a low score that it will not be used; close it! */
720 GNUNET_CADET_channel_destroy (exit->cadet_channel);
721 exit->cadet_channel = NULL;
722 GNUNET_CONTAINER_DLL_remove (exit_head,
725 GNUNET_CONTAINER_DLL_insert_tail (exit_head,
728 /* go back to semi-innocent: mark as not great, but
729 avoid a prohibitively negative score (see
730 #get_channel_weight(), which checks for a certain
731 minimum number of transmissions before making
733 exit->num_transmitted = 5;
734 exit->num_answered = 0;
735 dns_exit_available--;
736 /* now try to open an alternative exit */
743 * This function is called *before* the DNS request has been
744 * given to a "local" DNS resolver. Channeling for DNS requests
745 * was enabled, so we now need to send the request via some CADET
746 * channel to a DNS EXIT for resolution.
749 * @param rh request handle to user for reply
750 * @param request_length number of bytes in request
751 * @param request udp payload of the DNS request
754 dns_pre_request_handler (void *cls,
755 struct GNUNET_DNS_RequestHandle *rh,
756 size_t request_length,
759 struct RequestContext *rc;
760 struct GNUNET_MQ_Envelope *env;
761 struct GNUNET_MessageHeader *hdr;
762 struct GNUNET_TUN_DnsHeader dns;
763 struct CadetExit *exit;
765 GNUNET_STATISTICS_update (stats,
766 gettext_noop ("# DNS requests intercepted"),
768 if (0 == dns_exit_available)
770 GNUNET_STATISTICS_update (stats,
771 gettext_noop ("# DNS requests dropped (DNS cadet channel down)"),
773 GNUNET_DNS_request_drop (rh);
776 if (request_length < sizeof (dns))
778 GNUNET_STATISTICS_update (stats,
779 gettext_noop ("# DNS requests dropped (malformed)"),
781 GNUNET_DNS_request_drop (rh);
784 exit = choose_exit ();
785 GNUNET_assert (NULL != exit);
786 GNUNET_assert (NULL != exit->cadet_channel);
788 env = GNUNET_MQ_msg_extra (hdr,
790 GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET);
791 GNUNET_memcpy (&hdr[1],
794 rc = GNUNET_new (struct RequestContext);
797 rc->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
805 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
806 exit->receive_queue_tail,
810 exit->num_transmitted++;
811 GNUNET_MQ_send (GNUNET_CADET_get_mq (exit->cadet_channel),
812 GNUNET_MQ_env_copy (env));
816 GNUNET_NETWORK_STRUCT_BEGIN
819 * Message with a DNS response.
821 struct DnsResponseMessage
824 * GNUnet header, of type #GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET
826 struct GNUNET_MessageHeader header;
831 struct GNUNET_TUN_DnsHeader dns;
833 /* Followed by more DNS payload */
836 GNUNET_NETWORK_STRUCT_END
839 * Process a request via cadet to perform a DNS query.
841 * @param cls the `struct CadetExit` which got the message
842 * @param msg the actual message
843 * @return #GNUNET_OK to keep the connection open,
844 * #GNUNET_SYSERR to close it (signal serious error)
847 check_dns_response (void *cls,
848 const struct DnsResponseMessage *msg)
850 return GNUNET_OK; /* all OK */
855 * Process a request via cadet to perform a DNS query.
857 * @param cls the `struct CadetExit` which got the message
858 * @param msg the actual message
861 handle_dns_response (void *cls,
862 const struct DnsResponseMessage *msg)
864 struct CadetExit *exit = cls;
866 struct RequestContext *rc;
868 mlen = ntohs (msg->header.size) - sizeof (*msg);
869 for (rc = exit->receive_queue_head; NULL != rc; rc = rc->next)
871 if (msg->dns.id == rc->dns_id)
873 GNUNET_STATISTICS_update (stats,
874 gettext_noop ("# DNS replies received"),
877 GNUNET_DNS_request_answer (rc->rh,
878 mlen + sizeof (struct GNUNET_TUN_DnsHeader),
879 (const void*) &msg->dns);
880 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
881 exit->receive_queue_tail,
883 GNUNET_SCHEDULER_cancel (rc->timeout_task);
884 GNUNET_MQ_discard (rc->env);
886 exit->num_answered++;
890 GNUNET_STATISTICS_update (stats,
891 gettext_noop ("# DNS replies dropped (too late?)"),
897 * Abort all pending DNS requests with the given cadet exit.
899 * @param exit cadet exit to abort requests for
902 abort_all_requests (struct CadetExit *exit)
904 struct RequestContext *rc;
906 while (NULL != (rc = exit->receive_queue_head))
908 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
909 exit->receive_queue_tail,
911 GNUNET_DNS_request_drop (rc->rh);
912 GNUNET_SCHEDULER_cancel (rc->timeout_task);
913 GNUNET_MQ_discard (rc->env);
920 * Function scheduled as very last function, cleans up after us
922 * @param cls closure, NULL
927 struct CadetExit *exit;
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "Protocol translation daemon is shutting down now\n");
931 if (NULL != vpn_handle)
933 GNUNET_VPN_disconnect (vpn_handle);
936 while (NULL != (exit = exit_head))
938 GNUNET_CONTAINER_DLL_remove (exit_head,
941 if (NULL != exit->cadet_channel)
943 GNUNET_CADET_channel_destroy (exit->cadet_channel);
944 exit->cadet_channel = NULL;
946 abort_all_requests (exit);
949 if (NULL != cadet_handle)
951 GNUNET_CADET_disconnect (cadet_handle);
954 if (NULL != dns_post_handle)
956 GNUNET_DNS_disconnect (dns_post_handle);
957 dns_post_handle = NULL;
959 if (NULL != dns_pre_handle)
961 GNUNET_DNS_disconnect (dns_pre_handle);
962 dns_pre_handle = NULL;
966 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
971 GNUNET_DHT_get_stop (dht_get);
976 GNUNET_DHT_disconnect (dht);
983 * Function called whenever a channel is destroyed. Should clean up
984 * the associated state and attempt to build a new one.
986 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
988 * @param cls closure (the `struct CadetExit` set from #GNUNET_CADET_connect)
989 * @param channel connection to the other end (henceforth invalid)
990 * @param channel_ctx place where local state associated
991 * with the channel is stored
994 cadet_channel_end_cb (void *cls,
995 const struct GNUNET_CADET_Channel *channel)
997 struct CadetExit *exit = cls;
998 struct CadetExit *alt;
999 struct RequestContext *rc;
1001 exit->cadet_channel = NULL;
1002 dns_exit_available--;
1003 /* open alternative channels */
1004 /* our channel is now closed, move our requests to an alternative
1006 alt = choose_exit ();
1007 while (NULL != (rc = exit->receive_queue_head))
1009 GNUNET_CONTAINER_DLL_remove (exit->receive_queue_head,
1010 exit->receive_queue_tail,
1013 GNUNET_CONTAINER_DLL_insert (alt->receive_queue_head,
1014 alt->receive_queue_tail,
1016 GNUNET_MQ_send (GNUNET_CADET_get_mq (alt->cadet_channel),
1017 GNUNET_MQ_env_copy (rc->env));
1024 * Function called whenever a channel has excess capacity.
1026 * @param cls the `struct CadetExit`
1027 * @param channel connection to the other end
1028 * @param window_size how much capacity do we have
1031 channel_idle_notify_cb (void *cls,
1032 const struct GNUNET_CADET_Channel *channel,
1035 struct CadetExit *pos = cls;
1037 pos->idle = window_size;
1042 * We are short on cadet exits, try to open another one.
1047 struct CadetExit *pos;
1048 uint32_t candidate_count;
1049 uint32_t candidate_selected;
1050 struct GNUNET_HashCode port;
1052 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER,
1053 strlen (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER),
1055 candidate_count = 0;
1056 for (pos = exit_head; NULL != pos; pos = pos->next)
1057 if (NULL == pos->cadet_channel)
1059 if (0 == candidate_count)
1061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1062 "No DNS exits available yet.\n");
1065 candidate_selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1067 candidate_count = 0;
1068 for (pos = exit_head; NULL != pos; pos = pos->next)
1069 if (NULL == pos->cadet_channel)
1072 if (candidate_selected < candidate_count)
1074 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
1075 GNUNET_MQ_hd_var_size (dns_response,
1076 GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET,
1077 struct DnsResponseMessage,
1079 GNUNET_MQ_handler_end ()
1083 /* move to the head of the DLL */
1085 = GNUNET_CADET_channel_create (cadet_handle,
1089 GNUNET_CADET_OPTION_DEFAULT,
1090 &channel_idle_notify_cb,
1091 &cadet_channel_end_cb,
1093 if (NULL == pos->cadet_channel)
1098 GNUNET_CONTAINER_DLL_remove (exit_head,
1101 GNUNET_CONTAINER_DLL_insert (exit_head,
1104 dns_exit_available++;
1108 GNUNET_assert (NULL == exit_head);
1113 * Function called whenever we find an advertisement for a
1114 * DNS exit in the DHT. If we don't have a cadet channel,
1115 * we should build one; otherwise, we should save the
1116 * advertisement for later use.
1118 * @param cls closure
1119 * @param exp when will this value expire
1120 * @param key key of the result
1121 * @param get_path peers on reply path (or NULL if not recorded)
1122 * [0] = datastore's first neighbor, [length - 1] = local peer
1123 * @param get_path_length number of entries in @a get_path
1124 * @param put_path peers on the PUT path (or NULL if not recorded)
1125 * [0] = origin, [length - 1] = datastore
1126 * @param put_path_length number of entries in @a put_path
1127 * @param type type of the result
1128 * @param size number of bytes in @a data
1129 * @param data pointer to the result data
1132 handle_dht_result (void *cls,
1133 struct GNUNET_TIME_Absolute exp,
1134 const struct GNUNET_HashCode *key,
1135 const struct GNUNET_PeerIdentity *get_path,
1136 unsigned int get_path_length,
1137 const struct GNUNET_PeerIdentity *put_path,
1138 unsigned int put_path_length,
1139 enum GNUNET_BLOCK_Type type,
1140 size_t size, const void *data)
1142 const struct GNUNET_DNS_Advertisement *ad;
1143 struct CadetExit *exit;
1145 if (sizeof (struct GNUNET_DNS_Advertisement) != size)
1151 for (exit = exit_head; NULL != exit; exit = exit->next)
1152 if (0 == memcmp (&ad->peer,
1154 sizeof (struct GNUNET_PeerIdentity)))
1158 exit = GNUNET_new (struct CadetExit);
1159 exit->peer = ad->peer;
1160 /* channel is closed, so insert at the end */
1161 GNUNET_CONTAINER_DLL_insert_tail (exit_head,
1165 exit->expiration = GNUNET_TIME_absolute_max (exit->expiration,
1166 GNUNET_TIME_absolute_ntoh (ad->expiration_time));
1167 if (dns_exit_available < MAX_OPEN_TUNNELS)
1173 * @brief Main function that will be run by the scheduler.
1175 * @param cls closure
1176 * @param args remaining command-line arguments
1177 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1178 * @param cfg_ configuration
1181 run (void *cls, char *const *args GNUNET_UNUSED,
1182 const char *cfgfile GNUNET_UNUSED,
1183 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1185 struct GNUNET_HashCode dns_key;
1188 stats = GNUNET_STATISTICS_create ("pt",
1190 ipv4_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1193 ipv6_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1196 dns_channel = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1199 if (! (ipv4_pt || ipv6_pt || dns_channel))
1201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1202 _("No useful service enabled. Exiting.\n"));
1203 GNUNET_SCHEDULER_shutdown ();
1206 GNUNET_SCHEDULER_add_shutdown (&cleanup, cls);
1207 if (ipv4_pt || ipv6_pt)
1210 = GNUNET_DNS_connect (cfg,
1211 GNUNET_DNS_FLAG_POST_RESOLUTION,
1212 &dns_post_request_handler,
1214 if (NULL == dns_post_handle)
1216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1217 _("Failed to connect to %s service. Exiting.\n"),
1219 GNUNET_SCHEDULER_shutdown ();
1222 vpn_handle = GNUNET_VPN_connect (cfg);
1223 if (NULL == vpn_handle)
1225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1226 _("Failed to connect to %s service. Exiting.\n"),
1228 GNUNET_SCHEDULER_shutdown ();
1235 = GNUNET_DNS_connect (cfg,
1236 GNUNET_DNS_FLAG_PRE_RESOLUTION,
1237 &dns_pre_request_handler,
1239 if (NULL == dns_pre_handle)
1241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1242 _("Failed to connect to %s service. Exiting.\n"),
1244 GNUNET_SCHEDULER_shutdown ();
1247 cadet_handle = GNUNET_CADET_connect (cfg);
1248 if (NULL == cadet_handle)
1250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1251 _("Failed to connect to %s service. Exiting.\n"),
1253 GNUNET_SCHEDULER_shutdown ();
1256 dht = GNUNET_DHT_connect (cfg, 1);
1259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1260 _("Failed to connect to %s service. Exiting.\n"),
1262 GNUNET_SCHEDULER_shutdown ();
1265 GNUNET_CRYPTO_hash ("dns",
1268 dht_get = GNUNET_DHT_get_start (dht,
1269 GNUNET_BLOCK_TYPE_DNS,
1272 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1283 * @param argc number of arguments from the command line
1284 * @param argv command line arguments
1285 * @return 0 ok, 1 on error
1291 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1292 GNUNET_GETOPT_OPTION_END
1296 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
1302 GNUNET_PROGRAM_run (argc,
1305 gettext_noop ("Daemon to run to perform IP protocol translation to GNUnet"),
1311 GNUNET_free ((void*) argv);
1316 /* end of gnunet-daemon-pt.c */