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.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_ats_service.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_signatures.h"
39 * How long is a PONG signature valid? We'll recycle a signature until
40 * 1/4 of this time is remaining. PONGs should expire so that if our
41 * external addresses change an adversary cannot replay them indefinitely.
42 * OTOH, we don't want to spend too much time generating PONG signatures,
43 * so they must have some lifetime to reduce our CPU usage.
45 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
48 * After how long do we expire an address in a HELLO that we just
49 * validated? This value is also used for our own addresses when we
52 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
55 * How often do we allow PINGing an address that we have not yet
56 * validated? This also determines how long we track an address that
57 * we cannot validate (because after this time we can destroy the
60 #define UNVALIDATED_PING_KEEPALIVE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
63 * How often do we PING an address that we have successfully validated
64 * in the past but are not actively using? Should be (significantly)
65 * smaller than HELLO_ADDRESS_EXPIRATION.
67 #define VALIDATED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
70 * How often do we PING an address that we are currently using?
72 #define CONNECTED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
75 * How much delay is acceptable for sending the PING or PONG?
77 #define ACCEPTABLE_PING_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
80 * Size of the validation map hashmap.
82 #define VALIDATION_MAP_SIZE 256
85 * Priority to use for PINGs
87 #define PING_PRIORITY 2
90 * Priority to use for PONGs
92 #define PONG_PRIORITY 4
95 GNUNET_NETWORK_STRUCT_BEGIN
98 * Message used to ask a peer to validate receipt (to check an address
99 * from a HELLO). Followed by the address we are trying to validate,
100 * or an empty address if we are just sending a PING to confirm that a
101 * connection which the receiver (of the PING) initiated is still valid.
103 struct TransportPingMessage
107 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
109 struct GNUNET_MessageHeader header;
112 * Challenge code (to ensure fresh reply).
114 uint32_t challenge GNUNET_PACKED;
117 * Who is the intended recipient?
119 struct GNUNET_PeerIdentity target;
125 * Message used to validate a HELLO. The challenge is included in the
126 * confirmation to make matching of replies to requests possible. The
127 * signature signs our public key, an expiration time and our address.<p>
129 * This message is followed by our transport address that the PING tried
130 * to confirm (if we liked it). The address can be empty (zero bytes)
131 * if the PING had not address either (and we received the request via
132 * a connection that we initiated).
134 struct TransportPongMessage
138 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
140 struct GNUNET_MessageHeader header;
143 * Challenge code from PING (showing freshness). Not part of what
144 * is signed so that we can re-use signatures.
146 uint32_t challenge GNUNET_PACKED;
151 struct GNUNET_CRYPTO_EddsaSignature signature;
154 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
155 * plausible address for the signing peer.
157 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
160 * When does this signature expire?
162 struct GNUNET_TIME_AbsoluteNBO expiration;
165 * Size of address appended to this message (part of what is
166 * being signed, hence not redundant).
168 uint32_t addrlen GNUNET_PACKED;
171 GNUNET_NETWORK_STRUCT_END
174 * Information about an address under validation
176 struct ValidationEntry
182 struct GNUNET_HELLO_Address *address;
185 * Handle to the blacklist check (if we're currently in it).
187 struct GST_BlacklistCheck *bc;
190 * Public key of the peer.
192 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
195 * The identity of the peer. FIXME: duplicated (also in 'address')
197 struct GNUNET_PeerIdentity pid;
200 * Cached PONG signature
202 struct GNUNET_CRYPTO_EddsaSignature pong_sig_cache;
205 * ID of task that will clean up this entry if nothing happens.
207 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
210 * ID of task that will trigger address revalidation.
212 GNUNET_SCHEDULER_TaskIdentifier revalidation_task;
215 * At what time did we send the latest validation request (PING)?
217 struct GNUNET_TIME_Absolute send_time;
220 * Until when is this address valid?
221 * ZERO if it is not currently considered valid.
223 struct GNUNET_TIME_Absolute valid_until;
226 * Until when is the cached PONG signature valid?
227 * ZERO if it is not currently considered valid.
229 struct GNUNET_TIME_Absolute pong_sig_valid_until;
232 * How long until we can try to validate this address again?
233 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
234 * ZERO if the address is considered valid (no validation needed)
235 * otherwise a time in the future if we're currently denying re-validation
237 struct GNUNET_TIME_Absolute revalidation_block;
240 * Last observed latency for this address (round-trip), delay between
241 * last PING sent and PONG received; FOREVER if we never got a PONG.
243 struct GNUNET_TIME_Relative latency;
246 * Challenge number we used.
251 * When passing the address in 'add_valid_peer_address', did we
252 * copy the address to the HELLO yet?
257 * Are we currently using this address for a connection?
262 * Are we expecting a PONG message for this validation entry?
266 /* FIXME: DEBUGGING */
267 int last_line_set_to_no;
268 int last_line_set_to_yes;
270 enum GNUNET_ATS_Network_Type network;
275 * Context of currently active requests to peerinfo
276 * for validation of HELLOs.
278 struct CheckHelloValidatedContext
282 * This is a doubly-linked list.
284 struct CheckHelloValidatedContext *next;
287 * This is a doubly-linked list.
289 struct CheckHelloValidatedContext *prev;
292 * Hello that we are validating.
294 const struct GNUNET_HELLO_Message *hello;
300 * Head of linked list of HELLOs awaiting validation.
302 static struct CheckHelloValidatedContext *chvc_head;
305 * Tail of linked list of HELLOs awaiting validation
307 static struct CheckHelloValidatedContext *chvc_tail;
310 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
311 * of the given peer that we are currently validating, have validated
312 * or are blocked from re-validation for a while).
314 static struct GNUNET_CONTAINER_MultiPeerMap *validation_map;
317 * Context for peerinfo iteration.
319 static struct GNUNET_PEERINFO_NotifyContext *pnc;
323 * Minimum delay between to validations
325 static struct GNUNET_TIME_Relative validation_delay;
328 * Number of validations running
330 static unsigned int validations_running;
333 * Validition fast start threshold
335 static unsigned int validations_fast_start_threshold;
338 * When is next validation allowed
340 static struct GNUNET_TIME_Absolute validation_next;
343 * Context for the validation entry match function.
345 struct ValidationEntryMatchContext
348 * Where to store the result?
350 struct ValidationEntry *ve;
353 * Address we're interested in.
355 const struct GNUNET_HELLO_Address *address;
361 * Iterate over validation entries until a matching one is found.
363 * @param cls the 'struct ValidationEntryMatchContext'
364 * @param key peer identity (unused)
365 * @param value a 'struct ValidationEntry' to match
366 * @return GNUNET_YES if the entry does not match,
367 * GNUNET_NO if the entry does match
370 validation_entry_match (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
372 struct ValidationEntryMatchContext *vemc = cls;
373 struct ValidationEntry *ve = value;
375 if (0 == GNUNET_HELLO_address_cmp (ve->address, vemc->address))
385 * Iterate over validation entries and free them.
387 * @param cls (unused)
388 * @param key peer identity (unused)
389 * @param value a 'struct ValidationEntry' to clean up
390 * @return GNUNET_YES (continue to iterate)
393 cleanup_validation_entry (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
395 struct ValidationEntry *ve = value;
399 GST_blacklist_test_cancel (ve->bc);
402 GNUNET_break (GNUNET_OK ==
403 GNUNET_CONTAINER_multipeermap_remove (validation_map,
405 GNUNET_HELLO_address_free (ve->address);
406 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
408 GNUNET_SCHEDULER_cancel (ve->timeout_task);
409 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
411 if (GNUNET_SCHEDULER_NO_TASK != ve->revalidation_task)
413 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
414 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
416 if ((GNUNET_YES == ve->expecting_pong) &&
417 (validations_running > 0))
419 validations_running --;
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Validation finished, %u validation processes running\n",
422 validations_running);
430 * Address validation cleanup task. Assesses if the record is no
431 * longer valid and then possibly triggers its removal.
433 * @param cls the 'struct ValidationEntry'
434 * @param tc scheduler context (unused)
437 timeout_hello_validation (void *cls,
438 const struct GNUNET_SCHEDULER_TaskContext *tc)
440 struct ValidationEntry *ve = cls;
441 struct GNUNET_TIME_Absolute max;
442 struct GNUNET_TIME_Relative left;
444 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
445 max = GNUNET_TIME_absolute_max (ve->valid_until, ve->revalidation_block);
446 left = GNUNET_TIME_absolute_get_remaining (max);
447 if (left.rel_value_us > 0)
449 /* should wait a bit longer */
451 GNUNET_SCHEDULER_add_delayed (left, &timeout_hello_validation, ve);
454 GNUNET_STATISTICS_update (GST_stats,
455 gettext_noop ("# address records discarded"), 1,
457 cleanup_validation_entry (NULL, &ve->pid, ve);
462 * Function called with the result from blacklisting.
463 * Send a PING to the other peer if a communication is allowed.
465 * @param cls our 'struct ValidationEntry'
466 * @param pid identity of the other peer
467 * @param result GNUNET_OK if the connection is allowed, GNUNET_NO if not
470 transmit_ping_if_allowed (void *cls, const struct GNUNET_PeerIdentity *pid,
473 struct ValidationEntry *ve = cls;
474 struct TransportPingMessage ping;
475 struct GNUNET_TRANSPORT_PluginFunctions *papi;
476 struct GNUNET_TIME_Absolute next;
477 const struct GNUNET_MessageHeader *hello;
478 enum GNUNET_ATS_Network_Type network;
486 if (GNUNET_NO == result)
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist denies to send PING to `%s' %s %s\n",
489 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting plain PING to `%s' %s %s\n",
494 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
496 next = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), validation_delay);
497 if (next.abs_value_us > validation_next.abs_value_us)
498 validation_next = next; /* We're going to send a PING so delay next validation */
500 slen = strlen (ve->address->transport_name) + 1;
501 hello = GST_hello_get ();
502 hsize = ntohs (hello->size);
504 sizeof (struct TransportPingMessage) + ve->address->address_length +
508 htons (sizeof (struct TransportPingMessage) +
509 ve->address->address_length + slen);
510 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
511 ping.challenge = htonl (ve->challenge);
514 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
518 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
519 "HELLO", "PING", (unsigned int) tsize);
520 /* message too big (!?), get rid of HELLO */
523 sizeof (struct TransportPingMessage) + ve->address->address_length +
527 char message_buf[tsize];
529 /* build message with structure:
530 * [HELLO][TransportPingMessage][Transport name][Address] */
531 memcpy (message_buf, hello, hsize);
532 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
533 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
534 ve->address->transport_name, slen);
535 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
536 ve->address->address, ve->address->address_length);
537 papi = GST_plugins_find (ve->address->transport_name);
542 GNUNET_assert (papi->send != NULL);
543 GNUNET_assert (papi->get_session != NULL);
544 struct Session * session = papi->get_session(papi->cls, ve->address);
548 ret = papi->send (papi->cls, session,
550 PING_PRIORITY, ACCEPTABLE_PING_DELAY,
552 network = papi->get_network (ve->address, session);
553 if (GNUNET_ATS_NET_UNSPECIFIED == network)
555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
556 "Could not obtain a valid network for `%s' %s\n",
557 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
563 /* Could not get a valid session */
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not get a valid session for `%s' %s\n",
565 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
572 ve->send_time = GNUNET_TIME_absolute_get ();
573 GNUNET_STATISTICS_update (GST_stats,
575 ("# PING without HELLO messages sent"), 1,
578 ve->network = network;
579 ve->expecting_pong = GNUNET_YES;
580 validations_running ++;
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Validation started, %u validation processes running\n",
583 validations_running);
589 * Do address validation again to keep address valid.
591 * @param cls the 'struct ValidationEntry'
592 * @param tc scheduler context (unused)
595 revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
597 struct ValidationEntry *ve = cls;
598 struct GNUNET_TIME_Relative canonical_delay;
599 struct GNUNET_TIME_Relative delay;
600 struct GNUNET_TIME_Relative blocked_for;
601 struct GST_BlacklistCheck *bc;
604 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
605 delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
606 /* How long until we can possibly permit the next PING? */
609 GNUNET_YES) ? CONNECTED_PING_FREQUENCY
610 : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value_us >
611 0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE);
612 if (delay.rel_value_us > canonical_delay.rel_value_us * 2)
614 /* situation changed, recalculate delay */
615 delay = canonical_delay;
616 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
618 if (delay.rel_value_us > 0)
620 /* should wait a bit longer */
621 ve->revalidation_task =
622 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
625 blocked_for = GNUNET_TIME_absolute_get_remaining(validation_next);
626 if ((validations_running > validations_fast_start_threshold) &&
627 (blocked_for.rel_value_us > 0))
629 /* Validations are blocked, have to wait for blocked_for time */
630 ve->revalidation_task =
631 GNUNET_SCHEDULER_add_delayed (blocked_for, &revalidate_address, ve);
634 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
636 /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
638 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
639 canonical_delay.rel_value_us);
641 /* Debug code for mantis 0002726 */
642 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
643 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, rdelay).rel_value_us)
646 delay = canonical_delay;
650 delay = GNUNET_TIME_relative_add (canonical_delay,
651 GNUNET_TIME_relative_multiply
652 (GNUNET_TIME_UNIT_MICROSECONDS, rdelay));
654 /* End debug code for mantis 0002726*/
655 ve->revalidation_task =
656 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
658 /* start PINGing by checking blacklist */
659 GNUNET_STATISTICS_update (GST_stats,
660 gettext_noop ("# address revalidations started"), 1,
662 bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
663 &transmit_ping_if_allowed, ve);
665 ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already
671 * Find a ValidationEntry entry for the given neighbour that matches
672 * the given address and transport. If none exists, create one (but
673 * without starting any validation).
675 * @param public_key public key of the peer, NULL for unknown
676 * @param address address to find
677 * @return validation entry matching the given specifications, NULL
678 * if we don't have an existing entry and no public key was given
680 static struct ValidationEntry *
681 find_validation_entry (const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
682 const struct GNUNET_HELLO_Address *address)
684 struct ValidationEntryMatchContext vemc;
685 struct ValidationEntry *ve;
688 vemc.address = address;
689 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
691 &validation_entry_match, &vemc);
692 if (NULL != (ve = vemc.ve))
694 if (public_key == NULL)
696 ve = GNUNET_malloc (sizeof (struct ValidationEntry));
697 ve->in_use = GNUNET_SYSERR; /* not defined */
698 ve->last_line_set_to_no = 0;
699 ve->last_line_set_to_yes = 0;
700 ve->address = GNUNET_HELLO_address_copy (address);
701 ve->public_key = *public_key;
702 ve->pid = address->peer;
703 ve->pong_sig_valid_until = GNUNET_TIME_absolute_get_zero_();
704 memset (&ve->pong_sig_cache, '\0', sizeof (struct GNUNET_CRYPTO_EddsaSignature));
705 ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
707 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
709 GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
710 &timeout_hello_validation, ve);
711 GNUNET_CONTAINER_multipeermap_put (validation_map, &address->peer,
713 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
714 ve->expecting_pong = GNUNET_NO;
720 * Iterator which adds the given address to the set of validated
723 * @param cls original HELLO message
724 * @param address the address
725 * @param expiration expiration time
726 * @return GNUNET_OK (keep the address)
729 add_valid_address (void *cls, const struct GNUNET_HELLO_Address *address,
730 struct GNUNET_TIME_Absolute expiration)
732 const struct GNUNET_HELLO_Message *hello = cls;
733 struct ValidationEntry *ve;
734 struct GNUNET_PeerIdentity pid;
735 struct GNUNET_ATS_Information ats;
736 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
738 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
739 return GNUNET_OK; /* expired */
740 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
741 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
744 return GNUNET_OK; /* invalid HELLO !? */
746 if (0 == memcmp (&GST_my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
748 /* Peerinfo returned own identity, skip validation */
752 ve = find_validation_entry (&public_key, address);
753 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
755 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
756 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
758 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
759 ats.value = htonl (ve->network);
760 GNUNET_ATS_address_add (GST_ats, address, NULL, &ats, 1);
767 * Function called for any HELLO known to PEERINFO.
770 * @param peer id of the peer, NULL for last call
771 * @param hello hello message for the peer (can be NULL)
772 * @param err_msg error message
775 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
776 const struct GNUNET_HELLO_Message *hello,
779 GNUNET_assert (NULL != peer);
782 GNUNET_assert (NULL ==
783 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
790 * Start the validation subsystem.
792 * @param max_fds maximum number of fds to use
795 GST_validation_start (unsigned int max_fds)
798 * Initialization for validation throttling
800 * We have a maximum number max_fds of connections we can use for validation
801 * We monitor the number of validations in parallel and start to throttle it
802 * when doing to many validations in parallel:
803 * if (running validations < (max_fds / 2))
804 * - "fast start": run validation immediately
805 * - have delay of (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2)
806 * (300 sec / ~150 == ~2 sec.) between two validations
809 validation_next = GNUNET_TIME_absolute_get();
810 validation_delay.rel_value_us = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2);
811 validations_fast_start_threshold = (max_fds / 2);
812 validations_running = 0;
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Validation uses a fast start threshold of %u connections and a delay between of %s\n ",
814 validations_fast_start_threshold,
815 GNUNET_STRINGS_relative_time_to_string (validation_delay,
817 validation_map = GNUNET_CONTAINER_multipeermap_create (VALIDATION_MAP_SIZE,
819 pnc = GNUNET_PEERINFO_notify (GST_cfg, GNUNET_YES, &process_peerinfo_hello, NULL);
824 * Stop the validation subsystem.
827 GST_validation_stop ()
829 struct CheckHelloValidatedContext *chvc;
831 GNUNET_CONTAINER_multipeermap_iterate (validation_map,
832 &cleanup_validation_entry, NULL);
833 GNUNET_CONTAINER_multipeermap_destroy (validation_map);
834 validation_map = NULL;
835 while (NULL != (chvc = chvc_head))
837 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
840 GNUNET_PEERINFO_notify_cancel (pnc);
845 * Send the given PONG to the given address.
847 * @param cls the PONG message
848 * @param public_key public key for the peer, never NULL
849 * @param valid_until is ZERO if we never validated the address,
850 * otherwise a time up to when we consider it (or was) valid
851 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
852 * is ZERO if the address is considered valid (no validation needed)
853 * otherwise a time in the future if we're currently denying re-validation
854 * @param address target address
857 multicast_pong (void *cls,
858 const struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
859 struct GNUNET_TIME_Absolute valid_until,
860 struct GNUNET_TIME_Absolute validation_block,
861 const struct GNUNET_HELLO_Address *address)
863 struct TransportPongMessage *pong = cls;
864 struct GNUNET_TRANSPORT_PluginFunctions *papi;
866 papi = GST_plugins_find (address->transport_name);
870 GNUNET_assert (papi->send != NULL);
871 GNUNET_assert (papi->get_session != NULL);
873 struct Session * session = papi->get_session(papi->cls, address);
880 papi->send (papi->cls, session,
881 (const char *) pong, ntohs (pong->header.size),
882 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
888 * We've received a PING. If appropriate, generate a PONG.
890 * @param sender peer sending the PING
891 * @param hdr the PING
892 * @param sender_address the sender address as we got it
893 * @param session session we got the PING from
896 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
897 const struct GNUNET_MessageHeader *hdr,
898 const struct GNUNET_HELLO_Address *sender_address,
899 struct Session *session)
901 const struct TransportPingMessage *ping;
902 struct TransportPongMessage *pong;
903 struct GNUNET_TRANSPORT_PluginFunctions *papi;
904 struct GNUNET_CRYPTO_EddsaSignature *sig_cache;
905 struct GNUNET_TIME_Absolute *sig_cache_exp;
913 int buggy = GNUNET_NO;
914 struct GNUNET_HELLO_Address address;
916 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
921 ping = (const struct TransportPingMessage *) hdr;
923 memcmp (&ping->target, &GST_my_identity,
924 sizeof (struct GNUNET_PeerIdentity)))
926 GNUNET_STATISTICS_update (GST_stats,
928 ("# PING message for different peer received"), 1,
932 GNUNET_STATISTICS_update (GST_stats,
933 gettext_noop ("# PING messages received"), 1,
935 addr = (const char *) &ping[1];
936 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
937 /* peer wants to confirm that this is one of our addresses, this is what is
938 * used for address validation */
941 sig_cache_exp = NULL;
945 addrend = memchr (addr, '\0', alen);
952 slen = strlen (addr) + 1;
954 address.address = addrend;
955 address.address_length = alen;
956 address.transport_name = addr;
957 address.peer = GST_my_identity;
959 if (NULL == address.transport_name)
964 if (0 != strstr (address.transport_name, "_client"))
966 plugin_name = GNUNET_strdup (address.transport_name);
967 pos = strstr (plugin_name, "_client");
968 GNUNET_assert (NULL != pos);
969 GNUNET_snprintf (pos, strlen ("_server") + 1, "%s", "_server");
972 plugin_name = GNUNET_strdup (address.transport_name);
974 if (NULL == (papi = GST_plugins_find (plugin_name)))
976 /* we don't have the plugin for this address */
977 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Plugin `%s' not available, cannot confirm having this address \n",
979 GNUNET_free (plugin_name);
982 GNUNET_free (plugin_name);
983 if (GNUNET_OK != papi->check_address (papi->cls, addrend, alen))
985 GNUNET_STATISTICS_update (GST_stats,
987 ("# failed address checks during validation"), 1,
989 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Address `%s' is not one of my addresses, not confirming PING\n",
990 GST_plugins_a2s (&address));
995 GNUNET_STATISTICS_update (GST_stats,
997 ("# successful address checks during validation"), 1,
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Address `%s' is one of my addresses, confirming PING\n",
1000 GST_plugins_a2s (&address));
1003 if (GNUNET_YES != GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
1005 if (GNUNET_NO == buggy)
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n",
1009 GNUNET_i2s (sender),
1010 GST_plugins_a2s (&address));
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 _("Received a PING message with validation bug from `%s'\n"),
1017 GNUNET_i2s (sender));
1023 addrend = NULL; /* make gcc happy */
1025 static struct GNUNET_CRYPTO_EddsaSignature no_address_signature;
1026 static struct GNUNET_TIME_Absolute no_address_signature_expiration;
1028 sig_cache = &no_address_signature;
1029 sig_cache_exp = &no_address_signature_expiration;
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "I am `%s', sending PONG to peer `%s'\n",
1034 GNUNET_i2s_full (&GST_my_identity),
1035 GNUNET_i2s (sender));
1037 /* message with structure:
1038 * [TransportPongMessage][Transport name][Address] */
1040 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
1042 htons (sizeof (struct TransportPongMessage) + alen + slen);
1043 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1044 pong->purpose.size =
1045 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1046 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1048 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
1049 memcpy (&pong->challenge, &ping->challenge, sizeof (ping->challenge));
1050 pong->addrlen = htonl (alen + slen);
1051 memcpy (&pong[1], addr, slen); /* Copy transport plugin */
1054 GNUNET_assert (NULL != addrend);
1055 memcpy (&((char *) &pong[1])[slen], addrend, alen);
1057 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value_us <
1058 PONG_SIGNATURE_LIFETIME.rel_value_us / 4)
1060 /* create / update cached sig */
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062 "Creating PONG signature to indicate ownership.\n");
1063 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
1064 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1066 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key, &pong->purpose,
1069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1070 _("Failed to create PONG signature for peer `%s'\n"), GNUNET_i2s (sender));
1075 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1077 pong->signature = *sig_cache;
1079 GNUNET_assert (sender_address != NULL);
1081 /* first see if the session we got this PING from can be used to transmit
1082 * a response reliably */
1087 GNUNET_assert (papi->send != NULL);
1088 GNUNET_assert (papi->get_session != NULL);
1090 if (session == NULL)
1092 session = papi->get_session (papi->cls, sender_address);
1094 if (session == NULL)
1101 ret = papi->send (papi->cls, session,
1102 (const char *) pong, ntohs (pong->header.size),
1103 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Transmitted PONG to `%s' via reliable mechanism\n",
1111 GNUNET_i2s (sender));
1113 GNUNET_STATISTICS_update (GST_stats,
1115 ("# PONGs unicast via reliable transport"), 1,
1121 /* no reliable method found, try transmission via all known addresses */
1122 GNUNET_STATISTICS_update (GST_stats,
1124 ("# PONGs multicast to all available addresses"), 1,
1126 GST_validation_get_addresses (sender, &multicast_pong, pong);
1132 * Context for the 'validate_address' function
1134 struct ValidateAddressContext
1137 * Hash of the public key of the peer whose address is being validated.
1139 struct GNUNET_PeerIdentity pid;
1142 * Public key of the peer whose address is being validated.
1144 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
1149 * Iterator callback to go over all addresses and try to validate them
1150 * (unless blocked or already validated).
1152 * @param cls pointer to a 'struct ValidateAddressContext'
1153 * @param address the address
1154 * @param expiration expiration time
1155 * @return GNUNET_OK (keep the address)
1158 validate_address_iterator (void *cls,
1159 const struct GNUNET_HELLO_Address *address,
1160 struct GNUNET_TIME_Absolute expiration)
1162 const struct ValidateAddressContext *vac = cls;
1163 struct ValidationEntry *ve;
1165 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
1166 return GNUNET_OK; /* expired */
1167 ve = find_validation_entry (&vac->public_key, address);
1168 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
1169 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1175 * Add the validated peer address to the HELLO.
1177 * @param cls the 'struct ValidationEntry' with the validated address
1178 * @param max space in buf
1179 * @param buf where to add the address
1180 * @return number of bytes written, 0 to signal the
1181 * end of the iteration.
1184 add_valid_peer_address (void *cls, size_t max, void *buf)
1186 struct ValidationEntry *ve = cls;
1188 if (GNUNET_YES == ve->copied)
1189 return 0; /* terminate */
1190 ve->copied = GNUNET_YES;
1191 return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1196 * We've received a PONG. Check if it matches a pending PING and
1197 * mark the respective address as confirmed.
1199 * @param sender peer sending the PONG
1200 * @param hdr the PONG
1203 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1204 const struct GNUNET_MessageHeader *hdr)
1206 const struct TransportPongMessage *pong;
1207 struct ValidationEntry *ve;
1213 struct GNUNET_HELLO_Message *hello;
1214 struct GNUNET_HELLO_Address address;
1218 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1220 GNUNET_break_op (0);
1223 GNUNET_STATISTICS_update (GST_stats,
1224 gettext_noop ("# PONG messages received"), 1,
1227 /* message with structure:
1228 * [TransportPongMessage][Transport name][Address] */
1230 pong = (const struct TransportPongMessage *) hdr;
1231 tname = (const char *) &pong[1];
1232 size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1233 addr = memchr (tname, '\0', size);
1236 GNUNET_break_op (0);
1240 slen = strlen (tname) + 1;
1241 addrlen = size - slen;
1242 address.peer = *sender;
1243 address.address = addr;
1244 address.address_length = addrlen;
1245 address.transport_name = tname;
1246 ve = find_validation_entry (NULL, &address);
1247 if ((NULL == ve) || (GNUNET_NO == ve->expecting_pong))
1249 GNUNET_STATISTICS_update (GST_stats,
1251 ("# PONGs dropped, no matching pending validation"),
1255 /* now check that PONG is well-formed */
1256 if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
1258 GNUNET_break_op (0);
1261 if (GNUNET_TIME_absolute_get_remaining
1262 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value_us == 0)
1264 GNUNET_STATISTICS_update (GST_stats,
1266 ("# PONGs dropped, signature expired"), 1,
1271 sig_res = GNUNET_SYSERR;
1272 do_verify = GNUNET_YES;
1273 if (0 != GNUNET_TIME_absolute_get_remaining(ve->pong_sig_valid_until).rel_value_us)
1275 /* We have a cached and valid signature for this peer,
1276 * try to compare instead of verify */
1277 if (0 == memcmp (&ve->pong_sig_cache, &pong->signature, sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
1279 /* signatures are identical, we can skip verification */
1280 sig_res = GNUNET_OK;
1281 do_verify = GNUNET_NO;
1285 sig_res = GNUNET_SYSERR;
1286 /* signatures do not match, we have to verify */
1290 if (GNUNET_YES == do_verify)
1292 /* Do expensive verification */
1293 sig_res = GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1294 &pong->purpose, &pong->signature,
1296 if (sig_res == GNUNET_SYSERR)
1297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1298 "Failed to verify: invalid signature on address %s:%s from peer `%s'\n",
1299 tname, GST_plugins_a2s (ve->address),GNUNET_i2s (sender));
1302 if (sig_res == GNUNET_SYSERR)
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 "Address validated for peer `%s' with plugin `%s': `%s'\n",
1307 GNUNET_i2s (sender), tname, GST_plugins_a2s (ve->address));
1308 /* validity achieved, remember it! */
1309 ve->expecting_pong = GNUNET_NO;
1310 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1311 ve->pong_sig_cache = pong->signature;
1312 ve->pong_sig_valid_until = GNUNET_TIME_absolute_ntoh (pong->expiration);
1313 ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1315 struct GNUNET_ATS_Information ats[2];
1316 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1317 ats[0].value = htonl ((uint32_t) ve->latency.rel_value_us);
1318 ats[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1319 ats[1].value = htonl ((uint32_t) ve->network);
1320 GNUNET_ATS_address_add (GST_ats, ve->address, NULL, ats, 2);
1322 if (validations_running > 0)
1324 validations_running --;
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "Validation finished, %u validation processes running\n",
1327 validations_running);
1332 /* build HELLO to store in PEERINFO */
1333 ve->copied = GNUNET_NO;
1334 hello = GNUNET_HELLO_create (&ve->public_key, &add_valid_peer_address, ve, GNUNET_NO);
1335 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello, NULL, NULL);
1336 GNUNET_free (hello);
1341 * We've received a HELLO, check which addresses are new and trigger
1344 * @param hello the HELLO we received
1347 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1349 const struct GNUNET_HELLO_Message *hm =
1350 (const struct GNUNET_HELLO_Message *) hello;
1351 struct ValidateAddressContext vac;
1352 struct GNUNET_HELLO_Message *h;
1355 friend = GNUNET_HELLO_is_friend_only (hm);
1356 if (((GNUNET_YES != friend) && (GNUNET_NO != friend)) ||
1357 (GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1358 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1360 /* malformed HELLO */
1365 memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1367 /* Add peer identity without addresses to peerinfo service */
1368 h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL, friend);
1369 GNUNET_PEERINFO_add_peer (GST_peerinfo, h, NULL, NULL);
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1373 GNUNET_i2s (&vac.pid));
1376 GNUNET_assert (NULL ==
1377 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1378 &validate_address_iterator,
1384 * Closure for 'iterate_addresses'
1386 struct IteratorContext
1389 * Function to call on each address.
1391 GST_ValidationAddressCallback cb;
1402 * Call the callback in the closure for each validation entry.
1404 * @param cls the 'struct GST_ValidationIteratorContext'
1405 * @param key the peer's identity
1406 * @param value the 'struct ValidationEntry'
1407 * @return #GNUNET_OK (continue to iterate)
1410 iterate_addresses (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1412 struct IteratorContext *ic = cls;
1413 struct ValidationEntry *ve = value;
1415 ic->cb (ic->cb_cls, &ve->public_key, ve->valid_until, ve->revalidation_block,
1422 * Call the given function for each address for the given target.
1423 * Can either give a snapshot (synchronous API) or be continuous.
1425 * @param target peer information is requested for
1426 * @param cb function to call; will not be called after this function returns
1427 * @param cb_cls closure for 'cb'
1430 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1431 GST_ValidationAddressCallback cb, void *cb_cls)
1433 struct IteratorContext ic;
1437 GNUNET_CONTAINER_multipeermap_get_multiple (validation_map,
1439 &iterate_addresses, &ic);
1444 * Update if we are using an address for a connection actively right now.
1445 * Based on this, the validation module will measure latency for the
1446 * address more or less often.
1448 * @param address the address
1449 * @param session the session
1450 * @param in_use GNUNET_YES if we are now using the address for a connection,
1451 * GNUNET_NO if we are no longer using the address for a connection
1452 * @param line line of caller just for DEBUGGING!
1455 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1456 struct Session *session,
1460 struct ValidationEntry *ve;
1462 if (NULL != address)
1463 ve = find_validation_entry (NULL, address);
1465 ve = NULL; /* FIXME: lookup based on session... */
1468 /* this can happen for inbound connections (sender_address_len == 0); */
1471 if (ve->in_use == in_use)
1474 if (GNUNET_YES == in_use)
1476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1477 "Error setting address in use for peer `%s' `%s' to USED: set last time by %i, called now by %i\n",
1478 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1479 ve->last_line_set_to_yes, line);
1481 if (GNUNET_NO == in_use)
1483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1484 "Error setting address in use for peer `%s' `%s' to NOT_USED: set last time by %i, called now by %i\n",
1485 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1486 ve->last_line_set_to_no, line);
1490 if (GNUNET_YES == in_use)
1492 ve->last_line_set_to_yes = line;
1494 if (GNUNET_NO == in_use)
1496 ve->last_line_set_to_no = line;
1499 GNUNET_break (ve->in_use != in_use); /* should be different... */
1500 ve->in_use = in_use;
1501 if (in_use == GNUNET_YES)
1503 /* from now on, higher frequeny, so reschedule now */
1504 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1505 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1511 * Query validation about the latest observed latency on a given
1514 * @param sender peer
1515 * @param address the address
1516 * @param session session
1517 * @return observed latency of the address, FOREVER if the address was
1518 * never successfully validated
1520 struct GNUNET_TIME_Relative
1521 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1522 const struct GNUNET_HELLO_Address *address,
1523 struct Session *session)
1525 struct ValidationEntry *ve;
1527 if (NULL == address)
1529 GNUNET_break (0); // FIXME: support having latency only with session...
1530 return GNUNET_TIME_UNIT_FOREVER_REL;
1532 ve = find_validation_entry (NULL, address);
1534 return GNUNET_TIME_UNIT_FOREVER_REL;
1539 /* end of file gnunet-service-transport_validation.c */