ca686057d611d1ed391c2fe13520670ff34693b3
[oweals/gnunet.git] / src / transport / gnunet-service-transport_clients.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 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.h"
34 #include "transport.h"
35
36 /**
37  * How many messages can we have pending for a given client process
38  * before we start to drop incoming messages?  We typically should
39  * have only one client and so this would be the primary buffer for
40   * messages, so the number should be chosen rather generously.
41  *
42  * The expectation here is that most of the time the queue is large
43  * enough so that a drop is virtually never required.  Note that
44  * this value must be about as large as 'TOTAL_MSGS' in the
45  * 'test_transport_api_reliability.c', otherwise that testcase may
46  * fail.
47  */
48 #define MAX_PENDING (128 * 1024)
49
50
51 /**
52  * Linked list of messages to be transmitted to the client.  Each
53  * entry is followed by the actual message.
54  */
55 struct ClientMessageQueueEntry
56 {
57   /**
58    * This is a doubly-linked list.
59    */
60   struct ClientMessageQueueEntry *next;
61
62   /**
63    * This is a doubly-linked list.
64    */
65   struct ClientMessageQueueEntry *prev;
66 };
67
68
69 /**
70  * Client connected to the transport service.
71  */
72 struct TransportClient
73 {
74
75   /**
76    * This is a doubly-linked list.
77    */
78   struct TransportClient *next;
79
80   /**
81    * This is a doubly-linked list.
82    */
83   struct TransportClient *prev;
84
85   /**
86    * Handle to the client.
87    */
88   struct GNUNET_SERVER_Client *client;
89
90   /**
91    * Linked list of messages yet to be transmitted to
92    * the client.
93    */
94   struct ClientMessageQueueEntry *message_queue_head;
95
96   /**
97    * Tail of linked list of messages yet to be transmitted to the
98    * client.
99    */
100   struct ClientMessageQueueEntry *message_queue_tail;
101
102   /**
103    * Current transmit request handle.
104    */
105   struct GNUNET_CONNECTION_TransmitHandle *th;
106
107   /**
108    * Length of the list of messages pending for this client.
109    */
110   unsigned int message_count;
111
112 };
113
114
115 /**
116  * Head of linked list of all clients to this service.
117  */
118 static struct TransportClient *clients_head;
119
120 /**
121  * Tail of linked list of all clients to this service.
122  */
123 static struct TransportClient *clients_tail;
124
125
126 /**
127  * Find the internal handle associated with the given client handle
128  *
129  * @param client server's client handle to look up
130  * @return internal client handle
131  */
132 static struct TransportClient *
133 lookup_client (struct GNUNET_SERVER_Client *client)
134 {
135   struct TransportClient *tc;
136
137   tc = clients_head; 
138   while (tc != NULL)
139     {
140       if (tc->client == client)
141         return tc;
142       tc = tc->next;
143     }
144   return NULL;
145 }
146
147
148 /**
149  * Create the internal handle for the given server client handle
150  *
151  * @param client server's client handle to create our internal handle for
152  * @return fresh internal client handle
153  */
154 static struct TransportClient *
155 setup_client (struct GNUNET_SERVER_Client *client)
156 {
157   struct TransportClient *tc;
158   
159   tc = GNUNET_malloc (sizeof (struct TransportClient));
160   tc->client = client;
161   GNUNET_CONTAINER_DLL_insert (clients_head,
162                                clients_tail,
163                                tc);
164   return tc;
165 }
166
167
168 /**
169  * Function called to notify a client about the socket being ready to
170  * queue more data.  "buf" will be NULL and "size" zero if the socket
171  * was closed for writing in the meantime.
172  *
173  * @param cls closure
174  * @param size number of bytes available in buf
175  * @param buf where the callee should write the message
176  * @return number of bytes written to buf
177  */
178 static size_t
179 transmit_to_client_callback (void *cls, 
180                              size_t size, 
181                              void *buf)
182 {
183   struct TransportClient *tc = cls;
184   struct ClientMessageQueueEntry *q;
185   const struct GNUNET_MessageHeader *msg;
186   char *cbuf;
187   uint16_t msize;
188   size_t tsize;
189
190   tc->th = NULL;
191   if (buf == NULL)
192     {
193 #if DEBUG_TRANSPORT 
194       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195                   "Transmission to client failed, closing connection.\n");
196 #endif
197       return 0;
198     }
199   cbuf = buf;
200   tsize = 0;
201   while (NULL != (q = tc->message_queue_head))
202     {
203       msg = (const struct GNUNET_MessageHeader *) &q[1];
204       msize = ntohs (msg->size);
205       if (msize + tsize > size)
206         break;
207 #if DEBUG_TRANSPORT
208       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209                   "Transmitting message of type %u to client.\n",
210                   ntohs (msg->type));
211 #endif
212       GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
213                                    tc->message_queue_tail,
214                                    q);
215       tc->message_count--;
216       memcpy (&cbuf[tsize], 
217               msg, 
218               msize);
219       GNUNET_free (q);
220       tsize += msize;
221     }
222   if (NULL != q)
223     {
224       GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
225       tc->th = GNUNET_SERVER_notify_transmit_ready (tc->client,
226                                                     msize,
227                                                     GNUNET_TIME_UNIT_FOREVER_REL,
228                                                     &transmit_to_client_callback,
229                                                     tc);
230       GNUNET_assert (tc->th != NULL);
231     }
232   return tsize;
233 }
234
235
236 /**
237  * Queue the given message for transmission to the given client
238  *
239  * @param client target of the message
240  * @param msg message to transmit
241  * @param may_drop GNUNET_YES if the message can be dropped
242  */
243 static void
244 unicast (struct TransportClient *tc,
245          const struct GNUNET_MessageHeader *msg,
246          int may_drop)
247 {
248   struct ClientMessageQueueEntry *q;
249   uint16_t msize;
250
251   if ( (tc->message_count >= MAX_PENDING) && 
252        (GNUNET_YES == may_drop) )
253     {
254       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
255                   _("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
256                   ntohs (msg->type),
257                   ntohs (msg->size),
258                   tc->message_count,
259                   MAX_PENDING);
260       GNUNET_STATISTICS_update (GST_stats,
261                                 gettext_noop ("# messages dropped due to slow client"),
262                                 1,
263                                 GNUNET_NO);
264       return;
265     }
266   msize = ntohs (msg->size);
267   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
268   q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
269   memcpy (&q[1], msg, msize);
270   GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
271                                     tc->message_queue_tail,
272                                     q);
273   tc->message_count++;
274   if (tc->th != NULL)
275     return;
276   tc->th = GNUNET_SERVER_notify_transmit_ready (tc->client,
277                                                 msize,
278                                                 GNUNET_TIME_UNIT_FOREVER_REL,
279                                                 &transmit_to_client_callback,
280                                                 tc);
281   GNUNET_assert (tc->th != NULL);    
282 }
283
284
285 /**
286  * Called whenever a client is disconnected.  Frees our
287  * resources associated with that client.
288  *
289  * @param cls closure
290  * @param client identification of the client
291  */
292 static void
293 client_disconnect_notification (void *cls,
294                                 struct GNUNET_SERVER_Client *client)
295 {
296   struct TransportClient *tc;
297   struct ClientMessageQueueEntry *mqe;
298
299   if (client == NULL)
300     return;
301   tc = lookup_client (client);
302   if (tc == NULL)
303     return;
304 #if DEBUG_TRANSPORT
305   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
306               "Client disconnected, cleaning up.\n");
307 #endif
308   while (NULL != (mqe = tc->message_queue_head))
309     {
310       GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
311                                    tc->message_queue_tail,
312                                    mqe);
313       tc->message_count--;
314       GNUNET_free (mqe);
315     }
316   GNUNET_CONTAINER_DLL_remove (clients_head,
317                                clients_tail,
318                                tc);
319   if (tc->th != NULL)
320     {
321       GNUNET_CONNECTION_notify_transmit_ready_cancel (tc->th);
322       tc->th = NULL;
323     }
324   GNUNET_break (0 == tc->message_count);
325   GNUNET_free (tc);
326 }
327
328
329 /**
330  * Function called for each of our connected neighbours.  Notify the
331  * client about the existing neighbour.
332  *
333  * @param cls the 'struct TransportClient' to notify
334  * @param peer identity of the neighbour
335  * @param ats performance data
336  * @param ats_count number of entries in ats (excluding 0-termination)
337  */
338 static void
339 notify_client_about_neighbour (void *cls,
340                                const struct GNUNET_PeerIdentity *peer,
341                                const struct GNUNET_TRANSPORT_ATS_Information *ats,
342                                uint32_t ats_count)
343 {
344   struct TransportClient *tc = cls;
345   struct ConnectInfoMessage *cim;
346   size_t size;
347
348   size  = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
349   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
350   cim = GNUNET_malloc (size);
351   cim->header.size = htons (size);
352   cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
353   cim->ats_count = htonl(ats_count);
354   cim->id = *peer;
355   memcpy (&cim->ats,
356           ats, 
357           ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
358   unicast (tc, &cim->header, GNUNET_NO);
359   GNUNET_free (cim);
360 }
361
362
363 /**
364  * Initialize a normal client.  We got a start message from this
365  * client, add him to the list of clients for broadcasting of inbound
366  * messages.
367  *
368  * @param cls unused
369  * @param client the client
370  * @param message the start message that was sent
371  */
372 static void
373 clients_handle_start (void *cls,
374                       struct GNUNET_SERVER_Client *client,
375                       const struct GNUNET_MessageHeader *message)
376 {
377   const struct StartMessage *start;
378   struct TransportClient *tc;
379
380   tc = lookup_client (client);
381   if (tc != NULL)
382     {
383       /* got 'start' twice from the same client, not allowed */
384       GNUNET_break (0);
385       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
386       return;
387     }
388   start = (const struct StartMessage*) message;
389   if ( (GNUNET_NO != ntohl (start->do_check)) &&
390        (0 != memcmp (&start->self,
391                      &GST_my_identity,
392                      sizeof (struct GNUNET_PeerIdentity))) )
393     {
394       /* client thinks this is a different peer, reject */
395       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
396                   _("Rejecting control connection from peer `%s', which is not me!\n"),
397                   GNUNET_i2s (&start->self));
398       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
399       return;
400     }  
401   tc = setup_client (client);
402   unicast (tc, GST_hello_get(), GNUNET_NO);
403   GST_neighbours_iterate (&notify_client_about_neighbour, tc);
404   GNUNET_SERVER_receive_done (client, GNUNET_OK);
405 }
406
407
408 /**
409  * Client sent us a HELLO.  Process the request.
410  *
411  * @param cls unused
412  * @param client the client
413  * @param message the HELLO message
414  */
415 static void
416 clients_handle_hello (void *cls,
417                       struct GNUNET_SERVER_Client *client,
418                       const struct GNUNET_MessageHeader *message)
419 {
420   GST_validation_handle_hello (message);
421   GNUNET_SERVER_receive_done (client, GNUNET_OK);
422 }
423
424
425 /**
426  * Closure for 'handle_send_transmit_continuation'
427  */
428 struct SendTransmitContinuationContext
429 {
430   /**
431    * Client that made the request.
432    */
433   struct GNUNET_SERVER_Client *client;
434
435   /**
436    * Peer that was the target.
437    */
438   struct GNUNET_PeerIdentity target;
439 };
440
441
442 /**
443  * Function called after the transmission is done.  Notify the client that it is
444  * OK to send the next message.
445  *
446  * @param cls closure
447  * @param success GNUNET_OK on success, GNUNET_NO on failure, GNUNET_SYSERR if we're not connected
448  */
449 static void
450 handle_send_transmit_continuation (void *cls,
451                                    int success)
452 {
453   struct SendTransmitContinuationContext *stcc = cls;
454   struct SendOkMessage send_ok_msg;
455
456   send_ok_msg.header.size = htons (sizeof (send_ok_msg));
457   send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
458   send_ok_msg.success = htonl (success);
459   send_ok_msg.latency = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
460   send_ok_msg.peer = stcc->target;
461   GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO); 
462   GNUNET_SERVER_client_drop (stcc->client);
463   GNUNET_free (stcc);
464 }
465
466
467 /**
468  * Client asked for transmission to a peer.  Process the request.
469  *
470  * @param cls unused
471  * @param client the client
472  * @param message the send message that was sent
473  */
474 static void
475 clients_handle_send (void *cls,
476                      struct GNUNET_SERVER_Client *client,
477                      const struct GNUNET_MessageHeader *message)
478 {
479   const struct OutboundMessage *obm;
480   const struct GNUNET_MessageHeader *obmm;
481   struct SendTransmitContinuationContext *stcc;
482   uint16_t size;
483   uint16_t msize;
484
485   size = ntohs (message->size);
486   if (size < sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
487     {
488       GNUNET_break (0);
489       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
490       return;
491     }
492   obm = (const struct OutboundMessage *) message;
493   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
494   msize = size - sizeof (struct OutboundMessage);
495   if (msize < sizeof (struct GNUNET_MessageHeader))
496     {
497       GNUNET_break (0);
498       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
499       return;
500     }
501   GNUNET_STATISTICS_update (GST_stats,
502                             gettext_noop ("# bytes payload received for other peers"),
503                             msize,
504                             GNUNET_NO);
505 #if DEBUG_TRANSPORT
506   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507               "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
508               "SEND", 
509               GNUNET_i2s (&obm->peer),
510               ntohs (obmm->type),
511               msize);
512 #endif
513   if (GNUNET_NO == 
514       GST_neighbours_test_connected (&obm->peer))
515     {
516       /* not connected, not allowed to send; can happen due to asynchronous operations */
517       GNUNET_STATISTICS_update (GST_stats,
518                                 gettext_noop ("# bytes payload dropped (other peer was not connected)"),
519                                 msize,
520                                 GNUNET_NO);
521       GNUNET_SERVER_receive_done (client, GNUNET_OK);
522       return;      
523     }
524   GNUNET_SERVER_receive_done (client, GNUNET_OK);
525   stcc = GNUNET_malloc (sizeof (struct SendTransmitContinuationContext));
526   stcc->target = obm->peer;
527   stcc->client = client;
528   GNUNET_SERVER_client_keep (client);
529   GST_neighbours_send (&obm->peer,
530                        obmm, msize,
531                        GNUNET_TIME_relative_ntoh (obm->timeout),
532                        &handle_send_transmit_continuation,
533                        stcc);
534 }
535                         
536
537 /**
538  * Try to initiate a connection to the given peer if the blacklist
539  * allowed it.
540  *
541  * @param cls closure (unused, NULL)
542  * @param peer identity of peer that was tested
543  * @param result GNUNET_OK if the connection is allowed,
544  *               GNUNET_NO if not
545  */
546 static void
547 try_connect_if_allowed (void *cls,
548                         const struct GNUNET_PeerIdentity *peer,
549                         int result)
550 {
551   if (GNUNET_OK != result)
552     return; /* not allowed */
553   GST_neighbours_try_connect (peer);
554 }
555
556
557 /**
558  * Handle request connect message
559  *
560  * @param cls closure (always NULL)
561  * @param client identification of the client
562  * @param message the actual message
563  */
564 static void
565 clients_handle_request_connect (void *cls,
566                                 struct GNUNET_SERVER_Client *client,
567                                 const struct GNUNET_MessageHeader *message)
568 {
569   const struct TransportRequestConnectMessage *trcm =
570     (const struct TransportRequestConnectMessage *) message;
571
572   GNUNET_STATISTICS_update (GST_stats,
573                             gettext_noop ("# REQUEST CONNECT messages received"),
574                             1,
575                             GNUNET_NO);
576 #if DEBUG_TRANSPORT
577   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 
578              "Received a request connect message for peer `%s'\n", 
579              GNUNET_i2s(&trcm->peer));
580 #endif
581   (void) GST_blacklist_test_allowed (&trcm->peer, NULL,
582                                      &try_connect_if_allowed, NULL);
583   GNUNET_SERVER_receive_done (client, GNUNET_OK);
584 }
585
586
587 /**
588  * Client asked for a quota change for a particular peer.  Process the request.
589  *
590  * @param cls unused
591  * @param client the client
592  * @param message the quota changing message
593  */
594 static void
595 clients_handle_set_quota (void *cls,
596                           struct GNUNET_SERVER_Client *client,
597                           const struct GNUNET_MessageHeader *message)
598 {
599   const struct QuotaSetMessage *qsm;
600
601   qsm = (const struct QuotaSetMessage *) message;
602   GNUNET_STATISTICS_update (GST_stats,
603                             gettext_noop ("# SET QUOTA messages received"),
604                             1,
605                             GNUNET_NO);
606 #if DEBUG_TRANSPORT 
607   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
608               "Received `%s' request (new quota %u) from client for peer `%4s'\n",
609               "SET_QUOTA",
610               (unsigned int) ntohl (qsm->quota.value__),
611               GNUNET_i2s (&qsm->peer));
612 #endif
613   GST_neighbours_set_incoming_quota (&qsm->peer,
614                                      qsm->quota);
615   GNUNET_SERVER_receive_done (client, GNUNET_OK);
616 }
617
618
619 /**
620  * Take the given address and append it to the set of results sent back to
621  * the client.
622  *
623  * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
624  * @param address the resolved name, NULL to indicate the last response
625  */
626 static void
627 transmit_address_to_client (void *cls, 
628                             const char *address)
629 {
630   struct GNUNET_SERVER_TransmitContext *tc = cls;
631
632   if (NULL == address)
633     {
634       GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
635                                                   GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
636       GNUNET_SERVER_transmit_context_run (tc,
637                                           GNUNET_TIME_UNIT_FOREVER_REL);
638       return;
639     }
640   GNUNET_SERVER_transmit_context_append_data (tc, 
641                                               address, strlen (address) + 1,
642                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
643 }
644
645
646 /**
647  * Client asked to resolve an address.  Process the request.
648  *
649  * @param cls unused
650  * @param client the client
651  * @param message the resolution request
652  */
653 static void
654 clients_handle_address_lookup (void *cls,
655                                struct GNUNET_SERVER_Client *client,
656                                const struct GNUNET_MessageHeader *message)
657 {
658   const struct AddressLookupMessage *alum;
659   struct GNUNET_TRANSPORT_PluginFunctions *papi;
660   const char *plugin_name;
661   const char *address;
662   uint32_t address_len;
663   uint16_t size;
664   struct GNUNET_SERVER_TransmitContext *tc;
665   struct GNUNET_TIME_Relative rtimeout;
666   int32_t numeric;
667
668   size = ntohs (message->size);
669   if (size < sizeof (struct AddressLookupMessage))
670     {
671       GNUNET_break (0);
672       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
673       return;
674     }
675   alum = (const struct AddressLookupMessage *) message;
676   address_len = ntohl (alum->addrlen);
677   if (size <= sizeof (struct AddressLookupMessage) + address_len)
678     {
679       GNUNET_break (0);
680       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
681       return;
682     }
683   address = (const char *) &alum[1];
684   plugin_name = (const char *) &address[address_len];
685   if (plugin_name
686       [size - sizeof (struct AddressLookupMessage) - address_len - 1] != '\0')
687     {
688       GNUNET_break (0);
689       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
690       return;
691     }
692   rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
693   numeric = ntohl (alum->numeric_only);
694   tc = GNUNET_SERVER_transmit_context_create (client);
695   papi = GST_plugins_find (plugin_name);
696   if (NULL == papi)
697     {
698       GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
699                                                   GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
700       GNUNET_SERVER_transmit_context_run (tc, rtimeout);
701       return;
702     }
703   GNUNET_SERVER_disable_receive_done_warning (client);
704   papi->address_pretty_printer (papi->cls,
705                                 plugin_name,
706                                 address, address_len,
707                                 numeric,
708                                 rtimeout,
709                                 &transmit_address_to_client, tc);
710 }
711
712
713 /**
714  * Send an address to the client.
715  *
716  * @param cls our 'struct GNUNET_SERVER_TransmitContext' (for sending)
717  * @param public_key public key for the peer, never NULL
718  * @param target peer this change is about, never NULL
719  * @param valid_until until what time do we consider the address valid?
720  * @param validation_block  is FOREVER if the address is for an unsupported plugin (from PEERINFO)
721  *                          is ZERO if the address is considered valid (no validation needed)
722  *                          is a time in the future if we're currently denying re-validation
723  * @param plugin_name name of the plugin
724  * @param plugin_address binary address
725  * @param plugin_address_len length of address
726  */
727 static void
728 send_address_to_client (void *cls,
729                         const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
730                         const struct GNUNET_PeerIdentity *target,
731                         struct GNUNET_TIME_Absolute valid_until,
732                         struct GNUNET_TIME_Absolute validation_block,
733                         const char *plugin_name,
734                         const void *plugin_address,
735                         size_t plugin_address_len)
736 {
737   struct GNUNET_SERVER_TransmitContext *tc = cls;
738   char *addr_buf;
739
740   /* FIXME: move to a binary format!!! */
741   GNUNET_asprintf (&addr_buf, "%s --- %s, %s",
742                    GST_plugins_a2s (plugin_name,
743                                     plugin_address,
744                                     plugin_address_len),
745                    (GNUNET_YES == GST_neighbours_test_connected (target))
746                    ? "CONNECTED"
747                    : "DISCONNECTED",
748                    (GNUNET_TIME_absolute_get_remaining (valid_until).rel_value > 0)
749                    ? "VALIDATED"
750                    : "UNVALIDATED");
751   transmit_address_to_client (tc, addr_buf);
752   GNUNET_free (addr_buf);
753 }
754
755
756 /**
757  * Client asked to obtain information about a peer's addresses.
758  * Process the request.
759  * FIXME: use better name!
760  *
761  * @param cls unused
762  * @param client the client
763  * @param message the peer address information request
764  */
765 static void
766 clients_handle_peer_address_lookup (void *cls,
767                                     struct GNUNET_SERVER_Client *client,
768                                     const struct GNUNET_MessageHeader *message)
769 {
770   const struct PeerAddressLookupMessage *peer_address_lookup;
771   struct GNUNET_SERVER_TransmitContext *tc;
772
773   peer_address_lookup = (const struct PeerAddressLookupMessage *) message;
774   GNUNET_break (ntohl (peer_address_lookup->reserved) == 0);
775   tc = GNUNET_SERVER_transmit_context_create (client);
776   GST_validation_get_addresses (&peer_address_lookup->peer,
777                                 &send_address_to_client,
778                                 tc);
779   GNUNET_SERVER_transmit_context_append_data (tc,
780                                               NULL, 0,
781                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
782   GNUNET_SERVER_transmit_context_run (tc, 
783                                       GNUNET_TIME_UNIT_FOREVER_REL);
784 }
785
786
787 /**
788  * Output the active address of connected neighbours to the given client.
789  *
790  * @param cls the 'struct GNUNET_SERVER_TransmitContext' for transmission to the client
791  * @param neighbour identity of the neighbour
792  * @param ats performance data
793  * @param ats_count number of entries in ats (excluding 0-termination)
794  */
795 static void
796 output_addresses (void *cls,
797                   const struct GNUNET_PeerIdentity *neighbour,
798                   const struct GNUNET_TRANSPORT_ATS_Information *ats,
799                   uint32_t ats_count)
800 {
801   struct GNUNET_SERVER_TransmitContext *tc = cls;
802   char *addr_buf;
803
804   /* FIXME: move to a binary format!!! */
805   GNUNET_asprintf (&addr_buf, 
806                    "%s: %s",
807                    GNUNET_i2s(neighbour),
808                    GST_plugins_a2s ("FIXME", NULL, 0));
809   transmit_address_to_client (tc, addr_buf);
810   GNUNET_free (addr_buf);
811 }
812
813
814 /**
815  * Client asked to obtain information about all actively used addresses.
816  * Process the request.  FIXME: use better name!
817  *
818  * @param cls unused
819  * @param client the client
820  * @param message the peer address information request
821  */
822 static void
823 clients_handle_address_iterate (void *cls,
824                                 struct GNUNET_SERVER_Client *client,
825                                 const struct GNUNET_MessageHeader *message)
826
827   struct GNUNET_SERVER_TransmitContext *tc;
828
829   GNUNET_SERVER_disable_receive_done_warning (client);
830   tc = GNUNET_SERVER_transmit_context_create (client);
831   GST_neighbours_iterate (&output_addresses,
832                           tc);
833   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
834                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
835   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
836 }
837
838
839 /**
840  * Start handling requests from clients.
841  *
842  * @param server server used to accept clients from.
843  */
844 void 
845 GST_clients_start (struct GNUNET_SERVER_Handle *server)
846 {
847   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
848     { &clients_handle_start, NULL, 
849       GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
850     { &clients_handle_hello, NULL, 
851       GNUNET_MESSAGE_TYPE_HELLO, 0},
852     { &clients_handle_send,  NULL, 
853       GNUNET_MESSAGE_TYPE_TRANSPORT_SEND , 0},
854     { &clients_handle_request_connect, NULL,  
855       GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT, sizeof (struct TransportRequestConnectMessage)},
856     { &clients_handle_set_quota, NULL, 
857       GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA, sizeof (struct QuotaSetMessage)},
858     { &clients_handle_address_lookup, NULL,
859       GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP , 0},
860     { &clients_handle_peer_address_lookup, NULL, 
861       GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP, sizeof (struct PeerAddressLookupMessage)},
862     { &clients_handle_address_iterate, NULL,
863       GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE, sizeof (struct GNUNET_MessageHeader)},
864     { &GST_blacklist_handle_init, NULL,
865       GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT, sizeof (struct GNUNET_MessageHeader)},
866     { &GST_blacklist_handle_reply, NULL,
867       GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY, sizeof (struct BlacklistMessage)},
868     {NULL, NULL, 0, 0}
869   };
870   GNUNET_SERVER_add_handlers (server, handlers);
871   GNUNET_SERVER_disconnect_notify (server,
872                                    &client_disconnect_notification, NULL);
873 }
874
875
876 /**
877  * Stop processing clients.
878  */
879 void
880 GST_clients_stop ()
881 {
882   /* nothing to do */
883 }
884
885
886 /**
887  * Broadcast the given message to all of our clients.
888  *
889  * @param msg message to broadcast
890  * @param may_drop GNUNET_YES if the message can be dropped
891  */
892 void
893 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg,
894                        int may_drop)
895 {
896   struct TransportClient *tc;
897
898   for (tc = clients_head; tc != NULL; tc = tc->next)
899     unicast (tc, msg, may_drop);
900 }
901
902
903 /**
904  * Send the given message to a particular client
905  *
906  * @param client target of the message
907  * @param msg message to transmit
908  * @param may_drop GNUNET_YES if the message can be dropped
909  */
910 void
911 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
912                      const struct GNUNET_MessageHeader *msg,
913                      int may_drop)
914 {
915   struct TransportClient *tc;
916
917   tc = lookup_client (client);
918   if (NULL == tc)
919     tc = setup_client (client);
920   unicast (tc, msg, may_drop);
921 }
922
923
924 /* end of file gnunet-service-transport_clients.c */