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