-allow caller ID to differ from zone used for resolution
[oweals/gnunet.git] / src / transport / gnunet-service-transport_clients.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010-2013 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 plugin management API
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  * Context for address to string operations
122  */
123 struct AddressToStringContext
124 {
125   /**
126    * This is a doubly-linked list.
127    */
128   struct AddressToStringContext *next;
129
130   /**
131    * This is a doubly-linked list.
132    */
133   struct AddressToStringContext *prev;
134
135   /**
136    * Transmission context
137    */
138   struct GNUNET_SERVER_TransmitContext* tc;
139 };
140
141 /**
142  * Client monitoring changes of active addresses of our neighbours.
143  */
144 struct MonitoringClient
145 {
146   /**
147    * This is a doubly-linked list.
148    */
149   struct MonitoringClient *next;
150
151   /**
152    * This is a doubly-linked list.
153    */
154   struct MonitoringClient *prev;
155
156   /**
157    * Handle to the client.
158    */
159   struct GNUNET_SERVER_Client *client;
160
161   /**
162    * Peer identity to monitor the addresses of.
163    * Zero to monitor all neighrours.
164    */
165   struct GNUNET_PeerIdentity peer;
166
167 };
168
169
170 /**
171  * Head of linked list of all clients to this service.
172  */
173 static struct TransportClient *clients_head;
174
175 /**
176  * Tail of linked list of all clients to this service.
177  */
178 static struct TransportClient *clients_tail;
179
180 /**
181  * Head of linked list of all pending address iterations
182  */
183 struct AddressToStringContext *a2s_head;
184
185 /**
186  * Tail of linked list of all pending address iterations
187  */
188 struct AddressToStringContext *a2s_tail;
189
190 /**
191  * Head of linked list of monitoring clients.
192  */
193 static struct MonitoringClient *peer_monitoring_clients_head;
194
195 /**
196  * Tail of linked list of monitoring clients.
197  */
198 static struct MonitoringClient *peer_monitoring_clients_tail;
199
200 /**
201  * Head of linked list of validation monitoring clients.
202  */
203 static struct MonitoringClient *val_monitoring_clients_head;
204
205 /**
206  * Tail of linked list of validation monitoring clients.
207  */
208 static struct MonitoringClient *val_monitoring_clients_tail;
209
210 /**
211  * Notification context, to send updates on changes to active addresses
212  * of our neighbours.
213  */
214 static struct GNUNET_SERVER_NotificationContext *peer_nc;
215
216 /**
217  * Notification context, to send updates on changes to active addresses
218  * of our neighbours.
219  */
220 static struct GNUNET_SERVER_NotificationContext *val_nc;
221
222 /**
223  * Find the internal handle associated with the given client handle
224  *
225  * @param client server's client handle to look up
226  * @return internal client handle
227  */
228 static struct TransportClient *
229 lookup_client (struct GNUNET_SERVER_Client *client)
230 {
231   struct TransportClient *tc;
232
233   for (tc = clients_head; NULL != tc; tc = tc->next)
234     if (tc->client == client)
235       return tc;
236   return NULL;
237 }
238
239
240 /**
241  * Create the internal handle for the given server client handle
242  *
243  * @param client server's client handle to create our internal handle for
244  * @return fresh internal client handle
245  */
246 static struct TransportClient *
247 setup_client (struct GNUNET_SERVER_Client *client)
248 {
249   struct TransportClient *tc;
250
251   GNUNET_assert (NULL == lookup_client (client));
252   tc = GNUNET_new (struct TransportClient);
253   tc->client = client;
254   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255               "Client %p connected\n",
256               tc);
257   return tc;
258 }
259
260
261 /**
262  * Find the handle to the monitoring client associated with the given
263  * client handle
264  *
265  * @param head the head of the client queue to look in
266  * @param client server's client handle to look up
267  * @return handle to the monitoring client
268  */
269 static struct MonitoringClient *
270 lookup_monitoring_client (struct MonitoringClient *head,
271     struct GNUNET_SERVER_Client *client)
272 {
273   struct MonitoringClient *mc;
274
275   for (mc = head; NULL != mc; mc = mc->next)
276     if (mc->client == client)
277       return mc;
278   return NULL;
279 }
280
281
282 /**
283  * Setup a new monitoring client using the given server client handle and
284  * the peer identity.
285  *
286  * @param client server's client handle to create our internal handle for
287  * @param peer identity of the peer to monitor the addresses of,
288  *             zero to monitor all neighrours.
289  * @return handle to the new monitoring client
290  */
291 static struct MonitoringClient *
292 setup_peer_monitoring_client (struct GNUNET_SERVER_Client *client,
293                          struct GNUNET_PeerIdentity *peer)
294 {
295   struct MonitoringClient *mc;
296   static struct GNUNET_PeerIdentity all_zeros;
297
298   GNUNET_assert (lookup_monitoring_client (peer_monitoring_clients_head, client) == NULL);
299   mc = GNUNET_new (struct MonitoringClient);
300   mc->client = client;
301   mc->peer = *peer;
302   GNUNET_CONTAINER_DLL_insert (peer_monitoring_clients_head, peer_monitoring_clients_tail, mc);
303   GNUNET_SERVER_notification_context_add (peer_nc, client);
304
305   if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
306     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
307                 "Client %p started monitoring of the peer `%s'\n",
308                 mc, GNUNET_i2s (peer));
309   else
310     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311               "Client %p started monitoring all peers\n", mc);
312   return mc;
313 }
314
315 /**
316  * Setup a new monitoring client using the given server client handle and
317  * the peer identity.
318  *
319  * @param client server's client handle to create our internal handle for
320  * @param peer identity of the peer to monitor the addresses of,
321  *             zero to monitor all neighrours.
322  * @return handle to the new monitoring client
323  */
324 static struct MonitoringClient *
325 setup_val_monitoring_client (struct GNUNET_SERVER_Client *client,
326                          struct GNUNET_PeerIdentity *peer)
327 {
328   struct MonitoringClient *mc;
329   static struct GNUNET_PeerIdentity all_zeros;
330
331   GNUNET_assert (lookup_monitoring_client (val_monitoring_clients_head, client) == NULL);
332   mc = GNUNET_new (struct MonitoringClient);
333   mc->client = client;
334   mc->peer = *peer;
335   GNUNET_CONTAINER_DLL_insert (val_monitoring_clients_head, val_monitoring_clients_tail, mc);
336   GNUNET_SERVER_notification_context_add (val_nc, client);
337
338   if (0 != memcmp (peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
339     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
340                 "Client %p started monitoring of the peer `%s'\n",
341                 mc, GNUNET_i2s (peer));
342   else
343     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
344               "Client %p started monitoring all peers\n", mc);
345   return mc;
346 }
347
348
349 /**
350  * Function called to notify a client about the socket being ready to
351  * queue more data.  "buf" will be NULL and "size" zero if the socket
352  * was closed for writing in the meantime.
353  *
354  * @param cls closure
355  * @param size number of bytes available in @a buf
356  * @param buf where the callee should write the message
357  * @return number of bytes written to @a buf
358  */
359 static size_t
360 transmit_to_client_callback (void *cls, size_t size, void *buf)
361 {
362   struct TransportClient *tc = cls;
363   struct ClientMessageQueueEntry *q;
364   const struct GNUNET_MessageHeader *msg;
365   char *cbuf;
366   uint16_t msize;
367   size_t tsize;
368
369   tc->th = NULL;
370   if (NULL == buf)
371   {
372     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373                 "Transmission to client failed, closing connection.\n");
374     return 0;
375   }
376   cbuf = buf;
377   tsize = 0;
378   while (NULL != (q = tc->message_queue_head))
379   {
380     msg = (const struct GNUNET_MessageHeader *) &q[1];
381     msize = ntohs (msg->size);
382     if (msize + tsize > size)
383       break;
384     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385                 "Transmitting message of type %u to client %p.\n",
386                 ntohs (msg->type), tc);
387     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
388                                  tc->message_queue_tail,
389                                  q);
390     tc->message_count--;
391     memcpy (&cbuf[tsize], msg, msize);
392     GNUNET_free (q);
393     tsize += msize;
394   }
395   if (NULL != q)
396   {
397     GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
398     tc->th =
399         GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
400                                              GNUNET_TIME_UNIT_FOREVER_REL,
401                                              &transmit_to_client_callback, tc);
402     GNUNET_assert (NULL != tc->th);
403   }
404   return tsize;
405 }
406
407
408 /**
409  * Queue the given message for transmission to the given client
410  *
411  * @param tc target of the message
412  * @param msg message to transmit
413  * @param may_drop #GNUNET_YES if the message can be dropped
414  */
415 static void
416 unicast (struct TransportClient *tc,
417          const struct GNUNET_MessageHeader *msg,
418          int may_drop)
419 {
420   struct ClientMessageQueueEntry *q;
421   uint16_t msize;
422
423   if (NULL == msg)
424   {
425     GNUNET_break (0);
426     return;
427   }
428
429   if ((tc->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
430   {
431     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
432                 _("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
433                 ntohs (msg->type),
434                 ntohs (msg->size),
435                 tc->message_count,
436                 MAX_PENDING);
437     GNUNET_STATISTICS_update (GST_stats,
438                               gettext_noop
439                               ("# messages dropped due to slow client"), 1,
440                               GNUNET_NO);
441     return;
442   }
443   msize = ntohs (msg->size);
444   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
445   q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
446   memcpy (&q[1], msg, msize);
447   GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
448                                     tc->message_queue_tail, q);
449   tc->message_count++;
450   if (NULL != tc->th)
451     return;
452   tc->th =
453       GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
454                                            GNUNET_TIME_UNIT_FOREVER_REL,
455                                            &transmit_to_client_callback, tc);
456   GNUNET_assert (NULL != tc->th);
457 }
458
459
460 /**
461  * Called whenever a client is disconnected.  Frees our
462  * resources associated with that client.
463  *
464  * @param cls closure
465  * @param client identification of the client
466  */
467 static void
468 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
469 {
470   struct TransportClient *tc;
471   struct MonitoringClient *mc;
472   struct ClientMessageQueueEntry *mqe;
473
474   if (client == NULL)
475     return;
476   mc = lookup_monitoring_client (peer_monitoring_clients_head, client);
477   if (mc != NULL)
478   {
479     GNUNET_CONTAINER_DLL_remove (peer_monitoring_clients_head,
480                                  peer_monitoring_clients_tail,
481                                  mc);
482     GNUNET_free (mc);
483   }
484   mc = lookup_monitoring_client (val_monitoring_clients_head, client);
485   if (mc != NULL)
486   {
487     GNUNET_CONTAINER_DLL_remove (val_monitoring_clients_head,
488                                  val_monitoring_clients_tail,
489                                  mc);
490     GNUNET_free (mc);
491   }
492   tc = lookup_client (client);
493   if (tc == NULL)
494     return;
495   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
496               "Client %p disconnected, cleaning up.\n", tc);
497   while (NULL != (mqe = tc->message_queue_head))
498   {
499     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
500                                  mqe);
501     tc->message_count--;
502     GNUNET_free (mqe);
503   }
504   GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
505   if (tc->th != NULL)
506   {
507     GNUNET_SERVER_notify_transmit_ready_cancel (tc->th);
508     tc->th = NULL;
509   }
510   GNUNET_break (0 == tc->message_count);
511   GNUNET_free (tc);
512 }
513
514
515 /**
516  * Function called for each of our connected neighbours.  Notify the
517  * client about the existing neighbour.
518  *
519  * @param cls the `struct TransportClient *` to notify
520  * @param peer identity of the neighbour
521  * @param address the address
522  * @param state the current state of the peer
523  * @param state_timeout the time out for the state
524  * @param bandwidth_in inbound bandwidth in NBO
525  * @param bandwidth_out outbound bandwidth in NBO
526  */
527 static void
528 notify_client_about_neighbour (void *cls,
529     const struct GNUNET_PeerIdentity *peer,
530     const struct GNUNET_HELLO_Address *address,
531     enum GNUNET_TRANSPORT_PeerState state,
532     struct GNUNET_TIME_Absolute state_timeout,
533     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
534     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
535 {
536   struct TransportClient *tc = cls;
537   struct ConnectInfoMessage *cim;
538   size_t size = sizeof (struct ConnectInfoMessage);
539   char buf[size] GNUNET_ALIGN;
540
541   if (GNUNET_NO == GST_neighbours_test_connected (peer))
542     return;
543
544   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
545   cim = (struct ConnectInfoMessage *) buf;
546   cim->header.size = htons (size);
547   cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
548   cim->id = *peer;
549   cim->quota_in = bandwidth_in;
550   cim->quota_out = bandwidth_out;
551   unicast (tc, &cim->header, GNUNET_NO);
552 }
553
554
555 /**
556  * Initialize a normal client.  We got a start message from this
557  * client, add him to the list of clients for broadcasting of inbound
558  * messages.
559  *
560  * @param cls unused
561  * @param client the client
562  * @param message the start message that was sent
563  */
564 static void
565 clients_handle_start (void *cls, struct GNUNET_SERVER_Client *client,
566                       const struct GNUNET_MessageHeader *message)
567 {
568   const struct StartMessage *start;
569   struct TransportClient *tc;
570   uint32_t options;
571
572   tc = lookup_client (client);
573
574   GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
575               "Client %p sent START\n", tc);
576   if (tc != NULL)
577   {
578     /* got 'start' twice from the same client, not allowed */
579     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
580                 "TransportClient %p ServerClient %p sent multiple START messages\n",
581                 tc, tc->client);
582     GNUNET_break (0);
583     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
584     return;
585   }
586   start = (const struct StartMessage *) message;
587   options = ntohl (start->options);
588   if ((0 != (1 & options)) &&
589       (0 !=
590        memcmp (&start->self, &GST_my_identity,
591                sizeof (struct GNUNET_PeerIdentity))))
592   {
593     /* client thinks this is a different peer, reject */
594     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
595                 _
596                 ("Rejecting control connection from peer `%s', which is not me!\n"),
597                 GNUNET_i2s (&start->self));
598     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
599     return;
600   }
601   tc = setup_client (client);
602   tc->send_payload = (0 != (2 & options));
603   unicast (tc, GST_hello_get (), GNUNET_NO);
604   GST_neighbours_iterate (&notify_client_about_neighbour, tc);
605   GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
606   GNUNET_SERVER_receive_done (client, GNUNET_OK);
607 }
608
609
610 /**
611  * Client sent us a HELLO.  Process the request.
612  *
613  * @param cls unused
614  * @param client the client
615  * @param message the HELLO message
616  */
617 static void
618 clients_handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
619                       const struct GNUNET_MessageHeader *message)
620 {
621   GST_validation_handle_hello (message);
622   GNUNET_SERVER_receive_done (client, GNUNET_OK);
623 }
624
625
626 /**
627  * Closure for 'handle_send_transmit_continuation'
628  */
629 struct SendTransmitContinuationContext
630 {
631   /**
632    * Client that made the request.
633    */
634   struct GNUNET_SERVER_Client *client;
635
636   /**
637    * Peer that was the target.
638    */
639   struct GNUNET_PeerIdentity target;
640 };
641
642
643 /**
644  * Function called after the transmission is done.  Notify the client that it is
645  * OK to send the next message.
646  *
647  * @param cls closure
648  * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
649  * @param bytes_payload bytes payload sent
650  * @param bytes_on_wire bytes sent on wire
651  */
652 static void
653 handle_send_transmit_continuation (void *cls, int success,
654                                    size_t bytes_payload,
655                                    size_t bytes_on_wire)
656 {
657   struct SendTransmitContinuationContext *stcc = cls;
658   struct SendOkMessage send_ok_msg;
659
660   if (GNUNET_OK == success)
661     GST_neighbours_notify_payload_sent (&stcc->target, bytes_payload);
662
663   send_ok_msg.header.size = htons (sizeof (send_ok_msg));
664   send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
665   send_ok_msg.bytes_msg = htonl (bytes_payload);
666   send_ok_msg.bytes_physical = htonl (bytes_on_wire);
667   send_ok_msg.success = htonl (success);
668   send_ok_msg.latency =
669       GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
670   send_ok_msg.peer = stcc->target;
671   GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO);
672   GNUNET_SERVER_client_drop (stcc->client);
673   GNUNET_free (stcc);
674 }
675
676
677 /**
678  * Client asked for transmission to a peer.  Process the request.
679  *
680  * @param cls unused
681  * @param client the client
682  * @param message the send message that was sent
683  */
684 static void
685 clients_handle_send (void *cls,
686                      struct GNUNET_SERVER_Client *client,
687                      const struct GNUNET_MessageHeader *message)
688 {
689   const struct OutboundMessage *obm;
690   const struct GNUNET_MessageHeader *obmm;
691   struct SendTransmitContinuationContext *stcc;
692   uint16_t size;
693   uint16_t msize;
694   struct TransportClient *tc;
695
696   tc = lookup_client (client);
697   if (NULL == tc)
698   {
699     /* client asked for transmission before 'START' */
700     GNUNET_break (0);
701     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
702     return;
703   }
704
705   size = ntohs (message->size);
706   if (size <
707       sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
708   {
709     GNUNET_break (0);
710     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
711     return;
712   }
713   obm = (const struct OutboundMessage *) message;
714   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
715   msize = size - sizeof (struct OutboundMessage);
716   if (msize < sizeof (struct GNUNET_MessageHeader))
717   {
718     GNUNET_break (0);
719     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
720     return;
721   }
722
723   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724               "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
725               "SEND",
726               GNUNET_i2s (&obm->peer),
727               ntohs (obmm->type),
728               msize);
729   if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
730   {
731     /* not connected, not allowed to send; can happen due to asynchronous operations */
732     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733                 "Could not send message to peer `%s': not connected\n",
734                 GNUNET_i2s (&obm->peer));
735     GNUNET_STATISTICS_update (GST_stats,
736                               gettext_noop
737                               ("# bytes payload dropped (other peer was not connected)"),
738                               msize, GNUNET_NO);
739     GNUNET_SERVER_receive_done (client, GNUNET_OK);
740     return;
741   }
742   GNUNET_SERVER_receive_done (client, GNUNET_OK);
743   stcc = GNUNET_new (struct SendTransmitContinuationContext);
744   stcc->target = obm->peer;
745   stcc->client = client;
746   GNUNET_SERVER_client_keep (client);
747   GST_manipulation_send (&obm->peer, obmm, msize,
748                        GNUNET_TIME_relative_ntoh (obm->timeout),
749                        &handle_send_transmit_continuation, stcc);
750 }
751
752
753 /**
754  * Try to initiate a connection to the given peer if the blacklist
755  * allowed it.
756  *
757  * @param cls closure (unused, NULL)
758  * @param peer identity of peer that was tested
759  * @param result #GNUNET_OK if the connection is allowed,
760  *               #GNUNET_NO if not
761  */
762 static void
763 try_connect_if_allowed (void *cls,
764                         const struct GNUNET_PeerIdentity *peer,
765                         int result)
766 {
767   if (GNUNET_OK != result)
768   {
769     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
770                 _("Blacklist refuses connection attempt to peer `%s'\n"),
771                 GNUNET_i2s (peer));
772     return;                     /* not allowed */
773   }
774
775   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
776               _("Blacklist allows connection attempt to peer `%s'\n"),
777               GNUNET_i2s (peer));
778
779   GST_neighbours_try_connect (peer);
780 }
781
782
783 /**
784  * Handle request connect message
785  *
786  * @param cls closure (always NULL)
787  * @param client identification of the client
788  * @param message the actual message
789  */
790 static void
791 clients_handle_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
792                                 const struct GNUNET_MessageHeader *message)
793 {
794   const struct TransportRequestConnectMessage *trcm =
795       (const struct TransportRequestConnectMessage *) message;
796
797   if (GNUNET_YES == ntohl (trcm->connect))
798   {
799     GNUNET_STATISTICS_update (GST_stats,
800                               gettext_noop
801                               ("# REQUEST CONNECT messages received"), 1,
802                               GNUNET_NO);
803
804     if (0 == memcmp (&trcm->peer, &GST_my_identity,
805                   sizeof (struct GNUNET_PeerIdentity)))
806     {
807       GNUNET_break_op (0);
808       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
809                   "Received a request connect message myself `%s'\n",
810                   GNUNET_i2s (&trcm->peer));
811     }
812     else
813     {
814       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
815                   _("Received a request connect message for peer `%s'\n"),
816                   GNUNET_i2s (&trcm->peer));
817
818       (void) GST_blacklist_test_allowed (&trcm->peer, NULL, &try_connect_if_allowed,
819                                        NULL);
820     }
821   }
822   else if (GNUNET_NO == ntohl (trcm->connect))
823   {
824     GNUNET_STATISTICS_update (GST_stats,
825                               gettext_noop
826                               ("# REQUEST DISCONNECT messages received"), 1,
827                               GNUNET_NO);
828
829     if (0 == memcmp (&trcm->peer, &GST_my_identity,
830                   sizeof (struct GNUNET_PeerIdentity)))
831     {
832       GNUNET_break_op (0);
833       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
834                   "Received a request disconnect message myself `%s'\n",
835                   GNUNET_i2s (&trcm->peer));
836     }
837     else
838     {
839       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
840                   _("Received a request disconnect message for peer `%s'\n"),
841                   GNUNET_i2s (&trcm->peer));
842       (void) GST_neighbours_force_disconnect (&trcm->peer);
843     }
844   }
845   else
846   {
847     GNUNET_break_op (0);
848     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
849     return;
850   }
851   GNUNET_SERVER_receive_done (client, GNUNET_OK);
852 }
853
854
855 /**
856  * Take the given address and append it to the set of results sent back to
857  * the client.
858  *
859  * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
860  * @param buf text to transmit
861  * @param res GNUNET_OK if conversion was successful, GNUNET_SYSERR on error
862  */
863 static void
864 transmit_address_to_client (void *cls, const char *buf, int res)
865 {
866   struct AddressToStringContext *actx = cls;
867   struct AddressToStringResultMessage *atsm;
868   size_t len;
869
870   if (NULL == buf)
871   {
872     GNUNET_assert ((res == GNUNET_OK) ||  (res == GNUNET_SYSERR));
873
874     len = sizeof (struct AddressToStringResultMessage);
875     atsm = GNUNET_malloc (len);
876     atsm->header.size = ntohs (len);
877     atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
878
879     if (GNUNET_OK == res)
880     {
881       /* done, transmit */
882       atsm->res = htonl (GNUNET_YES);
883       atsm->addr_len = htonl (0);
884       GNUNET_SERVER_transmit_context_append_message (actx->tc,
885           (const struct GNUNET_MessageHeader *) atsm);
886
887       GNUNET_SERVER_transmit_context_run (actx->tc, GNUNET_TIME_UNIT_FOREVER_REL);
888       GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, actx);
889       GNUNET_free (actx);
890     }
891     if (GNUNET_SYSERR == res)
892     {
893       /* address conversion failed */
894
895       atsm->res = htonl (GNUNET_NO);
896       atsm->addr_len = htonl (0);
897       GNUNET_SERVER_transmit_context_append_message (actx->tc,
898           (const struct GNUNET_MessageHeader *) atsm);
899       GNUNET_free (atsm);
900     }
901   }
902   else
903   {
904     GNUNET_assert (res == GNUNET_OK);
905     /* succesful conversion, append*/
906     len = sizeof (struct AddressToStringResultMessage) + strlen (buf) + 1;
907     atsm = GNUNET_malloc (len);
908     atsm->header.size = ntohs (len);
909     atsm->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
910     atsm->res = htonl (GNUNET_YES);
911     atsm->addr_len = htonl (strlen (buf) + 1);
912     memcpy (&atsm[1], buf, strlen (buf) + 1);
913     GNUNET_SERVER_transmit_context_append_message (actx->tc,
914         (const struct GNUNET_MessageHeader *) atsm);
915     GNUNET_free (atsm);
916   }
917 }
918
919
920 /**
921  * Client asked to resolve an address.  Process the request.
922  *
923  * @param cls unused
924  * @param client the client
925  * @param message the resolution request
926  */
927 static void
928 clients_handle_address_to_string (void *cls,
929                                   struct GNUNET_SERVER_Client *client,
930                                   const struct GNUNET_MessageHeader *message)
931 {
932   const struct AddressLookupMessage *alum;
933   struct GNUNET_TRANSPORT_PluginFunctions *papi;
934   const char *plugin_name;
935   const char *address;
936   uint32_t address_len;
937   uint16_t size;
938   struct GNUNET_SERVER_TransmitContext *tc;
939   struct AddressToStringContext *actx;
940   struct AddressToStringResultMessage atsm;
941   struct GNUNET_TIME_Relative rtimeout;
942   int32_t numeric;
943
944   size = ntohs (message->size);
945   if (size < sizeof (struct AddressLookupMessage))
946   {
947     GNUNET_break (0);
948     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
949     return;
950   }
951   alum = (const struct AddressLookupMessage *) message;
952   address_len = ntohs (alum->addrlen);
953   if (size <= sizeof (struct AddressLookupMessage) + address_len)
954   {
955     GNUNET_break (0);
956     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
957     return;
958   }
959   address = (const char *) &alum[1];
960   plugin_name = (const char *) &address[address_len];
961   if ('\0' != plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1])
962   {
963     GNUNET_break (0);
964     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
965     return;
966   }
967   rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
968   numeric = ntohs (alum->numeric_only);
969   tc = GNUNET_SERVER_transmit_context_create (client);
970   papi = GST_plugins_printer_find (plugin_name);
971   if (NULL == papi)
972   {
973     atsm.header.size = ntohs (sizeof (struct AddressToStringResultMessage));
974     atsm.header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
975     atsm.res = htonl (GNUNET_NO);
976     atsm.addr_len = htonl (0);
977     GNUNET_SERVER_transmit_context_append_message (tc,
978         (const struct GNUNET_MessageHeader *) &atsm);
979     GNUNET_SERVER_transmit_context_run (tc, rtimeout);
980     return;
981   }
982   actx = GNUNET_new (struct AddressToStringContext);
983   actx->tc = tc;
984   GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
985   GNUNET_SERVER_disable_receive_done_warning (client);
986   papi->address_pretty_printer (papi->cls, plugin_name, address, address_len,
987                                 numeric, rtimeout, &transmit_address_to_client,
988                                 actx);
989 }
990
991
992 /**
993  * Compose #PeerIterateResponseMessage using the given peer and address.
994  *
995  * @param peer identity of the peer
996  * @param address the address, NULL on disconnect
997  * @return composed message
998  */
999 static struct PeerIterateResponseMessage *
1000 compose_address_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1001                                           const struct GNUNET_HELLO_Address *address)
1002 {
1003   struct PeerIterateResponseMessage *msg;
1004   size_t size;
1005   size_t tlen;
1006   size_t alen;
1007   char *addr;
1008
1009   GNUNET_assert (NULL != peer);
1010   if (NULL != address)
1011   {
1012     tlen = strlen (address->transport_name) + 1;
1013     alen = address->address_length;
1014   }
1015   else
1016     tlen = alen = 0;
1017   size = (sizeof (struct PeerIterateResponseMessage) + alen + tlen);
1018   msg = GNUNET_malloc (size);
1019   msg->header.size = htons (size);
1020   msg->header.type =
1021       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1022   msg->reserved = htonl (0);
1023   msg->peer = *peer;
1024   msg->addrlen = htonl (alen);
1025   msg->pluginlen = htonl (tlen);
1026
1027   if (NULL != address)
1028   {
1029     msg->local_address_info = htonl((uint32_t) address->local_info);
1030     addr = (char *) &msg[1];
1031     memcpy (addr, address->address, alen);
1032     memcpy (&addr[alen], address->transport_name, tlen);
1033   }
1034   return msg;
1035 }
1036
1037 /**
1038  * Compose #PeerIterateResponseMessage using the given peer and address.
1039  *
1040  * @param peer identity of the peer
1041  * @param address the address, NULL on disconnect
1042  * @return composed message
1043  */
1044 static struct ValidationIterateResponseMessage *
1045 compose_validation_iterate_response_message (const struct GNUNET_PeerIdentity *peer,
1046                                           const struct GNUNET_HELLO_Address *address)
1047 {
1048   struct ValidationIterateResponseMessage *msg;
1049   size_t size;
1050   size_t tlen;
1051   size_t alen;
1052   char *addr;
1053
1054   GNUNET_assert (NULL != peer);
1055   if (NULL != address)
1056   {
1057     tlen = strlen (address->transport_name) + 1;
1058     alen = address->address_length;
1059   }
1060   else
1061     tlen = alen = 0;
1062   size = (sizeof (struct ValidationIterateResponseMessage) + alen + tlen);
1063   msg = GNUNET_malloc (size);
1064   msg->header.size = htons (size);
1065   msg->header.type =
1066       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1067   msg->reserved = htonl (0);
1068   msg->peer = *peer;
1069   msg->addrlen = htonl (alen);
1070   msg->pluginlen = htonl (tlen);
1071
1072   if (NULL != address)
1073   {
1074     msg->local_address_info = htonl((uint32_t) address->local_info);
1075     addr = (char *) &msg[1];
1076     memcpy (addr, address->address, alen);
1077     memcpy (&addr[alen], address->transport_name, tlen);
1078   }
1079   return msg;
1080 }
1081
1082 struct IterationContext
1083 {
1084   struct GNUNET_SERVER_TransmitContext *tc;
1085
1086   struct GNUNET_PeerIdentity id;
1087
1088   int all;
1089 };
1090
1091 /**
1092  * Output information of validation entries to the given client.
1093  *
1094  * @param cls the 'struct IterationContext'
1095  * @param peer identity of the neighbour
1096  * @param address the address
1097  * @param last_validation point in time when last validation was performed
1098  * @param valid_until point in time how long address is valid
1099  * @param next_validation point in time when next validation will be performed
1100  * @param state state of validation notification
1101  */
1102 static void
1103 send_validation_information (void *cls,
1104     const struct GNUNET_PeerIdentity *peer,
1105     const struct GNUNET_HELLO_Address *address,
1106     struct GNUNET_TIME_Absolute last_validation,
1107     struct GNUNET_TIME_Absolute valid_until,
1108     struct GNUNET_TIME_Absolute next_validation,
1109     enum GNUNET_TRANSPORT_ValidationState state)
1110 {
1111   struct IterationContext *pc = cls;
1112   struct ValidationIterateResponseMessage *msg;
1113
1114   if ( (GNUNET_YES == pc->all) ||
1115        (0 == memcmp (peer, &pc->id, sizeof (pc->id))) )
1116   {
1117     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118         "Sending information about for validation entry for peer `%s' using address `%s'\n",
1119         GNUNET_i2s(peer), (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1120     msg = compose_validation_iterate_response_message (peer, address);
1121     msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1122     msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1123     msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1124     msg->state = htonl ((uint32_t) state);
1125     GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1126     GNUNET_free (msg);
1127   }
1128 }
1129
1130
1131 /**
1132  * Output information of neighbours to the given client.
1133  *
1134  * @param cls the 'struct PeerIterationContext'
1135  * @param peer identity of the neighbour
1136  * @param address the address
1137  * @param state current state this peer is in
1138  * @param state_timeout timeout for the current state of the peer
1139  * @param bandwidth_in inbound quota in NBO
1140  * @param bandwidth_out outbound quota in NBO
1141  */
1142 static void
1143 send_peer_information (void *cls,
1144     const struct GNUNET_PeerIdentity *peer,
1145     const struct GNUNET_HELLO_Address *address,
1146     enum GNUNET_TRANSPORT_PeerState state,
1147     struct GNUNET_TIME_Absolute state_timeout,
1148     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1149     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1150 {
1151   struct IterationContext *pc = cls;
1152   struct PeerIterateResponseMessage *msg;
1153
1154   if ( (GNUNET_YES == pc->all) ||
1155        (0 == memcmp (peer, &pc->id, sizeof (pc->id))) )
1156   {
1157     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158         "Sending information about `%s' using address `%s' in state `%s'\n",
1159         GNUNET_i2s(peer),
1160         (address != NULL) ? GST_plugins_a2s (address) : "<none>",
1161         GNUNET_TRANSPORT_ps2s (state));
1162     msg = compose_address_iterate_response_message (peer, address);
1163     msg->state = htonl (state);
1164     msg->state_timeout = GNUNET_TIME_absolute_hton(state_timeout);
1165     GNUNET_SERVER_transmit_context_append_message (pc->tc, &msg->header);
1166     GNUNET_free (msg);
1167   }
1168 }
1169
1170
1171
1172 /**
1173  * Client asked to obtain information about a specific or all peers
1174  * Process the request.
1175  *
1176  * @param cls unused
1177  * @param client the client
1178  * @param message the peer address information request
1179  */
1180 static void
1181 clients_handle_monitor_peers (void *cls, struct GNUNET_SERVER_Client *client,
1182                                 const struct GNUNET_MessageHeader *message)
1183 {
1184   static struct GNUNET_PeerIdentity all_zeros;
1185   struct GNUNET_SERVER_TransmitContext *tc;
1186   struct PeerMonitorMessage *msg;
1187   struct IterationContext pc;
1188
1189   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST)
1190   {
1191     GNUNET_break (0);
1192     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1193     return;
1194   }
1195   if (ntohs (message->size) != sizeof (struct PeerMonitorMessage))
1196   {
1197     GNUNET_break (0);
1198     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1199     return;
1200   }
1201   msg = (struct PeerMonitorMessage *) message;
1202   if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1203        (NULL != lookup_monitoring_client (peer_monitoring_clients_head, client)) )
1204   {
1205     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1206                 "ServerClient %p tried to start monitoring twice\n",
1207                 client);
1208     GNUNET_break (0);
1209     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1210     return;
1211   }
1212   GNUNET_SERVER_disable_receive_done_warning (client);
1213   pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1214
1215   /* Send initial list */
1216   if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
1217   {
1218     /* iterate over all neighbours */
1219     pc.all = GNUNET_YES;
1220     pc.id = msg->peer;
1221   }
1222   else
1223   {
1224     /* just return one neighbour */
1225     pc.all = GNUNET_NO;
1226     pc.id = msg->peer;
1227   }
1228   GST_neighbours_iterate (&send_peer_information, &pc);
1229
1230   if (GNUNET_YES != ntohl (msg->one_shot))
1231   {
1232     setup_peer_monitoring_client (client, &msg->peer);
1233   }
1234   else
1235   {
1236     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1237         GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
1238   }
1239
1240   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1241 }
1242
1243
1244 /**
1245  * Client asked to obtain information about a specific or all validation
1246  * processes
1247  *
1248  * @param cls unused
1249  * @param client the client
1250  * @param message the peer address information request
1251  */
1252 static void
1253 clients_handle_monitor_validation (void *cls, struct GNUNET_SERVER_Client *client,
1254                                 const struct GNUNET_MessageHeader *message)
1255 {
1256   static struct GNUNET_PeerIdentity all_zeros;
1257   struct GNUNET_SERVER_TransmitContext *tc;
1258   struct PeerMonitorMessage *msg;
1259   struct IterationContext pc;
1260
1261   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST)
1262   {
1263     GNUNET_break (0);
1264     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1265     return;
1266   }
1267   if (ntohs (message->size) != sizeof (struct ValidationMonitorMessage))
1268   {
1269     GNUNET_break (0);
1270     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1271     return;
1272   }
1273   msg = (struct PeerMonitorMessage *) message;
1274   if ( (GNUNET_YES != ntohl (msg->one_shot)) &&
1275        (NULL != lookup_monitoring_client (val_monitoring_clients_head, client)) )
1276   {
1277     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1278                 "ServerClient %p tried to start monitoring twice\n",
1279                 client);
1280     GNUNET_break (0);
1281     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1282     return;
1283   }
1284   GNUNET_SERVER_disable_receive_done_warning (client);
1285   pc.tc = tc = GNUNET_SERVER_transmit_context_create (client);
1286
1287   /* Send initial list */
1288   if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
1289   {
1290     /* iterate over all neighbours */
1291     pc.all = GNUNET_YES;
1292     pc.id = msg->peer;
1293   }
1294   else
1295   {
1296     /* just return one neighbour */
1297     pc.all = GNUNET_NO;
1298     pc.id = msg->peer;
1299   }
1300
1301   GST_validation_iterate (&send_validation_information, &pc);
1302
1303   if (GNUNET_YES != ntohl (msg->one_shot))
1304   {
1305     setup_val_monitoring_client (client, &msg->peer);
1306   }
1307   else
1308   {
1309     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
1310         GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
1311   }
1312   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1313 }
1314
1315 /**
1316  * Start handling requests from clients.
1317  *
1318  * @param server server used to accept clients from.
1319  */
1320 void
1321 GST_clients_start (struct GNUNET_SERVER_Handle *server)
1322 {
1323   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1324     {&clients_handle_start, NULL,
1325      GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
1326     {&clients_handle_hello, NULL,
1327      GNUNET_MESSAGE_TYPE_HELLO, 0},
1328     {&clients_handle_send, NULL,
1329      GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
1330     {&clients_handle_request_connect, NULL,
1331      GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
1332      sizeof (struct TransportRequestConnectMessage)},
1333     {&clients_handle_address_to_string, NULL,
1334      GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
1335     {&clients_handle_monitor_peers, NULL,
1336      GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST,
1337      sizeof (struct PeerMonitorMessage)},
1338     {&clients_handle_monitor_validation, NULL,
1339      GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST,
1340      sizeof (struct ValidationMonitorMessage)},
1341     {&GST_blacklist_handle_init, NULL,
1342      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
1343      sizeof (struct GNUNET_MessageHeader)},
1344     {&GST_blacklist_handle_reply, NULL,
1345      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
1346      sizeof (struct BlacklistMessage)},
1347     {&GST_manipulation_set_metric, NULL,
1348      GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC, 0},
1349     {NULL, NULL, 0, 0}
1350   };
1351   peer_nc = GNUNET_SERVER_notification_context_create (server, 0);
1352   val_nc = GNUNET_SERVER_notification_context_create (server, 0);
1353   GNUNET_SERVER_add_handlers (server, handlers);
1354   GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
1355                                    NULL);
1356 }
1357
1358
1359 /**
1360  * Stop processing clients.
1361  */
1362 void
1363 GST_clients_stop ()
1364 {
1365   struct AddressToStringContext *cur;
1366
1367   while (NULL != (cur = a2s_head))
1368   {
1369     GNUNET_SERVER_transmit_context_destroy (cur->tc, GNUNET_NO);
1370     GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
1371     GNUNET_free (cur);
1372   }
1373   if (NULL != peer_nc)
1374   {
1375     GNUNET_SERVER_notification_context_destroy (peer_nc);
1376     peer_nc = NULL;
1377   }
1378   if (NULL != val_nc)
1379   {
1380     GNUNET_SERVER_notification_context_destroy (val_nc);
1381     val_nc = NULL;
1382   }
1383 }
1384
1385
1386 /**
1387  * Broadcast the given message to all of our clients.
1388  *
1389  * @param msg message to broadcast
1390  * @param may_drop #GNUNET_YES if the message can be dropped / is payload
1391  */
1392 void
1393 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
1394 {
1395   struct TransportClient *tc;
1396
1397   for (tc = clients_head; NULL != tc; tc = tc->next)
1398   {
1399     if ((GNUNET_YES == may_drop) && (GNUNET_YES != tc->send_payload))
1400       continue;                 /* skip, this client does not care about payload */
1401     unicast (tc, msg, may_drop);
1402   }
1403 }
1404
1405
1406 /**
1407  * Send the given message to a particular client
1408  *
1409  * @param client target of the message
1410  * @param msg message to transmit
1411  * @param may_drop #GNUNET_YES if the message can be dropped
1412  */
1413 void
1414 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
1415                      const struct GNUNET_MessageHeader *msg, int may_drop)
1416 {
1417   struct TransportClient *tc;
1418
1419   tc = lookup_client (client);
1420   if (NULL == tc)
1421     return;                     /* client got disconnected in the meantime, drop message */
1422   unicast (tc, msg, may_drop);
1423 }
1424
1425
1426 /**
1427  * Broadcast the new active address to all clients monitoring the peer.
1428  *
1429  * @param peer peer this update is about (never NULL)
1430  * @param address address, NULL on disconnect
1431  * @param state the current state of the peer
1432  * @param state_timeout the time out for the state
1433  */
1434 void
1435 GST_clients_broadcast_peer_notification (const struct GNUNET_PeerIdentity *peer,
1436     const struct GNUNET_HELLO_Address *address,
1437     enum GNUNET_TRANSPORT_PeerState state,
1438     struct GNUNET_TIME_Absolute state_timeout)
1439 {
1440   struct PeerIterateResponseMessage *msg;
1441   struct MonitoringClient *mc;
1442   static struct GNUNET_PeerIdentity all_zeros;
1443   msg = compose_address_iterate_response_message (peer, address);
1444   msg->state = htonl (state);
1445   msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
1446   mc = peer_monitoring_clients_head;
1447   while (mc != NULL)
1448   {
1449     if ((0 == memcmp (&mc->peer, &all_zeros,
1450                       sizeof (struct GNUNET_PeerIdentity))) ||
1451         (0 == memcmp (&mc->peer, peer,
1452                       sizeof (struct GNUNET_PeerIdentity))))
1453     {
1454       GNUNET_SERVER_notification_context_unicast (peer_nc, mc->client,
1455                                                   &msg->header, GNUNET_NO);
1456     }
1457
1458     mc = mc->next;
1459   }
1460   GNUNET_free (msg);
1461 }
1462
1463 /**
1464  * Broadcast the new validation changes to all clients monitoring the peer.
1465  *
1466  * @param peer peer this update is about (never NULL)
1467  * @param address address, NULL on disconnect
1468  * @param last_validation point in time when last validation was performed
1469  * @param valid_until point in time how long address is valid
1470  * @param next_validation point in time when next validation will be performed
1471  * @param state state of validation notification
1472  */
1473 void
1474 GST_clients_broadcast_validation_notification (
1475     const struct GNUNET_PeerIdentity *peer,
1476     const struct GNUNET_HELLO_Address *address,
1477     struct GNUNET_TIME_Absolute last_validation,
1478     struct GNUNET_TIME_Absolute valid_until,
1479     struct GNUNET_TIME_Absolute next_validation,
1480     enum GNUNET_TRANSPORT_ValidationState state)
1481 {
1482   struct ValidationIterateResponseMessage *msg;
1483   struct MonitoringClient *mc;
1484   static struct GNUNET_PeerIdentity all_zeros;
1485
1486   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1487       "Sending information about for validation entry for peer `%s' using address `%s'\n",
1488       GNUNET_i2s(peer), (address != NULL) ? GST_plugins_a2s (address) : "<none>");
1489
1490   msg = compose_validation_iterate_response_message (peer, address);
1491   msg->last_validation = GNUNET_TIME_absolute_hton(last_validation);
1492   msg->valid_until = GNUNET_TIME_absolute_hton(valid_until);
1493   msg->next_validation = GNUNET_TIME_absolute_hton(next_validation);
1494   msg->state = htonl ((uint32_t) state);
1495   mc = val_monitoring_clients_head;
1496   while (mc != NULL)
1497   {
1498     if ((0 == memcmp (&mc->peer, &all_zeros,
1499                       sizeof (struct GNUNET_PeerIdentity))) ||
1500         (0 == memcmp (&mc->peer, peer,
1501                       sizeof (struct GNUNET_PeerIdentity))))
1502     {
1503       GNUNET_SERVER_notification_context_unicast (val_nc, mc->client,
1504                                                   &msg->header, GNUNET_NO);
1505
1506     }
1507     mc = mc->next;
1508   }
1509   GNUNET_free (msg);
1510 }
1511
1512
1513 /* end of file gnunet-service-transport_clients.c */