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;
190 GNUNET_CONNECTION_TransmitReadyNotify callback;
198 * After how long should an idle connection time
201 struct GNUNET_TIME_Relative idle_timeout;
204 * Number of external entities with a reference to
205 * this client object.
207 unsigned int reference_count;
210 * Was processing if incoming messages suspended while
211 * we were still processing data already received?
212 * This is a counter saying how often processing was
213 * suspended (once per handler invoked).
215 unsigned int suspended;
218 * Are we currently in the "process_client_buffer" function (and
219 * will hence restart the receive job on exit if suspended == 0 once
220 * we are done?). If this is set, then "receive_done" will
221 * essentially only decrement suspended; if this is not set, then
222 * "receive_done" may need to restart the receive process (either
223 * from the side-buffer or via select/recv).
225 int in_process_client_buffer;
228 * We're about to close down this client due to some serious
234 * Are we currently trying to receive? (YES if we are, NO if we are not,
235 * SYSERR if data is already available in MST).
240 * Persist the file handle for this client no matter what happens,
241 * force the OS to close once the process actually dies. Should only
242 * be used in special cases!
247 * Type of last message processed (for warn_no_receive_done).
254 * Scheduler says our listen socket is ready. Process it!
256 * @param cls handle to our server for which we are processing the listen
258 * @param tc reason why we are running right now
261 process_listen_socket (void *cls,
262 const struct GNUNET_SCHEDULER_TaskContext *tc)
264 struct GNUNET_SERVER_Handle *server = cls;
265 struct GNUNET_CONNECTION_Handle *sock;
266 struct GNUNET_SERVER_Client *client;
267 struct GNUNET_NETWORK_FDSet *r;
270 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
271 r = GNUNET_NETWORK_fdset_create ();
273 while (NULL != server->listen_sockets[i])
274 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
275 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
277 /* ignore shutdown, someone else will take care of it! */
278 server->listen_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
279 GNUNET_SCHEDULER_NO_TASK,
280 GNUNET_TIME_UNIT_FOREVER_REL,
282 &process_listen_socket,
284 GNUNET_NETWORK_fdset_destroy (r);
288 while (NULL != server->listen_sockets[i])
290 if (GNUNET_NETWORK_fdset_isset
291 (tc->read_ready, server->listen_sockets[i]))
294 GNUNET_CONNECTION_create_from_accept (server->access,
296 server->listen_sockets[i]);
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Server accepted incoming connection.\n");
303 client = GNUNET_SERVER_connect_socket (server, sock);
304 GNUNET_CONNECTION_ignore_shutdown (sock,
305 server->clients_ignore_shutdown);
306 /* decrement reference count, we don't keep "client" alive */
307 GNUNET_SERVER_client_drop (client);
312 /* listen for more! */
313 server->listen_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
314 GNUNET_SCHEDULER_NO_TASK,
315 GNUNET_TIME_UNIT_FOREVER_REL,
317 &process_listen_socket,
319 GNUNET_NETWORK_fdset_destroy (r);
324 * Create and initialize a listen socket for the server.
326 * @param serverAddr address to listen on
327 * @param socklen length of address
328 * @return NULL on error, otherwise the listen socket
330 static struct GNUNET_NETWORK_Handle *
331 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
333 const static int on = 1;
334 struct GNUNET_NETWORK_Handle *sock;
338 switch (serverAddr->sa_family)
341 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
344 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
354 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
357 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
363 if (GNUNET_NETWORK_socket_setsockopt
364 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
365 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
368 if ( (serverAddr->sa_family == AF_INET6) &&
369 (GNUNET_NETWORK_socket_setsockopt
370 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK) )
371 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
375 /* bind the socket */
376 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
379 if (errno != EADDRINUSE)
381 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
382 fail if we already took the port on IPv6; if both IPv4 and
383 IPv6 binds fail, then our caller will log using the
384 errno preserved in 'eno' */
385 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389 ("`%s' failed for port %d (%s).\n"),
391 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
397 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
399 ("`%s' failed for port %d (%s): address already in use\n"),
401 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
402 else if (serverAddr->sa_family == AF_UNIX)
403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
405 ("`%s' failed for `%s': address already in use\n"),
407 ((const struct sockaddr_un*) serverAddr)->sun_path);
410 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
414 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
416 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
417 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Server starts to listen on port %u.\n", port);
431 * Create a new server.
433 * @param access function for access control
434 * @param access_cls closure for access
435 * @param lsocks NULL-terminated array of listen sockets
436 * @param idle_timeout after how long should we timeout idle connections?
437 * @param require_found if YES, connections sending messages of unknown type
439 * @return handle for the new server, NULL on error
440 * (typically, "port" already in use)
442 struct GNUNET_SERVER_Handle *
443 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
444 struct GNUNET_NETWORK_Handle **lsocks,
445 struct GNUNET_TIME_Relative
449 struct GNUNET_SERVER_Handle *ret;
450 struct GNUNET_NETWORK_FDSet *r;
453 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
454 ret->idle_timeout = idle_timeout;
455 ret->listen_sockets = lsocks;
456 ret->access = access;
457 ret->access_cls = access_cls;
458 ret->require_found = require_found;
461 r = GNUNET_NETWORK_fdset_create ();
463 while (NULL != ret->listen_sockets[i])
464 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]);
465 ret->listen_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
466 GNUNET_SCHEDULER_NO_TASK,
467 GNUNET_TIME_UNIT_FOREVER_REL,
469 &process_listen_socket,
471 GNUNET_NETWORK_fdset_destroy (r);
478 * Create a new server.
480 * @param access function for access control
481 * @param access_cls closure for access
482 * @param serverAddr address to listen on (including port), NULL terminated array
483 * @param socklen length of serverAddr
484 * @param idle_timeout after how long should we timeout idle connections?
485 * @param require_found if YES, connections sending messages of unknown type
487 * @return handle for the new server, NULL on error
488 * (typically, "port" already in use)
490 struct GNUNET_SERVER_Handle *
491 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access,
493 struct sockaddr *const *serverAddr,
494 const socklen_t * socklen,
495 struct GNUNET_TIME_Relative
496 idle_timeout, int require_found)
498 struct GNUNET_NETWORK_Handle **lsocks;
503 while (serverAddr[i] != NULL)
508 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
511 while (serverAddr[i] != NULL)
513 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
514 if (lsocks[j] != NULL)
521 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
522 GNUNET_free (lsocks);
530 return GNUNET_SERVER_create_with_sockets (access, access_cls,
538 * Free resources held by this server.
540 * @param s server to destroy
543 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
545 struct HandlerList *hpos;
546 struct NotifyList *npos;
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
552 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
554 GNUNET_SCHEDULER_cancel (s->listen_task);
555 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
557 if (s->listen_sockets != NULL)
560 while (s->listen_sockets[i] != NULL)
561 GNUNET_break (GNUNET_OK ==
562 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
563 GNUNET_free (s->listen_sockets);
564 s->listen_sockets = NULL;
566 while (s->clients != NULL)
567 GNUNET_SERVER_client_disconnect (s->clients);
568 while (NULL != (hpos = s->handlers))
570 s->handlers = hpos->next;
573 while (NULL != (npos = s->disconnect_notify_list))
575 npos->callback (npos->callback_cls, NULL);
576 s->disconnect_notify_list = npos->next;
584 * Add additional handlers to an existing server.
586 * @param server the server to add handlers to
587 * @param handlers array of message handlers for
588 * incoming messages; the last entry must
589 * have "NULL" for the "callback"; multiple
590 * entries for the same type are allowed,
591 * they will be called in order of occurence.
592 * These handlers can be removed later;
593 * the handlers array must exist until removed
594 * (or server is destroyed).
597 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
598 const struct GNUNET_SERVER_MessageHandler
601 struct HandlerList *p;
603 p = GNUNET_malloc (sizeof (struct HandlerList));
604 p->handlers = handlers;
605 p->next = server->handlers;
606 server->handlers = p;
611 * Task run to warn about missing calls to 'GNUNET_SERVER_receive_done'.
613 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
614 * @param tc scheduler context (unused)
617 warn_no_receive_done (void *cls,
618 const struct GNUNET_SCHEDULER_TaskContext *tc)
620 struct GNUNET_SERVER_Client *client = cls;
622 client->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
623 &warn_no_receive_done,
625 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
627 _("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"),
628 (unsigned int) client->warn_type,
629 (unsigned long long) GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value);
634 * Disable the warning the server issues if a message is not acknowledged
635 * in a timely fashion. Use this call if a client is intentionally delayed
636 * for a while. Only applies to the current message.
638 * @param client client for which to disable the warning
641 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
643 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
645 GNUNET_SCHEDULER_cancel (client->warn_task);
646 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
652 * Inject a message into the server, pretend it came
653 * from the specified client. Delivery of the message
654 * will happen instantly (if a handler is installed;
655 * otherwise the call does nothing).
657 * @param server the server receiving the message
658 * @param sender the "pretended" sender of the message
660 * @param message message to transmit
661 * @return GNUNET_OK if the message was OK and the
662 * connection can stay open
663 * GNUNET_SYSERR if the connection to the
664 * client should be shut down
667 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
668 struct GNUNET_SERVER_Client *sender,
669 const struct GNUNET_MessageHeader *message)
671 struct HandlerList *pos;
672 const struct GNUNET_SERVER_MessageHandler *mh;
678 type = ntohs (message->type);
679 size = ntohs (message->size);
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Server schedules transmission of %u-byte message of type %u to client.\n",
685 pos = server->handlers;
690 while (pos->handlers[i].callback != NULL)
692 mh = &pos->handlers[i];
693 if ( (mh->type == type) ||
694 (mh->type == GNUNET_MESSAGE_TYPE_ALL) )
696 if ((mh->expected_size != 0) && (mh->expected_size != size))
698 #if GNUNET8_NETWORK_IS_DEAD
699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
700 "Expected %u bytes for message of type %u, got %u\n",
706 return GNUNET_SYSERR;
710 if (0 == sender->suspended)
712 sender->warn_start = GNUNET_TIME_absolute_get ();
713 sender->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
714 &warn_no_receive_done,
716 sender->warn_type = type;
720 mh->callback (mh->callback_cls, sender, message);
727 if (found == GNUNET_NO)
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
730 "Received message of unknown type %d\n",
732 if (server->require_found == GNUNET_YES)
733 return GNUNET_SYSERR;
740 * We are receiving an incoming message. Process it.
742 * @param cls our closure (handle for the client)
743 * @param buf buffer with data received from network
744 * @param available number of bytes available in buf
745 * @param addr address of the sender
746 * @param addrlen length of addr
747 * @param errCode code indicating errors receiving, 0 for success
750 process_incoming (void *cls,
753 const struct sockaddr *addr,
754 socklen_t addrlen, int errCode);
758 * Process messages from the client's message tokenizer until either
759 * the tokenizer is empty (and then schedule receiving more), or
760 * until some handler is not immediately done (then wait for restart_processing)
763 * @param client the client to process, RC must have already been increased
764 * using GNUNET_SERVER_client_keep and will be decreased by one in this
766 * @param ret GNUNET_NO to start processing from the buffer,
767 * GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
768 * GNUNET_SYSERR if we should instantly abort due to error in a previous step
771 process_mst (struct GNUNET_SERVER_Client *client,
774 while ( (ret != GNUNET_SYSERR) &&
775 (client->server != NULL) &&
776 (GNUNET_YES != client->shutdown_now) &&
777 (0 == client->suspended) )
779 if (ret == GNUNET_OK)
781 client->receive_pending = GNUNET_YES;
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "Server re-enters receive loop, timeout: %llu.\n", client->server->idle_timeout.rel_value);
786 GNUNET_CONNECTION_receive (client->connection,
787 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
788 client->idle_timeout,
789 &process_incoming, client);
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server processes additional messages instantly.\n");
795 ret = GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, GNUNET_YES);
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
802 client->shutdown_now,
806 if (ret == GNUNET_NO)
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Server has more data pending but is suspended.\n");
812 client->receive_pending = GNUNET_SYSERR; /* data pending */
814 if ( (ret == GNUNET_SYSERR) ||
815 (GNUNET_YES == client->shutdown_now) )
816 GNUNET_SERVER_client_disconnect (client);
817 GNUNET_SERVER_client_drop (client);
822 * We are receiving an incoming message. Process it.
824 * @param cls our closure (handle for the client)
825 * @param buf buffer with data received from network
826 * @param available number of bytes available in buf
827 * @param addr address of the sender
828 * @param addrlen length of addr
829 * @param errCode code indicating errors receiving, 0 for success
832 process_incoming (void *cls,
835 const struct sockaddr *addr,
836 socklen_t addrlen, int errCode)
838 struct GNUNET_SERVER_Client *client = cls;
839 struct GNUNET_SERVER_Handle *server = client->server;
840 struct GNUNET_TIME_Absolute start;
843 GNUNET_assert (client->receive_pending == GNUNET_YES);
844 client->receive_pending = GNUNET_NO;
845 start = GNUNET_TIME_absolute_subtract(GNUNET_TIME_absolute_get(),client->idle_timeout);
848 if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) &&
849 (client->last_activity.abs_value == GNUNET_TIME_absolute_max(start, client->last_activity).abs_value))
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "Receive time out, but no disconnect due to sending (%p)\n",
855 GNUNET_a2s (addr, addrlen));
857 GNUNET_SERVER_client_keep (client);
858 client->last_activity = GNUNET_TIME_absolute_get ();
859 process_mst (client, ret);
867 (client->shutdown_now == GNUNET_YES) ||
868 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
870 /* other side closed connection, error connecting, etc. */
871 GNUNET_SERVER_client_disconnect (client);
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Server receives %u bytes from `%s'.\n",
877 (unsigned int) available,
878 GNUNET_a2s (addr, addrlen));
880 GNUNET_SERVER_client_keep (client);
881 client->last_activity = GNUNET_TIME_absolute_get ();
882 ret = GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, GNUNET_YES);
883 process_mst (client, ret);
888 * Task run to start again receiving from the network
889 * and process requests.
891 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
892 * @param tc scheduler context (unused)
895 restart_processing (void *cls,
896 const struct GNUNET_SCHEDULER_TaskContext *tc)
898 struct GNUNET_SERVER_Client *client = cls;
899 struct GNUNET_SERVER_Handle *server = client->server;
901 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
902 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
903 (GNUNET_NO == server->clients_ignore_shutdown) )
905 GNUNET_SERVER_client_disconnect (client);
908 if (client->receive_pending == GNUNET_NO)
911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
912 "Server begins to read again from client.\n");
914 client->receive_pending = GNUNET_YES;
915 GNUNET_CONNECTION_receive (client->connection,
916 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
917 client->idle_timeout, &process_incoming, client);
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Server continues processing messages still in the buffer.\n");
925 GNUNET_SERVER_client_keep (client);
926 client->receive_pending = GNUNET_NO;
927 process_mst (client, GNUNET_NO);
932 * This function is called whenever our inbound message tokenizer has
933 * received a complete message.
935 * @param cls closure (struct GNUNET_SERVER_Handle)
936 * @param client identification of the client (struct GNUNET_SERVER_Client*)
937 * @param message the actual message
940 client_message_tokenizer_callback (void *cls,
942 const struct GNUNET_MessageHeader *message)
944 struct GNUNET_SERVER_Handle *server = cls;
945 struct GNUNET_SERVER_Client *sender = client;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Tokenizer gives server message of type %u from client\n",
951 ntohs (message->type));
953 sender->in_process_client_buffer = GNUNET_YES;
954 ret = GNUNET_SERVER_inject (server, sender, message);
955 sender->in_process_client_buffer = GNUNET_NO;
956 if (GNUNET_OK != ret)
957 GNUNET_SERVER_client_disconnect (sender);
962 * Add a TCP socket-based connection to the set of handles managed by
963 * this server. Use this function for outgoing (P2P) connections that
964 * we initiated (and where this server should process incoming
967 * @param server the server to use
968 * @param connection the connection to manage (client must
969 * stop using this connection from now on)
970 * @return the client handle (client should call
971 * "client_drop" on the return value eventually)
973 struct GNUNET_SERVER_Client *
974 GNUNET_SERVER_connect_socket (struct
977 struct GNUNET_CONNECTION_Handle *connection)
979 struct GNUNET_SERVER_Client *client;
981 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
982 client->connection = connection;
983 client->mst = GNUNET_SERVER_mst_create (&client_message_tokenizer_callback,
985 client->reference_count = 1;
986 client->server = server;
987 client->last_activity = GNUNET_TIME_absolute_get ();
988 client->next = server->clients;
989 client->idle_timeout = server->idle_timeout;
990 server->clients = client;
991 client->receive_pending = GNUNET_YES;
992 client->callback = NULL;
993 client->callback_cls = NULL;
994 GNUNET_CONNECTION_receive (client->connection,
995 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
996 client->idle_timeout, &process_incoming, client);
1002 * Change the timeout for a particular client. Decreasing the timeout
1003 * may not go into effect immediately (only after the previous timeout
1004 * times out or activity happens on the socket).
1006 * @param client the client to update
1007 * @param timeout new timeout for activities on the socket
1010 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
1011 struct GNUNET_TIME_Relative timeout)
1013 client->idle_timeout = timeout;
1018 * Notify the server that the given client handle should
1019 * be kept (keeps the connection up if possible, increments
1020 * the internal reference counter).
1022 * @param client the client to keep
1025 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1027 client->reference_count++;
1032 * Notify the server that the given client handle is no
1033 * longer required. Decrements the reference counter. If
1034 * that counter reaches zero an inactive connection maybe
1037 * @param client the client to drop
1040 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1042 GNUNET_assert (client->reference_count > 0);
1043 client->reference_count--;
1044 if ( (client->shutdown_now == GNUNET_YES) &&
1045 (client->reference_count == 0) )
1046 GNUNET_SERVER_client_disconnect (client);
1051 * Obtain the network address of the other party.
1053 * @param client the client to get the address for
1054 * @param addr where to store the address
1055 * @param addrlen where to store the length of the address
1056 * @return GNUNET_OK on success
1059 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1060 void **addr, size_t * addrlen)
1062 return GNUNET_CONNECTION_get_address (client->connection,
1068 * Ask the server to notify us whenever a client disconnects.
1069 * This function is called whenever the actual network connection
1070 * is closed; the reference count may be zero or larger than zero
1073 * @param server the server manageing the clients
1074 * @param callback function to call on disconnect
1075 * @param callback_cls closure for callback
1078 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1079 GNUNET_SERVER_DisconnectCallback callback,
1082 struct NotifyList *n;
1084 n = GNUNET_malloc (sizeof (struct NotifyList));
1085 n->callback = callback;
1086 n->callback_cls = callback_cls;
1087 n->next = server->disconnect_notify_list;
1088 server->disconnect_notify_list = n;
1093 * Ask the server to stop notifying us whenever a client disconnects.
1095 * @param server the server manageing the clients
1096 * @param callback function to call on disconnect
1097 * @param callback_cls closure for callback
1100 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1101 GNUNET_SERVER_DisconnectCallback callback,
1104 struct NotifyList *pos;
1105 struct NotifyList *prev;
1108 pos = server->disconnect_notify_list;
1111 if ( (pos->callback == callback) &&
1112 (pos->callback_cls == callback_cls ) )
1123 server->disconnect_notify_list = pos->next;
1125 prev->next = pos->next;
1131 * Ask the server to disconnect from the given client.
1132 * This is the same as returning GNUNET_SYSERR from a message
1133 * handler, except that it allows dropping of a client even
1134 * when not handling a message from that client.
1136 * @param client the client to disconnect from
1139 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1141 struct GNUNET_SERVER_Client *prev;
1142 struct GNUNET_SERVER_Client *pos;
1143 struct GNUNET_SERVER_Handle *server;
1144 struct NotifyList *n;
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 "Client is being disconnected from the server.\n");
1151 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1153 GNUNET_SCHEDULER_cancel (client->restart_task);
1154 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1156 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1158 GNUNET_SCHEDULER_cancel (client->warn_task);
1159 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1161 if (GNUNET_YES == client->receive_pending)
1163 GNUNET_CONNECTION_receive_cancel (client->connection);
1164 client->receive_pending = GNUNET_NO;
1167 rc = client->reference_count;
1168 if (client->server != NULL)
1170 server = client->server;
1171 client->server = NULL;
1172 client->shutdown_now = GNUNET_YES;
1174 pos = server->clients;
1175 while ((pos != NULL) && (pos != client))
1180 GNUNET_assert (pos != NULL);
1182 server->clients = pos->next;
1184 prev->next = pos->next;
1185 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1187 GNUNET_SCHEDULER_cancel (client->restart_task);
1188 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1190 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1192 GNUNET_SCHEDULER_cancel (client->warn_task);
1193 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1195 n = server->disconnect_notify_list;
1198 n->callback (n->callback_cls, client);
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "RC still positive, not destroying everything.\n");
1210 if (client->in_process_client_buffer == GNUNET_YES)
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214 "Still processing inputs, not destroying everything.\n");
1219 if (client->persist == GNUNET_YES)
1220 GNUNET_CONNECTION_persist_ (client->connection);
1221 GNUNET_CONNECTION_destroy (client->connection, GNUNET_NO);
1222 GNUNET_SERVER_mst_destroy (client->mst);
1223 GNUNET_free (client);
1228 * Disable the "CORK" feature for communication with the given client,
1229 * forcing the OS to immediately flush the buffer on transmission
1230 * instead of potentially buffering multiple messages.
1232 * @param client handle to the client
1233 * @return GNUNET_OK on success
1236 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client)
1238 return GNUNET_CONNECTION_disable_corking (client->connection);
1241 size_t transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
1244 struct GNUNET_SERVER_Client *client = cls;
1246 GNUNET_CONNECTION_TransmitReadyNotify callback = client->callback;
1247 void * callback_cls = client->callback_cls;
1249 client->last_activity = GNUNET_TIME_absolute_get();
1250 client->callback = NULL;
1251 client->callback_cls = NULL;
1253 return callback (callback_cls, size, buf);
1257 * Notify us when the server has enough space to transmit
1258 * a message of the given size to the given client.
1260 * @param client client to transmit message to
1261 * @param size requested amount of buffer space
1262 * @param timeout after how long should we give up (and call
1263 * notify with buf NULL and size 0)?
1264 * @param callback function to call when space is available
1265 * @param callback_cls closure for callback
1266 * @return non-NULL if the notify callback was queued; can be used
1267 * to cancel the request using
1268 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1269 * NULL if we are already going to notify someone else (busy)
1271 struct GNUNET_CONNECTION_TransmitHandle *
1272 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1274 struct GNUNET_TIME_Relative timeout,
1275 GNUNET_CONNECTION_TransmitReadyNotify
1276 callback, void *callback_cls)
1278 GNUNET_assert (client->callback == NULL);
1280 client->callback_cls = callback_cls;
1281 client->callback = callback;
1283 return GNUNET_CONNECTION_notify_transmit_ready (client->connection,
1285 timeout, transmit_ready_callback_wrapper, client);
1291 * Set the persistent flag on this client, used to setup client connection
1292 * to only be killed when the service it's connected to is actually dead.
1294 * @param client the client to set the persistent flag on
1297 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1299 client->persist = GNUNET_YES;
1303 * Resume receiving from this client, we are done processing the
1304 * current request. This function must be called from within each
1305 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1307 * @param client client we were processing a message of
1308 * @param success GNUNET_OK to keep the connection open and
1309 * continue to receive
1310 * GNUNET_NO to close the connection (normal behavior)
1311 * GNUNET_SYSERR to close the connection (signal
1315 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1319 GNUNET_assert (client->suspended > 0);
1320 client->suspended--;
1321 if (success != GNUNET_OK)
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "GNUNET_SERVER_receive_done called with failure indication\n");
1327 GNUNET_SERVER_client_disconnect (client);
1330 if (client->suspended > 0)
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1338 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1340 GNUNET_SCHEDULER_cancel (client->warn_task);
1341 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1343 if (client->in_process_client_buffer == GNUNET_YES)
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1351 if (client->server == NULL)
1353 GNUNET_SERVER_client_disconnect (client);
1357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1358 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1360 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1361 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing,
1367 * Configure this server's connections to continue handling client
1368 * requests as usual even after we get a shutdown signal. The change
1369 * only applies to clients that connect to the server from the outside
1370 * using TCP after this call. Clients managed previously or those
1371 * added using GNUNET_SERVER_connect_socket and
1372 * GNUNET_SERVER_connect_callback are not affected by this option.
1374 * @param h server handle
1375 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1378 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore)
1380 h->clients_ignore_shutdown = do_ignore;
1383 /* end of server.c */