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)
44 * List of arrays of message handlers.
49 * This is a linked list.
51 struct HandlerList *next;
54 * NULL-terminated array of handlers.
56 const struct GNUNET_SERVER_MessageHandler *handlers;
61 * List of arrays of message handlers.
66 * This is a linked list.
68 struct NotifyList *next;
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 * List of our current clients.
95 struct GNUNET_SERVER_Client *clients;
98 * Linked list of functions to call on disconnects by clients.
100 struct NotifyList *disconnect_notify_list;
103 * Function to call for access control.
105 GNUNET_CONNECTION_AccessCheck access;
108 * Closure for access.
113 * NULL-terminated array of sockets used to listen for new
116 struct GNUNET_NETWORK_Handle **listen_sockets;
119 * After how long should an idle connection time
122 struct GNUNET_TIME_Relative idle_timeout;
125 * Task scheduled to do the listening.
127 GNUNET_SCHEDULER_TaskIdentifier listen_task;
130 * Do we ignore messages of types that we do not understand or do we
131 * require that a handler is found (and if not kill the connection)?
136 * Should all of the clients of this server continue to process
137 * connections as usual even if we get a shutdown request? (the
138 * listen socket always ignores shutdown).
140 int clients_ignore_shutdown;
142 GNUNET_SERVER_MstCreateCallback mst_create;
143 GNUNET_SERVER_MstDestroyCallback mst_destroy;
144 GNUNET_SERVER_MstReceiveCallback mst_receive;
150 * @brief handle for a client of the server
152 struct GNUNET_SERVER_Client
156 * This is a linked list.
158 struct GNUNET_SERVER_Client *next;
161 * Processing of incoming data.
166 * Server that this client belongs to.
168 struct GNUNET_SERVER_Handle *server;
171 * Client closure for callbacks.
173 struct GNUNET_CONNECTION_Handle *connection;
176 * ID of task used to restart processing.
178 GNUNET_SCHEDULER_TaskIdentifier restart_task;
181 * Task that warns about missing calls to 'GNUNET_SERVER_receive_done'.
183 GNUNET_SCHEDULER_TaskIdentifier warn_task;
186 * Time when the warn task was started.
188 struct GNUNET_TIME_Absolute warn_start;
191 * Last activity on this socket (used to time it out
192 * if reference_count == 0).
194 struct GNUNET_TIME_Absolute last_activity;
199 GNUNET_CONNECTION_TransmitReadyNotify callback;
207 * After how long should an idle connection time
210 struct GNUNET_TIME_Relative idle_timeout;
213 * Number of external entities with a reference to
214 * this client object.
216 unsigned int reference_count;
219 * Was processing if incoming messages suspended while
220 * we were still processing data already received?
221 * This is a counter saying how often processing was
222 * suspended (once per handler invoked).
224 unsigned int suspended;
227 * Are we currently in the "process_client_buffer" function (and
228 * will hence restart the receive job on exit if suspended == 0 once
229 * we are done?). If this is set, then "receive_done" will
230 * essentially only decrement suspended; if this is not set, then
231 * "receive_done" may need to restart the receive process (either
232 * from the side-buffer or via select/recv).
234 int in_process_client_buffer;
237 * We're about to close down this client due to some serious
243 * Are we currently trying to receive? (YES if we are, NO if we are not,
244 * SYSERR if data is already available in MST).
249 * Finish pending write when disconnecting?
251 int finish_pending_write;
254 * Persist the file handle for this client no matter what happens,
255 * force the OS to close once the process actually dies. Should only
256 * be used in special cases!
261 * Type of last message processed (for warn_no_receive_done).
268 * Scheduler says our listen socket is ready. Process it!
270 * @param cls handle to our server for which we are processing the listen
272 * @param tc reason why we are running right now
275 process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
277 struct GNUNET_SERVER_Handle *server = cls;
278 struct GNUNET_CONNECTION_Handle *sock;
279 struct GNUNET_SERVER_Client *client;
280 struct GNUNET_NETWORK_FDSet *r;
283 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
284 r = GNUNET_NETWORK_fdset_create ();
286 while (NULL != server->listen_sockets[i])
287 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
288 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
290 /* ignore shutdown, someone else will take care of it! */
291 server->listen_task =
292 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
293 GNUNET_SCHEDULER_NO_TASK,
294 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
295 &process_listen_socket, server);
296 GNUNET_NETWORK_fdset_destroy (r);
300 while (NULL != server->listen_sockets[i])
302 if (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_sockets[i]))
305 GNUNET_CONNECTION_create_from_accept (server->access,
307 server->listen_sockets[i]);
310 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server accepted incoming connection.\n");
311 client = GNUNET_SERVER_connect_socket (server, sock);
312 GNUNET_CONNECTION_ignore_shutdown (sock,
313 server->clients_ignore_shutdown);
314 /* decrement reference count, we don't keep "client" alive */
315 GNUNET_SERVER_client_drop (client);
320 /* listen for more! */
321 server->listen_task =
322 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
323 GNUNET_SCHEDULER_NO_TASK,
324 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
325 &process_listen_socket, server);
326 GNUNET_NETWORK_fdset_destroy (r);
331 * Create and initialize a listen socket for the server.
333 * @param serverAddr address to listen on
334 * @param socklen length of address
335 * @return NULL on error, otherwise the listen socket
337 static struct GNUNET_NETWORK_Handle *
338 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
340 const static int on = 1;
341 struct GNUNET_NETWORK_Handle *sock;
345 switch (serverAddr->sa_family)
348 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
351 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
361 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
364 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
370 if (GNUNET_NETWORK_socket_setsockopt
371 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
372 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
375 if ((serverAddr->sa_family == AF_INET6) &&
376 (GNUNET_NETWORK_socket_setsockopt
377 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
378 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
382 /* bind the socket */
383 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
386 if (errno != EADDRINUSE)
388 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
389 * fail if we already took the port on IPv6; if both IPv4 and
390 * IPv6 binds fail, then our caller will log using the
391 * errno preserved in 'eno' */
392 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
394 LOG (GNUNET_ERROR_TYPE_ERROR, _("`%s' failed for port %d (%s).\n"),
396 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
402 LOG (GNUNET_ERROR_TYPE_WARNING,
403 _("`%s' failed for port %d (%s): address already in use\n"),
405 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
406 else if (serverAddr->sa_family == AF_UNIX)
407 LOG (GNUNET_ERROR_TYPE_WARNING,
408 _("`%s' failed for `%s': address already in use\n"), "bind",
409 ((const struct sockaddr_un *) serverAddr)->sun_path);
412 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
416 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
418 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen");
419 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
424 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server starts to listen on port %u.\n",
431 * Create a new server.
433 * @param access function for access control
434 * @param access_cls closure for access
435 * @param lsocks NULL-terminated array of listen sockets
436 * @param idle_timeout after how long should we timeout idle connections?
437 * @param require_found if YES, connections sending messages of unknown type
439 * @return handle for the new server, NULL on error
440 * (typically, "port" already in use)
442 struct GNUNET_SERVER_Handle *
443 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
445 struct GNUNET_NETWORK_Handle **lsocks,
446 struct GNUNET_TIME_Relative idle_timeout,
449 struct GNUNET_SERVER_Handle *ret;
450 struct GNUNET_NETWORK_FDSet *r;
453 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
454 ret->idle_timeout = idle_timeout;
455 ret->listen_sockets = lsocks;
456 ret->access = access;
457 ret->access_cls = access_cls;
458 ret->require_found = require_found;
461 r = GNUNET_NETWORK_fdset_create ();
463 while (NULL != ret->listen_sockets[i])
464 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]);
466 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
467 GNUNET_SCHEDULER_NO_TASK,
468 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
469 &process_listen_socket, ret);
470 GNUNET_NETWORK_fdset_destroy (r);
477 * Create a new server.
479 * @param access function for access control
480 * @param access_cls closure for access
481 * @param serverAddr address to listen on (including port), NULL terminated array
482 * @param socklen length of serverAddr
483 * @param idle_timeout after how long should we timeout idle connections?
484 * @param require_found if YES, connections sending messages of unknown type
486 * @return handle for the new server, NULL on error
487 * (typically, "port" already in use)
489 struct GNUNET_SERVER_Handle *
490 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
491 struct sockaddr *const *serverAddr,
492 const socklen_t * socklen,
493 struct GNUNET_TIME_Relative idle_timeout,
496 struct GNUNET_NETWORK_Handle **lsocks;
501 while (serverAddr[i] != NULL)
505 lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
508 while (serverAddr[i] != NULL)
510 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
511 if (lsocks[j] != NULL)
518 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
519 GNUNET_free (lsocks);
527 return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks,
528 idle_timeout, require_found);
533 * Free resources held by this server.
535 * @param s server to destroy
538 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
540 struct HandlerList *hpos;
541 struct NotifyList *npos;
544 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
545 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
547 GNUNET_SCHEDULER_cancel (s->listen_task);
548 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
550 if (s->listen_sockets != NULL)
553 while (s->listen_sockets[i] != NULL)
554 GNUNET_break (GNUNET_OK ==
555 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
556 GNUNET_free (s->listen_sockets);
557 s->listen_sockets = NULL;
559 while (s->clients != NULL)
560 GNUNET_SERVER_client_disconnect (s->clients);
561 while (NULL != (hpos = s->handlers))
563 s->handlers = hpos->next;
566 while (NULL != (npos = s->disconnect_notify_list))
568 npos->callback (npos->callback_cls, NULL);
569 s->disconnect_notify_list = npos->next;
577 * Add additional handlers to an existing server.
579 * @param server the server to add handlers to
580 * @param handlers array of message handlers for
581 * incoming messages; the last entry must
582 * have "NULL" for the "callback"; multiple
583 * entries for the same type are allowed,
584 * they will be called in order of occurence.
585 * These handlers can be removed later;
586 * the handlers array must exist until removed
587 * (or server is destroyed).
590 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
591 const struct GNUNET_SERVER_MessageHandler *handlers)
593 struct HandlerList *p;
595 p = GNUNET_malloc (sizeof (struct HandlerList));
596 p->handlers = handlers;
597 p->next = server->handlers;
598 server->handlers = p;
603 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
604 GNUNET_SERVER_MstCreateCallback create,
605 GNUNET_SERVER_MstDestroyCallback destroy,
606 GNUNET_SERVER_MstReceiveCallback receive,
609 server->mst_create = create;
610 server->mst_destroy = destroy;
611 server->mst_receive = receive;
612 server->mst_cls = cls;
617 * Task run to warn about missing calls to 'GNUNET_SERVER_receive_done'.
619 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
620 * @param tc scheduler context (unused)
623 warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
625 struct GNUNET_SERVER_Client *client = cls;
628 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
629 &warn_no_receive_done, client);
630 if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
631 LOG (GNUNET_ERROR_TYPE_WARNING,
633 ("Processing code for message of type %u did not call GNUNET_SERVER_receive_done after %llums\n"),
634 (unsigned int) client->warn_type,
636 GNUNET_TIME_absolute_get_duration (client->warn_start).rel_value);
641 * Disable the warning the server issues if a message is not acknowledged
642 * in a timely fashion. Use this call if a client is intentionally delayed
643 * for a while. Only applies to the current message.
645 * @param client client for which to disable the warning
648 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
650 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
652 GNUNET_SCHEDULER_cancel (client->warn_task);
653 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
659 * Inject a message into the server, pretend it came
660 * from the specified client. Delivery of the message
661 * will happen instantly (if a handler is installed;
662 * otherwise the call does nothing).
664 * @param server the server receiving the message
665 * @param sender the "pretended" sender of the message
667 * @param message message to transmit
668 * @return GNUNET_OK if the message was OK and the
669 * connection can stay open
670 * GNUNET_SYSERR if the connection to the
671 * client should be shut down
674 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
675 struct GNUNET_SERVER_Client *sender,
676 const struct GNUNET_MessageHeader *message)
678 struct HandlerList *pos;
679 const struct GNUNET_SERVER_MessageHandler *mh;
685 type = ntohs (message->type);
686 size = ntohs (message->size);
687 LOG (GNUNET_ERROR_TYPE_DEBUG,
688 "Server schedules transmission of %u-byte message of type %u to client.\n",
690 pos = server->handlers;
695 while (pos->handlers[i].callback != NULL)
697 mh = &pos->handlers[i];
698 if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
700 if ((mh->expected_size != 0) && (mh->expected_size != size))
702 #if GNUNET8_NETWORK_IS_DEAD
703 LOG (GNUNET_ERROR_TYPE_WARNING,
704 "Expected %u bytes for message of type %u, got %u\n",
705 mh->expected_size, mh->type, size);
708 return GNUNET_SYSERR;
712 if (0 == sender->suspended)
714 sender->warn_start = GNUNET_TIME_absolute_get ();
716 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
717 &warn_no_receive_done, sender);
718 sender->warn_type = type;
722 mh->callback (mh->callback_cls, sender, message);
729 if (found == GNUNET_NO)
731 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
732 "Received message of unknown type %d\n", type);
733 if (server->require_found == GNUNET_YES)
734 return GNUNET_SYSERR;
741 * We are receiving an incoming message. Process it.
743 * @param cls our closure (handle for the client)
744 * @param buf buffer with data received from network
745 * @param available number of bytes available in buf
746 * @param addr address of the sender
747 * @param addrlen length of addr
748 * @param errCode code indicating errors receiving, 0 for success
751 process_incoming (void *cls, const void *buf, size_t available,
752 const struct sockaddr *addr, socklen_t addrlen, int errCode);
756 * Process messages from the client's message tokenizer until either
757 * the tokenizer is empty (and then schedule receiving more), or
758 * until some handler is not immediately done (then wait for restart_processing)
761 * @param client the client to process, RC must have already been increased
762 * using GNUNET_SERVER_client_keep and will be decreased by one in this
764 * @param ret GNUNET_NO to start processing from the buffer,
765 * GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
766 * GNUNET_SYSERR if we should instantly abort due to error in a previous step
769 process_mst (struct GNUNET_SERVER_Client *client, int ret)
771 while ((ret != GNUNET_SYSERR) && (client->server != NULL) &&
772 (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
774 if (ret == GNUNET_OK)
776 client->receive_pending = GNUNET_YES;
777 LOG (GNUNET_ERROR_TYPE_DEBUG,
778 "Server re-enters receive loop, timeout: %llu.\n",
779 client->idle_timeout.rel_value);
780 GNUNET_CONNECTION_receive (client->connection,
781 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
782 client->idle_timeout, &process_incoming,
786 LOG (GNUNET_ERROR_TYPE_DEBUG,
787 "Server processes additional messages instantly.\n");
788 if (client->server->mst_receive != NULL)
790 client->server->mst_receive (client->server->mst_cls, client->mst,
791 client, NULL, 0, GNUNET_NO, GNUNET_YES);
794 GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
797 LOG (GNUNET_ERROR_TYPE_DEBUG,
798 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
799 ret, client->server, client->shutdown_now, client->suspended);
800 if (ret == GNUNET_NO)
802 LOG (GNUNET_ERROR_TYPE_DEBUG,
803 "Server has more data pending but is suspended.\n");
804 client->receive_pending = GNUNET_SYSERR; /* data pending */
806 if ((ret == GNUNET_SYSERR) || (GNUNET_YES == client->shutdown_now))
807 GNUNET_SERVER_client_disconnect (client);
808 GNUNET_SERVER_client_drop (client);
813 * We are receiving an incoming message. Process it.
815 * @param cls our closure (handle for the client)
816 * @param buf buffer with data received from network
817 * @param available number of bytes available in buf
818 * @param addr address of the sender
819 * @param addrlen length of addr
820 * @param errCode code indicating errors receiving, 0 for success
823 process_incoming (void *cls, const void *buf, size_t available,
824 const struct sockaddr *addr, socklen_t addrlen, int errCode)
826 struct GNUNET_SERVER_Client *client = cls;
827 struct GNUNET_SERVER_Handle *server = client->server;
828 struct GNUNET_TIME_Absolute end;
829 struct GNUNET_TIME_Absolute now;
832 GNUNET_assert (client->receive_pending == GNUNET_YES);
833 client->receive_pending = GNUNET_NO;
834 now = GNUNET_TIME_absolute_get ();
835 end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout);
837 if ((buf == NULL) && (available == 0) && (addr == NULL) && (errCode == 0) &&
838 (client->shutdown_now != GNUNET_YES) && (server != NULL) &&
839 (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
840 (end.abs_value > now.abs_value))
842 /* wait longer, timeout changed (i.e. due to us sending) */
843 LOG (GNUNET_ERROR_TYPE_DEBUG,
844 "Receive time out, but no disconnect due to sending (%p)\n",
845 GNUNET_a2s (addr, addrlen));
846 client->receive_pending = GNUNET_YES;
847 GNUNET_CONNECTION_receive (client->connection,
848 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
849 GNUNET_TIME_absolute_get_remaining (end),
850 &process_incoming, client);
853 if ((buf == NULL) || (available == 0) || (errCode != 0) || (server == NULL) ||
854 (client->shutdown_now == GNUNET_YES) ||
855 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
857 /* other side closed connection, error connecting, etc. */
858 GNUNET_SERVER_client_disconnect (client);
861 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n",
862 (unsigned int) available, GNUNET_a2s (addr, addrlen));
863 GNUNET_SERVER_client_keep (client);
864 client->last_activity = now;
866 if (server->mst_receive != NULL)
868 client->server->mst_receive (client->server->mst_cls, client->mst,
869 client, buf, available, GNUNET_NO, GNUNET_YES);
872 GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO,
875 process_mst (client, ret);
880 * Task run to start again receiving from the network
881 * and process requests.
883 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
884 * @param tc scheduler context (unused)
887 restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
889 struct GNUNET_SERVER_Client *client = cls;
890 struct GNUNET_SERVER_Handle *server = client->server;
892 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
893 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
894 (GNUNET_NO == server->clients_ignore_shutdown))
896 GNUNET_SERVER_client_disconnect (client);
899 if (client->receive_pending == GNUNET_NO)
901 LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n");
902 client->receive_pending = GNUNET_YES;
903 GNUNET_CONNECTION_receive (client->connection,
904 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
905 client->idle_timeout, &process_incoming, client);
908 LOG (GNUNET_ERROR_TYPE_DEBUG,
909 "Server continues processing messages still in the buffer.\n");
910 GNUNET_SERVER_client_keep (client);
911 client->receive_pending = GNUNET_NO;
912 process_mst (client, GNUNET_NO);
917 * This function is called whenever our inbound message tokenizer has
918 * received a complete message.
920 * @param cls closure (struct GNUNET_SERVER_Handle)
921 * @param client identification of the client (struct GNUNET_SERVER_Client*)
922 * @param message the actual message
925 client_message_tokenizer_callback (void *cls, void *client,
926 const struct GNUNET_MessageHeader *message)
928 struct GNUNET_SERVER_Handle *server = cls;
929 struct GNUNET_SERVER_Client *sender = client;
932 LOG (GNUNET_ERROR_TYPE_DEBUG,
933 "Tokenizer gives server message of type %u from client\n",
934 ntohs (message->type));
935 sender->in_process_client_buffer = GNUNET_YES;
936 ret = GNUNET_SERVER_inject (server, sender, message);
937 sender->in_process_client_buffer = GNUNET_NO;
938 if (GNUNET_OK != ret)
939 GNUNET_SERVER_client_disconnect (sender);
944 * Add a TCP socket-based connection to the set of handles managed by
945 * this server. Use this function for outgoing (P2P) connections that
946 * we initiated (and where this server should process incoming
949 * @param server the server to use
950 * @param connection the connection to manage (client must
951 * stop using this connection from now on)
952 * @return the client handle (client should call
953 * "client_drop" on the return value eventually)
955 struct GNUNET_SERVER_Client *
956 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
957 struct GNUNET_CONNECTION_Handle *connection)
959 struct GNUNET_SERVER_Client *client;
961 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
962 client->connection = connection;
963 client->reference_count = 1;
964 client->server = server;
965 client->last_activity = GNUNET_TIME_absolute_get ();
966 client->next = server->clients;
967 client->idle_timeout = server->idle_timeout;
968 server->clients = client;
969 client->receive_pending = GNUNET_YES;
970 client->callback = NULL;
971 client->callback_cls = NULL;
973 if (server->mst_create != NULL)
975 server->mst_create (server->mst_cls, client);
978 GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server);
980 GNUNET_CONNECTION_receive (client->connection,
981 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
982 client->idle_timeout, &process_incoming, client);
988 * Change the timeout for a particular client. Decreasing the timeout
989 * may not go into effect immediately (only after the previous timeout
990 * times out or activity happens on the socket).
992 * @param client the client to update
993 * @param timeout new timeout for activities on the socket
996 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
997 struct GNUNET_TIME_Relative timeout)
999 client->idle_timeout = timeout;
1004 * Notify the server that the given client handle should
1005 * be kept (keeps the connection up if possible, increments
1006 * the internal reference counter).
1008 * @param client the client to keep
1011 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1013 client->reference_count++;
1018 * Notify the server that the given client handle is no
1019 * longer required. Decrements the reference counter. If
1020 * that counter reaches zero an inactive connection maybe
1023 * @param client the client to drop
1026 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1028 GNUNET_assert (client->reference_count > 0);
1029 client->reference_count--;
1030 if ((client->shutdown_now == GNUNET_YES) && (client->reference_count == 0))
1031 GNUNET_SERVER_client_disconnect (client);
1036 * Obtain the network address of the other party.
1038 * @param client the client to get the address for
1039 * @param addr where to store the address
1040 * @param addrlen where to store the length of the address
1041 * @return GNUNET_OK on success
1044 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1045 void **addr, size_t * addrlen)
1047 return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen);
1052 * Ask the server to notify us whenever a client disconnects.
1053 * This function is called whenever the actual network connection
1054 * is closed; the reference count may be zero or larger than zero
1057 * @param server the server manageing the clients
1058 * @param callback function to call on disconnect
1059 * @param callback_cls closure for callback
1062 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1063 GNUNET_SERVER_DisconnectCallback callback,
1066 struct NotifyList *n;
1068 n = GNUNET_malloc (sizeof (struct NotifyList));
1069 n->callback = callback;
1070 n->callback_cls = callback_cls;
1071 n->next = server->disconnect_notify_list;
1072 server->disconnect_notify_list = n;
1077 * Ask the server to stop notifying us whenever a client disconnects.
1079 * @param server the server manageing the clients
1080 * @param callback function to call on disconnect
1081 * @param callback_cls closure for callback
1084 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1085 GNUNET_SERVER_DisconnectCallback
1086 callback, void *callback_cls)
1088 struct NotifyList *pos;
1089 struct NotifyList *prev;
1092 pos = server->disconnect_notify_list;
1095 if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1106 server->disconnect_notify_list = pos->next;
1108 prev->next = pos->next;
1114 * Ask the server to disconnect from the given client.
1115 * This is the same as returning GNUNET_SYSERR from a message
1116 * handler, except that it allows dropping of a client even
1117 * when not handling a message from that client.
1119 * @param client the client to disconnect from
1122 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1124 struct GNUNET_SERVER_Client *prev;
1125 struct GNUNET_SERVER_Client *pos;
1126 struct GNUNET_SERVER_Handle *server;
1127 struct NotifyList *n;
1130 LOG (GNUNET_ERROR_TYPE_DEBUG,
1131 "Client is being disconnected from the server.\n");
1132 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1134 GNUNET_SCHEDULER_cancel (client->restart_task);
1135 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1137 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1139 GNUNET_SCHEDULER_cancel (client->warn_task);
1140 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1142 if (GNUNET_YES == client->receive_pending)
1144 GNUNET_CONNECTION_receive_cancel (client->connection);
1145 client->receive_pending = GNUNET_NO;
1148 rc = client->reference_count;
1149 if (client->shutdown_now != GNUNET_YES)
1151 server = client->server;
1152 client->shutdown_now = GNUNET_YES;
1154 pos = server->clients;
1155 while ((pos != NULL) && (pos != client))
1160 GNUNET_assert (pos != NULL);
1162 server->clients = pos->next;
1164 prev->next = pos->next;
1165 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1167 GNUNET_SCHEDULER_cancel (client->restart_task);
1168 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1170 if (client->warn_task != GNUNET_SCHEDULER_NO_TASK)
1172 GNUNET_SCHEDULER_cancel (client->warn_task);
1173 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1175 n = server->disconnect_notify_list;
1178 n->callback (n->callback_cls, client);
1184 LOG (GNUNET_ERROR_TYPE_DEBUG,
1185 "RC still positive, not destroying everything.\n");
1188 if (client->in_process_client_buffer == GNUNET_YES)
1190 LOG (GNUNET_ERROR_TYPE_DEBUG,
1191 "Still processing inputs, not destroying everything.\n");
1195 if (client->persist == GNUNET_YES)
1196 GNUNET_CONNECTION_persist_ (client->connection);
1197 GNUNET_CONNECTION_destroy (client->connection, GNUNET_NO);
1199 if (client->server->mst_destroy != NULL)
1200 client->server->mst_destroy (client->server->mst_cls, client->mst);
1202 GNUNET_SERVER_mst_destroy (client->mst);
1204 GNUNET_free (client);
1209 * Disable the "CORK" feature for communication with the given client,
1210 * forcing the OS to immediately flush the buffer on transmission
1211 * instead of potentially buffering multiple messages.
1213 * @param client handle to the client
1214 * @return GNUNET_OK on success
1217 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client)
1219 return GNUNET_CONNECTION_disable_corking (client->connection);
1224 * Wrapper for transmission notification that calls the original
1225 * callback and update the last activity time for our connection.
1227 * @param cls the 'struct GNUNET_SERVER_Client'
1228 * @param size number of bytes we can transmit
1229 * @param buf where to copy the message
1230 * @return number of bytes actually transmitted
1233 transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
1235 struct GNUNET_SERVER_Client *client = cls;
1238 ret = client->callback (client->callback_cls, size, buf);
1240 client->last_activity = GNUNET_TIME_absolute_get ();
1246 * Notify us when the server has enough space to transmit
1247 * a message of the given size to the given client.
1249 * @param client client to transmit message to
1250 * @param size requested amount of buffer space
1251 * @param timeout after how long should we give up (and call
1252 * notify with buf NULL and size 0)?
1253 * @param callback function to call when space is available
1254 * @param callback_cls closure for callback
1255 * @return non-NULL if the notify callback was queued; can be used
1256 * to cancel the request using
1257 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1258 * NULL if we are already going to notify someone else (busy)
1260 struct GNUNET_CONNECTION_TransmitHandle *
1261 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1263 struct GNUNET_TIME_Relative timeout,
1264 GNUNET_CONNECTION_TransmitReadyNotify
1265 callback, void *callback_cls)
1267 client->callback_cls = callback_cls;
1268 client->callback = callback;
1269 return GNUNET_CONNECTION_notify_transmit_ready (client->connection, size,
1271 &transmit_ready_callback_wrapper,
1277 * Set the persistent flag on this client, used to setup client connection
1278 * to only be killed when the service it's connected to is actually dead.
1280 * @param client the client to set the persistent flag on
1283 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1285 client->persist = GNUNET_YES;
1290 * Resume receiving from this client, we are done processing the
1291 * current request. This function must be called from within each
1292 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1294 * @param client client we were processing a message of
1295 * @param success GNUNET_OK to keep the connection open and
1296 * continue to receive
1297 * GNUNET_NO to close the connection (normal behavior)
1298 * GNUNET_SYSERR to close the connection (signal
1302 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1306 GNUNET_assert (client->suspended > 0);
1307 client->suspended--;
1308 if (success != GNUNET_OK)
1310 LOG (GNUNET_ERROR_TYPE_DEBUG,
1311 "GNUNET_SERVER_receive_done called with failure indication\n");
1312 GNUNET_SERVER_client_disconnect (client);
1315 if (client->suspended > 0)
1317 LOG (GNUNET_ERROR_TYPE_DEBUG,
1318 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1321 if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1323 GNUNET_SCHEDULER_cancel (client->warn_task);
1324 client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1326 if (client->in_process_client_buffer == GNUNET_YES)
1328 LOG (GNUNET_ERROR_TYPE_DEBUG,
1329 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1332 if ((client->server == NULL) || (GNUNET_YES == client->shutdown_now))
1334 GNUNET_SERVER_client_disconnect (client);
1337 LOG (GNUNET_ERROR_TYPE_DEBUG,
1338 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1339 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1340 client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing, client);
1345 * Configure this server's connections to continue handling client
1346 * requests as usual even after we get a shutdown signal. The change
1347 * only applies to clients that connect to the server from the outside
1348 * using TCP after this call. Clients managed previously or those
1349 * added using GNUNET_SERVER_connect_socket and
1350 * GNUNET_SERVER_connect_callback are not affected by this option.
1352 * @param h server handle
1353 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1356 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore)
1358 h->clients_ignore_shutdown = do_ignore;
1361 /* end of server.c */