(no commit message)
[oweals/gnunet.git] / src / transport / gnunet-service-transport.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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.c
23  * @brief low-level P2P messaging
24  * @author Christian Grothoff
25  *
26  */
27 #include "platform.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_plugin_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_service_lib.h"
38 #include "gnunet_signatures.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
41 #include "transport_ats.h"
42
43
44 #define DEBUG_BLACKLIST GNUNET_NO
45
46 #define DEBUG_PING_PONG GNUNET_NO
47
48 #define DEBUG_TRANSPORT_HELLO GNUNET_NO
49
50 #define DEBUG_INBOUND GNUNET_NO
51
52 /**
53  * Should we do some additional checks (to validate behavior
54  * of clients)?
55  */
56 #define EXTRA_CHECKS GNUNET_YES
57
58 /**
59  * How many messages can we have pending for a given client process
60  * before we start to drop incoming messages?  We typically should
61  * have only one client and so this would be the primary buffer for
62   * messages, so the number should be chosen rather generously.
63  *
64  * The expectation here is that most of the time the queue is large
65  * enough so that a drop is virtually never required.  Note that
66  * this value must be about as large as 'TOTAL_MSGS' in the
67  * 'test_transport_api_reliability.c', otherwise that testcase may
68  * fail.
69  */
70 #define MAX_PENDING (128 * 1024)
71
72 /**
73  * Size of the per-transport blacklist hash maps.
74  */
75 #define TRANSPORT_BLACKLIST_HT_SIZE 16
76
77 /**
78  * How often should we try to reconnect to a peer using a particular
79  * transport plugin before giving up?  Note that the plugin may be
80  * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
81  */
82 #define MAX_CONNECT_RETRY 3
83
84 /**
85  * Limit on the number of ready-to-run tasks when validating
86  * HELLOs.  If more tasks are ready to run, we will drop
87  * HELLOs instead of validating them.
88  */
89 #define MAX_HELLO_LOAD 4
90
91 /**
92  * How often must a peer violate bandwidth quotas before we start
93  * to simply drop its messages?
94  */
95 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
96
97 /**
98  * How long until a HELLO verification attempt should time out?
99  * Must be rather small, otherwise a partially successful HELLO
100  * validation (some addresses working) might not be available
101  * before a client's request for a connection fails for good.
102  * Besides, if a single request to an address takes a long time,
103  * then the peer is unlikely worthwhile anyway.
104  */
105 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
106
107 /**
108  * How long is a PONG signature valid?  We'll recycle a signature until
109  * 1/4 of this time is remaining.  PONGs should expire so that if our
110  * external addresses change an adversary cannot replay them indefinitely.
111  * OTOH, we don't want to spend too much time generating PONG signatures,
112  * so they must have some lifetime to reduce our CPU usage.
113  */
114 #define PONG_SIGNATURE_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
115
116 /**
117  * Priority to use for PONG messages.
118  */
119 #define TRANSPORT_PONG_PRIORITY 4
120
121 /**
122  * How often do we re-add (cheaper) plugins to our list of plugins
123  * to try for a given connected peer?
124  */
125 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
126
127 /**
128  * After how long do we expire an address in a HELLO that we just
129  * validated?  This value is also used for our own addresses when we
130  * create a HELLO.
131  */
132 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
133
134
135 /**
136  * How long before an existing address expires should we again try to
137  * validate it?  Must be (significantly) smaller than
138  * HELLO_ADDRESS_EXPIRATION.
139  */
140 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
141
142 /**
143  * List of addresses of other peers
144  */
145 struct ForeignAddressList
146 {
147   /**
148    * This is a linked list.
149    */
150   struct ForeignAddressList *next;
151
152   /**
153    * Which ready list does this entry belong to.
154    */
155   struct ReadyList *ready_list;
156
157   /**
158    * How long until we auto-expire this address (unless it is
159    * re-confirmed by the transport)?
160    */
161   struct GNUNET_TIME_Absolute expires;
162
163   /**
164    * Task used to re-validate addresses, updates latencies and
165    * verifies liveness.
166    */
167   GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
168
169   /**
170    * The address.
171    */
172   const void *addr;
173
174   /**
175    * Session (or NULL if no valid session currently exists or if the
176    * plugin does not use sessions).
177    */
178   struct Session *session;
179
180   struct ATS_ressource_entry * ressources;
181
182   struct ATS_quality_entry * quality;
183
184   /**
185    * What was the last latency observed for this address, plugin and peer?
186    */
187   struct GNUNET_TIME_Relative latency;
188
189   /**
190    * If we did not successfully transmit a message to the given peer
191    * via this connection during the specified time, we should consider
192    * the connection to be dead.  This is used in the case that a TCP
193    * transport simply stalls writing to the stream but does not
194    * formerly get a signal that the other peer died.
195    */
196   struct GNUNET_TIME_Absolute timeout;
197
198   /**
199    * How often have we tried to connect using this plugin?  Used to
200    * discriminate against addresses that do not work well.
201    * FIXME: not yet used, but should be!
202    */
203   unsigned int connect_attempts;
204
205   /**
206    * DV distance to this peer (1 if no DV is used).
207    * FIXME: need to set this from transport plugins!
208    */
209   uint32_t distance;
210
211   /**
212    * Length of addr.
213    */
214   uint16_t addrlen;
215
216   /**
217    * Have we ever estimated the latency of this address?  Used to
218    * ensure that the first time we add an address, we immediately
219    * probe its latency.
220    */
221   int8_t estimated;
222
223   /**
224    * Are we currently connected via this address?  The first time we
225    * successfully transmit or receive data to a peer via a particular
226    * address, we set this to GNUNET_YES.  If we later get an error
227    * (disconnect notification, transmission failure, timeout), we set
228    * it back to GNUNET_NO.
229    */
230   int8_t connected;
231
232   /**
233    * Is this plugin currently busy transmitting to the specific target?
234    * GNUNET_NO if not (initial, default state is GNUNET_NO).   Internal
235    * messages do not count as 'in transmit'.
236    */
237   int8_t in_transmit;
238
239   /**
240    * Has this address been validated yet?
241    */
242   int8_t validated;
243
244 };
245
246
247 /**
248  * Entry in linked list of network addresses for ourselves.  Also
249  * includes a cached signature for 'struct TransportPongMessage's.
250  */
251 struct OwnAddressList
252 {
253   /**
254    * This is a linked list.
255    */
256   struct OwnAddressList *next;
257
258   /**
259    * How long until the current signature expires? (ZERO if the
260    * signature was never created).
261    */
262   struct GNUNET_TIME_Absolute pong_sig_expires;
263
264   /**
265    * Signature for a 'struct TransportPongMessage' for this address.
266    */
267   struct GNUNET_CRYPTO_RsaSignature pong_signature;
268
269   /**
270    * Length of addr.
271    */
272   uint32_t addrlen;
273
274 };
275
276
277 /**
278  * Entry in linked list of all of our plugins.
279  */
280 struct TransportPlugin
281 {
282   /**
283    * This is a linked list.
284    */
285   struct TransportPlugin *next;
286
287   /**
288    * API of the transport as returned by the plugin's
289    * initialization function.
290    */
291   struct GNUNET_TRANSPORT_PluginFunctions *api;
292
293   /**
294    * Short name for the plugin (i.e. "tcp").
295    */
296   char *short_name;
297
298   /**
299    * Name of the library (i.e. "gnunet_plugin_transport_tcp").
300    */
301   char *lib_name;
302
303   /**
304    * List of our known addresses for this transport.
305    */
306   struct OwnAddressList *addresses;
307
308   /**
309    * Environment this transport service is using
310    * for this plugin.
311    */
312   struct GNUNET_TRANSPORT_PluginEnvironment env;
313
314   /**
315    * ID of task that is used to clean up expired addresses.
316    */
317   GNUNET_SCHEDULER_TaskIdentifier address_update_task;
318
319   /**
320    * Set to GNUNET_YES if we need to scrap the existing list of
321    * "addresses" and start fresh when we receive the next address
322    * update from a transport.  Set to GNUNET_NO if we should just add
323    * the new address to the list and wait for the commit call.
324    */
325   int rebuild;
326
327   struct ATS_plugin * rc;
328
329   /**
330    * Hashmap of blacklisted peers for this particular transport.
331    */
332   struct GNUNET_CONTAINER_MultiHashMap *blacklist;
333 };
334
335 struct NeighbourList;
336
337 /**
338  * For each neighbour we keep a list of messages
339  * that we still want to transmit to the neighbour.
340  */
341 struct MessageQueue
342 {
343
344   /**
345    * This is a doubly linked list.
346    */
347   struct MessageQueue *next;
348
349   /**
350    * This is a doubly linked list.
351    */
352   struct MessageQueue *prev;
353
354   /**
355    * The message(s) we want to transmit, GNUNET_MessageHeader(s)
356    * stuck together in memory.  Allocated at the end of this struct.
357    */
358   const char *message_buf;
359
360   /**
361    * Size of the message buf
362    */
363   size_t message_buf_size;
364
365   /**
366    * Client responsible for queueing the message;
367    * used to check that a client has no two messages
368    * pending for the same target.  Can be NULL.
369    */
370   struct TransportClient *client;
371
372   /**
373    * Using which specific address should we send this message?
374    */
375   struct ForeignAddressList *specific_address;
376
377   /**
378    * Peer ID of the Neighbour this entry belongs to.
379    */
380   struct GNUNET_PeerIdentity neighbour_id;
381
382   /**
383    * Plugin that we used for the transmission.
384    * NULL until we scheduled a transmission.
385    */
386   struct TransportPlugin *plugin;
387
388   /**
389    * At what time should we fail?
390    */
391   struct GNUNET_TIME_Absolute timeout;
392
393   /**
394    * Internal message of the transport system that should not be
395    * included in the usual SEND-SEND_OK transmission confirmation
396    * traffic management scheme.  Typically, "internal_msg" will
397    * be set whenever "client" is NULL (but it is not strictly
398    * required).
399    */
400   int internal_msg;
401
402   /**
403    * How important is the message?
404    */
405   unsigned int priority;
406
407 };
408
409
410 /**
411  * For a given Neighbour, which plugins are available
412  * to talk to this peer and what are their costs?
413  */
414 struct ReadyList
415 {
416   /**
417    * This is a linked list.
418    */
419   struct ReadyList *next;
420
421   /**
422    * Which of our transport plugins does this entry
423    * represent?
424    */
425   struct TransportPlugin *plugin;
426
427   /**
428    * Transport addresses, latency, and readiness for
429    * this particular plugin.
430    */
431   struct ForeignAddressList *addresses;
432
433   /**
434    * To which neighbour does this ready list belong to?
435    */
436   struct NeighbourList *neighbour;
437 };
438
439
440 /**
441  * Entry in linked list of all of our current neighbours.
442  */
443 struct NeighbourList
444 {
445
446   /**
447    * This is a linked list.
448    */
449   struct NeighbourList *next;
450
451   /**
452    * Which of our transports is connected to this peer
453    * and what is their status?
454    */
455   struct ReadyList *plugins;
456
457   /**
458    * Head of list of messages we would like to send to this peer;
459    * must contain at most one message per client.
460    */
461   struct MessageQueue *messages_head;
462
463   /**
464    * Tail of list of messages we would like to send to this peer; must
465    * contain at most one message per client.
466    */
467   struct MessageQueue *messages_tail;
468
469   /**
470    * Head of list of messages of messages we expected the continuation
471    * to be called to destroy the message
472    */
473   struct MessageQueue *cont_head;
474
475   /**
476    * Tail of list of messages of messages we expected the continuation
477    * to be called to destroy the message
478    */
479   struct MessageQueue *cont_tail;
480
481   /**
482    * Buffer for at most one payload message used when we receive
483    * payload data before our PING-PONG has succeeded.  We then
484    * store such messages in this intermediary buffer until the
485    * connection is fully up.
486    */
487   struct GNUNET_MessageHeader *pre_connect_message_buffer;
488
489   /**
490    * Context for peerinfo iteration.
491    * NULL after we are done processing peerinfo's information.
492    */
493   struct GNUNET_PEERINFO_IteratorContext *piter;
494
495   /**
496    * Public key for this peer.   Valid only if the respective flag is set below.
497    */
498   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
499
500   /**
501    * Identity of this neighbour.
502    */
503   struct GNUNET_PeerIdentity id;
504
505   /**
506    * ID of task scheduled to run when this peer is about to
507    * time out (will free resources associated with the peer).
508    */
509   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
510
511   /**
512    * ID of task scheduled to run when we should retry transmitting
513    * the head of the message queue.  Actually triggered when the
514    * transmission is timing out (we trigger instantly when we have
515    * a chance of success).
516    */
517   GNUNET_SCHEDULER_TaskIdentifier retry_task;
518
519   /**
520    * How long until we should consider this peer dead
521    * (if we don't receive another message in the
522    * meantime)?
523    */
524   struct GNUNET_TIME_Absolute peer_timeout;
525
526   /**
527    * Tracker for inbound bandwidth.
528    */
529   struct GNUNET_BANDWIDTH_Tracker in_tracker;
530
531   /**
532    * The latency we have seen for this particular address for
533    * this particular peer.  This latency may have been calculated
534    * over multiple transports.  This value reflects how long it took
535    * us to receive a response when SENDING via this particular
536    * transport/neighbour/address combination!
537    *
538    * FIXME: we need to periodically send PINGs to update this
539    * latency (at least more often than the current "huge" (11h?)
540    * update interval).
541    */
542   struct GNUNET_TIME_Relative latency;
543
544   /**
545    * How often has the other peer (recently) violated the
546    * inbound traffic limit?  Incremented by 10 per violation,
547    * decremented by 1 per non-violation (for each
548    * time interval).
549    */
550   unsigned int quota_violation_count;
551
552   /**
553    * DV distance to this peer (1 if no DV is used).
554    */
555   uint32_t distance;
556
557   /**
558    * Have we seen an PONG from this neighbour in the past (and
559    * not had a disconnect since)?
560    */
561   int received_pong;
562
563   /**
564    * Do we have a valid public key for this neighbour?
565    */
566   int public_key_valid;
567
568   /**
569    * Performance data for the peer.
570    */
571   struct GNUNET_TRANSPORT_ATS_Information *ats;
572 };
573
574 /**
575  * Message used to ask a peer to validate receipt (to check an address
576  * from a HELLO).  Followed by the address we are trying to validate,
577  * or an empty address if we are just sending a PING to confirm that a
578  * connection which the receiver (of the PING) initiated is still valid.
579  */
580 struct TransportPingMessage
581 {
582
583   /**
584    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
585    */
586   struct GNUNET_MessageHeader header;
587
588   /**
589    * Challenge code (to ensure fresh reply).
590    */
591   uint32_t challenge GNUNET_PACKED;
592
593   /**
594    * Who is the intended recipient?
595    */
596   struct GNUNET_PeerIdentity target;
597
598 };
599
600
601 /**
602  * Message used to validate a HELLO.  The challenge is included in the
603  * confirmation to make matching of replies to requests possible.  The
604  * signature signs our public key, an expiration time and our address.<p>
605  *
606  * This message is followed by our transport address that the PING tried
607  * to confirm (if we liked it).  The address can be empty (zero bytes)
608  * if the PING had not address either (and we received the request via
609  * a connection that we initiated).
610  */
611 struct TransportPongMessage
612 {
613
614   /**
615    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
616    */
617   struct GNUNET_MessageHeader header;
618
619   /**
620    * Challenge code from PING (showing freshness).  Not part of what
621    * is signed so that we can re-use signatures.
622    */
623   uint32_t challenge GNUNET_PACKED;
624
625   /**
626    * Signature.
627    */
628   struct GNUNET_CRYPTO_RsaSignature signature;
629
630   /**
631    * What are we signing and why?  Two possible reason codes can be here:
632    * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN to confirm that this is a
633    * plausible address for this peer (pid is set to identity of signer); or
634    * GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING to confirm that this is
635    * an address we used to connect to the peer with the given pid.
636    */
637   struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
638
639   /**
640    * When does this signature expire?
641    */
642   struct GNUNET_TIME_AbsoluteNBO expiration;
643
644   /**
645    * Either the identity of the peer Who signed this message, or the
646    * identity of the peer that we're connected to using the given
647    * address (depending on purpose.type).
648    */
649   struct GNUNET_PeerIdentity pid;
650
651   /**
652    * Size of address appended to this message (part of what is
653    * being signed, hence not redundant).
654    */
655   uint32_t addrlen;
656
657 };
658
659
660 /**
661  * Linked list of messages to be transmitted to the client.  Each
662  * entry is followed by the actual message.
663  */
664 struct ClientMessageQueueEntry
665 {
666   /**
667    * This is a doubly-linked list.
668    */
669   struct ClientMessageQueueEntry *next;
670
671   /**
672    * This is a doubly-linked list.
673    */
674   struct ClientMessageQueueEntry *prev;
675 };
676
677
678 /**
679  * Client connected to the transport service.
680  */
681 struct TransportClient
682 {
683
684   /**
685    * This is a linked list.
686    */
687   struct TransportClient *next;
688
689   /**
690    * Handle to the client.
691    */
692   struct GNUNET_SERVER_Client *client;
693
694   /**
695    * Linked list of messages yet to be transmitted to
696    * the client.
697    */
698   struct ClientMessageQueueEntry *message_queue_head;
699
700   /**
701    * Tail of linked list of messages yet to be transmitted to the
702    * client.
703    */
704   struct ClientMessageQueueEntry *message_queue_tail;
705
706   /**
707    * Current transmit request handle.
708    */
709   struct GNUNET_CONNECTION_TransmitHandle *th;
710
711   /**
712    * Is a call to "transmit_send_continuation" pending?  If so, we
713    * must not free this struct (even if the corresponding client
714    * disconnects) and instead only remove it from the linked list and
715    * set the "client" field to NULL.
716    */
717   int tcs_pending;
718
719   /**
720    * Length of the list of messages pending for this client.
721    */
722   unsigned int message_count;
723
724 };
725
726
727 /**
728  * Context of currently active requests to peerinfo
729  * for validation of HELLOs.
730  */
731 struct CheckHelloValidatedContext;
732
733
734 /**
735  * Entry in map of all HELLOs awaiting validation.
736  */
737 struct ValidationEntry 
738 {
739
740   /**
741    * NULL if this entry is not part of a larger HELLO validation.
742    */
743   struct CheckHelloValidatedContext *chvc;
744
745   /**
746    * The address, actually a pointer to the end
747    * of this struct.  Do not free!
748    */
749   const void *addr;
750
751   /**
752    * Name of the transport.
753    */
754   char *transport_name;
755
756   /**
757    * The public key of the peer.
758    */
759   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
760
761   /**
762    * ID of task that will clean up this entry if we don't succeed
763    * with the validation first.
764    */
765   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
766
767   /**
768    * At what time did we send this validation?
769    */
770   struct GNUNET_TIME_Absolute send_time;
771
772   /**
773    * Session being validated (or NULL for none).
774    */
775   struct Session *session;
776
777   /**
778    * Challenge number we used.
779    */
780   uint32_t challenge;
781
782   /**
783    * Length of addr.
784    */
785   uint16_t addrlen;
786
787 };
788
789
790 /**
791  * Context of currently active requests to peerinfo
792  * for validation of HELLOs.
793  */
794 struct CheckHelloValidatedContext
795 {
796
797   /**
798    * This is a doubly-linked list.
799    */
800   struct CheckHelloValidatedContext *next;
801
802   /**
803    * This is a doubly-linked list.
804    */
805   struct CheckHelloValidatedContext *prev;
806
807   /**
808    * Hello that we are validating.
809    */
810   const struct GNUNET_HELLO_Message *hello;
811
812   /**
813    * Context for peerinfo iteration.
814    * NULL after we are done processing peerinfo's information.
815    */
816   struct GNUNET_PEERINFO_IteratorContext *piter;
817
818   /**
819    * Was a HELLO known for this peer to peerinfo?
820    */
821   int hello_known;
822
823   /**
824    * Number of validation entries currently referring to this
825    * CHVC.
826    */
827   unsigned int ve_count;
828 };
829
830
831 /**
832  * All zero hash for comparison.
833  */
834 static GNUNET_HashCode null_hash;
835
836 /**
837  * Our HELLO message.
838  */
839 static struct GNUNET_HELLO_Message *our_hello;
840
841 /**
842  * Our public key.
843  */
844 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
845
846 /**
847  * Our identity.
848  */
849 static struct GNUNET_PeerIdentity my_identity;
850
851 /**
852  * Our private key.
853  */
854 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
855
856 /**
857  * Our configuration.
858  */
859 const struct GNUNET_CONFIGURATION_Handle *cfg;
860
861 /**
862  * Linked list of all clients to this service.
863  */
864 static struct TransportClient *clients;
865
866 /**
867  * All loaded plugins.
868  */
869 static struct TransportPlugin *plugins;
870
871 /**
872  * Handle to peerinfo service.
873  */
874 static struct GNUNET_PEERINFO_Handle *peerinfo;
875
876 /**
877  * All known neighbours and their HELLOs.
878  */
879 static struct NeighbourList *neighbours;
880
881 /**
882  * Number of neighbours we'd like to have.
883  */
884 static uint32_t max_connect_per_transport;
885
886 /**
887  * Head of linked list.
888  */
889 static struct CheckHelloValidatedContext *chvc_head;
890
891 /**
892  * Tail of linked list.
893  */
894 static struct CheckHelloValidatedContext *chvc_tail;
895
896 /**
897  * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
898  * of the given peer that we are currently validating).
899  */
900 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
901
902 /**
903  * Handle for reporting statistics.
904  */
905 static struct GNUNET_STATISTICS_Handle *stats;
906
907 /**
908  * Identifier of 'refresh_hello' task.
909  */
910 static GNUNET_SCHEDULER_TaskIdentifier hello_task;
911
912 /**
913  * Identifier of ats scheduler task.
914  */
915 static GNUNET_SCHEDULER_TaskIdentifier ats_task;
916
917 /**
918  * Is transport service shutting down ?
919  */
920 static int shutdown_in_progress;
921
922 /**
923  * Handle for ats information
924  */
925 static struct ATS_Handle *ats;
926
927 /**
928  * Time of last ats execution
929  */
930 struct GNUNET_TIME_Absolute last_ats_execution;
931 /**
932  * Minimum interval between two ATS executions
933  */
934 struct GNUNET_TIME_Relative ats_minimum_interval;
935 /**
936  * Regular interval when ATS execution is triggered
937  */
938 struct GNUNET_TIME_Relative ats_regular_interval;
939
940 /**
941  * The peer specified by the given neighbour has timed-out or a plugin
942  * has disconnected.  We may either need to do nothing (other plugins
943  * still up), or trigger a full disconnect and clean up.  This
944  * function updates our state and do the necessary notifications.
945  * Also notifies our clients that the neighbour is now officially
946  * gone.
947  *
948  * @param n the neighbour list entry for the peer
949  * @param check should we just check if all plugins
950  *        disconnected or must we ask all plugins to
951  *        disconnect?
952  */
953 static void disconnect_neighbour (struct NeighbourList *n, int check);
954
955 /**
956  * Check the ready list for the given neighbour and if a plugin is
957  * ready for transmission (and if we have a message), do so!
958  *
959  * @param nexi target peer for which to transmit
960  */
961 static void try_transmission_to_peer (struct NeighbourList *n);
962
963 struct ForeignAddressList * get_preferred_ats_address (
964                 struct NeighbourList *n);
965
966 /**
967  * Find an entry in the neighbour list for a particular peer.
968  *
969  * @return NULL if not found.
970  */
971 static struct NeighbourList *
972 find_neighbour (const struct GNUNET_PeerIdentity *key)
973 {
974   struct NeighbourList *head = neighbours;
975
976   while ((head != NULL) &&
977         (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
978     head = head->next;
979   return head;
980 }
981
982 static int update_addr_value (struct ForeignAddressList *fal, uint32_t value , int ats_index)
983 {
984   int c;
985   int set = GNUNET_NO;
986   for (c=0; c<available_quality_metrics; c++)
987   {
988     if (ats_index == qm[c].atis_index)
989     {
990       fal->quality[c].values[0] = fal->quality[c].values[1];
991       fal->quality[c].values[1] = fal->quality[c].values[2];
992       fal->quality[c].values[2] = value;
993       set = GNUNET_YES;
994       ats_modify_problem_state (ats, ATS_QUALITY_UPDATED);
995     }
996   }
997   if (set == GNUNET_NO)
998   {
999     for (c=0; c<available_ressources; c++)
1000     {
1001       if (ats_index == ressources[c].atis_index)
1002       {
1003         fal->ressources[c].c = value;
1004         set = GNUNET_YES;
1005         ats_modify_problem_state (ats, ATS_COST_UPDATED);
1006       }
1007     }
1008   }
1009   return set;
1010 }
1011
1012 static int
1013 update_addr_ats (struct ForeignAddressList *fal, 
1014                  const struct GNUNET_TRANSPORT_ATS_Information *ats_data, 
1015                  int ats_count)
1016 {
1017   int c1, set;
1018   set = GNUNET_NO;
1019   for (c1=0; c1<ats_count; c1++)
1020     {
1021       set = update_addr_value(fal, ntohl(ats_data[c1].value), ntohl(ats_data[c1].type));
1022     }
1023   return set;
1024 }
1025
1026 /**
1027  * Find an entry in the transport list for a particular transport.
1028  *
1029  * @return NULL if not found.
1030  */
1031 static struct TransportPlugin *
1032 find_transport (const char *short_name)
1033 {
1034   struct TransportPlugin *head = plugins;
1035   while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
1036     head = head->next;
1037   return head;
1038 }
1039
1040 /**
1041  * Is a particular peer blacklisted for a particular transport?
1042  *
1043  * @param peer the peer to check for
1044  * @param plugin the plugin used to connect to the peer
1045  *
1046  * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
1047  */
1048 static int
1049 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
1050 {
1051
1052   if (plugin->blacklist != NULL)
1053     {
1054       if (GNUNET_CONTAINER_multihashmap_contains (plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
1055         {
1056 #if DEBUG_BLACKLIST
1057           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058                       "Peer `%s:%s' is blacklisted!\n",
1059                       plugin->short_name, GNUNET_i2s (peer));
1060 #endif
1061           if (stats != NULL)
1062             GNUNET_STATISTICS_update (stats, "# blacklisted peers refused", 1, GNUNET_NO);
1063           return GNUNET_YES;
1064         }
1065     }
1066
1067   return GNUNET_NO;
1068 }
1069
1070
1071 static void
1072 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, 
1073                        char *transport_name)
1074 {
1075   struct TransportPlugin *plugin;
1076
1077   plugin = find_transport(transport_name);
1078   if (plugin == NULL) /* Nothing to do */
1079     return;
1080 #if DEBUG_TRANSPORT
1081   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082               "Adding peer `%s' with plugin `%s' to blacklist\n",
1083               GNUNET_i2s (peer),
1084               transport_name);
1085 #endif
1086   if (plugin->blacklist == NULL)
1087     plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);
1088   GNUNET_assert(plugin->blacklist != NULL);
1089   GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
1090                                     NULL,
1091                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
1092 }
1093
1094
1095 /**
1096  * Read the blacklist file, containing transport:peer entries.
1097  * Provided the transport is loaded, set up hashmap with these
1098  * entries to blacklist peers by transport.
1099  *
1100  */
1101 static void
1102 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1103 {
1104   char *fn;
1105   char *data;
1106   size_t pos;
1107   size_t colon_pos;
1108   int tsize;
1109   struct GNUNET_PeerIdentity pid;
1110   struct stat frstat;
1111   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
1112   unsigned int entries_found;
1113   char *transport_name;
1114
1115   if (GNUNET_OK !=
1116       GNUNET_CONFIGURATION_get_value_filename (cfg,
1117                                                "TRANSPORT",
1118                                                "BLACKLIST_FILE",
1119                                                &fn))
1120     {
1121 #if DEBUG_TRANSPORT
1122       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123                   "Option `%s' in section `%s' not specified!\n",
1124                   "BLACKLIST_FILE",
1125                   "TRANSPORT");
1126 #endif
1127       return;
1128     }
1129   if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1130     GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1131         | GNUNET_DISK_PERM_USER_WRITE);
1132   if (0 != STAT (fn, &frstat))
1133     {
1134       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1135                   _("Could not read blacklist file `%s'\n"), fn);
1136       GNUNET_free (fn);
1137       return;
1138     }
1139   if (frstat.st_size == 0)
1140     {
1141 #if DEBUG_TRANSPORT
1142       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143                   _("Blacklist file `%s' is empty.\n"),
1144                   fn);
1145 #endif
1146       GNUNET_free (fn);
1147       return;
1148     }
1149   /* FIXME: use mmap */
1150   data = GNUNET_malloc_large (frstat.st_size);
1151   GNUNET_assert(data != NULL);
1152   if (frstat.st_size !=
1153       GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1154     {
1155       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1156                   _("Failed to read blacklist from `%s'\n"), fn);
1157       GNUNET_free (fn);
1158       GNUNET_free (data);
1159       return;
1160     }
1161   entries_found = 0;
1162   pos = 0;
1163   while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1164     pos++;
1165   while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1166          (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1167     {
1168       colon_pos = pos;
1169       while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace ( (unsigned char) data[colon_pos]))
1170         colon_pos++;
1171
1172       if (colon_pos >= frstat.st_size)
1173         {
1174           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1175                       _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1176                       (unsigned long long) colon_pos);
1177           GNUNET_free (fn);
1178           GNUNET_free (data);
1179           return;
1180         }
1181
1182       if (isspace( (unsigned char) data[colon_pos]))
1183       {
1184         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1185                     _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1186                     (unsigned long long) colon_pos);
1187         pos = colon_pos;
1188         while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1189           pos++;
1190         continue;
1191       }
1192       tsize = colon_pos - pos;
1193       if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size) || (tsize == 0))
1194         {
1195           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1196                       _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1197                       (unsigned long long) colon_pos);
1198           GNUNET_free (fn);
1199           GNUNET_free (data);
1200           return;
1201         }
1202
1203       if (tsize < 1)
1204         continue;
1205
1206       transport_name = GNUNET_malloc(tsize + 1);
1207       memcpy(transport_name, &data[pos], tsize);
1208       pos = colon_pos + 1;
1209 #if DEBUG_TRANSPORT
1210       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211                   "Read transport name %s in blacklist file.\n",
1212                   transport_name);
1213 #endif
1214       memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1215       if (!isspace ( (unsigned char) enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1216         {
1217           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1218                       _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1219                       (unsigned long long) pos);
1220           pos++;
1221           while ((pos < frstat.st_size) && (!isspace ( (unsigned char) data[pos])))
1222             pos++;
1223           GNUNET_free_non_null(transport_name);
1224           continue;
1225         }
1226       enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1227       if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1228         {
1229           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1230                       _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1231                       (unsigned long long) pos,
1232                       &enc);
1233         }
1234       else
1235         {
1236           if (0 != memcmp (&pid,
1237                            &my_identity,
1238                            sizeof (struct GNUNET_PeerIdentity)))
1239             {
1240               entries_found++;
1241               add_peer_to_blacklist (&pid,
1242                                      transport_name);
1243             }
1244           else
1245             {
1246               GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1247                           _("Found myself `%s' in blacklist (useless, ignored)\n"),
1248                           GNUNET_i2s (&pid));
1249             }
1250         }
1251       pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1252       GNUNET_free_non_null(transport_name);
1253       while ((pos < frstat.st_size) && isspace ( (unsigned char) data[pos]))
1254         pos++;
1255     }
1256   GNUNET_STATISTICS_update (stats, "# Transport entries blacklisted", entries_found, GNUNET_NO);
1257   GNUNET_free (data);
1258   GNUNET_free (fn);
1259 }
1260
1261
1262 /**
1263  * Function called to notify a client about the socket being ready to
1264  * queue more data.  "buf" will be NULL and "size" zero if the socket
1265  * was closed for writing in the meantime.
1266  *
1267  * @param cls closure
1268  * @param size number of bytes available in buf
1269  * @param buf where the callee should write the message
1270  * @return number of bytes written to buf
1271  */
1272 static size_t
1273 transmit_to_client_callback (void *cls, size_t size, void *buf)
1274 {
1275   struct TransportClient *client = cls;
1276   struct ClientMessageQueueEntry *q;
1277   uint16_t msize;
1278   size_t tsize;
1279   const struct GNUNET_MessageHeader *msg;
1280   char *cbuf;
1281
1282   client->th = NULL;
1283   if (buf == NULL)
1284     {
1285 #if DEBUG_TRANSPORT 
1286       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1287                   "Transmission to client failed, closing connection.\n");
1288 #endif
1289       /* fatal error with client, free message queue! */
1290       while (NULL != (q = client->message_queue_head))
1291         {
1292           GNUNET_STATISTICS_update (stats,
1293                                     gettext_noop ("# bytes discarded (could not transmit to client)"),
1294                                     ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1295                                     GNUNET_NO);
1296           GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1297                                        client->message_queue_tail,
1298                                        q);
1299           GNUNET_free (q);
1300         }
1301       client->message_count = 0;
1302       return 0;
1303     }
1304   cbuf = buf;
1305   tsize = 0;
1306   while (NULL != (q = client->message_queue_head))
1307     {
1308       msg = (const struct GNUNET_MessageHeader *) &q[1];
1309       msize = ntohs (msg->size);
1310       if (msize + tsize > size)
1311         break;
1312 #if DEBUG_TRANSPORT
1313       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1314                   "Transmitting message of type %u to client.\n",
1315                   ntohs (msg->type));
1316 #endif
1317       GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1318                                    client->message_queue_tail,
1319                                    q);
1320       memcpy (&cbuf[tsize], msg, msize);
1321       tsize += msize;
1322       GNUNET_free (q);
1323       client->message_count--;
1324     }
1325   if (NULL != q)
1326     {
1327       GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1328       client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1329                                                         msize,
1330                                                         GNUNET_TIME_UNIT_FOREVER_REL,
1331                                                         &transmit_to_client_callback,
1332                                                         client);
1333       GNUNET_assert (client->th != NULL);
1334     }
1335   return tsize;
1336 }
1337
1338
1339 /**
1340  * Convert an address to a string.
1341  *
1342  * @param plugin name of the plugin responsible for the address
1343  * @param addr binary address
1344  * @param addr_len number of bytes in addr
1345  * @return NULL on error, otherwise address string
1346  */
1347 static const char*
1348 a2s (const char *plugin,
1349      const void *addr,
1350      uint16_t addr_len)
1351 {
1352   struct TransportPlugin *p;
1353
1354   if (plugin == NULL)
1355     return NULL;
1356   p = find_transport (plugin);
1357   if ((p == NULL) || (addr_len == 0) || (addr == NULL))
1358     return NULL;
1359
1360   return p->api->address_to_string (NULL,
1361                                     addr,
1362                                     addr_len);
1363   return NULL;
1364 }
1365
1366
1367
1368
1369 /**
1370  * Iterator to free entries in the validation_map.
1371  *
1372  * @param cls closure (unused)
1373  * @param key current key code
1374  * @param value value in the hash map (validation to abort)
1375  * @return GNUNET_YES (always)
1376  */
1377 static int
1378 abort_validation (void *cls,
1379                   const GNUNET_HashCode * key,
1380                   void *value)
1381 {
1382   struct ValidationEntry *va = value;
1383
1384   if (GNUNET_SCHEDULER_NO_TASK != va->timeout_task)
1385     GNUNET_SCHEDULER_cancel (va->timeout_task);
1386   GNUNET_free (va->transport_name);
1387   if (va->chvc != NULL)
1388     {
1389       va->chvc->ve_count--;
1390       if (va->chvc->ve_count == 0)
1391         {
1392           GNUNET_CONTAINER_DLL_remove (chvc_head,
1393                                        chvc_tail,
1394                                        va->chvc);
1395           GNUNET_free (va->chvc);
1396         }
1397       va->chvc = NULL;
1398     }
1399   GNUNET_free (va);
1400   return GNUNET_YES;
1401 }
1402
1403
1404 /**
1405  * HELLO validation cleanup task (validation failed).
1406  *
1407  * @param cls the 'struct ValidationEntry' that failed
1408  * @param tc scheduler context (unused)
1409  */
1410 static void
1411 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1412 {
1413   struct ValidationEntry *va = cls;
1414   struct GNUNET_PeerIdentity pid;
1415
1416   va->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1417   GNUNET_STATISTICS_update (stats,
1418                             gettext_noop ("# address validation timeouts"),
1419                             1,
1420                             GNUNET_NO);
1421   GNUNET_CRYPTO_hash (&va->publicKey,
1422                       sizeof (struct
1423                               GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1424                       &pid.hashPubKey);
1425   GNUNET_break (GNUNET_OK ==
1426                 GNUNET_CONTAINER_multihashmap_remove (validation_map,
1427                                                       &pid.hashPubKey,
1428                                                       va));
1429   abort_validation (NULL, NULL, va);
1430 }
1431
1432
1433
1434 /**
1435  * Send the specified message to the specified client.  Since multiple
1436  * messages may be pending for the same client at a time, this code
1437  * makes sure that no message is lost.
1438  *
1439  * @param client client to transmit the message to
1440  * @param msg the message to send
1441  * @param may_drop can this message be dropped if the
1442  *        message queue for this client is getting far too large?
1443  */
1444 static void
1445 transmit_to_client (struct TransportClient *client,
1446                     const struct GNUNET_MessageHeader *msg, int may_drop)
1447 {
1448   struct ClientMessageQueueEntry *q;
1449   uint16_t msize;
1450
1451   if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1452     {
1453       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1454                   _
1455                   ("Dropping message of type %u and size %u, have %u messages pending (%u is the soft limit)\n"),
1456                   ntohs (msg->type),
1457                   ntohs (msg->size),
1458                   client->message_count,
1459                   MAX_PENDING);
1460       GNUNET_STATISTICS_update (stats,
1461                                 gettext_noop ("# messages dropped due to slow client"),
1462                                 1,
1463                                 GNUNET_NO);
1464       return;
1465     }
1466   msize = ntohs (msg->size);
1467   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1468   q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1469   memcpy (&q[1], msg, msize);
1470   GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1471                                      client->message_queue_tail,
1472                                      client->message_queue_tail,
1473                                      q);
1474   client->message_count++;
1475   if (client->th == NULL)
1476     {
1477       client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1478                                                         msize,
1479                                                         GNUNET_TIME_UNIT_FOREVER_REL,
1480                                                         &transmit_to_client_callback,
1481                                                         client);
1482       GNUNET_assert (client->th != NULL);
1483     }
1484 }
1485
1486
1487 /**
1488  * Transmit a 'SEND_OK' notification to the given client for the
1489  * given neighbour.
1490  *
1491  * @param client who to notify
1492  * @param n neighbour to notify about, can be NULL (on failure)
1493  * @param target target of the transmission
1494  * @param result status code for the transmission request
1495  */
1496 static void
1497 transmit_send_ok (struct TransportClient *client,
1498                   struct NeighbourList *n,
1499                   const struct GNUNET_PeerIdentity *target,
1500                   int result)
1501 {
1502   struct SendOkMessage send_ok_msg;
1503
1504   send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1505   send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1506   send_ok_msg.success = htonl (result);
1507   if (n != NULL)
1508     send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1509   else
1510     send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
1511   send_ok_msg.peer = *target;
1512   transmit_to_client (client, &send_ok_msg.header, GNUNET_NO);
1513 }
1514
1515
1516 /**
1517  * Mark the given FAL entry as 'connected' (and hence preferred for
1518  * sending); also mark all others for the same peer as 'not connected'
1519  * (since only one can be preferred).
1520  *
1521  * @param fal address to set to 'connected'
1522  */
1523 static void
1524 mark_address_connected (struct ForeignAddressList *fal);
1525
1526
1527
1528 /**
1529  * We should re-try transmitting to the given peer,
1530  * hopefully we've learned something in the meantime.
1531  */
1532 static void
1533 retry_transmission_task (void *cls,
1534                          const struct GNUNET_SCHEDULER_TaskContext *tc)
1535 {
1536   struct NeighbourList *n = cls;
1537
1538   n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1539   try_transmission_to_peer (n);
1540 }
1541
1542
1543 /**
1544  * Function called by the GNUNET_TRANSPORT_TransmitFunction
1545  * upon "completion" of a send request.  This tells the API
1546  * that it is now legal to send another message to the given
1547  * peer.
1548  *
1549  * @param cls closure, identifies the entry on the
1550  *            message queue that was transmitted and the
1551  *            client responsible for queuing the message
1552  * @param target the peer receiving the message
1553  * @param result GNUNET_OK on success, if the transmission
1554  *           failed, we should not tell the client to transmit
1555  *           more messages
1556  */
1557 static void
1558 transmit_send_continuation (void *cls,
1559                             const struct GNUNET_PeerIdentity *target,
1560                             int result)
1561 {
1562   struct MessageQueue *mq = cls;
1563   struct NeighbourList *n;
1564
1565   GNUNET_STATISTICS_update (stats,
1566                             gettext_noop ("# bytes pending with plugins"),
1567                             - (int64_t) mq->message_buf_size,
1568                             GNUNET_NO);
1569   if (result == GNUNET_OK)
1570     {
1571       GNUNET_STATISTICS_update (stats,
1572                                 gettext_noop ("# bytes successfully transmitted by plugins"),
1573                                 mq->message_buf_size,
1574                                 GNUNET_NO);
1575     }
1576   else
1577     {
1578       GNUNET_STATISTICS_update (stats,
1579                                 gettext_noop ("# bytes with transmission failure by plugins"),
1580                                 mq->message_buf_size,
1581                                 GNUNET_NO);
1582     }
1583   if (mq->specific_address != NULL)
1584     {
1585       if (result == GNUNET_OK)
1586         {
1587           mq->specific_address->timeout =
1588             GNUNET_TIME_relative_to_absolute
1589             (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1590           if (mq->specific_address->validated == GNUNET_YES)
1591             mark_address_connected (mq->specific_address);
1592         }
1593       else
1594         {
1595           if (mq->specific_address->connected != GNUNET_NO)
1596             {
1597 #if DEBUG_TRANSPORT
1598               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599                           "Marking address `%s' as no longer connected (due to transmission problem)\n",
1600                           a2s (mq->specific_address->ready_list->plugin->short_name,
1601                                mq->specific_address->addr,
1602                                mq->specific_address->addrlen));
1603 #endif
1604               GNUNET_STATISTICS_update (stats,
1605                                         gettext_noop ("# connected addresses"),
1606                                         -1,
1607                                         GNUNET_NO);
1608               mq->specific_address->connected = GNUNET_NO;
1609             }
1610         }
1611       if (! mq->internal_msg)
1612         mq->specific_address->in_transmit = GNUNET_NO;
1613     }
1614   n = find_neighbour (&mq->neighbour_id);
1615   if (mq->client != NULL)
1616     transmit_send_ok (mq->client, n, target, result);
1617   if (n != NULL)
1618     {
1619       GNUNET_CONTAINER_DLL_remove (n->cont_head,
1620                                    n->cont_tail,
1621                                    mq);
1622     }
1623   GNUNET_free (mq);
1624   if (n != NULL) 
1625     {
1626       if (result == GNUNET_OK) 
1627         try_transmission_to_peer (n);
1628       else if (GNUNET_SCHEDULER_NO_TASK == n->retry_task)
1629         n->retry_task = GNUNET_SCHEDULER_add_now (&retry_transmission_task,
1630                                                   n);
1631     }   
1632 }
1633
1634
1635 /**
1636  * Check the ready list for the given neighbour and if a plugin is
1637  * ready for transmission (and if we have a message), do so!
1638  *
1639  * @param neighbour target peer for which to transmit
1640  */
1641 static void
1642 try_transmission_to_peer (struct NeighbourList *n)
1643 {
1644   struct ReadyList *rl;
1645   struct MessageQueue *mq;
1646   struct GNUNET_TIME_Relative timeout;
1647   ssize_t ret;
1648   int force_address;
1649
1650   if (n->messages_head == NULL)
1651     {
1652 #if DEBUG_TRANSPORT
1653       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1654                   "Transmission queue for `%4s' is empty\n",
1655                   GNUNET_i2s (&n->id));
1656 #endif
1657       return;                     /* nothing to do */
1658     }
1659   rl = NULL;
1660   mq = n->messages_head;
1661   force_address = GNUNET_YES;
1662   if (mq->specific_address == NULL)
1663     {
1664       /* TODO: ADD ATS */
1665       mq->specific_address = get_preferred_ats_address(n);
1666       GNUNET_STATISTICS_update (stats,
1667                                 gettext_noop ("# transport selected peer address freely"),
1668                                 1,
1669                                 GNUNET_NO);
1670       force_address = GNUNET_NO;
1671     }
1672   if (mq->specific_address == NULL)
1673     {
1674       GNUNET_STATISTICS_update (stats,
1675                                 gettext_noop ("# transport failed to selected peer address"),
1676                                 1,
1677                                 GNUNET_NO);
1678       timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1679       if (timeout.rel_value == 0)
1680         {
1681 #if DEBUG_TRANSPORT
1682           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1683                       "No destination address available to transmit message of size %u to peer `%4s'\n",
1684                       mq->message_buf_size,
1685                       GNUNET_i2s (&mq->neighbour_id));
1686 #endif
1687           GNUNET_STATISTICS_update (stats,
1688                                     gettext_noop ("# bytes in message queue for other peers"),
1689                                     - (int64_t) mq->message_buf_size,
1690                                     GNUNET_NO);
1691           GNUNET_STATISTICS_update (stats,
1692                                     gettext_noop ("# bytes discarded (no destination address available)"),
1693                                     mq->message_buf_size,
1694                                     GNUNET_NO);
1695           if (mq->client != NULL)
1696             transmit_send_ok (mq->client, n, &n->id, GNUNET_NO);
1697           GNUNET_CONTAINER_DLL_remove (n->messages_head,
1698                                        n->messages_tail,
1699                                        mq);
1700           GNUNET_free (mq);
1701           return;               /* nobody ready */
1702         }
1703       GNUNET_STATISTICS_update (stats,
1704                                 gettext_noop ("# message delivery deferred (no address)"),
1705                                 1,
1706                                 GNUNET_NO);
1707       if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
1708         GNUNET_SCHEDULER_cancel (n->retry_task);
1709       n->retry_task = GNUNET_SCHEDULER_add_delayed (timeout,
1710                                                     &retry_transmission_task,
1711                                                     n);
1712 #if DEBUG_TRANSPORT
1713       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714                   "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1715                   mq->message_buf_size,
1716                   GNUNET_i2s (&mq->neighbour_id),
1717                   timeout.rel_value);
1718 #endif
1719       /* FIXME: might want to trigger peerinfo lookup here
1720          (unless that's already pending...) */
1721       return;
1722     }
1723   GNUNET_CONTAINER_DLL_remove (n->messages_head,
1724                                n->messages_tail,
1725                                mq);
1726   if (mq->specific_address->connected == GNUNET_NO)
1727     mq->specific_address->connect_attempts++;
1728   rl = mq->specific_address->ready_list;
1729   mq->plugin = rl->plugin;
1730   if (!mq->internal_msg)
1731     mq->specific_address->in_transmit = GNUNET_YES;
1732 #if DEBUG_TRANSPORT
1733   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734               "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1735               mq->message_buf_size,
1736               GNUNET_i2s (&n->id),
1737               (mq->specific_address->addr != NULL)
1738               ? a2s (mq->plugin->short_name,
1739                      mq->specific_address->addr,
1740                      mq->specific_address->addrlen)
1741               : "<inbound>",
1742               rl->plugin->short_name);
1743 #endif
1744   GNUNET_STATISTICS_update (stats,
1745                             gettext_noop ("# bytes in message queue for other peers"),
1746                             - (int64_t) mq->message_buf_size,
1747                             GNUNET_NO);
1748   GNUNET_STATISTICS_update (stats,
1749                             gettext_noop ("# bytes pending with plugins"),
1750                             mq->message_buf_size,
1751                             GNUNET_NO);
1752
1753   GNUNET_CONTAINER_DLL_insert (n->cont_head,
1754                                n->cont_tail,
1755                                mq);
1756
1757   ret = rl->plugin->api->send (rl->plugin->api->cls,
1758                                &mq->neighbour_id,
1759                                mq->message_buf,
1760                                mq->message_buf_size,
1761                                mq->priority,
1762                                GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1763                                mq->specific_address->session,
1764                                mq->specific_address->addr,
1765                                mq->specific_address->addrlen,
1766                                force_address,
1767                                &transmit_send_continuation, mq);
1768   if (ret == -1)
1769     {
1770       /* failure, but 'send' would not call continuation in this case,
1771          so we need to do it here! */
1772       transmit_send_continuation (mq,
1773                                   &mq->neighbour_id,
1774                                   GNUNET_SYSERR);
1775     }
1776 }
1777
1778
1779 /**
1780  * Send the specified message to the specified peer.
1781  *
1782  * @param client source of the transmission request (can be NULL)
1783  * @param peer_address ForeignAddressList where we should send this message
1784  * @param priority how important is the message
1785  * @param timeout how long do we have to transmit?
1786  * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1787  * @param message_buf_size total size of all messages in message_buf
1788  * @param is_internal is this an internal message; these are pre-pended and
1789  *                    also do not count for plugins being "ready" to transmit
1790  * @param neighbour handle to the neighbour for transmission
1791  */
1792 static void
1793 transmit_to_peer (struct TransportClient *client,
1794                   struct ForeignAddressList *peer_address,
1795                   unsigned int priority,
1796                   struct GNUNET_TIME_Relative timeout,
1797                   const char *message_buf,
1798                   size_t message_buf_size,
1799                   int is_internal, struct NeighbourList *neighbour)
1800 {
1801   struct MessageQueue *mq;
1802
1803 #if EXTRA_CHECKS
1804   if (client != NULL)
1805     {
1806       /* check for duplicate submission */
1807       mq = neighbour->messages_head;
1808       while (NULL != mq)
1809         {
1810           if (mq->client == client)
1811             {
1812               /* client transmitted to same peer twice
1813                  before getting SEND_OK! */
1814               GNUNET_break (0);
1815               return;
1816             }
1817           mq = mq->next;
1818         }
1819     }
1820 #endif
1821   GNUNET_STATISTICS_update (stats,
1822                             gettext_noop ("# bytes in message queue for other peers"),
1823                             message_buf_size,
1824                             GNUNET_NO);
1825   mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1826   mq->specific_address = peer_address;
1827   mq->client = client;
1828   /* FIXME: this memcpy can be up to 7% of our total runtime! */
1829   memcpy (&mq[1], message_buf, message_buf_size);
1830   mq->message_buf = (const char*) &mq[1];
1831   mq->message_buf_size = message_buf_size;
1832   memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1833   mq->internal_msg = is_internal;
1834   mq->priority = priority;
1835   mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1836   if (is_internal)
1837     GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1838                                  neighbour->messages_tail,
1839                                  mq);
1840   else
1841     GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1842                                        neighbour->messages_tail,
1843                                        neighbour->messages_tail,
1844                                        mq);
1845   try_transmission_to_peer (neighbour);
1846 }
1847
1848
1849 /**
1850  * Send a plain PING (without address or our HELLO) to the given
1851  * foreign address to try to establish a connection (and validate
1852  * that the other peer is really who he claimed he is).
1853  *
1854  * @param n neighbour to PING
1855  */
1856 static void
1857 transmit_plain_ping (struct NeighbourList *n)
1858 {
1859   struct ValidationEntry *ve;
1860   struct TransportPingMessage ping;
1861   struct ReadyList *rl;
1862   struct TransportPlugin *plugin;
1863   struct ForeignAddressList *fal;
1864
1865   if (! n->public_key_valid)
1866     {
1867       /* This should not happen since the other peer
1868          should send us a HELLO prior to sending his
1869          PING */
1870       GNUNET_break_op (0);
1871       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1872                   "Could not transmit plain PING to `%s': public key not known\n",
1873                   GNUNET_i2s (&n->id));
1874       return;
1875     }
1876   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877               "Looking for addresses to transmit plain PING to `%s'\n",
1878               GNUNET_i2s (&n->id));
1879   for (rl = n->plugins; rl != NULL; rl = rl->next)
1880     {
1881       plugin = rl->plugin;
1882       for (fal = rl->addresses; fal != NULL; fal = fal->next)
1883         {
1884           if (! fal->connected)
1885             continue;      
1886           ve = GNUNET_malloc (sizeof (struct ValidationEntry));
1887           ve->transport_name = GNUNET_strdup (plugin->short_name);
1888           ve->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1889                                                     UINT_MAX);
1890           ve->send_time = GNUNET_TIME_absolute_get();
1891           ve->session = fal->session;
1892           memcpy(&ve->publicKey,
1893                  &n->publicKey,
1894                  sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1895           ve->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
1896                                                            &timeout_hello_validation,
1897                                                            ve);
1898           GNUNET_CONTAINER_multihashmap_put (validation_map,
1899                                              &n->id.hashPubKey,
1900                                              ve,
1901                                              GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1902           ping.header.size = htons(sizeof(struct TransportPingMessage));
1903           ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
1904           ping.challenge = htonl(ve->challenge);
1905           memcpy(&ping.target, &n->id, sizeof(struct GNUNET_PeerIdentity));
1906           GNUNET_STATISTICS_update (stats,
1907                                     gettext_noop ("# PING without HELLO messages sent"),
1908                                     1,
1909                                     GNUNET_NO);
1910           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1911                       "Transmitting plain PING to `%s'\n",
1912                       GNUNET_i2s (&n->id));
1913           transmit_to_peer (NULL, 
1914                             fal,
1915                             GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1916                             HELLO_VERIFICATION_TIMEOUT,
1917                             (const char*) &ping, sizeof (ping),
1918                             GNUNET_YES, n);
1919         }
1920     }
1921 }
1922
1923
1924 /**
1925  * Mark the given FAL entry as 'connected' (and hence preferred for
1926  * sending); also mark all others for the same peer as 'not connected'
1927  * (since only one can be preferred).
1928  *
1929  * @param fal address to set to 'connected'
1930  */
1931 static void
1932 mark_address_connected(struct ForeignAddressList *fal)
1933 {
1934   struct ForeignAddressList *pos;
1935   struct ForeignAddressList *inbound;
1936   struct ForeignAddressList *outbound;
1937   int cnt;
1938
1939   GNUNET_assert (GNUNET_YES == fal->validated);
1940   if (fal->connected == GNUNET_YES)
1941     return; /* nothing to do */
1942   cnt = GNUNET_YES;
1943   inbound = NULL;
1944   outbound = NULL;
1945
1946   pos = fal->ready_list->addresses;
1947   while (pos != NULL)
1948     {
1949       /* Already have inbound address, and this is also an inbound address, don't switch!! */
1950       if ((GNUNET_YES == pos->connected) && (0 == pos->addrlen) && (0
1951           == fal->addrlen))
1952         return;
1953       else if ((0 == pos->addrlen) && (GNUNET_YES == pos->connected))
1954         inbound = pos;
1955       pos = pos->next;
1956     }
1957
1958   pos = fal->ready_list->addresses;
1959   while (pos != NULL)
1960     {
1961       /* Already have outbound address, and this is also an outbound address, don't switch!! */
1962       if ((GNUNET_YES == pos->connected) && (0 < pos->addrlen) && (0
1963           < fal->addrlen))
1964         return;
1965       else if ((0 < pos->addrlen) && (GNUNET_YES == pos->connected))
1966         outbound = pos;
1967       pos = pos->next;
1968     }
1969
1970 #if DEBUG_INBOUND
1971   if (inbound != NULL)
1972     fprintf(stderr, "Peer: %s, have inbound connection.\n", GNUNET_i2s(&my_identity));
1973   if (outbound != NULL)
1974     fprintf(stderr, "Peer: %s, have outbound connection.\n", GNUNET_i2s(&my_identity));
1975 #endif
1976
1977   /* Have an inbound connection to this peer which is valid; our id is lower, ignore outbound connection! */
1978   if ((inbound != NULL) && (0 != fal->addrlen) && (1
1979       == GNUNET_CRYPTO_hash_xorcmp (&inbound->ready_list->neighbour->id.hashPubKey,
1980                                     &my_identity.hashPubKey, &null_hash)))
1981     {
1982 #if DEBUG_INBOUND
1983       fprintf(stderr, "Peer: %s, had inbound connection, ignoring outbound!\n", GNUNET_i2s(&my_identity));
1984 #endif
1985       return;
1986     }
1987   else if ((outbound != NULL) && (0 == fal->addrlen) && ((-1
1988       == GNUNET_CRYPTO_hash_xorcmp (&outbound->ready_list->neighbour->id.hashPubKey,
1989                                     &my_identity.hashPubKey, &null_hash))))
1990     {
1991 #if DEBUG_INBOUND
1992       fprintf(stderr, "Peer: %s, have outbound connection, ignoring inbound!\n", GNUNET_i2s(&my_identity));
1993 #endif
1994       return;
1995     }
1996
1997   pos = fal->ready_list->addresses;
1998   while (pos != NULL)
1999     {
2000       if ((GNUNET_YES == pos->connected) && (0 < pos->addrlen))
2001         {
2002 #if DEBUG_TRANSPORT
2003           GNUNET_log (
2004                       GNUNET_ERROR_TYPE_DEBUG,
2005                       "Marking address `%s' as no longer connected (due to connect on other address)\n",
2006                       a2s (pos->ready_list->plugin->short_name, pos->addr,
2007                            pos->addrlen));
2008 #endif
2009           GNUNET_break (cnt == GNUNET_YES);
2010           cnt = GNUNET_NO;
2011 #if DEBUG_INBOUND
2012           fprintf(stderr, "Peer: %s, setting %s connection to disconnected.\n", GNUNET_i2s(&my_identity), (0 == pos->addrlen) ? "INBOUND" : "OUTBOUND");
2013 #endif
2014           pos->connected = GNUNET_NO;
2015           GNUNET_STATISTICS_update (stats,
2016                                     gettext_noop ("# connected addresses"), -1,
2017                                     GNUNET_NO);
2018         }
2019       pos = pos->next;
2020     }
2021
2022   fal->connected = GNUNET_YES;
2023   if (GNUNET_YES == cnt)
2024     {
2025       GNUNET_STATISTICS_update (stats, gettext_noop ("# connected addresses"),
2026                                 1, GNUNET_NO);
2027     }
2028 }
2029
2030
2031 /**
2032  * Find an address in any of the available transports for
2033  * the given neighbour that would be good for message
2034  * transmission.  This is essentially the transport selection
2035  * routine.
2036  *
2037  * @param neighbour for whom to select an address
2038  * @return selected address, NULL if we have none
2039  */
2040 struct ForeignAddressList *
2041 find_ready_address(struct NeighbourList *neighbour)
2042 {
2043   struct ReadyList *head = neighbour->plugins;
2044   struct ForeignAddressList *addresses;
2045   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
2046   struct ForeignAddressList *best_address;
2047
2048   /* Hack to prefer unix domain sockets */
2049   struct ForeignAddressList *unix_address = NULL;
2050
2051   best_address = NULL;
2052   while (head != NULL)
2053     {
2054       addresses = head->addresses;
2055       while (addresses != NULL)
2056         {
2057           if ( (addresses->timeout.abs_value < now.abs_value) &&
2058                (addresses->connected == GNUNET_YES) )
2059             {
2060 #if DEBUG_TRANSPORT
2061               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2062                           "Marking long-time inactive connection to `%4s' as down.\n",
2063                           GNUNET_i2s (&neighbour->id));
2064 #endif
2065               GNUNET_STATISTICS_update (stats,
2066                                         gettext_noop ("# connected addresses"),
2067                                         -1,
2068                                         GNUNET_NO);
2069               addresses->connected = GNUNET_NO;
2070             }
2071           addresses = addresses->next;
2072         }
2073
2074       addresses = head->addresses;
2075       while (addresses != NULL)
2076         {
2077 #if DEBUG_TRANSPORT
2078           if (addresses->addr != NULL)
2079             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2080                         "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
2081                         a2s (head->plugin->short_name,
2082                              addresses->addr,
2083                              addresses->addrlen),
2084                         GNUNET_i2s (&neighbour->id),
2085                         addresses->connected,
2086                         addresses->in_transmit,
2087                         addresses->validated,
2088                         addresses->connect_attempts,
2089                         (unsigned long long) addresses->timeout.abs_value,
2090                         (unsigned int) addresses->distance);
2091 #endif
2092           if (0==strcmp(head->plugin->short_name,"unix"))
2093             {
2094               if ( (unix_address == NULL) || 
2095                    ( (unix_address != NULL) &&
2096                      (addresses->latency.rel_value < unix_address->latency.rel_value) ) )
2097                 unix_address = addresses;
2098             }
2099           if ( ( (best_address == NULL) ||
2100                  (addresses->connected == GNUNET_YES) ||
2101                  (best_address->connected == GNUNET_NO) ) &&
2102                (addresses->in_transmit == GNUNET_NO) &&
2103                ( (best_address == NULL) ||
2104                  (addresses->latency.rel_value < best_address->latency.rel_value)) )
2105             best_address = addresses;
2106           /* FIXME: also give lower-latency addresses that are not
2107              connected a chance some times... */
2108           addresses = addresses->next;
2109         }
2110       if (unix_address != NULL)
2111           break;
2112       head = head->next;
2113     }
2114   if (unix_address != NULL)
2115     {
2116       best_address = unix_address;
2117 #if DEBUG_TRANSPORT
2118       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
2119                   "Found UNIX address, forced this address\n");
2120 #endif
2121     }
2122   if (best_address != NULL)
2123     {
2124 #if DEBUG_TRANSPORT
2125       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2126                   "Best address found (`%s') has latency of %llu ms.\n",
2127                   (best_address->addrlen > 0)
2128                   ? a2s (best_address->ready_list->plugin->short_name,
2129                          best_address->addr,
2130                        best_address->addrlen)
2131                   : "<inbound>",
2132                   best_address->latency.rel_value);
2133 #endif
2134     }
2135   else
2136     {
2137       GNUNET_STATISTICS_update (stats,
2138                                 gettext_noop ("# transmission attempts failed (no address)"),
2139                                 1,
2140                                 GNUNET_NO);
2141     }
2142
2143   return best_address;
2144 }
2145
2146
2147 /**
2148  * FIXME: document.
2149  */
2150 struct GeneratorContext
2151 {
2152   struct TransportPlugin *plug_pos;
2153   struct OwnAddressList *addr_pos;
2154   struct GNUNET_TIME_Absolute expiration;
2155 };
2156
2157
2158 /**
2159  * FIXME: document.
2160  */
2161 static size_t
2162 address_generator (void *cls, size_t max, void *buf)
2163 {
2164   struct GeneratorContext *gc = cls;
2165   size_t ret;
2166
2167   while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
2168     {
2169       gc->plug_pos = gc->plug_pos->next;
2170       gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
2171     }
2172   if (NULL == gc->plug_pos)
2173     {
2174
2175       return 0;
2176     }
2177   ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
2178                                   gc->expiration,
2179                                   &gc->addr_pos[1],
2180                                   gc->addr_pos->addrlen, buf, max);
2181   gc->addr_pos = gc->addr_pos->next;
2182   return ret;
2183 }
2184
2185
2186 /**
2187  * Construct our HELLO message from all of the addresses of
2188  * all of the transports.
2189  *
2190  * @param cls unused
2191  * @param tc scheduler context
2192  */
2193 static void
2194 refresh_hello_task (void *cls,
2195                     const struct GNUNET_SCHEDULER_TaskContext *tc)
2196 {
2197   struct GNUNET_HELLO_Message *hello;
2198   struct TransportClient *cpos;
2199   struct NeighbourList *npos;
2200   struct GeneratorContext gc;
2201
2202   hello_task = GNUNET_SCHEDULER_NO_TASK;
2203   gc.plug_pos = plugins;
2204   gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
2205   gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
2206   hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
2207 #if DEBUG_TRANSPORT
2208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2209               "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
2210 #endif
2211   GNUNET_STATISTICS_update (stats,
2212                             gettext_noop ("# refreshed my HELLO"),
2213                             1,
2214                             GNUNET_NO);
2215   cpos = clients;
2216   while (cpos != NULL)
2217     {
2218       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219                   "Transmitting my HELLO to client!\n");
2220       transmit_to_client (cpos,
2221                           (const struct GNUNET_MessageHeader *) hello,
2222                           GNUNET_NO);
2223       cpos = cpos->next;
2224     }
2225
2226   GNUNET_free_non_null (our_hello);
2227   our_hello = hello;
2228   GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
2229   for (npos = neighbours; npos != NULL; npos = npos->next)
2230     {
2231       if (! npos->received_pong)
2232         continue;
2233 #if DEBUG_TRANSPORT
2234       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2235                   "Transmitting updated `%s' to neighbour `%4s'\n",
2236                   "HELLO", GNUNET_i2s (&npos->id));
2237 #endif
2238       GNUNET_STATISTICS_update (stats,
2239                                 gettext_noop ("# transmitted my HELLO to other peers"),
2240                                 1,
2241                                 GNUNET_NO);
2242       transmit_to_peer (NULL, NULL, 0,
2243                         HELLO_ADDRESS_EXPIRATION,
2244                         (const char *) our_hello,
2245                         GNUNET_HELLO_size(our_hello),
2246                         GNUNET_NO, npos);
2247     }
2248 }
2249
2250
2251 /**
2252  * Schedule task to refresh hello (unless such a
2253  * task exists already).
2254  */
2255 static void
2256 refresh_hello ()
2257 {
2258 #if DEBUG_TRANSPORT_HELLO
2259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2260               "refresh_hello() called!\n");
2261 #endif
2262   if (hello_task != GNUNET_SCHEDULER_NO_TASK)
2263     return;
2264   hello_task
2265     = GNUNET_SCHEDULER_add_now (&refresh_hello_task,
2266                                 NULL);
2267 }
2268
2269
2270 /**
2271  * Iterator over hash map entries that NULLs the session of validation
2272  * entries that match the given session.
2273  *
2274  * @param cls closure (the 'struct Session*' to match against)
2275  * @param key current key code (peer ID, not used)
2276  * @param value value in the hash map ('struct ValidationEntry*')
2277  * @return GNUNET_YES (we should continue to iterate)
2278  */
2279 static int
2280 remove_session_validations (void *cls,
2281                             const GNUNET_HashCode * key,
2282                             void *value)
2283 {
2284   struct Session *session = cls;
2285   struct ValidationEntry *ve = value;
2286
2287   if (session == ve->session)
2288     ve->session = NULL;
2289   return GNUNET_YES;
2290 }
2291
2292
2293 /**
2294  * We've been disconnected from the other peer (for some
2295  * connection-oriented transport).  Either quickly
2296  * re-establish the connection or signal the disconnect
2297  * to the CORE.
2298  *
2299  * Only signal CORE level disconnect if ALL addresses
2300  * for the peer are exhausted.
2301  *
2302  * @param p overall plugin context
2303  * @param nl neighbour that was disconnected
2304  */
2305 static void
2306 try_fast_reconnect (struct TransportPlugin *p,
2307                     struct NeighbourList *nl)
2308 {
2309   /* FIXME-MW: fast reconnect / transport switching not implemented... */
2310   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2311               "try_fast_reconnect not implemented!\n");
2312   /* Note: the idea here is to hide problems with transports (or
2313      switching between plugins) from the core to eliminate the need to
2314      re-negotiate session keys and the like; OTOH, we should tell core
2315      quickly (much faster than timeout) `if a connection was lost and
2316      could not be re-established (i.e. other peer went down or is
2317      unable / refuses to communicate);
2318
2319      So we should consider:
2320      1) ideally: our own willingness / need to connect
2321      2) prior failures to connect to this peer (by plugin)
2322      3) ideally: reasons why other peer terminated (as far as knowable)
2323
2324      Most importantly, it must be POSSIBLE for another peer to terminate
2325      a connection for a while (without us instantly re-establishing it).
2326      Similarly, if another peer is gone we should quickly notify CORE.
2327      OTOH, if there was a minor glitch (i.e. crash of gnunet-service-transport
2328      on the other end), we should reconnect in such a way that BOTH CORE
2329      services never even notice.
2330      Furthermore, the same mechanism (or small variation) could be used
2331      to switch to a better-performing plugin (ATS).
2332
2333      Finally, this needs to be tested throughly... */
2334
2335   /*
2336    * GNUNET_NO in the call below makes transport disconnect the peer,
2337    * even if only a single address (out of say, six) went away.  This
2338    * function must be careful to ONLY disconnect if the peer is gone,
2339    * not just a specific address.
2340    *
2341    * More specifically, half the places it was used had it WRONG.
2342    */
2343
2344   /* No reconnect, signal disconnect instead! */
2345 #if DEBUG_TRANSPORT
2346 #endif
2347   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348             "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&nl->id),
2349             "try_fast_reconnect");
2350
2351   GNUNET_STATISTICS_update (stats,
2352                             gettext_noop ("# disconnects due to try_fast_reconnect"),
2353                             1,
2354                             GNUNET_NO);
2355 #if DISCONNECT || 1
2356   disconnect_neighbour (nl, GNUNET_YES);
2357 #endif
2358 }
2359
2360
2361 /**
2362  * Function that will be called whenever the plugin internally
2363  * cleans up a session pointer and hence the service needs to
2364  * discard all of those sessions as well.  Plugins that do not
2365  * use sessions can simply omit calling this function and always
2366  * use NULL wherever a session pointer is needed.
2367  *
2368  * @param cls closure
2369  * @param peer which peer was the session for
2370  * @param session which session is being destoyed
2371  */
2372 static void
2373 plugin_env_session_end  (void *cls,
2374                          const struct GNUNET_PeerIdentity *peer,
2375                          struct Session *session)
2376 {
2377   struct TransportPlugin *p = cls;
2378   struct NeighbourList *nl;
2379   struct ReadyList *rl;
2380   struct ForeignAddressList *pos;
2381   struct ForeignAddressList *prev;
2382
2383 #if DEBUG_TRANSPORT
2384   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385               "Session ended with peer `%4s', %s\n", 
2386               GNUNET_i2s(peer),
2387               "plugin_env_session_end");
2388 #endif
2389   GNUNET_CONTAINER_multihashmap_iterate (validation_map,
2390                                          &remove_session_validations,
2391                                          session);
2392   nl = find_neighbour (peer);
2393   if (nl == NULL)
2394     {
2395 #if DEBUG_TRANSPORT
2396       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2397                   "No neighbour record found for peer `%4s'\n", 
2398                   GNUNET_i2s(peer));
2399 #endif
2400       return; /* was never marked as connected */
2401     }
2402   rl = nl->plugins;
2403   while (rl != NULL)
2404     {
2405       if (rl->plugin == p)
2406         break;
2407       rl = rl->next;
2408     }
2409   if (rl == NULL)
2410     {
2411 #if DEBUG_TRANSPORT
2412       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413                   "Plugin was associated with peer `%4s'\n", 
2414                   GNUNET_i2s(peer));
2415 #endif
2416       GNUNET_STATISTICS_update (stats,
2417                                 gettext_noop ("# disconnects due to session end"),
2418                                 1,
2419                                 GNUNET_NO);
2420       disconnect_neighbour (nl, GNUNET_YES);
2421       return;
2422     }
2423   prev = NULL;
2424   pos = rl->addresses;
2425   while ( (pos != NULL) &&
2426           (pos->session != session) )
2427     {
2428       prev = pos;
2429       pos = pos->next;
2430     }
2431   if (pos == NULL)
2432     {
2433 #if DEBUG_TRANSPORT
2434       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2435                   "Session was never marked as ready for peer `%4s'\n", 
2436                   GNUNET_i2s(peer));
2437 #endif
2438
2439       int validations_pending = GNUNET_CONTAINER_multihashmap_contains (validation_map, &peer->hashPubKey);
2440
2441       /* No session was marked as ready, but we have pending validations so do not disconnect from neighbour */
2442       if (validations_pending ==GNUNET_YES)
2443         {
2444 #if DEBUG_TRANSPORT
2445       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446                   "Not disconnecting from peer `%4s due to pending address validations\n", GNUNET_i2s(peer));
2447 #endif
2448         return;
2449         }
2450
2451       //FIXME: This conflicts with inbound tcp connections and tcp nat ... debugging in progress
2452       GNUNET_STATISTICS_update (stats,
2453                                 gettext_noop ("# disconnects due to unready session"),
2454                                 1,
2455                                 GNUNET_NO);
2456
2457       disconnect_neighbour (nl, GNUNET_YES);
2458       return; /* was never marked as connected */
2459     }
2460   pos->session = NULL;
2461   pos->connected = GNUNET_NO;
2462   if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2463     {
2464       GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2465       pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2466     }
2467
2468   if (pos->addrlen != 0)
2469     {
2470       if (nl->received_pong != GNUNET_NO)
2471         {
2472           GNUNET_STATISTICS_update (stats,
2473                                     gettext_noop ("# try_fast_reconnect thanks to plugin_env_session_end"),
2474                                     1,
2475                                     GNUNET_NO);
2476           if (GNUNET_YES == pos->connected)                  
2477               try_fast_reconnect (p, nl);           
2478         }
2479       else
2480         {
2481           GNUNET_STATISTICS_update (stats,
2482                                     gettext_noop ("# disconnects due to missing pong"),
2483                                     1,
2484                                     GNUNET_NO);
2485           /* FIXME this is never true?! See: line 2416*/
2486           if (GNUNET_YES == pos->connected)
2487             disconnect_neighbour (nl, GNUNET_YES);
2488         }
2489       return;
2490     }
2491
2492   GNUNET_STATISTICS_update (stats,
2493                               gettext_noop ("# connected addresses"),
2494                               -1,
2495                               GNUNET_NO);
2496
2497   /* was inbound connection, free 'pos' */
2498   if (prev == NULL)
2499     rl->addresses = pos->next;
2500   else
2501     prev->next = pos->next;
2502   if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2503     {
2504       GNUNET_SCHEDULER_cancel (pos->revalidate_task);
2505       pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2506     }
2507   GNUNET_free_non_null(pos->ressources);
2508   GNUNET_free_non_null(pos->quality);
2509   ats_modify_problem_state (ats, ATS_MODIFIED);
2510
2511   if (GNUNET_YES != pos->connected)
2512     {
2513       /* nothing else to do, connection was never up... */
2514       GNUNET_free (pos);
2515       return; 
2516     }
2517   GNUNET_free (pos);
2518
2519   if (nl->received_pong == GNUNET_NO)
2520     {
2521       GNUNET_STATISTICS_update (stats,
2522                                 gettext_noop ("# disconnects due to NO pong"),
2523                                 1,
2524                                 GNUNET_NO);
2525       disconnect_neighbour (nl, GNUNET_YES);
2526       return; /* nothing to do, never connected... */
2527     }
2528   /* check if we have any validated addresses left */
2529   pos = rl->addresses;
2530   while (pos != NULL)
2531     {
2532       if (GNUNET_YES == pos->validated)
2533         {
2534           GNUNET_STATISTICS_update (stats,
2535                                     gettext_noop ("# try_fast_reconnect thanks to validated_address"),
2536                                     1,
2537                                     GNUNET_NO);
2538           try_fast_reconnect (p, nl);
2539           return;
2540         }
2541       pos = pos->next;
2542     }
2543   /* no valid addresses left, signal disconnect! */
2544
2545 #if DEBUG_TRANSPORT
2546   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2547               "Disconnecting peer `%4s', %s\n", 
2548               GNUNET_i2s(peer),
2549               "plugin_env_session_end");
2550 #endif
2551   /* FIXME: This doesn't mean there are no addresses left for this PEER,
2552    * it means there aren't any left for this PLUGIN/PEER combination! So
2553    * calling disconnect_neighbour here with GNUNET_NO forces disconnect
2554    * when it isn't necessary. Using GNUNET_YES at least checks to see
2555    * if there are any addresses that work first, so as not to overdo it.
2556    * --NE
2557    */
2558   GNUNET_STATISTICS_update (stats,
2559                             gettext_noop ("# disconnects due to plugin_env_session_end"),
2560                             1,
2561                             GNUNET_NO);
2562   disconnect_neighbour (nl, GNUNET_YES);
2563 }
2564
2565
2566 /**
2567  * Function that must be called by each plugin to notify the
2568  * transport service about the addresses under which the transport
2569  * provided by the plugin can be reached.
2570  *
2571  * @param cls closure
2572  * @param add_remove GNUNET_YES to add, GNUNET_NO to remove the address
2573  * @param addr one of the addresses of the host, NULL for the last address
2574  *        the specific address format depends on the transport
2575  * @param addrlen length of the address
2576  */
2577 static void
2578 plugin_env_notify_address (void *cls,
2579                            int add_remove,
2580                            const void *addr,
2581                            size_t addrlen)
2582 {
2583   struct TransportPlugin *p = cls;
2584   struct OwnAddressList *al;
2585   struct OwnAddressList *prev;
2586
2587   GNUNET_assert (p->api != NULL);
2588 #if DEBUG_TRANSPORT
2589   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2590               (add_remove == GNUNET_YES)
2591               ? "Adding `%s':%s to the set of our addresses\n"
2592               : "Removing `%s':%s from the set of our addresses\n",
2593               a2s (p->short_name,
2594                    addr, addrlen),
2595               p->short_name);
2596 #endif
2597   GNUNET_assert (addr != NULL);
2598   if (GNUNET_NO == add_remove)
2599     {
2600       prev = NULL;
2601       al = p->addresses;
2602       while (al != NULL)
2603         {
2604           if ( (addrlen == al->addrlen) &&
2605                (0 == memcmp (addr, &al[1], addrlen)) )
2606             {
2607               if (prev == NULL)
2608                 p->addresses = al->next;
2609               else
2610                 prev->next = al->next;
2611               GNUNET_free (al);
2612               refresh_hello ();
2613               return;
2614             }
2615           prev = al;
2616           al = al->next;
2617         }
2618       GNUNET_break (0);
2619       return;
2620     }
2621   al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2622   al->next = p->addresses;
2623   p->addresses = al;
2624   al->addrlen = addrlen;
2625   memcpy (&al[1], addr, addrlen);
2626   refresh_hello ();
2627 }
2628
2629
2630 /**
2631  * Notify all of our clients about a peer connecting.
2632  */
2633 static void
2634 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2635                         struct GNUNET_TIME_Relative latency,
2636                         uint32_t distance)
2637 {
2638   struct ConnectInfoMessage * cim;
2639   struct TransportClient *cpos;
2640   uint32_t ats_count;
2641   size_t size;
2642
2643   if (0 == memcmp (peer,
2644                    &my_identity,
2645                    sizeof (struct GNUNET_PeerIdentity)))
2646     {
2647       GNUNET_break (0);
2648       return;
2649     }
2650 #if DEBUG_TRANSPORT
2651   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2652               "Notifying clients about connection with `%s'\n",
2653               GNUNET_i2s (peer));
2654 #endif
2655   GNUNET_STATISTICS_update (stats,
2656                             gettext_noop ("# peers connected"),
2657                             1,
2658                             GNUNET_NO);
2659
2660   ats_count = 2;
2661   size  = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
2662   if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
2663     {
2664       GNUNET_break(0);
2665     }
2666   cim = GNUNET_malloc (size);
2667   cim->header.size = htons (size);
2668   cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2669   cim->ats_count = htonl(2);
2670   (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2671   (&(cim->ats))[0].value = htonl (distance);
2672   (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
2673   (&(cim->ats))[1].value = htonl ((uint32_t) latency.rel_value);
2674   (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2675   (&(cim->ats))[2].value = htonl (0);
2676   memcpy (&cim->id, peer, sizeof (struct GNUNET_PeerIdentity));
2677
2678   /* notify ats about connecting peer */
2679   /* notify ats about connecting peer */
2680   if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2681   {
2682     ats_modify_problem_state(ats, ATS_MODIFIED);
2683     ats_calculate_bandwidth_distribution (ats, stats);
2684   }
2685
2686
2687   cpos = clients;
2688   while (cpos != NULL)
2689     {
2690       transmit_to_client (cpos, &(cim->header), GNUNET_NO);
2691       cpos = cpos->next;
2692     }
2693
2694   GNUNET_free (cim);
2695 }
2696
2697
2698 /**
2699  * Notify all of our clients about a peer disconnecting.
2700  */
2701 static void
2702 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2703 {
2704   struct DisconnectInfoMessage dim;
2705   struct TransportClient *cpos;
2706
2707   if (0 == memcmp (peer,
2708                    &my_identity,
2709                    sizeof (struct GNUNET_PeerIdentity)))
2710     {
2711       GNUNET_break (0);
2712       return;
2713     }
2714 #if DEBUG_TRANSPORT
2715   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716               "Notifying clients about lost connection to `%s'\n",
2717               GNUNET_i2s (peer));
2718 #endif
2719   GNUNET_STATISTICS_update (stats,
2720                             gettext_noop ("# peers connected"),
2721                             -1,
2722                             GNUNET_NO);
2723   dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2724   dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2725   dim.reserved = htonl (0);
2726   memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2727
2728   /* notify ats about connecting peer */
2729   if ((ats != NULL) && (shutdown_in_progress == GNUNET_NO))
2730   {
2731     ats_modify_problem_state(ats, ATS_MODIFIED);
2732     ats_calculate_bandwidth_distribution (ats, stats);
2733   }
2734
2735   cpos = clients;
2736   while (cpos != NULL)
2737     {
2738       transmit_to_client (cpos, &dim.header, GNUNET_NO);
2739       cpos = cpos->next;
2740     }
2741 }
2742
2743
2744 /**
2745  * Find a ForeignAddressList entry for the given neighbour
2746  * that matches the given address and transport.
2747  *
2748  * @param neighbour which peer we care about
2749  * @param tname name of the transport plugin
2750  * @param session session to look for, NULL for 'any'; otherwise
2751  *        can be used for the service to "learn" this session ID
2752  *        if 'addr' matches
2753  * @param addr binary address
2754  * @param addrlen length of addr
2755  * @return NULL if no such entry exists
2756  */
2757 static struct ForeignAddressList *
2758 find_peer_address(struct NeighbourList *neighbour,
2759                   const char *tname,
2760                   struct Session *session,
2761                   const char *addr,
2762                   uint16_t addrlen)
2763 {
2764   struct ReadyList *head;
2765   struct ForeignAddressList *pos;
2766
2767   head = neighbour->plugins;
2768   while (head != NULL)
2769     {
2770       if (0 == strcmp (tname, head->plugin->short_name))
2771         break;
2772       head = head->next;
2773     }
2774   if (head == NULL)
2775     return NULL;
2776   pos = head->addresses;
2777   while ( (pos != NULL) &&
2778           ( (pos->addrlen != addrlen) ||
2779             (memcmp(pos->addr, addr, addrlen) != 0) ) )
2780     {
2781       if ( (session != NULL) &&
2782            (pos->session == session) )
2783         return pos;
2784       pos = pos->next;
2785     }
2786   if ( (session != NULL) && (pos != NULL) )
2787     pos->session = session; /* learn it! */
2788   return pos;
2789 }
2790
2791
2792 /**
2793  * Get the peer address struct for the given neighbour and
2794  * address.  If it doesn't yet exist, create it.
2795  *
2796  * @param neighbour which peer we care about
2797  * @param tname name of the transport plugin
2798  * @param session session of the plugin, or NULL for none
2799  * @param addr binary address
2800  * @param addrlen length of addr
2801  * @return NULL if we do not have a transport plugin for 'tname'
2802  */
2803 static struct ForeignAddressList *
2804 add_peer_address (struct NeighbourList *neighbour,
2805                   const char *tname,
2806                   struct Session *session,
2807                   const char *addr,
2808                   uint16_t addrlen)
2809 {
2810   struct ReadyList *head;
2811   struct ForeignAddressList *ret;
2812   int c;
2813
2814   ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2815   if (ret != NULL)
2816     return ret;
2817   head = neighbour->plugins;
2818
2819   while (head != NULL)
2820     {
2821       if (0 == strcmp (tname, head->plugin->short_name))
2822         break;
2823       head = head->next;
2824     }
2825   if (head == NULL)
2826     return NULL;
2827   ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2828   ret->session = session;
2829   if ((addrlen > 0) && (addr != NULL))
2830     {
2831       ret->addr = (const char*) &ret[1];
2832       memcpy (&ret[1], addr, addrlen);
2833     }
2834   else
2835     {
2836       ret->addr = NULL;
2837     }
2838
2839   ret->ressources = GNUNET_malloc(available_ressources * sizeof (struct ATS_ressource_entry));
2840   for (c=0; c<available_ressources; c++)
2841     {
2842       struct ATS_ressource_entry *r = ret->ressources;
2843       r[c].index = c;
2844       r[c].atis_index = ressources[c].atis_index;
2845       if (0 == strcmp(neighbour->plugins->plugin->short_name,"unix"))
2846         {
2847           r[c].c = ressources[c].c_unix;
2848         }
2849       else if (0 == strcmp(neighbour->plugins->plugin->short_name,"udp"))
2850         {
2851           r[c].c = ressources[c].c_udp;
2852         }
2853       else if (0 == strcmp(neighbour->plugins->plugin->short_name,"tcp"))
2854         {
2855           r[c].c = ressources[c].c_tcp;
2856         }
2857       else if (0 == strcmp(neighbour->plugins->plugin->short_name,"http"))
2858         {
2859           r[c].c = ressources[c].c_http;
2860         }
2861       else if (0 == strcmp(neighbour->plugins->plugin->short_name,"https"))
2862         {
2863           r[c].c = ressources[c].c_https;
2864         }
2865       else if (0 == strcmp(neighbour->plugins->plugin->short_name,"wlan"))
2866         {
2867           r[c].c = ressources[c].c_wlan;
2868         }
2869       else
2870         {
2871           r[c].c = ressources[c].c_default;
2872           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2873                       "Assigning default cost to peer `%s' addr plugin `%s'! This should not happen!\n",
2874                       GNUNET_i2s(&neighbour->id),
2875                       neighbour->plugins->plugin->short_name);
2876         }
2877     }
2878
2879   ret->quality = GNUNET_malloc (available_quality_metrics * sizeof (struct ATS_quality_entry));
2880   ret->addrlen = addrlen;
2881   ret->expires = GNUNET_TIME_relative_to_absolute
2882     (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2883   ret->latency = GNUNET_TIME_relative_get_forever();
2884   ret->distance = -1;
2885   ret->timeout = GNUNET_TIME_relative_to_absolute
2886     (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2887   ret->ready_list = head;
2888   ret->next = head->addresses;
2889   head->addresses = ret;
2890   return ret;
2891 }
2892
2893
2894 /**
2895  * Closure for 'add_validated_address'.
2896  */
2897 struct AddValidatedAddressContext
2898 {
2899   /**
2900    * Entry that has been validated.
2901    */
2902   const struct ValidationEntry *ve;
2903
2904   /**
2905    * Flag set after we have added the address so
2906    * that we terminate the iteration next time.
2907    */
2908   int done;
2909 };
2910
2911
2912 /**
2913  * Callback function used to fill a buffer of max bytes with a list of
2914  * addresses in the format used by HELLOs.  Should use
2915  * "GNUNET_HELLO_add_address" as a helper function.
2916  *
2917  * @param cls the 'struct AddValidatedAddressContext' with the validated address
2918  * @param max maximum number of bytes that can be written to buf
2919  * @param buf where to write the address information
2920  * @return number of bytes written, 0 to signal the
2921  *         end of the iteration.
2922  */
2923 static size_t
2924 add_validated_address (void *cls,
2925                        size_t max, void *buf)
2926 {
2927   struct AddValidatedAddressContext *avac = cls;
2928   const struct ValidationEntry *ve = avac->ve;
2929
2930   if (GNUNET_YES == avac->done)
2931     return 0;
2932   avac->done = GNUNET_YES;
2933   return GNUNET_HELLO_add_address (ve->transport_name,
2934                                    GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2935                                    ve->addr,
2936                                    ve->addrlen,
2937                                    buf,
2938                                    max);
2939 }
2940
2941
2942
2943 /**
2944  * Closure for 'check_address_exists'.
2945  */
2946 struct CheckAddressExistsClosure
2947 {
2948   /**
2949    * Address to check for.
2950    */
2951   const void *addr;
2952
2953   /**
2954    * Name of the transport.
2955    */
2956   const char *tname;
2957
2958   /**
2959    * Session, or NULL.
2960    */
2961   struct Session *session;
2962
2963   /**
2964    * Set to GNUNET_YES if the address exists.
2965    */
2966   int exists;
2967
2968   /**
2969    * Length of addr.
2970    */
2971   uint16_t addrlen;
2972
2973 };
2974
2975
2976 /**
2977  * Iterator over hash map entries.  Checks if the given
2978  * validation entry is for the same address as what is given
2979  * in the closure.
2980  *
2981  * @param cls the 'struct CheckAddressExistsClosure*'
2982  * @param key current key code (ignored)
2983  * @param value value in the hash map ('struct ValidationEntry')
2984  * @return GNUNET_YES if we should continue to
2985  *         iterate (mismatch), GNUNET_NO if not (entry matched)
2986  */
2987 static int
2988 check_address_exists (void *cls,
2989                       const GNUNET_HashCode * key,
2990                       void *value)
2991 {
2992   struct CheckAddressExistsClosure *caec = cls;
2993   struct ValidationEntry *ve = value;
2994
2995   if ( (0 == strcmp (caec->tname,
2996                      ve->transport_name)) &&
2997        (caec->addrlen == ve->addrlen) &&
2998        (0 == memcmp (caec->addr,
2999                      ve->addr,
3000                      caec->addrlen)) )
3001     {
3002       caec->exists = GNUNET_YES;
3003       return GNUNET_NO;
3004     }
3005   if ( (ve->session != NULL) &&
3006        (caec->session == ve->session) )
3007     {
3008       caec->exists = GNUNET_YES;
3009       return GNUNET_NO;
3010     }
3011   return GNUNET_YES;
3012 }
3013
3014
3015 static void
3016 neighbour_timeout_task (void *cls,
3017                        const struct GNUNET_SCHEDULER_TaskContext *tc)
3018 {
3019   struct NeighbourList *n = cls;
3020
3021 #if DEBUG_TRANSPORT
3022   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3023               "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
3024 #endif
3025   GNUNET_STATISTICS_update (stats,
3026                             gettext_noop ("# disconnects due to timeout"),
3027                             1,
3028                             GNUNET_NO);
3029   n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3030   disconnect_neighbour (n, GNUNET_NO);
3031 }
3032
3033
3034 /**
3035  * Schedule the job that will cause us to send a PING to the
3036  * foreign address to evaluate its validity and latency.
3037  *
3038  * @param fal address to PING
3039  */
3040 static void
3041 schedule_next_ping (struct ForeignAddressList *fal);
3042
3043
3044 /**
3045  * Add the given address to the list of foreign addresses
3046  * available for the given peer (check for duplicates).
3047  *
3048  * @param cls the respective 'struct NeighbourList' to update
3049  * @param tname name of the transport
3050  * @param expiration expiration time
3051  * @param addr the address
3052  * @param addrlen length of the address
3053  * @return GNUNET_OK (always)
3054  */
3055 static int
3056 add_to_foreign_address_list (void *cls,
3057                              const char *tname,
3058                              struct GNUNET_TIME_Absolute expiration,
3059                              const void *addr,
3060                              uint16_t addrlen)
3061 {
3062   struct NeighbourList *n = cls;
3063   struct ForeignAddressList *fal;
3064   int try;
3065
3066   GNUNET_STATISTICS_update (stats,
3067                             gettext_noop ("# valid peer addresses returned by PEERINFO"),
3068                             1,
3069                             GNUNET_NO);
3070   try = GNUNET_NO;
3071   fal = find_peer_address (n, tname, NULL, addr, addrlen);
3072   if (fal == NULL)
3073     {
3074 #if DEBUG_TRANSPORT_HELLO
3075       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3076                   "Adding address `%s' (%s) for peer `%4s' due to PEERINFO data for %llums.\n",
3077                   a2s (tname, addr, addrlen),
3078                   tname,
3079                   GNUNET_i2s (&n->id),
3080                   expiration.abs_value);
3081 #endif
3082       fal = add_peer_address (n, tname, NULL, addr, addrlen);
3083       if (fal == NULL)
3084         {
3085           GNUNET_STATISTICS_update (stats,
3086                                     gettext_noop ("# previously validated addresses lacking transport"),
3087                                     1,
3088                                     GNUNET_NO);
3089         }
3090       else
3091         {
3092           fal->expires = GNUNET_TIME_absolute_max (expiration,
3093                                                    fal->expires);
3094           schedule_next_ping (fal);
3095         }
3096       try = GNUNET_YES;
3097     }
3098   else
3099     {
3100       fal->expires = GNUNET_TIME_absolute_max (expiration,
3101                                                fal->expires);
3102     }
3103   if (fal == NULL)
3104     {
3105 #if DEBUG_TRANSPORT
3106       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3107                   "Failed to add new address for `%4s'\n",
3108                   GNUNET_i2s (&n->id));
3109 #endif
3110       return GNUNET_OK;
3111     }
3112   if (fal->validated == GNUNET_NO)
3113     {
3114       fal->validated = GNUNET_YES;
3115       GNUNET_STATISTICS_update (stats,
3116                                 gettext_noop ("# peer addresses considered valid"),
3117                                 1,
3118                                 GNUNET_NO);
3119     }
3120   if (try == GNUNET_YES)
3121     {
3122 #if DEBUG_TRANSPORT
3123       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3124                   "Have new addresses, will try to trigger transmissions.\n");
3125 #endif
3126       try_transmission_to_peer (n);
3127     }
3128   return GNUNET_OK;
3129 }
3130
3131
3132 /**
3133  * Add addresses in validated HELLO "h" to the set of addresses
3134  * we have for this peer.
3135  *
3136  * @param cls closure ('struct NeighbourList*')
3137  * @param peer id of the peer, NULL for last call
3138  * @param h hello message for the peer (can be NULL)
3139  * @param err_msg NULL if successful, otherwise contains error message
3140  */
3141 static void
3142 add_hello_for_peer (void *cls,
3143                     const struct GNUNET_PeerIdentity *peer,
3144                     const struct GNUNET_HELLO_Message *h,
3145                     const char *err_msg)
3146 {
3147   struct NeighbourList *n = cls;
3148
3149   if (err_msg != NULL)
3150     {
3151 #if DEBUG_TRANSPORT
3152       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3153                   _("Error in communication with PEERINFO service: %s\n"),
3154                   err_msg);
3155 #endif
3156       /* return; */
3157     }
3158   if (peer == NULL)
3159     {
3160       GNUNET_STATISTICS_update (stats,
3161                                 gettext_noop ("# outstanding peerinfo iterate requests"),
3162                                 -1,
3163                                 GNUNET_NO);
3164       n->piter = NULL;
3165       return;
3166     }
3167   if (h == NULL)
3168     return; /* no HELLO available */
3169 #if DEBUG_TRANSPORT
3170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3171               "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
3172               "HELLO",
3173               GNUNET_i2s (peer));
3174 #endif
3175   if (GNUNET_YES != n->public_key_valid)
3176     {
3177       GNUNET_HELLO_get_key (h, &n->publicKey);
3178       n->public_key_valid = GNUNET_YES;
3179     }
3180   GNUNET_HELLO_iterate_addresses (h,
3181                                   GNUNET_NO,
3182                                   &add_to_foreign_address_list,
3183                                   n);
3184 }
3185
3186
3187 /**
3188  * Create a fresh entry in our neighbour list for the given peer.
3189  * Will try to transmit our current HELLO to the new neighbour.
3190  * Do not call this function directly, use 'setup_peer_check_blacklist.
3191  *
3192  * @param peer the peer for which we create the entry
3193  * @param do_hello should we schedule transmitting a HELLO
3194  * @return the new neighbour list entry
3195  */
3196 static struct NeighbourList *
3197 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
3198                      int do_hello)
3199 {
3200   struct NeighbourList *n;
3201   struct TransportPlugin *tp;
3202   struct ReadyList *rl;
3203
3204   GNUNET_assert (0 != memcmp (peer,
3205                               &my_identity,
3206                               sizeof (struct GNUNET_PeerIdentity)));
3207 #if DEBUG_TRANSPORT
3208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3209               "Setting up state for neighbour `%4s'\n",
3210               GNUNET_i2s (peer));
3211 #endif
3212   GNUNET_STATISTICS_update (stats,
3213                             gettext_noop ("# active neighbours"),
3214                             1,
3215                             GNUNET_NO);
3216   n = GNUNET_malloc (sizeof (struct NeighbourList));
3217   n->next = neighbours;
3218   neighbours = n;
3219   n->id = *peer;
3220   n->peer_timeout =
3221     GNUNET_TIME_relative_to_absolute
3222     (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3223   GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
3224                                  GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3225                                  MAX_BANDWIDTH_CARRY_S);
3226   tp = plugins;
3227   while (tp != NULL)
3228     {
3229       if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
3230         {
3231           rl = GNUNET_malloc (sizeof (struct ReadyList));
3232           rl->neighbour = n;
3233           rl->next = n->plugins;
3234           n->plugins = rl;
3235           rl->plugin = tp;
3236           rl->addresses = NULL;
3237         }
3238       tp = tp->next;
3239     }
3240   n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
3241   n->distance = -1;
3242   n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3243                                                   &neighbour_timeout_task, n);
3244   if (do_hello)
3245     {
3246       GNUNET_STATISTICS_update (stats,
3247                                 gettext_noop ("# peerinfo new neighbor iterate requests"),
3248                                 1,
3249                                 GNUNET_NO);
3250       GNUNET_STATISTICS_update (stats,
3251                                 gettext_noop ("# outstanding peerinfo iterate requests"),
3252                                 1,
3253                                 GNUNET_NO);
3254       n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
3255                                           GNUNET_TIME_UNIT_FOREVER_REL,
3256                                           &add_hello_for_peer, n);
3257
3258       GNUNET_STATISTICS_update (stats,
3259                                 gettext_noop ("# HELLO's sent to new neighbors"),
3260                                 1,
3261                                 GNUNET_NO);
3262       if (NULL != our_hello)
3263         transmit_to_peer (NULL, NULL, 0,
3264                           HELLO_ADDRESS_EXPIRATION,
3265                           (const char *) our_hello, GNUNET_HELLO_size(our_hello),
3266                           GNUNET_NO, n);
3267     }
3268   return n;
3269 }
3270
3271
3272 /**
3273  * Function called after we have checked if communicating
3274  * with a given peer is acceptable.
3275  *
3276  * @param cls closure
3277  * @param n NULL if communication is not acceptable
3278  */
3279 typedef void (*SetupContinuation)(void *cls,
3280                                   struct NeighbourList *n);
3281
3282
3283 /**
3284  * Information kept for each client registered to perform
3285  * blacklisting.
3286  */
3287 struct Blacklisters
3288 {
3289   /**
3290    * This is a linked list.
3291    */
3292   struct Blacklisters *next;
3293
3294   /**
3295    * This is a linked list.
3296    */
3297   struct Blacklisters *prev;
3298
3299   /**
3300    * Client responsible for this entry.
3301    */
3302   struct GNUNET_SERVER_Client *client;
3303
3304   /**
3305    * Blacklist check that we're currently performing.
3306    */
3307   struct BlacklistCheck *bc;
3308
3309 };
3310
3311
3312 /**
3313  * Head of DLL of blacklisting clients.
3314  */
3315 static struct Blacklisters *bl_head;
3316
3317 /**
3318  * Tail of DLL of blacklisting clients.
3319  */
3320 static struct Blacklisters *bl_tail;
3321
3322
3323 /**
3324  * Context we use when performing a blacklist check.
3325  */
3326 struct BlacklistCheck
3327 {
3328
3329   /**
3330    * This is a linked list.
3331    */
3332   struct BlacklistCheck *next;
3333
3334   /**
3335    * This is a linked list.
3336    */
3337   struct BlacklistCheck *prev;
3338
3339   /**
3340    * Peer being checked.
3341    */
3342   struct GNUNET_PeerIdentity peer;
3343
3344   /**
3345    * Option for setup neighbour afterwards.
3346    */
3347   int do_hello;
3348
3349   /**
3350    * Continuation to call with the result.
3351    */
3352   SetupContinuation cont;
3353
3354   /**
3355    * Closure for cont.
3356    */
3357   void *cont_cls;
3358
3359   /**
3360    * Current transmission request handle for this client, or NULL if no
3361    * request is pending.
3362    */
3363   struct GNUNET_CONNECTION_TransmitHandle *th;
3364
3365   /**
3366    * Our current position in the blacklisters list.
3367    */
3368   struct Blacklisters *bl_pos;
3369
3370   /**
3371    * Current task performing the check.
3372    */
3373   GNUNET_SCHEDULER_TaskIdentifier task;
3374
3375 };
3376
3377 /**
3378  * Head of DLL of active blacklisting queries.
3379  */
3380 static struct BlacklistCheck *bc_head;
3381
3382 /**
3383  * Tail of DLL of active blacklisting queries.
3384  */
3385 static struct BlacklistCheck *bc_tail;
3386
3387
3388 /**
3389  * Perform next action in the blacklist check.
3390  *
3391  * @param cls the 'struct BlacklistCheck*'
3392  * @param tc unused
3393  */
3394 static void
3395 do_blacklist_check (void *cls,
3396                     const struct GNUNET_SCHEDULER_TaskContext *tc);
3397
3398 /**
3399  * Transmit blacklist query to the client.
3400  *
3401  * @param cls the 'struct BlacklistCheck'
3402  * @param size number of bytes allowed
3403  * @param buf where to copy the message
3404  * @return number of bytes copied to buf
3405  */
3406 static size_t
3407 transmit_blacklist_message (void *cls,
3408                             size_t size,
3409                             void *buf)
3410 {
3411   struct BlacklistCheck *bc = cls;
3412   struct Blacklisters *bl;
3413   struct BlacklistMessage bm;
3414
3415   bc->th = NULL;
3416   if (size == 0)
3417     {
3418       GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
3419       bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3420                                            bc);
3421       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3422                   "Failed to send blacklist test for peer `%s' to client\n",
3423                   GNUNET_i2s (&bc->peer));
3424       return 0;
3425     }
3426 #if DEBUG_TRANSPORT
3427   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3428               "Sending blacklist test for peer `%s' to client\n",
3429               GNUNET_i2s (&bc->peer));
3430 #endif
3431   bl = bc->bl_pos;
3432   bm.header.size = htons (sizeof (struct BlacklistMessage));
3433   bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
3434   bm.is_allowed = htonl (0);
3435   bm.peer = bc->peer;
3436   memcpy (buf, &bm, sizeof (bm));
3437   GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
3438   return sizeof (bm);
3439 }
3440
3441
3442 /**
3443  * Perform next action in the blacklist check.
3444  *
3445  * @param cls the 'struct BlacklistCheck*'
3446  * @param tc unused
3447  */
3448 static void
3449 do_blacklist_check (void *cls,
3450                     const struct GNUNET_SCHEDULER_TaskContext *tc)
3451 {
3452   struct BlacklistCheck *bc = cls;
3453   struct Blacklisters *bl;
3454
3455   bc->task = GNUNET_SCHEDULER_NO_TASK;
3456   bl = bc->bl_pos;
3457   if (bl == NULL)
3458     {
3459 #if DEBUG_TRANSPORT
3460       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3461                   "No blacklist clients active, will now setup neighbour record for peer `%s'\n",
3462                   GNUNET_i2s (&bc->peer));
3463 #endif
3464       bc->cont (bc->cont_cls,
3465                 setup_new_neighbour (&bc->peer, bc->do_hello));
3466       GNUNET_free (bc);
3467       return;
3468     }
3469   if (bl->bc == NULL)
3470     {
3471       bl->bc = bc;
3472       bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
3473                                                     sizeof (struct BlacklistMessage),
3474                                                     GNUNET_TIME_UNIT_FOREVER_REL,
3475                                                     &transmit_blacklist_message,
3476                                                     bc);
3477     }
3478 }
3479
3480
3481 /**
3482  * Obtain a 'struct NeighbourList' for the given peer.  If such an entry
3483  * does not yet exist, check the blacklist.  If the blacklist says creating
3484  * one is acceptable, create one and call the continuation; otherwise
3485  * call the continuation with NULL.
3486  *
3487  * @param peer peer to setup or look up a struct NeighbourList for
3488  * @param do_hello should we also schedule sending our HELLO to the peer
3489  *        if this is a new record
3490  * @param cont function to call with the 'struct NeigbhbourList*'
3491  * @param cont_cls closure for cont
3492  */
3493 static void
3494 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
3495                             int do_hello,
3496                             SetupContinuation cont,
3497                             void *cont_cls)
3498 {
3499   struct NeighbourList *n;
3500   struct BlacklistCheck *bc;
3501
3502   n = find_neighbour(peer);
3503   if (n != NULL)
3504     {
3505 #if DEBUG_TRANSPORT
3506       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 
3507                  "Neighbour record exists for peer `%s'\n", 
3508                  GNUNET_i2s(peer));
3509 #endif
3510       if (cont != NULL)
3511         cont (cont_cls, n);
3512       return;
3513     }
3514   if (bl_head == NULL)
3515     {
3516       if (cont != NULL)
3517         cont (cont_cls, setup_new_neighbour (peer, do_hello));
3518       else
3519         setup_new_neighbour(peer, do_hello);
3520       return;
3521     }
3522   bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3523   GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3524   bc->peer = *peer;
3525   bc->do_hello = do_hello;
3526   bc->cont = cont;
3527   bc->cont_cls = cont_cls;
3528   bc->bl_pos = bl_head;
3529   bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3530                                        bc);
3531 }
3532
3533
3534 /**
3535  * Function called with the result of querying a new blacklister about
3536  * it being allowed (or not) to continue to talk to an existing neighbour.
3537  *
3538  * @param cls the original 'struct NeighbourList'
3539  * @param n NULL if we need to disconnect
3540  */
3541 static void
3542 confirm_or_drop_neighbour (void *cls,
3543                            struct NeighbourList *n)
3544 {
3545   struct NeighbourList * orig = cls;
3546
3547   if (n == NULL)
3548     {
3549 #if DEBUG_TRANSPORT
3550       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3551               "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&orig->id),
3552               "confirm_or_drop_neighboUr");
3553 #endif
3554       GNUNET_STATISTICS_update (stats,
3555                                 gettext_noop ("# disconnects due to blacklist"),
3556                                 1,
3557                                 GNUNET_NO);
3558       disconnect_neighbour (orig, GNUNET_NO);
3559     }
3560 }
3561
3562
3563 /**
3564  * Handle a request to start a blacklist.
3565  *
3566  * @param cls closure (always NULL)
3567  * @param client identification of the client
3568  * @param message the actual message
3569  */
3570 static void
3571 handle_blacklist_init (void *cls,
3572                        struct GNUNET_SERVER_Client *client,
3573                        const struct GNUNET_MessageHeader *message)
3574 {
3575   struct Blacklisters *bl;
3576   struct BlacklistCheck *bc;
3577   struct NeighbourList *n;
3578
3579   bl = bl_head;
3580   while (bl != NULL)
3581     {
3582       if (bl->client == client)
3583         {
3584           GNUNET_break (0);
3585           GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3586           return;
3587         }
3588       bl = bl->next;
3589     }
3590   bl = GNUNET_malloc (sizeof (struct Blacklisters));
3591   bl->client = client;
3592   GNUNET_SERVER_client_keep (client);
3593   GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
3594   /* confirm that all existing connections are OK! */
3595   n = neighbours;
3596   while (NULL != n)
3597     {
3598       bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
3599       GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
3600       bc->peer = n->id;
3601       bc->do_hello = GNUNET_NO;
3602       bc->cont = &confirm_or_drop_neighbour;
3603       bc->cont_cls = n;
3604       bc->bl_pos = bl;
3605       if (n == neighbours) /* all would wait for the same client, no need to
3606                               create more than just the first task right now */
3607         bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3608                                              bc);
3609       n = n->next;
3610     }
3611 }
3612
3613
3614 /**
3615  * Handle a request to blacklist a peer.
3616  *
3617  * @param cls closure (always NULL)
3618  * @param client identification of the client
3619  * @param message the actual message
3620  */
3621 static void
3622 handle_blacklist_reply (void *cls,
3623                         struct GNUNET_SERVER_Client *client,
3624                         const struct GNUNET_MessageHeader *message)
3625 {
3626   const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
3627   struct Blacklisters *bl;
3628   struct BlacklistCheck *bc;
3629
3630   bl = bl_head;
3631   while ( (bl != NULL) &&
3632           (bl->client != client) )
3633     bl = bl->next;
3634   if (bl == NULL)
3635     {
3636 #if DEBUG_TRANSPORT
3637       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3638                   "Blacklist client disconnected\n");
3639 #endif
3640       /* FIXME: other error handling here!? */
3641       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3642       return;
3643     }
3644   bc = bl->bc;
3645   bl->bc = NULL;
3646   if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
3647     {
3648 #if DEBUG_TRANSPORT
3649       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3650                   "Blacklist check failed, peer not allowed\n");
3651 #endif
3652       bc->cont (bc->cont_cls, NULL);
3653       GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
3654       GNUNET_free (bc);
3655     }
3656   else
3657     {
3658 #if DEBUG_TRANSPORT
3659       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3660                   "Blacklist check succeeded, continuing with checks\n");
3661 #endif
3662       bc->bl_pos = bc->bl_pos->next;
3663       bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3664                                            bc);
3665     }
3666   /* check if any other bc's are waiting for this blacklister */
3667   bc = bc_head;
3668   while (bc != NULL)
3669     {
3670       if ( (bc->bl_pos == bl) &&
3671            (GNUNET_SCHEDULER_NO_TASK == bc->task) )
3672         bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
3673                                              bc);
3674       bc = bc->next;
3675     }
3676 }
3677
3678
3679 /**
3680  * Send periodic PING messages to a given foreign address.
3681  *
3682  * @param cls our 'struct PeriodicValidationContext*'
3683  * @param tc task context
3684  */
3685 static void
3686 send_periodic_ping (void *cls,
3687                     const struct GNUNET_SCHEDULER_TaskContext *tc)
3688 {
3689   struct ForeignAddressList *peer_address = cls;
3690   struct TransportPlugin *tp;
3691   struct ValidationEntry *va;
3692   struct NeighbourList *neighbour;
3693   struct TransportPingMessage ping;
3694   struct CheckAddressExistsClosure caec;
3695   char * message_buf;
3696   uint16_t hello_size;
3697   size_t slen;
3698   size_t tsize;
3699
3700   if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
3701     return;
3702
3703   GNUNET_assert (peer_address != NULL);
3704   peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3705
3706   tp = peer_address->ready_list->plugin;
3707   neighbour = peer_address->ready_list->neighbour;
3708   if (GNUNET_YES != neighbour->public_key_valid)
3709     {
3710       /* no public key yet, try again later */
3711       schedule_next_ping (peer_address);
3712       return;
3713     }
3714   caec.addr = peer_address->addr;
3715   caec.addrlen = peer_address->addrlen;
3716   caec.tname = tp->short_name;
3717   caec.session = peer_address->session;
3718   caec.exists = GNUNET_NO;
3719
3720   GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3721                                          &check_address_exists,
3722                                          &caec);
3723   if (caec.exists == GNUNET_YES)
3724     {
3725       /* During validation attempts we will likely trigger the other
3726          peer trying to validate our address which in turn will cause
3727          it to send us its HELLO, so we expect to hit this case rather
3728          frequently.  Only print something if we are very verbose. */
3729 #if DEBUG_TRANSPORT > 1
3730       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3731                   "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3732                   (peer_address->addr != NULL)
3733                   ? a2s (tp->short_name,
3734                          peer_address->addr,
3735                          peer_address->addrlen)
3736                   : "<inbound>",
3737                   tp->short_name,
3738                   GNUNET_i2s (&neighbour->id));
3739 #endif
3740       schedule_next_ping (peer_address);
3741       return;
3742     }
3743   va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3744   va->transport_name = GNUNET_strdup (tp->short_name);
3745   va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
3746                                             UINT_MAX);
3747   va->send_time = GNUNET_TIME_absolute_get();
3748   va->session = peer_address->session;
3749   if (peer_address->addr != NULL)
3750     {
3751       va->addr = (const void*) &va[1];
3752       memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3753       va->addrlen = peer_address->addrlen;
3754     }
3755   memcpy(&va->publicKey,
3756          &neighbour->publicKey,
3757          sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3758
3759   va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
3760                                                    &timeout_hello_validation,
3761                                                    va);
3762   GNUNET_CONTAINER_multihashmap_put (validation_map,
3763                                      &neighbour->id.hashPubKey,
3764                                      va,
3765                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3766
3767   if (peer_address->validated != GNUNET_YES)
3768     hello_size = GNUNET_HELLO_size(our_hello);
3769   else
3770     hello_size = 0;
3771
3772   tsize = sizeof(struct TransportPingMessage) + hello_size;
3773
3774   if (peer_address->addr != NULL)
3775     {
3776       slen = strlen (tp->short_name) + 1;
3777       tsize += slen + peer_address->addrlen;
3778     }
3779   else
3780     {
3781       slen = 0; /* make gcc happy */
3782     }
3783   message_buf = GNUNET_malloc(tsize);
3784   ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3785   ping.challenge = htonl(va->challenge);
3786   memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3787   if (peer_address->validated != GNUNET_YES)
3788     {
3789       memcpy(message_buf, our_hello, hello_size);
3790     }
3791
3792   if (peer_address->addr != NULL)
3793     {
3794       ping.header.size = htons(sizeof(struct TransportPingMessage) +
3795                                peer_address->addrlen +
3796                                slen);
3797       memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
3798              tp->short_name,
3799              slen);
3800       memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
3801              peer_address->addr,
3802              peer_address->addrlen);
3803     }
3804   else
3805     {
3806       ping.header.size = htons(sizeof(struct TransportPingMessage));
3807     }
3808
3809   memcpy(&message_buf[hello_size],
3810          &ping,
3811          sizeof(struct TransportPingMessage));
3812
3813 #if DEBUG_TRANSPORT_REVALIDATION
3814   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3815               "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s'\n",
3816               (peer_address->addr != NULL)
3817               ? a2s (peer_address->plugin->short_name,
3818                      peer_address->addr,
3819                      peer_address->addrlen)
3820               : "<inbound>",
3821               tp->short_name,
3822               GNUNET_i2s (&neighbour->id),
3823               "HELLO", hello_size,
3824               "PING");
3825 #endif
3826   if (peer_address->validated != GNUNET_YES)
3827     GNUNET_STATISTICS_update (stats,
3828                               gettext_noop ("# PING with HELLO messages sent"),
3829                               1,
3830                               GNUNET_NO);
3831   else
3832     GNUNET_STATISTICS_update (stats,
3833                               gettext_noop ("# PING without HELLO messages sent"),
3834                               1,
3835                               GNUNET_NO);
3836   GNUNET_STATISTICS_update (stats,
3837                             gettext_noop ("# PING messages sent for re-validation"),
3838                             1,
3839                             GNUNET_NO);
3840   transmit_to_peer (NULL, peer_address,
3841                     GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3842                     HELLO_VERIFICATION_TIMEOUT,
3843                     message_buf, tsize,
3844                     GNUNET_YES, neighbour);
3845   GNUNET_free(message_buf);
3846   schedule_next_ping (peer_address);
3847 }
3848
3849
3850 /**
3851  * Schedule the job that will cause us to send a PING to the
3852  * foreign address to evaluate its validity and latency.
3853  *
3854  * @param fal address to PING
3855  */
3856 static void
3857 schedule_next_ping (struct ForeignAddressList *fal)
3858 {
3859   struct GNUNET_TIME_Relative delay;
3860
3861   if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3862     {
3863       GNUNET_SCHEDULER_cancel(fal->revalidate_task);
3864       fal->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3865     }
3866   delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3867   delay.rel_value /= 2; /* do before expiration */
3868   delay = GNUNET_TIME_relative_min (delay,
3869                                     LATENCY_EVALUATION_MAX_DELAY);
3870   if (GNUNET_YES != fal->estimated)
3871     {
3872       delay = GNUNET_TIME_UNIT_ZERO;
3873       fal->estimated = GNUNET_YES;
3874     }
3875
3876   if (GNUNET_YES == fal->connected)
3877     {
3878       delay = GNUNET_TIME_relative_min (delay,
3879                                         CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3880     }
3881   /* FIXME: also adjust delay based on how close the last
3882      observed latency is to the latency of the best alternative */
3883   /* bound how fast we can go */
3884   delay = GNUNET_TIME_relative_max (delay,
3885                                     GNUNET_TIME_UNIT_SECONDS);
3886   /* randomize a bit (to avoid doing all at the same time) */
3887   delay.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3888
3889   GNUNET_assert (fal->revalidate_task == GNUNET_SCHEDULER_NO_TASK);
3890   fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(delay,
3891                                                       &send_periodic_ping,
3892                                                       fal);
3893 }
3894
3895
3896
3897
3898 /**
3899  * Function that will be called if we receive some payload
3900  * from another peer.
3901  *
3902  * @param message the payload
3903  * @param n peer who claimed to be the sender
3904  */
3905 static void
3906 handle_payload_message (const struct GNUNET_MessageHeader *message,
3907                         struct NeighbourList *n)
3908 {
3909   struct InboundMessage *im;
3910   struct TransportClient *cpos;
3911   uint16_t msize;
3912
3913   msize = ntohs (message->size);
3914   if (n->received_pong == GNUNET_NO)
3915     {
3916 #if DEBUG_TRANSPORT
3917       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3918                   "Received message of type %u and size %u from `%4s', but no pong yet!\n",
3919                   ntohs (message->type),
3920                   ntohs (message->size),
3921                   GNUNET_i2s (&n->id));
3922 #endif
3923       GNUNET_free_non_null (n->pre_connect_message_buffer);
3924       n->pre_connect_message_buffer = GNUNET_malloc (msize);
3925       memcpy (n->pre_connect_message_buffer, message, msize);
3926       return;
3927     }
3928
3929 #if DEBUG_TRANSPORT
3930   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3931               "Received message of type %u and size %u from `%4s', sending to all clients.\n",
3932               ntohs (message->type),
3933               ntohs (message->size),
3934               GNUNET_i2s (&n->id));
3935 #endif
3936   if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3937                                                       (ssize_t) msize))
3938     {
3939       n->quota_violation_count++;
3940 #if DEBUG_TRANSPORT
3941       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3942                   "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
3943                   n->in_tracker.available_bytes_per_s__,
3944                   n->quota_violation_count);
3945 #endif
3946       /* Discount 32k per violation */
3947       GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3948                                         - 32 * 1024);
3949     }
3950   else
3951     {
3952       if (n->quota_violation_count > 0)
3953         {
3954           /* try to add 32k back */
3955           GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3956                                             32 * 1024);
3957           n->quota_violation_count--;
3958         }
3959     }
3960   GNUNET_STATISTICS_update (stats,
3961                             gettext_noop ("# payload received from other peers"),
3962                             msize,
3963                             GNUNET_NO);
3964   /* transmit message to all clients */
3965   uint32_t ats_count = 2;
3966   size_t size = sizeof (struct InboundMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + msize;
3967   if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
3968           GNUNET_break(0);
3969
3970   im = GNUNET_malloc (size);
3971   im->header.size = htons (size);
3972   im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3973   im->peer = n->id;
3974   im->ats_count = htonl(ats_count);
3975   /* Setting ATS data */
3976   (&(im->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
3977   (&(im->ats))[0].value = htonl (n->distance);
3978   (&(im->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
3979   (&(im->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
3980   (&(im->ats))[ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3981   (&(im->ats))[ats_count].value = htonl (0);
3982
3983   memcpy (&((&(im->ats))[ats_count+1]), message, msize);
3984   cpos = clients;
3985   while (cpos != NULL)
3986     {
3987       transmit_to_client (cpos, &im->header, GNUNET_YES);
3988       cpos = cpos->next;
3989     }
3990   GNUNET_free (im);
3991 }
3992
3993
3994 /**
3995  * Iterator over hash map entries.  Checks if the given validation
3996  * entry is for the same challenge as what is given in the PONG.
3997  *
3998  * @param cls the 'struct TransportPongMessage*'
3999  * @param key peer identity
4000  * @param value value in the hash map ('struct ValidationEntry')
4001  * @return GNUNET_YES if we should continue to
4002  *         iterate (mismatch), GNUNET_NO if not (entry matched)
4003  */
4004 static int
4005 check_pending_validation (void *cls,
4006                           const GNUNET_HashCode * key,
4007                           void *value)
4008 {
4009   const struct TransportPongMessage *pong = cls;
4010   struct ValidationEntry *ve = value;
4011   struct AddValidatedAddressContext avac;
4012   unsigned int challenge = ntohl(pong->challenge);
4013   struct GNUNET_HELLO_Message *hello;
4014   struct GNUNET_PeerIdentity target;
4015   struct NeighbourList *n;
4016   struct ForeignAddressList *fal;
4017   struct OwnAddressList *oal;
4018   struct TransportPlugin *tp;
4019   struct GNUNET_MessageHeader *prem;
4020   uint16_t ps;
4021   const char *addr;
4022   size_t slen;
4023   size_t alen;
4024
4025   ps = ntohs (pong->header.size);
4026   if (ps < sizeof (struct TransportPongMessage))
4027     {
4028       GNUNET_break_op (0);
4029       return GNUNET_NO;
4030     }
4031   addr = (const char*) &pong[1];
4032   slen = strlen (ve->transport_name) + 1;
4033   if ( (ps - sizeof (struct TransportPongMessage) < slen) ||
4034        (ve->challenge != challenge) ||
4035        (addr[slen-1] != '\0') ||
4036        (0 != strcmp (addr, ve->transport_name)) ||
4037        (ntohl (pong->purpose.size)
4038         != sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
4039         sizeof (uint32_t) +
4040         sizeof (struct GNUNET_TIME_AbsoluteNBO) +
4041         sizeof (struct GNUNET_PeerIdentity) + ps - sizeof (struct TransportPongMessage)) )
4042     {
4043       return GNUNET_YES;
4044     }
4045
4046   alen = ps - sizeof (struct TransportPongMessage) - slen;
4047   switch (ntohl (pong->purpose.purpose))
4048     {
4049     case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN:
4050       if ( (ve->addrlen + slen != ntohl (pong->addrlen)) ||
4051            (0 != memcmp (&addr[slen],
4052                          ve->addr,
4053                          ve->addrlen)) )
4054         {
4055           return GNUNET_YES; /* different entry, keep trying! */
4056         }
4057       if (0 != memcmp (&pong->pid,
4058                        key,
4059                        sizeof (struct GNUNET_PeerIdentity)))
4060         {
4061           GNUNET_break_op (0);
4062           return GNUNET_NO;
4063         }
4064       if (GNUNET_OK !=
4065           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
4066                                     &pong->purpose,
4067                                     &pong->signature,
4068                                     &ve->publicKey))
4069         {
4070           GNUNET_break_op (0);
4071           return GNUNET_NO;
4072         }
4073
4074 #if DEBUG_TRANSPORT
4075       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4076                   "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
4077                   GNUNET_h2s (key),
4078                   a2s (ve->transport_name,
4079                        (const struct sockaddr *) ve->addr,
4080                        ve->addrlen),
4081                   ve->transport_name);
4082 #endif
4083       break;
4084     case GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING:
4085       if (0 != memcmp (&pong->pid,
4086                          &my_identity,
4087                          sizeof (struct GNUNET_PeerIdentity)))
4088         {
4089           char * peer;
4090
4091           GNUNET_asprintf(&peer, "%s",GNUNET_i2s (&pong->pid));
4092 #if DEBUG_TRANSPORT
4093           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4094                       "Received PONG for different identity: I am `%s', PONG identity: `%s'\n",
4095                       GNUNET_i2s (&my_identity), 
4096                       peer );
4097 #endif
4098           GNUNET_free (peer);
4099           return GNUNET_NO;
4100         }
4101       if (ve->addrlen != 0)
4102         {
4103           /* must have been for a different validation entry */
4104           return GNUNET_YES;
4105         }
4106       tp = find_transport (ve->transport_name);
4107       if (tp == NULL)
4108         {
4109           GNUNET_break (0);
4110           return GNUNET_YES;
4111         }
4112       oal = tp->addresses;
4113       while (NULL != oal)
4114         {
4115           if ( (oal->addrlen == alen) &&
4116                (0 == memcmp (&oal[1],
4117                              &addr[slen],
4118                              alen)) )
4119             break;
4120           oal = oal->next;
4121         }
4122       if (oal == NULL)
4123         {
4124           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4125                       _("Not accepting PONG from `%s' with address `%s' since I cannot confirm using this address.\n"),
4126                       GNUNET_i2s (&pong->pid),
4127                       a2s (ve->transport_name,
4128                            &addr[slen],
4129                            alen));
4130           /* FIXME: since the sender of the PONG currently uses the
4131              wrong address (see FIMXE there!), we cannot run a 
4132              proper check here... */
4133 #if FIXME_URGENT
4134           return GNUNET_NO;
4135 #endif
4136         }
4137       if (GNUNET_OK !=
4138           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING,
4139                                     &pong->purpose,
4140                                     &pong->signature,
4141                                     &ve->publicKey))
4142         {
4143           GNUNET_break_op (0);
4144           return GNUNET_NO;
4145         }
4146
4147 #if DEBUG_TRANSPORT
4148       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4149                   "Confirmed that peer `%4s' is talking to us using address `%s' (%s) for us.\n",
4150                   GNUNET_h2s (key),
4151                   a2s (ve->transport_name,
4152                        &addr[slen],
4153                        alen),
4154                   ve->transport_name);
4155 #endif
4156       break;
4157     default:
4158       GNUNET_break_op (0);
4159       return GNUNET_NO;
4160     }
4161   if (GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (pong->expiration)).rel_value == 0)
4162     {
4163       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4164                   _("Received expired signature.  Check system time.\n"));
4165       return GNUNET_NO;
4166     }
4167   GNUNET_STATISTICS_update (stats,
4168                             gettext_noop ("# address validation successes"),
4169                             1,
4170                             GNUNET_NO);
4171   /* create the updated HELLO */
4172   GNUNET_CRYPTO_hash (&ve->publicKey,
4173                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4174                       &target.hashPubKey);
4175   if (ve->addr != NULL)
4176     {
4177       avac.done = GNUNET_NO;
4178       avac.ve = ve;
4179       hello = GNUNET_HELLO_create (&ve->publicKey,
4180                                    &add_validated_address,
4181                                    &avac);
4182       GNUNET_PEERINFO_add_peer (peerinfo,
4183                                 hello);
4184       GNUNET_free (hello);
4185     }
4186   n = find_neighbour (&target);
4187   if (n != NULL)
4188     {
4189       n->publicKey = ve->publicKey;
4190       n->public_key_valid = GNUNET_YES;
4191       fal = add_peer_address (n,
4192                               ve->transport_name,
4193                               ve->session,
4194                               ve->addr,
4195                               ve->addrlen);
4196       GNUNET_assert (fal != NULL);
4197       fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
4198       fal->validated = GNUNET_YES;
4199       mark_address_connected (fal);
4200       GNUNET_STATISTICS_update (stats,
4201                                 gettext_noop ("# peer addresses considered valid"),
4202                                 1,
4203                                 GNUNET_NO);
4204       fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
4205       update_addr_value (fal, GNUNET_TIME_absolute_get_duration (ve->send_time).rel_value, GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
4206
4207       schedule_next_ping (fal);
4208       if (n->latency.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
4209         n->latency = fal->latency;
4210       else
4211         n->latency.rel_value = (fal->latency.rel_value + n->latency.rel_value) / 2;
4212
4213       n->distance = fal->distance;
4214       if (GNUNET_NO == n->received_pong)
4215         {
4216           n->received_pong = GNUNET_YES;
4217           notify_clients_connect (&target, n->latency, n->distance);
4218           if (NULL != (prem = n->pre_connect_message_buffer))
4219             {
4220               n->pre_connect_message_buffer = NULL;
4221               handle_payload_message (prem, n);
4222               GNUNET_free (prem);
4223             }
4224         }
4225       if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4226         {
4227           GNUNET_SCHEDULER_cancel (n->retry_task);
4228           n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4229           try_transmission_to_peer (n);
4230         }
4231     }
4232
4233   /* clean up validation entry */
4234   GNUNET_assert (GNUNET_YES ==
4235                  GNUNET_CONTAINER_multihashmap_remove (validation_map,
4236                                                        key,
4237                                                        ve));
4238   abort_validation (NULL, NULL, ve);
4239   return GNUNET_NO;
4240 }
4241
4242
4243 /**
4244  * Function that will be called if we receive a validation
4245  * of an address challenge that we transmitted to another
4246  * peer.  Note that the validation should only be considered
4247  * acceptable if the challenge matches AND if the sender
4248  * address is at least a plausible address for this peer
4249  * (otherwise we may be seeing a MiM attack).
4250  *
4251  * @param cls closure
4252  * @param message the pong message
4253  * @param peer who responded to our challenge
4254  * @param sender_address string describing our sender address (as observed
4255  *         by the other peer in binary format)
4256  * @param sender_address_len number of bytes in 'sender_address'
4257  */
4258 static void
4259 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
4260              const struct GNUNET_PeerIdentity *peer,
4261              const char *sender_address,
4262              size_t sender_address_len)
4263 {
4264   if (0 == memcmp (peer,
4265                    &my_identity,
4266                    sizeof (struct GNUNET_PeerIdentity)))
4267     {
4268       /* PONG send to self, ignore */
4269       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4270                   "Receiving `%s' message from myself\n", 
4271                   "PONG");
4272       return;
4273     }
4274 #if DEBUG_TRANSPORT > 1
4275   /* we get tons of these that just get discarded, only log
4276      if we are quite verbose */
4277   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4278               "Receiving `%s' message from `%4s'.\n", "PONG",
4279               GNUNET_i2s (peer));
4280 #endif
4281   GNUNET_STATISTICS_update (stats,
4282                             gettext_noop ("# PONG messages received"),
4283                             1,
4284                             GNUNET_NO);
4285   if (GNUNET_SYSERR !=
4286       GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
4287                                                   &peer->hashPubKey,
4288                                                   &check_pending_validation,
4289                                                   (void*) message))
4290     {
4291       /* This is *expected* to happen a lot since we send
4292          PONGs to *all* known addresses of the sender of
4293          the PING, so most likely we get multiple PONGs
4294          per PING, and all but the first PONG will end up
4295          here. So really we should not print anything here
4296          unless we want to be very, very verbose... */
4297 #if DEBUG_TRANSPORT > 2
4298       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4299                   "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
4300                   "PONG",
4301                   GNUNET_i2s (peer),
4302                   "PING");
4303 #endif
4304       return;
4305     }
4306
4307 }
4308
4309
4310 /**
4311  * Try to validate a neighbour's address by sending him our HELLO and a PING.
4312  *
4313  * @param cls the 'struct ValidationEntry*'
4314  * @param neighbour neighbour to validate, NULL if validation failed
4315  */
4316 static void
4317 transmit_hello_and_ping (void *cls,
4318                          struct NeighbourList *neighbour)
4319 {
4320   struct ValidationEntry *va = cls;
4321   struct ForeignAddressList *peer_address;
4322   struct TransportPingMessage ping;
4323   uint16_t hello_size;
4324   size_t tsize;
4325   char * message_buf;
4326   struct GNUNET_PeerIdentity id;
4327   size_t slen;
4328
4329   GNUNET_CRYPTO_hash (&va->publicKey,
4330                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4331                       &id.hashPubKey);
4332   if (neighbour == NULL)
4333     {
4334       /* FIXME: stats... */
4335       GNUNET_break (GNUNET_OK ==
4336                     GNUNET_CONTAINER_multihashmap_remove (validation_map,
4337                                                           &id.hashPubKey,
4338                                                           va));
4339       abort_validation (NULL, NULL, va);
4340       return;
4341     }
4342   neighbour->publicKey = va->publicKey;
4343   neighbour->public_key_valid = GNUNET_YES;
4344   peer_address = add_peer_address (neighbour,
4345                                    va->transport_name, NULL,
4346                                    (const void*) &va[1],
4347                                    va->addrlen);
4348   if (peer_address == NULL)
4349     {
4350       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4351                   "Failed to add peer `%4s' for plugin `%s'\n",
4352                   GNUNET_i2s (&neighbour->id),
4353                   va->transport_name);
4354       GNUNET_break (GNUNET_OK ==
4355                     GNUNET_CONTAINER_multihashmap_remove (validation_map,
4356                                                           &id.hashPubKey,
4357                                                           va));
4358       abort_validation (NULL, NULL, va);
4359       return;
4360     }
4361   if (NULL == our_hello)
4362     refresh_hello_task (NULL, NULL);
4363   hello_size = GNUNET_HELLO_size(our_hello);
4364   slen = strlen(va->transport_name) + 1;
4365   tsize = sizeof(struct TransportPingMessage) + hello_size + va->addrlen + slen;
4366   message_buf = GNUNET_malloc(tsize);
4367   ping.challenge = htonl(va->challenge);
4368   ping.header.size = htons(sizeof(struct TransportPingMessage) + slen + va->addrlen);
4369   ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
4370   memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
4371   memcpy(message_buf, our_hello, hello_size);
4372   memcpy(&message_buf[hello_size],
4373          &ping,
4374          sizeof(struct TransportPingMessage));
4375   memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage)],
4376          va->transport_name,
4377          slen);
4378   memcpy(&message_buf[hello_size + sizeof (struct TransportPingMessage) + slen],
4379          &va[1],
4380          va->addrlen);
4381 #if DEBUG_TRANSPORT
4382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4383               "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
4384               (va->addrlen == 0)
4385               ? "<inbound>"
4386               : a2s (va->transport_name,
4387                      (const void*) &va[1], va->addrlen),
4388               va->transport_name,
4389               GNUNET_i2s (&neighbour->id),
4390               "HELLO", hello_size,
4391               "PING", sizeof (struct TransportPingMessage) + va->addrlen + slen);
4392 #endif
4393
4394   GNUNET_STATISTICS_update (stats,
4395                             gettext_noop ("# PING messages sent for initial validation"),
4396                             1,
4397                             GNUNET_NO);
4398   transmit_to_peer (NULL, peer_address,
4399                     GNUNET_SCHEDULER_PRIORITY_DEFAULT,
4400                     HELLO_VERIFICATION_TIMEOUT,
4401                     message_buf, tsize,
4402                     GNUNET_YES, neighbour);
4403   GNUNET_free(message_buf);
4404 }
4405
4406
4407 /**
4408  * Check if the given address is already being validated; if not,
4409  * append the given address to the list of entries that are being be
4410  * validated and initiate validation.
4411  *
4412  * @param cls closure ('struct CheckHelloValidatedContext *')
4413  * @param tname name of the transport
4414  * @param expiration expiration time
4415  * @param addr the address
4416  * @param addrlen length of the address
4417  * @return GNUNET_OK (always)
4418  */
4419 static int
4420 run_validation (void *cls,
4421                 const char *tname,
4422                 struct GNUNET_TIME_Absolute expiration,
4423                 const void *addr,
4424                 uint16_t addrlen)
4425 {
4426   struct CheckHelloValidatedContext *chvc = cls;
4427   struct GNUNET_PeerIdentity id;
4428   struct TransportPlugin *tp;
4429   struct ValidationEntry *va;
4430   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4431   struct CheckAddressExistsClosure caec;
4432   struct OwnAddressList *oal;
4433
4434   GNUNET_assert (addr != NULL);
4435
4436   GNUNET_STATISTICS_update (stats,
4437                             gettext_noop ("# peer addresses scheduled for validation"),
4438                             1,
4439                             GNUNET_NO);
4440   tp = find_transport (tname);
4441   if (tp == NULL)
4442     {
4443       GNUNET_log (GNUNET_ERROR_TYPE_INFO |
4444                   GNUNET_ERROR_TYPE_BULK,
4445                   _
4446                   ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
4447                   tname);
4448       GNUNET_STATISTICS_update (stats,
4449                                 gettext_noop ("# peer addresses not validated (plugin not available)"),
4450                                 1,
4451                                 GNUNET_NO);
4452       return GNUNET_OK;
4453     }
4454   /* check if this is one of our own addresses */
4455   oal = tp->addresses;
4456   while (NULL != oal)
4457     {
4458       if ( (oal->addrlen == addrlen) &&
4459            (0 == memcmp (&oal[1],
4460                          addr,
4461                          addrlen)) )
4462         {
4463           /* not plausible, this address is equivalent to our own address! */
4464           GNUNET_STATISTICS_update (stats,
4465                                     gettext_noop ("# peer addresses not validated (loopback)"),
4466                                     1,
4467                                     GNUNET_NO);
4468           return GNUNET_OK;
4469         }
4470       oal = oal->next;
4471     }
4472   GNUNET_HELLO_get_key (chvc->hello, &pk);
4473   GNUNET_CRYPTO_hash (&pk,
4474                       sizeof (struct
4475                               GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4476                       &id.hashPubKey);
4477
4478   if (is_blacklisted(&id, tp))
4479     {
4480 #if DEBUG_TRANSPORT
4481       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4482                   "Attempted to validate blacklisted peer `%s' using `%s'!\n",
4483                   GNUNET_i2s(&id),
4484                   tname);
4485 #endif
4486       return GNUNET_OK;
4487     }
4488
4489   caec.addr = addr;
4490   caec.addrlen = addrlen;
4491   caec.session = NULL;
4492   caec.tname = tname;
4493   caec.exists = GNUNET_NO;
4494   GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4495                                          &check_address_exists,
4496                                          &caec);
4497   if (caec.exists == GNUNET_YES)
4498     {
4499       /* During validation attempts we will likely trigger the other
4500          peer trying to validate our address which in turn will cause
4501          it to send us its HELLO, so we expect to hit this case rather
4502          frequently.  Only print something if we are very verbose. */
4503 #if DEBUG_TRANSPORT > 1
4504       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4505                   "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
4506                   a2s (tname, addr, addrlen),
4507                   tname,
4508                   GNUNET_i2s (&id));
4509 #endif
4510       GNUNET_STATISTICS_update (stats,
4511                                 gettext_noop ("# peer addresses not validated (in progress)"),
4512                                 1,
4513                                 GNUNET_NO);
4514       return GNUNET_OK;
4515     }
4516   va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
4517   va->chvc = chvc;
4518   chvc->ve_count++;
4519   va->transport_name = GNUNET_strdup (tname);
4520   va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
4521                                             UINT_MAX);
4522   va->send_time = GNUNET_TIME_absolute_get();
4523   va->addr = (const void*) &va[1];
4524   memcpy (&va[1], addr, addrlen);
4525   va->addrlen = addrlen;
4526   GNUNET_HELLO_get_key (chvc->hello,
4527                         &va->publicKey);
4528   va->timeout_task = GNUNET_SCHEDULER_add_delayed (HELLO_VERIFICATION_TIMEOUT,
4529                                                    &timeout_hello_validation,
4530                                                    va);
4531   GNUNET_CONTAINER_multihashmap_put (validation_map,
4532                                      &id.hashPubKey,
4533                                      va,
4534                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
4535   setup_peer_check_blacklist (&id, GNUNET_NO,
4536                               &transmit_hello_and_ping,
4537                               va);
4538   return GNUNET_OK;
4539 }
4540
4541
4542 /**
4543  * Check if addresses in validated hello "h" overlap with
4544  * those in "chvc->hello" and validate the rest.
4545  *
4546  * @param cls closure
4547  * @param peer id of the peer, NULL for last call
4548  * @param h hello message for the peer (can be NULL)
4549  * @param err_msg NULL if successful, otherwise contains error message
4550  */
4551 static void
4552 check_hello_validated (void *cls,
4553                        const struct GNUNET_PeerIdentity *peer,
4554                        const struct GNUNET_HELLO_Message *h,
4555                        const char *err_msg)
4556 {
4557   struct CheckHelloValidatedContext *chvc = cls;
4558   struct GNUNET_HELLO_Message *plain_hello;
4559   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
4560   struct GNUNET_PeerIdentity target;
4561   struct NeighbourList *n;
4562
4563   if (err_msg != NULL)
4564     {
4565 #if DEBUG_TRANSPORT
4566       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4567                   _("Error in communication with PEERINFO service: %s\n"),
4568                   err_msg);
4569 #endif
4570       /* return; */
4571   }
4572
4573   if (peer == NULL)
4574     {
4575       GNUNET_STATISTICS_update (stats,
4576                                 gettext_noop ("# outstanding peerinfo iterate requests"),
4577                                 -1,
4578                                 GNUNET_NO);
4579       chvc->piter = NULL;
4580       if (GNUNET_NO == chvc->hello_known)
4581         {
4582           /* notify PEERINFO about the peer now, so that we at least
4583              have the public key if some other component needs it */
4584           GNUNET_HELLO_get_key (chvc->hello, &pk);
4585           GNUNET_CRYPTO_hash (&pk,
4586                               sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4587                               &target.hashPubKey);
4588           plain_hello = GNUNET_HELLO_create (&pk,
4589                                              NULL,
4590                                              NULL);
4591           GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
4592           GNUNET_free (plain_hello);
4593 #if DEBUG_TRANSPORT_HELLO
4594           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4595                       "PEERINFO had no `%s' message for peer `%4s', full validation needed.\n",
4596                       "HELLO",
4597                       GNUNET_i2s (&target));
4598 #endif
4599           GNUNET_STATISTICS_update (stats,
4600                                     gettext_noop ("# new HELLOs requiring full validation"),
4601                                     1,
4602                                     GNUNET_NO);
4603           GNUNET_HELLO_iterate_addresses (chvc->hello,
4604                                           GNUNET_NO,
4605                                           &run_validation,
4606                                           chvc);
4607         }
4608       else
4609         {
4610           GNUNET_STATISTICS_update (stats,
4611                                     gettext_noop ("# duplicate HELLO (peer known)"),
4612                                     1,
4613                                     GNUNET_NO);
4614         }
4615       chvc->ve_count--;
4616       if (chvc->ve_count == 0)
4617         {
4618           GNUNET_CONTAINER_DLL_remove (chvc_head,
4619                                        chvc_tail,
4620                                        chvc);
4621           GNUNET_free (chvc);
4622         }
4623       return;
4624     }
4625   if (h == NULL)
4626     return;
4627 #if DEBUG_TRANSPORT_HELLO
4628   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4629               "PEERINFO had `%s' message for peer `%4s', validating only new addresses.\n",
4630               "HELLO",
4631               GNUNET_i2s (peer));
4632 #endif
4633   chvc->hello_known = GNUNET_YES;
4634   n = find_neighbour (peer);
4635   if (n != NULL)
4636     {
4637 #if DEBUG_TRANSPORT_HELLO
4638       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4639                   "Calling hello_iterate_addresses for %s!\n",
4640                   GNUNET_i2s (peer));
4641 #endif
4642       GNUNET_HELLO_iterate_addresses (h,
4643                                       GNUNET_NO,
4644                                       &add_to_foreign_address_list,
4645                                       n);
4646       try_transmission_to_peer (n);
4647     }
4648   else
4649     {
4650 #if DEBUG_TRANSPORT_HELLO
4651       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4652                   "No existing neighbor record for %s!\n",
4653                   GNUNET_i2s (peer));
4654 #endif
4655       GNUNET_STATISTICS_update (stats,
4656                                 gettext_noop ("# no existing neighbour record (validating HELLO)"),
4657                                 1,
4658                                 GNUNET_NO);
4659     }
4660   GNUNET_STATISTICS_update (stats,
4661                             gettext_noop ("# HELLO validations (update case)"),
4662                             1,
4663                             GNUNET_NO);
4664   GNUNET_HELLO_iterate_new_addresses (chvc->hello,
4665                                       h,
4666                                       GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
4667                                       &run_validation,
4668                                       chvc);
4669 }
4670
4671
4672 /**
4673  * Process HELLO-message.
4674  *
4675  * @param plugin transport involved, may be NULL
4676  * @param message the actual message
4677  * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
4678  */
4679 static int
4680 process_hello (struct TransportPlugin *plugin,
4681                const struct GNUNET_MessageHeader *message)
4682 {
4683   uint16_t hsize;
4684   struct GNUNET_PeerIdentity target;
4685   const struct GNUNET_HELLO_Message *hello;
4686   struct CheckHelloValidatedContext *chvc;
4687   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
4688   struct NeighbourList *n;
4689 #if DEBUG_TRANSPORT_HELLO > 2
4690   char *my_id;
4691 #endif
4692
4693   hsize = ntohs (message->size);
4694   if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
4695       (hsize < sizeof (struct GNUNET_MessageHeader)))
4696     {
4697       GNUNET_break (0);
4698       return GNUNET_SYSERR;
4699     }
4700   GNUNET_STATISTICS_update (stats,
4701                             gettext_noop ("# HELLOs received for validation"),
4702                             1,
4703                             GNUNET_NO);
4704
4705   hello = (const struct GNUNET_HELLO_Message *) message;
4706   if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
4707     {
4708 #if DEBUG_TRANSPORT_HELLO
4709       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4710                   "Unable to get public key from `%s' for `%4s'!\n",
4711                   "HELLO",
4712                   GNUNET_i2s (&target));
4713 #endif
4714       GNUNET_break_op (0);
4715       return GNUNET_SYSERR;
4716     }
4717   GNUNET_CRYPTO_hash (&publicKey,
4718                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
4719                       &target.hashPubKey);
4720
4721 #if DEBUG_TRANSPORT_HELLO
4722   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4723               "Received `%s' message for `%4s'\n",
4724               "HELLO",
4725               GNUNET_i2s (&target));
4726 #endif
4727   if (0 == memcmp (&my_identity,
4728                    &target,
4729                    sizeof (struct GNUNET_PeerIdentity)))
4730     {
4731       GNUNET_STATISTICS_update (stats,
4732                                 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
4733                                 1,
4734                                 GNUNET_NO);
4735       return GNUNET_OK;
4736     }
4737   n = find_neighbour (&target);
4738   if ( (NULL != n) &&
4739        (! n->public_key_valid) )
4740     {
4741       GNUNET_HELLO_get_key (hello, &n->publicKey);
4742       n->public_key_valid = GNUNET_YES;
4743     }
4744
4745   /* check if load is too high before doing expensive stuff */
4746   if (GNUNET_SCHEDULER_get_load (GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
4747     {
4748       GNUNET_STATISTICS_update (stats,
4749                                 gettext_noop ("# HELLOs ignored due to high load"),
4750                                 1,
4751                                 GNUNET_NO);
4752 #if DEBUG_TRANSPORT_HELLO
4753       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4754                   "Ignoring `%s' for `%4s', load too high.\n",
4755                   "HELLO",
4756                   GNUNET_i2s (&target));
4757 #endif
4758       return GNUNET_OK;
4759     }
4760
4761
4762   chvc = chvc_head;
4763   while (NULL != chvc)
4764     {
4765       if (GNUNET_HELLO_equals (hello,
4766                                chvc->hello,
4767                                GNUNET_TIME_absolute_get ()).abs_value > 0)
4768         {
4769 #if DEBUG_TRANSPORT_HELLO > 2
4770           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4771                       "Received duplicate `%s' message for `%4s'; ignored\n",
4772                       "HELLO",
4773                       GNUNET_i2s (&target));
4774 #endif
4775           return GNUNET_OK; /* validation already pending */
4776         }
4777       if (GNUNET_HELLO_size (hello) == GNUNET_HELLO_size (chvc->hello))
4778         GNUNET_break (0 != memcmp (hello, chvc->hello,
4779                                    GNUNET_HELLO_size(hello)));
4780       chvc = chvc->next;
4781     }
4782
4783 #if BREAK_TESTS
4784   struct NeighbourList *temp_neighbor = find_neighbour(&target);
4785   if ((NULL != temp_neighbor))
4786     {
4787       fprintf(stderr, "Already know peer, ignoring hello\n");
4788       return GNUNET_OK;
4789     }
4790 #endif
4791
4792 #if DEBUG_TRANSPORT_HELLO > 2
4793   if (plugin != NULL)
4794     {
4795       my_id = GNUNET_strdup(GNUNET_i2s(plugin->env.my_identity));
4796 #if DEBUG_TRANSPORT
4797       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4798                   "%s: Starting validation of `%s' message for `%4s' via '%s' of size %u\n",
4799                   my_id,
4800                   "HELLO",
4801                   GNUNET_i2s (&target),
4802                   plugin->short_name,
4803                   GNUNET_HELLO_size(hello));
4804 #endif
4805       GNUNET_free(my_id);
4806     }
4807 #endif
4808   chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
4809   chvc->ve_count = 1;
4810   chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
4811   memcpy (&chvc[1], hello, hsize);
4812   GNUNET_CONTAINER_DLL_insert (chvc_head,
4813                                chvc_tail,
4814                                chvc);
4815   /* finally, check if HELLO was previously validated
4816      (continuation will then schedule actual validation) */
4817   GNUNET_STATISTICS_update (stats,
4818                             gettext_noop ("# peerinfo process hello iterate requests"),
4819                             1,
4820                             GNUNET_NO);
4821   GNUNET_STATISTICS_update (stats,
4822                             gettext_noop ("# outstanding peerinfo iterate requests"),
4823                             1,
4824                             GNUNET_NO);
4825   chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
4826                                          &target,
4827                                          HELLO_VERIFICATION_TIMEOUT,
4828                                          &check_hello_validated, chvc);
4829   return GNUNET_OK;
4830 }
4831
4832
4833 /**
4834  * The peer specified by the given neighbour has timed-out or a plugin
4835  * has disconnected.  We may either need to do nothing (other plugins
4836  * still up), or trigger a full disconnect and clean up.  This
4837  * function updates our state and does the necessary notifications.
4838  * Also notifies our clients that the neighbour is now officially
4839  * gone.
4840  *
4841  * @param n the neighbour list entry for the peer
4842  * @param check GNUNET_YES to check if ALL addresses for this peer
4843  *              are gone, GNUNET_NO to force a disconnect of the peer
4844  *              regardless of whether other addresses exist.
4845  */
4846 static void
4847 disconnect_neighbour (struct NeighbourList *n, int check)
4848 {
4849   struct ReadyList *rpos;
4850   struct NeighbourList *npos;
4851   struct NeighbourList *nprev;
4852   struct MessageQueue *mq;
4853   struct ForeignAddressList *peer_addresses;
4854   struct ForeignAddressList *peer_pos;
4855
4856   if (GNUNET_YES == check)
4857     {
4858       rpos = n->plugins;
4859       while (NULL != rpos)
4860         {
4861           peer_addresses = rpos->addresses;
4862           while (peer_addresses != NULL)
4863             {
4864               // Do not disconnect if: an address is connected or an inbound address exists
4865               if ((GNUNET_YES == peer_addresses->connected) || (peer_addresses->addrlen == 0))
4866                 {
4867 #if DEBUG_TRANSPORT
4868                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4869                               "NOT Disconnecting from `%4s', still have live address `%s'!\n",
4870                               GNUNET_i2s (&n->id),
4871                               a2s (peer_addresses->ready_list->plugin->short_name,
4872                                    peer_addresses->addr,
4873                                    peer_addresses->addrlen));
4874 #endif
4875                   return;             /* still connected */
4876                 }
4877               peer_addresses = peer_addresses->next;
4878             }
4879           rpos = rpos->next;
4880         }
4881     }
4882 #if DEBUG_TRANSPORT
4883   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4884               "Disconnecting from `%4s'\n",
4885               GNUNET_i2s (&n->id));
4886 #endif
4887
4888   /* remove n from neighbours list */
4889   nprev = NULL;
4890   npos = neighbours;
4891   while ((npos != NULL) && (npos != n))
4892     {
4893       nprev = npos;
4894       npos = npos->next;
4895     }
4896   GNUNET_assert (npos != NULL);
4897   if (nprev == NULL)
4898     neighbours = n->next;
4899   else
4900     nprev->next = n->next;
4901
4902   /* notify all clients about disconnect */
4903   if (GNUNET_YES == n->received_pong)
4904     notify_clients_disconnect (&n->id);
4905
4906   ats_modify_problem_state(ats, ATS_MODIFIED);
4907
4908   /* clean up all plugins, cancel connections and pending transmissions */
4909   while (NULL != (rpos = n->plugins))
4910     {
4911       n->plugins = rpos->next;
4912       rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
4913       while (rpos->addresses != NULL)
4914         {
4915           peer_pos = rpos->addresses;
4916           rpos->addresses = peer_pos->next;
4917           if (peer_pos->connected == GNUNET_YES)
4918             GNUNET_STATISTICS_update (stats,
4919                                       gettext_noop ("# connected addresses"),
4920                                       -1,
4921                                       GNUNET_NO);
4922           if (GNUNET_YES == peer_pos->validated)
4923             GNUNET_STATISTICS_update (stats,
4924                                       gettext_noop ("# peer addresses considered valid"),
4925                                       -1,
4926                                       GNUNET_NO);
4927           if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
4928             {
4929               GNUNET_SCHEDULER_cancel (peer_pos->revalidate_task);
4930               peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
4931             }
4932           GNUNET_free(peer_pos->ressources);
4933           peer_pos->ressources = NULL;
4934           GNUNET_free(peer_pos->quality);
4935           peer_pos->ressources = NULL;
4936           GNUNET_free(peer_pos);
4937         }
4938       GNUNET_free (rpos);
4939     }
4940
4941   /* free all messages on the queue */
4942   while (NULL != (mq = n->messages_head))
4943     {
4944       GNUNET_STATISTICS_update (stats,
4945                                 gettext_noop ("# bytes in message queue for other peers"),
4946                                 - (int64_t) mq->message_buf_size,
4947                                 GNUNET_NO);
4948       GNUNET_STATISTICS_update (stats,
4949                                 gettext_noop ("# bytes discarded due to disconnect"),
4950                                 mq->message_buf_size,
4951                                 GNUNET_NO);
4952       GNUNET_CONTAINER_DLL_remove (n->messages_head,
4953                                    n->messages_tail,
4954                                    mq);
4955       GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4956                                  &n->id,
4957                                  sizeof(struct GNUNET_PeerIdentity)));
4958       GNUNET_free (mq);
4959     }
4960
4961   while (NULL != (mq = n->cont_head))
4962     {
4963
4964       GNUNET_CONTAINER_DLL_remove (n->cont_head,
4965                                    n->cont_tail,
4966                                    mq);
4967       GNUNET_assert (0 == memcmp(&mq->neighbour_id,
4968                                  &n->id,
4969                                  sizeof(struct GNUNET_PeerIdentity)));
4970       GNUNET_free (mq);
4971     }
4972
4973   if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
4974     {
4975       GNUNET_SCHEDULER_cancel (n->timeout_task);
4976       n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4977     }
4978   if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
4979     {
4980       GNUNET_SCHEDULER_cancel (n->retry_task);
4981       n->retry_task = GNUNET_SCHEDULER_NO_TASK;
4982     }
4983   if (n->piter != NULL)
4984     {
4985       GNUNET_PEERINFO_iterate_cancel (n->piter);
4986       GNUNET_STATISTICS_update (stats,
4987                                 gettext_noop ("# outstanding peerinfo iterate requests"),
4988                                 -1,
4989                                 GNUNET_NO);
4990       n->piter = NULL;
4991     }
4992   /* finally, free n itself */
4993   GNUNET_STATISTICS_update (stats,
4994                             gettext_noop ("# active neighbours"),
4995                             -1,
4996                             GNUNET_NO);
4997   GNUNET_free_non_null (n->pre_connect_message_buffer);
4998   GNUNET_free (n);
4999 }
5000
5001
5002 /**
5003  * We have received a PING message from someone.  Need to send a PONG message
5004  * in response to the peer by any means necessary.
5005  */
5006 static int
5007 handle_ping (void *cls, const struct GNUNET_MessageHeader *message,
5008              const struct GNUNET_PeerIdentity *peer,
5009              struct Session *session,
5010              const char *sender_address,
5011              uint16_t sender_address_len)
5012 {
5013   struct TransportPlugin *plugin = cls;
5014   struct SessionHeader *session_header = (struct SessionHeader*) session;
5015   struct TransportPingMessage *ping;
5016   struct TransportPongMessage *pong;
5017   struct NeighbourList *n;
5018   struct ReadyList *rl;
5019   struct ForeignAddressList *fal;
5020   struct OwnAddressList *oal;
5021   const char *addr;
5022   size_t alen;
5023   size_t slen;
5024   int did_pong;
5025
5026   if (ntohs (message->size) < sizeof (struct TransportPingMessage))
5027     {
5028       GNUNET_break_op (0);
5029       return GNUNET_SYSERR;
5030     }
5031
5032   ping = (struct TransportPingMessage *) message;
5033   if (0 != memcmp (&ping->target,
5034                    plugin->env.my_identity,
5035                    sizeof (struct GNUNET_PeerIdentity)))
5036     {
5037 #if DEBUG_TRANSPORT
5038       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5039                   _("Received `%s' message from `%s' destined for `%s' which is not me!\n"),
5040                   "PING",
5041                   (sender_address != NULL)
5042                   ? a2s (plugin->short_name,
5043                          (const struct sockaddr *)sender_address,
5044                          sender_address_len)
5045                   : "<inbound>",
5046                   GNUNET_i2s (&ping->target));
5047 #endif
5048       return GNUNET_SYSERR;
5049     }
5050 #if DEBUG_PING_PONG
5051   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
5052               "Processing `%s' from `%s'\n",
5053               "PING",
5054               (sender_address != NULL)
5055               ? a2s (plugin->short_name,
5056                      (const struct sockaddr *)sender_address,
5057                      sender_address_len)
5058               : "<inbound>");
5059 #endif
5060   GNUNET_STATISTICS_update (stats,
5061                             gettext_noop ("# PING messages received"),
5062                             1,
5063                             GNUNET_NO);
5064   addr = (const char*) &ping[1];
5065   alen = ntohs (message->size) - sizeof (struct TransportPingMessage);
5066   slen = strlen (plugin->short_name) + 1;
5067   if (alen == 0)
5068     {
5069       /* peer wants to confirm that we have an outbound connection to him */
5070       if (session == NULL)
5071         {
5072           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5073                       _("Refusing to create PONG since I do not have a session with `%s'.\n"),
5074                       GNUNET_i2s (peer));
5075           return GNUNET_SYSERR;
5076         }
5077       /* FIXME-urg: the use of 'sender_address' in the code below is doubly-wrong:
5078         1) it is NULL when we need to have a real value
5079         2) it is documented to be the address of the sender (source-IP), where
5080         what we actually want is our LISTEN IP (what we 'bound' to); which we don't even
5081         have...
5082       */
5083       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5084                   "Creating PONG indicating that we received a connection at our address `%s' from `%s'.\n",
5085                   a2s (plugin->short_name,
5086                        sender_address,
5087                        sender_address_len),                    
5088                   GNUNET_i2s (peer));
5089
5090       pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5091       pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len + slen);
5092       pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5093       pong->purpose.size =
5094         htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5095                sizeof (uint32_t) +
5096                sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5097                sizeof (struct GNUNET_PeerIdentity) + sender_address_len + slen);
5098       pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_USING);
5099       pong->challenge = ping->challenge;
5100       pong->addrlen = htonl(sender_address_len + slen);
5101       memcpy(&pong->pid,
5102              peer,
5103              sizeof(struct GNUNET_PeerIdentity));
5104       memcpy (&pong[1],
5105               plugin->short_name,
5106               slen);
5107       if ((sender_address!=NULL) && (sender_address_len > 0))
5108                   memcpy (&((char*)&pong[1])[slen],
5109                           sender_address,
5110                           sender_address_len);
5111       if (GNUNET_TIME_absolute_get_remaining (session_header->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4)
5112         {
5113           /* create / update cached sig */
5114 #if DEBUG_TRANSPORT
5115           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5116                       "Creating PONG signature to indicate active connection.\n");
5117 #endif
5118           session_header->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5119           pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5120           GNUNET_assert (GNUNET_OK ==
5121                          GNUNET_CRYPTO_rsa_sign (my_private_key,
5122                                                  &pong->purpose,
5123                                                  &session_header->pong_signature));
5124         }
5125       else
5126         {
5127           pong->expiration = GNUNET_TIME_absolute_hton (session_header->pong_sig_expires);
5128         }
5129       memcpy (&pong->signature,
5130               &session_header->pong_signature,
5131               sizeof (struct GNUNET_CRYPTO_RsaSignature));
5132
5133
5134     }
5135   else
5136     {
5137       /* peer wants to confirm that this is one of our addresses */
5138       addr += slen;
5139       alen -= slen;
5140       if (GNUNET_OK !=
5141           plugin->api->check_address (plugin->api->cls,
5142                                       addr,
5143                                       alen))
5144         {
5145           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5146                       _("Not confirming PING with address `%s' since I cannot confirm having this address.\n"),
5147                       a2s (plugin->short_name,
5148                            addr,
5149                            alen));
5150           return GNUNET_NO;
5151         }
5152       oal = plugin->addresses;
5153       while (NULL != oal)
5154         {
5155           if ( (oal->addrlen == alen) &&
5156                (0 == memcmp (addr,
5157                              &oal[1],
5158                              alen)) )
5159             break;
5160           oal = oal->next;
5161         }
5162       pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + alen + slen);
5163       pong->header.size = htons (sizeof (struct TransportPongMessage) + alen + slen);
5164       pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
5165       pong->purpose.size =
5166         htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
5167                sizeof (uint32_t) +
5168                sizeof (struct GNUNET_TIME_AbsoluteNBO) +
5169                sizeof (struct GNUNET_PeerIdentity) + alen + slen);
5170       pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN);
5171       pong->challenge = ping->challenge;
5172       pong->addrlen = htonl(alen + slen);
5173       memcpy(&pong->pid,
5174              &my_identity,
5175              sizeof(struct GNUNET_PeerIdentity));
5176       memcpy (&pong[1], plugin->short_name, slen);
5177       memcpy (&((char*)&pong[1])[slen], addr, alen);
5178       if ( (oal != NULL) &&
5179            (GNUNET_TIME_absolute_get_remaining (oal->pong_sig_expires).rel_value < PONG_SIGNATURE_LIFETIME.rel_value / 4) )
5180         {
5181           /* create / update cached sig */
5182 #if DEBUG_TRANSPORT
5183           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5184                       "Creating PONG signature to indicate ownership.\n");
5185 #endif
5186           oal->pong_sig_expires = GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME);
5187           pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5188           GNUNET_assert (GNUNET_OK ==
5189                          GNUNET_CRYPTO_rsa_sign (my_private_key,
5190                                                  &pong->purpose,
5191                                                  &oal->pong_signature));
5192           memcpy (&pong->signature,
5193                   &oal->pong_signature,
5194                   sizeof (struct GNUNET_CRYPTO_RsaSignature));
5195         }
5196       else if (oal == NULL)
5197         {
5198           /* not using cache (typically DV-only) */
5199           pong->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (PONG_SIGNATURE_LIFETIME));
5200           GNUNET_assert (GNUNET_OK ==
5201                          GNUNET_CRYPTO_rsa_sign (my_private_key,
5202                                                  &pong->purpose,
5203                                                  &pong->signature));
5204         }
5205       else
5206         {
5207           /* can used cached version */
5208           pong->expiration = GNUNET_TIME_absolute_hton (oal->pong_sig_expires);
5209           memcpy (&pong->signature,
5210                   &oal->pong_signature,
5211                   sizeof (struct GNUNET_CRYPTO_RsaSignature));
5212         }
5213     }
5214   n = find_neighbour(peer);
5215   GNUNET_assert (n != NULL);
5216   did_pong = GNUNET_NO;
5217   /* first try reliable response transmission */
5218   rl = n->plugins;
5219   while (rl != NULL)
5220     {
5221       fal = rl->addresses;
5222       while (fal != NULL)
5223         {
5224           if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
5225                                            peer,
5226                                            (const char*) pong,
5227                                            ntohs (pong->header.size),
5228                                            TRANSPORT_PONG_PRIORITY,
5229                                            HELLO_VERIFICATION_TIMEOUT,
5230                                            fal->session,
5231                                            fal->addr,
5232                                            fal->addrlen,
5233                                            GNUNET_SYSERR,
5234                                            NULL, NULL))
5235             {
5236               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5237                           "Transmitted PONG to `%s' via reliable mechanism\n",
5238                           GNUNET_i2s (peer));
5239               /* done! */
5240               GNUNET_STATISTICS_update (stats,
5241                                         gettext_noop ("# PONGs unicast via reliable transport"),
5242                                         1,
5243                                         GNUNET_NO);
5244               GNUNET_free (pong);
5245               return GNUNET_OK;
5246             }
5247           did_pong = GNUNET_YES;
5248           fal = fal->next;
5249         }
5250       rl = rl->next;
5251     }
5252   /* no reliable method found, do multicast */
5253   GNUNET_STATISTICS_update (stats,
5254                             gettext_noop ("# PONGs multicast to all available addresses"),
5255                             1,
5256                             GNUNET_NO);
5257   rl = n->plugins;
5258   while (rl != NULL)
5259     {
5260       fal = rl->addresses;
5261       while (fal != NULL)
5262         {
5263           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5264                       "Transmitting PONG to `%s' via unreliable mechanism `%s':%s\n",
5265                       GNUNET_i2s (peer),
5266                       a2s (rl->plugin->short_name,
5267                            fal->addr,
5268                            fal->addrlen),
5269                       rl->plugin->short_name);
5270           transmit_to_peer(NULL, fal,
5271                            TRANSPORT_PONG_PRIORITY,
5272                            HELLO_VERIFICATION_TIMEOUT,
5273                            (const char *)pong,
5274                            ntohs(pong->header.size),
5275                            GNUNET_YES,
5276                            n);
5277           did_pong = GNUNET_YES;
5278           fal = fal->next;
5279         }
5280       rl = rl->next;
5281     }
5282   GNUNET_free(pong);
5283   if (GNUNET_YES != did_pong)
5284     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5285                 _("Could not send PONG to `%s': no address available\n"),
5286                 GNUNET_i2s (peer));
5287   return GNUNET_OK;
5288 }
5289
5290
5291 /**
5292  * Function called by the plugin for each received message.  Update
5293  * data volumes, possibly notify plugins about reducing the rate at
5294  * which they read from the socket and generally forward to our
5295  * receive callback.
5296  *
5297  * @param cls the "struct TransportPlugin *" we gave to the plugin
5298  * @param peer (claimed) identity of the other peer
5299  * @param message the message, NULL if we only care about
5300  *                learning about the delay until we should receive again
5301  * @param ats_data information for automatic transport selection
5302  * @param ats_count number of elements in ats not including 0-terminator
5303  * @param session identifier used for this session (can be NULL)
5304  * @param sender_address binary address of the sender (if observed)
5305  * @param sender_address_len number of bytes in sender_address
5306  * @return how long in ms the plugin should wait until receiving more data
5307  *         (plugins that do not support this, can ignore the return value)
5308  */
5309 static struct GNUNET_TIME_Relative
5310 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
5311                     const struct GNUNET_MessageHeader *message,
5312                     const struct GNUNET_TRANSPORT_ATS_Information *ats_data,
5313                     uint32_t ats_count,
5314                     struct Session *session,
5315                     const char *sender_address,
5316                     uint16_t sender_address_len)
5317 {
5318   struct TransportPlugin *plugin = cls;
5319   struct ReadyList *service_context;
5320   struct ForeignAddressList *peer_address;
5321   uint16_t msize;
5322   struct NeighbourList *n;
5323   struct GNUNET_TIME_Relative ret;
5324   uint32_t distance;
5325   int c;
5326
5327   if (0 == memcmp (peer,
5328                    &my_identity,
5329                    sizeof (struct GNUNET_PeerIdentity)))
5330     {
5331       /* refuse to receive from myself */
5332       GNUNET_break (0); 
5333       return GNUNET_TIME_UNIT_FOREVER_REL;
5334     }
5335   if (is_blacklisted (peer, plugin))
5336     return GNUNET_TIME_UNIT_FOREVER_REL;
5337   n = find_neighbour (peer);
5338   if (n == NULL)
5339     n = setup_new_neighbour (peer, GNUNET_YES);
5340   service_context = n->plugins;
5341   while ((service_context != NULL) && (plugin != service_context->plugin))
5342     service_context = service_context->next;
5343   GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
5344   peer_address = NULL;
5345   distance = 1;
5346
5347   for (c=0; c<ats_count; c++)
5348     if (ntohl(ats_data[c].type) == GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE)
5349       distance = ntohl(ats_data[c].value);
5350
5351
5352   if (message != NULL)
5353     {
5354       if ( (session != NULL) ||
5355            (sender_address != NULL) )
5356         peer_address = add_peer_address (n,
5357                                          plugin->short_name,
5358                                          session,
5359                                          sender_address,
5360                                          sender_address_len);
5361       if (peer_address != NULL)
5362         {
5363           update_addr_ats(peer_address, ats_data, ats_count);
5364           update_addr_value(peer_address, distance, GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5365           
5366           peer_address->distance = distance;
5367           if (GNUNET_YES == peer_address->validated)
5368             mark_address_connected (peer_address);
5369           else
5370           {
5371 #if DEBUG_TRANSPORT
5372             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5373                         "New address is unvalidated, trying to validate it now\n");
5374 #endif
5375              if (peer_address->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
5376              {
5377                GNUNET_SCHEDULER_cancel (peer_address->revalidate_task);
5378                peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
5379              }
5380              GNUNET_SCHEDULER_add_now (send_periodic_ping, peer_address);
5381           }
5382           peer_address->timeout
5383             = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5384           schedule_next_ping (peer_address);
5385         }
5386       /* update traffic received amount ... */
5387       msize = ntohs (message->size);
5388
5389       GNUNET_STATISTICS_update (stats,
5390                                 gettext_noop ("# bytes received from other peers"),
5391                                 msize,
5392                                 GNUNET_NO);
5393       n->distance = distance;
5394       n->peer_timeout =
5395         GNUNET_TIME_relative_to_absolute
5396         (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
5397       GNUNET_SCHEDULER_cancel (n->timeout_task);
5398       n->timeout_task =
5399         GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
5400                                       &neighbour_timeout_task, n);
5401       if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
5402         {
5403           /* dropping message due to frequent inbound volume violations! */
5404           GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
5405                       GNUNET_ERROR_TYPE_BULK,
5406                       _
5407                       ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"),
5408                       n->in_tracker.available_bytes_per_s__,
5409                       n->quota_violation_count);
5410           GNUNET_STATISTICS_update (stats,
5411                                     gettext_noop ("# bandwidth quota violations by other peers"),
5412                                     1,
5413                                     GNUNET_NO);
5414           return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
5415         }
5416     if ((ntohs(message->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ATS) &&
5417         (ntohs(message->size) == (sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t))))
5418       {
5419         uint32_t value =  ntohl(*((uint32_t *) &message[1]));
5420         //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GNUNET_MESSAGE_TYPE_TRANSPORT_ATS: %i \n", value);
5421         /* Force ressource and quality update */
5422         if ((value == 4) && (ats != NULL))
5423             ats_modify_problem_state(ats, ATS_QUALITY_COST_UPDATED);
5424         /* Force cost update */
5425         if ((value == 3) && (ats != NULL))
5426           ats_modify_problem_state(ats, ATS_COST_UPDATED);
5427         /* Force quality update */
5428         if ((value == 2) && (ats != NULL))
5429           ats_modify_problem_state(ats, ATS_QUALITY_UPDATED);
5430         /* Force full rebuild */
5431         if ((value == 1) && (ats != NULL))
5432           ats_modify_problem_state(ats, ATS_MODIFIED);
5433       }
5434     
5435 #if DEBUG_PING_PONG
5436     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5437                 "Received message of type %u and size %u from `%4s', sending to all clients.\n",
5438                 ntohs (message->type),
5439                 ntohs (message->size),
5440                 GNUNET_i2s (peer));
5441 #endif
5442       switch (ntohs (message->type))
5443         {
5444         case GNUNET_MESSAGE_TYPE_HELLO:
5445           GNUNET_STATISTICS_update (stats,
5446                                     gettext_noop ("# HELLO messages received from other peers"),
5447                                     1,
5448                                     GNUNET_NO);
5449           process_hello (plugin, message);
5450           break;
5451         case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
5452           handle_ping (plugin, message, peer, session, sender_address, sender_address_len);
5453           if (! n->received_pong)
5454             transmit_plain_ping (n);
5455           break;
5456         case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
5457           handle_pong (plugin, message, peer, sender_address, sender_address_len);
5458           break;
5459         case GNUNET_MESSAGE_TYPE_TRANSPORT_ATS:
5460           break;
5461         default:
5462           handle_payload_message (message, n);
5463           break;
5464         }
5465     }
5466   ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
5467   if (ret.rel_value > 0)
5468     {
5469 #if DEBUG_TRANSPORT 
5470       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5471                   "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
5472                   (unsigned long long) n->in_tracker.consumption_since_last_update__,
5473                   (unsigned int) n->in_tracker.available_bytes_per_s__,
5474                   (unsigned long long) ret.rel_value);
5475 #endif
5476       GNUNET_STATISTICS_update (stats,
5477                                 gettext_noop ("# ms throttling suggested"),
5478                                 (int64_t) ret.rel_value,
5479                                 GNUNET_NO);
5480     }
5481   return ret;
5482 }
5483
5484 /**
5485  * Handle START-message.  This is the first message sent to us
5486  * by any client which causes us to add it to our list.
5487  *
5488  * @param cls closure (always NULL)
5489  * @param client identification of the client
5490  * @param message the actual message
5491  */
5492 static void
5493 handle_start (void *cls,
5494               struct GNUNET_SERVER_Client *client,
5495               const struct GNUNET_MessageHeader *message)
5496 {
5497   const struct StartMessage *start;
5498   struct TransportClient *c;
5499   struct ConnectInfoMessage * cim;
5500   struct NeighbourList *n;
5501   uint32_t ats_count;
5502   size_t size;
5503
5504   start = (const struct StartMessage*) message;
5505 #if DEBUG_TRANSPORT
5506   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5507               "Received `%s' request from client\n", "START");
5508 #endif
5509   c = clients;
5510   while (c != NULL)
5511     {
5512       if (c->client == client)
5513         {
5514           /* client already on our list! */
5515           GNUNET_break (0);
5516           GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5517           return;
5518         }
5519       c = c->next;
5520     }
5521   if ( (GNUNET_NO != ntohl (start->do_check)) &&
5522        (0 != memcmp (&start->self,
5523                      &my_identity,
5524                      sizeof (struct GNUNET_PeerIdentity))) )
5525     {
5526       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5527                   _("Rejecting control connection from peer `%s', which is not me!\n"),
5528                   GNUNET_i2s (&start->self));
5529       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5530       return;
5531     }
5532   c = GNUNET_malloc (sizeof (struct TransportClient));
5533   c->next = clients;
5534   clients = c;
5535   c->client = client;
5536   if (our_hello != NULL)
5537     {
5538 #if DEBUG_TRANSPORT
5539       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5540                   "Sending our own `%s' to new client\n", "HELLO");
5541 #endif
5542       transmit_to_client (c,
5543                           (const struct GNUNET_MessageHeader *) our_hello,
5544                           GNUNET_NO);
5545       /* tell new client about all existing connections */
5546       ats_count = 2;
5547       size  = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
5548       if (size > GNUNET_SERVER_MAX_MESSAGE_SIZE)
5549         {
5550           GNUNET_break(0);
5551         }
5552       cim = GNUNET_malloc (size);
5553       cim->header.size = htons (size);
5554       cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
5555       cim->ats_count = htonl(ats_count);
5556       (&(cim->ats))[2].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
5557       (&(cim->ats))[2].value = htonl (0);
5558       n = neighbours;
5559       while (n != NULL)
5560         {
5561           if (GNUNET_YES == n->received_pong)
5562             {
5563               (&(cim->ats))[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
5564               (&(cim->ats))[0].value = htonl (n->distance);
5565               (&(cim->ats))[1].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DELAY);
5566               (&(cim->ats))[1].value = htonl ((uint32_t) n->latency.rel_value);
5567               cim->id = n->id;
5568               transmit_to_client (c, &cim->header, GNUNET_NO);
5569             }
5570           n = n->next;
5571         }
5572       GNUNET_free (cim);
5573     }
5574   else
5575     {
5576 #if DEBUG_TRANSPORT_HELLO
5577       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5578                   "No HELLO created yet, will transmit HELLO to client later!\n");
5579 #endif
5580       refresh_hello ();
5581     }
5582   GNUNET_SERVER_receive_done (client, GNUNET_OK);
5583 }
5584
5585
5586 /**
5587  * Handle HELLO-message.
5588  *
5589  * @param cls closure (always NULL)
5590  * @param client identification of the client
5591  * @param message the actual message
5592  */
5593 static void
5594 handle_hello (void *cls,
5595               struct GNUNET_SERVER_Client *client,
5596               const struct GNUNET_MessageHeader *message)
5597 {
5598   int ret;
5599
5600   GNUNET_STATISTICS_update (stats,
5601                             gettext_noop ("# HELLOs received from clients"),
5602                             1,
5603                             GNUNET_NO);
5604   ret = process_hello (NULL, message);
5605   GNUNET_SERVER_receive_done (client, ret);
5606 }
5607
5608
5609 /**
5610  * Closure for 'transmit_client_message'; followed by
5611  * 'msize' bytes of the actual message.
5612  */
5613 struct TransmitClientMessageContext
5614 {
5615   /**
5616    * Client on whom's behalf we are sending.
5617    */
5618   struct GNUNET_SERVER_Client *client;
5619
5620   /**
5621    * Timeout for the transmission.
5622    */
5623   struct GNUNET_TIME_Absolute timeout;
5624
5625   /**
5626    * Message priority.
5627    */
5628   uint32_t priority;
5629
5630   /**
5631    * Size of the message in bytes.
5632    */
5633   uint16_t msize;
5634 };
5635
5636
5637 /**
5638  * Schedule transmission of a message we got from a client to a peer.
5639  *
5640  * @param cls the 'struct TransmitClientMessageContext*'
5641  * @param n destination, or NULL on error (in that case, drop the message)
5642  */
5643 static void
5644 transmit_client_message (void *cls,
5645                          struct NeighbourList *n)
5646 {
5647   struct TransmitClientMessageContext *tcmc = cls;
5648   struct TransportClient *tc;
5649
5650   tc = clients;
5651   while ((tc != NULL) && (tc->client != tcmc->client))
5652     tc = tc->next;
5653
5654   if (n != NULL)
5655     {
5656       transmit_to_peer (tc, NULL, tcmc->priority,
5657                         GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
5658                         (char *)&tcmc[1],
5659                         tcmc->msize, GNUNET_NO, n);
5660     }
5661   GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
5662   GNUNET_SERVER_client_drop (tcmc->client);
5663   GNUNET_free (tcmc);
5664 }
5665
5666
5667 /**
5668  * Handle SEND-message.
5669  *
5670  * @param cls closure (always NULL)
5671  * @param client identification of the client
5672  * @param message the actual message
5673  */
5674 static void
5675 handle_send (void *cls,
5676              struct GNUNET_SERVER_Client *client,
5677              const struct GNUNET_MessageHeader *message)
5678 {
5679   const struct OutboundMessage *obm;
5680   const struct GNUNET_MessageHeader *obmm;
5681   struct TransmitClientMessageContext *tcmc;
5682   uint16_t size;
5683   uint16_t msize;
5684
5685   size = ntohs (message->size);
5686   if (size <
5687       sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
5688     {
5689       GNUNET_break (0);
5690       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5691       return;
5692     }
5693   GNUNET_STATISTICS_update (stats,
5694                             gettext_noop ("# payload received for other peers"),
5695                             size,
5696                             GNUNET_NO);
5697   obm = (const struct OutboundMessage *) message;
5698   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
5699   msize = size - sizeof (struct OutboundMessage);
5700 #if DEBUG_TRANSPORT
5701   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5702               "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
5703               "SEND", GNUNET_i2s (&obm->peer),
5704               ntohs (obmm->type),
5705               msize);
5706 #endif
5707   tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
5708   tcmc->client = client;
5709   tcmc->priority = ntohl (obm->priority);
5710   tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
5711   tcmc->msize = msize;
5712   /* FIXME: this memcpy can be up to 7% of our total runtime */
5713   memcpy (&tcmc[1], obmm, msize);
5714   GNUNET_SERVER_client_keep (client);
5715   setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
5716                               &transmit_client_message,
5717                               tcmc);
5718 }
5719
5720
5721 /**
5722  * Handle request connect message
5723  *
5724  * @param cls closure (always NULL)
5725  * @param client identification of the client
5726  * @param message the actual message
5727  */
5728 static void
5729 handle_request_connect (void *cls,
5730                         struct GNUNET_SERVER_Client *client,
5731                         const struct GNUNET_MessageHeader *message)
5732 {
5733   const struct TransportRequestConnectMessage *trcm =
5734     (const struct TransportRequestConnectMessage *) message;
5735
5736   GNUNET_STATISTICS_update (stats,
5737                             gettext_noop ("# REQUEST CONNECT messages received"),
5738                             1,
5739                             GNUNET_NO);
5740 #if DEBUG_TRANSPORT
5741   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 
5742              "Received a request connect message for peer `%s'\n", 
5743              GNUNET_i2s(&trcm->peer));
5744 #endif
5745   setup_peer_check_blacklist (&trcm->peer, GNUNET_YES,
5746                               NULL, NULL);
5747   GNUNET_SERVER_receive_done (client, GNUNET_OK);
5748 }
5749
5750
5751 /**
5752  * Handle SET_QUOTA-message.
5753  *
5754  * @param cls closure (always NULL)
5755  * @param client identification of the client
5756  * @param message the actual message
5757  */
5758 static void
5759 handle_set_quota (void *cls,
5760                   struct GNUNET_SERVER_Client *client,
5761                   const struct GNUNET_MessageHeader *message)
5762 {
5763   const struct QuotaSetMessage *qsm =
5764     (const struct QuotaSetMessage *) message;
5765   struct NeighbourList *n;
5766
5767   GNUNET_STATISTICS_update (stats,
5768                             gettext_noop ("# SET QUOTA messages received"),
5769                             1,
5770                             GNUNET_NO);
5771   n = find_neighbour (&qsm->peer);
5772   if (n == NULL)
5773     {
5774       GNUNET_SERVER_receive_done (client, GNUNET_OK);
5775       GNUNET_STATISTICS_update (stats,
5776                                 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
5777                                 1,
5778                                 GNUNET_NO);
5779       return;
5780     }
5781 #if DEBUG_TRANSPORT 
5782   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5783               "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
5784               "SET_QUOTA",
5785               (unsigned int) ntohl (qsm->quota.value__),
5786               (unsigned int) n->in_tracker.available_bytes_per_s__,
5787               GNUNET_i2s (&qsm->peer));
5788 #endif
5789   GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
5790                                          qsm->quota);
5791   if (0 == ntohl (qsm->quota.value__))
5792     {
5793 #if DEBUG_TRANSPORT
5794       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5795                 "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&n->id),
5796                 "SET_QUOTA");
5797 #endif
5798       GNUNET_STATISTICS_update (stats,
5799                                 gettext_noop ("# disconnects due to quota of 0"),
5800                                 1,
5801                                 GNUNET_NO);
5802       disconnect_neighbour (n, GNUNET_NO);
5803     }
5804   GNUNET_SERVER_receive_done (client, GNUNET_OK);
5805 }
5806
5807
5808 /**
5809  * Take the given address and append it to the set of results sent back to
5810  * the client.
5811  *
5812  * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
5813  * @param address the resolved name, NULL to indicate the last response
5814  */
5815 static void
5816 transmit_address_to_client (void *cls, const char *address)
5817 {
5818   struct GNUNET_SERVER_TransmitContext *tc = cls;
5819   size_t slen;
5820
5821   if (NULL != address)
5822     {
5823       slen = strlen (address) + 1;
5824       GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
5825                                                   GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5826     }
5827   else
5828     {
5829       GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
5830     }
5831 }
5832
5833
5834 /**
5835  * Handle AddressLookup-message.
5836  *
5837  * @param cls closure (always NULL)
5838  * @param client identification of the client
5839  * @param message the actual message
5840  */
5841 static void
5842 handle_address_lookup (void *cls,
5843                        struct GNUNET_SERVER_Client *client,
5844                        const struct GNUNET_MessageHeader *message)
5845 {
5846   const struct AddressLookupMessage *alum;
5847   struct TransportPlugin *lsPlugin;
5848   const char *nameTransport;
5849   const char *address;
5850   uint16_t size;
5851   struct GNUNET_SERVER_TransmitContext *tc;
5852   struct GNUNET_TIME_Absolute timeout;
5853   struct GNUNET_TIME_Relative rtimeout;
5854   int32_t numeric;
5855
5856   size = ntohs (message->size);
5857   if (size < sizeof (struct AddressLookupMessage))
5858     {
5859       GNUNET_break_op (0);
5860       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5861       return;
5862     }
5863   alum = (const struct AddressLookupMessage *) message;
5864   uint32_t addressLen = ntohl (alum->addrlen);
5865   if (size <= sizeof (struct AddressLookupMessage) + addressLen)
5866     {
5867       GNUNET_break_op (0);
5868       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5869       return;
5870     }
5871   address = (const char *) &alum[1];
5872   nameTransport = (const char *) &address[addressLen];
5873   if (nameTransport
5874       [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
5875     {
5876       GNUNET_break_op (0);
5877       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5878       return;
5879     }
5880   timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
5881   rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5882   numeric = ntohl (alum->numeric_only);
5883   lsPlugin = find_transport (nameTransport);
5884   if (NULL == lsPlugin)
5885     {
5886       tc = GNUNET_SERVER_transmit_context_create (client);
5887       GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5888                                                   GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5889       GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5890       return;
5891     }
5892   GNUNET_SERVER_disable_receive_done_warning (client);
5893   tc = GNUNET_SERVER_transmit_context_create (client);
5894   lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
5895                                          nameTransport,
5896                                          address, addressLen,
5897                                          numeric,
5898                                          rtimeout,
5899                                          &transmit_address_to_client, tc);
5900 }
5901
5902 /**
5903  * Handle PeerAddressLookupMessage.
5904  *
5905  * @param cls closure (always NULL)
5906  * @param client identification of the client
5907  * @param message the actual message
5908  */
5909 static void
5910 handle_peer_address_lookup (void *cls,
5911                        struct GNUNET_SERVER_Client *client,
5912                        const struct GNUNET_MessageHeader *message)
5913 {
5914   const struct PeerAddressLookupMessage *peer_address_lookup;
5915   struct NeighbourList *neighbor_iterator;
5916   struct ReadyList *ready_iterator;
5917   struct ForeignAddressList *foreign_address_iterator;
5918   struct TransportPlugin *transport_plugin;
5919
5920   uint16_t size;
5921   struct GNUNET_SERVER_TransmitContext *tc;
5922   struct GNUNET_TIME_Absolute timeout;
5923   struct GNUNET_TIME_Relative rtimeout;
5924   char *addr_buf;
5925
5926   size = ntohs (message->size);
5927   if (size < sizeof (struct PeerAddressLookupMessage))
5928     {
5929       GNUNET_break_op (0);
5930       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5931       return;
5932     }
5933   peer_address_lookup = (const struct PeerAddressLookupMessage *) message;
5934
5935   timeout = GNUNET_TIME_absolute_ntoh (peer_address_lookup->timeout);
5936   rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
5937
5938   neighbor_iterator = neighbours;
5939   while (neighbor_iterator != NULL)
5940     {
5941       if (0 == memcmp(&neighbor_iterator->id, &peer_address_lookup->peer, sizeof(struct GNUNET_PeerIdentity)))
5942         break;
5943       neighbor_iterator = neighbor_iterator->next;
5944     }
5945
5946   /* Found no neighbor matching this peer id (shouldn't be possible, but...) */
5947   if (neighbor_iterator == NULL)
5948     {
5949       GNUNET_break(0);
5950       tc = GNUNET_SERVER_transmit_context_create (client);
5951       GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5952                                                   GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
5953       GNUNET_SERVER_transmit_context_run (tc, rtimeout);
5954       return;
5955     }
5956
5957   ready_iterator = neighbor_iterator->plugins;
5958   GNUNET_SERVER_disable_receive_done_warning (client);
5959   tc = GNUNET_SERVER_transmit_context_create (client);
5960   while(ready_iterator != NULL)
5961     {
5962       foreign_address_iterator = ready_iterator->addresses;
5963       while (foreign_address_iterator != NULL)
5964         {
5965           transport_plugin = foreign_address_iterator->ready_list->plugin;
5966           if (foreign_address_iterator->addr != NULL)
5967             {
5968               GNUNET_asprintf (&addr_buf, "%s --- %s",
5969                                a2s (transport_plugin->short_name,
5970                                     foreign_address_iterator->addr,
5971                                     foreign_address_iterator->addrlen),
5972                                (foreign_address_iterator->connected
5973                                    == GNUNET_YES) ? "CONNECTED"
5974                                    : "DISCONNECTED",
5975                                (foreign_address_iterator->validated
5976                                    == GNUNET_YES) ? "VALIDATED"
5977                                    : "UNVALIDATED");
5978               transmit_address_to_client(tc, addr_buf);
5979               GNUNET_free(addr_buf);
5980             }
5981           else if (foreign_address_iterator->addrlen == 0)
5982             {
5983               GNUNET_asprintf (&addr_buf, "%s --- %s, %s", "<inbound>",
5984                                (foreign_address_iterator->connected
5985                                    == GNUNET_YES) ? "CONNECTED"
5986                                    : "DISCONNECTED",
5987                                (foreign_address_iterator->validated
5988                                    == GNUNET_YES) ? "VALIDATED"
5989                                    : "UNVALIDATED");
5990               transmit_address_to_client (tc, addr_buf);
5991               GNUNET_free(addr_buf);
5992             }
5993
5994           foreign_address_iterator = foreign_address_iterator->next;
5995         }
5996       ready_iterator = ready_iterator->next;
5997     }
5998   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
5999                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
6000   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
6001 }
6002
6003 /**
6004  * Handle AddressIterateMessage
6005  *
6006  * @param cls closure (always NULL)
6007  * @param client identification of the client
6008  * @param message the actual message
6009  */
6010 static void
6011 handle_address_iterate (void *cls,
6012                         struct GNUNET_SERVER_Client *client,
6013                         const struct GNUNET_MessageHeader *message)
6014 {
6015   const struct AddressIterateMessage *address_iterate;
6016   struct NeighbourList *neighbor_iterator;
6017   struct ReadyList *ready_iterator;
6018   struct ForeignAddressList *foreign_address_iterator;
6019   struct TransportPlugin *transport_plugin;
6020
6021   uint16_t size;
6022   struct GNUNET_SERVER_TransmitContext *tc;
6023   struct GNUNET_TIME_Absolute timeout;
6024   struct GNUNET_TIME_Relative rtimeout;
6025   char *addr_buf;
6026
6027   size = ntohs (message->size);
6028   if (size < sizeof (struct AddressIterateMessage))
6029     {
6030       GNUNET_break_op (0);
6031       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6032       return;
6033     }
6034   address_iterate = (const struct AddressIterateMessage *) message;
6035
6036   timeout = GNUNET_TIME_absolute_ntoh (address_iterate->timeout);
6037   rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
6038
6039   GNUNET_SERVER_disable_receive_done_warning (client);
6040   tc = GNUNET_SERVER_transmit_context_create (client);
6041
6042   neighbor_iterator = neighbours;
6043   while (neighbor_iterator != NULL)
6044     {
6045       ready_iterator = neighbor_iterator->plugins;
6046       while (ready_iterator != NULL)
6047         {
6048           foreign_address_iterator = ready_iterator->addresses;
6049           while (foreign_address_iterator != NULL)
6050             {
6051               transport_plugin = foreign_address_iterator->ready_list->plugin;
6052               if (foreign_address_iterator->addr != NULL)
6053                 {
6054                   GNUNET_asprintf (&addr_buf, "%s:%s --- %s, %s",
6055                                    GNUNET_i2s(&neighbor_iterator->id),
6056                                    a2s (transport_plugin->short_name,
6057                                         foreign_address_iterator->addr,
6058                                         foreign_address_iterator->addrlen),
6059                                    (foreign_address_iterator->connected
6060                                        == GNUNET_YES) ? "CONNECTED"
6061                                        : "DISCONNECTED",
6062                                    (foreign_address_iterator->validated
6063                                        == GNUNET_YES) ? "VALIDATED"
6064                                        : "UNVALIDATED");
6065                   transmit_address_to_client (tc, addr_buf);
6066                   GNUNET_free(addr_buf);
6067                 }
6068               else if (foreign_address_iterator->addrlen == 0)
6069                 {
6070                   GNUNET_asprintf (&addr_buf, "%s:%s --- %s, %s",
6071                                      GNUNET_i2s (&neighbor_iterator->id),
6072                                      "<inbound>",
6073                                      (foreign_address_iterator->connected
6074                                          == GNUNET_YES) ? "CONNECTED"
6075                                          : "DISCONNECTED",
6076                                      (foreign_address_iterator->validated
6077                                          == GNUNET_YES) ? "VALIDATED"
6078                                          : "UNVALIDATED");
6079                   transmit_address_to_client (tc, addr_buf);
6080                   GNUNET_free(addr_buf);
6081                 }
6082
6083               foreign_address_iterator = foreign_address_iterator->next;
6084             }
6085           ready_iterator = ready_iterator->next;
6086         }
6087       neighbor_iterator = neighbor_iterator->next;
6088     }
6089
6090   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
6091                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
6092   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
6093 }
6094
6095
6096 /**
6097  * Setup the environment for this plugin.
6098  */
6099 static void
6100 create_environment (struct TransportPlugin *plug)
6101 {
6102   plug->env.cfg = cfg;
6103   plug->env.my_identity = &my_identity;
6104   plug->env.our_hello = &our_hello;
6105   plug->env.cls = plug;
6106   plug->env.receive = &plugin_env_receive;
6107   plug->env.notify_address = &plugin_env_notify_address;
6108   plug->env.session_end = &plugin_env_session_end;
6109   plug->env.max_connections = max_connect_per_transport;
6110   plug->env.stats = stats;
6111 }
6112
6113
6114 /**
6115  * Start the specified transport (load the plugin).
6116  */
6117 static void
6118 start_transport (struct GNUNET_SERVER_Handle *server,
6119                  const char *name)
6120 {
6121   struct TransportPlugin *plug;
6122   char *libname;
6123
6124   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6125               _("Loading `%s' transport plugin\n"), name);
6126   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
6127   plug = GNUNET_malloc (sizeof (struct TransportPlugin));
6128   create_environment (plug);
6129   plug->short_name = GNUNET_strdup (name);
6130   plug->lib_name = libname;
6131   plug->next = plugins;
6132   plugins = plug;
6133   plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
6134   if (plug->api == NULL)
6135     {
6136       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6137                   _("Failed to load transport plugin for `%s'\n"), name);
6138       GNUNET_free (plug->short_name);
6139       plugins = plug->next;
6140       GNUNET_free (libname);
6141       GNUNET_free (plug);
6142     }
6143 }
6144
6145
6146 /**
6147  * Called whenever a client is disconnected.  Frees our
6148  * resources associated with that client.
6149  *
6150  * @param cls closure
6151  * @param client identification of the client
6152  */
6153 static void
6154 client_disconnect_notification (void *cls,
6155                                 struct GNUNET_SERVER_Client *client)
6156 {
6157   struct TransportClient *pos;
6158   struct TransportClient *prev;
6159   struct ClientMessageQueueEntry *mqe;
6160   struct Blacklisters *bl;
6161   struct BlacklistCheck *bc;
6162
6163   if (client == NULL)
6164     return;
6165 #if DEBUG_TRANSPORT
6166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
6167               "Client disconnected, cleaning up.\n");
6168 #endif
6169   /* clean up blacklister */
6170   bl = bl_head;
6171   while (bl != NULL)
6172     {
6173       if (bl->client == client)
6174         {
6175           bc = bc_head;
6176           while (bc != NULL)
6177             {
6178               if (bc->bl_pos == bl)
6179                 {
6180                   bc->bl_pos = bl->next;
6181                   if (bc->th != NULL)
6182                     {
6183                       GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
6184                       bc->th = NULL;
6185                     }
6186                   if (bc->task == GNUNET_SCHEDULER_NO_TASK)
6187                     bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
6188                                                          bc);
6189                   break;
6190                 }
6191               bc = bc->next;
6192             }
6193           GNUNET_CONTAINER_DLL_remove (bl_head,
6194                                        bl_tail,
6195                                        bl);
6196           GNUNET_SERVER_client_drop (bl->client);
6197           GNUNET_free (bl);
6198           break;
6199         }
6200       bl = bl->next;
6201     }
6202   /* clean up 'normal' clients */
6203   prev = NULL;
6204   pos = clients;
6205   while ((pos != NULL) && (pos->client != client))
6206     {
6207       prev = pos;
6208       pos = pos->next;
6209     }
6210   if (pos == NULL)
6211     return;
6212   while (NULL != (mqe = pos->message_queue_head))
6213     {
6214       GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
6215                                    pos->message_queue_tail,
6216                                    mqe);
6217       pos->message_count--;
6218       GNUNET_free (mqe);
6219     }
6220   if (prev == NULL)
6221     clients = pos->next;
6222   else
6223     prev->next = pos->next;
6224   if (GNUNET_YES == pos->tcs_pending)
6225     {
6226       pos->client = NULL;
6227       return;
6228     }
6229   if (pos->th != NULL)
6230     {
6231       GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
6232       pos->th = NULL;
6233     }
6234   GNUNET_break (0 == pos->message_count);
6235   GNUNET_free (pos);
6236 }
6237
6238
6239 /**
6240  * Function called when the service shuts down.  Unloads our plugins
6241  * and cancels pending validations.
6242  *
6243  * @param cls closure, unused
6244  * @param tc task context (unused)
6245  */
6246 static void
6247 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6248 {
6249   struct TransportPlugin *plug;
6250   struct TransportPlugin *tmp;
6251   struct OwnAddressList *al;
6252   struct CheckHelloValidatedContext *chvc;
6253
6254   shutdown_in_progress = GNUNET_YES;
6255   while (neighbours != NULL)
6256     {
6257 #if DEBUG_TRANSPORT
6258       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6259                   "Disconnecting peer `%4s', %s\n", GNUNET_i2s(&neighbours->id),
6260                   "SHUTDOWN_TASK");
6261 #endif
6262       disconnect_neighbour (neighbours, GNUNET_NO);
6263     }
6264 #if DEBUG_TRANSPORT
6265   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6266               "Transport service is unloading plugins...\n");
6267 #endif
6268   plug = plugins;
6269   while (plug != NULL)
6270     {
6271       if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
6272         {
6273           GNUNET_SCHEDULER_cancel (plug->address_update_task);
6274           plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
6275         }
6276       GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
6277       GNUNET_free (plug->lib_name);
6278       GNUNET_free (plug->short_name);
6279       while (NULL != (al = plug->addresses))
6280         {
6281           plug->addresses = al->next;
6282           GNUNET_free (al);
6283         }
6284       tmp = plug->next;
6285       GNUNET_free (plug);
6286       plug = tmp;
6287     }
6288   if (my_private_key != NULL)
6289     GNUNET_CRYPTO_rsa_key_free (my_private_key);
6290   GNUNET_free_non_null (our_hello);
6291
6292   GNUNET_CONTAINER_multihashmap_iterate (validation_map,
6293                                          &abort_validation,
6294                                          NULL);
6295   GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6296   validation_map = NULL;
6297
6298
6299   if (ats_task != GNUNET_SCHEDULER_NO_TASK)
6300   {
6301     GNUNET_SCHEDULER_cancel(ats_task);
6302     ats_task = GNUNET_SCHEDULER_NO_TASK;
6303   }
6304   if (ats != NULL)
6305     ats_shutdown (ats);
6306
6307   /* free 'chvc' data structure */
6308   while (NULL != (chvc = chvc_head))
6309     {
6310       chvc_head = chvc->next;
6311       if (chvc->piter != NULL)
6312         {
6313           GNUNET_PEERINFO_iterate_cancel (chvc->piter);
6314           GNUNET_STATISTICS_update (stats,
6315                                     gettext_noop ("# outstanding peerinfo iterate requests"),
6316                                     -1,
6317                                     GNUNET_NO);
6318           chvc->ve_count --;
6319         }
6320       else
6321           GNUNET_break (0);
6322       GNUNET_assert (chvc->ve_count == 0);
6323       GNUNET_free (chvc);
6324     }
6325   chvc_tail = NULL;
6326
6327   if (stats != NULL)
6328     {
6329       GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6330       stats = NULL;
6331     }
6332   if (peerinfo != NULL)
6333     {
6334       GNUNET_PEERINFO_disconnect (peerinfo);
6335       peerinfo = NULL;
6336     }
6337   if (GNUNET_SCHEDULER_NO_TASK != hello_task)
6338     {
6339       GNUNET_SCHEDULER_cancel (hello_task);
6340       hello_task = GNUNET_SCHEDULER_NO_TASK;
6341     }
6342   /* Can we assume those are gone by now, or do we need to clean up
6343      explicitly!? */
6344   GNUNET_break (bl_head == NULL);
6345   GNUNET_break (bc_head == NULL);
6346 }
6347
6348
6349 void ats_result_cb ()
6350 {
6351   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6352       "ATS Result callback\n");
6353 }
6354
6355
6356 void create_ats_information ( struct ATS_peer **p,
6357                               int * c_p,
6358                               struct ATS_mechanism ** m,
6359                               int * c_m )
6360 {
6361 #if VERBOSE_ATS
6362   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6363       "ATS requires clean address information\n");
6364 #endif
6365   struct ATS_mechanism * mechanisms;
6366   struct ATS_peer *peers;
6367
6368   int connected_addresses = 0;
6369   int c_peers = 0;
6370   int c_mechs = 0;
6371   struct NeighbourList *next = neighbours;
6372
6373   while (next!=NULL)
6374   {
6375     int found_addresses = GNUNET_NO;
6376     struct ReadyList *r_next = next->plugins;
6377     while (r_next != NULL)
6378     {
6379         struct ForeignAddressList * a_next = r_next->addresses;
6380         while (a_next != NULL)
6381         {
6382             c_mechs++;
6383             found_addresses = GNUNET_YES;
6384             a_next = a_next->next;
6385         }
6386         r_next = r_next->next;
6387     }
6388     if (found_addresses) c_peers++;
6389     next = next->next;
6390   }
6391
6392 #if VERBOSE_ATS
6393   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6394       "Found %u peers with % u transport mechanisms\n", c_peers, c_mechs);
6395 #endif
6396
6397   if ((c_peers == 0) && (c_mechs == 0))
6398   {
6399     peers = NULL;
6400     (*c_p) = 0;
6401     mechanisms = NULL;
6402     (*c_m) = 0;
6403     return;
6404   }
6405
6406   mechanisms = GNUNET_malloc((1+c_mechs) * sizeof (struct ATS_mechanism));
6407   peers =  GNUNET_malloc((1+c_peers) * sizeof (struct ATS_peer));
6408
6409   c_mechs = 1;
6410   c_peers = 1;
6411
6412   next = neighbours;
6413   while (next!=NULL)
6414   {
6415     int found_addresses = GNUNET_NO;
6416     struct ReadyList *r_next = next->plugins;
6417     while (r_next != NULL)
6418     {
6419         struct ForeignAddressList * a_next = r_next->addresses;
6420         while (a_next != NULL)
6421         {
6422             if (a_next->connected == GNUNET_YES)
6423               connected_addresses ++;
6424             if (found_addresses == GNUNET_NO)
6425             {
6426               peers[c_peers].peer = next->id;
6427               peers[c_peers].m_head = NULL;
6428               peers[c_peers].m_tail = NULL;
6429               peers[c_peers].f = 1.0 / c_mechs;
6430             }
6431
6432             mechanisms[c_mechs].addr = a_next;
6433             mechanisms[c_mechs].col_index = c_mechs;
6434             mechanisms[c_mechs].peer = &peers[c_peers];
6435             mechanisms[c_mechs].next = NULL;
6436             mechanisms[c_mechs].plugin = r_next->plugin;
6437             mechanisms[c_mechs].ressources = a_next->ressources;
6438             mechanisms[c_mechs].quality = a_next->quality;
6439
6440             GNUNET_CONTAINER_DLL_insert_tail(peers[c_peers].m_head,
6441                                              peers[c_peers].m_tail,
6442                                              &mechanisms[c_mechs]);
6443             found_addresses = GNUNET_YES;
6444             c_mechs++;
6445
6446             a_next = a_next->next;
6447         }
6448         r_next = r_next->next;
6449     }
6450     if (found_addresses == GNUNET_YES)
6451         c_peers++;
6452     next = next->next;
6453   }
6454   c_mechs--;
6455   c_peers--;
6456   (*c_m) = c_mechs;
6457   (*c_p) = c_peers;
6458   (*p) = peers;
6459   (*m) = mechanisms;
6460
6461   GNUNET_STATISTICS_set(stats,
6462                         gettext_noop ("# connected addresses"),
6463                         connected_addresses,
6464                         GNUNET_NO);
6465 }
6466
6467 static void
6468 schedule_ats (void *cls,
6469               const struct GNUNET_SCHEDULER_TaskContext *tc)
6470 {
6471   struct ATS_Handle *ats = (struct ATS_Handle *) cls;
6472   if (ats==NULL)
6473     return;
6474
6475   ats_task = GNUNET_SCHEDULER_NO_TASK;
6476   if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
6477       return;
6478
6479   if (shutdown_in_progress == GNUNET_YES)
6480           return;
6481
6482   struct GNUNET_TIME_Relative delta =
6483       GNUNET_TIME_absolute_get_difference (last_ats_execution, GNUNET_TIME_absolute_get());
6484   if (delta.rel_value < ats_minimum_interval.rel_value)
6485   {
6486 #if DEBUG_ATS
6487     GNUNET_log (GNUNET_ERROR_TYPE_BULK,
6488         "Minimum time between cycles not reached\n");
6489 #endif
6490     return;
6491   }
6492
6493 #if DEBUG_ATS
6494   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running scheduled calculation\n");
6495 #endif
6496   ats_calculate_bandwidth_distribution (ats, stats);
6497   last_ats_execution = GNUNET_TIME_absolute_get();
6498
6499   ats_task = GNUNET_SCHEDULER_add_delayed (ats_regular_interval,
6500                                   &schedule_ats, ats);
6501 }
6502
6503 struct ForeignAddressList * get_preferred_ats_address (
6504                 struct NeighbourList *n)
6505 {
6506   // TODO get ATS prefered address
6507   return find_ready_address(n);
6508 }
6509
6510 /**
6511  * Initiate transport service.
6512  *
6513  * @param cls closure
6514  * @param server the initialized server
6515  * @param c configuration to use
6516  */
6517 static void
6518 run (void *cls,
6519      struct GNUNET_SERVER_Handle *server,
6520      const struct GNUNET_CONFIGURATION_Handle *c)
6521 {
6522   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
6523     {&handle_start, NULL,
6524      GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
6525     {&handle_hello, NULL,
6526      GNUNET_MESSAGE_TYPE_HELLO, 0},
6527     {&handle_send, NULL,
6528      GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
6529     {&handle_request_connect, NULL,
6530      GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof(struct TransportRequestConnectMessage)},
6531     {&handle_set_quota, NULL,
6532      GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
6533     {&handle_address_lookup, NULL,
6534      GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
6535      0},
6536     {&handle_peer_address_lookup, NULL,
6537      GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP,
6538      0},
6539     {&handle_address_iterate, NULL,
6540      GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE,
6541      0},
6542     {&handle_blacklist_init, NULL,
6543      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
6544     {&handle_blacklist_reply, NULL,
6545      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
6546     {NULL, NULL, 0, 0}
6547   };
6548   char *plugs;
6549   char *pos;
6550   int no_transports;
6551   unsigned long long tneigh;
6552   char *keyfile;
6553
6554   shutdown_in_progress = GNUNET_NO;
6555   cfg = c;
6556   stats = GNUNET_STATISTICS_create ("transport", cfg);
6557   validation_map = GNUNET_CONTAINER_multihashmap_create (64);
6558   /* parse configuration */
6559   if ((GNUNET_OK !=
6560        GNUNET_CONFIGURATION_get_value_number (c,
6561                                               "TRANSPORT",
6562                                               "NEIGHBOUR_LIMIT",
6563                                               &tneigh)) ||
6564       (GNUNET_OK !=
6565        GNUNET_CONFIGURATION_get_value_filename (c,
6566                                                 "GNUNETD",
6567                                                 "HOSTKEY", &keyfile)))
6568     {
6569       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6570                   _
6571                   ("Transport service is lacking key configuration settings.  Exiting.\n"));
6572       GNUNET_SCHEDULER_shutdown ();
6573       if (stats != NULL)
6574         {
6575           GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6576           stats = NULL;
6577         }
6578       GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6579       validation_map = NULL;
6580       return;
6581     }
6582
6583   max_connect_per_transport = (uint32_t) tneigh;
6584   peerinfo = GNUNET_PEERINFO_connect (cfg);
6585   if (peerinfo == NULL)
6586     {
6587       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6588                   _("Could not access PEERINFO service.  Exiting.\n"));
6589       GNUNET_SCHEDULER_shutdown ();
6590       if (stats != NULL)
6591         {
6592           GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6593           stats = NULL;
6594         }
6595       GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6596       validation_map = NULL;
6597       GNUNET_free (keyfile);
6598       return;
6599     }
6600   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
6601   GNUNET_free (keyfile);
6602   if (my_private_key == NULL)
6603     {
6604       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6605                   _
6606                   ("Transport service could not access hostkey.  Exiting.\n"));
6607       GNUNET_SCHEDULER_shutdown ();
6608       if (stats != NULL)
6609         {
6610           GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
6611           stats = NULL;
6612         }
6613       GNUNET_CONTAINER_multihashmap_destroy (validation_map);
6614       validation_map = NULL;
6615       return;
6616     }
6617   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
6618   GNUNET_CRYPTO_hash (&my_public_key,
6619                       sizeof (my_public_key), &my_identity.hashPubKey);
6620   /* setup notification */
6621   GNUNET_SERVER_disconnect_notify (server,
6622                                    &client_disconnect_notification, NULL);
6623   /* load plugins... */
6624   no_transports = 1;
6625   if (GNUNET_OK ==
6626       GNUNET_CONFIGURATION_get_value_string (c,
6627                                              "TRANSPORT", "PLUGINS", &plugs))
6628     {
6629       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6630                   _("Starting transport plugins `%s'\n"), plugs);
6631       pos = strtok (plugs, " ");
6632       while (pos != NULL)
6633         {
6634           start_transport (server, pos);
6635           no_transports = 0;
6636           pos = strtok (NULL, " ");
6637         }
6638       GNUNET_free (plugs);
6639     }
6640   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
6641                                 &shutdown_task, NULL);
6642   if (no_transports)
6643     refresh_hello ();
6644
6645   /* Initializing ATS */
6646   int co;
6647   char * section;
6648   unsigned long long  value;
6649
6650   double D = 1.0;
6651   double U = 1.0;
6652   double R = 1.0;
6653   int v_b_min = 64000;
6654   int v_n_min = 5;
6655
6656   ats_minimum_interval = ATS_MIN_INTERVAL;
6657   ats_regular_interval = ATS_EXEC_INTERVAL;
6658
6659   /* loading cost ressources */
6660   for (co=0; co<available_ressources; co++)
6661   {
6662     GNUNET_asprintf(&section,"%s_UP",ressources[co].cfg_param);
6663     if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6664     {
6665       if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg,
6666           "transport",
6667           section,
6668           &value))
6669       {
6670 #if DEBUG_ATS
6671               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6672                   "Found ressource cost: [%s] = %llu\n",
6673                   section, value);
6674 #endif
6675               ressources[co].c_max = value;
6676       }
6677     }
6678     GNUNET_free (section);
6679     GNUNET_asprintf(&section,"%s_DOWN",ressources[co].cfg_param);
6680     if (GNUNET_CONFIGURATION_have_value(cfg, "transport", section))
6681     {
6682       if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg,
6683           "transport",
6684           section,
6685           &value))
6686       {
6687 #if DEBUG_ATS
6688               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6689                   "Found ressource cost: [%s] = %llu\n",
6690                   section, value);
6691 #endif
6692               ressources[co].c_min = value;
6693       }
6694     }
6695     GNUNET_free (section);
6696   }
6697
6698   ats = ats_init (D, U, R, v_b_min, v_n_min,
6699                   ATS_MAX_ITERATIONS, ATS_MAX_EXEC_DURATION,
6700                   create_ats_information,
6701                   ats_result_cb);
6702
6703   int log_problem = GNUNET_NO;
6704   int log_solution = GNUNET_NO;
6705   int overwrite_dump = GNUNET_NO;
6706   int minimum_peers = 0;
6707   int minimum_addresses = 0;
6708
6709   if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MLP"))
6710     log_problem = GNUNET_CONFIGURATION_get_value_yesno (cfg,
6711                              "transport","DUMP_MLP");
6712
6713   if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_SOLUTION"))
6714     log_solution = GNUNET_CONFIGURATION_get_value_yesno (cfg,
6715                                   "transport","DUMP_SOLUTION");
6716   if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_OVERWRITE"))
6717     overwrite_dump = GNUNET_CONFIGURATION_get_value_yesno (cfg,
6718                                   "transport","DUMP_OVERWRITE");
6719   if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "DUMP_MIN_PEERS"))
6720   {
6721           GNUNET_CONFIGURATION_get_value_number(cfg,
6722               "transport","DUMP_MIN_PEERS", &value);
6723           minimum_peers = value;
6724   }
6725   if (GNUNET_CONFIGURATION_have_value(cfg,
6726       "transport", "DUMP_MIN_ADDRS"))
6727   {
6728       GNUNET_CONFIGURATION_get_value_number(cfg,
6729         "transport","DUMP_MIN_ADDRS", &value);
6730       minimum_addresses= value;
6731   }
6732   if (GNUNET_CONFIGURATION_have_value(cfg,
6733       "transport", "DUMP_OVERWRITE"))
6734   {
6735       GNUNET_CONFIGURATION_get_value_number(cfg,
6736           "transport","DUMP_OVERWRITE", &value);
6737       overwrite_dump = value;
6738   }
6739
6740   if (GNUNET_CONFIGURATION_have_value(cfg,
6741       "transport", "ATS_MIN_INTERVAL"))
6742   {
6743       GNUNET_CONFIGURATION_get_value_number(cfg,
6744           "transport","ATS_MIN_INTERVAL", &value);
6745       ats_minimum_interval.rel_value = value;
6746   }
6747
6748   if (GNUNET_CONFIGURATION_have_value(cfg,
6749       "transport", "ATS_EXEC_INTERVAL"))
6750   {
6751       GNUNET_CONFIGURATION_get_value_number(cfg,
6752           "transport","ATS_EXEC_INTERVAL", &value);
6753       ats_regular_interval.rel_value = value;
6754   }
6755   if (GNUNET_CONFIGURATION_have_value(cfg, "transport", "ATS_MIN_INTERVAL"))
6756   {
6757       GNUNET_CONFIGURATION_get_value_number(cfg,
6758           "transport","ATS_MIN_INTERVAL", &value);
6759       ats_minimum_interval.rel_value = value;
6760   }
6761
6762   ats_set_logging_options (ats,
6763                           minimum_addresses,
6764                           minimum_peers,
6765                           overwrite_dump,
6766                           log_solution,
6767                           log_problem);
6768
6769   if (ats != NULL)
6770     ats_task = GNUNET_SCHEDULER_add_now (&schedule_ats, ats);
6771
6772
6773
6774
6775 #if DEBUG_TRANSPORT
6776   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
6777               _("Transport service ready.\n"));
6778 #endif
6779   /* If we have a blacklist file, read from it */
6780   read_blacklist_file(cfg);
6781   /* process client requests */
6782   GNUNET_SERVER_add_handlers (server, handlers);
6783 }
6784
6785
6786 /**
6787  * The main function for the transport service.
6788  *
6789  * @param argc number of arguments from the command line
6790  * @param argv command line arguments
6791  * @return 0 ok, 1 on error
6792  */
6793 int
6794 main (int argc, char *const *argv)
6795 {
6796   a2s (NULL, NULL, 0); /* make compiler happy */
6797   return (GNUNET_OK ==
6798           GNUNET_SERVICE_run (argc,
6799                               argv,
6800                               "transport",
6801                               GNUNET_SERVICE_OPTION_NONE,
6802                               &run, NULL)) ? 0 : 1;
6803 }
6804
6805 /* end of gnunet-service-transport.c */