2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief library for building GNUnet network servers
24 * @author Christian Grothoff
28 #include "gnunet_common.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_server_lib.h"
32 #include "gnunet_time_lib.h"
33 #include "gnunet_disk_lib.h"
34 #include "gnunet_protocols.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
38 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
40 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42 #define DEBUG_SERVER GNUNET_EXTRA_LOGGING
45 * List of arrays of message handlers.
50 * This is a linked list.
52 struct HandlerList *next;
55 * NULL-terminated array of handlers.
57 const struct GNUNET_SERVER_MessageHandler *handlers;
62 * List of arrays of message handlers.
67 * This is a linked list.
69 struct NotifyList *next;
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 * Linked list of functions to call on disconnects by clients.
101 struct NotifyList *disconnect_notify_list;
104 * Function to call for access control.
106 GNUNET_CONNECTION_AccessCheck access;
109 * Closure for access.
114 * NULL-terminated array of sockets used to listen for new
117 struct GNUNET_NETWORK_Handle **listen_sockets;
120 * After how long should an idle connection time
123 struct GNUNET_TIME_Relative idle_timeout;
126 * Task scheduled to do the listening.
128 GNUNET_SCHEDULER_TaskIdentifier listen_task;
131 * Do we ignore messages of types that we do not understand or do we
132 * require that a handler is found (and if not kill the connection)?
137 * Should all of the clients of this server continue to process
138 * connections as usual even if we get a shutdown request? (the
139 * listen socket always ignores shutdown).
141 int clients_ignore_shutdown;
143 GNUNET_SERVER_MstCreateCallback mst_create;
144 GNUNET_SERVER_MstDestroyCallback mst_destroy;
145 GNUNET_SERVER_MstReceiveCallback mst_receive;
151 * @brief handle for a client of the server
153 struct GNUNET_SERVER_Client
157 * This is a linked list.
159 struct GNUNET_SERVER_Client *next;
162 * Processing of incoming data.
167 * Server that this client belongs to.
169 struct GNUNET_SERVER_Handle *server;
172 * Client closure for callbacks.
174 struct GNUNET_CONNECTION_Handle *connection;
177 * ID of task used to restart processing.
179 GNUNET_SCHEDULER_TaskIdentifier restart_task;
182 * Task that warns about missing calls to 'GNUNET_SERVER_receive_done'.
184 GNUNET_SCHEDULER_TaskIdentifier warn_task;
187 * Time when the warn task was started.
189 struct GNUNET_TIME_Absolute warn_start;
192 * Last activity on this socket (used to time it out
193 * if reference_count == 0).
195 struct GNUNET_TIME_Absolute last_activity;
200 GNUNET_CONNECTION_TransmitReadyNotify callback;
208 * After how long should an idle connection time
211 struct GNUNET_TIME_Relative idle_timeout;
214 * Number of external entities with a reference to
215 * this client object.
217 unsigned int reference_count;
220 * Was processing if incoming messages suspended while
221 * we were still processing data already received?
222 * This is a counter saying how often processing was
223 * suspended (once per handler invoked).
225 unsigned int suspended;
228 * Are we currently in the "process_client_buffer" function (and
229 * will hence restart the receive job on exit if suspended == 0 once
230 * we are done?). If this is set, then "receive_done" will
231 * essentially only decrement suspended; if this is not set, then
232 * "receive_done" may need to restart the receive process (either
233 * from the side-buffer or via select/recv).
235 int in_process_client_buffer;
238 * We're about to close down this client due to some serious
244 * Are we currently trying to receive? (YES if we are, NO if we are not,
245 * SYSERR if data is already available in MST).
250 * Finish pending write when disconnecting?
252 int finish_pending_write;
255 * Persist the file handle for this client no matter what happens,
256 * force the OS to close once the process actually dies. Should only
257 * be used in special cases!
262 * Type of last message processed (for warn_no_receive_done).
269 * Scheduler says our listen socket is ready. Process it!
271 * @param cls handle to our server for which we are processing the listen
273 * @param tc reason why we are running right now
276 process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
278 struct GNUNET_SERVER_Handle *server = cls;
279 struct GNUNET_CONNECTION_Handle *sock;
280 struct GNUNET_SERVER_Client *client;
281 struct GNUNET_NETWORK_FDSet *r;
284 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
285 r = GNUNET_NETWORK_fdset_create ();
287 while (NULL != server->listen_sockets[i])
288 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
289 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
291 /* ignore shutdown, someone else will take care of it! */
292 server->listen_task =
293 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
294 GNUNET_SCHEDULER_NO_TASK,
295 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
296 &process_listen_socket, server);
297 GNUNET_NETWORK_fdset_destroy (r);
301 while (NULL != server->listen_sockets[i])
303 if (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_sockets[i]))
306 GNUNET_CONNECTION_create_from_accept (server->access,
308 server->listen_sockets[i]);
312 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server accepted incoming connection.\n");
314 client = GNUNET_SERVER_connect_socket (server, sock);
315 GNUNET_CONNECTION_ignore_shutdown (sock,
316 server->clients_ignore_shutdown);
317 /* decrement reference count, we don't keep "client" alive */
318 GNUNET_SERVER_client_drop (client);
323 /* listen for more! */
324 server->listen_task =
325 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
326 GNUNET_SCHEDULER_NO_TASK,
327 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
328 &process_listen_socket, server);
329 GNUNET_NETWORK_fdset_destroy (r);
334 * Create and initialize a listen socket for the server.
336 * @param serverAddr address to listen on
337 * @param socklen length of address
338 * @return NULL on error, otherwise the listen socket
340 static struct GNUNET_NETWORK_Handle *
341 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
343 const static int on = 1;
344 struct GNUNET_NETWORK_Handle *sock;
348 switch (serverAddr->sa_family)
351 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
354 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
364 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
367 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
373 if (GNUNET_NETWORK_socket_setsockopt
374 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
375 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
378 if ((serverAddr->sa_family == AF_INET6) &&
379 (GNUNET_NETWORK_socket_setsockopt
380 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
381 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
385 /* bind the socket */
386 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
389 if (errno != EADDRINUSE)
391 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
392 * fail if we already took the port on IPv6; if both IPv4 and
393 * IPv6 binds fail, then our caller will log using the
394 * errno preserved in 'eno' */
395 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
397 LOG (GNUNET_ERROR_TYPE_ERROR, _("`%s' failed for port %d (%s).\n"),
399 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
405 LOG (GNUNET_ERROR_TYPE_WARNING,
406 _("`%s' failed for port %d (%s): address already in use\n"),
408 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
409 else if (serverAddr->sa_family == AF_UNIX)
410 LOG (GNUNET_ERROR_TYPE_WARNING,
411 _("`%s' failed for `%s': address already in use\n"), "bind",
412 ((const struct sockaddr_un *) serverAddr)->sun_path);
415 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
419 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
421 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen");
422 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
428 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server starts to listen on port %u.\n",
436 * Create a new server.
438 * @param access function for access control
439 * @param access_cls closure for access
440 * @param lsocks NULL-terminated array of listen sockets
441 * @param idle_timeout after how long should we timeout idle connections?
442 * @param require_found if YES, connections sending messages of unknown type
444 * @return handle for the new server, NULL on error
445 * (typically, "port" already in use)
447 struct GNUNET_SERVER_Handle *
448 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
450 struct GNUNET_NETWORK_Handle **lsocks,
451 struct GNUNET_TIME_Relative idle_timeout,
454 struct GNUNET_SERVER_Handle *ret;
455 struct GNUNET_NETWORK_FDSet *r;
458 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
459 ret->idle_timeout = idle_timeout;
460 ret->listen_sockets = lsocks;
461 ret->access = access;
462 ret->access_cls = access_cls;
463 ret->require_found = require_found;
466 r = GNUNET_NETWORK_fdset_create ();
468 while (NULL != ret->listen_sockets[i])
469 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]);
471 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
472 GNUNET_SCHEDULER_NO_TASK,
473 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
474 &process_listen_socket, ret);
475 GNUNET_NETWORK_fdset_destroy (r);
482 * Create a new server.
484 * @param access function for access control
485 * @param access_cls closure for access
486 * @param serverAddr address to listen on (including port), NULL terminated array
487 * @param socklen length of serverAddr
488 * @param idle_timeout after how long should we timeout idle connections?
489 * @param require_found if YES, connections sending messages of unknown type
491 * @return handle for the new server, NULL on error
492 * (typically, "port" already in use)
494 struct GNUNET_SERVER_Handle *
495 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
496 struct sockaddr *const *serverAddr,
497 const socklen_t * socklen,
498 struct GNUNET_TIME_Relative idle_timeout,
501 struct GNUNET_NETWORK_Handle **lsocks;
506 while (serverAddr[i] != NULL)
510 lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
513 while (serverAddr[i] != NULL)
515 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
516 if (lsocks[j] != NULL)
523 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
524 GNUNET_free (lsocks);
532 return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks,
533 idle_timeout, require_found);
538 * Free resources held by this server.
540 * @param s server to destroy
543 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
545 struct HandlerList *hpos;
546 struct NotifyList *npos;
550 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
552 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
554 GNUNET_SCHEDULER_cancel (s->listen_task);
555 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
557 if (s->listen_sockets != NULL)
560 while (s->listen_sockets[i] != NULL)
561 GNUNET_break (GNUNET_OK ==
562 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
563 GNUNET_free (s->listen_sockets);
564 s->listen_sockets = NULL;
566 while (s->clients != NULL)
567 GNUNET_SERVER_client_disconnect (s->clients);
568 while (NULL != (hpos = s->handlers))
570 s->handlers = hpos->next;
573 while (NULL != (npos = s->disconnect_notify_list))
575 npos->callback (npos->callback_cls, NULL);
576 s->disconnect_notify_list = npos->next;
584 * Add additional handlers to an existing server.
586 * @param server the server to add handlers to
587 * @param handlers array of message handlers for
588 * incoming messages; the last entry must
589 * have "NULL" for the "callback"; multiple
590 * entries for the same type are allowed,
591 * they will be called in order of occurence.
592 * These handlers can be removed later;
593 * the handlers array must exist until removed
594 * (or server is destroyed).
597 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
598 const struct GNUNET_SERVER_MessageHandler *handlers)
600 struct HandlerList *p;
602 p = GNUNET_malloc (sizeof (struct HandlerList));
603 p->handlers = handlers;
604 p->next = server->handlers;
605 server->handlers = p;
610 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
611 GNUNET_SERVER_MstCreateCallback create,
612 GNUNET_SERVER_MstDestroyCallback destroy,
613 GNUNET_SERVER_MstReceiveCallback receive,
616 server->mst_create = create;
617 server->mst_destroy = destroy;
618 server->mst_receive = receive;
619 server->mst_cls = cls;
624 * Task run to warn about missing calls to 'GNUNET_SERVER_receive_done'.
626 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
627 * @param tc scheduler context (unused)
630 warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
632 struct GNUNET_SERVER_Client *client = cls;
635 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
636 &warn_no_receive_done, client);
637 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
638 LOG (GNUNET_ERROR_TYPE_WARNING,
640 ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"),
641 (unsigned int) client->warn_type,
643 GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value);
648 * Disable the warning the server issues if a message is not acknowledged
649 * in a timely fashion. Use this call if a client is intentionally delayed
650 * for a while. Only applies to the current message.
652 * @param client client for which to disable the warning
655 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
657 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
659 GNUNET_SCHEDULER_cancel (client->warn_task);
660 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
666 * Inject a message into the server, pretend it came
667 * from the specified client. Delivery of the message
668 * will happen instantly (if a handler is installed;
669 * otherwise the call does nothing).
671 * @param server the server receiving the message
672 * @param sender the "pretended" sender of the message
674 * @param message message to transmit
675 * @return GNUNET_OK if the message was OK and the
676 * connection can stay open
677 * GNUNET_SYSERR if the connection to the
678 * client should be shut down
681 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
682 struct GNUNET_SERVER_Client *sender,
683 const struct GNUNET_MessageHeader *message)
685 struct HandlerList *pos;
686 const struct GNUNET_SERVER_MessageHandler *mh;
692 type = ntohs (message->type);
693 size = ntohs (message->size);
696 LOG (GNUNET_ERROR_TYPE_DEBUG,
697 "Server schedules transmission of %u-byte message of type %u to client.\n",
700 pos = server->handlers;
705 while (pos->handlers[i].callback != NULL)
707 mh = &pos->handlers[i];
708 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
710 if ((mh->expected_size != 0) && (mh->expected_size != size))
712 #if GNUNET8_NETWORK_IS_DEAD
713 LOG (GNUNET_ERROR_TYPE_WARNING,
714 "Expected %u bytes for message of type %u, got %u\n",
715 mh->expected_size, mh->type, size);
718 return GNUNET_SYSERR;
722 if (0 == sender->suspended)
724 sender->warn_start = GNUNET_TIME_absolute_get ();
726 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
727 &warn_no_receive_done, sender);
728 sender->warn_type = type;
732 mh->callback (mh->callback_cls, sender, message);
739 if (found == GNUNET_NO)
741 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
742 "Received message of unknown type %d\n", type);
743 if (server->require_found == GNUNET_YES)
744 return GNUNET_SYSERR;
751 * We are receiving an incoming message. Process it.
753 * @param cls our closure (handle for the client)
754 * @param buf buffer with data received from network
755 * @param available number of bytes available in buf
756 * @param addr address of the sender
757 * @param addrlen length of addr
758 * @param errCode code indicating errors receiving, 0 for success
761 process_incoming (void *cls, const void *buf, size_t available,
762 const struct sockaddr *addr, socklen_t addrlen, int errCode);
766 * Process messages from the client's message tokenizer until either
767 * the tokenizer is empty (and then schedule receiving more), or
768 * until some handler is not immediately done (then wait for restart_processing)
771 * @param client the client to process, RC must have already been increased
772 * using GNUNET_SERVER_client_keep and will be decreased by one in this
774 * @param ret GNUNET_NO to start processing from the buffer,
775 * GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
776 * GNUNET_SYSERR if we should instantly abort due to error in a previous step
779 process_mst (struct GNUNET_SERVER_Client *client, int ret)
781 while ((ret != GNUNET_SYSERR) && (client->server != NULL) &&
782 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
784 if (ret == GNUNET_OK)
786 client->receive_pending = GNUNET_YES;
788 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 "Server re-enters receive loop, timeout: %llu.\n",
790 client->idle_timeout.rel_value);
792 GNUNET_CONNECTION_receive (client->connection,
793 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
794 client->idle_timeout, &process_incoming,
799 LOG (GNUNET_ERROR_TYPE_DEBUG,
800 "Server processes additional messages instantly.\n");
802 if (client->server->mst_receive != NULL)
804 client->server->mst_receive (client->server->mst_cls, client->mst,
805 client, NULL, 0, GNUNET_NO, GNUNET_YES);
808 GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
812 LOG (GNUNET_ERROR_TYPE_DEBUG,
813 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
814 ret, client->server, client->shutdown_now, client->suspended);
817 if (ret == GNUNET_NO)
820 LOG (GNUNET_ERROR_TYPE_DEBUG,
821 "Server has more data pending but is suspended.\n");
823 client->receive_pending = GNUNET_SYSERR; /* data pending */
825 if ((ret == GNUNET_SYSERR) || (GNUNET_YES == client->shutdown_now))
826 GNUNET_SERVER_client_disconnect (client);
827 GNUNET_SERVER_client_drop (client);
832 * We are receiving an incoming message. Process it.
834 * @param cls our closure (handle for the client)
835 * @param buf buffer with data received from network
836 * @param available number of bytes available in buf
837 * @param addr address of the sender
838 * @param addrlen length of addr
839 * @param errCode code indicating errors receiving, 0 for success
842 process_incoming (void *cls, const void *buf, size_t available,
843 const struct sockaddr *addr, socklen_t addrlen, int errCode)
845 struct GNUNET_SERVER_Client *client = cls;
846 struct GNUNET_SERVER_Handle *server = client->server;
847 struct GNUNET_TIME_Absolute end;
848 struct GNUNET_TIME_Absolute now;
851 GNUNET_assert (client->receive_pending == GNUNET_YES);
852 client->receive_pending = GNUNET_NO;
853 now = GNUNET_TIME_absolute_get ();
854 end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout);
856 if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) &&
857 (client->shutdown_now != GNUNET_YES) && (server != NULL) &&
858 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
859 (end.abs_value > now.abs_value))
861 /* wait longer, timeout changed (i.e. due to us sending) */
863 LOG (GNUNET_ERROR_TYPE_DEBUG,
864 "Receive time out, but no disconnect due to sending (%p)\n",
865 GNUNET_a2s (addr, addrlen));
867 client->receive_pending = GNUNET_YES;
868 GNUNET_CONNECTION_receive (client->connection,
869 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
870 GNUNET_TIME_absolute_get_remaining (end),
871 &process_incoming, client);
874 if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL) ||
875 (client->shutdown_now == GNUNET_YES) ||
876 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
878 /* other side closed connection, error connecting, etc. */
879 GNUNET_SERVER_client_disconnect (client);
883 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n",
884 (unsigned int) available, GNUNET_a2s (addr, addrlen));
886 GNUNET_SERVER_client_keep (client);
887 client->last_activity = now;
889 if (server->mst_receive != NULL)
891 client->server->mst_receive (client->server->mst_cls, client->mst,
892 client, buf, available, GNUNET_NO, GNUNET_YES);
895 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO,
898 process_mst (client, ret);
903 * Task run to start again receiving from the network
904 * and process requests.
906 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
907 * @param tc scheduler context (unused)
910 restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
912 struct GNUNET_SERVER_Client *client = cls;
913 struct GNUNET_SERVER_Handle *server = client->server;
915 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
916 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
917 (GNUNET_NO == server->clients_ignore_shutdown))
919 GNUNET_SERVER_client_disconnect (client);
922 if (client->receive_pending == GNUNET_NO)
925 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n");
927 client->receive_pending = GNUNET_YES;
928 GNUNET_CONNECTION_receive (client->connection,
929 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
930 client->idle_timeout, &process_incoming, client);
934 LOG (GNUNET_ERROR_TYPE_DEBUG,
935 "Server continues processing messages still in the buffer.\n");
937 GNUNET_SERVER_client_keep (client);
938 client->receive_pending = GNUNET_NO;
939 process_mst (client, GNUNET_NO);
944 * This function is called whenever our inbound message tokenizer has
945 * received a complete message.
947 * @param cls closure (struct GNUNET_SERVER_Handle)
948 * @param client identification of the client (struct GNUNET_SERVER_Client*)
949 * @param message the actual message
952 client_message_tokenizer_callback (void *cls, void *client,
953 const struct GNUNET_MessageHeader *message)
955 struct GNUNET_SERVER_Handle *server = cls;
956 struct GNUNET_SERVER_Client *sender = client;
961 LOG (GNUNET_ERROR_TYPE_DEBUG,
962 "Tokenizer gives server message of type %u from client\n",
963 ntohs (message->type));
965 sender->in_process_client_buffer = GNUNET_YES;
966 ret = GNUNET_SERVER_inject (server, sender, message);
967 sender->in_process_client_buffer = GNUNET_NO;
968 if (GNUNET_OK != ret)
969 GNUNET_SERVER_client_disconnect (sender);
974 * Add a TCP socket-based connection to the set of handles managed by
975 * this server. Use this function for outgoing (P2P) connections that
976 * we initiated (and where this server should process incoming
979 * @param server the server to use
980 * @param connection the connection to manage (client must
981 * stop using this connection from now on)
982 * @return the client handle (client should call
983 * "client_drop" on the return value eventually)
985 struct GNUNET_SERVER_Client *
986 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
987 struct GNUNET_CONNECTION_Handle *connection)
989 struct GNUNET_SERVER_Client *client;
991 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
992 client->connection = connection;
993 client->reference_count = 1;
994 client->server = server;
995 client->last_activity = GNUNET_TIME_absolute_get ();
996 client->next = server->clients;
997 client->idle_timeout = server->idle_timeout;
998 server->clients = client;
999 client->receive_pending = GNUNET_YES;
1000 client->callback = NULL;
1001 client->callback_cls = NULL;
1003 if (server->mst_create != NULL)
1005 server->mst_create (server->mst_cls, client);
1008 GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server);
1010 GNUNET_CONNECTION_receive (client->connection,
1011 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1012 client->idle_timeout, &process_incoming, client);
1018 * Change the timeout for a particular client. Decreasing the timeout
1019 * may not go into effect immediately (only after the previous timeout
1020 * times out or activity happens on the socket).
1022 * @param client the client to update
1023 * @param timeout new timeout for activities on the socket
1026 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
1027 struct GNUNET_TIME_Relative timeout)
1029 client->idle_timeout = timeout;
1034 GNUNET_SERVER_client_set_finish_pending_write (struct GNUNET_SERVER_Client *client,
1037 client->finish_pending_write = finish;
1042 * Notify the server that the given client handle should
1043 * be kept (keeps the connection up if possible, increments
1044 * the internal reference counter).
1046 * @param client the client to keep
1049 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1051 client->reference_count++;
1056 * Notify the server that the given client handle is no
1057 * longer required. Decrements the reference counter. If
1058 * that counter reaches zero an inactive connection maybe
1061 * @param client the client to drop
1064 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1066 GNUNET_assert (client->reference_count > 0);
1067 client->reference_count--;
1068 if ((client->shutdown_now == GNUNET_YES) && (client->reference_count == 0))
1069 GNUNET_SERVER_client_disconnect (client);
1074 * Obtain the network address of the other party.
1076 * @param client the client to get the address for
1077 * @param addr where to store the address
1078 * @param addrlen where to store the length of the address
1079 * @return GNUNET_OK on success
1082 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1083 void **addr, size_t * addrlen)
1085 return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen);
1090 * Ask the server to notify us whenever a client disconnects.
1091 * This function is called whenever the actual network connection
1092 * is closed; the reference count may be zero or larger than zero
1095 * @param server the server manageing the clients
1096 * @param callback function to call on disconnect
1097 * @param callback_cls closure for callback
1100 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1101 GNUNET_SERVER_DisconnectCallback callback,
1104 struct NotifyList *n;
1106 n = GNUNET_malloc (sizeof (struct NotifyList));
1107 n->callback = callback;
1108 n->callback_cls = callback_cls;
1109 n->next = server->disconnect_notify_list;
1110 server->disconnect_notify_list = n;
1115 * Ask the server to stop notifying us whenever a client disconnects.
1117 * @param server the server manageing the clients
1118 * @param callback function to call on disconnect
1119 * @param callback_cls closure for callback
1122 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1123 GNUNET_SERVER_DisconnectCallback
1124 callback, void *callback_cls)
1126 struct NotifyList *pos;
1127 struct NotifyList *prev;
1130 pos = server->disconnect_notify_list;
1133 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1144 server->disconnect_notify_list = pos->next;
1146 prev->next = pos->next;
1152 * Ask the server to disconnect from the given client.
1153 * This is the same as returning GNUNET_SYSERR from a message
1154 * handler, except that it allows dropping of a client even
1155 * when not handling a message from that client.
1157 * @param client the client to disconnect from
1160 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1162 struct GNUNET_SERVER_Client *prev;
1163 struct GNUNET_SERVER_Client *pos;
1164 struct GNUNET_SERVER_Handle *server;
1165 struct NotifyList *n;
1169 LOG (GNUNET_ERROR_TYPE_DEBUG,
1170 "Client is being disconnected from the server.\n");
1172 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1174 GNUNET_SCHEDULER_cancel (client->restart_task);
1175 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1177 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1179 GNUNET_SCHEDULER_cancel (client->warn_task);
1180 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1182 if (GNUNET_YES == client->receive_pending)
1184 GNUNET_CONNECTION_receive_cancel (client->connection);
1185 client->receive_pending = GNUNET_NO;
1188 rc = client->reference_count;
1189 if (client->shutdown_now != GNUNET_YES)
1191 server = client->server;
1192 client->shutdown_now = GNUNET_YES;
1194 pos = server->clients;
1195 while ((pos != NULL) && (pos != client))
1200 GNUNET_assert (pos != NULL);
1202 server->clients = pos->next;
1204 prev->next = pos->next;
1205 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1207 GNUNET_SCHEDULER_cancel (client->restart_task);
1208 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1210 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1212 GNUNET_SCHEDULER_cancel (client->warn_task);
1213 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1215 n = server->disconnect_notify_list;
1218 n->callback (n->callback_cls, client);
1225 LOG (GNUNET_ERROR_TYPE_DEBUG,
1226 "RC still positive, not destroying everything.\n");
1230 if (client->in_process_client_buffer == GNUNET_YES)
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1234 "Still processing inputs, not destroying everything.\n");
1239 if (client->persist == GNUNET_YES)
1240 GNUNET_CONNECTION_persist_ (client->connection);
1241 GNUNET_CONNECTION_destroy (client->connection, client->finish_pending_write);
1243 if (client->server->mst_destroy != NULL)
1244 client->server->mst_destroy (client->server->mst_cls, client->mst);
1246 GNUNET_SERVER_mst_destroy (client->mst);
1248 GNUNET_free (client);
1253 * Disable the "CORK" feature for communication with the given client,
1254 * forcing the OS to immediately flush the buffer on transmission
1255 * instead of potentially buffering multiple messages.
1257 * @param client handle to the client
1258 * @return GNUNET_OK on success
1261 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client)
1263 return GNUNET_CONNECTION_disable_corking (client->connection);
1268 * Wrapper for transmission notification that calls the original
1269 * callback and update the last activity time for our connection.
1271 * @param cls the 'struct GNUNET_SERVER_Client'
1272 * @param size number of bytes we can transmit
1273 * @param buf where to copy the message
1274 * @return number of bytes actually transmitted
1277 transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
1279 struct GNUNET_SERVER_Client *client = cls;
1282 ret = client->callback (client->callback_cls, size, buf);
1284 client->last_activity = GNUNET_TIME_absolute_get ();
1290 * Notify us when the server has enough space to transmit
1291 * a message of the given size to the given client.
1293 * @param client client to transmit message to
1294 * @param size requested amount of buffer space
1295 * @param timeout after how long should we give up (and call
1296 * notify with buf NULL and size 0)?
1297 * @param callback function to call when space is available
1298 * @param callback_cls closure for callback
1299 * @return non-NULL if the notify callback was queued; can be used
1300 * to cancel the request using
1301 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1302 * NULL if we are already going to notify someone else (busy)
1304 struct GNUNET_CONNECTION_TransmitHandle *
1305 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1307 struct GNUNET_TIME_Relative timeout,
1308 GNUNET_CONNECTION_TransmitReadyNotify
1309 callback, void *callback_cls)
1311 client->callback_cls = callback_cls;
1312 client->callback = callback;
1313 return GNUNET_CONNECTION_notify_transmit_ready (client->connection, size,
1315 &transmit_ready_callback_wrapper,
1321 * Set the persistent flag on this client, used to setup client connection
1322 * to only be killed when the service it's connected to is actually dead.
1324 * @param client the client to set the persistent flag on
1327 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1329 client->persist = GNUNET_YES;
1334 * Resume receiving from this client, we are done processing the
1335 * current request. This function must be called from within each
1336 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1338 * @param client client we were processing a message of
1339 * @param success GNUNET_OK to keep the connection open and
1340 * continue to receive
1341 * GNUNET_NO to close the connection (normal behavior)
1342 * GNUNET_SYSERR to close the connection (signal
1346 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1350 GNUNET_assert (client->suspended > 0);
1351 client->suspended--;
1352 if (success != GNUNET_OK)
1355 LOG (GNUNET_ERROR_TYPE_DEBUG,
1356 "GNUNET_SERVER_receive_done called with failure indication\n");
1358 GNUNET_SERVER_client_disconnect (client);
1361 if (client->suspended > 0)
1364 LOG (GNUNET_ERROR_TYPE_DEBUG,
1365 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1369 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1371 GNUNET_SCHEDULER_cancel (client->warn_task);
1372 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1374 if (client->in_process_client_buffer == GNUNET_YES)
1377 LOG (GNUNET_ERROR_TYPE_DEBUG,
1378 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1382 if ((client->server == NULL) || (GNUNET_YES == client->shutdown_now))
1384 GNUNET_SERVER_client_disconnect (client);
1388 LOG (GNUNET_ERROR_TYPE_DEBUG,
1389 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1391 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1392 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing, client);
1397 * Configure this server's connections to continue handling client
1398 * requests as usual even after we get a shutdown signal. The change
1399 * only applies to clients that connect to the server from the outside
1400 * using TCP after this call. Clients managed previously or those
1401 * added using GNUNET_SERVER_connect_socket and
1402 * GNUNET_SERVER_connect_callback are not affected by this option.
1404 * @param h server handle
1405 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1408 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore)
1410 h->clients_ignore_shutdown = do_ignore;
1413 /* end of server.c */