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 // TODO: observe latency between PING/PONG and give information to ATS!
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 long before an existing address expires should we again try to
57 * validate it? Must be (significantly) smaller than
58 * HELLO_ADDRESS_EXPIRATION.
60 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
63 * Size of the validation map hashmap.
65 #define VALIDATION_MAP_SIZE 256
68 * Priority to use for PINGs
70 #define PING_PRIORITY 2
73 * Priority to use for PONGs
75 #define PONG_PRIORITY 4
79 * Message used to ask a peer to validate receipt (to check an address
80 * from a HELLO). Followed by the address we are trying to validate,
81 * or an empty address if we are just sending a PING to confirm that a
82 * connection which the receiver (of the PING) initiated is still valid.
84 struct TransportPingMessage
88 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
90 struct GNUNET_MessageHeader header;
93 * Challenge code (to ensure fresh reply).
95 uint32_t challenge GNUNET_PACKED;
98 * Who is the intended recipient?
100 struct GNUNET_PeerIdentity target;
106 * Message used to validate a HELLO. The challenge is included in the
107 * confirmation to make matching of replies to requests possible. The
108 * signature signs our public key, an expiration time and our address.<p>
110 * This message is followed by our transport address that the PING tried
111 * to confirm (if we liked it). The address can be empty (zero bytes)
112 * if the PING had not address either (and we received the request via
113 * a connection that we initiated).
115 struct TransportPongMessage
119 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
121 struct GNUNET_MessageHeader header;
124 * Challenge code from PING (showing freshness). Not part of what
125 * is signed so that we can re-use signatures.
127 uint32_t challenge GNUNET_PACKED;
132 struct GNUNET_CRYPTO_RsaSignature signature;
135 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
136 * plausible address for the signing peer.
138 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
141 * When does this signature expire?
143 struct GNUNET_TIME_AbsoluteNBO expiration;
146 * Size of address appended to this message (part of what is
147 * being signed, hence not redundant).
149 uint32_t addrlen GNUNET_PACKED;
155 * Information about an address under validation
157 struct ValidationEntry
161 * Name of the transport.
163 char *transport_name;
166 * The address, actually a pointer to the end
167 * of this struct. Do not free!
172 * Handle to the blacklist check (if we're currently in it).
174 struct GST_BlacklistCheck *bc;
177 * Public key of the peer.
179 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
182 * The identity of the peer.
184 struct GNUNET_PeerIdentity pid;
187 * ID of task that will clean up this entry if nothing happens.
189 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
192 * At what time did we send the latest validation request?
194 struct GNUNET_TIME_Absolute send_time;
197 * Until when is this address valid?
198 * ZERO if it is not currently considered valid.
200 struct GNUNET_TIME_Absolute valid_until;
203 * How long until we can try to validate this address again?
204 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
205 * ZERO if the address is considered valid (no validation needed)
206 * otherwise a time in the future if we're currently denying re-validation
208 struct GNUNET_TIME_Absolute validation_block;
211 * Challenge number we used.
221 * When passing the address in 'add_valid_peer_address', did we
222 * copy the address to the HELLO yet?
230 * Context of currently active requests to peerinfo
231 * for validation of HELLOs.
233 struct CheckHelloValidatedContext
237 * This is a doubly-linked list.
239 struct CheckHelloValidatedContext *next;
242 * This is a doubly-linked list.
244 struct CheckHelloValidatedContext *prev;
247 * Hello that we are validating.
249 const struct GNUNET_HELLO_Message *hello;
255 * Head of linked list of HELLOs awaiting validation.
257 static struct CheckHelloValidatedContext *chvc_head;
260 * Tail of linked list of HELLOs awaiting validation
262 static struct CheckHelloValidatedContext *chvc_tail;
265 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
266 * of the given peer that we are currently validating, have validated
267 * or are blocked from re-validation for a while).
269 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
272 * Context for peerinfo iteration.
274 static struct GNUNET_PEERINFO_NotifyContext *pnc;
278 * Context for the validation entry match function.
280 struct ValidationEntryMatchContext
283 * Where to store the result?
285 struct ValidationEntry *ve;
288 * Transport name we're looking for.
290 const char *transport_name;
293 * Address we're interested in.
298 * Number of bytes in 'addr'.
305 * Iterate over validation entries until a matching one is found.
307 * @param cls the 'struct ValidationEntryMatchContext'
308 * @param key peer identity (unused)
309 * @param value a 'struct ValidationEntry' to match
310 * @return GNUNET_YES if the entry does not match,
311 * GNUNET_NO if the entry does match
314 validation_entry_match (void *cls, const GNUNET_HashCode * key, void *value)
316 struct ValidationEntryMatchContext *vemc = cls;
317 struct ValidationEntry *ve = value;
319 if ((ve->addrlen == vemc->addrlen) &&
320 (0 == memcmp (ve->addr, vemc->addr, ve->addrlen)) &&
321 (0 == strcmp (ve->transport_name, vemc->transport_name)))
331 * Find a ValidationEntry entry for the given neighbour that matches
332 * the given address and transport. If none exists, create one (but
333 * without starting any validation).
335 * @param public_key public key of the peer, NULL for unknown
336 * @param neighbour which peer we care about
337 * @param tname name of the transport plugin
338 * @param addr binary address
339 * @param addrlen length of addr
340 * @return validation entry matching the given specifications, NULL
341 * if we don't have an existing entry and no public key was given
343 static struct ValidationEntry *
344 find_validation_entry (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
345 *public_key, const struct GNUNET_PeerIdentity *neighbour,
346 const char *tname, const char *addr, size_t addrlen)
348 struct ValidationEntryMatchContext vemc;
349 struct ValidationEntry *ve;
352 vemc.transport_name = tname;
354 vemc.addrlen = addrlen;
355 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
356 &neighbour->hashPubKey,
357 &validation_entry_match, &vemc);
358 if (NULL != (ve = vemc.ve))
360 if (public_key == NULL)
362 ve = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
363 ve->transport_name = GNUNET_strdup (tname);
364 ve->addr = (void *) &ve[1];
365 ve->public_key = *public_key;
366 ve->pid = *neighbour;
368 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
369 memcpy (&ve[1], addr, addrlen);
370 ve->addrlen = addrlen;
371 GNUNET_CONTAINER_multihashmap_put (validation_map, &neighbour->hashPubKey, ve,
372 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
378 * Iterator which adds the given address to the set of validated
381 * @param cls original HELLO message
382 * @param tname name of the transport
383 * @param expiration expiration time
384 * @param addr the address
385 * @param addrlen length of the address
386 * @return GNUNET_OK (keep the address)
389 add_valid_address (void *cls, const char *tname,
390 struct GNUNET_TIME_Absolute expiration, const void *addr,
393 const struct GNUNET_HELLO_Message *hello = cls;
394 struct ValidationEntry *ve;
395 struct GNUNET_PeerIdentity pid;
396 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
398 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
399 return GNUNET_OK; /* expired */
400 if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
401 (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
404 return GNUNET_OK; /* invalid HELLO !? */
406 ve = find_validation_entry (&public_key, &pid, tname, addr, addrlen);
407 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
408 struct GNUNET_TRANSPORT_ATS_Information ats;
410 ats.type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
411 ats.value = htonl (0);
413 GNUNET_ATS_address_update (GST_ats, &pid, ve->valid_until, tname, NULL, addr,
420 * Function called for any HELLO known to PEERINFO.
423 * @param peer id of the peer, NULL for last call
424 * @param hello hello message for the peer (can be NULL)
425 * @param err_msg error message
428 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
429 const struct GNUNET_HELLO_Message *hello,
432 GNUNET_assert (NULL != peer);
435 GNUNET_assert (NULL ==
436 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
443 * Start the validation subsystem.
446 GST_validation_start ()
448 validation_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE);
449 pnc = GNUNET_PEERINFO_notify (GST_cfg, &process_peerinfo_hello, NULL);
454 * Iterate over validation entries and free them.
456 * @param cls (unused)
457 * @param key peer identity (unused)
458 * @param value a 'struct ValidationEntry' to clean up
459 * @return GNUNET_YES (continue to iterate)
462 cleanup_validation_entry (void *cls, const GNUNET_HashCode * key, void *value)
464 struct ValidationEntry *ve = value;
468 GST_blacklist_test_cancel (ve->bc);
471 GNUNET_break (GNUNET_OK ==
472 GNUNET_CONTAINER_multihashmap_remove (validation_map,
473 &ve->pid.hashPubKey, ve));
474 GNUNET_free (ve->transport_name);
475 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
477 GNUNET_SCHEDULER_cancel (ve->timeout_task);
478 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
486 * Stop the validation subsystem.
489 GST_validation_stop ()
491 struct CheckHelloValidatedContext *chvc;
493 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
494 &cleanup_validation_entry, NULL);
495 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
496 validation_map = NULL;
497 while (NULL != (chvc = chvc_head))
499 GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
502 GNUNET_PEERINFO_notify_cancel (pnc);
507 * Address validation cleanup task (record no longer needed).
509 * @param cls the 'struct ValidationEntry'
510 * @param tc scheduler context (unused)
513 timeout_hello_validation (void *cls,
514 const struct GNUNET_SCHEDULER_TaskContext *tc)
516 struct ValidationEntry *ve = cls;
518 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
519 GNUNET_STATISTICS_update (GST_stats,
520 gettext_noop ("# address records discarded"), 1,
522 cleanup_validation_entry (NULL, &ve->pid.hashPubKey, ve);
527 * Send the given PONG to the given address.
529 * @param cls the PONG message
530 * @param public_key public key for the peer, never NULL
531 * @param target peer this change is about, never NULL
532 * @param valid_until is ZERO if we never validated the address,
533 * otherwise a time up to when we consider it (or was) valid
534 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
535 * is ZERO if the address is considered valid (no validation needed)
536 * otherwise a time in the future if we're currently denying re-validation
537 * @param plugin_name name of the plugin
538 * @param plugin_address binary address
539 * @param plugin_address_len length of address
542 multicast_pong (void *cls,
543 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
544 *public_key, const struct GNUNET_PeerIdentity *target,
545 struct GNUNET_TIME_Absolute valid_until,
546 struct GNUNET_TIME_Absolute validation_block,
547 const char *plugin_name, const void *plugin_address,
548 size_t plugin_address_len)
550 struct TransportPongMessage *pong = cls;
551 struct GNUNET_TRANSPORT_PluginFunctions *papi;
553 papi = GST_plugins_find (plugin_name);
556 (void) papi->send (papi->cls, target, (const char *) pong,
557 ntohs (pong->header.size), PONG_PRIORITY,
558 HELLO_REVALIDATION_START_TIME, NULL, plugin_address,
559 plugin_address_len, GNUNET_YES, NULL, NULL);
564 * We've received a PING. If appropriate, generate a PONG.
566 * @param sender peer sending the PING
567 * @param hdr the PING
568 * @param session session we got the PING from
569 * @param plugin_name name of plugin that received the PING
570 * @param sender_address address of the sender as known to the plugin, NULL
571 * if we did not initiate the connection
572 * @param sender_address_len number of bytes in sender_address
575 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
576 const struct GNUNET_MessageHeader *hdr,
577 const char *plugin_name, struct Session *session,
578 const void *sender_address,
579 size_t sender_address_len)
581 const struct TransportPingMessage *ping;
582 struct TransportPongMessage *pong;
583 struct GNUNET_TRANSPORT_PluginFunctions *papi;
584 struct GNUNET_CRYPTO_RsaSignature *sig_cache;
585 struct GNUNET_TIME_Absolute *sig_cache_exp;
592 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
597 ping = (const struct TransportPingMessage *) hdr;
599 memcmp (&ping->target, &GST_my_identity,
600 sizeof (struct GNUNET_PeerIdentity)))
605 GNUNET_STATISTICS_update (GST_stats,
606 gettext_noop ("# PING messages received"), 1,
608 addr = (const char *) &ping[1];
609 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
610 /* peer wants to confirm that this is one of our addresses, this is what is
611 * used for address validation */
613 addrend = memchr (addr, '\0', alen);
620 slen = strlen (addr) + 1;
624 GST_hello_test_address (addr, addrend, alen, &sig_cache, &sig_cache_exp))
626 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
628 ("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
629 GST_plugins_a2s (addr, addrend, alen));
633 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
635 htons (sizeof (struct TransportPongMessage) + alen + slen);
636 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
638 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
639 sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
641 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
642 pong->challenge = ping->challenge;
643 pong->addrlen = htonl (alen + slen);
644 memcpy (&pong[1], addr, slen);
645 memcpy (&((char *) &pong[1])[slen], addrend, alen);
646 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value <
647 PONG_SIGNATURE_LIFETIME.rel_value / 4)
649 /* create / update cached sig */
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
652 "Creating PONG signature to indicate ownership.\n");
654 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
655 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
656 GNUNET_assert (GNUNET_OK ==
657 GNUNET_CRYPTO_rsa_sign (GST_my_private_key, &pong->purpose,
662 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
664 pong->signature = *sig_cache;
666 /* first see if the session we got this PING from can be used to transmit
667 * a response reliably */
668 papi = GST_plugins_find (plugin_name);
673 papi->send (papi->cls, sender, (const char *) pong,
674 ntohs (pong->header.size), PONG_PRIORITY,
675 HELLO_REVALIDATION_START_TIME, session, sender_address,
676 sender_address_len, GNUNET_SYSERR, NULL, NULL);
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "Transmitted PONG to `%s' via reliable mechanism\n",
681 GNUNET_i2s (sender));
683 GNUNET_STATISTICS_update (GST_stats,
685 ("# PONGs unicast via reliable transport"), 1,
691 /* no reliable method found, try transmission via all known addresses */
692 GNUNET_STATISTICS_update (GST_stats,
694 ("# PONGs multicast to all available addresses"), 1,
696 GST_validation_get_addresses (sender, &multicast_pong, pong);
702 * Context for the 'validate_address' function
704 struct ValidateAddressContext
707 * Hash of the public key of the peer whose address is being validated.
709 struct GNUNET_PeerIdentity pid;
712 * Public key of the peer whose address is being validated.
714 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
719 * Function called with the result from blacklisting.
720 * Send a PING to the other peer if a communication is allowed.
722 * @param cls ou r'struct ValidationEntry'
723 * @param pid identity of the other peer
724 * @param result GNUNET_OK if the connection is allowed, GNUNET_NO if not
727 transmit_ping_if_allowed (void *cls, const struct GNUNET_PeerIdentity *pid,
730 struct ValidationEntry *ve = cls;
731 struct TransportPingMessage ping;
732 struct GNUNET_TRANSPORT_PluginFunctions *papi;
733 const struct GNUNET_MessageHeader *hello;
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting plain PING to `%s'\n",
743 slen = strlen (ve->transport_name) + 1;
744 hello = GST_hello_get ();
745 hsize = ntohs (hello->size);
746 tsize = sizeof (struct TransportPingMessage) + ve->addrlen + slen + hsize;
749 htons (sizeof (struct TransportPingMessage) + ve->addrlen + slen);
750 ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
751 ping.challenge = htonl (ve->challenge);
754 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
758 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
759 "HELLO", "PING", (unsigned int) tsize);
760 /* message too big (!?), get rid of HELLO */
762 tsize = sizeof (struct TransportPingMessage) + ve->addrlen + slen + hsize;
765 char message_buf[tsize];
767 /* build message with structure:
768 * [HELLO][TransportPingMessage][Transport name][Address] */
769 memcpy (message_buf, hello, hsize);
770 memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
771 memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
772 ve->transport_name, slen);
773 memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
774 ve->addr, ve->addrlen);
775 papi = GST_plugins_find (ve->transport_name);
780 GNUNET_assert (papi->send != NULL);
782 papi->send (papi->cls, pid, message_buf, tsize, PING_PRIORITY,
783 HELLO_REVALIDATION_START_TIME, NULL /* no session */ ,
784 ve->addr, ve->addrlen, GNUNET_YES, NULL, NULL);
789 ve->send_time = GNUNET_TIME_absolute_get ();
790 GNUNET_STATISTICS_update (GST_stats,
792 ("# PING without HELLO messages sent"), 1,
799 * Iterator callback to go over all addresses and try to validate them
800 * (unless blocked or already validated).
802 * @param cls pointer to a 'struct ValidateAddressContext'
803 * @param tname name of the transport
804 * @param expiration expiration time
805 * @param addr the address
806 * @param addrlen length of the address
807 * @return GNUNET_OK (keep the address)
810 validate_address (void *cls, const char *tname,
811 struct GNUNET_TIME_Absolute expiration, const void *addr,
814 const struct ValidateAddressContext *vac = cls;
815 const struct GNUNET_PeerIdentity *pid = &vac->pid;
816 struct ValidationEntry *ve;
817 struct GST_BlacklistCheck *bc;
819 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
820 return GNUNET_OK; /* expired */
821 ve = find_validation_entry (&vac->public_key, pid, tname, addr, addrlen);
822 if (GNUNET_TIME_absolute_get_remaining (ve->validation_block).rel_value > 0)
823 return GNUNET_OK; /* blocked */
824 if ((GNUNET_SCHEDULER_NO_TASK != ve->timeout_task) &&
825 (GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value > 0))
826 return GNUNET_OK; /* revalidation task already scheduled & still valid */
827 ve->validation_block =
828 GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME);
829 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
830 GNUNET_SCHEDULER_cancel (ve->timeout_task);
832 GNUNET_SCHEDULER_add_delayed (HELLO_REVALIDATION_START_TIME,
833 &timeout_hello_validation, ve);
834 bc = GST_blacklist_test_allowed (pid, tname, &transmit_ping_if_allowed, ve);
842 * Do address validation again to keep address valid.
844 * @param cls the 'struct ValidationEntry'
845 * @param tc scheduler context (unused)
848 revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
850 struct ValidationEntry *ve = cls;
851 struct GNUNET_TIME_Relative delay;
852 struct ValidateAddressContext vac;
854 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
855 delay = GNUNET_TIME_absolute_get_remaining (ve->validation_block);
856 if (delay.rel_value > 0)
858 /* should wait a bit longer */
860 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
863 GNUNET_STATISTICS_update (GST_stats,
864 gettext_noop ("# address revalidations started"), 1,
867 vac.public_key = ve->public_key;
868 validate_address (&vac, ve->transport_name, ve->valid_until, ve->addr,
869 (uint16_t) ve->addrlen);
874 * Add the validated peer address to the HELLO.
876 * @param cls the 'struct ValidationEntry' with the validated address
877 * @param max space in buf
878 * @param buf where to add the address
879 * @return number of bytes written, 0 to signal the
880 * end of the iteration.
883 add_valid_peer_address (void *cls, size_t max, void *buf)
885 struct ValidationEntry *ve = cls;
887 if (GNUNET_YES == ve->copied)
888 return 0; /* terminate */
889 ve->copied = GNUNET_YES;
890 return GNUNET_HELLO_add_address (ve->transport_name, ve->valid_until,
891 ve->addr, ve->addrlen, buf, max);
896 * We've received a PONG. Check if it matches a pending PING and
897 * mark the respective address as confirmed.
899 * @param sender peer sending the PONG
900 * @param hdr the PONG
903 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
904 const struct GNUNET_MessageHeader *hdr)
906 const struct TransportPongMessage *pong;
907 struct ValidationEntry *ve;
914 struct GNUNET_TIME_Relative delay;
915 struct GNUNET_HELLO_Message *hello;
917 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
922 GNUNET_STATISTICS_update (GST_stats,
923 gettext_noop ("# PONG messages received"), 1,
926 pong = (const struct TransportPongMessage *) hdr;
927 tname = (const char *) &pong[1];
928 size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
929 addr = memchr (tname, '\0', size);
936 slen = strlen (tname) + 1;
937 addrlen = size - slen;
939 ve = find_validation_entry (NULL, sender, tname, addr, addrlen);
943 GNUNET_STATISTICS_update (GST_stats,
945 ("# PONGs dropped, no matching pending validation"),
949 /* now check that PONG is well-formed */
950 if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
956 if (GNUNET_TIME_absolute_get_remaining
957 (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
959 GNUNET_STATISTICS_update (GST_stats,
961 ("# PONGs dropped, signature expired"), 1,
966 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
967 &pong->purpose, &pong->signature,
974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
975 "Address validated for peer `%s' with plugin `%s': `%s'\n",
976 GNUNET_i2s (sender), tname, GST_plugins_a2s (tname, addr,
980 /* validity achieved, remember it! */
981 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
982 struct GNUNET_TRANSPORT_ATS_Information ats;
984 ats.type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
985 ats.value = htonl (0);
986 GNUNET_ATS_address_update (GST_ats, &ve->pid, ve->valid_until, ve->transport_name, NULL, ve->addr, ve->addrlen, &ats, 1); /* FIXME: compute and add latency here... */
988 /* build HELLO to store in PEERINFO */
989 ve->copied = GNUNET_NO;
990 hello = GNUNET_HELLO_create (&ve->public_key, &add_valid_peer_address, ve);
991 GNUNET_PEERINFO_add_peer (GST_peerinfo, hello);
994 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
995 GNUNET_SCHEDULER_cancel (ve->timeout_task);
997 /* randomly delay by up to 1h to avoid synchronous validations */
998 rdelay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 60 * 60);
1000 GNUNET_TIME_relative_add (HELLO_REVALIDATION_START_TIME,
1001 GNUNET_TIME_relative_multiply
1002 (GNUNET_TIME_UNIT_SECONDS, rdelay));
1004 GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
1009 * We've received a HELLO, check which addresses are new and trigger
1012 * @param hello the HELLO we received
1015 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1017 const struct GNUNET_HELLO_Message *hm =
1018 (const struct GNUNET_HELLO_Message *) hello;
1019 struct ValidateAddressContext vac;
1021 if ((GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1022 (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1024 /* malformed HELLO */
1028 GNUNET_assert (NULL ==
1029 GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1030 &validate_address, &vac));
1035 * Closure for 'iterate_addresses'
1037 struct IteratorContext
1040 * Function to call on each address.
1042 GST_ValidationAddressCallback cb;
1053 * Call the callback in the closure for each validation entry.
1055 * @param cls the 'struct GST_ValidationIteratorContext'
1056 * @param key the peer's identity
1057 * @param value the 'struct ValidationEntry'
1058 * @return GNUNET_OK (continue to iterate)
1061 iterate_addresses (void *cls, const GNUNET_HashCode * key, void *value)
1063 struct IteratorContext *ic = cls;
1064 struct ValidationEntry *ve = value;
1066 ic->cb (ic->cb_cls, &ve->public_key, &ve->pid, ve->valid_until,
1067 ve->validation_block, ve->transport_name, ve->addr, ve->addrlen);
1073 * Call the given function for each address for the given target.
1074 * Can either give a snapshot (synchronous API) or be continuous.
1076 * @param target peer information is requested for
1077 * @param cb function to call; will not be called after this function returns
1078 * @param cb_cls closure for 'cb'
1081 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1082 GST_ValidationAddressCallback cb, void *cb_cls)
1084 struct IteratorContext ic;
1088 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
1089 &target->hashPubKey,
1090 &iterate_addresses, &ic);
1094 /* end of file gnunet-service-transport_validation.c */