fix for mantis #2008
[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    * Is this client interested in payload messages?
114    */
115   int send_payload;
116 };
117
118
119 /**
120  * Head of linked list of all clients to this service.
121  */
122 static struct TransportClient *clients_head;
123
124 /**
125  * Tail of linked list of all clients to this service.
126  */
127 static struct TransportClient *clients_tail;
128
129 /**
130  * Find the internal handle associated with the given client handle
131  *
132  * @param client server's client handle to look up
133  * @return internal client handle
134  */
135 static struct TransportClient *
136 lookup_client (struct GNUNET_SERVER_Client *client)
137 {
138   struct TransportClient *tc;
139
140   tc = clients_head;
141   while (tc != NULL)
142   {
143     if (tc->client == client)
144       return tc;
145     tc = tc->next;
146   }
147   return NULL;
148 }
149
150
151 /**
152  * Create the internal handle for the given server client handle
153  *
154  * @param client server's client handle to create our internal handle for
155  * @return fresh internal client handle
156  */
157 static struct TransportClient *
158 setup_client (struct GNUNET_SERVER_Client *client)
159 {
160   struct TransportClient *tc;
161
162   GNUNET_assert (lookup_client (client) == NULL);
163   tc = GNUNET_malloc (sizeof (struct TransportClient));
164   tc->client = client;
165
166 #if DEBUG_TRANSPORT
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %X connected\n", tc);
168 #endif
169   return tc;
170 }
171
172
173 /**
174  * Function called to notify a client about the socket being ready to
175  * queue more data.  "buf" will be NULL and "size" zero if the socket
176  * was closed for writing in the meantime.
177  *
178  * @param cls closure
179  * @param size number of bytes available in buf
180  * @param buf where the callee should write the message
181  * @return number of bytes written to buf
182  */
183 static size_t
184 transmit_to_client_callback (void *cls, size_t size, void *buf)
185 {
186   struct TransportClient *tc = cls;
187   struct ClientMessageQueueEntry *q;
188   const struct GNUNET_MessageHeader *msg;
189   char *cbuf;
190   uint16_t msize;
191   size_t tsize;
192
193   tc->th = NULL;
194   if (buf == NULL)
195   {
196 #if DEBUG_TRANSPORT
197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198                 "Transmission to client failed, closing connection.\n");
199 #endif
200     return 0;
201   }
202   cbuf = buf;
203   tsize = 0;
204   while (NULL != (q = tc->message_queue_head))
205   {
206     msg = (const struct GNUNET_MessageHeader *) &q[1];
207     msize = ntohs (msg->size);
208     if (msize + tsize > size)
209       break;
210 #if DEBUG_TRANSPORT
211     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212                 "Transmitting message of type %u to client %X.\n",
213                 ntohs (msg->type), tc);
214 #endif
215     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
216                                  q);
217     tc->message_count--;
218     memcpy (&cbuf[tsize], msg, 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 =
226         GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
227                                              GNUNET_TIME_UNIT_FOREVER_REL,
228                                              &transmit_to_client_callback, 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 tc 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, const struct GNUNET_MessageHeader *msg,
244          int may_drop)
245 {
246   struct ClientMessageQueueEntry *q;
247   uint16_t msize;
248
249   if ((tc->message_count >= MAX_PENDING) && (GNUNET_YES == may_drop))
250   {
251     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252                 _
253                 ("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
254                 ntohs (msg->type), ntohs (msg->size), tc->message_count,
255                 MAX_PENDING);
256     GNUNET_STATISTICS_update (GST_stats,
257                               gettext_noop
258                               ("# messages dropped due to slow client"), 1,
259                               GNUNET_NO);
260     return;
261   }
262   msize = ntohs (msg->size);
263   GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
264   q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
265   memcpy (&q[1], msg, msize);
266   GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
267                                     tc->message_queue_tail, q);
268   tc->message_count++;
269   if (tc->th != NULL)
270     return;
271   tc->th =
272       GNUNET_SERVER_notify_transmit_ready (tc->client, msize,
273                                            GNUNET_TIME_UNIT_FOREVER_REL,
274                                            &transmit_to_client_callback, tc);
275   GNUNET_assert (tc->th != NULL);
276 }
277
278
279 /**
280  * Called whenever a client is disconnected.  Frees our
281  * resources associated with that client.
282  *
283  * @param cls closure
284  * @param client identification of the client
285  */
286 static void
287 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
288 {
289   struct TransportClient *tc;
290   struct ClientMessageQueueEntry *mqe;
291
292   if (client == NULL)
293     return;
294   tc = lookup_client (client);
295   if (tc == NULL)
296     return;
297 #if DEBUG_TRANSPORT
298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
299               "Client %X disconnected, cleaning up.\n", tc);
300 #endif
301   while (NULL != (mqe = tc->message_queue_head))
302   {
303     GNUNET_CONTAINER_DLL_remove (tc->message_queue_head, tc->message_queue_tail,
304                                  mqe);
305     tc->message_count--;
306     GNUNET_free (mqe);
307   }
308   GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
309   if (tc->th != NULL)
310   {
311     GNUNET_CONNECTION_notify_transmit_ready_cancel (tc->th);
312     tc->th = NULL;
313   }
314   GNUNET_break (0 == tc->message_count);
315   GNUNET_free (tc);
316 }
317
318
319 /**
320  * Function called for each of our connected neighbours.  Notify the
321  * client about the existing neighbour.
322  *
323  * @param cls the 'struct TransportClient' to notify
324  * @param peer identity of the neighbour
325  * @param ats performance data
326  * @param ats_count number of entries in ats (excluding 0-termination)
327  * @param address the address
328  */
329 static void
330 notify_client_about_neighbour (void *cls,
331                                const struct GNUNET_PeerIdentity *peer,
332                                const struct GNUNET_ATS_Information *ats,
333                                uint32_t ats_count,
334                                const struct GNUNET_HELLO_Address *address)
335 {
336   struct TransportClient *tc = cls;
337   struct ConnectInfoMessage *cim;
338   struct GNUNET_ATS_Information *ap;
339   size_t size =
340       sizeof (struct ConnectInfoMessage) +
341       ats_count * sizeof (struct GNUNET_ATS_Information);
342   char buf[size];
343
344   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
345   cim = (struct ConnectInfoMessage *) buf;
346   cim->header.size = htons (size);
347   cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
348   cim->ats_count = htonl (ats_count);
349   cim->id = *peer;
350   ap = (struct GNUNET_ATS_Information *) &cim[1];
351   memcpy (ap, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
352   unicast (tc, &cim->header, GNUNET_NO);
353 }
354
355
356 /**
357  * Initialize a normal client.  We got a start message from this
358  * client, add him to the list of clients for broadcasting of inbound
359  * messages.
360  *
361  * @param cls unused
362  * @param client the client
363  * @param message the start message that was sent
364  */
365 static void
366 clients_handle_start (void *cls, struct GNUNET_SERVER_Client *client,
367                       const struct GNUNET_MessageHeader *message)
368 {
369   const struct StartMessage *start;
370   struct TransportClient *tc;
371   uint32_t options;
372
373   tc = lookup_client (client);
374
375 #if DEBUG_TRANSPORT
376   if (tc != NULL)
377     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
378                 "Client %X sent START\n", tc);
379   else
380     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
381                 "Client %X sent START\n", tc);
382 #endif
383   if (tc != NULL)
384   {
385     /* got 'start' twice from the same client, not allowed */
386 #if DEBUG_TRANSPORT
387     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
388                 "TransportClient %X ServerClient %X  sent multiple START messages\n",
389                 tc, tc->client);
390 #endif
391     GNUNET_break (0);
392     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
393     return;
394   }
395   start = (const struct StartMessage *) message;
396   options = ntohl (start->options);
397   if ((0 != (1 & options)) &&
398       (0 !=
399        memcmp (&start->self, &GST_my_identity,
400                sizeof (struct GNUNET_PeerIdentity))))
401   {
402     /* client thinks this is a different peer, reject */
403     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404                 _
405                 ("Rejecting control connection from peer `%s', which is not me!\n"),
406                 GNUNET_i2s (&start->self));
407     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
408     return;
409   }
410   tc = setup_client (client);
411   tc->send_payload = (0 != (2 & options));
412   unicast (tc, GST_hello_get (), GNUNET_NO);
413   GST_neighbours_iterate (&notify_client_about_neighbour, tc);
414   GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
415   GNUNET_SERVER_receive_done (client, GNUNET_OK);
416 }
417
418
419 /**
420  * Client sent us a HELLO.  Process the request.
421  *
422  * @param cls unused
423  * @param client the client
424  * @param message the HELLO message
425  */
426 static void
427 clients_handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
428                       const struct GNUNET_MessageHeader *message)
429 {
430   GST_validation_handle_hello (message);
431   GNUNET_SERVER_receive_done (client, GNUNET_OK);
432 }
433
434
435 /**
436  * Closure for 'handle_send_transmit_continuation'
437  */
438 struct SendTransmitContinuationContext
439 {
440   /**
441    * Client that made the request.
442    */
443   struct GNUNET_SERVER_Client *client;
444
445   /**
446    * Peer that was the target.
447    */
448   struct GNUNET_PeerIdentity target;
449 };
450
451
452 /**
453  * Function called after the transmission is done.  Notify the client that it is
454  * OK to send the next message.
455  *
456  * @param cls closure
457  * @param success GNUNET_OK on success, GNUNET_NO on failure, GNUNET_SYSERR if we're not connected
458  */
459 static void
460 handle_send_transmit_continuation (void *cls, int success)
461 {
462   struct SendTransmitContinuationContext *stcc = cls;
463   struct SendOkMessage send_ok_msg;
464
465   send_ok_msg.header.size = htons (sizeof (send_ok_msg));
466   send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
467   send_ok_msg.success = htonl (success);
468   send_ok_msg.latency =
469       GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_FOREVER_REL);
470   send_ok_msg.peer = stcc->target;
471   GST_clients_unicast (stcc->client, &send_ok_msg.header, GNUNET_NO);
472   GNUNET_SERVER_client_drop (stcc->client);
473   GNUNET_free (stcc);
474 }
475
476
477 /**
478  * Client asked for transmission to a peer.  Process the request.
479  *
480  * @param cls unused
481  * @param client the client
482  * @param message the send message that was sent
483  */
484 static void
485 clients_handle_send (void *cls, struct GNUNET_SERVER_Client *client,
486                      const struct GNUNET_MessageHeader *message)
487 {
488   const struct OutboundMessage *obm;
489   const struct GNUNET_MessageHeader *obmm;
490   struct SendTransmitContinuationContext *stcc;
491   uint16_t size;
492   uint16_t msize;
493   struct TransportClient *tc;
494
495   tc = lookup_client (client);
496   if (NULL == tc)
497   {
498     /* client asked for transmission before 'START' */
499     GNUNET_break (0);
500     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
501     return;
502   }
503
504   size = ntohs (message->size);
505   if (size <
506       sizeof (struct OutboundMessage) + sizeof (struct GNUNET_MessageHeader))
507   {
508     GNUNET_break (0);
509     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
510     return;
511   }
512   obm = (const struct OutboundMessage *) message;
513   obmm = (const struct GNUNET_MessageHeader *) &obm[1];
514   msize = size - sizeof (struct OutboundMessage);
515   if (msize < sizeof (struct GNUNET_MessageHeader))
516   {
517     GNUNET_break (0);
518     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
519     return;
520   }
521   GNUNET_STATISTICS_update (GST_stats,
522                             gettext_noop
523                             ("# bytes payload received for other peers"), msize,
524                             GNUNET_NO);
525 #if DEBUG_TRANSPORT
526   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527               "Received `%s' request from client with target `%4s' and first message of type %u and total size %u\n",
528               "SEND", GNUNET_i2s (&obm->peer), ntohs (obmm->type), msize);
529 #endif
530   if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
531   {
532     /* not connected, not allowed to send; can happen due to asynchronous operations */
533 #if DEBUG_TRANSPORT
534     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535                 "Could not send message to peer `%s': not connected\n",
536                 GNUNET_i2s (&obm->peer));
537 #endif
538     GNUNET_STATISTICS_update (GST_stats,
539                               gettext_noop
540                               ("# bytes payload dropped (other peer was not connected)"),
541                               msize, GNUNET_NO);
542     GNUNET_SERVER_receive_done (client, GNUNET_OK);
543     return;
544   }
545   GNUNET_SERVER_receive_done (client, GNUNET_OK);
546   stcc = GNUNET_malloc (sizeof (struct SendTransmitContinuationContext));
547   stcc->target = obm->peer;
548   stcc->client = client;
549   GNUNET_SERVER_client_keep (client);
550   GST_neighbours_send (&obm->peer, obmm, msize,
551                        GNUNET_TIME_relative_ntoh (obm->timeout),
552                        &handle_send_transmit_continuation, stcc);
553 }
554
555
556 /**
557  * Try to initiate a connection to the given peer if the blacklist
558  * allowed it.
559  *
560  * @param cls closure (unused, NULL)
561  * @param peer identity of peer that was tested
562  * @param result GNUNET_OK if the connection is allowed,
563  *               GNUNET_NO if not
564  */
565 static void
566 try_connect_if_allowed (void *cls, const struct GNUNET_PeerIdentity *peer,
567                         int result)
568 {
569   if (GNUNET_OK != result)
570     return;                     /* not allowed */
571   GST_neighbours_try_connect (peer);
572 }
573
574
575 /**
576  * Handle request connect message
577  *
578  * @param cls closure (always NULL)
579  * @param client identification of the client
580  * @param message the actual message
581  */
582 static void
583 clients_handle_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
584                                 const struct GNUNET_MessageHeader *message)
585 {
586   const struct TransportRequestConnectMessage *trcm =
587       (const struct TransportRequestConnectMessage *) message;
588
589   GNUNET_STATISTICS_update (GST_stats,
590                             gettext_noop
591                             ("# REQUEST CONNECT messages received"), 1,
592                             GNUNET_NO);
593 #if DEBUG_TRANSPORT
594   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595               "Received a request connect message for peer `%s'\n",
596               GNUNET_i2s (&trcm->peer));
597 #endif
598   (void) GST_blacklist_test_allowed (&trcm->peer, NULL, &try_connect_if_allowed,
599                                      NULL);
600   GNUNET_SERVER_receive_done (client, GNUNET_OK);
601 }
602
603
604 /**
605  * Take the given address and append it to the set of results sent back to
606  * the client.
607  *
608  * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
609  * @param buf text to transmit
610  */
611 static void
612 transmit_address_to_client (void *cls, const char *buf)
613 {
614   struct GNUNET_SERVER_TransmitContext *tc = cls;
615
616   if (NULL == buf)
617   {
618     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
619                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
620     GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
621     return;
622   }
623   GNUNET_SERVER_transmit_context_append_data (tc, buf, strlen (buf) + 1,
624                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
625 }
626
627
628 /**
629  * Client asked to resolve an address.  Process the request.
630  *
631  * @param cls unused
632  * @param client the client
633  * @param message the resolution request
634  */
635 static void
636 clients_handle_address_to_string (void *cls,
637                                   struct GNUNET_SERVER_Client *client,
638                                   const struct GNUNET_MessageHeader *message)
639 {
640   const struct AddressLookupMessage *alum;
641   struct GNUNET_TRANSPORT_PluginFunctions *papi;
642   const char *plugin_name;
643   const char *address;
644   uint32_t address_len;
645   uint16_t size;
646   struct GNUNET_SERVER_TransmitContext *tc;
647   struct GNUNET_TIME_Relative rtimeout;
648   int32_t numeric;
649
650   size = ntohs (message->size);
651   if (size < sizeof (struct AddressLookupMessage))
652   {
653     GNUNET_break (0);
654     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
655     return;
656   }
657   alum = (const struct AddressLookupMessage *) message;
658   address_len = ntohs (alum->addrlen);
659   if (size <= sizeof (struct AddressLookupMessage) + address_len)
660   {
661     GNUNET_break (0);
662     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
663     return;
664   }
665   address = (const char *) &alum[1];
666   plugin_name = (const char *) &address[address_len];
667   if (plugin_name[size - sizeof (struct AddressLookupMessage) - address_len - 1]
668       != '\0')
669   {
670     GNUNET_break (0);
671     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
672     return;
673   }
674   rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
675   numeric = ntohs (alum->numeric_only);
676   tc = GNUNET_SERVER_transmit_context_create (client);
677   papi = GST_plugins_find (plugin_name);
678   if (NULL == papi)
679   {
680     GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
681                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
682     GNUNET_SERVER_transmit_context_run (tc, rtimeout);
683     return;
684   }
685   GNUNET_SERVER_disable_receive_done_warning (client);
686   papi->address_pretty_printer (papi->cls, plugin_name, address, address_len,
687                                 numeric, rtimeout, &transmit_address_to_client,
688                                 tc);
689 }
690
691
692 /**
693  * Output the active address of connected neighbours to the given client.
694  *
695  * @param cls the 'struct GNUNET_SERVER_TransmitContext' for transmission to the client
696  * @param peer identity of the neighbour
697  * @param ats performance data
698  * @param ats_count number of entries in ats (excluding 0-termination)
699  * @param address the address
700  */
701 static void
702 output_address (void *cls, const struct GNUNET_PeerIdentity *peer,
703                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count,
704                 const struct GNUNET_HELLO_Address *address)
705 {
706   struct GNUNET_SERVER_TransmitContext *tc = cls;
707   struct AddressIterateResponseMessage *msg;
708   size_t size;
709   size_t tlen;
710   size_t alen;
711   char *addr;
712
713   tlen = strlen (address->transport_name) + 1;
714   alen = address->address_length;
715   size = (sizeof (struct AddressIterateResponseMessage) + alen + tlen);
716   {
717     char buf[size];
718
719     msg = (struct AddressIterateResponseMessage *) buf;
720     msg->reserved = htonl (0);
721     msg->peer = *peer;
722     msg->addrlen = htonl (alen);
723     msg->pluginlen = htonl (tlen);
724     addr = (char *) &msg[1];
725     memcpy (addr, address->address, alen);
726     memcpy (&addr[alen], address->transport_name, tlen);
727     GNUNET_SERVER_transmit_context_append_data (tc,
728                                                 &buf[sizeof
729                                                      (struct
730                                                       GNUNET_MessageHeader)],
731                                                 size -
732                                                 sizeof (struct
733                                                         GNUNET_MessageHeader),
734                                                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
735   }
736 }
737
738
739 /**
740  * Client asked to obtain information about all actively used addresses
741  * of connected peers
742  * Process the request.
743  *
744  * @param cls unused
745  * @param client the client
746  * @param message the peer address information request
747  */
748 static void
749 clients_handle_address_iterate (void *cls, struct GNUNET_SERVER_Client *client,
750                                 const struct GNUNET_MessageHeader *message)
751 {
752   static struct GNUNET_PeerIdentity all_zeros;
753   struct GNUNET_SERVER_TransmitContext *tc;
754   struct AddressIterateMessage *msg;
755   struct GNUNET_HELLO_Address *address;
756
757   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE)
758   {
759     GNUNET_break (0);
760     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
761     return;
762   }
763   if (ntohs (message->size) != sizeof (struct AddressIterateMessage))
764   {
765     GNUNET_break (0);
766     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
767     return;
768   }
769   msg = (struct AddressIterateMessage *) message;
770   if (GNUNET_YES != ntohl (msg->one_shot))
771   {
772     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
773                 "Address monitoring not implemented\n");
774     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
775     return;
776   }
777   GNUNET_SERVER_disable_receive_done_warning (client);
778   tc = GNUNET_SERVER_transmit_context_create (client);
779   if (0 == memcmp (&msg->peer, &all_zeros, sizeof (struct GNUNET_PeerIdentity)))
780   {
781     /* iterate over all neighbours */
782     GST_neighbours_iterate (&output_address, tc);
783   }
784   else
785   {
786     /* just return one neighbour */
787     address = GST_neighbour_get_current_address (&msg->peer);
788     if (address != NULL)
789       output_address (tc, &msg->peer, NULL, 0, address);
790   }
791   GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
792                                               GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
793   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
794 }
795
796
797 /**
798  * Start handling requests from clients.
799  *
800  * @param server server used to accept clients from.
801  */
802 void
803 GST_clients_start (struct GNUNET_SERVER_Handle *server)
804 {
805   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
806     {&clients_handle_start, NULL,
807      GNUNET_MESSAGE_TYPE_TRANSPORT_START, sizeof (struct StartMessage)},
808     {&clients_handle_hello, NULL,
809      GNUNET_MESSAGE_TYPE_HELLO, 0},
810     {&clients_handle_send, NULL,
811      GNUNET_MESSAGE_TYPE_TRANSPORT_SEND, 0},
812     {&clients_handle_request_connect, NULL,
813      GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT,
814      sizeof (struct TransportRequestConnectMessage)},
815     {&clients_handle_address_to_string, NULL,
816      GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING, 0},
817     {&clients_handle_address_iterate, NULL,
818      GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE,
819      sizeof (struct AddressIterateMessage)},
820     {&GST_blacklist_handle_init, NULL,
821      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
822      sizeof (struct GNUNET_MessageHeader)},
823     {&GST_blacklist_handle_reply, NULL,
824      GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
825      sizeof (struct BlacklistMessage)},
826     {NULL, NULL, 0, 0}
827   };
828   GNUNET_SERVER_add_handlers (server, handlers);
829   GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
830                                    NULL);
831 }
832
833
834 /**
835  * Stop processing clients.
836  */
837 void
838 GST_clients_stop ()
839 {
840   /* nothing to do */
841 }
842
843
844 /**
845  * Broadcast the given message to all of our clients.
846  *
847  * @param msg message to broadcast
848  * @param may_drop GNUNET_YES if the message can be dropped / is payload
849  */
850 void
851 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
852 {
853   struct TransportClient *tc;
854
855   for (tc = clients_head; tc != NULL; tc = tc->next)
856   {
857     if ((GNUNET_YES == may_drop) && (GNUNET_YES != tc->send_payload))
858       continue;                 /* skip, this client does not care about payload */
859     unicast (tc, msg, may_drop);
860   }
861 }
862
863
864 /**
865  * Send the given message to a particular client
866  *
867  * @param client target of the message
868  * @param msg message to transmit
869  * @param may_drop GNUNET_YES if the message can be dropped
870  */
871 void
872 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
873                      const struct GNUNET_MessageHeader *msg, int may_drop)
874 {
875   struct TransportClient *tc;
876
877   tc = lookup_client (client);
878   if (NULL == tc)
879     return;                     /* client got disconnected in the meantime, drop message */
880   unicast (tc, msg, may_drop);
881 }
882
883
884 /* end of file gnunet-service-transport_clients.c */