2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport_validation.c
23 * @brief address validation subsystem
24 * @author Christian Grothoff
27 #include "gnunet-service-transport_validation.h"
28 #include "gnunet-service-transport_plugins.h"
29 #include "gnunet-service-transport_hello.h"
30 #include "gnunet-service-transport_blacklist.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_ats_service.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_signatures.h"
40 * How long is a PONG signature valid? We'll recycle a signature until
41 * 1/4 of this time is remaining. PONGs should expire so that if our
42 * external addresses change an adversary cannot replay them indefinitely.
43 * OTOH, we don't want to spend too much time generating PONG signatures,
44 * so they must have some lifetime to reduce our CPU usage.
46 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
49 * After how long do we expire an address in a HELLO that we just
50 * validated? This value is also used for our own addresses when we
53 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
56 * How often do we allow PINGing an address that we have not yet
57 * validated? This also determines how long we track an address that
58 * we cannot validate (because after this time we can destroy the
61 #define UNVALIDATED_PING_KEEPALIVE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
64 * How often do we PING an address that we have successfully validated
65 * in the past but are not actively using? Should be (significantly)
66 * smaller than HELLO_ADDRESS_EXPIRATION.
68 #define VALIDATED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
71 * How often do we PING an address that we are currently using?
73 #define CONNECTED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
76 * How much delay is acceptable for sending the PING or PONG?
78 #define ACCEPTABLE_PING_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
81 * Size of the validation map hashmap.
83 #define VALIDATION_MAP_SIZE 256
86 * Priority to use for PINGs
88 #define PING_PRIORITY 2
91 * Priority to use for PONGs
93 #define PONG_PRIORITY 4
96 GNUNET_NETWORK_STRUCT_BEGIN
99 * Message used to ask a peer to validate receipt (to check an address
100 * from a HELLO). Followed by the address we are trying to validate,
101 * or an empty address if we are just sending a PING to confirm that a
102 * connection which the receiver (of the PING) initiated is still valid.
104 struct TransportPingMessage
108 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
110 struct GNUNET_MessageHeader header;
113 * Challenge code (to ensure fresh reply).
115 uint32_t challenge GNUNET_PACKED;
118 * Who is the intended recipient?
120 struct GNUNET_PeerIdentity target;
126 * Message used to validate a HELLO. The challenge is included in the
127 * confirmation to make matching of replies to requests possible. The
128 * signature signs our public key, an expiration time and our address.<p>
130 * This message is followed by our transport address that the PING tried
131 * to confirm (if we liked it). The address can be empty (zero bytes)
132 * if the PING had not address either (and we received the request via
133 * a connection that we initiated).
135 struct TransportPongMessage
139 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
141 struct GNUNET_MessageHeader header;
144 * Challenge code from PING (showing freshness). Not part of what
145 * is signed so that we can re-use signatures.
147 uint32_t challenge GNUNET_PACKED;
152 struct GNUNET_CRYPTO_EddsaSignature signature;
155 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
156 * plausible address for the signing peer.
158 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
161 * When does this signature expire?
163 struct GNUNET_TIME_AbsoluteNBO expiration;
166 * Size of address appended to this message (part of what is
167 * being signed, hence not redundant).
169 uint32_t addrlen GNUNET_PACKED;
172 GNUNET_NETWORK_STRUCT_END
175 * Information about an address under validation
177 struct ValidationEntry
183 struct GNUNET_HELLO_Address *address;
186 * Handle to the blacklist check (if we're currently in it).
188 struct GST_BlacklistCheck *bc;
191 * Public key of the peer.
193 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
196 * The identity of the peer. FIXME: duplicated (also in 'address')
198 struct GNUNET_PeerIdentity pid;
201 * Cached PONG signature
203 struct GNUNET_CRYPTO_EddsaSignature pong_sig_cache;
206 * ID of task that will clean up this entry if nothing happens.
208 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
211 * ID of task that will trigger address revalidation.
213 GNUNET_SCHEDULER_TaskIdentifier revalidation_task;
216 * At what time did we send the latest validation request (PING)?
218 struct GNUNET_TIME_Absolute send_time;
221 * Until when is this address valid?
222 * ZERO if it is not currently considered valid.
224 struct GNUNET_TIME_Absolute valid_until;
227 * Until when is the cached PONG signature valid?
228 * ZERO if it is not currently considered valid.
230 struct GNUNET_TIME_Absolute pong_sig_valid_until;
233 * How long until we can try to validate this address again?
234 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
235 * ZERO if the address is considered valid (no validation needed)
236 * otherwise a time in the future if we're currently denying re-validation
238 struct GNUNET_TIME_Absolute revalidation_block;
241 * Last observed latency for this address (round-trip), delay between
242 * last PING sent and PONG received; FOREVER if we never got a PONG.
244 struct GNUNET_TIME_Relative latency;
247 * Challenge number we used.
252 * When passing the address in 'add_valid_peer_address', did we
253 * copy the address to the HELLO yet?
258 * Are we currently using this address for a connection?
263 * Are we expecting a PONG message for this validation entry?
267 enum GNUNET_ATS_Network_Type network;
272 * Context of currently active requests to peerinfo
273 * for validation of HELLOs.
275 struct CheckHelloValidatedContext
279 * This is a doubly-linked list.
281 struct CheckHelloValidatedContext *next;
284 * This is a doubly-linked list.
286 struct CheckHelloValidatedContext *prev;
289 * Hello that we are validating.
291 const struct GNUNET_HELLO_Message *hello;
297 * Head of linked list of HELLOs awaiting validation.
299 static struct CheckHelloValidatedContext *chvc_head;
302 * Tail of linked list of HELLOs awaiting validation
304 static struct CheckHelloValidatedContext *chvc_tail;
307 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
308 * of the given peer that we are currently validating, have validated
309 * or are blocked from re-validation for a while).
311 static struct GNUNET_CONTAINER_MultiPeerMap *validation_map;
314 * Context for peerinfo iteration.
316 static struct GNUNET_PEERINFO_NotifyContext *pnc;
320 * Minimum delay between to validations
322 static struct GNUNET_TIME_Relative validation_delay;
325 * Number of validations running
327 static unsigned int validations_running;
330 * Validition fast start threshold
332 static unsigned int validations_fast_start_threshold;
335 * When is next validation allowed
337 static struct GNUNET_TIME_Absolute validation_next;
340 * Context for the validation entry match function.
342 struct ValidationEntryMatchContext
345 * Where to store the result?
347 struct ValidationEntry *ve;
350 * Address we're interested in.
352 const struct GNUNET_HELLO_Address *address;
358 * Iterate over validation entries until a matching one is found.
360 * @param cls the 'struct ValidationEntryMatchContext'
361 * @param key peer identity (unused)
362 * @param value a 'struct ValidationEntry' to match
363 * @return GNUNET_YES if the entry does not match,
364 * GNUNET_NO if the entry does match
367 validation_entry_match (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
369 struct ValidationEntryMatchContext *vemc = cls;
370 struct ValidationEntry *ve = value;
372 if (0 == GNUNET_HELLO_address_cmp (ve->address, vemc->address))
382 * Iterate over validation entries and free them.
384 * @param cls (unused)
385 * @param key peer identity (unused)
386 * @param value a 'struct ValidationEntry' to clean up
387 * @return GNUNET_YES (continue to iterate)
390 cleanup_validation_entry (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
392 struct ValidationEntry *ve = value;
396 GST_blacklist_test_cancel (ve->bc);
399 GNUNET_break (GNUNET_OK ==
400 GNUNET_CONTAINER_multipeermap_remove (validation_map,
402 GNUNET_HELLO_address_free (ve->address);
403 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
405 GNUNET_SCHEDULER_cancel (ve->timeout_task);
406 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
408 if (GNUNET_SCHEDULER_NO_TASK != ve->revalidation_task)
410 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
411 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
413 if ((GNUNET_YES == ve->expecting_pong) &&
414 (validations_running > 0))
416 validations_running --;
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Validation finished, %u validation processes running\n",
419 validations_running);
427 * Address validation cleanup task. Assesses if the record is no
428 * longer valid and then possibly triggers its removal.
430 * @param cls the 'struct ValidationEntry'
431 * @param tc scheduler context (unused)
434 timeout_hello_validation (void *cls,
435 const struct GNUNET_SCHEDULER_TaskContext *tc)
437 struct ValidationEntry *ve = cls;
438 struct GNUNET_TIME_Absolute max;
439 struct GNUNET_TIME_Relative left;
441 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
442 max = GNUNET_TIME_absolute_max (ve->valid_until, ve->revalidation_block);
443 left = GNUNET_TIME_absolute_get_remaining (max);
444 if (left.rel_value_us > 0)
446 /* should wait a bit longer */
448 GNUNET_SCHEDULER_add_delayed (left, &timeout_hello_validation, ve);
451 GNUNET_STATISTICS_update (GST_stats,
452 gettext_noop ("# address records discarded"), 1,
454 cleanup_validation_entry (NULL, &ve->pid, ve);
459 * Function called with the result from blacklisting.
460 * Send a PING to the other peer if a communication is allowed.
462 * @param cls our 'struct ValidationEntry'
463 * @param pid identity of the other peer
464 * @param result #GNUNET_OK if the connection is allowed, #GNUNET_NO if not
467 transmit_ping_if_allowed (void *cls,
468 const struct GNUNET_PeerIdentity *pid,
471 struct ValidationEntry *ve = cls;
472 struct TransportPingMessage ping;
473 struct GNUNET_TRANSPORT_PluginFunctions *papi;
474 struct GNUNET_TIME_Absolute next;
475 const struct GNUNET_MessageHeader *hello;
476 enum GNUNET_ATS_Network_Type network;
483 if (GNUNET_NO == result)
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "Blacklist denies to send PING to `%s' %s %s\n",
488 GST_plugins_a2s (ve->address),
489 ve->address->transport_name);
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Transmitting plain PING to `%s' %s %s\n",
496 GST_plugins_a2s (ve->address),
497 ve->address->transport_name);
499 next = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
501 if (next.abs_value_us > validation_next.abs_value_us)
502 validation_next = next; /* We're going to send a PING so delay next validation */
504 slen = strlen (ve->address->transport_name) + 1;
505 hello = GST_hello_get ();
506 hsize = ntohs (hello->size);
508 sizeof (struct TransportPingMessage) + ve->address->address_length +
512 htons (sizeof (struct TransportPingMessage) +
513 ve->address->address_length + slen);
514 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
515 ping.challenge = htonl (ve->challenge);
518 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
522 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
523 "HELLO", "PING", (unsigned int) tsize);
524 /* message too big (!?), get rid of HELLO */
527 sizeof (struct TransportPingMessage) + ve->address->address_length +
531 char message_buf[tsize];
533 /* build message with structure:
534 * [HELLO][TransportPingMessage][Transport name][Address] */
535 memcpy (message_buf, hello, hsize);
536 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
537 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
538 ve->address->transport_name, slen);
539 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
540 ve->address->address, ve->address->address_length);
541 papi = GST_plugins_find (ve->address->transport_name);
546 GNUNET_assert (papi->send != NULL);
547 GNUNET_assert (papi->get_session != NULL);
548 struct Session * session = papi->get_session(papi->cls, ve->address);
552 ret = papi->send (papi->cls, session,
554 PING_PRIORITY, ACCEPTABLE_PING_DELAY,
556 network = papi->get_network (ve->address, session);
557 if (GNUNET_ATS_NET_UNSPECIFIED == network)
559 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
560 "Could not obtain a valid network for `%s' %s\n",
561 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
564 GST_neighbours_notify_data_sent (pid, ve->address, session, tsize);
568 /* Could not get a valid session */
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not get a valid session for `%s' %s\n",
570 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
577 ve->send_time = GNUNET_TIME_absolute_get ();
578 GNUNET_STATISTICS_update (GST_stats,
580 ("# PING without HELLO messages sent"), 1,
583 ve->network = network;
584 ve->expecting_pong = GNUNET_YES;
585 validations_running ++;
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Validation started, %u validation processes running\n",
588 validations_running);
594 * Do address validation again to keep address valid.
596 * @param cls the 'struct ValidationEntry'
597 * @param tc scheduler context (unused)
600 revalidate_address (void *cls,
601 const struct GNUNET_SCHEDULER_TaskContext *tc)
603 struct ValidationEntry *ve = cls;
604 struct GNUNET_TIME_Relative canonical_delay;
605 struct GNUNET_TIME_Relative delay;
606 struct GNUNET_TIME_Relative blocked_for;
607 struct GST_BlacklistCheck *bc;
610 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
611 delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
612 /* How long until we can possibly permit the next PING? */
615 GNUNET_YES) ? CONNECTED_PING_FREQUENCY
616 : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value_us >
617 0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE);
618 if (delay.rel_value_us > canonical_delay.rel_value_us * 2)
620 /* situation changed, recalculate delay */
621 delay = canonical_delay;
622 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
624 if (delay.rel_value_us > 0)
626 /* should wait a bit longer */
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Waiting for %s longer before validating address %s\n",
629 GNUNET_STRINGS_relative_time_to_string (delay,
631 GST_plugins_a2s (ve->address));
632 ve->revalidation_task =
633 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
636 blocked_for = GNUNET_TIME_absolute_get_remaining(validation_next);
637 if ((validations_running > validations_fast_start_threshold) &&
638 (blocked_for.rel_value_us > 0))
640 /* Validations are blocked, have to wait for blocked_for time */
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Validations blocked for another %s, delaying validating address %s\n",
643 GNUNET_STRINGS_relative_time_to_string (blocked_for,
645 GST_plugins_a2s (ve->address));
646 ve->revalidation_task =
647 GNUNET_SCHEDULER_add_delayed (blocked_for, &revalidate_address, ve);
650 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
652 /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
654 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
655 canonical_delay.rel_value_us);
657 /* Debug code for mantis 0002726 */
658 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
659 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, rdelay).rel_value_us)
662 delay = canonical_delay;
666 delay = GNUNET_TIME_relative_add (canonical_delay,
667 GNUNET_TIME_relative_multiply
668 (GNUNET_TIME_UNIT_MICROSECONDS, rdelay));
670 /* End debug code for mantis 0002726*/
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "Validating now, next scheduled for %s, now validating address %s\n",
673 GNUNET_STRINGS_relative_time_to_string (blocked_for,
675 GST_plugins_a2s (ve->address));
676 ve->revalidation_task =
677 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
679 /* start PINGing by checking blacklist */
680 GNUNET_STATISTICS_update (GST_stats,
681 gettext_noop ("# address revalidations started"), 1,
683 bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
684 &transmit_ping_if_allowed, ve);
686 ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already
692 * Find a ValidationEntry entry for the given neighbour that matches
693 * the given address and transport. If none exists, create one (but
694 * without starting any validation).
696 * @param public_key public key of the peer, NULL for unknown
697 * @param address address to find
698 * @return validation entry matching the given specifications, NULL
699 * if we don't have an existing entry and no public key was given
701 static struct ValidationEntry *
702 find_validation_entry (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
703 const struct GNUNET_HELLO_Address *address)
705 struct ValidationEntryMatchContext vemc;
706 struct ValidationEntry *ve;
709 vemc.address = address;
710 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
712 &validation_entry_match, &vemc);
713 if (NULL != (ve = vemc.ve))
715 if (public_key == NULL)
717 ve = GNUNET_new (struct ValidationEntry);
718 ve->in_use = GNUNET_SYSERR; /* not defined */
719 ve->address = GNUNET_HELLO_address_copy (address);
720 ve->public_key = *public_key;
721 ve->pid = address->peer;
722 ve->pong_sig_valid_until = GNUNET_TIME_absolute_get_zero_();
723 memset (&ve->pong_sig_cache, '\0', sizeof (struct GNUNET_CRYPTO_EddsaSignature));
724 ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
726 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
728 GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
729 &timeout_hello_validation, ve);
730 GNUNET_CONTAINER_multipeermap_put (validation_map, &address->peer,
732 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
733 ve->expecting_pong = GNUNET_NO;
739 * Iterator which adds the given address to the set of validated
742 * @param cls original HELLO message
743 * @param address the address
744 * @param expiration expiration time
745 * @return #GNUNET_OK (keep the address)
748 add_valid_address (void *cls,
749 const struct GNUNET_HELLO_Address *address,
750 struct GNUNET_TIME_Absolute expiration)
752 const struct GNUNET_HELLO_Message *hello = cls;
753 struct ValidationEntry *ve;
754 struct GNUNET_PeerIdentity pid;
755 struct GNUNET_ATS_Information ats;
756 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
758 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
759 return GNUNET_OK; /* expired */
760 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
761 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
764 return GNUNET_OK; /* invalid HELLO !? */
766 if (0 == memcmp (&GST_my_identity,
768 sizeof (struct GNUNET_PeerIdentity)))
770 /* Peerinfo returned own identity, skip validation */
774 ve = find_validation_entry (&public_key, address);
775 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
777 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Starting revalidations for valid address %s\n",
781 GST_plugins_a2s (ve->address));
782 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
785 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
786 ats.value = htonl (ve->network);
787 GNUNET_ATS_address_add (GST_ats, address, NULL, &ats, 1);
794 * Function called for any HELLO known to PEERINFO.
797 * @param peer id of the peer, NULL for last call
798 * @param hello hello message for the peer (can be NULL)
799 * @param err_msg error message
802 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
803 const struct GNUNET_HELLO_Message *hello,
806 GNUNET_assert (NULL != peer);
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Handling HELLO for peer %s\n",
812 GNUNET_assert (NULL ==
813 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
820 * Start the validation subsystem.
822 * @param max_fds maximum number of fds to use
825 GST_validation_start (unsigned int max_fds)
828 * Initialization for validation throttling
830 * We have a maximum number max_fds of connections we can use for validation
831 * We monitor the number of validations in parallel and start to throttle it
832 * when doing to many validations in parallel:
833 * if (running validations < (max_fds / 2))
834 * - "fast start": run validation immediately
835 * - have delay of (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2)
836 * (300 sec / ~150 == ~2 sec.) between two validations
839 validation_next = GNUNET_TIME_absolute_get();
840 validation_delay.rel_value_us = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2);
841 validations_fast_start_threshold = (max_fds / 2);
842 validations_running = 0;
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Validation uses a fast start threshold of %u connections and a delay between of %s\n ",
844 validations_fast_start_threshold,
845 GNUNET_STRINGS_relative_time_to_string (validation_delay,
847 validation_map = GNUNET_CONTAINER_multipeermap_create (VALIDATION_MAP_SIZE,
849 pnc = GNUNET_PEERINFO_notify (GST_cfg, GNUNET_YES,
850 &process_peerinfo_hello, NULL);
855 * Stop the validation subsystem.
858 GST_validation_stop ()
860 struct CheckHelloValidatedContext *chvc;
862 GNUNET_CONTAINER_multipeermap_iterate (validation_map,
863 &cleanup_validation_entry, NULL);
864 GNUNET_CONTAINER_multipeermap_destroy (validation_map);
865 validation_map = NULL;
866 while (NULL != (chvc = chvc_head))
868 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
871 GNUNET_PEERINFO_notify_cancel (pnc);
876 * Send the given PONG to the given address.
878 * @param cls the PONG message
879 * @param public_key public key for the peer, never NULL
880 * @param valid_until is ZERO if we never validated the address,
881 * otherwise a time up to when we consider it (or was) valid
882 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
883 * is ZERO if the address is considered valid (no validation needed)
884 * otherwise a time in the future if we're currently denying re-validation
885 * @param address target address
888 multicast_pong (void *cls,
889 const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
890 struct GNUNET_TIME_Absolute valid_until,
891 struct GNUNET_TIME_Absolute validation_block,
892 const struct GNUNET_HELLO_Address *address)
894 struct TransportPongMessage *pong = cls;
895 struct GNUNET_TRANSPORT_PluginFunctions *papi;
897 papi = GST_plugins_find (address->transport_name);
901 GNUNET_assert (papi->send != NULL);
902 GNUNET_assert (papi->get_session != NULL);
904 struct Session * session = papi->get_session(papi->cls, address);
911 papi->send (papi->cls, session,
912 (const char *) pong, ntohs (pong->header.size),
913 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
915 GST_neighbours_notify_data_sent (&address->peer,
916 address, session, pong->header.size);
922 * We've received a PING. If appropriate, generate a PONG.
924 * @param sender peer sending the PING
925 * @param hdr the PING
926 * @param sender_address the sender address as we got it
927 * @param session session we got the PING from
928 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
931 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
932 const struct GNUNET_MessageHeader *hdr,
933 const struct GNUNET_HELLO_Address *sender_address,
934 struct Session *session)
936 const struct TransportPingMessage *ping;
937 struct TransportPongMessage *pong;
938 struct GNUNET_TRANSPORT_PluginFunctions *papi;
939 struct GNUNET_CRYPTO_EddsaSignature *sig_cache;
940 struct GNUNET_TIME_Absolute *sig_cache_exp;
948 int buggy = GNUNET_NO;
949 struct GNUNET_HELLO_Address address;
951 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
954 return GNUNET_SYSERR;
956 ping = (const struct TransportPingMessage *) hdr;
958 memcmp (&ping->target, &GST_my_identity,
959 sizeof (struct GNUNET_PeerIdentity)))
961 GNUNET_STATISTICS_update (GST_stats,
963 ("# PING message for different peer received"), 1,
965 return GNUNET_SYSERR;
967 GNUNET_STATISTICS_update (GST_stats,
968 gettext_noop ("# PING messages received"), 1,
970 addr = (const char *) &ping[1];
971 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
972 /* peer wants to confirm that this is one of our addresses, this is what is
973 * used for address validation */
976 sig_cache_exp = NULL;
980 addrend = memchr (addr, '\0', alen);
984 return GNUNET_SYSERR;
987 slen = strlen (addr) + 1;
989 address.address = addrend;
990 address.address_length = alen;
991 address.transport_name = addr;
992 address.peer = GST_my_identity;
994 if (NULL == address.transport_name)
999 if (0 != strstr (address.transport_name, "_client"))
1001 plugin_name = GNUNET_strdup (address.transport_name);
1002 pos = strstr (plugin_name, "_client");
1003 GNUNET_assert (NULL != pos);
1004 GNUNET_snprintf (pos, strlen ("_server") + 1, "%s", "_server");
1007 plugin_name = GNUNET_strdup (address.transport_name);
1009 if (NULL == (papi = GST_plugins_find (plugin_name)))
1011 /* we don't have the plugin for this address */
1012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1013 _("Plugin `%s' not available, cannot confirm having this address\n"),
1015 GNUNET_free (plugin_name);
1016 return GNUNET_SYSERR;
1018 GNUNET_free (plugin_name);
1019 if (GNUNET_OK != papi->check_address (papi->cls, addrend, alen))
1021 GNUNET_STATISTICS_update (GST_stats,
1023 ("# failed address checks during validation"), 1,
1025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1026 _("Address `%s' is not one of my addresses, not confirming PING\n"),
1027 GST_plugins_a2s (&address));
1028 return GNUNET_SYSERR;
1032 GNUNET_STATISTICS_update (GST_stats,
1034 ("# successful address checks during validation"), 1,
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Address `%s' is one of my addresses, confirming PING\n",
1038 GST_plugins_a2s (&address));
1041 if (GNUNET_YES != GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
1043 if (GNUNET_NO == buggy)
1045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046 _("Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n"),
1047 GNUNET_i2s (sender),
1048 GST_plugins_a2s (&address));
1049 return GNUNET_SYSERR;
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 _("Received a PING message with validation bug from `%s'\n"),
1055 GNUNET_i2s (sender));
1061 addrend = NULL; /* make gcc happy */
1063 static struct GNUNET_CRYPTO_EddsaSignature no_address_signature;
1064 static struct GNUNET_TIME_Absolute no_address_signature_expiration;
1066 sig_cache = &no_address_signature;
1067 sig_cache_exp = &no_address_signature_expiration;
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "I am `%s', sending PONG to peer `%s'\n",
1072 GNUNET_i2s_full (&GST_my_identity),
1073 GNUNET_i2s (sender));
1075 /* message with structure:
1076 * [TransportPongMessage][Transport name][Address] */
1078 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
1080 htons (sizeof (struct TransportPongMessage) + alen + slen);
1081 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1082 pong->purpose.size =
1083 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1084 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1086 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
1087 memcpy (&pong->challenge, &ping->challenge, sizeof (ping->challenge));
1088 pong->addrlen = htonl (alen + slen);
1089 memcpy (&pong[1], addr, slen); /* Copy transport plugin */
1092 GNUNET_assert (NULL != addrend);
1093 memcpy (&((char *) &pong[1])[slen], addrend, alen);
1095 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value_us <
1096 PONG_SIGNATURE_LIFETIME.rel_value_us / 4)
1098 /* create / update cached sig */
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 "Creating PONG signature to indicate ownership.\n");
1101 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
1102 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1104 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key, &pong->purpose,
1107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1108 _("Failed to create PONG signature for peer `%s'\n"), GNUNET_i2s (sender));
1113 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1115 pong->signature = *sig_cache;
1117 GNUNET_assert (sender_address != NULL);
1119 /* first see if the session we got this PING from can be used to transmit
1120 * a response reliably */
1125 GNUNET_assert (papi->send != NULL);
1126 GNUNET_assert (papi->get_session != NULL);
1128 if (session == NULL)
1130 session = papi->get_session (papi->cls, sender_address);
1132 if (session == NULL)
1139 ret = papi->send (papi->cls, session,
1140 (const char *) pong, ntohs (pong->header.size),
1141 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
1144 GST_neighbours_notify_data_sent (sender,
1145 sender_address, session,
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1152 "Transmitted PONG to `%s' via reliable mechanism\n",
1153 GNUNET_i2s (sender));
1155 GNUNET_STATISTICS_update (GST_stats,
1157 ("# PONGs unicast via reliable transport"), 1,
1163 /* no reliable method found, try transmission via all known addresses */
1164 GNUNET_STATISTICS_update (GST_stats,
1166 ("# PONGs multicast to all available addresses"), 1,
1168 GST_validation_get_addresses (sender,
1169 &multicast_pong, pong);
1176 * Context for the #validate_address_iterator() function
1178 struct ValidateAddressContext
1181 * Hash of the public key of the peer whose address is being validated.
1183 struct GNUNET_PeerIdentity pid;
1186 * Public key of the peer whose address is being validated.
1188 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
1194 * Iterator callback to go over all addresses and try to validate them
1195 * (unless blocked or already validated).
1197 * @param cls pointer to a `struct ValidateAddressContext`
1198 * @param address the address
1199 * @param expiration expiration time
1200 * @return #GNUNET_OK (keep the address)
1203 validate_address_iterator (void *cls,
1204 const struct GNUNET_HELLO_Address *address,
1205 struct GNUNET_TIME_Absolute expiration)
1207 const struct ValidateAddressContext *vac = cls;
1208 struct ValidationEntry *ve;
1210 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "Skipping expired address from HELLO\n");
1214 return GNUNET_OK; /* expired */
1216 ve = find_validation_entry (&vac->public_key, address);
1217 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "Starting validation for fresh address %s\n",
1221 GST_plugins_a2s (ve->address));
1222 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1229 * Add the validated peer address to the HELLO.
1231 * @param cls the 'struct ValidationEntry' with the validated address
1232 * @param max space in buf
1233 * @param buf where to add the address
1234 * @return number of bytes written, 0 to signal the
1235 * end of the iteration.
1238 add_valid_peer_address (void *cls, size_t max, void *buf)
1240 struct ValidationEntry *ve = cls;
1242 if (GNUNET_YES == ve->copied)
1243 return 0; /* terminate */
1244 ve->copied = GNUNET_YES;
1245 return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1250 * We've received a PONG. Check if it matches a pending PING and
1251 * mark the respective address as confirmed.
1253 * @param sender peer sending the PONG
1254 * @param hdr the PONG
1255 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1258 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1259 const struct GNUNET_MessageHeader *hdr)
1261 const struct TransportPongMessage *pong;
1262 struct ValidationEntry *ve;
1268 struct GNUNET_HELLO_Message *hello;
1269 struct GNUNET_HELLO_Address address;
1273 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1275 GNUNET_break_op (0);
1276 return GNUNET_SYSERR;
1278 GNUNET_STATISTICS_update (GST_stats,
1279 gettext_noop ("# PONG messages received"), 1,
1282 /* message with structure:
1283 * [TransportPongMessage][Transport name][Address] */
1285 pong = (const struct TransportPongMessage *) hdr;
1286 tname = (const char *) &pong[1];
1287 size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1288 addr = memchr (tname, '\0', size);
1291 GNUNET_break_op (0);
1292 return GNUNET_SYSERR;
1295 slen = strlen (tname) + 1;
1296 addrlen = size - slen;
1297 address.peer = *sender;
1298 address.address = addr;
1299 address.address_length = addrlen;
1300 address.transport_name = tname;
1301 ve = find_validation_entry (NULL, &address);
1302 if ((NULL == ve) || (GNUNET_NO == ve->expecting_pong))
1304 GNUNET_STATISTICS_update (GST_stats,
1306 ("# PONGs dropped, no matching pending validation"),
1310 /* now check that PONG is well-formed */
1311 if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
1313 GNUNET_break_op (0);
1314 return GNUNET_SYSERR;
1316 if (GNUNET_TIME_absolute_get_remaining
1317 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value_us == 0)
1319 GNUNET_STATISTICS_update (GST_stats,
1321 ("# PONGs dropped, signature expired"), 1,
1323 return GNUNET_SYSERR;
1326 sig_res = GNUNET_SYSERR;
1327 do_verify = GNUNET_YES;
1328 if (0 != GNUNET_TIME_absolute_get_remaining(ve->pong_sig_valid_until).rel_value_us)
1330 /* We have a cached and valid signature for this peer,
1331 * try to compare instead of verify */
1332 if (0 == memcmp (&ve->pong_sig_cache, &pong->signature, sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
1334 /* signatures are identical, we can skip verification */
1335 sig_res = GNUNET_OK;
1336 do_verify = GNUNET_NO;
1340 sig_res = GNUNET_SYSERR;
1341 /* signatures do not match, we have to verify */
1345 if (GNUNET_YES == do_verify)
1347 /* Do expensive verification */
1348 sig_res = GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1349 &pong->purpose, &pong->signature,
1351 if (sig_res == GNUNET_SYSERR)
1353 GNUNET_break_op (0);
1354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1355 "Failed to verify: invalid signature on address %s:%s from peer `%s'\n",
1357 GST_plugins_a2s (ve->address),
1358 GNUNET_i2s (sender));
1361 if (sig_res == GNUNET_SYSERR)
1363 GNUNET_break_op (0);
1364 return GNUNET_SYSERR;
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "Address validated for peer `%s' with plugin `%s': `%s'\n",
1369 GNUNET_i2s (sender), tname, GST_plugins_a2s (ve->address));
1370 /* validity achieved, remember it! */
1371 ve->expecting_pong = GNUNET_NO;
1372 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1373 ve->pong_sig_cache = pong->signature;
1374 ve->pong_sig_valid_until = GNUNET_TIME_absolute_ntoh (pong->expiration);
1375 ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1377 struct GNUNET_ATS_Information ats[2];
1379 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1380 ats[0].value = htonl ((uint32_t) ve->latency.rel_value_us);
1381 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1382 ats[1].value = htonl ((uint32_t) ve->network);
1383 GNUNET_ATS_address_add (GST_ats, ve->address, NULL, ats, 2);
1385 if (validations_running > 0)
1387 validations_running --;
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "Validation finished, %u validation processes running\n",
1390 validations_running);
1397 /* build HELLO to store in PEERINFO */
1398 ve->copied = GNUNET_NO;
1399 hello = GNUNET_HELLO_create (&ve->public_key,
1400 &add_valid_peer_address, ve,
1402 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello, NULL, NULL);
1403 GNUNET_free (hello);
1409 * We've received a HELLO, check which addresses are new and trigger
1412 * @param hello the HELLO we received
1413 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1416 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1418 const struct GNUNET_HELLO_Message *hm =
1419 (const struct GNUNET_HELLO_Message *) hello;
1420 struct ValidateAddressContext vac;
1421 struct GNUNET_HELLO_Message *h;
1424 friend = GNUNET_HELLO_is_friend_only (hm);
1425 if ( ( (GNUNET_YES != friend) &&
1426 (GNUNET_NO != friend) ) ||
1427 (GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1428 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1430 /* malformed HELLO */
1431 GNUNET_break_op (0);
1432 return GNUNET_SYSERR;
1435 memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1437 /* Add peer identity without addresses to peerinfo service */
1438 h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL, friend);
1439 GNUNET_PEERINFO_add_peer (GST_peerinfo, h, NULL, NULL);
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1443 GNUNET_i2s (&vac.pid));
1446 GNUNET_assert (NULL ==
1447 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1448 &validate_address_iterator,
1455 * Closure for #iterate_addresses().
1457 struct IteratorContext
1460 * Function to call on each address.
1462 GST_ValidationAddressCallback cb;
1465 * Closure for @e cb.
1473 * Call the callback in the closure for each validation entry.
1475 * @param cls the `struct IteratorContext`
1476 * @param key the peer's identity
1477 * @param value the `struct ValidationEntry`
1478 * @return #GNUNET_OK (continue to iterate)
1481 iterate_addresses (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1483 struct IteratorContext *ic = cls;
1484 struct ValidationEntry *ve = value;
1486 ic->cb (ic->cb_cls, &ve->public_key, ve->valid_until, ve->revalidation_block,
1493 * Call the given function for each address for the given target.
1494 * Can either give a snapshot (synchronous API) or be continuous.
1496 * @param target peer information is requested for
1497 * @param cb function to call; will not be called after this function returns
1498 * @param cb_cls closure for 'cb'
1501 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1502 GST_ValidationAddressCallback cb, void *cb_cls)
1504 struct IteratorContext ic;
1508 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
1510 &iterate_addresses, &ic);
1515 * Update if we are using an address for a connection actively right now.
1516 * Based on this, the validation module will measure latency for the
1517 * address more or less often.
1519 * @param address the address
1520 * @param session the session
1521 * @param in_use #GNUNET_YES if we are now using the address for a connection,
1522 * #GNUNET_NO if we are no longer using the address for a connection
1525 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1526 struct Session *session,
1529 struct ValidationEntry *ve;
1531 if (NULL != address)
1532 ve = find_validation_entry (NULL, address);
1534 ve = NULL; /* FIXME: lookup based on session... */
1537 /* this can happen for inbound connections (sender_address_len == 0); */
1540 if (ve->in_use == in_use)
1543 if (GNUNET_YES == in_use)
1545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1546 "Error setting address in use for peer `%s' `%s' to USED\n",
1547 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
1549 if (GNUNET_NO == in_use)
1551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1552 "Error setting address in use for peer `%s' `%s' to NOT_USED\n",
1553 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
1557 GNUNET_break (ve->in_use != in_use); /* should be different... */
1558 ve->in_use = in_use;
1559 if (in_use == GNUNET_YES)
1561 /* from now on, higher frequeny, so reschedule now */
1562 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1563 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1569 * Query validation about the latest observed latency on a given
1572 * @param sender peer
1573 * @param address the address
1574 * @param session session
1575 * @return observed latency of the address, FOREVER if the address was
1576 * never successfully validated
1578 struct GNUNET_TIME_Relative
1579 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1580 const struct GNUNET_HELLO_Address *address,
1581 struct Session *session)
1583 struct ValidationEntry *ve;
1585 if (NULL == address)
1587 GNUNET_break (0); // FIXME: support having latency only with session...
1588 return GNUNET_TIME_UNIT_FOREVER_REL;
1590 ve = find_validation_entry (NULL, address);
1592 return GNUNET_TIME_UNIT_FOREVER_REL;
1597 /* end of file gnunet-service-transport_validation.c */