2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @brief library for building GNUnet network servers
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
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 doubly linked list.
63 struct NotifyList *next;
66 * This is a doubly linked list.
68 struct NotifyList *prev;
73 GNUNET_SERVER_DisconnectCallback callback;
76 * Closure for callback.
83 * @brief handle for a server
85 struct GNUNET_SERVER_Handle
88 * List of handlers for incoming messages.
90 struct HandlerList *handlers;
93 * Head of list of our current clients.
95 struct GNUNET_SERVER_Client *clients_head;
98 * Head of list of our current clients.
100 struct GNUNET_SERVER_Client *clients_tail;
103 * Head of linked list of functions to call on disconnects by clients.
105 struct NotifyList *disconnect_notify_list_head;
108 * Tail of linked list of functions to call on disconnects by clients.
110 struct NotifyList *disconnect_notify_list_tail;
113 * Head of linked list of functions to call on connects by clients.
115 struct NotifyList *connect_notify_list_head;
118 * Tail of linked list of functions to call on connects by clients.
120 struct NotifyList *connect_notify_list_tail;
123 * Function to call for access control.
125 GNUNET_CONNECTION_AccessCheck access_cb;
128 * Closure for @e access_cb.
133 * NULL-terminated array of sockets used to listen for new
136 struct GNUNET_NETWORK_Handle **listen_sockets;
139 * After how long should an idle connection time
142 struct GNUNET_TIME_Relative idle_timeout;
145 * Task scheduled to do the listening.
147 struct GNUNET_SCHEDULER_Task * listen_task;
150 * Alternative function to create a MST instance.
152 GNUNET_SERVER_MstCreateCallback mst_create;
155 * Alternative function to destroy a MST instance.
157 GNUNET_SERVER_MstDestroyCallback mst_destroy;
160 * Alternative function to give data to a MST instance.
162 GNUNET_SERVER_MstReceiveCallback mst_receive;
165 * Closure for 'mst_'-callbacks.
170 * Do we ignore messages of types that we do not understand or do we
171 * require that a handler is found (and if not kill the connection)?
176 * Set to #GNUNET_YES once we are in 'soft' shutdown where we wait for
177 * all non-monitor clients to disconnect before we call
178 * #GNUNET_SERVER_destroy. See test_monitor_clients(). Set to
179 * #GNUNET_SYSERR once the final destroy task has been scheduled
180 * (we cannot run it in the same task).
182 int in_soft_shutdown;
187 * Handle server returns for aborting transmission to a client.
189 struct GNUNET_SERVER_TransmitHandle
192 * Function to call to get the message.
194 GNUNET_CONNECTION_TransmitReadyNotify callback;
197 * Closure for @e callback
202 * Active connection transmission handle.
204 struct GNUNET_CONNECTION_TransmitHandle *cth;
210 * @brief handle for a client of the server
212 struct GNUNET_SERVER_Client
216 * This is a doubly linked list.
218 struct GNUNET_SERVER_Client *next;
221 * This is a doubly linked list.
223 struct GNUNET_SERVER_Client *prev;
226 * Processing of incoming data.
231 * Server that this client belongs to.
233 struct GNUNET_SERVER_Handle *server;
236 * Client closure for callbacks.
238 struct GNUNET_CONNECTION_Handle *connection;
241 * User context value, manipulated using
242 * 'GNUNET_SERVER_client_{get/set}_user_context' functions.
247 * ID of task used to restart processing.
249 struct GNUNET_SCHEDULER_Task * restart_task;
252 * Task that warns about missing calls to #GNUNET_SERVER_receive_done.
254 struct GNUNET_SCHEDULER_Task * warn_task;
257 * Time when the warn task was started.
259 struct GNUNET_TIME_Absolute warn_start;
262 * Last activity on this socket (used to time it out
263 * if reference_count == 0).
265 struct GNUNET_TIME_Absolute last_activity;
268 * Transmission handle we return for this client from
269 * #GNUNET_SERVER_notify_transmit_ready.
271 struct GNUNET_SERVER_TransmitHandle th;
274 * After how long should an idle connection time
277 struct GNUNET_TIME_Relative idle_timeout;
280 * Number of external entities with a reference to
281 * this client object.
283 unsigned int reference_count;
286 * Was processing if incoming messages suspended while
287 * we were still processing data already received?
288 * This is a counter saying how often processing was
289 * suspended (once per handler invoked).
291 unsigned int suspended;
294 * Last size given when user context was initialized; used for
297 size_t user_context_size;
300 * Are we currently in the "process_client_buffer" function (and
301 * will hence restart the receive job on exit if suspended == 0 once
302 * we are done?). If this is set, then "receive_done" will
303 * essentially only decrement suspended; if this is not set, then
304 * "receive_done" may need to restart the receive process (either
305 * from the side-buffer or via select/recv).
307 int in_process_client_buffer;
310 * We're about to close down this client.
315 * Are we currently trying to receive? (#GNUNET_YES if we are,
316 * #GNUNET_NO if we are not, #GNUNET_SYSERR if data is already
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,
378 client->user_context_size = 0;
379 client->user_context = ptr;
382 client->user_context_size = size;
383 client->user_context = ptr;
388 * Scheduler says our listen socket is ready. Process it!
390 * @param cls handle to our server for which we are processing the listen
394 process_listen_socket (void *cls)
396 struct GNUNET_SERVER_Handle *server = cls;
397 const struct GNUNET_SCHEDULER_TaskContext *tc;
398 struct GNUNET_CONNECTION_Handle *sock;
401 server->listen_task = NULL;
402 tc = GNUNET_SCHEDULER_get_task_context ();
403 for (i = 0; NULL != server->listen_sockets[i]; i++)
405 if (GNUNET_NETWORK_fdset_isset (tc->read_ready,
406 server->listen_sockets[i]))
409 GNUNET_CONNECTION_create_from_accept (server->access_cb,
410 server->access_cb_cls,
411 server->listen_sockets[i]);
414 LOG (GNUNET_ERROR_TYPE_DEBUG,
415 "Server accepted incoming connection.\n");
416 (void) GNUNET_SERVER_connect_socket (server,
421 /* listen for more! */
422 GNUNET_SERVER_resume (server);
427 * Create and initialize a listen socket for the server.
429 * @param server_addr address to listen on
430 * @param socklen length of @a server_addr
431 * @return NULL on error, otherwise the listen socket
433 static struct GNUNET_NETWORK_Handle *
434 open_listen_socket (const struct sockaddr *server_addr,
437 struct GNUNET_NETWORK_Handle *sock;
441 switch (server_addr->sa_family)
444 port = ntohs (((const struct sockaddr_in *) server_addr)->sin_port);
447 port = ntohs (((const struct sockaddr_in6 *) server_addr)->sin6_port);
457 sock = GNUNET_NETWORK_socket_create (server_addr->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, server_addr, socklen))
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,
477 LOG (GNUNET_ERROR_TYPE_ERROR,
478 _("`%s' failed for port %d (%s).\n"),
481 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
487 LOG (GNUNET_ERROR_TYPE_WARNING,
488 _("`%s' failed for port %d (%s): address already in use\n"),
490 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
491 else if (AF_UNIX == server_addr->sa_family)
493 LOG (GNUNET_ERROR_TYPE_WARNING,
494 _("`%s' failed for `%s': address already in use\n"),
496 GNUNET_a2s (server_addr, socklen));
499 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
503 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
505 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
507 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
512 LOG (GNUNET_ERROR_TYPE_DEBUG,
513 "Server starts to listen on port %u.\n",
520 * Create a new server.
522 * @param access_cb function for access control
523 * @param access_cb_cls closure for @a access_cb
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 #GNUNET_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_cb,
534 struct GNUNET_NETWORK_Handle **lsocks,
535 struct GNUNET_TIME_Relative idle_timeout,
538 struct GNUNET_SERVER_Handle *server;
540 server = GNUNET_new (struct GNUNET_SERVER_Handle);
541 server->idle_timeout = idle_timeout;
542 server->listen_sockets = lsocks;
543 server->access_cb = access_cb;
544 server->access_cb_cls = access_cb_cls;
545 server->require_found = require_found;
547 GNUNET_SERVER_resume (server);
553 * Create a new server.
555 * @param access_cb function for access control
556 * @param access_cb_cls closure for @a access_cb
557 * @param server_addr address to listen on (including port), NULL terminated array
558 * @param socklen length of server_addr
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_cb,
568 struct sockaddr *const *server_addr,
569 const socklen_t * socklen,
570 struct GNUNET_TIME_Relative idle_timeout,
573 struct GNUNET_NETWORK_Handle **lsocks;
580 while (NULL != server_addr[i])
584 lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
587 while (NULL != server_addr[i])
591 if ( (socklen[k] == socklen[i]) &&
592 (0 == memcmp (server_addr[k], server_addr[i], socklen[i])) )
599 /* duplicate address, skip */
603 lsocks[j] = open_listen_socket (server_addr[i], socklen[i]);
604 if (NULL != lsocks[j])
611 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
612 GNUNET_free (lsocks);
620 return GNUNET_SERVER_create_with_sockets (access_cb,
629 * Set the 'monitor' flag on this client. Clients which have been
630 * marked as 'monitors' won't prevent the server from shutting down
631 * once '#GNUNET_SERVER_stop_listening()' has been invoked. The idea is
632 * that for "normal" clients we likely want to allow them to process
633 * their requests; however, monitor-clients are likely to 'never'
634 * disconnect during shutdown and thus will not be considered when
635 * determining if the server should continue to exist after
636 * #GNUNET_SERVER_destroy() has been called.
638 * @param client the client to set the 'monitor' flag on
641 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client)
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Marking client as monitor!\n");
645 client->is_monitor = GNUNET_YES;
650 * Helper function for #test_monitor_clients() to trigger
651 * #GNUNET_SERVER_destroy() after the stack has unwound.
653 * @param cls the `struct GNUNET_SERVER_Handle *` to destroy
656 do_destroy (void *cls)
658 struct GNUNET_SERVER_Handle *server = cls;
660 GNUNET_SERVER_destroy (server);
665 * Check if only 'monitor' clients are left. If so, destroy the
668 * @param server server to test for full shutdown
671 test_monitor_clients (struct GNUNET_SERVER_Handle *server)
673 struct GNUNET_SERVER_Client *client;
675 if (GNUNET_YES != server->in_soft_shutdown)
677 for (client = server->clients_head; NULL != client; client = client->next)
678 if (GNUNET_NO == client->is_monitor)
679 return; /* not done yet */
680 server->in_soft_shutdown = GNUNET_SYSERR;
681 (void) GNUNET_SCHEDULER_add_now (&do_destroy, server);
686 * Suspend accepting connections from the listen socket temporarily.
688 * @param server server to stop accepting connections.
691 GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server)
693 if (NULL != server->listen_task)
695 GNUNET_SCHEDULER_cancel (server->listen_task);
696 server->listen_task = NULL;
702 * Resume accepting connections from the listen socket.
704 * @param server server to stop accepting connections.
707 GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server)
709 struct GNUNET_NETWORK_FDSet *r;
712 if (NULL == server->listen_sockets)
714 if (NULL == server->listen_sockets[0])
715 return; /* nothing to do, no listen sockets! */
716 if (NULL == server->listen_sockets[1])
718 /* simplified method: no fd set needed; this is then much simpler
719 and much more efficient */
720 server->listen_task =
721 GNUNET_SCHEDULER_add_read_net_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
722 GNUNET_SCHEDULER_PRIORITY_HIGH,
723 server->listen_sockets[0],
724 &process_listen_socket, server);
727 r = GNUNET_NETWORK_fdset_create ();
729 while (NULL != server->listen_sockets[i])
730 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
731 server->listen_task =
732 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
733 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
734 &process_listen_socket, server);
735 GNUNET_NETWORK_fdset_destroy (r);
740 * Stop the listen socket and get ready to shutdown the server
741 * once only 'monitor' clients are left.
743 * @param server server to stop listening on
746 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server)
750 LOG (GNUNET_ERROR_TYPE_DEBUG,
751 "Server in soft shutdown\n");
752 if (NULL != server->listen_task)
754 GNUNET_SCHEDULER_cancel (server->listen_task);
755 server->listen_task = NULL;
757 if (NULL != server->listen_sockets)
760 while (NULL != server->listen_sockets[i])
761 GNUNET_break (GNUNET_OK ==
762 GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
763 GNUNET_free (server->listen_sockets);
764 server->listen_sockets = NULL;
766 if (GNUNET_NO == server->in_soft_shutdown)
767 server->in_soft_shutdown = GNUNET_YES;
768 test_monitor_clients (server);
773 * Free resources held by this server.
775 * @param server server to destroy
778 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server)
780 struct HandlerList *hpos;
781 struct NotifyList *npos;
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 "Server shutting down.\n");
786 if (NULL != server->listen_task)
788 GNUNET_SCHEDULER_cancel (server->listen_task);
789 server->listen_task = NULL;
791 if (NULL != server->listen_sockets)
794 while (NULL != server->listen_sockets[i])
795 GNUNET_break (GNUNET_OK ==
796 GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
797 GNUNET_free (server->listen_sockets);
798 server->listen_sockets = NULL;
800 while (NULL != server->clients_head)
801 GNUNET_SERVER_client_disconnect (server->clients_head);
802 while (NULL != (hpos = server->handlers))
804 server->handlers = hpos->next;
807 while (NULL != (npos = server->disconnect_notify_list_head))
809 npos->callback (npos->callback_cls,
811 GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
812 server->disconnect_notify_list_tail,
816 while (NULL != (npos = server->connect_notify_list_head))
818 npos->callback (npos->callback_cls,
820 GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
821 server->connect_notify_list_tail,
825 GNUNET_free (server);
830 * Add additional handlers to an existing server.
832 * @param server the server to add handlers to
833 * @param handlers array of message handlers for
834 * incoming messages; the last entry must
835 * have "NULL" for the "callback"; multiple
836 * entries for the same type are allowed,
837 * they will be called in order of occurence.
838 * These handlers can be removed later;
839 * the handlers array must exist until removed
840 * (or server is destroyed).
843 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
844 const struct GNUNET_SERVER_MessageHandler *handlers)
846 struct HandlerList *p;
848 p = GNUNET_new (struct HandlerList);
849 p->handlers = handlers;
850 p->next = server->handlers;
851 server->handlers = p;
856 * Change functions used by the server to tokenize the message stream.
857 * (very rarely used).
859 * @param server server to modify
860 * @param create new tokenizer initialization function
861 * @param destroy new tokenizer destruction function
862 * @param receive new tokenizer receive function
863 * @param cls closure for @a create, @a receive, @a destroy
866 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
867 GNUNET_SERVER_MstCreateCallback create,
868 GNUNET_SERVER_MstDestroyCallback destroy,
869 GNUNET_SERVER_MstReceiveCallback receive,
872 server->mst_create = create;
873 server->mst_destroy = destroy;
874 server->mst_receive = receive;
875 server->mst_cls = cls;
880 * Task run to warn about missing calls to #GNUNET_SERVER_receive_done.
882 * @param cls our `struct GNUNET_SERVER_Client *` to process more requests from
885 warn_no_receive_done (void *cls)
887 struct GNUNET_SERVER_Client *client = cls;
889 GNUNET_break (0 != client->warn_type); /* type should never be 0 here, as we don't use 0 */
891 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
892 &warn_no_receive_done, client);
893 LOG (GNUNET_ERROR_TYPE_WARNING,
894 _("Processing code for message of type %u did not call `GNUNET_SERVER_receive_done' after %s\n"),
895 (unsigned int) client->warn_type,
896 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (client->warn_start),
902 * Disable the warning the server issues if a message is not acknowledged
903 * in a timely fashion. Use this call if a client is intentionally delayed
904 * for a while. Only applies to the current message.
906 * @param client client for which to disable the warning
909 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
911 if (NULL != client->warn_task)
913 GNUNET_SCHEDULER_cancel (client->warn_task);
914 client->warn_task = NULL;
920 * Inject a message into the server, pretend it came
921 * from the specified client. Delivery of the message
922 * will happen instantly (if a handler is installed;
923 * otherwise the call does nothing).
925 * @param server the server receiving the message
926 * @param sender the "pretended" sender of the message
928 * @param message message to transmit
929 * @return #GNUNET_OK if the message was OK and the
930 * connection can stay open
931 * #GNUNET_SYSERR if the connection to the
932 * client should be shut down
935 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
936 struct GNUNET_SERVER_Client *sender,
937 const struct GNUNET_MessageHeader *message)
939 struct HandlerList *pos;
940 const struct GNUNET_SERVER_MessageHandler *mh;
946 type = ntohs (message->type);
947 size = ntohs (message->size);
948 LOG (GNUNET_ERROR_TYPE_DEBUG,
949 "Server schedules transmission of %u-byte message of type %u to client.\n",
952 for (pos = server->handlers; NULL != pos; pos = pos->next)
955 while (pos->handlers[i].callback != NULL)
957 mh = &pos->handlers[i];
958 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
960 if ((0 != mh->expected_size) && (mh->expected_size != size))
962 #if GNUNET8_NETWORK_IS_DEAD
963 LOG (GNUNET_ERROR_TYPE_WARNING,
964 "Expected %u bytes for message of type %u, got %u\n",
965 mh->expected_size, mh->type, size);
968 LOG (GNUNET_ERROR_TYPE_DEBUG,
969 "Expected %u bytes for message of type %u, got %u\n",
970 mh->expected_size, mh->type, size);
972 return GNUNET_SYSERR;
976 if ( (0 == sender->suspended) &&
977 (NULL == sender->warn_task) )
979 GNUNET_break (0 != type); /* type should never be 0 here, as we don't use 0 */
980 sender->warn_start = GNUNET_TIME_absolute_get ();
982 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
983 &warn_no_receive_done,
985 sender->warn_type = type;
989 mh->callback (mh->callback_cls, sender, message);
995 if (GNUNET_NO == found)
997 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
998 "Received message of unknown type %d\n", type);
999 if (GNUNET_YES == server->require_found)
1000 return GNUNET_SYSERR;
1007 * We are receiving an incoming message. Process it.
1009 * @param cls our closure (handle for the client)
1010 * @param buf buffer with data received from network
1011 * @param available number of bytes available in buf
1012 * @param addr address of the sender
1013 * @param addrlen length of @a addr
1014 * @param errCode code indicating errors receiving, 0 for success
1017 process_incoming (void *cls,
1020 const struct sockaddr *addr,
1026 * Process messages from the client's message tokenizer until either
1027 * the tokenizer is empty (and then schedule receiving more), or
1028 * until some handler is not immediately done (then wait for restart_processing)
1031 * @param client the client to process, RC must have already been increased
1032 * using #GNUNET_SERVER_client_keep and will be decreased by one in this
1034 * @param ret #GNUNET_NO to start processing from the buffer,
1035 * #GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
1036 * #GNUNET_SYSERR if we should instantly abort due to error in a previous step
1039 process_mst (struct GNUNET_SERVER_Client *client,
1042 while ((GNUNET_SYSERR != ret) && (NULL != client->server) &&
1043 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
1045 if (GNUNET_OK == ret)
1047 LOG (GNUNET_ERROR_TYPE_DEBUG,
1048 "Server re-enters receive loop, timeout: %s.\n",
1049 GNUNET_STRINGS_relative_time_to_string (client->idle_timeout, GNUNET_YES));
1050 client->receive_pending = GNUNET_YES;
1051 GNUNET_CONNECTION_receive (client->connection,
1052 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1053 client->idle_timeout,
1058 LOG (GNUNET_ERROR_TYPE_DEBUG,
1059 "Server processes additional messages instantly.\n");
1060 if (NULL != client->server->mst_receive)
1062 client->server->mst_receive (client->server->mst_cls, client->mst,
1063 client, NULL, 0, GNUNET_NO, GNUNET_YES);
1066 GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
1069 LOG (GNUNET_ERROR_TYPE_DEBUG,
1070 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
1071 ret, client->server,
1072 client->shutdown_now,
1074 if (GNUNET_NO == ret)
1076 LOG (GNUNET_ERROR_TYPE_DEBUG,
1077 "Server has more data pending but is suspended.\n");
1078 client->receive_pending = GNUNET_SYSERR; /* data pending */
1080 if ( (GNUNET_SYSERR == ret) ||
1081 (GNUNET_YES == client->shutdown_now) )
1082 GNUNET_SERVER_client_disconnect (client);
1087 * We are receiving an incoming message. Process it.
1089 * @param cls our closure (handle for the client)
1090 * @param buf buffer with data received from network
1091 * @param available number of bytes available in buf
1092 * @param addr address of the sender
1093 * @param addrlen length of @a addr
1094 * @param errCode code indicating errors receiving, 0 for success
1097 process_incoming (void *cls,
1100 const struct sockaddr *addr,
1104 struct GNUNET_SERVER_Client *client = cls;
1105 struct GNUNET_SERVER_Handle *server = client->server;
1106 struct GNUNET_TIME_Absolute end;
1107 struct GNUNET_TIME_Absolute now;
1110 GNUNET_assert (GNUNET_YES == client->receive_pending);
1111 client->receive_pending = GNUNET_NO;
1112 now = GNUNET_TIME_absolute_get ();
1113 end = GNUNET_TIME_absolute_add (client->last_activity,
1114 client->idle_timeout);
1116 if ( (NULL == buf) &&
1120 (GNUNET_YES != client->shutdown_now) &&
1122 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
1123 (end.abs_value_us > now.abs_value_us) )
1125 /* wait longer, timeout changed (i.e. due to us sending) */
1126 LOG (GNUNET_ERROR_TYPE_DEBUG,
1127 "Receive time out, but no disconnect due to sending (%p)\n",
1129 client->receive_pending = GNUNET_YES;
1130 GNUNET_CONNECTION_receive (client->connection,
1131 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1132 GNUNET_TIME_absolute_get_remaining (end),
1137 if ( (NULL == buf) ||
1141 (GNUNET_YES == client->shutdown_now) ||
1142 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)) )
1144 /* other side closed connection, error connecting, etc. */
1145 LOG (GNUNET_ERROR_TYPE_DEBUG,
1146 "Failed to connect or other side closed connection (%p)\n",
1148 GNUNET_SERVER_client_disconnect (client);
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "Server receives %u bytes from `%s'.\n",
1153 (unsigned int) available,
1154 GNUNET_a2s (addr, addrlen));
1155 GNUNET_SERVER_client_keep (client);
1156 client->last_activity = now;
1158 if (NULL != server->mst_receive)
1160 ret = client->server->mst_receive (client->server->mst_cls,
1168 else if (NULL != client->mst)
1171 GNUNET_SERVER_mst_receive (client->mst,
1183 process_mst (client,
1185 GNUNET_SERVER_client_drop (client);
1190 * Task run to start again receiving from the network
1191 * and process requests.
1193 * @param cls our `struct GNUNET_SERVER_Client *` to process more requests from
1196 restart_processing (void *cls)
1198 struct GNUNET_SERVER_Client *client = cls;
1200 GNUNET_assert (GNUNET_YES != client->shutdown_now);
1201 client->restart_task = NULL;
1202 if (GNUNET_NO == client->receive_pending)
1204 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n");
1205 client->receive_pending = GNUNET_YES;
1206 GNUNET_CONNECTION_receive (client->connection,
1207 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1208 client->idle_timeout,
1213 LOG (GNUNET_ERROR_TYPE_DEBUG,
1214 "Server continues processing messages still in the buffer.\n");
1215 GNUNET_SERVER_client_keep (client);
1216 client->receive_pending = GNUNET_NO;
1217 process_mst (client,
1219 GNUNET_SERVER_client_drop (client);
1224 * This function is called whenever our inbound message tokenizer has
1225 * received a complete message.
1227 * @param cls closure (struct GNUNET_SERVER_Handle)
1228 * @param client identification of the client (`struct GNUNET_SERVER_Client *`)
1229 * @param message the actual message
1231 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
1234 client_message_tokenizer_callback (void *cls,
1236 const struct GNUNET_MessageHeader *message)
1238 struct GNUNET_SERVER_Handle *server = cls;
1239 struct GNUNET_SERVER_Client *sender = client;
1242 LOG (GNUNET_ERROR_TYPE_DEBUG,
1243 "Tokenizer gives server message of type %u from client\n",
1244 ntohs (message->type));
1245 sender->in_process_client_buffer = GNUNET_YES;
1246 ret = GNUNET_SERVER_inject (server, sender, message);
1247 sender->in_process_client_buffer = GNUNET_NO;
1248 if ( (GNUNET_OK != ret) || (GNUNET_YES == sender->shutdown_now) )
1250 GNUNET_SERVER_client_disconnect (sender);
1251 return GNUNET_SYSERR;
1258 * Add a TCP socket-based connection to the set of handles managed by
1259 * this server. Use this function for outgoing (P2P) connections that
1260 * we initiated (and where this server should process incoming
1263 * @param server the server to use
1264 * @param connection the connection to manage (client must
1265 * stop using this connection from now on)
1266 * @return the client handle
1268 struct GNUNET_SERVER_Client *
1269 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
1270 struct GNUNET_CONNECTION_Handle *connection)
1272 struct GNUNET_SERVER_Client *client;
1273 struct NotifyList *n;
1275 client = GNUNET_new (struct GNUNET_SERVER_Client);
1276 client->connection = connection;
1277 client->server = server;
1278 client->last_activity = GNUNET_TIME_absolute_get ();
1279 client->idle_timeout = server->idle_timeout;
1280 GNUNET_CONTAINER_DLL_insert (server->clients_head,
1281 server->clients_tail,
1283 if (NULL != server->mst_create)
1285 server->mst_create (server->mst_cls, client);
1288 GNUNET_SERVER_mst_create (&client_message_tokenizer_callback,
1290 GNUNET_assert (NULL != client->mst);
1291 for (n = server->connect_notify_list_head; NULL != n; n = n->next)
1292 n->callback (n->callback_cls, client);
1293 client->receive_pending = GNUNET_YES;
1294 GNUNET_CONNECTION_receive (client->connection,
1295 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1296 client->idle_timeout,
1304 * Change the timeout for a particular client. Decreasing the timeout
1305 * may not go into effect immediately (only after the previous timeout
1306 * times out or activity happens on the socket).
1308 * @param client the client to update
1309 * @param timeout new timeout for activities on the socket
1312 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
1313 struct GNUNET_TIME_Relative timeout)
1315 client->idle_timeout = timeout;
1320 * Notify the server that the given client handle should
1321 * be kept (keeps the connection up if possible, increments
1322 * the internal reference counter).
1324 * @param client the client to keep
1327 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1329 client->reference_count++;
1334 * Notify the server that the given client handle is no
1335 * longer required. Decrements the reference counter. If
1336 * that counter reaches zero an inactive connection maybe
1339 * @param client the client to drop
1342 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1344 GNUNET_assert (client->reference_count > 0);
1345 client->reference_count--;
1346 if ((GNUNET_YES == client->shutdown_now) && (0 == client->reference_count))
1347 GNUNET_SERVER_client_disconnect (client);
1352 * Obtain the network address of the other party.
1354 * @param client the client to get the address for
1355 * @param addr where to store the address
1356 * @param addrlen where to store the length of the @a addr
1357 * @return #GNUNET_OK on success
1360 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1361 void **addr, size_t * addrlen)
1363 return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen);
1368 * Ask the server to notify us whenever a client disconnects.
1369 * This function is called whenever the actual network connection
1370 * is closed; the reference count may be zero or larger than zero
1373 * @param server the server manageing the clients
1374 * @param callback function to call on disconnect
1375 * @param callback_cls closure for @a callback
1378 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1379 GNUNET_SERVER_DisconnectCallback callback,
1382 struct NotifyList *n;
1384 n = GNUNET_new (struct NotifyList);
1385 n->callback = callback;
1386 n->callback_cls = callback_cls;
1387 GNUNET_CONTAINER_DLL_insert (server->disconnect_notify_list_head,
1388 server->disconnect_notify_list_tail,
1394 * Ask the server to notify us whenever a client connects.
1395 * This function is called whenever the actual network connection
1396 * is opened. If the server is destroyed before this
1397 * notification is explicitly cancelled, the 'callback' will
1398 * once be called with a 'client' argument of NULL to indicate
1399 * that the server itself is now gone (and that the callback
1400 * won't be called anymore and also can no longer be cancelled).
1402 * @param server the server manageing the clients
1403 * @param callback function to call on sconnect
1404 * @param callback_cls closure for @a callback
1407 GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
1408 GNUNET_SERVER_ConnectCallback callback,
1411 struct NotifyList *n;
1412 struct GNUNET_SERVER_Client *client;
1414 n = GNUNET_new (struct NotifyList);
1415 n->callback = callback;
1416 n->callback_cls = callback_cls;
1417 GNUNET_CONTAINER_DLL_insert (server->connect_notify_list_head,
1418 server->connect_notify_list_tail,
1420 for (client = server->clients_head; NULL != client; client = client->next)
1421 callback (callback_cls, client);
1426 * Ask the server to stop notifying us whenever a client connects.
1428 * @param server the server manageing the clients
1429 * @param callback function to call on connect
1430 * @param callback_cls closure for @a callback
1433 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1434 GNUNET_SERVER_DisconnectCallback callback,
1437 struct NotifyList *pos;
1439 for (pos = server->disconnect_notify_list_head; NULL != pos; pos = pos->next)
1440 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1447 GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
1448 server->disconnect_notify_list_tail,
1455 * Ask the server to stop notifying us whenever a client disconnects.
1457 * @param server the server manageing the clients
1458 * @param callback function to call on disconnect
1459 * @param callback_cls closure for @a callback
1462 GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1463 GNUNET_SERVER_ConnectCallback callback,
1466 struct NotifyList *pos;
1468 for (pos = server->connect_notify_list_head; NULL != pos; pos = pos->next)
1469 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1476 GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
1477 server->connect_notify_list_tail,
1484 * Destroy the connection that is passed in via @a cls. Used
1485 * as calling #GNUNET_CONNECTION_destroy from within a function
1486 * that was itself called from within process_notify() of
1487 * 'connection.c' is not allowed (see #2329).
1489 * @param cls connection to destroy
1492 destroy_connection (void *cls)
1494 struct GNUNET_CONNECTION_Handle *connection = cls;
1496 GNUNET_CONNECTION_destroy (connection);
1501 * Ask the server to disconnect from the given client.
1502 * This is the same as returning #GNUNET_SYSERR from a message
1503 * handler, except that it allows dropping of a client even
1504 * when not handling a message from that client.
1506 * @param client the client to disconnect from
1509 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1511 struct GNUNET_SERVER_Handle *server = client->server;
1512 struct NotifyList *n;
1514 LOG (GNUNET_ERROR_TYPE_DEBUG,
1515 "Client is being disconnected from the server.\n");
1516 if (NULL != client->restart_task)
1518 GNUNET_SCHEDULER_cancel (client->restart_task);
1519 client->restart_task = NULL;
1521 if (NULL != client->warn_task)
1523 GNUNET_SCHEDULER_cancel (client->warn_task);
1524 client->warn_task = NULL;
1526 if (GNUNET_YES == client->receive_pending)
1528 GNUNET_CONNECTION_receive_cancel (client->connection);
1529 client->receive_pending = GNUNET_NO;
1531 client->shutdown_now = GNUNET_YES;
1532 client->reference_count++; /* make sure nobody else clean up client... */
1533 if ( (NULL != client->mst) &&
1536 GNUNET_CONTAINER_DLL_remove (server->clients_head,
1537 server->clients_tail,
1539 if (NULL != server->mst_destroy)
1540 server->mst_destroy (server->mst_cls,
1543 GNUNET_SERVER_mst_destroy (client->mst);
1545 for (n = server->disconnect_notify_list_head; NULL != n; n = n->next)
1546 n->callback (n->callback_cls,
1549 client->reference_count--;
1550 if (client->reference_count > 0)
1552 LOG (GNUNET_ERROR_TYPE_DEBUG,
1553 "RC of %p still positive, not destroying everything.\n",
1555 client->server = NULL;
1558 if (GNUNET_YES == client->in_process_client_buffer)
1560 LOG (GNUNET_ERROR_TYPE_DEBUG,
1561 "Still processing inputs of %p, not destroying everything.\n",
1565 LOG (GNUNET_ERROR_TYPE_DEBUG,
1566 "RC of %p now zero, destroying everything.\n",
1568 if (GNUNET_YES == client->persist)
1569 GNUNET_CONNECTION_persist_ (client->connection);
1570 if (NULL != client->th.cth)
1571 GNUNET_SERVER_notify_transmit_ready_cancel (&client->th);
1572 (void) GNUNET_SCHEDULER_add_now (&destroy_connection,
1573 client->connection);
1574 /* need to cancel again, as it might have been re-added
1575 in the meantime (i.e. during callbacks) */
1576 if (NULL != client->warn_task)
1578 GNUNET_SCHEDULER_cancel (client->warn_task);
1579 client->warn_task = NULL;
1581 if (GNUNET_YES == client->receive_pending)
1583 GNUNET_CONNECTION_receive_cancel (client->connection);
1584 client->receive_pending = GNUNET_NO;
1586 GNUNET_free (client);
1587 /* we might be in soft-shutdown, test if we're done */
1589 test_monitor_clients (server);
1594 * Disable the "CORK" feature for communication with the given client,
1595 * forcing the OS to immediately flush the buffer on transmission
1596 * instead of potentially buffering multiple messages.
1598 * @param client handle to the client
1599 * @return #GNUNET_OK on success
1602 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client)
1604 return GNUNET_CONNECTION_disable_corking (client->connection);
1609 * Wrapper for transmission notification that calls the original
1610 * callback and update the last activity time for our connection.
1612 * @param cls the `struct GNUNET_SERVER_Client *`
1613 * @param size number of bytes we can transmit
1614 * @param buf where to copy the message
1615 * @return number of bytes actually transmitted
1618 transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
1620 struct GNUNET_SERVER_Client *client = cls;
1621 GNUNET_CONNECTION_TransmitReadyNotify callback;
1623 client->th.cth = NULL;
1624 callback = client->th.callback;
1625 client->th.callback = NULL;
1626 client->last_activity = GNUNET_TIME_absolute_get ();
1627 return callback (client->th.callback_cls, size, buf);
1632 * Notify us when the server has enough space to transmit
1633 * a message of the given size to the given client.
1635 * @param client client to transmit message to
1636 * @param size requested amount of buffer space
1637 * @param timeout after how long should we give up (and call
1638 * notify with buf NULL and size 0)?
1639 * @param callback function to call when space is available
1640 * @param callback_cls closure for @a callback
1641 * @return non-NULL if the notify callback was queued; can be used
1642 * to cancel the request using
1643 * #GNUNET_SERVER_notify_transmit_ready_cancel().
1644 * NULL if we are already going to notify someone else (busy)
1646 struct GNUNET_SERVER_TransmitHandle *
1647 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1649 struct GNUNET_TIME_Relative timeout,
1650 GNUNET_CONNECTION_TransmitReadyNotify callback,
1653 if (NULL != client->th.callback)
1655 client->th.callback_cls = callback_cls;
1656 client->th.callback = callback;
1657 client->th.cth = GNUNET_CONNECTION_notify_transmit_ready (client->connection, size,
1659 &transmit_ready_callback_wrapper,
1666 * Abort transmission request.
1668 * @param th request to abort
1671 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th)
1673 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->cth);
1675 th->callback = NULL;
1680 * Set the persistent flag on this client, used to setup client connection
1681 * to only be killed when the service it's connected to is actually dead.
1683 * @param client the client to set the persistent flag on
1686 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1688 client->persist = GNUNET_YES;
1693 * Resume receiving from this client, we are done processing the
1694 * current request. This function must be called from within each
1695 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1697 * @param client client we were processing a message of
1698 * @param success #GNUNET_OK to keep the connection open and
1699 * continue to receive
1700 * #GNUNET_NO to close the connection (normal behavior)
1701 * #GNUNET_SYSERR to close the connection (signal
1705 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client,
1710 GNUNET_assert (client->suspended > 0);
1711 client->suspended--;
1712 if (GNUNET_OK != success)
1714 LOG (GNUNET_ERROR_TYPE_DEBUG,
1715 "GNUNET_SERVER_receive_done called with failure indication\n");
1716 if ( (client->reference_count > 0) || (client->suspended > 0) )
1717 client->shutdown_now = GNUNET_YES;
1719 GNUNET_SERVER_client_disconnect (client);
1722 if (client->suspended > 0)
1724 LOG (GNUNET_ERROR_TYPE_DEBUG,
1725 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1728 if (NULL != client->warn_task)
1730 GNUNET_SCHEDULER_cancel (client->warn_task);
1731 client->warn_task = NULL;
1733 if (GNUNET_YES == client->in_process_client_buffer)
1735 LOG (GNUNET_ERROR_TYPE_DEBUG,
1736 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1739 if ((NULL == client->server) || (GNUNET_YES == client->shutdown_now))
1741 GNUNET_SERVER_client_disconnect (client);
1744 LOG (GNUNET_ERROR_TYPE_DEBUG,
1745 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1746 GNUNET_assert (NULL == client->restart_task);
1747 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing,
1752 /* end of server.c */