more dead and duplicate code elimination
[oweals/gnunet.git] / src / transport / gnunet-service-transport_clients.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2014 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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   struct TransportClient *tc;
617   uint32_t options;
618
619   tc = lookup_client (client);
620   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621               "Client %p sent START\n", tc);
622   if (NULL != tc)
623   {
624     /* got 'start' twice from the same client, not allowed */
625     GNUNET_break (0);
626     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
627     return;
628   }
629   start = (const struct StartMessage *) message;
630   options = ntohl (start->options);
631   if ((0 != (1 & options)) &&
632       (0 !=
633        memcmp (&start->self, &GST_my_identity,
634                sizeof (struct GNUNET_PeerIdentity))))
635   {
636     /* client thinks this is a different peer, reject */
637     GNUNET_break (0);
638     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
639     return;
640   }
641   tc = setup_client (client);
642   tc->send_payload = (0 != (2 & options));
643   unicast (tc,
644            GST_hello_get (),
645            GNUNET_NO);
646   GST_neighbours_iterate (&notify_client_about_neighbour, tc);
647   GNUNET_SERVER_receive_done (client, GNUNET_OK);
648 }
649
650
651 /**
652  * Client sent us a HELLO.  Process the request.
653  *
654  * @param cls unused
655  * @param client the client
656  * @param message the HELLO message
657  */
658 static void
659 clients_handle_hello (void *cls,
660                       struct GNUNET_SERVER_Client *client,
661                       const struct GNUNET_MessageHeader *message)
662 {
663   GST_validation_handle_hello (message);
664   GNUNET_SERVER_receive_done (client, GNUNET_OK);
665 }
666
667
668 /**
669  * Closure for #handle_send_transmit_continuation()
670  */
671 struct SendTransmitContinuationContext
672 {
673   /**
674    * Client that made the request.
675    */
676   struct GNUNET_SERVER_Client *client;
677
678   /**
679    * Peer that was the target.
680    */
681   struct GNUNET_PeerIdentity target;
682 };
683
684
685 /**
686  * Function called after the transmission is done.  Notify the client that it is
687  * OK to send the next message.
688  *
689  * @param cls closure
690  * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
691  * @param bytes_payload bytes payload sent
692  * @param bytes_on_wire bytes sent on wire
693  */
694 static void
695 handle_send_transmit_continuation (void *cls,
696                                    int success,
697                                    size_t bytes_payload,
698                                    size_t bytes_on_wire)
699 {
700   struct SendTransmitContinuationContext *stcc = cls;
701   struct SendOkMessage send_ok_msg;
702
703   send_ok_msg.header.size = htons (sizeof (send_ok_msg));
704   send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
705   send_ok_msg.bytes_msg = htonl (bytes_payload);
706   send_ok_msg.bytes_physical = htonl (bytes_on_wire);
707   send_ok_msg.success = htonl (success);
708   send_ok_msg.peer = stcc->target;
709   GST_clients_unicast (stcc->client,
710                        &send_ok_msg.header,
711                        GNUNET_NO);
712   GNUNET_SERVER_client_drop (stcc->client);
713   GNUNET_free (stcc);
714 }
715
716
717 /**
718  * Client asked for transmission to a peer.  Process the request.
719  *
720  * @param cls unused
721  * @param client the client
722  * @param message the send message that was sent
723  */
724 static void
725 clients_handle_send (void *cls,
726                      struct GNUNET_SERVER_Client *client,
727                      const struct GNUNET_MessageHeader *message)
728 {
729   const struct OutboundMessage *obm;
730   const struct GNUNET_MessageHeader *obmm;
731   struct SendTransmitContinuationContext *stcc;
732   uint16_t size;
733   uint16_t msize;
734   struct TransportClient *tc;
735
736   tc = lookup_client (client);
737   if (NULL == tc)
738   {
739     /* client asked for transmission before 'START' */
740     GNUNET_break (0);
741     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
742     return;
743   }
744
745   size = ntohs (message->size);
746   if (size <
747       sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
748   {
749     GNUNET_break (0);
750     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
751     return;
752   }
753   obm = (const struct OutboundMessage *) message;
754   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
755   msize = size - sizeof (struct OutboundMessage);
756   if (msize < sizeof (struct GNUNET_MessageHeader))
757   {
758     GNUNET_break (0);
759     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
760     return;
761   }
762
763   if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
764   {
765     /* not connected, not allowed to send; can happen due to asynchronous operations */
766     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767                 "Could not send message to peer `%s': not connected\n",
768                 GNUNET_i2s (&obm->peer));
769     GNUNET_STATISTICS_update (GST_stats,
770                               gettext_noop
771                               ("# bytes payload dropped (other peer was not connected)"),
772                               msize, GNUNET_NO);
773     GNUNET_SERVER_receive_done (client, GNUNET_OK);
774     return;
775   }
776   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777               "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
778               "SEND",
779               GNUNET_i2s (&obm->peer),
780               ntohs (obmm->type),
781               msize);
782   GNUNET_SERVER_receive_done (client, GNUNET_OK);
783   stcc = GNUNET_new (struct SendTransmitContinuationContext);
784   stcc->target = obm->peer;
785   stcc->client = client;
786   GNUNET_SERVER_client_keep (client);
787   GST_manipulation_send (&obm->peer, obmm, msize,
788                          GNUNET_TIME_relative_ntoh (obm->timeout),
789                          &handle_send_transmit_continuation, stcc);
790 }
791
792
793 /**
794  * Try to initiate a connection to the given peer if the blacklist
795  * allowed it.
796  *
797  * @param cls closure (unused, NULL)
798  * @param peer identity of peer that was tested
799  * @param result #GNUNET_OK if the connection is allowed,
800  *               #GNUNET_NO if not
801  */
802 static void
803 try_connect_if_allowed (void *cls,
804                         const struct GNUNET_PeerIdentity *peer,
805                         int result)
806 {
807   if (GNUNET_OK != result)
808   {
809     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
810                 _("Blacklist refuses connection attempt to peer `%s'\n"),
811                 GNUNET_i2s (peer));
812     return;                     /* not allowed */
813   }
814   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815               "Blacklist allows connection attempt to peer `%s'\n",
816               GNUNET_i2s (peer));
817
818   GST_neighbours_try_connect (peer);
819 }
820
821
822 /**
823  * Handle request connect message
824  *
825  * @param cls closure (always NULL)
826  * @param client identification of the client
827  * @param message the actual message
828  */
829 static void
830 clients_handle_request_connect (void *cls,
831                                 struct GNUNET_SERVER_Client *client,
832                                 const struct GNUNET_MessageHeader *message)
833 {
834   const struct TransportRequestConnectMessage *trcm;
835
836   trcm = (const struct TransportRequestConnectMessage *) message;
837   GNUNET_break (0 == ntohl (trcm->reserved));
838   GNUNET_STATISTICS_update (GST_stats,
839                             gettext_noop
840                             ("# REQUEST CONNECT messages received"), 1,
841                             GNUNET_NO);
842   if (0 == memcmp (&trcm->peer,
843                    &GST_my_identity,
844                    sizeof (struct GNUNET_PeerIdentity)))
845   {
846     GNUNET_break (0);
847     GNUNET_SERVER_receive_done (client, GNUNET_OK);
848     return;
849   }
850   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851               "Received a request connect message for peer `%s'\n",
852               GNUNET_i2s (&trcm->peer));
853   (void) GST_blacklist_test_allowed (&trcm->peer,
854                                      NULL,
855                                      &try_connect_if_allowed,
856                                      NULL);
857   GNUNET_SERVER_receive_done (client, GNUNET_OK);
858 }
859
860
861 /**
862  * Handle request disconnect message
863  *
864  * @param cls closure (always NULL)
865  * @param client identification of the client
866  * @param message the actual message
867  */
868 static void
869 clients_handle_request_disconnect (void *cls,
870                                    struct GNUNET_SERVER_Client *client,
871                                    const struct GNUNET_MessageHeader *message)
872 {
873   const struct TransportRequestDisconnectMessage *trdm;
874
875   trdm = (const struct TransportRequestDisconnectMessage *) message;
876   GNUNET_break (0 == ntohl (trdm->reserved));
877   GNUNET_STATISTICS_update (GST_stats,
878                             gettext_noop
879                             ("# REQUEST DISCONNECT messages received"), 1,
880                             GNUNET_NO);
881   if (0 == memcmp (&trdm->peer,
882                    &GST_my_identity,
883                    sizeof (struct GNUNET_PeerIdentity)))
884   {
885     GNUNET_break (0);
886     GNUNET_SERVER_receive_done (client, GNUNET_OK);
887     return;
888   }
889   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890               "Received a request disconnect message for peer `%s'\n",
891               GNUNET_i2s (&trdm->peer));
892   (void) GST_neighbours_force_disconnect (&trdm->peer);
893   GNUNET_SERVER_receive_done (client, GNUNET_OK);
894 }
895
896
897 /**
898  * Take the given address and append it to the set of results sent back to
899  * the client.  This function may be called serveral times for a single
900  * conversion.   The last invocation will be with a @a address of
901  * NULL and a @a res of #GNUNET_OK.  Thus, to indicate conversion
902  * errors, the callback might be called first with @a address NULL and
903  * @a res being #GNUNET_SYSERR.  In that case, there will still be a
904  * subsequent call later with @a address NULL and @a res #GNUNET_OK.
905  *
906  * @param cls the transmission context used (`struct GNUNET_SERVER_TransmitContext *`)
907  * @param buf text to transmit (contains the human-readable address, or NULL)
908  * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on error,
909  *            never #GNUNET_NO
910  */
911 static void
912 transmit_address_to_client (void *cls,
913                             const char *buf,
914                             int res)
915 {
916   struct AddressToStringContext *actx = cls;
917   struct AddressToStringResultMessage *atsm;
918   size_t len;
919   size_t slen;
920
921   GNUNET_assert ( (GNUNET_OK == res) ||
922                   (GNUNET_SYSERR == res) );
923   if (NULL == buf)
924   {
925     len = sizeof (struct AddressToStringResultMessage);
926     atsm = GNUNET_malloc (len);
927     atsm->header.size = ntohs (len);
928     atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
929     if (GNUNET_OK == res)
930     {
931       /* this was the last call, transmit */
932       atsm->res = htonl (GNUNET_OK);
933       atsm->addr_len = htonl (0);
934       GNUNET_SERVER_transmit_context_append_message (actx->tc,
935                                                      (const struct GNUNET_MessageHeader *) atsm);
936       GNUNET_SERVER_transmit_context_run (actx->tc,
937                                           GNUNET_TIME_UNIT_FOREVER_REL);
938       GNUNET_CONTAINER_DLL_remove (a2s_head,
939                                    a2s_tail,
940                                    actx);
941       GNUNET_free (atsm);
942       GNUNET_free (actx);
943       return;
944     }
945     if (GNUNET_SYSERR == res)
946     {
947       /* address conversion failed, but there will be more callbacks */
948       atsm->res = htonl (GNUNET_SYSERR);
949       atsm->addr_len = htonl (0);
950       GNUNET_SERVER_transmit_context_append_message (actx->tc,
951                                                      (const struct GNUNET_MessageHeader *) atsm);
952       GNUNET_free (atsm);
953       return;
954     }
955   }
956   GNUNET_assert (GNUNET_OK == res);
957   /* succesful conversion, append*/
958   slen = strlen (buf) + 1;
959   len = sizeof (struct AddressToStringResultMessage) + slen;
960   atsm = GNUNET_malloc (len);
961   atsm->header.size = ntohs (len);
962   atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
963   atsm->res = htonl (GNUNET_YES);
964   atsm->addr_len = htonl (slen);
965   memcpy (&atsm[1],
966           buf,
967           slen);
968   GNUNET_SERVER_transmit_context_append_message (actx->tc,
969                                                  (const struct GNUNET_MessageHeader *) atsm);
970   GNUNET_free (atsm);
971 }
972
973
974 /**
975  * Client asked to resolve an address.  Process the request.
976  *
977  * @param cls unused
978  * @param client the client
979  * @param message the resolution request
980  */
981 static void
982 clients_handle_address_to_string (void *cls,
983                                   struct GNUNET_SERVER_Client *client,
984                                   const struct GNUNET_MessageHeader *message)
985 {
986   const struct AddressLookupMessage *alum;
987   struct GNUNET_TRANSPORT_PluginFunctions *papi;
988   const char *plugin_name;
989   const char *address;
990   uint32_t address_len;
991   uint16_t size;
992   struct GNUNET_SERVER_TransmitContext *tc;
993   struct AddressToStringContext *actx;
994   struct AddressToStringResultMessage atsm;
995   struct GNUNET_TIME_Relative rtimeout;
996   int32_t numeric;
997
998   size = ntohs (message->size);
999   if (size < sizeof (struct AddressLookupMessage))
1000   {
1001     GNUNET_break (0);
1002     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1003     return;
1004   }
1005   alum = (const struct AddressLookupMessage *) message;
1006   address_len = ntohs (alum->addrlen);
1007   if (size <= sizeof (struct AddressLookupMessage) + address_len)
1008   {
1009     GNUNET_break (0);
1010     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1011     return;
1012   }
1013   address = (const char *) &alum[1];
1014   plugin_name = (const char *) &address[address_len];
1015   if ('\0' != plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1])
1016   {
1017     GNUNET_break (0);
1018     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1019     return;
1020   }
1021   rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
1022   numeric = ntohs (alum->numeric_only);
1023   tc = GNUNET_SERVER_transmit_context_create (client);
1024   papi = GST_plugins_printer_find (plugin_name);
1025   if (NULL == papi)
1026   {
1027     atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
1028     atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
1029     atsm.res = htonl (GNUNET_SYSERR);
1030     atsm.addr_len = htonl (0);
1031     GNUNET_SERVER_transmit_context_append_message (tc,
1032                                                    &atsm.header);
1033     atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
1034     atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
1035     atsm.res = htonl (GNUNET_OK);
1036     atsm.addr_len = htonl (0);
1037     GNUNET_SERVER_transmit_context_append_message (tc,
1038                                                    &atsm.header);
1039     GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1040     return;
1041   }
1042   actx = GNUNET_new (struct AddressToStringContext);
1043   actx->tc = tc;
1044   GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
1045   GNUNET_SERVER_disable_receive_done_warning (client);
1046   papi->address_pretty_printer (papi->cls,
1047                                 plugin_name,
1048                                 address, address_len,
1049                                 numeric,
1050                                 rtimeout,
1051                                 &transmit_address_to_client,
1052                                 actx);
1053 }
1054
1055
1056 /**
1057  * Compose #PeerIterateResponseMessage using the given peer and address.
1058  *
1059  * @param peer identity of the peer
1060  * @param address the address, NULL on disconnect
1061  * @return composed message
1062  */
1063 static struct PeerIterateResponseMessage *
1064 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1065                                           const struct GNUNET_HELLO_Address *address)
1066 {
1067   struct PeerIterateResponseMessage *msg;
1068   size_t size;
1069   size_t tlen;
1070   size_t alen;
1071   char *addr;
1072
1073   GNUNET_assert (NULL != peer);
1074   if (NULL != address)
1075   {
1076     tlen = strlen (address->transport_name) + 1;
1077     alen = address->address_length;
1078   }
1079   else
1080   {
1081     tlen = 0;
1082     alen = 0;
1083   }
1084   size = (sizeof (struct PeerIterateResponseMessage) + alen + tlen);
1085   msg = GNUNET_malloc (size);
1086   msg->header.size = htons (size);
1087   msg->header.type =
1088       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1089   msg->reserved = htonl (0);
1090   msg->peer = *peer;
1091   msg->addrlen = htonl (alen);
1092   msg->pluginlen = htonl (tlen);
1093
1094   if (NULL != address)
1095   {
1096     msg->local_address_info = htonl((uint32_t) address->local_info);
1097     addr = (char *) &msg[1];
1098     memcpy (addr, address->address, alen);
1099     memcpy (&addr[alen], address->transport_name, tlen);
1100   }
1101   return msg;
1102 }
1103
1104
1105 /**
1106  * Compose #PeerIterateResponseMessage using the given peer and address.
1107  *
1108  * @param peer identity of the peer
1109  * @param address the address, NULL on disconnect
1110  * @return composed message
1111  */
1112 static struct ValidationIterateResponseMessage *
1113 compose_validation_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1114                                              const struct GNUNET_HELLO_Address *address)
1115 {
1116   struct ValidationIterateResponseMessage *msg;
1117   size_t size;
1118   size_t tlen;
1119   size_t alen;
1120   char *addr;
1121
1122   GNUNET_assert (NULL != peer);
1123   if (NULL != address)
1124   {
1125     tlen = strlen (address->transport_name) + 1;
1126     alen = address->address_length;
1127   }
1128   else
1129   {
1130     tlen = 0;
1131     alen = 0;
1132   }
1133   size = (sizeof (struct ValidationIterateResponseMessage) + alen + tlen);
1134   msg = GNUNET_malloc (size);
1135   msg->header.size = htons (size);
1136   msg->header.type =
1137       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1138   msg->reserved = htonl (0);
1139   msg->peer = *peer;
1140   msg->addrlen = htonl (alen);
1141   msg->pluginlen = htonl (tlen);
1142
1143   if (NULL != address)
1144   {
1145     msg->local_address_info = htonl((uint32_t) address->local_info);
1146     addr = (char *) &msg[1];
1147     memcpy (addr, address->address, alen);
1148     memcpy (&addr[alen], address->transport_name, tlen);
1149   }
1150   return msg;
1151 }
1152
1153
1154 /**
1155  * Context for #send_validation_information() and
1156  * #send_peer_information().
1157  */
1158 struct IterationContext
1159 {
1160   /**
1161    * Context to use for the transmission.
1162    */
1163   struct GNUNET_SERVER_TransmitContext *tc;
1164
1165   /**
1166    * Which peers do we care about?
1167    */
1168   struct GNUNET_PeerIdentity id;
1169
1170   /**
1171    * #GNUNET_YES if @e id should be ignored because we want all peers.
1172    */
1173   int all;
1174 };
1175
1176
1177 /**
1178  * Output information of validation entries to the given client.
1179  *
1180  * @param cls the `struct IterationContext *`
1181  * @param address the address
1182  * @param last_validation point in time when last validation was performed
1183  * @param valid_until point in time how long address is valid
1184  * @param next_validation point in time when next validation will be performed
1185  * @param state state of validation notification
1186  */
1187 static void
1188 send_validation_information (void *cls,
1189                              const struct GNUNET_HELLO_Address *address,
1190                              struct GNUNET_TIME_Absolute last_validation,
1191                              struct GNUNET_TIME_Absolute valid_until,
1192                              struct GNUNET_TIME_Absolute next_validation,
1193                              enum GNUNET_TRANSPORT_ValidationState state)
1194 {
1195   struct IterationContext *pc = cls;
1196   struct ValidationIterateResponseMessage *msg;
1197
1198   if ( (GNUNET_YES != pc->all) &&
1199        (0 != memcmp (&address->peer, &pc->id, sizeof (pc->id))) )
1200     return;
1201   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202               "Sending information about for validation entry for peer `%s' using address `%s'\n",
1203               GNUNET_i2s (&address->peer),
1204               (NULL != address) ? GST_plugins_a2s (address) : "<none>");
1205   msg = compose_validation_iterate_response_message (&address->peer, address);
1206   msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1207   msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1208   msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1209   msg->state = htonl ((uint32_t) state);
1210   GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1211   GNUNET_free (msg);
1212 }
1213
1214
1215 /**
1216  * Output information of neighbours to the given client.
1217  *
1218  * @param cls the `struct PeerIterationContext *`
1219  * @param peer identity of the neighbour
1220  * @param address the address
1221  * @param state current state this peer is in
1222  * @param state_timeout timeout for the current state of the peer
1223  * @param bandwidth_in inbound quota in NBO
1224  * @param bandwidth_out outbound quota in NBO
1225  */
1226 static void
1227 send_peer_information (void *cls,
1228                        const struct GNUNET_PeerIdentity *peer,
1229                        const struct GNUNET_HELLO_Address *address,
1230                        enum GNUNET_TRANSPORT_PeerState state,
1231                        struct GNUNET_TIME_Absolute state_timeout,
1232                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1233                        struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1234 {
1235   struct IterationContext *pc = cls;
1236   struct PeerIterateResponseMessage *msg;
1237
1238   if ( (GNUNET_YES != pc->all) &&
1239        (0 != memcmp (peer, &pc->id, sizeof (pc->id))) )
1240     return;
1241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242               "Sending information about `%s' using address `%s' in state `%s'\n",
1243               GNUNET_i2s(peer),
1244               (NULL != address) ? GST_plugins_a2s (address) : "<none>",
1245               GNUNET_TRANSPORT_ps2s (state));
1246   msg = compose_address_iterate_response_message (peer, address);
1247   msg->state = htonl (state);
1248   msg->state_timeout = GNUNET_TIME_absolute_hton(state_timeout);
1249   GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1250   GNUNET_free (msg);
1251 }
1252
1253
1254 /**
1255  * Client asked to obtain information about a specific or all peers
1256  * Process the request.
1257  *
1258  * @param cls unused
1259  * @param client the client
1260  * @param message the peer address information request
1261  */
1262 static void
1263 clients_handle_monitor_peers (void *cls,
1264                               struct GNUNET_SERVER_Client *client,
1265                               const struct GNUNET_MessageHeader *message)
1266 {
1267   struct GNUNET_SERVER_TransmitContext *tc;
1268   const struct PeerMonitorMessage *msg;
1269   struct IterationContext pc;
1270
1271   msg = (const struct PeerMonitorMessage *) message;
1272   if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1273        (NULL != lookup_monitoring_client (peer_monitoring_clients_head,
1274                                           client)) )
1275   {
1276     GNUNET_break (0);
1277     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1278     return;
1279   }
1280   GNUNET_SERVER_disable_receive_done_warning (client);
1281   GNUNET_SERVER_client_mark_monitor (client);
1282   pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1283
1284   /* Send initial list */
1285   if (0 == memcmp (&msg->peer,
1286                    &all_zeros,
1287                    sizeof (struct GNUNET_PeerIdentity)))
1288   {
1289     /* iterate over all neighbours */
1290     pc.all = GNUNET_YES;
1291     pc.id = msg->peer;
1292   }
1293   else
1294   {
1295     /* just return one neighbour */
1296     pc.all = GNUNET_NO;
1297     pc.id = msg->peer;
1298   }
1299   GST_neighbours_iterate (&send_peer_information, &pc);
1300
1301   if (GNUNET_YES != ntohl (msg->one_shot))
1302   {
1303     setup_peer_monitoring_client (client, &msg->peer);
1304   }
1305   else
1306   {
1307     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1308         GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1309   }
1310
1311   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1312 }
1313
1314
1315 /**
1316  * Client asked to obtain information about a specific or all validation
1317  * processes
1318  *
1319  * @param cls unused
1320  * @param client the client
1321  * @param message the peer address information request
1322  */
1323 static void
1324 clients_handle_monitor_validation (void *cls,
1325                                    struct GNUNET_SERVER_Client *client,
1326                                    const struct GNUNET_MessageHeader *message)
1327 {
1328   struct GNUNET_SERVER_TransmitContext *tc;
1329   struct PeerMonitorMessage *msg;
1330   struct IterationContext pc;
1331
1332   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST)
1333   {
1334     GNUNET_break (0);
1335     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1336     return;
1337   }
1338   if (ntohs (message->size) != sizeof (struct ValidationMonitorMessage))
1339   {
1340     GNUNET_break (0);
1341     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1342     return;
1343   }
1344   msg = (struct PeerMonitorMessage *) message;
1345   if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1346        (NULL != lookup_monitoring_client (val_monitoring_clients_head, client)) )
1347   {
1348     GNUNET_break (0);
1349     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1350     return;
1351   }
1352   GNUNET_SERVER_disable_receive_done_warning (client);
1353   GNUNET_SERVER_client_mark_monitor (client);
1354   pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1355
1356   /* Send initial list */
1357   if (0 == memcmp (&msg->peer,
1358                    &all_zeros,
1359                    sizeof (struct GNUNET_PeerIdentity)))
1360   {
1361     /* iterate over all neighbours */
1362     pc.all = GNUNET_YES;
1363     pc.id = msg->peer;
1364   }
1365   else
1366   {
1367     /* just return one neighbour */
1368     pc.all = GNUNET_NO;
1369     pc.id = msg->peer;
1370   }
1371   GST_validation_iterate (&send_validation_information,
1372                           &pc);
1373
1374   if (GNUNET_YES != ntohl (msg->one_shot))
1375   {
1376     setup_val_monitoring_client (client, &msg->peer);
1377   }
1378   else
1379   {
1380     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1381                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1382   }
1383   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1384 }
1385
1386
1387 /**
1388  * Function called by the plugin with information about the
1389  * current sessions managed by the plugin (for monitoring).
1390  *
1391  * @param cls closure
1392  * @param session session handle this information is about,
1393  *        NULL to indicate that we are "in sync" (initial
1394  *        iteration complete)
1395  * @param info information about the state of the session,
1396  *        NULL if @a session is also NULL and we are
1397  *        merely signalling that the initial iteration is over
1398  */
1399 static void
1400 plugin_session_info_cb (void *cls,
1401                         struct Session *session,
1402                         const struct GNUNET_TRANSPORT_SessionInfo *info)
1403 {
1404   struct TransportPluginMonitorMessage *msg;
1405   struct GNUNET_MessageHeader sync;
1406   size_t size;
1407   size_t slen;
1408   uint16_t alen;
1409   char *name;
1410   char *addr;
1411
1412   if (0 == GNUNET_SERVER_notification_context_get_size (plugin_nc))
1413   {
1414     fprintf (stderr, "UNSUB!\n");
1415     GST_plugins_monitor_subscribe (NULL, NULL);
1416     return;
1417   }
1418   if ( (NULL == info) &&
1419        (NULL == session) )
1420   {
1421     /* end of initial iteration */
1422     if (NULL != sync_client)
1423     {
1424       sync.size = htons (sizeof (struct GNUNET_MessageHeader));
1425       sync.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC);
1426       GNUNET_SERVER_notification_context_unicast (plugin_nc,
1427                                                   sync_client,
1428                                                   &sync,
1429                                                   GNUNET_NO);
1430       sync_client = NULL;
1431     }
1432     return;
1433   }
1434   GNUNET_assert (NULL != info);
1435   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1436               "Plugin event for peer %s on transport %s\n",
1437               GNUNET_i2s (&info->address->peer),
1438               info->address->transport_name);
1439   slen = strlen (info->address->transport_name) + 1;
1440   alen = info->address->address_length;
1441   size = sizeof (struct TransportPluginMonitorMessage) + slen + alen;
1442   if (size > UINT16_MAX)
1443   {
1444     GNUNET_break (0);
1445     return;
1446   }
1447   msg = GNUNET_malloc (size);
1448   msg->header.size = htons (size);
1449   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT);
1450   msg->session_state = htons ((uint16_t) info->state);
1451   msg->is_inbound = htons ((int16_t) info->is_inbound);
1452   msg->msgs_pending = htonl (info->num_msg_pending);
1453   msg->bytes_pending = htonl (info->num_bytes_pending);
1454   msg->timeout = GNUNET_TIME_absolute_hton (info->session_timeout);
1455   msg->delay = GNUNET_TIME_absolute_hton (info->receive_delay);
1456   msg->peer = info->address->peer;
1457   msg->session_id = (uint64_t) (intptr_t) session;
1458   msg->plugin_name_len = htons (slen);
1459   msg->plugin_address_len = htons (alen);
1460   name = (char *) &msg[1];
1461   memcpy (name, info->address->transport_name, slen);
1462   addr = &name[slen];
1463   memcpy (addr, info->address->address, 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, client);
1492   sync_client = client;
1493   GST_plugins_monitor_subscribe (&plugin_session_info_cb, NULL);
1494 }
1495
1496
1497 /**
1498  * Start handling requests from clients.
1499  *
1500  * @param server server used to accept clients from.
1501  */
1502 void
1503 GST_clients_start (struct GNUNET_SERVER_Handle *server)
1504 {
1505   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1506     {&clients_handle_start, NULL,
1507      GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
1508     {&clients_handle_hello, NULL,
1509      GNUNET_MESSAGE_TYPE_HELLO, 0},
1510     {&clients_handle_send, NULL,
1511      GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
1512     {&clients_handle_request_connect, NULL,
1513      GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
1514      sizeof (struct TransportRequestConnectMessage)},
1515     {&clients_handle_request_disconnect, NULL,
1516      GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_DISCONNECT,
1517      sizeof (struct TransportRequestDisconnectMessage)},
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, 0},
1534     {&clients_handle_monitor_plugins, NULL,
1535      GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_START,
1536      sizeof (struct GNUNET_MessageHeader) },
1537     {NULL, NULL, 0, 0}
1538   };
1539   peer_nc = GNUNET_SERVER_notification_context_create (server, 0);
1540   val_nc = GNUNET_SERVER_notification_context_create (server, 0);
1541   plugin_nc = GNUNET_SERVER_notification_context_create (server, 0);
1542   GNUNET_SERVER_add_handlers (server, handlers);
1543   GNUNET_SERVER_disconnect_notify (server,
1544                                    &client_disconnect_notification,
1545                                    NULL);
1546 }
1547
1548
1549 /**
1550  * Stop processing clients.
1551  */
1552 void
1553 GST_clients_stop ()
1554 {
1555   struct AddressToStringContext *cur;
1556
1557   while (NULL != (cur = a2s_head))
1558   {
1559     GNUNET_SERVER_transmit_context_destroy (cur->tc, GNUNET_NO);
1560     GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
1561     GNUNET_free (cur);
1562   }
1563   if (NULL != peer_nc)
1564   {
1565     GNUNET_SERVER_notification_context_destroy (peer_nc);
1566     peer_nc = NULL;
1567   }
1568   if (NULL != val_nc)
1569   {
1570     GNUNET_SERVER_notification_context_destroy (val_nc);
1571     val_nc = NULL;
1572   }
1573   if (NULL != plugin_nc)
1574   {
1575     GNUNET_SERVER_notification_context_destroy (plugin_nc);
1576     plugin_nc = NULL;
1577   }
1578 }
1579
1580
1581 /**
1582  * Broadcast the given message to all of our clients.
1583  *
1584  * @param msg message to broadcast
1585  * @param may_drop #GNUNET_YES if the message can be dropped / is payload
1586  */
1587 void
1588 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg,
1589                        int may_drop)
1590 {
1591   struct TransportClient *tc;
1592
1593   for (tc = clients_head; NULL != tc; tc = tc->next)
1594   {
1595     if ( (GNUNET_YES == may_drop) &&
1596          (GNUNET_YES != tc->send_payload) )
1597       continue; /* skip, this client does not care about payload */
1598     unicast (tc, msg, may_drop);
1599   }
1600 }
1601
1602
1603 /**
1604  * Send the given message to a particular client
1605  *
1606  * @param client target of the message
1607  * @param msg message to transmit
1608  * @param may_drop #GNUNET_YES if the message can be dropped
1609  */
1610 void
1611 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1612                      const struct GNUNET_MessageHeader *msg,
1613                      int may_drop)
1614 {
1615   struct TransportClient *tc;
1616
1617   tc = lookup_client (client);
1618   if (NULL == tc)
1619     return;                     /* client got disconnected in the meantime, drop message */
1620   unicast (tc, msg, may_drop);
1621 }
1622
1623
1624 /**
1625  * Broadcast the new active address to all clients monitoring the peer.
1626  *
1627  * @param peer peer this update is about (never NULL)
1628  * @param address address, NULL on disconnect
1629  * @param state the current state of the peer
1630  * @param state_timeout the time out for the state
1631  */
1632 void
1633 GST_clients_broadcast_peer_notification (const struct GNUNET_PeerIdentity *peer,
1634                                          const struct GNUNET_HELLO_Address *address,
1635                                          enum GNUNET_TRANSPORT_PeerState state,
1636                                          struct GNUNET_TIME_Absolute state_timeout)
1637 {
1638   struct PeerIterateResponseMessage *msg;
1639   struct MonitoringClient *mc;
1640
1641   msg = compose_address_iterate_response_message (peer, address);
1642   msg->state = htonl (state);
1643   msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
1644   for (mc = peer_monitoring_clients_head; NULL != mc; mc = mc->next)
1645     if ((0 == memcmp (&mc->peer, &all_zeros,
1646                       sizeof (struct GNUNET_PeerIdentity))) ||
1647         (0 == memcmp (&mc->peer, peer,
1648                       sizeof (struct GNUNET_PeerIdentity))))
1649       GNUNET_SERVER_notification_context_unicast (peer_nc,
1650                                                   mc->client,
1651                                                   &msg->header,
1652                                                   GNUNET_NO);
1653   GNUNET_free (msg);
1654 }
1655
1656
1657 /**
1658  * Broadcast the new validation changes to all clients monitoring the peer.
1659  *
1660  * @param peer peer this update is about (never NULL)
1661  * @param address address, NULL on disconnect
1662  * @param last_validation point in time when last validation was performed
1663  * @param valid_until point in time how long address is valid
1664  * @param next_validation point in time when next validation will be performed
1665  * @param state state of validation notification
1666  */
1667 void
1668 GST_clients_broadcast_validation_notification (const struct GNUNET_PeerIdentity *peer,
1669                                                const struct GNUNET_HELLO_Address *address,
1670                                                struct GNUNET_TIME_Absolute last_validation,
1671                                                struct GNUNET_TIME_Absolute valid_until,
1672                                                struct GNUNET_TIME_Absolute next_validation,
1673                                                enum GNUNET_TRANSPORT_ValidationState state)
1674 {
1675   struct ValidationIterateResponseMessage *msg;
1676   struct MonitoringClient *mc;
1677
1678   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1679               "Sending information about for validation entry for peer `%s' using address `%s'\n",
1680               GNUNET_i2s(peer),
1681               (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1682   msg = compose_validation_iterate_response_message (peer, address);
1683   msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1684   msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1685   msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1686   msg->state = htonl ((uint32_t) state);
1687   for (mc = val_monitoring_clients_head; NULL != mc; mc = mc->next)
1688     if ((0 == memcmp (&mc->peer, &all_zeros,
1689                       sizeof (struct GNUNET_PeerIdentity))) ||
1690         (0 == memcmp (&mc->peer, peer,
1691                       sizeof (struct GNUNET_PeerIdentity))))
1692       GNUNET_SERVER_notification_context_unicast (val_nc,
1693                                                   mc->client,
1694                                                   &msg->header,
1695                                                   GNUNET_NO);
1696   GNUNET_free (msg);
1697 }
1698
1699
1700 /* end of file gnunet-service-transport_clients.c */