2 This file is part of GNUnet.
3 (C) 2009 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 2, 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.
23 * @brief library for building GNUnet network servers
24 * @author Christian Grothoff
28 #include "gnunet_common.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_server_lib.h"
32 #include "gnunet_time_lib.h"
33 #include "gnunet_disk_lib.h"
34 #include "gnunet_protocols.h"
36 #define DEBUG_SERVER GNUNET_NO
39 * List of arrays of message handlers.
44 * This is a linked list.
46 struct HandlerList *next;
49 * NULL-terminated array of handlers.
51 const struct GNUNET_SERVER_MessageHandler *handlers;
56 * List of arrays of message handlers.
61 * This is a linked list.
63 struct NotifyList *next;
68 GNUNET_SERVER_DisconnectCallback callback;
71 * Closure for callback.
78 * @brief handle for a server
80 struct GNUNET_SERVER_Handle
83 * List of handlers for incoming messages.
85 struct HandlerList *handlers;
88 * List of our current clients.
90 struct GNUNET_SERVER_Client *clients;
93 * Linked list of functions to call on disconnects by clients.
95 struct NotifyList *disconnect_notify_list;
98 * Function to call for access control.
100 GNUNET_CONNECTION_AccessCheck access;
103 * Closure for access.
108 * NULL-terminated array of sockets used to listen for new
111 struct GNUNET_NETWORK_Handle **listen_sockets;
114 * After how long should an idle connection time
117 struct GNUNET_TIME_Relative idle_timeout;
120 * Task scheduled to do the listening.
122 GNUNET_SCHEDULER_TaskIdentifier listen_task;
125 * Do we ignore messages of types that we do not understand or do we
126 * require that a handler is found (and if not kill the connection)?
131 * Should all of the clients of this server continue to process
132 * connections as usual even if we get a shutdown request? (the
133 * listen socket always ignores shutdown).
135 int clients_ignore_shutdown;
141 * @brief handle for a client of the server
143 struct GNUNET_SERVER_Client
147 * This is a linked list.
149 struct GNUNET_SERVER_Client *next;
152 * Processing of incoming data.
154 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
157 * Server that this client belongs to.
159 struct GNUNET_SERVER_Handle *server;
162 * Client closure for callbacks.
164 struct GNUNET_CONNECTION_Handle *connection;
167 * ID of task used to restart processing.
169 GNUNET_SCHEDULER_TaskIdentifier restart_task;
172 * Task that warns about missing calls to 'GNUNET_SERVER_receive_done'.
174 GNUNET_SCHEDULER_TaskIdentifier warn_task;
177 * Time when the warn task was started.
179 struct GNUNET_TIME_Absolute warn_start;
182 * Last activity on this socket (used to time it out
183 * if reference_count == 0).
185 struct GNUNET_TIME_Absolute last_activity;
188 * Number of external entities with a reference to
189 * this client object.
191 unsigned int reference_count;
194 * Was processing if incoming messages suspended while
195 * we were still processing data already received?
196 * This is a counter saying how often processing was
197 * suspended (once per handler invoked).
199 unsigned int suspended;
202 * Are we currently in the "process_client_buffer" function (and
203 * will hence restart the receive job on exit if suspended == 0 once
204 * we are done?). If this is set, then "receive_done" will
205 * essentially only decrement suspended; if this is not set, then
206 * "receive_done" may need to restart the receive process (either
207 * from the side-buffer or via select/recv).
209 int in_process_client_buffer;
212 * We're about to close down this client due to some serious
218 * Are we currently trying to receive? (YES if we are, NO if we are not,
219 * SYSERR if data is already available in MST).
224 * Persist the file handle for this client no matter what happens,
225 * force the OS to close once the process actually dies. Should only
226 * be used in special cases!
231 * Type of last message processed (for warn_no_receive_done).
238 * Scheduler says our listen socket is ready. Process it!
240 * @param cls handle to our server for which we are processing the listen
242 * @param tc reason why we are running right now
245 process_listen_socket (void *cls,
246 const struct GNUNET_SCHEDULER_TaskContext *tc)
248 struct GNUNET_SERVER_Handle *server = cls;
249 struct GNUNET_CONNECTION_Handle *sock;
250 struct GNUNET_SERVER_Client *client;
251 struct GNUNET_NETWORK_FDSet *r;
254 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
255 r = GNUNET_NETWORK_fdset_create ();
257 while (NULL != server->listen_sockets[i])
258 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
259 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
261 /* ignore shutdown, someone else will take care of it! */
262 server->listen_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
263 GNUNET_SCHEDULER_NO_TASK,
264 GNUNET_TIME_UNIT_FOREVER_REL,
266 &process_listen_socket,
268 GNUNET_NETWORK_fdset_destroy (r);
272 while (NULL != server->listen_sockets[i])
274 if (GNUNET_NETWORK_fdset_isset
275 (tc->read_ready, server->listen_sockets[i]))
278 GNUNET_CONNECTION_create_from_accept (server->access,
280 server->listen_sockets[i]);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Server accepted incoming connection.\n");
287 client = GNUNET_SERVER_connect_socket (server, sock);
288 GNUNET_CONNECTION_ignore_shutdown (sock,
289 server->clients_ignore_shutdown);
290 /* decrement reference count, we don't keep "client" alive */
291 GNUNET_SERVER_client_drop (client);
296 /* listen for more! */
297 server->listen_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
298 GNUNET_SCHEDULER_NO_TASK,
299 GNUNET_TIME_UNIT_FOREVER_REL,
301 &process_listen_socket,
303 GNUNET_NETWORK_fdset_destroy (r);
308 * Create and initialize a listen socket for the server.
310 * @param serverAddr address to listen on
311 * @param socklen length of address
312 * @return NULL on error, otherwise the listen socket
314 static struct GNUNET_NETWORK_Handle *
315 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
317 const static int on = 1;
318 struct GNUNET_NETWORK_Handle *sock;
322 switch (serverAddr->sa_family)
325 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
328 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
334 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
337 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
343 if (GNUNET_NETWORK_socket_setsockopt
344 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
345 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
348 if ( (serverAddr->sa_family == AF_INET6) &&
349 (GNUNET_NETWORK_socket_setsockopt
350 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK) )
351 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
355 /* bind the socket */
356 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
359 if (errno != EADDRINUSE)
361 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
362 fail if we already took the port on IPv6; if both IPv4 and
363 IPv6 binds fail, then our caller will log using the
364 errno preserved in 'eno' */
365 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 ("`%s' failed for port %d (%s).\n"),
371 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
374 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
378 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
380 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
381 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Server starts to listen on port %u.\n", port);
395 * Create a new server.
397 * @param access function for access control
398 * @param access_cls closure for access
399 * @param lsocks NULL-terminated array of listen sockets
400 * @param idle_timeout after how long should we timeout idle connections?
401 * @param require_found if YES, connections sending messages of unknown type
403 * @return handle for the new server, NULL on error
404 * (typically, "port" already in use)
406 struct GNUNET_SERVER_Handle *
407 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
408 struct GNUNET_NETWORK_Handle **lsocks,
409 struct GNUNET_TIME_Relative
413 struct GNUNET_SERVER_Handle *ret;
414 struct GNUNET_NETWORK_FDSet *r;
417 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
418 ret->idle_timeout = idle_timeout;
419 ret->listen_sockets = lsocks;
420 ret->access = access;
421 ret->access_cls = access_cls;
422 ret->require_found = require_found;
425 r = GNUNET_NETWORK_fdset_create ();
427 while (NULL != ret->listen_sockets[i])
428 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]);
429 ret->listen_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
430 GNUNET_SCHEDULER_NO_TASK,
431 GNUNET_TIME_UNIT_FOREVER_REL,
433 &process_listen_socket,
435 GNUNET_NETWORK_fdset_destroy (r);
442 * Create a new server.
444 * @param access function for access control
445 * @param access_cls closure for access
446 * @param serverAddr address to listen on (including port), NULL terminated array
447 * @param socklen length of serverAddr
448 * @param idle_timeout after how long should we timeout idle connections?
449 * @param require_found if YES, connections sending messages of unknown type
451 * @return handle for the new server, NULL on error
452 * (typically, "port" already in use)
454 struct GNUNET_SERVER_Handle *
455 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access,
457 struct sockaddr *const *serverAddr,
458 const socklen_t * socklen,
459 struct GNUNET_TIME_Relative
460 idle_timeout, int require_found)
462 struct GNUNET_NETWORK_Handle **lsocks;
467 while (serverAddr[i] != NULL)
472 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
475 while (serverAddr[i] != NULL)
477 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
478 if (lsocks[j] != NULL)
485 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
486 GNUNET_free (lsocks);
494 return GNUNET_SERVER_create_with_sockets (access, access_cls,
502 * Free resources held by this server.
504 * @param s server to destroy
507 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
509 struct HandlerList *hpos;
510 struct NotifyList *npos;
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
516 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
518 GNUNET_SCHEDULER_cancel (s->listen_task);
519 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
521 if (s->listen_sockets != NULL)
524 while (s->listen_sockets[i] != NULL)
525 GNUNET_break (GNUNET_OK ==
526 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
527 GNUNET_free (s->listen_sockets);
528 s->listen_sockets = NULL;
530 while (s->clients != NULL)
531 GNUNET_SERVER_client_disconnect (s->clients);
532 while (NULL != (hpos = s->handlers))
534 s->handlers = hpos->next;
537 while (NULL != (npos = s->disconnect_notify_list))
539 npos->callback (npos->callback_cls, NULL);
540 s->disconnect_notify_list = npos->next;
548 * Add additional handlers to an existing server.
550 * @param server the server to add handlers to
551 * @param handlers array of message handlers for
552 * incoming messages; the last entry must
553 * have "NULL" for the "callback"; multiple
554 * entries for the same type are allowed,
555 * they will be called in order of occurence.
556 * These handlers can be removed later;
557 * the handlers array must exist until removed
558 * (or server is destroyed).
561 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
562 const struct GNUNET_SERVER_MessageHandler
565 struct HandlerList *p;
567 p = GNUNET_malloc (sizeof (struct HandlerList));
568 p->handlers = handlers;
569 p->next = server->handlers;
570 server->handlers = p;
575 * Task run to warn about missing calls to 'GNUNET_SERVER_receive_done'.
577 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
578 * @param tc scheduler context (unused)
581 warn_no_receive_done (void *cls,
582 const struct GNUNET_SCHEDULER_TaskContext *tc)
584 struct GNUNET_SERVER_Client *client = cls;
586 client->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
587 &warn_no_receive_done,
589 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
590 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
591 _("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"),
592 (unsigned int) client->warn_type,
593 (unsigned long long) GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value);
598 * Inject a message into the server, pretend it came
599 * from the specified client. Delivery of the message
600 * will happen instantly (if a handler is installed;
601 * otherwise the call does nothing).
603 * @param server the server receiving the message
604 * @param sender the "pretended" sender of the message
606 * @param message message to transmit
607 * @return GNUNET_OK if the message was OK and the
608 * connection can stay open
609 * GNUNET_SYSERR if the connection to the
610 * client should be shut down
613 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
614 struct GNUNET_SERVER_Client *sender,
615 const struct GNUNET_MessageHeader *message)
617 struct HandlerList *pos;
618 const struct GNUNET_SERVER_MessageHandler *mh;
624 type = ntohs (message->type);
625 size = ntohs (message->size);
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Server schedules transmission of %u-byte message of type %u to client.\n",
631 pos = server->handlers;
636 while (pos->handlers[i].callback != NULL)
638 mh = &pos->handlers[i];
639 if ( (mh->type == type) ||
640 (mh->type == GNUNET_MESSAGE_TYPE_ALL) )
642 if ((mh->expected_size != 0) && (mh->expected_size != size))
644 #if GNUNET8_NETWORK_IS_DEAD
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
646 "Expected %u bytes for message of type %u, got %u\n",
652 return GNUNET_SYSERR;
656 if (0 == sender->suspended)
658 sender->warn_start = GNUNET_TIME_absolute_get ();
659 sender->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
660 &warn_no_receive_done,
662 sender->warn_type = type;
666 mh->callback (mh->callback_cls, sender, message);
673 if (found == GNUNET_NO)
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
676 "Received message of unknown type %d\n",
678 if (server->require_found == GNUNET_YES)
679 return GNUNET_SYSERR;
686 * We are receiving an incoming message. Process it.
688 * @param cls our closure (handle for the client)
689 * @param buf buffer with data received from network
690 * @param available number of bytes available in buf
691 * @param addr address of the sender
692 * @param addrlen length of addr
693 * @param errCode code indicating errors receiving, 0 for success
696 process_incoming (void *cls,
699 const struct sockaddr *addr,
700 socklen_t addrlen, int errCode);
704 * Process messages from the client's message tokenizer until either
705 * the tokenizer is empty (and then schedule receiving more), or
706 * until some handler is not immediately done (then wait for restart_processing)
709 * @param client the client to process, RC must have already been increased
710 * using GNUNET_SERVER_client_keep and will be decreased by one in this
712 * @param ret GNUNET_NO to start processing from the buffer,
713 * GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
714 * GNUNET_SYSERR if we should instantly abort due to error in a previous step
717 process_mst (struct GNUNET_SERVER_Client *client,
720 while ( (ret != GNUNET_SYSERR) &&
721 (client->server != NULL) &&
722 (GNUNET_YES != client->shutdown_now) &&
723 (0 == client->suspended) )
725 if (ret == GNUNET_OK)
727 client->receive_pending = GNUNET_YES;
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Server re-enters receive loop.\n");
732 GNUNET_CONNECTION_receive (client->connection,
733 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
734 client->server->idle_timeout,
735 &process_incoming, client);
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server processes additional messages instantly.\n");
741 ret = GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, GNUNET_YES);
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
748 client->shutdown_now,
752 if (ret == GNUNET_NO)
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "Server has more data pending but is suspended.\n");
758 client->receive_pending = GNUNET_SYSERR; /* data pending */
760 if ( (ret == GNUNET_SYSERR) ||
761 (GNUNET_YES == client->shutdown_now) )
762 GNUNET_SERVER_client_disconnect (client);
763 GNUNET_SERVER_client_drop (client);
768 * We are receiving an incoming message. Process it.
770 * @param cls our closure (handle for the client)
771 * @param buf buffer with data received from network
772 * @param available number of bytes available in buf
773 * @param addr address of the sender
774 * @param addrlen length of addr
775 * @param errCode code indicating errors receiving, 0 for success
778 process_incoming (void *cls,
781 const struct sockaddr *addr,
782 socklen_t addrlen, int errCode)
784 struct GNUNET_SERVER_Client *client = cls;
785 struct GNUNET_SERVER_Handle *server = client->server;
788 GNUNET_assert (client->receive_pending == GNUNET_YES);
789 client->receive_pending = GNUNET_NO;
794 (client->shutdown_now == GNUNET_YES) ||
795 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
797 /* other side closed connection, error connecting, etc. */
798 GNUNET_SERVER_client_disconnect (client);
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Server receives %u bytes from `%s'.\n",
804 (unsigned int) available,
805 GNUNET_a2s (addr, addrlen));
807 GNUNET_SERVER_client_keep (client);
808 client->last_activity = GNUNET_TIME_absolute_get ();
809 ret = GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, GNUNET_YES);
810 process_mst (client, ret);
815 * Task run to start again receiving from the network
816 * and process requests.
818 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
819 * @param tc scheduler context (unused)
822 restart_processing (void *cls,
823 const struct GNUNET_SCHEDULER_TaskContext *tc)
825 struct GNUNET_SERVER_Client *client = cls;
826 struct GNUNET_SERVER_Handle *server = client->server;
828 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
829 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
830 (GNUNET_NO == server->clients_ignore_shutdown) )
832 GNUNET_SERVER_client_disconnect (client);
835 if (client->receive_pending == GNUNET_NO)
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 "Server begins to read again from client.\n");
841 client->receive_pending = GNUNET_YES;
842 GNUNET_CONNECTION_receive (client->connection,
843 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
844 client->server->idle_timeout, &process_incoming, client);
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Server continues processing messages still in the buffer.\n");
851 GNUNET_SERVER_client_keep (client);
852 client->receive_pending = GNUNET_NO;
853 process_mst (client, GNUNET_NO);
858 * This function is called whenever our inbound message tokenizer has
859 * received a complete message.
861 * @param cls closure (struct GNUNET_SERVER_Handle)
862 * @param client identification of the client (struct GNUNET_SERVER_Client*)
863 * @param message the actual message
866 client_message_tokenizer_callback (void *cls,
868 const struct GNUNET_MessageHeader *message)
870 struct GNUNET_SERVER_Handle *server = cls;
871 struct GNUNET_SERVER_Client *sender = client;
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Tokenizer gives server message of type %u from client\n",
877 ntohs (message->type));
879 sender->in_process_client_buffer = GNUNET_YES;
880 ret = GNUNET_SERVER_inject (server, sender, message);
881 sender->in_process_client_buffer = GNUNET_NO;
882 if (GNUNET_OK != ret)
883 GNUNET_SERVER_client_disconnect (sender);
888 * Add a TCP socket-based connection to the set of handles managed by
889 * this server. Use this function for outgoing (P2P) connections that
890 * we initiated (and where this server should process incoming
893 * @param server the server to use
894 * @param connection the connection to manage (client must
895 * stop using this connection from now on)
896 * @return the client handle (client should call
897 * "client_drop" on the return value eventually)
899 struct GNUNET_SERVER_Client *
900 GNUNET_SERVER_connect_socket (struct
903 struct GNUNET_CONNECTION_Handle *connection)
905 struct GNUNET_SERVER_Client *client;
907 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
908 client->connection = connection;
909 client->mst = GNUNET_SERVER_mst_create (&client_message_tokenizer_callback,
911 client->reference_count = 1;
912 client->server = server;
913 client->last_activity = GNUNET_TIME_absolute_get ();
914 client->next = server->clients;
915 server->clients = client;
916 client->receive_pending = GNUNET_YES;
917 GNUNET_CONNECTION_receive (client->connection,
918 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
919 server->idle_timeout, &process_incoming, client);
925 * Notify the server that the given client handle should
926 * be kept (keeps the connection up if possible, increments
927 * the internal reference counter).
929 * @param client the client to keep
932 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
934 client->reference_count++;
939 * Notify the server that the given client handle is no
940 * longer required. Decrements the reference counter. If
941 * that counter reaches zero an inactive connection maybe
944 * @param client the client to drop
947 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
949 GNUNET_assert (client->reference_count > 0);
950 client->reference_count--;
951 if ( (client->shutdown_now == GNUNET_YES) &&
952 (client->reference_count == 0) )
953 GNUNET_SERVER_client_disconnect (client);
958 * Obtain the network address of the other party.
960 * @param client the client to get the address for
961 * @param addr where to store the address
962 * @param addrlen where to store the length of the address
963 * @return GNUNET_OK on success
966 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
967 void **addr, size_t * addrlen)
969 return GNUNET_CONNECTION_get_address (client->connection,
975 * Ask the server to notify us whenever a client disconnects.
976 * This function is called whenever the actual network connection
977 * is closed; the reference count may be zero or larger than zero
980 * @param server the server manageing the clients
981 * @param callback function to call on disconnect
982 * @param callback_cls closure for callback
985 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
986 GNUNET_SERVER_DisconnectCallback callback,
989 struct NotifyList *n;
991 n = GNUNET_malloc (sizeof (struct NotifyList));
992 n->callback = callback;
993 n->callback_cls = callback_cls;
994 n->next = server->disconnect_notify_list;
995 server->disconnect_notify_list = n;
1000 * Ask the server to stop notifying us whenever a client disconnects.
1002 * @param server the server manageing the clients
1003 * @param callback function to call on disconnect
1004 * @param callback_cls closure for callback
1007 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1008 GNUNET_SERVER_DisconnectCallback callback,
1011 struct NotifyList *pos;
1012 struct NotifyList *prev;
1015 pos = server->disconnect_notify_list;
1018 if ( (pos->callback == callback) &&
1019 (pos->callback_cls == callback_cls ) )
1030 server->disconnect_notify_list = pos->next;
1032 prev->next = pos->next;
1038 * Ask the server to disconnect from the given client.
1039 * This is the same as returning GNUNET_SYSERR from a message
1040 * handler, except that it allows dropping of a client even
1041 * when not handling a message from that client.
1043 * @param client the client to disconnect from
1046 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1048 struct GNUNET_SERVER_Client *prev;
1049 struct GNUNET_SERVER_Client *pos;
1050 struct GNUNET_SERVER_Handle *server;
1051 struct NotifyList *n;
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "Client is being disconnected from the server.\n");
1058 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1060 GNUNET_SCHEDULER_cancel (client->restart_task);
1061 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1063 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1065 GNUNET_SCHEDULER_cancel (client->warn_task);
1066 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1068 if (GNUNET_YES == client->receive_pending)
1070 GNUNET_CONNECTION_receive_cancel (client->connection);
1071 client->receive_pending = GNUNET_NO;
1074 rc = client->reference_count;
1075 if (client->server != NULL)
1077 server = client->server;
1078 client->server = NULL;
1079 client->shutdown_now = GNUNET_YES;
1081 pos = server->clients;
1082 while ((pos != NULL) && (pos != client))
1087 GNUNET_assert (pos != NULL);
1089 server->clients = pos->next;
1091 prev->next = pos->next;
1092 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1094 GNUNET_SCHEDULER_cancel (client->restart_task);
1095 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1097 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1099 GNUNET_SCHEDULER_cancel (client->warn_task);
1100 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1102 n = server->disconnect_notify_list;
1105 n->callback (n->callback_cls, client);
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "RC still positive, not destroying everything.\n");
1117 if (client->in_process_client_buffer == GNUNET_YES)
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1121 "Still processing inputs, not destroying everything.\n");
1126 if (client->persist == GNUNET_YES)
1127 GNUNET_CONNECTION_persist_ (client->connection);
1128 GNUNET_CONNECTION_destroy (client->connection, GNUNET_NO);
1129 GNUNET_SERVER_mst_destroy (client->mst);
1130 GNUNET_free (client);
1135 * Notify us when the server has enough space to transmit
1136 * a message of the given size to the given client.
1138 * @param client client to transmit message to
1139 * @param size requested amount of buffer space
1140 * @param timeout after how long should we give up (and call
1141 * notify with buf NULL and size 0)?
1142 * @param callback function to call when space is available
1143 * @param callback_cls closure for callback
1144 * @return non-NULL if the notify callback was queued; can be used
1145 * to cancel the request using
1146 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1147 * NULL if we are already going to notify someone else (busy)
1149 struct GNUNET_CONNECTION_TransmitHandle *
1150 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1152 struct GNUNET_TIME_Relative timeout,
1153 GNUNET_CONNECTION_TransmitReadyNotify
1154 callback, void *callback_cls)
1156 return GNUNET_CONNECTION_notify_transmit_ready (client->connection,
1158 timeout, callback, callback_cls);
1162 * Set the persistent flag on this client, used to setup client connection
1163 * to only be killed when the service it's connected to is actually dead.
1165 * @param client the client to set the persistent flag on
1168 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1170 client->persist = GNUNET_YES;
1174 * Resume receiving from this client, we are done processing the
1175 * current request. This function must be called from within each
1176 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1178 * @param client client we were processing a message of
1179 * @param success GNUNET_OK to keep the connection open and
1180 * continue to receive
1181 * GNUNET_NO to close the connection (normal behavior)
1182 * GNUNET_SYSERR to close the connection (signal
1186 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1190 GNUNET_assert (client->suspended > 0);
1191 client->suspended--;
1192 if (success != GNUNET_OK)
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "GNUNET_SERVER_receive_done called with failure indication\n");
1198 GNUNET_SERVER_client_disconnect (client);
1201 if (client->suspended > 0)
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1205 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1209 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1211 GNUNET_SCHEDULER_cancel (client->warn_task);
1212 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1214 if (client->in_process_client_buffer == GNUNET_YES)
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1218 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1222 if (client->server == NULL)
1224 GNUNET_SERVER_client_disconnect (client);
1228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1229 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1231 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1232 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing,
1238 * Configure this server's connections to continue handling client
1239 * requests as usual even after we get a shutdown signal. The change
1240 * only applies to clients that connect to the server from the outside
1241 * using TCP after this call. Clients managed previously or those
1242 * added using GNUNET_SERVER_connect_socket and
1243 * GNUNET_SERVER_connect_callback are not affected by this option.
1245 * @param h server handle
1246 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1249 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore)
1251 h->clients_ignore_shutdown = do_ignore;
1254 /* end of server.c */