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