2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/gnunet-service-transport_clients.c
23 * @brief plugin management API
24 * @author Christian Grothoff
27 #include "gnunet-service-transport_clients.h"
28 #include "gnunet-service-transport_hello.h"
29 #include "gnunet-service-transport_neighbours.h"
30 #include "gnunet-service-transport_plugins.h"
31 #include "gnunet-service-transport_validation.h"
32 #include "gnunet-service-transport.h"
33 #include "transport.h"
36 * How many messages can we have pending for a given client process
37 * before we start to drop incoming messages? We typically should
38 * have only one client and so this would be the primary buffer for
39 * messages, so the number should be chosen rather generously.
41 * The expectation here is that most of the time the queue is large
42 * enough so that a drop is virtually never required. Note that
43 * this value must be about as large as 'TOTAL_MSGS' in the
44 * 'test_transport_api_reliability.c', otherwise that testcase may
47 #define MAX_PENDING (128 * 1024)
51 * Linked list of messages to be transmitted to the client. Each
52 * entry is followed by the actual message.
54 struct ClientMessageQueueEntry
57 * This is a doubly-linked list.
59 struct ClientMessageQueueEntry *next;
62 * This is a doubly-linked list.
64 struct ClientMessageQueueEntry *prev;
69 * Client connected to the transport service.
71 struct TransportClient
75 * This is a doubly-linked list.
77 struct TransportClient *next;
80 * This is a doubly-linked list.
82 struct TransportClient *prev;
85 * Handle to the client.
87 struct GNUNET_SERVER_Client *client;
90 * Linked list of messages yet to be transmitted to
93 struct ClientMessageQueueEntry *message_queue_head;
96 * Tail of linked list of messages yet to be transmitted to the
99 struct ClientMessageQueueEntry *message_queue_tail;
102 * Current transmit request handle.
104 struct GNUNET_CONNECTION_TransmitHandle *th;
107 * Length of the list of messages pending for this client.
109 unsigned int message_count;
115 * Head of linked list of all clients to this service.
117 static struct TransportClient *clients_head;
120 * Tail of linked list of all clients to this service.
122 static struct TransportClient *clients_tail;
126 * Find the internal handle associated with the given client handle
128 * @param client server's client handle to look up
129 * @return internal client handle
131 static struct TransportClient *
132 lookup_client (struct GNUNET_SERVER_Client *client)
134 struct TransportClient *tc;
139 if (tc->client == client)
148 * Create the internal handle for the given server client handle
150 * @param client server's client handle to create our internal handle for
151 * @return fresh internal client handle
153 static struct TransportClient *
154 setup_client (struct GNUNET_SERVER_Client *client)
156 struct TransportClient *tc;
158 tc = GNUNET_malloc (sizeof (struct TransportClient));
160 GNUNET_CONTAINER_DLL_insert (clients_head,
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.
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
178 transmit_to_client_callback (void *cls,
182 struct TransportClient *tc = cls;
183 struct ClientMessageQueueEntry *q;
184 const struct GNUNET_MessageHeader *msg;
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Transmission to client failed, closing connection.\n");
200 while (NULL != (q = tc->message_queue_head))
202 msg = (const struct GNUNET_MessageHeader *) &q[1];
203 msize = ntohs (msg->size);
204 if (msize + tsize > size)
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Transmitting message of type %u to client.\n",
211 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
212 tc->message_queue_tail,
215 memcpy (&cbuf[tsize],
223 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
224 tc->th = GNUNET_SERVER_notify_transmit_ready (tc->client,
226 GNUNET_TIME_UNIT_FOREVER_REL,
227 &transmit_to_client_callback,
229 GNUNET_assert (tc->th != NULL);
236 * Queue the given message for transmission to the given client
238 * @param client target of the message
239 * @param msg message to transmit
240 * @param may_drop GNUNET_YES if the message can be dropped
243 unicast (struct TransportClient *tc,
244 const struct GNUNET_MessageHeader *msg,
247 struct ClientMessageQueueEntry *q;
250 if ( (tc->message_count >= MAX_PENDING) &&
251 (GNUNET_YES == may_drop) )
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
254 _("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
259 GNUNET_STATISTICS_update (GST_stats,
260 gettext_noop ("# messages dropped due to slow client"),
265 msize = ntohs (msg->size);
266 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
267 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
268 memcpy (&q[1], msg, msize);
269 GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
270 tc->message_queue_tail,
275 tc->th = GNUNET_SERVER_notify_transmit_ready (tc->client,
277 GNUNET_TIME_UNIT_FOREVER_REL,
278 &transmit_to_client_callback,
280 GNUNET_assert (tc->th != NULL);
285 * Called whenever a client is disconnected. Frees our
286 * resources associated with that client.
289 * @param client identification of the client
292 client_disconnect_notification (void *cls,
293 struct GNUNET_SERVER_Client *client)
295 struct TransportClient *tc;
296 struct ClientMessageQueueEntry *mqe;
300 tc = lookup_client (client);
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
305 "Client disconnected, cleaning up.\n");
307 while (NULL != (mqe = tc->message_queue_head))
309 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
310 tc->message_queue_tail,
315 GNUNET_CONTAINER_DLL_remove (clients_head,
320 GNUNET_CONNECTION_notify_transmit_ready_cancel (tc->th);
323 GNUNET_break (0 == tc->message_count);
329 * Start handling requests from clients.
331 * @param server server used to accept clients from.
334 GST_clients_start (struct GNUNET_SERVER_Handle *server)
336 GNUNET_SERVER_disconnect_notify (server,
337 &client_disconnect_notification, NULL);
342 * Stop processing clients.
352 * Function called for each of our connected neighbours. Notify the
353 * client about the existing neighbour.
355 * @param cls the 'struct TransportClient' to notify
356 * @param peer identity of the neighbour
357 * @param ats performance data
358 * @param ats_count number of entries in ats (excluding 0-termination)
361 notify_client_about_neighbour (void *cls,
362 const struct GNUNET_PeerIdentity *peer,
363 const struct GNUNET_TRANSPORT_ATS_Information *ats,
366 struct TransportClient *tc = cls;
367 struct ConnectInfoMessage *cim;
370 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
371 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
372 cim = GNUNET_malloc (size);
373 cim->header.size = htons (size);
374 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
375 cim->ats_count = htonl(ats_count);
379 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
380 unicast (tc, &cim->header, GNUNET_NO);
386 * Initialize a normal client. We got a start message from this
387 * client, add him to the list of clients for broadcasting of inbound
391 * @param client the client
392 * @param message the start message that was sent
395 GST_clients_handle_start (void *cls,
396 struct GNUNET_SERVER_Client *client,
397 const struct GNUNET_MessageHeader *message)
399 const struct StartMessage *start;
400 struct TransportClient *tc;
402 tc = lookup_client (client);
405 /* got 'start' twice from the same client, not allowed */
407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
410 start = (const struct StartMessage*) message;
411 if ( (GNUNET_NO != ntohl (start->do_check)) &&
412 (0 != memcmp (&start->self,
414 sizeof (struct GNUNET_PeerIdentity))) )
416 /* client thinks this is a different peer, reject */
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418 _("Rejecting control connection from peer `%s', which is not me!\n"),
419 GNUNET_i2s (&start->self));
420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
423 tc = setup_client (client);
424 unicast (tc, GST_hello_get(), GNUNET_NO);
425 GST_neighbours_iterate (¬ify_client_about_neighbour, tc);
426 GNUNET_SERVER_receive_done (client, GNUNET_OK);
431 * Client sent us a HELLO. Process the request.
434 * @param client the client
435 * @param message the HELLO message
438 GST_clients_handle_hello (void *cls,
439 struct GNUNET_SERVER_Client *client,
440 const struct GNUNET_MessageHeader *message)
442 GST_validation_handle_hello (message);
443 GNUNET_SERVER_receive_done (client, GNUNET_OK);
448 * Client asked for transmission to a peer. Process the request.
451 * @param client the client
452 * @param message the send message that was sent
455 GST_clients_handle_send (void *cls,
456 struct GNUNET_SERVER_Client *client,
457 const struct GNUNET_MessageHeader *message)
460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
465 * Client asked for a quota change for a particular peer. Process the request.
468 * @param client the client
469 * @param message the quota changing message
472 GST_clients_handle_set_quota (void *cls,
473 struct GNUNET_SERVER_Client *client,
474 const struct GNUNET_MessageHeader *message)
476 const struct QuotaSetMessage *qsm;
478 qsm = (const struct QuotaSetMessage *) message;
479 GNUNET_STATISTICS_update (GST_stats,
480 gettext_noop ("# SET QUOTA messages received"),
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Received `%s' request (new quota %u) from client for peer `%4s'\n",
487 (unsigned int) ntohl (qsm->quota.value__),
488 GNUNET_i2s (&qsm->peer));
490 GST_neighbours_set_incoming_quota (&qsm->peer,
492 GNUNET_SERVER_receive_done (client, GNUNET_OK);
497 * Take the given address and append it to the set of results sent back to
500 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
501 * @param address the resolved name, NULL to indicate the last response
504 transmit_address_to_client (void *cls,
507 struct GNUNET_SERVER_TransmitContext *tc = cls;
511 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
512 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
513 GNUNET_SERVER_transmit_context_run (tc,
514 GNUNET_TIME_UNIT_FOREVER_REL);
517 GNUNET_SERVER_transmit_context_append_data (tc,
518 address, strlen (address) + 1,
519 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
524 * Client asked to resolve an address. Process the request.
527 * @param client the client
528 * @param message the resolution request
531 GST_clients_handle_address_lookup (void *cls,
532 struct GNUNET_SERVER_Client *client,
533 const struct GNUNET_MessageHeader *message)
535 const struct AddressLookupMessage *alum;
536 struct GNUNET_TRANSPORT_PluginFunctions *papi;
537 const char *plugin_name;
539 uint32_t address_len;
541 struct GNUNET_SERVER_TransmitContext *tc;
542 struct GNUNET_TIME_Relative rtimeout;
545 size = ntohs (message->size);
546 if (size < sizeof (struct AddressLookupMessage))
549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
552 alum = (const struct AddressLookupMessage *) message;
553 address_len = ntohl (alum->addrlen);
554 if (size <= sizeof (struct AddressLookupMessage) + address_len)
557 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
560 address = (const char *) &alum[1];
561 plugin_name = (const char *) &address[address_len];
563 [size - sizeof (struct AddressLookupMessage) - address_len - 1] != '\0')
566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
569 rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
570 numeric = ntohl (alum->numeric_only);
571 tc = GNUNET_SERVER_transmit_context_create (client);
572 papi = GST_plugins_find (plugin_name);
575 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
576 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
577 GNUNET_SERVER_transmit_context_run (tc, rtimeout);
580 GNUNET_SERVER_disable_receive_done_warning (client);
581 papi->address_pretty_printer (papi->cls,
583 address, address_len,
586 &transmit_address_to_client, tc);
591 * Send an address to the client.
593 * @param cls our 'struct GNUNET_SERVER_TransmitContext' (for sending)
594 * @param public_key public key for the peer, never NULL
595 * @param target peer this change is about, never NULL
596 * @param valid_until until what time do we consider the address valid?
597 * @param validation_block is FOREVER if the address is for an unsupported plugin (from PEERINFO)
598 * is ZERO if the address is considered valid (no validation needed)
599 * is a time in the future if we're currently denying re-validation
600 * @param plugin_name name of the plugin
601 * @param plugin_address binary address
602 * @param plugin_address_len length of address
605 send_address_to_client (void *cls,
606 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key,
607 const struct GNUNET_PeerIdentity *target,
608 struct GNUNET_TIME_Absolute valid_until,
609 struct GNUNET_TIME_Absolute validation_block,
610 const char *plugin_name,
611 const void *plugin_address,
612 size_t plugin_address_len)
614 struct GNUNET_SERVER_TransmitContext *tc = cls;
617 /* FIXME: move to a binary format!!! */
618 GNUNET_asprintf (&addr_buf, "%s --- %s, %s",
619 GST_plugins_a2s (plugin_name,
622 (GNUNET_YES == GST_neighbours_test_connected (target))
625 (GNUNET_TIME_absolute_get_remaining (valid_until).rel_value > 0)
628 transmit_address_to_client (tc, addr_buf);
629 GNUNET_free (addr_buf);
634 * Client asked to obtain information about a peer's addresses.
635 * Process the request.
636 * FIXME: use better name!
639 * @param client the client
640 * @param message the peer address information request
643 GST_clients_handle_peer_address_lookup (void *cls,
644 struct GNUNET_SERVER_Client *client,
645 const struct GNUNET_MessageHeader *message)
647 const struct PeerAddressLookupMessage *peer_address_lookup;
648 struct GNUNET_SERVER_TransmitContext *tc;
650 peer_address_lookup = (const struct PeerAddressLookupMessage *) message;
651 GNUNET_break (ntohl (peer_address_lookup->reserved) == 0);
652 tc = GNUNET_SERVER_transmit_context_create (client);
653 (void) GST_validation_get_addresses (&peer_address_lookup->peer,
655 &send_address_to_client,
657 GNUNET_SERVER_transmit_context_append_data (tc,
659 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
660 GNUNET_SERVER_transmit_context_run (tc,
661 GNUNET_TIME_UNIT_FOREVER_REL);
666 * Output the active address of connected neighbours to the given client.
668 * @param cls the 'struct GNUNET_SERVER_TransmitContext' for transmission to the client
669 * @param neighbour identity of the neighbour
670 * @param ats performance data
671 * @param ats_count number of entries in ats (excluding 0-termination)
674 output_addresses (void *cls,
675 const struct GNUNET_PeerIdentity *neighbour,
676 const struct GNUNET_TRANSPORT_ATS_Information *ats,
679 struct GNUNET_SERVER_TransmitContext *tc = cls;
682 /* FIXME: move to a binary format!!! */
683 GNUNET_asprintf (&addr_buf,
685 GNUNET_i2s(neighbour),
686 GST_plugins_a2s ("FIXME", NULL, 0));
687 transmit_address_to_client (tc, addr_buf);
688 GNUNET_free (addr_buf);
693 * Client asked to obtain information about all actively used addresses.
694 * Process the request. FIXME: use better name!
697 * @param client the client
698 * @param message the peer address information request
701 GST_clients_handle_address_iterate (void *cls,
702 struct GNUNET_SERVER_Client *client,
703 const struct GNUNET_MessageHeader *message)
705 struct GNUNET_SERVER_TransmitContext *tc;
707 GNUNET_SERVER_disable_receive_done_warning (client);
708 tc = GNUNET_SERVER_transmit_context_create (client);
709 GST_neighbours_iterate (&output_addresses,
711 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
712 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
713 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
718 * Broadcast the given message to all of our clients.
720 * @param msg message to broadcast
721 * @param may_drop GNUNET_YES if the message can be dropped
724 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg,
727 struct TransportClient *tc;
729 for (tc = clients_head; tc != NULL; tc = tc->next)
730 unicast (tc, msg, may_drop);
735 * Send the given message to a particular client
737 * @param client target of the message
738 * @param msg message to transmit
739 * @param may_drop GNUNET_YES if the message can be dropped
742 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
743 const struct GNUNET_MessageHeader *msg,
746 struct TransportClient *tc;
748 tc = lookup_client (client);
750 tc = setup_client (client);
751 unicast (tc, msg, may_drop);
755 /* end of file gnunet-service-transport_clients.c */