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);
338 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
341 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
347 if (GNUNET_NETWORK_socket_setsockopt
348 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
349 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
352 if ( (serverAddr->sa_family == AF_INET6) &&
353 (GNUNET_NETWORK_socket_setsockopt
354 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK) )
355 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
359 /* bind the socket */
360 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
363 if (errno != EADDRINUSE)
365 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
366 fail if we already took the port on IPv6; if both IPv4 and
367 IPv6 binds fail, then our caller will log using the
368 errno preserved in 'eno' */
369 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
373 ("`%s' failed for port %d (%s).\n"),
375 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
381 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
383 ("`%s' failed for port %d (%s): address already in use\n"),
385 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
386 else if (serverAddr->sa_family == AF_UNIX)
387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
389 ("`%s' failed for `%s': address already in use\n"),
391 ((const struct sockaddr_un*) serverAddr)->sun_path);
394 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
398 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
400 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
401 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Server starts to listen on port %u.\n", port);
415 * Create a new server.
417 * @param access function for access control
418 * @param access_cls closure for access
419 * @param lsocks NULL-terminated array of listen sockets
420 * @param idle_timeout after how long should we timeout idle connections?
421 * @param require_found if YES, connections sending messages of unknown type
423 * @return handle for the new server, NULL on error
424 * (typically, "port" already in use)
426 struct GNUNET_SERVER_Handle *
427 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
428 struct GNUNET_NETWORK_Handle **lsocks,
429 struct GNUNET_TIME_Relative
433 struct GNUNET_SERVER_Handle *ret;
434 struct GNUNET_NETWORK_FDSet *r;
437 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
438 ret->idle_timeout = idle_timeout;
439 ret->listen_sockets = lsocks;
440 ret->access = access;
441 ret->access_cls = access_cls;
442 ret->require_found = require_found;
445 r = GNUNET_NETWORK_fdset_create ();
447 while (NULL != ret->listen_sockets[i])
448 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]);
449 ret->listen_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
450 GNUNET_SCHEDULER_NO_TASK,
451 GNUNET_TIME_UNIT_FOREVER_REL,
453 &process_listen_socket,
455 GNUNET_NETWORK_fdset_destroy (r);
462 * Create a new server.
464 * @param access function for access control
465 * @param access_cls closure for access
466 * @param serverAddr address to listen on (including port), NULL terminated array
467 * @param socklen length of serverAddr
468 * @param idle_timeout after how long should we timeout idle connections?
469 * @param require_found if YES, connections sending messages of unknown type
471 * @return handle for the new server, NULL on error
472 * (typically, "port" already in use)
474 struct GNUNET_SERVER_Handle *
475 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access,
477 struct sockaddr *const *serverAddr,
478 const socklen_t * socklen,
479 struct GNUNET_TIME_Relative
480 idle_timeout, int require_found)
482 struct GNUNET_NETWORK_Handle **lsocks;
487 while (serverAddr[i] != NULL)
492 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
495 while (serverAddr[i] != NULL)
497 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
498 if (lsocks[j] != NULL)
505 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
506 GNUNET_free (lsocks);
514 return GNUNET_SERVER_create_with_sockets (access, access_cls,
522 * Free resources held by this server.
524 * @param s server to destroy
527 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
529 struct HandlerList *hpos;
530 struct NotifyList *npos;
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
536 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
538 GNUNET_SCHEDULER_cancel (s->listen_task);
539 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
541 if (s->listen_sockets != NULL)
544 while (s->listen_sockets[i] != NULL)
545 GNUNET_break (GNUNET_OK ==
546 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
547 GNUNET_free (s->listen_sockets);
548 s->listen_sockets = NULL;
550 while (s->clients != NULL)
551 GNUNET_SERVER_client_disconnect (s->clients);
552 while (NULL != (hpos = s->handlers))
554 s->handlers = hpos->next;
557 while (NULL != (npos = s->disconnect_notify_list))
559 npos->callback (npos->callback_cls, NULL);
560 s->disconnect_notify_list = npos->next;
568 * Add additional handlers to an existing server.
570 * @param server the server to add handlers to
571 * @param handlers array of message handlers for
572 * incoming messages; the last entry must
573 * have "NULL" for the "callback"; multiple
574 * entries for the same type are allowed,
575 * they will be called in order of occurence.
576 * These handlers can be removed later;
577 * the handlers array must exist until removed
578 * (or server is destroyed).
581 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
582 const struct GNUNET_SERVER_MessageHandler
585 struct HandlerList *p;
587 p = GNUNET_malloc (sizeof (struct HandlerList));
588 p->handlers = handlers;
589 p->next = server->handlers;
590 server->handlers = p;
595 * Task run to warn about missing calls to 'GNUNET_SERVER_receive_done'.
597 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
598 * @param tc scheduler context (unused)
601 warn_no_receive_done (void *cls,
602 const struct GNUNET_SCHEDULER_TaskContext *tc)
604 struct GNUNET_SERVER_Client *client = cls;
606 client->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
607 &warn_no_receive_done,
609 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
610 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
611 _("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"),
612 (unsigned int) client->warn_type,
613 (unsigned long long) GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value);
618 * Disable the warning the server issues if a message is not acknowledged
619 * in a timely fashion. Use this call if a client is intentionally delayed
620 * for a while. Only applies to the current message.
622 * @param client client for which to disable the warning
625 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
627 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
629 GNUNET_SCHEDULER_cancel (client->warn_task);
630 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
636 * Inject a message into the server, pretend it came
637 * from the specified client. Delivery of the message
638 * will happen instantly (if a handler is installed;
639 * otherwise the call does nothing).
641 * @param server the server receiving the message
642 * @param sender the "pretended" sender of the message
644 * @param message message to transmit
645 * @return GNUNET_OK if the message was OK and the
646 * connection can stay open
647 * GNUNET_SYSERR if the connection to the
648 * client should be shut down
651 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
652 struct GNUNET_SERVER_Client *sender,
653 const struct GNUNET_MessageHeader *message)
655 struct HandlerList *pos;
656 const struct GNUNET_SERVER_MessageHandler *mh;
662 type = ntohs (message->type);
663 size = ntohs (message->size);
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Server schedules transmission of %u-byte message of type %u to client.\n",
669 pos = server->handlers;
674 while (pos->handlers[i].callback != NULL)
676 mh = &pos->handlers[i];
677 if ( (mh->type == type) ||
678 (mh->type == GNUNET_MESSAGE_TYPE_ALL) )
680 if ((mh->expected_size != 0) && (mh->expected_size != size))
682 #if GNUNET8_NETWORK_IS_DEAD
683 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
684 "Expected %u bytes for message of type %u, got %u\n",
690 return GNUNET_SYSERR;
694 if (0 == sender->suspended)
696 sender->warn_start = GNUNET_TIME_absolute_get ();
697 sender->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
698 &warn_no_receive_done,
700 sender->warn_type = type;
704 mh->callback (mh->callback_cls, sender, message);
711 if (found == GNUNET_NO)
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
714 "Received message of unknown type %d\n",
716 if (server->require_found == GNUNET_YES)
717 return GNUNET_SYSERR;
724 * We are receiving an incoming message. Process it.
726 * @param cls our closure (handle for the client)
727 * @param buf buffer with data received from network
728 * @param available number of bytes available in buf
729 * @param addr address of the sender
730 * @param addrlen length of addr
731 * @param errCode code indicating errors receiving, 0 for success
734 process_incoming (void *cls,
737 const struct sockaddr *addr,
738 socklen_t addrlen, int errCode);
742 * Process messages from the client's message tokenizer until either
743 * the tokenizer is empty (and then schedule receiving more), or
744 * until some handler is not immediately done (then wait for restart_processing)
747 * @param client the client to process, RC must have already been increased
748 * using GNUNET_SERVER_client_keep and will be decreased by one in this
750 * @param ret GNUNET_NO to start processing from the buffer,
751 * GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
752 * GNUNET_SYSERR if we should instantly abort due to error in a previous step
755 process_mst (struct GNUNET_SERVER_Client *client,
758 while ( (ret != GNUNET_SYSERR) &&
759 (client->server != NULL) &&
760 (GNUNET_YES != client->shutdown_now) &&
761 (0 == client->suspended) )
763 if (ret == GNUNET_OK)
765 client->receive_pending = GNUNET_YES;
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Server re-enters receive loop.\n");
770 GNUNET_CONNECTION_receive (client->connection,
771 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
772 client->server->idle_timeout,
773 &process_incoming, client);
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server processes additional messages instantly.\n");
779 ret = GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, GNUNET_YES);
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
786 client->shutdown_now,
790 if (ret == GNUNET_NO)
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
794 "Server has more data pending but is suspended.\n");
796 client->receive_pending = GNUNET_SYSERR; /* data pending */
798 if ( (ret == GNUNET_SYSERR) ||
799 (GNUNET_YES == client->shutdown_now) )
800 GNUNET_SERVER_client_disconnect (client);
801 GNUNET_SERVER_client_drop (client);
806 * We are receiving an incoming message. Process it.
808 * @param cls our closure (handle for the client)
809 * @param buf buffer with data received from network
810 * @param available number of bytes available in buf
811 * @param addr address of the sender
812 * @param addrlen length of addr
813 * @param errCode code indicating errors receiving, 0 for success
816 process_incoming (void *cls,
819 const struct sockaddr *addr,
820 socklen_t addrlen, int errCode)
822 struct GNUNET_SERVER_Client *client = cls;
823 struct GNUNET_SERVER_Handle *server = client->server;
826 GNUNET_assert (client->receive_pending == GNUNET_YES);
827 client->receive_pending = GNUNET_NO;
832 (client->shutdown_now == GNUNET_YES) ||
833 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
835 /* other side closed connection, error connecting, etc. */
836 GNUNET_SERVER_client_disconnect (client);
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841 "Server receives %u bytes from `%s'.\n",
842 (unsigned int) available,
843 GNUNET_a2s (addr, addrlen));
845 GNUNET_SERVER_client_keep (client);
846 client->last_activity = GNUNET_TIME_absolute_get ();
847 ret = GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, GNUNET_YES);
848 process_mst (client, ret);
853 * Task run to start again receiving from the network
854 * and process requests.
856 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
857 * @param tc scheduler context (unused)
860 restart_processing (void *cls,
861 const struct GNUNET_SCHEDULER_TaskContext *tc)
863 struct GNUNET_SERVER_Client *client = cls;
864 struct GNUNET_SERVER_Handle *server = client->server;
866 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
867 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
868 (GNUNET_NO == server->clients_ignore_shutdown) )
870 GNUNET_SERVER_client_disconnect (client);
873 if (client->receive_pending == GNUNET_NO)
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
877 "Server begins to read again from client.\n");
879 client->receive_pending = GNUNET_YES;
880 GNUNET_CONNECTION_receive (client->connection,
881 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
882 client->server->idle_timeout, &process_incoming, client);
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 "Server continues processing messages still in the buffer.\n");
889 GNUNET_SERVER_client_keep (client);
890 client->receive_pending = GNUNET_NO;
891 process_mst (client, GNUNET_NO);
896 * This function is called whenever our inbound message tokenizer has
897 * received a complete message.
899 * @param cls closure (struct GNUNET_SERVER_Handle)
900 * @param client identification of the client (struct GNUNET_SERVER_Client*)
901 * @param message the actual message
904 client_message_tokenizer_callback (void *cls,
906 const struct GNUNET_MessageHeader *message)
908 struct GNUNET_SERVER_Handle *server = cls;
909 struct GNUNET_SERVER_Client *sender = client;
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 "Tokenizer gives server message of type %u from client\n",
915 ntohs (message->type));
917 sender->in_process_client_buffer = GNUNET_YES;
918 ret = GNUNET_SERVER_inject (server, sender, message);
919 sender->in_process_client_buffer = GNUNET_NO;
920 if (GNUNET_OK != ret)
921 GNUNET_SERVER_client_disconnect (sender);
926 * Add a TCP socket-based connection to the set of handles managed by
927 * this server. Use this function for outgoing (P2P) connections that
928 * we initiated (and where this server should process incoming
931 * @param server the server to use
932 * @param connection the connection to manage (client must
933 * stop using this connection from now on)
934 * @return the client handle (client should call
935 * "client_drop" on the return value eventually)
937 struct GNUNET_SERVER_Client *
938 GNUNET_SERVER_connect_socket (struct
941 struct GNUNET_CONNECTION_Handle *connection)
943 struct GNUNET_SERVER_Client *client;
945 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
946 client->connection = connection;
947 client->mst = GNUNET_SERVER_mst_create (&client_message_tokenizer_callback,
949 client->reference_count = 1;
950 client->server = server;
951 client->last_activity = GNUNET_TIME_absolute_get ();
952 client->next = server->clients;
953 server->clients = client;
954 client->receive_pending = GNUNET_YES;
955 GNUNET_CONNECTION_receive (client->connection,
956 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
957 server->idle_timeout, &process_incoming, client);
963 * Notify the server that the given client handle should
964 * be kept (keeps the connection up if possible, increments
965 * the internal reference counter).
967 * @param client the client to keep
970 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
972 client->reference_count++;
977 * Notify the server that the given client handle is no
978 * longer required. Decrements the reference counter. If
979 * that counter reaches zero an inactive connection maybe
982 * @param client the client to drop
985 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
987 GNUNET_assert (client->reference_count > 0);
988 client->reference_count--;
989 if ( (client->shutdown_now == GNUNET_YES) &&
990 (client->reference_count == 0) )
991 GNUNET_SERVER_client_disconnect (client);
996 * Obtain the network address of the other party.
998 * @param client the client to get the address for
999 * @param addr where to store the address
1000 * @param addrlen where to store the length of the address
1001 * @return GNUNET_OK on success
1004 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1005 void **addr, size_t * addrlen)
1007 return GNUNET_CONNECTION_get_address (client->connection,
1013 * Ask the server to notify us whenever a client disconnects.
1014 * This function is called whenever the actual network connection
1015 * is closed; the reference count may be zero or larger than zero
1018 * @param server the server manageing the clients
1019 * @param callback function to call on disconnect
1020 * @param callback_cls closure for callback
1023 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1024 GNUNET_SERVER_DisconnectCallback callback,
1027 struct NotifyList *n;
1029 n = GNUNET_malloc (sizeof (struct NotifyList));
1030 n->callback = callback;
1031 n->callback_cls = callback_cls;
1032 n->next = server->disconnect_notify_list;
1033 server->disconnect_notify_list = n;
1038 * Ask the server to stop notifying us whenever a client disconnects.
1040 * @param server the server manageing the clients
1041 * @param callback function to call on disconnect
1042 * @param callback_cls closure for callback
1045 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1046 GNUNET_SERVER_DisconnectCallback callback,
1049 struct NotifyList *pos;
1050 struct NotifyList *prev;
1053 pos = server->disconnect_notify_list;
1056 if ( (pos->callback == callback) &&
1057 (pos->callback_cls == callback_cls ) )
1068 server->disconnect_notify_list = pos->next;
1070 prev->next = pos->next;
1076 * Ask the server to disconnect from the given client.
1077 * This is the same as returning GNUNET_SYSERR from a message
1078 * handler, except that it allows dropping of a client even
1079 * when not handling a message from that client.
1081 * @param client the client to disconnect from
1084 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1086 struct GNUNET_SERVER_Client *prev;
1087 struct GNUNET_SERVER_Client *pos;
1088 struct GNUNET_SERVER_Handle *server;
1089 struct NotifyList *n;
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "Client is being disconnected from the server.\n");
1096 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1098 GNUNET_SCHEDULER_cancel (client->restart_task);
1099 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1101 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1103 GNUNET_SCHEDULER_cancel (client->warn_task);
1104 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1106 if (GNUNET_YES == client->receive_pending)
1108 GNUNET_CONNECTION_receive_cancel (client->connection);
1109 client->receive_pending = GNUNET_NO;
1112 rc = client->reference_count;
1113 if (client->server != NULL)
1115 server = client->server;
1116 client->server = NULL;
1117 client->shutdown_now = GNUNET_YES;
1119 pos = server->clients;
1120 while ((pos != NULL) && (pos != client))
1125 GNUNET_assert (pos != NULL);
1127 server->clients = pos->next;
1129 prev->next = pos->next;
1130 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1132 GNUNET_SCHEDULER_cancel (client->restart_task);
1133 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1135 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1137 GNUNET_SCHEDULER_cancel (client->warn_task);
1138 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1140 n = server->disconnect_notify_list;
1143 n->callback (n->callback_cls, client);
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1151 "RC still positive, not destroying everything.\n");
1155 if (client->in_process_client_buffer == GNUNET_YES)
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "Still processing inputs, not destroying everything.\n");
1164 if (client->persist == GNUNET_YES)
1165 GNUNET_CONNECTION_persist_ (client->connection);
1166 GNUNET_CONNECTION_destroy (client->connection, GNUNET_NO);
1167 GNUNET_SERVER_mst_destroy (client->mst);
1168 GNUNET_free (client);
1173 * Disable the "CORK" feature for communication with the given client,
1174 * forcing the OS to immediately flush the buffer on transmission
1175 * instead of potentially buffering multiple messages.
1177 * @param client handle to the client
1178 * @return GNUNET_OK on success
1181 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client)
1183 return GNUNET_CONNECTION_disable_corking (client->connection);
1188 * Notify us when the server has enough space to transmit
1189 * a message of the given size to the given client.
1191 * @param client client to transmit message to
1192 * @param size requested amount of buffer space
1193 * @param timeout after how long should we give up (and call
1194 * notify with buf NULL and size 0)?
1195 * @param callback function to call when space is available
1196 * @param callback_cls closure for callback
1197 * @return non-NULL if the notify callback was queued; can be used
1198 * to cancel the request using
1199 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1200 * NULL if we are already going to notify someone else (busy)
1202 struct GNUNET_CONNECTION_TransmitHandle *
1203 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1205 struct GNUNET_TIME_Relative timeout,
1206 GNUNET_CONNECTION_TransmitReadyNotify
1207 callback, void *callback_cls)
1209 return GNUNET_CONNECTION_notify_transmit_ready (client->connection,
1211 timeout, callback, callback_cls);
1215 * Set the persistent flag on this client, used to setup client connection
1216 * to only be killed when the service it's connected to is actually dead.
1218 * @param client the client to set the persistent flag on
1221 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1223 client->persist = GNUNET_YES;
1227 * Resume receiving from this client, we are done processing the
1228 * current request. This function must be called from within each
1229 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1231 * @param client client we were processing a message of
1232 * @param success GNUNET_OK to keep the connection open and
1233 * continue to receive
1234 * GNUNET_NO to close the connection (normal behavior)
1235 * GNUNET_SYSERR to close the connection (signal
1239 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1243 GNUNET_assert (client->suspended > 0);
1244 client->suspended--;
1245 if (success != GNUNET_OK)
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "GNUNET_SERVER_receive_done called with failure indication\n");
1251 GNUNET_SERVER_client_disconnect (client);
1254 if (client->suspended > 0)
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1262 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1264 GNUNET_SCHEDULER_cancel (client->warn_task);
1265 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1267 if (client->in_process_client_buffer == GNUNET_YES)
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1275 if (client->server == NULL)
1277 GNUNET_SERVER_client_disconnect (client);
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1282 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1284 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1285 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing,
1291 * Configure this server's connections to continue handling client
1292 * requests as usual even after we get a shutdown signal. The change
1293 * only applies to clients that connect to the server from the outside
1294 * using TCP after this call. Clients managed previously or those
1295 * added using GNUNET_SERVER_connect_socket and
1296 * GNUNET_SERVER_connect_callback are not affected by this option.
1298 * @param h server handle
1299 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1302 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore)
1304 h->clients_ignore_shutdown = do_ignore;
1307 /* end of server.c */