2 This file is part of GNUnet.
3 (C) 2009, 2012 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_util_lib.h"
30 #include "gnunet_protocols.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
40 * List of arrays of message handlers.
45 * This is a linked list.
47 struct HandlerList *next;
50 * NULL-terminated array of handlers.
52 const struct GNUNET_SERVER_MessageHandler *handlers;
57 * List of arrays of message handlers.
62 * This is a doubly linked list.
64 struct NotifyList *next;
67 * This is a doubly linked list.
69 struct NotifyList *prev;
74 GNUNET_SERVER_DisconnectCallback callback;
77 * Closure for callback.
84 * @brief handle for a server
86 struct GNUNET_SERVER_Handle
89 * List of handlers for incoming messages.
91 struct HandlerList *handlers;
94 * Head of list of our current clients.
96 struct GNUNET_SERVER_Client *clients_head;
99 * Head of list of our current clients.
101 struct GNUNET_SERVER_Client *clients_tail;
104 * Head of linked list of functions to call on disconnects by clients.
106 struct NotifyList *disconnect_notify_list_head;
109 * Tail of linked list of functions to call on disconnects by clients.
111 struct NotifyList *disconnect_notify_list_tail;
114 * Head of linked list of functions to call on connects by clients.
116 struct NotifyList *connect_notify_list_head;
119 * Tail of linked list of functions to call on connects by clients.
121 struct NotifyList *connect_notify_list_tail;
124 * Function to call for access control.
126 GNUNET_CONNECTION_AccessCheck access;
129 * Closure for access.
134 * NULL-terminated array of sockets used to listen for new
137 struct GNUNET_NETWORK_Handle **listen_sockets;
140 * After how long should an idle connection time
143 struct GNUNET_TIME_Relative idle_timeout;
146 * Task scheduled to do the listening.
148 GNUNET_SCHEDULER_TaskIdentifier listen_task;
151 * Alternative function to create a MST instance.
153 GNUNET_SERVER_MstCreateCallback mst_create;
156 * Alternative function to destroy a MST instance.
158 GNUNET_SERVER_MstDestroyCallback mst_destroy;
161 * Alternative function to give data to a MST instance.
163 GNUNET_SERVER_MstReceiveCallback mst_receive;
166 * Closure for 'mst_'-callbacks.
171 * Do we ignore messages of types that we do not understand or do we
172 * require that a handler is found (and if not kill the connection)?
177 * Set to GNUNET_YES once we are in 'soft' shutdown where we wait for
178 * all non-monitor clients to disconnect before we call
179 * GNUNET_SERVER_destroy. See 'test_monitor_clients'. Set to
180 * GNUNET_SYSERR once the final destroy task has been scheduled
181 * (we cannot run it in the same task).
183 int in_soft_shutdown;
188 * Handle server returns for aborting transmission to a client.
190 struct GNUNET_SERVER_TransmitHandle
193 * Function to call to get the message.
195 GNUNET_CONNECTION_TransmitReadyNotify callback;
198 * Closure for 'callback'
203 * Active connection transmission handle.
205 struct GNUNET_CONNECTION_TransmitHandle *cth;
211 * @brief handle for a client of the server
213 struct GNUNET_SERVER_Client
217 * This is a doubly linked list.
219 struct GNUNET_SERVER_Client *next;
222 * This is a doubly linked list.
224 struct GNUNET_SERVER_Client *prev;
227 * Processing of incoming data.
232 * Server that this client belongs to.
234 struct GNUNET_SERVER_Handle *server;
237 * Client closure for callbacks.
239 struct GNUNET_CONNECTION_Handle *connection;
242 * User context value, manipulated using
243 * 'GNUNET_SERVER_client_{get/set}_user_context' functions.
248 * ID of task used to restart processing.
250 GNUNET_SCHEDULER_TaskIdentifier restart_task;
253 * Task that warns about missing calls to 'GNUNET_SERVER_receive_done'.
255 GNUNET_SCHEDULER_TaskIdentifier warn_task;
258 * Time when the warn task was started.
260 struct GNUNET_TIME_Absolute warn_start;
263 * Last activity on this socket (used to time it out
264 * if reference_count == 0).
266 struct GNUNET_TIME_Absolute last_activity;
269 * Transmission handle we return for this client from
270 * GNUNET_SERVER_notify_transmit_ready.
272 struct GNUNET_SERVER_TransmitHandle th;
275 * After how long should an idle connection time
278 struct GNUNET_TIME_Relative idle_timeout;
281 * Number of external entities with a reference to
282 * this client object.
284 unsigned int reference_count;
287 * Was processing if incoming messages suspended while
288 * we were still processing data already received?
289 * This is a counter saying how often processing was
290 * suspended (once per handler invoked).
292 unsigned int suspended;
295 * Last size given when user context was initialized; used for
298 size_t user_context_size;
301 * Are we currently in the "process_client_buffer" function (and
302 * will hence restart the receive job on exit if suspended == 0 once
303 * we are done?). If this is set, then "receive_done" will
304 * essentially only decrement suspended; if this is not set, then
305 * "receive_done" may need to restart the receive process (either
306 * from the side-buffer or via select/recv).
308 int in_process_client_buffer;
311 * We're about to close down this client.
316 * Are we currently trying to receive? (YES if we are, NO if we are not,
317 * SYSERR if data is already available in MST).
322 * Persist the file handle for this client no matter what happens,
323 * force the OS to close once the process actually dies. Should only
324 * be used in special cases!
329 * Is this client a 'monitor' client that should not be counted
330 * when deciding on destroying the server during soft shutdown?
331 * (see also GNUNET_SERVICE_start)
336 * Type of last message processed (for warn_no_receive_done).
344 * Return user context associated with the given client.
345 * Note: you should probably use the macro (call without the underscore).
347 * @param client client to query
348 * @param size number of bytes in user context struct (for verification only)
349 * @return pointer to user context
352 GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
355 if ((0 == client->user_context_size) &&
356 (NULL == client->user_context))
357 return NULL; /* never set */
358 GNUNET_assert (size == client->user_context_size);
359 return client->user_context;
364 * Set user context to be associated with the given client.
365 * Note: you should probably use the macro (call without the underscore).
367 * @param client client to query
368 * @param ptr pointer to user context
369 * @param size number of bytes in user context struct (for verification only)
372 GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
376 client->user_context_size = size;
377 client->user_context = ptr;
382 * Scheduler says our listen socket is ready. Process it!
384 * @param cls handle to our server for which we are processing the listen
386 * @param tc reason why we are running right now
389 process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
391 struct GNUNET_SERVER_Handle *server = cls;
392 struct GNUNET_CONNECTION_Handle *sock;
393 struct GNUNET_SERVER_Client *client;
396 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
397 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
399 /* ignore shutdown, someone else will take care of it! */
400 GNUNET_SERVER_resume (server);
404 while (NULL != server->listen_sockets[i])
406 if (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_sockets[i]))
409 GNUNET_CONNECTION_create_from_accept (server->access,
411 server->listen_sockets[i]);
414 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server accepted incoming connection.\n");
415 client = GNUNET_SERVER_connect_socket (server, sock);
416 /* decrement reference count, we don't keep "client" alive */
417 GNUNET_SERVER_client_drop (client);
422 /* listen for more! */
423 GNUNET_SERVER_resume (server);
428 * Create and initialize a listen socket for the server.
430 * @param serverAddr address to listen on
431 * @param socklen length of address
432 * @return NULL on error, otherwise the listen socket
434 static struct GNUNET_NETWORK_Handle *
435 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
437 struct GNUNET_NETWORK_Handle *sock;
441 switch (serverAddr->sa_family)
444 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
447 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
457 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
460 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
464 /* bind the socket */
465 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen, 0))
468 if (EADDRINUSE != errno)
470 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
471 * fail if we already took the port on IPv6; if both IPv4 and
472 * IPv6 binds fail, then our caller will log using the
473 * errno preserved in 'eno' */
474 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
476 LOG (GNUNET_ERROR_TYPE_ERROR, _("`%s' failed for port %d (%s).\n"),
478 (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6");
484 LOG (GNUNET_ERROR_TYPE_WARNING,
485 _("`%s' failed for port %d (%s): address already in use\n"),
487 (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6");
488 else if (AF_UNIX == serverAddr->sa_family)
490 const struct sockaddr_un *un = (const struct sockaddr_un *) serverAddr;
491 unsigned int off = 0;
493 if ('\0' == un->sun_path[0])
494 off = 1; /* some UNIXPATHs start with 0 */
495 LOG (GNUNET_ERROR_TYPE_WARNING,
496 _("`%s' failed for `%.*s': address already in use\n"), "bind",
497 (int) ((sizeof (un->sun_path) - off)),
498 (&un->sun_path[off]));
501 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
505 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
507 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen");
508 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
513 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server starts to listen on port %u.\n",
520 * Create a new server.
522 * @param access function for access control
523 * @param access_cls closure for access
524 * @param lsocks NULL-terminated array of listen sockets
525 * @param idle_timeout after how long should we timeout idle connections?
526 * @param require_found if YES, connections sending messages of unknown type
528 * @return handle for the new server, NULL on error
529 * (typically, "port" already in use)
531 struct GNUNET_SERVER_Handle *
532 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
534 struct GNUNET_NETWORK_Handle **lsocks,
535 struct GNUNET_TIME_Relative idle_timeout,
538 struct GNUNET_SERVER_Handle *server;
540 server = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
541 server->idle_timeout = idle_timeout;
542 server->listen_sockets = lsocks;
543 server->access = access;
544 server->access_cls = access_cls;
545 server->require_found = require_found;
547 GNUNET_SERVER_resume (server);
553 * Create a new server.
555 * @param access function for access control
556 * @param access_cls closure for access
557 * @param serverAddr address to listen on (including port), NULL terminated array
558 * @param socklen length of serverAddr
559 * @param idle_timeout after how long should we timeout idle connections?
560 * @param require_found if YES, connections sending messages of unknown type
562 * @return handle for the new server, NULL on error
563 * (typically, "port" already in use)
565 struct GNUNET_SERVER_Handle *
566 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
567 struct sockaddr *const *serverAddr,
568 const socklen_t * socklen,
569 struct GNUNET_TIME_Relative idle_timeout,
572 struct GNUNET_NETWORK_Handle **lsocks;
579 while (NULL != serverAddr[i])
583 lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
586 while (NULL != serverAddr[i])
590 if ( (socklen[k] == socklen[i]) &&
591 (0 == memcmp (serverAddr[k], serverAddr[i], socklen[i])) )
598 /* duplicate address, skip */
602 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
603 if (NULL != lsocks[j])
610 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
611 GNUNET_free (lsocks);
619 return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks,
620 idle_timeout, require_found);
625 * Set the 'monitor' flag on this client. Clients which have been
626 * marked as 'monitors' won't prevent the server from shutting down
627 * once 'GNUNET_SERVER_stop_listening' has been invoked. The idea is
628 * that for "normal" clients we likely want to allow them to process
629 * their requests; however, monitor-clients are likely to 'never'
630 * disconnect during shutdown and thus will not be considered when
631 * determining if the server should continue to exist after
632 * 'GNUNET_SERVER_destroy' has been called.
634 * @param client the client to set the 'monitor' flag on
637 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client)
639 client->is_monitor = GNUNET_YES;
644 * Helper function for 'test_monitor_clients' to trigger
645 * 'GNUNET_SERVER_destroy' after the stack has unwound.
647 * @param cls the 'struct GNUNET_SERVER_Handle' to destroy
651 do_destroy (void *cls,
652 const struct GNUNET_SCHEDULER_TaskContext *tc)
654 struct GNUNET_SERVER_Handle *server = cls;
655 GNUNET_SERVER_destroy (server);
660 * Check if only 'monitor' clients are left. If so, destroy the
663 * @param server server to test for full shutdown
666 test_monitor_clients (struct GNUNET_SERVER_Handle *server)
668 struct GNUNET_SERVER_Client *client;
670 if (GNUNET_YES != server->in_soft_shutdown)
672 for (client = server->clients_head; NULL != client; client = client->next)
673 if (GNUNET_NO == client->is_monitor)
674 return; /* not done yet */
675 server->in_soft_shutdown = GNUNET_SYSERR;
676 GNUNET_SCHEDULER_add_continuation (&do_destroy, server,
677 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
682 * Suspend accepting connections from the listen socket temporarily.
684 * @param server server to stop accepting connections.
687 GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server)
689 if (GNUNET_SCHEDULER_NO_TASK != server->listen_task)
691 GNUNET_SCHEDULER_cancel (server->listen_task);
692 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
698 * Resume accepting connections from the listen socket.
700 * @param server server to stop accepting connections.
703 GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server)
705 struct GNUNET_NETWORK_FDSet *r;
708 if (NULL == server->listen_sockets)
710 if (NULL == server->listen_sockets[0])
711 return; /* nothing to do, no listen sockets! */
712 if (NULL == server->listen_sockets[1])
714 /* simplified method: no fd set needed; this is then much simpler and
715 much more efficient */
716 server->listen_task =
717 GNUNET_SCHEDULER_add_read_net_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
718 GNUNET_SCHEDULER_PRIORITY_HIGH,
719 server->listen_sockets[0],
720 &process_listen_socket, server);
723 r = GNUNET_NETWORK_fdset_create ();
725 while (NULL != server->listen_sockets[i])
726 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
727 server->listen_task =
728 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
729 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
730 &process_listen_socket, server);
731 GNUNET_NETWORK_fdset_destroy (r);
736 * Stop the listen socket and get ready to shutdown the server
737 * once only 'monitor' clients are left.
739 * @param server server to stop listening on
742 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server)
746 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server in soft shutdown\n");
747 if (GNUNET_SCHEDULER_NO_TASK != server->listen_task)
749 GNUNET_SCHEDULER_cancel (server->listen_task);
750 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
752 if (NULL != server->listen_sockets)
755 while (NULL != server->listen_sockets[i])
756 GNUNET_break (GNUNET_OK ==
757 GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
758 GNUNET_free (server->listen_sockets);
759 server->listen_sockets = NULL;
761 if (GNUNET_NO == server->in_soft_shutdown)
762 server->in_soft_shutdown = GNUNET_YES;
763 test_monitor_clients (server);
768 * Free resources held by this server.
770 * @param server server to destroy
773 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server)
775 struct HandlerList *hpos;
776 struct NotifyList *npos;
779 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
780 if (GNUNET_SCHEDULER_NO_TASK != server->listen_task)
782 GNUNET_SCHEDULER_cancel (server->listen_task);
783 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
785 if (NULL != server->listen_sockets)
788 while (NULL != server->listen_sockets[i])
789 GNUNET_break (GNUNET_OK ==
790 GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
791 GNUNET_free (server->listen_sockets);
792 server->listen_sockets = NULL;
794 while (NULL != server->clients_head)
795 GNUNET_SERVER_client_disconnect (server->clients_head);
796 while (NULL != (hpos = server->handlers))
798 server->handlers = hpos->next;
801 while (NULL != (npos = server->disconnect_notify_list_head))
803 npos->callback (npos->callback_cls, NULL);
804 GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
805 server->disconnect_notify_list_tail,
809 while (NULL != (npos = server->connect_notify_list_head))
811 npos->callback (npos->callback_cls, NULL);
812 GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
813 server->connect_notify_list_tail,
817 GNUNET_free (server);
822 * Add additional handlers to an existing server.
824 * @param server the server to add handlers to
825 * @param handlers array of message handlers for
826 * incoming messages; the last entry must
827 * have "NULL" for the "callback"; multiple
828 * entries for the same type are allowed,
829 * they will be called in order of occurence.
830 * These handlers can be removed later;
831 * the handlers array must exist until removed
832 * (or server is destroyed).
835 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
836 const struct GNUNET_SERVER_MessageHandler *handlers)
838 struct HandlerList *p;
840 p = GNUNET_malloc (sizeof (struct HandlerList));
841 p->handlers = handlers;
842 p->next = server->handlers;
843 server->handlers = p;
848 * Change functions used by the server to tokenize the message stream.
849 * (very rarely used).
851 * @param server server to modify
852 * @param create new tokenizer initialization function
853 * @param destroy new tokenizer destruction function
854 * @param receive new tokenizer receive function
855 * @param cls closure for 'create', 'receive', 'destroy'
858 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
859 GNUNET_SERVER_MstCreateCallback create,
860 GNUNET_SERVER_MstDestroyCallback destroy,
861 GNUNET_SERVER_MstReceiveCallback receive,
864 server->mst_create = create;
865 server->mst_destroy = destroy;
866 server->mst_receive = receive;
867 server->mst_cls = cls;
872 * Task run to warn about missing calls to 'GNUNET_SERVER_receive_done'.
874 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
875 * @param tc scheduler context (unused)
878 warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
880 struct GNUNET_SERVER_Client *client = cls;
882 GNUNET_break (0 != client->warn_type); /* type should never be 0 here, as we don't use 0 */
884 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
885 &warn_no_receive_done, client);
886 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
887 LOG (GNUNET_ERROR_TYPE_WARNING,
888 _("Processing code for message of type %u did not call `GNUNET_SERVER_receive_done' after %s\n"),
889 (unsigned int) client->warn_type,
890 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (client->warn_start), GNUNET_YES));
895 * Disable the warning the server issues if a message is not acknowledged
896 * in a timely fashion. Use this call if a client is intentionally delayed
897 * for a while. Only applies to the current message.
899 * @param client client for which to disable the warning
902 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
904 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
906 GNUNET_SCHEDULER_cancel (client->warn_task);
907 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
913 * Inject a message into the server, pretend it came
914 * from the specified client. Delivery of the message
915 * will happen instantly (if a handler is installed;
916 * otherwise the call does nothing).
918 * @param server the server receiving the message
919 * @param sender the "pretended" sender of the message
921 * @param message message to transmit
922 * @return GNUNET_OK if the message was OK and the
923 * connection can stay open
924 * GNUNET_SYSERR if the connection to the
925 * client should be shut down
928 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
929 struct GNUNET_SERVER_Client *sender,
930 const struct GNUNET_MessageHeader *message)
932 struct HandlerList *pos;
933 const struct GNUNET_SERVER_MessageHandler *mh;
939 type = ntohs (message->type);
940 size = ntohs (message->size);
941 LOG (GNUNET_ERROR_TYPE_DEBUG,
942 "Server schedules transmission of %u-byte message of type %u to client.\n",
945 for (pos = server->handlers; NULL != pos; pos = pos->next)
948 while (pos->handlers[i].callback != NULL)
950 mh = &pos->handlers[i];
951 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
953 if ((0 != mh->expected_size) && (mh->expected_size != size))
955 #if GNUNET8_NETWORK_IS_DEAD
956 LOG (GNUNET_ERROR_TYPE_WARNING,
957 "Expected %u bytes for message of type %u, got %u\n",
958 mh->expected_size, mh->type, size);
961 LOG (GNUNET_ERROR_TYPE_DEBUG,
962 "Expected %u bytes for message of type %u, got %u\n",
963 mh->expected_size, mh->type, size);
965 return GNUNET_SYSERR;
969 if ( (0 == sender->suspended) &&
970 (GNUNET_SCHEDULER_NO_TASK == sender->warn_task) )
972 GNUNET_break (0 != type); /* type should never be 0 here, as we don't use 0 */
973 sender->warn_start = GNUNET_TIME_absolute_get ();
975 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
976 &warn_no_receive_done, sender);
977 sender->warn_type = type;
981 mh->callback (mh->callback_cls, sender, message);
987 if (GNUNET_NO == found)
989 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
990 "Received message of unknown type %d\n", type);
991 if (GNUNET_YES == server->require_found)
992 return GNUNET_SYSERR;
999 * We are receiving an incoming message. Process it.
1001 * @param cls our closure (handle for the client)
1002 * @param buf buffer with data received from network
1003 * @param available number of bytes available in buf
1004 * @param addr address of the sender
1005 * @param addrlen length of addr
1006 * @param errCode code indicating errors receiving, 0 for success
1009 process_incoming (void *cls, const void *buf, size_t available,
1010 const struct sockaddr *addr, socklen_t addrlen, int errCode);
1014 * Process messages from the client's message tokenizer until either
1015 * the tokenizer is empty (and then schedule receiving more), or
1016 * until some handler is not immediately done (then wait for restart_processing)
1019 * @param client the client to process, RC must have already been increased
1020 * using GNUNET_SERVER_client_keep and will be decreased by one in this
1022 * @param ret GNUNET_NO to start processing from the buffer,
1023 * GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
1024 * GNUNET_SYSERR if we should instantly abort due to error in a previous step
1027 process_mst (struct GNUNET_SERVER_Client *client, int ret)
1029 while ((GNUNET_SYSERR != ret) && (NULL != client->server) &&
1030 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
1032 if (GNUNET_OK == ret)
1034 LOG (GNUNET_ERROR_TYPE_DEBUG,
1035 "Server re-enters receive loop, timeout: %llu.\n",
1036 client->idle_timeout.rel_value);
1037 client->receive_pending = GNUNET_YES;
1038 GNUNET_CONNECTION_receive (client->connection,
1039 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1040 client->idle_timeout, &process_incoming,
1044 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 "Server processes additional messages instantly.\n");
1046 if (NULL != client->server->mst_receive)
1048 client->server->mst_receive (client->server->mst_cls, client->mst,
1049 client, NULL, 0, GNUNET_NO, GNUNET_YES);
1052 GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
1055 LOG (GNUNET_ERROR_TYPE_DEBUG,
1056 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
1057 ret, client->server, client->shutdown_now, client->suspended);
1058 if (GNUNET_NO == ret)
1060 LOG (GNUNET_ERROR_TYPE_DEBUG,
1061 "Server has more data pending but is suspended.\n");
1062 client->receive_pending = GNUNET_SYSERR; /* data pending */
1064 if ((GNUNET_SYSERR == ret) || (GNUNET_YES == client->shutdown_now))
1065 GNUNET_SERVER_client_disconnect (client);
1070 * We are receiving an incoming message. Process it.
1072 * @param cls our closure (handle for the client)
1073 * @param buf buffer with data received from network
1074 * @param available number of bytes available in buf
1075 * @param addr address of the sender
1076 * @param addrlen length of addr
1077 * @param errCode code indicating errors receiving, 0 for success
1080 process_incoming (void *cls, const void *buf, size_t available,
1081 const struct sockaddr *addr, socklen_t addrlen, int errCode)
1083 struct GNUNET_SERVER_Client *client = cls;
1084 struct GNUNET_SERVER_Handle *server = client->server;
1085 struct GNUNET_TIME_Absolute end;
1086 struct GNUNET_TIME_Absolute now;
1089 GNUNET_assert (GNUNET_YES == client->receive_pending);
1090 client->receive_pending = GNUNET_NO;
1091 now = GNUNET_TIME_absolute_get ();
1092 end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout);
1094 if ((NULL == buf) && (0 == available) && (NULL == addr) && (0 == errCode) &&
1095 (GNUNET_YES != client->shutdown_now) && (NULL != server) &&
1096 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
1097 (end.abs_value > now.abs_value))
1099 /* wait longer, timeout changed (i.e. due to us sending) */
1100 LOG (GNUNET_ERROR_TYPE_DEBUG,
1101 "Receive time out, but no disconnect due to sending (%p)\n",
1102 GNUNET_a2s (addr, addrlen));
1103 client->receive_pending = GNUNET_YES;
1104 GNUNET_CONNECTION_receive (client->connection,
1105 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1106 GNUNET_TIME_absolute_get_remaining (end),
1107 &process_incoming, client);
1110 if ((NULL == buf) || (0 == available) || (0 != errCode) || (NULL == server) ||
1111 (GNUNET_YES == client->shutdown_now) ||
1112 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
1114 /* other side closed connection, error connecting, etc. */
1115 GNUNET_SERVER_client_disconnect (client);
1118 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n",
1119 (unsigned int) available, GNUNET_a2s (addr, addrlen));
1120 GNUNET_SERVER_client_keep (client);
1121 client->last_activity = now;
1123 if (NULL != server->mst_receive)
1125 client->server->mst_receive (client->server->mst_cls, client->mst,
1126 client, buf, available, GNUNET_NO, GNUNET_YES);
1127 else if (NULL != client->mst)
1130 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO,
1139 process_mst (client, ret);
1140 GNUNET_SERVER_client_drop (client);
1145 * Task run to start again receiving from the network
1146 * and process requests.
1148 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
1149 * @param tc scheduler context (unused)
1152 restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1154 struct GNUNET_SERVER_Client *client = cls;
1156 GNUNET_assert (GNUNET_YES != client->shutdown_now);
1157 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1158 if (GNUNET_NO == client->receive_pending)
1160 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n");
1161 client->receive_pending = GNUNET_YES;
1162 GNUNET_CONNECTION_receive (client->connection,
1163 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1164 client->idle_timeout, &process_incoming, client);
1167 LOG (GNUNET_ERROR_TYPE_DEBUG,
1168 "Server continues processing messages still in the buffer.\n");
1169 GNUNET_SERVER_client_keep (client);
1170 client->receive_pending = GNUNET_NO;
1171 process_mst (client, GNUNET_NO);
1172 GNUNET_SERVER_client_drop (client);
1177 * This function is called whenever our inbound message tokenizer has
1178 * received a complete message.
1180 * @param cls closure (struct GNUNET_SERVER_Handle)
1181 * @param client identification of the client (struct GNUNET_SERVER_Client*)
1182 * @param message the actual message
1184 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1187 client_message_tokenizer_callback (void *cls, void *client,
1188 const struct GNUNET_MessageHeader *message)
1190 struct GNUNET_SERVER_Handle *server = cls;
1191 struct GNUNET_SERVER_Client *sender = client;
1194 LOG (GNUNET_ERROR_TYPE_DEBUG,
1195 "Tokenizer gives server message of type %u from client\n",
1196 ntohs (message->type));
1197 sender->in_process_client_buffer = GNUNET_YES;
1198 ret = GNUNET_SERVER_inject (server, sender, message);
1199 sender->in_process_client_buffer = GNUNET_NO;
1200 if ( (GNUNET_OK != ret) || (GNUNET_YES == sender->shutdown_now) )
1202 GNUNET_SERVER_client_disconnect (sender);
1203 return GNUNET_SYSERR;
1210 * Add a TCP socket-based connection to the set of handles managed by
1211 * this server. Use this function for outgoing (P2P) connections that
1212 * we initiated (and where this server should process incoming
1215 * @param server the server to use
1216 * @param connection the connection to manage (client must
1217 * stop using this connection from now on)
1218 * @return the client handle (client should call
1219 * "client_drop" on the return value eventually)
1221 struct GNUNET_SERVER_Client *
1222 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
1223 struct GNUNET_CONNECTION_Handle *connection)
1225 struct GNUNET_SERVER_Client *client;
1226 struct NotifyList *n;
1228 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
1229 client->connection = connection;
1230 client->reference_count = 1;
1231 client->server = server;
1232 client->last_activity = GNUNET_TIME_absolute_get ();
1233 client->idle_timeout = server->idle_timeout;
1234 GNUNET_CONTAINER_DLL_insert (server->clients_head,
1235 server->clients_tail,
1237 if (NULL != server->mst_create)
1239 server->mst_create (server->mst_cls, client);
1242 GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server);
1243 GNUNET_assert (NULL != client->mst);
1244 for (n = server->connect_notify_list_head; NULL != n; n = n->next)
1245 n->callback (n->callback_cls, client);
1247 client->receive_pending = GNUNET_YES;
1248 GNUNET_CONNECTION_receive (client->connection,
1249 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1250 client->idle_timeout, &process_incoming, client);
1256 * Change the timeout for a particular client. Decreasing the timeout
1257 * may not go into effect immediately (only after the previous timeout
1258 * times out or activity happens on the socket).
1260 * @param client the client to update
1261 * @param timeout new timeout for activities on the socket
1264 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
1265 struct GNUNET_TIME_Relative timeout)
1267 client->idle_timeout = timeout;
1272 * Notify the server that the given client handle should
1273 * be kept (keeps the connection up if possible, increments
1274 * the internal reference counter).
1276 * @param client the client to keep
1279 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1281 client->reference_count++;
1286 * Notify the server that the given client handle is no
1287 * longer required. Decrements the reference counter. If
1288 * that counter reaches zero an inactive connection maybe
1291 * @param client the client to drop
1294 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1296 GNUNET_assert (client->reference_count > 0);
1297 client->reference_count--;
1298 if ((GNUNET_YES == client->shutdown_now) && (0 == client->reference_count))
1299 GNUNET_SERVER_client_disconnect (client);
1304 * Obtain the network address of the other party.
1306 * @param client the client to get the address for
1307 * @param addr where to store the address
1308 * @param addrlen where to store the length of the address
1309 * @return GNUNET_OK on success
1312 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1313 void **addr, size_t * addrlen)
1315 return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen);
1320 * Ask the server to notify us whenever a client disconnects.
1321 * This function is called whenever the actual network connection
1322 * is closed; the reference count may be zero or larger than zero
1325 * @param server the server manageing the clients
1326 * @param callback function to call on disconnect
1327 * @param callback_cls closure for callback
1330 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1331 GNUNET_SERVER_DisconnectCallback callback,
1334 struct NotifyList *n;
1336 n = GNUNET_malloc (sizeof (struct NotifyList));
1337 n->callback = callback;
1338 n->callback_cls = callback_cls;
1339 GNUNET_CONTAINER_DLL_insert (server->disconnect_notify_list_head,
1340 server->disconnect_notify_list_tail,
1346 * Ask the server to notify us whenever a client connects.
1347 * This function is called whenever the actual network connection
1348 * is opened. If the server is destroyed before this
1349 * notification is explicitly cancelled, the 'callback' will
1350 * once be called with a 'client' argument of NULL to indicate
1351 * that the server itself is now gone (and that the callback
1352 * won't be called anymore and also can no longer be cancelled).
1354 * @param server the server manageing the clients
1355 * @param callback function to call on sconnect
1356 * @param callback_cls closure for callback
1359 GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
1360 GNUNET_SERVER_ConnectCallback callback, void *callback_cls)
1362 struct NotifyList *n;
1364 n = GNUNET_malloc (sizeof (struct NotifyList));
1365 n->callback = callback;
1366 n->callback_cls = callback_cls;
1367 GNUNET_CONTAINER_DLL_insert (server->connect_notify_list_head,
1368 server->connect_notify_list_tail,
1374 * Ask the server to stop notifying us whenever a client connects.
1376 * @param server the server manageing the clients
1377 * @param callback function to call on connect
1378 * @param callback_cls closure for callback
1381 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1382 GNUNET_SERVER_DisconnectCallback
1383 callback, void *callback_cls)
1385 struct NotifyList *pos;
1387 for (pos = server->disconnect_notify_list_head; NULL != pos; pos = pos->next)
1388 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1395 GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
1396 server->disconnect_notify_list_tail,
1403 * Ask the server to stop notifying us whenever a client disconnects.
1405 * @param server the server manageing the clients
1406 * @param callback function to call on disconnect
1407 * @param callback_cls closure for callback
1410 GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1411 GNUNET_SERVER_ConnectCallback callback, void *callback_cls)
1413 struct NotifyList *pos;
1415 for (pos = server->connect_notify_list_head; NULL != pos; pos = pos->next)
1416 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1423 GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
1424 server->connect_notify_list_tail,
1431 * Destroy the connection that is passed in via 'cls'. Used
1432 * as calling 'GNUNET_CONNECTION_destroy' from within a function
1433 * that was itself called from within 'process_notify' of
1434 * 'connection.c' is not allowed (see #2329).
1436 * @param cls connection to destroy
1437 * @param tc scheduler context (unused)
1440 destroy_connection (void *cls,
1441 const struct GNUNET_SCHEDULER_TaskContext *tc)
1443 struct GNUNET_CONNECTION_Handle *connection = cls;
1445 GNUNET_CONNECTION_destroy (connection);
1450 * Ask the server to disconnect from the given client.
1451 * This is the same as returning GNUNET_SYSERR from a message
1452 * handler, except that it allows dropping of a client even
1453 * when not handling a message from that client.
1455 * @param client the client to disconnect from
1458 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1460 struct GNUNET_SERVER_Handle *server = client->server;
1461 struct NotifyList *n;
1463 LOG (GNUNET_ERROR_TYPE_DEBUG,
1464 "Client is being disconnected from the server.\n");
1465 if (GNUNET_SCHEDULER_NO_TASK != client->restart_task)
1467 GNUNET_SCHEDULER_cancel (client->restart_task);
1468 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1470 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1472 GNUNET_SCHEDULER_cancel (client->warn_task);
1473 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1475 if (GNUNET_YES == client->receive_pending)
1477 GNUNET_CONNECTION_receive_cancel (client->connection);
1478 client->receive_pending = GNUNET_NO;
1480 client->shutdown_now = GNUNET_YES;
1481 client->reference_count++; /* make sure nobody else clean up client... */
1482 if ( (NULL != client->mst) &&
1485 GNUNET_CONTAINER_DLL_remove (server->clients_head,
1486 server->clients_tail,
1488 if (NULL != server->mst_destroy)
1489 server->mst_destroy (server->mst_cls, client->mst);
1491 GNUNET_SERVER_mst_destroy (client->mst);
1493 for (n = server->disconnect_notify_list_head; NULL != n; n = n->next)
1494 n->callback (n->callback_cls, client);
1496 client->reference_count--;
1497 if (client->reference_count > 0)
1499 LOG (GNUNET_ERROR_TYPE_DEBUG,
1500 "RC still positive, not destroying everything.\n");
1501 client->server = NULL;
1504 if (GNUNET_YES == client->in_process_client_buffer)
1506 LOG (GNUNET_ERROR_TYPE_DEBUG,
1507 "Still processing inputs, not destroying everything.\n");
1510 if (GNUNET_YES == client->persist)
1511 GNUNET_CONNECTION_persist_ (client->connection);
1512 if (NULL != client->th.cth)
1513 GNUNET_SERVER_notify_transmit_ready_cancel (&client->th);
1514 (void) GNUNET_SCHEDULER_add_now (&destroy_connection,
1515 client->connection);
1516 /* need to cancel again, as it might have been re-added
1517 in the meantime (i.e. during callbacks) */
1518 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1520 GNUNET_SCHEDULER_cancel (client->warn_task);
1521 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1523 if (GNUNET_YES == client->receive_pending)
1525 GNUNET_CONNECTION_receive_cancel (client->connection);
1526 client->receive_pending = GNUNET_NO;
1528 GNUNET_free (client);
1529 /* we might be in soft-shutdown, test if we're done */
1531 test_monitor_clients (server);
1536 * Disable the "CORK" feature for communication with the given client,
1537 * forcing the OS to immediately flush the buffer on transmission
1538 * instead of potentially buffering multiple messages.
1540 * @param client handle to the client
1541 * @return GNUNET_OK on success
1544 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client)
1546 return GNUNET_CONNECTION_disable_corking (client->connection);
1551 * Wrapper for transmission notification that calls the original
1552 * callback and update the last activity time for our connection.
1554 * @param cls the 'struct GNUNET_SERVER_Client'
1555 * @param size number of bytes we can transmit
1556 * @param buf where to copy the message
1557 * @return number of bytes actually transmitted
1560 transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
1562 struct GNUNET_SERVER_Client *client = cls;
1563 GNUNET_CONNECTION_TransmitReadyNotify callback;
1565 client->th.cth = NULL;
1566 callback = client->th.callback;
1567 client->th.callback = NULL;
1568 client->last_activity = GNUNET_TIME_absolute_get ();
1569 return callback (client->th.callback_cls, size, buf);
1574 * Notify us when the server has enough space to transmit
1575 * a message of the given size to the given client.
1577 * @param client client to transmit message to
1578 * @param size requested amount of buffer space
1579 * @param timeout after how long should we give up (and call
1580 * notify with buf NULL and size 0)?
1581 * @param callback function to call when space is available
1582 * @param callback_cls closure for callback
1583 * @return non-NULL if the notify callback was queued; can be used
1584 * to cancel the request using
1585 * GNUNET_SERVER_notify_transmit_ready_cancel.
1586 * NULL if we are already going to notify someone else (busy)
1588 struct GNUNET_SERVER_TransmitHandle *
1589 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1591 struct GNUNET_TIME_Relative timeout,
1592 GNUNET_CONNECTION_TransmitReadyNotify
1593 callback, void *callback_cls)
1595 if (NULL != client->th.callback)
1597 client->th.callback_cls = callback_cls;
1598 client->th.callback = callback;
1599 client->th.cth = GNUNET_CONNECTION_notify_transmit_ready (client->connection, size,
1601 &transmit_ready_callback_wrapper,
1608 * Abort transmission request.
1610 * @param th request to abort
1613 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th)
1615 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->cth);
1617 th->callback = NULL;
1622 * Set the persistent flag on this client, used to setup client connection
1623 * to only be killed when the service it's connected to is actually dead.
1625 * @param client the client to set the persistent flag on
1628 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1630 client->persist = GNUNET_YES;
1635 * Resume receiving from this client, we are done processing the
1636 * current request. This function must be called from within each
1637 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1639 * @param client client we were processing a message of
1640 * @param success GNUNET_OK to keep the connection open and
1641 * continue to receive
1642 * GNUNET_NO to close the connection (normal behavior)
1643 * GNUNET_SYSERR to close the connection (signal
1647 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1651 GNUNET_assert (client->suspended > 0);
1652 client->suspended--;
1653 if (GNUNET_OK != success)
1655 LOG (GNUNET_ERROR_TYPE_DEBUG,
1656 "GNUNET_SERVER_receive_done called with failure indication\n");
1657 if ( (client->reference_count > 0) || (client->suspended > 0) )
1658 client->shutdown_now = GNUNET_YES;
1660 GNUNET_SERVER_client_disconnect (client);
1663 if (client->suspended > 0)
1665 LOG (GNUNET_ERROR_TYPE_DEBUG,
1666 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1669 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1671 GNUNET_SCHEDULER_cancel (client->warn_task);
1672 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1674 if (GNUNET_YES == client->in_process_client_buffer)
1676 LOG (GNUNET_ERROR_TYPE_DEBUG,
1677 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1680 if ((NULL == client->server) || (GNUNET_YES == client->shutdown_now))
1682 GNUNET_SERVER_client_disconnect (client);
1685 LOG (GNUNET_ERROR_TYPE_DEBUG,
1686 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1687 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1688 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing, client);
1692 /* end of server.c */