2 This file is part of GNUnet.
3 (C) 2010-2015 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_ats.h"
28 #include "gnunet-service-transport_blacklist.h"
29 #include "gnunet-service-transport_clients.h"
30 #include "gnunet-service-transport_hello.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport_plugins.h"
33 #include "gnunet-service-transport_validation.h"
34 #include "gnunet-service-transport.h"
35 #include "gnunet_hello_lib.h"
36 #include "gnunet_ats_service.h"
37 #include "gnunet_peerinfo_service.h"
38 #include "gnunet_signatures.h"
42 * How long is a PONG signature valid? We'll recycle a signature until
43 * 1/4 of this time is remaining. PONGs should expire so that if our
44 * external addresses change an adversary cannot replay them indefinitely.
45 * OTOH, we don't want to spend too much time generating PONG signatures,
46 * so they must have some lifetime to reduce our CPU usage.
48 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
51 * After how long do we expire an address in a HELLO that we just
52 * validated? This value is also used for our own addresses when we
55 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
58 * How often do we allow PINGing an address that we have not yet
59 * validated? This also determines how long we track an address that
60 * we cannot validate (because after this time we can destroy the
63 #define UNVALIDATED_PING_KEEPALIVE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
66 * How often do we PING an address that we have successfully validated
67 * in the past but are not actively using? Should be (significantly)
68 * smaller than HELLO_ADDRESS_EXPIRATION.
70 #define VALIDATED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
73 * How often do we PING an address that we are currently using?
75 #define CONNECTED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
78 * How much delay is acceptable for sending the PING or PONG?
80 #define ACCEPTABLE_PING_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
83 * Size of the validation map hashmap.
85 #define VALIDATION_MAP_SIZE 256
88 * Priority to use for PINGs
90 #define PING_PRIORITY 2
93 * Priority to use for PONGs
95 #define PONG_PRIORITY 4
98 GNUNET_NETWORK_STRUCT_BEGIN
101 * Message used to ask a peer to validate receipt (to check an address
102 * from a HELLO). Followed by the address we are trying to validate,
103 * or an empty address if we are just sending a PING to confirm that a
104 * connection which the receiver (of the PING) initiated is still valid.
106 struct TransportPingMessage
110 * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_PING
112 struct GNUNET_MessageHeader header;
115 * Challenge code (to ensure fresh reply).
117 uint32_t challenge GNUNET_PACKED;
120 * Who is the intended recipient?
122 struct GNUNET_PeerIdentity target;
128 * Message used to validate a HELLO. The challenge is included in the
129 * confirmation to make matching of replies to requests possible. The
130 * signature signs our public key, an expiration time and our address.<p>
132 * This message is followed by our transport address that the PING tried
133 * to confirm (if we liked it). The address can be empty (zero bytes)
134 * if the PING had not address either (and we received the request via
135 * a connection that we initiated).
137 struct TransportPongMessage
141 * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
143 struct GNUNET_MessageHeader header;
146 * Challenge code from PING (showing freshness). Not part of what
147 * is signed so that we can re-use signatures.
149 uint32_t challenge GNUNET_PACKED;
154 struct GNUNET_CRYPTO_EddsaSignature signature;
157 * #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
158 * plausible address for the signing peer.
160 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
163 * When does this signature expire?
165 struct GNUNET_TIME_AbsoluteNBO expiration;
168 * Size of address appended to this message (part of what is
169 * being signed, hence not redundant).
171 uint32_t addrlen GNUNET_PACKED;
174 GNUNET_NETWORK_STRUCT_END
177 * Information about an address under validation
179 struct ValidationEntry
185 struct GNUNET_HELLO_Address *address;
188 * Handle to the blacklist check (if we're currently in it).
190 struct GST_BlacklistCheck *bc;
193 * Public key of the peer.
195 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
198 * The identity of the peer. FIXME: duplicated (also in 'address')
200 struct GNUNET_PeerIdentity pid;
203 * Cached PONG signature
205 struct GNUNET_CRYPTO_EddsaSignature pong_sig_cache;
208 * ID of task that will clean up this entry if nothing happens.
210 struct GNUNET_SCHEDULER_Task *timeout_task;
213 * ID of task that will trigger address revalidation.
215 struct GNUNET_SCHEDULER_Task *revalidation_task;
218 * At what time did we send the latest validation request (PING)?
220 struct GNUNET_TIME_Absolute send_time;
223 * At what time do we send the next validation request (PING)?
225 struct GNUNET_TIME_Absolute next_validation;
228 * Until when is this address valid?
229 * ZERO if it is not currently considered valid.
231 struct GNUNET_TIME_Absolute valid_until;
234 * Until when is the cached PONG signature valid?
235 * ZERO if it is not currently considered valid.
237 struct GNUNET_TIME_Absolute pong_sig_valid_until;
240 * How long until we can try to validate this address again?
241 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
242 * ZERO if the address is considered valid (no validation needed)
243 * otherwise a time in the future if we're currently denying re-validation
245 struct GNUNET_TIME_Absolute revalidation_block;
248 * Last observed latency for this address (round-trip), delay between
249 * last PING sent and PONG received; FOREVER if we never got a PONG.
251 struct GNUNET_TIME_Relative latency;
254 * Current state of this validation entry
256 enum GNUNET_TRANSPORT_ValidationState state;
259 * Challenge number we used.
264 * When passing the address in 'add_valid_peer_address', did we
265 * copy the address to the HELLO yet?
270 * Are we currently using this address for a connection?
275 * Are we expecting a PONG message for this validation entry?
280 * Is this address known to ATS as valid right now?
285 * Which network type does our address belong to?
287 enum GNUNET_ATS_Network_Type network;
292 * Context of currently active requests to peerinfo
293 * for validation of HELLOs.
295 struct CheckHelloValidatedContext
299 * This is a doubly-linked list.
301 struct CheckHelloValidatedContext *next;
304 * This is a doubly-linked list.
306 struct CheckHelloValidatedContext *prev;
309 * Hello that we are validating.
311 const struct GNUNET_HELLO_Message *hello;
317 * Head of linked list of HELLOs awaiting validation.
319 static struct CheckHelloValidatedContext *chvc_head;
322 * Tail of linked list of HELLOs awaiting validation
324 static struct CheckHelloValidatedContext *chvc_tail;
327 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
328 * of the given peer that we are currently validating, have validated
329 * or are blocked from re-validation for a while).
331 static struct GNUNET_CONTAINER_MultiPeerMap *validation_map;
334 * Context for peerinfo iteration.
336 static struct GNUNET_PEERINFO_NotifyContext *pnc;
339 * Minimum delay between to validations
341 static struct GNUNET_TIME_Relative validation_delay;
344 * Number of validations running; any PING that was not yet
345 * matched by a PONG and for which we have not yet hit the
346 * timeout is considered a running 'validation'.
348 static unsigned int validations_running;
351 * Validition fast start threshold
353 static unsigned int validations_fast_start_threshold;
356 * When is next validation allowed
358 static struct GNUNET_TIME_Absolute validation_next;
362 * Context for the validation entry match function.
364 struct ValidationEntryMatchContext
367 * Where to store the result?
369 struct ValidationEntry *ve;
372 * Address we're interested in.
374 const struct GNUNET_HELLO_Address *address;
380 * Iterate over validation entries until a matching one is found.
382 * @param cls the `struct ValidationEntryMatchContext *`
383 * @param key peer identity (unused)
384 * @param value a `struct ValidationEntry *` to match
385 * @return #GNUNET_YES if the entry does not match,
386 * #GNUNET_NO if the entry does match
389 validation_entry_match (void *cls,
390 const struct GNUNET_PeerIdentity *key,
393 struct ValidationEntryMatchContext *vemc = cls;
394 struct ValidationEntry *ve = value;
396 if (0 == GNUNET_HELLO_address_cmp (ve->address, vemc->address))
406 * A validation entry changed. Update the state and notify
409 * @param ve validation entry that changed
410 * @param state new state
413 validation_entry_changed (struct ValidationEntry *ve,
414 enum GNUNET_TRANSPORT_ValidationState state)
417 GST_clients_broadcast_validation_notification (&ve->pid,
427 * Iterate over validation entries and free them.
429 * @param cls (unused)
430 * @param key peer identity (unused)
431 * @param value a `struct ValidationEntry *` to clean up
432 * @return #GNUNET_YES (continue to iterate)
435 cleanup_validation_entry (void *cls,
436 const struct GNUNET_PeerIdentity *key,
439 struct ValidationEntry *ve = value;
441 ve->next_validation = GNUNET_TIME_UNIT_ZERO_ABS;
442 ve->valid_until = GNUNET_TIME_UNIT_ZERO_ABS;
444 /* Notify about deleted entry */
445 validation_entry_changed (ve, GNUNET_TRANSPORT_VS_REMOVE);
449 GST_blacklist_test_cancel (ve->bc);
452 GNUNET_break (GNUNET_OK ==
453 GNUNET_CONTAINER_multipeermap_remove (validation_map,
455 if (GNUNET_YES == ve->known_to_ats)
457 GST_ats_expire_address (ve->address);
458 ve->known_to_ats = GNUNET_NO;
460 GNUNET_HELLO_address_free (ve->address);
461 if (NULL != ve->timeout_task)
463 GNUNET_SCHEDULER_cancel (ve->timeout_task);
464 ve->timeout_task = NULL;
466 if (NULL != ve->revalidation_task)
468 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
469 ve->revalidation_task = NULL;
471 if ( (GNUNET_YES == ve->expecting_pong) &&
472 (validations_running > 0) )
474 validations_running --;
475 GNUNET_STATISTICS_set (GST_stats,
476 gettext_noop ("# validations running"),
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Validation finished, %u validation processes running\n",
481 validations_running);
489 * Address validation cleanup task. Assesses if the record is no
490 * longer valid and then possibly triggers its removal.
492 * @param cls the `struct ValidationEntry`
493 * @param tc scheduler context (unused)
496 timeout_hello_validation (void *cls,
497 const struct GNUNET_SCHEDULER_TaskContext *tc)
499 struct ValidationEntry *ve = cls;
500 struct GNUNET_TIME_Absolute max;
501 struct GNUNET_TIME_Relative left;
503 ve->timeout_task = NULL;
504 max = GNUNET_TIME_absolute_max (ve->valid_until,
505 ve->revalidation_block);
506 left = GNUNET_TIME_absolute_get_remaining (max);
507 if (left.rel_value_us > 0)
509 /* should wait a bit longer */
511 GNUNET_SCHEDULER_add_delayed (left, &timeout_hello_validation, ve);
514 GNUNET_STATISTICS_update (GST_stats,
515 gettext_noop ("# address records discarded"), 1,
517 cleanup_validation_entry (NULL, &ve->pid, ve);
522 * Function called with the result from blacklisting.
523 * Send a PING to the other peer if a communication is allowed.
525 * @param cls our `struct ValidationEntry`
526 * @param pid identity of the other peer
527 * @param result #GNUNET_OK if the connection is allowed, #GNUNET_NO if not
530 transmit_ping_if_allowed (void *cls,
531 const struct GNUNET_PeerIdentity *pid,
534 struct ValidationEntry *ve = cls;
535 struct TransportPingMessage ping;
536 struct GNUNET_TRANSPORT_PluginFunctions *papi;
537 struct GNUNET_TIME_Absolute next;
538 const struct GNUNET_MessageHeader *hello;
539 enum GNUNET_ATS_Network_Type network;
546 if (GNUNET_NO == result)
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Blacklist denies to send PING to `%s' `%s' `%s'\n",
551 GST_plugins_a2s (ve->address),
552 ve->address->transport_name);
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Transmitting plain PING to `%s' `%s' `%s'\n",
559 GST_plugins_a2s (ve->address),
560 ve->address->transport_name);
562 slen = strlen (ve->address->transport_name) + 1;
563 hello = GST_hello_get ();
564 hsize = ntohs (hello->size);
566 sizeof (struct TransportPingMessage) + ve->address->address_length +
570 htons (sizeof (struct TransportPingMessage) +
571 ve->address->address_length + slen);
572 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
573 ping.challenge = htonl (ve->challenge);
576 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
581 sizeof (struct TransportPingMessage) + ve->address->address_length +
585 char message_buf[tsize];
587 /* build message with structure:
588 * [HELLO][TransportPingMessage][Transport name][Address] */
589 memcpy (message_buf, hello, hsize);
590 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
591 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
592 ve->address->transport_name, slen);
593 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
594 ve->address->address, ve->address->address_length);
595 papi = GST_plugins_find (ve->address->transport_name);
599 GNUNET_STATISTICS_update (GST_stats,
600 gettext_noop ("# validations not attempted (no plugin)"),
606 GNUNET_assert (NULL != papi->send);
607 GNUNET_assert (NULL != papi->get_session);
608 struct Session *session = papi->get_session (papi->cls,
613 ret = papi->send (papi->cls, session,
615 PING_PRIORITY, ACCEPTABLE_PING_DELAY,
617 network = papi->get_network (papi->cls, session);
618 if (GNUNET_ATS_NET_UNSPECIFIED == network)
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Could not obtain a valid network for `%s' `%s'\n",
623 GST_plugins_a2s (ve->address));
626 GST_neighbours_notify_data_sent (pid, ve->address, session, tsize);
630 /* Could not get a valid session */
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Could not get a valid session for `%s' `%s'\n",
634 GST_plugins_a2s (ve->address));
641 next = GNUNET_TIME_relative_to_absolute (validation_delay);
642 validation_next = GNUNET_TIME_absolute_max (next,
644 ve->send_time = GNUNET_TIME_absolute_get ();
645 GNUNET_STATISTICS_update (GST_stats,
646 gettext_noop ("# PINGs for address validation sent"),
649 ve->network = network;
650 ve->expecting_pong = GNUNET_YES;
651 validations_running++;
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 "Validation started, %u validation processes running\n",
654 validations_running);
655 GNUNET_STATISTICS_set (GST_stats,
656 gettext_noop ("# validations running"),
659 /* Notify about PING sent */
660 validation_entry_changed (ve, GNUNET_TRANSPORT_VS_UPDATE);
666 * Do address validation again to keep address valid.
668 * @param cls the `struct ValidationEntry`
669 * @param tc scheduler context (unused)
672 revalidate_address (void *cls,
673 const struct GNUNET_SCHEDULER_TaskContext *tc)
675 struct ValidationEntry *ve = cls;
676 struct GNUNET_TIME_Relative canonical_delay;
677 struct GNUNET_TIME_Relative delay;
678 struct GNUNET_TIME_Relative blocked_for;
679 struct GST_BlacklistCheck *bc;
682 ve->revalidation_task = NULL;
683 delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
684 /* Considering current connectivity situation, what is the maximum
685 block period permitted? */
686 if (GNUNET_YES == ve->in_use)
687 canonical_delay = CONNECTED_PING_FREQUENCY;
688 else if (GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value_us > 0)
689 canonical_delay = VALIDATED_PING_FREQUENCY;
691 canonical_delay = UNVALIDATED_PING_KEEPALIVE;
692 /* Use delay that is MIN of original delay and possibly adjusted
693 new maximum delay (which may be lower); the real delay
694 is originally randomized between "canonical_delay" and "2 * canonical_delay",
695 so continue to permit that window for the operation. */
696 delay = GNUNET_TIME_relative_min (delay,
697 GNUNET_TIME_relative_multiply (canonical_delay,
699 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
700 if (delay.rel_value_us > 0)
702 /* should wait a bit longer */
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "Waiting for %s longer before validating address `%s'\n",
705 GNUNET_STRINGS_relative_time_to_string (delay,
707 GST_plugins_a2s (ve->address));
708 ve->revalidation_task =
709 GNUNET_SCHEDULER_add_delayed (delay,
710 &revalidate_address, ve);
711 ve->next_validation = GNUNET_TIME_relative_to_absolute (delay);
714 /* check if globally we have too many active validations at a
715 too high rate, if so, delay ours */
716 blocked_for = GNUNET_TIME_absolute_get_remaining (validation_next);
717 if ( (validations_running > validations_fast_start_threshold) &&
718 (blocked_for.rel_value_us > 0) )
720 /* Validations are blocked, have to wait for blocked_for time */
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Validations blocked for another %s, delaying validating address `%s'\n",
723 GNUNET_STRINGS_relative_time_to_string (blocked_for,
725 GST_plugins_a2s (ve->address));
726 ve->revalidation_task =
727 GNUNET_SCHEDULER_add_delayed (blocked_for, &revalidate_address, ve);
728 ve->next_validation = GNUNET_TIME_relative_to_absolute (blocked_for);
732 /* We are good to go; remember to not go again for `canonical_delay` time;
733 add up to `canonical_delay` to randomize start time */
734 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
735 /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
737 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
738 canonical_delay.rel_value_us);
740 delay = GNUNET_TIME_relative_add (canonical_delay,
741 GNUNET_TIME_relative_multiply
742 (GNUNET_TIME_UNIT_MICROSECONDS, rdelay));
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Validating now, next scheduled for %s, now validating address `%s'\n",
746 GNUNET_STRINGS_relative_time_to_string (blocked_for,
748 GST_plugins_a2s (ve->address));
749 ve->revalidation_task =
750 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
751 ve->next_validation = GNUNET_TIME_relative_to_absolute (delay);
753 /* start PINGing by checking blacklist */
754 GNUNET_STATISTICS_update (GST_stats,
755 gettext_noop ("# address revalidations started"), 1,
757 bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
758 &transmit_ping_if_allowed, ve);
760 ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already
766 * Find a ValidationEntry entry for the given neighbour that matches
767 * the given address and transport. If none exists, create one (but
768 * without starting any validation).
770 * @param public_key public key of the peer, NULL for unknown
771 * @param address address to find
772 * @return validation entry matching the given specifications, NULL
773 * if we don't have an existing entry and no public key was given
775 static struct ValidationEntry *
776 find_validation_entry (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
777 const struct GNUNET_HELLO_Address *address)
779 struct ValidationEntryMatchContext vemc;
780 struct ValidationEntry *ve;
783 vemc.address = address;
784 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
786 &validation_entry_match, &vemc);
787 if (NULL != (ve = vemc.ve))
789 if (NULL == public_key)
791 ve = GNUNET_new (struct ValidationEntry);
792 ve->in_use = GNUNET_SYSERR; /* not defined */
793 ve->address = GNUNET_HELLO_address_copy (address);
794 ve->public_key = *public_key;
795 ve->pid = address->peer;
796 ve->pong_sig_valid_until = GNUNET_TIME_absolute_get_zero_();
797 memset (&ve->pong_sig_cache, '\0', sizeof (struct GNUNET_CRYPTO_EddsaSignature));
798 ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
800 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
802 GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
803 &timeout_hello_validation, ve);
804 GNUNET_CONTAINER_multipeermap_put (validation_map, &address->peer,
806 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
807 validation_entry_changed (ve, GNUNET_TRANSPORT_VS_NEW);
813 * Iterator which adds the given address to the set of validated
816 * @param cls original HELLO message
817 * @param address the address
818 * @param expiration expiration time
819 * @return #GNUNET_OK (keep the address)
822 add_valid_address (void *cls,
823 const struct GNUNET_HELLO_Address *address,
824 struct GNUNET_TIME_Absolute expiration)
826 const struct GNUNET_HELLO_Message *hello = cls;
827 struct ValidationEntry *ve;
828 struct GNUNET_PeerIdentity pid;
829 struct GNUNET_ATS_Information ats;
830 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
832 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
833 return GNUNET_OK; /* expired */
834 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
835 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
838 return GNUNET_OK; /* invalid HELLO !? */
840 if (0 == memcmp (&GST_my_identity,
842 sizeof (struct GNUNET_PeerIdentity)))
844 /* Peerinfo returned own identity, skip validation */
847 if (NULL == GST_plugins_find (address->transport_name))
849 /* might have been valid in the past, but we don't have that
850 plugin loaded right now */
854 ve = find_validation_entry (&public_key, address);
855 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until,
857 if (NULL == ve->revalidation_task)
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Starting revalidations for valid address `%s'\n",
861 GST_plugins_a2s (ve->address));
862 ve->next_validation = GNUNET_TIME_absolute_get();
863 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
865 validation_entry_changed (ve, GNUNET_TRANSPORT_VS_UPDATE);
867 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
868 ats.value = htonl (ve->network);
869 if (GNUNET_YES != ve->known_to_ats)
871 ve->known_to_ats = GNUNET_YES;
872 GST_ats_add_address (address, NULL, &ats, 1);
879 * Function called for any HELLO known to PEERINFO.
882 * @param peer id of the peer, NULL for last call
883 * @param hello hello message for the peer (can be NULL)
884 * @param err_msg error message
887 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
888 const struct GNUNET_HELLO_Message *hello,
891 GNUNET_assert (NULL != peer);
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Handling HELLO for peer `%s'\n",
897 GNUNET_assert (NULL ==
898 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
905 * Start the validation subsystem.
907 * @param max_fds maximum number of fds to use
910 GST_validation_start (unsigned int max_fds)
913 * Initialization for validation throttling
915 * We have a maximum number max_fds of connections we can use for validation
916 * We monitor the number of validations in parallel and start to throttle it
917 * when doing to many validations in parallel:
918 * if (running validations < (max_fds / 2))
919 * - "fast start": run validation immediately
920 * - have delay of (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2)
921 * (300 sec / ~150 == ~2 sec.) between two validations
924 validation_next = GNUNET_TIME_absolute_get();
925 validation_delay.rel_value_us = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2);
926 validations_fast_start_threshold = (max_fds / 2);
927 validations_running = 0;
928 GNUNET_STATISTICS_set (GST_stats,
929 gettext_noop ("# validations running"),
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
933 "Validation uses a fast start threshold of %u connections and a delay between of %s\n ",
934 validations_fast_start_threshold,
935 GNUNET_STRINGS_relative_time_to_string (validation_delay,
937 validation_map = GNUNET_CONTAINER_multipeermap_create (VALIDATION_MAP_SIZE,
939 pnc = GNUNET_PEERINFO_notify (GST_cfg, GNUNET_YES,
940 &process_peerinfo_hello, NULL);
945 * Stop the validation subsystem.
948 GST_validation_stop ()
950 struct CheckHelloValidatedContext *chvc;
952 GNUNET_CONTAINER_multipeermap_iterate (validation_map,
953 &cleanup_validation_entry, NULL);
954 GNUNET_CONTAINER_multipeermap_destroy (validation_map);
955 validation_map = NULL;
956 while (NULL != (chvc = chvc_head))
958 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
961 GNUNET_PEERINFO_notify_cancel (pnc);
966 * Send the given PONG to the given address.
968 * @param cls the PONG message
969 * @param public_key public key for the peer, never NULL
970 * @param valid_until is ZERO if we never validated the address,
971 * otherwise a time up to when we consider it (or was) valid
972 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
973 * is ZERO if the address is considered valid (no validation needed)
974 * otherwise a time in the future if we're currently denying re-validation
975 * @param address target address
978 multicast_pong (void *cls,
979 const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
980 struct GNUNET_TIME_Absolute valid_until,
981 struct GNUNET_TIME_Absolute validation_block,
982 const struct GNUNET_HELLO_Address *address)
984 struct TransportPongMessage *pong = cls;
985 struct GNUNET_TRANSPORT_PluginFunctions *papi;
986 struct Session *session;
988 papi = GST_plugins_find (address->transport_name);
992 GNUNET_assert (NULL != papi->send);
993 GNUNET_assert (NULL != papi->get_session);
994 session = papi->get_session(papi->cls, address);
1000 GST_ats_new_session (address, session);
1001 papi->send (papi->cls, session,
1002 (const char *) pong,
1003 ntohs (pong->header.size),
1005 ACCEPTABLE_PING_DELAY,
1007 GST_neighbours_notify_data_sent (&address->peer,
1016 * We've received a PING. If appropriate, generate a PONG.
1018 * @param sender peer sending the PING
1019 * @param hdr the PING
1020 * @param sender_address the sender address as we got it
1021 * @param session session we got the PING from
1022 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1025 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
1026 const struct GNUNET_MessageHeader *hdr,
1027 const struct GNUNET_HELLO_Address *sender_address,
1028 struct Session *session)
1030 const struct TransportPingMessage *ping;
1031 struct TransportPongMessage *pong;
1032 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1033 struct GNUNET_CRYPTO_EddsaSignature *sig_cache;
1034 struct GNUNET_TIME_Absolute *sig_cache_exp;
1036 const char *addrend;
1042 int buggy = GNUNET_NO;
1043 struct GNUNET_HELLO_Address address;
1045 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
1047 GNUNET_break_op (0);
1048 return GNUNET_SYSERR;
1050 ping = (const struct TransportPingMessage *) hdr;
1052 memcmp (&ping->target, &GST_my_identity,
1053 sizeof (struct GNUNET_PeerIdentity)))
1055 GNUNET_STATISTICS_update (GST_stats,
1057 ("# PING message for different peer received"), 1,
1059 return GNUNET_SYSERR;
1061 GNUNET_STATISTICS_update (GST_stats,
1062 gettext_noop ("# PING messages received"), 1,
1064 addr = (const char *) &ping[1];
1065 len_address = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
1066 /* peer wants to confirm that this is one of our addresses, this is what is
1067 * used for address validation */
1070 sig_cache_exp = NULL;
1072 if (len_address > 0)
1074 addrend = memchr (addr, '\0', len_address);
1075 if (NULL == addrend)
1077 GNUNET_break_op (0);
1078 return GNUNET_SYSERR;
1081 len_plugin = strlen (addr) + 1;
1082 len_address -= len_plugin;
1083 address.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
1084 address.address = addrend;
1085 address.address_length = len_address;
1086 address.transport_name = addr;
1087 address.peer = GST_my_identity;
1089 if (NULL == address.transport_name)
1094 if (0 != strstr (address.transport_name, "_client"))
1096 plugin_name = GNUNET_strdup (address.transport_name);
1097 pos = strstr (plugin_name, "_client");
1098 GNUNET_assert (NULL != pos);
1099 GNUNET_snprintf (pos, strlen ("_server") + 1, "%s", "_server");
1102 plugin_name = GNUNET_strdup (address.transport_name);
1104 if (NULL == (papi = GST_plugins_find (plugin_name)))
1106 /* we don't have the plugin for this address */
1107 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1108 _("Plugin `%s' not available, cannot confirm having this address\n"),
1110 GNUNET_free (plugin_name);
1111 return GNUNET_SYSERR;
1113 GNUNET_free (plugin_name);
1114 if (GNUNET_OK != papi->check_address (papi->cls, addrend, len_address))
1116 GNUNET_STATISTICS_update (GST_stats,
1118 ("# failed address checks during validation"), 1,
1120 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1121 _("Address `%s' is not one of my addresses, not confirming PING\n"),
1122 GST_plugins_a2s (&address));
1123 return GNUNET_SYSERR;
1127 GNUNET_STATISTICS_update (GST_stats,
1129 ("# successful address checks during validation"), 1,
1131 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1132 "Address `%s' is one of my addresses, confirming PING\n",
1133 GST_plugins_a2s (&address));
1136 if (GNUNET_YES != GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
1138 if (GNUNET_NO == buggy)
1140 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1141 _("Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n"),
1142 GNUNET_i2s (sender),
1143 GST_plugins_a2s (&address));
1144 return GNUNET_SYSERR;
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 _("Received a PING message with validation bug from `%s'\n"),
1150 GNUNET_i2s (sender));
1156 addrend = NULL; /* make gcc happy */
1158 static struct GNUNET_CRYPTO_EddsaSignature no_address_signature;
1159 static struct GNUNET_TIME_Absolute no_address_signature_expiration;
1161 sig_cache = &no_address_signature;
1162 sig_cache_exp = &no_address_signature_expiration;
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1166 "I am `%s', sending PONG to peer `%s'\n",
1167 GNUNET_i2s_full (&GST_my_identity),
1168 GNUNET_i2s (sender));
1170 /* message with structure:
1171 * [TransportPongMessage][Transport name][Address] */
1173 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + len_address + len_plugin);
1175 htons (sizeof (struct TransportPongMessage) + len_address + len_plugin);
1176 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1177 pong->purpose.size =
1178 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1179 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1180 len_address + len_plugin);
1181 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
1182 memcpy (&pong->challenge, &ping->challenge, sizeof (ping->challenge));
1183 pong->addrlen = htonl (len_address + len_plugin);
1184 memcpy (&pong[1], addr, len_plugin); /* Copy transport plugin */
1185 if (len_address > 0)
1187 GNUNET_assert (NULL != addrend);
1188 memcpy (&((char *) &pong[1])[len_plugin], addrend, len_address);
1190 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value_us <
1191 PONG_SIGNATURE_LIFETIME.rel_value_us / 4)
1193 /* create / update cached sig */
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1195 "Creating PONG signature to indicate ownership.\n");
1196 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
1197 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1199 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key, &pong->purpose,
1202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1203 _("Failed to create PONG signature for peer `%s'\n"), GNUNET_i2s (sender));
1208 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1210 pong->signature = *sig_cache;
1212 GNUNET_assert (sender_address != NULL);
1214 /* first see if the session we got this PING from can be used to transmit
1215 * a response reliably */
1222 GNUNET_assert (NULL != papi->send);
1223 GNUNET_assert (NULL != papi->get_session);
1224 if (NULL == session)
1226 session = papi->get_session (papi->cls, sender_address);
1228 if (NULL == session)
1235 ret = papi->send (papi->cls, session,
1236 (const char *) pong, ntohs (pong->header.size),
1237 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
1240 GST_neighbours_notify_data_sent (sender,
1241 sender_address, session,
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "Transmitted PONG to `%s' via reliable mechanism\n",
1249 GNUNET_i2s (sender));
1251 GNUNET_STATISTICS_update (GST_stats,
1253 ("# PONGs unicast via reliable transport"), 1,
1259 /* no reliable method found, try transmission via all known addresses */
1260 GNUNET_STATISTICS_update (GST_stats,
1262 ("# PONGs multicast to all available addresses"), 1,
1264 GST_validation_get_addresses (sender,
1265 &multicast_pong, pong);
1272 * Context for the #validate_address_iterator() function
1274 struct ValidateAddressContext
1277 * Hash of the public key of the peer whose address is being validated.
1279 struct GNUNET_PeerIdentity pid;
1282 * Public key of the peer whose address is being validated.
1284 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
1290 * Iterator callback to go over all addresses and try to validate them
1291 * (unless blocked or already validated).
1293 * @param cls pointer to a `struct ValidateAddressContext *`
1294 * @param address the address
1295 * @param expiration expiration time
1296 * @return #GNUNET_OK (keep the address)
1299 validate_address_iterator (void *cls,
1300 const struct GNUNET_HELLO_Address *address,
1301 struct GNUNET_TIME_Absolute expiration)
1303 const struct ValidateAddressContext *vac = cls;
1304 struct GNUNET_TRANSPORT_PluginFunctions * papi;
1305 struct ValidationEntry *ve;
1306 struct GNUNET_TIME_Relative canonical_delay;
1308 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1311 "Skipping expired address from HELLO\n");
1312 return GNUNET_OK; /* expired */
1314 ve = find_validation_entry (&vac->public_key, address);
1316 papi = GST_plugins_find (ve->address->transport_name);
1319 /* This plugin is currently unvailable ... retry later */
1320 if (NULL == ve->revalidation_task)
1322 if (GNUNET_YES == ve->in_use)
1323 canonical_delay = CONNECTED_PING_FREQUENCY;
1324 else if (GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value_us > 0)
1325 canonical_delay = VALIDATED_PING_FREQUENCY;
1327 canonical_delay = UNVALIDATED_PING_KEEPALIVE;
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Plugin `%s' unavailable, validation process for peer `%s' delayed for %llu ms\n",
1331 ve->address->transport_name,
1332 GNUNET_i2s (&ve->address->peer),
1333 (long long unsigned) canonical_delay.rel_value_us / 1000);
1335 ve->revalidation_task = GNUNET_SCHEDULER_add_delayed (canonical_delay,
1336 &revalidate_address, ve);
1342 if (NULL == ve->revalidation_task)
1344 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1345 "Validation process started for fresh address `%s'\n",
1346 GST_plugins_a2s (ve->address));
1347 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1354 * Add the validated peer address to the HELLO.
1356 * @param cls the `struct ValidationEntry *` with the validated address
1357 * @param max space in @a buf
1358 * @param buf where to add the address
1359 * @return number of bytes written, #GNUNET_SYSERR to signal the
1360 * end of the iteration.
1363 add_valid_peer_address (void *cls,
1367 struct ValidationEntry *ve = cls;
1369 if (GNUNET_YES == ve->copied)
1370 return GNUNET_SYSERR; /* Done */
1371 ve->copied = GNUNET_YES;
1372 return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1377 * We've received a PONG. Check if it matches a pending PING and
1378 * mark the respective address as confirmed.
1380 * @param sender peer sending the PONG
1381 * @param hdr the PONG
1382 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1385 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1386 const struct GNUNET_MessageHeader *hdr)
1388 const struct TransportPongMessage *pong;
1389 struct ValidationEntry *ve;
1395 struct GNUNET_HELLO_Message *hello;
1396 struct GNUNET_HELLO_Address address;
1400 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1402 GNUNET_break_op (0);
1403 return GNUNET_SYSERR;
1405 GNUNET_STATISTICS_update (GST_stats,
1406 gettext_noop ("# PONG messages received"), 1,
1409 /* message with structure:
1410 * [TransportPongMessage][Transport name][Address] */
1412 pong = (const struct TransportPongMessage *) hdr;
1413 tname = (const char *) &pong[1];
1414 size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1415 addr = memchr (tname, '\0', size);
1418 GNUNET_break_op (0);
1419 return GNUNET_SYSERR;
1422 slen = strlen (tname) + 1;
1423 addrlen = size - slen;
1424 address.peer = *sender;
1425 address.address = addr;
1426 address.address_length = addrlen;
1427 address.transport_name = tname;
1428 address.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
1429 ve = find_validation_entry (NULL, &address);
1430 if ((NULL == ve) || (GNUNET_NO == ve->expecting_pong))
1432 GNUNET_STATISTICS_update (GST_stats,
1434 ("# PONGs dropped, no matching pending validation"),
1438 /* now check that PONG is well-formed */
1439 if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
1441 GNUNET_break_op (0);
1442 return GNUNET_SYSERR;
1445 GNUNET_TIME_absolute_get_remaining
1446 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value_us)
1448 GNUNET_STATISTICS_update (GST_stats,
1450 ("# PONGs dropped, signature expired"), 1,
1452 return GNUNET_SYSERR;
1455 sig_res = GNUNET_SYSERR;
1456 do_verify = GNUNET_YES;
1457 if (0 != GNUNET_TIME_absolute_get_remaining (ve->pong_sig_valid_until).rel_value_us)
1459 /* We have a cached and valid signature for this peer,
1460 * try to compare instead of verify */
1461 if (0 == memcmp (&ve->pong_sig_cache, &pong->signature, sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
1463 /* signatures are identical, we can skip verification */
1464 sig_res = GNUNET_OK;
1465 do_verify = GNUNET_NO;
1469 sig_res = GNUNET_SYSERR;
1470 /* signatures do not match, we have to verify */
1474 if (GNUNET_YES == do_verify)
1476 /* Do expensive verification */
1477 sig_res = GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1478 &pong->purpose, &pong->signature,
1480 if (sig_res == GNUNET_SYSERR)
1482 GNUNET_break_op (0);
1483 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1484 "Failed to verify: invalid signature on address `%s':%s from peer `%s'\n",
1486 GST_plugins_a2s (ve->address),
1487 GNUNET_i2s (sender));
1490 if (sig_res == GNUNET_SYSERR)
1492 GNUNET_break_op (0);
1493 return GNUNET_SYSERR;
1496 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1497 "Validation process successful for peer `%s' with plugin `%s' address `%s'\n",
1498 GNUNET_i2s (sender),
1500 GST_plugins_a2s (ve->address));
1501 GNUNET_STATISTICS_update (GST_stats,
1502 gettext_noop ("# validations succeeded"),
1505 /* validity achieved, remember it! */
1506 ve->expecting_pong = GNUNET_NO;
1507 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1508 ve->pong_sig_cache = pong->signature;
1509 ve->pong_sig_valid_until = GNUNET_TIME_absolute_ntoh (pong->expiration);
1510 ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1512 struct GNUNET_ATS_Information ats[2];
1514 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1515 ats[0].value = htonl ((uint32_t) ve->latency.rel_value_us);
1516 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1517 ats[1].value = htonl ((uint32_t) ve->network);
1518 if (GNUNET_YES == ve->known_to_ats)
1520 GST_ats_update_metrics (ve->address, NULL, ats, 2);
1524 ve->known_to_ats = GNUNET_YES;
1525 GST_ats_add_address (ve->address, NULL, ats, 2);
1528 if (validations_running > 0)
1530 validations_running --;
1531 GNUNET_STATISTICS_set (GST_stats,
1532 gettext_noop ("# validations running"),
1533 validations_running,
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 "Validation finished, %u validation processes running\n",
1537 validations_running);
1544 /* Notify about new validity */
1545 validation_entry_changed (ve, GNUNET_TRANSPORT_VS_UPDATE);
1547 /* build HELLO to store in PEERINFO */
1548 ve->copied = GNUNET_NO;
1549 hello = GNUNET_HELLO_create (&ve->public_key,
1550 &add_valid_peer_address, ve,
1552 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello, NULL, NULL);
1553 GNUNET_free (hello);
1559 * We've received a HELLO, check which addresses are new and trigger
1562 * @param hello the HELLO we received
1563 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1566 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1568 const struct GNUNET_HELLO_Message *hm =
1569 (const struct GNUNET_HELLO_Message *) hello;
1570 struct ValidateAddressContext vac;
1571 struct GNUNET_HELLO_Message *h;
1574 friend = GNUNET_HELLO_is_friend_only (hm);
1575 if ( ( (GNUNET_YES != friend) &&
1576 (GNUNET_NO != friend) ) ||
1577 (GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1578 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1580 /* malformed HELLO */
1581 GNUNET_break_op (0);
1582 return GNUNET_SYSERR;
1585 memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1587 /* Add peer identity without addresses to peerinfo service */
1588 h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL, friend);
1589 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1590 _("Validation received new %s message for peer `%s' with size %u\n"),
1592 GNUNET_i2s (&vac.pid),
1593 ntohs (hello->size));
1594 GNUNET_PEERINFO_add_peer (GST_peerinfo, h, NULL, NULL);
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1597 _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1598 GNUNET_i2s (&vac.pid));
1601 GNUNET_assert (NULL ==
1602 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1603 &validate_address_iterator,
1610 * Closure for #iterate_addresses().
1612 struct IteratorContext
1615 * Function to call on each address.
1617 GST_ValidationAddressCallback cb;
1620 * Closure for @e cb.
1628 * Call the callback in the closure for each validation entry.
1630 * @param cls the `struct IteratorContext`
1631 * @param key the peer's identity
1632 * @param value the `struct ValidationEntry`
1633 * @return #GNUNET_OK (continue to iterate)
1636 iterate_addresses (void *cls,
1637 const struct GNUNET_PeerIdentity *key,
1640 struct IteratorContext *ic = cls;
1641 struct ValidationEntry *ve = value;
1646 ve->revalidation_block,
1653 * Call the given function for each address for the given target.
1654 * Can either give a snapshot (synchronous API) or be continuous.
1656 * @param target peer information is requested for
1657 * @param cb function to call; will not be called after this function returns
1658 * @param cb_cls closure for @a cb
1661 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1662 GST_ValidationAddressCallback cb, void *cb_cls)
1664 struct IteratorContext ic;
1668 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
1670 &iterate_addresses, &ic);
1675 * Update if we are using an address for a connection actively right now.
1676 * Based on this, the validation module will measure latency for the
1677 * address more or less often.
1679 * @param address the address
1680 * @param session the session
1681 * @param in_use #GNUNET_YES if we are now using the address for a connection,
1682 * #GNUNET_NO if we are no longer using the address for a connection
1685 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1686 struct Session *session,
1689 struct ValidationEntry *ve;
1691 if (NULL != address)
1692 ve = find_validation_entry (NULL, address);
1694 ve = NULL; /* FIXME: lookup based on session... */
1697 /* this can happen for inbound connections (sender_address_len == 0); */
1700 if (ve->in_use == in_use)
1702 if (GNUNET_YES == in_use)
1704 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1705 "Error setting address in use for peer `%s' `%s' to USED\n",
1706 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
1708 if (GNUNET_NO == in_use)
1710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1711 "Error setting address in use for peer `%s' `%s' to NOT_USED\n",
1712 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
1716 GNUNET_break (ve->in_use != in_use); /* should be different... */
1717 ve->in_use = in_use;
1718 if (in_use == GNUNET_YES)
1720 /* from now on, higher frequeny, so reschedule now */
1721 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1722 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1728 * Query validation about the latest observed latency on a given
1731 * @param sender peer
1732 * @param address the address
1733 * @param session session
1734 * @return observed latency of the address, FOREVER if the address was
1735 * never successfully validated
1737 struct GNUNET_TIME_Relative
1738 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1739 const struct GNUNET_HELLO_Address *address,
1740 struct Session *session)
1742 struct ValidationEntry *ve;
1744 if (NULL == address)
1746 GNUNET_break (0); // FIXME: support having latency only with session...
1747 return GNUNET_TIME_UNIT_FOREVER_REL;
1749 ve = find_validation_entry (NULL, address);
1751 return GNUNET_TIME_UNIT_FOREVER_REL;
1756 * Closure for the validation_entries_iterate function.
1758 struct ValidationIteratorContext
1761 * Function to call on each validation entry
1763 GST_ValidationChangedCallback cb;
1766 * Closure for @e cb.
1773 validation_entries_iterate (void *cls,
1774 const struct GNUNET_PeerIdentity *key,
1777 struct ValidationIteratorContext *ic = cls;
1778 struct ValidationEntry *ve = value;
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Notifying about validation entry for peer `%s' address `%s' \n",
1781 GNUNET_i2s (&ve->pid), GST_plugins_a2s (ve->address));
1782 ic->cb (ic->cb_cls, &ve->pid, ve->address, ve->send_time,
1783 ve->valid_until, ve->next_validation, ve->state);
1789 * Iterate over all iteration entries
1791 * @param cb function to call
1792 * @param cb_cls closure for cb
1795 GST_validation_iterate (GST_ValidationChangedCallback cb,
1798 struct ValidationIteratorContext ic;
1800 if (NULL == validation_map)
1801 return; /* can happen during shutdown */
1804 GNUNET_CONTAINER_multipeermap_iterate (validation_map,
1805 &validation_entries_iterate,
1809 /* end of file gnunet-service-transport_validation.c */