fixing major issue with how IP addresses go over the network (previously ill-defined...
[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 2, 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  * NOTE:
27  * - This code uses 'GNUNET_a2s' for debug printing in many places,
28  *   which is technically wrong since it assumes we have IP+Port 
29  *   (v4/v6) addresses.  Once we add transports like http or smtp
30  *   this will have to be changed!
31  * - Already wrong with dv.
32  */
33 #include "platform.h"
34 #include "gnunet_client_lib.h"
35 #include "gnunet_container_lib.h"
36 #include "gnunet_constants.h"
37 #include "gnunet_getopt_lib.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_os_lib.h"
40 #include "gnunet_peerinfo_service.h"
41 #include "gnunet_plugin_lib.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_service_lib.h"
44 #include "gnunet_signatures.h"
45 #include "plugin_transport.h"
46 #include "transport.h"
47
48 #define DEBUG_BLACKLIST GNUNET_YES
49
50 #define DEBUG_PING_PONG GNUNET_YES
51
52 /**
53  * Should we do some additional checks (to validate behavior
54  * of clients)?
55  */
56 #define EXTRA_CHECKS GNUNET_YES
57
58 /**
59  * How many messages can we have pending for a given client process
60  * before we start to drop incoming messages?  We typically should
61  * have only one client and so this would be the primary buffer for
62  * messages, so the number should be chosen rather generously.
63  *
64  * The expectation here is that most of the time the queue is large
65  * enough so that a drop is virtually never required.
66  */
67 #define MAX_PENDING 128
68
69 /**
70  * Size of the per-transport blacklist hash maps.
71  */
72 #define TRANSPORT_BLACKLIST_HT_SIZE 16
73
74 /**
75  * How often should we try to reconnect to a peer using a particular
76  * transport plugin before giving up?  Note that the plugin may be
77  * added back to the list after PLUGIN_RETRY_FREQUENCY expires.
78  */
79 #define MAX_CONNECT_RETRY 3
80
81 /**
82  * Limit on the number of ready-to-run tasks when validating 
83  * HELLOs.  If more tasks are ready to run, we will drop 
84  * HELLOs instead of validating them.
85  */
86 #define MAX_HELLO_LOAD 4
87
88 /**
89  * How often must a peer violate bandwidth quotas before we start
90  * to simply drop its messages?
91  */
92 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
93
94 /**
95  * How long until a HELLO verification attempt should time out?
96  * Must be rather small, otherwise a partially successful HELLO
97  * validation (some addresses working) might not be available
98  * before a client's request for a connection fails for good.
99  * Besides, if a single request to an address takes a long time,
100  * then the peer is unlikely worthwhile anyway.
101  */
102 #define HELLO_VERIFICATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
103
104 /**
105  * Priority to use for PONG messages.
106  */
107 #define TRANSPORT_PONG_PRIORITY 4
108
109 /**
110  * How often do we re-add (cheaper) plugins to our list of plugins
111  * to try for a given connected peer?
112  */
113 #define PLUGIN_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
114
115 /**
116  * After how long do we expire an address in a HELLO that we just
117  * validated?  This value is also used for our own addresses when we
118  * create a HELLO.
119  */
120 #define HELLO_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 12)
121
122
123 /**
124  * How long before an existing address expires should we again try to
125  * validate it?  Must be (significantly) smaller than
126  * HELLO_ADDRESS_EXPIRATION.
127  */
128 #define HELLO_REVALIDATION_START_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
129
130 /**
131  * Maximum frequency for re-evaluating latencies for all transport addresses.
132  */
133 #define LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
134
135 /**
136  * Maximum frequency for re-evaluating latencies for connected addresses.
137  */
138 #define CONNECTED_LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
139
140
141 /**
142  * List of addresses of other peers
143  */
144 struct ForeignAddressList
145 {
146   /**
147    * This is a linked list.
148    */
149   struct ForeignAddressList *next;
150
151   /**
152    * Which ready list does this entry belong to.
153    */
154   struct ReadyList *ready_list;
155
156   /**
157    * How long until we auto-expire this address (unless it is
158    * re-confirmed by the transport)?
159    */
160   struct GNUNET_TIME_Absolute expires;
161
162   /**
163    * Task used to re-validate addresses, updates latencies and
164    * verifies liveness.
165    */
166   GNUNET_SCHEDULER_TaskIdentifier revalidate_task;
167
168   /**
169    * Length of addr.
170    */
171   size_t addrlen;
172
173   /**
174    * The address.
175    */
176   const void *addr;
177
178   /**
179    * Session (or NULL if no valid session currently exists or if the
180    * plugin does not use sessions).
181    */
182   struct Session *session;
183
184   /**
185    * What was the last latency observed for this address, plugin and peer?
186    */
187   struct GNUNET_TIME_Relative latency;
188
189   /**
190    * If we did not successfully transmit a message to the given peer
191    * via this connection during the specified time, we should consider
192    * the connection to be dead.  This is used in the case that a TCP
193    * transport simply stalls writing to the stream but does not
194    * formerly get a signal that the other peer died.
195    */
196   struct GNUNET_TIME_Absolute timeout;
197
198   /**
199    * How often have we tried to connect using this plugin?  Used to
200    * discriminate against addresses that do not work well.
201    * FIXME: not yet used, but should be!
202    */
203   unsigned int connect_attempts;
204
205   /**
206    * DV distance to this peer (1 if no DV is used). 
207    * FIXME: need to set this from transport plugins!
208    */
209   uint32_t distance;
210
211   /**
212    * Have we ever estimated the latency of this address?  Used to
213    * ensure that the first time we add an address, we immediately
214    * probe its latency.
215    */
216   int8_t estimated;
217
218   /**
219    * Are we currently connected via this address?  The first time we
220    * successfully transmit or receive data to a peer via a particular
221    * address, we set this to GNUNET_YES.  If we later get an error
222    * (disconnect notification, transmission failure, timeout), we set
223    * it back to GNUNET_NO.  
224    */
225   int8_t connected;
226
227   /**
228    * Is this plugin currently busy transmitting to the specific target?
229    * GNUNET_NO if not (initial, default state is GNUNET_NO).   Internal
230    * messages do not count as 'in transmit'.
231    */
232   int8_t in_transmit;
233
234   /**
235    * Has this address been validated yet?
236    */
237   int8_t validated;
238
239 };
240
241
242 /**
243  * Entry in linked list of network addresses for ourselves.
244  */
245 struct OwnAddressList
246 {
247   /**
248    * This is a linked list.
249    */
250   struct OwnAddressList *next;
251
252   /**
253    * The address, actually a pointer to the end
254    * of this struct.  Do not free!
255    */
256   const void *addr;
257   
258   /**
259    * How long until we auto-expire this address (unless it is
260    * re-confirmed by the transport)?
261    */
262   struct GNUNET_TIME_Absolute expires;
263
264   /**
265    * Length of addr.
266    */
267   size_t addrlen;
268
269 };
270
271
272 /**
273  * Entry in linked list of all of our plugins.
274  */
275 struct TransportPlugin
276 {
277
278   /**
279    * This is a linked list.
280    */
281   struct TransportPlugin *next;
282
283   /**
284    * API of the transport as returned by the plugin's
285    * initialization function.
286    */
287   struct GNUNET_TRANSPORT_PluginFunctions *api;
288
289   /**
290    * Short name for the plugin (i.e. "tcp").
291    */
292   char *short_name;
293
294   /**
295    * Name of the library (i.e. "gnunet_plugin_transport_tcp").
296    */
297   char *lib_name;
298
299   /**
300    * List of our known addresses for this transport.
301    */
302   struct OwnAddressList *addresses;
303
304   /**
305    * Environment this transport service is using
306    * for this plugin.
307    */
308   struct GNUNET_TRANSPORT_PluginEnvironment env;
309
310   /**
311    * ID of task that is used to clean up expired addresses.
312    */
313   GNUNET_SCHEDULER_TaskIdentifier address_update_task;
314
315   /**
316    * Set to GNUNET_YES if we need to scrap the existing list of
317    * "addresses" and start fresh when we receive the next address
318    * update from a transport.  Set to GNUNET_NO if we should just add
319    * the new address to the list and wait for the commit call.
320    */
321   int rebuild;
322
323   /**
324    * Hashmap of blacklisted peers for this particular transport.
325    */
326   struct GNUNET_CONTAINER_MultiHashMap *blacklist;
327 };
328
329 struct NeighbourList;
330
331 /**
332  * For each neighbour we keep a list of messages
333  * that we still want to transmit to the neighbour.
334  */
335 struct MessageQueue
336 {
337
338   /**
339    * This is a doubly linked list.
340    */
341   struct MessageQueue *next;
342
343   /**
344    * This is a doubly linked list.
345    */
346   struct MessageQueue *prev;
347
348   /**
349    * The message(s) we want to transmit, GNUNET_MessageHeader(s)
350    * stuck together in memory.  Allocated at the end of this struct.
351    */
352   const char *message_buf;
353
354   /**
355    * Size of the message buf
356    */
357   size_t message_buf_size;
358
359   /**
360    * Client responsible for queueing the message;
361    * used to check that a client has no two messages
362    * pending for the same target.  Can be NULL.
363    */
364   struct TransportClient *client;
365
366   /**
367    * Using which specific address should we send this message?
368    */
369   struct ForeignAddressList *specific_address;
370
371   /**
372    * Peer ID of the Neighbour this entry belongs to.
373    */
374   struct GNUNET_PeerIdentity neighbour_id;
375
376   /**
377    * Plugin that we used for the transmission.
378    * NULL until we scheduled a transmission.
379    */
380   struct TransportPlugin *plugin;
381
382   /**
383    * At what time should we fail?
384    */
385   struct GNUNET_TIME_Absolute timeout;
386
387   /**
388    * Internal message of the transport system that should not be
389    * included in the usual SEND-SEND_OK transmission confirmation
390    * traffic management scheme.  Typically, "internal_msg" will
391    * be set whenever "client" is NULL (but it is not strictly
392    * required).
393    */
394   int internal_msg;
395
396   /**
397    * How important is the message?
398    */
399   unsigned int priority;
400
401 };
402
403
404 /**
405  * For a given Neighbour, which plugins are available
406  * to talk to this peer and what are their costs?
407  */
408 struct ReadyList
409 {
410   /**
411    * This is a linked list.
412    */
413   struct ReadyList *next;
414
415   /**
416    * Which of our transport plugins does this entry
417    * represent?
418    */
419   struct TransportPlugin *plugin;
420
421   /**
422    * Transport addresses, latency, and readiness for
423    * this particular plugin.
424    */
425   struct ForeignAddressList *addresses;
426
427   /**
428    * To which neighbour does this ready list belong to?
429    */
430   struct NeighbourList *neighbour;
431
432 };
433
434
435 /**
436  * Entry in linked list of all of our current neighbours.
437  */
438 struct NeighbourList
439 {
440
441   /**
442    * This is a linked list.
443    */
444   struct NeighbourList *next;
445
446   /**
447    * Which of our transports is connected to this peer
448    * and what is their status?
449    */
450   struct ReadyList *plugins;
451
452   /**
453    * Head of list of messages we would like to send to this peer;
454    * must contain at most one message per client.
455    */
456   struct MessageQueue *messages_head;
457
458   /**
459    * Tail of list of messages we would like to send to this peer; must
460    * contain at most one message per client.
461    */
462   struct MessageQueue *messages_tail;
463
464   /**
465    * Buffer for at most one payload message used when we receive
466    * payload data before our PING-PONG has succeeded.  We then
467    * store such messages in this intermediary buffer until the
468    * connection is fully up.  
469    */
470   struct GNUNET_MessageHeader *pre_connect_message_buffer;
471
472   /**
473    * Context for peerinfo iteration.
474    * NULL after we are done processing peerinfo's information.
475    */
476   struct GNUNET_PEERINFO_IteratorContext *piter;
477
478   /**
479    * Public key for this peer.   Valid only if the respective flag is set below.
480    */
481   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
482
483   /**
484    * Identity of this neighbour.
485    */
486   struct GNUNET_PeerIdentity id;
487
488   /**
489    * ID of task scheduled to run when this peer is about to
490    * time out (will free resources associated with the peer).
491    */
492   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
493
494   /**
495    * ID of task scheduled to run when we should retry transmitting
496    * the head of the message queue.  Actually triggered when the
497    * transmission is timing out (we trigger instantly when we have
498    * a chance of success).
499    */
500   GNUNET_SCHEDULER_TaskIdentifier retry_task;
501
502   /**
503    * How long until we should consider this peer dead
504    * (if we don't receive another message in the
505    * meantime)?
506    */
507   struct GNUNET_TIME_Absolute peer_timeout;
508
509   /**
510    * Tracker for inbound bandwidth.
511    */
512   struct GNUNET_BANDWIDTH_Tracker in_tracker;
513
514   /**
515    * The latency we have seen for this particular address for
516    * this particular peer.  This latency may have been calculated
517    * over multiple transports.  This value reflects how long it took
518    * us to receive a response when SENDING via this particular
519    * transport/neighbour/address combination!
520    *
521    * FIXME: we need to periodically send PINGs to update this
522    * latency (at least more often than the current "huge" (11h?)
523    * update interval).
524    */
525   struct GNUNET_TIME_Relative latency;
526
527   /**
528    * How often has the other peer (recently) violated the
529    * inbound traffic limit?  Incremented by 10 per violation,
530    * decremented by 1 per non-violation (for each
531    * time interval).
532    */
533   unsigned int quota_violation_count;
534
535   /**
536    * DV distance to this peer (1 if no DV is used). 
537    */
538   uint32_t distance;
539
540   /**
541    * Have we seen an PONG from this neighbour in the past (and
542    * not had a disconnect since)?
543    */
544   int received_pong;
545
546   /**
547    * Do we have a valid public key for this neighbour?
548    */
549   int public_key_valid;
550
551 };
552
553 /**
554  * Message used to ask a peer to validate receipt (to check an address
555  * from a HELLO).  
556  */
557 struct TransportPingMessage
558 {
559
560   /**
561    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PING
562    */
563   struct GNUNET_MessageHeader header;
564
565   /**
566    * Random challenge number (in network byte order).
567    */
568   uint32_t challenge GNUNET_PACKED;
569
570   /**
571    * Who is the intended recipient?
572    */
573   struct GNUNET_PeerIdentity target;
574
575 };
576
577
578 /**
579  * Message used to validate a HELLO.  The challenge is included in the
580  * confirmation to make matching of replies to requests possible.  The
581  * signature signs the original challenge number, our public key, the
582  * sender's address (so that the sender can check that the address we
583  * saw is plausible for him and possibly detect a MiM attack) and a
584  * timestamp (to limit replay).<p>
585  *
586  * This message is followed by the address of the
587  * client that we are observing (which is part of what
588  * is being signed).
589  */
590 struct TransportPongMessage
591 {
592
593   /**
594    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_PONG
595    */
596   struct GNUNET_MessageHeader header;
597
598   /**
599    * For padding, always zero.
600    */
601   uint32_t reserved GNUNET_PACKED;
602
603   /**
604    * Signature.
605    */
606   struct GNUNET_CRYPTO_RsaSignature signature;
607
608   /**
609    * What are we signing and why?
610    */
611   struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
612
613   /**
614    * Random challenge number (in network byte order).
615    */
616   uint32_t challenge GNUNET_PACKED;
617
618   /**
619    * Who signed this message?
620    */
621   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded signer;
622
623   /**
624    * Size of address appended to this message
625    */
626   size_t addrlen;
627
628 };
629
630
631 /**
632  * Linked list of messages to be transmitted to the client.  Each
633  * entry is followed by the actual message.
634  */
635 struct ClientMessageQueueEntry
636 {
637   /**
638    * This is a doubly-linked list.
639    */
640   struct ClientMessageQueueEntry *next;
641
642   /**
643    * This is a doubly-linked list.
644    */
645   struct ClientMessageQueueEntry *prev;
646 };
647
648
649 /**
650  * Client connected to the transport service.
651  */
652 struct TransportClient
653 {
654
655   /**
656    * This is a linked list.
657    */
658   struct TransportClient *next;
659
660   /**
661    * Handle to the client.
662    */
663   struct GNUNET_SERVER_Client *client;
664
665   /**
666    * Linked list of messages yet to be transmitted to
667    * the client.
668    */
669   struct ClientMessageQueueEntry *message_queue_head;
670
671   /**
672    * Tail of linked list of messages yet to be transmitted to the
673    * client.
674    */
675   struct ClientMessageQueueEntry *message_queue_tail;
676
677   /**
678    * Current transmit request handle.
679    */ 
680   struct GNUNET_CONNECTION_TransmitHandle *th;
681
682   /**
683    * Is a call to "transmit_send_continuation" pending?  If so, we
684    * must not free this struct (even if the corresponding client
685    * disconnects) and instead only remove it from the linked list and
686    * set the "client" field to NULL.
687    */
688   int tcs_pending;
689
690   /**
691    * Length of the list of messages pending for this client.
692    */
693   unsigned int message_count;
694
695 };
696
697
698 /**
699  * Entry in map of all HELLOs awaiting validation.
700  */
701 struct ValidationEntry
702 {
703
704   /**
705    * The address, actually a pointer to the end
706    * of this struct.  Do not free!
707    */
708   const void *addr;
709
710   /**
711    * Name of the transport.
712    */
713   char *transport_name;
714
715   /**
716    * The public key of the peer.
717    */
718   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
719
720   /**
721    * ID of task that will clean up this entry if we don't succeed
722    * with the validation first.
723    */
724   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
725
726   /**
727    * At what time did we send this validation?
728    */
729   struct GNUNET_TIME_Absolute send_time;
730
731   /**
732    * Session being validated (or NULL for none).
733    */
734   struct Session *session;
735
736   /**
737    * Length of addr.
738    */
739   size_t addrlen;
740
741   /**
742    * Challenge number we used.
743    */
744   uint32_t challenge;
745
746 };
747
748
749 /**
750  * Context of currently active requests to peerinfo
751  * for validation of HELLOs.
752  */
753 struct CheckHelloValidatedContext
754 {
755
756   /**
757    * This is a doubly-linked list.
758    */
759   struct CheckHelloValidatedContext *next;
760
761   /**
762    * This is a doubly-linked list.
763    */
764   struct CheckHelloValidatedContext *prev;
765
766   /**
767    * Hello that we are validating.
768    */
769   const struct GNUNET_HELLO_Message *hello;
770
771   /**
772    * Context for peerinfo iteration.
773    * NULL after we are done processing peerinfo's information.
774    */
775   struct GNUNET_PEERINFO_IteratorContext *piter;
776   
777   /**
778    * Was a HELLO known for this peer to peerinfo?
779    */
780   int hello_known;
781
782 };
783
784
785
786 /**
787  * Our HELLO message.
788  */
789 static struct GNUNET_HELLO_Message *our_hello;
790
791 /**
792  * "version" of "our_hello".  Used to see if a given neighbour has
793  * already been sent the latest version of our HELLO message.
794  */
795 static unsigned int our_hello_version;
796
797 /**
798  * Our public key.
799  */
800 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
801
802 /**
803  * Our identity.
804  */
805 static struct GNUNET_PeerIdentity my_identity;
806
807 /**
808  * Our private key.
809  */
810 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
811
812 /**
813  * Our scheduler.
814  */
815 struct GNUNET_SCHEDULER_Handle *sched;
816
817 /**
818  * Our configuration.
819  */
820 const struct GNUNET_CONFIGURATION_Handle *cfg;
821
822 /**
823  * Linked list of all clients to this service.
824  */
825 static struct TransportClient *clients;
826
827 /**
828  * All loaded plugins.
829  */
830 static struct TransportPlugin *plugins;
831
832 /**
833  * Our server.
834  */
835 static struct GNUNET_SERVER_Handle *server;
836
837 /**
838  * Handle to peerinfo service.
839  */
840 static struct GNUNET_PEERINFO_Handle *peerinfo;
841
842 /**
843  * All known neighbours and their HELLOs.
844  */
845 static struct NeighbourList *neighbours;
846
847 /**
848  * Number of neighbours we'd like to have.
849  */
850 static uint32_t max_connect_per_transport;
851
852 /**
853  * Head of linked list.
854  */
855 static struct CheckHelloValidatedContext *chvc_head;
856
857 /**
858  * Tail of linked list.
859  */
860 static struct CheckHelloValidatedContext *chvc_tail;
861
862 /**
863  * Map of PeerIdentities to 'struct ValidationEntry*'s (addresses
864  * of the given peer that we are currently validating).
865  */
866 static struct GNUNET_CONTAINER_MultiHashMap *validation_map;
867
868 /**
869  * Handle for reporting statistics.
870  */
871 static struct GNUNET_STATISTICS_Handle *stats;
872
873
874 /**
875  * The peer specified by the given neighbour has timed-out or a plugin
876  * has disconnected.  We may either need to do nothing (other plugins
877  * still up), or trigger a full disconnect and clean up.  This
878  * function updates our state and do the necessary notifications.
879  * Also notifies our clients that the neighbour is now officially
880  * gone.
881  *
882  * @param n the neighbour list entry for the peer
883  * @param check should we just check if all plugins
884  *        disconnected or must we ask all plugins to
885  *        disconnect?
886  */
887 static void disconnect_neighbour (struct NeighbourList *n, int check);
888
889 /**
890  * Check the ready list for the given neighbour and if a plugin is
891  * ready for transmission (and if we have a message), do so!
892  *
893  * @param neighbour target peer for which to transmit
894  */
895 static void try_transmission_to_peer (struct NeighbourList *neighbour);
896
897
898 /**
899  * Find an entry in the neighbour list for a particular peer.
900  *  
901  * @return NULL if not found.
902  */
903 static struct NeighbourList *
904 find_neighbour (const struct GNUNET_PeerIdentity *key)
905 {
906   struct NeighbourList *head = neighbours;
907
908   while ((head != NULL) &&
909         (0 != memcmp (key, &head->id, sizeof (struct GNUNET_PeerIdentity))))
910     head = head->next;
911   return head;
912 }
913
914
915 /**
916  * Find an entry in the transport list for a particular transport.
917  *
918  * @return NULL if not found.
919  */
920 static struct TransportPlugin *
921 find_transport (const char *short_name)
922 {
923   struct TransportPlugin *head = plugins;
924   while ((head != NULL) && (0 != strcmp (short_name, head->short_name)))
925     head = head->next;
926   return head;
927 }
928
929 /**
930  * Is a particular peer blacklisted for a particular transport?
931  *
932  * @param peer the peer to check for
933  * @param plugin the plugin used to connect to the peer
934  *
935  * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
936  */
937 static int
938 is_blacklisted (const struct GNUNET_PeerIdentity *peer, struct TransportPlugin *plugin)
939 {
940
941   if (plugin->blacklist != NULL)
942     {
943       if (GNUNET_CONTAINER_multihashmap_contains(plugin->blacklist, &peer->hashPubKey) == GNUNET_YES)
944         {
945 #if DEBUG_BLACKLIST
946           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
947                       _("Peer `%s:%s' is blacklisted!\n"),
948                       plugin->short_name, GNUNET_i2s (peer));
949 #endif
950           return GNUNET_YES;
951         }
952     }
953
954   return GNUNET_NO;
955 }
956
957
958 static void
959 add_peer_to_blacklist (struct GNUNET_PeerIdentity *peer, char *transport_name)
960 {
961   struct TransportPlugin *plugin;
962
963   plugin = find_transport(transport_name);
964   if (plugin == NULL) /* Nothing to do */
965     return;
966   if (plugin->blacklist == NULL)    
967     plugin->blacklist = GNUNET_CONTAINER_multihashmap_create(TRANSPORT_BLACKLIST_HT_SIZE);    
968   GNUNET_assert(plugin->blacklist != NULL);
969   GNUNET_CONTAINER_multihashmap_put(plugin->blacklist, &peer->hashPubKey,
970                                     NULL, 
971                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
972 }
973
974
975 /**
976  * Read the blacklist file, containing transport:peer entries.
977  * Provided the transport is loaded, set up hashmap with these
978  * entries to blacklist peers by transport.
979  *
980  */
981 static void
982 read_blacklist_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
983 {
984   char *fn;
985   char *data;
986   size_t pos;
987   size_t colon_pos;
988   int tsize;
989   struct GNUNET_PeerIdentity pid;
990   struct stat frstat;
991   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
992   unsigned int entries_found;
993   char *transport_name;
994
995   if (GNUNET_OK !=
996       GNUNET_CONFIGURATION_get_value_filename (cfg,
997                                                "TRANSPORT",
998                                                "BLACKLIST_FILE",
999                                                &fn))
1000     {
1001       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002                   _("Option `%s' in section `%s' not specified!\n"),
1003                   "BLACKLIST_FILE",
1004                   "TRANSPORT");
1005       return;
1006     }
1007   if (GNUNET_OK != GNUNET_DISK_file_test (fn))
1008     GNUNET_DISK_fn_write (fn, NULL, 0, GNUNET_DISK_PERM_USER_READ
1009         | GNUNET_DISK_PERM_USER_WRITE);
1010   if (0 != STAT (fn, &frstat))
1011     {
1012       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1013                   _("Could not read blacklist file `%s'\n"), fn);
1014       GNUNET_free (fn);
1015       return;
1016     }
1017   if (frstat.st_size == 0)
1018     {
1019       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1020                   _("Blacklist file `%s' is empty.\n"),
1021                   fn);
1022       GNUNET_free (fn);
1023       return;
1024     }
1025   /* FIXME: use mmap */
1026   data = GNUNET_malloc_large (frstat.st_size);
1027   if (frstat.st_size !=
1028       GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1029     {
1030       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1031                   _("Failed to read blacklist from `%s'\n"), fn);
1032       GNUNET_free (fn);
1033       GNUNET_free (data);
1034       return;
1035     }
1036   entries_found = 0;
1037   pos = 0;
1038   while ((pos < frstat.st_size) && isspace (data[pos]))
1039     pos++;
1040   while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1041          (pos <= frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1042     {
1043       colon_pos = pos;
1044       while ((colon_pos < frstat.st_size) && (data[colon_pos] != ':') && !isspace (data[colon_pos]))
1045         colon_pos++;
1046
1047       if (colon_pos >= frstat.st_size)
1048         {
1049           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1050                       _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1051                       (unsigned long long) colon_pos);
1052           GNUNET_free (fn);
1053           GNUNET_free (data);
1054           return;
1055         }
1056
1057       if (isspace(data[colon_pos]))
1058       {
1059         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1060                     _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1061                     (unsigned long long) colon_pos);
1062         pos = colon_pos;
1063         while ((pos < frstat.st_size) && isspace (data[pos]))
1064           pos++;
1065         continue;
1066       }
1067       tsize = colon_pos - pos;
1068       if ((pos >= frstat.st_size) || (pos + tsize >= frstat.st_size))
1069         {
1070           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1071                       _("Syntax error in blacklist file at offset %llu, giving up!\n"),
1072                       (unsigned long long) colon_pos);
1073           GNUNET_free (fn);
1074           GNUNET_free (data);
1075           return;
1076         }
1077
1078       transport_name = GNUNET_malloc(tsize);
1079       memcpy(transport_name, &data[pos], tsize);
1080       pos = colon_pos + 1;
1081
1082
1083       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084                   _("Read transport name %s in blacklist file.\n"),
1085                   transport_name);
1086
1087       memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1088       if (!isspace (enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1089         {
1090           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1091                       _("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
1092                       (unsigned long long) pos);
1093           pos++;
1094           while ((pos < frstat.st_size) && (!isspace (data[pos])))
1095             pos++;
1096           GNUNET_free_non_null(transport_name);
1097           continue;
1098         }
1099       enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1100       if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1101         {
1102           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1103                       _("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
1104                       (unsigned long long) pos,
1105                       &enc);
1106         }
1107       else
1108         {
1109           if (0 != memcmp (&pid,
1110                            &my_identity,
1111                            sizeof (struct GNUNET_PeerIdentity)))
1112             {
1113               entries_found++;
1114               add_peer_to_blacklist (&pid,
1115                               transport_name);
1116               GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1117                           _("Found blacklisted peer `%s:%s' in configuration\n"),
1118                           transport_name, GNUNET_i2s (&pid));
1119             }
1120           else
1121             {
1122               GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1123                           _("Found myself `%s' in blacklist (useless, ignored)\n"),
1124                           GNUNET_i2s (&pid));
1125             }
1126         }
1127       pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1128       GNUNET_free_non_null(transport_name);
1129       while ((pos < frstat.st_size) && isspace (data[pos]))
1130         pos++;
1131     }
1132   GNUNET_free (data);
1133   GNUNET_free (fn);
1134 }
1135
1136
1137 /**
1138  * Function called to notify a client about the socket being ready to
1139  * queue more data.  "buf" will be NULL and "size" zero if the socket
1140  * was closed for writing in the meantime.
1141  *
1142  * @param cls closure
1143  * @param size number of bytes available in buf
1144  * @param buf where the callee should write the message
1145  * @return number of bytes written to buf
1146  */
1147 static size_t
1148 transmit_to_client_callback (void *cls, size_t size, void *buf)
1149 {
1150   struct TransportClient *client = cls;
1151   struct ClientMessageQueueEntry *q;
1152   uint16_t msize;
1153   size_t tsize;
1154   const struct GNUNET_MessageHeader *msg;
1155   char *cbuf;
1156
1157   client->th = NULL;
1158   if (buf == NULL)
1159     {
1160       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161                   "Transmission to client failed, closing connection.\n");
1162       /* fatal error with client, free message queue! */
1163       while (NULL != (q = client->message_queue_head))
1164         {
1165           GNUNET_STATISTICS_update (stats,
1166                                     gettext_noop ("# bytes discarded (could not transmit to client)"),
1167                                     ntohs (((const struct GNUNET_MessageHeader*)&q[1])->size),
1168                                     GNUNET_NO);      
1169           GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1170                                        client->message_queue_tail,
1171                                        q);
1172           GNUNET_free (q);
1173         }
1174       client->message_count = 0;
1175       return 0;
1176     }
1177   cbuf = buf;
1178   tsize = 0;
1179   while (NULL != (q = client->message_queue_head))
1180     {
1181       msg = (const struct GNUNET_MessageHeader *) &q[1];
1182       msize = ntohs (msg->size);
1183       if (msize + tsize > size)
1184         break;
1185 #if DEBUG_TRANSPORT
1186       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187                   "Transmitting message of type %u to client.\n",
1188                   ntohs (msg->type));
1189 #endif
1190       GNUNET_CONTAINER_DLL_remove (client->message_queue_head,
1191                                    client->message_queue_tail,
1192                                    q);
1193       memcpy (&cbuf[tsize], msg, msize);
1194       tsize += msize;
1195       GNUNET_free (q);
1196       client->message_count--;
1197     }
1198   if (NULL != q)
1199     {
1200       GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1201       client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1202                                                         msize,
1203                                                         GNUNET_TIME_UNIT_FOREVER_REL,
1204                                                         &transmit_to_client_callback,
1205                                                         client);
1206       GNUNET_assert (client->th != NULL);
1207     }
1208   return tsize;
1209 }
1210
1211
1212 /**
1213  * Mark the given FAL entry as 'connected' (and hence preferred for
1214  * sending); also mark all others for the same peer as 'not connected'
1215  * (since only one can be preferred).
1216  *
1217  * @param fal address to set to 'connected'
1218  */
1219 static void
1220 mark_address_connected (struct ForeignAddressList *fal)
1221 {
1222   struct ForeignAddressList *pos;
1223   int cnt;
1224
1225   GNUNET_assert (GNUNET_YES == fal->validated);
1226   if (fal->connected == GNUNET_YES)
1227     return; /* nothing to do */
1228   cnt = GNUNET_YES;
1229   pos = fal->ready_list->addresses;
1230   while (pos != NULL)
1231     {
1232       if (GNUNET_YES == pos->connected)
1233         {
1234           GNUNET_break (cnt == GNUNET_YES);
1235           cnt = GNUNET_NO;
1236           pos->connected = GNUNET_NO;
1237         }
1238       pos = pos->next;
1239     }
1240   fal->connected = GNUNET_YES;
1241   if (GNUNET_YES == cnt)
1242     {
1243       GNUNET_STATISTICS_update (stats,
1244                                 gettext_noop ("# connected addresses"),
1245                                 1,
1246                                 GNUNET_NO);
1247     }
1248 }
1249
1250
1251 /**
1252  * Send the specified message to the specified client.  Since multiple
1253  * messages may be pending for the same client at a time, this code
1254  * makes sure that no message is lost.
1255  *
1256  * @param client client to transmit the message to
1257  * @param msg the message to send
1258  * @param may_drop can this message be dropped if the
1259  *        message queue for this client is getting far too large?
1260  */
1261 static void
1262 transmit_to_client (struct TransportClient *client,
1263                     const struct GNUNET_MessageHeader *msg, int may_drop)
1264 {
1265   struct ClientMessageQueueEntry *q;
1266   uint16_t msize;
1267
1268   if ((client->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
1269     {
1270       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1271                   _
1272                   ("Dropping message, have %u messages pending (%u is the soft limit)\n"),
1273                   client->message_count, MAX_PENDING);
1274       /* TODO: call to statistics... */
1275       return;
1276     }
1277   msize = ntohs (msg->size);
1278   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
1279   q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
1280   memcpy (&q[1], msg, msize);
1281   GNUNET_CONTAINER_DLL_insert_after (client->message_queue_head,
1282                                      client->message_queue_tail,
1283                                      client->message_queue_tail,
1284                                      q);                                     
1285   client->message_count++;
1286   if (client->th == NULL)
1287     {
1288       client->th = GNUNET_SERVER_notify_transmit_ready (client->client,
1289                                                         msize,
1290                                                         GNUNET_TIME_UNIT_FOREVER_REL,
1291                                                         &transmit_to_client_callback,
1292                                                         client);
1293       GNUNET_assert (client->th != NULL);
1294     }
1295 }
1296
1297
1298 /**
1299  * Transmit a 'SEND_OK' notification to the given client for the
1300  * given neighbour.
1301  *
1302  * @param client who to notify
1303  * @param n neighbour to notify about
1304  * @param result status code for the transmission request
1305  */
1306 static void
1307 transmit_send_ok (struct TransportClient *client,
1308                   struct NeighbourList *n,
1309                   int result)
1310 {
1311   struct SendOkMessage send_ok_msg;
1312
1313   send_ok_msg.header.size = htons (sizeof (send_ok_msg));
1314   send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
1315   send_ok_msg.success = htonl (result);
1316   send_ok_msg.latency = GNUNET_TIME_relative_hton (n->latency);
1317   send_ok_msg.peer = n->id;
1318   transmit_to_client (client, &send_ok_msg.header, GNUNET_NO); 
1319 }
1320
1321
1322 /**
1323  * Function called by the GNUNET_TRANSPORT_TransmitFunction
1324  * upon "completion" of a send request.  This tells the API
1325  * that it is now legal to send another message to the given
1326  * peer.
1327  *
1328  * @param cls closure, identifies the entry on the
1329  *            message queue that was transmitted and the
1330  *            client responsible for queueing the message
1331  * @param target the peer receiving the message
1332  * @param result GNUNET_OK on success, if the transmission
1333  *           failed, we should not tell the client to transmit
1334  *           more messages
1335  */
1336 static void
1337 transmit_send_continuation (void *cls,
1338                             const struct GNUNET_PeerIdentity *target,
1339                             int result)
1340 {
1341   struct MessageQueue *mq = cls;
1342   struct NeighbourList *n;
1343   
1344   GNUNET_STATISTICS_update (stats,
1345                             gettext_noop ("# bytes pending with plugins"),
1346                             - (int64_t) mq->message_buf_size,
1347                             GNUNET_NO);
1348   if (result == GNUNET_OK)
1349     {
1350       GNUNET_STATISTICS_update (stats,
1351                                 gettext_noop ("# bytes successfully transmitted by plugins"),
1352                                 mq->message_buf_size,
1353                                 GNUNET_NO);      
1354     }
1355   else
1356     {
1357       GNUNET_STATISTICS_update (stats,
1358                                 gettext_noop ("# bytes with transmission failure by plugins"),
1359                                 mq->message_buf_size,
1360                                 GNUNET_NO);      
1361     }  
1362   n = find_neighbour(&mq->neighbour_id);
1363   GNUNET_assert (n != NULL);
1364   if (mq->specific_address != NULL)
1365     {
1366       if (result == GNUNET_OK)    
1367         {
1368           mq->specific_address->timeout =
1369             GNUNET_TIME_relative_to_absolute
1370             (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1371           if (mq->specific_address->validated == GNUNET_YES)
1372             mark_address_connected (mq->specific_address);
1373         }    
1374       else
1375         {
1376           if (mq->specific_address->connected != GNUNET_NO)
1377             {
1378               GNUNET_STATISTICS_update (stats,
1379                                         gettext_noop ("# connected addresses"),
1380                                         -1,
1381                                         GNUNET_NO);
1382               mq->specific_address->connected = GNUNET_NO;
1383             }
1384         }    
1385       if (! mq->internal_msg) 
1386         mq->specific_address->in_transmit = GNUNET_NO;
1387     }
1388   if (mq->client != NULL)
1389     transmit_send_ok (mq->client, n, result);
1390   GNUNET_free (mq);
1391   try_transmission_to_peer (n);
1392 }
1393
1394
1395 /**
1396  * Convert an address to a string.
1397  *
1398  * @param plugin name of the plugin responsible for the address
1399  * @param addr binary address
1400  * @param addr_len number of bytes in addr
1401  * @return NULL on error, otherwise address string
1402  */
1403 static const char*
1404 a2s (const char *plugin,
1405      const void *addr,
1406      size_t addr_len)
1407 {
1408   struct TransportPlugin *p;
1409
1410   if (plugin == NULL)
1411     return NULL;
1412   p = find_transport (plugin);
1413   if (p == NULL)
1414     return NULL;
1415   return p->api->address_to_string (p->api->cls,
1416                                     addr,
1417                                     addr_len);
1418 }   
1419
1420
1421 /**
1422  * Find an address in any of the available transports for
1423  * the given neighbour that would be good for message
1424  * transmission.  This is essentially the transport selection
1425  * routine.
1426  *
1427  * @param neighbour for whom to select an address
1428  * @return selected address, NULL if we have none
1429  */
1430 struct ForeignAddressList *
1431 find_ready_address(struct NeighbourList *neighbour)
1432 {
1433   struct ReadyList *head = neighbour->plugins;
1434   struct ForeignAddressList *addresses;
1435   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1436   struct ForeignAddressList *best_address;
1437
1438   best_address = NULL;
1439   while (head != NULL)
1440     {
1441       addresses = head->addresses;
1442       while (addresses != NULL)
1443         {
1444           if ( (addresses->timeout.value < now.value) && 
1445                (addresses->connected == GNUNET_YES) )
1446             {
1447 #if DEBUG_TRANSPORT
1448               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449                           "Marking long-time inactive connection to `%4s' as down.\n",
1450                           GNUNET_i2s (&neighbour->id));
1451 #endif
1452               GNUNET_STATISTICS_update (stats,
1453                                         gettext_noop ("# connected addresses"),
1454                                         -1,
1455                                         GNUNET_NO);
1456               addresses->connected = GNUNET_NO;
1457             }
1458           addresses = addresses->next;
1459         }
1460
1461       addresses = head->addresses;
1462       while (addresses != NULL)
1463         {
1464 #if DEBUG_TRANSPORT > 1
1465           if (addresses->addr != NULL)
1466             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467                         "Have address `%s' for peer `%4s' (status: %d, %d, %d, %u, %llums, %u)\n",
1468                         a2s (head->plugin->short_name,
1469                              addresses->addr,
1470                              addresses->addrlen),
1471                         GNUNET_i2s (&neighbour->id),
1472                         addresses->connected,
1473                         addresses->in_transmit,
1474                         addresses->validated,
1475                         addresses->connect_attempts,
1476                         (unsigned long long) addresses->timeout.value,
1477                         (unsigned int) addresses->distance);
1478 #endif
1479           if ( ( (best_address == NULL) || 
1480                  (addresses->connected == GNUNET_YES) ||
1481                  (best_address->connected == GNUNET_NO) ) &&
1482                (addresses->in_transmit == GNUNET_NO) &&
1483                ( (best_address == NULL) || 
1484                  (addresses->latency.value < best_address->latency.value)) )
1485             best_address = addresses;            
1486           /* FIXME: also give lower-latency addresses that are not
1487              connected a chance some times... */
1488           addresses = addresses->next;
1489         }
1490       head = head->next;
1491     }
1492   if (best_address != NULL)
1493     {
1494 #if DEBUG_TRANSPORT
1495       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1496                   "Best address found has latency of %llu ms.\n",
1497                   best_address->latency.value);
1498 #endif
1499     }
1500   else
1501     {
1502       GNUNET_STATISTICS_update (stats,
1503                                 gettext_noop ("# transmission attempts failed (no address)"),
1504                                 1,
1505                                 GNUNET_NO);
1506     }
1507   return best_address;
1508
1509 }
1510
1511
1512 /**
1513  * We should re-try transmitting to the given peer,
1514  * hopefully we've learned something in the meantime.
1515  */
1516 static void
1517 retry_transmission_task (void *cls,
1518                          const struct GNUNET_SCHEDULER_TaskContext *tc)
1519 {
1520   struct NeighbourList *n = cls;
1521
1522   n->retry_task = GNUNET_SCHEDULER_NO_TASK;
1523   try_transmission_to_peer (n);
1524 }
1525
1526
1527 /**
1528  * Check the ready list for the given neighbour and if a plugin is
1529  * ready for transmission (and if we have a message), do so!
1530  *
1531  * @param neighbour target peer for which to transmit
1532  */
1533 static void
1534 try_transmission_to_peer (struct NeighbourList *neighbour)
1535 {
1536   struct ReadyList *rl;
1537   struct MessageQueue *mq;
1538   struct GNUNET_TIME_Relative timeout;
1539   ssize_t ret;
1540   int force_address;
1541
1542   if (neighbour->messages_head == NULL)
1543     {
1544 #if DEBUG_TRANSPORT
1545       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546                   "Transmission queue for `%4s' is empty\n",
1547                   GNUNET_i2s (&neighbour->id));
1548 #endif
1549       return;                     /* nothing to do */
1550     }
1551   rl = NULL;
1552   mq = neighbour->messages_head;
1553   force_address = GNUNET_YES;
1554   if (mq->specific_address == NULL)
1555     {
1556       mq->specific_address = find_ready_address(neighbour); 
1557       GNUNET_STATISTICS_update (stats,
1558                                 gettext_noop ("# transport selected peer address freely"),
1559                                 1,
1560                                 GNUNET_NO); 
1561       force_address = GNUNET_NO;
1562     }
1563   if (mq->specific_address == NULL)
1564     {
1565       GNUNET_STATISTICS_update (stats,
1566                                 gettext_noop ("# transport failed to selected peer address"),
1567                                 1,
1568                                 GNUNET_NO); 
1569       timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1570       if (timeout.value == 0)
1571         {
1572 #if DEBUG_TRANSPORT
1573           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574                       "No destination address available to transmit message of size %u to peer `%4s'\n",
1575                       mq->message_buf_size,
1576                       GNUNET_i2s (&mq->neighbour_id));
1577 #endif
1578           GNUNET_STATISTICS_update (stats,
1579                                     gettext_noop ("# bytes in message queue for other peers"),
1580                                     - (int64_t) mq->message_buf_size,
1581                                     GNUNET_NO);
1582           GNUNET_STATISTICS_update (stats,
1583                                     gettext_noop ("# bytes discarded (no destination address available)"),
1584                                     mq->message_buf_size,
1585                                     GNUNET_NO);      
1586           if (mq->client != NULL)
1587             transmit_send_ok (mq->client, neighbour, GNUNET_NO);
1588           GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1589                                        neighbour->messages_tail,
1590                                        mq);
1591           GNUNET_free (mq);
1592           return;               /* nobody ready */ 
1593         }
1594       GNUNET_STATISTICS_update (stats,
1595                                 gettext_noop ("# message delivery deferred (no address)"),
1596                                 1,
1597                                 GNUNET_NO);
1598       if (neighbour->retry_task != GNUNET_SCHEDULER_NO_TASK)
1599         GNUNET_SCHEDULER_cancel (sched,
1600                                  neighbour->retry_task);
1601       neighbour->retry_task = GNUNET_SCHEDULER_add_delayed (sched,
1602                                                             timeout,
1603                                                             &retry_transmission_task,
1604                                                             neighbour);
1605 #if DEBUG_TRANSPORT
1606       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1607                   "No validated destination address available to transmit message of size %u to peer `%4s', will wait %llums to find an address.\n",
1608                   mq->message_buf_size,
1609                   GNUNET_i2s (&mq->neighbour_id),
1610                   timeout.value);
1611 #endif
1612       /* FIXME: might want to trigger peerinfo lookup here
1613          (unless that's already pending...) */
1614       return;    
1615     }
1616   GNUNET_CONTAINER_DLL_remove (neighbour->messages_head,
1617                                neighbour->messages_tail,
1618                                mq);
1619   if (mq->specific_address->connected == GNUNET_NO)
1620     mq->specific_address->connect_attempts++;
1621   rl = mq->specific_address->ready_list;
1622   mq->plugin = rl->plugin;
1623   if (!mq->internal_msg)
1624     mq->specific_address->in_transmit = GNUNET_YES;
1625 #if DEBUG_TRANSPORT
1626   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627               "Sending message of size %u for `%4s' to `%s' via plugin `%s'\n",
1628               mq->message_buf_size,
1629               GNUNET_i2s (&neighbour->id), 
1630               (mq->specific_address->addr != NULL)
1631               ? a2s (mq->specific_address->plugin->short_name,
1632                      mq->specific_address->addr,
1633                      mq->specific_address->addrlen)
1634               : "<inbound>",
1635               rl->plugin->short_name);
1636 #endif
1637   GNUNET_STATISTICS_update (stats,
1638                             gettext_noop ("# bytes in message queue for other peers"),
1639                             - (int64_t) mq->message_buf_size,
1640                             GNUNET_NO);
1641   GNUNET_STATISTICS_update (stats,
1642                             gettext_noop ("# bytes pending with plugins"),
1643                             mq->message_buf_size,
1644                             GNUNET_NO);
1645   ret = rl->plugin->api->send (rl->plugin->api->cls,
1646                                &mq->neighbour_id,
1647                                mq->message_buf,
1648                                mq->message_buf_size,
1649                                mq->priority,
1650                                GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1651                                mq->specific_address->session,
1652                                mq->specific_address->addr,
1653                                mq->specific_address->addrlen,
1654                                force_address,
1655                                &transmit_send_continuation, mq);
1656   if (ret == -1)
1657     {
1658       /* failure, but 'send' would not call continuation in this case,
1659          so we need to do it here! */
1660       transmit_send_continuation (mq, 
1661                                   &mq->neighbour_id,
1662                                   GNUNET_SYSERR);
1663     }
1664 }
1665
1666
1667 /**
1668  * Send the specified message to the specified peer.
1669  *
1670  * @param client source of the transmission request (can be NULL)
1671  * @param peer_address ForeignAddressList where we should send this message
1672  * @param priority how important is the message
1673  * @param timeout how long do we have to transmit?
1674  * @param message_buf message(s) to send GNUNET_MessageHeader(s)
1675  * @param message_buf_size total size of all messages in message_buf
1676  * @param is_internal is this an internal message; these are pre-pended and
1677  *                    also do not count for plugins being "ready" to transmit
1678  * @param neighbour handle to the neighbour for transmission
1679  */
1680 static void
1681 transmit_to_peer (struct TransportClient *client,
1682                   struct ForeignAddressList *peer_address,
1683                   unsigned int priority,
1684                   struct GNUNET_TIME_Relative timeout,
1685                   const char *message_buf,
1686                   size_t message_buf_size,
1687                   int is_internal, struct NeighbourList *neighbour)
1688 {
1689   struct MessageQueue *mq;
1690
1691 #if EXTRA_CHECKS
1692   if (client != NULL)
1693     {
1694       /* check for duplicate submission */
1695       mq = neighbour->messages_head;
1696       while (NULL != mq)
1697         {
1698           if (mq->client == client)
1699             {
1700               /* client transmitted to same peer twice
1701                  before getting SEND_OK! */
1702               GNUNET_break (0);
1703               return;
1704             }
1705           mq = mq->next;
1706         }
1707     }
1708 #endif
1709   GNUNET_STATISTICS_update (stats,
1710                             gettext_noop ("# bytes in message queue for other peers"),
1711                             message_buf_size,
1712                             GNUNET_NO);
1713   mq = GNUNET_malloc (sizeof (struct MessageQueue) + message_buf_size);
1714   mq->specific_address = peer_address;
1715   mq->client = client;
1716   memcpy (&mq[1], message_buf, message_buf_size);
1717   mq->message_buf = (const char*) &mq[1];
1718   mq->message_buf_size = message_buf_size;
1719   memcpy(&mq->neighbour_id, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
1720   mq->internal_msg = is_internal;
1721   mq->priority = priority;
1722   mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1723   if (is_internal)    
1724     GNUNET_CONTAINER_DLL_insert (neighbour->messages_head,
1725                                  neighbour->messages_tail,
1726                                  mq);
1727   else
1728     GNUNET_CONTAINER_DLL_insert_after (neighbour->messages_head,
1729                                        neighbour->messages_tail,
1730                                        neighbour->messages_tail,
1731                                        mq);
1732   try_transmission_to_peer (neighbour);
1733 }
1734
1735
1736 /**
1737  * FIXME: document.
1738  */
1739 struct GeneratorContext
1740 {
1741   struct TransportPlugin *plug_pos;
1742   struct OwnAddressList *addr_pos;
1743   struct GNUNET_TIME_Absolute expiration;
1744 };
1745
1746
1747 /**
1748  * FIXME: document.
1749  */
1750 static size_t
1751 address_generator (void *cls, size_t max, void *buf)
1752 {
1753   struct GeneratorContext *gc = cls;
1754   size_t ret;
1755
1756   while ((gc->addr_pos == NULL) && (gc->plug_pos != NULL))
1757     {
1758       gc->plug_pos = gc->plug_pos->next;
1759       gc->addr_pos = (gc->plug_pos != NULL) ? gc->plug_pos->addresses : NULL;
1760     }
1761   if (NULL == gc->plug_pos)
1762     {
1763
1764       return 0;
1765     }
1766   ret = GNUNET_HELLO_add_address (gc->plug_pos->short_name,
1767                                   gc->expiration,
1768                                   gc->addr_pos->addr,
1769                                   gc->addr_pos->addrlen, buf, max);
1770   gc->addr_pos = gc->addr_pos->next;
1771   return ret;
1772 }
1773
1774
1775 /**
1776  * Construct our HELLO message from all of the addresses of
1777  * all of the transports.
1778  */
1779 static void
1780 refresh_hello ()
1781 {
1782   struct GNUNET_HELLO_Message *hello;
1783   struct TransportClient *cpos;
1784   struct NeighbourList *npos;
1785   struct GeneratorContext gc;
1786
1787   gc.plug_pos = plugins;
1788   gc.addr_pos = plugins != NULL ? plugins->addresses : NULL;
1789   gc.expiration = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
1790   hello = GNUNET_HELLO_create (&my_public_key, &address_generator, &gc);
1791 #if DEBUG_TRANSPORT
1792   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1793               "Refreshed my `%s', new size is %d\n", "HELLO", GNUNET_HELLO_size(hello));
1794 #endif
1795   GNUNET_STATISTICS_update (stats,
1796                             gettext_noop ("# refreshed my HELLO"),
1797                             1,
1798                             GNUNET_NO);
1799   cpos = clients;
1800   while (cpos != NULL)
1801     {
1802       transmit_to_client (cpos,
1803                           (const struct GNUNET_MessageHeader *) hello,
1804                           GNUNET_NO);
1805       cpos = cpos->next;
1806     }
1807
1808   GNUNET_free_non_null (our_hello);
1809   our_hello = hello;
1810   our_hello_version++;
1811   GNUNET_PEERINFO_add_peer (peerinfo, our_hello);
1812   npos = neighbours;
1813   while (npos != NULL)
1814     {
1815 #if DEBUG_TRANSPORT
1816       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1817                   "Transmitting updated `%s' to neighbour `%4s'\n",
1818                   "HELLO", GNUNET_i2s (&npos->id));
1819 #endif
1820       GNUNET_STATISTICS_update (stats,
1821                                 gettext_noop ("# transmitted my HELLO to other peers"),
1822                                 1,
1823                                 GNUNET_NO);
1824       transmit_to_peer (NULL, NULL, 0,
1825                         HELLO_ADDRESS_EXPIRATION,
1826                         (const char *) our_hello, 
1827                         GNUNET_HELLO_size(our_hello),
1828                         GNUNET_NO, npos);
1829       npos = npos->next;
1830     }
1831 }
1832
1833
1834 /**
1835  * Task used to clean up expired addresses for a plugin.
1836  *
1837  * @param cls closure
1838  * @param tc context
1839  */
1840 static void
1841 expire_address_task (void *cls,
1842                      const struct GNUNET_SCHEDULER_TaskContext *tc);
1843
1844
1845 /**
1846  * Update the list of addresses for this plugin,
1847  * expiring those that are past their expiration date.
1848  *
1849  * @param plugin addresses of which plugin should be recomputed?
1850  * @param fresh set to GNUNET_YES if a new address was added
1851  *        and we need to regenerate the HELLO even if nobody
1852  *        expired
1853  */
1854 static void
1855 update_addresses (struct TransportPlugin *plugin, int fresh)
1856 {
1857   static struct GNUNET_TIME_Absolute last_update;
1858   struct GNUNET_TIME_Relative min_remaining;
1859   struct GNUNET_TIME_Relative remaining;
1860   struct GNUNET_TIME_Absolute now;
1861   struct OwnAddressList *pos;
1862   struct OwnAddressList *prev;
1863   struct OwnAddressList *next;
1864   int expired;
1865
1866   if (plugin->address_update_task != GNUNET_SCHEDULER_NO_TASK)
1867     GNUNET_SCHEDULER_cancel (plugin->env.sched, plugin->address_update_task);
1868   plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1869   now = GNUNET_TIME_absolute_get ();
1870   min_remaining = GNUNET_TIME_UNIT_FOREVER_REL;
1871   expired = (GNUNET_TIME_absolute_get_duration (last_update).value > (HELLO_ADDRESS_EXPIRATION.value / 4));
1872   prev = NULL;
1873   pos = plugin->addresses;
1874   while (pos != NULL)
1875     {
1876       next = pos->next;
1877       if (pos->expires.value < now.value)
1878         {
1879           expired = GNUNET_YES;
1880           if (prev == NULL)
1881             plugin->addresses = pos->next;
1882           else
1883             prev->next = pos->next;  
1884           GNUNET_free (pos);
1885         }
1886       else
1887         {
1888           remaining = GNUNET_TIME_absolute_get_remaining (pos->expires);
1889           if (remaining.value < min_remaining.value)
1890             min_remaining = remaining;
1891           prev = pos;
1892         }
1893       pos = next;
1894     }
1895
1896   if (expired || fresh)
1897     {
1898       last_update = now;
1899       refresh_hello ();
1900     }
1901   min_remaining = GNUNET_TIME_relative_min (min_remaining,
1902                                             GNUNET_TIME_relative_divide (HELLO_ADDRESS_EXPIRATION,
1903                                                                          2));
1904   plugin->address_update_task
1905     = GNUNET_SCHEDULER_add_delayed (plugin->env.sched,
1906                                     min_remaining,
1907                                     &expire_address_task, plugin);
1908 }
1909
1910
1911 /**
1912  * Task used to clean up expired addresses for a plugin.
1913  *
1914  * @param cls closure
1915  * @param tc context
1916  */
1917 static void
1918 expire_address_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1919 {
1920   struct TransportPlugin *plugin = cls;
1921
1922   plugin->address_update_task = GNUNET_SCHEDULER_NO_TASK;
1923   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1924     update_addresses (plugin, GNUNET_NO);
1925 }
1926
1927
1928 /**
1929  * Iterator over hash map entries that NULLs the session of validation
1930  * entries that match the given session.
1931  *
1932  * @param cls closure (the 'struct Session*' to match against)
1933  * @param key current key code (peer ID, not used)
1934  * @param value value in the hash map ('struct ValidationEntry*')
1935  * @return GNUNET_YES (we should continue to iterate)
1936  */
1937 static int 
1938 remove_session_validations (void *cls,
1939                             const GNUNET_HashCode * key,
1940                             void *value)
1941 {
1942   struct Session *session = cls;
1943   struct ValidationEntry *ve = value;
1944
1945   if (session == ve->session)
1946     ve->session = NULL;
1947   return GNUNET_YES;
1948 }
1949
1950
1951 /**
1952  * Function that will be called whenever the plugin internally
1953  * cleans up a session pointer and hence the service needs to
1954  * discard all of those sessions as well.  Plugins that do not
1955  * use sessions can simply omit calling this function and always
1956  * use NULL wherever a session pointer is needed.
1957  * 
1958  * @param cls closure
1959  * @param peer which peer was the session for 
1960  * @param session which session is being destoyed
1961  */
1962 static void
1963 plugin_env_session_end  (void *cls,
1964                          const struct GNUNET_PeerIdentity *peer,
1965                          struct Session *session)
1966 {
1967   struct TransportPlugin *p = cls;
1968   struct NeighbourList *nl;
1969   struct ReadyList *rl;
1970   struct ForeignAddressList *pos;
1971   struct ForeignAddressList *prev;
1972
1973   GNUNET_CONTAINER_multihashmap_iterate (validation_map,
1974                                          &remove_session_validations,
1975                                          session);
1976   nl = find_neighbour (peer);
1977   if (nl == NULL)
1978     return;
1979   rl = nl->plugins;
1980   while (rl != NULL)
1981     {
1982       if (rl->plugin == p)
1983         break;
1984       rl = rl->next;
1985     }
1986   if (rl == NULL)
1987     return;
1988   prev = NULL;
1989   pos = rl->addresses;
1990   while ( (pos != NULL) &&
1991           (pos->session != session) )
1992     {
1993       prev = pos;
1994       pos = pos->next;
1995     }
1996   if (pos == NULL)
1997     return;
1998   pos->session = NULL;
1999   if (pos->addrlen != 0)
2000     return;
2001   if (prev == NULL)
2002     rl->addresses = pos->next;
2003   else
2004     prev->next = pos->next;
2005   if (GNUNET_SCHEDULER_NO_TASK != pos->revalidate_task)
2006     {
2007       GNUNET_SCHEDULER_cancel (sched,
2008                                pos->revalidate_task);
2009       pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
2010     }
2011   GNUNET_free (pos);
2012   if (nl->received_pong == GNUNET_NO)
2013     return; /* nothing to do */
2014   /* check if we have any validated addresses left */
2015   pos = rl->addresses;
2016   while (pos != NULL)
2017     {
2018       if (pos->validated)
2019         return;
2020       pos = pos->next;
2021     }
2022   /* no valid addresses left, signal disconnect! */
2023   disconnect_neighbour (nl, GNUNET_NO);  
2024 }
2025
2026
2027 /**
2028  * Function that must be called by each plugin to notify the
2029  * transport service about the addresses under which the transport
2030  * provided by the plugin can be reached.
2031  *
2032  * @param cls closure
2033  * @param name name of the transport that generated the address
2034  * @param addr one of the addresses of the host, NULL for the last address
2035  *        the specific address format depends on the transport
2036  * @param addrlen length of the address
2037  * @param expires when should this address automatically expire?
2038  */
2039 static void
2040 plugin_env_notify_address (void *cls,
2041                            const char *name,
2042                            const void *addr,
2043                            size_t addrlen,
2044                            struct GNUNET_TIME_Relative expires)
2045 {
2046   struct TransportPlugin *p = cls;
2047   struct OwnAddressList *al;
2048   struct GNUNET_TIME_Absolute abex;
2049
2050   GNUNET_assert (addr != NULL);
2051   abex = GNUNET_TIME_relative_to_absolute (expires);
2052   GNUNET_assert (p == find_transport (name));
2053   al = p->addresses;
2054   while (al != NULL)
2055     {
2056       if ((addrlen == al->addrlen) && (0 == memcmp (addr, &al[1], addrlen)))
2057         {
2058           if (al->expires.value < abex.value)
2059             al->expires = abex;
2060           return;
2061         }
2062       al = al->next;
2063     }
2064
2065   al = GNUNET_malloc (sizeof (struct OwnAddressList) + addrlen);
2066   al->addr = &al[1];
2067   al->next = p->addresses;
2068   p->addresses = al;
2069   al->expires = abex;
2070   al->addrlen = addrlen;
2071   memcpy (&al[1], addr, addrlen);
2072   update_addresses (p, GNUNET_YES);
2073 }
2074
2075
2076 /**
2077  * Notify all of our clients about a peer connecting.
2078  */
2079 static void
2080 notify_clients_connect (const struct GNUNET_PeerIdentity *peer,
2081                         struct GNUNET_TIME_Relative latency,
2082                         uint32_t distance)
2083 {
2084   struct ConnectInfoMessage cim;
2085   struct TransportClient *cpos;
2086
2087 #if DEBUG_TRANSPORT
2088   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2089               "Notifying clients about connection from `%s'\n",
2090               GNUNET_i2s (peer));
2091 #endif
2092   GNUNET_STATISTICS_update (stats,
2093                             gettext_noop ("# peers connected"),
2094                             1,
2095                             GNUNET_NO);
2096   cim.header.size = htons (sizeof (struct ConnectInfoMessage));
2097   cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
2098   cim.distance = htonl (distance);
2099   cim.latency = GNUNET_TIME_relative_hton (latency);
2100   memcpy (&cim.id, peer, sizeof (struct GNUNET_PeerIdentity));
2101   cpos = clients;
2102   while (cpos != NULL)
2103     {
2104       transmit_to_client (cpos, &cim.header, GNUNET_NO);
2105       cpos = cpos->next;
2106     }
2107 }
2108
2109
2110 /**
2111  * Notify all of our clients about a peer disconnecting.
2112  */
2113 static void
2114 notify_clients_disconnect (const struct GNUNET_PeerIdentity *peer)
2115 {
2116   struct DisconnectInfoMessage dim;
2117   struct TransportClient *cpos;
2118
2119 #if DEBUG_TRANSPORT
2120   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121               "Notifying clients about lost connection to `%s'\n",
2122               GNUNET_i2s (peer));
2123 #endif
2124   GNUNET_STATISTICS_update (stats,
2125                             gettext_noop ("# peers connected"),
2126                             -1,
2127                             GNUNET_NO);
2128   dim.header.size = htons (sizeof (struct DisconnectInfoMessage));
2129   dim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
2130   dim.reserved = htonl (0);
2131   memcpy (&dim.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2132   cpos = clients;
2133   while (cpos != NULL)
2134     {
2135       transmit_to_client (cpos, &dim.header, GNUNET_NO);
2136       cpos = cpos->next;
2137     }
2138 }
2139
2140
2141 /**
2142  * Find a ForeignAddressList entry for the given neighbour
2143  * that matches the given address and transport.
2144  *
2145  * @param neighbour which peer we care about
2146  * @param tname name of the transport plugin
2147  * @param session session to look for, NULL for 'any'; otherwise
2148  *        can be used for the service to "learn" this session ID
2149  *        if 'addr' matches
2150  * @param addr binary address
2151  * @param addrlen length of addr
2152  * @return NULL if no such entry exists
2153  */
2154 static struct ForeignAddressList *
2155 find_peer_address(struct NeighbourList *neighbour,
2156                   const char *tname,
2157                   struct Session *session,
2158                   const char *addr,
2159                   size_t addrlen)
2160 {
2161   struct ReadyList *head;
2162   struct ForeignAddressList *pos;
2163
2164   head = neighbour->plugins;
2165   while (head != NULL)
2166     {
2167       if (0 == strcmp (tname, head->plugin->short_name))
2168         break;
2169       head = head->next;
2170     }
2171   if (head == NULL)
2172     return NULL;
2173   pos = head->addresses;
2174   while ( (pos != NULL) &&
2175           ( (pos->addrlen != addrlen) ||
2176             (memcmp(pos->addr, addr, addrlen) != 0) ) )
2177     {
2178       if ( (session != NULL) &&
2179            (pos->session == session) )
2180         return pos;
2181       pos = pos->next;
2182     }
2183   if ( (session != NULL) && (pos != NULL) )
2184     pos->session = session; /* learn it! */
2185   return pos;
2186 }
2187
2188
2189 /**
2190  * Get the peer address struct for the given neighbour and
2191  * address.  If it doesn't yet exist, create it.
2192  *
2193  * @param neighbour which peer we care about
2194  * @param tname name of the transport plugin
2195  * @param session session of the plugin, or NULL for none
2196  * @param addr binary address
2197  * @param addrlen length of addr
2198  * @return NULL if we do not have a transport plugin for 'tname'
2199  */
2200 static struct ForeignAddressList *
2201 add_peer_address (struct NeighbourList *neighbour,
2202                   const char *tname,
2203                   struct Session *session,
2204                   const char *addr, 
2205                   size_t addrlen)
2206 {
2207   struct ReadyList *head;
2208   struct ForeignAddressList *ret;
2209
2210   ret = find_peer_address (neighbour, tname, session, addr, addrlen);
2211   if (ret != NULL)
2212     return ret;
2213   head = neighbour->plugins;
2214
2215   while (head != NULL)
2216     {
2217       if (0 == strcmp (tname, head->plugin->short_name))
2218         break;
2219       head = head->next;
2220     }
2221   if (head == NULL)
2222     return NULL;
2223   ret = GNUNET_malloc(sizeof(struct ForeignAddressList) + addrlen);
2224   ret->session = session;
2225   if (addrlen > 0)
2226     {
2227       ret->addr = (const char*) &ret[1];
2228       memcpy (&ret[1], addr, addrlen);
2229     }
2230   else
2231     {
2232       ret->addr = NULL;
2233     }
2234   ret->addrlen = addrlen;
2235   ret->expires = GNUNET_TIME_relative_to_absolute
2236     (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2237   ret->latency = GNUNET_TIME_relative_get_forever();
2238   ret->distance = -1;
2239   ret->timeout = GNUNET_TIME_relative_to_absolute
2240     (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 
2241   ret->ready_list = head;
2242   ret->next = head->addresses;
2243   head->addresses = ret;
2244   return ret;
2245 }
2246
2247
2248 /**
2249  * Closure for 'add_validated_address'.
2250  */
2251 struct AddValidatedAddressContext
2252 {
2253   /**
2254    * Entry that has been validated.
2255    */
2256   const struct ValidationEntry *ve;
2257
2258   /**
2259    * Flag set after we have added the address so
2260    * that we terminate the iteration next time.
2261    */
2262   int done;
2263 };
2264
2265
2266 /**
2267  * Callback function used to fill a buffer of max bytes with a list of
2268  * addresses in the format used by HELLOs.  Should use
2269  * "GNUNET_HELLO_add_address" as a helper function.
2270  *
2271  * @param cls the 'struct AddValidatedAddressContext' with the validated address
2272  * @param max maximum number of bytes that can be written to buf
2273  * @param buf where to write the address information
2274  * @return number of bytes written, 0 to signal the
2275  *         end of the iteration.
2276  */
2277 static size_t
2278 add_validated_address (void *cls,
2279                        size_t max, void *buf)
2280 {
2281   struct AddValidatedAddressContext *avac = cls;
2282   const struct ValidationEntry *ve = avac->ve;
2283
2284   if (GNUNET_YES == avac->done)
2285     return 0;
2286   avac->done = GNUNET_YES;
2287   return GNUNET_HELLO_add_address (ve->transport_name,
2288                                    GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION),
2289                                    ve->addr,
2290                                    ve->addrlen,
2291                                    buf,
2292                                    max);
2293 }
2294
2295
2296
2297 /**
2298  * Closure for 'check_address_exists'.
2299  */
2300 struct CheckAddressExistsClosure
2301 {
2302   /**
2303    * Address to check for.
2304    */
2305   const void *addr;
2306
2307   /**
2308    * Name of the transport.
2309    */
2310   const char *tname;
2311
2312   /**
2313    * Session, or NULL.
2314    */
2315   struct Session *session;
2316
2317   /**
2318    * Length of addr.
2319    */
2320   size_t addrlen;
2321
2322   /**
2323    * Set to GNUNET_YES if the address exists.
2324    */
2325   int exists;
2326 };
2327
2328
2329 /**
2330  * Iterator over hash map entries.  Checks if the given
2331  * validation entry is for the same address as what is given
2332  * in the closure.
2333  *
2334  * @param cls the 'struct CheckAddressExistsClosure*'
2335  * @param key current key code (ignored)
2336  * @param value value in the hash map ('struct ValidationEntry')
2337  * @return GNUNET_YES if we should continue to
2338  *         iterate (mismatch), GNUNET_NO if not (entry matched)
2339  */
2340 static int
2341 check_address_exists (void *cls,
2342                       const GNUNET_HashCode * key,
2343                       void *value)
2344 {
2345   struct CheckAddressExistsClosure *caec = cls;
2346   struct ValidationEntry *ve = value;
2347
2348   if ( (0 == strcmp (caec->tname,
2349                      ve->transport_name)) &&
2350        (caec->addrlen == ve->addrlen) &&
2351        (0 == memcmp (caec->addr,
2352                      ve->addr,
2353                      caec->addrlen)) )
2354     {
2355       caec->exists = GNUNET_YES;
2356       return GNUNET_NO;
2357     }
2358   if ( (ve->session != NULL) &&
2359        (caec->session == ve->session) )
2360     {
2361       caec->exists = GNUNET_YES;
2362       return GNUNET_NO;
2363     }
2364   return GNUNET_YES;
2365 }
2366
2367
2368 /**
2369  * HELLO validation cleanup task (validation failed).
2370  *
2371  * @param cls the 'struct ValidationEntry' that failed
2372  * @param tc scheduler context (unused)
2373  */
2374 static void
2375 timeout_hello_validation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2376 {
2377   struct ValidationEntry *va = cls;
2378   struct GNUNET_PeerIdentity pid;
2379
2380   GNUNET_STATISTICS_update (stats,
2381                             gettext_noop ("# address validation timeouts"),
2382                             1,
2383                             GNUNET_NO);
2384   GNUNET_CRYPTO_hash (&va->publicKey,
2385                       sizeof (struct
2386                               GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2387                       &pid.hashPubKey);
2388   GNUNET_CONTAINER_multihashmap_remove (validation_map,
2389                                         &pid.hashPubKey,
2390                                         va);
2391   GNUNET_free (va->transport_name);
2392   GNUNET_free (va);
2393 }
2394
2395
2396 static void
2397 neighbour_timeout_task (void *cls,
2398                        const struct GNUNET_SCHEDULER_TaskContext *tc)
2399 {
2400   struct NeighbourList *n = cls;
2401
2402 #if DEBUG_TRANSPORT
2403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2404               "Neighbour `%4s' has timed out!\n", GNUNET_i2s (&n->id));
2405 #endif
2406   GNUNET_STATISTICS_update (stats,
2407                             gettext_noop ("# disconnects due to timeout"),
2408                             1,
2409                             GNUNET_NO);
2410   n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2411   disconnect_neighbour (n, GNUNET_NO);
2412 }
2413
2414
2415 /**
2416  * Schedule the job that will cause us to send a PING to the
2417  * foreign address to evaluate its validity and latency.
2418  *
2419  * @param fal address to PING
2420  */
2421 static void
2422 schedule_next_ping (struct ForeignAddressList *fal);
2423
2424
2425 /**
2426  * Add the given address to the list of foreign addresses
2427  * available for the given peer (check for duplicates).
2428  *
2429  * @param cls the respective 'struct NeighbourList' to update
2430  * @param tname name of the transport
2431  * @param expiration expiration time
2432  * @param addr the address
2433  * @param addrlen length of the address
2434  * @return GNUNET_OK (always)
2435  */
2436 static int
2437 add_to_foreign_address_list (void *cls,
2438                              const char *tname,
2439                              struct GNUNET_TIME_Absolute expiration,
2440                              const void *addr, size_t addrlen)
2441 {
2442   struct NeighbourList *n = cls;
2443   struct ForeignAddressList *fal;
2444   int try;
2445
2446   GNUNET_STATISTICS_update (stats,
2447                             gettext_noop ("# valid peer addresses returned by peerinfo"),
2448                             1,
2449                             GNUNET_NO);      
2450   try = GNUNET_NO;
2451   fal = find_peer_address (n, tname, NULL, addr, addrlen);
2452   if (fal == NULL)
2453     {
2454 #if DEBUG_TRANSPORT
2455       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2456                   "Adding address `%s' (%s) for peer `%4s' due to peerinfo data for %llums.\n",
2457                   a2s (tname, addr, addrlen),
2458                   tname,
2459                   GNUNET_i2s (&n->id),
2460                   expiration.value);
2461 #endif
2462       fal = add_peer_address (n, tname, NULL, addr, addrlen);
2463       if (fal == NULL)
2464         {
2465           GNUNET_STATISTICS_update (stats,
2466                                     gettext_noop ("# previously validated addresses lacking transport"),
2467                                     1,
2468                                     GNUNET_NO); 
2469         }
2470       else
2471         {
2472           fal->expires = GNUNET_TIME_absolute_max (expiration,
2473                                                    fal->expires);
2474           schedule_next_ping (fal);
2475         }
2476       try = GNUNET_YES;
2477     }
2478   else
2479     {
2480       fal->expires = GNUNET_TIME_absolute_max (expiration,
2481                                                fal->expires);
2482     }
2483   if (fal == NULL)
2484     {
2485       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2486                   "Failed to add new address for `%4s'\n",
2487                   GNUNET_i2s (&n->id));
2488       return GNUNET_OK;
2489     }
2490   if (fal->validated == GNUNET_NO)
2491     {
2492       fal->validated = GNUNET_YES;  
2493       GNUNET_STATISTICS_update (stats,
2494                                 gettext_noop ("# peer addresses considered valid"),
2495                                 1,
2496                                 GNUNET_NO);      
2497     }
2498   if (try == GNUNET_YES)
2499     {
2500       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2501                   "Have new addresses, will try to trigger transmissions.\n");
2502       try_transmission_to_peer (n);
2503     }
2504   return GNUNET_OK;
2505 }
2506
2507
2508 /**
2509  * Add addresses in validated HELLO "h" to the set of addresses
2510  * we have for this peer.
2511  *
2512  * @param cls closure ('struct NeighbourList*')
2513  * @param peer id of the peer, NULL for last call
2514  * @param h hello message for the peer (can be NULL)
2515  * @param trust amount of trust we have in the peer (not used)
2516  */
2517 static void
2518 add_hello_for_peer (void *cls,
2519                     const struct GNUNET_PeerIdentity *peer,
2520                     const struct GNUNET_HELLO_Message *h, 
2521                     uint32_t trust)
2522 {
2523   struct NeighbourList *n = cls;
2524
2525   if (peer == NULL)
2526     {
2527       n->piter = NULL;
2528       return;
2529     } 
2530   if (h == NULL)
2531     return; /* no HELLO available */
2532 #if DEBUG_TRANSPORT
2533   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534               "Peerinfo had `%s' message for peer `%4s', adding existing addresses.\n",
2535               "HELLO",
2536               GNUNET_i2s (peer));
2537 #endif
2538   if (GNUNET_YES != n->public_key_valid)
2539     {
2540       GNUNET_HELLO_get_key (h, &n->publicKey);
2541       n->public_key_valid = GNUNET_YES;
2542     }
2543   GNUNET_HELLO_iterate_addresses (h,
2544                                   GNUNET_NO,
2545                                   &add_to_foreign_address_list,
2546                                   n);
2547 }
2548
2549
2550 /**
2551  * Create a fresh entry in our neighbour list for the given peer.
2552  * Will try to transmit our current HELLO to the new neighbour. 
2553  * Do not call this function directly, use 'setup_peer_check_blacklist.
2554  *
2555  * @param peer the peer for which we create the entry
2556  * @param do_hello should we schedule transmitting a HELLO
2557  * @return the new neighbour list entry
2558  */
2559 static struct NeighbourList *
2560 setup_new_neighbour (const struct GNUNET_PeerIdentity *peer,
2561                      int do_hello)
2562 {
2563   struct NeighbourList *n;
2564   struct TransportPlugin *tp;
2565   struct ReadyList *rl;
2566
2567   GNUNET_assert (our_hello != NULL);
2568   GNUNET_STATISTICS_update (stats,
2569                             gettext_noop ("# active neighbours"),
2570                             1,
2571                             GNUNET_NO);
2572   n = GNUNET_malloc (sizeof (struct NeighbourList));
2573   n->next = neighbours;
2574   neighbours = n;
2575   n->id = *peer;
2576   n->peer_timeout =
2577     GNUNET_TIME_relative_to_absolute
2578     (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2579   GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2580                                  GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2581                                  MAX_BANDWIDTH_CARRY_S);
2582   tp = plugins;
2583   while (tp != NULL)
2584     {
2585       if ((tp->api->send != NULL) && (!is_blacklisted(peer, tp)))
2586         {
2587           rl = GNUNET_malloc (sizeof (struct ReadyList));
2588           rl->neighbour = n;
2589           rl->next = n->plugins;
2590           n->plugins = rl;
2591           rl->plugin = tp;
2592           rl->addresses = NULL;
2593         }
2594       tp = tp->next;
2595     }
2596   n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2597   n->distance = -1;
2598   n->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
2599                                                   GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2600                                                   &neighbour_timeout_task, n);
2601   if (do_hello)
2602     {
2603       n->piter = GNUNET_PEERINFO_iterate (peerinfo, peer,
2604                                           0, GNUNET_TIME_UNIT_FOREVER_REL,
2605                                           &add_hello_for_peer, n);
2606       transmit_to_peer (NULL, NULL, 0,
2607                         HELLO_ADDRESS_EXPIRATION,
2608                         (const char *) our_hello, GNUNET_HELLO_size(our_hello),
2609                         GNUNET_NO, n);
2610     }
2611   return n;
2612 }
2613
2614
2615 /**
2616  * Function called after we have checked if communicating
2617  * with a given peer is acceptable.  
2618  *
2619  * @param cls closure
2620  * @param n NULL if communication is not acceptable
2621  */
2622 typedef void (*SetupContinuation)(void *cls,
2623                                   struct NeighbourList *n);
2624
2625
2626 /**
2627  * Information kept for each client registered to perform
2628  * blacklisting.
2629  */
2630 struct Blacklisters
2631 {
2632   /**
2633    * This is a linked list.
2634    */
2635   struct Blacklisters *next;
2636
2637   /**
2638    * This is a linked list.
2639    */
2640   struct Blacklisters *prev;
2641
2642   /**
2643    * Client responsible for this entry.
2644    */
2645   struct GNUNET_SERVER_Client *client;
2646
2647   /**
2648    * Blacklist check that we're currently performing.
2649    */
2650   struct BlacklistCheck *bc;
2651
2652 };
2653
2654
2655 /**
2656  * Head of DLL of blacklisting clients.
2657  */
2658 static struct Blacklisters *bl_head;
2659
2660 /**
2661  * Tail of DLL of blacklisting clients.
2662  */
2663 static struct Blacklisters *bl_tail;
2664
2665
2666 /**
2667  * Context we use when performing a blacklist check.
2668  */
2669 struct BlacklistCheck
2670 {
2671   
2672   /**
2673    * This is a linked list.
2674    */
2675   struct BlacklistCheck *next;
2676   
2677   /**
2678    * This is a linked list.
2679    */
2680   struct BlacklistCheck *prev;
2681
2682   /**
2683    * Peer being checked.
2684    */
2685   struct GNUNET_PeerIdentity peer;
2686
2687   /**
2688    * Option for setup neighbour afterwards.
2689    */
2690   int do_hello;
2691
2692   /**
2693    * Continuation to call with the result.
2694    */
2695   SetupContinuation cont;
2696
2697   /**
2698    * Closure for cont.
2699    */
2700   void *cont_cls;
2701
2702   /**
2703    * Current transmission request handle for this client, or NULL if no
2704    * request is pending.
2705    */
2706   struct GNUNET_CONNECTION_TransmitHandle *th;
2707
2708   /**
2709    * Our current position in the blacklisters list.
2710    */
2711   struct Blacklisters *bl_pos;
2712
2713   /**
2714    * Current task performing the check.
2715    */
2716   GNUNET_SCHEDULER_TaskIdentifier task;
2717
2718 };
2719
2720 /**
2721  * Head of DLL of active blacklisting queries.
2722  */
2723 static struct BlacklistCheck *bc_head;
2724
2725 /**
2726  * Tail of DLL of active blacklisting queries.
2727  */
2728 static struct BlacklistCheck *bc_tail;
2729
2730
2731 /**
2732  * Perform next action in the blacklist check.
2733  *
2734  * @param cls the 'struct BlacklistCheck*'
2735  * @param tc unused 
2736  */
2737 static void
2738 do_blacklist_check (void *cls,
2739                     const struct GNUNET_SCHEDULER_TaskContext *tc);
2740
2741
2742 /**
2743  * Transmit blacklist query to the client.
2744  *
2745  * @param cls the 'struct BlacklistCheck'
2746  * @param size number of bytes allowed
2747  * @param buf where to copy the message
2748  * @return number of bytes copied to buf
2749  */
2750 static size_t
2751 transmit_blacklist_message (void *cls,
2752                             size_t size,
2753                             void *buf)
2754 {
2755   struct BlacklistCheck *bc = cls;
2756   struct Blacklisters *bl;
2757   struct BlacklistMessage bm;
2758
2759   bc->th = NULL;
2760   if (size == 0)
2761     {
2762       GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
2763       bc->task = GNUNET_SCHEDULER_add_now (sched,
2764                                            &do_blacklist_check,
2765                                            bc);
2766       return 0;
2767     }
2768   bl = bc->bl_pos;
2769   bm.header.size = htons (sizeof (struct BlacklistMessage));
2770   bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
2771   bm.is_allowed = htonl (0);
2772   bm.peer = bc->peer;
2773   memcpy (buf, &bm, sizeof (bm)); 
2774   GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
2775   return sizeof (bm);
2776 }
2777
2778
2779 /**
2780  * Perform next action in the blacklist check.
2781  *
2782  * @param cls the 'struct BlacklistCheck*'
2783  * @param tc unused 
2784  */
2785 static void
2786 do_blacklist_check (void *cls,
2787                     const struct GNUNET_SCHEDULER_TaskContext *tc)
2788 {
2789   struct BlacklistCheck *bc = cls;
2790   struct Blacklisters *bl;
2791
2792   bc->task = GNUNET_SCHEDULER_NO_TASK;
2793   bl = bc->bl_pos;
2794   if (bl == NULL)
2795     {
2796       bc->cont (bc->cont_cls,
2797                 setup_new_neighbour (&bc->peer, bc->do_hello));         
2798       GNUNET_free (bc);
2799       return;
2800     }
2801   if (bl->bc == NULL) 
2802     {
2803       bl->bc = bc;
2804       bc->th = GNUNET_SERVER_notify_transmit_ready (bl->client,
2805                                                     sizeof (struct BlacklistMessage),
2806                                                     GNUNET_TIME_UNIT_FOREVER_REL,
2807                                                     &transmit_blacklist_message,
2808                                                     bc); 
2809     }
2810 }
2811
2812
2813 /**
2814  * Obtain a 'struct NeighbourList' for the given peer.  If such an entry
2815  * does not yet exist, check the blacklist.  If the blacklist says creating
2816  * one is acceptable, create one and call the continuation; otherwise
2817  * call the continuation with NULL.
2818  *
2819  * @param peer peer to setup or look up a struct NeighbourList for
2820  * @param do_hello should we also schedule sending our HELLO to the peer
2821  *        if this is a new record
2822  * @param cont function to call with the 'struct NeigbhbourList*'
2823  * @param cont_cls closure for cont
2824  */
2825 static void
2826 setup_peer_check_blacklist (const struct GNUNET_PeerIdentity *peer,
2827                             int do_hello,
2828                             SetupContinuation cont,
2829                             void *cont_cls)
2830 {
2831   struct NeighbourList *n;
2832   struct BlacklistCheck *bc;
2833
2834   n = find_neighbour(peer);
2835   if (n != NULL)
2836     {
2837       cont (cont_cls, n);
2838       return;
2839     }
2840   if (bl_head == NULL)
2841     {
2842       cont (cont_cls,
2843             setup_new_neighbour (peer, do_hello));
2844       return;
2845     }
2846   bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
2847   GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
2848   bc->peer = *peer;
2849   bc->do_hello = do_hello;
2850   bc->cont = cont;
2851   bc->cont_cls = cont_cls;
2852   bc->bl_pos = bl_head;
2853   bc->task = GNUNET_SCHEDULER_add_now (sched,
2854                                        &do_blacklist_check,
2855                                        bc);
2856 }
2857
2858
2859 /**
2860  * Function called with the result of querying a new blacklister about 
2861  * it being allowed (or not) to continue to talk to an existing neighbour.
2862  *
2863  * @param cls the original 'struct NeighbourList'
2864  * @param n NULL if we need to disconnect
2865  */
2866 static void
2867 confirm_or_drop_neighbour (void *cls,
2868                            struct NeighbourList *n)
2869 {
2870   struct NeighbourList * orig = cls;
2871
2872   if (n == NULL)
2873     disconnect_neighbour (orig, GNUNET_NO);
2874 }
2875
2876
2877 /**
2878  * Handle a request to start a blacklist.
2879  *
2880  * @param cls closure (always NULL)
2881  * @param client identification of the client
2882  * @param message the actual message
2883  */
2884 static void
2885 handle_blacklist_init (void *cls,
2886                        struct GNUNET_SERVER_Client *client,
2887                        const struct GNUNET_MessageHeader *message)
2888 {
2889   struct Blacklisters *bl;
2890   struct BlacklistCheck *bc;
2891   struct NeighbourList *n;
2892
2893   bl = bl_head;
2894   while (bl != NULL)
2895     {
2896       if (bl->client == client)
2897         {
2898           GNUNET_break (0);
2899           GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2900           return;
2901         }
2902       bl = bl->next;
2903     }
2904   bl = GNUNET_malloc (sizeof (struct Blacklisters));
2905   bl->client = client;
2906   GNUNET_SERVER_client_keep (client);
2907   GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
2908   /* confirm that all existing connections are OK! */
2909   n = neighbours;
2910   while (NULL != n)
2911     {
2912       bc = GNUNET_malloc (sizeof (struct BlacklistCheck));
2913       GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
2914       bc->peer = n->id;
2915       bc->do_hello = GNUNET_NO;
2916       bc->cont = &confirm_or_drop_neighbour;
2917       bc->cont_cls = n;
2918       bc->bl_pos = bl;
2919       if (n == neighbours) /* all would wait for the same client, no need to
2920                               create more than just the first task right now */
2921         bc->task = GNUNET_SCHEDULER_add_now (sched,
2922                                              &do_blacklist_check,
2923                                              bc);
2924       n = n->next;
2925     }
2926 }
2927
2928
2929 /**
2930  * Handle a request to blacklist a peer.
2931  *
2932  * @param cls closure (always NULL)
2933  * @param client identification of the client
2934  * @param message the actual message
2935  */
2936 static void
2937 handle_blacklist_reply (void *cls,
2938                         struct GNUNET_SERVER_Client *client,
2939                         const struct GNUNET_MessageHeader *message)
2940 {
2941   const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
2942   struct Blacklisters *bl;
2943   struct BlacklistCheck *bc;
2944
2945   bl = bl_head;
2946   while ( (bl != NULL) &&
2947           (bl->client != client) )
2948     bl = bl->next;  
2949   if (bl == NULL)
2950     {
2951       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2952       return;
2953     }
2954   bc = bl->bc;
2955   bl->bc = NULL;  
2956   if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
2957     {    
2958       bc->cont (bc->cont_cls, NULL);
2959       GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
2960       GNUNET_free (bc);
2961     }
2962   else
2963     {
2964       bc->bl_pos = bc->bl_pos->next;
2965       bc->task = GNUNET_SCHEDULER_add_now (sched,
2966                                            &do_blacklist_check,
2967                                            bc);      
2968     }
2969   /* check if any other bc's are waiting for this blacklister */
2970   bc = bc_head;
2971   while (bc != NULL)
2972     {
2973       if ( (bc->bl_pos == bl) &&
2974            (GNUNET_SCHEDULER_NO_TASK == bc->task) )
2975         bc->task = GNUNET_SCHEDULER_add_now (sched,
2976                                              &do_blacklist_check,
2977                                              bc);      
2978       bc = bc->next;
2979     }
2980 }
2981
2982
2983 /**
2984  * Send periodic PING messages to a give foreign address.
2985  *
2986  * @param cls our 'struct PeriodicValidationContext*'
2987  * @param tc task context
2988  */
2989 static void 
2990 send_periodic_ping (void *cls, 
2991                     const struct GNUNET_SCHEDULER_TaskContext *tc)
2992 {
2993   struct ForeignAddressList *peer_address = cls;
2994   struct TransportPlugin *tp;
2995   struct ValidationEntry *va;
2996   struct NeighbourList *neighbour;
2997   struct TransportPingMessage ping;
2998   struct CheckAddressExistsClosure caec;
2999   char * message_buf;
3000   uint16_t hello_size;
3001   size_t tsize;
3002
3003   peer_address->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3004   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3005     return; 
3006   tp = peer_address->ready_list->plugin;
3007   neighbour = peer_address->ready_list->neighbour;
3008   if (GNUNET_YES != neighbour->public_key_valid)
3009     {
3010       /* no public key yet, try again later */
3011       schedule_next_ping (peer_address);     
3012       return;
3013     }
3014   caec.addr = peer_address->addr;
3015   caec.addrlen = peer_address->addrlen;
3016   caec.tname = tp->short_name;
3017   caec.session = peer_address->session;
3018   caec.exists = GNUNET_NO;
3019   GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3020                                          &check_address_exists,
3021                                          &caec);
3022   if (caec.exists == GNUNET_YES)
3023     {
3024       /* During validation attempts we will likely trigger the other
3025          peer trying to validate our address which in turn will cause
3026          it to send us its HELLO, so we expect to hit this case rather
3027          frequently.  Only print something if we are very verbose. */
3028 #if DEBUG_TRANSPORT > 1
3029       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3030                   "Some validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3031                   (peer_address->addr != NULL)
3032                   ? a2s (peer_address->plugin->short_name,
3033                          peer_address->addr,
3034                          peer_address->addrlen)
3035                   : "<inbound>",
3036                   tp->short_name,
3037                   GNUNET_i2s (&neighbour->id));
3038 #endif
3039       schedule_next_ping (peer_address);     
3040       return;
3041     }
3042   va = GNUNET_malloc (sizeof (struct ValidationEntry) + peer_address->addrlen);
3043   va->transport_name = GNUNET_strdup (tp->short_name);
3044   va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3045                                             (unsigned int) -1);
3046   va->send_time = GNUNET_TIME_absolute_get();
3047   va->session = peer_address->session;
3048   if (peer_address->addr != NULL)
3049     {
3050       va->addr = (const void*) &va[1];
3051       memcpy (&va[1], peer_address->addr, peer_address->addrlen);
3052       va->addrlen = peer_address->addrlen;
3053     }
3054   memcpy(&va->publicKey,
3055          &neighbour->publicKey, 
3056          sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3057
3058   va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
3059                                                    HELLO_VERIFICATION_TIMEOUT,
3060                                                    &timeout_hello_validation,
3061                                                    va);
3062   GNUNET_CONTAINER_multihashmap_put (validation_map,
3063                                      &neighbour->id.hashPubKey,
3064                                      va,
3065                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3066   hello_size = GNUNET_HELLO_size(our_hello);
3067   tsize = sizeof(struct TransportPingMessage) + hello_size;
3068   message_buf = GNUNET_malloc(tsize);
3069   ping.challenge = htonl(va->challenge);
3070   ping.header.size = htons(sizeof(struct TransportPingMessage));
3071   ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3072   memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3073   memcpy(message_buf, our_hello, hello_size);
3074   memcpy(&message_buf[hello_size],
3075          &ping,
3076          sizeof(struct TransportPingMessage));
3077 #if DEBUG_TRANSPORT_REVALIDATION
3078   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3079               "Performing re-validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
3080               (peer_address->addr != NULL) 
3081               ? a2s (peer_address->plugin->short_name,
3082                      peer_address->addr,
3083                      peer_address->addrlen)
3084               : "<inbound>",
3085               tp->short_name,
3086               GNUNET_i2s (&neighbour->id),
3087               "HELLO", hello_size,
3088               "PING", sizeof (struct TransportPingMessage));
3089 #endif
3090   GNUNET_STATISTICS_update (stats,
3091                             gettext_noop ("# PING messages sent for re-validation"),
3092                             1,
3093                             GNUNET_NO);
3094   transmit_to_peer (NULL, peer_address,
3095                     GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3096                     HELLO_VERIFICATION_TIMEOUT,
3097                     message_buf, tsize,
3098                     GNUNET_YES, neighbour);
3099   GNUNET_free(message_buf);
3100   schedule_next_ping (peer_address);
3101 }
3102
3103
3104 /**
3105  * Schedule the job that will cause us to send a PING to the
3106  * foreign address to evaluate its validity and latency.
3107  *
3108  * @param fal address to PING
3109  */
3110 static void
3111 schedule_next_ping (struct ForeignAddressList *fal)
3112 {
3113   struct GNUNET_TIME_Relative delay;
3114
3115   if (fal->revalidate_task != GNUNET_SCHEDULER_NO_TASK)
3116     return;
3117   delay = GNUNET_TIME_absolute_get_remaining (fal->expires);
3118   delay.value /= 2; /* do before expiration */
3119   delay = GNUNET_TIME_relative_min (delay,
3120                                     LATENCY_EVALUATION_MAX_DELAY);
3121   if (GNUNET_YES != fal->estimated)
3122     {
3123       delay = GNUNET_TIME_UNIT_ZERO;
3124       fal->estimated = GNUNET_YES;
3125     }                               
3126   if (GNUNET_YES == fal->connected)
3127     {
3128       delay = GNUNET_TIME_relative_min (delay,
3129                                         CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
3130     }  
3131   /* FIXME: also adjust delay based on how close the last
3132      observed latency is to the latency of the best alternative */
3133   /* bound how fast we can go */
3134   delay = GNUNET_TIME_relative_max (delay,
3135                                     GNUNET_TIME_UNIT_SECONDS);
3136   /* randomize a bit (to avoid doing all at the same time) */
3137   delay.value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
3138   fal->revalidate_task = GNUNET_SCHEDULER_add_delayed(sched, 
3139                                                       delay,
3140                                                       &send_periodic_ping, 
3141                                                       fal);
3142 }
3143
3144
3145
3146
3147 /**
3148  * Function that will be called if we receive some payload
3149  * from another peer.
3150  *
3151  * @param message the payload
3152  * @param n peer who claimed to be the sender
3153  */
3154 static void
3155 handle_payload_message (const struct GNUNET_MessageHeader *message,
3156                         struct NeighbourList *n)
3157 {
3158   struct InboundMessage *im;
3159   struct TransportClient *cpos;
3160   uint16_t msize;
3161
3162   msize = ntohs (message->size);
3163   if (n->received_pong == GNUNET_NO)
3164     {
3165       GNUNET_free_non_null (n->pre_connect_message_buffer);
3166       n->pre_connect_message_buffer = GNUNET_malloc (msize);
3167       memcpy (n->pre_connect_message_buffer, message, msize);
3168       return;
3169     }
3170 #if DEBUG_TRANSPORT
3171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3172               "Received message of type %u from `%4s', sending to all clients.\n",
3173               ntohs (message->type), 
3174               GNUNET_i2s (&n->id));
3175 #endif
3176   if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3177                                                       (ssize_t) msize))
3178     {
3179       n->quota_violation_count++;
3180 #if DEBUG_TRANSPORT
3181       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,                        
3182                   "Bandwidth quota (%u b/s) violation detected (total of %u).\n", 
3183                   n->in_tracker.available_bytes_per_s__,
3184                   n->quota_violation_count);
3185 #endif
3186       /* Discount 32k per violation */
3187       GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3188                                         - 32 * 1024);           
3189     }
3190   else 
3191     {
3192       if (n->quota_violation_count > 0)
3193         {
3194           /* try to add 32k back */
3195           GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker,
3196                                             32 * 1024);
3197           n->quota_violation_count--;
3198         }
3199     }
3200   GNUNET_STATISTICS_update (stats,
3201                             gettext_noop ("# payload received from other peers"),
3202                             msize,
3203                             GNUNET_NO);
3204   /* transmit message to all clients */
3205   im = GNUNET_malloc (sizeof (struct InboundMessage) + msize);
3206   im->header.size = htons (sizeof (struct InboundMessage) + msize);
3207   im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
3208   im->latency = GNUNET_TIME_relative_hton (n->latency);
3209   im->peer = n->id;
3210   im->distance = ntohl(n->distance);
3211   memcpy (&im[1], message, msize);
3212   cpos = clients;
3213   while (cpos != NULL)
3214     {
3215       transmit_to_client (cpos, &im->header, GNUNET_YES);
3216       cpos = cpos->next;
3217     }
3218   GNUNET_free (im);
3219 }
3220
3221
3222 /**
3223  * Iterator over hash map entries.  Checks if the given validation
3224  * entry is for the same challenge as what is given in the PONG.
3225  *
3226  * @param cls the 'struct TransportPongMessage*'
3227  * @param key peer identity
3228  * @param value value in the hash map ('struct ValidationEntry')
3229  * @return GNUNET_YES if we should continue to
3230  *         iterate (mismatch), GNUNET_NO if not (entry matched)
3231  */
3232 static int
3233 check_pending_validation (void *cls,
3234                           const GNUNET_HashCode * key,
3235                           void *value)
3236 {
3237   const struct TransportPongMessage *pong = cls;
3238   struct ValidationEntry *ve = value;
3239   struct AddValidatedAddressContext avac;
3240   unsigned int challenge = ntohl(pong->challenge);
3241   struct GNUNET_HELLO_Message *hello;
3242   struct GNUNET_PeerIdentity target;
3243   struct NeighbourList *n;
3244   struct ForeignAddressList *fal;
3245   struct GNUNET_MessageHeader *prem;
3246
3247   if (ve->challenge != challenge)
3248     return GNUNET_YES;
3249   if (GNUNET_OK !=
3250       GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PING,
3251                                 &pong->purpose, 
3252                                 &pong->signature,
3253                                 &ve->publicKey))
3254     {
3255       GNUNET_break_op (0);
3256       return GNUNET_YES;
3257     }
3258
3259 #if DEBUG_TRANSPORT
3260   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3261               "Confirmed validity of address, peer `%4s' has address `%s' (%s).\n",
3262               GNUNET_h2s (key),
3263               (ve->addr != NULL) 
3264               ? a2s (ve->transport_name,
3265                      (const struct sockaddr *) ve->addr,
3266                      ve->addrlen)
3267               : "<inbound>",
3268               ve->transport_name);
3269 #endif
3270   GNUNET_STATISTICS_update (stats,
3271                             gettext_noop ("# address validation successes"),
3272                             1,
3273                             GNUNET_NO);
3274   /* create the updated HELLO */
3275   GNUNET_CRYPTO_hash (&ve->publicKey,
3276                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3277                       &target.hashPubKey);
3278   if (ve->addr != NULL)
3279     {
3280       avac.done = GNUNET_NO;
3281       avac.ve = ve;
3282       hello = GNUNET_HELLO_create (&ve->publicKey,
3283                                    &add_validated_address,
3284                                    &avac);
3285       GNUNET_PEERINFO_add_peer (peerinfo,
3286                                 hello);
3287       GNUNET_free (hello);
3288     }
3289   n = find_neighbour (&target);
3290   if (n != NULL)
3291     {
3292       n->publicKey = ve->publicKey;
3293       n->public_key_valid = GNUNET_YES;
3294       fal = add_peer_address (n,
3295                               ve->transport_name,
3296                               ve->session,
3297                               ve->addr,
3298                               ve->addrlen);
3299       GNUNET_assert (fal != NULL);
3300       fal->expires = GNUNET_TIME_relative_to_absolute (HELLO_ADDRESS_EXPIRATION);
3301       fal->validated = GNUNET_YES;
3302       mark_address_connected (fal);
3303       GNUNET_STATISTICS_update (stats,
3304                                 gettext_noop ("# peer addresses considered valid"),
3305                                 1,
3306                                 GNUNET_NO);      
3307       fal->latency = GNUNET_TIME_absolute_get_duration (ve->send_time);
3308       schedule_next_ping (fal);
3309       if (n->latency.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
3310         n->latency = fal->latency;
3311       else
3312         n->latency.value = (fal->latency.value + n->latency.value) / 2;
3313
3314       n->distance = fal->distance;
3315       if (GNUNET_NO == n->received_pong)
3316         {
3317           n->received_pong = GNUNET_YES;
3318           notify_clients_connect (&target, n->latency, n->distance);
3319           if (NULL != (prem = n->pre_connect_message_buffer))
3320             {
3321               n->pre_connect_message_buffer = NULL;
3322               handle_payload_message (prem, n);
3323               GNUNET_free (prem);
3324             }
3325         }
3326       if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
3327         {
3328           GNUNET_SCHEDULER_cancel (sched,
3329                                    n->retry_task);
3330           n->retry_task = GNUNET_SCHEDULER_NO_TASK;
3331           try_transmission_to_peer (n);
3332         }
3333     }
3334
3335   /* clean up validation entry */
3336   GNUNET_assert (GNUNET_YES ==
3337                  GNUNET_CONTAINER_multihashmap_remove (validation_map,
3338                                                        key,
3339                                                        ve));
3340   GNUNET_SCHEDULER_cancel (sched,
3341                            ve->timeout_task);
3342   GNUNET_free (ve->transport_name);
3343   GNUNET_free (ve);
3344   return GNUNET_NO;
3345 }
3346
3347
3348 /**
3349  * Function that will be called if we receive a validation
3350  * of an address challenge that we transmitted to another
3351  * peer.  Note that the validation should only be considered
3352  * acceptable if the challenge matches AND if the sender
3353  * address is at least a plausible address for this peer
3354  * (otherwise we may be seeing a MiM attack).
3355  *
3356  * @param cls closure
3357  * @param message the pong message
3358  * @param peer who responded to our challenge
3359  * @param sender_address string describing our sender address (as observed
3360  *         by the other peer in binary format)
3361  * @param sender_address_len number of bytes in 'sender_address'
3362  */
3363 static void
3364 handle_pong (void *cls, const struct GNUNET_MessageHeader *message,
3365              const struct GNUNET_PeerIdentity *peer,
3366              const char *sender_address,
3367              size_t sender_address_len)
3368 {
3369 #if DEBUG_TRANSPORT > 1
3370   /* we get tons of these that just get discarded, only log
3371      if we are quite verbose */
3372   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3373               "Receiving `%s' message from `%4s'.\n", "PONG",
3374               GNUNET_i2s (peer));
3375 #endif
3376   GNUNET_STATISTICS_update (stats,
3377                             gettext_noop ("# PONG messages received"),
3378                             1,
3379                             GNUNET_NO);
3380   if (GNUNET_SYSERR !=
3381       GNUNET_CONTAINER_multihashmap_get_multiple (validation_map,
3382                                                   &peer->hashPubKey,
3383                                                   &check_pending_validation,
3384                                                   (void*) message))
3385     {
3386       /* This is *expected* to happen a lot since we send
3387          PONGs to *all* known addresses of the sender of
3388          the PING, so most likely we get multiple PONGs
3389          per PING, and all but the first PONG will end up
3390          here. So really we should not print anything here
3391          unless we want to be very, very verbose... */
3392 #if DEBUG_TRANSPORT > 2
3393       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394                   "Received `%s' message from `%4s' but have no record of a matching `%s' message. Ignoring.\n",
3395                   "PONG",
3396                   GNUNET_i2s (peer),
3397                   "PING");
3398 #endif
3399       return;
3400     }
3401
3402 }
3403
3404
3405 /**
3406  * Try to validate a neighbour's address by sending him our HELLO and a PING.
3407  *
3408  * @param cls the 'struct ValidationEntry*'
3409  * @param neighbour neighbour to validate, NULL if validation failed
3410  */
3411 static void
3412 transmit_hello_and_ping (void *cls,
3413                          struct NeighbourList *neighbour)
3414 {
3415   struct ValidationEntry *va = cls;
3416   struct ForeignAddressList *peer_address;
3417   struct TransportPingMessage ping;
3418   uint16_t hello_size;
3419   size_t tsize;
3420   char * message_buf;
3421
3422   if (neighbour == NULL)
3423     {
3424       /* FIXME: stats... */
3425       GNUNET_free (va->transport_name);
3426       GNUNET_free (va);
3427       return;
3428     }
3429   neighbour->publicKey = va->publicKey;
3430   neighbour->public_key_valid = GNUNET_YES;
3431   peer_address = add_peer_address (neighbour,
3432                                    va->transport_name, NULL,
3433                                    (const void*) &va[1],
3434                                    va->addrlen);
3435   if (peer_address == NULL)
3436     {
3437       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3438                   "Failed to add peer `%4s' for plugin `%s'\n",
3439                   GNUNET_i2s (&neighbour->id), 
3440                   va->transport_name);
3441       GNUNET_free (va->transport_name);
3442       GNUNET_free (va);
3443       return;
3444     }
3445   hello_size = GNUNET_HELLO_size(our_hello);
3446   tsize = sizeof(struct TransportPingMessage) + hello_size;
3447   message_buf = GNUNET_malloc(tsize);
3448   ping.challenge = htonl(va->challenge);
3449   ping.header.size = htons(sizeof(struct TransportPingMessage));
3450   ping.header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_PING);
3451   memcpy(&ping.target, &neighbour->id, sizeof(struct GNUNET_PeerIdentity));
3452   memcpy(message_buf, our_hello, hello_size);
3453   memcpy(&message_buf[hello_size],
3454          &ping,
3455          sizeof(struct TransportPingMessage));
3456 #if DEBUG_TRANSPORT
3457   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3458               "Performing validation of address `%s' via `%s' for peer `%4s' sending `%s' (%u bytes) and `%s' (%u bytes)\n",
3459               a2s (va->transport_name,
3460                    (const void*) &va[1], va->addrlen),
3461               va->transport_name,
3462               GNUNET_i2s (&neighbour->id),
3463               "HELLO", hello_size,
3464               "PING", sizeof (struct TransportPingMessage));
3465 #endif
3466   GNUNET_STATISTICS_update (stats,
3467                             gettext_noop ("# PING messages sent for initial validation"),
3468                             1,
3469                             GNUNET_NO);      
3470   transmit_to_peer (NULL, peer_address,
3471                     GNUNET_SCHEDULER_PRIORITY_DEFAULT,
3472                     HELLO_VERIFICATION_TIMEOUT,
3473                     message_buf, tsize,
3474                     GNUNET_YES, neighbour);
3475   GNUNET_free(message_buf);
3476 }
3477
3478
3479 /**
3480  * Check if the given address is already being validated; if not,
3481  * append the given address to the list of entries that are being be
3482  * validated and initiate validation.
3483  *
3484  * @param cls closure ('struct CheckHelloValidatedContext *')
3485  * @param tname name of the transport
3486  * @param expiration expiration time
3487  * @param addr the address
3488  * @param addrlen length of the address
3489  * @return GNUNET_OK (always)
3490  */
3491 static int
3492 run_validation (void *cls,
3493                 const char *tname,
3494                 struct GNUNET_TIME_Absolute expiration,
3495                 const void *addr, size_t addrlen)
3496 {
3497   struct CheckHelloValidatedContext *chvc = cls;
3498   struct GNUNET_PeerIdentity id;
3499   struct TransportPlugin *tp;
3500   struct ValidationEntry *va;
3501   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
3502   struct CheckAddressExistsClosure caec;
3503   struct OwnAddressList *oal;
3504
3505   GNUNET_assert (addr != NULL);
3506   GNUNET_STATISTICS_update (stats,
3507                             gettext_noop ("# peer addresses scheduled for validation"),
3508                             1,
3509                             GNUNET_NO);      
3510   tp = find_transport (tname);
3511   if (tp == NULL)
3512     {
3513       GNUNET_log (GNUNET_ERROR_TYPE_INFO |
3514                   GNUNET_ERROR_TYPE_BULK,
3515                   _
3516                   ("Transport `%s' not loaded, will not try to validate peer address using this transport.\n"),
3517                   tname);
3518       GNUNET_STATISTICS_update (stats,
3519                                 gettext_noop ("# peer addresses not validated (plugin not available)"),
3520                                 1,
3521                                 GNUNET_NO);      
3522       return GNUNET_OK;
3523     }
3524   /* check if this is one of our own addresses */
3525   oal = tp->addresses;
3526   while (NULL != oal)
3527     {
3528       if ( (oal->addrlen == addrlen) &&
3529            (0 == memcmp (oal->addr,
3530                          addr,
3531                          addrlen)) )
3532         {
3533           /* not plausible, this address is equivalent to our own address! */
3534           GNUNET_STATISTICS_update (stats,
3535                                     gettext_noop ("# peer addresses not validated (loopback)"),
3536                                     1,
3537                                     GNUNET_NO);      
3538           return GNUNET_OK;
3539         }
3540       oal = oal->next;
3541     }
3542   GNUNET_HELLO_get_key (chvc->hello, &pk);
3543   GNUNET_CRYPTO_hash (&pk,
3544                       sizeof (struct
3545                               GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3546                       &id.hashPubKey);
3547
3548   if (is_blacklisted(&id, tp))
3549     {
3550 #if DEBUG_TRANSPORT
3551       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3552                   _("Attempted to validate blacklisted peer `%s' using `%s'!\n"), GNUNET_i2s(&id), tname);
3553 #endif
3554       return GNUNET_OK;
3555     }
3556
3557   caec.addr = addr;
3558   caec.addrlen = addrlen;
3559   caec.session = NULL;
3560   caec.tname = tname;
3561   caec.exists = GNUNET_NO;
3562   GNUNET_CONTAINER_multihashmap_iterate (validation_map,
3563                                          &check_address_exists,
3564                                          &caec);
3565   if (caec.exists == GNUNET_YES)
3566     {
3567       /* During validation attempts we will likely trigger the other
3568          peer trying to validate our address which in turn will cause
3569          it to send us its HELLO, so we expect to hit this case rather
3570          frequently.  Only print something if we are very verbose. */
3571 #if DEBUG_TRANSPORT > 1
3572       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573                   "Validation of address `%s' via `%s' for peer `%4s' already in progress.\n",
3574                   a2s (tname, addr, addrlen),
3575                   tname,
3576                   GNUNET_i2s (&id));
3577 #endif
3578       GNUNET_STATISTICS_update (stats,
3579                                 gettext_noop ("# peer addresses not validated (in progress)"),
3580                                 1,
3581                                 GNUNET_NO);      
3582       return GNUNET_OK;
3583     }
3584   va = GNUNET_malloc (sizeof (struct ValidationEntry) + addrlen);
3585   va->transport_name = GNUNET_strdup (tname);
3586   va->challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
3587                                             (unsigned int) -1);
3588   va->send_time = GNUNET_TIME_absolute_get();
3589   va->addr = (const void*) &va[1];
3590   memcpy (&va[1], addr, addrlen);
3591   va->addrlen = addrlen;
3592   GNUNET_HELLO_get_key (chvc->hello,
3593                         &va->publicKey);
3594   va->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
3595                                                    HELLO_VERIFICATION_TIMEOUT,
3596                                                    &timeout_hello_validation,
3597                                                    va);
3598   GNUNET_CONTAINER_multihashmap_put (validation_map,
3599                                      &id.hashPubKey,
3600                                      va,
3601                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3602   setup_peer_check_blacklist (&id, GNUNET_NO,
3603                               &transmit_hello_and_ping,
3604                               va);
3605   return GNUNET_OK;
3606 }
3607
3608
3609 /**
3610  * Check if addresses in validated hello "h" overlap with
3611  * those in "chvc->hello" and validate the rest.
3612  *
3613  * @param cls closure
3614  * @param peer id of the peer, NULL for last call
3615  * @param h hello message for the peer (can be NULL)
3616  * @param trust amount of trust we have in the peer (not used)
3617  */
3618 static void
3619 check_hello_validated (void *cls,
3620                        const struct GNUNET_PeerIdentity *peer,
3621                        const struct GNUNET_HELLO_Message *h, 
3622                        uint32_t trust)
3623 {
3624   struct CheckHelloValidatedContext *chvc = cls;
3625   struct GNUNET_HELLO_Message *plain_hello;
3626   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
3627   struct GNUNET_PeerIdentity target;
3628   struct NeighbourList *n;
3629
3630   if (peer == NULL)
3631     {
3632       chvc->piter = NULL;
3633       GNUNET_CONTAINER_DLL_remove (chvc_head,
3634                                    chvc_tail,
3635                                    chvc);
3636       if (GNUNET_NO == chvc->hello_known)
3637         {
3638           /* notify PEERINFO about the peer now, so that we at least
3639              have the public key if some other component needs it */
3640           GNUNET_HELLO_get_key (chvc->hello, &pk);
3641           GNUNET_CRYPTO_hash (&pk,
3642                               sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3643                               &target.hashPubKey);
3644           plain_hello = GNUNET_HELLO_create (&pk,
3645                                              NULL, 
3646                                              NULL);
3647           GNUNET_PEERINFO_add_peer (peerinfo, plain_hello);
3648           GNUNET_free (plain_hello);
3649 #if DEBUG_TRANSPORT
3650           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3651                       "Peerinfo had no `%s' message for peer `%4s', full validation needed.\n",
3652                       "HELLO",
3653                       GNUNET_i2s (&target));
3654 #endif
3655           GNUNET_STATISTICS_update (stats,
3656                                     gettext_noop ("# new HELLOs requiring full validation"),
3657                                     1,
3658                                     GNUNET_NO);      
3659           GNUNET_HELLO_iterate_addresses (chvc->hello,
3660                                           GNUNET_NO, 
3661                                           &run_validation, 
3662                                           chvc);
3663         }
3664       else
3665         {
3666           GNUNET_STATISTICS_update (stats,
3667                                     gettext_noop ("# duplicate HELLO (peer known)"),
3668                                     1,
3669                                     GNUNET_NO);      
3670         }
3671       GNUNET_free (chvc);
3672       return;
3673     } 
3674   if (h == NULL)
3675     return;
3676 #if DEBUG_TRANSPORT
3677   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3678               "Peerinfo had `%s' message for peer `%4s', validating only new addresses.\n",
3679               "HELLO",
3680               GNUNET_i2s (peer));
3681 #endif
3682   chvc->hello_known = GNUNET_YES;
3683   n = find_neighbour (peer);
3684   if (n != NULL)
3685     {
3686       GNUNET_HELLO_iterate_addresses (h,
3687                                       GNUNET_NO,
3688                                       &add_to_foreign_address_list,
3689                                       n);
3690       try_transmission_to_peer (n);
3691     }
3692   else
3693     {
3694       GNUNET_STATISTICS_update (stats,
3695                                 gettext_noop ("# no existing neighbour record (validating HELLO)"),
3696                                 1,
3697                                 GNUNET_NO);      
3698     }
3699   GNUNET_STATISTICS_update (stats,
3700                             gettext_noop ("# HELLO validations (update case)"),
3701                             1,
3702                             GNUNET_NO);      
3703   GNUNET_HELLO_iterate_new_addresses (chvc->hello,
3704                                       h,
3705                                       GNUNET_TIME_relative_to_absolute (HELLO_REVALIDATION_START_TIME),
3706                                       &run_validation, 
3707                                       chvc);
3708 }
3709
3710
3711 /**
3712  * Process HELLO-message.
3713  *
3714  * @param plugin transport involved, may be NULL
3715  * @param message the actual message
3716  * @return GNUNET_OK if the HELLO was well-formed, GNUNET_SYSERR otherwise
3717  */
3718 static int
3719 process_hello (struct TransportPlugin *plugin,
3720                const struct GNUNET_MessageHeader *message)
3721 {
3722   uint16_t hsize;
3723   struct GNUNET_PeerIdentity target;
3724   const struct GNUNET_HELLO_Message *hello;
3725   struct CheckHelloValidatedContext *chvc;
3726   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
3727
3728   hsize = ntohs (message->size);
3729   if ((ntohs (message->type) != GNUNET_MESSAGE_TYPE_HELLO) ||
3730       (hsize < sizeof (struct GNUNET_MessageHeader)))
3731     {
3732       GNUNET_break (0);
3733       return GNUNET_SYSERR;
3734     }
3735   GNUNET_STATISTICS_update (stats,
3736                             gettext_noop ("# HELLOs received for validation"),
3737                             1,
3738                             GNUNET_NO);      
3739   /* first, check if load is too high */
3740   if (GNUNET_SCHEDULER_get_load (sched,
3741                                  GNUNET_SCHEDULER_PRIORITY_BACKGROUND) > MAX_HELLO_LOAD)
3742     {
3743       GNUNET_STATISTICS_update (stats,
3744                                 gettext_noop ("# HELLOs ignored due to high load"),
3745                                 1,
3746                                 GNUNET_NO);      
3747       return GNUNET_OK;
3748     }
3749   hello = (const struct GNUNET_HELLO_Message *) message;
3750   if (GNUNET_OK != GNUNET_HELLO_get_key (hello, &publicKey))
3751     {
3752       GNUNET_break_op (0);
3753       return GNUNET_SYSERR;
3754     }
3755   GNUNET_CRYPTO_hash (&publicKey,
3756                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3757                       &target.hashPubKey);
3758   if (0 == memcmp (&my_identity,
3759                    &target,
3760                    sizeof (struct GNUNET_PeerIdentity)))
3761     {
3762       GNUNET_STATISTICS_update (stats,
3763                                 gettext_noop ("# HELLOs ignored for validation (is my own HELLO)"),
3764                                 1,
3765                                 GNUNET_NO);      
3766       return GNUNET_OK;      
3767     }
3768 #if DEBUG_TRANSPORT > 1
3769   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3770               "Processing `%s' message for `%4s' of size %u\n",
3771               "HELLO", 
3772               GNUNET_i2s (&target), 
3773               GNUNET_HELLO_size(hello));
3774 #endif
3775   chvc = GNUNET_malloc (sizeof (struct CheckHelloValidatedContext) + hsize);
3776   chvc->hello = (const struct GNUNET_HELLO_Message *) &chvc[1];
3777   memcpy (&chvc[1], hello, hsize);
3778   GNUNET_CONTAINER_DLL_insert (chvc_head,
3779                                chvc_tail,
3780                                chvc);
3781   /* finally, check if HELLO was previously validated
3782      (continuation will then schedule actual validation) */
3783   chvc->piter = GNUNET_PEERINFO_iterate (peerinfo,
3784                                          &target,
3785                                          0,
3786                                          HELLO_VERIFICATION_TIMEOUT,
3787                                          &check_hello_validated, chvc);
3788   return GNUNET_OK;
3789 }
3790
3791
3792 /**
3793  * The peer specified by the given neighbour has timed-out or a plugin
3794  * has disconnected.  We may either need to do nothing (other plugins
3795  * still up), or trigger a full disconnect and clean up.  This
3796  * function updates our state and does the necessary notifications.
3797  * Also notifies our clients that the neighbour is now officially
3798  * gone.
3799  *
3800  * @param n the neighbour list entry for the peer
3801  * @param check should we just check if all plugins
3802  *        disconnected or must we ask all plugins to
3803  *        disconnect?
3804  */
3805 static void
3806 disconnect_neighbour (struct NeighbourList *n, int check)
3807 {
3808   struct ReadyList *rpos;
3809   struct NeighbourList *npos;
3810   struct NeighbourList *nprev;
3811   struct MessageQueue *mq;
3812   struct ForeignAddressList *peer_addresses;
3813   struct ForeignAddressList *peer_pos;
3814
3815   if (GNUNET_YES == check)
3816     {
3817       rpos = n->plugins;
3818       while (NULL != rpos)
3819         {
3820           peer_addresses = rpos->addresses;
3821           while (peer_addresses != NULL)
3822             {
3823               if (GNUNET_YES == peer_addresses->connected)
3824                 return;             /* still connected */
3825               peer_addresses = peer_addresses->next;
3826             }
3827           rpos = rpos->next;
3828         }
3829     }
3830 #if DEBUG_TRANSPORT
3831   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3832               "Disconnecting from `%4s'\n",
3833               GNUNET_i2s (&n->id));
3834 #endif
3835   /* remove n from neighbours list */
3836   nprev = NULL;
3837   npos = neighbours;
3838   while ((npos != NULL) && (npos != n))
3839     {
3840       nprev = npos;
3841       npos = npos->next;
3842     }
3843   GNUNET_assert (npos != NULL);
3844   if (nprev == NULL)
3845     neighbours = n->next;
3846   else
3847     nprev->next = n->next;
3848
3849   /* notify all clients about disconnect */
3850   if (GNUNET_YES == n->received_pong)
3851     notify_clients_disconnect (&n->id);
3852
3853   /* clean up all plugins, cancel connections and pending transmissions */
3854   while (NULL != (rpos = n->plugins))
3855     {
3856       n->plugins = rpos->next;
3857       rpos->plugin->api->disconnect (rpos->plugin->api->cls, &n->id);
3858       while (rpos->addresses != NULL)
3859         {
3860           peer_pos = rpos->addresses;
3861           rpos->addresses = peer_pos->next;
3862           if (peer_pos->connected == GNUNET_YES)
3863             GNUNET_STATISTICS_update (stats,
3864                                       gettext_noop ("# connected addresses"),
3865                                       -1,
3866                                       GNUNET_NO); 
3867           if (GNUNET_YES == peer_pos->validated)
3868             GNUNET_STATISTICS_update (stats,
3869                                       gettext_noop ("# peer addresses considered valid"),
3870                                       -1,
3871                                       GNUNET_NO);      
3872           if (GNUNET_SCHEDULER_NO_TASK != peer_pos->revalidate_task)
3873             {
3874               GNUNET_SCHEDULER_cancel (sched,
3875                                        peer_pos->revalidate_task);
3876               peer_pos->revalidate_task = GNUNET_SCHEDULER_NO_TASK;
3877             }
3878           GNUNET_free(peer_pos);
3879         }
3880       GNUNET_free (rpos);
3881     }
3882
3883   /* free all messages on the queue */
3884   while (NULL != (mq = n->messages_head))
3885     {
3886       GNUNET_STATISTICS_update (stats,
3887                                 gettext_noop ("# bytes in message queue for other peers"),
3888                                 - (int64_t) mq->message_buf_size,
3889                                 GNUNET_NO);
3890       GNUNET_STATISTICS_update (stats,
3891                                 gettext_noop ("# bytes discarded due to disconnect"),
3892                                 mq->message_buf_size,
3893                                 GNUNET_NO);
3894       GNUNET_CONTAINER_DLL_remove (n->messages_head,
3895                                    n->messages_tail,
3896                                    mq);
3897       GNUNET_assert (0 == memcmp(&mq->neighbour_id, 
3898                                  &n->id,
3899                                  sizeof(struct GNUNET_PeerIdentity)));
3900       GNUNET_free (mq);
3901     }
3902   if (n->timeout_task != GNUNET_SCHEDULER_NO_TASK)
3903     {
3904       GNUNET_SCHEDULER_cancel (sched, n->timeout_task);
3905       n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3906     }
3907   if (n->retry_task != GNUNET_SCHEDULER_NO_TASK)
3908     {
3909       GNUNET_SCHEDULER_cancel (sched, n->retry_task);
3910       n->retry_task = GNUNET_SCHEDULER_NO_TASK;
3911     }
3912   if (n->piter != NULL)
3913     {
3914       GNUNET_PEERINFO_iterate_cancel (n->piter);
3915       n->piter = NULL;
3916     }
3917   /* finally, free n itself */
3918   GNUNET_STATISTICS_update (stats,
3919                             gettext_noop ("# active neighbours"),
3920                             -1,
3921                             GNUNET_NO);
3922   GNUNET_free_non_null (n->pre_connect_message_buffer);
3923   GNUNET_free (n);
3924 }
3925
3926
3927 /**
3928  * We have received a PING message from someone.  Need to send a PONG message
3929  * in response to the peer by any means necessary. 
3930  */
3931 static int 
3932 handle_ping(void *cls, const struct GNUNET_MessageHeader *message,
3933             const struct GNUNET_PeerIdentity *peer,
3934             const char *sender_address,
3935             size_t sender_address_len)
3936 {
3937   struct TransportPlugin *plugin = cls;
3938   struct TransportPingMessage *ping;
3939   struct TransportPongMessage *pong;
3940   struct NeighbourList *n;
3941   struct ReadyList *rl;
3942   struct ForeignAddressList *fal;
3943
3944   if (ntohs (message->size) != sizeof (struct TransportPingMessage))
3945     {
3946       GNUNET_break_op (0);
3947       return GNUNET_SYSERR;
3948     }
3949
3950   ping = (struct TransportPingMessage *) message;
3951   if (0 != memcmp (&ping->target,
3952                    plugin->env.my_identity,
3953                    sizeof (struct GNUNET_PeerIdentity)))
3954     {
3955       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3956                   _("Received `%s' message not destined for me!\n"), 
3957                   "PING");
3958       return GNUNET_SYSERR;
3959     }
3960 #if DEBUG_PING_PONG
3961   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
3962               "Processing `%s' from `%s'\n",
3963               "PING", 
3964               (sender_address != NULL) 
3965               ? a2s (plugin->short_name,
3966                      (const struct sockaddr *)sender_address, 
3967                      sender_address_len)
3968               : "<inbound>");
3969 #endif
3970   GNUNET_STATISTICS_update (stats,
3971                             gettext_noop ("# PING messages received"),
3972                             1,
3973                             GNUNET_NO);
3974   pong = GNUNET_malloc (sizeof (struct TransportPongMessage) + sender_address_len);
3975   pong->header.size = htons (sizeof (struct TransportPongMessage) + sender_address_len);
3976   pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PONG);
3977   pong->purpose.size =
3978     htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3979            sizeof (uint32_t) +
3980            sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + sender_address_len);
3981   pong->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PING);
3982   pong->challenge = ping->challenge;
3983   pong->addrlen = htons(sender_address_len);
3984   memcpy(&pong->signer, 
3985          &my_public_key, 
3986          sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3987   if (sender_address != NULL)
3988     memcpy (&pong[1], sender_address, sender_address_len);
3989   GNUNET_assert (GNUNET_OK ==
3990                  GNUNET_CRYPTO_rsa_sign (my_private_key,
3991                                          &pong->purpose, &pong->signature));
3992   n = find_neighbour(peer);
3993   GNUNET_assert (n != NULL);
3994   /* first try reliable response transmission */
3995   rl = n->plugins;
3996   while (rl != NULL)
3997     {
3998       fal = rl->addresses;
3999       while (fal != NULL)
4000         {
4001           if (-1 != rl->plugin->api->send (rl->plugin->api->cls,
4002                                            peer,
4003                                            (const char*) pong,
4004                                            ntohs (pong->header.size),
4005                                            TRANSPORT_PONG_PRIORITY, 
4006                                            HELLO_VERIFICATION_TIMEOUT,
4007                                            fal->session,
4008                                            fal->addr,
4009                                            fal->addrlen,
4010                                            GNUNET_SYSERR,
4011                                            NULL, NULL))
4012             {
4013               /* done! */
4014               GNUNET_STATISTICS_update (stats,
4015                                         gettext_noop ("# PONGs unicast via reliable transport"),
4016                                         1,
4017                                         GNUNET_NO);      
4018               GNUNET_free (pong);
4019               return GNUNET_OK;
4020             }
4021           fal = fal->next;
4022         }
4023       rl = rl->next;
4024     }
4025   /* no reliable method found, do multicast */
4026   GNUNET_STATISTICS_update (stats,
4027                             gettext_noop ("# PONGs multicast to all available addresses"),
4028                             1,
4029                             GNUNET_NO);      
4030   rl = n->plugins;
4031   while (rl != NULL)
4032     {
4033       fal = rl->addresses;
4034       while (fal != NULL)
4035         {
4036           transmit_to_peer(NULL, fal,
4037                            TRANSPORT_PONG_PRIORITY, 
4038                            HELLO_VERIFICATION_TIMEOUT,
4039                            (const char *)pong, 
4040                            ntohs(pong->header.size), 
4041                            GNUNET_YES, 
4042                            n);
4043           fal = fal->next;
4044         }
4045       rl = rl->next;
4046     }
4047   GNUNET_free(pong);
4048   return GNUNET_OK;
4049 }
4050
4051
4052 /**
4053  * Function called by the plugin for each received message.
4054  * Update data volumes, possibly notify plugins about
4055  * reducing the rate at which they read from the socket
4056  * and generally forward to our receive callback.
4057  *
4058  * @param cls the "struct TransportPlugin *" we gave to the plugin
4059  * @param peer (claimed) identity of the other peer
4060  * @param message the message, NULL if we only care about
4061  *                learning about the delay until we should receive again
4062  * @param distance in overlay hops; use 1 unless DV (or 0 if message == NULL)
4063  * @param session identifier used for this session (can be NULL)
4064  * @param sender_address binary address of the sender (if observed)
4065  * @param sender_address_len number of bytes in sender_address
4066  * @return how long the plugin should wait until receiving more data
4067  *         (plugins that do not support this, can ignore the return value)
4068  */
4069 static struct GNUNET_TIME_Relative
4070 plugin_env_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
4071                     const struct GNUNET_MessageHeader *message,
4072                     uint32_t distance,
4073                     struct Session *session,
4074                     const char *sender_address,
4075                     size_t sender_address_len)
4076 {
4077   struct TransportPlugin *plugin = cls;
4078   struct ReadyList *service_context;
4079   struct ForeignAddressList *peer_address;
4080   uint16_t msize;
4081   struct NeighbourList *n;
4082   struct GNUNET_TIME_Relative ret;
4083
4084   if (is_blacklisted (peer, plugin))
4085     return GNUNET_TIME_UNIT_FOREVER_REL;
4086
4087   n = find_neighbour (peer);
4088   if (n == NULL)
4089     n = setup_new_neighbour (peer, GNUNET_YES);
4090   service_context = n->plugins;
4091   while ((service_context != NULL) && (plugin != service_context->plugin))
4092     service_context = service_context->next;
4093   GNUNET_assert ((plugin->api->send == NULL) || (service_context != NULL));
4094   peer_address = NULL;
4095   if (message != NULL)
4096     {
4097       if ( (session != NULL) ||
4098            (sender_address != NULL) )
4099         peer_address = add_peer_address (n, 
4100                                          plugin->short_name,
4101                                          session,
4102                                          sender_address, 
4103                                          sender_address_len);  
4104       if (peer_address != NULL)
4105         {
4106           peer_address->distance = distance;
4107           if (GNUNET_YES == peer_address->validated)
4108             mark_address_connected (peer_address);
4109           peer_address->timeout
4110             =
4111             GNUNET_TIME_relative_to_absolute
4112             (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
4113           schedule_next_ping (peer_address);
4114         }
4115       /* update traffic received amount ... */
4116       msize = ntohs (message->size);      
4117       GNUNET_STATISTICS_update (stats,
4118                                 gettext_noop ("# bytes received from other peers"),
4119                                 msize,
4120                                 GNUNET_NO);
4121       n->distance = distance;
4122       n->peer_timeout =
4123         GNUNET_TIME_relative_to_absolute
4124         (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
4125       GNUNET_SCHEDULER_cancel (sched,
4126                                n->timeout_task);
4127       n->timeout_task =
4128         GNUNET_SCHEDULER_add_delayed (sched,
4129                                       GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
4130                                       &neighbour_timeout_task, n);
4131       if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
4132         {
4133           /* dropping message due to frequent inbound volume violations! */
4134           GNUNET_log (GNUNET_ERROR_TYPE_WARNING |
4135                       GNUNET_ERROR_TYPE_BULK,
4136                       _
4137                       ("Dropping incoming message due to repeated bandwidth quota (%u b/s) violations (total of %u).\n"), 
4138                       n->in_tracker.available_bytes_per_s__,
4139                       n->quota_violation_count);
4140           GNUNET_STATISTICS_update (stats,
4141                                     gettext_noop ("# bandwidth quota violations by other peers"),
4142                                     1,
4143                                     GNUNET_NO);
4144           return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
4145         }
4146 #if DEBUG_PING_PONG
4147           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4148                       "Received message of type %u from `%4s', sending to all clients.\n",
4149                       ntohs (message->type), GNUNET_i2s (peer));
4150 #endif
4151       switch (ntohs (message->type))
4152         {
4153         case GNUNET_MESSAGE_TYPE_HELLO:
4154           GNUNET_STATISTICS_update (stats,
4155                                     gettext_noop ("# HELLO messages received from other peers"),
4156                                     1,
4157                                     GNUNET_NO);
4158           process_hello (plugin, message);
4159           break;
4160         case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
4161           handle_ping (plugin, message, peer, sender_address, sender_address_len);
4162           break;
4163         case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
4164           handle_pong (plugin, message, peer, sender_address, sender_address_len);
4165           break;
4166         default:
4167           handle_payload_message (message, n);
4168           break;
4169         }
4170     }  
4171   ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
4172   if (ret.value > 0)
4173     {
4174       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4175                   "Throttling read (%llu bytes excess at %u b/s), waiting %llums before reading more.\n",
4176                   (unsigned long long) n->in_tracker.consumption_since_last_update__,
4177                   (unsigned int) n->in_tracker.available_bytes_per_s__,
4178                   (unsigned long long) ret.value);
4179       GNUNET_STATISTICS_update (stats,
4180                                 gettext_noop ("# ms throttling suggested"),
4181                                 (int64_t) ret.value,
4182                                 GNUNET_NO);      
4183     }
4184   return ret;
4185 }
4186
4187 /**
4188  * Handle START-message.  This is the first message sent to us
4189  * by any client which causes us to add it to our list.
4190  *
4191  * @param cls closure (always NULL)
4192  * @param client identification of the client
4193  * @param message the actual message
4194  */
4195 static void
4196 handle_start (void *cls,
4197               struct GNUNET_SERVER_Client *client,
4198               const struct GNUNET_MessageHeader *message)
4199 {
4200   struct TransportClient *c;
4201   struct ConnectInfoMessage cim;
4202   struct NeighbourList *n;
4203
4204 #if DEBUG_TRANSPORT
4205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4206               "Received `%s' request from client\n", "START");
4207 #endif
4208   c = clients;
4209   while (c != NULL)
4210     {
4211       if (c->client == client)
4212         {
4213           /* client already on our list! */
4214           GNUNET_break (0);
4215           GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4216           return;
4217         }
4218       c = c->next;
4219     }
4220   c = GNUNET_malloc (sizeof (struct TransportClient));
4221   c->next = clients;
4222   clients = c;
4223   c->client = client;
4224   if (our_hello != NULL)
4225     {
4226 #if DEBUG_TRANSPORT
4227       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4228                   "Sending our own `%s' to new client\n", "HELLO");
4229 #endif
4230       transmit_to_client (c,
4231                           (const struct GNUNET_MessageHeader *) our_hello,
4232                           GNUNET_NO);
4233       /* tell new client about all existing connections */
4234       cim.header.size = htons (sizeof (struct ConnectInfoMessage));
4235       cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
4236       n = neighbours; 
4237       while (n != NULL)
4238         {
4239           if (GNUNET_YES == n->received_pong)
4240             {
4241               cim.id = n->id;
4242               cim.latency = GNUNET_TIME_relative_hton (n->latency);
4243               cim.distance = htonl (n->distance);
4244               transmit_to_client (c, &cim.header, GNUNET_NO);
4245             }
4246             n = n->next;
4247         }
4248     }
4249   GNUNET_SERVER_receive_done (client, GNUNET_OK);
4250 }
4251
4252
4253 /**
4254  * Handle HELLO-message.
4255  *
4256  * @param cls closure (always NULL)
4257  * @param client identification of the client
4258  * @param message the actual message
4259  */
4260 static void
4261 handle_hello (void *cls,
4262               struct GNUNET_SERVER_Client *client,
4263               const struct GNUNET_MessageHeader *message)
4264 {
4265   int ret;
4266
4267   GNUNET_STATISTICS_update (stats,
4268                             gettext_noop ("# HELLOs received from clients"),
4269                             1,
4270                             GNUNET_NO);      
4271   ret = process_hello (NULL, message);
4272   GNUNET_SERVER_receive_done (client, ret);
4273 }
4274
4275
4276 /**
4277  * Closure for 'transmit_client_message'; followed by
4278  * 'msize' bytes of the actual message.
4279  */
4280 struct TransmitClientMessageContext 
4281 {
4282   /**
4283    * Client on whom's behalf we are sending.
4284    */
4285   struct GNUNET_SERVER_Client *client;
4286
4287   /**
4288    * Timeout for the transmission.
4289    */
4290   struct GNUNET_TIME_Absolute timeout;
4291   
4292   /**
4293    * Message priority.
4294    */
4295   uint32_t priority;
4296
4297   /**
4298    * Size of the message in bytes.
4299    */ 
4300   uint16_t msize;
4301 };
4302
4303
4304 /**
4305  * Schedule transmission of a message we got from a client to a peer.
4306  *
4307  * @param cls the 'struct TransmitClientMessageContext*'
4308  * @param n destination, or NULL on error (in that case, drop the message)
4309  */
4310 static void
4311 transmit_client_message (void *cls,
4312                          struct NeighbourList *n)
4313 {
4314   struct TransmitClientMessageContext *tcmc = cls;
4315   struct TransportClient *tc;
4316
4317   tc = clients;
4318   while ((tc != NULL) && (tc->client != tcmc->client))
4319     tc = tc->next;
4320
4321   if (n != NULL)
4322     {
4323       transmit_to_peer (tc, NULL, tcmc->priority, 
4324                         GNUNET_TIME_absolute_get_remaining (tcmc->timeout),
4325                         (char *)&tcmc[1],
4326                         tcmc->msize, GNUNET_NO, n);
4327     }
4328   GNUNET_SERVER_receive_done (tcmc->client, GNUNET_OK);
4329   GNUNET_SERVER_client_drop (tcmc->client);
4330   GNUNET_free (tcmc);
4331 }
4332
4333
4334 /**
4335  * Handle SEND-message.
4336  *
4337  * @param cls closure (always NULL)
4338  * @param client identification of the client
4339  * @param message the actual message
4340  */
4341 static void
4342 handle_send (void *cls,
4343              struct GNUNET_SERVER_Client *client,
4344              const struct GNUNET_MessageHeader *message)
4345 {
4346   const struct OutboundMessage *obm;
4347   const struct GNUNET_MessageHeader *obmm;
4348   struct TransmitClientMessageContext *tcmc;
4349   uint16_t size;
4350   uint16_t msize;
4351
4352   size = ntohs (message->size);
4353   if (size <
4354       sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
4355     {
4356       GNUNET_break (0);
4357       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4358       return;
4359     }
4360   GNUNET_STATISTICS_update (stats,
4361                             gettext_noop ("# payload received for other peers"),
4362                             size,
4363                             GNUNET_NO);      
4364   obm = (const struct OutboundMessage *) message;
4365   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
4366   msize = ntohs (obmm->size);
4367 #if DEBUG_TRANSPORT
4368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4369               "Received `%s' request from client with target `%4s' and message of type %u and size %u\n",
4370               "SEND", GNUNET_i2s (&obm->peer),
4371               ntohs (obmm->type),
4372               msize);
4373 #endif
4374   if (size != msize + sizeof (struct OutboundMessage))
4375     {
4376       GNUNET_break (0);
4377       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4378       return;
4379     }
4380   tcmc = GNUNET_malloc (sizeof (struct TransmitClientMessageContext) + msize);
4381   tcmc->client = client;
4382   tcmc->priority = ntohl (obm->priority);
4383   tcmc->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_ntoh (obm->timeout));
4384   tcmc->msize = msize;
4385   memcpy (&tcmc[1], obmm, msize);
4386   GNUNET_SERVER_client_keep (client);
4387   setup_peer_check_blacklist (&obm->peer, GNUNET_YES,
4388                               &transmit_client_message,
4389                               tcmc);
4390 }
4391
4392
4393 /**
4394  * Handle SET_QUOTA-message.
4395  *
4396  * @param cls closure (always NULL)
4397  * @param client identification of the client
4398  * @param message the actual message
4399  */
4400 static void
4401 handle_set_quota (void *cls,
4402                   struct GNUNET_SERVER_Client *client,
4403                   const struct GNUNET_MessageHeader *message)
4404 {
4405   const struct QuotaSetMessage *qsm =
4406     (const struct QuotaSetMessage *) message;
4407   struct NeighbourList *n;
4408   
4409   GNUNET_STATISTICS_update (stats,
4410                             gettext_noop ("# SET QUOTA messages received"),
4411                             1,
4412                             GNUNET_NO);      
4413   n = find_neighbour (&qsm->peer);
4414   if (n == NULL)
4415     {
4416       GNUNET_SERVER_receive_done (client, GNUNET_OK);
4417       GNUNET_STATISTICS_update (stats,
4418                                 gettext_noop ("# SET QUOTA messages ignored (no such peer)"),
4419                                 1,
4420                                 GNUNET_NO);      
4421       return;
4422     }
4423 #if DEBUG_TRANSPORT
4424   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4425               "Received `%s' request (new quota %u, old quota %u) from client for peer `%4s'\n",
4426               "SET_QUOTA", 
4427               (unsigned int) ntohl (qsm->quota.value__),
4428               (unsigned int) n->in_tracker.available_bytes_per_s__,
4429               GNUNET_i2s (&qsm->peer));
4430 #endif
4431   GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
4432                                          qsm->quota);
4433   if (0 == ntohl (qsm->quota.value__)) 
4434     disconnect_neighbour (n, GNUNET_NO);    
4435   GNUNET_SERVER_receive_done (client, GNUNET_OK);
4436 }
4437
4438
4439 /**
4440  * Take the given address and append it to the set of results send back to
4441  * the client.
4442  * 
4443  * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
4444  * @param address the resolved name, NULL to indicate the last response
4445  */
4446 static void
4447 transmit_address_to_client (void *cls, const char *address)
4448 {
4449   struct GNUNET_SERVER_TransmitContext *tc = cls;
4450   size_t slen;
4451
4452   if (NULL == address)
4453     slen = 0;
4454   else
4455     slen = strlen (address) + 1;
4456   GNUNET_SERVER_transmit_context_append_data (tc, address, slen,
4457                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
4458   if (NULL == address)
4459     GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
4460 }
4461
4462
4463 /**
4464  * Handle AddressLookup-message.
4465  *
4466  * @param cls closure (always NULL)
4467  * @param client identification of the client
4468  * @param message the actual message
4469  */
4470 static void
4471 handle_address_lookup (void *cls,
4472                        struct GNUNET_SERVER_Client *client,
4473                        const struct GNUNET_MessageHeader *message)
4474 {
4475   const struct AddressLookupMessage *alum;
4476   struct TransportPlugin *lsPlugin;
4477   const char *nameTransport;
4478   const char *address;
4479   uint16_t size;
4480   struct GNUNET_SERVER_TransmitContext *tc;
4481   struct GNUNET_TIME_Absolute timeout;
4482   struct GNUNET_TIME_Relative rtimeout;
4483   int32_t numeric;
4484
4485   size = ntohs (message->size);
4486   if (size < sizeof (struct AddressLookupMessage))
4487     {
4488       GNUNET_break_op (0);
4489       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4490       return;
4491     }
4492   alum = (const struct AddressLookupMessage *) message;
4493   uint32_t addressLen = ntohl (alum->addrlen);
4494   if (size <= sizeof (struct AddressLookupMessage) + addressLen)
4495     {
4496       GNUNET_break_op (0);
4497       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4498       return;
4499     }
4500   address = (const char *) &alum[1];
4501   nameTransport = (const char *) &address[addressLen];
4502   if (nameTransport
4503       [size - sizeof (struct AddressLookupMessage) - addressLen - 1] != '\0')
4504     {
4505       GNUNET_break_op (0);
4506       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4507       return;
4508     }
4509   timeout = GNUNET_TIME_absolute_ntoh (alum->timeout);
4510   rtimeout = GNUNET_TIME_absolute_get_remaining (timeout);
4511   numeric = ntohl (alum->numeric_only);
4512   lsPlugin = find_transport (nameTransport);
4513   if (NULL == lsPlugin)
4514     {
4515       tc = GNUNET_SERVER_transmit_context_create (client);
4516       GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
4517                                                   GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
4518       GNUNET_SERVER_transmit_context_run (tc, rtimeout);
4519       return;
4520     }
4521   tc = GNUNET_SERVER_transmit_context_create (client);
4522   lsPlugin->api->address_pretty_printer (lsPlugin->api->cls,
4523                                          nameTransport,
4524                                          address, addressLen, 
4525                                          numeric,
4526                                          rtimeout,
4527                                          &transmit_address_to_client, tc);
4528 }
4529
4530 /**
4531  * List of handlers for the messages understood by this
4532  * service.
4533  */
4534 static struct GNUNET_SERVER_MessageHandler handlers[] = {
4535   {&handle_start, NULL,
4536    GNUNET_MESSAGE_TYPE_TRANSPORT_START, 0},
4537   {&handle_hello, NULL,
4538    GNUNET_MESSAGE_TYPE_HELLO, 0},
4539   {&handle_send, NULL,
4540    GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
4541   {&handle_set_quota, NULL,
4542    GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
4543   {&handle_address_lookup, NULL,
4544    GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP,
4545    0},
4546   {&handle_blacklist_init, NULL,
4547    GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
4548   {&handle_blacklist_reply, NULL,
4549    GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
4550   {NULL, NULL, 0, 0}
4551 };
4552
4553
4554 /**
4555  * Setup the environment for this plugin.
4556  */
4557 static void
4558 create_environment (struct TransportPlugin *plug)
4559 {
4560   plug->env.cfg = cfg;
4561   plug->env.sched = sched;
4562   plug->env.my_identity = &my_identity;
4563   plug->env.cls = plug;
4564   plug->env.receive = &plugin_env_receive;
4565   plug->env.notify_address = &plugin_env_notify_address;
4566   plug->env.session_end = &plugin_env_session_end;
4567   plug->env.max_connections = max_connect_per_transport;
4568   plug->env.stats = stats;
4569 }
4570
4571
4572 /**
4573  * Start the specified transport (load the plugin).
4574  */
4575 static void
4576 start_transport (struct GNUNET_SERVER_Handle *server, 
4577                  const char *name)
4578 {
4579   struct TransportPlugin *plug;
4580   char *libname;
4581
4582   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4583               _("Loading `%s' transport plugin\n"), name);
4584   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
4585   plug = GNUNET_malloc (sizeof (struct TransportPlugin));
4586   create_environment (plug);
4587   plug->short_name = GNUNET_strdup (name);
4588   plug->lib_name = libname;
4589   plug->next = plugins;
4590   plugins = plug;
4591   plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
4592   if (plug->api == NULL)
4593     {
4594       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4595                   _("Failed to load transport plugin for `%s'\n"), name);
4596       GNUNET_free (plug->short_name);
4597       plugins = plug->next;
4598       GNUNET_free (libname);
4599       GNUNET_free (plug);
4600     }
4601 }
4602
4603
4604 /**
4605  * Called whenever a client is disconnected.  Frees our
4606  * resources associated with that client.
4607  *
4608  * @param cls closure
4609  * @param client identification of the client
4610  */
4611 static void
4612 client_disconnect_notification (void *cls,
4613                                 struct GNUNET_SERVER_Client *client)
4614 {
4615   struct TransportClient *pos;
4616   struct TransportClient *prev;
4617   struct ClientMessageQueueEntry *mqe;
4618   struct Blacklisters *bl;
4619   struct BlacklistCheck *bc;
4620
4621   if (client == NULL)
4622     return;
4623 #if DEBUG_TRANSPORT
4624   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
4625               "Client disconnected, cleaning up.\n");
4626 #endif
4627   /* clean up blacklister */
4628   bl = bl_head;
4629   while (bl != NULL)
4630     {
4631       if (bl->client == client)
4632         {
4633           bc = bc_head;
4634           while (bc != NULL)
4635             {
4636               if (bc->bl_pos == bl)
4637                 {
4638                   bc->bl_pos = bl->next;
4639                   if (bc->th != NULL)
4640                     {
4641                       GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
4642                       bc->th = NULL;                  
4643                     }
4644                   if (bc->task == GNUNET_SCHEDULER_NO_TASK)
4645                     bc->task = GNUNET_SCHEDULER_add_now (sched,
4646                                                          &do_blacklist_check,
4647                                                          bc);
4648                   break;
4649                 }
4650               bc = bc->next;
4651             }
4652           GNUNET_CONTAINER_DLL_remove (bl_head,
4653                                        bl_tail,
4654                                        bl);
4655           GNUNET_SERVER_client_drop (bl->client);
4656           GNUNET_free (bl);
4657           break;
4658         }
4659       bl = bl->next;
4660     }
4661   /* clean up 'normal' clients */
4662   prev = NULL;
4663   pos = clients;
4664   while ((pos != NULL) && (pos->client != client))
4665     {
4666       prev = pos;
4667       pos = pos->next;
4668     }
4669   if (pos == NULL)
4670     return;
4671   while (NULL != (mqe = pos->message_queue_head))
4672     {
4673       GNUNET_CONTAINER_DLL_remove (pos->message_queue_head,
4674                                    pos->message_queue_tail,
4675                                    mqe);
4676       pos->message_count--;
4677       GNUNET_free (mqe);
4678     }
4679   if (prev == NULL)
4680     clients = pos->next;
4681   else
4682     prev->next = pos->next;
4683   if (GNUNET_YES == pos->tcs_pending)
4684     {
4685       pos->client = NULL;
4686       return;
4687     }
4688   if (pos->th != NULL)
4689     {
4690       GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
4691       pos->th = NULL;
4692     }
4693   GNUNET_break (0 == pos->message_count);
4694   GNUNET_free (pos);
4695 }
4696
4697
4698 /**
4699  * Iterator to free entries in the validation_map.
4700  *
4701  * @param cls closure (unused)
4702  * @param key current key code
4703  * @param value value in the hash map (validation to abort)
4704  * @return GNUNET_YES (always)
4705  */
4706 static int 
4707 abort_validation (void *cls,
4708                   const GNUNET_HashCode * key,
4709                   void *value)
4710 {
4711   struct ValidationEntry *va = value;
4712
4713   GNUNET_SCHEDULER_cancel (sched, va->timeout_task);
4714   GNUNET_free (va->transport_name);
4715   GNUNET_free (va);
4716   return GNUNET_YES;
4717 }
4718
4719
4720 /**
4721  * Function called when the service shuts down.  Unloads our plugins
4722  * and cancels pending validations.
4723  *
4724  * @param cls closure, unused
4725  * @param tc task context (unused)
4726  */
4727 static void
4728 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4729 {
4730   struct TransportPlugin *plug;
4731   struct OwnAddressList *al;
4732   struct CheckHelloValidatedContext *chvc;
4733
4734   while (neighbours != NULL)
4735     disconnect_neighbour (neighbours, GNUNET_NO);
4736 #if DEBUG_TRANSPORT
4737   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4738               "Transport service is unloading plugins...\n");
4739 #endif
4740   while (NULL != (plug = plugins))
4741     {
4742       plugins = plug->next;
4743       if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
4744         {
4745           GNUNET_SCHEDULER_cancel (plug->env.sched, 
4746                                    plug->address_update_task);
4747           plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
4748         }
4749       GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
4750       GNUNET_free (plug->lib_name);
4751       GNUNET_free (plug->short_name);
4752       while (NULL != (al = plug->addresses))
4753         {
4754           plug->addresses = al->next;
4755           GNUNET_free (al);
4756         }
4757       GNUNET_free (plug);
4758     }
4759   if (my_private_key != NULL)
4760     GNUNET_CRYPTO_rsa_key_free (my_private_key);
4761   GNUNET_free_non_null (our_hello);
4762
4763   /* free 'chvc' data structure */
4764   while (NULL != (chvc = chvc_head))
4765     {
4766       chvc_head = chvc->next;
4767       GNUNET_PEERINFO_iterate_cancel (chvc->piter);
4768       GNUNET_free (chvc);
4769     }
4770   chvc_tail = NULL;
4771
4772   GNUNET_CONTAINER_multihashmap_iterate (validation_map,
4773                                          &abort_validation,
4774                                          NULL);
4775   GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4776   validation_map = NULL;
4777   if (stats != NULL)
4778     {
4779       GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4780       stats = NULL;
4781     }
4782
4783   /* Can we assume those are gone by now, or do we need to clean up
4784      explicitly!? */
4785   GNUNET_break (bl_head == NULL);
4786   GNUNET_break (bc_head == NULL);
4787 }
4788
4789
4790 /**
4791  * Initiate transport service.
4792  *
4793  * @param cls closure
4794  * @param s scheduler to use
4795  * @param serv the initialized server
4796  * @param c configuration to use
4797  */
4798 static void
4799 run (void *cls,
4800      struct GNUNET_SCHEDULER_Handle *s,
4801      struct GNUNET_SERVER_Handle *serv,
4802      const struct GNUNET_CONFIGURATION_Handle *c)
4803 {
4804   char *plugs;
4805   char *pos;
4806   int no_transports;
4807   unsigned long long tneigh;
4808   char *keyfile;
4809
4810   sched = s;
4811   cfg = c;
4812   stats = GNUNET_STATISTICS_create (sched, "transport", cfg);
4813   validation_map = GNUNET_CONTAINER_multihashmap_create (64);
4814   /* parse configuration */
4815   if ((GNUNET_OK !=
4816        GNUNET_CONFIGURATION_get_value_number (c,
4817                                               "TRANSPORT",
4818                                               "NEIGHBOUR_LIMIT",
4819                                               &tneigh)) ||
4820       (GNUNET_OK !=
4821        GNUNET_CONFIGURATION_get_value_filename (c,
4822                                                 "GNUNETD",
4823                                                 "HOSTKEY", &keyfile)))
4824     {
4825       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4826                   _
4827                   ("Transport service is lacking key configuration settings.  Exiting.\n"));
4828       GNUNET_SCHEDULER_shutdown (s);
4829       if (stats != NULL)
4830         {
4831           GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4832           stats = NULL;
4833         }
4834       GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4835       validation_map = NULL;
4836       return;
4837     }
4838   max_connect_per_transport = (uint32_t) tneigh;
4839   peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
4840   if (peerinfo == NULL)
4841     {
4842       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4843                   _("Could not access PEERINFO service.  Exiting.\n"));     
4844       GNUNET_SCHEDULER_shutdown (s);
4845       if (stats != NULL)
4846         {
4847           GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4848           stats = NULL;
4849         }
4850       GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4851       validation_map = NULL;
4852       GNUNET_free (keyfile);
4853       return;
4854     }
4855   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4856   GNUNET_free (keyfile);
4857   if (my_private_key == NULL)
4858     {
4859       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4860                   _
4861                   ("Transport service could not access hostkey.  Exiting.\n"));
4862       GNUNET_SCHEDULER_shutdown (s);
4863       if (stats != NULL)
4864         {
4865           GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4866           stats = NULL;
4867         }
4868       GNUNET_CONTAINER_multihashmap_destroy (validation_map);
4869       validation_map = NULL;
4870       return;
4871     }
4872   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4873   GNUNET_CRYPTO_hash (&my_public_key,
4874                       sizeof (my_public_key), &my_identity.hashPubKey);
4875   /* setup notification */
4876   server = serv;
4877   GNUNET_SERVER_disconnect_notify (server,
4878                                    &client_disconnect_notification, NULL);
4879   /* load plugins... */
4880   no_transports = 1;
4881   if (GNUNET_OK ==
4882       GNUNET_CONFIGURATION_get_value_string (c,
4883                                              "TRANSPORT", "PLUGINS", &plugs))
4884     {
4885       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4886                   _("Starting transport plugins `%s'\n"), plugs);
4887       pos = strtok (plugs, " ");
4888       while (pos != NULL)
4889         {
4890           start_transport (server, pos);
4891           no_transports = 0;
4892           pos = strtok (NULL, " ");
4893         }
4894       GNUNET_free (plugs);
4895     }
4896   GNUNET_SCHEDULER_add_delayed (sched,
4897                                 GNUNET_TIME_UNIT_FOREVER_REL,
4898                                 &shutdown_task, NULL);
4899   if (no_transports)
4900     refresh_hello ();
4901
4902 #if DEBUG_TRANSPORT
4903   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport service ready.\n"));
4904 #endif
4905   /* If we have a blacklist file, read from it */
4906   read_blacklist_file(cfg);
4907   /* process client requests */
4908   GNUNET_SERVER_add_handlers (server, handlers);
4909 }
4910
4911
4912 /**
4913  * The main function for the transport service.
4914  *
4915  * @param argc number of arguments from the command line
4916  * @param argv command line arguments
4917  * @return 0 ok, 1 on error
4918  */
4919 int
4920 main (int argc, char *const *argv)
4921 {
4922   a2s (NULL, NULL, 0); /* make compiler happy */
4923   return (GNUNET_OK ==
4924           GNUNET_SERVICE_run (argc,
4925                               argv,
4926                               "transport",
4927                               GNUNET_SERVICE_OPTION_NONE,
4928                               &run, NULL)) ? 0 : 1;
4929 }
4930
4931 /* end of gnunet-service-transport.c */