-remove deprecated try_disconnect API
[oweals/gnunet.git] / src / transport / gnunet-service-transport_clients.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file transport/gnunet-service-transport_clients.c
23  * @brief communication with clients (core service and monitors)
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet-service-transport_blacklist.h"
28 #include "gnunet-service-transport_clients.h"
29 #include "gnunet-service-transport_hello.h"
30 #include "gnunet-service-transport_neighbours.h"
31 #include "gnunet-service-transport_plugins.h"
32 #include "gnunet-service-transport_validation.h"
33 #include "gnunet-service-transport_manipulation.h"
34 #include "gnunet-service-transport.h"
35 #include "transport.h"
36
37
38 /**
39  * How many messages can we have pending for a given client process
40  * before we start to drop incoming messages?  We typically should
41  * have only one client and so this would be the primary buffer for
42   * messages, so the number should be chosen rather generously.
43  *
44  * The expectation here is that most of the time the queue is large
45  * enough so that a drop is virtually never required.  Note that
46  * this value must be about as large as 'TOTAL_MSGS' in the
47  * 'test_transport_api_reliability.c', otherwise that testcase may
48  * fail.
49  */
50 #define MAX_PENDING (128 * 1024)
51
52
53 /**
54  * Linked list of messages to be transmitted to the client.  Each
55  * entry is followed by the actual message.
56  */
57 struct ClientMessageQueueEntry
58 {
59   /**
60    * This is a doubly-linked list.
61    */
62   struct ClientMessageQueueEntry *next;
63
64   /**
65    * This is a doubly-linked list.
66    */
67   struct ClientMessageQueueEntry *prev;
68 };
69
70
71 /**
72  * Client connected to the transport service.
73  */
74 struct TransportClient
75 {
76
77   /**
78    * This is a doubly-linked list.
79    */
80   struct TransportClient *next;
81
82   /**
83    * This is a doubly-linked list.
84    */
85   struct TransportClient *prev;
86
87   /**
88    * Handle to the client.
89    */
90   struct GNUNET_SERVER_Client *client;
91
92   /**
93    * Linked list of messages yet to be transmitted to
94    * the client.
95    */
96   struct ClientMessageQueueEntry *message_queue_head;
97
98   /**
99    * Tail of linked list of messages yet to be transmitted to the
100    * client.
101    */
102   struct ClientMessageQueueEntry *message_queue_tail;
103
104   /**
105    * Current transmit request handle.
106    */
107   struct GNUNET_SERVER_TransmitHandle *th;
108
109   /**
110    * Length of the list of messages pending for this client.
111    */
112   unsigned int message_count;
113
114   /**
115    * Is this client interested in payload messages?
116    */
117   int send_payload;
118 };
119
120
121 /**
122  * Context for address to string operations
123  */
124 struct AddressToStringContext
125 {
126   /**
127    * This is a doubly-linked list.
128    */
129   struct AddressToStringContext *next;
130
131   /**
132    * This is a doubly-linked list.
133    */
134   struct AddressToStringContext *prev;
135
136   /**
137    * Transmission context
138    */
139   struct GNUNET_SERVER_TransmitContext* tc;
140 };
141
142
143 /**
144  * Client monitoring changes of active addresses or validations
145  * of our neighbours. Which type is being monitored depends on the
146  * DLL this struct is in.
147  */
148 struct MonitoringClient
149 {
150   /**
151    * This is a doubly-linked list.
152    */
153   struct MonitoringClient *next;
154
155   /**
156    * This is a doubly-linked list.
157    */
158   struct MonitoringClient *prev;
159
160   /**
161    * Handle to the client.
162    */
163   struct GNUNET_SERVER_Client *client;
164
165   /**
166    * Peer identity to monitor the addresses of.
167    * Zero to monitor all neighrours.
168    */
169   struct GNUNET_PeerIdentity peer;
170
171 };
172
173
174 /**
175  * Head of linked list of all clients to this service.
176  */
177 static struct TransportClient *clients_head;
178
179 /**
180  * Tail of linked list of all clients to this service.
181  */
182 static struct TransportClient *clients_tail;
183
184 /**
185  * Head of linked list of all pending address iterations
186  */
187 static struct AddressToStringContext *a2s_head;
188
189 /**
190  * Tail of linked list of all pending address iterations
191  */
192 static struct AddressToStringContext *a2s_tail;
193
194 /**
195  * Head of linked list of monitoring clients.
196  */
197 static struct MonitoringClient *peer_monitoring_clients_head;
198
199 /**
200  * Tail of linked list of monitoring clients.
201  */
202 static struct MonitoringClient *peer_monitoring_clients_tail;
203
204 /**
205  * Head of linked list of validation monitoring clients.
206  */
207 static struct MonitoringClient *val_monitoring_clients_head;
208
209 /**
210  * Tail of linked list of validation monitoring clients.
211  */
212 static struct MonitoringClient *val_monitoring_clients_tail;
213
214 /**
215  * Notification context, to send updates on changes to active addresses
216  * of our neighbours.
217  */
218 static struct GNUNET_SERVER_NotificationContext *peer_nc;
219
220 /**
221  * Notification context, to send updates on changes to active addresses
222  * of our neighbours.
223  */
224 static struct GNUNET_SERVER_NotificationContext *val_nc;
225
226 /**
227  * Notification context, to send updates on changes to active plugin
228  * connections.
229  */
230 static struct GNUNET_SERVER_NotificationContext *plugin_nc;
231
232 /**
233  * Plugin monitoring client we are currently syncing, NULL if all
234  * monitoring clients are in sync.
235  */
236 static struct GNUNET_SERVER_Client *sync_client;
237
238 /**
239  * Peer identity that is all zeros, used as a way to indicate
240  * "all peers".  Used for comparissons.
241  */
242 static struct GNUNET_PeerIdentity all_zeros;
243
244
245 /**
246  * Find the internal handle associated with the given client handle.
247  *
248  * @param client server's client handle to look up
249  * @return internal client handle
250  */
251 static struct TransportClient *
252 lookup_client (struct GNUNET_SERVER_Client *client)
253 {
254   return GNUNET_SERVER_client_get_user_context (client,
255                                                 struct TransportClient);
256 }
257
258
259 /**
260  * Create the internal handle for the given server client handle.
261  *
262  * @param client server's client handle to create our internal handle for
263  * @return fresh internal client handle
264  */
265 static struct TransportClient *
266 setup_client (struct GNUNET_SERVER_Client *client)
267 {
268   struct TransportClient *tc;
269
270   GNUNET_assert (NULL == lookup_client (client));
271   tc = GNUNET_new (struct TransportClient);
272   tc->client = client;
273   GNUNET_SERVER_client_set_user_context (client, tc);
274   GNUNET_CONTAINER_DLL_insert (clients_head,
275                                clients_tail,
276                                tc);
277   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278               "Client %p connected\n",
279               tc);
280   return tc;
281 }
282
283
284 /**
285  * Find the handle to the monitoring client associated with the given
286  * client handle.
287  *
288  * @param head the head of the client queue to look in
289  * @param client server's client handle to look up
290  * @return handle to the monitoring client
291  */
292 static struct MonitoringClient *
293 lookup_monitoring_client (struct MonitoringClient *head,
294                           struct GNUNET_SERVER_Client *client)
295 {
296   struct MonitoringClient *mc;
297
298   for (mc = head; NULL != mc; mc = mc->next)
299     if (mc->client == client)
300       return mc;
301   return NULL;
302 }
303
304
305 /**
306  * Setup a new monitoring client using the given server client handle and
307  * the peer identity.
308  *
309  * @param client server's client handle to create our internal handle for
310  * @param peer identity of the peer to monitor the addresses of,
311  *             zero to monitor all neighrours.
312  * @return handle to the new monitoring client
313  */
314 static struct MonitoringClient *
315 setup_peer_monitoring_client (struct GNUNET_SERVER_Client *client,
316                               const struct GNUNET_PeerIdentity *peer)
317 {
318   struct MonitoringClient *mc;
319
320   GNUNET_assert (NULL ==
321                  lookup_monitoring_client (peer_monitoring_clients_head,
322                                            client));
323   mc = GNUNET_new (struct MonitoringClient);
324   mc->client = client;
325   mc->peer = *peer;
326   GNUNET_CONTAINER_DLL_insert (peer_monitoring_clients_head,
327                                peer_monitoring_clients_tail,
328                                mc);
329   GNUNET_SERVER_client_mark_monitor (client);
330   GNUNET_SERVER_notification_context_add (peer_nc,
331                                           client);
332   if (0 != memcmp (peer,
333                    &all_zeros,
334                    sizeof (struct GNUNET_PeerIdentity)))
335     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336                 "Client %p started monitoring of the peer `%s'\n",
337                 mc,
338                 GNUNET_i2s (peer));
339   else
340     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341                 "Client %p started monitoring all peers\n",
342                 mc);
343   return mc;
344 }
345
346
347 /**
348  * Setup a new monitoring client using the given server client handle and
349  * the peer identity.
350  *
351  * @param client server's client handle to create our internal handle for
352  * @param peer identity of the peer to monitor the addresses of,
353  *             zero to monitor all neighrours.
354  * @return handle to the new monitoring client
355  */
356 static struct MonitoringClient *
357 setup_val_monitoring_client (struct GNUNET_SERVER_Client *client,
358                              struct GNUNET_PeerIdentity *peer)
359 {
360   struct MonitoringClient *mc;
361
362   GNUNET_assert (NULL ==
363                  lookup_monitoring_client (val_monitoring_clients_head,
364                                            client));
365   mc = GNUNET_new (struct MonitoringClient);
366   mc->client = client;
367   mc->peer = *peer;
368   GNUNET_CONTAINER_DLL_insert (val_monitoring_clients_head,
369                                val_monitoring_clients_tail,
370                                mc);
371   GNUNET_SERVER_notification_context_add (val_nc, client);
372
373   if (0 != memcmp (peer,
374                    &all_zeros,
375                    sizeof (struct GNUNET_PeerIdentity)))
376     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377                 "Client %p started monitoring of the peer `%s'\n",
378                 mc,
379                 GNUNET_i2s (peer));
380   else
381     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382                 "Client %p started monitoring all peers\n",
383                 mc);
384   return mc;
385 }
386
387
388 /**
389  * Function called to notify a client about the socket being ready to
390  * queue more data.  @a buf will be NULL and @a size zero if the socket
391  * was closed for writing in the meantime.
392  *
393  * @param cls closure
394  * @param size number of bytes available in @a buf
395  * @param buf where the callee should write the message
396  * @return number of bytes written to @a buf
397  */
398 static size_t
399 transmit_to_client_callback (void *cls,
400                              size_t size,
401                              void *buf)
402 {
403   struct TransportClient *tc = cls;
404   struct ClientMessageQueueEntry *q;
405   const struct GNUNET_MessageHeader *msg;
406   char *cbuf;
407   uint16_t msize;
408   size_t tsize;
409
410   tc->th = NULL;
411   if (NULL == buf)
412   {
413     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414                 "Transmission to client failed, closing connection.\n");
415     return 0;
416   }
417   cbuf = buf;
418   tsize = 0;
419   while (NULL != (q = tc->message_queue_head))
420   {
421     msg = (const struct GNUNET_MessageHeader *) &q[1];
422     msize = ntohs (msg->size);
423     if (msize + tsize > size)
424       break;
425     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426                 "Transmitting message of type %u to client %p.\n",
427                 ntohs (msg->type),
428                 tc);
429     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
430                                  tc->message_queue_tail,
431                                  q);
432     tc->message_count--;
433     memcpy (&cbuf[tsize], msg, msize);
434     GNUNET_free (q);
435     tsize += msize;
436   }
437   if (NULL != q)
438   {
439     GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
440     tc->th =
441         GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
442                                              GNUNET_TIME_UNIT_FOREVER_REL,
443                                              &transmit_to_client_callback, tc);
444     GNUNET_assert (NULL != tc->th);
445   }
446   return tsize;
447 }
448
449
450 /**
451  * Queue the given message for transmission to the given client
452  *
453  * @param tc target of the message
454  * @param msg message to transmit
455  * @param may_drop #GNUNET_YES if the message can be dropped
456  */
457 static void
458 unicast (struct TransportClient *tc,
459          const struct GNUNET_MessageHeader *msg,
460          int may_drop)
461 {
462   struct ClientMessageQueueEntry *q;
463   uint16_t msize;
464
465   if (NULL == msg)
466   {
467     GNUNET_break (0);
468     return;
469   }
470   if ( (tc->message_count >= MAX_PENDING) &&
471        (GNUNET_YES == may_drop) )
472   {
473     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474                 "Dropping message of type %u and size %u, have %u/%u messages pending\n",
475                 ntohs (msg->type),
476                 ntohs (msg->size),
477                 tc->message_count,
478                 MAX_PENDING);
479     GNUNET_STATISTICS_update (GST_stats,
480                               gettext_noop
481                               ("# messages dropped due to slow client"), 1,
482                               GNUNET_NO);
483     return;
484   }
485   msize = ntohs (msg->size);
486   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
487   q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
488   memcpy (&q[1], msg, msize);
489   GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
490                                     tc->message_queue_tail,
491                                     q);
492   tc->message_count++;
493   if (NULL != tc->th)
494     return;
495   tc->th =
496       GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
497                                            GNUNET_TIME_UNIT_FOREVER_REL,
498                                            &transmit_to_client_callback, tc);
499   GNUNET_assert (NULL != tc->th);
500 }
501
502
503 /**
504  * Called whenever a client is disconnected.  Frees our
505  * resources associated with that client.
506  *
507  * @param cls closure, NULL
508  * @param client identification of the client
509  */
510 static void
511 client_disconnect_notification (void *cls,
512                                 struct GNUNET_SERVER_Client *client)
513 {
514   struct TransportClient *tc;
515   struct MonitoringClient *mc;
516   struct ClientMessageQueueEntry *mqe;
517
518   if (NULL == client)
519     return;
520   mc = lookup_monitoring_client (peer_monitoring_clients_head,
521                                  client);
522   if (NULL != mc)
523   {
524     GNUNET_CONTAINER_DLL_remove (peer_monitoring_clients_head,
525                                  peer_monitoring_clients_tail,
526                                  mc);
527     GNUNET_free (mc);
528   }
529   mc = lookup_monitoring_client (val_monitoring_clients_head,
530                                  client);
531   if (NULL != mc)
532   {
533     GNUNET_CONTAINER_DLL_remove (val_monitoring_clients_head,
534                                  val_monitoring_clients_tail,
535                                  mc);
536     GNUNET_free (mc);
537   }
538   tc = lookup_client (client);
539   if (NULL == tc)
540     return;
541   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542               "Client %p disconnected, cleaning up.\n",
543               tc);
544   while (NULL != (mqe = tc->message_queue_head))
545   {
546     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
547                                  tc->message_queue_tail,
548                                  mqe);
549     tc->message_count--;
550     GNUNET_free (mqe);
551   }
552   GNUNET_CONTAINER_DLL_remove (clients_head,
553                                clients_tail,
554                                tc);
555   GNUNET_SERVER_client_set_user_context (client, NULL);
556   if (NULL != tc->th)
557   {
558     GNUNET_SERVER_notify_transmit_ready_cancel (tc->th);
559     tc->th = NULL;
560   }
561   GNUNET_break (0 == tc->message_count);
562   GNUNET_free (tc);
563 }
564
565
566 /**
567  * Function called for each of our connected neighbours.  Notify the
568  * client about the existing neighbour.
569  *
570  * @param cls the `struct TransportClient *` to notify
571  * @param peer identity of the neighbour
572  * @param address the address
573  * @param state the current state of the peer
574  * @param state_timeout the time out for the state
575  * @param bandwidth_in inbound bandwidth in NBO
576  * @param bandwidth_out outbound bandwidth in NBO
577  */
578 static void
579 notify_client_about_neighbour (void *cls,
580                                const struct GNUNET_PeerIdentity *peer,
581                                const struct GNUNET_HELLO_Address *address,
582                                enum GNUNET_TRANSPORT_PeerState state,
583                                struct GNUNET_TIME_Absolute state_timeout,
584                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
585                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
586 {
587   struct TransportClient *tc = cls;
588   struct ConnectInfoMessage cim;
589
590   if (GNUNET_NO == GST_neighbours_test_connected (peer))
591     return;
592   cim.header.size = htons (sizeof (struct ConnectInfoMessage));
593   cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
594   cim.id = *peer;
595   cim.quota_in = bandwidth_in;
596   cim.quota_out = bandwidth_out;
597   unicast (tc, &cim.header, GNUNET_NO);
598 }
599
600
601 /**
602  * Initialize a normal client.  We got a start message from this
603  * client, add him to the list of clients for broadcasting of inbound
604  * messages.
605  *
606  * @param cls unused
607  * @param client the client
608  * @param message the start message that was sent
609  */
610 static void
611 clients_handle_start (void *cls,
612                       struct GNUNET_SERVER_Client *client,
613                       const struct GNUNET_MessageHeader *message)
614 {
615   const struct StartMessage *start;
616   const struct GNUNET_MessageHeader *hello;
617   struct TransportClient *tc;
618   uint32_t options;
619
620   tc = lookup_client (client);
621   if (NULL != tc)
622   {
623     /* got 'start' twice from the same client, not allowed */
624     GNUNET_break (0);
625     GNUNET_SERVER_receive_done (client,
626                                 GNUNET_SYSERR);
627     return;
628   }
629   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
630               "Client %p sent START\n",
631               client);
632   start = (const struct StartMessage *) message;
633   options = ntohl (start->options);
634   if ((0 != (1 & options)) &&
635       (0 !=
636        memcmp (&start->self,
637                &GST_my_identity,
638                sizeof (struct GNUNET_PeerIdentity))))
639   {
640     /* client thinks this is a different peer, reject */
641     GNUNET_break (0);
642     GNUNET_SERVER_receive_done (client,
643                                 GNUNET_SYSERR);
644     return;
645   }
646   tc = setup_client (client);
647   tc->send_payload = (0 != (2 & options));
648   hello = GST_hello_get ();
649   if (NULL != hello)
650     unicast (tc,
651              hello,
652              GNUNET_NO);
653   GST_neighbours_iterate (&notify_client_about_neighbour,
654                           tc);
655   GNUNET_SERVER_receive_done (client,
656                               GNUNET_OK);
657 }
658
659
660 /**
661  * Client sent us a HELLO.  Process the request.
662  *
663  * @param cls unused
664  * @param client the client
665  * @param message the HELLO message
666  */
667 static void
668 clients_handle_hello (void *cls,
669                       struct GNUNET_SERVER_Client *client,
670                       const struct GNUNET_MessageHeader *message)
671 {
672   GST_validation_handle_hello (message);
673   GNUNET_SERVER_receive_done (client, GNUNET_OK);
674 }
675
676
677 /**
678  * Closure for #handle_send_transmit_continuation()
679  */
680 struct SendTransmitContinuationContext
681 {
682   /**
683    * Client that made the request.
684    */
685   struct GNUNET_SERVER_Client *client;
686
687   /**
688    * Peer that was the target.
689    */
690   struct GNUNET_PeerIdentity target;
691
692   /**
693    * At what time did we receive the message?
694    */
695   struct GNUNET_TIME_Absolute send_time;
696 };
697
698
699 /**
700  * Function called after the transmission is done.  Notify the client that it is
701  * OK to send the next message.
702  *
703  * @param cls closure
704  * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
705  * @param bytes_payload bytes payload sent
706  * @param bytes_on_wire bytes sent on wire
707  */
708 static void
709 handle_send_transmit_continuation (void *cls,
710                                    int success,
711                                    size_t bytes_payload,
712                                    size_t bytes_on_wire)
713 {
714   struct SendTransmitContinuationContext *stcc = cls;
715   struct SendOkMessage send_ok_msg;
716   struct GNUNET_TIME_Relative delay;
717   const struct GNUNET_HELLO_Address *addr;
718
719   delay = GNUNET_TIME_absolute_get_duration (stcc->send_time);
720   addr = GST_neighbour_get_current_address (&stcc->target);
721   if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
722     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
723                 "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
724                 GNUNET_STRINGS_relative_time_to_string (delay,
725                                                         GNUNET_YES),
726                 (unsigned int) bytes_payload,
727                 (unsigned int) bytes_on_wire,
728                 GNUNET_i2s (&stcc->target),
729                 success,
730                 (NULL != addr) ? addr->transport_name : "%");
731   else
732     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733                 "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
734                 GNUNET_STRINGS_relative_time_to_string (delay,
735                                                         GNUNET_YES),
736                 (unsigned int) bytes_payload,
737                 (unsigned int) bytes_on_wire,
738                 GNUNET_i2s (&stcc->target),
739                 success,
740                 (NULL != addr) ? addr->transport_name : "%");
741
742   if (GST_neighbours_test_connected (&stcc->target))
743   {
744     /* Only send confirmation if we are still connected */
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     GST_clients_unicast (stcc->client,
752                          &send_ok_msg.header,
753                          GNUNET_NO);
754   }
755   GNUNET_SERVER_client_drop (stcc->client);
756   GNUNET_free (stcc);
757 }
758
759
760 /**
761  * Client asked for transmission to a peer.  Process the request.
762  *
763  * @param cls unused
764  * @param client the client
765  * @param message the send message that was sent
766  */
767 static void
768 clients_handle_send (void *cls,
769                      struct GNUNET_SERVER_Client *client,
770                      const struct GNUNET_MessageHeader *message)
771 {
772   const struct OutboundMessage *obm;
773   const struct GNUNET_MessageHeader *obmm;
774   struct SendTransmitContinuationContext *stcc;
775   uint16_t size;
776   uint16_t msize;
777   struct TransportClient *tc;
778
779   tc = lookup_client (client);
780   if (NULL == tc)
781   {
782     /* client asked for transmission before 'START' */
783     GNUNET_break (0);
784     GNUNET_SERVER_receive_done (client,
785                                 GNUNET_SYSERR);
786     return;
787   }
788
789   size = ntohs (message->size);
790   if (size <
791       sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
792   {
793     GNUNET_break (0);
794     GNUNET_SERVER_receive_done (client,
795                                 GNUNET_SYSERR);
796     return;
797   }
798   obm = (const struct OutboundMessage *) message;
799   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
800   msize = size - sizeof (struct OutboundMessage);
801   if (msize < sizeof (struct GNUNET_MessageHeader))
802   {
803     GNUNET_break (0);
804     GNUNET_SERVER_receive_done (client,
805                                 GNUNET_SYSERR);
806     return;
807   }
808
809   if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
810   {
811     /* not connected, not allowed to send; can happen due to asynchronous operations */
812     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813                 "Could not send message to peer `%s': not connected\n",
814                 GNUNET_i2s (&obm->peer));
815     GNUNET_STATISTICS_update (GST_stats,
816                               gettext_noop
817                               ("# bytes payload dropped (other peer was not connected)"),
818                               msize, GNUNET_NO);
819     GNUNET_SERVER_receive_done (client,
820                                 GNUNET_OK);
821     return;
822   }
823   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824               "Received SEND request for `%s' and first message of type %u and total size %u\n",
825               GNUNET_i2s (&obm->peer),
826               ntohs (obmm->type),
827               msize);
828   GNUNET_SERVER_receive_done (client,
829                               GNUNET_OK);
830   stcc = GNUNET_new (struct SendTransmitContinuationContext);
831   stcc->target = obm->peer;
832   stcc->client = client;
833   stcc->send_time = GNUNET_TIME_absolute_get ();
834   GNUNET_SERVER_client_keep (client);
835   GST_manipulation_send (&obm->peer,
836                          obmm,
837                          msize,
838                          GNUNET_TIME_relative_ntoh (obm->timeout),
839                          &handle_send_transmit_continuation,
840                          stcc);
841 }
842
843
844 /**
845  * Handle request connect message
846  *
847  * @param cls closure (always NULL)
848  * @param client identification of the client
849  * @param message the actual message
850  */
851 static void
852 clients_handle_request_connect (void *cls,
853                                 struct GNUNET_SERVER_Client *client,
854                                 const struct GNUNET_MessageHeader *message)
855 {
856   const struct TransportRequestConnectMessage *trcm;
857
858   trcm = (const struct TransportRequestConnectMessage *) message;
859   GNUNET_break (0 == ntohl (trcm->reserved));
860   GNUNET_STATISTICS_update (GST_stats,
861                             gettext_noop
862                             ("# REQUEST CONNECT messages received"), 1,
863                             GNUNET_NO);
864   if (0 == memcmp (&trcm->peer,
865                    &GST_my_identity,
866                    sizeof (struct GNUNET_PeerIdentity)))
867   {
868     GNUNET_break (0);
869     GNUNET_SERVER_receive_done (client,
870                                 GNUNET_OK);
871     return;
872   }
873   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874               "Received a request connect message for peer `%s'\n",
875               GNUNET_i2s (&trcm->peer));
876   GST_neighbours_try_connect (&trcm->peer);
877   GNUNET_SERVER_receive_done (client, GNUNET_OK);
878 }
879
880
881 /**
882  * Take the given address and append it to the set of results sent back to
883  * the client.  This function may be called serveral times for a single
884  * conversion.   The last invocation will be with a @a address of
885  * NULL and a @a res of #GNUNET_OK.  Thus, to indicate conversion
886  * errors, the callback might be called first with @a address NULL and
887  * @a res being #GNUNET_SYSERR.  In that case, there will still be a
888  * subsequent call later with @a address NULL and @a res #GNUNET_OK.
889  *
890  * @param cls the transmission context used (`struct GNUNET_SERVER_TransmitContext *`)
891  * @param buf text to transmit (contains the human-readable address, or NULL)
892  * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on error,
893  *            never #GNUNET_NO
894  */
895 static void
896 transmit_address_to_client (void *cls,
897                             const char *buf,
898                             int res)
899 {
900   struct AddressToStringContext *actx = cls;
901   struct AddressToStringResultMessage *atsm;
902   size_t len;
903   size_t slen;
904
905   GNUNET_assert ( (GNUNET_OK == res) ||
906                   (GNUNET_SYSERR == res) );
907   if (NULL == buf)
908   {
909     len = sizeof (struct AddressToStringResultMessage);
910     atsm = GNUNET_malloc (len);
911     atsm->header.size = ntohs (len);
912     atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
913     if (GNUNET_OK == res)
914     {
915       /* this was the last call, transmit */
916       atsm->res = htonl (GNUNET_OK);
917       atsm->addr_len = htonl (0);
918       GNUNET_SERVER_transmit_context_append_message (actx->tc,
919                                                      (const struct GNUNET_MessageHeader *) atsm);
920       GNUNET_SERVER_transmit_context_run (actx->tc,
921                                           GNUNET_TIME_UNIT_FOREVER_REL);
922       GNUNET_CONTAINER_DLL_remove (a2s_head,
923                                    a2s_tail,
924                                    actx);
925       GNUNET_free (atsm);
926       GNUNET_free (actx);
927       return;
928     }
929     if (GNUNET_SYSERR == res)
930     {
931       /* address conversion failed, but there will be more callbacks */
932       atsm->res = htonl (GNUNET_SYSERR);
933       atsm->addr_len = htonl (0);
934       GNUNET_SERVER_transmit_context_append_message (actx->tc,
935                                                      (const struct GNUNET_MessageHeader *) atsm);
936       GNUNET_free (atsm);
937       return;
938     }
939   }
940   GNUNET_assert (GNUNET_OK == res);
941   /* succesful conversion, append*/
942   slen = strlen (buf) + 1;
943   len = sizeof (struct AddressToStringResultMessage) + slen;
944   atsm = GNUNET_malloc (len);
945   atsm->header.size = ntohs (len);
946   atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
947   atsm->res = htonl (GNUNET_YES);
948   atsm->addr_len = htonl (slen);
949   memcpy (&atsm[1],
950           buf,
951           slen);
952   GNUNET_SERVER_transmit_context_append_message (actx->tc,
953                                                  (const struct GNUNET_MessageHeader *) atsm);
954   GNUNET_free (atsm);
955 }
956
957
958 /**
959  * Client asked to resolve an address.  Process the request.
960  *
961  * @param cls unused
962  * @param client the client
963  * @param message the resolution request
964  */
965 static void
966 clients_handle_address_to_string (void *cls,
967                                   struct GNUNET_SERVER_Client *client,
968                                   const struct GNUNET_MessageHeader *message)
969 {
970   const struct AddressLookupMessage *alum;
971   struct GNUNET_TRANSPORT_PluginFunctions *papi;
972   const char *plugin_name;
973   const char *address;
974   uint32_t address_len;
975   uint16_t size;
976   struct GNUNET_SERVER_TransmitContext *tc;
977   struct AddressToStringContext *actx;
978   struct AddressToStringResultMessage atsm;
979   struct GNUNET_TIME_Relative rtimeout;
980   int32_t numeric;
981
982   size = ntohs (message->size);
983   if (size < sizeof (struct AddressLookupMessage))
984   {
985     GNUNET_break (0);
986     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
987     return;
988   }
989   alum = (const struct AddressLookupMessage *) message;
990   address_len = ntohs (alum->addrlen);
991   if (size <= sizeof (struct AddressLookupMessage) + address_len)
992   {
993     GNUNET_break (0);
994     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
995     return;
996   }
997   address = (const char *) &alum[1];
998   plugin_name = (const char *) &address[address_len];
999   if ('\0' != plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1])
1000   {
1001     GNUNET_break (0);
1002     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1003     return;
1004   }
1005   rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
1006   numeric = ntohs (alum->numeric_only);
1007   tc = GNUNET_SERVER_transmit_context_create (client);
1008   papi = GST_plugins_printer_find (plugin_name);
1009   if (NULL == papi)
1010   {
1011     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1012                 "Failed to find plugin `%s'\n",
1013                 plugin_name);
1014     atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
1015     atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
1016     atsm.res = htonl (GNUNET_SYSERR);
1017     atsm.addr_len = htonl (0);
1018     GNUNET_SERVER_transmit_context_append_message (tc,
1019                                                    &atsm.header);
1020     atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
1021     atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
1022     atsm.res = htonl (GNUNET_OK);
1023     atsm.addr_len = htonl (0);
1024     GNUNET_SERVER_transmit_context_append_message (tc,
1025                                                    &atsm.header);
1026     GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1027     return;
1028   }
1029   actx = GNUNET_new (struct AddressToStringContext);
1030   actx->tc = tc;
1031   GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
1032   GNUNET_SERVER_disable_receive_done_warning (client);
1033   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1034               "Pretty-printing address of %u bytes using plugin `%s'\n",
1035               address_len,
1036               plugin_name);
1037   papi->address_pretty_printer (papi->cls,
1038                                 plugin_name,
1039                                 address, address_len,
1040                                 numeric,
1041                                 rtimeout,
1042                                 &transmit_address_to_client,
1043                                 actx);
1044 }
1045
1046
1047 /**
1048  * Compose #PeerIterateResponseMessage using the given peer and address.
1049  *
1050  * @param peer identity of the peer
1051  * @param address the address, NULL on disconnect
1052  * @return composed message
1053  */
1054 static struct PeerIterateResponseMessage *
1055 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1056                                           const struct GNUNET_HELLO_Address *address)
1057 {
1058   struct PeerIterateResponseMessage *msg;
1059   size_t size;
1060   size_t tlen;
1061   size_t alen;
1062   char *addr;
1063
1064   GNUNET_assert (NULL != peer);
1065   if (NULL != address)
1066   {
1067     tlen = strlen (address->transport_name) + 1;
1068     alen = address->address_length;
1069   }
1070   else
1071   {
1072     tlen = 0;
1073     alen = 0;
1074   }
1075   size = (sizeof (struct PeerIterateResponseMessage) + alen + tlen);
1076   msg = GNUNET_malloc (size);
1077   msg->header.size = htons (size);
1078   msg->header.type =
1079       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1080   msg->reserved = htonl (0);
1081   msg->peer = *peer;
1082   msg->addrlen = htonl (alen);
1083   msg->pluginlen = htonl (tlen);
1084
1085   if (NULL != address)
1086   {
1087     msg->local_address_info = htonl((uint32_t) address->local_info);
1088     addr = (char *) &msg[1];
1089     memcpy (addr, address->address, alen);
1090     memcpy (&addr[alen], address->transport_name, tlen);
1091   }
1092   return msg;
1093 }
1094
1095
1096 /**
1097  * Compose #PeerIterateResponseMessage using the given peer and address.
1098  *
1099  * @param peer identity of the peer
1100  * @param address the address, NULL on disconnect
1101  * @return composed message
1102  */
1103 static struct ValidationIterateResponseMessage *
1104 compose_validation_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1105                                              const struct GNUNET_HELLO_Address *address)
1106 {
1107   struct ValidationIterateResponseMessage *msg;
1108   size_t size;
1109   size_t tlen;
1110   size_t alen;
1111   char *addr;
1112
1113   GNUNET_assert (NULL != peer);
1114   if (NULL != address)
1115   {
1116     tlen = strlen (address->transport_name) + 1;
1117     alen = address->address_length;
1118   }
1119   else
1120   {
1121     tlen = 0;
1122     alen = 0;
1123   }
1124   size = (sizeof (struct ValidationIterateResponseMessage) + alen + tlen);
1125   msg = GNUNET_malloc (size);
1126   msg->header.size = htons (size);
1127   msg->header.type =
1128       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1129   msg->reserved = htonl (0);
1130   msg->peer = *peer;
1131   msg->addrlen = htonl (alen);
1132   msg->pluginlen = htonl (tlen);
1133
1134   if (NULL != address)
1135   {
1136     msg->local_address_info = htonl((uint32_t) address->local_info);
1137     addr = (char *) &msg[1];
1138     memcpy (addr, address->address, alen);
1139     memcpy (&addr[alen], address->transport_name, tlen);
1140   }
1141   return msg;
1142 }
1143
1144
1145 /**
1146  * Context for #send_validation_information() and
1147  * #send_peer_information().
1148  */
1149 struct IterationContext
1150 {
1151   /**
1152    * Context to use for the transmission.
1153    */
1154   struct GNUNET_SERVER_TransmitContext *tc;
1155
1156   /**
1157    * Which peers do we care about?
1158    */
1159   struct GNUNET_PeerIdentity id;
1160
1161   /**
1162    * #GNUNET_YES if @e id should be ignored because we want all peers.
1163    */
1164   int all;
1165 };
1166
1167
1168 /**
1169  * Output information of validation entries to the given client.
1170  *
1171  * @param cls the `struct IterationContext *`
1172  * @param address the address
1173  * @param last_validation point in time when last validation was performed
1174  * @param valid_until point in time how long address is valid
1175  * @param next_validation point in time when next validation will be performed
1176  * @param state state of validation notification
1177  */
1178 static void
1179 send_validation_information (void *cls,
1180                              const struct GNUNET_HELLO_Address *address,
1181                              struct GNUNET_TIME_Absolute last_validation,
1182                              struct GNUNET_TIME_Absolute valid_until,
1183                              struct GNUNET_TIME_Absolute next_validation,
1184                              enum GNUNET_TRANSPORT_ValidationState state)
1185 {
1186   struct IterationContext *pc = cls;
1187   struct ValidationIterateResponseMessage *msg;
1188
1189   if ( (GNUNET_YES != pc->all) &&
1190        (0 != memcmp (&address->peer, &pc->id, sizeof (pc->id))) )
1191     return;
1192   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193               "Sending information about for validation entry for peer `%s' using address `%s'\n",
1194               GNUNET_i2s (&address->peer),
1195               (NULL != address) ? GST_plugins_a2s (address) : "<none>");
1196   msg = compose_validation_iterate_response_message (&address->peer, address);
1197   msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1198   msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1199   msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1200   msg->state = htonl ((uint32_t) state);
1201   GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1202   GNUNET_free (msg);
1203 }
1204
1205
1206 /**
1207  * Output information of neighbours to the given client.
1208  *
1209  * @param cls the `struct PeerIterationContext *`
1210  * @param peer identity of the neighbour
1211  * @param address the address
1212  * @param state current state this peer is in
1213  * @param state_timeout timeout for the current state of the peer
1214  * @param bandwidth_in inbound quota in NBO
1215  * @param bandwidth_out outbound quota in NBO
1216  */
1217 static void
1218 send_peer_information (void *cls,
1219                        const struct GNUNET_PeerIdentity *peer,
1220                        const struct GNUNET_HELLO_Address *address,
1221                        enum GNUNET_TRANSPORT_PeerState state,
1222                        struct GNUNET_TIME_Absolute state_timeout,
1223                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1224                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1225 {
1226   struct IterationContext *pc = cls;
1227   struct PeerIterateResponseMessage *msg;
1228
1229   if ( (GNUNET_YES != pc->all) &&
1230        (0 != memcmp (peer, &pc->id, sizeof (pc->id))) )
1231     return;
1232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233               "Sending information about `%s' using address `%s' in state `%s'\n",
1234               GNUNET_i2s(peer),
1235               (NULL != address) ? GST_plugins_a2s (address) : "<none>",
1236               GNUNET_TRANSPORT_ps2s (state));
1237   msg = compose_address_iterate_response_message (peer, address);
1238   msg->state = htonl (state);
1239   msg->state_timeout = GNUNET_TIME_absolute_hton(state_timeout);
1240   GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1241   GNUNET_free (msg);
1242 }
1243
1244
1245 /**
1246  * Client asked to obtain information about a specific or all peers
1247  * Process the request.
1248  *
1249  * @param cls unused
1250  * @param client the client
1251  * @param message the peer address information request
1252  */
1253 static void
1254 clients_handle_monitor_peers (void *cls,
1255                               struct GNUNET_SERVER_Client *client,
1256                               const struct GNUNET_MessageHeader *message)
1257 {
1258   struct GNUNET_SERVER_TransmitContext *tc;
1259   const struct PeerMonitorMessage *msg;
1260   struct IterationContext pc;
1261
1262   msg = (const struct PeerMonitorMessage *) message;
1263   if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1264        (NULL != lookup_monitoring_client (peer_monitoring_clients_head,
1265                                           client)) )
1266   {
1267     GNUNET_break (0);
1268     GNUNET_SERVER_receive_done (client,
1269                                 GNUNET_SYSERR);
1270     return;
1271   }
1272   GNUNET_SERVER_disable_receive_done_warning (client);
1273   GNUNET_SERVER_client_mark_monitor (client);
1274   pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1275
1276   /* Send initial list */
1277   if (0 == memcmp (&msg->peer,
1278                    &all_zeros,
1279                    sizeof (struct GNUNET_PeerIdentity)))
1280   {
1281     /* iterate over all neighbours */
1282     pc.all = GNUNET_YES;
1283     pc.id = msg->peer;
1284   }
1285   else
1286   {
1287     /* just return one neighbour */
1288     pc.all = GNUNET_NO;
1289     pc.id = msg->peer;
1290   }
1291   GST_neighbours_iterate (&send_peer_information,
1292                           &pc);
1293
1294   if (GNUNET_YES != ntohl (msg->one_shot))
1295   {
1296     setup_peer_monitoring_client (client,
1297                                   &msg->peer);
1298   }
1299   else
1300   {
1301     GNUNET_SERVER_transmit_context_append_data (tc,
1302                                                 NULL,
1303                                                 0,
1304                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1305   }
1306   GNUNET_SERVER_transmit_context_run (tc,
1307                                       GNUNET_TIME_UNIT_FOREVER_REL);
1308 }
1309
1310
1311 /**
1312  * Client asked to obtain information about a specific or all validation
1313  * processes
1314  *
1315  * @param cls unused
1316  * @param client the client
1317  * @param message the peer address information request
1318  */
1319 static void
1320 clients_handle_monitor_validation (void *cls,
1321                                    struct GNUNET_SERVER_Client *client,
1322                                    const struct GNUNET_MessageHeader *message)
1323 {
1324   struct GNUNET_SERVER_TransmitContext *tc;
1325   struct PeerMonitorMessage *msg;
1326   struct IterationContext pc;
1327
1328   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST)
1329   {
1330     GNUNET_break (0);
1331     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1332     return;
1333   }
1334   if (ntohs (message->size) != sizeof (struct ValidationMonitorMessage))
1335   {
1336     GNUNET_break (0);
1337     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1338     return;
1339   }
1340   msg = (struct PeerMonitorMessage *) message;
1341   if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1342        (NULL != lookup_monitoring_client (val_monitoring_clients_head, client)) )
1343   {
1344     GNUNET_break (0);
1345     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1346     return;
1347   }
1348   GNUNET_SERVER_disable_receive_done_warning (client);
1349   GNUNET_SERVER_client_mark_monitor (client);
1350   pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1351
1352   /* Send initial list */
1353   if (0 == memcmp (&msg->peer,
1354                    &all_zeros,
1355                    sizeof (struct GNUNET_PeerIdentity)))
1356   {
1357     /* iterate over all neighbours */
1358     pc.all = GNUNET_YES;
1359     pc.id = msg->peer;
1360   }
1361   else
1362   {
1363     /* just return one neighbour */
1364     pc.all = GNUNET_NO;
1365     pc.id = msg->peer;
1366   }
1367   GST_validation_iterate (&send_validation_information,
1368                           &pc);
1369
1370   if (GNUNET_YES != ntohl (msg->one_shot))
1371   {
1372     setup_val_monitoring_client (client, &msg->peer);
1373   }
1374   else
1375   {
1376     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1377                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1378   }
1379   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1380 }
1381
1382
1383 /**
1384  * Function called by the plugin with information about the
1385  * current sessions managed by the plugin (for monitoring).
1386  *
1387  * @param cls closure
1388  * @param session session handle this information is about,
1389  *        NULL to indicate that we are "in sync" (initial
1390  *        iteration complete)
1391  * @param info information about the state of the session,
1392  *        NULL if @a session is also NULL and we are
1393  *        merely signalling that the initial iteration is over
1394  */
1395 static void
1396 plugin_session_info_cb (void *cls,
1397                         struct GNUNET_ATS_Session *session,
1398                         const struct GNUNET_TRANSPORT_SessionInfo *info)
1399 {
1400   struct TransportPluginMonitorMessage *msg;
1401   struct GNUNET_MessageHeader sync;
1402   size_t size;
1403   size_t slen;
1404   uint16_t alen;
1405   char *name;
1406   char *addr;
1407
1408   if (0 == GNUNET_SERVER_notification_context_get_size (plugin_nc))
1409   {
1410     GST_plugins_monitor_subscribe (NULL,
1411                                    NULL);
1412     return;
1413   }
1414   if ( (NULL == info) &&
1415        (NULL == session) )
1416   {
1417     /* end of initial iteration */
1418     if (NULL != sync_client)
1419     {
1420       sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1421       sync.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC);
1422       GNUNET_SERVER_notification_context_unicast (plugin_nc,
1423                                                   sync_client,
1424                                                   &sync,
1425                                                   GNUNET_NO);
1426       sync_client = NULL;
1427     }
1428     return;
1429   }
1430   GNUNET_assert (NULL != info);
1431   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432               "Plugin event for peer %s on transport %s\n",
1433               GNUNET_i2s (&info->address->peer),
1434               info->address->transport_name);
1435   slen = strlen (info->address->transport_name) + 1;
1436   alen = info->address->address_length;
1437   size = sizeof (struct TransportPluginMonitorMessage) + slen + alen;
1438   if (size > UINT16_MAX)
1439   {
1440     GNUNET_break (0);
1441     return;
1442   }
1443   msg = GNUNET_malloc (size);
1444   msg->header.size = htons (size);
1445   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT);
1446   msg->session_state = htons ((uint16_t) info->state);
1447   msg->is_inbound = htons ((int16_t) info->is_inbound);
1448   msg->msgs_pending = htonl (info->num_msg_pending);
1449   msg->bytes_pending = htonl (info->num_bytes_pending);
1450   msg->timeout = GNUNET_TIME_absolute_hton (info->session_timeout);
1451   msg->delay = GNUNET_TIME_absolute_hton (info->receive_delay);
1452   msg->peer = info->address->peer;
1453   msg->session_id = (uint64_t) (intptr_t) session;
1454   msg->plugin_name_len = htons (slen);
1455   msg->plugin_address_len = htons (alen);
1456   name = (char *) &msg[1];
1457   memcpy (name,
1458           info->address->transport_name,
1459           slen);
1460   addr = &name[slen];
1461   memcpy (addr,
1462           info->address->address,
1463           alen);
1464   if (NULL != sync_client)
1465     GNUNET_SERVER_notification_context_unicast (plugin_nc,
1466                                                 sync_client,
1467                                                 &msg->header,
1468                                                 GNUNET_NO);
1469   else
1470     GNUNET_SERVER_notification_context_broadcast (plugin_nc,
1471                                                   &msg->header,
1472                                                   GNUNET_NO);
1473   GNUNET_free (msg);
1474 }
1475
1476
1477 /**
1478  * Client asked to obtain information about all plugin connections.
1479  *
1480  * @param cls unused
1481  * @param client the client
1482  * @param message the peer address information request
1483  */
1484 static void
1485 clients_handle_monitor_plugins (void *cls,
1486                                 struct GNUNET_SERVER_Client *client,
1487                                 const struct GNUNET_MessageHeader *message)
1488 {
1489   GNUNET_SERVER_client_mark_monitor (client);
1490   GNUNET_SERVER_disable_receive_done_warning (client);
1491   GNUNET_SERVER_notification_context_add (plugin_nc,
1492                                           client);
1493   GNUNET_assert (NULL == sync_client);
1494   sync_client = client;
1495   GST_plugins_monitor_subscribe (&plugin_session_info_cb,
1496                                  NULL);
1497 }
1498
1499
1500 /**
1501  * Start handling requests from clients.
1502  *
1503  * @param server server used to accept clients from.
1504  */
1505 void
1506 GST_clients_start (struct GNUNET_SERVER_Handle *server)
1507 {
1508   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1509     {&clients_handle_start, NULL,
1510      GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
1511     {&clients_handle_hello, NULL,
1512      GNUNET_MESSAGE_TYPE_HELLO, 0},
1513     {&clients_handle_send, NULL,
1514      GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
1515     {&clients_handle_request_connect, NULL,
1516      GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
1517      sizeof (struct TransportRequestConnectMessage)},
1518     {&clients_handle_address_to_string, NULL,
1519      GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
1520     {&clients_handle_monitor_peers, NULL,
1521      GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST,
1522      sizeof (struct PeerMonitorMessage)},
1523     {&clients_handle_monitor_validation, NULL,
1524      GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST,
1525      sizeof (struct ValidationMonitorMessage)},
1526     {&GST_blacklist_handle_init, NULL,
1527      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
1528      sizeof (struct GNUNET_MessageHeader)},
1529     {&GST_blacklist_handle_reply, NULL,
1530      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
1531      sizeof (struct BlacklistMessage)},
1532     {&GST_manipulation_set_metric, NULL,
1533      GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC,
1534      sizeof (struct TrafficMetricMessage) },
1535     {&clients_handle_monitor_plugins, NULL,
1536      GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_START,
1537      sizeof (struct GNUNET_MessageHeader) },
1538     {NULL, NULL, 0, 0}
1539   };
1540   peer_nc = GNUNET_SERVER_notification_context_create (server, 0);
1541   val_nc = GNUNET_SERVER_notification_context_create (server, 0);
1542   plugin_nc = GNUNET_SERVER_notification_context_create (server, 0);
1543   GNUNET_SERVER_add_handlers (server, handlers);
1544   GNUNET_SERVER_disconnect_notify (server,
1545                                    &client_disconnect_notification,
1546                                    NULL);
1547 }
1548
1549
1550 /**
1551  * Stop processing clients.
1552  */
1553 void
1554 GST_clients_stop ()
1555 {
1556   struct AddressToStringContext *cur;
1557
1558   while (NULL != (cur = a2s_head))
1559   {
1560     GNUNET_SERVER_transmit_context_destroy (cur->tc, GNUNET_NO);
1561     GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
1562     GNUNET_free (cur);
1563   }
1564   if (NULL != peer_nc)
1565   {
1566     GNUNET_SERVER_notification_context_destroy (peer_nc);
1567     peer_nc = NULL;
1568   }
1569   if (NULL != val_nc)
1570   {
1571     GNUNET_SERVER_notification_context_destroy (val_nc);
1572     val_nc = NULL;
1573   }
1574   if (NULL != plugin_nc)
1575   {
1576     GNUNET_SERVER_notification_context_destroy (plugin_nc);
1577     plugin_nc = NULL;
1578   }
1579 }
1580
1581
1582 /**
1583  * Broadcast the given message to all of our clients.
1584  *
1585  * @param msg message to broadcast
1586  * @param may_drop #GNUNET_YES if the message can be dropped / is payload
1587  */
1588 void
1589 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg,
1590                        int may_drop)
1591 {
1592   struct TransportClient *tc;
1593
1594   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1595               "Asked to broadcast message of type %u with %u bytes\n",
1596               (unsigned int) ntohs (msg->type),
1597               (unsigned int) ntohs (msg->size));
1598   for (tc = clients_head; NULL != tc; tc = tc->next)
1599   {
1600     if ( (GNUNET_YES == may_drop) &&
1601          (GNUNET_YES != tc->send_payload) )
1602       continue; /* skip, this client does not care about payload */
1603     unicast (tc, msg, may_drop);
1604   }
1605 }
1606
1607
1608 /**
1609  * Send the given message to a particular client
1610  *
1611  * @param client target of the message
1612  * @param msg message to transmit
1613  * @param may_drop #GNUNET_YES if the message can be dropped
1614  */
1615 void
1616 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1617                      const struct GNUNET_MessageHeader *msg,
1618                      int may_drop)
1619 {
1620   struct TransportClient *tc;
1621
1622   tc = lookup_client (client);
1623   if (NULL == tc)
1624     return;                     /* client got disconnected in the meantime, drop message */
1625   unicast (tc, msg, may_drop);
1626 }
1627
1628
1629 /**
1630  * Broadcast the new active address to all clients monitoring the peer.
1631  *
1632  * @param peer peer this update is about (never NULL)
1633  * @param address address, NULL on disconnect
1634  * @param state the current state of the peer
1635  * @param state_timeout the time out for the state
1636  */
1637 void
1638 GST_clients_broadcast_peer_notification (const struct GNUNET_PeerIdentity *peer,
1639                                          const struct GNUNET_HELLO_Address *address,
1640                                          enum GNUNET_TRANSPORT_PeerState state,
1641                                          struct GNUNET_TIME_Absolute state_timeout)
1642 {
1643   struct PeerIterateResponseMessage *msg;
1644   struct MonitoringClient *mc;
1645
1646   msg = compose_address_iterate_response_message (peer, address);
1647   msg->state = htonl (state);
1648   msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
1649   for (mc = peer_monitoring_clients_head; NULL != mc; mc = mc->next)
1650     if ((0 == memcmp (&mc->peer, &all_zeros,
1651                       sizeof (struct GNUNET_PeerIdentity))) ||
1652         (0 == memcmp (&mc->peer, peer,
1653                       sizeof (struct GNUNET_PeerIdentity))))
1654       GNUNET_SERVER_notification_context_unicast (peer_nc,
1655                                                   mc->client,
1656                                                   &msg->header,
1657                                                   GNUNET_NO);
1658   GNUNET_free (msg);
1659 }
1660
1661
1662 /**
1663  * Broadcast the new validation changes to all clients monitoring the peer.
1664  *
1665  * @param peer peer this update is about (never NULL)
1666  * @param address address, NULL on disconnect
1667  * @param last_validation point in time when last validation was performed
1668  * @param valid_until point in time how long address is valid
1669  * @param next_validation point in time when next validation will be performed
1670  * @param state state of validation notification
1671  */
1672 void
1673 GST_clients_broadcast_validation_notification (const struct GNUNET_PeerIdentity *peer,
1674                                                const struct GNUNET_HELLO_Address *address,
1675                                                struct GNUNET_TIME_Absolute last_validation,
1676                                                struct GNUNET_TIME_Absolute valid_until,
1677                                                struct GNUNET_TIME_Absolute next_validation,
1678                                                enum GNUNET_TRANSPORT_ValidationState state)
1679 {
1680   struct ValidationIterateResponseMessage *msg;
1681   struct MonitoringClient *mc;
1682
1683   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684               "Sending information about for validation entry for peer `%s' using address `%s'\n",
1685               GNUNET_i2s(peer),
1686               (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1687   msg = compose_validation_iterate_response_message (peer, address);
1688   msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1689   msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1690   msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1691   msg->state = htonl ((uint32_t) state);
1692   for (mc = val_monitoring_clients_head; NULL != mc; mc = mc->next)
1693     if ((0 == memcmp (&mc->peer, &all_zeros,
1694                       sizeof (struct GNUNET_PeerIdentity))) ||
1695         (0 == memcmp (&mc->peer, peer,
1696                       sizeof (struct GNUNET_PeerIdentity))))
1697       GNUNET_SERVER_notification_context_unicast (val_nc,
1698                                                   mc->client,
1699                                                   &msg->header,
1700                                                   GNUNET_NO);
1701   GNUNET_free (msg);
1702 }
1703
1704
1705 /* end of file gnunet-service-transport_clients.c */