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