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