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.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_signatures.h"
37 * How long is a PONG signature valid? We'll recycle a signature until
38 * 1/4 of this time is remaining. PONGs should expire so that if our
39 * external addresses change an adversary cannot replay them indefinitely.
40 * OTOH, we don't want to spend too much time generating PONG signatures,
41 * so they must have some lifetime to reduce our CPU usage.
43 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
46 * After how long do we expire an address in a HELLO that we just
47 * validated? This value is also used for our own addresses when we
50 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
53 * How long before an existing address expires should we again try to
54 * validate it? Must be (significantly) smaller than
55 * HELLO_ADDRESS_EXPIRATION.
57 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
60 * Size of the validation map hashmap.
62 #define VALIDATION_MAP_SIZE 256
65 * Priority to use for PINGs
67 #define PING_PRIORITY 2
70 * Priority to use for PONGs
72 #define PONG_PRIORITY 4
76 * Message used to ask a peer to validate receipt (to check an address
77 * from a HELLO). Followed by the address we are trying to validate,
78 * or an empty address if we are just sending a PING to confirm that a
79 * connection which the receiver (of the PING) initiated is still valid.
81 struct TransportPingMessage
85 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
87 struct GNUNET_MessageHeader header;
90 * Challenge code (to ensure fresh reply).
92 uint32_t challenge GNUNET_PACKED;
95 * Who is the intended recipient?
97 struct GNUNET_PeerIdentity target;
103 * Message used to validate a HELLO. The challenge is included in the
104 * confirmation to make matching of replies to requests possible. The
105 * signature signs our public key, an expiration time and our address.<p>
107 * This message is followed by our transport address that the PING tried
108 * to confirm (if we liked it). The address can be empty (zero bytes)
109 * if the PING had not address either (and we received the request via
110 * a connection that we initiated).
112 struct TransportPongMessage
116 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
118 struct GNUNET_MessageHeader header;
121 * Challenge code from PING (showing freshness). Not part of what
122 * is signed so that we can re-use signatures.
124 uint32_t challenge GNUNET_PACKED;
129 struct GNUNET_CRYPTO_RsaSignature signature;
132 * What are we signing and why? Two possible reason codes can be here:
133 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
134 * plausible address for this peer (pid is set to identity of signer); or
135 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
136 * an address we used to connect to the peer with the given pid.
138 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
141 * When does this signature expire?
143 struct GNUNET_TIME_AbsoluteNBO expiration;
146 * Either the identity of the peer Who signed this message, or the
147 * identity of the peer that we're connected to using the given
148 * address (depending on purpose.type).
150 struct GNUNET_PeerIdentity pid;
153 * Size of address appended to this message (part of what is
154 * being signed, hence not redundant).
162 * Information about an address under validation
164 struct ValidationEntry
168 * Name of the transport.
170 char *transport_name;
173 * The address, actually a pointer to the end
174 * of this struct. Do not free!
179 * Public key of the peer.
181 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
184 * The identity of the peer.
186 struct GNUNET_PeerIdentity pid;
189 * ID of task that will clean up this entry if nothing happens.
191 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
194 * At what time did we send the latest validation request?
196 struct GNUNET_TIME_Absolute send_time;
199 * Until when is this address valid?
200 * ZERO if it is not currently considered valid.
202 struct GNUNET_TIME_Absolute valid_until;
205 * How long until we can try to validate this address again?
206 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
207 * ZERO if the address is considered valid (no validation needed)
208 * otherwise a time in the future if we're currently denying re-validation
210 struct GNUNET_TIME_Absolute validation_block;
213 * Challenge number we used.
226 * Context of currently active requests to peerinfo
227 * for validation of HELLOs.
229 struct CheckHelloValidatedContext
233 * This is a doubly-linked list.
235 struct CheckHelloValidatedContext *next;
238 * This is a doubly-linked list.
240 struct CheckHelloValidatedContext *prev;
243 * Hello that we are validating.
245 const struct GNUNET_HELLO_Message *hello;
248 * Context for peerinfo iteration.
250 struct GNUNET_PEERINFO_IteratorContext *piter;
256 * Head of linked list of HELLOs awaiting validation.
258 static struct CheckHelloValidatedContext *chvc_head;
261 * Tail of linked list of HELLOs awaiting validation
263 static struct CheckHelloValidatedContext *chvc_tail;
266 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
267 * of the given peer that we are currently validating, have validated
268 * or are blocked from re-validation for a while).
270 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
273 * Map of PeerIdentities to 'struct GST_ValidationIteratorContext's.
275 static struct GNUNET_CONTAINER_MultiHashMap *notify_map;
279 * Start the validation subsystem.
282 GST_validation_start ()
284 validation_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE);
285 notify_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE);
290 * Iterate over validation entries and free them.
292 * @param cls (unused)
293 * @param key peer identity (unused)
294 * @param value a 'struct ValidationEntry' to clean up
295 * @return GNUNET_YES (continue to iterate)
298 cleanup_validation_entry (void *cls,
299 const GNUNET_HashCode *key,
302 struct ValidationEntry *ve = value;
304 GNUNET_free (ve->transport_name);
305 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
307 GNUNET_SCHEDULER_cancel (ve->timeout_task);
308 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
316 * Stop the validation subsystem.
319 GST_validation_stop ()
321 struct CheckHelloValidatedContext *chvc;
323 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
324 &cleanup_validation_entry,
326 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
327 validation_map = NULL;
328 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (notify_map) == 0);
329 GNUNET_CONTAINER_multihashmap_destroy (notify_map);
331 while (NULL != (chvc = chvc_head))
333 GNUNET_CONTAINER_DLL_remove (chvc_head,
336 GNUNET_PEERINFO_iterate_cancel (chvc->piter);
343 * Address validation cleanup task (record no longer needed).
345 * @param cls the 'struct ValidationEntry'
346 * @param tc scheduler context (unused)
349 timeout_hello_validation (void *cls,
350 const struct GNUNET_SCHEDULER_TaskContext *tc)
352 struct ValidationEntry *va = cls;
354 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
355 GNUNET_STATISTICS_update (GST_stats,
356 gettext_noop ("# address records discarded"),
359 GNUNET_break (GNUNET_OK ==
360 GNUNET_CONTAINER_multihashmap_remove (validation_map,
363 GNUNET_free (va->transport_name);
369 * Context for the validation entry match function.
371 struct ValidationEntryMatchContext
374 * Where to store the result?
376 struct ValidationEntry *ve;
379 * Transport name we're looking for.
381 const char *transport_name;
384 * Address we're interested in.
389 * Number of bytes in 'addr'.
396 * Iterate over validation entries until a matching one is found.
398 * @param cls the 'struct ValidationEntryMatchContext'
399 * @param key peer identity (unused)
400 * @param value a 'struct ValidationEntry' to match
401 * @return GNUNET_YES if the entry does not match,
402 * GNUNET_NO if the entry does match
405 validation_entry_match (void *cls,
406 const GNUNET_HashCode *key,
409 struct ValidationEntryMatchContext *vemc = cls;
410 struct ValidationEntry *ve = value;
412 if ( (ve->addrlen == vemc->addrlen) &&
413 (0 == memcmp (ve->addr, vemc->addr, ve->addrlen)) &&
414 (0 == strcmp (ve->transport_name, vemc->transport_name)) )
424 * Find a ValidationEntry entry for the given neighbour that matches
425 * the given address and transport. If none exists, create one (but
426 * without starting any validation).
428 * @param public_key public key of the peer, NULL for unknown
429 * @param neighbour which peer we care about
430 * @param tname name of the transport plugin
431 * @param session session to look for, NULL for 'any'; otherwise
432 * can be used for the service to "learn" this session ID
434 * @param addr binary address
435 * @param addrlen length of addr
436 * @return validation entry matching the given specifications, NULL
437 * if we don't have an existing entry and no public key was given
439 static struct ValidationEntry *
440 find_validation_entry (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
441 const struct GNUNET_PeerIdentity *neighbour,
446 struct ValidationEntryMatchContext vemc;
447 struct ValidationEntry *ve;
450 vemc.transport_name = tname;
452 vemc.addrlen = addrlen;
453 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
454 &neighbour->hashPubKey,
455 &validation_entry_match,
457 if (NULL != (ve = vemc.ve))
459 if (public_key == NULL)
461 ve = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
462 ve->transport_name = GNUNET_strdup (tname);
463 ve->addr = (void*) &ve[1];
464 ve->public_key = *public_key;
465 ve->pid = *neighbour;
466 ve->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
468 memcpy (&ve[1], addr, addrlen);
469 ve->addrlen = addrlen;
470 GNUNET_CONTAINER_multihashmap_put (validation_map,
471 &neighbour->hashPubKey,
473 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
479 * Send the given PONG to the given address.
481 * @param cls the PONG message
482 * @param target peer this change is about, never NULL
483 * @param valid_until is ZERO if we never validated the address,
484 * otherwise a time up to when we consider it (or was) valid
485 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
486 * is ZERO if the address is considered valid (no validation needed)
487 * otherwise a time in the future if we're currently denying re-validation
488 * @param plugin_name name of the plugin
489 * @param plugin_address binary address
490 * @param plugin_address_len length of address
493 multicast_pong (void *cls,
494 const struct GNUNET_PeerIdentity *target,
495 struct GNUNET_TIME_Absolute valid_until,
496 struct GNUNET_TIME_Absolute validation_block,
497 const char *plugin_name,
498 const void *plugin_address,
499 size_t plugin_address_len)
501 struct TransportPongMessage *pong = cls;
502 struct GNUNET_TRANSPORT_PluginFunctions *papi;
504 papi = GST_plugins_find (plugin_name);
507 (void) papi->send (papi->cls,
510 ntohs (pong->header.size),
512 HELLO_REVALIDATION_START_TIME,
522 * We've received a PING. If appropriate, generate a PONG.
524 * @param sender peer sending the PING
525 * @param hdr the PING
526 * @param session session we got the PING from
527 * @param plugin_name name of plugin that received the PING
528 * @param sender_address address of the sender as known to the plugin, NULL
529 * if we did not initiate the connection
530 * @param sender_address_len number of bytes in sender_address
533 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
534 const struct GNUNET_MessageHeader *hdr,
535 const char *plugin_name,
536 struct Session *session,
537 const void *sender_address,
538 size_t sender_address_len)
541 const struct TransportPingMessage *ping;
542 struct TransportPongMessage *pong;
543 struct GNUNET_TRANSPORT_PluginFunctions *papi;
544 struct SessionHeader *session_header;
551 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
556 ping = (const struct TransportPingMessage *) hdr;
557 if (0 != memcmp (&ping->target,
559 sizeof (struct GNUNET_PeerIdentity)))
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
565 (sender_address != NULL)
566 ? GST_plugin_a2s (plugin_name,
570 GNUNET_i2s (&ping->target));
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
576 "Processing `%s' from `%s'\n",
578 (sender_address != NULL)
579 ? GST_plugin_a2s (plugin_name,
584 GNUNET_STATISTICS_update (GST_stats,
585 gettext_noop ("# PING messages received"),
588 addr = (const char*) &ping[1];
589 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
592 /* peer wants to confirm that we have an outbound connection to him;
593 we handle this case here even though it has nothing to do with
594 address validation (!) */
595 if ( (sender_address == NULL) || (session == NULL) )
597 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
598 _("Refusing to create PONG since I do initiate the session with `%s'.\n"),
599 GNUNET_i2s (sender));
602 session_header = (struct SessionHeader *)session;
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Creating PONG indicating that we initiated a connection to peer `%s' using address `%s' \n",
607 GST_plugin_a2s (plugin_name,
609 sender_address_len));
611 slen = strlen (plugin_name) + 1;
612 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
613 pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
614 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
616 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
618 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
619 sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
620 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
621 pong->challenge = ping->challenge;
622 pong->addrlen = htonl(sender_address_len + slen);
627 memcpy (&((char*)&pong[1])[slen],
630 if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value <
631 PONG_SIGNATURE_LIFETIME.rel_value / 4)
633 /* create / update cached sig */
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Creating PONG signature to indicate active connection.\n");
638 session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
639 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
640 GNUNET_assert (GNUNET_OK ==
641 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
643 &session_header->pong_signature));
647 pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
649 pong->signature = session_header->pong_signature;
653 /* peer wants to confirm that this is one of our addresses, this is what is
654 used for address validation */
655 struct GNUNET_CRYPTO_RsaSignature *sig_cache;
656 struct GNUNET_TIME_Absolute *sig_cache_exp;
658 addrend = memchr (addr, '\0', alen);
669 GST_hello_test_address (addr,
675 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
676 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
677 GST_plugins_a2s (addr,
683 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
684 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
685 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
687 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
689 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
690 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
691 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
692 pong->challenge = ping->challenge;
693 pong->addrlen = htonl(alen + slen);
694 pong->pid = GST_my_identity;
695 memcpy (&pong[1], addr, slen);
696 memcpy (&((char*)&pong[1])[slen], addrend, alen);
697 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
699 /* create / update cached sig */
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Creating PONG signature to indicate ownership.\n");
704 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
705 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
706 GNUNET_assert (GNUNET_OK ==
707 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
713 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
715 pong->signature = *sig_cache;
718 /* first see if the session we got this PING from can be used to transmit
719 a response reliably */
720 papi = GST_plugins_find (plugin_name);
724 ret = papi->send (papi->cls,
727 ntohs (pong->header.size),
729 HELLO_REVALIDATION_START_TIME,
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Transmitted PONG to `%s' via reliable mechanism\n",
739 GNUNET_i2s (sender));
741 GNUNET_STATISTICS_update (GST_stats,
742 gettext_noop ("# PONGs unicast via reliable transport"),
749 /* no reliable method found, try transmission via all known addresses */
750 GNUNET_STATISTICS_update (GST_stats,
751 gettext_noop ("# PONGs multicast to all available addresses"),
754 (void) GST_validation_get_addresses (sender,
763 * Context for the 'validate_address' function
765 struct ValidateAddressContext
768 * Hash of the public key of the peer whose address is being validated.
770 struct GNUNET_PeerIdentity pid;
773 * Public key of the peer whose address is being validated.
775 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
780 * Iterator callback to go over all addresses and try to validate them
781 * (unless blocked or already validated).
783 * @param cls pointer to a 'struct ValidateAddressContext'
784 * @param tname name of the transport
785 * @param expiration expiration time
786 * @param addr the address
787 * @param addrlen length of the address
788 * @return GNUNET_OK (keep the address)
791 validate_address (void *cls,
793 struct GNUNET_TIME_Absolute expiration,
797 const struct ValidateAddressContext *vac = cls;
798 const struct GNUNET_PeerIdentity *pid = &vac->pid;
799 struct ValidationEntry *ve;
800 struct TransportPingMessage ping;
801 struct GNUNET_TRANSPORT_PluginFunctions *papi;
806 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
807 return GNUNET_OK; /* expired */
808 ve = find_validation_entry (&vac->public_key, pid, tname, addr, addrlen);
809 if (GNUNET_TIME_absolute_get_remaining (ve->validation_block).rel_value > 0)
810 return GNUNET_OK; /* blocked */
811 if ( (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task) &&
812 (GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value > 0) )
813 return GNUNET_OK; /* revalidation task already scheduled & still valid */
814 ve->validation_block = GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME);
815 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
816 GNUNET_SCHEDULER_cancel (ve->timeout_task);
817 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_REVALIDATION_START_TIME,
818 &timeout_hello_validation,
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Transmitting plain PING to `%s'\n",
823 ping.header.size = htons(sizeof(struct TransportPingMessage));
824 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
825 ping.challenge = htonl(ve->challenge);
828 slen = strlen(ve->transport_name) + 1;
829 tsize = sizeof(struct TransportPingMessage) + ve->addrlen + slen;
831 char message_buf[tsize];
833 memcpy(message_buf, &ping, sizeof (struct TransportPingMessage));
834 memcpy(&message_buf[sizeof (struct TransportPingMessage)],
837 memcpy(&message_buf[sizeof (struct TransportPingMessage) + slen],
840 papi = GST_plugins_find (ve->transport_name);
844 ret = papi->send (papi->cls,
849 HELLO_REVALIDATION_START_TIME,
850 NULL /* no session */,
858 ve->send_time = GNUNET_TIME_absolute_get ();
859 GNUNET_STATISTICS_update (GST_stats,
860 gettext_noop ("# PING without HELLO messages sent"),
869 * Do address validation again to keep address valid.
871 * @param cls the 'struct ValidationEntry'
872 * @param tc scheduler context (unused)
875 revalidate_address (void *cls,
876 const struct GNUNET_SCHEDULER_TaskContext *tc)
878 struct ValidationEntry *ve = cls;
879 struct GNUNET_TIME_Relative delay;
880 struct ValidateAddressContext vac;
882 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
883 delay = GNUNET_TIME_absolute_get_remaining (ve->validation_block);
884 if (delay.rel_value > 0)
886 /* should wait a bit longer */
887 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
892 GNUNET_STATISTICS_update (GST_stats,
893 gettext_noop ("# address revalidations started"),
897 vac.public_key = ve->public_key;
898 validate_address (&vac,
902 (uint16_t) ve->addrlen);
907 * Add the validated peer address to the HELLO.
909 * @param cls the 'struct ValidationEntry' with the validated address
910 * @param max space in buf
911 * @param buf where to add the address
914 add_valid_peer_address (void *cls,
918 struct ValidationEntry *ve = cls;
920 return GNUNET_HELLO_add_address (ve->transport_name,
930 * We've received a PONG. Check if it matches a pending PING and
931 * mark the respective address as confirmed.
933 * @param sender peer sending the PONG
934 * @param hdr the PONG
935 * @param plugin_name name of plugin that received the PONG
936 * @param sender_address address of the sender as known to the plugin, NULL
937 * if we did not initiate the connection
938 * @param sender_address_len number of bytes in sender_address
941 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
942 const struct GNUNET_MessageHeader *hdr,
943 const char *plugin_name,
944 const void *sender_address,
945 size_t sender_address_len)
947 const struct TransportPongMessage *pong;
948 struct ValidationEntry *ve;
954 struct GNUNET_TIME_Relative delay;
955 struct GNUNET_HELLO_Message *hello;
957 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
962 GNUNET_STATISTICS_update (GST_stats,
963 gettext_noop ("# PONG messages received"),
966 pong = (const struct TransportPongMessage *) hdr;
967 if (0 != memcmp (&pong->pid,
969 sizeof (struct GNUNET_PeerIdentity)))
971 /* PONG is validating inbound session, not an address, not the case
972 used for address validation, ignore here! */
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
977 "Processing `%s' from `%s'\n",
979 (sender_address != NULL)
980 ? GST_plugin_a2s (plugin_name,
985 addr = (const char*) &pong[1];
986 alen = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
987 addrend = memchr (addr, '\0', alen);
996 ve = find_validation_entry (NULL,
1003 GNUNET_STATISTICS_update (GST_stats,
1004 gettext_noop ("# PONGs dropped, no matching pending validation"),
1009 /* now check that PONG is well-formed */
1010 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
1012 GNUNET_STATISTICS_update (GST_stats,
1013 gettext_noop ("# PONGs dropped, signature expired"),
1019 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1024 GNUNET_break_op (0);
1028 /* validity achieved, remember it! */
1029 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1031 /* build HELLO to store in PEERINFO */
1032 hello = GNUNET_HELLO_create (&ve->public_key,
1033 &add_valid_peer_address,
1035 GNUNET_PEERINFO_add_peer (GST_peerinfo,
1037 GNUNET_free (hello);
1039 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
1040 GNUNET_SCHEDULER_cancel (ve->timeout_task);
1042 /* randomly delay by up to 1h to avoid synchronous validations */
1043 rdelay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1045 delay = GNUNET_TIME_relative_add (HELLO_REVALIDATION_START_TIME,
1046 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1048 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
1049 &revalidate_address,
1055 * We've received a HELLO, check which addresses are new and trigger
1058 * @param hello the HELLO we received
1061 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1063 const struct GNUNET_HELLO_Message* hm = (const struct GNUNET_HELLO_Message*) hello;
1064 struct ValidateAddressContext vac;
1067 GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1069 GNUNET_HELLO_get_key (hm, &vac.public_key)) )
1071 /* malformed HELLO */
1075 GNUNET_assert (NULL ==
1076 GNUNET_HELLO_iterate_addresses (hm,
1084 * Opaque handle to stop incremental validation address callbacks.
1086 struct GST_ValidationIteratorContext
1089 * Function to call on each address.
1091 GST_ValidationAddressCallback cb;
1099 * Which peer are we monitoring?
1101 struct GNUNET_PeerIdentity target;
1106 * Call the callback in the closure for each validation entry.
1108 * @param cls the 'struct GST_ValidationIteratorContext'
1109 * @param key the peer's identity
1110 * @param value the 'struct ValidationEntry'
1111 * @return GNUNET_OK (continue to iterate)
1114 iterate_addresses (void *cls,
1115 const GNUNET_HashCode *key,
1118 struct GST_ValidationIteratorContext *vic = cls;
1119 struct ValidationEntry *ve = value;
1121 vic->cb (vic->cb_cls,
1124 ve->validation_block,
1133 * Call the given function for each address for the given target.
1134 * Can either give a snapshot (synchronous API) or be continuous.
1136 * @param target peer information is requested for
1137 * @param snapshot_only GNUNET_YES to iterate over addresses once, GNUNET_NO to
1138 * continue to give information about addresses as it evolves
1139 * @param cb function to call; will not be called after this function returns
1140 * if snapshot_only is GNUNET_YES
1141 * @param cb_cls closure for 'cb'
1142 * @return context to cancel, NULL if 'snapshot_only' is GNUNET_YES
1144 struct GST_ValidationIteratorContext *
1145 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1147 GST_ValidationAddressCallback cb,
1150 struct GST_ValidationIteratorContext *vic;
1152 vic = GNUNET_malloc (sizeof (struct GST_ValidationIteratorContext));
1154 vic->cb_cls = cb_cls;
1155 vic->target = *target;
1156 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
1157 &target->hashPubKey,
1160 if (GNUNET_YES == snapshot_only)
1165 GNUNET_CONTAINER_multihashmap_put (notify_map,
1166 &target->hashPubKey,
1168 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1174 * Cancel an active validation address iteration.
1176 * @param ctx the context of the operation that is cancelled
1179 GST_validation_get_addresses_cancel (struct GST_ValidationIteratorContext *ctx)
1181 GNUNET_assert (GNUNET_OK ==
1182 GNUNET_CONTAINER_multihashmap_remove (notify_map,
1183 &ctx->target.hashPubKey,
1189 /* end of file gnunet-service-transport_validation.c */