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_NO
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 * 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;
146 * @brief handle for a client of the server
148 struct GNUNET_SERVER_Client
152 * This is a linked list.
154 struct GNUNET_SERVER_Client *next;
157 * Processing of incoming data.
159 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
162 * Server that this client belongs to.
164 struct GNUNET_SERVER_Handle *server;
167 * Client closure for callbacks.
169 struct GNUNET_CONNECTION_Handle *connection;
172 * ID of task used to restart processing.
174 GNUNET_SCHEDULER_TaskIdentifier restart_task;
177 * Last activity on this socket (used to time it out
178 * if reference_count == 0).
180 struct GNUNET_TIME_Absolute last_activity;
183 * Number of external entities with a reference to
184 * this client object.
186 unsigned int reference_count;
189 * Was processing if incoming messages suspended while
190 * we were still processing data already received?
191 * This is a counter saying how often processing was
192 * suspended (once per handler invoked).
194 unsigned int suspended;
197 * Are we currently in the "process_client_buffer" function (and
198 * will hence restart the receive job on exit if suspended == 0 once
199 * we are done?). If this is set, then "receive_done" will
200 * essentially only decrement suspended; if this is not set, then
201 * "receive_done" may need to restart the receive process (either
202 * from the side-buffer or via select/recv).
204 int in_process_client_buffer;
207 * We're about to close down this client due to some serious
213 * Are we currently trying to receive? (YES if we are, NO if we are not,
214 * SYSERR if data is already available in MST).
219 * Persist the file handle for this client no matter what happens,
220 * force the OS to close once the process actually dies. Should only
221 * be used in special cases!
228 * Scheduler says our listen socket is ready. Process it!
230 * @param cls handle to our server for which we are processing the listen
232 * @param tc reason why we are running right now
235 process_listen_socket (void *cls,
236 const struct GNUNET_SCHEDULER_TaskContext *tc)
238 struct GNUNET_SERVER_Handle *server = cls;
239 struct GNUNET_CONNECTION_Handle *sock;
240 struct GNUNET_SERVER_Client *client;
241 struct GNUNET_NETWORK_FDSet *r;
244 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
245 r = GNUNET_NETWORK_fdset_create ();
247 while (NULL != server->listen_sockets[i])
248 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
249 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
251 /* ignore shutdown, someone else will take care of it! */
252 server->listen_task = GNUNET_SCHEDULER_add_select (server->sched,
253 GNUNET_SCHEDULER_PRIORITY_HIGH,
254 GNUNET_SCHEDULER_NO_TASK,
255 GNUNET_TIME_UNIT_FOREVER_REL,
257 &process_listen_socket,
259 GNUNET_NETWORK_fdset_destroy (r);
263 while (NULL != server->listen_sockets[i])
265 if (GNUNET_NETWORK_fdset_isset
266 (tc->read_ready, server->listen_sockets[i]))
269 GNUNET_CONNECTION_create_from_accept (tc->sched, server->access,
271 server->listen_sockets[i]);
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Server accepted incoming connection.\n");
278 client = GNUNET_SERVER_connect_socket (server, sock);
279 GNUNET_CONNECTION_ignore_shutdown (sock,
280 server->clients_ignore_shutdown);
281 /* decrement reference count, we don't keep "client" alive */
282 GNUNET_SERVER_client_drop (client);
287 /* listen for more! */
288 server->listen_task = GNUNET_SCHEDULER_add_select (server->sched,
289 GNUNET_SCHEDULER_PRIORITY_HIGH,
290 GNUNET_SCHEDULER_NO_TASK,
291 GNUNET_TIME_UNIT_FOREVER_REL,
293 &process_listen_socket,
295 GNUNET_NETWORK_fdset_destroy (r);
300 * Create and initialize a listen socket for the server.
302 * @param serverAddr address to listen on
303 * @param socklen length of address
304 * @return NULL on error, otherwise the listen socket
306 static struct GNUNET_NETWORK_Handle *
307 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
309 const static int on = 1;
310 struct GNUNET_NETWORK_Handle *sock;
314 switch (serverAddr->sa_family)
317 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
320 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
326 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
329 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
335 if (GNUNET_NETWORK_socket_setsockopt
336 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
337 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
340 if ( (serverAddr->sa_family == AF_INET6) &&
341 (GNUNET_NETWORK_socket_setsockopt
342 (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK) )
343 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
347 /* bind the socket */
348 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
351 if (errno != EADDRINUSE)
353 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
354 fail if we already took the port on IPv6; if both IPv4 and
355 IPv6 binds fail, then our caller will log using the
356 errno preserved in 'eno' */
357 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
361 ("`%s' failed for port %d (%s).\n"),
363 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
366 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
370 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
372 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
373 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Server starts to listen on port %u.\n", port);
387 * Create a new server.
389 * @param sched scheduler to use
390 * @param access function for access control
391 * @param access_cls closure for access
392 * @param lsocks NULL-terminated array of listen sockets
393 * @param idle_timeout after how long should we timeout idle connections?
394 * @param require_found if YES, connections sending messages of unknown type
396 * @return handle for the new server, NULL on error
397 * (typically, "port" already in use)
399 struct GNUNET_SERVER_Handle *
400 GNUNET_SERVER_create_with_sockets (struct GNUNET_SCHEDULER_Handle *sched,
401 GNUNET_CONNECTION_AccessCheck access, void *access_cls,
402 struct GNUNET_NETWORK_Handle **lsocks,
403 struct GNUNET_TIME_Relative
407 struct GNUNET_SERVER_Handle *ret;
408 struct GNUNET_NETWORK_FDSet *r;
411 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
413 ret->idle_timeout = idle_timeout;
414 ret->listen_sockets = lsocks;
415 ret->access = access;
416 ret->access_cls = access_cls;
417 ret->require_found = require_found;
420 r = GNUNET_NETWORK_fdset_create ();
422 while (NULL != ret->listen_sockets[i])
423 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]);
424 ret->listen_task = GNUNET_SCHEDULER_add_select (sched,
425 GNUNET_SCHEDULER_PRIORITY_HIGH,
426 GNUNET_SCHEDULER_NO_TASK,
427 GNUNET_TIME_UNIT_FOREVER_REL,
429 &process_listen_socket,
431 GNUNET_NETWORK_fdset_destroy (r);
438 * Create a new server.
440 * @param sched scheduler to use
441 * @param access function for access control
442 * @param access_cls closure for access
443 * @param serverAddr address to listen on (including port), NULL terminated array
444 * @param socklen length of serverAddr
445 * @param idle_timeout after how long should we timeout idle connections?
446 * @param require_found if YES, connections sending messages of unknown type
448 * @return handle for the new server, NULL on error
449 * (typically, "port" already in use)
451 struct GNUNET_SERVER_Handle *
452 GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched,
453 GNUNET_CONNECTION_AccessCheck access,
455 struct sockaddr *const *serverAddr,
456 const socklen_t * socklen,
457 struct GNUNET_TIME_Relative
458 idle_timeout, int require_found)
460 struct GNUNET_NETWORK_Handle **lsocks;
465 while (serverAddr[i] != NULL)
470 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
473 while (serverAddr[i] != NULL)
475 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
476 if (lsocks[j] != NULL)
483 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
484 GNUNET_free (lsocks);
492 return GNUNET_SERVER_create_with_sockets (sched,
501 * Free resources held by this server.
503 * @param s server to destroy
506 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
508 struct HandlerList *hpos;
509 struct NotifyList *npos;
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
515 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
517 GNUNET_SCHEDULER_cancel (s->sched, s->listen_task);
518 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
520 if (s->listen_sockets != NULL)
523 while (s->listen_sockets[i] != NULL)
524 GNUNET_break (GNUNET_OK ==
525 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
526 GNUNET_free (s->listen_sockets);
527 s->listen_sockets = NULL;
529 while (s->clients != NULL)
530 GNUNET_SERVER_client_disconnect (s->clients);
531 while (NULL != (hpos = s->handlers))
533 s->handlers = hpos->next;
536 while (NULL != (npos = s->disconnect_notify_list))
538 npos->callback (npos->callback_cls, NULL);
539 s->disconnect_notify_list = npos->next;
547 * Add additional handlers to an existing server.
549 * @param server the server to add handlers to
550 * @param handlers array of message handlers for
551 * incoming messages; the last entry must
552 * have "NULL" for the "callback"; multiple
553 * entries for the same type are allowed,
554 * they will be called in order of occurence.
555 * These handlers can be removed later;
556 * the handlers array must exist until removed
557 * (or server is destroyed).
560 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
561 const struct GNUNET_SERVER_MessageHandler
564 struct HandlerList *p;
566 p = GNUNET_malloc (sizeof (struct HandlerList));
567 p->handlers = handlers;
568 p->next = server->handlers;
569 server->handlers = p;
574 * Inject a message into the server, pretend it came
575 * from the specified client. Delivery of the message
576 * will happen instantly (if a handler is installed;
577 * otherwise the call does nothing).
579 * @param server the server receiving the message
580 * @param sender the "pretended" sender of the message
582 * @param message message to transmit
583 * @return GNUNET_OK if the message was OK and the
584 * connection can stay open
585 * GNUNET_SYSERR if the connection to the
586 * client should be shut down
589 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
590 struct GNUNET_SERVER_Client *sender,
591 const struct GNUNET_MessageHeader *message)
593 struct HandlerList *pos;
594 const struct GNUNET_SERVER_MessageHandler *mh;
600 type = ntohs (message->type);
601 size = ntohs (message->size);
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Server schedules transmission of %u-byte message of type %u to client.\n",
607 pos = server->handlers;
612 while (pos->handlers[i].callback != NULL)
614 mh = &pos->handlers[i];
615 if ( (mh->type == type) ||
616 (mh->type == GNUNET_MESSAGE_TYPE_ALL) )
618 if ((mh->expected_size != 0) && (mh->expected_size != size))
620 #if GNUNET8_NETWORK_IS_DEAD
621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
622 "Expected %u bytes for message of type %u, got %u\n",
628 return GNUNET_SYSERR;
632 mh->callback (mh->callback_cls, sender, message);
639 if (found == GNUNET_NO)
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
642 "Received message of unknown type %d\n",
644 if (server->require_found == GNUNET_YES)
645 return GNUNET_SYSERR;
652 * We are receiving an incoming message. Process it.
654 * @param cls our closure (handle for the client)
655 * @param buf buffer with data received from network
656 * @param available number of bytes available in buf
657 * @param addr address of the sender
658 * @param addrlen length of addr
659 * @param errCode code indicating errors receiving, 0 for success
662 process_incoming (void *cls,
665 const struct sockaddr *addr,
666 socklen_t addrlen, int errCode);
670 * Process messages from the client's message tokenizer until either
671 * the tokenizer is empty (and then schedule receiving more), or
672 * until some handler is not immediately done (then wait for restart_processing)
675 * @param client the client to process, RC must have already been increased
676 * using GNUNET_SERVER_client_keep and will be decreased by one in this
678 * @param ret GNUNET_NO to start processing from the buffer,
679 * GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
680 * GNUNET_SYSERR if we should instantly abort due to error in a previous step
683 process_mst (struct GNUNET_SERVER_Client *client,
686 while ( (ret != GNUNET_SYSERR) &&
687 (client->server != NULL) &&
688 (GNUNET_YES != client->shutdown_now) &&
689 (0 == client->suspended) )
691 if (ret == GNUNET_OK)
693 client->receive_pending = GNUNET_YES;
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 "Server re-enters receive loop.\n");
698 GNUNET_CONNECTION_receive (client->connection,
699 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
700 client->server->idle_timeout,
701 &process_incoming, client);
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server processes additional messages instantly.\n");
707 ret = GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO, GNUNET_YES);
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
714 client->shutdown_now,
718 if (ret == GNUNET_NO)
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Server has more data pending but is suspended.\n");
724 client->receive_pending = GNUNET_SYSERR; /* data pending */
726 if ( (ret == GNUNET_SYSERR) ||
727 (GNUNET_YES == client->shutdown_now) )
728 GNUNET_SERVER_client_disconnect (client);
729 GNUNET_SERVER_client_drop (client);
734 * We are receiving an incoming message. Process it.
736 * @param cls our closure (handle for the client)
737 * @param buf buffer with data received from network
738 * @param available number of bytes available in buf
739 * @param addr address of the sender
740 * @param addrlen length of addr
741 * @param errCode code indicating errors receiving, 0 for success
744 process_incoming (void *cls,
747 const struct sockaddr *addr,
748 socklen_t addrlen, int errCode)
750 struct GNUNET_SERVER_Client *client = cls;
751 struct GNUNET_SERVER_Handle *server = client->server;
754 GNUNET_assert (client->receive_pending == GNUNET_YES);
755 client->receive_pending = GNUNET_NO;
760 (client->shutdown_now == GNUNET_YES) ||
761 (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
763 /* other side closed connection, error connecting, etc. */
764 GNUNET_SERVER_client_disconnect (client);
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Server receives %u bytes from `%s'.\n",
770 (unsigned int) available,
771 GNUNET_a2s (addr, addrlen));
773 GNUNET_SERVER_client_keep (client);
774 client->last_activity = GNUNET_TIME_absolute_get ();
775 ret = GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO, GNUNET_YES);
776 process_mst (client, ret);
781 * Task run to start again receiving from the network
782 * and process requests.
784 * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
785 * @param tc scheduler context (unused)
788 restart_processing (void *cls,
789 const struct GNUNET_SCHEDULER_TaskContext *tc)
791 struct GNUNET_SERVER_Client *client = cls;
792 struct GNUNET_SERVER_Handle *server = client->server;
794 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
795 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) &&
796 (GNUNET_NO == server->clients_ignore_shutdown) )
798 GNUNET_SERVER_client_disconnect (client);
801 if (client->receive_pending == GNUNET_NO)
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "Server begins to read again from client.\n");
807 client->receive_pending = GNUNET_YES;
808 GNUNET_CONNECTION_receive (client->connection,
809 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
810 client->server->idle_timeout, &process_incoming, client);
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Server continues processing messages still in the buffer.\n");
817 GNUNET_SERVER_client_keep (client);
818 client->receive_pending = GNUNET_NO;
819 process_mst (client, GNUNET_NO);
824 * This function is called whenever our inbound message tokenizer has
825 * received a complete message.
827 * @param cls closure (struct GNUNET_SERVER_Handle)
828 * @param client identification of the client (struct GNUNET_SERVER_Client*)
829 * @param message the actual message
832 client_message_tokenizer_callback (void *cls,
834 const struct GNUNET_MessageHeader *message)
836 struct GNUNET_SERVER_Handle *server = cls;
837 struct GNUNET_SERVER_Client *sender = client;
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Tokenizer gives server message of type %u from client\n",
843 ntohs (message->type));
845 sender->in_process_client_buffer = GNUNET_YES;
846 ret = GNUNET_SERVER_inject (server, sender, message);
847 sender->in_process_client_buffer = GNUNET_NO;
848 if (GNUNET_OK != ret)
849 GNUNET_SERVER_client_disconnect (sender);
854 * Add a TCP socket-based connection to the set of handles managed by
855 * this server. Use this function for outgoing (P2P) connections that
856 * we initiated (and where this server should process incoming
859 * @param server the server to use
860 * @param connection the connection to manage (client must
861 * stop using this connection from now on)
862 * @return the client handle (client should call
863 * "client_drop" on the return value eventually)
865 struct GNUNET_SERVER_Client *
866 GNUNET_SERVER_connect_socket (struct
869 struct GNUNET_CONNECTION_Handle *connection)
871 struct GNUNET_SERVER_Client *client;
873 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
874 client->connection = connection;
875 client->mst = GNUNET_SERVER_mst_create (&client_message_tokenizer_callback,
877 client->reference_count = 1;
878 client->server = server;
879 client->last_activity = GNUNET_TIME_absolute_get ();
880 client->next = server->clients;
881 server->clients = client;
882 client->receive_pending = GNUNET_YES;
883 GNUNET_CONNECTION_receive (client->connection,
884 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
885 server->idle_timeout, &process_incoming, client);
891 * Notify the server that the given client handle should
892 * be kept (keeps the connection up if possible, increments
893 * the internal reference counter).
895 * @param client the client to keep
898 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
900 client->reference_count++;
905 * Notify the server that the given client handle is no
906 * longer required. Decrements the reference counter. If
907 * that counter reaches zero an inactive connection maybe
910 * @param client the client to drop
913 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
915 GNUNET_assert (client->reference_count > 0);
916 client->reference_count--;
917 if ( (client->shutdown_now == GNUNET_YES) &&
918 (client->reference_count == 0) )
919 GNUNET_SERVER_client_disconnect (client);
924 * Obtain the network address of the other party.
926 * @param client the client to get the address for
927 * @param addr where to store the address
928 * @param addrlen where to store the length of the address
929 * @return GNUNET_OK on success
932 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
933 void **addr, size_t * addrlen)
935 return GNUNET_CONNECTION_get_address (client->connection,
941 * Ask the server to notify us whenever a client disconnects.
942 * This function is called whenever the actual network connection
943 * is closed; the reference count may be zero or larger than zero
946 * @param server the server manageing the clients
947 * @param callback function to call on disconnect
948 * @param callback_cls closure for callback
951 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
952 GNUNET_SERVER_DisconnectCallback callback,
955 struct NotifyList *n;
957 n = GNUNET_malloc (sizeof (struct NotifyList));
958 n->callback = callback;
959 n->callback_cls = callback_cls;
960 n->next = server->disconnect_notify_list;
961 server->disconnect_notify_list = n;
966 * Ask the server to stop notifying us whenever a client disconnects.
968 * @param server the server manageing the clients
969 * @param callback function to call on disconnect
970 * @param callback_cls closure for callback
973 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
974 GNUNET_SERVER_DisconnectCallback callback,
977 struct NotifyList *pos;
978 struct NotifyList *prev;
981 pos = server->disconnect_notify_list;
984 if ( (pos->callback == callback) &&
985 (pos->callback_cls == callback_cls ) )
996 server->disconnect_notify_list = pos->next;
998 prev->next = pos->next;
1004 * Ask the server to disconnect from the given client.
1005 * This is the same as returning GNUNET_SYSERR from a message
1006 * handler, except that it allows dropping of a client even
1007 * when not handling a message from that client.
1009 * @param client the client to disconnect from
1012 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1014 struct GNUNET_SERVER_Client *prev;
1015 struct GNUNET_SERVER_Client *pos;
1016 struct GNUNET_SERVER_Handle *server;
1017 struct NotifyList *n;
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "Client is being disconnected from the server.\n");
1024 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1026 GNUNET_SCHEDULER_cancel (client->server->sched,
1027 client->restart_task);
1028 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1030 if (GNUNET_YES == client->receive_pending)
1032 GNUNET_CONNECTION_receive_cancel (client->connection);
1033 client->receive_pending = GNUNET_NO;
1036 rc = client->reference_count;
1037 if (client->server != NULL)
1039 server = client->server;
1040 client->server = NULL;
1041 client->shutdown_now = GNUNET_YES;
1043 pos = server->clients;
1044 while ((pos != NULL) && (pos != client))
1049 GNUNET_assert (pos != NULL);
1051 server->clients = pos->next;
1053 prev->next = pos->next;
1054 if (client->restart_task != GNUNET_SCHEDULER_NO_TASK)
1056 GNUNET_SCHEDULER_cancel (server->sched,
1057 client->restart_task);
1058 client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1060 n = server->disconnect_notify_list;
1063 n->callback (n->callback_cls, client);
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "RC still positive, not destroying everything.\n");
1075 if (client->in_process_client_buffer == GNUNET_YES)
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Still processing inputs, not destroying everything.\n");
1084 if (client->persist == GNUNET_YES)
1085 GNUNET_CONNECTION_persist_ (client->connection);
1086 GNUNET_CONNECTION_destroy (client->connection, GNUNET_NO);
1087 GNUNET_SERVER_mst_destroy (client->mst);
1088 GNUNET_free (client);
1093 * Notify us when the server has enough space to transmit
1094 * a message of the given size to the given client.
1096 * @param client client to transmit message to
1097 * @param size requested amount of buffer space
1098 * @param timeout after how long should we give up (and call
1099 * notify with buf NULL and size 0)?
1100 * @param callback function to call when space is available
1101 * @param callback_cls closure for callback
1102 * @return non-NULL if the notify callback was queued; can be used
1103 * to cancel the request using
1104 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1105 * NULL if we are already going to notify someone else (busy)
1107 struct GNUNET_CONNECTION_TransmitHandle *
1108 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1110 struct GNUNET_TIME_Relative timeout,
1111 GNUNET_CONNECTION_TransmitReadyNotify
1112 callback, void *callback_cls)
1114 return GNUNET_CONNECTION_notify_transmit_ready (client->connection,
1116 timeout, callback, callback_cls);
1120 * Set the persistent flag on this client, used to setup client connection
1121 * to only be killed when the service it's connected to is actually dead.
1123 * @param client the client to set the persistent flag on
1126 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1128 client->persist = GNUNET_YES;
1132 * Resume receiving from this client, we are done processing the
1133 * current request. This function must be called from within each
1134 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1136 * @param client client we were processing a message of
1137 * @param success GNUNET_OK to keep the connection open and
1138 * continue to receive
1139 * GNUNET_NO to close the connection (normal behavior)
1140 * GNUNET_SYSERR to close the connection (signal
1144 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1148 GNUNET_assert (client->suspended > 0);
1149 client->suspended--;
1150 if (success != GNUNET_OK)
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 "GNUNET_SERVER_receive_done called with failure indication\n");
1156 GNUNET_SERVER_client_disconnect (client);
1159 if (client->suspended > 0)
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 "GNUNET_SERVER_receive_done called, but more clients pending\n");
1167 if (client->in_process_client_buffer == GNUNET_YES)
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171 "GNUNET_SERVER_receive_done called while still in processing loop\n");
1175 if (client->server == NULL)
1177 GNUNET_SERVER_client_disconnect (client);
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1184 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1185 client->restart_task = GNUNET_SCHEDULER_add_now (client->server->sched,
1186 &restart_processing,
1192 * Configure this server's connections to continue handling client
1193 * requests as usual even after we get a shutdown signal. The change
1194 * only applies to clients that connect to the server from the outside
1195 * using TCP after this call. Clients managed previously or those
1196 * added using GNUNET_SERVER_connect_socket and
1197 * GNUNET_SERVER_connect_callback are not affected by this option.
1199 * @param h server handle
1200 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1203 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore)
1205 h->clients_ignore_shutdown = do_ignore;
1208 /* end of server.c */