2 This file is part of GNUnet.
3 (C) 2010,2011 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_EccSignature 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_EccPublicKeyBinaryEncoded 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_EccSignature 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;
273 * Context of currently active requests to peerinfo
274 * for validation of HELLOs.
276 struct CheckHelloValidatedContext
280 * This is a doubly-linked list.
282 struct CheckHelloValidatedContext *next;
285 * This is a doubly-linked list.
287 struct CheckHelloValidatedContext *prev;
290 * Hello that we are validating.
292 const struct GNUNET_HELLO_Message *hello;
298 * Head of linked list of HELLOs awaiting validation.
300 static struct CheckHelloValidatedContext *chvc_head;
303 * Tail of linked list of HELLOs awaiting validation
305 static struct CheckHelloValidatedContext *chvc_tail;
308 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
309 * of the given peer that we are currently validating, have validated
310 * or are blocked from re-validation for a while).
312 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
315 * Context for peerinfo iteration.
317 static struct GNUNET_PEERINFO_NotifyContext *pnc;
321 * Minimum delay between to validations
323 static struct GNUNET_TIME_Relative validation_delay;
326 * Number of validations running
328 static unsigned int validations_running;
331 * Validition fast start threshold
333 static unsigned int validations_fast_start_threshold;
336 * When is next validation allowed
338 static struct GNUNET_TIME_Absolute validation_next;
341 * Context for the validation entry match function.
343 struct ValidationEntryMatchContext
346 * Where to store the result?
348 struct ValidationEntry *ve;
351 * Address we're interested in.
353 const struct GNUNET_HELLO_Address *address;
359 * Iterate over validation entries until a matching one is found.
361 * @param cls the 'struct ValidationEntryMatchContext'
362 * @param key peer identity (unused)
363 * @param value a 'struct ValidationEntry' to match
364 * @return GNUNET_YES if the entry does not match,
365 * GNUNET_NO if the entry does match
368 validation_entry_match (void *cls, const struct GNUNET_HashCode * key, void *value)
370 struct ValidationEntryMatchContext *vemc = cls;
371 struct ValidationEntry *ve = value;
373 if (0 == GNUNET_HELLO_address_cmp (ve->address, vemc->address))
383 * Iterate over validation entries and free them.
385 * @param cls (unused)
386 * @param key peer identity (unused)
387 * @param value a 'struct ValidationEntry' to clean up
388 * @return GNUNET_YES (continue to iterate)
391 cleanup_validation_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
393 struct ValidationEntry *ve = value;
397 GST_blacklist_test_cancel (ve->bc);
400 GNUNET_break (GNUNET_OK ==
401 GNUNET_CONTAINER_multihashmap_remove (validation_map,
402 &ve->pid.hashPubKey, ve));
403 GNUNET_HELLO_address_free (ve->address);
404 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
406 GNUNET_SCHEDULER_cancel (ve->timeout_task);
407 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
409 if (GNUNET_SCHEDULER_NO_TASK != ve->revalidation_task)
411 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
412 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
414 if ((GNUNET_YES == ve->expecting_pong) &&
415 (validations_running > 0))
417 validations_running --;
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Validation finished, %u validation processes running\n",
420 validations_running);
428 * Address validation cleanup task. Assesses if the record is no
429 * longer valid and then possibly triggers its removal.
431 * @param cls the 'struct ValidationEntry'
432 * @param tc scheduler context (unused)
435 timeout_hello_validation (void *cls,
436 const struct GNUNET_SCHEDULER_TaskContext *tc)
438 struct ValidationEntry *ve = cls;
439 struct GNUNET_TIME_Absolute max;
440 struct GNUNET_TIME_Relative left;
442 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
443 max = GNUNET_TIME_absolute_max (ve->valid_until, ve->revalidation_block);
444 left = GNUNET_TIME_absolute_get_remaining (max);
445 if (left.rel_value > 0)
447 /* should wait a bit longer */
449 GNUNET_SCHEDULER_add_delayed (left, &timeout_hello_validation, ve);
452 GNUNET_STATISTICS_update (GST_stats,
453 gettext_noop ("# address records discarded"), 1,
455 cleanup_validation_entry (NULL, &ve->pid.hashPubKey, ve);
460 * Function called with the result from blacklisting.
461 * Send a PING to the other peer if a communication is allowed.
463 * @param cls our 'struct ValidationEntry'
464 * @param pid identity of the other peer
465 * @param result GNUNET_OK if the connection is allowed, GNUNET_NO if not
468 transmit_ping_if_allowed (void *cls, 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;
483 if (GNUNET_NO == result)
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist denies to send PING to `%s' %s %s\n",
486 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting plain PING to `%s' %s %s\n",
491 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
493 next = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), validation_delay);
494 if (next.abs_value > validation_next.abs_value)
495 validation_next = next; /* We're going to send a PING so delay next validation */
497 slen = strlen (ve->address->transport_name) + 1;
498 hello = GST_hello_get ();
499 hsize = ntohs (hello->size);
501 sizeof (struct TransportPingMessage) + ve->address->address_length +
505 htons (sizeof (struct TransportPingMessage) +
506 ve->address->address_length + slen);
507 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
508 ping.challenge = htonl (ve->challenge);
511 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
515 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
516 "HELLO", "PING", (unsigned int) tsize);
517 /* message too big (!?), get rid of HELLO */
520 sizeof (struct TransportPingMessage) + ve->address->address_length +
524 char message_buf[tsize];
526 /* build message with structure:
527 * [HELLO][TransportPingMessage][Transport name][Address] */
528 memcpy (message_buf, hello, hsize);
529 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
530 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
531 ve->address->transport_name, slen);
532 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
533 ve->address->address, ve->address->address_length);
534 papi = GST_plugins_find (ve->address->transport_name);
539 GNUNET_assert (papi->send != NULL);
540 GNUNET_assert (papi->get_session != NULL);
541 struct Session * session = papi->get_session(papi->cls, ve->address);
545 ret = papi->send (papi->cls, session,
547 PING_PRIORITY, ACCEPTABLE_PING_DELAY,
552 /* Could not get a valid session */
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not get a valid session for `%s' %s\n",
554 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
561 ve->send_time = GNUNET_TIME_absolute_get ();
562 GNUNET_STATISTICS_update (GST_stats,
564 ("# PING without HELLO messages sent"), 1,
566 ve->expecting_pong = GNUNET_YES;
567 validations_running ++;
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Validation started, %u validation processes running\n",
570 validations_running);
576 * Do address validation again to keep address valid.
578 * @param cls the 'struct ValidationEntry'
579 * @param tc scheduler context (unused)
582 revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
584 struct ValidationEntry *ve = cls;
585 struct GNUNET_TIME_Relative canonical_delay;
586 struct GNUNET_TIME_Relative delay;
587 struct GNUNET_TIME_Relative blocked_for;
588 struct GST_BlacklistCheck *bc;
591 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
592 delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
593 /* How long until we can possibly permit the next PING? */
596 GNUNET_YES) ? CONNECTED_PING_FREQUENCY
597 : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value >
598 0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE);
599 if (delay.rel_value > canonical_delay.rel_value * 2)
601 /* situation changed, recalculate delay */
602 delay = canonical_delay;
603 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
605 if (delay.rel_value > 0)
607 /* should wait a bit longer */
608 ve->revalidation_task =
609 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
612 blocked_for = GNUNET_TIME_absolute_get_remaining(validation_next);
613 if (validations_running > validations_fast_start_threshold)
614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Throttle!\n");
615 if ((validations_running > validations_fast_start_threshold) &&
616 (blocked_for.rel_value > 0))
618 /* Validations are blocked, have to wait for blocked_for time */
619 ve->revalidation_task =
620 GNUNET_SCHEDULER_add_delayed (blocked_for, &revalidate_address, ve);
623 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
625 /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
627 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
628 canonical_delay.rel_value);
630 /* Debug code for mantis 0002726*/
631 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
632 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rdelay).rel_value)
634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
635 "Revalidation interval for peer `%s' for is FOREVER (debug: rdelay: %llu, canonical delay %llu)\n",
636 GNUNET_i2s (&ve->pid),
637 (unsigned long long) delay.rel_value,
638 (unsigned long long) canonical_delay.rel_value);
639 delay = canonical_delay;
643 delay = GNUNET_TIME_relative_add (canonical_delay,
644 GNUNET_TIME_relative_multiply
645 (GNUNET_TIME_UNIT_MILLISECONDS, rdelay));
647 /* End debug code for mantis 0002726*/
648 ve->revalidation_task =
649 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
651 /* start PINGing by checking blacklist */
652 GNUNET_STATISTICS_update (GST_stats,
653 gettext_noop ("# address revalidations started"), 1,
655 bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
656 &transmit_ping_if_allowed, ve);
658 ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already
664 * Find a ValidationEntry entry for the given neighbour that matches
665 * the given address and transport. If none exists, create one (but
666 * without starting any validation).
668 * @param public_key public key of the peer, NULL for unknown
669 * @param address address to find
670 * @return validation entry matching the given specifications, NULL
671 * if we don't have an existing entry and no public key was given
673 static struct ValidationEntry *
674 find_validation_entry (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
675 *public_key, const struct GNUNET_HELLO_Address *address)
677 struct ValidationEntryMatchContext vemc;
678 struct ValidationEntry *ve;
681 vemc.address = address;
682 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
683 &address->peer.hashPubKey,
684 &validation_entry_match, &vemc);
685 if (NULL != (ve = vemc.ve))
687 if (public_key == NULL)
689 ve = GNUNET_malloc (sizeof (struct ValidationEntry));
690 ve->in_use = GNUNET_SYSERR; /* not defined */
691 ve->last_line_set_to_no = 0;
692 ve->last_line_set_to_yes = 0;
693 ve->address = GNUNET_HELLO_address_copy (address);
694 ve->public_key = *public_key;
695 ve->pid = address->peer;
696 ve->pong_sig_valid_until = GNUNET_TIME_absolute_get_zero_();
697 memset (&ve->pong_sig_cache, '\0', sizeof (struct GNUNET_CRYPTO_EccSignature));
698 ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
700 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
702 GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
703 &timeout_hello_validation, ve);
704 GNUNET_CONTAINER_multihashmap_put (validation_map, &address->peer.hashPubKey,
706 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
707 ve->expecting_pong = GNUNET_NO;
713 * Iterator which adds the given address to the set of validated
716 * @param cls original HELLO message
717 * @param address the address
718 * @param expiration expiration time
719 * @return GNUNET_OK (keep the address)
722 add_valid_address (void *cls, const struct GNUNET_HELLO_Address *address,
723 struct GNUNET_TIME_Absolute expiration)
725 const struct GNUNET_HELLO_Message *hello = cls;
726 struct ValidationEntry *ve;
727 struct GNUNET_PeerIdentity pid;
728 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
730 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
731 return GNUNET_OK; /* expired */
732 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
733 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
736 return GNUNET_OK; /* invalid HELLO !? */
738 if (0 == memcmp (&GST_my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
740 /* Peerinfo returned own identity, skip validation */
744 ve = find_validation_entry (&public_key, address);
745 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
747 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
748 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
749 GNUNET_ATS_address_add (GST_ats, address, NULL, NULL, 0);
755 * Function called for any HELLO known to PEERINFO.
758 * @param peer id of the peer, NULL for last call
759 * @param hello hello message for the peer (can be NULL)
760 * @param err_msg error message
763 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
764 const struct GNUNET_HELLO_Message *hello,
767 GNUNET_assert (NULL != peer);
770 GNUNET_assert (NULL ==
771 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
778 * Start the validation subsystem.
780 * @param max_fds maximum number of fds to use
783 GST_validation_start (unsigned int max_fds)
786 * Initialization for validation throttling
788 * We have a maximum number max_fds of connections we can use for validation
789 * We monitor the number of validations in parallel and start to throttle it
790 * when doing to many validations in parallel:
791 * if (running validations < (max_fds / 2))
792 * - "fast start": run validation immediately
793 * - have delay of (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value) / (max_fds / 2)
794 * (300 sec / ~150 == ~2 sec.) between two validations
797 validation_next = GNUNET_TIME_absolute_get();
798 validation_delay.rel_value = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value) / (max_fds / 2);
799 validations_fast_start_threshold = (max_fds / 2);
800 validations_running = 0;
801 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Validation uses a fast start threshold of %u connections and a delay between of %u ms\n ",
802 validations_fast_start_threshold, validation_delay.rel_value);
803 validation_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE,
805 pnc = GNUNET_PEERINFO_notify (GST_cfg, &process_peerinfo_hello, NULL);
810 * Stop the validation subsystem.
813 GST_validation_stop ()
815 struct CheckHelloValidatedContext *chvc;
817 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
818 &cleanup_validation_entry, NULL);
819 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
820 validation_map = NULL;
821 while (NULL != (chvc = chvc_head))
823 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
826 GNUNET_PEERINFO_notify_cancel (pnc);
831 * Send the given PONG to the given address.
833 * @param cls the PONG message
834 * @param public_key public key for the peer, never NULL
835 * @param valid_until is ZERO if we never validated the address,
836 * otherwise a time up to when we consider it (or was) valid
837 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
838 * is ZERO if the address is considered valid (no validation needed)
839 * otherwise a time in the future if we're currently denying re-validation
840 * @param address target address
843 multicast_pong (void *cls,
844 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
845 *public_key, struct GNUNET_TIME_Absolute valid_until,
846 struct GNUNET_TIME_Absolute validation_block,
847 const struct GNUNET_HELLO_Address *address)
849 struct TransportPongMessage *pong = cls;
850 struct GNUNET_TRANSPORT_PluginFunctions *papi;
852 papi = GST_plugins_find (address->transport_name);
856 GNUNET_assert (papi->send != NULL);
857 GNUNET_assert (papi->get_session != NULL);
859 struct Session * session = papi->get_session(papi->cls, address);
866 papi->send (papi->cls, session,
867 (const char *) pong, ntohs (pong->header.size),
868 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
874 * We've received a PING. If appropriate, generate a PONG.
876 * @param sender peer sending the PING
877 * @param hdr the PING
878 * @param sender_address the sender address as we got it
879 * @param session session we got the PING from
882 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
883 const struct GNUNET_MessageHeader *hdr,
884 const struct GNUNET_HELLO_Address *sender_address,
885 struct Session *session)
887 const struct TransportPingMessage *ping;
888 struct TransportPongMessage *pong;
889 struct GNUNET_TRANSPORT_PluginFunctions *papi;
890 struct GNUNET_CRYPTO_EccSignature *sig_cache;
891 struct GNUNET_TIME_Absolute *sig_cache_exp;
897 int buggy = GNUNET_NO;
898 struct GNUNET_HELLO_Address address;
900 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
905 ping = (const struct TransportPingMessage *) hdr;
907 memcmp (&ping->target, &GST_my_identity,
908 sizeof (struct GNUNET_PeerIdentity)))
910 GNUNET_STATISTICS_update (GST_stats,
912 ("# PING message for different peer received"), 1,
916 GNUNET_STATISTICS_update (GST_stats,
917 gettext_noop ("# PING messages received"), 1,
919 addr = (const char *) &ping[1];
920 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
921 /* peer wants to confirm that this is one of our addresses, this is what is
922 * used for address validation */
925 sig_cache_exp = NULL;
929 addrend = memchr (addr, '\0', alen);
936 slen = strlen (addr) + 1;
938 address.address = addrend;
939 address.address_length = alen;
940 address.transport_name = addr;
941 address.peer = GST_my_identity;
944 if (GNUNET_YES != GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
946 if (GNUNET_NO == buggy)
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n",
951 GST_plugins_a2s (&address));
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 _("Received a PING message with validation bug from `%s'\n"),
958 GNUNET_i2s (sender));
964 addrend = NULL; /* make gcc happy */
966 static struct GNUNET_CRYPTO_EccSignature no_address_signature;
967 static struct GNUNET_TIME_Absolute no_address_signature_expiration;
969 sig_cache = &no_address_signature;
970 sig_cache_exp = &no_address_signature_expiration;
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "I am `%s', sending PONG to peer `%s'\n",
975 GNUNET_h2s (&GST_my_identity.hashPubKey),
976 GNUNET_i2s (sender));
978 /* message with structure:
979 * [TransportPongMessage][Transport name][Address] */
981 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
983 htons (sizeof (struct TransportPongMessage) + alen + slen);
984 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
986 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
987 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
989 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
990 memcpy (&pong->challenge, &ping->challenge, sizeof (ping->challenge));
991 pong->addrlen = htonl (alen + slen);
992 memcpy (&pong[1], addr, slen); /* Copy transport plugin */
995 GNUNET_assert (NULL != addrend);
996 memcpy (&((char *) &pong[1])[slen], addrend, alen);
998 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value <
999 PONG_SIGNATURE_LIFETIME.rel_value / 4)
1001 /* create / update cached sig */
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Creating PONG signature to indicate ownership.\n");
1004 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
1005 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1006 GNUNET_assert (GNUNET_OK ==
1007 GNUNET_CRYPTO_ecc_sign (GST_my_private_key, &pong->purpose,
1012 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1014 pong->signature = *sig_cache;
1016 GNUNET_assert (sender_address != NULL);
1018 /* first see if the session we got this PING from can be used to transmit
1019 * a response reliably */
1020 papi = GST_plugins_find (sender_address->transport_name);
1025 GNUNET_assert (papi->send != NULL);
1026 GNUNET_assert (papi->get_session != NULL);
1028 if (session == NULL)
1030 session = papi->get_session (papi->cls, sender_address);
1032 if (session == NULL)
1039 ret = papi->send (papi->cls, session,
1040 (const char *) pong, ntohs (pong->header.size),
1041 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Transmitted PONG to `%s' via reliable mechanism\n",
1049 GNUNET_i2s (sender));
1051 GNUNET_STATISTICS_update (GST_stats,
1053 ("# PONGs unicast via reliable transport"), 1,
1059 /* no reliable method found, try transmission via all known addresses */
1060 GNUNET_STATISTICS_update (GST_stats,
1062 ("# PONGs multicast to all available addresses"), 1,
1064 GST_validation_get_addresses (sender, &multicast_pong, pong);
1070 * Context for the 'validate_address' function
1072 struct ValidateAddressContext
1075 * Hash of the public key of the peer whose address is being validated.
1077 struct GNUNET_PeerIdentity pid;
1080 * Public key of the peer whose address is being validated.
1082 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
1087 * Iterator callback to go over all addresses and try to validate them
1088 * (unless blocked or already validated).
1090 * @param cls pointer to a 'struct ValidateAddressContext'
1091 * @param address the address
1092 * @param expiration expiration time
1093 * @return GNUNET_OK (keep the address)
1096 validate_address_iterator (void *cls,
1097 const struct GNUNET_HELLO_Address *address,
1098 struct GNUNET_TIME_Absolute expiration)
1100 const struct ValidateAddressContext *vac = cls;
1101 struct ValidationEntry *ve;
1103 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
1104 return GNUNET_OK; /* expired */
1105 ve = find_validation_entry (&vac->public_key, address);
1106 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
1107 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1113 * Add the validated peer address to the HELLO.
1115 * @param cls the 'struct ValidationEntry' with the validated address
1116 * @param max space in buf
1117 * @param buf where to add the address
1118 * @return number of bytes written, 0 to signal the
1119 * end of the iteration.
1122 add_valid_peer_address (void *cls, size_t max, void *buf)
1124 struct ValidationEntry *ve = cls;
1126 if (GNUNET_YES == ve->copied)
1127 return 0; /* terminate */
1128 ve->copied = GNUNET_YES;
1129 return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1134 * We've received a PONG. Check if it matches a pending PING and
1135 * mark the respective address as confirmed.
1137 * @param sender peer sending the PONG
1138 * @param hdr the PONG
1141 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1142 const struct GNUNET_MessageHeader *hdr)
1144 const struct TransportPongMessage *pong;
1145 struct ValidationEntry *ve;
1151 struct GNUNET_HELLO_Message *hello;
1152 struct GNUNET_HELLO_Address address;
1155 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1157 GNUNET_break_op (0);
1160 GNUNET_STATISTICS_update (GST_stats,
1161 gettext_noop ("# PONG messages received"), 1,
1164 /* message with structure:
1165 * [TransportPongMessage][Transport name][Address] */
1167 pong = (const struct TransportPongMessage *) hdr;
1168 tname = (const char *) &pong[1];
1169 size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1170 addr = memchr (tname, '\0', size);
1173 GNUNET_break_op (0);
1177 slen = strlen (tname) + 1;
1178 addrlen = size - slen;
1179 address.peer = *sender;
1180 address.address = addr;
1181 address.address_length = addrlen;
1182 address.transport_name = tname;
1183 ve = find_validation_entry (NULL, &address);
1184 if ((NULL == ve) || (GNUNET_NO == ve->expecting_pong))
1186 GNUNET_STATISTICS_update (GST_stats,
1188 ("# PONGs dropped, no matching pending validation"),
1192 /* now check that PONG is well-formed */
1193 if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
1195 GNUNET_break_op (0);
1198 if (GNUNET_TIME_absolute_get_remaining
1199 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
1201 GNUNET_STATISTICS_update (GST_stats,
1203 ("# PONGs dropped, signature expired"), 1,
1208 sig_res = GNUNET_SYSERR;
1209 if (0 != GNUNET_TIME_absolute_get_remaining(ve->pong_sig_valid_until).rel_value)
1211 if (0 == memcmp (&ve->pong_sig_cache, &pong->signature, sizeof (struct GNUNET_CRYPTO_EccSignature)))
1212 sig_res = GNUNET_OK;
1214 sig_res = GNUNET_SYSERR;
1218 sig_res = GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1219 &pong->purpose, &pong->signature,
1223 if (sig_res == GNUNET_SYSERR)
1225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1226 "Invalid signature on address %s:%s from peer `%s'\n",
1227 tname, GST_plugins_a2s (ve->address),
1228 GNUNET_i2s (sender));
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Address validated for peer `%s' with plugin `%s': `%s'\n",
1233 GNUNET_i2s (sender), tname, GST_plugins_a2s (ve->address));
1234 /* validity achieved, remember it! */
1235 ve->expecting_pong = GNUNET_NO;
1236 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1237 ve->pong_sig_cache = pong->signature;
1238 ve->pong_sig_valid_until = GNUNET_TIME_absolute_ntoh (pong->expiration);
1239 ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1241 struct GNUNET_ATS_Information ats;
1242 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1243 ats.value = htonl ((uint32_t) ve->latency.rel_value);
1244 GNUNET_ATS_address_add (GST_ats, ve->address, NULL, &ats, 1);
1246 if (validations_running > 0)
1248 validations_running --;
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1250 "Validation finished, %u validation processes running\n",
1251 validations_running);
1256 /* build HELLO to store in PEERINFO */
1257 ve->copied = GNUNET_NO;
1258 hello = GNUNET_HELLO_create (&ve->public_key, &add_valid_peer_address, ve);
1259 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello, NULL, NULL);
1260 GNUNET_free (hello);
1265 * We've received a HELLO, check which addresses are new and trigger
1268 * @param hello the HELLO we received
1271 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1273 const struct GNUNET_HELLO_Message *hm =
1274 (const struct GNUNET_HELLO_Message *) hello;
1275 struct ValidateAddressContext vac;
1276 struct GNUNET_HELLO_Message *h;
1278 if ((GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1279 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1281 /* malformed HELLO */
1286 memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1288 /* Add peer identity without addresses to peerinfo service */
1289 h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL);
1290 GNUNET_PEERINFO_add_peer (GST_peerinfo, h, NULL, NULL);
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293 _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1294 GNUNET_i2s (&vac.pid));
1297 GNUNET_assert (NULL ==
1298 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1299 &validate_address_iterator,
1305 * Closure for 'iterate_addresses'
1307 struct IteratorContext
1310 * Function to call on each address.
1312 GST_ValidationAddressCallback cb;
1323 * Call the callback in the closure for each validation entry.
1325 * @param cls the 'struct GST_ValidationIteratorContext'
1326 * @param key the peer's identity
1327 * @param value the 'struct ValidationEntry'
1328 * @return GNUNET_OK (continue to iterate)
1331 iterate_addresses (void *cls, const struct GNUNET_HashCode * key, void *value)
1333 struct IteratorContext *ic = cls;
1334 struct ValidationEntry *ve = value;
1336 ic->cb (ic->cb_cls, &ve->public_key, ve->valid_until, ve->revalidation_block,
1343 * Call the given function for each address for the given target.
1344 * Can either give a snapshot (synchronous API) or be continuous.
1346 * @param target peer information is requested for
1347 * @param cb function to call; will not be called after this function returns
1348 * @param cb_cls closure for 'cb'
1351 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1352 GST_ValidationAddressCallback cb, void *cb_cls)
1354 struct IteratorContext ic;
1358 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
1359 &target->hashPubKey,
1360 &iterate_addresses, &ic);
1365 * Update if we are using an address for a connection actively right now.
1366 * Based on this, the validation module will measure latency for the
1367 * address more or less often.
1369 * @param address the address
1370 * @param session the session
1371 * @param in_use GNUNET_YES if we are now using the address for a connection,
1372 * GNUNET_NO if we are no longer using the address for a connection
1373 * @param line line of caller just for DEBUGGING!
1376 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1377 struct Session *session,
1381 struct ValidationEntry *ve;
1383 if (NULL != address)
1384 ve = find_validation_entry (NULL, address);
1386 ve = NULL; /* FIXME: lookup based on session... */
1389 /* this can happen for inbound connections (sender_address_len == 0); */
1392 if (ve->in_use == in_use)
1395 if (GNUNET_YES == in_use)
1397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1398 "Error setting address in use for peer `%s' `%s' to USED: set last time by %i, called now by %i\n",
1399 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1400 ve->last_line_set_to_yes, line);
1402 if (GNUNET_NO == in_use)
1404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1405 "Error setting address in use for peer `%s' `%s' to NOT_USED: set last time by %i, called now by %i\n",
1406 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1407 ve->last_line_set_to_no, line);
1411 if (GNUNET_YES == in_use)
1413 ve->last_line_set_to_yes = line;
1415 if (GNUNET_NO == in_use)
1417 ve->last_line_set_to_no = line;
1420 GNUNET_break (ve->in_use != in_use); /* should be different... */
1421 ve->in_use = in_use;
1422 if (in_use == GNUNET_YES)
1424 /* from now on, higher frequeny, so reschedule now */
1425 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1426 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1432 * Query validation about the latest observed latency on a given
1435 * @param sender peer
1436 * @param address the address
1437 * @param session session
1438 * @return observed latency of the address, FOREVER if the address was
1439 * never successfully validated
1441 struct GNUNET_TIME_Relative
1442 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1443 const struct GNUNET_HELLO_Address *address,
1444 struct Session *session)
1446 struct ValidationEntry *ve;
1448 if (NULL == address)
1450 GNUNET_break (0); // FIXME: support having latency only with session...
1451 return GNUNET_TIME_UNIT_FOREVER_REL;
1453 ve = find_validation_entry (NULL, address);
1455 return GNUNET_TIME_UNIT_FOREVER_REL;
1460 /* end of file gnunet-service-transport_validation.c */