disable LAN/loopback for AC
[oweals/gnunet.git] / src / transport / gnunet-service-transport.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010-2016 GNUnet e.V.
4
5  GNUnet is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published
7  by the Free Software Foundation; either version 3, or (at your
8  option) any later version.
9
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with GNUnet; see the file COPYING.  If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19  */
20 /**
21  * @file transport/gnunet-service-transport.c
22  * @brief main for gnunet-service-transport
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_hello_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_peerinfo_service.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet-service-transport_ats.h"
34 #include "gnunet-service-transport_hello.h"
35 #include "gnunet-service-transport_neighbours.h"
36 #include "gnunet-service-transport_plugins.h"
37 #include "gnunet-service-transport_validation.h"
38 #include "gnunet-service-transport_manipulation.h"
39 #include "transport.h"
40
41 /**
42  * Size of the blacklist hash map.
43  */
44 #define TRANSPORT_BLACKLIST_HT_SIZE 64
45
46 /**
47  * How many messages can we have pending for a given client process
48  * before we start to drop incoming messages?  We typically should
49  * have only one client and so this would be the primary buffer for
50   * messages, so the number should be chosen rather generously.
51  *
52  * The expectation here is that most of the time the queue is large
53  * enough so that a drop is virtually never required.  Note that
54  * this value must be about as large as 'TOTAL_MSGS' in the
55  * 'test_transport_api_reliability.c', otherwise that testcase may
56  * fail.
57  */
58 #define MAX_PENDING (128 * 1024)
59
60
61 /**
62  * Information we need for an asynchronous session kill.
63  */
64 struct GNUNET_ATS_SessionKiller
65 {
66   /**
67    * Kept in a DLL.
68    */
69   struct GNUNET_ATS_SessionKiller *next;
70
71   /**
72    * Kept in a DLL.
73    */
74   struct GNUNET_ATS_SessionKiller *prev;
75
76   /**
77    * Session to kill.
78    */
79   struct GNUNET_ATS_Session *session;
80
81   /**
82    * Plugin for the session.
83    */
84   struct GNUNET_TRANSPORT_PluginFunctions *plugin;
85
86   /**
87    * The kill task.
88    */
89   struct GNUNET_SCHEDULER_Task *task;
90 };
91
92
93 /**
94  * What type of client is the `struct TransportClient` about?
95  */
96 enum ClientType
97 {
98   /**
99    * We do not know yet (client is fresh).
100    */
101   CT_NONE = 0,
102
103   /**
104    * Is the CORE service, we need to forward traffic to it.
105    */
106   CT_CORE = 1,
107
108   /**
109    * It is a monitor, forward monitor data.
110    */
111   CT_MONITOR = 2,
112
113   /**
114    * It is a blacklist, query about allowed connections.
115    */
116   CT_BLACKLIST = 3
117 };
118
119
120 /**
121  * Context we use when performing a blacklist check.
122  */
123 struct GST_BlacklistCheck;
124
125 /**
126  * Client connected to the transport service.
127  */
128 struct TransportClient
129 {
130
131   /**
132    * This is a doubly-linked list.
133    */
134   struct TransportClient *next;
135
136   /**
137    * This is a doubly-linked list.
138    */
139   struct TransportClient *prev;
140
141   /**
142    * Handle to the client.
143    */
144   struct GNUNET_SERVICE_Client *client;
145
146   /**
147    * Message queue to the client.
148    */
149   struct GNUNET_MQ_Handle *mq;
150
151   /**
152    * What type of client is this?
153    */
154   enum ClientType type;
155
156   union {
157
158     /**
159      * Peer identity to monitor the addresses of.
160      * Zero to monitor all neighbours.  Valid if
161      * @e type is CT_MONITOR.
162      */
163     struct GNUNET_PeerIdentity monitor_peer;
164
165     /**
166      * Additional details if @e type is CT_BLACKLIST.
167      */
168     struct {
169
170       /**
171        * Blacklist check that we're currently performing (or NULL
172        * if we're performing one that has been cancelled).
173        */
174       struct GST_BlacklistCheck *bc;
175
176       /**
177        * Set to #GNUNET_YES if we're currently waiting for a reply.
178        */
179       int waiting_for_reply;
180
181       /**
182        * #GNUNET_YES if we have to call receive_done for this client
183        */
184       int call_receive_done;
185
186     } blacklist;
187
188   } details;
189
190 };
191
192
193
194 /**
195  * Context we use when performing a blacklist check.
196  */
197 struct GST_BlacklistCheck
198 {
199
200   /**
201    * This is a linked list.
202    */
203   struct GST_BlacklistCheck *next;
204
205   /**
206    * This is a linked list.
207    */
208   struct GST_BlacklistCheck *prev;
209
210   /**
211    * Peer being checked.
212    */
213   struct GNUNET_PeerIdentity peer;
214
215   /**
216    * Continuation to call with the result.
217    */
218   GST_BlacklistTestContinuation cont;
219
220   /**
221    * Closure for @e cont.
222    */
223   void *cont_cls;
224
225   /**
226    * Address for #GST_blacklist_abort_matching(), can be NULL.
227    */
228   struct GNUNET_HELLO_Address *address;
229
230   /**
231    * Session for #GST_blacklist_abort_matching(), can be NULL.
232    */
233   struct GNUNET_ATS_Session *session;
234
235   /**
236    * Our current position in the blacklisters list.
237    */
238   struct TransportClient *bl_pos;
239
240   /**
241    * Current task performing the check.
242    */
243   struct GNUNET_SCHEDULER_Task *task;
244
245 };
246
247
248 /**
249  * Context for address to string operations
250  */
251 struct AddressToStringContext
252 {
253   /**
254    * This is a doubly-linked list.
255    */
256   struct AddressToStringContext *next;
257
258   /**
259    * This is a doubly-linked list.
260    */
261   struct AddressToStringContext *prev;
262
263   /**
264    * Client that made the request.
265    */
266   struct TransportClient* tc;
267 };
268
269
270 /**
271  * Closure for #handle_send_transmit_continuation()
272  */
273 struct SendTransmitContinuationContext
274 {
275
276   /**
277    * Client that made the request.
278    */
279   struct TransportClient *tc;
280
281   /**
282    * Peer that was the target.
283    */
284   struct GNUNET_PeerIdentity target;
285
286   /**
287    * At what time did we receive the message?
288    */
289   struct GNUNET_TIME_Absolute send_time;
290
291   /**
292    * Unique ID, for logging.
293    */
294   unsigned long long uuid;
295
296   /**
297    * Set to #GNUNET_YES if the connection for @e target goes
298    * down and we thus must no longer send the
299    * #GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK message.
300    */
301   int down;
302 };
303
304
305 /**
306  * Head of linked list of all clients to this service.
307  */
308 static struct TransportClient *clients_head;
309
310 /**
311  * Tail of linked list of all clients to this service.
312  */
313 static struct TransportClient *clients_tail;
314
315 /**
316  * Map of peer identities to active send transmit continuation
317  * contexts. Used to flag contexts as 'dead' when a connection goes
318  * down. Values are of type `struct SendTransmitContinuationContext
319  * *`.
320  */
321 static struct GNUNET_CONTAINER_MultiPeerMap *active_stccs;
322
323 /**
324  * Head of linked list of all pending address iterations
325  */
326 static struct AddressToStringContext *a2s_head;
327
328 /**
329  * Tail of linked list of all pending address iterations
330  */
331 static struct AddressToStringContext *a2s_tail;
332
333 /**
334  * Head of DLL of active blacklisting queries.
335  */
336 static struct GST_BlacklistCheck *bc_head;
337
338 /**
339  * Tail of DLL of active blacklisting queries.
340  */
341 static struct GST_BlacklistCheck *bc_tail;
342
343 /**
344  * Hashmap of blacklisted peers.  Values are of type 'char *' (transport names),
345  * can be NULL if we have no static blacklist.
346  */
347 static struct GNUNET_CONTAINER_MultiPeerMap *blacklist;
348
349 /**
350  * Notification context, to send updates on changes to active plugin
351  * connections.
352  */
353 static struct GNUNET_NotificationContext *plugin_nc;
354
355 /**
356  * Plugin monitoring client we are currently syncing, NULL if all
357  * monitoring clients are in sync.
358  */
359 static struct TransportClient *sync_client;
360
361 /**
362  * Peer identity that is all zeros, used as a way to indicate
363  * "all peers".  Used for comparissons.
364  */
365 static struct GNUNET_PeerIdentity all_zeros;
366
367 /**
368  * Statistics handle.
369  */
370 struct GNUNET_STATISTICS_Handle *GST_stats;
371
372 /**
373  * Configuration handle.
374  */
375 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
376
377 /**
378  * Configuration handle.
379  */
380 struct GNUNET_PeerIdentity GST_my_identity;
381
382 /**
383  * Handle to peerinfo service.
384  */
385 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
386
387 /**
388  * Our private key.
389  */
390 struct GNUNET_CRYPTO_EddsaPrivateKey *GST_my_private_key;
391
392 /**
393  * ATS scheduling handle.
394  */
395 struct GNUNET_ATS_SchedulingHandle *GST_ats;
396
397 /**
398  * ATS connectivity handle.
399  */
400 struct GNUNET_ATS_ConnectivityHandle *GST_ats_connect;
401
402 /**
403  * Hello address expiration
404  */
405 struct GNUNET_TIME_Relative hello_expiration;
406
407 /**
408  * Head of DLL of asynchronous tasks to kill sessions.
409  */
410 static struct GNUNET_ATS_SessionKiller *sk_head;
411
412 /**
413  * Tail of DLL of asynchronous tasks to kill sessions.
414  */
415 static struct GNUNET_ATS_SessionKiller *sk_tail;
416
417 /**
418  * Interface scanner determines our LAN address range(s).
419  */
420 struct GNUNET_ATS_InterfaceScanner *GST_is;
421
422
423 /**
424  * Queue the given message for transmission to the given client
425  *
426  * @param tc target of the message
427  * @param msg message to transmit
428  * @param may_drop #GNUNET_YES if the message can be dropped
429  */
430 static void
431 unicast (struct TransportClient *tc,
432          const struct GNUNET_MessageHeader *msg,
433          int may_drop)
434 {
435   struct GNUNET_MQ_Envelope *env;
436
437   if ( (GNUNET_MQ_get_length (tc->mq) >= MAX_PENDING) &&
438        (GNUNET_YES == may_drop) )
439   {
440     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441                 "Dropping message of type %u and size %u, have %u/%u messages pending\n",
442                 ntohs (msg->type),
443                 ntohs (msg->size),
444                 GNUNET_MQ_get_length (tc->mq),
445                 MAX_PENDING);
446     GNUNET_STATISTICS_update (GST_stats,
447                               gettext_noop
448                               ("# messages dropped due to slow client"), 1,
449                               GNUNET_NO);
450     return;
451   }
452   env = GNUNET_MQ_msg_copy (msg);
453   GNUNET_MQ_send (tc->mq,
454                   env);
455 }
456
457
458 /**
459  * Called whenever a client connects.  Allocates our
460  * data structures associated with that client.
461  *
462  * @param cls closure, NULL
463  * @param client identification of the client
464  * @param mq message queue for the client
465  * @return our `struct TransportClient`
466  */
467 static void *
468 client_connect_cb (void *cls,
469                    struct GNUNET_SERVICE_Client *client,
470                    struct GNUNET_MQ_Handle *mq)
471 {
472   struct TransportClient *tc;
473
474   tc = GNUNET_new (struct TransportClient);
475   tc->client = client;
476   tc->mq = mq;
477   GNUNET_CONTAINER_DLL_insert (clients_head,
478                                clients_tail,
479                                tc);
480   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481               "Client %p connected\n",
482               tc);
483   return tc;
484 }
485
486
487 /**
488  * Perform next action in the blacklist check.
489  *
490  * @param cls the `struct BlacklistCheck*`
491  */
492 static void
493 do_blacklist_check (void *cls);
494
495
496 /**
497  * Mark the peer as down so we don't call the continuation
498  * context in the future.
499  *
500  * @param cls a `struct TransportClient`
501  * @param peer a peer we are sending to
502  * @param value a `struct SendTransmitContinuationContext` to mark
503  * @return #GNUNET_OK (continue to iterate)
504  */
505 static int
506 mark_match_down (void *cls,
507                  const struct GNUNET_PeerIdentity *peer,
508                  void *value)
509 {
510   struct TransportClient *tc = cls;
511   struct SendTransmitContinuationContext *stcc = value;
512
513   if (tc == stcc->tc)
514   {
515     stcc->down = GNUNET_YES;
516     stcc->tc = NULL;
517   }
518   return GNUNET_OK;
519 }
520
521
522 /**
523  * Called whenever a client is disconnected.  Frees our
524  * resources associated with that client.
525  *
526  * @param cls closure, NULL
527  * @param client identification of the client
528  * @param app_ctx our `struct TransportClient`
529  */
530 static void
531 client_disconnect_cb (void *cls,
532                       struct GNUNET_SERVICE_Client *client,
533                       void *app_ctx)
534 {
535   struct TransportClient *tc = app_ctx;
536   struct GST_BlacklistCheck *bc;
537
538   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539               "Client %p disconnected, cleaning up.\n",
540               tc);
541   GNUNET_CONTAINER_multipeermap_iterate (active_stccs,
542                                          &mark_match_down,
543                                          tc);
544   for (struct AddressToStringContext *cur = a2s_head;
545        NULL != cur;
546        cur = cur->next)
547   {
548     if (cur->tc == tc)
549       cur->tc = NULL;
550   }
551   GNUNET_CONTAINER_DLL_remove (clients_head,
552                                clients_tail,
553                                tc);
554   switch (tc->type)
555   {
556   case CT_NONE:
557     break;
558   case CT_CORE:
559     break;
560   case CT_MONITOR:
561     break;
562   case CT_BLACKLIST:
563     for (bc = bc_head; NULL != bc; bc = bc->next)
564     {
565       if (bc->bl_pos != tc)
566         continue;
567       bc->bl_pos = tc->next;
568       if (NULL == bc->task)
569         bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
570                                              bc);
571     }
572     break;
573   }
574   GNUNET_free (tc);
575 }
576
577
578 /**
579  * Function called for each of our connected neighbours.  Notify the
580  * client about the existing neighbour.
581  *
582  * @param cls the `struct TransportClient *` to notify
583  * @param peer identity of the neighbour
584  * @param address the address
585  * @param state the current state of the peer
586  * @param state_timeout the time out for the state
587  * @param bandwidth_in inbound bandwidth in NBO
588  * @param bandwidth_out outbound bandwidth in NBO
589  */
590 static void
591 notify_client_about_neighbour (void *cls,
592                                const struct GNUNET_PeerIdentity *peer,
593                                const struct GNUNET_HELLO_Address *address,
594                                enum GNUNET_TRANSPORT_PeerState state,
595                                struct GNUNET_TIME_Absolute state_timeout,
596                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
597                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
598 {
599   struct TransportClient *tc = cls;
600   struct ConnectInfoMessage cim;
601
602   if (GNUNET_NO == GST_neighbours_test_connected (peer))
603     return;
604   cim.header.size = htons (sizeof (struct ConnectInfoMessage));
605   cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
606   cim.id = *peer;
607   cim.quota_in = bandwidth_in;
608   cim.quota_out = bandwidth_out;
609   unicast (tc,
610            &cim.header,
611            GNUNET_NO);
612 }
613
614
615 /**
616  * Initialize a normal client.  We got a start message from this
617  * client, add him to the list of clients for broadcasting of inbound
618  * messages.
619  *
620  * @param cls the client
621  * @param start the start message that was sent
622  */
623 static void
624 handle_client_start (void *cls,
625                      const struct StartMessage *start)
626 {
627   struct TransportClient *tc = cls;
628   const struct GNUNET_MessageHeader *hello;
629   uint32_t options;
630
631   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632               "Client %p sent START\n",
633               tc);
634   options = ntohl (start->options);
635   if ((0 != (1 & options)) &&
636       (0 !=
637        memcmp (&start->self,
638                &GST_my_identity,
639                sizeof (struct GNUNET_PeerIdentity))))
640   {
641     /* client thinks this is a different peer, reject */
642     GNUNET_break (0);
643     GNUNET_SERVICE_client_drop (tc->client);
644     return;
645   }
646   if (CT_NONE != tc->type)
647   {
648     GNUNET_break (0);
649     GNUNET_SERVICE_client_drop (tc->client);
650     return;
651   }
652   if (0 != (2 & options))
653     tc->type = CT_CORE;
654   hello = GST_hello_get ();
655   if (NULL != hello)
656     unicast (tc,
657              hello,
658              GNUNET_NO);
659   GST_neighbours_iterate (&notify_client_about_neighbour,
660                           tc);
661   GNUNET_SERVICE_client_continue (tc->client);
662 }
663
664
665 /**
666  * Client sent us a HELLO.  Check the request.
667  *
668  * @param cls the client
669  * @param message the HELLO message
670  */
671 static int
672 check_client_hello (void *cls,
673                     const struct GNUNET_MessageHeader *message)
674 {
675   return GNUNET_OK; /* FIXME: check here? */
676 }
677
678
679 /**
680  * Client sent us a HELLO.  Process the request.
681  *
682  * @param cls the client
683  * @param message the HELLO message
684  */
685 static void
686 handle_client_hello (void *cls,
687                      const struct GNUNET_MessageHeader *message)
688 {
689   struct TransportClient *tc = cls;
690
691   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
692               "Received HELLO message\n");
693   GST_validation_handle_hello (message);
694   GNUNET_SERVICE_client_continue (tc->client);
695 }
696
697
698 /**
699  * Function called after the transmission is done.  Notify the client that it is
700  * OK to send the next message.
701  *
702  * @param cls closure
703  * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
704  * @param bytes_payload bytes payload sent
705  * @param bytes_on_wire bytes sent on wire
706  */
707 static void
708 handle_send_transmit_continuation (void *cls,
709                                    int success,
710                                    size_t bytes_payload,
711                                    size_t bytes_on_wire)
712 {
713   struct SendTransmitContinuationContext *stcc = cls;
714   struct SendOkMessage send_ok_msg;
715   struct GNUNET_TIME_Relative delay;
716   const struct GNUNET_HELLO_Address *addr;
717
718   delay = GNUNET_TIME_absolute_get_duration (stcc->send_time);
719   addr = GST_neighbour_get_current_address (&stcc->target);
720   if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
721     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
722                 "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
723                 GNUNET_STRINGS_relative_time_to_string (delay,
724                                                         GNUNET_YES),
725                 (unsigned int) bytes_payload,
726                 (unsigned int) bytes_on_wire,
727                 GNUNET_i2s (&stcc->target),
728                 success,
729                 (NULL != addr) ? addr->transport_name : "%");
730   else
731     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732                 "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
733                 GNUNET_STRINGS_relative_time_to_string (delay,
734                                                         GNUNET_YES),
735                 (unsigned int) bytes_payload,
736                 (unsigned int) bytes_on_wire,
737                 GNUNET_i2s (&stcc->target),
738                 success,
739                 (NULL != addr) ? addr->transport_name : "%");
740
741   if (GNUNET_NO == stcc->down)
742   {
743     /* Only send confirmation if we are still connected */
744     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745                 "Sending SEND_OK for transmission request %llu\n",
746                 stcc->uuid);
747     send_ok_msg.header.size = htons (sizeof (send_ok_msg));
748     send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
749     send_ok_msg.bytes_msg = htonl (bytes_payload);
750     send_ok_msg.bytes_physical = htonl (bytes_on_wire);
751     send_ok_msg.success = htonl (success);
752     send_ok_msg.peer = stcc->target;
753     unicast (stcc->tc,
754              &send_ok_msg.header,
755              GNUNET_NO);
756   }
757   GNUNET_assert (GNUNET_OK ==
758                  GNUNET_CONTAINER_multipeermap_remove (active_stccs,
759                                                        &stcc->target,
760                                                        stcc));
761   GNUNET_free (stcc);
762 }
763
764
765 /**
766  * Client asked for transmission to a peer.  Process the request.
767  *
768  * @param cls the client
769  * @param obm the send message that was sent
770  */
771 static int
772 check_client_send (void *cls,
773                    const struct OutboundMessage *obm)
774 {
775   uint16_t size;
776   const struct GNUNET_MessageHeader *obmm;
777
778   size = ntohs (obm->header.size) - sizeof (struct OutboundMessage);
779   if (size < sizeof (struct GNUNET_MessageHeader))
780   {
781     GNUNET_break (0);
782     return GNUNET_SYSERR;
783   }
784   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
785   if (size != ntohs (obmm->size))
786   {
787     GNUNET_break (0);
788     return GNUNET_SYSERR;
789   }
790   return GNUNET_OK;
791 }
792
793
794 /**
795  * Client asked for transmission to a peer.  Process the request.
796  *
797  * @param cls the client
798  * @param obm the send message that was sent
799  */
800 static void
801 handle_client_send (void *cls,
802                     const struct OutboundMessage *obm)
803 {
804   static unsigned long long uuid_gen;
805   struct TransportClient *tc = cls;
806   const struct GNUNET_MessageHeader *obmm;
807   struct SendTransmitContinuationContext *stcc;
808
809   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
810   if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
811   {
812     /* not connected, not allowed to send; can happen due to asynchronous operations */
813     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814                 "Could not send message to peer `%s': not connected\n",
815                 GNUNET_i2s (&obm->peer));
816     GNUNET_STATISTICS_update (GST_stats,
817                               gettext_noop
818                               ("# bytes payload dropped (other peer was not connected)"),
819                               ntohs (obmm->size),
820                               GNUNET_NO);
821     GNUNET_SERVICE_client_continue (tc->client);
822     return;
823   }
824   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825               "Received SEND request %llu for `%s' and first message of type %u and total size %u\n",
826               uuid_gen,
827               GNUNET_i2s (&obm->peer),
828               ntohs (obmm->type),
829               ntohs (obmm->size));
830   GNUNET_SERVICE_client_continue (tc->client);
831
832   stcc = GNUNET_new (struct SendTransmitContinuationContext);
833   stcc->target = obm->peer;
834   stcc->tc = tc;
835   stcc->send_time = GNUNET_TIME_absolute_get ();
836   stcc->uuid = uuid_gen++;
837   (void) GNUNET_CONTAINER_multipeermap_put (active_stccs,
838                                             &stcc->target,
839                                             stcc,
840                                             GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
841   GST_manipulation_send (&obm->peer,
842                          obmm,
843                          ntohs (obmm->size),
844                          GNUNET_TIME_relative_ntoh (obm->timeout),
845                          &handle_send_transmit_continuation,
846                          stcc);
847 }
848
849
850 /**
851  * Take the given address and append it to the set of results sent back to
852  * the client.  This function may be called serveral times for a single
853  * conversion.   The last invocation will be with a @a address of
854  * NULL and a @a res of #GNUNET_OK.  Thus, to indicate conversion
855  * errors, the callback might be called first with @a address NULL and
856  * @a res being #GNUNET_SYSERR.  In that case, there will still be a
857  * subsequent call later with @a address NULL and @a res #GNUNET_OK.
858  *
859  * @param cls the `struct AddressToStringContext`
860  * @param buf text to transmit (contains the human-readable address, or NULL)
861  * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on error,
862  *            never #GNUNET_NO
863  */
864 static void
865 transmit_address_to_client (void *cls,
866                             const char *buf,
867                             int res)
868 {
869   struct AddressToStringContext *actx = cls;
870   struct GNUNET_MQ_Envelope *env;
871   struct AddressToStringResultMessage *atsm;
872   size_t slen;
873
874   GNUNET_assert ( (GNUNET_OK == res) ||
875                   (GNUNET_SYSERR == res) );
876   if (NULL == actx->tc)
877     return;
878   if (NULL == buf)
879   {
880     env = GNUNET_MQ_msg (atsm,
881                          GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
882     if (GNUNET_OK == res)
883     {
884       /* this was the last call, transmit */
885       atsm->res = htonl (GNUNET_OK);
886       atsm->addr_len = htonl (0);
887       GNUNET_MQ_send (actx->tc->mq,
888                       env);
889       GNUNET_CONTAINER_DLL_remove (a2s_head,
890                                    a2s_tail,
891                                    actx);
892       GNUNET_free (actx);
893       return;
894     }
895     if (GNUNET_SYSERR == res)
896     {
897       /* address conversion failed, but there will be more callbacks */
898       atsm->res = htonl (GNUNET_SYSERR);
899       atsm->addr_len = htonl (0);
900       GNUNET_MQ_send (actx->tc->mq,
901                       env);
902       return;
903     }
904   }
905   GNUNET_assert (GNUNET_OK == res);
906   /* succesful conversion, append*/
907   slen = strlen (buf) + 1;
908   env = GNUNET_MQ_msg_extra (atsm,
909                              slen,
910                              GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
911   atsm->res = htonl (GNUNET_YES);
912   atsm->addr_len = htonl (slen);
913   GNUNET_memcpy (&atsm[1],
914                  buf,
915                  slen);
916   GNUNET_MQ_send (actx->tc->mq,
917                   env);
918 }
919
920
921 /**
922  * Client asked to resolve an address.  Check the request.
923  *
924  * @param cls the client
925  * @param alum the resolution request
926  * @return #GNUNET_OK if @a alum is well-formed
927  */
928 static int
929 check_client_address_to_string (void *cls,
930                                 const struct AddressLookupMessage *alum)
931 {
932   const char *plugin_name;
933   const char *address;
934   uint32_t address_len;
935   uint16_t size;
936
937   size = ntohs (alum->header.size);
938   address_len = ntohs (alum->addrlen);
939   if (size <= sizeof (struct AddressLookupMessage) + address_len)
940   {
941     GNUNET_break (0);
942     return GNUNET_SYSERR;
943   }
944   address = (const char *) &alum[1];
945   plugin_name = (const char *) &address[address_len];
946   if ('\0' != plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1])
947   {
948     GNUNET_break (0);
949     return GNUNET_SYSERR;
950   }
951   return GNUNET_OK;
952 }
953
954
955 /**
956  * Client asked to resolve an address.  Process the request.
957  *
958  * @param cls the client
959  * @param alum the resolution request
960  */
961 static void
962 handle_client_address_to_string (void *cls,
963                                  const struct AddressLookupMessage *alum)
964 {
965   struct TransportClient *tc = cls;
966   struct GNUNET_TRANSPORT_PluginFunctions *papi;
967   const char *plugin_name;
968   const char *address;
969   uint32_t address_len;
970   struct AddressToStringContext *actx;
971   struct GNUNET_MQ_Envelope *env;
972   struct AddressToStringResultMessage *atsm;
973   struct GNUNET_TIME_Relative rtimeout;
974   int32_t numeric;
975
976   address_len = ntohs (alum->addrlen);
977   address = (const char *) &alum[1];
978   plugin_name = (const char *) &address[address_len];
979   rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
980   numeric = ntohs (alum->numeric_only);
981   papi = GST_plugins_printer_find (plugin_name);
982   if (NULL == papi)
983   {
984     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
985                 "Failed to find plugin `%s'\n",
986                 plugin_name);
987     env = GNUNET_MQ_msg (atsm,
988                          GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
989     atsm->res = htonl (GNUNET_SYSERR);
990     atsm->addr_len = htonl (0);
991     GNUNET_MQ_send (tc->mq,
992                     env);
993     env = GNUNET_MQ_msg (atsm,
994                          GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
995     atsm->res = htonl (GNUNET_OK);
996     atsm->addr_len = htonl (0);
997     GNUNET_MQ_send (tc->mq,
998                     env);
999     return;
1000   }
1001   actx = GNUNET_new (struct AddressToStringContext);
1002   actx->tc = tc;
1003   GNUNET_CONTAINER_DLL_insert (a2s_head,
1004                                a2s_tail,
1005                                actx);
1006   GNUNET_SERVICE_client_disable_continue_warning (tc->client);
1007   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008               "Pretty-printing address of %u bytes using plugin `%s'\n",
1009               address_len,
1010               plugin_name);
1011   papi->address_pretty_printer (papi->cls,
1012                                 plugin_name,
1013                                 address,
1014                                 address_len,
1015                                 numeric,
1016                                 rtimeout,
1017                                 &transmit_address_to_client,
1018                                 actx);
1019 }
1020
1021
1022 /**
1023  * Compose #PeerIterateResponseMessage using the given peer and address.
1024  *
1025  * @param peer identity of the peer
1026  * @param address the address, NULL on disconnect
1027  * @return composed message
1028  */
1029 static struct PeerIterateResponseMessage *
1030 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1031                                           const struct GNUNET_HELLO_Address *address)
1032 {
1033   struct PeerIterateResponseMessage *msg;
1034   size_t size;
1035   size_t tlen;
1036   size_t alen;
1037   char *addr;
1038
1039   GNUNET_assert (NULL != peer);
1040   if (NULL != address)
1041   {
1042     tlen = strlen (address->transport_name) + 1;
1043     alen = address->address_length;
1044   }
1045   else
1046   {
1047     tlen = 0;
1048     alen = 0;
1049   }
1050   size = (sizeof (struct PeerIterateResponseMessage) + alen + tlen);
1051   msg = GNUNET_malloc (size);
1052   msg->header.size = htons (size);
1053   msg->header.type
1054     = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1055   msg->reserved = htonl (0);
1056   msg->peer = *peer;
1057   msg->addrlen = htonl (alen);
1058   msg->pluginlen = htonl (tlen);
1059
1060   if (NULL != address)
1061   {
1062     msg->local_address_info = htonl((uint32_t) address->local_info);
1063     addr = (char *) &msg[1];
1064     GNUNET_memcpy (addr,
1065                    address->address,
1066                    alen);
1067     GNUNET_memcpy (&addr[alen],
1068                    address->transport_name,
1069                    tlen);
1070   }
1071   return msg;
1072 }
1073
1074
1075 /**
1076  * Context for #send_validation_information() and
1077  * #send_peer_information().
1078  */
1079 struct IterationContext
1080 {
1081   /**
1082    * Context to use for the transmission.
1083    */
1084   struct TransportClient *tc;
1085
1086   /**
1087    * Which peers do we care about?
1088    */
1089   struct GNUNET_PeerIdentity id;
1090
1091   /**
1092    * #GNUNET_YES if @e id should be ignored because we want all peers.
1093    */
1094   int all;
1095 };
1096
1097
1098 /**
1099  * Output information of neighbours to the given client.
1100  *
1101  * @param cls the `struct PeerIterationContext *`
1102  * @param peer identity of the neighbour
1103  * @param address the address
1104  * @param state current state this peer is in
1105  * @param state_timeout timeout for the current state of the peer
1106  * @param bandwidth_in inbound quota in NBO
1107  * @param bandwidth_out outbound quota in NBO
1108  */
1109 static void
1110 send_peer_information (void *cls,
1111                        const struct GNUNET_PeerIdentity *peer,
1112                        const struct GNUNET_HELLO_Address *address,
1113                        enum GNUNET_TRANSPORT_PeerState state,
1114                        struct GNUNET_TIME_Absolute state_timeout,
1115                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1116                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1117 {
1118   struct IterationContext *pc = cls;
1119   struct GNUNET_MQ_Envelope *env;
1120   struct PeerIterateResponseMessage *msg;
1121
1122   if ( (GNUNET_YES != pc->all) &&
1123        (0 != memcmp (peer,
1124                      &pc->id,
1125                      sizeof (pc->id))) )
1126     return;
1127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1128               "Sending information about `%s' using address `%s' in state `%s'\n",
1129               GNUNET_i2s(peer),
1130               (NULL != address) ? GST_plugins_a2s (address) : "<none>",
1131               GNUNET_TRANSPORT_ps2s (state));
1132   msg = compose_address_iterate_response_message (peer,
1133                                                   address);
1134   msg->state = htonl (state);
1135   msg->state_timeout = GNUNET_TIME_absolute_hton(state_timeout);
1136   env = GNUNET_MQ_msg_copy (&msg->header);
1137   GNUNET_free (msg);
1138   GNUNET_MQ_send (pc->tc->mq,
1139                   env);
1140 }
1141
1142
1143 /**
1144  * Client asked to obtain information about a specific or all peers
1145  * Process the request.
1146  *
1147  * @param cls the client
1148  * @param msg the peer address information request
1149  */
1150 static void
1151 handle_client_monitor_peers (void *cls,
1152                              const struct PeerMonitorMessage *msg)
1153 {
1154   struct TransportClient *tc = cls;
1155   struct IterationContext pc;
1156
1157   if (CT_NONE != tc->type)
1158   {
1159     GNUNET_break (0);
1160     GNUNET_SERVICE_client_drop (tc->client);
1161     return;
1162   }
1163   GNUNET_SERVICE_client_disable_continue_warning (tc->client);
1164   GNUNET_SERVICE_client_mark_monitor (tc->client);
1165
1166   /* Send initial list */
1167   pc.tc = tc;
1168   if (0 == memcmp (&msg->peer,
1169                    &all_zeros,
1170                    sizeof (struct GNUNET_PeerIdentity)))
1171   {
1172     /* iterate over all neighbours */
1173     pc.all = GNUNET_YES;
1174     pc.id = msg->peer;
1175   }
1176   else
1177   {
1178     /* just return one neighbour */
1179     pc.all = GNUNET_NO;
1180     pc.id = msg->peer;
1181   }
1182   GST_neighbours_iterate (&send_peer_information,
1183                           &pc);
1184
1185   if (GNUNET_YES != ntohl (msg->one_shot))
1186   {
1187     tc->details.monitor_peer = msg->peer;
1188     tc->type = CT_MONITOR;
1189     if (0 != memcmp (&msg->peer,
1190                      &all_zeros,
1191                      sizeof (struct GNUNET_PeerIdentity)))
1192       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193                   "Client %p started monitoring of the peer `%s'\n",
1194                   tc,
1195                   GNUNET_i2s (&msg->peer));
1196     else
1197       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198                   "Client %p started monitoring all peers\n",
1199                   tc);
1200   }
1201   else
1202   {
1203     struct GNUNET_MessageHeader *msg;
1204     struct GNUNET_MQ_Envelope *env;
1205
1206     env = GNUNET_MQ_msg (msg,
1207                          GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE_END);
1208     GNUNET_MQ_send (tc->mq,
1209                     env);
1210   }
1211 }
1212
1213
1214 /**
1215  * Function called by the plugin with information about the
1216  * current sessions managed by the plugin (for monitoring).
1217  *
1218  * @param cls closure
1219  * @param session session handle this information is about,
1220  *        NULL to indicate that we are "in sync" (initial
1221  *        iteration complete)
1222  * @param info information about the state of the session,
1223  *        NULL if @a session is also NULL and we are
1224  *        merely signalling that the initial iteration is over
1225  */
1226 static void
1227 plugin_session_info_cb (void *cls,
1228                         struct GNUNET_ATS_Session *session,
1229                         const struct GNUNET_TRANSPORT_SessionInfo *info)
1230 {
1231   struct GNUNET_MQ_Envelope *env;
1232   struct TransportPluginMonitorMessage *msg;
1233   struct GNUNET_MessageHeader *sync;
1234   size_t size;
1235   size_t slen;
1236   uint16_t alen;
1237   char *name;
1238   char *addr;
1239
1240   if (0 == GNUNET_notification_context_get_size (plugin_nc))
1241   {
1242     GST_plugins_monitor_subscribe (NULL,
1243                                    NULL);
1244     return;
1245   }
1246   if ( (NULL == info) &&
1247        (NULL == session) )
1248   {
1249     /* end of initial iteration */
1250     if (NULL != sync_client)
1251     {
1252       env = GNUNET_MQ_msg (sync,
1253                            GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC);
1254       GNUNET_MQ_send (sync_client->mq,
1255                       env);
1256       sync_client = NULL;
1257     }
1258     return;
1259   }
1260   GNUNET_assert (NULL != info);
1261   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262               "Plugin event for peer %s on transport %s\n",
1263               GNUNET_i2s (&info->address->peer),
1264               info->address->transport_name);
1265   slen = strlen (info->address->transport_name) + 1;
1266   alen = info->address->address_length;
1267   size = sizeof (struct TransportPluginMonitorMessage) + slen + alen;
1268   if (size > UINT16_MAX)
1269   {
1270     GNUNET_break (0);
1271     return;
1272   }
1273   msg = GNUNET_malloc (size);
1274   msg->header.size = htons (size);
1275   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT);
1276   msg->session_state = htons ((uint16_t) info->state);
1277   msg->is_inbound = htons ((int16_t) info->is_inbound);
1278   msg->msgs_pending = htonl (info->num_msg_pending);
1279   msg->bytes_pending = htonl (info->num_bytes_pending);
1280   msg->timeout = GNUNET_TIME_absolute_hton (info->session_timeout);
1281   msg->delay = GNUNET_TIME_absolute_hton (info->receive_delay);
1282   msg->peer = info->address->peer;
1283   msg->session_id = (uint64_t) (intptr_t) session;
1284   msg->plugin_name_len = htons (slen);
1285   msg->plugin_address_len = htons (alen);
1286   name = (char *) &msg[1];
1287   GNUNET_memcpy (name,
1288                  info->address->transport_name,
1289                  slen);
1290   addr = &name[slen];
1291   GNUNET_memcpy (addr,
1292           info->address->address,
1293           alen);
1294   if (NULL != sync_client)
1295   {
1296     struct GNUNET_MQ_Envelope *env;
1297
1298     env = GNUNET_MQ_msg_copy (&msg->header);
1299     GNUNET_MQ_send (sync_client->mq,
1300                     env);
1301   }
1302   else
1303   {
1304     GNUNET_notification_context_broadcast (plugin_nc,
1305                                            &msg->header,
1306                                            GNUNET_NO);
1307   }
1308   GNUNET_free (msg);
1309 }
1310
1311
1312 /**
1313  * Client asked to obtain information about all plugin connections.
1314  *
1315  * @param cls the client
1316  * @param message the peer address information request
1317  */
1318 static void
1319 handle_client_monitor_plugins (void *cls,
1320                                const struct GNUNET_MessageHeader *message)
1321 {
1322   struct TransportClient *tc = cls;
1323
1324   GNUNET_SERVICE_client_mark_monitor (tc->client);
1325   GNUNET_SERVICE_client_disable_continue_warning (tc->client);
1326   GNUNET_notification_context_add (plugin_nc,
1327                                    tc->mq);
1328   GNUNET_assert (NULL == sync_client);
1329   sync_client = tc;
1330   GST_plugins_monitor_subscribe (&plugin_session_info_cb,
1331                                  NULL);
1332 }
1333
1334
1335 /**
1336  * Broadcast the given message to all of our clients.
1337  *
1338  * @param msg message to broadcast
1339  * @param may_drop #GNUNET_YES if the message can be dropped / is payload
1340  */
1341 void
1342 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg,
1343                        int may_drop)
1344 {
1345   struct TransportClient *tc;
1346   int done;
1347
1348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349               "Asked to broadcast message of type %u with %u bytes\n",
1350               (unsigned int) ntohs (msg->type),
1351               (unsigned int) ntohs (msg->size));
1352   done = GNUNET_NO;
1353   for (tc = clients_head; NULL != tc; tc = tc->next)
1354   {
1355     if ( (GNUNET_YES == may_drop) &&
1356          (CT_CORE != tc->type) )
1357       continue; /* skip, this client does not care about payload */
1358     unicast (tc,
1359              msg,
1360              may_drop);
1361     done = GNUNET_YES;
1362   }
1363   if (GNUNET_NO == done)
1364     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1365                 "Message of type %u not delivered, is CORE service up?\n",
1366                 ntohs (msg->type));
1367 }
1368
1369
1370 /**
1371  * Broadcast the new active address to all clients monitoring the peer.
1372  *
1373  * @param peer peer this update is about (never NULL)
1374  * @param address address, NULL on disconnect
1375  * @param state the current state of the peer
1376  * @param state_timeout the time out for the state
1377  */
1378 void
1379 GST_clients_broadcast_peer_notification (const struct GNUNET_PeerIdentity *peer,
1380                                          const struct GNUNET_HELLO_Address *address,
1381                                          enum GNUNET_TRANSPORT_PeerState state,
1382                                          struct GNUNET_TIME_Absolute state_timeout)
1383 {
1384   struct GNUNET_MQ_Envelope *env;
1385   struct PeerIterateResponseMessage *msg;
1386   struct TransportClient *tc;
1387
1388   msg = compose_address_iterate_response_message (peer,
1389                                                   address);
1390   msg->state = htonl (state);
1391   msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
1392   for (tc = clients_head; NULL != tc; tc = tc->next)
1393   {
1394     if (CT_MONITOR != tc->type)
1395       continue;
1396     if ((0 == memcmp (&tc->details.monitor_peer,
1397                       &all_zeros,
1398                       sizeof (struct GNUNET_PeerIdentity))) ||
1399         (0 == memcmp (&tc->details.monitor_peer,
1400                       peer,
1401                       sizeof (struct GNUNET_PeerIdentity))))
1402     {
1403       env = GNUNET_MQ_msg_copy (&msg->header);
1404       GNUNET_MQ_send (tc->mq,
1405                       env);
1406     }
1407   }
1408   GNUNET_free (msg);
1409 }
1410
1411
1412 /**
1413  * Mark the peer as down so we don't call the continuation
1414  * context in the future.
1415  *
1416  * @param cls NULL
1417  * @param peer peer that got disconnected
1418  * @param value a `struct SendTransmitContinuationContext` to mark
1419  * @return #GNUNET_OK (continue to iterate)
1420  */
1421 static int
1422 mark_peer_down (void *cls,
1423                 const struct GNUNET_PeerIdentity *peer,
1424                 void *value)
1425 {
1426   struct SendTransmitContinuationContext *stcc = value;
1427
1428   stcc->down = GNUNET_YES;
1429   return GNUNET_OK;
1430 }
1431
1432
1433 /**
1434  * Notify all clients about a disconnect, and cancel
1435  * pending SEND_OK messages for this peer.
1436  *
1437  * @param peer peer that disconnected
1438  */
1439 void
1440 GST_clients_broadcast_disconnect (const struct GNUNET_PeerIdentity *peer)
1441 {
1442   struct DisconnectInfoMessage disconnect_msg;
1443
1444   GNUNET_CONTAINER_multipeermap_get_multiple (active_stccs,
1445                                               peer,
1446                                               &mark_peer_down,
1447                                               NULL);
1448   disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
1449   disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1450   disconnect_msg.reserved = htonl (0);
1451   disconnect_msg.peer = *peer;
1452   GST_clients_broadcast (&disconnect_msg.header,
1453                          GNUNET_NO);
1454
1455 }
1456
1457
1458 /**
1459  * Transmit our HELLO message to the given (connected) neighbour.
1460  *
1461  * @param cls the 'HELLO' message
1462  * @param peer identity of the peer
1463  * @param address the address
1464  * @param state current state this peer is in
1465  * @param state_timeout timeout for the current state of the peer
1466  * @param bandwidth_in inbound quota in NBO
1467  * @param bandwidth_out outbound quota in NBO
1468  */
1469 static void
1470 transmit_our_hello (void *cls,
1471                     const struct GNUNET_PeerIdentity *peer,
1472                     const struct GNUNET_HELLO_Address *address,
1473                     enum GNUNET_TRANSPORT_PeerState state,
1474                     struct GNUNET_TIME_Absolute state_timeout,
1475                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1476                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1477 {
1478   const struct GNUNET_MessageHeader *hello = cls;
1479
1480   if (0 ==
1481       memcmp (peer,
1482               &GST_my_identity,
1483               sizeof (struct GNUNET_PeerIdentity)))
1484     return; /* not to ourselves */
1485   if (GNUNET_NO == GST_neighbours_test_connected (peer))
1486     return;
1487
1488   GST_neighbours_send (peer,
1489                        hello,
1490                        ntohs (hello->size),
1491                        hello_expiration,
1492                        NULL,
1493                        NULL);
1494 }
1495
1496
1497 /**
1498  * My HELLO has changed. Tell everyone who should know.
1499  *
1500  * @param cls unused
1501  * @param hello new HELLO
1502  */
1503 static void
1504 process_hello_update (void *cls,
1505                       const struct GNUNET_MessageHeader *hello)
1506 {
1507   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508               "Broadcasting HELLO to clients\n");
1509   GST_clients_broadcast (hello, GNUNET_NO);
1510   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511               "Broadcasting HELLO to neighbours\n");
1512   GST_neighbours_iterate (&transmit_our_hello,
1513                           (void *) hello);
1514 }
1515
1516
1517 /**
1518  * We received some payload.  Prepare to pass it on to our clients.
1519  *
1520  * @param address address and (claimed) identity of the other peer
1521  * @param session identifier used for this session (NULL for plugins
1522  *                that do not offer bi-directional communication to the sender
1523  *                using the same "connection")
1524  * @param message the message to process
1525  * @return how long the plugin should wait until receiving more data
1526  */
1527 static struct GNUNET_TIME_Relative
1528 process_payload (const struct GNUNET_HELLO_Address *address,
1529                  struct GNUNET_ATS_Session *session,
1530                  const struct GNUNET_MessageHeader *message)
1531 {
1532   struct GNUNET_TIME_Relative ret;
1533   int do_forward;
1534   struct InboundMessage *im;
1535   size_t msg_size = ntohs (message->size);
1536   size_t size = sizeof(struct InboundMessage) + msg_size;
1537   char buf[size] GNUNET_ALIGN;
1538
1539   do_forward = GNUNET_SYSERR;
1540   ret = GST_neighbours_calculate_receive_delay (&address->peer,
1541                                                 msg_size,
1542                                                 &do_forward);
1543   if (! GST_neighbours_test_connected (&address->peer))
1544   {
1545     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546                 "Discarded %u bytes type %u payload from peer `%s'\n",
1547                 (unsigned int) msg_size,
1548                 ntohs (message->type),
1549                 GNUNET_i2s (&address->peer));
1550     GNUNET_STATISTICS_update (GST_stats, gettext_noop
1551                               ("# bytes payload discarded due to not connected peer"),
1552                               msg_size,
1553                               GNUNET_NO);
1554     return ret;
1555   }
1556
1557   if (GNUNET_YES != do_forward)
1558     return ret;
1559   im = (struct InboundMessage *) buf;
1560   im->header.size = htons (size);
1561   im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
1562   im->peer = address->peer;
1563   GNUNET_memcpy (&im[1],
1564                  message,
1565                  ntohs (message->size));
1566   GST_clients_broadcast (&im->header,
1567                          GNUNET_YES);
1568   return ret;
1569 }
1570
1571
1572 /**
1573  * Task to asynchronously terminate a session.
1574  *
1575  * @param cls the `struct GNUNET_ATS_SessionKiller` with the information for the kill
1576  */
1577 static void
1578 kill_session_task (void *cls)
1579 {
1580   struct GNUNET_ATS_SessionKiller *sk = cls;
1581
1582   sk->task = NULL;
1583   GNUNET_CONTAINER_DLL_remove (sk_head,
1584                                sk_tail,
1585                                sk);
1586   sk->plugin->disconnect_session (sk->plugin->cls,
1587                                   sk->session);
1588   GNUNET_free(sk);
1589 }
1590
1591
1592 /**
1593  * Force plugin to terminate session due to communication
1594  * issue.
1595  *
1596  * @param plugin_name name of the plugin
1597  * @param session session to termiante
1598  */
1599 static void
1600 kill_session (const char *plugin_name,
1601               struct GNUNET_ATS_Session *session)
1602 {
1603   struct GNUNET_TRANSPORT_PluginFunctions *plugin;
1604   struct GNUNET_ATS_SessionKiller *sk;
1605
1606   for (sk = sk_head; NULL != sk; sk = sk->next)
1607     if (sk->session == session)
1608       return;
1609   plugin = GST_plugins_find (plugin_name);
1610   if (NULL == plugin)
1611   {
1612     GNUNET_break(0);
1613     return;
1614   }
1615   /* need to issue disconnect asynchronously */
1616   sk = GNUNET_new (struct GNUNET_ATS_SessionKiller);
1617   sk->session = session;
1618   sk->plugin = plugin;
1619   sk->task = GNUNET_SCHEDULER_add_now (&kill_session_task,
1620                                        sk);
1621   GNUNET_CONTAINER_DLL_insert (sk_head,
1622                                sk_tail,
1623                                sk);
1624 }
1625
1626
1627 /**
1628  * Black list check result for try_connect call
1629  * If connection to the peer is allowed request adddress and ???
1630  *
1631  * @param cls the message
1632  * @param peer the peer
1633  * @param address the address
1634  * @param session the session
1635  * @param result the result
1636  */
1637 static void
1638 connect_bl_check_cont (void *cls,
1639                        const struct GNUNET_PeerIdentity *peer,
1640                        const struct GNUNET_HELLO_Address *address,
1641                        struct GNUNET_ATS_Session *session,
1642                        int result)
1643 {
1644   struct GNUNET_MessageHeader *msg = cls;
1645
1646   if (GNUNET_OK == result)
1647   {
1648     /* Blacklist allows to speak to this peer, forward SYN to neighbours  */
1649     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1650                 "Received SYN message from peer `%s' at `%s'\n",
1651                 GNUNET_i2s (peer),
1652                 GST_plugins_a2s (address));
1653     if (GNUNET_OK !=
1654         GST_neighbours_handle_session_syn (msg,
1655                                            peer))
1656     {
1657       GST_blacklist_abort_matching (address,
1658                                     session);
1659       kill_session (address->transport_name,
1660                     session);
1661     }
1662     GNUNET_free (msg);
1663     return;
1664   }
1665   GNUNET_free (msg);
1666   if (GNUNET_SYSERR == result)
1667     return; /* check was aborted, session destroyed */
1668   /* Blacklist denies to speak to this peer */
1669   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1670               "Discarding SYN message from `%s' due to denied blacklist check\n",
1671               GNUNET_i2s (peer));
1672   kill_session (address->transport_name,
1673                 session);
1674 }
1675
1676
1677 /**
1678  * Function called by the transport for each received message.
1679  *
1680  * @param cls closure, const char* with the name of the plugin we received the message from
1681  * @param address address and (claimed) identity of the other peer
1682  * @param message the message, NULL if we only care about
1683  *                learning about the delay until we should receive again
1684  * @param session identifier used for this session (NULL for plugins
1685  *                that do not offer bi-directional communication to the sender
1686  *                using the same "connection")
1687  * @return how long the plugin should wait until receiving more data
1688  *         (plugins that do not support this, can ignore the return value)
1689  */
1690 struct GNUNET_TIME_Relative
1691 GST_receive_callback (void *cls,
1692                       const struct GNUNET_HELLO_Address *address,
1693                       struct GNUNET_ATS_Session *session,
1694                       const struct GNUNET_MessageHeader *message)
1695 {
1696   const char *plugin_name = cls;
1697   struct GNUNET_TIME_Relative ret;
1698   uint16_t type;
1699
1700   ret = GNUNET_TIME_UNIT_ZERO;
1701   if (NULL == message)
1702     goto end;
1703   type = ntohs (message->type);
1704   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705               "Received message with type %u from peer `%s' at %s\n",
1706               type,
1707               GNUNET_i2s (&address->peer),
1708               GST_plugins_a2s (address));
1709
1710   GNUNET_STATISTICS_update (GST_stats,
1711                             gettext_noop ("# bytes total received"),
1712                             ntohs (message->size),
1713                             GNUNET_NO);
1714   GST_neighbours_notify_data_recv (address,
1715                                    message);
1716   switch (type)
1717   {
1718   case GNUNET_MESSAGE_TYPE_HELLO_LEGACY:
1719     /* Legacy HELLO message, discard  */
1720     return ret;
1721   case GNUNET_MESSAGE_TYPE_HELLO:
1722     if (GNUNET_OK != GST_validation_handle_hello (message))
1723     {
1724       GNUNET_break_op (0);
1725       GST_blacklist_abort_matching (address,
1726                                     session);
1727     }
1728     return ret;
1729   case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
1730     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1731                 "Processing PING from `%s'\n",
1732                 GST_plugins_a2s (address));
1733     if (GNUNET_OK !=
1734         GST_validation_handle_ping (&address->peer,
1735                                     message,
1736                                     address,
1737                                     session))
1738     {
1739       GST_blacklist_abort_matching (address,
1740                                     session);
1741       kill_session (plugin_name,
1742                     session);
1743     }
1744     break;
1745   case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
1746     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1747                "Processing PONG from `%s'\n",
1748                GST_plugins_a2s (address));
1749     if (GNUNET_OK !=
1750         GST_validation_handle_pong (&address->peer,
1751                                     message))
1752     {
1753       GNUNET_break_op (0);
1754       GST_blacklist_abort_matching (address,
1755                                     session);
1756       kill_session (plugin_name, session);
1757     }
1758     break;
1759   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN:
1760     /* Do blacklist check if communication with this peer is allowed */
1761     (void) GST_blacklist_test_allowed (&address->peer,
1762                                        NULL,
1763                                        &connect_bl_check_cont,
1764                                        GNUNET_copy_message (message),
1765                                        address,
1766                                        session);
1767     break;
1768   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK:
1769     if (GNUNET_OK !=
1770         GST_neighbours_handle_session_syn_ack (message,
1771                                                address,
1772                                                session))
1773     {
1774       GST_blacklist_abort_matching (address, session);
1775       kill_session (plugin_name, session);
1776     }
1777     break;
1778   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
1779     if (GNUNET_OK !=
1780         GST_neighbours_handle_session_ack (message,
1781                                            address,
1782                                            session))
1783     {
1784       GNUNET_break_op(0);
1785       GST_blacklist_abort_matching (address, session);
1786       kill_session (plugin_name, session);
1787     }
1788     break;
1789   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
1790     GST_neighbours_handle_disconnect_message (&address->peer,
1791                                               message);
1792     break;
1793   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA:
1794     GST_neighbours_handle_quota_message (&address->peer,
1795                                          message);
1796     break;
1797   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
1798     GST_neighbours_keepalive (&address->peer,
1799                               message);
1800     break;
1801   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
1802     GST_neighbours_keepalive_response (&address->peer,
1803                                        message);
1804     break;
1805   default:
1806     /* should be payload */
1807     GNUNET_STATISTICS_update (GST_stats,
1808                               gettext_noop ("# bytes payload received"),
1809                               ntohs (message->size),
1810                               GNUNET_NO);
1811     ret = process_payload (address,
1812                            session,
1813                            message);
1814     break;
1815   }
1816  end:
1817   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818               "Allowing receive from peer %s to continue in %s\n",
1819               GNUNET_i2s (&address->peer),
1820               GNUNET_STRINGS_relative_time_to_string (ret,
1821                                                       GNUNET_YES));
1822   return ret;
1823 }
1824
1825
1826 /**
1827  * Function that will be called for each address the transport
1828  * is aware that it might be reachable under.  Update our HELLO.
1829  *
1830  * @param cls name of the plugin (const char*)
1831  * @param add_remove should the address added (YES) or removed (NO) from the
1832  *                   set of valid addresses?
1833  * @param address the address to add or remove
1834  */
1835 static void
1836 plugin_env_address_change_notification (void *cls,
1837                                         int add_remove,
1838                                         const struct GNUNET_HELLO_Address *address)
1839 {
1840   static int addresses = 0;
1841
1842   if (GNUNET_YES == add_remove)
1843   {
1844     addresses ++;
1845     GNUNET_STATISTICS_update (GST_stats,
1846                               "# transport addresses",
1847                               1,
1848                               GNUNET_NO);
1849   }
1850   else if (GNUNET_NO == add_remove)
1851   {
1852     if (0 == addresses)
1853     {
1854       GNUNET_break (0);
1855     }
1856     else
1857     {
1858       addresses --;
1859       GNUNET_STATISTICS_update (GST_stats,
1860                                 "# transport addresses",
1861                                 -1,
1862                                 GNUNET_NO);
1863     }
1864   }
1865   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1866               "Transport now has %u addresses to communicate\n",
1867               addresses);
1868   GST_hello_modify_addresses (add_remove,
1869                               address);
1870 }
1871
1872
1873 /**
1874  * Function that will be called whenever the plugin internally
1875  * cleans up a session pointer and hence the service needs to
1876  * discard all of those sessions as well.  Plugins that do not
1877  * use sessions can simply omit calling this function and always
1878  * use NULL wherever a session pointer is needed.  This function
1879  * should be called BEFORE a potential "TransmitContinuation"
1880  * from the "TransmitFunction".
1881  *
1882  * @param cls closure
1883  * @param address which address was the session for
1884  * @param session which session is being destoyed
1885  */
1886 static void
1887 plugin_env_session_end (void *cls,
1888                         const struct GNUNET_HELLO_Address *address,
1889                         struct GNUNET_ATS_Session *session)
1890 {
1891   struct GNUNET_ATS_SessionKiller *sk;
1892
1893   if (NULL == address)
1894   {
1895     GNUNET_break (0);
1896     return;
1897   }
1898   if (NULL == session)
1899   {
1900     GNUNET_break (0);
1901     return;
1902   }
1903   GNUNET_assert (strlen (address->transport_name) > 0);
1904
1905   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1906               "Notification from plugin about terminated session %p from peer `%s' address `%s'\n",
1907               session,
1908               GNUNET_i2s (&address->peer),
1909               GST_plugins_a2s (address));
1910
1911   GST_neighbours_session_terminated (&address->peer,
1912                                      session);
1913   GST_ats_del_session (address,
1914                        session);
1915   GST_blacklist_abort_matching (address,
1916                                 session);
1917
1918   for (sk = sk_head; NULL != sk; sk = sk->next)
1919   {
1920     if (sk->session == session)
1921     {
1922       GNUNET_CONTAINER_DLL_remove (sk_head,
1923                                    sk_tail,
1924                                    sk);
1925       GNUNET_SCHEDULER_cancel (sk->task);
1926       GNUNET_free(sk);
1927       break;
1928     }
1929   }
1930 }
1931
1932
1933 /**
1934  * Black list check result from blacklist check triggered when a
1935  * plugin gave us a new session in #plugin_env_session_start().  If
1936  * connection to the peer is disallowed, kill the session.
1937  *
1938  * @param cls NULL
1939  * @param peer the peer
1940  * @param address address associated with the request
1941  * @param session session associated with the request
1942  * @param result the result
1943  */
1944 static void
1945 plugin_env_session_start_bl_check_cont (void *cls,
1946                                         const struct GNUNET_PeerIdentity *peer,
1947                                         const struct GNUNET_HELLO_Address *address,
1948                                         struct GNUNET_ATS_Session *session,
1949                                         int result)
1950 {
1951   if (GNUNET_OK != result)
1952   {
1953     kill_session (address->transport_name,
1954                   session);
1955     return;
1956   }
1957   if (GNUNET_YES !=
1958       GNUNET_HELLO_address_check_option (address,
1959                                          GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1960   {
1961     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962                 "Informing verifier about inbound session's address `%s'\n",
1963                 GST_plugins_a2s (address));
1964     GST_validation_handle_address (address);
1965   }
1966 }
1967
1968
1969 /**
1970  * Plugin tells transport service about a new inbound session
1971  *
1972  * @param cls unused
1973  * @param address the address
1974  * @param session the new session
1975  * @param scope network scope information
1976  */
1977 static void
1978 plugin_env_session_start (void *cls,
1979                           const struct GNUNET_HELLO_Address *address,
1980                           struct GNUNET_ATS_Session *session,
1981                           enum GNUNET_ATS_Network_Type scope)
1982 {
1983   struct GNUNET_ATS_Properties prop;
1984
1985   if (NULL == address)
1986   {
1987     GNUNET_break(0);
1988     return;
1989   }
1990   if (NULL == session)
1991   {
1992     GNUNET_break(0);
1993     return;
1994   }
1995   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1996               "Notification from plugin `%s' about new session from peer `%s' address `%s'\n",
1997               address->transport_name,
1998               GNUNET_i2s (&address->peer),
1999               GST_plugins_a2s (address));
2000   if (GNUNET_YES ==
2001       GNUNET_HELLO_address_check_option (address,
2002                                          GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2003   {
2004     /* inbound is always new, but outbound MAY already be known, but
2005        for example for UNIX, we have symmetric connections and thus we
2006        may not know the address yet; add if necessary! */
2007     /* FIXME: maybe change API here so we just pass scope? */
2008     memset (&prop,
2009             0,
2010             sizeof (prop));
2011     GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != scope);
2012     prop.scope = scope;
2013     GST_ats_add_inbound_address (address,
2014                                  session,
2015                                  &prop);
2016   }
2017   /* Do blacklist check if communication with this peer is allowed */
2018   (void) GST_blacklist_test_allowed (&address->peer,
2019                                      address->transport_name,
2020                                      &plugin_env_session_start_bl_check_cont,
2021                                      NULL,
2022                                      address,
2023                                      session);
2024 }
2025
2026
2027 /**
2028  * Function called by ATS to notify the callee that the
2029  * assigned bandwidth or address for a given peer was changed.  If the
2030  * callback is called with address/bandwidth assignments of zero, the
2031  * ATS disconnect function will still be called once the disconnect
2032  * actually happened.
2033  *
2034  * @param cls closure
2035  * @param peer the peer this address is intended for
2036  * @param address address to use (for peer given in address)
2037  * @param session session to use (if available)
2038  * @param bandwidth_out assigned outbound bandwidth for the connection in NBO,
2039  *      0 to disconnect from peer
2040  * @param bandwidth_in assigned inbound bandwidth for the connection in NBO,
2041  *      0 to disconnect from peer
2042  * @param ats ATS information
2043  * @param ats_count number of @a ats elements
2044  */
2045 static void
2046 ats_request_address_change (void *cls,
2047                             const struct GNUNET_PeerIdentity *peer,
2048                             const struct GNUNET_HELLO_Address *address,
2049                             struct GNUNET_ATS_Session *session,
2050                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2051                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
2052 {
2053   uint32_t bw_in = ntohl (bandwidth_in.value__);
2054   uint32_t bw_out = ntohl (bandwidth_out.value__);
2055
2056   if (NULL == peer)
2057   {
2058     /* ATS service died, all suggestions become invalid!
2059        (but we'll keep using the allocations for a little
2060        while, to keep going while ATS restarts) */
2061     /* FIXME: We should drop all
2062        connections now, as ATS won't explicitly tell
2063        us and be unaware of ongoing resource allocations! */
2064     return;
2065   }
2066   /* ATS tells me to disconnect from peer */
2067   if ((0 == bw_in) && (0 == bw_out))
2068   {
2069     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2070                 "ATS tells me to disconnect from peer `%s'\n",
2071                 GNUNET_i2s (peer));
2072     GST_neighbours_force_disconnect (peer);
2073     return;
2074   }
2075   GNUNET_assert (NULL != address);
2076   GNUNET_STATISTICS_update (GST_stats,
2077                             "# ATS suggestions received",
2078                             1,
2079                             GNUNET_NO);
2080   GST_neighbours_switch_to_address (address,
2081                                     session,
2082                                     bandwidth_in,
2083                                     bandwidth_out);
2084 }
2085
2086
2087 /**
2088  * Closure for #test_connection_ok().
2089  */
2090 struct TestConnectionContext
2091 {
2092   /**
2093    * Is this the first neighbour we're checking?
2094    */
2095   int first;
2096
2097   /**
2098    * Handle to the blacklisting client we need to ask.
2099    */
2100   struct TransportClient *tc;
2101 };
2102
2103
2104 /**
2105  * Got the result about an existing connection from a new blacklister.
2106  * Shutdown the neighbour if necessary.
2107  *
2108  * @param cls unused
2109  * @param peer the neighbour that was investigated
2110  * @param address address associated with the request
2111  * @param session session associated with the request
2112  * @param allowed #GNUNET_OK if we can keep it,
2113  *                #GNUNET_NO if we must shutdown the connection
2114  */
2115 static void
2116 confirm_or_drop_neighbour (void *cls,
2117                            const struct GNUNET_PeerIdentity *peer,
2118                            const struct GNUNET_HELLO_Address *address,
2119                            struct GNUNET_ATS_Session *session,
2120                            int allowed)
2121 {
2122   if (GNUNET_OK == allowed)
2123     return;                     /* we're done */
2124   GNUNET_STATISTICS_update (GST_stats,
2125                             gettext_noop ("# disconnects due to blacklist"),
2126                             1,
2127                             GNUNET_NO);
2128   GST_neighbours_force_disconnect (peer);
2129 }
2130
2131
2132 /**
2133  * Test if an existing connection is still acceptable given a new
2134  * blacklisting client.
2135  *
2136  * @param cls the `struct TestConnectionContext *`
2137  * @param peer identity of the peer
2138  * @param address the address
2139  * @param state current state this peer is in
2140  * @param state_timeout timeout for the current state of the peer
2141  * @param bandwidth_in bandwidth assigned inbound
2142  * @param bandwidth_out bandwidth assigned outbound
2143  */
2144 static void
2145 test_connection_ok (void *cls,
2146                     const struct GNUNET_PeerIdentity *peer,
2147                     const struct GNUNET_HELLO_Address *address,
2148                     enum GNUNET_TRANSPORT_PeerState state,
2149                     struct GNUNET_TIME_Absolute state_timeout,
2150                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2151                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2152 {
2153   struct TestConnectionContext *tcc = cls;
2154   struct GST_BlacklistCheck *bc;
2155
2156   bc = GNUNET_new (struct GST_BlacklistCheck);
2157   GNUNET_CONTAINER_DLL_insert (bc_head,
2158                                bc_tail,
2159                                bc);
2160   bc->peer = *peer;
2161   bc->address = GNUNET_HELLO_address_copy (address);
2162   bc->cont = &confirm_or_drop_neighbour;
2163   bc->cont_cls = NULL;
2164   bc->bl_pos = tcc->tc;
2165   if (GNUNET_YES == tcc->first)
2166   {
2167     /* all would wait for the same client, no need to
2168      * create more than just the first task right now */
2169     bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
2170                                          bc);
2171     tcc->first = GNUNET_NO;
2172   }
2173 }
2174
2175
2176 /**
2177  * Initialize a blacklisting client.  We got a blacklist-init
2178  * message from this client, add him to the list of clients
2179  * to query for blacklisting.
2180  *
2181  * @param cls the client
2182  * @param message the blacklist-init message that was sent
2183  */
2184 static void
2185 handle_client_blacklist_init (void *cls,
2186                               const struct GNUNET_MessageHeader *message)
2187 {
2188   struct TransportClient *tc = cls;
2189   struct TestConnectionContext tcc;
2190
2191   if (CT_NONE != tc->type)
2192   {
2193     GNUNET_break (0);
2194     GNUNET_SERVICE_client_drop (tc->client);
2195     return;
2196   }
2197   GNUNET_SERVICE_client_mark_monitor (tc->client);
2198   tc->type = CT_BLACKLIST;
2199   tc->details.blacklist.call_receive_done = GNUNET_YES;
2200   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2201               "New blacklist client %p\n",
2202               tc);
2203   /* confirm that all existing connections are OK! */
2204   tcc.tc = tc;
2205   tcc.first = GNUNET_YES;
2206   GST_neighbours_iterate (&test_connection_ok,
2207                           &tcc);
2208 }
2209
2210
2211 /**
2212  * Free the given entry in the blacklist.
2213  *
2214  * @param cls unused
2215  * @param key host identity (unused)
2216  * @param value the blacklist entry
2217  * @return #GNUNET_OK (continue to iterate)
2218  */
2219 static int
2220 free_blacklist_entry (void *cls,
2221                       const struct GNUNET_PeerIdentity *key,
2222                       void *value)
2223 {
2224   char *be = value;
2225
2226   GNUNET_free_non_null (be);
2227   return GNUNET_OK;
2228 }
2229
2230
2231 /**
2232  * Set traffic metric to manipulate
2233  *
2234  * @param cls closure
2235  * @param message containing information
2236  */
2237 static void
2238 handle_client_set_metric (void *cls,
2239                           const struct TrafficMetricMessage *tm)
2240 {
2241   struct TransportClient *tc = cls;
2242
2243   GST_manipulation_set_metric (tm);
2244   GNUNET_SERVICE_client_continue (tc->client);
2245 }
2246
2247
2248 /**
2249  * Function called when the service shuts down.  Unloads our plugins
2250  * and cancels pending validations.
2251  *
2252  * @param cls closure, unused
2253  */
2254 static void
2255 shutdown_task (void *cls)
2256 {
2257   struct AddressToStringContext *cur;
2258
2259   GST_neighbours_stop ();
2260   GST_plugins_unload ();
2261   GST_validation_stop ();
2262   GST_ats_done ();
2263   GNUNET_ATS_scheduling_done (GST_ats);
2264   GST_ats = NULL;
2265   GNUNET_ATS_connectivity_done (GST_ats_connect);
2266   GST_ats_connect = NULL;
2267   GNUNET_ATS_scanner_done (GST_is);
2268   GST_is = NULL;
2269   while (NULL != (cur = a2s_head))
2270   {
2271     GNUNET_CONTAINER_DLL_remove (a2s_head,
2272                                  a2s_tail,
2273                                  cur);
2274     GNUNET_free (cur);
2275   }
2276   if (NULL != plugin_nc)
2277   {
2278     GNUNET_notification_context_destroy (plugin_nc);
2279     plugin_nc = NULL;
2280   }
2281   GNUNET_CONTAINER_multipeermap_destroy (active_stccs);
2282   active_stccs = NULL;
2283   if (NULL != blacklist)
2284   {
2285     GNUNET_CONTAINER_multipeermap_iterate (blacklist,
2286                                            &free_blacklist_entry,
2287                                            NULL);
2288     GNUNET_CONTAINER_multipeermap_destroy (blacklist);
2289     blacklist = NULL;
2290   }
2291   GST_hello_stop ();
2292   GST_manipulation_stop ();
2293
2294   if (NULL != GST_peerinfo)
2295   {
2296     GNUNET_PEERINFO_disconnect (GST_peerinfo);
2297     GST_peerinfo = NULL;
2298   }
2299   if (NULL != GST_stats)
2300   {
2301     GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
2302     GST_stats = NULL;
2303   }
2304   if (NULL != GST_my_private_key)
2305   {
2306     GNUNET_free (GST_my_private_key);
2307     GST_my_private_key = NULL;
2308   }
2309 }
2310
2311
2312 /**
2313  * Perform next action in the blacklist check.
2314  *
2315  * @param cls the `struct GST_BlacklistCheck *`
2316  */
2317 static void
2318 do_blacklist_check (void *cls)
2319 {
2320   struct GST_BlacklistCheck *bc = cls;
2321   struct TransportClient *tc;
2322   struct GNUNET_MQ_Envelope *env;
2323   struct BlacklistMessage *bm;
2324
2325   bc->task = NULL;
2326   while (NULL != (tc = bc->bl_pos))
2327   {
2328     if (CT_BLACKLIST == tc->type)
2329       break;
2330     bc->bl_pos = tc->next;
2331   }
2332   if (NULL == tc)
2333   {
2334     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335                 "No other blacklist clients active, will allow neighbour `%s'\n",
2336                 GNUNET_i2s (&bc->peer));
2337
2338     bc->cont (bc->cont_cls,
2339               &bc->peer,
2340               bc->address,
2341               bc->session,
2342               GNUNET_OK);
2343     GST_blacklist_test_cancel (bc);
2344     return;
2345   }
2346   if ( (NULL != tc->details.blacklist.bc) ||
2347        (GNUNET_NO != tc->details.blacklist.waiting_for_reply) )
2348     return;                     /* someone else busy with this client */
2349   tc->details.blacklist.bc = bc;
2350   env = GNUNET_MQ_msg (bm,
2351                        GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
2352   bm->is_allowed = htonl (0);
2353   bm->peer = bc->peer;
2354   GNUNET_MQ_send (tc->mq,
2355                   env);
2356   if (GNUNET_YES == tc->details.blacklist.call_receive_done)
2357   {
2358     tc->details.blacklist.call_receive_done = GNUNET_NO;
2359     GNUNET_SERVICE_client_continue (tc->client);
2360   }
2361   tc->details.blacklist.waiting_for_reply = GNUNET_YES;
2362 }
2363
2364
2365 /**
2366  * A blacklisting client has sent us reply. Process it.
2367  *
2368  * @param cls the client
2369  * @param msg the blacklist-reply message that was sent
2370  */
2371 static void
2372 handle_client_blacklist_reply (void *cls,
2373                                const struct BlacklistMessage *msg)
2374 {
2375   struct TransportClient *tc = cls;
2376   struct GST_BlacklistCheck *bc;
2377
2378   if (CT_BLACKLIST != tc->type)
2379   {
2380     GNUNET_break (0);
2381     GNUNET_SERVICE_client_drop (tc->client);
2382     return;
2383   }
2384   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385               "Blacklist client %p sent reply for `%s'\n",
2386               tc,
2387               GNUNET_i2s (&msg->peer));
2388   bc = tc->details.blacklist.bc;
2389   tc->details.blacklist.bc = NULL;
2390   tc->details.blacklist.waiting_for_reply = GNUNET_NO;
2391   tc->details.blacklist.call_receive_done = GNUNET_YES;
2392   if (NULL != bc)
2393   {
2394     /* only run this if the blacklist check has not been
2395      * cancelled in the meantime... */
2396     GNUNET_assert (bc->bl_pos == tc);
2397     if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
2398     {
2399       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400                   "Blacklist check failed, peer not allowed\n");
2401       /* For the duration of the continuation, make the ongoing
2402          check invisible (to avoid double-cancellation); then
2403          add it back again so we can re-use GST_blacklist_test_cancel() */
2404       GNUNET_CONTAINER_DLL_remove (bc_head,
2405                                    bc_tail,
2406                                    bc);
2407       bc->cont (bc->cont_cls,
2408                 &bc->peer,
2409                 bc->address,
2410                 bc->session,
2411                 GNUNET_NO);
2412       GNUNET_CONTAINER_DLL_insert (bc_head,
2413                                    bc_tail,
2414                                    bc);
2415       GST_blacklist_test_cancel (bc);
2416       tc->details.blacklist.call_receive_done = GNUNET_NO;
2417       GNUNET_SERVICE_client_continue (tc->client);
2418       return;
2419     }
2420     else
2421     {
2422       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2423                   "Blacklist check succeeded, continuing with checks\n");
2424       tc->details.blacklist.call_receive_done = GNUNET_NO;
2425       GNUNET_SERVICE_client_continue (tc->client);
2426       bc->bl_pos = tc->next;
2427       bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
2428                                            bc);
2429     }
2430   }
2431   /* check if any other blacklist checks are waiting for this blacklister */
2432   for (bc = bc_head; bc != NULL; bc = bc->next)
2433     if ( (bc->bl_pos == tc) &&
2434          (NULL == bc->task) )
2435     {
2436       bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
2437                                            bc);
2438       break;
2439     }
2440 }
2441
2442
2443 /**
2444  * Add the given peer to the blacklist (for the given transport).
2445  *
2446  * @param peer peer to blacklist
2447  * @param transport_name transport to blacklist for this peer, NULL for all
2448  */
2449 void
2450 GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
2451                         const char *transport_name)
2452 {
2453   char *transport = NULL;
2454
2455   if (NULL != transport_name)
2456   {
2457     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2458                 "Adding peer `%s' with plugin `%s' to blacklist\n",
2459                 GNUNET_i2s (peer),
2460                 transport_name);
2461     transport = GNUNET_strdup (transport_name);
2462   }
2463   else
2464     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2465                 "Adding peer `%s' with all plugins to blacklist\n",
2466                 GNUNET_i2s (peer));
2467   if (NULL == blacklist)
2468     blacklist =
2469       GNUNET_CONTAINER_multipeermap_create (TRANSPORT_BLACKLIST_HT_SIZE,
2470                                             GNUNET_NO);
2471
2472   GNUNET_CONTAINER_multipeermap_put (blacklist,
2473                                      peer,
2474                                      transport,
2475                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2476 }
2477
2478
2479 /**
2480  * Abort blacklist if @a address and @a session match.
2481  *
2482  * @param address address used to abort matching checks
2483  * @param session session used to abort matching checks
2484  */
2485 void
2486 GST_blacklist_abort_matching (const struct GNUNET_HELLO_Address *address,
2487                               struct GNUNET_ATS_Session *session)
2488 {
2489   struct GST_BlacklistCheck *bc;
2490   struct GST_BlacklistCheck *n;
2491
2492   n = bc_head;
2493   while (NULL != (bc = n))
2494   {
2495     n = bc->next;
2496     if ( (bc->session == session) &&
2497          (0 == GNUNET_HELLO_address_cmp (bc->address,
2498                                          address)) )
2499     {
2500       bc->cont (bc->cont_cls,
2501                 &bc->peer,
2502                 bc->address,
2503                 bc->session,
2504                 GNUNET_SYSERR);
2505       GST_blacklist_test_cancel (bc);
2506     }
2507   }
2508 }
2509
2510
2511 /**
2512  * Test if the given blacklist entry matches.  If so,
2513  * abort the iteration.
2514  *
2515  * @param cls the transport name to match (const char*)
2516  * @param key the key (unused)
2517  * @param value the 'char *' (name of a blacklisted transport)
2518  * @return #GNUNET_OK if the entry does not match, #GNUNET_NO if it matches
2519  */
2520 static int
2521 test_blacklisted (void *cls,
2522                   const struct GNUNET_PeerIdentity *key,
2523                   void *value)
2524 {
2525   const char *transport_name = cls;
2526   char *be = value;
2527
2528   /* Blacklist entry be:
2529    *  (NULL == be): peer is blacklisted with all plugins
2530    *  (NULL != be): peer is blacklisted for a specific plugin
2531    *
2532    * If (NULL != transport_name) we look for a transport specific entry:
2533    *  if (transport_name == be) forbidden
2534    *
2535    */
2536
2537   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2538               "Comparing BL request for peer `%4s':`%s' with BL entry: `%s'\n",
2539               GNUNET_i2s (key),
2540               (NULL == transport_name) ? "unspecified" : transport_name,
2541               (NULL == be) ? "all plugins" : be);
2542   /* all plugins for this peer were blacklisted: disallow */
2543   if (NULL == value)
2544     return GNUNET_NO;
2545
2546   /* blacklist check for specific transport */
2547   if ( (NULL != transport_name) &&
2548        (NULL != value) )
2549   {
2550     if (0 == strcmp (transport_name,
2551                      be))
2552       return GNUNET_NO;           /* plugin is blacklisted! */
2553   }
2554   return GNUNET_OK;
2555 }
2556
2557
2558 /**
2559  * Test if a peer/transport combination is blacklisted.
2560  *
2561  * @param peer the identity of the peer to test
2562  * @param transport_name name of the transport to test, never NULL
2563  * @param cont function to call with result
2564  * @param cont_cls closure for @a cont
2565  * @param address address to pass back to @a cont, can be NULL
2566  * @param session session to pass back to @a cont, can be NULL
2567  * @return handle to the blacklist check, NULL if the decision
2568  *        was made instantly and @a cont was already called
2569  */
2570 struct GST_BlacklistCheck *
2571 GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
2572                             const char *transport_name,
2573                             GST_BlacklistTestContinuation cont,
2574                             void *cont_cls,
2575                             const struct GNUNET_HELLO_Address *address,
2576                             struct GNUNET_ATS_Session *session)
2577 {
2578   struct GST_BlacklistCheck *bc;
2579   struct TransportClient *tc;
2580
2581   GNUNET_assert (NULL != peer);
2582   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583               "Blacklist check for peer `%s':%s\n",
2584               GNUNET_i2s (peer),
2585               (NULL != transport_name) ? transport_name : "unspecified");
2586
2587   /* Check local blacklist by iterating over hashmap
2588    * If iteration is aborted, we found a matching blacklist entry */
2589   if ((NULL != blacklist) &&
2590       (GNUNET_SYSERR ==
2591        GNUNET_CONTAINER_multipeermap_get_multiple (blacklist, peer,
2592                                                    &test_blacklisted,
2593                                                    (void *) transport_name)))
2594   {
2595     /* Disallowed by config, disapprove instantly */
2596     GNUNET_STATISTICS_update (GST_stats,
2597                               gettext_noop ("# disconnects due to blacklist"),
2598                               1,
2599                               GNUNET_NO);
2600     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2601                 _("Disallowing connection to peer `%s' on transport %s\n"),
2602                 GNUNET_i2s (peer),
2603                 (NULL != transport_name) ? transport_name : "unspecified");
2604     if (NULL != cont)
2605       cont (cont_cls,
2606             peer,
2607             address,
2608             session,
2609             GNUNET_NO);
2610     return NULL;
2611   }
2612
2613   for (tc = clients_head; NULL != tc; tc = tc->next)
2614     if (CT_BLACKLIST == tc->type)
2615       break;
2616   if (NULL == tc)
2617   {
2618     /* no blacklist clients, approve instantly */
2619     if (NULL != cont)
2620       cont (cont_cls,
2621             peer,
2622             address,
2623             session,
2624             GNUNET_OK);
2625     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626                 "Allowing connection to peer `%s' %s\n",
2627                 GNUNET_i2s (peer),
2628                 (NULL != transport_name) ? transport_name : "");
2629     return NULL;
2630   }
2631
2632   /* need to query blacklist clients */
2633   bc = GNUNET_new (struct GST_BlacklistCheck);
2634   GNUNET_CONTAINER_DLL_insert (bc_head,
2635                                bc_tail,
2636                                bc);
2637   bc->peer = *peer;
2638   bc->address = GNUNET_HELLO_address_copy (address);
2639   bc->session = session;
2640   bc->cont = cont;
2641   bc->cont_cls = cont_cls;
2642   bc->bl_pos = tc;
2643   bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check,
2644                                        bc);
2645   return bc;
2646 }
2647
2648
2649 /**
2650  * Cancel a blacklist check.
2651  *
2652  * @param bc check to cancel
2653  */
2654 void
2655 GST_blacklist_test_cancel (struct GST_BlacklistCheck *bc)
2656 {
2657   GNUNET_CONTAINER_DLL_remove (bc_head,
2658                                bc_tail,
2659                                bc);
2660   if (NULL != bc->bl_pos)
2661   {
2662     if ( (CT_BLACKLIST == bc->bl_pos->type) &&
2663          (bc->bl_pos->details.blacklist.bc == bc) )
2664     {
2665       /* we're at the head of the queue, remove us! */
2666       bc->bl_pos->details.blacklist.bc = NULL;
2667     }
2668   }
2669   if (NULL != bc->task)
2670   {
2671     GNUNET_SCHEDULER_cancel (bc->task);
2672     bc->task = NULL;
2673   }
2674   GNUNET_free_non_null (bc->address);
2675   GNUNET_free (bc);
2676 }
2677
2678
2679 /**
2680  * Function to iterate over options in the blacklisting section for a peer.
2681  *
2682  * @param cls closure
2683  * @param section name of the section
2684  * @param option name of the option
2685  * @param value value of the option
2686  */
2687 static void
2688 blacklist_cfg_iter (void *cls,
2689                     const char *section,
2690                     const char *option,
2691                     const char *value)
2692 {
2693   unsigned int *res = cls;
2694   struct GNUNET_PeerIdentity peer;
2695   char *plugs;
2696   char *pos;
2697
2698   if (GNUNET_OK !=
2699       GNUNET_CRYPTO_eddsa_public_key_from_string (option,
2700                                                   strlen (option),
2701                                                   &peer.public_key))
2702     return;
2703
2704   if ((NULL == value) || (0 == strcmp(value, "")))
2705   {
2706     /* Blacklist whole peer */
2707     GST_blacklist_add_peer (&peer, NULL);
2708     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2709                 _("Adding blacklisting entry for peer `%s'\n"),
2710                 GNUNET_i2s (&peer));
2711   }
2712   else
2713   {
2714     plugs = GNUNET_strdup (value);
2715     for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
2716       {
2717         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2718                     _("Adding blacklisting entry for peer `%s':`%s'\n"),
2719                     GNUNET_i2s (&peer), pos);
2720         GST_blacklist_add_peer (&peer, pos);
2721       }
2722     GNUNET_free (plugs);
2723   }
2724   (*res)++;
2725 }
2726
2727
2728 /**
2729  * Read blacklist configuration
2730  *
2731  * @param cfg the configuration handle
2732  * @param my_id my peer identity
2733  */
2734 static void
2735 read_blacklist_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg,
2736                               const struct GNUNET_PeerIdentity *my_id)
2737 {
2738   char cfg_sect[512];
2739   unsigned int res = 0;
2740
2741   GNUNET_snprintf (cfg_sect,
2742                    sizeof (cfg_sect),
2743                    "transport-blacklist-%s",
2744                    GNUNET_i2s_full (my_id));
2745   GNUNET_CONFIGURATION_iterate_section_values (cfg,
2746                                                cfg_sect,
2747                                                &blacklist_cfg_iter,
2748                                                &res);
2749   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750               "Loaded %u blacklisting entries from configuration\n",
2751               res);
2752 }
2753
2754
2755 /**
2756  * Initiate transport service.
2757  *
2758  * @param cls closure
2759  * @param c configuration to use
2760  * @param service the initialized service
2761  */
2762 static void
2763 run (void *cls,
2764      const struct GNUNET_CONFIGURATION_Handle *c,
2765      struct GNUNET_SERVICE_Handle *service)
2766 {
2767   char *keyfile;
2768   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
2769   long long unsigned int max_fd_cfg;
2770   int max_fd_rlimit;
2771   int max_fd;
2772   int friend_only;
2773
2774   /* setup globals */
2775   GST_cfg = c;
2776   if (GNUNET_OK !=
2777       GNUNET_CONFIGURATION_get_value_filename (c,
2778                                                "PEER",
2779                                                "PRIVATE_KEY",
2780                                                &keyfile))
2781   {
2782     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2783         _("Transport service is lacking key configuration settings. Exiting.\n"));
2784     GNUNET_SCHEDULER_shutdown ();
2785     return;
2786   }
2787   if (GNUNET_OK !=
2788       GNUNET_CONFIGURATION_get_value_time (c,
2789                                            "transport",
2790                                            "HELLO_EXPIRATION",
2791                                            &hello_expiration))
2792   {
2793     hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
2794   }
2795   pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
2796   GNUNET_free (keyfile);
2797   GNUNET_assert (NULL != pk);
2798   GST_my_private_key = pk;
2799
2800   GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
2801   GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
2802   GNUNET_CRYPTO_eddsa_key_get_public (GST_my_private_key,
2803                                       &GST_my_identity.public_key);
2804   GNUNET_assert (NULL != GST_my_private_key);
2805
2806   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
2807              "My identity is `%s'\n",
2808              GNUNET_i2s_full (&GST_my_identity));
2809
2810   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
2811                                  NULL);
2812   if (NULL == GST_peerinfo)
2813   {
2814     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2815                 _("Could not access PEERINFO service.  Exiting.\n"));
2816     GNUNET_SCHEDULER_shutdown ();
2817     return;
2818   }
2819
2820   max_fd_rlimit = 0;
2821 #if HAVE_GETRLIMIT
2822   {
2823     struct rlimit r_file;
2824
2825     if (0 == getrlimit (RLIMIT_NOFILE,
2826                         &r_file))
2827     {
2828       max_fd_rlimit = r_file.rlim_cur;
2829       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2830                   "Maximum number of open files was: %u/%u\n",
2831                   (unsigned int) r_file.rlim_cur,
2832                   (unsigned int) r_file.rlim_max);
2833     }
2834     max_fd_rlimit = (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
2835   }
2836 #endif
2837   if (GNUNET_OK !=
2838       GNUNET_CONFIGURATION_get_value_number (GST_cfg,
2839                                              "transport",
2840                                              "MAX_FD",
2841                                              &max_fd_cfg))
2842     max_fd_cfg = max_fd_rlimit;
2843
2844   if (max_fd_cfg > max_fd_rlimit)
2845     max_fd = max_fd_cfg;
2846   else
2847     max_fd = max_fd_rlimit;
2848   if (max_fd < DEFAULT_MAX_FDS)
2849     max_fd = DEFAULT_MAX_FDS;
2850
2851   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2852               "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
2853               max_fd,
2854               (max_fd / 3),
2855               (max_fd / 3) * 2);
2856
2857   friend_only = GNUNET_CONFIGURATION_get_value_yesno (GST_cfg,
2858                                                       "topology",
2859                                                       "FRIENDS-ONLY");
2860   if (GNUNET_SYSERR == friend_only)
2861     friend_only = GNUNET_NO; /* According to topology defaults */
2862   /* start subsystems */
2863   read_blacklist_configuration (GST_cfg,
2864                                 &GST_my_identity);
2865   GST_is = GNUNET_ATS_scanner_init ();
2866   GST_ats_connect = GNUNET_ATS_connectivity_init (GST_cfg);
2867   GST_ats = GNUNET_ATS_scheduling_init (GST_cfg,
2868                                         &ats_request_address_change,
2869                                         NULL);
2870   GST_ats_init ();
2871   GST_manipulation_init ();
2872   GST_plugins_load (&GST_manipulation_recv,
2873                     &plugin_env_address_change_notification,
2874                     &plugin_env_session_start,
2875                     &plugin_env_session_end);
2876   GST_hello_start (friend_only,
2877                    &process_hello_update,
2878                    NULL);
2879   GST_neighbours_start ((max_fd / 3) * 2);
2880   active_stccs = GNUNET_CONTAINER_multipeermap_create (128,
2881                                                        GNUNET_YES);
2882   plugin_nc = GNUNET_notification_context_create (0);
2883   GST_validation_start ((max_fd / 3));
2884 }
2885
2886
2887 /**
2888  * Define "main" method using service macro.
2889  */
2890 GNUNET_SERVICE_MAIN
2891 ("transport",
2892  GNUNET_SERVICE_OPTION_NONE,
2893  &run,
2894  &client_connect_cb,
2895  &client_disconnect_cb,
2896  NULL,
2897  GNUNET_MQ_hd_fixed_size (client_start,
2898                           GNUNET_MESSAGE_TYPE_TRANSPORT_START,
2899                           struct StartMessage,
2900                           NULL),
2901  GNUNET_MQ_hd_var_size (client_hello,
2902                         GNUNET_MESSAGE_TYPE_HELLO,
2903                         struct GNUNET_MessageHeader,
2904                         NULL),
2905  GNUNET_MQ_hd_var_size (client_send,
2906                         GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
2907                         struct OutboundMessage,
2908                         NULL),
2909  GNUNET_MQ_hd_var_size (client_address_to_string,
2910                         GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING,
2911                         struct AddressLookupMessage,
2912                         NULL),
2913  GNUNET_MQ_hd_fixed_size (client_monitor_peers,
2914                           GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST,
2915                           struct PeerMonitorMessage,
2916                           NULL),
2917  GNUNET_MQ_hd_fixed_size (client_blacklist_init,
2918                           GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
2919                           struct GNUNET_MessageHeader,
2920                           NULL),
2921  GNUNET_MQ_hd_fixed_size (client_blacklist_reply,
2922                           GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
2923                           struct BlacklistMessage,
2924                           NULL),
2925  GNUNET_MQ_hd_fixed_size (client_set_metric,
2926                           GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC,
2927                           struct TrafficMetricMessage,
2928                           NULL),
2929  GNUNET_MQ_hd_fixed_size (client_monitor_plugins,
2930                           GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_START,
2931                           struct GNUNET_MessageHeader,
2932                           NULL),
2933  GNUNET_MQ_handler_end ());
2934
2935
2936 /* end of file gnunet-service-transport.c */