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