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