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