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 * ID of task that will clean up this entry if nothing happens.
202 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
205 * ID of task that will trigger address revalidation.
207 GNUNET_SCHEDULER_TaskIdentifier revalidation_task;
210 * At what time did we send the latest validation request (PING)?
212 struct GNUNET_TIME_Absolute send_time;
215 * Until when is this address valid?
216 * ZERO if it is not currently considered valid.
218 struct GNUNET_TIME_Absolute valid_until;
221 * How long until we can try to validate this address again?
222 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
223 * ZERO if the address is considered valid (no validation needed)
224 * otherwise a time in the future if we're currently denying re-validation
226 struct GNUNET_TIME_Absolute revalidation_block;
229 * Last observed latency for this address (round-trip), delay between
230 * last PING sent and PONG received; FOREVER if we never got a PONG.
232 struct GNUNET_TIME_Relative latency;
235 * Challenge number we used.
240 * When passing the address in 'add_valid_peer_address', did we
241 * copy the address to the HELLO yet?
246 * Are we currently using this address for a connection?
251 * Are we expecting a PONG message for this validation entry?
255 /* FIXME: DEBUGGING */
256 int last_line_set_to_no;
257 int last_line_set_to_yes;
262 * Context of currently active requests to peerinfo
263 * for validation of HELLOs.
265 struct CheckHelloValidatedContext
269 * This is a doubly-linked list.
271 struct CheckHelloValidatedContext *next;
274 * This is a doubly-linked list.
276 struct CheckHelloValidatedContext *prev;
279 * Hello that we are validating.
281 const struct GNUNET_HELLO_Message *hello;
287 * Head of linked list of HELLOs awaiting validation.
289 static struct CheckHelloValidatedContext *chvc_head;
292 * Tail of linked list of HELLOs awaiting validation
294 static struct CheckHelloValidatedContext *chvc_tail;
297 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
298 * of the given peer that we are currently validating, have validated
299 * or are blocked from re-validation for a while).
301 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
304 * Context for peerinfo iteration.
306 static struct GNUNET_PEERINFO_NotifyContext *pnc;
310 * Minimum delay between to validations
312 static struct GNUNET_TIME_Relative validation_delay;
315 * When is next validation allowed
317 static struct GNUNET_TIME_Absolute validation_next;
320 * Context for the validation entry match function.
322 struct ValidationEntryMatchContext
325 * Where to store the result?
327 struct ValidationEntry *ve;
330 * Address we're interested in.
332 const struct GNUNET_HELLO_Address *address;
338 * Iterate over validation entries until a matching one is found.
340 * @param cls the 'struct ValidationEntryMatchContext'
341 * @param key peer identity (unused)
342 * @param value a 'struct ValidationEntry' to match
343 * @return GNUNET_YES if the entry does not match,
344 * GNUNET_NO if the entry does match
347 validation_entry_match (void *cls, const struct GNUNET_HashCode * key, void *value)
349 struct ValidationEntryMatchContext *vemc = cls;
350 struct ValidationEntry *ve = value;
352 if (0 == GNUNET_HELLO_address_cmp (ve->address, vemc->address))
362 * Iterate over validation entries and free them.
364 * @param cls (unused)
365 * @param key peer identity (unused)
366 * @param value a 'struct ValidationEntry' to clean up
367 * @return GNUNET_YES (continue to iterate)
370 cleanup_validation_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
372 struct ValidationEntry *ve = value;
376 GST_blacklist_test_cancel (ve->bc);
379 GNUNET_break (GNUNET_OK ==
380 GNUNET_CONTAINER_multihashmap_remove (validation_map,
381 &ve->pid.hashPubKey, ve));
382 GNUNET_HELLO_address_free (ve->address);
383 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
385 GNUNET_SCHEDULER_cancel (ve->timeout_task);
386 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
388 if (GNUNET_SCHEDULER_NO_TASK != ve->revalidation_task)
390 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
391 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
399 * Address validation cleanup task. Assesses if the record is no
400 * longer valid and then possibly triggers its removal.
402 * @param cls the 'struct ValidationEntry'
403 * @param tc scheduler context (unused)
406 timeout_hello_validation (void *cls,
407 const struct GNUNET_SCHEDULER_TaskContext *tc)
409 struct ValidationEntry *ve = cls;
410 struct GNUNET_TIME_Absolute max;
411 struct GNUNET_TIME_Relative left;
413 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
414 max = GNUNET_TIME_absolute_max (ve->valid_until, ve->revalidation_block);
415 left = GNUNET_TIME_absolute_get_remaining (max);
416 if (left.rel_value > 0)
418 /* should wait a bit longer */
420 GNUNET_SCHEDULER_add_delayed (left, &timeout_hello_validation, ve);
423 GNUNET_STATISTICS_update (GST_stats,
424 gettext_noop ("# address records discarded"), 1,
426 cleanup_validation_entry (NULL, &ve->pid.hashPubKey, ve);
431 * Function called with the result from blacklisting.
432 * Send a PING to the other peer if a communication is allowed.
434 * @param cls our 'struct ValidationEntry'
435 * @param pid identity of the other peer
436 * @param result GNUNET_OK if the connection is allowed, GNUNET_NO if not
439 transmit_ping_if_allowed (void *cls, const struct GNUNET_PeerIdentity *pid,
442 struct ValidationEntry *ve = cls;
443 struct TransportPingMessage ping;
444 struct GNUNET_TRANSPORT_PluginFunctions *papi;
445 struct GNUNET_TIME_Absolute next;
446 const struct GNUNET_MessageHeader *hello;
454 if (GNUNET_NO == result)
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist denies to send PING to `%s' %s %s\n",
457 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting plain PING to `%s' %s %s\n",
462 GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
464 next = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), validation_delay);
465 if (next.abs_value > validation_next.abs_value)
466 validation_next = next; /* We're going to send a PING so delay next validation */
468 slen = strlen (ve->address->transport_name) + 1;
469 hello = GST_hello_get ();
470 hsize = ntohs (hello->size);
472 sizeof (struct TransportPingMessage) + ve->address->address_length +
476 htons (sizeof (struct TransportPingMessage) +
477 ve->address->address_length + slen);
478 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
479 ping.challenge = htonl (ve->challenge);
482 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
486 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
487 "HELLO", "PING", (unsigned int) tsize);
488 /* message too big (!?), get rid of HELLO */
491 sizeof (struct TransportPingMessage) + ve->address->address_length +
495 char message_buf[tsize];
497 /* build message with structure:
498 * [HELLO][TransportPingMessage][Transport name][Address] */
499 memcpy (message_buf, hello, hsize);
500 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
501 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
502 ve->address->transport_name, slen);
503 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
504 ve->address->address, ve->address->address_length);
505 papi = GST_plugins_find (ve->address->transport_name);
510 GNUNET_assert (papi->send != NULL);
511 GNUNET_assert (papi->get_session != NULL);
512 struct Session * session = papi->get_session(papi->cls, ve->address);
516 ret = papi->send (papi->cls, session,
518 PING_PRIORITY, ACCEPTABLE_PING_DELAY,
523 /* Could not get a valid session */
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not get a valid session for `%s' %s\n",
525 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
532 ve->send_time = GNUNET_TIME_absolute_get ();
533 GNUNET_STATISTICS_update (GST_stats,
535 ("# PING without HELLO messages sent"), 1,
537 ve->expecting_pong = GNUNET_YES;
543 * Do address validation again to keep address valid.
545 * @param cls the 'struct ValidationEntry'
546 * @param tc scheduler context (unused)
549 revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
551 struct ValidationEntry *ve = cls;
552 struct GNUNET_TIME_Relative canonical_delay;
553 struct GNUNET_TIME_Relative delay;
554 struct GNUNET_TIME_Relative blocked_for;
555 struct GST_BlacklistCheck *bc;
558 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
559 delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
560 /* How long until we can possibly permit the next PING? */
563 GNUNET_YES) ? CONNECTED_PING_FREQUENCY
564 : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value >
565 0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE);
566 if (delay.rel_value > canonical_delay.rel_value * 2)
568 /* situation changed, recalculate delay */
569 delay = canonical_delay;
570 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
572 if (delay.rel_value > 0)
574 /* should wait a bit longer */
575 ve->revalidation_task =
576 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
579 blocked_for = GNUNET_TIME_absolute_get_remaining(validation_next);
580 if ((blocked_for.rel_value) > 0)
582 /* Validations are blocked, have to wait for blocked_for time */
583 ve->revalidation_task =
584 GNUNET_SCHEDULER_add_delayed (blocked_for, &revalidate_address, ve);
587 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
589 /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
591 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
592 canonical_delay.rel_value);
594 /* Debug code for mantis 0002726*/
595 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
596 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rdelay).rel_value)
598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
599 "Revalidation interval for peer `%s' for is FOREVER (debug: rdelay: %llu, canonical delay %llu)\n",
600 GNUNET_i2s (&ve->pid),
601 (unsigned long long) delay.rel_value,
602 (unsigned long long) canonical_delay.rel_value);
603 delay = canonical_delay;
607 delay = GNUNET_TIME_relative_add (canonical_delay,
608 GNUNET_TIME_relative_multiply
609 (GNUNET_TIME_UNIT_MILLISECONDS, rdelay));
611 /* End debug code for mantis 0002726*/
612 ve->revalidation_task =
613 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
615 /* start PINGing by checking blacklist */
616 GNUNET_STATISTICS_update (GST_stats,
617 gettext_noop ("# address revalidations started"), 1,
619 bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
620 &transmit_ping_if_allowed, ve);
622 ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already
628 * Find a ValidationEntry entry for the given neighbour that matches
629 * the given address and transport. If none exists, create one (but
630 * without starting any validation).
632 * @param public_key public key of the peer, NULL for unknown
633 * @param address address to find
634 * @return validation entry matching the given specifications, NULL
635 * if we don't have an existing entry and no public key was given
637 static struct ValidationEntry *
638 find_validation_entry (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
639 *public_key, const struct GNUNET_HELLO_Address *address)
641 struct ValidationEntryMatchContext vemc;
642 struct ValidationEntry *ve;
645 vemc.address = address;
646 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
647 &address->peer.hashPubKey,
648 &validation_entry_match, &vemc);
649 if (NULL != (ve = vemc.ve))
651 if (public_key == NULL)
653 ve = GNUNET_malloc (sizeof (struct ValidationEntry));
654 ve->in_use = GNUNET_SYSERR; /* not defined */
655 ve->last_line_set_to_no = 0;
656 ve->last_line_set_to_yes = 0;
657 ve->address = GNUNET_HELLO_address_copy (address);
658 ve->public_key = *public_key;
659 ve->pid = address->peer;
660 ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
662 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
664 GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
665 &timeout_hello_validation, ve);
666 GNUNET_CONTAINER_multihashmap_put (validation_map, &address->peer.hashPubKey,
668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
669 ve->expecting_pong = GNUNET_NO;
675 * Iterator which adds the given address to the set of validated
678 * @param cls original HELLO message
679 * @param address the address
680 * @param expiration expiration time
681 * @return GNUNET_OK (keep the address)
684 add_valid_address (void *cls, const struct GNUNET_HELLO_Address *address,
685 struct GNUNET_TIME_Absolute expiration)
687 const struct GNUNET_HELLO_Message *hello = cls;
688 struct ValidationEntry *ve;
689 struct GNUNET_PeerIdentity pid;
690 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
692 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
693 return GNUNET_OK; /* expired */
694 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
695 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
698 return GNUNET_OK; /* invalid HELLO !? */
700 if (0 == memcmp (&GST_my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
702 /* Peerinfo returned own identity, skip validation */
706 ve = find_validation_entry (&public_key, address);
707 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
709 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
710 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
711 GNUNET_ATS_address_add (GST_ats, address, NULL, NULL, 0);
717 * Function called for any HELLO known to PEERINFO.
720 * @param peer id of the peer, NULL for last call
721 * @param hello hello message for the peer (can be NULL)
722 * @param err_msg error message
725 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
726 const struct GNUNET_HELLO_Message *hello,
729 GNUNET_assert (NULL != peer);
732 GNUNET_assert (NULL ==
733 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
740 * Start the validation subsystem.
742 * @param max_fds maximum number of fds to use
745 GST_validation_start (unsigned int max_fds)
747 validation_next = GNUNET_TIME_absolute_get();
748 validation_delay.rel_value = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value) / max_fds;
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Delay between validations: %u ms\n ", validation_delay.rel_value);
750 validation_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE,
752 pnc = GNUNET_PEERINFO_notify (GST_cfg, &process_peerinfo_hello, NULL);
757 * Stop the validation subsystem.
760 GST_validation_stop ()
762 struct CheckHelloValidatedContext *chvc;
764 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
765 &cleanup_validation_entry, NULL);
766 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
767 validation_map = NULL;
768 while (NULL != (chvc = chvc_head))
770 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
773 GNUNET_PEERINFO_notify_cancel (pnc);
778 * Send the given PONG to the given address.
780 * @param cls the PONG message
781 * @param public_key public key for the peer, never NULL
782 * @param valid_until is ZERO if we never validated the address,
783 * otherwise a time up to when we consider it (or was) valid
784 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
785 * is ZERO if the address is considered valid (no validation needed)
786 * otherwise a time in the future if we're currently denying re-validation
787 * @param address target address
790 multicast_pong (void *cls,
791 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
792 *public_key, struct GNUNET_TIME_Absolute valid_until,
793 struct GNUNET_TIME_Absolute validation_block,
794 const struct GNUNET_HELLO_Address *address)
796 struct TransportPongMessage *pong = cls;
797 struct GNUNET_TRANSPORT_PluginFunctions *papi;
799 papi = GST_plugins_find (address->transport_name);
803 GNUNET_assert (papi->send != NULL);
804 GNUNET_assert (papi->get_session != NULL);
806 struct Session * session = papi->get_session(papi->cls, address);
813 papi->send (papi->cls, session,
814 (const char *) pong, ntohs (pong->header.size),
815 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
821 * We've received a PING. If appropriate, generate a PONG.
823 * @param sender peer sending the PING
824 * @param hdr the PING
825 * @param sender_address the sender address as we got it
826 * @param session session we got the PING from
829 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
830 const struct GNUNET_MessageHeader *hdr,
831 const struct GNUNET_HELLO_Address *sender_address,
832 struct Session *session)
834 const struct TransportPingMessage *ping;
835 struct TransportPongMessage *pong;
836 struct GNUNET_TRANSPORT_PluginFunctions *papi;
837 struct GNUNET_CRYPTO_EccSignature *sig_cache;
838 struct GNUNET_TIME_Absolute *sig_cache_exp;
844 int buggy = GNUNET_NO;
845 struct GNUNET_HELLO_Address address;
847 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
852 ping = (const struct TransportPingMessage *) hdr;
854 memcmp (&ping->target, &GST_my_identity,
855 sizeof (struct GNUNET_PeerIdentity)))
857 GNUNET_STATISTICS_update (GST_stats,
859 ("# PING message for different peer received"), 1,
863 GNUNET_STATISTICS_update (GST_stats,
864 gettext_noop ("# PING messages received"), 1,
866 addr = (const char *) &ping[1];
867 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
868 /* peer wants to confirm that this is one of our addresses, this is what is
869 * used for address validation */
872 sig_cache_exp = NULL;
876 addrend = memchr (addr, '\0', alen);
883 slen = strlen (addr) + 1;
885 address.address = addrend;
886 address.address_length = alen;
887 address.transport_name = addr;
888 address.peer = GST_my_identity;
891 if (GNUNET_YES != GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
893 if (GNUNET_NO == buggy)
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n",
898 GST_plugins_a2s (&address));
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 _("Received a PING message with validation bug from `%s'\n"),
905 GNUNET_i2s (sender));
911 addrend = NULL; /* make gcc happy */
913 static struct GNUNET_CRYPTO_EccSignature no_address_signature;
914 static struct GNUNET_TIME_Absolute no_address_signature_expiration;
916 sig_cache = &no_address_signature;
917 sig_cache_exp = &no_address_signature_expiration;
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "I am `%s', sending PONG to peer `%s'\n",
922 GNUNET_h2s (&GST_my_identity.hashPubKey),
923 GNUNET_i2s (sender));
925 /* message with structure:
926 * [TransportPongMessage][Transport name][Address] */
928 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
930 htons (sizeof (struct TransportPongMessage) + alen + slen);
931 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
933 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
934 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
936 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
937 memcpy (&pong->challenge, &ping->challenge, sizeof (ping->challenge));
938 pong->addrlen = htonl (alen + slen);
939 memcpy (&pong[1], addr, slen); /* Copy transport plugin */
942 GNUNET_assert (NULL != addrend);
943 memcpy (&((char *) &pong[1])[slen], addrend, alen);
945 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value <
946 PONG_SIGNATURE_LIFETIME.rel_value / 4)
948 /* create / update cached sig */
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Creating PONG signature to indicate ownership.\n");
951 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
952 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
953 GNUNET_assert (GNUNET_OK ==
954 GNUNET_CRYPTO_ecc_sign (GST_my_private_key, &pong->purpose,
959 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
961 pong->signature = *sig_cache;
963 GNUNET_assert (sender_address != NULL);
965 /* first see if the session we got this PING from can be used to transmit
966 * a response reliably */
967 papi = GST_plugins_find (sender_address->transport_name);
972 GNUNET_assert (papi->send != NULL);
973 GNUNET_assert (papi->get_session != NULL);
977 session = papi->get_session (papi->cls, sender_address);
986 ret = papi->send (papi->cls, session,
987 (const char *) pong, ntohs (pong->header.size),
988 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "Transmitted PONG to `%s' via reliable mechanism\n",
996 GNUNET_i2s (sender));
998 GNUNET_STATISTICS_update (GST_stats,
1000 ("# PONGs unicast via reliable transport"), 1,
1006 /* no reliable method found, try transmission via all known addresses */
1007 GNUNET_STATISTICS_update (GST_stats,
1009 ("# PONGs multicast to all available addresses"), 1,
1011 GST_validation_get_addresses (sender, &multicast_pong, pong);
1017 * Context for the 'validate_address' function
1019 struct ValidateAddressContext
1022 * Hash of the public key of the peer whose address is being validated.
1024 struct GNUNET_PeerIdentity pid;
1027 * Public key of the peer whose address is being validated.
1029 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
1034 * Iterator callback to go over all addresses and try to validate them
1035 * (unless blocked or already validated).
1037 * @param cls pointer to a 'struct ValidateAddressContext'
1038 * @param address the address
1039 * @param expiration expiration time
1040 * @return GNUNET_OK (keep the address)
1043 validate_address_iterator (void *cls,
1044 const struct GNUNET_HELLO_Address *address,
1045 struct GNUNET_TIME_Absolute expiration)
1047 const struct ValidateAddressContext *vac = cls;
1048 struct ValidationEntry *ve;
1050 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
1051 return GNUNET_OK; /* expired */
1052 ve = find_validation_entry (&vac->public_key, address);
1053 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
1054 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1060 * Add the validated peer address to the HELLO.
1062 * @param cls the 'struct ValidationEntry' with the validated address
1063 * @param max space in buf
1064 * @param buf where to add the address
1065 * @return number of bytes written, 0 to signal the
1066 * end of the iteration.
1069 add_valid_peer_address (void *cls, size_t max, void *buf)
1071 struct ValidationEntry *ve = cls;
1073 if (GNUNET_YES == ve->copied)
1074 return 0; /* terminate */
1075 ve->copied = GNUNET_YES;
1076 return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1081 * We've received a PONG. Check if it matches a pending PING and
1082 * mark the respective address as confirmed.
1084 * @param sender peer sending the PONG
1085 * @param hdr the PONG
1088 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1089 const struct GNUNET_MessageHeader *hdr)
1091 const struct TransportPongMessage *pong;
1092 struct ValidationEntry *ve;
1098 struct GNUNET_HELLO_Message *hello;
1099 struct GNUNET_HELLO_Address address;
1101 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1103 GNUNET_break_op (0);
1106 GNUNET_STATISTICS_update (GST_stats,
1107 gettext_noop ("# PONG messages received"), 1,
1110 /* message with structure:
1111 * [TransportPongMessage][Transport name][Address] */
1113 pong = (const struct TransportPongMessage *) hdr;
1114 tname = (const char *) &pong[1];
1115 size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1116 addr = memchr (tname, '\0', size);
1119 GNUNET_break_op (0);
1123 slen = strlen (tname) + 1;
1124 addrlen = size - slen;
1125 address.peer = *sender;
1126 address.address = addr;
1127 address.address_length = addrlen;
1128 address.transport_name = tname;
1129 ve = find_validation_entry (NULL, &address);
1130 if ((NULL == ve) || (GNUNET_NO == ve->expecting_pong))
1132 GNUNET_STATISTICS_update (GST_stats,
1134 ("# PONGs dropped, no matching pending validation"),
1138 /* now check that PONG is well-formed */
1139 if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
1141 GNUNET_break_op (0);
1144 if (GNUNET_TIME_absolute_get_remaining
1145 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
1147 GNUNET_STATISTICS_update (GST_stats,
1149 ("# PONGs dropped, signature expired"), 1,
1154 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1155 &pong->purpose, &pong->signature,
1158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1159 "Invalid signature on address %s:%s from peer `%s'\n",
1160 tname, GST_plugins_a2s (ve->address),
1161 GNUNET_i2s (sender));
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "Address validated for peer `%s' with plugin `%s': `%s'\n",
1166 GNUNET_i2s (sender), tname, GST_plugins_a2s (ve->address));
1167 /* validity achieved, remember it! */
1168 ve->expecting_pong = GNUNET_NO;
1169 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1170 ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1172 struct GNUNET_ATS_Information ats;
1173 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1174 ats.value = htonl ((uint32_t) ve->latency.rel_value);
1175 GNUNET_ATS_address_add (GST_ats, ve->address, NULL, &ats, 1);
1177 /* build HELLO to store in PEERINFO */
1178 ve->copied = GNUNET_NO;
1179 hello = GNUNET_HELLO_create (&ve->public_key, &add_valid_peer_address, ve);
1180 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello, NULL, NULL);
1181 GNUNET_free (hello);
1186 * We've received a HELLO, check which addresses are new and trigger
1189 * @param hello the HELLO we received
1192 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1194 const struct GNUNET_HELLO_Message *hm =
1195 (const struct GNUNET_HELLO_Message *) hello;
1196 struct ValidateAddressContext vac;
1197 struct GNUNET_HELLO_Message *h;
1199 if ((GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1200 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1202 /* malformed HELLO */
1207 memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1209 /* Add peer identity without addresses to peerinfo service */
1210 h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL);
1211 GNUNET_PEERINFO_add_peer (GST_peerinfo, h, NULL, NULL);
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214 _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1215 GNUNET_i2s (&vac.pid));
1218 GNUNET_assert (NULL ==
1219 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1220 &validate_address_iterator,
1226 * Closure for 'iterate_addresses'
1228 struct IteratorContext
1231 * Function to call on each address.
1233 GST_ValidationAddressCallback cb;
1244 * Call the callback in the closure for each validation entry.
1246 * @param cls the 'struct GST_ValidationIteratorContext'
1247 * @param key the peer's identity
1248 * @param value the 'struct ValidationEntry'
1249 * @return GNUNET_OK (continue to iterate)
1252 iterate_addresses (void *cls, const struct GNUNET_HashCode * key, void *value)
1254 struct IteratorContext *ic = cls;
1255 struct ValidationEntry *ve = value;
1257 ic->cb (ic->cb_cls, &ve->public_key, ve->valid_until, ve->revalidation_block,
1264 * Call the given function for each address for the given target.
1265 * Can either give a snapshot (synchronous API) or be continuous.
1267 * @param target peer information is requested for
1268 * @param cb function to call; will not be called after this function returns
1269 * @param cb_cls closure for 'cb'
1272 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1273 GST_ValidationAddressCallback cb, void *cb_cls)
1275 struct IteratorContext ic;
1279 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
1280 &target->hashPubKey,
1281 &iterate_addresses, &ic);
1286 * Update if we are using an address for a connection actively right now.
1287 * Based on this, the validation module will measure latency for the
1288 * address more or less often.
1290 * @param address the address
1291 * @param session the session
1292 * @param in_use GNUNET_YES if we are now using the address for a connection,
1293 * GNUNET_NO if we are no longer using the address for a connection
1294 * @param line line of caller just for DEBUGGING!
1297 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1298 struct Session *session,
1302 struct ValidationEntry *ve;
1304 if (NULL != address)
1305 ve = find_validation_entry (NULL, address);
1307 ve = NULL; /* FIXME: lookup based on session... */
1310 /* this can happen for inbound connections (sender_address_len == 0); */
1313 if (ve->in_use == in_use)
1316 if (GNUNET_YES == in_use)
1318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1319 "Error setting address in use for peer `%s' `%s' to USED: set last time by %i, called now by %i\n",
1320 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1321 ve->last_line_set_to_yes, line);
1323 if (GNUNET_NO == in_use)
1325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1326 "Error setting address in use for peer `%s' `%s' to NOT_USED: set last time by %i, called now by %i\n",
1327 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1328 ve->last_line_set_to_no, line);
1332 if (GNUNET_YES == in_use)
1334 ve->last_line_set_to_yes = line;
1336 if (GNUNET_NO == in_use)
1338 ve->last_line_set_to_no = line;
1341 GNUNET_break (ve->in_use != in_use); /* should be different... */
1342 ve->in_use = in_use;
1343 if (in_use == GNUNET_YES)
1345 /* from now on, higher frequeny, so reschedule now */
1346 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1347 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1353 * Query validation about the latest observed latency on a given
1356 * @param sender peer
1357 * @param address the address
1358 * @param session session
1359 * @return observed latency of the address, FOREVER if the address was
1360 * never successfully validated
1362 struct GNUNET_TIME_Relative
1363 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1364 const struct GNUNET_HELLO_Address *address,
1365 struct Session *session)
1367 struct ValidationEntry *ve;
1369 if (NULL == address)
1371 GNUNET_break (0); // FIXME: support having latency only with session...
1372 return GNUNET_TIME_UNIT_FOREVER_REL;
1374 ve = find_validation_entry (NULL, address);
1376 return GNUNET_TIME_UNIT_FOREVER_REL;
1381 /* end of file gnunet-service-transport_validation.c */