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"
35 // TODO: send our HELLO with the PING!
38 * How long is a PONG signature valid? We'll recycle a signature until
39 * 1/4 of this time is remaining. PONGs should expire so that if our
40 * external addresses change an adversary cannot replay them indefinitely.
41 * OTOH, we don't want to spend too much time generating PONG signatures,
42 * so they must have some lifetime to reduce our CPU usage.
44 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
47 * After how long do we expire an address in a HELLO that we just
48 * validated? This value is also used for our own addresses when we
51 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
54 * How long before an existing address expires should we again try to
55 * validate it? Must be (significantly) smaller than
56 * HELLO_ADDRESS_EXPIRATION.
58 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
61 * Size of the validation map hashmap.
63 #define VALIDATION_MAP_SIZE 256
66 * Priority to use for PINGs
68 #define PING_PRIORITY 2
71 * Priority to use for PONGs
73 #define PONG_PRIORITY 4
77 * Message used to ask a peer to validate receipt (to check an address
78 * from a HELLO). Followed by the address we are trying to validate,
79 * or an empty address if we are just sending a PING to confirm that a
80 * connection which the receiver (of the PING) initiated is still valid.
82 struct TransportPingMessage
86 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
88 struct GNUNET_MessageHeader header;
91 * Challenge code (to ensure fresh reply).
93 uint32_t challenge GNUNET_PACKED;
96 * Who is the intended recipient?
98 struct GNUNET_PeerIdentity target;
104 * Message used to validate a HELLO. The challenge is included in the
105 * confirmation to make matching of replies to requests possible. The
106 * signature signs our public key, an expiration time and our address.<p>
108 * This message is followed by our transport address that the PING tried
109 * to confirm (if we liked it). The address can be empty (zero bytes)
110 * if the PING had not address either (and we received the request via
111 * a connection that we initiated).
113 struct TransportPongMessage
117 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
119 struct GNUNET_MessageHeader header;
122 * Challenge code from PING (showing freshness). Not part of what
123 * is signed so that we can re-use signatures.
125 uint32_t challenge GNUNET_PACKED;
130 struct GNUNET_CRYPTO_RsaSignature signature;
133 * What are we signing and why? Two possible reason codes can be here:
134 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
135 * plausible address for this peer (pid is set to identity of signer); or
136 * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
137 * an address we used to connect to the peer with the given pid.
139 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
142 * When does this signature expire?
144 struct GNUNET_TIME_AbsoluteNBO expiration;
147 * Either the identity of the peer Who signed this message, or the
148 * identity of the peer that we're connected to using the given
149 * address (depending on purpose.type).
151 struct GNUNET_PeerIdentity pid;
154 * Size of address appended to this message (part of what is
155 * being signed, hence not redundant).
163 * Information about an address under validation
165 struct ValidationEntry
169 * Name of the transport.
171 char *transport_name;
174 * The address, actually a pointer to the end
175 * of this struct. Do not free!
180 * Public key of the peer.
182 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
185 * The identity of the peer.
187 struct GNUNET_PeerIdentity pid;
190 * ID of task that will clean up this entry if nothing happens.
192 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
195 * At what time did we send the latest validation request?
197 struct GNUNET_TIME_Absolute send_time;
200 * Until when is this address valid?
201 * ZERO if it is not currently considered valid.
203 struct GNUNET_TIME_Absolute valid_until;
206 * How long until we can try to validate this address again?
207 * FOREVER if the address is for an unsupported plugin (from PEERINFO)
208 * ZERO if the address is considered valid (no validation needed)
209 * otherwise a time in the future if we're currently denying re-validation
211 struct GNUNET_TIME_Absolute validation_block;
214 * Challenge number we used.
227 * Context of currently active requests to peerinfo
228 * for validation of HELLOs.
230 struct CheckHelloValidatedContext
234 * This is a doubly-linked list.
236 struct CheckHelloValidatedContext *next;
239 * This is a doubly-linked list.
241 struct CheckHelloValidatedContext *prev;
244 * Hello that we are validating.
246 const struct GNUNET_HELLO_Message *hello;
252 * Head of linked list of HELLOs awaiting validation.
254 static struct CheckHelloValidatedContext *chvc_head;
257 * Tail of linked list of HELLOs awaiting validation
259 static struct CheckHelloValidatedContext *chvc_tail;
262 * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
263 * of the given peer that we are currently validating, have validated
264 * or are blocked from re-validation for a while).
266 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
269 * Map of PeerIdentities to 'struct GST_ValidationIteratorContext's.
271 static struct GNUNET_CONTAINER_MultiHashMap *notify_map;
274 * Context for peerinfo iteration.
276 static struct GNUNET_PEERINFO_NotifyContext *pnc;
280 * Context for the validation entry match function.
282 struct ValidationEntryMatchContext
285 * Where to store the result?
287 struct ValidationEntry *ve;
290 * Transport name we're looking for.
292 const char *transport_name;
295 * Address we're interested in.
300 * Number of bytes in 'addr'.
307 * Iterate over validation entries until a matching one is found.
309 * @param cls the 'struct ValidationEntryMatchContext'
310 * @param key peer identity (unused)
311 * @param value a 'struct ValidationEntry' to match
312 * @return GNUNET_YES if the entry does not match,
313 * GNUNET_NO if the entry does match
316 validation_entry_match (void *cls,
317 const GNUNET_HashCode *key,
320 struct ValidationEntryMatchContext *vemc = cls;
321 struct ValidationEntry *ve = value;
323 if ( (ve->addrlen == vemc->addrlen) &&
324 (0 == memcmp (ve->addr, vemc->addr, ve->addrlen)) &&
325 (0 == strcmp (ve->transport_name, vemc->transport_name)) )
335 * Find a ValidationEntry entry for the given neighbour that matches
336 * the given address and transport. If none exists, create one (but
337 * without starting any validation).
339 * @param public_key public key of the peer, NULL for unknown
340 * @param neighbour which peer we care about
341 * @param tname name of the transport plugin
342 * @param session session to look for, NULL for 'any'; otherwise
343 * can be used for the service to "learn" this session ID
345 * @param addr binary address
346 * @param addrlen length of addr
347 * @return validation entry matching the given specifications, NULL
348 * if we don't have an existing entry and no public key was given
350 static struct ValidationEntry *
351 find_validation_entry (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
352 const struct GNUNET_PeerIdentity *neighbour,
357 struct ValidationEntryMatchContext vemc;
358 struct ValidationEntry *ve;
361 vemc.transport_name = tname;
363 vemc.addrlen = addrlen;
364 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
365 &neighbour->hashPubKey,
366 &validation_entry_match,
368 if (NULL != (ve = vemc.ve))
370 if (public_key == NULL)
372 ve = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
373 ve->transport_name = GNUNET_strdup (tname);
374 ve->addr = (void*) &ve[1];
375 ve->public_key = *public_key;
376 ve->pid = *neighbour;
377 ve->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
379 memcpy (&ve[1], addr, addrlen);
380 ve->addrlen = addrlen;
381 GNUNET_CONTAINER_multihashmap_put (validation_map,
382 &neighbour->hashPubKey,
384 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
390 * Iterator which adds the given address to the set of validated
393 * @param cls original HELLO message
394 * @param tname name of the transport
395 * @param expiration expiration time
396 * @param addr the address
397 * @param addrlen length of the address
398 * @return GNUNET_OK (keep the address)
401 add_valid_address (void *cls,
403 struct GNUNET_TIME_Absolute expiration,
407 const struct GNUNET_HELLO_Message *hello = cls;
408 struct ValidationEntry *ve;
409 struct GNUNET_PeerIdentity pid;
410 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
412 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
413 return GNUNET_OK; /* expired */
415 GNUNET_HELLO_get_id (hello, &pid)) ||
417 GNUNET_HELLO_get_key (hello, &public_key)) )
420 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,
431 * Function called for any HELLO known to PEERINFO.
434 * @param peer id of the peer, NULL for last call
435 * @param hello hello message for the peer (can be NULL)
436 * @param error message
439 process_peerinfo_hello (void *cls,
440 const struct GNUNET_PeerIdentity *peer,
441 const struct GNUNET_HELLO_Message *hello,
444 GNUNET_assert (NULL != peer);
447 GNUNET_assert (NULL ==
448 GNUNET_HELLO_iterate_addresses (hello,
456 * Start the validation subsystem.
459 GST_validation_start ()
461 validation_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE);
462 notify_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE);
463 pnc = GNUNET_PEERINFO_notify (GST_cfg,
464 &process_peerinfo_hello,
470 * Iterate over validation entries and free them.
472 * @param cls (unused)
473 * @param key peer identity (unused)
474 * @param value a 'struct ValidationEntry' to clean up
475 * @return GNUNET_YES (continue to iterate)
478 cleanup_validation_entry (void *cls,
479 const GNUNET_HashCode *key,
482 struct ValidationEntry *ve = value;
484 GNUNET_free (ve->transport_name);
485 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
487 GNUNET_SCHEDULER_cancel (ve->timeout_task);
488 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
496 * Stop the validation subsystem.
499 GST_validation_stop ()
501 struct CheckHelloValidatedContext *chvc;
503 GNUNET_CONTAINER_multihashmap_iterate (validation_map,
504 &cleanup_validation_entry,
506 GNUNET_CONTAINER_multihashmap_destroy (validation_map);
507 validation_map = NULL;
508 GNUNET_assert (GNUNET_CONTAINER_multihashmap_size (notify_map) == 0);
509 GNUNET_CONTAINER_multihashmap_destroy (notify_map);
511 while (NULL != (chvc = chvc_head))
513 GNUNET_CONTAINER_DLL_remove (chvc_head,
518 GNUNET_PEERINFO_notify_cancel (pnc);
523 * Address validation cleanup task (record no longer needed).
525 * @param cls the 'struct ValidationEntry'
526 * @param tc scheduler context (unused)
529 timeout_hello_validation (void *cls,
530 const struct GNUNET_SCHEDULER_TaskContext *tc)
532 struct ValidationEntry *va = cls;
534 va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
535 GNUNET_STATISTICS_update (GST_stats,
536 gettext_noop ("# address records discarded"),
539 GNUNET_break (GNUNET_OK ==
540 GNUNET_CONTAINER_multihashmap_remove (validation_map,
543 GNUNET_free (va->transport_name);
549 * Send the given PONG to the given address.
551 * @param cls the PONG message
552 * @param public_key public key for the peer, never NULL
553 * @param target peer this change is about, never NULL
554 * @param valid_until is ZERO if we never validated the address,
555 * otherwise a time up to when we consider it (or was) valid
556 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
557 * is ZERO if the address is considered valid (no validation needed)
558 * otherwise a time in the future if we're currently denying re-validation
559 * @param plugin_name name of the plugin
560 * @param plugin_address binary address
561 * @param plugin_address_len length of address
564 multicast_pong (void *cls,
565 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
566 const struct GNUNET_PeerIdentity *target,
567 struct GNUNET_TIME_Absolute valid_until,
568 struct GNUNET_TIME_Absolute validation_block,
569 const char *plugin_name,
570 const void *plugin_address,
571 size_t plugin_address_len)
573 struct TransportPongMessage *pong = cls;
574 struct GNUNET_TRANSPORT_PluginFunctions *papi;
576 papi = GST_plugins_find (plugin_name);
579 (void) papi->send (papi->cls,
582 ntohs (pong->header.size),
584 HELLO_REVALIDATION_START_TIME,
594 * We've received a PING. If appropriate, generate a PONG.
596 * @param sender peer sending the PING
597 * @param hdr the PING
598 * @param session session we got the PING from
599 * @param plugin_name name of plugin that received the PING
600 * @param sender_address address of the sender as known to the plugin, NULL
601 * if we did not initiate the connection
602 * @param sender_address_len number of bytes in sender_address
605 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
606 const struct GNUNET_MessageHeader *hdr,
607 const char *plugin_name,
608 struct Session *session,
609 const void *sender_address,
610 size_t sender_address_len)
613 const struct TransportPingMessage *ping;
614 struct TransportPongMessage *pong;
615 struct GNUNET_TRANSPORT_PluginFunctions *papi;
616 struct GNUNET_CRYPTO_RsaSignature *sig_cache;
617 struct GNUNET_TIME_Absolute *sig_cache_exp;
624 if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
629 ping = (const struct TransportPingMessage *) hdr;
630 if (0 != memcmp (&ping->target,
632 sizeof (struct GNUNET_PeerIdentity)))
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
639 "Processing `%s' from `%s'\n",
641 (sender_address != NULL)
642 ? GST_plugin_a2s (plugin_name,
647 GNUNET_STATISTICS_update (GST_stats,
648 gettext_noop ("# PING messages received"),
651 addr = (const char*) &ping[1];
652 alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
653 /* peer wants to confirm that this is one of our addresses, this is what is
654 used for address validation */
656 addrend = memchr (addr, '\0', alen);
667 GST_hello_test_address (addr,
673 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
674 _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
675 GST_plugins_a2s (addr,
681 pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
682 pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
683 pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
685 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
687 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
688 sizeof (struct GNUNET_PeerIdentity) + alen + slen);
689 pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
690 pong->challenge = ping->challenge;
691 pong->addrlen = htonl(alen + slen);
692 pong->pid = GST_my_identity;
693 memcpy (&pong[1], addr, slen);
694 memcpy (&((char*)&pong[1])[slen], addrend, alen);
695 if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
697 /* create / update cached sig */
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "Creating PONG signature to indicate ownership.\n");
702 *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
703 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
704 GNUNET_assert (GNUNET_OK ==
705 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
711 pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
713 pong->signature = *sig_cache;
715 /* first see if the session we got this PING from can be used to transmit
716 a response reliably */
717 papi = GST_plugins_find (plugin_name);
721 ret = papi->send (papi->cls,
724 ntohs (pong->header.size),
726 HELLO_REVALIDATION_START_TIME,
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "Transmitted PONG to `%s' via reliable mechanism\n",
736 GNUNET_i2s (sender));
738 GNUNET_STATISTICS_update (GST_stats,
739 gettext_noop ("# PONGs unicast via reliable transport"),
746 /* no reliable method found, try transmission via all known addresses */
747 GNUNET_STATISTICS_update (GST_stats,
748 gettext_noop ("# PONGs multicast to all available addresses"),
751 (void) GST_validation_get_addresses (sender,
760 * Context for the 'validate_address' function
762 struct ValidateAddressContext
765 * Hash of the public key of the peer whose address is being validated.
767 struct GNUNET_PeerIdentity pid;
770 * Public key of the peer whose address is being validated.
772 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
777 * Iterator callback to go over all addresses and try to validate them
778 * (unless blocked or already validated).
780 * @param cls pointer to a 'struct ValidateAddressContext'
781 * @param tname name of the transport
782 * @param expiration expiration time
783 * @param addr the address
784 * @param addrlen length of the address
785 * @return GNUNET_OK (keep the address)
788 validate_address (void *cls,
790 struct GNUNET_TIME_Absolute expiration,
794 const struct ValidateAddressContext *vac = cls;
795 const struct GNUNET_PeerIdentity *pid = &vac->pid;
796 struct ValidationEntry *ve;
797 struct TransportPingMessage ping;
798 struct GNUNET_TRANSPORT_PluginFunctions *papi;
803 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
804 return GNUNET_OK; /* expired */
805 ve = find_validation_entry (&vac->public_key, pid, tname, addr, addrlen);
806 if (GNUNET_TIME_absolute_get_remaining (ve->validation_block).rel_value > 0)
807 return GNUNET_OK; /* blocked */
808 if ( (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task) &&
809 (GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value > 0) )
810 return GNUNET_OK; /* revalidation task already scheduled & still valid */
811 ve->validation_block = GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME);
812 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
813 GNUNET_SCHEDULER_cancel (ve->timeout_task);
814 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_REVALIDATION_START_TIME,
815 &timeout_hello_validation,
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Transmitting plain PING to `%s'\n",
820 ping.header.size = htons(sizeof(struct TransportPingMessage));
821 ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
822 ping.challenge = htonl(ve->challenge);
825 slen = strlen(ve->transport_name) + 1;
826 tsize = sizeof(struct TransportPingMessage) + ve->addrlen + slen;
828 char message_buf[tsize];
830 memcpy(message_buf, &ping, sizeof (struct TransportPingMessage));
831 memcpy(&message_buf[sizeof (struct TransportPingMessage)],
834 memcpy(&message_buf[sizeof (struct TransportPingMessage) + slen],
837 papi = GST_plugins_find (ve->transport_name);
841 ret = papi->send (papi->cls,
846 HELLO_REVALIDATION_START_TIME,
847 NULL /* no session */,
855 ve->send_time = GNUNET_TIME_absolute_get ();
856 GNUNET_STATISTICS_update (GST_stats,
857 gettext_noop ("# PING without HELLO messages sent"),
866 * Do address validation again to keep address valid.
868 * @param cls the 'struct ValidationEntry'
869 * @param tc scheduler context (unused)
872 revalidate_address (void *cls,
873 const struct GNUNET_SCHEDULER_TaskContext *tc)
875 struct ValidationEntry *ve = cls;
876 struct GNUNET_TIME_Relative delay;
877 struct ValidateAddressContext vac;
879 ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
880 delay = GNUNET_TIME_absolute_get_remaining (ve->validation_block);
881 if (delay.rel_value > 0)
883 /* should wait a bit longer */
884 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
889 GNUNET_STATISTICS_update (GST_stats,
890 gettext_noop ("# address revalidations started"),
894 vac.public_key = ve->public_key;
895 validate_address (&vac,
899 (uint16_t) ve->addrlen);
904 * Add the validated peer address to the HELLO.
906 * @param cls the 'struct ValidationEntry' with the validated address
907 * @param max space in buf
908 * @param buf where to add the address
911 add_valid_peer_address (void *cls,
915 struct ValidationEntry *ve = cls;
917 return GNUNET_HELLO_add_address (ve->transport_name,
927 * We've received a PONG. Check if it matches a pending PING and
928 * mark the respective address as confirmed.
930 * @param sender peer sending the PONG
931 * @param hdr the PONG
932 * @param plugin_name name of plugin that received the PONG
933 * @param sender_address address of the sender as known to the plugin, NULL
934 * if we did not initiate the connection
935 * @param sender_address_len number of bytes in sender_address
938 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
939 const struct GNUNET_MessageHeader *hdr,
940 const char *plugin_name,
941 const void *sender_address,
942 size_t sender_address_len)
944 const struct TransportPongMessage *pong;
945 struct ValidationEntry *ve;
951 struct GNUNET_TIME_Relative delay;
952 struct GNUNET_HELLO_Message *hello;
954 if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
959 GNUNET_STATISTICS_update (GST_stats,
960 gettext_noop ("# PONG messages received"),
963 pong = (const struct TransportPongMessage *) hdr;
964 if (0 != memcmp (&pong->pid,
966 sizeof (struct GNUNET_PeerIdentity)))
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
973 "Processing `%s' from `%s'\n",
975 (sender_address != NULL)
976 ? GST_plugin_a2s (plugin_name,
981 addr = (const char*) &pong[1];
982 alen = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
983 addrend = memchr (addr, '\0', alen);
992 ve = find_validation_entry (NULL,
999 GNUNET_STATISTICS_update (GST_stats,
1000 gettext_noop ("# PONGs dropped, no matching pending validation"),
1005 /* now check that PONG is well-formed */
1006 if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
1008 GNUNET_STATISTICS_update (GST_stats,
1009 gettext_noop ("# PONGs dropped, signature expired"),
1015 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1020 GNUNET_break_op (0);
1024 /* validity achieved, remember it! */
1025 ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1027 /* build HELLO to store in PEERINFO */
1028 hello = GNUNET_HELLO_create (&ve->public_key,
1029 &add_valid_peer_address,
1031 GNUNET_PEERINFO_add_peer (GST_peerinfo,
1033 GNUNET_free (hello);
1035 if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
1036 GNUNET_SCHEDULER_cancel (ve->timeout_task);
1038 /* randomly delay by up to 1h to avoid synchronous validations */
1039 rdelay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1041 delay = GNUNET_TIME_relative_add (HELLO_REVALIDATION_START_TIME,
1042 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1044 ve->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
1045 &revalidate_address,
1051 * We've received a HELLO, check which addresses are new and trigger
1054 * @param hello the HELLO we received
1057 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1059 const struct GNUNET_HELLO_Message* hm = (const struct GNUNET_HELLO_Message*) hello;
1060 struct ValidateAddressContext vac;
1063 GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1065 GNUNET_HELLO_get_key (hm, &vac.public_key)) )
1067 /* malformed HELLO */
1071 GNUNET_assert (NULL ==
1072 GNUNET_HELLO_iterate_addresses (hm,
1080 * Opaque handle to stop incremental validation address callbacks.
1082 struct GST_ValidationIteratorContext
1085 * Function to call on each address.
1087 GST_ValidationAddressCallback cb;
1095 * Which peer are we monitoring?
1097 struct GNUNET_PeerIdentity target;
1102 * Call the callback in the closure for each validation entry.
1104 * @param cls the 'struct GST_ValidationIteratorContext'
1105 * @param key the peer's identity
1106 * @param value the 'struct ValidationEntry'
1107 * @return GNUNET_OK (continue to iterate)
1110 iterate_addresses (void *cls,
1111 const GNUNET_HashCode *key,
1114 struct GST_ValidationIteratorContext *vic = cls;
1115 struct ValidationEntry *ve = value;
1117 vic->cb (vic->cb_cls,
1121 ve->validation_block,
1130 * Call the given function for each address for the given target.
1131 * Can either give a snapshot (synchronous API) or be continuous.
1133 * @param target peer information is requested for
1134 * @param snapshot_only GNUNET_YES to iterate over addresses once, GNUNET_NO to
1135 * continue to give information about addresses as it evolves
1136 * @param cb function to call; will not be called after this function returns
1137 * if snapshot_only is GNUNET_YES
1138 * @param cb_cls closure for 'cb'
1139 * @return context to cancel, NULL if 'snapshot_only' is GNUNET_YES
1141 struct GST_ValidationIteratorContext *
1142 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1144 GST_ValidationAddressCallback cb,
1147 struct GST_ValidationIteratorContext *vic;
1149 vic = GNUNET_malloc (sizeof (struct GST_ValidationIteratorContext));
1151 vic->cb_cls = cb_cls;
1152 vic->target = *target;
1153 GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
1154 &target->hashPubKey,
1157 if (GNUNET_YES == snapshot_only)
1162 GNUNET_CONTAINER_multihashmap_put (notify_map,
1163 &target->hashPubKey,
1165 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1171 * Cancel an active validation address iteration.
1173 * @param ctx the context of the operation that is cancelled
1176 GST_validation_get_addresses_cancel (struct GST_ValidationIteratorContext *ctx)
1178 GNUNET_assert (GNUNET_OK ==
1179 GNUNET_CONTAINER_multihashmap_remove (notify_map,
1180 &ctx->target.hashPubKey,
1186 /* end of file gnunet-service-transport_validation.c */