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_RsaSignature 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_RsaSignaturePurpose 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_RsaPublicKeyBinaryEncoded 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?
258 * Context of currently active requests to peerinfo
259 * for validation of HELLOs.
261 struct CheckHelloValidatedContext
265 * This is a doubly-linked list.
267 struct CheckHelloValidatedContext *next;
270 * This is a doubly-linked list.
272 struct CheckHelloValidatedContext *prev;
275 * Hello that we are validating.
277 const struct GNUNET_HELLO_Message *hello;
283 * Head of linked list of HELLOs awaiting validation.
285 static struct CheckHelloValidatedContext *chvc_head;
288 * Tail of linked list of HELLOs awaiting validation
290 static struct CheckHelloValidatedContext *chvc_tail;
293 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
294 * of the given peer that we are currently validating, have validated
295 * or are blocked from re-validation for a while).
297 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
300 * Context for peerinfo iteration.
302 static struct GNUNET_PEERINFO_NotifyContext *pnc;
306 * Context for the validation entry match function.
308 struct ValidationEntryMatchContext
311 * Where to store the result?
313 struct ValidationEntry *ve;
316 * Address we're interested in.
318 const struct GNUNET_HELLO_Address *address;
324 * Iterate over validation entries until a matching one is found.
326 * @param cls the 'struct ValidationEntryMatchContext'
327 * @param key peer identity (unused)
328 * @param value a 'struct ValidationEntry' to match
329 * @return GNUNET_YES if the entry does not match,
330 * GNUNET_NO if the entry does match
333 validation_entry_match (void *cls, const GNUNET_HashCode * key, void *value)
335 struct ValidationEntryMatchContext *vemc = cls;
336 struct ValidationEntry *ve = value;
338 if (0 == GNUNET_HELLO_address_cmp (ve->address, vemc->address))
348 * Iterate over validation entries and free them.
350 * @param cls (unused)
351 * @param key peer identity (unused)
352 * @param value a 'struct ValidationEntry' to clean up
353 * @return GNUNET_YES (continue to iterate)
356 cleanup_validation_entry (void *cls, const GNUNET_HashCode * key, void *value)
358 struct ValidationEntry *ve = value;
362 GST_blacklist_test_cancel (ve->bc);
365 GNUNET_break (GNUNET_OK ==
366 GNUNET_CONTAINER_multihashmap_remove (validation_map,
367 &ve->pid.hashPubKey, ve));
368 GNUNET_HELLO_address_free (ve->address);
369 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
371 GNUNET_SCHEDULER_cancel (ve->timeout_task);
372 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
374 if (GNUNET_SCHEDULER_NO_TASK != ve->revalidation_task)
376 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
377 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
385 * Address validation cleanup task. Assesses if the record is no
386 * longer valid and then possibly triggers its removal.
388 * @param cls the 'struct ValidationEntry'
389 * @param tc scheduler context (unused)
392 timeout_hello_validation (void *cls,
393 const struct GNUNET_SCHEDULER_TaskContext *tc)
395 struct ValidationEntry *ve = cls;
396 struct GNUNET_TIME_Absolute max;
397 struct GNUNET_TIME_Relative left;
399 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
400 max = GNUNET_TIME_absolute_max (ve->valid_until, ve->revalidation_block);
401 left = GNUNET_TIME_absolute_get_remaining (max);
402 if (left.rel_value > 0)
404 /* should wait a bit longer */
406 GNUNET_SCHEDULER_add_delayed (left, &timeout_hello_validation, ve);
409 GNUNET_STATISTICS_update (GST_stats,
410 gettext_noop ("# address records discarded"), 1,
412 cleanup_validation_entry (NULL, &ve->pid.hashPubKey, ve);
417 * Function called with the result from blacklisting.
418 * Send a PING to the other peer if a communication is allowed.
420 * @param cls our 'struct ValidationEntry'
421 * @param pid identity of the other peer
422 * @param result GNUNET_OK if the connection is allowed, GNUNET_NO if not
425 transmit_ping_if_allowed (void *cls, const struct GNUNET_PeerIdentity *pid,
428 struct ValidationEntry *ve = cls;
429 struct TransportPingMessage ping;
430 struct GNUNET_TRANSPORT_PluginFunctions *papi;
431 const struct GNUNET_MessageHeader *hello;
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting plain PING to `%s' %s\n",
439 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
441 slen = strlen (ve->address->transport_name) + 1;
442 hello = GST_hello_get ();
443 hsize = ntohs (hello->size);
445 sizeof (struct TransportPingMessage) + ve->address->address_length +
449 htons (sizeof (struct TransportPingMessage) +
450 ve->address->address_length + slen);
451 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
452 ping.challenge = htonl (ve->challenge);
455 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
459 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
460 "HELLO", "PING", (unsigned int) tsize);
461 /* message too big (!?), get rid of HELLO */
464 sizeof (struct TransportPingMessage) + ve->address->address_length +
468 char message_buf[tsize];
470 /* build message with structure:
471 * [HELLO][TransportPingMessage][Transport name][Address] */
472 memcpy (message_buf, hello, hsize);
473 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
474 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
475 ve->address->transport_name, slen);
476 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
477 ve->address, ve->address->address_length);
478 papi = GST_plugins_find (ve->address->transport_name);
483 GNUNET_assert (papi->send != NULL);
484 GNUNET_assert (papi->get_session != NULL);
485 struct Session * session = papi->get_session(papi->cls, ve->address);
489 ret = papi->send (papi->cls, session,
491 PING_PRIORITY, ACCEPTABLE_PING_DELAY,
496 /* Could not get a valid session */
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not get a valid session for `%s' %s\n",
498 GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
505 ve->send_time = GNUNET_TIME_absolute_get ();
506 GNUNET_STATISTICS_update (GST_stats,
508 ("# PING without HELLO messages sent"), 1,
510 ve->expecting_pong = GNUNET_YES;
516 * Do address validation again to keep address valid.
518 * @param cls the 'struct ValidationEntry'
519 * @param tc scheduler context (unused)
522 revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
524 struct ValidationEntry *ve = cls;
525 struct GNUNET_TIME_Relative canonical_delay;
526 struct GNUNET_TIME_Relative delay;
527 struct GST_BlacklistCheck *bc;
530 ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
531 delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
532 /* How long until we can possibly permit the next PING? */
535 GNUNET_YES) ? CONNECTED_PING_FREQUENCY
536 : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value >
537 0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE);
538 if (delay.rel_value > canonical_delay.rel_value * 2)
540 /* situation changed, recalculate delay */
541 delay = canonical_delay;
542 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
544 if (delay.rel_value > 0)
546 /* should wait a bit longer */
547 ve->revalidation_task =
548 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
551 ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
553 /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
555 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
556 canonical_delay.rel_value);
558 GNUNET_TIME_relative_add (canonical_delay,
559 GNUNET_TIME_relative_multiply
560 (GNUNET_TIME_UNIT_MILLISECONDS, rdelay));
561 ve->revalidation_task =
562 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
564 /* start PINGing by checking blacklist */
565 GNUNET_STATISTICS_update (GST_stats,
566 gettext_noop ("# address revalidations started"), 1,
568 bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
569 &transmit_ping_if_allowed, ve);
571 ve->bc = bc; /* only set 'bc' if 'transmit_ping_if_allowed' was not already
577 * Find a ValidationEntry entry for the given neighbour that matches
578 * the given address and transport. If none exists, create one (but
579 * without starting any validation).
581 * @param public_key public key of the peer, NULL for unknown
582 * @param address address to find
583 * @return validation entry matching the given specifications, NULL
584 * if we don't have an existing entry and no public key was given
586 static struct ValidationEntry *
587 find_validation_entry (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
588 *public_key, const struct GNUNET_HELLO_Address *address)
590 struct ValidationEntryMatchContext vemc;
591 struct ValidationEntry *ve;
594 vemc.address = address;
595 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
596 &address->peer.hashPubKey,
597 &validation_entry_match, &vemc);
598 if (NULL != (ve = vemc.ve))
600 if (public_key == NULL)
602 ve = GNUNET_malloc (sizeof (struct ValidationEntry));
603 ve->address = GNUNET_HELLO_address_copy (address);
604 ve->public_key = *public_key;
605 ve->pid = address->peer;
606 ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
608 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
610 GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
611 &timeout_hello_validation, ve);
612 GNUNET_CONTAINER_multihashmap_put (validation_map, &address->peer.hashPubKey,
614 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
615 ve->expecting_pong = GNUNET_NO;
621 * Iterator which adds the given address to the set of validated
624 * @param cls original HELLO message
625 * @param address the address
626 * @param expiration expiration time
627 * @return GNUNET_OK (keep the address)
630 add_valid_address (void *cls, const struct GNUNET_HELLO_Address *address,
631 struct GNUNET_TIME_Absolute expiration)
633 const struct GNUNET_HELLO_Message *hello = cls;
634 struct ValidationEntry *ve;
635 struct GNUNET_PeerIdentity pid;
636 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
638 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
639 return GNUNET_OK; /* expired */
640 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
641 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
644 return GNUNET_OK; /* invalid HELLO !? */
646 if (0 == memcmp (&GST_my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
648 /* Peerinfo returned own identity, skip validation */
652 ve = find_validation_entry (&public_key, address);
653 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
655 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
656 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
657 GNUNET_ATS_address_update (GST_ats, address, NULL, NULL, 0);
663 * Function called for any HELLO known to PEERINFO.
666 * @param peer id of the peer, NULL for last call
667 * @param hello hello message for the peer (can be NULL)
668 * @param err_msg error message
671 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
672 const struct GNUNET_HELLO_Message *hello,
675 GNUNET_assert (NULL != peer);
678 GNUNET_assert (NULL ==
679 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
686 * Start the validation subsystem.
689 GST_validation_start ()
691 validation_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE);
692 pnc = GNUNET_PEERINFO_notify (GST_cfg, &process_peerinfo_hello, NULL);
697 * Stop the validation subsystem.
700 GST_validation_stop ()
702 struct CheckHelloValidatedContext *chvc;
704 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
705 &cleanup_validation_entry, NULL);
706 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
707 validation_map = NULL;
708 while (NULL != (chvc = chvc_head))
710 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
713 GNUNET_PEERINFO_notify_cancel (pnc);
718 * Send the given PONG to the given address.
720 * @param cls the PONG message
721 * @param public_key public key for the peer, never NULL
722 * @param valid_until is ZERO if we never validated the address,
723 * otherwise a time up to when we consider it (or was) valid
724 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
725 * is ZERO if the address is considered valid (no validation needed)
726 * otherwise a time in the future if we're currently denying re-validation
727 * @param address target address
730 multicast_pong (void *cls,
731 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
732 *public_key, struct GNUNET_TIME_Absolute valid_until,
733 struct GNUNET_TIME_Absolute validation_block,
734 const struct GNUNET_HELLO_Address *address)
736 struct TransportPongMessage *pong = cls;
737 struct GNUNET_TRANSPORT_PluginFunctions *papi;
739 papi = GST_plugins_find (address->transport_name);
743 GNUNET_assert (papi->send != NULL);
744 GNUNET_assert (papi->get_session != NULL);
746 struct Session * session = papi->get_session(papi->cls, address);
753 papi->send (papi->cls, session,
754 (const char *) pong, ntohs (pong->header.size),
755 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
761 * We've received a PING. If appropriate, generate a PONG.
763 * @param sender peer sending the PING
764 * @param hdr the PING
765 * @param sender_address the sender address as we got it
766 * @param session session we got the PING from
769 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
770 const struct GNUNET_MessageHeader *hdr,
771 const struct GNUNET_HELLO_Address *sender_address,
772 struct Session *session)
774 const struct TransportPingMessage *ping;
775 struct TransportPongMessage *pong;
776 struct GNUNET_TRANSPORT_PluginFunctions *papi;
777 struct GNUNET_CRYPTO_RsaSignature *sig_cache;
778 struct GNUNET_TIME_Absolute *sig_cache_exp;
784 struct GNUNET_HELLO_Address address;
786 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
791 ping = (const struct TransportPingMessage *) hdr;
793 memcmp (&ping->target, &GST_my_identity,
794 sizeof (struct GNUNET_PeerIdentity)))
796 GNUNET_STATISTICS_update (GST_stats,
798 ("# PING message for different peer received"), 1,
802 GNUNET_STATISTICS_update (GST_stats,
803 gettext_noop ("# PING messages received"), 1,
805 addr = (const char *) &ping[1];
806 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
807 /* peer wants to confirm that this is one of our addresses, this is what is
808 * used for address validation */
811 sig_cache_exp = NULL;
815 addrend = memchr (addr, '\0', alen);
822 slen = strlen (addr) + 1;
824 address.address = addrend;
825 address.address_length = alen;
826 address.transport_name = addr;
827 address.peer = *sender;
829 GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
831 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
833 ("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
834 GST_plugins_a2s (&address));
840 addrend = NULL; /* make gcc happy */
842 static struct GNUNET_CRYPTO_RsaSignature no_address_signature;
843 static struct GNUNET_TIME_Absolute no_address_signature_expiration;
845 sig_cache = &no_address_signature;
846 sig_cache_exp = &no_address_signature_expiration;
849 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
851 htons (sizeof (struct TransportPongMessage) + alen + slen);
852 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
854 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
855 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
857 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
858 pong->challenge = ping->challenge;
859 pong->addrlen = htonl (alen + slen);
860 memcpy (&pong[1], addr, slen);
861 memcpy (&((char *) &pong[1])[slen], addrend, alen);
862 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value <
863 PONG_SIGNATURE_LIFETIME.rel_value / 4)
865 /* create / update cached sig */
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "Creating PONG signature to indicate ownership.\n");
870 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
871 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
872 GNUNET_assert (GNUNET_OK ==
873 GNUNET_CRYPTO_rsa_sign (GST_my_private_key, &pong->purpose,
878 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
880 pong->signature = *sig_cache;
882 GNUNET_assert (sender_address != NULL);
884 /* first see if the session we got this PING from can be used to transmit
885 * a response reliably */
886 papi = GST_plugins_find (sender_address->transport_name);
891 GNUNET_assert (papi->send != NULL);
892 GNUNET_assert (papi->get_session != NULL);
896 session = papi->get_session (papi->cls, sender_address);
905 ret = papi->send (papi->cls, session,
906 (const char *) pong, ntohs (pong->header.size),
907 PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 "Transmitted PONG to `%s' via reliable mechanism\n",
915 GNUNET_i2s (sender));
917 GNUNET_STATISTICS_update (GST_stats,
919 ("# PONGs unicast via reliable transport"), 1,
925 /* no reliable method found, try transmission via all known addresses */
926 GNUNET_STATISTICS_update (GST_stats,
928 ("# PONGs multicast to all available addresses"), 1,
930 GST_validation_get_addresses (sender, &multicast_pong, pong);
936 * Context for the 'validate_address' function
938 struct ValidateAddressContext
941 * Hash of the public key of the peer whose address is being validated.
943 struct GNUNET_PeerIdentity pid;
946 * Public key of the peer whose address is being validated.
948 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
953 * Iterator callback to go over all addresses and try to validate them
954 * (unless blocked or already validated).
956 * @param cls pointer to a 'struct ValidateAddressContext'
957 * @param address the address
958 * @param expiration expiration time
959 * @return GNUNET_OK (keep the address)
962 validate_address_iterator (void *cls,
963 const struct GNUNET_HELLO_Address *address,
964 struct GNUNET_TIME_Absolute expiration)
966 const struct ValidateAddressContext *vac = cls;
967 struct ValidationEntry *ve;
969 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
970 return GNUNET_OK; /* expired */
971 ve = find_validation_entry (&vac->public_key, address);
972 if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
973 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
979 * Add the validated peer address to the HELLO.
981 * @param cls the 'struct ValidationEntry' with the validated address
982 * @param max space in buf
983 * @param buf where to add the address
984 * @return number of bytes written, 0 to signal the
985 * end of the iteration.
988 add_valid_peer_address (void *cls, size_t max, void *buf)
990 struct ValidationEntry *ve = cls;
992 if (GNUNET_YES == ve->copied)
993 return 0; /* terminate */
994 ve->copied = GNUNET_YES;
995 return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1000 * We've received a PONG. Check if it matches a pending PING and
1001 * mark the respective address as confirmed.
1003 * @param sender peer sending the PONG
1004 * @param hdr the PONG
1007 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1008 const struct GNUNET_MessageHeader *hdr)
1010 const struct TransportPongMessage *pong;
1011 struct ValidationEntry *ve;
1017 struct GNUNET_HELLO_Message *hello;
1018 struct GNUNET_HELLO_Address address;
1020 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1022 GNUNET_break_op (0);
1025 GNUNET_STATISTICS_update (GST_stats,
1026 gettext_noop ("# PONG messages received"), 1,
1029 pong = (const struct TransportPongMessage *) hdr;
1030 tname = (const char *) &pong[1];
1031 size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1032 addr = memchr (tname, '\0', size);
1035 GNUNET_break_op (0);
1039 slen = strlen (tname) + 1;
1040 addrlen = size - slen;
1041 address.peer = *sender;
1042 address.address = addr;
1043 address.address_length = addrlen;
1044 address.transport_name = tname;
1045 ve = find_validation_entry (NULL, &address);
1046 if ((NULL == ve) || (ve->expecting_pong == GNUNET_NO))
1048 GNUNET_STATISTICS_update (GST_stats,
1050 ("# PONGs dropped, no matching pending validation"),
1054 /* now check that PONG is well-formed */
1055 if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
1057 GNUNET_break_op (0);
1062 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1063 &pong->purpose, &pong->signature,
1066 GNUNET_break_op (0);
1070 if (GNUNET_TIME_absolute_get_remaining
1071 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
1073 GNUNET_STATISTICS_update (GST_stats,
1075 ("# PONGs dropped, signature expired"), 1,
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "Address validated for peer `%s' with plugin `%s': `%s'\n",
1082 GNUNET_i2s (sender), tname, GST_plugins_a2s (tname, addr,
1086 /* validity achieved, remember it! */
1087 ve->expecting_pong = GNUNET_NO;
1088 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1089 ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1091 struct GNUNET_ATS_Information ats;
1093 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1094 ats.value = htonl ((uint32_t) ve->latency.rel_value);
1095 GNUNET_ATS_address_update (GST_ats, ve->address, NULL, &ats, 1);
1097 /* build HELLO to store in PEERINFO */
1098 ve->copied = GNUNET_NO;
1099 hello = GNUNET_HELLO_create (&ve->public_key, &add_valid_peer_address, ve);
1100 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello);
1101 GNUNET_free (hello);
1106 * We've received a HELLO, check which addresses are new and trigger
1109 * @param hello the HELLO we received
1112 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1114 const struct GNUNET_HELLO_Message *hm =
1115 (const struct GNUNET_HELLO_Message *) hello;
1116 struct ValidateAddressContext vac;
1117 struct GNUNET_HELLO_Message *h;
1119 if ((GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1120 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1122 /* malformed HELLO */
1127 memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1129 /* Add peer identity without addresses to peerinfo service */
1130 h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL);
1131 GNUNET_PEERINFO_add_peer (GST_peerinfo, h);
1132 #if VERBOSE_VALIDATION
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1135 GNUNET_i2s (&vac.pid));
1138 GNUNET_assert (NULL ==
1139 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1140 &validate_address_iterator,
1146 * Closure for 'iterate_addresses'
1148 struct IteratorContext
1151 * Function to call on each address.
1153 GST_ValidationAddressCallback cb;
1164 * Call the callback in the closure for each validation entry.
1166 * @param cls the 'struct GST_ValidationIteratorContext'
1167 * @param key the peer's identity
1168 * @param value the 'struct ValidationEntry'
1169 * @return GNUNET_OK (continue to iterate)
1172 iterate_addresses (void *cls, const GNUNET_HashCode * key, void *value)
1174 struct IteratorContext *ic = cls;
1175 struct ValidationEntry *ve = value;
1177 ic->cb (ic->cb_cls, &ve->public_key, ve->valid_until, ve->revalidation_block,
1184 * Call the given function for each address for the given target.
1185 * Can either give a snapshot (synchronous API) or be continuous.
1187 * @param target peer information is requested for
1188 * @param cb function to call; will not be called after this function returns
1189 * @param cb_cls closure for 'cb'
1192 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1193 GST_ValidationAddressCallback cb, void *cb_cls)
1195 struct IteratorContext ic;
1199 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
1200 &target->hashPubKey,
1201 &iterate_addresses, &ic);
1206 * Update if we are using an address for a connection actively right now.
1207 * Based on this, the validation module will measure latency for the
1208 * address more or less often.
1210 * @param address the address
1211 * @param session the session
1212 * @param in_use GNUNET_YES if we are now using the address for a connection,
1213 * GNUNET_NO if we are no longer using the address for a connection
1216 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1217 struct Session *session,
1220 struct ValidationEntry *ve;
1222 if (NULL != address)
1223 ve = find_validation_entry (NULL, address);
1225 ve = NULL; /* FIXME: lookup based on session... */
1228 /* this can happen for inbound connections (sender_address_len == 0); */
1231 if (ve->in_use == in_use)
1232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1233 "GST_validation_set_address_use: %s %s: ve->in_use %i <-> in_use %i\n",
1234 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), ve->in_use,
1236 GNUNET_break (ve->in_use != in_use); /* should be different... */
1237 ve->in_use = in_use;
1238 if (in_use == GNUNET_YES)
1240 /* from now on, higher frequeny, so reschedule now */
1241 GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1242 ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1248 * Query validation about the latest observed latency on a given
1251 * @param sender peer
1252 * @param address the address
1253 * @param session session
1254 * @return observed latency of the address, FOREVER if the address was
1255 * never successfully validated
1257 struct GNUNET_TIME_Relative
1258 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1259 const struct GNUNET_HELLO_Address *address,
1260 struct Session *session)
1262 struct ValidationEntry *ve;
1264 if (NULL == address)
1266 GNUNET_break (0); // FIXME: support having latency only with session...
1267 return GNUNET_TIME_UNIT_FOREVER_REL;
1269 ve = find_validation_entry (NULL, address);
1271 return GNUNET_TIME_UNIT_FOREVER_REL;
1276 /* end of file gnunet-service-transport_validation.c */