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