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"
37 #define KEEP_093_COMPATIBILITY GNUNET_NO
40 * How long is a PONG signature valid? We'll recycle a signature until
41 * 1/4 of this time is remaining. PONGs should expire so that if our
42 * external addresses change an adversary cannot replay them indefinitely.
43 * OTOH, we don't want to spend too much time generating PONG signatures,
44 * so they must have some lifetime to reduce our CPU usage.
46 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
49 * After how long do we expire an address in a HELLO that we just
50 * validated? This value is also used for our own addresses when we
53 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
56 * How often do we allow PINGing an address that we have not yet
57 * validated? This also determines how long we track an address that
58 * we cannot validate (because after this time we can destroy the
61 #define UNVALIDATED_PING_KEEPALIVE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
64 * How often do we PING an address that we have successfully validated
65 * in the past but are not actively using? Should be (significantly)
66 * smaller than HELLO_ADDRESS_EXPIRATION.
68 #define VALIDATED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
71 * How often do we PING an address that we are currently using?
73 #define CONNECTED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
76 * How much delay is acceptable for sending the PING or PONG?
78 #define ACCEPTABLE_PING_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
81 * Size of the validation map hashmap.
83 #define VALIDATION_MAP_SIZE 256
86 * Priority to use for PINGs
88 #define PING_PRIORITY 2
91 * Priority to use for PONGs
93 #define PONG_PRIORITY 4
96 GNUNET_NETWORK_STRUCT_BEGIN
99 * Message used to ask a peer to validate receipt (to check an address
100 * from a HELLO). Followed by the address we are trying to validate,
101 * or an empty address if we are just sending a PING to confirm that a
102 * connection which the receiver (of the PING) initiated is still valid.
104 struct TransportPingMessage
108 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
110 struct GNUNET_MessageHeader header;
113 * Challenge code (to ensure fresh reply).
115 uint32_t challenge GNUNET_PACKED;
118 * Who is the intended recipient?
120 struct GNUNET_PeerIdentity target;
126 * Message used to validate a HELLO. The challenge is included in the
127 * confirmation to make matching of replies to requests possible. The
128 * signature signs our public key, an expiration time and our address.<p>
130 * This message is followed by our transport address that the PING tried
131 * to confirm (if we liked it). The address can be empty (zero bytes)
132 * if the PING had not address either (and we received the request via
133 * a connection that we initiated).
135 struct TransportPongMessage
139 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
141 struct GNUNET_MessageHeader header;
144 * Challenge code from PING (showing freshness). Not part of what
145 * is signed so that we can re-use signatures.
147 uint32_t challenge GNUNET_PACKED;
152 struct GNUNET_CRYPTO_EccSignature signature;
155 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
156 * plausible address for the signing peer.
158 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
161 * When does this signature expire?
163 struct GNUNET_TIME_AbsoluteNBO expiration;
166 * Size of address appended to this message (part of what is
167 * being signed, hence not redundant).
169 uint32_t addrlen GNUNET_PACKED;
172 GNUNET_NETWORK_STRUCT_END
175 * Information about an address under validation
177 struct ValidationEntry
183 struct GNUNET_HELLO_Address *address;
186 * Handle to the blacklist check (if we're currently in it).
188 struct GST_BlacklistCheck *bc;
191 * Public key of the peer.
193 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
196 * The identity of the peer. FIXME: duplicated (also in 'address')
198 struct GNUNET_PeerIdentity pid;
201 * ID of task that will clean up this entry if nothing happens.
203 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
206 * ID of task that will trigger address revalidation.
208 GNUNET_SCHEDULER_TaskIdentifier revalidation_task;
211 * At what time did we send the latest validation request (PING)?
213 struct GNUNET_TIME_Absolute send_time;
216 * Until when is this address valid?
217 * ZERO if it is not currently considered valid.
219 struct GNUNET_TIME_Absolute valid_until;
222 * How long until we can try to validate this address again?
223 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
224 * ZERO if the address is considered valid (no validation needed)
225 * otherwise a time in the future if we're currently denying re-validation
227 struct GNUNET_TIME_Absolute revalidation_block;
230 * Last observed latency for this address (round-trip), delay between
231 * last PING sent and PONG received; FOREVER if we never got a PONG.
233 struct GNUNET_TIME_Relative latency;
236 * Challenge number we used.
241 * When passing the address in 'add_valid_peer_address', did we
242 * copy the address to the HELLO yet?
247 * Are we currently using this address for a connection?
252 * Are we expecting a PONG message for this validation entry?
256 /* FIXME: DEBUGGING */
257 int last_line_set_to_no;
258 int last_line_set_to_yes;
263 * Context of currently active requests to peerinfo
264 * for validation of HELLOs.
266 struct CheckHelloValidatedContext
270 * This is a doubly-linked list.
272 struct CheckHelloValidatedContext *next;
275 * This is a doubly-linked list.
277 struct CheckHelloValidatedContext *prev;
280 * Hello that we are validating.
282 const struct GNUNET_HELLO_Message *hello;
288 * Head of linked list of HELLOs awaiting validation.
290 static struct CheckHelloValidatedContext *chvc_head;
293 * Tail of linked list of HELLOs awaiting validation
295 static struct CheckHelloValidatedContext *chvc_tail;
298 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
299 * of the given peer that we are currently validating, have validated
300 * or are blocked from re-validation for a while).
302 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
305 * Context for peerinfo iteration.
307 static struct GNUNET_PEERINFO_NotifyContext *pnc;
311 * Minimum delay between to validations
313 static struct GNUNET_TIME_Relative validation_delay;
316 * When is next validation allowed
318 static struct GNUNET_TIME_Absolute validation_next;
321 * Context for the validation entry match function.
323 struct ValidationEntryMatchContext
326 * Where to store the result?
328 struct ValidationEntry *ve;
331 * Address we're interested in.
333 const struct GNUNET_HELLO_Address *address;
339 * Iterate over validation entries until a matching one is found.
341 * @param cls the 'struct ValidationEntryMatchContext'
342 * @param key peer identity (unused)
343 * @param value a 'struct ValidationEntry' to match
344 * @return GNUNET_YES if the entry does not match,
345 * GNUNET_NO if the entry does match
348 validation_entry_match (void *cls, const struct GNUNET_HashCode * key, void *value)
350 struct ValidationEntryMatchContext *vemc = cls;
351 struct ValidationEntry *ve = value;
353 if (0 == GNUNET_HELLO_address_cmp (ve->address, vemc->address))
363 * Iterate over validation entries and free them.
365 * @param cls (unused)
366 * @param key peer identity (unused)
367 * @param value a 'struct ValidationEntry' to clean up
368 * @return GNUNET_YES (continue to iterate)
371 cleanup_validation_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
373 struct ValidationEntry *ve = value;
377 GST_blacklist_test_cancel (ve->bc);
380 GNUNET_break (GNUNET_OK ==
381 GNUNET_CONTAINER_multihashmap_remove (validation_map,
382 &ve->pid.hashPubKey, ve));
383 GNUNET_HELLO_address_free (ve->address);
384 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
386 GNUNET_SCHEDULER_cancel (ve->timeout_task);
387 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
389 if (GNUNET_SCHEDULER_NO_TASK != ve->revalidation_task)
391 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
392 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
400 * Address validation cleanup task. Assesses if the record is no
401 * longer valid and then possibly triggers its removal.
403 * @param cls the 'struct ValidationEntry'
404 * @param tc scheduler context (unused)
407 timeout_hello_validation (void *cls,
408 const struct GNUNET_SCHEDULER_TaskContext *tc)
410 struct ValidationEntry *ve = cls;
411 struct GNUNET_TIME_Absolute max;
412 struct GNUNET_TIME_Relative left;
414 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
415 max = GNUNET_TIME_absolute_max (ve->valid_until, ve->revalidation_block);
416 left = GNUNET_TIME_absolute_get_remaining (max);
417 if (left.rel_value > 0)
419 /* should wait a bit longer */
421 GNUNET_SCHEDULER_add_delayed (left, &timeout_hello_validation, ve);
424 GNUNET_STATISTICS_update (GST_stats,
425 gettext_noop ("# address records discarded"), 1,
427 cleanup_validation_entry (NULL, &ve->pid.hashPubKey, ve);
432 * Function called with the result from blacklisting.
433 * Send a PING to the other peer if a communication is allowed.
435 * @param cls our 'struct ValidationEntry'
436 * @param pid identity of the other peer
437 * @param result GNUNET_OK if the connection is allowed, GNUNET_NO if not
440 transmit_ping_if_allowed (void *cls, const struct GNUNET_PeerIdentity *pid,
443 struct ValidationEntry *ve = cls;
444 struct TransportPingMessage ping;
445 struct GNUNET_TRANSPORT_PluginFunctions *papi;
446 struct GNUNET_TIME_Absolute next;
447 const struct GNUNET_MessageHeader *hello;
455 if (GNUNET_NO == result)
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist denies to send PING to `%s' %s %s\n",
458 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting plain PING to `%s' %s %s\n",
463 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
465 next = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), validation_delay);
466 if (next.abs_value > validation_next.abs_value)
467 validation_next = next; /* We're going to send a PING so delay next validation */
469 slen = strlen (ve->address->transport_name) + 1;
470 hello = GST_hello_get ();
471 hsize = ntohs (hello->size);
473 sizeof (struct TransportPingMessage) + ve->address->address_length +
477 htons (sizeof (struct TransportPingMessage) +
478 ve->address->address_length + slen);
479 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
480 ping.challenge = htonl (ve->challenge);
483 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
487 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
488 "HELLO", "PING", (unsigned int) tsize);
489 /* message too big (!?), get rid of HELLO */
492 sizeof (struct TransportPingMessage) + ve->address->address_length +
496 char message_buf[tsize];
498 /* build message with structure:
499 * [HELLO][TransportPingMessage][Transport name][Address] */
500 memcpy (message_buf, hello, hsize);
501 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
502 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
503 ve->address->transport_name, slen);
504 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
505 ve->address->address, ve->address->address_length);
506 papi = GST_plugins_find (ve->address->transport_name);
511 GNUNET_assert (papi->send != NULL);
512 GNUNET_assert (papi->get_session != NULL);
513 struct Session * session = papi->get_session(papi->cls, ve->address);
517 ret = papi->send (papi->cls, session,
519 PING_PRIORITY, ACCEPTABLE_PING_DELAY,
524 /* Could not get a valid session */
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not get a valid session for `%s' %s\n",
526 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
533 ve->send_time = GNUNET_TIME_absolute_get ();
534 GNUNET_STATISTICS_update (GST_stats,
536 ("# PING without HELLO messages sent"), 1,
538 ve->expecting_pong = GNUNET_YES;
544 * Do address validation again to keep address valid.
546 * @param cls the 'struct ValidationEntry'
547 * @param tc scheduler context (unused)
550 revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
552 struct ValidationEntry *ve = cls;
553 struct GNUNET_TIME_Relative canonical_delay;
554 struct GNUNET_TIME_Relative delay;
555 struct GNUNET_TIME_Relative blocked_for;
556 struct GST_BlacklistCheck *bc;
559 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
560 delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
561 /* How long until we can possibly permit the next PING? */
564 GNUNET_YES) ? CONNECTED_PING_FREQUENCY
565 : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value >
566 0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE);
567 if (delay.rel_value > canonical_delay.rel_value * 2)
569 /* situation changed, recalculate delay */
570 delay = canonical_delay;
571 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
573 if (delay.rel_value > 0)
575 /* should wait a bit longer */
576 ve->revalidation_task =
577 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
580 blocked_for = GNUNET_TIME_absolute_get_remaining(validation_next);
581 if ((blocked_for.rel_value) > 0)
583 /* Validations are blocked, have to wait for blocked_for ms */
584 ve->revalidation_task =
585 GNUNET_SCHEDULER_add_delayed (blocked_for, &revalidate_address, ve);
588 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
590 /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
592 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
593 canonical_delay.rel_value);
595 /* Debug code for mantis 0002726*/
596 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
597 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rdelay).rel_value)
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600 "Revalidation interval for peer `%s' for is FOREVER (debug: rdelay: %llu, canonical delay %llu)\n",
601 GNUNET_i2s (&ve->pid),
602 (unsigned long long) delay.rel_value,
603 (unsigned long long) canonical_delay.rel_value);
604 delay = canonical_delay;
608 delay = GNUNET_TIME_relative_add (canonical_delay,
609 GNUNET_TIME_relative_multiply
610 (GNUNET_TIME_UNIT_MILLISECONDS, rdelay));
612 /* End debug code for mantis 0002726*/
613 ve->revalidation_task =
614 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
616 /* start PINGing by checking blacklist */
617 GNUNET_STATISTICS_update (GST_stats,
618 gettext_noop ("# address revalidations started"), 1,
620 bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
621 &transmit_ping_if_allowed, ve);
623 ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already
629 * Find a ValidationEntry entry for the given neighbour that matches
630 * the given address and transport. If none exists, create one (but
631 * without starting any validation).
633 * @param public_key public key of the peer, NULL for unknown
634 * @param address address to find
635 * @return validation entry matching the given specifications, NULL
636 * if we don't have an existing entry and no public key was given
638 static struct ValidationEntry *
639 find_validation_entry (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
640 *public_key, const struct GNUNET_HELLO_Address *address)
642 struct ValidationEntryMatchContext vemc;
643 struct ValidationEntry *ve;
646 vemc.address = address;
647 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
648 &address->peer.hashPubKey,
649 &validation_entry_match, &vemc);
650 if (NULL != (ve = vemc.ve))
652 if (public_key == NULL)
654 ve = GNUNET_malloc (sizeof (struct ValidationEntry));
655 ve->in_use = GNUNET_SYSERR; /* not defined */
656 ve->last_line_set_to_no = 0;
657 ve->last_line_set_to_yes = 0;
658 ve->address = GNUNET_HELLO_address_copy (address);
659 ve->public_key = *public_key;
660 ve->pid = address->peer;
661 ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
663 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
665 GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
666 &timeout_hello_validation, ve);
667 GNUNET_CONTAINER_multihashmap_put (validation_map, &address->peer.hashPubKey,
669 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
670 ve->expecting_pong = GNUNET_NO;
676 * Iterator which adds the given address to the set of validated
679 * @param cls original HELLO message
680 * @param address the address
681 * @param expiration expiration time
682 * @return GNUNET_OK (keep the address)
685 add_valid_address (void *cls, const struct GNUNET_HELLO_Address *address,
686 struct GNUNET_TIME_Absolute expiration)
688 const struct GNUNET_HELLO_Message *hello = cls;
689 struct ValidationEntry *ve;
690 struct GNUNET_PeerIdentity pid;
691 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
693 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
694 return GNUNET_OK; /* expired */
695 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
696 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
699 return GNUNET_OK; /* invalid HELLO !? */
701 if (0 == memcmp (&GST_my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
703 /* Peerinfo returned own identity, skip validation */
707 ve = find_validation_entry (&public_key, address);
708 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
710 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
711 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
712 GNUNET_ATS_address_add (GST_ats, address, NULL, NULL, 0);
718 * Function called for any HELLO known to PEERINFO.
721 * @param peer id of the peer, NULL for last call
722 * @param hello hello message for the peer (can be NULL)
723 * @param err_msg error message
726 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
727 const struct GNUNET_HELLO_Message *hello,
730 GNUNET_assert (NULL != peer);
733 GNUNET_assert (NULL ==
734 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
741 * Start the validation subsystem.
743 * @param max_fds maximum number of fds to use
746 GST_validation_start (unsigned int max_fds)
748 validation_next = GNUNET_TIME_absolute_get();
749 validation_delay.rel_value = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value) / max_fds;
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Delay between validations: %u ms\n ", validation_delay.rel_value);
751 validation_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE,
753 pnc = GNUNET_PEERINFO_notify (GST_cfg, &process_peerinfo_hello, NULL);
758 * Stop the validation subsystem.
761 GST_validation_stop ()
763 struct CheckHelloValidatedContext *chvc;
765 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
766 &cleanup_validation_entry, NULL);
767 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
768 validation_map = NULL;
769 while (NULL != (chvc = chvc_head))
771 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
774 GNUNET_PEERINFO_notify_cancel (pnc);
779 * Send the given PONG to the given address.
781 * @param cls the PONG message
782 * @param public_key public key for the peer, never NULL
783 * @param valid_until is ZERO if we never validated the address,
784 * otherwise a time up to when we consider it (or was) valid
785 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
786 * is ZERO if the address is considered valid (no validation needed)
787 * otherwise a time in the future if we're currently denying re-validation
788 * @param address target address
791 multicast_pong (void *cls,
792 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
793 *public_key, struct GNUNET_TIME_Absolute valid_until,
794 struct GNUNET_TIME_Absolute validation_block,
795 const struct GNUNET_HELLO_Address *address)
797 struct TransportPongMessage *pong = cls;
798 struct GNUNET_TRANSPORT_PluginFunctions *papi;
800 papi = GST_plugins_find (address->transport_name);
804 GNUNET_assert (papi->send != NULL);
805 GNUNET_assert (papi->get_session != NULL);
807 struct Session * session = papi->get_session(papi->cls, address);
814 papi->send (papi->cls, session,
815 (const char *) pong, ntohs (pong->header.size),
816 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
822 * We've received a PING. If appropriate, generate a PONG.
824 * @param sender peer sending the PING
825 * @param hdr the PING
826 * @param sender_address the sender address as we got it
827 * @param session session we got the PING from
830 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
831 const struct GNUNET_MessageHeader *hdr,
832 const struct GNUNET_HELLO_Address *sender_address,
833 struct Session *session)
835 const struct TransportPingMessage *ping;
836 struct TransportPongMessage *pong;
837 struct GNUNET_TRANSPORT_PluginFunctions *papi;
838 struct GNUNET_CRYPTO_EccSignature *sig_cache;
839 struct GNUNET_TIME_Absolute *sig_cache_exp;
845 int buggy = GNUNET_NO;
846 struct GNUNET_HELLO_Address address;
848 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
853 ping = (const struct TransportPingMessage *) hdr;
855 memcmp (&ping->target, &GST_my_identity,
856 sizeof (struct GNUNET_PeerIdentity)))
858 GNUNET_STATISTICS_update (GST_stats,
860 ("# PING message for different peer received"), 1,
864 GNUNET_STATISTICS_update (GST_stats,
865 gettext_noop ("# PING messages received"), 1,
867 addr = (const char *) &ping[1];
868 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
869 /* peer wants to confirm that this is one of our addresses, this is what is
870 * used for address validation */
873 sig_cache_exp = NULL;
877 addrend = memchr (addr, '\0', alen);
884 slen = strlen (addr) + 1;
886 address.address = addrend;
887 address.address_length = alen;
888 address.transport_name = addr;
889 address.peer = GST_my_identity;
892 if (GNUNET_YES != GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
894 #if KEEP_093_COMPATIBILITY
895 int idsize = sizeof (GST_my_identity);
898 if (0 == memcmp (address.address, &GST_my_identity, alen))
901 else if (alen <= (idsize + strlen (address.transport_name)))
903 char *achar = (char *) &address.address;
904 if ((0 == memcmp (address.address, &GST_my_identity, idsize)) &&
905 (0 == memcmp (&achar[idsize], address.transport_name, alen - idsize)))
910 /* Not predicatable */
914 if (GNUNET_NO == buggy)
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n",
919 GST_plugins_a2s (&address));
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
925 _("Received a PING message with validation bug from `%s'\n"),
926 GNUNET_i2s (sender));
932 addrend = NULL; /* make gcc happy */
934 static struct GNUNET_CRYPTO_EccSignature no_address_signature;
935 static struct GNUNET_TIME_Absolute no_address_signature_expiration;
937 sig_cache = &no_address_signature;
938 sig_cache_exp = &no_address_signature_expiration;
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "I am `%s', sending PONG to peer `%s'\n",
943 GNUNET_h2s (&GST_my_identity.hashPubKey),
944 GNUNET_i2s (sender));
946 /* message with structure:
947 * [TransportPongMessage][Transport name][Address] */
949 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
951 htons (sizeof (struct TransportPongMessage) + alen + slen);
952 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
954 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
955 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
957 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
958 memcpy (&pong->challenge, &ping->challenge, sizeof (ping->challenge));
959 pong->addrlen = htonl (alen + slen);
960 memcpy (&pong[1], addr, slen); /* Copy transport plugin */
961 #if KEEP_093_COMPATIBILITY
962 if (GNUNET_YES == buggy)
964 int idsize = sizeof (GST_my_identity);
967 memcpy (&((char *) &pong[1])[slen], &GST_my_identity, alen);
969 else if (alen <= (idsize + strlen (address.transport_name) + 1))
971 memcpy (&((char *) &pong[1])[slen], &GST_my_identity, idsize);
972 memcpy (&((char *) &pong[1])[slen + idsize], address.transport_name, alen-idsize);
976 /* If this would happen, we would have a inconsistent PING we cannot reproduce */
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating buggy PONG signature to indicate ownership.\n");
982 pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
983 GNUNET_assert (GNUNET_OK ==
984 GNUNET_CRYPTO_ecc_sign (GST_my_private_key, &pong->purpose,
992 GNUNET_assert (NULL != addrend);
993 memcpy (&((char *) &pong[1])[slen], addrend, alen);
995 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value <
996 PONG_SIGNATURE_LIFETIME.rel_value / 4)
998 /* create / update cached sig */
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1000 "Creating PONG signature to indicate ownership.\n");
1001 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
1002 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1003 GNUNET_assert (GNUNET_OK ==
1004 GNUNET_CRYPTO_ecc_sign (GST_my_private_key, &pong->purpose,
1009 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
1011 pong->signature = *sig_cache;
1013 #if KEEP_093_COMPATIBILITY
1017 GNUNET_assert (sender_address != NULL);
1019 /* first see if the session we got this PING from can be used to transmit
1020 * a response reliably */
1021 papi = GST_plugins_find (sender_address->transport_name);
1026 GNUNET_assert (papi->send != NULL);
1027 GNUNET_assert (papi->get_session != NULL);
1029 if (session == NULL)
1031 session = papi->get_session (papi->cls, sender_address);
1033 if (session == NULL)
1040 ret = papi->send (papi->cls, session,
1041 (const char *) pong, ntohs (pong->header.size),
1042 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1049 "Transmitted PONG to `%s' via reliable mechanism\n",
1050 GNUNET_i2s (sender));
1052 GNUNET_STATISTICS_update (GST_stats,
1054 ("# PONGs unicast via reliable transport"), 1,
1060 /* no reliable method found, try transmission via all known addresses */
1061 GNUNET_STATISTICS_update (GST_stats,
1063 ("# PONGs multicast to all available addresses"), 1,
1065 GST_validation_get_addresses (sender, &multicast_pong, pong);
1071 * Context for the 'validate_address' function
1073 struct ValidateAddressContext
1076 * Hash of the public key of the peer whose address is being validated.
1078 struct GNUNET_PeerIdentity pid;
1081 * Public key of the peer whose address is being validated.
1083 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
1088 * Iterator callback to go over all addresses and try to validate them
1089 * (unless blocked or already validated).
1091 * @param cls pointer to a 'struct ValidateAddressContext'
1092 * @param address the address
1093 * @param expiration expiration time
1094 * @return GNUNET_OK (keep the address)
1097 validate_address_iterator (void *cls,
1098 const struct GNUNET_HELLO_Address *address,
1099 struct GNUNET_TIME_Absolute expiration)
1101 const struct ValidateAddressContext *vac = cls;
1102 struct ValidationEntry *ve;
1104 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
1105 return GNUNET_OK; /* expired */
1106 ve = find_validation_entry (&vac->public_key, address);
1107 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
1108 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1114 * Add the validated peer address to the HELLO.
1116 * @param cls the 'struct ValidationEntry' with the validated address
1117 * @param max space in buf
1118 * @param buf where to add the address
1119 * @return number of bytes written, 0 to signal the
1120 * end of the iteration.
1123 add_valid_peer_address (void *cls, size_t max, void *buf)
1125 struct ValidationEntry *ve = cls;
1127 if (GNUNET_YES == ve->copied)
1128 return 0; /* terminate */
1129 ve->copied = GNUNET_YES;
1130 return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1135 * We've received a PONG. Check if it matches a pending PING and
1136 * mark the respective address as confirmed.
1138 * @param sender peer sending the PONG
1139 * @param hdr the PONG
1142 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1143 const struct GNUNET_MessageHeader *hdr)
1145 const struct TransportPongMessage *pong;
1146 struct ValidationEntry *ve;
1152 struct GNUNET_HELLO_Message *hello;
1153 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) || (ve->expecting_pong == GNUNET_NO))
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);
1200 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1201 &pong->purpose, &pong->signature,
1204 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1205 "Invalid signature on address %s:%s from peer `%s'\n",
1206 tname, GST_plugins_a2s (ve->address),
1207 GNUNET_i2s (sender));
1211 if (GNUNET_TIME_absolute_get_remaining
1212 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
1214 GNUNET_STATISTICS_update (GST_stats,
1216 ("# PONGs dropped, signature expired"), 1,
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1221 "Address validated for peer `%s' with plugin `%s': `%s'\n",
1222 GNUNET_i2s (sender), tname, GST_plugins_a2s (ve->address));
1223 /* validity achieved, remember it! */
1224 ve->expecting_pong = GNUNET_NO;
1225 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1226 ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1228 struct GNUNET_ATS_Information ats;
1229 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1230 ats.value = htonl ((uint32_t) ve->latency.rel_value);
1231 GNUNET_ATS_address_add (GST_ats, ve->address, NULL, &ats, 1);
1233 /* build HELLO to store in PEERINFO */
1234 ve->copied = GNUNET_NO;
1235 hello = GNUNET_HELLO_create (&ve->public_key, &add_valid_peer_address, ve);
1236 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello, NULL, NULL);
1237 GNUNET_free (hello);
1242 * We've received a HELLO, check which addresses are new and trigger
1245 * @param hello the HELLO we received
1248 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1250 const struct GNUNET_HELLO_Message *hm =
1251 (const struct GNUNET_HELLO_Message *) hello;
1252 struct ValidateAddressContext vac;
1253 struct GNUNET_HELLO_Message *h;
1255 if ((GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1256 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1258 /* malformed HELLO */
1263 memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1265 /* Add peer identity without addresses to peerinfo service */
1266 h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL);
1267 GNUNET_PEERINFO_add_peer (GST_peerinfo, h, NULL, NULL);
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1271 GNUNET_i2s (&vac.pid));
1274 GNUNET_assert (NULL ==
1275 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1276 &validate_address_iterator,
1282 * Closure for 'iterate_addresses'
1284 struct IteratorContext
1287 * Function to call on each address.
1289 GST_ValidationAddressCallback cb;
1300 * Call the callback in the closure for each validation entry.
1302 * @param cls the 'struct GST_ValidationIteratorContext'
1303 * @param key the peer's identity
1304 * @param value the 'struct ValidationEntry'
1305 * @return GNUNET_OK (continue to iterate)
1308 iterate_addresses (void *cls, const struct GNUNET_HashCode * key, void *value)
1310 struct IteratorContext *ic = cls;
1311 struct ValidationEntry *ve = value;
1313 ic->cb (ic->cb_cls, &ve->public_key, ve->valid_until, ve->revalidation_block,
1320 * Call the given function for each address for the given target.
1321 * Can either give a snapshot (synchronous API) or be continuous.
1323 * @param target peer information is requested for
1324 * @param cb function to call; will not be called after this function returns
1325 * @param cb_cls closure for 'cb'
1328 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1329 GST_ValidationAddressCallback cb, void *cb_cls)
1331 struct IteratorContext ic;
1335 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
1336 &target->hashPubKey,
1337 &iterate_addresses, &ic);
1342 * Update if we are using an address for a connection actively right now.
1343 * Based on this, the validation module will measure latency for the
1344 * address more or less often.
1346 * @param address the address
1347 * @param session the session
1348 * @param in_use GNUNET_YES if we are now using the address for a connection,
1349 * GNUNET_NO if we are no longer using the address for a connection
1350 * @param line line of caller just for DEBUGGING!
1353 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1354 struct Session *session,
1358 struct ValidationEntry *ve;
1360 if (NULL != address)
1361 ve = find_validation_entry (NULL, address);
1363 ve = NULL; /* FIXME: lookup based on session... */
1366 /* this can happen for inbound connections (sender_address_len == 0); */
1369 if (ve->in_use == in_use)
1372 if (GNUNET_YES == in_use)
1374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1375 "Error setting address in use for peer `%s' `%s' to USED: set last time by %i, called now by %i\n",
1376 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1377 ve->last_line_set_to_yes, line);
1379 if (GNUNET_NO == in_use)
1381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1382 "Error setting address in use for peer `%s' `%s' to NOT_USED: set last time by %i, called now by %i\n",
1383 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1384 ve->last_line_set_to_no, line);
1388 if (GNUNET_YES == in_use)
1390 ve->last_line_set_to_yes = line;
1392 if (GNUNET_NO == in_use)
1394 ve->last_line_set_to_no = line;
1397 GNUNET_break (ve->in_use != in_use); /* should be different... */
1398 ve->in_use = in_use;
1399 if (in_use == GNUNET_YES)
1401 /* from now on, higher frequeny, so reschedule now */
1402 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1403 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1409 * Query validation about the latest observed latency on a given
1412 * @param sender peer
1413 * @param address the address
1414 * @param session session
1415 * @return observed latency of the address, FOREVER if the address was
1416 * never successfully validated
1418 struct GNUNET_TIME_Relative
1419 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1420 const struct GNUNET_HELLO_Address *address,
1421 struct Session *session)
1423 struct ValidationEntry *ve;
1425 if (NULL == address)
1427 GNUNET_break (0); // FIXME: support having latency only with session...
1428 return GNUNET_TIME_UNIT_FOREVER_REL;
1430 ve = find_validation_entry (NULL, address);
1432 return GNUNET_TIME_UNIT_FOREVER_REL;
1437 /* end of file gnunet-service-transport_validation.c */