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 * List of our current clients.
96 struct GNUNET_SERVER_Client *clients;
99 * Head of linked list of functions to call on disconnects by clients.
101 struct NotifyList *disconnect_notify_list_head;
104 * Tail of linked list of functions to call on disconnects by clients.
106 struct NotifyList *disconnect_notify_list_tail;
109 * Function to call for access control.
111 GNUNET_CONNECTION_AccessCheck access;
114 * Closure for access.
119 * NULL-terminated array of sockets used to listen for new
122 struct GNUNET_NETWORK_Handle **listen_sockets;
125 * After how long should an idle connection time
128 struct GNUNET_TIME_Relative idle_timeout;
131 * Task scheduled to do the listening.
133 GNUNET_SCHEDULER_TaskIdentifier listen_task;
136 * Alternative function to create a MST instance.
138 GNUNET_SERVER_MstCreateCallback mst_create;
141 * Alternative function to destroy a MST instance.
143 GNUNET_SERVER_MstDestroyCallback mst_destroy;
146 * Alternative function to give data to a MST instance.
148 GNUNET_SERVER_MstReceiveCallback mst_receive;
151 * Closure for 'mst_'-callbacks.
156 * Do we ignore messages of types that we do not understand or do we
157 * require that a handler is found (and if not kill the connection)?
162 * Set to GNUNET_YES once we are in 'soft' shutdown where we wait for
163 * all non-monitor clients to disconnect before we call
164 * GNUNET_SERVER_destroy. See 'test_monitor_clients'. Set to
165 * GNUNET_SYSERR once the final destroy task has been scheduled
166 * (we cannot run it in the same task).
168 int in_soft_shutdown;
173 * Handle server returns for aborting transmission to a client.
175 struct GNUNET_SERVER_TransmitHandle
178 * Function to call to get the message.
180 GNUNET_CONNECTION_TransmitReadyNotify callback;
183 * Closure for 'callback'
188 * Active connection transmission handle.
190 struct GNUNET_CONNECTION_TransmitHandle *cth;
196 * @brief handle for a client of the server
198 struct GNUNET_SERVER_Client
202 * This is a linked list.
204 struct GNUNET_SERVER_Client *next;
207 * Processing of incoming data.
212 * Server that this client belongs to.
214 struct GNUNET_SERVER_Handle *server;
217 * Client closure for callbacks.
219 struct GNUNET_CONNECTION_Handle *connection;
222 * ID of task used to restart processing.
224 GNUNET_SCHEDULER_TaskIdentifier restart_task;
227 * Task that warns about missing calls to 'GNUNET_SERVER_receive_done'.
229 GNUNET_SCHEDULER_TaskIdentifier warn_task;
232 * Time when the warn task was started.
234 struct GNUNET_TIME_Absolute warn_start;
237 * Last activity on this socket (used to time it out
238 * if reference_count == 0).
240 struct GNUNET_TIME_Absolute last_activity;
243 * Transmission handle we return for this client from
244 * GNUNET_SERVER_notify_transmit_ready.
246 struct GNUNET_SERVER_TransmitHandle th;
249 * After how long should an idle connection time
252 struct GNUNET_TIME_Relative idle_timeout;
255 * Number of external entities with a reference to
256 * this client object.
258 unsigned int reference_count;
261 * Was processing if incoming messages suspended while
262 * we were still processing data already received?
263 * This is a counter saying how often processing was
264 * suspended (once per handler invoked).
266 unsigned int suspended;
269 * Are we currently in the "process_client_buffer" function (and
270 * will hence restart the receive job on exit if suspended == 0 once
271 * we are done?). If this is set, then "receive_done" will
272 * essentially only decrement suspended; if this is not set, then
273 * "receive_done" may need to restart the receive process (either
274 * from the side-buffer or via select/recv).
276 int in_process_client_buffer;
279 * We're about to close down this client due to some serious
285 * Are we currently trying to receive? (YES if we are, NO if we are not,
286 * SYSERR if data is already available in MST).
291 * Finish pending write when disconnecting?
293 int finish_pending_write;
296 * Persist the file handle for this client no matter what happens,
297 * force the OS to close once the process actually dies. Should only
298 * be used in special cases!
303 * Is this client a 'monitor' client that should not be counted
304 * when deciding on destroying the server during soft shutdown?
305 * (see also GNUNET_SERVICE_start)
310 * Type of last message processed (for warn_no_receive_done).
317 * Scheduler says our listen socket is ready. Process it!
319 * @param cls handle to our server for which we are processing the listen
321 * @param tc reason why we are running right now
324 process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 struct GNUNET_SERVER_Handle *server = cls;
327 struct GNUNET_CONNECTION_Handle *sock;
328 struct GNUNET_SERVER_Client *client;
329 struct GNUNET_NETWORK_FDSet *r;
332 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
333 r = GNUNET_NETWORK_fdset_create ();
335 while (NULL != server->listen_sockets[i])
336 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
337 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
339 /* ignore shutdown, someone else will take care of it! */
340 server->listen_task =
341 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
342 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
343 &process_listen_socket, server);
344 GNUNET_NETWORK_fdset_destroy (r);
348 while (NULL != server->listen_sockets[i])
350 if (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_sockets[i]))
353 GNUNET_CONNECTION_create_from_accept (server->access,
355 server->listen_sockets[i]);
358 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server accepted incoming connection.\n");
359 client = GNUNET_SERVER_connect_socket (server, sock);
360 /* decrement reference count, we don't keep "client" alive */
361 GNUNET_SERVER_client_drop (client);
366 /* listen for more! */
367 server->listen_task =
368 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
369 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
370 &process_listen_socket, server);
371 GNUNET_NETWORK_fdset_destroy (r);
376 * Create and initialize a listen socket for the server.
378 * @param serverAddr address to listen on
379 * @param socklen length of address
380 * @return NULL on error, otherwise the listen socket
382 static struct GNUNET_NETWORK_Handle *
383 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
385 const static int on = 1;
386 struct GNUNET_NETWORK_Handle *sock;
390 switch (serverAddr->sa_family)
393 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
396 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
406 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
409 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
415 if (GNUNET_NETWORK_socket_setsockopt
416 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
417 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
420 if ((AF_INET6 == serverAddr->sa_family) &&
421 (GNUNET_NETWORK_socket_setsockopt
422 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
423 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
427 /* bind the socket */
428 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen))
431 if (EADDRINUSE != errno)
433 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
434 * fail if we already took the port on IPv6; if both IPv4 and
435 * IPv6 binds fail, then our caller will log using the
436 * errno preserved in 'eno' */
437 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
439 LOG (GNUNET_ERROR_TYPE_ERROR, _("`%s' failed for port %d (%s).\n"),
441 (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6");
447 LOG (GNUNET_ERROR_TYPE_WARNING,
448 _("`%s' failed for port %d (%s): address already in use\n"),
450 (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6");
451 else if (AF_UNIX == serverAddr->sa_family)
452 LOG (GNUNET_ERROR_TYPE_WARNING,
453 _("`%s' failed for `%s': address already in use\n"), "bind",
454 ((const struct sockaddr_un *) serverAddr)->sun_path);
457 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
461 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
463 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen");
464 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
469 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server starts to listen on port %u.\n",
476 * Create a new server.
478 * @param access function for access control
479 * @param access_cls closure for access
480 * @param lsocks NULL-terminated array of listen sockets
481 * @param idle_timeout after how long should we timeout idle connections?
482 * @param require_found if YES, connections sending messages of unknown type
484 * @return handle for the new server, NULL on error
485 * (typically, "port" already in use)
487 struct GNUNET_SERVER_Handle *
488 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
490 struct GNUNET_NETWORK_Handle **lsocks,
491 struct GNUNET_TIME_Relative idle_timeout,
494 struct GNUNET_SERVER_Handle *server;
495 struct GNUNET_NETWORK_FDSet *r;
498 server = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
499 server->idle_timeout = idle_timeout;
500 server->listen_sockets = lsocks;
501 server->access = access;
502 server->access_cls = access_cls;
503 server->require_found = require_found;
506 r = GNUNET_NETWORK_fdset_create ();
508 while (NULL != server->listen_sockets[i])
509 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
510 server->listen_task =
511 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
512 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
513 &process_listen_socket, server);
514 GNUNET_NETWORK_fdset_destroy (r);
521 * Create a new server.
523 * @param access function for access control
524 * @param access_cls closure for access
525 * @param serverAddr address to listen on (including port), NULL terminated array
526 * @param socklen length of serverAddr
527 * @param idle_timeout after how long should we timeout idle connections?
528 * @param require_found if YES, connections sending messages of unknown type
530 * @return handle for the new server, NULL on error
531 * (typically, "port" already in use)
533 struct GNUNET_SERVER_Handle *
534 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
535 struct sockaddr *const *serverAddr,
536 const socklen_t * socklen,
537 struct GNUNET_TIME_Relative idle_timeout,
540 struct GNUNET_NETWORK_Handle **lsocks;
547 while (NULL != serverAddr[i])
551 lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
554 while (NULL != serverAddr[i])
558 if ( (socklen[k] == socklen[i]) &&
559 (0 == memcmp (serverAddr[k], serverAddr[i], socklen[i])) )
566 /* duplicate address, skip */
570 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
571 if (NULL != lsocks[j])
578 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
579 GNUNET_free (lsocks);
587 return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks,
588 idle_timeout, require_found);
593 * Set the 'monitor' flag on this client. Clients which have been
594 * marked as 'monitors' won't prevent the server from shutting down
595 * once 'GNUNET_SERVER_stop_listening' has been invoked. The idea is
596 * that for "normal" clients we likely want to allow them to process
597 * their requests; however, monitor-clients are likely to 'never'
598 * disconnect during shutdown and thus will not be considered when
599 * determining if the server should continue to exist after
600 * 'GNUNET_SERVER_destroy' has been called.
602 * @param client the client to set the 'monitor' flag on
605 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client)
607 client->is_monitor = GNUNET_YES;
612 * Helper function for 'test_monitor_clients' to trigger
613 * 'GNUNET_SERVER_destroy' after the stack has unwound.
615 * @param cls the 'struct GNUNET_SERVER_Handle' to destroy
619 do_destroy (void *cls,
620 const struct GNUNET_SCHEDULER_TaskContext *tc)
622 struct GNUNET_SERVER_Handle *server = cls;
623 GNUNET_SERVER_destroy (server);
628 * Check if only 'monitor' clients are left. If so, destroy the
631 * @param server server to test for full shutdown
634 test_monitor_clients (struct GNUNET_SERVER_Handle *server)
636 struct GNUNET_SERVER_Client *client;
638 if (GNUNET_YES != server->in_soft_shutdown)
640 for (client = server->clients; NULL != client; client = client->next)
641 if (GNUNET_NO == client->is_monitor)
642 return; /* not done yet */
643 server->in_soft_shutdown = GNUNET_SYSERR;
644 GNUNET_SCHEDULER_add_continuation (&do_destroy, server,
645 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
650 * Stop the listen socket and get ready to shutdown the server
651 * once only 'monitor' clients are left.
653 * @param server server to stop listening on
656 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server)
660 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server in soft shutdown\n");
661 if (GNUNET_SCHEDULER_NO_TASK != server->listen_task)
663 GNUNET_SCHEDULER_cancel (server->listen_task);
664 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
666 if (NULL != server->listen_sockets)
669 while (NULL != server->listen_sockets[i])
670 GNUNET_break (GNUNET_OK ==
671 GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
672 GNUNET_free (server->listen_sockets);
673 server->listen_sockets = NULL;
675 if (GNUNET_NO == server->in_soft_shutdown)
676 server->in_soft_shutdown = GNUNET_YES;
677 test_monitor_clients (server);
682 * Free resources held by this server.
684 * @param server server to destroy
687 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server)
689 struct HandlerList *hpos;
690 struct NotifyList *npos;
693 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
694 if (GNUNET_SCHEDULER_NO_TASK != server->listen_task)
696 GNUNET_SCHEDULER_cancel (server->listen_task);
697 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
699 if (NULL != server->listen_sockets)
702 while (NULL != server->listen_sockets[i])
703 GNUNET_break (GNUNET_OK ==
704 GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
705 GNUNET_free (server->listen_sockets);
706 server->listen_sockets = NULL;
708 while (NULL != server->clients)
709 GNUNET_SERVER_client_disconnect (server->clients);
710 while (NULL != (hpos = server->handlers))
712 server->handlers = hpos->next;
715 while (NULL != (npos = server->disconnect_notify_list_head))
717 npos->callback (npos->callback_cls, NULL);
718 GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
719 server->disconnect_notify_list_tail,
723 GNUNET_free (server);
728 * Add additional handlers to an existing server.
730 * @param server the server to add handlers to
731 * @param handlers array of message handlers for
732 * incoming messages; the last entry must
733 * have "NULL" for the "callback"; multiple
734 * entries for the same type are allowed,
735 * they will be called in order of occurence.
736 * These handlers can be removed later;
737 * the handlers array must exist until removed
738 * (or server is destroyed).
741 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
742 const struct GNUNET_SERVER_MessageHandler *handlers)
744 struct HandlerList *p;
746 p = GNUNET_malloc (sizeof (struct HandlerList));
747 p->handlers = handlers;
748 p->next = server->handlers;
749 server->handlers = p;
754 * Change functions used by the server to tokenize the message stream.
755 * (very rarely used).
757 * @param server server to modify
758 * @param create new tokenizer initialization function
759 * @param destroy new tokenizer destruction function
760 * @param receive new tokenizer receive function
761 * @param cls closure for 'create', 'receive', 'destroy'
764 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
765 GNUNET_SERVER_MstCreateCallback create,
766 GNUNET_SERVER_MstDestroyCallback destroy,
767 GNUNET_SERVER_MstReceiveCallback receive,
770 server->mst_create = create;
771 server->mst_destroy = destroy;
772 server->mst_receive = receive;
773 server->mst_cls = cls;
778 * Task run to warn about missing calls to 'GNUNET_SERVER_receive_done'.
780 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
781 * @param tc scheduler context (unused)
784 warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
786 struct GNUNET_SERVER_Client *client = cls;
789 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
790 &warn_no_receive_done, client);
791 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
792 LOG (GNUNET_ERROR_TYPE_WARNING,
794 ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"),
795 (unsigned int) client->warn_type,
797 GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value);
802 * Disable the warning the server issues if a message is not acknowledged
803 * in a timely fashion. Use this call if a client is intentionally delayed
804 * for a while. Only applies to the current message.
806 * @param client client for which to disable the warning
809 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
811 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
813 GNUNET_SCHEDULER_cancel (client->warn_task);
814 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
820 * Inject a message into the server, pretend it came
821 * from the specified client. Delivery of the message
822 * will happen instantly (if a handler is installed;
823 * otherwise the call does nothing).
825 * @param server the server receiving the message
826 * @param sender the "pretended" sender of the message
828 * @param message message to transmit
829 * @return GNUNET_OK if the message was OK and the
830 * connection can stay open
831 * GNUNET_SYSERR if the connection to the
832 * client should be shut down
835 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
836 struct GNUNET_SERVER_Client *sender,
837 const struct GNUNET_MessageHeader *message)
839 struct HandlerList *pos;
840 const struct GNUNET_SERVER_MessageHandler *mh;
846 type = ntohs (message->type);
847 size = ntohs (message->size);
848 LOG (GNUNET_ERROR_TYPE_DEBUG,
849 "Server schedules transmission of %u-byte message of type %u to client.\n",
852 for (pos = server->handlers; NULL != pos; pos = pos->next)
855 while (pos->handlers[i].callback != NULL)
857 mh = &pos->handlers[i];
858 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
860 if ((0 != mh->expected_size) && (mh->expected_size != size))
862 #if GNUNET8_NETWORK_IS_DEAD
863 LOG (GNUNET_ERROR_TYPE_WARNING,
864 "Expected %u bytes for message of type %u, got %u\n",
865 mh->expected_size, mh->type, size);
868 return GNUNET_SYSERR;
872 if (0 == sender->suspended)
874 sender->warn_start = GNUNET_TIME_absolute_get ();
876 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
877 &warn_no_receive_done, sender);
878 sender->warn_type = type;
882 mh->callback (mh->callback_cls, sender, message);
888 if (GNUNET_NO == found)
890 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
891 "Received message of unknown type %d\n", type);
892 if (GNUNET_YES == server->require_found)
893 return GNUNET_SYSERR;
900 * We are receiving an incoming message. Process it.
902 * @param cls our closure (handle for the client)
903 * @param buf buffer with data received from network
904 * @param available number of bytes available in buf
905 * @param addr address of the sender
906 * @param addrlen length of addr
907 * @param errCode code indicating errors receiving, 0 for success
910 process_incoming (void *cls, const void *buf, size_t available,
911 const struct sockaddr *addr, socklen_t addrlen, int errCode);
915 * Process messages from the client's message tokenizer until either
916 * the tokenizer is empty (and then schedule receiving more), or
917 * until some handler is not immediately done (then wait for restart_processing)
920 * @param client the client to process, RC must have already been increased
921 * using GNUNET_SERVER_client_keep and will be decreased by one in this
923 * @param ret GNUNET_NO to start processing from the buffer,
924 * GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
925 * GNUNET_SYSERR if we should instantly abort due to error in a previous step
928 process_mst (struct GNUNET_SERVER_Client *client, int ret)
930 while ((GNUNET_SYSERR != ret) && (NULL != client->server) &&
931 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
933 if (GNUNET_OK == ret)
935 LOG (GNUNET_ERROR_TYPE_DEBUG,
936 "Server re-enters receive loop, timeout: %llu.\n",
937 client->idle_timeout.rel_value);
938 client->receive_pending = GNUNET_YES;
939 GNUNET_CONNECTION_receive (client->connection,
940 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
941 client->idle_timeout, &process_incoming,
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 "Server processes additional messages instantly.\n");
947 if (NULL != client->server->mst_receive)
949 client->server->mst_receive (client->server->mst_cls, client->mst,
950 client, NULL, 0, GNUNET_NO, GNUNET_YES);
953 GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
956 LOG (GNUNET_ERROR_TYPE_DEBUG,
957 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
958 ret, client->server, client->shutdown_now, client->suspended);
959 if (GNUNET_NO == ret)
961 LOG (GNUNET_ERROR_TYPE_DEBUG,
962 "Server has more data pending but is suspended.\n");
963 client->receive_pending = GNUNET_SYSERR; /* data pending */
965 if ((GNUNET_SYSERR == ret) || (GNUNET_YES == client->shutdown_now))
966 GNUNET_SERVER_client_disconnect (client);
967 GNUNET_SERVER_client_drop (client);
972 * We are receiving an incoming message. Process it.
974 * @param cls our closure (handle for the client)
975 * @param buf buffer with data received from network
976 * @param available number of bytes available in buf
977 * @param addr address of the sender
978 * @param addrlen length of addr
979 * @param errCode code indicating errors receiving, 0 for success
982 process_incoming (void *cls, const void *buf, size_t available,
983 const struct sockaddr *addr, socklen_t addrlen, int errCode)
985 struct GNUNET_SERVER_Client *client = cls;
986 struct GNUNET_SERVER_Handle *server = client->server;
987 struct GNUNET_TIME_Absolute end;
988 struct GNUNET_TIME_Absolute now;
991 GNUNET_assert (GNUNET_YES == client->receive_pending);
992 client->receive_pending = GNUNET_NO;
993 now = GNUNET_TIME_absolute_get ();
994 end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout);
996 if ((NULL == buf) && (0 == available) && (NULL == addr) && (0 == errCode) &&
997 (GNUNET_YES != client->shutdown_now) && (NULL != server) &&
998 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
999 (end.abs_value > now.abs_value))
1001 /* wait longer, timeout changed (i.e. due to us sending) */
1002 LOG (GNUNET_ERROR_TYPE_DEBUG,
1003 "Receive time out, but no disconnect due to sending (%p)\n",
1004 GNUNET_a2s (addr, addrlen));
1005 client->receive_pending = GNUNET_YES;
1006 GNUNET_CONNECTION_receive (client->connection,
1007 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1008 GNUNET_TIME_absolute_get_remaining (end),
1009 &process_incoming, client);
1012 if ((NULL == buf) || (0 == available) || (0 != errCode) || (NULL == server) ||
1013 (GNUNET_YES == client->shutdown_now) ||
1014 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
1016 /* other side closed connection, error connecting, etc. */
1017 GNUNET_SERVER_client_disconnect (client);
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n",
1021 (unsigned int) available, GNUNET_a2s (addr, addrlen));
1022 GNUNET_SERVER_client_keep (client);
1023 client->last_activity = now;
1025 if (NULL != server->mst_receive)
1027 client->server->mst_receive (client->server->mst_cls, client->mst,
1028 client, buf, available, GNUNET_NO, GNUNET_YES);
1031 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO,
1033 process_mst (client, ret);
1038 * Task run to start again receiving from the network
1039 * and process requests.
1041 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
1042 * @param tc scheduler context (unused)
1045 restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1047 struct GNUNET_SERVER_Client *client = cls;
1049 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1050 if (GNUNET_NO == client->receive_pending)
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n");
1053 client->receive_pending = GNUNET_YES;
1054 GNUNET_CONNECTION_receive (client->connection,
1055 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1056 client->idle_timeout, &process_incoming, client);
1059 LOG (GNUNET_ERROR_TYPE_DEBUG,
1060 "Server continues processing messages still in the buffer.\n");
1061 GNUNET_SERVER_client_keep (client);
1062 client->receive_pending = GNUNET_NO;
1063 process_mst (client, GNUNET_NO);
1068 * This function is called whenever our inbound message tokenizer has
1069 * received a complete message.
1071 * @param cls closure (struct GNUNET_SERVER_Handle)
1072 * @param client identification of the client (struct GNUNET_SERVER_Client*)
1073 * @param message the actual message
1076 client_message_tokenizer_callback (void *cls, void *client,
1077 const struct GNUNET_MessageHeader *message)
1079 struct GNUNET_SERVER_Handle *server = cls;
1080 struct GNUNET_SERVER_Client *sender = client;
1083 LOG (GNUNET_ERROR_TYPE_DEBUG,
1084 "Tokenizer gives server message of type %u from client\n",
1085 ntohs (message->type));
1086 sender->in_process_client_buffer = GNUNET_YES;
1087 ret = GNUNET_SERVER_inject (server, sender, message);
1088 sender->in_process_client_buffer = GNUNET_NO;
1089 if (GNUNET_OK != ret)
1090 GNUNET_SERVER_client_disconnect (sender);
1095 * Add a TCP socket-based connection to the set of handles managed by
1096 * this server. Use this function for outgoing (P2P) connections that
1097 * we initiated (and where this server should process incoming
1100 * @param server the server to use
1101 * @param connection the connection to manage (client must
1102 * stop using this connection from now on)
1103 * @return the client handle (client should call
1104 * "client_drop" on the return value eventually)
1106 struct GNUNET_SERVER_Client *
1107 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
1108 struct GNUNET_CONNECTION_Handle *connection)
1110 struct GNUNET_SERVER_Client *client;
1112 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
1113 client->connection = connection;
1114 client->reference_count = 1;
1115 client->server = server;
1116 client->last_activity = GNUNET_TIME_absolute_get ();
1117 client->next = server->clients;
1118 client->idle_timeout = server->idle_timeout;
1119 server->clients = client;
1120 if (NULL != server->mst_create)
1122 server->mst_create (server->mst_cls, client);
1125 GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server);
1126 client->receive_pending = GNUNET_YES;
1127 GNUNET_CONNECTION_receive (client->connection,
1128 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1129 client->idle_timeout, &process_incoming, client);
1135 * Change the timeout for a particular client. Decreasing the timeout
1136 * may not go into effect immediately (only after the previous timeout
1137 * times out or activity happens on the socket).
1139 * @param client the client to update
1140 * @param timeout new timeout for activities on the socket
1143 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
1144 struct GNUNET_TIME_Relative timeout)
1146 client->idle_timeout = timeout;
1151 * Notify the server that the given client handle should
1152 * be kept (keeps the connection up if possible, increments
1153 * the internal reference counter).
1155 * @param client the client to keep
1158 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1160 client->reference_count++;
1165 * Notify the server that the given client handle is no
1166 * longer required. Decrements the reference counter. If
1167 * that counter reaches zero an inactive connection maybe
1170 * @param client the client to drop
1173 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1175 GNUNET_assert (client->reference_count > 0);
1176 client->reference_count--;
1177 if ((GNUNET_YES == client->shutdown_now) && (0 == client->reference_count))
1178 GNUNET_SERVER_client_disconnect (client);
1183 * Obtain the network address of the other party.
1185 * @param client the client to get the address for
1186 * @param addr where to store the address
1187 * @param addrlen where to store the length of the address
1188 * @return GNUNET_OK on success
1191 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1192 void **addr, size_t * addrlen)
1194 return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen);
1199 * Ask the server to notify us whenever a client disconnects.
1200 * This function is called whenever the actual network connection
1201 * is closed; the reference count may be zero or larger than zero
1204 * @param server the server manageing the clients
1205 * @param callback function to call on disconnect
1206 * @param callback_cls closure for callback
1209 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1210 GNUNET_SERVER_DisconnectCallback callback,
1213 struct NotifyList *n;
1215 n = GNUNET_malloc (sizeof (struct NotifyList));
1216 n->callback = callback;
1217 n->callback_cls = callback_cls;
1218 GNUNET_CONTAINER_DLL_insert (server->disconnect_notify_list_head,
1219 server->disconnect_notify_list_tail,
1225 * Ask the server to stop notifying us whenever a client disconnects.
1227 * @param server the server manageing the clients
1228 * @param callback function to call on disconnect
1229 * @param callback_cls closure for callback
1232 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1233 GNUNET_SERVER_DisconnectCallback
1234 callback, void *callback_cls)
1236 struct NotifyList *pos;
1238 for (pos = server->disconnect_notify_list_head; NULL != pos; pos = pos->next)
1239 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1246 GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
1247 server->disconnect_notify_list_tail,
1254 * Ask the server to disconnect from the given client.
1255 * This is the same as returning GNUNET_SYSERR from a message
1256 * handler, except that it allows dropping of a client even
1257 * when not handling a message from that client.
1259 * @param client the client to disconnect from
1262 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1264 struct GNUNET_SERVER_Handle *server = client->server;
1265 struct GNUNET_SERVER_Client *prev;
1266 struct GNUNET_SERVER_Client *pos;
1267 struct NotifyList *n;
1270 LOG (GNUNET_ERROR_TYPE_DEBUG,
1271 "Client is being disconnected from the server.\n");
1272 if (GNUNET_SCHEDULER_NO_TASK != client->restart_task)
1274 GNUNET_SCHEDULER_cancel (client->restart_task);
1275 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1277 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1279 GNUNET_SCHEDULER_cancel (client->warn_task);
1280 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1282 if (GNUNET_YES == client->receive_pending)
1284 GNUNET_CONNECTION_receive_cancel (client->connection);
1285 client->receive_pending = GNUNET_NO;
1287 rc = client->reference_count;
1288 client->reference_count++; /* make sure nobody else clean up client... */
1289 if ( (GNUNET_YES != client->shutdown_now) &&
1292 client->shutdown_now = GNUNET_YES;
1294 pos = server->clients;
1295 while ((NULL != pos) && (pos != client))
1300 GNUNET_assert (NULL != pos);
1302 server->clients = pos->next;
1304 prev->next = pos->next;
1305 if (GNUNET_SCHEDULER_NO_TASK != client->restart_task)
1307 GNUNET_SCHEDULER_cancel (client->restart_task);
1308 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1310 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1312 GNUNET_SCHEDULER_cancel (client->warn_task);
1313 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1315 for (n = server->disconnect_notify_list_head; NULL != n; n = n->next)
1316 n->callback (n->callback_cls, client);
1317 if (NULL != server->mst_destroy)
1318 server->mst_destroy (server->mst_cls, client->mst);
1320 GNUNET_SERVER_mst_destroy (client->mst);
1323 client->reference_count--;
1326 LOG (GNUNET_ERROR_TYPE_DEBUG,
1327 "RC still positive, not destroying everything.\n");
1328 client->server = NULL;
1331 if (GNUNET_YES == client->in_process_client_buffer)
1333 LOG (GNUNET_ERROR_TYPE_DEBUG,
1334 "Still processing inputs, not destroying everything.\n");
1337 if (GNUNET_YES == client->persist)
1338 GNUNET_CONNECTION_persist_ (client->connection);
1339 if (NULL != client->th.cth)
1340 GNUNET_SERVER_notify_transmit_ready_cancel (&client->th);
1341 GNUNET_CONNECTION_destroy (client->connection);
1342 GNUNET_free (client);
1343 /* we might be in soft-shutdown, test if we're done */
1345 test_monitor_clients (server);
1350 * Disable the "CORK" feature for communication with the given client,
1351 * forcing the OS to immediately flush the buffer on transmission
1352 * instead of potentially buffering multiple messages.
1354 * @param client handle to the client
1355 * @return GNUNET_OK on success
1358 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client)
1360 return GNUNET_CONNECTION_disable_corking (client->connection);
1365 * Wrapper for transmission notification that calls the original
1366 * callback and update the last activity time for our connection.
1368 * @param cls the 'struct GNUNET_SERVER_Client'
1369 * @param size number of bytes we can transmit
1370 * @param buf where to copy the message
1371 * @return number of bytes actually transmitted
1374 transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
1376 struct GNUNET_SERVER_Client *client = cls;
1377 GNUNET_CONNECTION_TransmitReadyNotify callback;
1380 client->th.cth = NULL;
1381 callback = client->th.callback;
1382 client->th.callback = NULL;
1383 client->last_activity = GNUNET_TIME_absolute_get ();
1384 ret = callback (client->th.callback_cls, size, buf);
1390 * Notify us when the server has enough space to transmit
1391 * a message of the given size to the given client.
1393 * @param client client to transmit message to
1394 * @param size requested amount of buffer space
1395 * @param timeout after how long should we give up (and call
1396 * notify with buf NULL and size 0)?
1397 * @param callback function to call when space is available
1398 * @param callback_cls closure for callback
1399 * @return non-NULL if the notify callback was queued; can be used
1400 * to cancel the request using
1401 * GNUNET_SERVER_notify_transmit_ready_cancel.
1402 * NULL if we are already going to notify someone else (busy)
1404 struct GNUNET_SERVER_TransmitHandle *
1405 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1407 struct GNUNET_TIME_Relative timeout,
1408 GNUNET_CONNECTION_TransmitReadyNotify
1409 callback, void *callback_cls)
1411 if (NULL != client->th.callback)
1413 client->th.callback_cls = callback_cls;
1414 client->th.callback = callback;
1415 client->th.cth = GNUNET_CONNECTION_notify_transmit_ready (client->connection, size,
1417 &transmit_ready_callback_wrapper,
1424 * Abort transmission request.
1426 * @param th request to abort
1429 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th)
1431 GNUNET_CONNECTION_notify_transmit_ready_cancel (th->cth);
1433 th->callback = NULL;
1438 * Set the persistent flag on this client, used to setup client connection
1439 * to only be killed when the service it's connected to is actually dead.
1441 * @param client the client to set the persistent flag on
1444 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1446 client->persist = GNUNET_YES;
1451 * Resume receiving from this client, we are done processing the
1452 * current request. This function must be called from within each
1453 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1455 * @param client client we were processing a message of
1456 * @param success GNUNET_OK to keep the connection open and
1457 * continue to receive
1458 * GNUNET_NO to close the connection (normal behavior)
1459 * GNUNET_SYSERR to close the connection (signal
1463 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1467 GNUNET_assert (client->suspended > 0);
1468 client->suspended--;
1469 if (GNUNET_OK != success)
1471 LOG (GNUNET_ERROR_TYPE_DEBUG,
1472 "GNUNET_SERVER_receive_done called with failure indication\n");
1473 GNUNET_SERVER_client_disconnect (client);
1476 if (client->suspended > 0)
1478 LOG (GNUNET_ERROR_TYPE_DEBUG,
1479 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1482 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1484 GNUNET_SCHEDULER_cancel (client->warn_task);
1485 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1487 if (GNUNET_YES == client->in_process_client_buffer)
1489 LOG (GNUNET_ERROR_TYPE_DEBUG,
1490 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1493 if ((NULL == client->server) || (GNUNET_YES == client->shutdown_now))
1495 GNUNET_SERVER_client_disconnect (client);
1498 LOG (GNUNET_ERROR_TYPE_DEBUG,
1499 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1500 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1501 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing, client);
1505 /* end of server.c */