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