uncrustify as demanded.
[oweals/gnunet.git] / src / transport / gnunet-service-transport_validation.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file transport/gnunet-service-transport_validation.c
23  * @brief address validation subsystem
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet-service-transport_ats.h"
28 #include "gnunet-service-transport_hello.h"
29 #include "gnunet-service-transport_neighbours.h"
30 #include "gnunet-service-transport_plugins.h"
31 #include "gnunet-service-transport_validation.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_ats_service.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_signatures.h"
37
38 /**
39  * Current state of a validation process.
40  *
41  * FIXME: what state is used to indicate that a validation
42  * was successful? If that is clarified/determined, "UGH" in
43  * ~gnunetpeerinfogtk.c:1103 should be resolved.
44  */
45 enum GNUNET_TRANSPORT_ValidationState {
46   /**
47    * Undefined state
48    *
49    * Used for final callback indicating operation done
50    */
51   GNUNET_TRANSPORT_VS_NONE,
52
53   /**
54    * Fresh validation entry
55    *
56    * Entry was just created, no validation process was executed
57    */
58   GNUNET_TRANSPORT_VS_NEW,
59
60   /**
61    * Updated validation entry
62    *
63    * This is an update for an existing validation entry
64    */
65   GNUNET_TRANSPORT_VS_UPDATE,
66
67   /**
68    * Timeout for validation entry
69    *
70    * A timeout occured during the validation process
71    */
72   GNUNET_TRANSPORT_VS_TIMEOUT,
73
74   /**
75    * Validation entry is removed
76    *
77    * The validation entry is getting removed due to a failed validation
78    */
79   GNUNET_TRANSPORT_VS_REMOVE
80 };
81
82
83
84
85 /**
86  * How long is a PONG signature valid?  We'll recycle a signature until
87  * 1/4 of this time is remaining.  PONGs should expire so that if our
88  * external addresses change an adversary cannot replay them indefinitely.
89  * OTOH, we don't want to spend too much time generating PONG signatures,
90  * so they must have some lifetime to reduce our CPU usage.
91  */
92 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 1)
93
94 /**
95  * After how long do we expire an address in a HELLO that we just
96  * validated?  This value is also used for our own addresses when we
97  * create a HELLO.
98  */
99 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 12)
100
101 /**
102  * How often do we allow PINGing an address that we have not yet
103  * validated?  This also determines how long we track an address that
104  * we cannot validate (because after this time we can destroy the
105  * validation record).
106  */
107 #define UNVALIDATED_PING_KEEPALIVE GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
108
109 /**
110  * How often do we PING an address that we have successfully validated
111  * in the past but are not actively using?  Should be (significantly)
112  * smaller than HELLO_ADDRESS_EXPIRATION.
113  */
114 #define VALIDATED_PING_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15)
115
116 /**
117  * How often do we PING an address that we are currently using?
118  */
119 #define CONNECTED_PING_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
120
121 /**
122  * How much delay is acceptable for sending the PING or PONG?
123  */
124 #define ACCEPTABLE_PING_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
125
126 /**
127  * Size of the validation map hashmap.
128  */
129 #define VALIDATION_MAP_SIZE 256
130
131 /**
132  * Priority to use for PINGs
133  */
134 #define PING_PRIORITY 2
135
136 /**
137  * Priority to use for PONGs
138  */
139 #define PONG_PRIORITY 4
140
141
142 GNUNET_NETWORK_STRUCT_BEGIN
143
144 /**
145  * Message used to ask a peer to validate receipt (to check an address
146  * from a HELLO).  Followed by the address we are trying to validate,
147  * or an empty address if we are just sending a PING to confirm that a
148  * connection which the receiver (of the PING) initiated is still valid.
149  */
150 struct TransportPingMessage {
151   /**
152    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_PING
153    */
154   struct GNUNET_MessageHeader header;
155
156   /**
157    * Challenge code (to ensure fresh reply).
158    */
159   uint32_t challenge GNUNET_PACKED;
160
161   /**
162    * Who is the intended recipient?
163    */
164   struct GNUNET_PeerIdentity target;
165 };
166
167
168 /**
169  * Message used to validate a HELLO.  The challenge is included in the
170  * confirmation to make matching of replies to requests possible.  The
171  * signature signs our public key, an expiration time and our address.<p>
172  *
173  * This message is followed by our transport address that the PING tried
174  * to confirm (if we liked it).  The address can be empty (zero bytes)
175  * if the PING had not address either (and we received the request via
176  * a connection that we initiated).
177  */
178 struct TransportPongMessage {
179   /**
180    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
181    */
182   struct GNUNET_MessageHeader header;
183
184   /**
185    * Challenge code from PING (showing freshness).  Not part of what
186    * is signed so that we can re-use signatures.
187    */
188   uint32_t challenge GNUNET_PACKED;
189
190   /**
191    * Signature.
192    */
193   struct GNUNET_CRYPTO_EddsaSignature signature;
194
195   /**
196    * #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
197    * plausible address for the signing peer.
198    */
199   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
200
201   /**
202    * When does this signature expire?
203    */
204   struct GNUNET_TIME_AbsoluteNBO expiration;
205
206   /**
207    * Size of address appended to this message (part of what is
208    * being signed, hence not redundant).
209    */
210   uint32_t addrlen GNUNET_PACKED;
211 };
212 GNUNET_NETWORK_STRUCT_END
213
214 /**
215  * Information about an address under validation
216  */
217 struct ValidationEntry {
218   /**
219    * The address.
220    */
221   struct GNUNET_HELLO_Address *address;
222
223   /**
224    * Handle to the blacklist check (if we're currently in it).
225    */
226   struct GST_BlacklistCheck *bc;
227
228   /**
229    * Cached PONG signature
230    */
231   struct GNUNET_CRYPTO_EddsaSignature pong_sig_cache;
232
233   /**
234    * ID of task that will clean up this entry if nothing happens.
235    */
236   struct GNUNET_SCHEDULER_Task *timeout_task;
237
238   /**
239    * ID of task that will trigger address revalidation.
240    */
241   struct GNUNET_SCHEDULER_Task *revalidation_task;
242
243   /**
244    * At what time did we send the latest validation request (PING)?
245    */
246   struct GNUNET_TIME_Absolute send_time;
247
248   /**
249    * At what time do we send the next validation request (PING)?
250    */
251   struct GNUNET_TIME_Absolute next_validation;
252
253   /**
254    * Until when is this address valid?
255    * ZERO if it is not currently considered valid.
256    */
257   struct GNUNET_TIME_Absolute valid_until;
258
259   /**
260    * Until when is the cached PONG signature valid?
261    * ZERO if it is not currently considered valid.
262    */
263   struct GNUNET_TIME_Absolute pong_sig_valid_until;
264
265   /**
266    * How long until we can try to validate this address again?
267    * FOREVER if the address is for an unsupported plugin (from PEERINFO)
268    * ZERO if the address is considered valid (no validation needed)
269    * otherwise a time in the future if we're currently denying re-validation
270    */
271   struct GNUNET_TIME_Absolute revalidation_block;
272
273   /**
274    * Last observed latency for this address (round-trip), delay between
275    * last PING sent and PONG received; FOREVER if we never got a PONG.
276    */
277   struct GNUNET_TIME_Relative latency;
278
279   /**
280    * Current state of this validation entry
281    */
282   enum GNUNET_TRANSPORT_ValidationState state;
283
284   /**
285    * Challenge number we used.
286    */
287   uint32_t challenge;
288
289   /**
290    * When passing the address in #add_valid_peer_address(), did we
291    * copy the address to the HELLO yet?
292    */
293   int copied;
294
295   /**
296    * Are we currently using this address for a connection?
297    */
298   int in_use;
299
300   /**
301    * Are we expecting a PONG message for this validation entry?
302    */
303   int expecting_pong;
304
305   /**
306    * Is this address known to ATS as valid right now?
307    */
308   int known_to_ats;
309
310   /**
311    * Which network type does our address belong to?
312    */
313   enum GNUNET_NetworkType network;
314 };
315
316
317 /**
318  * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
319  * of the given peer that we are currently validating, have validated
320  * or are blocked from re-validation for a while).
321  */
322 static struct GNUNET_CONTAINER_MultiPeerMap *validation_map;
323
324 /**
325  * Context for peerinfo iteration.
326  */
327 static struct GNUNET_PEERINFO_NotifyContext *pnc;
328
329 /**
330  * Minimum delay between to validations
331  */
332 static struct GNUNET_TIME_Relative validation_delay;
333
334 /**
335  * Number of validations running; any PING that was not yet
336  * matched by a PONG and for which we have not yet hit the
337  * timeout is considered a running 'validation'.
338  */
339 static unsigned int validations_running;
340
341 /**
342  * Validition fast start threshold
343  */
344 static unsigned int validations_fast_start_threshold;
345
346 /**
347  * When is next validation allowed
348  */
349 static struct GNUNET_TIME_Absolute validation_next;
350
351
352 /**
353  * Context for the validation entry match function.
354  */
355 struct ValidationEntryMatchContext {
356   /**
357    * Where to store the result?
358    */
359   struct ValidationEntry *ve;
360
361   /**
362    * Address we're interested in.
363    */
364   const struct GNUNET_HELLO_Address *address;
365 };
366
367
368 /**
369  * Provide an update on the `validation_map` map size to statistics.
370  * This function should be called whenever the `validation_map`
371  * is changed.
372  */
373 static void
374 publish_ve_stat_update()
375 {
376   GNUNET_STATISTICS_set(GST_stats,
377                         gettext_noop("# Addresses in validation map"),
378                         GNUNET_CONTAINER_multipeermap_size(validation_map),
379                         GNUNET_NO);
380 }
381
382
383 /**
384  * Iterate over validation entries until a matching one is found.
385  *
386  * @param cls the `struct ValidationEntryMatchContext *`
387  * @param key peer identity (unused)
388  * @param value a `struct ValidationEntry *` to match
389  * @return #GNUNET_YES if the entry does not match,
390  *         #GNUNET_NO if the entry does match
391  */
392 static int
393 validation_entry_match(void *cls,
394                        const struct GNUNET_PeerIdentity *key,
395                        void *value)
396 {
397   struct ValidationEntryMatchContext *vemc = cls;
398   struct ValidationEntry *ve = value;
399
400   if (0 == GNUNET_HELLO_address_cmp(ve->address,
401                                     vemc->address))
402     {
403       vemc->ve = ve;
404       return GNUNET_NO;
405     }
406   return GNUNET_YES;
407 }
408
409
410 /**
411  * A validation entry changed.  Update the state and notify
412  * monitors.
413  *
414  * @param ve validation entry that changed
415  * @param state new state
416  */
417 static void
418 validation_entry_changed(struct ValidationEntry *ve,
419                          enum GNUNET_TRANSPORT_ValidationState state)
420 {
421   ve->state = state;
422 }
423
424
425 /**
426  * Iterate over validation entries and free them.
427  *
428  * @param cls (unused)
429  * @param key peer identity (unused)
430  * @param value a `struct ValidationEntry *` to clean up
431  * @return #GNUNET_YES (continue to iterate)
432  */
433 static int
434 cleanup_validation_entry(void *cls,
435                          const struct GNUNET_PeerIdentity *key,
436                          void *value)
437 {
438   struct ValidationEntry *ve = value;
439
440   ve->next_validation = GNUNET_TIME_UNIT_ZERO_ABS;
441   ve->valid_until = GNUNET_TIME_UNIT_ZERO_ABS;
442
443   /* Notify about deleted entry */
444   validation_entry_changed(ve,
445                            GNUNET_TRANSPORT_VS_REMOVE);
446
447   if (NULL != ve->bc)
448     {
449       GST_blacklist_test_cancel(ve->bc);
450       ve->bc = NULL;
451     }
452   GNUNET_break(GNUNET_OK ==
453                GNUNET_CONTAINER_multipeermap_remove(validation_map,
454                                                     &ve->address->peer,
455                                                     ve));
456   publish_ve_stat_update();
457   if (GNUNET_YES == ve->known_to_ats)
458     {
459       GST_ats_expire_address(ve->address);
460       GNUNET_assert(GNUNET_NO ==
461                     GST_ats_is_known_no_session(ve->address));
462       ve->known_to_ats = GNUNET_NO;
463     }
464   GNUNET_HELLO_address_free(ve->address);
465   if (NULL != ve->timeout_task)
466     {
467       GNUNET_SCHEDULER_cancel(ve->timeout_task);
468       ve->timeout_task = NULL;
469     }
470   if (NULL != ve->revalidation_task)
471     {
472       GNUNET_SCHEDULER_cancel(ve->revalidation_task);
473       ve->revalidation_task = NULL;
474     }
475   if ((GNUNET_YES == ve->expecting_pong) &&
476       (validations_running > 0))
477     {
478       validations_running--;
479       GNUNET_STATISTICS_set(GST_stats,
480                             gettext_noop("# validations running"),
481                             validations_running,
482                             GNUNET_NO);
483     }
484   GNUNET_free(ve);
485   return GNUNET_OK;
486 }
487
488
489 /**
490  * Address validation cleanup task.  Assesses if the record is no
491  * longer valid and then possibly triggers its removal.
492  *
493  * @param cls the `struct ValidationEntry`
494  */
495 static void
496 timeout_hello_validation(void *cls)
497 {
498   struct ValidationEntry *ve = cls;
499   struct GNUNET_TIME_Absolute max;
500   struct GNUNET_TIME_Relative left;
501
502   ve->timeout_task = NULL;
503   /* For valid addresses, we want to wait until the expire;
504      for addresses under PING validation, we want to wait
505      until we give up on the PING */
506   max = GNUNET_TIME_absolute_max(ve->valid_until,
507                                  ve->revalidation_block);
508   left = GNUNET_TIME_absolute_get_remaining(max);
509   if (left.rel_value_us > 0)
510     {
511       /* We should wait a bit longer. This happens when
512          address lifetimes are extended due to successful
513          validations. */
514       ve->timeout_task =
515         GNUNET_SCHEDULER_add_delayed(left,
516                                      &timeout_hello_validation,
517                                      ve);
518       return;
519     }
520   GNUNET_STATISTICS_update(GST_stats,
521                            gettext_noop("# address records discarded (timeout)"),
522                            1,
523                            GNUNET_NO);
524   cleanup_validation_entry(NULL,
525                            &ve->address->peer,
526                            ve);
527 }
528
529
530 /**
531  * Function called with the result from blacklisting.
532  * Send a PING to the other peer if a communication is allowed.
533  *
534  * @param cls our `struct ValidationEntry`
535  * @param pid identity of the other peer
536  * @param address_null address associated with the request, always NULL
537  * @param session_null session associated with the request, always NULL
538  * @param result #GNUNET_OK if the connection is allowed,
539  *               #GNUNET_NO if not,
540  *               #GNUNET_SYSERR if operation was aborted
541  */
542 static void
543 transmit_ping_if_allowed(void *cls,
544                          const struct GNUNET_PeerIdentity *pid,
545                          const struct GNUNET_HELLO_Address *address_null,
546                          struct GNUNET_ATS_Session *session_null,
547                          int result)
548 {
549   struct ValidationEntry *ve = cls;
550   struct TransportPingMessage ping;
551   struct GNUNET_TRANSPORT_PluginFunctions *papi;
552   struct GNUNET_TIME_Absolute next;
553   const struct GNUNET_MessageHeader *hello;
554   ssize_t ret;
555   size_t tsize;
556   size_t slen;
557   uint16_t hsize;
558   struct GNUNET_ATS_Session *session;
559
560   ve->bc = NULL;
561   if (GNUNET_OK != result)
562     {
563       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
564                  "Blacklist denies sending PING to `%s' `%s' `%s'\n",
565                  GNUNET_i2s(pid),
566                  GST_plugins_a2s(ve->address),
567                  ve->address->transport_name);
568       GNUNET_STATISTICS_update(GST_stats,
569                                gettext_noop("# address records discarded (blacklist)"),
570                                1,
571                                GNUNET_NO);
572       cleanup_validation_entry(NULL,
573                                pid,
574                                ve);
575       return;
576     }
577   hello = GST_hello_get();
578   GNUNET_assert(NULL != hello);
579   slen = strlen(ve->address->transport_name) + 1;
580   hsize = ntohs(hello->size);
581   tsize = sizeof(struct TransportPingMessage) +
582           ve->address->address_length + slen + hsize;
583
584   ping.header.size =
585     htons(sizeof(struct TransportPingMessage) +
586           ve->address->address_length + slen);
587   ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
588   ping.challenge = htonl(ve->challenge);
589   ping.target = *pid;
590
591   if (tsize >= GNUNET_MAX_MESSAGE_SIZE)
592     {
593       GNUNET_break(0);
594       hsize = 0;
595       tsize =
596         sizeof(struct TransportPingMessage) + ve->address->address_length +
597         slen + hsize;
598     }
599   {
600     char message_buf[tsize] GNUNET_ALIGN;
601
602     GNUNET_memcpy(message_buf,
603                   hello,
604                   hsize);
605     GNUNET_memcpy(&message_buf[hsize],
606                   &ping,
607                   sizeof(struct TransportPingMessage));
608     GNUNET_memcpy(&message_buf[sizeof(struct TransportPingMessage) + hsize],
609                   ve->address->transport_name,
610                   slen);
611     GNUNET_memcpy(&message_buf[sizeof(struct TransportPingMessage) + slen + hsize],
612                   ve->address->address,
613                   ve->address->address_length);
614     papi = GST_plugins_find(ve->address->transport_name);
615     GNUNET_assert(NULL != papi);
616     session = papi->get_session(papi->cls,
617                                 ve->address);
618     if (NULL == session)
619       {
620         /* Could not get a valid session */
621         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
622                    "Failed to get session to send PING to `%s' at `%s'\n",
623                    GNUNET_i2s(pid),
624                    GST_plugins_a2s(ve->address));
625         return;
626       }
627
628     ret = papi->send(papi->cls, session,
629                      message_buf, tsize,
630                      PING_PRIORITY,
631                      ACCEPTABLE_PING_DELAY,
632                      NULL, NULL);
633     if (-1 == ret)
634       {
635         GNUNET_log(GNUNET_ERROR_TYPE_INFO,
636                    "Failed to send PING to `%s' at `%s'\n",
637                    GNUNET_i2s(pid),
638                    GST_plugins_a2s(ve->address));
639         return;
640       }
641     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
642                "Transmitted plain PING to `%s' `%s' `%s'\n",
643                GNUNET_i2s(pid),
644                GST_plugins_a2s(ve->address),
645                ve->address->transport_name);
646     ve->network = papi->get_network(papi->cls,
647                                     session);
648     GNUNET_break(GNUNET_NT_UNSPECIFIED != ve->network);
649     GST_neighbours_notify_data_sent(ve->address,
650                                     session,
651                                     tsize);
652     next = GNUNET_TIME_relative_to_absolute(validation_delay);
653     validation_next = GNUNET_TIME_absolute_max(next,
654                                                validation_next);
655     ve->send_time = GNUNET_TIME_absolute_get();
656     GNUNET_STATISTICS_update(GST_stats,
657                              gettext_noop("# PINGs for address validation sent"),
658                              1,
659                              GNUNET_NO);
660     ve->expecting_pong = GNUNET_YES;
661     validations_running++;
662     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
663                "Validation started, %u validation processes running\n",
664                validations_running);
665     GNUNET_STATISTICS_set(GST_stats,
666                           gettext_noop("# validations running"),
667                           validations_running,
668                           GNUNET_NO);
669     /*  Notify about PING sent */
670     validation_entry_changed(ve,
671                              GNUNET_TRANSPORT_VS_UPDATE);
672   }
673 }
674
675
676 /**
677  * Do address validation again to keep address valid.
678  *
679  * @param cls the `struct ValidationEntry`
680  */
681 static void
682 revalidate_address(void *cls)
683 {
684   struct ValidationEntry *ve = cls;
685   struct GNUNET_TIME_Relative canonical_delay;
686   struct GNUNET_TIME_Relative delay;
687   struct GNUNET_TIME_Relative blocked_for;
688   struct GST_BlacklistCheck *bc;
689   uint32_t rdelay;
690
691   ve->revalidation_task = NULL;
692   delay = GNUNET_TIME_absolute_get_remaining(ve->revalidation_block);
693   /* Considering current connectivity situation, what is the maximum
694      block period permitted? */
695   if (GNUNET_YES == ve->in_use)
696     canonical_delay = CONNECTED_PING_FREQUENCY;
697   else if (GNUNET_TIME_absolute_get_remaining(ve->valid_until).rel_value_us > 0)
698     canonical_delay = VALIDATED_PING_FREQUENCY;
699   else
700     canonical_delay = UNVALIDATED_PING_KEEPALIVE;
701   /* Use delay that is MIN of original delay and possibly adjusted
702      new maximum delay (which may be lower); the real delay
703      is originally randomized between "canonical_delay" and "2 * canonical_delay",
704      so continue to permit that window for the operation. */
705   delay = GNUNET_TIME_relative_min(delay,
706                                    GNUNET_TIME_relative_multiply(canonical_delay,
707                                                                  2));
708   ve->revalidation_block = GNUNET_TIME_relative_to_absolute(delay);
709   if (delay.rel_value_us > 0)
710     {
711       /* should wait a bit longer */
712       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
713                  "Waiting for %s longer before (re)validating address `%s'\n",
714                  GNUNET_STRINGS_relative_time_to_string(delay,
715                                                         GNUNET_YES),
716                  GST_plugins_a2s(ve->address));
717       ve->revalidation_task =
718         GNUNET_SCHEDULER_add_delayed(delay,
719                                      &revalidate_address, ve);
720       ve->next_validation = GNUNET_TIME_relative_to_absolute(delay);
721       return;
722     }
723   /* check if globally we have too many active validations at a
724      too high rate, if so, delay ours */
725   blocked_for = GNUNET_TIME_absolute_get_remaining(validation_next);
726   if ((validations_running > validations_fast_start_threshold) &&
727       (blocked_for.rel_value_us > 0))
728     {
729       /* Validations are blocked, have to wait for blocked_for time */
730       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
731                  "Validations blocked for another %s, delaying validating address `%s'\n",
732                  GNUNET_STRINGS_relative_time_to_string(blocked_for,
733                                                         GNUNET_YES),
734                  GST_plugins_a2s(ve->address));
735       GNUNET_STATISTICS_update(GST_stats,
736                                gettext_noop("# validations delayed by global throttle"),
737                                1,
738                                GNUNET_NO);
739       ve->revalidation_task =
740         GNUNET_SCHEDULER_add_delayed(blocked_for,
741                                      &revalidate_address,
742                                      ve);
743       ve->next_validation = GNUNET_TIME_relative_to_absolute(blocked_for);
744       return;
745     }
746
747   /* We are good to go; remember to not go again for `canonical_delay` time;
748      add up to `canonical_delay` to randomize start time */
749   ve->revalidation_block = GNUNET_TIME_relative_to_absolute(canonical_delay);
750   /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
751   rdelay =
752     GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
753                              canonical_delay.rel_value_us);
754
755   delay = GNUNET_TIME_relative_add(canonical_delay,
756                                    GNUNET_TIME_relative_multiply
757                                      (GNUNET_TIME_UNIT_MICROSECONDS,
758                                      rdelay));
759
760   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
761              "Validating now, next scheduled for %s, now validating address `%s'\n",
762              GNUNET_STRINGS_relative_time_to_string(blocked_for,
763                                                     GNUNET_YES),
764              GST_plugins_a2s(ve->address));
765   ve->revalidation_task =
766     GNUNET_SCHEDULER_add_delayed(delay,
767                                  &revalidate_address,
768                                  ve);
769   ve->next_validation = GNUNET_TIME_relative_to_absolute(delay);
770
771   /* start PINGing by checking blacklist */
772   GNUNET_STATISTICS_update(GST_stats,
773                            gettext_noop("# address revalidations started"), 1,
774                            GNUNET_NO);
775   if (NULL != ve->bc)
776     {
777       GST_blacklist_test_cancel(ve->bc);
778       ve->bc = NULL;
779     }
780   bc = GST_blacklist_test_allowed(&ve->address->peer,
781                                   ve->address->transport_name,
782                                   &transmit_ping_if_allowed,
783                                   ve,
784                                   NULL,
785                                   NULL);
786   if (NULL != bc)
787     {
788       /* If transmit_ping_if_allowed was already called it may have freed ve,
789        * so only set ve->bc if it has not been called.
790        */
791       ve->bc = bc;
792     }
793 }
794
795
796 /**
797  * Find a ValidationEntry entry for the given neighbour that matches
798  * the given address and transport.  If none exists, create one (but
799  * without starting any validation).
800  *
801  * @param address address to find
802  * @return validation entry matching the given specifications, NULL
803  *         if we don't have an existing entry and no public key was given
804  */
805 static struct ValidationEntry *
806 find_validation_entry(const struct GNUNET_HELLO_Address *address)
807 {
808   struct ValidationEntryMatchContext vemc;
809   struct ValidationEntry *ve;
810
811   vemc.ve = NULL;
812   vemc.address = address;
813   GNUNET_CONTAINER_multipeermap_get_multiple(validation_map,
814                                              &address->peer,
815                                              &validation_entry_match, &vemc);
816   if (NULL != (ve = vemc.ve))
817     return ve;
818   GNUNET_assert(GNUNET_NO ==
819                 GST_ats_is_known_no_session(address));
820   ve = GNUNET_new(struct ValidationEntry);
821   ve->in_use = GNUNET_SYSERR; /* not defined */
822   ve->address = GNUNET_HELLO_address_copy(address);
823   ve->pong_sig_valid_until = GNUNET_TIME_UNIT_ZERO_ABS;
824   memset(&ve->pong_sig_cache,
825          '\0',
826          sizeof(struct GNUNET_CRYPTO_EddsaSignature));
827   ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
828   ve->challenge =
829     GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
830   ve->timeout_task =
831     GNUNET_SCHEDULER_add_delayed(UNVALIDATED_PING_KEEPALIVE,
832                                  &timeout_hello_validation,
833                                  ve);
834   GNUNET_CONTAINER_multipeermap_put(validation_map,
835                                     &address->peer,
836                                     ve,
837                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
838   publish_ve_stat_update();
839   validation_entry_changed(ve,
840                            GNUNET_TRANSPORT_VS_NEW);
841   return ve;
842 }
843
844
845 /**
846  * Iterator which adds the given address to the set of validated
847  * addresses.
848  *
849  * @param cls original HELLO message
850  * @param address the address
851  * @param expiration expiration time
852  * @return #GNUNET_OK (keep the address), could return
853  *         #GNUNET_NO (delete address, but this is ignored);
854  *         #GNUNET_SYSERR would abort iteration (but we always iterate all)
855  */
856 static int
857 add_valid_address(void *cls,
858                   const struct GNUNET_HELLO_Address *address,
859                   struct GNUNET_TIME_Absolute expiration)
860 {
861   const struct GNUNET_HELLO_Message *hello = cls;
862   struct ValidationEntry *ve;
863   struct GNUNET_PeerIdentity pid;
864   struct GNUNET_ATS_Properties prop;
865   struct GNUNET_TRANSPORT_PluginFunctions *papi;
866
867   if (0 == GNUNET_TIME_absolute_get_remaining(expiration).rel_value_us)
868     return GNUNET_OK;           /* expired */
869   if (GNUNET_OK != GNUNET_HELLO_get_id(hello, &pid))
870     {
871       GNUNET_break(0);
872       return GNUNET_OK;         /* invalid HELLO !? */
873     }
874   if (NULL == (papi = GST_plugins_find(address->transport_name)))
875     {
876       /* might have been valid in the past, but we don't have that
877          plugin loaded right now */
878       return GNUNET_OK;
879     }
880   if (NULL ==
881       papi->address_to_string(papi->cls,
882                               address->address,
883                               address->address_length))
884     {
885       /* Why do we try to add an ill-formed address? */
886       GNUNET_break(0);
887       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
888                  "Address with %u bytes for plugin %s and peer %s is malformed\n",
889                  (unsigned int)address->address_length,
890                  address->transport_name,
891                  GNUNET_i2s(&pid));
892       return GNUNET_OK;
893     }
894
895   ve = find_validation_entry(address);
896   ve->network = papi->get_network_for_address(papi->cls,
897                                               address);
898   GNUNET_break(GNUNET_NT_UNSPECIFIED != ve->network);
899   ve->valid_until = GNUNET_TIME_absolute_max(ve->valid_until,
900                                              expiration);
901   if (NULL == ve->revalidation_task)
902     {
903       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
904                  "Starting revalidations for valid address `%s'\n",
905                  GST_plugins_a2s(ve->address));
906       ve->next_validation = GNUNET_TIME_absolute_get();
907       ve->revalidation_task = GNUNET_SCHEDULER_add_now(&revalidate_address, ve);
908     }
909   validation_entry_changed(ve,
910                            GNUNET_TRANSPORT_VS_UPDATE);
911   memset(&prop, 0, sizeof(prop));
912   prop.scope = ve->network;
913   prop.delay = GNUNET_TIME_relative_divide(ve->latency, 2);
914   if (GNUNET_YES != ve->known_to_ats)
915     {
916       ve->known_to_ats = GNUNET_YES;
917       GST_ats_add_address(address, &prop);
918       GNUNET_assert(GNUNET_YES ==
919                     GST_ats_is_known_no_session(ve->address));
920     }
921   return GNUNET_OK;
922 }
923
924
925 /**
926  * Function called for any HELLO known to PEERINFO.
927  *
928  * @param cls unused (NULL)
929  * @param peer id of the peer, NULL for last call (during iteration,
930  *             as we are monitoring, this should never happen)
931  * @param hello hello message for the peer (can be NULL)
932  * @param err_msg error message
933  */
934 static void
935 process_peerinfo_hello(void *cls,
936                        const struct GNUNET_PeerIdentity *peer,
937                        const struct GNUNET_HELLO_Message *hello,
938                        const char *err_msg)
939 {
940   GNUNET_assert(NULL != peer);
941   if (NULL == hello)
942     return;
943   if (0 == memcmp(&GST_my_identity,
944                   peer,
945                   sizeof(struct GNUNET_PeerIdentity)))
946     {
947       /* Peerinfo returned own identity, skip validation */
948       return;
949     }
950   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
951              "Handling HELLO for peer `%s'\n",
952              GNUNET_i2s(peer));
953   GNUNET_assert(NULL ==
954                 GNUNET_HELLO_iterate_addresses(hello, GNUNET_NO,
955                                                &add_valid_address,
956                                                (void *)hello));
957 }
958
959
960 /**
961  * Start the validation subsystem.
962  *
963  * @param max_fds maximum number of fds to use
964  */
965 void
966 GST_validation_start(unsigned int max_fds)
967 {
968   /**
969    * Initialization for validation throttling
970    *
971    * We have a maximum number max_fds of connections we can use for validation
972    * We monitor the number of validations in parallel and start to throttle it
973    * when doing to many validations in parallel:
974    * if (running validations < (max_fds / 2))
975    * - "fast start": run validation immediately
976    * - have delay of (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2)
977    *   (300 sec / ~150 == ~2 sec.) between two validations
978    */
979
980   validation_next = GNUNET_TIME_absolute_get();
981   validation_delay.rel_value_us = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us) / (max_fds / 2);
982   validations_fast_start_threshold = (max_fds / 2);
983   validations_running = 0;
984   GNUNET_STATISTICS_set(GST_stats,
985                         gettext_noop("# validations running"),
986                         validations_running,
987                         GNUNET_NO);
988   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
989              "Validation uses a fast start threshold of %u connections and a delay of %s\n",
990              validations_fast_start_threshold,
991              GNUNET_STRINGS_relative_time_to_string(validation_delay,
992                                                     GNUNET_YES));
993   validation_map = GNUNET_CONTAINER_multipeermap_create(VALIDATION_MAP_SIZE,
994                                                         GNUNET_NO);
995   pnc = GNUNET_PEERINFO_notify(GST_cfg, GNUNET_YES,
996                                &process_peerinfo_hello, NULL);
997 }
998
999
1000 /**
1001  * Stop the validation subsystem.
1002  */
1003 void
1004 GST_validation_stop()
1005 {
1006   GNUNET_CONTAINER_multipeermap_iterate(validation_map,
1007                                         &cleanup_validation_entry,
1008                                         NULL);
1009   GNUNET_CONTAINER_multipeermap_destroy(validation_map);
1010   validation_map = NULL;
1011   GNUNET_PEERINFO_notify_cancel(pnc);
1012 }
1013
1014
1015 /**
1016  * Send the given PONG to the given address.
1017  *
1018  * @param cls the PONG message
1019  * @param valid_until is ZERO if we never validated the address,
1020  *                    otherwise a time up to when we consider it (or was) valid
1021  * @param validation_block  is FOREVER if the address is for an unsupported plugin (from PEERINFO)
1022  *                          is ZERO if the address is considered valid (no validation needed)
1023  *                          otherwise a time in the future if we're currently denying re-validation
1024  * @param address target address
1025  */
1026 static void
1027 multicast_pong(void *cls,
1028                struct GNUNET_TIME_Absolute valid_until,
1029                struct GNUNET_TIME_Absolute validation_block,
1030                const struct GNUNET_HELLO_Address *address)
1031 {
1032   struct TransportPongMessage *pong = cls;
1033   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1034   struct GNUNET_ATS_Session *session;
1035
1036   papi = GST_plugins_find(address->transport_name);
1037   if (NULL == papi)
1038     {
1039       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1040                  "Plugin %s not supported, cannot send PONG\n",
1041                  address->transport_name);
1042       return;
1043     }
1044   GNUNET_assert(NULL != papi->send);
1045   GNUNET_assert(NULL != papi->get_session);
1046   session = papi->get_session(papi->cls, address);
1047   if (NULL == session)
1048     {
1049       GNUNET_break(0);
1050       return;
1051     }
1052   GST_ats_new_session(address, session);
1053   papi->send(papi->cls, session,
1054              (const char *)pong,
1055              ntohs(pong->header.size),
1056              PONG_PRIORITY,
1057              ACCEPTABLE_PING_DELAY,
1058              NULL, NULL);
1059   GST_neighbours_notify_data_sent(address,
1060                                   session,
1061                                   pong->header.size);
1062 }
1063
1064
1065 /**
1066  * We've received a PING.  If appropriate, generate a PONG.
1067  *
1068  * @param sender peer sending the PING
1069  * @param hdr the PING
1070  * @param sender_address the sender address as we got it
1071  * @param session session we got the PING from
1072  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1073  */
1074 int
1075 GST_validation_handle_ping(const struct GNUNET_PeerIdentity *sender,
1076                            const struct GNUNET_MessageHeader *hdr,
1077                            const struct GNUNET_HELLO_Address *sender_address,
1078                            struct GNUNET_ATS_Session *session)
1079 {
1080   const struct TransportPingMessage *ping;
1081   struct TransportPongMessage *pong;
1082   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1083   struct GNUNET_CRYPTO_EddsaSignature *sig_cache;
1084   struct GNUNET_TIME_Absolute *sig_cache_exp;
1085   const char *addr;
1086   const char *addrend;
1087   char *plugin_name;
1088   char *pos;
1089   size_t len_address;
1090   size_t len_plugin;
1091   ssize_t ret;
1092   struct GNUNET_HELLO_Address address;
1093
1094   if (0 ==
1095       memcmp(&GST_my_identity,
1096              sender,
1097              sizeof(struct GNUNET_PeerIdentity)))
1098     return GNUNET_OK; /* our own, ignore! */
1099   if (ntohs(hdr->size) < sizeof(struct TransportPingMessage))
1100     {
1101       GNUNET_break_op(0);
1102       return GNUNET_SYSERR;
1103     }
1104   ping = (const struct TransportPingMessage *)hdr;
1105   if (0 !=
1106       memcmp(&ping->target,
1107              &GST_my_identity,
1108              sizeof(struct GNUNET_PeerIdentity)))
1109     {
1110       GNUNET_STATISTICS_update(GST_stats,
1111                                gettext_noop
1112                                  ("# PING message for different peer received"), 1,
1113                                GNUNET_NO);
1114       return GNUNET_SYSERR;
1115     }
1116   GNUNET_STATISTICS_update(GST_stats,
1117                            gettext_noop("# PING messages received"), 1,
1118                            GNUNET_NO);
1119   addr = (const char *)&ping[1];
1120   len_address = ntohs(hdr->size) - sizeof(struct TransportPingMessage);
1121   /* peer wants to confirm that this is one of our addresses, this is what is
1122    * used for address validation */
1123
1124   sig_cache = NULL;
1125   sig_cache_exp = NULL;
1126   papi = NULL;
1127   if (len_address > 0)
1128     {
1129       addrend = memchr(addr, '\0', len_address);
1130       if (NULL == addrend)
1131         {
1132           GNUNET_break_op(0);
1133           return GNUNET_SYSERR;
1134         }
1135       addrend++;
1136       len_plugin = strlen(addr) + 1;
1137       len_address -= len_plugin;
1138       address.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
1139       address.address = addrend;
1140       address.address_length = len_address;
1141       address.transport_name = addr;
1142       address.peer = GST_my_identity;
1143
1144       if (NULL == address.transport_name)
1145         {
1146           GNUNET_break(0);
1147         }
1148
1149       if (0 != strstr(address.transport_name, "_client"))
1150         {
1151           plugin_name = GNUNET_strdup(address.transport_name);
1152           pos = strstr(plugin_name, "_client");
1153           GNUNET_assert(NULL != pos);
1154           GNUNET_snprintf(pos, strlen("_server") + 1, "%s", "_server");
1155         }
1156       else
1157         plugin_name = GNUNET_strdup(address.transport_name);
1158
1159       if (NULL == (papi = GST_plugins_find(plugin_name)))
1160         {
1161           /* we don't have the plugin for this address */
1162           GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1163                      _("Plugin `%s' not available, cannot confirm having this address\n"),
1164                      plugin_name);
1165           GNUNET_free(plugin_name);
1166           return GNUNET_SYSERR;
1167         }
1168       GNUNET_free(plugin_name);
1169       if (GNUNET_OK !=
1170           papi->check_address(papi->cls,
1171                               addrend,
1172                               len_address))
1173         {
1174           GNUNET_STATISTICS_update(GST_stats,
1175                                    gettext_noop
1176                                      ("# failed address checks during validation"), 1,
1177                                    GNUNET_NO);
1178           GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1179                      _("Address `%s' is not one of my addresses, not confirming PING\n"),
1180                      GST_plugins_a2s(&address));
1181           return GNUNET_SYSERR;
1182         }
1183       else
1184         {
1185           GNUNET_STATISTICS_update(GST_stats,
1186                                    gettext_noop
1187                                      ("# successful address checks during validation"), 1,
1188                                    GNUNET_NO);
1189           GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1190                      "Address `%s' is one of my addresses, confirming PING\n",
1191                      GST_plugins_a2s(&address));
1192         }
1193
1194       if (GNUNET_YES !=
1195           GST_hello_test_address(&address,
1196                                  &sig_cache,
1197                                  &sig_cache_exp))
1198         {
1199           GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1200                      _("Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n"),
1201                      GNUNET_i2s(sender),
1202                      GST_plugins_a2s(&address));
1203           return GNUNET_SYSERR;
1204         }
1205     }
1206   else
1207     {
1208       addrend = NULL;           /* make gcc happy */
1209       len_plugin = 0;
1210       static struct GNUNET_CRYPTO_EddsaSignature no_address_signature;
1211       static struct GNUNET_TIME_Absolute no_address_signature_expiration;
1212
1213       sig_cache = &no_address_signature;
1214       sig_cache_exp = &no_address_signature_expiration;
1215     }
1216
1217   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1218              "I am `%s', sending PONG to peer `%s'\n",
1219              GNUNET_i2s_full(&GST_my_identity),
1220              GNUNET_i2s(sender));
1221
1222   /* message with structure:
1223    * [TransportPongMessage][Transport name][Address] */
1224
1225   pong = GNUNET_malloc(sizeof(struct TransportPongMessage) + len_address + len_plugin);
1226   pong->header.size =
1227     htons(sizeof(struct TransportPongMessage) + len_address + len_plugin);
1228   pong->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
1229   pong->purpose.size =
1230     htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
1231           sizeof(uint32_t) + sizeof(struct GNUNET_TIME_AbsoluteNBO) +
1232           len_address + len_plugin);
1233   pong->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
1234   GNUNET_memcpy(&pong->challenge, &ping->challenge, sizeof(ping->challenge));
1235   pong->addrlen = htonl(len_address + len_plugin);
1236   GNUNET_memcpy(&pong[1], addr, len_plugin);    /* Copy transport plugin */
1237   if (len_address > 0)
1238     {
1239       GNUNET_assert(NULL != addrend);
1240       GNUNET_memcpy(&((char *)&pong[1])[len_plugin], addrend, len_address);
1241     }
1242   if (GNUNET_TIME_absolute_get_remaining(*sig_cache_exp).rel_value_us <
1243       PONG_SIGNATURE_LIFETIME.rel_value_us / 4)
1244     {
1245       /* create / update cached sig */
1246       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1247                  "Creating PONG signature to indicate ownership.\n");
1248       *sig_cache_exp = GNUNET_TIME_relative_to_absolute(PONG_SIGNATURE_LIFETIME);
1249       pong->expiration = GNUNET_TIME_absolute_hton(*sig_cache_exp);
1250       if (GNUNET_OK !=
1251           GNUNET_CRYPTO_eddsa_sign(GST_my_private_key, &pong->purpose,
1252                                    sig_cache))
1253         {
1254           GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1255                      _("Failed to create PONG signature for peer `%s'\n"), GNUNET_i2s(sender));
1256         }
1257     }
1258   else
1259     {
1260       pong->expiration = GNUNET_TIME_absolute_hton(*sig_cache_exp);
1261     }
1262   pong->signature = *sig_cache;
1263
1264   GNUNET_assert(NULL != sender_address);
1265
1266   /* first see if the session we got this PING from can be used to transmit
1267    * a response reliably */
1268   if (NULL == papi)
1269     {
1270       ret = -1;
1271     }
1272   else
1273     {
1274       GNUNET_assert(NULL != papi->send);
1275       GNUNET_assert(NULL != papi->get_session);
1276       if (NULL == session)
1277         {
1278           session = papi->get_session(papi->cls, sender_address);
1279         }
1280       if (NULL == session)
1281         {
1282           GNUNET_break(0);
1283           ret = -1;
1284         }
1285       else
1286         {
1287           ret = papi->send(papi->cls, session,
1288                            (const char *)pong,
1289                            ntohs(pong->header.size),
1290                            PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
1291                            NULL, NULL);
1292           if (-1 != ret)
1293             GST_neighbours_notify_data_sent(sender_address,
1294                                             session,
1295                                             pong->header.size);
1296         }
1297     }
1298   if (-1 != ret)
1299     {
1300       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1301                  "Transmitted PONG to `%s' via reliable mechanism\n",
1302                  GNUNET_i2s(sender));
1303       /* done! */
1304       GNUNET_STATISTICS_update(GST_stats,
1305                                gettext_noop
1306                                  ("# PONGs unicast via reliable transport"), 1,
1307                                GNUNET_NO);
1308       GNUNET_free(pong);
1309       return GNUNET_OK;
1310     }
1311
1312   /* no reliable method found, try transmission via all known addresses */
1313   GNUNET_STATISTICS_update(GST_stats,
1314                            gettext_noop
1315                              ("# PONGs multicast to all available addresses"), 1,
1316                            GNUNET_NO);
1317   GST_validation_get_addresses(sender,
1318                                &multicast_pong, pong);
1319   GNUNET_free(pong);
1320   return GNUNET_OK;
1321 }
1322
1323
1324 /**
1325  * Validate an individual address.
1326  *
1327  * @param address address we should try to validate
1328  */
1329 void
1330 GST_validation_handle_address(const struct GNUNET_HELLO_Address *address)
1331 {
1332   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1333   struct ValidationEntry *ve;
1334
1335   papi = GST_plugins_find(address->transport_name);
1336   if (NULL == papi)
1337     {
1338       /* This plugin is currently unvailable ... ignore */
1339       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1340                  "No plugin available for %s\n",
1341                  address->transport_name);
1342       return;
1343     }
1344   ve = find_validation_entry(address);
1345   if (NULL == ve->revalidation_task)
1346     {
1347       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1348                  "Validation process started for fresh address `%s' of %s\n",
1349                  GST_plugins_a2s(ve->address),
1350                  GNUNET_i2s(&ve->address->peer));
1351       ve->revalidation_task = GNUNET_SCHEDULER_add_now(&revalidate_address, ve);
1352     }
1353   else
1354     {
1355       GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1356                  "Validation already running for address `%s' of %s\n",
1357                  GST_plugins_a2s(ve->address),
1358                  GNUNET_i2s(&ve->address->peer));
1359     }
1360 }
1361
1362
1363 /**
1364  * Iterator callback to go over all addresses and try to validate them
1365  * (unless blocked or already validated).
1366  *
1367  * @param cls NULL
1368  * @param address the address
1369  * @param expiration expiration time
1370  * @return #GNUNET_OK (keep the address)
1371  */
1372 static int
1373 validate_address_iterator(void *cls,
1374                           const struct GNUNET_HELLO_Address *address,
1375                           struct GNUNET_TIME_Absolute expiration)
1376 {
1377   if (0 == GNUNET_TIME_absolute_get_remaining(expiration).rel_value_us)
1378     {
1379       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1380                  "Skipping expired address from HELLO\n");
1381       return GNUNET_OK;         /* expired */
1382     }
1383   GST_validation_handle_address(address);
1384   return GNUNET_OK;
1385 }
1386
1387
1388 /**
1389  * Add the validated peer address to the HELLO.
1390  *
1391  * @param cls the `struct ValidationEntry *` with the validated address
1392  * @param max space in @a buf
1393  * @param buf where to add the address
1394  * @return number of bytes written, #GNUNET_SYSERR to signal the
1395  *         end of the iteration.
1396  */
1397 static ssize_t
1398 add_valid_peer_address(void *cls,
1399                        size_t max,
1400                        void *buf)
1401 {
1402   struct ValidationEntry *ve = cls;
1403
1404   if (GNUNET_YES == ve->copied)
1405     return GNUNET_SYSERR; /* Done */
1406   ve->copied = GNUNET_YES;
1407   return GNUNET_HELLO_add_address(ve->address,
1408                                   ve->valid_until,
1409                                   buf,
1410                                   max);
1411 }
1412
1413
1414 /**
1415  * We've received a PONG.  Check if it matches a pending PING and
1416  * mark the respective address as confirmed.
1417  *
1418  * @param sender peer sending the PONG
1419  * @param hdr the PONG
1420  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1421  */
1422 int
1423 GST_validation_handle_pong(const struct GNUNET_PeerIdentity *sender,
1424                            const struct GNUNET_MessageHeader *hdr)
1425 {
1426   const struct TransportPongMessage *pong;
1427   struct ValidationEntry *ve;
1428   const char *tname;
1429   const char *addr;
1430   size_t addrlen;
1431   size_t slen;
1432   size_t size;
1433   struct GNUNET_HELLO_Message *hello;
1434   struct GNUNET_HELLO_Address address;
1435   int sig_res;
1436   int do_verify;
1437
1438   if (0 ==
1439       memcmp(&GST_my_identity,
1440              sender,
1441              sizeof(struct GNUNET_PeerIdentity)))
1442     return GNUNET_OK; /* our own, ignore! */
1443
1444   if (ntohs(hdr->size) < sizeof(struct TransportPongMessage))
1445     {
1446       GNUNET_break_op(0);
1447       return GNUNET_SYSERR;
1448     }
1449   GNUNET_STATISTICS_update(GST_stats,
1450                            gettext_noop("# PONG messages received"), 1,
1451                            GNUNET_NO);
1452
1453   /* message with structure:
1454    * [TransportPongMessage][Transport name][Address] */
1455
1456   pong = (const struct TransportPongMessage *)hdr;
1457   tname = (const char *)&pong[1];
1458   size = ntohs(hdr->size) - sizeof(struct TransportPongMessage);
1459   addr = memchr(tname, '\0', size);
1460   if (NULL == addr)
1461     {
1462       GNUNET_break_op(0);
1463       return GNUNET_SYSERR;
1464     }
1465   addr++;
1466   slen = strlen(tname) + 1;
1467   addrlen = size - slen;
1468
1469   if (NULL == GST_plugins_find(tname))
1470     {
1471       /* we got the PONG, but the transport plugin specified in it
1472          is not supported by this peer, so this cannot be a good
1473          PONG for us. */
1474       GNUNET_break_op(0);
1475       return GNUNET_OK;
1476     }
1477
1478   address.peer = *sender;
1479   address.address = addr;
1480   address.address_length = addrlen;
1481   address.transport_name = tname;
1482   address.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
1483   ve = find_validation_entry(&address);
1484   if ((NULL == ve) || (GNUNET_NO == ve->expecting_pong))
1485     {
1486       GNUNET_STATISTICS_update(GST_stats,
1487                                gettext_noop
1488                                  ("# PONGs dropped, no matching pending validation"),
1489                                1, GNUNET_NO);
1490       return GNUNET_OK;
1491     }
1492   /* now check that PONG is well-formed */
1493   if (0 != memcmp(&ve->address->peer,
1494                   sender,
1495                   sizeof(struct GNUNET_PeerIdentity)))
1496     {
1497       GNUNET_break_op(0);
1498       return GNUNET_SYSERR;
1499     }
1500   if (0 ==
1501       GNUNET_TIME_absolute_get_remaining
1502         (GNUNET_TIME_absolute_ntoh(pong->expiration)).rel_value_us)
1503     {
1504       GNUNET_STATISTICS_update(GST_stats,
1505                                gettext_noop
1506                                  ("# PONGs dropped, signature expired"), 1,
1507                                GNUNET_NO);
1508       return GNUNET_SYSERR;
1509     }
1510
1511   sig_res = GNUNET_SYSERR;
1512   do_verify = GNUNET_YES;
1513   if (0 != GNUNET_TIME_absolute_get_remaining(ve->pong_sig_valid_until).rel_value_us)
1514     {
1515       /* We have a cached and valid signature for this peer,
1516        * try to compare instead of verify */
1517       if (0 == memcmp(&ve->pong_sig_cache,
1518                       &pong->signature,
1519                       sizeof(struct GNUNET_CRYPTO_EddsaSignature)))
1520         {
1521           /* signatures are identical, we can skip verification */
1522           sig_res = GNUNET_OK;
1523           do_verify = GNUNET_NO;
1524         }
1525       else
1526         {
1527           sig_res = GNUNET_SYSERR;
1528           /* signatures do not match, we have to verify */
1529         }
1530     }
1531
1532   if (GNUNET_YES == do_verify)
1533     {
1534       /* Do expensive verification */
1535       sig_res = GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1536                                            &pong->purpose,
1537                                            &pong->signature,
1538                                            &ve->address->peer.public_key);
1539       if (sig_res == GNUNET_SYSERR)
1540         {
1541           GNUNET_break_op(0);
1542           GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1543                      "Failed to verify: invalid signature on address `%s':%s from peer `%s'\n",
1544                      tname,
1545                      GST_plugins_a2s(ve->address),
1546                      GNUNET_i2s(sender));
1547         }
1548     }
1549   if (sig_res == GNUNET_SYSERR)
1550     {
1551       GNUNET_break_op(0);
1552       return GNUNET_SYSERR;
1553     }
1554
1555   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1556              "Validation process successful for peer `%s' with plugin `%s' address `%s'\n",
1557              GNUNET_i2s(sender),
1558              tname,
1559              GST_plugins_a2s(ve->address));
1560   GNUNET_STATISTICS_update(GST_stats,
1561                            gettext_noop("# validations succeeded"),
1562                            1,
1563                            GNUNET_NO);
1564   /* validity achieved, remember it! */
1565   ve->expecting_pong = GNUNET_NO;
1566   ve->valid_until = GNUNET_TIME_relative_to_absolute(HELLO_ADDRESS_EXPIRATION);
1567   ve->pong_sig_cache = pong->signature;
1568   ve->pong_sig_valid_until = GNUNET_TIME_absolute_ntoh(pong->expiration);
1569   ve->latency = GNUNET_TIME_absolute_get_duration(ve->send_time);
1570   {
1571     if (GNUNET_YES == ve->known_to_ats)
1572       {
1573         GNUNET_assert(GNUNET_YES ==
1574                       GST_ats_is_known_no_session(ve->address));
1575         GST_ats_update_delay(ve->address,
1576                              GNUNET_TIME_relative_divide(ve->latency, 2));
1577       }
1578     else
1579       {
1580         struct GNUNET_ATS_Properties prop;
1581
1582         memset(&prop, 0, sizeof(prop));
1583         GNUNET_break(GNUNET_NT_UNSPECIFIED != ve->network);
1584         prop.scope = ve->network;
1585         prop.delay = GNUNET_TIME_relative_divide(ve->latency, 2);
1586         GNUNET_assert(GNUNET_NO ==
1587                       GST_ats_is_known_no_session(ve->address));
1588         ve->known_to_ats = GNUNET_YES;
1589         GST_ats_add_address(ve->address, &prop);
1590         GNUNET_assert(GNUNET_YES ==
1591                       GST_ats_is_known_no_session(ve->address));
1592       }
1593   }
1594   if (validations_running > 0)
1595     {
1596       validations_running--;
1597       GNUNET_STATISTICS_set(GST_stats,
1598                             gettext_noop("# validations running"),
1599                             validations_running,
1600                             GNUNET_NO);
1601       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1602                  "Validation finished, %u validation processes running\n",
1603                  validations_running);
1604     }
1605   else
1606     {
1607       GNUNET_break(0);
1608     }
1609
1610   /* Notify about new validity */
1611   validation_entry_changed(ve,
1612                            GNUNET_TRANSPORT_VS_UPDATE);
1613
1614   /* build HELLO to store in PEERINFO */
1615   GNUNET_STATISTICS_update(GST_stats,
1616                            gettext_noop("# HELLOs given to peerinfo"),
1617                            1,
1618                            GNUNET_NO);
1619   ve->copied = GNUNET_NO;
1620   hello = GNUNET_HELLO_create(&ve->address->peer.public_key,
1621                               &add_valid_peer_address,
1622                               ve,
1623                               GNUNET_NO);
1624   GNUNET_break(NULL !=
1625                GNUNET_PEERINFO_add_peer(GST_peerinfo,
1626                                         hello,
1627                                         NULL,
1628                                         NULL));
1629   GNUNET_free(hello);
1630   return GNUNET_OK;
1631 }
1632
1633
1634 /**
1635  * We've received a HELLO, check which addresses are new and trigger
1636  * validation.
1637  *
1638  * @param hello the HELLO we received
1639  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1640  */
1641 int
1642 GST_validation_handle_hello(const struct GNUNET_MessageHeader *hello)
1643 {
1644   const struct GNUNET_HELLO_Message *hm =
1645     (const struct GNUNET_HELLO_Message *)hello;
1646   struct GNUNET_PeerIdentity pid;
1647   int friend;
1648
1649   friend = GNUNET_HELLO_is_friend_only(hm);
1650   if (((GNUNET_YES != friend) &&
1651        (GNUNET_NO != friend)) ||
1652       (GNUNET_OK != GNUNET_HELLO_get_id(hm, &pid)))
1653     {
1654       /* malformed HELLO */
1655       GNUNET_break_op(0);
1656       return GNUNET_SYSERR;
1657     }
1658   if (0 ==
1659       memcmp(&GST_my_identity,
1660              &pid,
1661              sizeof(struct GNUNET_PeerIdentity)))
1662     {
1663       /* got our own HELLO, how boring */
1664       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1665                  "Validation received our own HELLO (%s), ignoring\n",
1666                  GNUNET_i2s(&pid));
1667       return GNUNET_OK;
1668     }
1669   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1670              "Validation received HELLO message for peer `%s' with size %u, checking for new addresses\n",
1671              GNUNET_i2s(&pid),
1672              ntohs(hello->size));
1673   GNUNET_assert(NULL ==
1674                 GNUNET_HELLO_iterate_addresses(hm,
1675                                                GNUNET_NO,
1676                                                &validate_address_iterator,
1677                                                NULL));
1678   return GNUNET_OK;
1679 }
1680
1681
1682 /**
1683  * Closure for #iterate_addresses().
1684  */
1685 struct IteratorContext {
1686   /**
1687    * Function to call on each address.
1688    */
1689   GST_ValidationAddressCallback cb;
1690
1691   /**
1692    * Closure for @e cb.
1693    */
1694   void *cb_cls;
1695 };
1696
1697
1698 /**
1699  * Call the callback in the closure for each validation entry.
1700  *
1701  * @param cls the `struct IteratorContext`
1702  * @param key the peer's identity
1703  * @param value the `struct ValidationEntry`
1704  * @return #GNUNET_OK (continue to iterate)
1705  */
1706 static int
1707 iterate_addresses(void *cls,
1708                   const struct GNUNET_PeerIdentity *key,
1709                   void *value)
1710 {
1711   struct IteratorContext *ic = cls;
1712   struct ValidationEntry *ve = value;
1713
1714   ic->cb(ic->cb_cls,
1715          ve->valid_until,
1716          ve->revalidation_block,
1717          ve->address);
1718   return GNUNET_OK;
1719 }
1720
1721
1722 /**
1723  * Call the given function for each address for the given target.
1724  * Can either give a snapshot (synchronous API) or be continuous.
1725  *
1726  * @param target peer information is requested for
1727  * @param cb function to call; will not be called after this function returns
1728  * @param cb_cls closure for @a cb
1729  */
1730 void
1731 GST_validation_get_addresses(const struct GNUNET_PeerIdentity *target,
1732                              GST_ValidationAddressCallback cb,
1733                              void *cb_cls)
1734 {
1735   struct IteratorContext ic;
1736
1737   ic.cb = cb;
1738   ic.cb_cls = cb_cls;
1739   GNUNET_CONTAINER_multipeermap_get_multiple(validation_map,
1740                                              target,
1741                                              &iterate_addresses, &ic);
1742 }
1743
1744
1745 /**
1746  * Update if we are using an address for a connection actively right now.
1747  * Based on this, the validation module will measure latency for the
1748  * address more or less often.
1749  *
1750  * @param address the address that we are now using (or not)
1751  * @param in_use #GNUNET_YES if we are now using the address for a connection,
1752  *               #GNUNET_NO if we are no longer using the address for a connection
1753  */
1754 void
1755 GST_validation_set_address_use(const struct GNUNET_HELLO_Address *address,
1756                                int in_use)
1757 {
1758   struct ValidationEntry *ve;
1759
1760   if (GNUNET_HELLO_address_check_option(address,
1761                                         GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1762     return; /* ignore inbound for validation */
1763   if (NULL == GST_plugins_find(address->transport_name))
1764     {
1765       /* How can we use an address for which we don't have the plugin? */
1766       GNUNET_break(0);
1767       return;
1768     }
1769   ve = find_validation_entry(address);
1770   if (NULL == ve)
1771     {
1772       GNUNET_break(0);
1773       return;
1774     }
1775   if (in_use == ve->in_use)
1776     return;
1777   ve->in_use = in_use;
1778   if (GNUNET_YES == in_use)
1779     {
1780       /* from now on, higher frequeny, so reschedule now */
1781       if (NULL != ve->revalidation_task)
1782         GNUNET_SCHEDULER_cancel(ve->revalidation_task);
1783       ve->revalidation_task = GNUNET_SCHEDULER_add_now(&revalidate_address,
1784                                                        ve);
1785     }
1786 }
1787
1788
1789 /* end of file gnunet-service-transport_validation.c */