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