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