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.h"
31 #include "transport.h"
34 * How many messages can we have pending for a given client process
35 * before we start to drop incoming messages? We typically should
36 * have only one client and so this would be the primary buffer for
37 * messages, so the number should be chosen rather generously.
39 * The expectation here is that most of the time the queue is large
40 * enough so that a drop is virtually never required. Note that
41 * this value must be about as large as 'TOTAL_MSGS' in the
42 * 'test_transport_api_reliability.c', otherwise that testcase may
45 #define MAX_PENDING (128 * 1024)
49 * Linked list of messages to be transmitted to the client. Each
50 * entry is followed by the actual message.
52 struct ClientMessageQueueEntry
55 * This is a doubly-linked list.
57 struct ClientMessageQueueEntry *next;
60 * This is a doubly-linked list.
62 struct ClientMessageQueueEntry *prev;
67 * Client connected to the transport service.
69 struct TransportClient
73 * This is a doubly-linked list.
75 struct TransportClient *next;
78 * This is a doubly-linked list.
80 struct TransportClient *prev;
83 * Handle to the client.
85 struct GNUNET_SERVER_Client *client;
88 * Linked list of messages yet to be transmitted to
91 struct ClientMessageQueueEntry *message_queue_head;
94 * Tail of linked list of messages yet to be transmitted to the
97 struct ClientMessageQueueEntry *message_queue_tail;
100 * Current transmit request handle.
102 struct GNUNET_CONNECTION_TransmitHandle *th;
105 * Length of the list of messages pending for this client.
107 unsigned int message_count;
113 * Head of linked list of all clients to this service.
115 static struct TransportClient *clients_head;
118 * Tail of linked list of all clients to this service.
120 static struct TransportClient *clients_tail;
124 * Find the internal handle associated with the given client handle
126 * @param client server's client handle to look up
127 * @return internal client handle
129 static struct TransportClient *
130 lookup_client (struct GNUNET_SERVER_Client *client)
132 struct TransportClient *tc;
137 if (tc->client == client)
146 * Create the internal handle for the given server client handle
148 * @param client server's client handle to create our internal handle for
149 * @return fresh internal client handle
151 static struct TransportClient *
152 setup_client (struct GNUNET_SERVER_Client *client)
154 struct TransportClient *tc;
156 tc = GNUNET_malloc (sizeof (struct TransportClient));
158 GNUNET_CONTAINER_DLL_insert (clients_head,
166 * Function called to notify a client about the socket being ready to
167 * queue more data. "buf" will be NULL and "size" zero if the socket
168 * was closed for writing in the meantime.
171 * @param size number of bytes available in buf
172 * @param buf where the callee should write the message
173 * @return number of bytes written to buf
176 transmit_to_client_callback (void *cls,
180 struct TransportClient *tc = cls;
181 struct ClientMessageQueueEntry *q;
182 const struct GNUNET_MessageHeader *msg;
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "Transmission to client failed, closing connection.\n");
198 while (NULL != (q = tc->message_queue_head))
200 msg = (const struct GNUNET_MessageHeader *) &q[1];
201 msize = ntohs (msg->size);
202 if (msize + tsize > size)
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Transmitting message of type %u to client.\n",
209 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
210 tc->message_queue_tail,
213 memcpy (&cbuf[tsize],
221 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
222 tc->th = GNUNET_SERVER_notify_transmit_ready (tc->client,
224 GNUNET_TIME_UNIT_FOREVER_REL,
225 &transmit_to_client_callback,
227 GNUNET_assert (tc->th != NULL);
234 * Queue the given message for transmission to the given client
236 * @param client target of the message
237 * @param msg message to transmit
238 * @param may_drop GNUNET_YES if the message can be dropped
241 unicast (struct TransportClient *tc,
242 const struct GNUNET_MessageHeader *msg,
245 struct ClientMessageQueueEntry *q;
248 if ( (tc->message_count >= MAX_PENDING) &&
249 (GNUNET_YES == may_drop) )
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252 _("Dropping message of type %u and size %u, have %u/%u messages pending\n"),
257 GNUNET_STATISTICS_update (GST_stats,
258 gettext_noop ("# messages dropped due to slow client"),
263 msize = ntohs (msg->size);
264 GNUNET_assert (msize >= sizeof (struct GNUNET_MessageHeader));
265 q = GNUNET_malloc (sizeof (struct ClientMessageQueueEntry) + msize);
266 memcpy (&q[1], msg, msize);
267 GNUNET_CONTAINER_DLL_insert_tail (tc->message_queue_head,
268 tc->message_queue_tail,
273 tc->th = GNUNET_SERVER_notify_transmit_ready (tc->client,
275 GNUNET_TIME_UNIT_FOREVER_REL,
276 &transmit_to_client_callback,
278 GNUNET_assert (tc->th != NULL);
283 * Called whenever a client is disconnected. Frees our
284 * resources associated with that client.
287 * @param client identification of the client
290 client_disconnect_notification (void *cls,
291 struct GNUNET_SERVER_Client *client)
293 struct TransportClient *tc;
294 struct ClientMessageQueueEntry *mqe;
298 tc = lookup_client (client);
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
303 "Client disconnected, cleaning up.\n");
305 while (NULL != (mqe = tc->message_queue_head))
307 GNUNET_CONTAINER_DLL_remove (tc->message_queue_head,
308 tc->message_queue_tail,
313 GNUNET_CONTAINER_DLL_remove (clients_head,
318 GNUNET_CONNECTION_notify_transmit_ready_cancel (tc->th);
321 GNUNET_break (0 == tc->message_count);
327 * Start handling requests from clients.
329 * @param server server used to accept clients from.
332 GST_clients_start (struct GNUNET_SERVER_Handle *server)
334 GNUNET_SERVER_disconnect_notify (server,
335 &client_disconnect_notification, NULL);
340 * Stop processing clients.
350 * Function called for each of our connected neighbours. Notify the
351 * client about the existing neighbour.
353 * @param cls the 'struct TransportClient' to notify
354 * @param peer identity of the neighbour
355 * @param ats performance data
356 * @param ats_count number of entries in ats (excluding 0-termination)
359 notify_client_about_neighbour (void *cls,
360 const struct GNUNET_PeerIdentity *peer,
361 const struct GNUNET_TRANSPORT_ATS_Information *ats,
364 struct TransportClient *tc = cls;
365 struct ConnectInfoMessage *cim;
368 size = sizeof (struct ConnectInfoMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
369 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
370 cim = GNUNET_malloc (size);
371 cim->header.size = htons (size);
372 cim->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
373 cim->ats_count = htonl(ats_count);
377 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
378 unicast (tc, &cim->header, GNUNET_NO);
384 * Initialize a normal client. We got a start message from this
385 * client, add him to the list of clients for broadcasting of inbound
389 * @param client the client
390 * @param message the start message that was sent
393 GST_clients_handle_start (void *cls,
394 struct GNUNET_SERVER_Client *client,
395 const struct GNUNET_MessageHeader *message)
397 const struct StartMessage *start;
398 struct TransportClient *tc;
400 tc = lookup_client (client);
403 /* got 'start' twice from the same client, not allowed */
405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
408 start = (const struct StartMessage*) message;
409 if ( (GNUNET_NO != ntohl (start->do_check)) &&
410 (0 != memcmp (&start->self,
412 sizeof (struct GNUNET_PeerIdentity))) )
414 /* client thinks this is a different peer, reject */
415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
416 _("Rejecting control connection from peer `%s', which is not me!\n"),
417 GNUNET_i2s (&start->self));
418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
421 tc = setup_client (client);
422 unicast (tc, GST_hello_get(), GNUNET_NO);
423 GST_neighbours_iterate (¬ify_client_about_neighbour, tc);
424 GNUNET_SERVER_receive_done (client, GNUNET_OK);
429 * Client asked for transmission to a peer. Process the request.
432 * @param client the client
433 * @param message the send message that was sent
436 GST_clients_handle_send (void *cls,
437 struct GNUNET_SERVER_Client *client,
438 const struct GNUNET_MessageHeader *message)
444 * Client asked for a quota change for a particular peer. Process the request.
447 * @param client the client
448 * @param message the quota changing message
451 GST_clients_handle_set_quota (void *cls,
452 struct GNUNET_SERVER_Client *client,
453 const struct GNUNET_MessageHeader *message)
459 * Client asked to resolve an address. Process the request.
462 * @param client the client
463 * @param message the resolution request
466 GST_clients_handle_address_lookup (void *cls,
467 struct GNUNET_SERVER_Client *client,
468 const struct GNUNET_MessageHeader *message)
474 * Client asked to obtain information about a peer's addresses.
475 * Process the request.
478 * @param client the client
479 * @param message the peer address information request
482 GST_clients_handle_peer_address_lookup (void *cls,
483 struct GNUNET_SERVER_Client *client,
484 const struct GNUNET_MessageHeader *message)
490 * Client asked to obtain information about all addresses.
491 * Process the request.
494 * @param client the client
495 * @param message the peer address information request
498 GST_clients_handle_address_iterate (void *cls,
499 struct GNUNET_SERVER_Client *client,
500 const struct GNUNET_MessageHeader *message)
506 * Broadcast the given message to all of our clients.
508 * @param msg message to broadcast
509 * @param may_drop GNUNET_YES if the message can be dropped
512 GST_clients_broadcast (const struct GNUNET_MessageHeader *msg,
515 struct TransportClient *tc;
517 for (tc = clients_head; tc != NULL; tc = tc->next)
518 unicast (tc, msg, may_drop);
523 * Send the given message to a particular client
525 * @param client target of the message
526 * @param msg message to transmit
527 * @param may_drop GNUNET_YES if the message can be dropped
530 GST_clients_unicast (struct GNUNET_SERVER_Client *client,
531 const struct GNUNET_MessageHeader *msg,
534 struct TransportClient *tc;
536 tc = lookup_client (client);
538 tc = setup_client (client);
539 unicast (tc, msg, may_drop);
543 /* end of file gnunet-service-transport_clients.c */