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