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