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 DEBUG_SERVER GNUNET_YES
39 * List of arrays of message handlers.
44 * This is a linked list.
46 struct HandlerList *next;
49 * NULL-terminated array of handlers.
51 const struct GNUNET_SERVER_MessageHandler *handlers;
56 * List of arrays of message handlers.
61 * This is a linked list.
63 struct NotifyList *next;
68 GNUNET_SERVER_DisconnectCallback callback;
71 * Closure for callback.
78 * @brief handle for a server
80 struct GNUNET_SERVER_Handle
85 struct GNUNET_SCHEDULER_Handle *sched;
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 * maximum write buffer size for accepted sockets
130 * Task scheduled to do the listening.
132 GNUNET_SCHEDULER_TaskIdentifier listen_task;
135 * Do we ignore messages of types that we do not understand or do we
136 * require that a handler is found (and if not kill the connection)?
141 * Should all of the clients of this server continue to process
142 * connections as usual even if we get a shutdown request? (the
143 * listen socket always ignores shutdown).
145 int clients_ignore_shutdown;
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.
164 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
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 * Last activity on this socket (used to time it out
183 * if reference_count == 0).
185 struct GNUNET_TIME_Absolute last_activity;
188 * Number of external entities with a reference to
189 * this client object.
191 unsigned int reference_count;
194 * Was processing if incoming messages suspended while
195 * we were still processing data already received?
196 * This is a counter saying how often processing was
197 * suspended (once per handler invoked).
199 unsigned int suspended;
202 * Are we currently in the "process_client_buffer" function (and
203 * will hence restart the receive job on exit if suspended == 0 once
204 * we are done?). If this is set, then "receive_done" will
205 * essentially only decrement suspended; if this is not set, then
206 * "receive_done" may need to restart the receive process (either
207 * from the side-buffer or via select/recv).
209 int in_process_client_buffer;
212 * We're about to close down this client due to some serious
218 * Are we currently trying to receive? (YES if we are, NO if we are not,
219 * SYSERR if data is already available in MST).
224 * Persist the file handle for this client no matter what happens,
225 * force the OS to close once the process actually dies. Should only
226 * be used in special cases!
233 * Scheduler says our listen socket is ready. Process it!
235 * @param cls handle to our server for which we are processing the listen
237 * @param tc reason why we are running right now
240 process_listen_socket (void *cls,
241 const struct GNUNET_SCHEDULER_TaskContext *tc)
243 struct GNUNET_SERVER_Handle *server = cls;
244 struct GNUNET_CONNECTION_Handle *sock;
245 struct GNUNET_SERVER_Client *client;
246 struct GNUNET_NETWORK_FDSet *r;
249 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
250 r = GNUNET_NETWORK_fdset_create ();
252 while (NULL != server->listen_sockets[i])
253 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
254 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
256 /* ignore shutdown, someone else will take care of it! */
257 server->listen_task = GNUNET_SCHEDULER_add_select (server->sched,
258 GNUNET_SCHEDULER_PRIORITY_HIGH,
259 GNUNET_SCHEDULER_NO_TASK,
260 GNUNET_TIME_UNIT_FOREVER_REL,
262 &process_listen_socket,
264 GNUNET_NETWORK_fdset_destroy (r);
268 while (NULL != server->listen_sockets[i])
270 if (GNUNET_NETWORK_fdset_isset
271 (tc->read_ready, server->listen_sockets[i]))
274 GNUNET_CONNECTION_create_from_accept (tc->sched, server->access,
276 server->listen_sockets[i],
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282 "Server accepted incoming connection.\n");
284 client = GNUNET_SERVER_connect_socket (server, sock);
285 GNUNET_CONNECTION_ignore_shutdown (sock,
286 server->clients_ignore_shutdown);
287 /* decrement reference count, we don't keep "client" alive */
288 GNUNET_SERVER_client_drop (client);
293 /* listen for more! */
294 server->listen_task = GNUNET_SCHEDULER_add_select (server->sched,
295 GNUNET_SCHEDULER_PRIORITY_HIGH,
296 GNUNET_SCHEDULER_NO_TASK,
297 GNUNET_TIME_UNIT_FOREVER_REL,
299 &process_listen_socket,
301 GNUNET_NETWORK_fdset_destroy (r);
306 * Create and initialize a listen socket for the server.
308 * @param serverAddr address to listen on
309 * @param socklen length of address
310 * @return NULL on error, otherwise the listen socket
312 static struct GNUNET_NETWORK_Handle *
313 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
315 const static int on = 1;
316 struct GNUNET_NETWORK_Handle *sock;
320 switch (serverAddr->sa_family)
323 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
326 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
332 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
335 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
341 if (GNUNET_NETWORK_socket_setsockopt
342 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
343 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
346 if ( (serverAddr->sa_family == AF_INET6) &&
347 (GNUNET_NETWORK_socket_setsockopt
348 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK) )
349 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
353 /* bind the socket */
354 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
357 if (errno != EADDRINUSE)
359 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
360 fail if we already took the port on IPv6; if both IPv4 and
361 IPv6 binds fail, then our caller will log using the
362 errno preserved in 'eno' */
363 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 ("`%s' failed for port %d (%s).\n"),
369 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
372 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
376 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
378 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
379 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "Server starts to listen on port %u.\n", port);
393 * Create a new server.
395 * @param sched scheduler to use
396 * @param access function for access control
397 * @param access_cls closure for access
398 * @param serverAddr address to listen on (including port), NULL terminated array
399 * @param socklen length of serverAddr
400 * @param maxbuf maximum write buffer size for accepted sockets
401 * @param idle_timeout after how long should we timeout idle connections?
402 * @param require_found if YES, connections sending messages of unknown type
404 * @return handle for the new server, NULL on error
405 * (typically, "port" already in use)
407 struct GNUNET_SERVER_Handle *
408 GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched,
409 GNUNET_CONNECTION_AccessCheck access,
411 struct sockaddr *const *serverAddr,
412 const socklen_t * socklen,
414 struct GNUNET_TIME_Relative
415 idle_timeout, int require_found)
417 struct GNUNET_SERVER_Handle *ret;
418 struct GNUNET_NETWORK_Handle **lsocks;
419 struct GNUNET_NETWORK_FDSet *r;
424 while (serverAddr[i] != NULL)
429 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
432 while (serverAddr[i] != NULL)
434 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
435 if (lsocks[j] != NULL)
442 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
443 GNUNET_free (lsocks);
451 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
453 ret->maxbuf = maxbuf;
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++]);
465 ret->listen_task = GNUNET_SCHEDULER_add_select (sched,
466 GNUNET_SCHEDULER_PRIORITY_HIGH,
467 GNUNET_SCHEDULER_NO_TASK,
468 GNUNET_TIME_UNIT_FOREVER_REL,
470 &process_listen_socket,
472 GNUNET_NETWORK_fdset_destroy (r);
479 * Free resources held by this server.
481 * @param s server to destroy
484 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
486 struct HandlerList *hpos;
487 struct NotifyList *npos;
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
493 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
495 GNUNET_SCHEDULER_cancel (s->sched, s->listen_task);
496 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
498 if (s->listen_sockets != NULL)
501 while (s->listen_sockets[i] != NULL)
502 GNUNET_break (GNUNET_OK ==
503 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
504 GNUNET_free (s->listen_sockets);
505 s->listen_sockets = NULL;
507 while (s->clients != NULL)
508 GNUNET_SERVER_client_disconnect (s->clients);
509 while (NULL != (hpos = s->handlers))
511 s->handlers = hpos->next;
514 while (NULL != (npos = s->disconnect_notify_list))
516 npos->callback (npos->callback_cls, NULL);
517 s->disconnect_notify_list = npos->next;
525 * Add additional handlers to an existing server.
527 * @param server the server to add handlers to
528 * @param handlers array of message handlers for
529 * incoming messages; the last entry must
530 * have "NULL" for the "callback"; multiple
531 * entries for the same type are allowed,
532 * they will be called in order of occurence.
533 * These handlers can be removed later;
534 * the handlers array must exist until removed
535 * (or server is destroyed).
538 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
539 const struct GNUNET_SERVER_MessageHandler
542 struct HandlerList *p;
544 p = GNUNET_malloc (sizeof (struct HandlerList));
545 p->handlers = handlers;
546 p->next = server->handlers;
547 server->handlers = p;
552 * Inject a message into the server, pretend it came
553 * from the specified client. Delivery of the message
554 * will happen instantly (if a handler is installed;
555 * otherwise the call does nothing).
557 * @param server the server receiving the message
558 * @param sender the "pretended" sender of the message
560 * @param message message to transmit
561 * @return GNUNET_OK if the message was OK and the
562 * connection can stay open
563 * GNUNET_SYSERR if the connection to the
564 * client should be shut down
567 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
568 struct GNUNET_SERVER_Client *sender,
569 const struct GNUNET_MessageHeader *message)
571 struct HandlerList *pos;
572 const struct GNUNET_SERVER_MessageHandler *mh;
578 type = ntohs (message->type);
579 size = ntohs (message->size);
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Server schedules transmission of %u-byte message of type %u to client.\n",
585 pos = server->handlers;
590 while (pos->handlers[i].callback != NULL)
592 mh = &pos->handlers[i];
593 if ( (mh->type == type) ||
594 (mh->type == GNUNET_MESSAGE_TYPE_ALL) )
596 if ((mh->expected_size != 0) && (mh->expected_size != size))
599 return GNUNET_SYSERR;
603 mh->callback (mh->callback_cls, sender, message);
610 if (found == GNUNET_NO)
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
613 "Received message of unknown type %d\n",
615 if (server->require_found == GNUNET_YES)
616 return GNUNET_SYSERR;
623 * We are receiving an incoming message. Process it.
625 * @param cls our closure (handle for the client)
626 * @param buf buffer with data received from network
627 * @param available number of bytes available in buf
628 * @param addr address of the sender
629 * @param addrlen length of addr
630 * @param errCode code indicating errors receiving, 0 for success
633 process_incoming (void *cls,
636 const struct sockaddr *addr,
637 socklen_t addrlen, int errCode);
641 * Process messages from the client's message tokenizer until either
642 * the tokenizer is empty (and then schedule receiving more), or
643 * until some handler is not immediately done (then wait for restart_processing)
646 * @param client the client to process, RC must have already been increased
647 * using GNUNET_SERVER_client_keep and will be decreased by one in this
649 * @param ret GNUNET_NO to start processing from the buffer,
650 * GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
651 * GNUNET_SYSERR if we should instantly abort due to error in a previous step
654 process_mst (struct GNUNET_SERVER_Client *client,
657 while ( (ret != GNUNET_SYSERR) &&
658 (client->server != NULL) &&
659 (GNUNET_YES != client->shutdown_now) &&
660 (0 == client->suspended) )
662 if (ret == GNUNET_OK)
664 client->receive_pending = GNUNET_YES;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Server re-enters receive loop.\n");
669 GNUNET_CONNECTION_receive (client->connection,
670 GNUNET_SERVER_MAX_MESSAGE_SIZE,
671 client->server->idle_timeout,
672 &process_incoming, client);
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server processes additional messages instantly.\n");
678 ret = GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, GNUNET_YES);
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
685 client->shutdown_now,
689 if (ret == GNUNET_NO)
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Server has more data pending but is suspended.\n");
695 client->receive_pending = GNUNET_SYSERR; /* data pending */
697 if ( (ret == GNUNET_SYSERR) ||
698 (GNUNET_YES == client->shutdown_now) )
699 GNUNET_SERVER_client_disconnect (client);
700 GNUNET_SERVER_client_drop (client);
705 * We are receiving an incoming message. Process it.
707 * @param cls our closure (handle for the client)
708 * @param buf buffer with data received from network
709 * @param available number of bytes available in buf
710 * @param addr address of the sender
711 * @param addrlen length of addr
712 * @param errCode code indicating errors receiving, 0 for success
715 process_incoming (void *cls,
718 const struct sockaddr *addr,
719 socklen_t addrlen, int errCode)
721 struct GNUNET_SERVER_Client *client = cls;
722 struct GNUNET_SERVER_Handle *server = client->server;
725 GNUNET_assert (client->receive_pending == GNUNET_YES);
726 client->receive_pending = GNUNET_NO;
731 (client->shutdown_now == GNUNET_YES) ||
732 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
734 /* other side closed connection, error connecting, etc. */
735 GNUNET_SERVER_client_disconnect (client);
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Server receives %u bytes from `%s'.\n",
741 (unsigned int) available,
742 GNUNET_a2s (addr, addrlen));
744 GNUNET_SERVER_client_keep (client);
745 client->last_activity = GNUNET_TIME_absolute_get ();
746 ret = GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, GNUNET_YES);
747 process_mst (client, ret);
752 * Task run to start again receiving from the network
753 * and process requests.
755 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
756 * @param tc scheduler context (unused)
759 restart_processing (void *cls,
760 const struct GNUNET_SCHEDULER_TaskContext *tc)
762 struct GNUNET_SERVER_Client *client = cls;
763 struct GNUNET_SERVER_Handle *server = client->server;
765 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
766 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
767 (GNUNET_NO == server->clients_ignore_shutdown) )
769 GNUNET_SERVER_client_disconnect (client);
772 if (client->receive_pending == GNUNET_NO)
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Server begins to read again from client.\n");
778 client->receive_pending = GNUNET_YES;
779 GNUNET_CONNECTION_receive (client->connection,
780 GNUNET_SERVER_MAX_MESSAGE_SIZE,
781 client->server->idle_timeout, &process_incoming, client);
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Server continues processing messages still in the buffer.\n");
788 GNUNET_SERVER_client_keep (client);
789 client->receive_pending = GNUNET_NO;
790 process_mst (client, GNUNET_NO);
795 * This function is called whenever our inbound message tokenizer has
796 * received a complete message.
798 * @param cls closure (struct GNUNET_SERVER_Handle)
799 * @param client identification of the client (struct GNUNET_SERVER_Client*)
800 * @param message the actual message
803 client_message_tokenizer_callback (void *cls,
805 const struct GNUNET_MessageHeader *message)
807 struct GNUNET_SERVER_Handle *server = cls;
808 struct GNUNET_SERVER_Client *sender = client;
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813 "Tokenizer gives server message of type %u from client\n",
814 ntohs (message->type));
816 sender->in_process_client_buffer = GNUNET_YES;
817 ret = GNUNET_SERVER_inject (server, sender, message);
818 sender->in_process_client_buffer = GNUNET_NO;
819 if (GNUNET_OK != ret)
820 GNUNET_SERVER_client_disconnect (sender);
825 * Add a TCP socket-based connection to the set of handles managed by
826 * this server. Use this function for outgoing (P2P) connections that
827 * we initiated (and where this server should process incoming
830 * @param server the server to use
831 * @param connection the connection to manage (client must
832 * stop using this connection from now on)
833 * @return the client handle (client should call
834 * "client_drop" on the return value eventually)
836 struct GNUNET_SERVER_Client *
837 GNUNET_SERVER_connect_socket (struct
840 struct GNUNET_CONNECTION_Handle *connection)
842 struct GNUNET_SERVER_Client *client;
844 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
845 client->connection = connection;
846 client->mst = GNUNET_SERVER_mst_create (GNUNET_SERVER_MAX_MESSAGE_SIZE,
847 &client_message_tokenizer_callback,
849 client->reference_count = 1;
850 client->server = server;
851 client->last_activity = GNUNET_TIME_absolute_get ();
852 client->next = server->clients;
853 server->clients = client;
854 client->receive_pending = GNUNET_YES;
855 GNUNET_CONNECTION_receive (client->connection,
856 GNUNET_SERVER_MAX_MESSAGE_SIZE,
857 server->idle_timeout, &process_incoming, client);
863 * Notify the server that the given client handle should
864 * be kept (keeps the connection up if possible, increments
865 * the internal reference counter).
867 * @param client the client to keep
870 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
872 client->reference_count++;
877 * Notify the server that the given client handle is no
878 * longer required. Decrements the reference counter. If
879 * that counter reaches zero an inactive connection maybe
882 * @param client the client to drop
885 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
887 GNUNET_assert (client->reference_count > 0);
888 client->reference_count--;
889 if ( (client->shutdown_now == GNUNET_YES) &&
890 (client->reference_count == 0) )
891 GNUNET_SERVER_client_disconnect (client);
896 * Obtain the network address of the other party.
898 * @param client the client to get the address for
899 * @param addr where to store the address
900 * @param addrlen where to store the length of the address
901 * @return GNUNET_OK on success
904 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
905 void **addr, size_t * addrlen)
907 return GNUNET_CONNECTION_get_address (client->connection,
913 * Ask the server to notify us whenever a client disconnects.
914 * This function is called whenever the actual network connection
915 * is closed; the reference count may be zero or larger than zero
918 * @param server the server manageing the clients
919 * @param callback function to call on disconnect
920 * @param callback_cls closure for callback
923 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
924 GNUNET_SERVER_DisconnectCallback callback,
927 struct NotifyList *n;
929 n = GNUNET_malloc (sizeof (struct NotifyList));
930 n->callback = callback;
931 n->callback_cls = callback_cls;
932 n->next = server->disconnect_notify_list;
933 server->disconnect_notify_list = n;
938 * Ask the server to stop notifying us whenever a client disconnects.
940 * @param server the server manageing the clients
941 * @param callback function to call on disconnect
942 * @param callback_cls closure for callback
945 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
946 GNUNET_SERVER_DisconnectCallback callback,
949 struct NotifyList *pos;
950 struct NotifyList *prev;
953 pos = server->disconnect_notify_list;
956 if ( (pos->callback == callback) &&
957 (pos->callback_cls == callback_cls ) )
968 server->disconnect_notify_list = pos->next;
970 prev->next = pos->next;
976 * Ask the server to disconnect from the given client.
977 * This is the same as returning GNUNET_SYSERR from a message
978 * handler, except that it allows dropping of a client even
979 * when not handling a message from that client.
981 * @param client the client to disconnect from
984 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
986 struct GNUNET_SERVER_Client *prev;
987 struct GNUNET_SERVER_Client *pos;
988 struct GNUNET_SERVER_Handle *server;
989 struct NotifyList *n;
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "Client is being disconnected from the server.\n");
996 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
998 GNUNET_SCHEDULER_cancel (client->server->sched,
999 client->restart_task);
1000 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1002 if (GNUNET_YES == client->receive_pending)
1004 GNUNET_CONNECTION_receive_cancel (client->connection);
1005 client->receive_pending = GNUNET_NO;
1008 rc = client->reference_count;
1009 if (client->server != NULL)
1011 server = client->server;
1012 client->server = NULL;
1013 client->shutdown_now = GNUNET_YES;
1015 pos = server->clients;
1016 while ((pos != NULL) && (pos != client))
1021 GNUNET_assert (pos != NULL);
1023 server->clients = pos->next;
1025 prev->next = pos->next;
1026 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1028 GNUNET_SCHEDULER_cancel (server->sched,
1029 client->restart_task);
1030 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1032 n = server->disconnect_notify_list;
1035 n->callback (n->callback_cls, client);
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "RC still positive, not destroying everything.\n");
1047 if (client->in_process_client_buffer == GNUNET_YES)
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "Still processing inputs, not destroying everything.\n");
1056 if (client->persist == GNUNET_YES)
1057 GNUNET_CONNECTION_persist_ (client->connection);
1058 GNUNET_CONNECTION_destroy (client->connection, GNUNET_NO);
1059 GNUNET_SERVER_mst_destroy (client->mst);
1060 GNUNET_free (client);
1065 * Notify us when the server has enough space to transmit
1066 * a message of the given size to the given client.
1068 * @param client client to transmit message to
1069 * @param size requested amount of buffer space
1070 * @param timeout after how long should we give up (and call
1071 * notify with buf NULL and size 0)?
1072 * @param callback function to call when space is available
1073 * @param callback_cls closure for callback
1074 * @return non-NULL if the notify callback was queued; can be used
1075 * to cancel the request using
1076 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1077 * NULL if we are already going to notify someone else (busy)
1079 struct GNUNET_CONNECTION_TransmitHandle *
1080 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1082 struct GNUNET_TIME_Relative timeout,
1083 GNUNET_CONNECTION_TransmitReadyNotify
1084 callback, void *callback_cls)
1086 return GNUNET_CONNECTION_notify_transmit_ready (client->connection,
1088 timeout, callback, callback_cls);
1092 * Set the persistent flag on this client, used to setup client connection
1093 * to only be killed when the service it's connected to is actually dead.
1095 * @param client the client to set the persistent flag on
1098 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1100 client->persist = GNUNET_YES;
1104 * Resume receiving from this client, we are done processing the
1105 * current request. This function must be called from within each
1106 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1108 * @param client client we were processing a message of
1109 * @param success GNUNET_OK to keep the connection open and
1110 * continue to receive
1111 * GNUNET_NO to close the connection (normal behavior)
1112 * GNUNET_SYSERR to close the connection (signal
1116 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1120 GNUNET_assert (client->suspended > 0);
1121 client->suspended--;
1122 if (success != GNUNET_OK)
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "GNUNET_SERVER_receive_done called with failure indication\n");
1128 GNUNET_SERVER_client_disconnect (client);
1131 if (client->suspended > 0)
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1135 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1139 if (client->in_process_client_buffer == GNUNET_YES)
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1147 if (client->server == NULL)
1149 GNUNET_SERVER_client_disconnect (client);
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1156 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1157 client->restart_task = GNUNET_SCHEDULER_add_now (client->server->sched,
1158 &restart_processing,
1164 * Configure this server's connections to continue handling client
1165 * requests as usual even after we get a shutdown signal. The change
1166 * only applies to clients that connect to the server from the outside
1167 * using TCP after this call. Clients managed previously or those
1168 * added using GNUNET_SERVER_connect_socket and
1169 * GNUNET_SERVER_connect_callback are not affected by this option.
1171 * @param h server handle
1172 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1175 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore)
1177 h->clients_ignore_shutdown = do_ignore;
1180 /* end of server.c */