f35967fb74aa357554eac76368cc4b4b8e1e99cd
[oweals/gnunet.git] / src / transport / gnunet-service-transport_validation.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
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_validation.h"
28 #include "gnunet-service-transport_plugins.h"
29 #include "gnunet-service-transport_hello.h"
30 #include "gnunet-service-transport_blacklist.h"
31 #include "gnunet-service-transport.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_ats_service.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_signatures.h"
36
37
38 /**
39  * How long is a PONG signature valid?  We'll recycle a signature until
40  * 1/4 of this time is remaining.  PONGs should expire so that if our
41  * external addresses change an adversary cannot replay them indefinitely.
42  * OTOH, we don't want to spend too much time generating PONG signatures,
43  * so they must have some lifetime to reduce our CPU usage.
44  */
45 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
46
47 /**
48  * After how long do we expire an address in a HELLO that we just
49  * validated?  This value is also used for our own addresses when we
50  * create a HELLO.
51  */
52 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
53
54 /**
55  * How often do we allow PINGing an address that we have not yet
56  * validated?  This also determines how long we track an address that
57  * we cannot validate (because after this time we can destroy the
58  * validation record).
59  */
60 #define UNVALIDATED_PING_KEEPALIVE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
61
62 /**
63  * How often do we PING an address that we have successfully validated
64  * in the past but are not actively using?  Should be (significantly)
65  * smaller than HELLO_ADDRESS_EXPIRATION.
66  */
67 #define VALIDATED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
68
69 /**
70  * How often do we PING an address that we are currently using?
71  */
72 #define CONNECTED_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
73
74 /**
75  * How much delay is acceptable for sending the PING or PONG?
76  */
77 #define ACCEPTABLE_PING_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
78
79 /**
80  * Size of the validation map hashmap.
81  */
82 #define VALIDATION_MAP_SIZE 256
83
84 /**
85  * Priority to use for PINGs
86  */
87 #define PING_PRIORITY 2
88
89 /**
90  * Priority to use for PONGs
91  */
92 #define PONG_PRIORITY 4
93
94
95 GNUNET_NETWORK_STRUCT_BEGIN
96
97 /**
98  * Message used to ask a peer to validate receipt (to check an address
99  * from a HELLO).  Followed by the address we are trying to validate,
100  * or an empty address if we are just sending a PING to confirm that a
101  * connection which the receiver (of the PING) initiated is still valid.
102  */
103 struct TransportPingMessage
104 {
105
106   /**
107    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
108    */
109   struct GNUNET_MessageHeader header;
110
111   /**
112    * Challenge code (to ensure fresh reply).
113    */
114   uint32_t challenge GNUNET_PACKED;
115
116   /**
117    * Who is the intended recipient?
118    */
119   struct GNUNET_PeerIdentity target;
120
121 };
122
123
124 /**
125  * Message used to validate a HELLO.  The challenge is included in the
126  * confirmation to make matching of replies to requests possible.  The
127  * signature signs our public key, an expiration time and our address.<p>
128  *
129  * This message is followed by our transport address that the PING tried
130  * to confirm (if we liked it).  The address can be empty (zero bytes)
131  * if the PING had not address either (and we received the request via
132  * a connection that we initiated).
133  */
134 struct TransportPongMessage
135 {
136
137   /**
138    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
139    */
140   struct GNUNET_MessageHeader header;
141
142   /**
143    * Challenge code from PING (showing freshness).  Not part of what
144    * is signed so that we can re-use signatures.
145    */
146   uint32_t challenge GNUNET_PACKED;
147
148   /**
149    * Signature.
150    */
151   struct GNUNET_CRYPTO_EccSignature signature;
152
153   /**
154    * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
155    * plausible address for the signing peer.
156    */
157   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
158
159   /**
160    * When does this signature expire?
161    */
162   struct GNUNET_TIME_AbsoluteNBO expiration;
163
164   /**
165    * Size of address appended to this message (part of what is
166    * being signed, hence not redundant).
167    */
168   uint32_t addrlen GNUNET_PACKED;
169
170 };
171 GNUNET_NETWORK_STRUCT_END
172
173 /**
174  * Information about an address under validation
175  */
176 struct ValidationEntry
177 {
178
179   /**
180    * The address.
181    */
182   struct GNUNET_HELLO_Address *address;
183
184   /**
185    * Handle to the blacklist check (if we're currently in it).
186    */
187   struct GST_BlacklistCheck *bc;
188
189   /**
190    * Public key of the peer.
191    */
192   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
193
194   /**
195    * The identity of the peer. FIXME: duplicated (also in 'address')
196    */
197   struct GNUNET_PeerIdentity pid;
198
199   /**
200    * ID of task that will clean up this entry if nothing happens.
201    */
202   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
203
204   /**
205    * ID of task that will trigger address revalidation.
206    */
207   GNUNET_SCHEDULER_TaskIdentifier revalidation_task;
208
209   /**
210    * At what time did we send the latest validation request (PING)?
211    */
212   struct GNUNET_TIME_Absolute send_time;
213
214   /**
215    * Until when is this address valid?
216    * ZERO if it is not currently considered valid.
217    */
218   struct GNUNET_TIME_Absolute valid_until;
219
220   /**
221    * How long until we can try to validate this address again?
222    * FOREVER if the address is for an unsupported plugin (from PEERINFO)
223    * ZERO if the address is considered valid (no validation needed)
224    * otherwise a time in the future if we're currently denying re-validation
225    */
226   struct GNUNET_TIME_Absolute revalidation_block;
227
228   /**
229    * Last observed latency for this address (round-trip), delay between
230    * last PING sent and PONG received; FOREVER if we never got a PONG.
231    */
232   struct GNUNET_TIME_Relative latency;
233
234   /**
235    * Challenge number we used.
236    */
237   uint32_t challenge;
238
239   /**
240    * When passing the address in 'add_valid_peer_address', did we
241    * copy the address to the HELLO yet?
242    */
243   int copied;
244
245   /**
246    * Are we currently using this address for a connection?
247    */
248   int in_use;
249
250   /**
251    * Are we expecting a PONG message for this validation entry?
252    */
253   int expecting_pong;
254
255   /* FIXME: DEBUGGING */
256   int last_line_set_to_no;
257   int last_line_set_to_yes;
258 };
259
260
261 /**
262  * Context of currently active requests to peerinfo
263  * for validation of HELLOs.
264  */
265 struct CheckHelloValidatedContext
266 {
267
268   /**
269    * This is a doubly-linked list.
270    */
271   struct CheckHelloValidatedContext *next;
272
273   /**
274    * This is a doubly-linked list.
275    */
276   struct CheckHelloValidatedContext *prev;
277
278   /**
279    * Hello that we are validating.
280    */
281   const struct GNUNET_HELLO_Message *hello;
282
283 };
284
285
286 /**
287  * Head of linked list of HELLOs awaiting validation.
288  */
289 static struct CheckHelloValidatedContext *chvc_head;
290
291 /**
292  * Tail of linked list of HELLOs awaiting validation
293  */
294 static struct CheckHelloValidatedContext *chvc_tail;
295
296 /**
297  * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
298  * of the given peer that we are currently validating, have validated
299  * or are blocked from re-validation for a while).
300  */
301 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
302
303 /**
304  * Context for peerinfo iteration.
305  */
306 static struct GNUNET_PEERINFO_NotifyContext *pnc;
307
308
309 /**
310  * Minimum delay between to validations
311  */
312 static struct GNUNET_TIME_Relative validation_delay;
313
314 /**
315  * When is next validation allowed
316  */
317 static struct GNUNET_TIME_Absolute validation_next;
318
319 /**
320  * Context for the validation entry match function.
321  */
322 struct ValidationEntryMatchContext
323 {
324   /**
325    * Where to store the result?
326    */
327   struct ValidationEntry *ve;
328
329   /**
330    * Address we're interested in.
331    */
332   const struct GNUNET_HELLO_Address *address;
333
334 };
335
336
337 /**
338  * Iterate over validation entries until a matching one is found.
339  *
340  * @param cls the 'struct ValidationEntryMatchContext'
341  * @param key peer identity (unused)
342  * @param value a 'struct ValidationEntry' to match
343  * @return GNUNET_YES if the entry does not match,
344  *         GNUNET_NO if the entry does match
345  */
346 static int
347 validation_entry_match (void *cls, const struct GNUNET_HashCode * key, void *value)
348 {
349   struct ValidationEntryMatchContext *vemc = cls;
350   struct ValidationEntry *ve = value;
351
352   if (0 == GNUNET_HELLO_address_cmp (ve->address, vemc->address))
353   {
354     vemc->ve = ve;
355     return GNUNET_NO;
356   }
357   return GNUNET_YES;
358 }
359
360
361 /**
362  * Iterate over validation entries and free them.
363  *
364  * @param cls (unused)
365  * @param key peer identity (unused)
366  * @param value a 'struct ValidationEntry' to clean up
367  * @return GNUNET_YES (continue to iterate)
368  */
369 static int
370 cleanup_validation_entry (void *cls, const struct GNUNET_HashCode * key, void *value)
371 {
372   struct ValidationEntry *ve = value;
373
374   if (NULL != ve->bc)
375   {
376     GST_blacklist_test_cancel (ve->bc);
377     ve->bc = NULL;
378   }
379   GNUNET_break (GNUNET_OK ==
380                 GNUNET_CONTAINER_multihashmap_remove (validation_map,
381                                                       &ve->pid.hashPubKey, ve));
382   GNUNET_HELLO_address_free (ve->address);
383   if (GNUNET_SCHEDULER_NO_TASK != ve->timeout_task)
384   {
385     GNUNET_SCHEDULER_cancel (ve->timeout_task);
386     ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
387   }
388   if (GNUNET_SCHEDULER_NO_TASK != ve->revalidation_task)
389   {
390     GNUNET_SCHEDULER_cancel (ve->revalidation_task);
391     ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
392   }
393   GNUNET_free (ve);
394   return GNUNET_OK;
395 }
396
397
398 /**
399  * Address validation cleanup task.  Assesses if the record is no
400  * longer valid and then possibly triggers its removal.
401  *
402  * @param cls the 'struct ValidationEntry'
403  * @param tc scheduler context (unused)
404  */
405 static void
406 timeout_hello_validation (void *cls,
407                           const struct GNUNET_SCHEDULER_TaskContext *tc)
408 {
409   struct ValidationEntry *ve = cls;
410   struct GNUNET_TIME_Absolute max;
411   struct GNUNET_TIME_Relative left;
412
413   ve->timeout_task = GNUNET_SCHEDULER_NO_TASK;
414   max = GNUNET_TIME_absolute_max (ve->valid_until, ve->revalidation_block);
415   left = GNUNET_TIME_absolute_get_remaining (max);
416   if (left.rel_value > 0)
417   {
418     /* should wait a bit longer */
419     ve->timeout_task =
420         GNUNET_SCHEDULER_add_delayed (left, &timeout_hello_validation, ve);
421     return;
422   }
423   GNUNET_STATISTICS_update (GST_stats,
424                             gettext_noop ("# address records discarded"), 1,
425                             GNUNET_NO);
426   cleanup_validation_entry (NULL, &ve->pid.hashPubKey, ve);
427 }
428
429
430 /**
431  * Function called with the result from blacklisting.
432  * Send a PING to the other peer if a communication is allowed.
433  *
434  * @param cls our 'struct ValidationEntry'
435  * @param pid identity of the other peer
436  * @param result GNUNET_OK if the connection is allowed, GNUNET_NO if not
437  */
438 static void
439 transmit_ping_if_allowed (void *cls, const struct GNUNET_PeerIdentity *pid,
440                           int result)
441 {
442   struct ValidationEntry *ve = cls;
443   struct TransportPingMessage ping;
444   struct GNUNET_TRANSPORT_PluginFunctions *papi;
445   struct GNUNET_TIME_Absolute next;
446   const struct GNUNET_MessageHeader *hello;
447   ssize_t ret;
448   size_t tsize;
449   size_t slen;
450   uint16_t hsize;
451
452   ve->bc = NULL;
453
454   if (GNUNET_NO == result)
455   {
456           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist denies to send PING to `%s' %s %s\n",
457                       GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
458                 return;
459   }
460
461   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting plain PING to `%s' %s %s\n",
462               GNUNET_i2s (pid), GST_plugins_a2s (ve->address), ve->address->transport_name);
463
464   next = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), validation_delay);
465   if (next.abs_value > validation_next.abs_value)
466         validation_next = next; /* We're going to send a PING so delay next validation */
467
468   slen = strlen (ve->address->transport_name) + 1;
469   hello = GST_hello_get ();
470   hsize = ntohs (hello->size);
471   tsize =
472       sizeof (struct TransportPingMessage) + ve->address->address_length +
473       slen + hsize;
474
475   ping.header.size =
476       htons (sizeof (struct TransportPingMessage) +
477              ve->address->address_length + slen);
478   ping.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
479   ping.challenge = htonl (ve->challenge);
480   ping.target = *pid;
481
482   if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
483   {
484     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
485                 _
486                 ("Not transmitting `%s' with `%s', message too big (%u bytes!). This should not happen.\n"),
487                 "HELLO", "PING", (unsigned int) tsize);
488     /* message too big (!?), get rid of HELLO */
489     hsize = 0;
490     tsize =
491         sizeof (struct TransportPingMessage) + ve->address->address_length +
492         slen + hsize;
493   }
494   {
495     char message_buf[tsize];
496
497     /* build message with structure:
498      *  [HELLO][TransportPingMessage][Transport name][Address] */
499     memcpy (message_buf, hello, hsize);
500     memcpy (&message_buf[hsize], &ping, sizeof (struct TransportPingMessage));
501     memcpy (&message_buf[sizeof (struct TransportPingMessage) + hsize],
502             ve->address->transport_name, slen);
503     memcpy (&message_buf[sizeof (struct TransportPingMessage) + slen + hsize],
504             ve->address->address, ve->address->address_length);
505     papi = GST_plugins_find (ve->address->transport_name);
506     if (papi == NULL)
507       ret = -1;
508     else
509     {
510       GNUNET_assert (papi->send != NULL);
511       GNUNET_assert (papi->get_session != NULL);
512       struct Session * session = papi->get_session(papi->cls, ve->address);
513
514       if (session != NULL)
515       {
516         ret = papi->send (papi->cls, session,
517                           message_buf, tsize,
518                           PING_PRIORITY, ACCEPTABLE_PING_DELAY,
519                           NULL, NULL);
520       }
521       else
522       {
523         /* Could not get a valid session */
524         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not get a valid session for `%s' %s\n",
525                     GNUNET_i2s (pid), GST_plugins_a2s (ve->address));
526         ret = -1;
527       }
528     }
529   }
530   if (-1 != ret)
531   {
532     ve->send_time = GNUNET_TIME_absolute_get ();
533     GNUNET_STATISTICS_update (GST_stats,
534                               gettext_noop
535                               ("# PING without HELLO messages sent"), 1,
536                               GNUNET_NO);
537     ve->expecting_pong = GNUNET_YES;
538   }
539 }
540
541
542 /**
543  * Do address validation again to keep address valid.
544  *
545  * @param cls the 'struct ValidationEntry'
546  * @param tc scheduler context (unused)
547  */
548 static void
549 revalidate_address (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
550 {
551   struct ValidationEntry *ve = cls;
552   struct GNUNET_TIME_Relative canonical_delay;
553   struct GNUNET_TIME_Relative delay;
554   struct GNUNET_TIME_Relative blocked_for;
555   struct GST_BlacklistCheck *bc;
556   uint32_t rdelay;
557
558   ve->revalidation_task = GNUNET_SCHEDULER_NO_TASK;
559   delay = GNUNET_TIME_absolute_get_remaining (ve->revalidation_block);
560   /* How long until we can possibly permit the next PING? */
561   canonical_delay =
562       (ve->in_use ==
563        GNUNET_YES) ? CONNECTED_PING_FREQUENCY
564       : ((GNUNET_TIME_absolute_get_remaining (ve->valid_until).rel_value >
565           0) ? VALIDATED_PING_FREQUENCY : UNVALIDATED_PING_KEEPALIVE);
566   if (delay.rel_value > canonical_delay.rel_value * 2)
567   {
568     /* situation changed, recalculate delay */
569     delay = canonical_delay;
570     ve->revalidation_block = GNUNET_TIME_relative_to_absolute (delay);
571   }
572   if (delay.rel_value > 0)
573   {
574     /* should wait a bit longer */
575     ve->revalidation_task =
576         GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
577     return;
578   }
579   blocked_for = GNUNET_TIME_absolute_get_remaining(validation_next);
580   if ((blocked_for.rel_value) > 0)
581   {
582     /* Validations are blocked, have to wait for blocked_for time */
583     ve->revalidation_task =
584       GNUNET_SCHEDULER_add_delayed (blocked_for, &revalidate_address, ve);
585     return;
586   }
587   ve->revalidation_block = GNUNET_TIME_relative_to_absolute (canonical_delay);
588
589   /* schedule next PINGing with some extra random delay to avoid synchronous re-validations */
590   rdelay =
591       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
592                                 canonical_delay.rel_value);
593
594   /* Debug code for mantis 0002726*/
595   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value ==
596       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rdelay).rel_value)
597   {
598     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
599                 "Revalidation interval for peer `%s' for is FOREVER (debug: rdelay: %llu, canonical delay %llu)\n",
600                 GNUNET_i2s (&ve->pid),
601                 (unsigned long long) delay.rel_value,
602                 (unsigned long long) canonical_delay.rel_value);
603     delay = canonical_delay;
604   }
605   else
606   {
607       delay = GNUNET_TIME_relative_add (canonical_delay,
608                                 GNUNET_TIME_relative_multiply
609                                 (GNUNET_TIME_UNIT_MILLISECONDS, rdelay));
610   }
611   /* End debug code for mantis 0002726*/
612   ve->revalidation_task =
613       GNUNET_SCHEDULER_add_delayed (delay, &revalidate_address, ve);
614
615   /* start PINGing by checking blacklist */
616   GNUNET_STATISTICS_update (GST_stats,
617                             gettext_noop ("# address revalidations started"), 1,
618                             GNUNET_NO);
619   bc = GST_blacklist_test_allowed (&ve->pid, ve->address->transport_name,
620                                    &transmit_ping_if_allowed, ve);
621   if (NULL != bc)
622     ve->bc = bc;                /* only set 'bc' if 'transmit_ping_if_allowed' was not already
623                                  * called... */
624 }
625
626
627 /**
628  * Find a ValidationEntry entry for the given neighbour that matches
629  * the given address and transport.  If none exists, create one (but
630  * without starting any validation).
631  *
632  * @param public_key public key of the peer, NULL for unknown
633  * @param address address to find
634  * @return validation entry matching the given specifications, NULL
635  *         if we don't have an existing entry and no public key was given
636  */
637 static struct ValidationEntry *
638 find_validation_entry (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
639                        *public_key, const struct GNUNET_HELLO_Address *address)
640 {
641   struct ValidationEntryMatchContext vemc;
642   struct ValidationEntry *ve;
643
644   vemc.ve = NULL;
645   vemc.address = address;
646   GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
647                                               &address->peer.hashPubKey,
648                                               &validation_entry_match, &vemc);
649   if (NULL != (ve = vemc.ve))
650     return ve;
651   if (public_key == NULL)
652     return NULL;
653   ve = GNUNET_malloc (sizeof (struct ValidationEntry));
654   ve->in_use = GNUNET_SYSERR; /* not defined */
655   ve->last_line_set_to_no  = 0;
656   ve->last_line_set_to_yes  = 0;
657   ve->address = GNUNET_HELLO_address_copy (address);
658   ve->public_key = *public_key;
659   ve->pid = address->peer;
660   ve->latency = GNUNET_TIME_UNIT_FOREVER_REL;
661   ve->challenge =
662       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
663   ve->timeout_task =
664       GNUNET_SCHEDULER_add_delayed (UNVALIDATED_PING_KEEPALIVE,
665                                     &timeout_hello_validation, ve);
666   GNUNET_CONTAINER_multihashmap_put (validation_map, &address->peer.hashPubKey,
667                                      ve,
668                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
669   ve->expecting_pong = GNUNET_NO;
670   return ve;
671 }
672
673
674 /**
675  * Iterator which adds the given address to the set of validated
676  * addresses.
677  *
678  * @param cls original HELLO message
679  * @param address the address
680  * @param expiration expiration time
681  * @return GNUNET_OK (keep the address)
682  */
683 static int
684 add_valid_address (void *cls, const struct GNUNET_HELLO_Address *address,
685                    struct GNUNET_TIME_Absolute expiration)
686 {
687   const struct GNUNET_HELLO_Message *hello = cls;
688   struct ValidationEntry *ve;
689   struct GNUNET_PeerIdentity pid;
690   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
691
692   if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
693     return GNUNET_OK;           /* expired */
694   if ((GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) ||
695       (GNUNET_OK != GNUNET_HELLO_get_key (hello, &public_key)))
696   {
697     GNUNET_break (0);
698     return GNUNET_OK;           /* invalid HELLO !? */
699   }
700   if (0 == memcmp (&GST_my_identity, &pid, sizeof (struct GNUNET_PeerIdentity)))
701   {
702     /* Peerinfo returned own identity, skip validation */
703     return GNUNET_OK;
704   }
705
706   ve = find_validation_entry (&public_key, address);
707   ve->valid_until = GNUNET_TIME_absolute_max (ve->valid_until, expiration);
708
709   if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
710     ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
711   GNUNET_ATS_address_add (GST_ats, address, NULL, NULL, 0);
712   return GNUNET_OK;
713 }
714
715
716 /**
717  * Function called for any HELLO known to PEERINFO.
718  *
719  * @param cls unused
720  * @param peer id of the peer, NULL for last call
721  * @param hello hello message for the peer (can be NULL)
722  * @param err_msg error message
723  */
724 static void
725 process_peerinfo_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
726                         const struct GNUNET_HELLO_Message *hello,
727                         const char *err_msg)
728 {
729   GNUNET_assert (NULL != peer);
730   if (NULL == hello)
731     return;
732   GNUNET_assert (NULL ==
733                  GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
734                                                  &add_valid_address,
735                                                  (void *) hello));
736 }
737
738
739 /**
740  * Start the validation subsystem.
741  *
742  * @param max_fds maximum number of fds to use
743  */
744 void
745 GST_validation_start (unsigned int max_fds)
746 {
747         validation_next = GNUNET_TIME_absolute_get();
748         validation_delay.rel_value = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value) /  max_fds;
749         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Delay between validations: %u ms\n ", validation_delay.rel_value);
750   validation_map = GNUNET_CONTAINER_multihashmap_create (VALIDATION_MAP_SIZE,
751                                                          GNUNET_NO);
752   pnc = GNUNET_PEERINFO_notify (GST_cfg, &process_peerinfo_hello, NULL);
753 }
754
755
756 /**
757  * Stop the validation subsystem.
758  */
759 void
760 GST_validation_stop ()
761 {
762   struct CheckHelloValidatedContext *chvc;
763
764   GNUNET_CONTAINER_multihashmap_iterate (validation_map,
765                                          &cleanup_validation_entry, NULL);
766   GNUNET_CONTAINER_multihashmap_destroy (validation_map);
767   validation_map = NULL;
768   while (NULL != (chvc = chvc_head))
769   {
770     GNUNET_CONTAINER_DLL_remove (chvc_head, chvc_tail, chvc);
771     GNUNET_free (chvc);
772   }
773   GNUNET_PEERINFO_notify_cancel (pnc);
774 }
775
776
777 /**
778  * Send the given PONG to the given address.
779  *
780  * @param cls the PONG message
781  * @param public_key public key for the peer, never NULL
782  * @param valid_until is ZERO if we never validated the address,
783  *                    otherwise a time up to when we consider it (or was) valid
784  * @param validation_block  is FOREVER if the address is for an unsupported plugin (from PEERINFO)
785  *                          is ZERO if the address is considered valid (no validation needed)
786  *                          otherwise a time in the future if we're currently denying re-validation
787  * @param address target address
788  */
789 static void
790 multicast_pong (void *cls,
791                 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
792                 *public_key, struct GNUNET_TIME_Absolute valid_until,
793                 struct GNUNET_TIME_Absolute validation_block,
794                 const struct GNUNET_HELLO_Address *address)
795 {
796   struct TransportPongMessage *pong = cls;
797   struct GNUNET_TRANSPORT_PluginFunctions *papi;
798
799   papi = GST_plugins_find (address->transport_name);
800   if (papi == NULL)
801     return;
802
803   GNUNET_assert (papi->send != NULL);
804   GNUNET_assert (papi->get_session != NULL);
805
806   struct Session * session = papi->get_session(papi->cls, address);
807   if (session == NULL)
808   {
809      GNUNET_break (0);
810      return;
811   }
812
813   papi->send (papi->cls, session,
814               (const char *) pong, ntohs (pong->header.size),
815               PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
816               NULL, NULL);
817 }
818
819
820 /**
821  * We've received a PING.  If appropriate, generate a PONG.
822  *
823  * @param sender peer sending the PING
824  * @param hdr the PING
825  * @param sender_address the sender address as we got it
826  * @param session session we got the PING from
827  */
828 void
829 GST_validation_handle_ping (const struct GNUNET_PeerIdentity *sender,
830                             const struct GNUNET_MessageHeader *hdr,
831                             const struct GNUNET_HELLO_Address *sender_address,
832                             struct Session *session)
833 {
834   const struct TransportPingMessage *ping;
835   struct TransportPongMessage *pong;
836   struct GNUNET_TRANSPORT_PluginFunctions *papi;
837   struct GNUNET_CRYPTO_EccSignature *sig_cache;
838   struct GNUNET_TIME_Absolute *sig_cache_exp;
839   const char *addr;
840   const char *addrend;
841   size_t alen;
842   size_t slen;
843   ssize_t ret;
844   int buggy = GNUNET_NO;
845   struct GNUNET_HELLO_Address address;
846
847   if (ntohs (hdr->size) < sizeof (struct TransportPingMessage))
848   {
849     GNUNET_break_op (0);
850     return;
851   }
852   ping = (const struct TransportPingMessage *) hdr;
853   if (0 !=
854       memcmp (&ping->target, &GST_my_identity,
855               sizeof (struct GNUNET_PeerIdentity)))
856   {
857     GNUNET_STATISTICS_update (GST_stats,
858                               gettext_noop
859                               ("# PING message for different peer received"), 1,
860                               GNUNET_NO);
861     return;
862   }
863   GNUNET_STATISTICS_update (GST_stats,
864                             gettext_noop ("# PING messages received"), 1,
865                             GNUNET_NO);
866   addr = (const char *) &ping[1];
867   alen = ntohs (hdr->size) - sizeof (struct TransportPingMessage);
868   /* peer wants to confirm that this is one of our addresses, this is what is
869    * used for address validation */
870
871   sig_cache = NULL;
872   sig_cache_exp = NULL;
873
874   if (alen > 0)
875   {
876     addrend = memchr (addr, '\0', alen);
877     if (NULL == addrend)
878     {
879       GNUNET_break_op (0);
880       return;
881     }
882     addrend++;
883     slen = strlen (addr) + 1;
884     alen -= slen;
885     address.address = addrend;
886     address.address_length = alen;
887     address.transport_name = addr;
888     address.peer = GST_my_identity;
889
890
891     if (GNUNET_YES != GST_hello_test_address (&address, &sig_cache, &sig_cache_exp))
892     {
893       if (GNUNET_NO == buggy)
894       {
895         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896                     "Not confirming PING from peer `%s' with address `%s' since I cannot confirm having this address.\n",
897                     GNUNET_i2s (sender),
898                     GST_plugins_a2s (&address));
899         return;
900       }
901       else
902       {
903         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904                     _("Received a PING message with validation bug from `%s'\n"),
905                     GNUNET_i2s (sender));
906       }
907     }
908   }
909   else
910   {
911     addrend = NULL;             /* make gcc happy */
912     slen = 0;
913     static struct GNUNET_CRYPTO_EccSignature no_address_signature;
914     static struct GNUNET_TIME_Absolute no_address_signature_expiration;
915
916     sig_cache = &no_address_signature;
917     sig_cache_exp = &no_address_signature_expiration;
918   }
919
920   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921               "I am `%s', sending PONG to peer `%s'\n",
922               GNUNET_h2s (&GST_my_identity.hashPubKey),
923               GNUNET_i2s (sender));
924
925   /* message with structure:
926    * [TransportPongMessage][Transport name][Address] */
927
928   pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
929   pong->header.size =
930       htons (sizeof (struct TransportPongMessage) + alen + slen);
931   pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
932   pong->purpose.size =
933       htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
934              sizeof (uint32_t) + sizeof (struct GNUNET_TIME_AbsoluteNBO) +
935              alen + slen);
936   pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
937   memcpy (&pong->challenge, &ping->challenge, sizeof (ping->challenge));
938   pong->addrlen = htonl (alen + slen);
939   memcpy (&pong[1], addr, slen);   /* Copy transport plugin */
940   if (alen > 0)
941   {
942     GNUNET_assert (NULL != addrend);
943     memcpy (&((char *) &pong[1])[slen], addrend, alen);
944   }
945   if (GNUNET_TIME_absolute_get_remaining (*sig_cache_exp).rel_value <
946       PONG_SIGNATURE_LIFETIME.rel_value / 4)
947   {
948     /* create / update cached sig */
949     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950                 "Creating PONG signature to indicate ownership.\n");
951     *sig_cache_exp = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
952     pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
953     GNUNET_assert (GNUNET_OK ==
954                    GNUNET_CRYPTO_ecc_sign (GST_my_private_key, &pong->purpose,
955                                            sig_cache));
956   }
957   else
958   {
959     pong->expiration = GNUNET_TIME_absolute_hton (*sig_cache_exp);
960   }
961   pong->signature = *sig_cache;
962   
963   GNUNET_assert (sender_address != NULL);
964
965   /* first see if the session we got this PING from can be used to transmit
966    * a response reliably */
967   papi = GST_plugins_find (sender_address->transport_name);
968   if (papi == NULL)
969     ret = -1;
970   else
971   {
972     GNUNET_assert (papi->send != NULL);
973     GNUNET_assert (papi->get_session != NULL);
974
975     if (session == NULL)
976     {
977       session = papi->get_session (papi->cls, sender_address);
978     }
979     if (session == NULL)
980     {
981       GNUNET_break (0);
982       ret = -1;
983     }
984     else
985     {
986       ret = papi->send (papi->cls, session,
987                         (const char *) pong, ntohs (pong->header.size),
988                         PONG_PRIORITY, ACCEPTABLE_PING_DELAY,
989                         NULL, NULL);
990     }
991   }
992   if (ret != -1)
993   {
994     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995                 "Transmitted PONG to `%s' via reliable mechanism\n",
996                 GNUNET_i2s (sender));
997     /* done! */
998     GNUNET_STATISTICS_update (GST_stats,
999                               gettext_noop
1000                               ("# PONGs unicast via reliable transport"), 1,
1001                               GNUNET_NO);
1002     GNUNET_free (pong);
1003     return;
1004   }
1005
1006   /* no reliable method found, try transmission via all known addresses */
1007   GNUNET_STATISTICS_update (GST_stats,
1008                             gettext_noop
1009                             ("# PONGs multicast to all available addresses"), 1,
1010                             GNUNET_NO);
1011   GST_validation_get_addresses (sender, &multicast_pong, pong);
1012   GNUNET_free (pong);
1013 }
1014
1015
1016 /**
1017  * Context for the 'validate_address' function
1018  */
1019 struct ValidateAddressContext
1020 {
1021   /**
1022    * Hash of the public key of the peer whose address is being validated.
1023    */
1024   struct GNUNET_PeerIdentity pid;
1025
1026   /**
1027    * Public key of the peer whose address is being validated.
1028    */
1029   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
1030 };
1031
1032
1033 /**
1034  * Iterator callback to go over all addresses and try to validate them
1035  * (unless blocked or already validated).
1036  *
1037  * @param cls pointer to a 'struct ValidateAddressContext'
1038  * @param address the address
1039  * @param expiration expiration time
1040  * @return GNUNET_OK (keep the address)
1041  */
1042 static int
1043 validate_address_iterator (void *cls,
1044                            const struct GNUNET_HELLO_Address *address,
1045                            struct GNUNET_TIME_Absolute expiration)
1046 {
1047   const struct ValidateAddressContext *vac = cls;
1048   struct ValidationEntry *ve;
1049
1050   if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
1051     return GNUNET_OK;           /* expired */
1052   ve = find_validation_entry (&vac->public_key, address);
1053   if (GNUNET_SCHEDULER_NO_TASK == ve->revalidation_task)
1054     ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1055   return GNUNET_OK;
1056 }
1057
1058
1059 /**
1060  * Add the validated peer address to the HELLO.
1061  *
1062  * @param cls the 'struct ValidationEntry' with the validated address
1063  * @param max space in buf
1064  * @param buf where to add the address
1065  * @return number of bytes written, 0 to signal the
1066  *         end of the iteration.
1067  */
1068 static size_t
1069 add_valid_peer_address (void *cls, size_t max, void *buf)
1070 {
1071   struct ValidationEntry *ve = cls;
1072
1073   if (GNUNET_YES == ve->copied)
1074     return 0;                   /* terminate */
1075   ve->copied = GNUNET_YES;
1076   return GNUNET_HELLO_add_address (ve->address, ve->valid_until, buf, max);
1077 }
1078
1079
1080 /**
1081  * We've received a PONG.  Check if it matches a pending PING and
1082  * mark the respective address as confirmed.
1083  *
1084  * @param sender peer sending the PONG
1085  * @param hdr the PONG
1086  */
1087 void
1088 GST_validation_handle_pong (const struct GNUNET_PeerIdentity *sender,
1089                             const struct GNUNET_MessageHeader *hdr)
1090 {
1091   const struct TransportPongMessage *pong;
1092   struct ValidationEntry *ve;
1093   const char *tname;
1094   const char *addr;
1095   size_t addrlen;
1096   size_t slen;
1097   size_t size;
1098   struct GNUNET_HELLO_Message *hello;
1099   struct GNUNET_HELLO_Address address;
1100
1101   if (ntohs (hdr->size) < sizeof (struct TransportPongMessage))
1102   {
1103     GNUNET_break_op (0);
1104     return;
1105   }
1106   GNUNET_STATISTICS_update (GST_stats,
1107                             gettext_noop ("# PONG messages received"), 1,
1108                             GNUNET_NO);
1109
1110   /* message with structure:
1111    * [TransportPongMessage][Transport name][Address] */
1112
1113   pong = (const struct TransportPongMessage *) hdr;
1114   tname = (const char *) &pong[1];
1115   size = ntohs (hdr->size) - sizeof (struct TransportPongMessage);
1116   addr = memchr (tname, '\0', size);
1117   if (NULL == addr)
1118   {
1119     GNUNET_break_op (0);
1120     return;
1121   }
1122   addr++;
1123   slen = strlen (tname) + 1;
1124   addrlen = size - slen;
1125   address.peer = *sender;
1126   address.address = addr;
1127   address.address_length = addrlen;
1128   address.transport_name = tname;
1129   ve = find_validation_entry (NULL, &address);
1130   if ((NULL == ve) || (GNUNET_NO == ve->expecting_pong))
1131   {
1132     GNUNET_STATISTICS_update (GST_stats,
1133                               gettext_noop
1134                               ("# PONGs dropped, no matching pending validation"),
1135                               1, GNUNET_NO);
1136     return;
1137   }
1138   /* now check that PONG is well-formed */
1139   if (0 != memcmp (&ve->pid, sender, sizeof (struct GNUNET_PeerIdentity)))
1140   {
1141     GNUNET_break_op (0);
1142     return;
1143   }
1144   if (GNUNET_TIME_absolute_get_remaining
1145       (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
1146   {
1147     GNUNET_STATISTICS_update (GST_stats,
1148                               gettext_noop
1149                               ("# PONGs dropped, signature expired"), 1,
1150                               GNUNET_NO);
1151     return;
1152   }
1153   if (GNUNET_OK !=
1154       GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
1155                                 &pong->purpose, &pong->signature,
1156                                 &ve->public_key))
1157   {
1158     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1159                 "Invalid signature on address %s:%s from peer `%s'\n",
1160                 tname, GST_plugins_a2s (ve->address),
1161                 GNUNET_i2s (sender));
1162     return;
1163   }
1164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165               "Address validated for peer `%s' with plugin `%s': `%s'\n",
1166               GNUNET_i2s (sender), tname, GST_plugins_a2s (ve->address));
1167   /* validity achieved, remember it! */
1168   ve->expecting_pong = GNUNET_NO;
1169   ve->valid_until = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1170   ve->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
1171   {
1172     struct GNUNET_ATS_Information ats;
1173     ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1174     ats.value = htonl ((uint32_t) ve->latency.rel_value);
1175     GNUNET_ATS_address_add (GST_ats, ve->address, NULL, &ats, 1);
1176   }
1177   /* build HELLO to store in PEERINFO */
1178   ve->copied = GNUNET_NO;
1179   hello = GNUNET_HELLO_create (&ve->public_key, &add_valid_peer_address, ve);
1180   GNUNET_PEERINFO_add_peer (GST_peerinfo, hello, NULL, NULL);
1181   GNUNET_free (hello);
1182 }
1183
1184
1185 /**
1186  * We've received a HELLO, check which addresses are new and trigger
1187  * validation.
1188  *
1189  * @param hello the HELLO we received
1190  */
1191 void
1192 GST_validation_handle_hello (const struct GNUNET_MessageHeader *hello)
1193 {
1194   const struct GNUNET_HELLO_Message *hm =
1195       (const struct GNUNET_HELLO_Message *) hello;
1196   struct ValidateAddressContext vac;
1197   struct GNUNET_HELLO_Message *h;
1198
1199   if ((GNUNET_OK != GNUNET_HELLO_get_id (hm, &vac.pid)) ||
1200       (GNUNET_OK != GNUNET_HELLO_get_key (hm, &vac.public_key)))
1201   {
1202     /* malformed HELLO */
1203     GNUNET_break (0);
1204     return;
1205   }
1206   if (0 ==
1207       memcmp (&GST_my_identity, &vac.pid, sizeof (struct GNUNET_PeerIdentity)))
1208     return;
1209   /* Add peer identity without addresses to peerinfo service */
1210   h = GNUNET_HELLO_create (&vac.public_key, NULL, NULL);
1211   GNUNET_PEERINFO_add_peer (GST_peerinfo, h, NULL, NULL);
1212
1213   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214               _("Adding `%s' without addresses for peer `%s'\n"), "HELLO",
1215               GNUNET_i2s (&vac.pid));
1216
1217   GNUNET_free (h);
1218   GNUNET_assert (NULL ==
1219                  GNUNET_HELLO_iterate_addresses (hm, GNUNET_NO,
1220                                                  &validate_address_iterator,
1221                                                  &vac));
1222 }
1223
1224
1225 /**
1226  * Closure for 'iterate_addresses'
1227  */
1228 struct IteratorContext
1229 {
1230   /**
1231    * Function to call on each address.
1232    */
1233   GST_ValidationAddressCallback cb;
1234
1235   /**
1236    * Closure for 'cb'.
1237    */
1238   void *cb_cls;
1239
1240 };
1241
1242
1243 /**
1244  * Call the callback in the closure for each validation entry.
1245  *
1246  * @param cls the 'struct GST_ValidationIteratorContext'
1247  * @param key the peer's identity
1248  * @param value the 'struct ValidationEntry'
1249  * @return GNUNET_OK (continue to iterate)
1250  */
1251 static int
1252 iterate_addresses (void *cls, const struct GNUNET_HashCode * key, void *value)
1253 {
1254   struct IteratorContext *ic = cls;
1255   struct ValidationEntry *ve = value;
1256
1257   ic->cb (ic->cb_cls, &ve->public_key, ve->valid_until, ve->revalidation_block,
1258           ve->address);
1259   return GNUNET_OK;
1260 }
1261
1262
1263 /**
1264  * Call the given function for each address for the given target.
1265  * Can either give a snapshot (synchronous API) or be continuous.
1266  *
1267  * @param target peer information is requested for
1268  * @param cb function to call; will not be called after this function returns
1269  * @param cb_cls closure for 'cb'
1270  */
1271 void
1272 GST_validation_get_addresses (const struct GNUNET_PeerIdentity *target,
1273                               GST_ValidationAddressCallback cb, void *cb_cls)
1274 {
1275   struct IteratorContext ic;
1276
1277   ic.cb = cb;
1278   ic.cb_cls = cb_cls;
1279   GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
1280                                               &target->hashPubKey,
1281                                               &iterate_addresses, &ic);
1282 }
1283
1284
1285 /**
1286  * Update if we are using an address for a connection actively right now.
1287  * Based on this, the validation module will measure latency for the
1288  * address more or less often.
1289  *
1290  * @param address the address
1291  * @param session the session
1292  * @param in_use GNUNET_YES if we are now using the address for a connection,
1293  *               GNUNET_NO if we are no longer using the address for a connection
1294  * @param line line of caller just for DEBUGGING!
1295  */
1296 void
1297 GST_validation_set_address_use (const struct GNUNET_HELLO_Address *address,
1298                                 struct Session *session,
1299                                 int in_use,
1300                                 int line)
1301 {
1302   struct ValidationEntry *ve;
1303
1304   if (NULL != address)
1305     ve = find_validation_entry (NULL, address);
1306   else
1307     ve = NULL;                  /* FIXME: lookup based on session... */
1308   if (NULL == ve)
1309   {
1310     /* this can happen for inbound connections (sender_address_len == 0); */
1311     return;
1312   }
1313   if (ve->in_use == in_use)
1314   {
1315
1316     if (GNUNET_YES == in_use)
1317     {
1318       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1319                   "Error setting address in use for peer `%s' `%s' to USED: set last time by %i, called now by %i\n",
1320                   GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1321                   ve->last_line_set_to_yes, line);
1322     }
1323     if (GNUNET_NO == in_use)
1324     {
1325       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1326                   "Error setting address in use for peer `%s' `%s' to NOT_USED: set last time by %i, called now by %i\n",
1327                   GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
1328                   ve->last_line_set_to_no, line);
1329     }
1330   }
1331
1332   if (GNUNET_YES == in_use)
1333   {
1334     ve->last_line_set_to_yes = line;
1335   }
1336   if (GNUNET_NO == in_use)
1337   {
1338     ve->last_line_set_to_no = line;
1339   }
1340
1341   GNUNET_break (ve->in_use != in_use);  /* should be different... */
1342   ve->in_use = in_use;
1343   if (in_use == GNUNET_YES)
1344   {
1345     /* from now on, higher frequeny, so reschedule now */
1346     GNUNET_SCHEDULER_cancel (ve->revalidation_task);
1347     ve->revalidation_task = GNUNET_SCHEDULER_add_now (&revalidate_address, ve);
1348   }
1349 }
1350
1351
1352 /**
1353  * Query validation about the latest observed latency on a given
1354  * address.
1355  *
1356  * @param sender peer
1357  * @param address the address
1358  * @param session session
1359  * @return observed latency of the address, FOREVER if the address was
1360  *         never successfully validated
1361  */
1362 struct GNUNET_TIME_Relative
1363 GST_validation_get_address_latency (const struct GNUNET_PeerIdentity *sender,
1364                                     const struct GNUNET_HELLO_Address *address,
1365                                     struct Session *session)
1366 {
1367   struct ValidationEntry *ve;
1368
1369   if (NULL == address)
1370   {
1371     GNUNET_break (0);           // FIXME: support having latency only with session...
1372     return GNUNET_TIME_UNIT_FOREVER_REL;
1373   }
1374   ve = find_validation_entry (NULL, address);
1375   if (NULL == ve)
1376     return GNUNET_TIME_UNIT_FOREVER_REL;
1377   return ve->latency;
1378 }
1379
1380
1381 /* end of file gnunet-service-transport_validation.c */