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_ats_service.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet_signatures.h"
36 // TODO: observe latency between PING/PONG and give information to ATS!
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 long before an existing address expires should we again try to
56 * validate it? Must be (significantly) smaller than
57 * HELLO_ADDRESS_EXPIRATION.
59 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
62 * Size of the validation map hashmap.
64 #define VALIDATION_MAP_SIZE 256
67 * Priority to use for PINGs
69 #define PING_PRIORITY 2
72 * Priority to use for PONGs
74 #define PONG_PRIORITY 4
78 * Message used to ask a peer to validate receipt (to check an address
79 * from a HELLO). Followed by the address we are trying to validate,
80 * or an empty address if we are just sending a PING to confirm that a
81 * connection which the receiver (of the PING) initiated is still valid.
83 struct TransportPingMessage
87 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
89 struct GNUNET_MessageHeader header;
92 * Challenge code (to ensure fresh reply).
94 uint32_t challenge GNUNET_PACKED;
97 * Who is the intended recipient?
99 struct GNUNET_PeerIdentity target;
105 * Message used to validate a HELLO. The challenge is included in the
106 * confirmation to make matching of replies to requests possible. The
107 * signature signs our public key, an expiration time and our address.<p>
109 * This message is followed by our transport address that the PING tried
110 * to confirm (if we liked it). The address can be empty (zero bytes)
111 * if the PING had not address either (and we received the request via
112 * a connection that we initiated).
114 struct TransportPongMessage
118 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
120 struct GNUNET_MessageHeader header;
123 * Challenge code from PING (showing freshness). Not part of what
124 * is signed so that we can re-use signatures.
126 uint32_t challenge GNUNET_PACKED;
131 struct GNUNET_CRYPTO_RsaSignature signature;
134 * What are we signing and why? Two possible reason codes can be here:
135 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
136 * plausible address for this peer (pid is set to identity of signer); or
137 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
138 * an address we used to connect to the peer with the given pid.
140 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
143 * When does this signature expire?
145 struct GNUNET_TIME_AbsoluteNBO expiration;
148 * Either the identity of the peer Who signed this message, or the
149 * identity of the peer that we're connected to using the given
150 * address (depending on purpose.type).
152 struct GNUNET_PeerIdentity pid;
155 * Size of address appended to this message (part of what is
156 * being signed, hence not redundant).
158 uint32_t addrlen GNUNET_PACKED;
164 * Information about an address under validation
166 struct ValidationEntry
170 * Name of the transport.
172 char *transport_name;
175 * The address, actually a pointer to the end
176 * of this struct. Do not free!
181 * Handle to the blacklist check (if we're currently in it).
183 struct GST_BlacklistCheck *bc;
186 * Public key of the peer.
188 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
191 * The identity of the peer.
193 struct GNUNET_PeerIdentity pid;
196 * ID of task that will clean up this entry if nothing happens.
198 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
201 * At what time did we send the latest validation request?
203 struct GNUNET_TIME_Absolute send_time;
206 * Until when is this address valid?
207 * ZERO if it is not currently considered valid.
209 struct GNUNET_TIME_Absolute valid_until;
212 * How long until we can try to validate this address again?
213 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
214 * ZERO if the address is considered valid (no validation needed)
215 * otherwise a time in the future if we're currently denying re-validation
217 struct GNUNET_TIME_Absolute validation_block;
220 * Challenge number we used.
233 * Context of currently active requests to peerinfo
234 * for validation of HELLOs.
236 struct CheckHelloValidatedContext
240 * This is a doubly-linked list.
242 struct CheckHelloValidatedContext *next;
245 * This is a doubly-linked list.
247 struct CheckHelloValidatedContext *prev;
250 * Hello that we are validating.
252 const struct GNUNET_HELLO_Message *hello;
258 * Head of linked list of HELLOs awaiting validation.
260 static struct CheckHelloValidatedContext *chvc_head;
263 * Tail of linked list of HELLOs awaiting validation
265 static struct CheckHelloValidatedContext *chvc_tail;
268 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
269 * of the given peer that we are currently validating, have validated
270 * or are blocked from re-validation for a while).
272 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
275 * Context for peerinfo iteration.
277 static struct GNUNET_PEERINFO_NotifyContext *pnc;
281 * Context for the validation entry match function.
283 struct ValidationEntryMatchContext
286 * Where to store the result?
288 struct ValidationEntry *ve;
291 * Transport name we're looking for.
293 const char *transport_name;
296 * Address we're interested in.
301 * Number of bytes in 'addr'.
308 * Iterate over validation entries until a matching one is found.
310 * @param cls the 'struct ValidationEntryMatchContext'
311 * @param key peer identity (unused)
312 * @param value a 'struct ValidationEntry' to match
313 * @return GNUNET_YES if the entry does not match,
314 * GNUNET_NO if the entry does match
317 validation_entry_match (void *cls,
318 const GNUNET_HashCode *key,
321 struct ValidationEntryMatchContext *vemc = cls;
322 struct ValidationEntry *ve = value;
324 if ( (ve->addrlen == vemc->addrlen) &&
325 (0 == memcmp (ve->addr, vemc->addr, ve->addrlen)) &&
326 (0 == strcmp (ve->transport_name, vemc->transport_name)) )
336 * Find a ValidationEntry entry for the given neighbour that matches
337 * the given address and transport. If none exists, create one (but
338 * without starting any validation).
340 * @param public_key public key of the peer, NULL for unknown
341 * @param neighbour which peer we care about
342 * @param tname name of the transport plugin
343 * @param session session to look for, NULL for 'any'; otherwise
344 * can be used for the service to "learn" this session ID
346 * @param addr binary address
347 * @param addrlen length of addr
348 * @return validation entry matching the given specifications, NULL
349 * if we don't have an existing entry and no public key was given
351 static struct ValidationEntry *
352 find_validation_entry (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
353 const struct GNUNET_PeerIdentity *neighbour,
358 struct ValidationEntryMatchContext vemc;
359 struct ValidationEntry *ve;
362 vemc.transport_name = tname;
364 vemc.addrlen = addrlen;
365 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
366 &neighbour->hashPubKey,
367 &validation_entry_match,
369 if (NULL != (ve = vemc.ve))
371 if (public_key == NULL)
373 ve = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
374 ve->transport_name = GNUNET_strdup (tname);
375 ve->addr = (void*) &ve[1];
376 ve->public_key = *public_key;
377 ve->pid = *neighbour;
378 ve->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
380 memcpy (&ve[1], addr, addrlen);
381 ve->addrlen = addrlen;
382 GNUNET_CONTAINER_multihashmap_put (validation_map,
383 &neighbour->hashPubKey,
385 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
391 * Iterator which adds the given address to the set of validated
394 * @param cls original HELLO message
395 * @param tname name of the transport
396 * @param expiration expiration time
397 * @param addr the address
398 * @param addrlen length of the address
399 * @return GNUNET_OK (keep the address)
402 add_valid_address (void *cls,
404 struct GNUNET_TIME_Absolute expiration,
408 const struct GNUNET_HELLO_Message *hello = cls;
409 struct ValidationEntry *ve;
410 struct GNUNET_PeerIdentity pid;
411 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
413 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
414 return GNUNET_OK; /* expired */
416 GNUNET_HELLO_get_id (hello, &pid)) ||
418 GNUNET_HELLO_get_key (hello, &public_key)) )
421 return GNUNET_OK; /* invalid HELLO !? */
423 ve = find_validation_entry (&public_key, &pid, tname, addr, addrlen);
424 ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until,
426 GNUNET_ATS_address_update (GST_ats,
439 * Function called for any HELLO known to PEERINFO.
442 * @param peer id of the peer, NULL for last call
443 * @param hello hello message for the peer (can be NULL)
444 * @param error message
447 process_peerinfo_hello (void *cls,
448 const struct GNUNET_PeerIdentity *peer,
449 const struct GNUNET_HELLO_Message *hello,
452 GNUNET_assert (NULL != peer);
455 GNUNET_assert (NULL ==
456 GNUNET_HELLO_iterate_addresses (hello,
464 * Start the validation subsystem.
467 GST_validation_start ()
469 validation_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE);
470 pnc = GNUNET_PEERINFO_notify (GST_cfg,
471 &process_peerinfo_hello,
477 * Iterate over validation entries and free them.
479 * @param cls (unused)
480 * @param key peer identity (unused)
481 * @param value a 'struct ValidationEntry' to clean up
482 * @return GNUNET_YES (continue to iterate)
485 cleanup_validation_entry (void *cls,
486 const GNUNET_HashCode *key,
489 struct ValidationEntry *ve = value;
493 GST_blacklist_test_cancel (ve->bc);
496 GNUNET_break (GNUNET_OK ==
497 GNUNET_CONTAINER_multihashmap_remove (validation_map,
500 GNUNET_free (ve->transport_name);
501 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
503 GNUNET_SCHEDULER_cancel (ve->timeout_task);
504 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
512 * Stop the validation subsystem.
515 GST_validation_stop ()
517 struct CheckHelloValidatedContext *chvc;
519 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
520 &cleanup_validation_entry,
522 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
523 validation_map = NULL;
524 while (NULL != (chvc = chvc_head))
526 GNUNET_CONTAINER_DLL_remove (chvc_head,
531 GNUNET_PEERINFO_notify_cancel (pnc);
536 * Address validation cleanup task (record no longer needed).
538 * @param cls the 'struct ValidationEntry'
539 * @param tc scheduler context (unused)
542 timeout_hello_validation (void *cls,
543 const struct GNUNET_SCHEDULER_TaskContext *tc)
545 struct ValidationEntry *ve = cls;
547 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
548 GNUNET_STATISTICS_update (GST_stats,
549 gettext_noop ("# address records discarded"),
552 cleanup_validation_entry (NULL, &ve->pid.hashPubKey, ve);
557 * Send the given PONG to the given address.
559 * @param cls the PONG message
560 * @param public_key public key for the peer, never NULL
561 * @param target peer this change is about, never NULL
562 * @param valid_until is ZERO if we never validated the address,
563 * otherwise a time up to when we consider it (or was) valid
564 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
565 * is ZERO if the address is considered valid (no validation needed)
566 * otherwise a time in the future if we're currently denying re-validation
567 * @param plugin_name name of the plugin
568 * @param plugin_address binary address
569 * @param plugin_address_len length of address
572 multicast_pong (void *cls,
573 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
574 const struct GNUNET_PeerIdentity *target,
575 struct GNUNET_TIME_Absolute valid_until,
576 struct GNUNET_TIME_Absolute validation_block,
577 const char *plugin_name,
578 const void *plugin_address,
579 size_t plugin_address_len)
581 struct TransportPongMessage *pong = cls;
582 struct GNUNET_TRANSPORT_PluginFunctions *papi;
584 papi = GST_plugins_find (plugin_name);
587 (void) papi->send (papi->cls,
590 ntohs (pong->header.size),
592 HELLO_REVALIDATION_START_TIME,
602 * We've received a PING. If appropriate, generate a PONG.
604 * @param sender peer sending the PING
605 * @param hdr the PING
606 * @param session session we got the PING from
607 * @param plugin_name name of plugin that received the PING
608 * @param sender_address address of the sender as known to the plugin, NULL
609 * if we did not initiate the connection
610 * @param sender_address_len number of bytes in sender_address
613 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
614 const struct GNUNET_MessageHeader *hdr,
615 const char *plugin_name,
616 struct Session *session,
617 const void *sender_address,
618 size_t sender_address_len)
621 const struct TransportPingMessage *ping;
622 struct TransportPongMessage *pong;
623 struct GNUNET_TRANSPORT_PluginFunctions *papi;
624 struct GNUNET_CRYPTO_RsaSignature *sig_cache;
625 struct GNUNET_TIME_Absolute *sig_cache_exp;
632 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
637 ping = (const struct TransportPingMessage *) hdr;
638 if (0 != memcmp (&ping->target,
640 sizeof (struct GNUNET_PeerIdentity)))
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
647 "Processing `%s' from `%s'\n",
649 (sender_address != NULL)
650 ? GST_plugin_a2s (plugin_name,
655 GNUNET_STATISTICS_update (GST_stats,
656 gettext_noop ("# PING messages received"),
659 addr = (const char*) &ping[1];
660 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
661 /* peer wants to confirm that this is one of our addresses, this is what is
662 used for address validation */
664 addrend = memchr (addr, '\0', alen);
675 GST_hello_test_address (addr,
681 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
682 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
683 GST_plugins_a2s (addr,
689 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
690 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
691 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
693 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
695 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
696 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
697 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
698 pong->challenge = ping->challenge;
699 pong->addrlen = htonl(alen + slen);
700 pong->pid = GST_my_identity;
701 memcpy (&pong[1], addr, slen);
702 memcpy (&((char*)&pong[1])[slen], addrend, alen);
703 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
705 /* create / update cached sig */
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Creating PONG signature to indicate ownership.\n");
710 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
711 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
712 GNUNET_assert (GNUNET_OK ==
713 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
719 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
721 pong->signature = *sig_cache;
723 /* first see if the session we got this PING from can be used to transmit
724 a response reliably */
725 papi = GST_plugins_find (plugin_name);
729 ret = papi->send (papi->cls,
732 ntohs (pong->header.size),
734 HELLO_REVALIDATION_START_TIME,
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "Transmitted PONG to `%s' via reliable mechanism\n",
744 GNUNET_i2s (sender));
746 GNUNET_STATISTICS_update (GST_stats,
747 gettext_noop ("# PONGs unicast via reliable transport"),
754 /* no reliable method found, try transmission via all known addresses */
755 GNUNET_STATISTICS_update (GST_stats,
756 gettext_noop ("# PONGs multicast to all available addresses"),
759 GST_validation_get_addresses (sender,
767 * Context for the 'validate_address' function
769 struct ValidateAddressContext
772 * Hash of the public key of the peer whose address is being validated.
774 struct GNUNET_PeerIdentity pid;
777 * Public key of the peer whose address is being validated.
779 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
784 * Function called with the result from blacklisting.
785 * Send a PING to the other peer if a communication is allowed.
787 * @param cls ou r'struct ValidationEntry'
788 * @param pid identity of the other peer
789 * @param result GNUNET_OK if the connection is allowed, GNUNET_NO if not
792 transmit_ping_if_allowed (void *cls,
793 const struct GNUNET_PeerIdentity *pid,
796 struct ValidationEntry *ve = cls;
797 struct TransportPingMessage ping;
798 struct GNUNET_TRANSPORT_PluginFunctions *papi;
799 const struct GNUNET_MessageHeader *hello;
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807 "Transmitting plain PING to `%s'\n",
809 ping.header.size = htons(sizeof(struct TransportPingMessage));
810 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
811 ping.challenge = htonl(ve->challenge);
814 slen = strlen(ve->transport_name) + 1;
815 hello = GST_hello_get ();
816 hsize = ntohs (hello->size);
817 tsize = sizeof(struct TransportPingMessage) + ve->addrlen + slen + hsize;
818 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
821 _("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
824 (unsigned int) tsize);
825 /* message too big (!?), get rid of HELLO */
827 tsize = sizeof(struct TransportPingMessage) + ve->addrlen + slen + hsize;
830 char message_buf[tsize];
832 memcpy(message_buf, hello, hsize);
833 memcpy(&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
834 memcpy(&message_buf[sizeof (struct TransportPingMessage) + hsize],
837 memcpy(&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
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"),
868 * Iterator callback to go over all addresses and try to validate them
869 * (unless blocked or already validated).
871 * @param cls pointer to a 'struct ValidateAddressContext'
872 * @param tname name of the transport
873 * @param expiration expiration time
874 * @param addr the address
875 * @param addrlen length of the address
876 * @return GNUNET_OK (keep the address)
879 validate_address (void *cls,
881 struct GNUNET_TIME_Absolute expiration,
885 const struct ValidateAddressContext *vac = cls;
886 const struct GNUNET_PeerIdentity *pid = &vac->pid;
887 struct ValidationEntry *ve;
889 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
890 return GNUNET_OK; /* expired */
891 ve = find_validation_entry (&vac->public_key, pid, tname, addr, addrlen);
892 if (GNUNET_TIME_absolute_get_remaining (ve->validation_block).rel_value > 0)
893 return GNUNET_OK; /* blocked */
894 if ( (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task) &&
895 (GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value > 0) )
896 return GNUNET_OK; /* revalidation task already scheduled & still valid */
897 ve->validation_block = GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME);
898 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
899 GNUNET_SCHEDULER_cancel (ve->timeout_task);
900 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_REVALIDATION_START_TIME,
901 &timeout_hello_validation,
903 ve->bc = GST_blacklist_test_allowed (pid,
905 &transmit_ping_if_allowed,
912 * Do address validation again to keep address valid.
914 * @param cls the 'struct ValidationEntry'
915 * @param tc scheduler context (unused)
918 revalidate_address (void *cls,
919 const struct GNUNET_SCHEDULER_TaskContext *tc)
921 struct ValidationEntry *ve = cls;
922 struct GNUNET_TIME_Relative delay;
923 struct ValidateAddressContext vac;
925 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
926 delay = GNUNET_TIME_absolute_get_remaining (ve->validation_block);
927 if (delay.rel_value > 0)
929 /* should wait a bit longer */
930 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
935 GNUNET_STATISTICS_update (GST_stats,
936 gettext_noop ("# address revalidations started"),
940 vac.public_key = ve->public_key;
941 validate_address (&vac,
945 (uint16_t) ve->addrlen);
950 * Add the validated peer address to the HELLO.
952 * @param cls the 'struct ValidationEntry' with the validated address
953 * @param max space in buf
954 * @param buf where to add the address
957 add_valid_peer_address (void *cls,
961 struct ValidationEntry *ve = cls;
963 return GNUNET_HELLO_add_address (ve->transport_name,
973 * We've received a PONG. Check if it matches a pending PING and
974 * mark the respective address as confirmed.
976 * @param sender peer sending the PONG
977 * @param hdr the PONG
978 * @param plugin_name name of plugin that received the PONG
979 * @param sender_address address of the sender as known to the plugin, NULL
980 * if we did not initiate the connection
981 * @param sender_address_len number of bytes in sender_address
984 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
985 const struct GNUNET_MessageHeader *hdr,
986 const char *plugin_name,
987 const void *sender_address,
988 size_t sender_address_len)
990 const struct TransportPongMessage *pong;
991 struct ValidationEntry *ve;
997 struct GNUNET_TIME_Relative delay;
998 struct GNUNET_HELLO_Message *hello;
1000 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1002 GNUNET_break_op (0);
1005 GNUNET_STATISTICS_update (GST_stats,
1006 gettext_noop ("# PONG messages received"),
1009 pong = (const struct TransportPongMessage *) hdr;
1010 if (0 != memcmp (&pong->pid,
1012 sizeof (struct GNUNET_PeerIdentity)))
1014 GNUNET_break_op (0);
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1019 "Processing `%s' from `%s'\n",
1021 (sender_address != NULL)
1022 ? GST_plugin_a2s (plugin_name,
1027 addr = (const char*) &pong[1];
1028 alen = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1029 addrend = memchr (addr, '\0', alen);
1030 if (NULL == addrend)
1032 GNUNET_break_op (0);
1036 slen = strlen(addr);
1038 ve = find_validation_entry (NULL,
1045 GNUNET_STATISTICS_update (GST_stats,
1046 gettext_noop ("# PONGs dropped, no matching pending validation"),
1051 /* now check that PONG is well-formed */
1052 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
1054 GNUNET_STATISTICS_update (GST_stats,
1055 gettext_noop ("# PONGs dropped, signature expired"),
1061 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1066 GNUNET_break_op (0);
1070 /* validity achieved, remember it! */
1071 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1072 GNUNET_ATS_address_update (GST_ats,
1079 NULL, 0); /* FIXME: compute and add latency here... */
1081 /* build HELLO to store in PEERINFO */
1082 hello = GNUNET_HELLO_create (&ve->public_key,
1083 &add_valid_peer_address,
1085 GNUNET_PEERINFO_add_peer (GST_peerinfo,
1087 GNUNET_free (hello);
1089 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
1090 GNUNET_SCHEDULER_cancel (ve->timeout_task);
1092 /* randomly delay by up to 1h to avoid synchronous validations */
1093 rdelay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1095 delay = GNUNET_TIME_relative_add (HELLO_REVALIDATION_START_TIME,
1096 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1098 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
1099 &revalidate_address,
1105 * We've received a HELLO, check which addresses are new and trigger
1108 * @param hello the HELLO we received
1111 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1113 const struct GNUNET_HELLO_Message* hm = (const struct GNUNET_HELLO_Message*) hello;
1114 struct ValidateAddressContext vac;
1117 GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1119 GNUNET_HELLO_get_key (hm, &vac.public_key)) )
1121 /* malformed HELLO */
1125 GNUNET_assert (NULL ==
1126 GNUNET_HELLO_iterate_addresses (hm,
1134 * Closure for 'iterate_addresses'
1136 struct IteratorContext
1139 * Function to call on each address.
1141 GST_ValidationAddressCallback cb;
1152 * Call the callback in the closure for each validation entry.
1154 * @param cls the 'struct GST_ValidationIteratorContext'
1155 * @param key the peer's identity
1156 * @param value the 'struct ValidationEntry'
1157 * @return GNUNET_OK (continue to iterate)
1160 iterate_addresses (void *cls,
1161 const GNUNET_HashCode *key,
1164 struct IteratorContext *ic = cls;
1165 struct ValidationEntry *ve = value;
1171 ve->validation_block,
1180 * Call the given function for each address for the given target.
1181 * Can either give a snapshot (synchronous API) or be continuous.
1183 * @param target peer information is requested for
1184 * @param snapshot_only GNUNET_YES to iterate over addresses once, GNUNET_NO to
1185 * continue to give information about addresses as it evolves
1186 * @param cb function to call; will not be called after this function returns
1187 * if snapshot_only is GNUNET_YES
1188 * @param cb_cls closure for 'cb'
1189 * @return context to cancel, NULL if 'snapshot_only' is GNUNET_YES
1192 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1193 GST_ValidationAddressCallback cb,
1196 struct IteratorContext ic;
1200 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
1201 &target->hashPubKey,
1207 /* end of file gnunet-service-transport_validation.c */