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
27 * - fix inefficient memmove in message processing
31 #include "gnunet_common.h"
32 #include "gnunet_connection_lib.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_time_lib.h"
36 #include "gnunet_disk_lib.h"
37 #include "gnunet_protocols.h"
39 #define DEBUG_SERVER GNUNET_NO
42 * List of arrays of message handlers.
47 * This is a linked list.
49 struct HandlerList *next;
52 * NULL-terminated array of handlers.
54 const struct GNUNET_SERVER_MessageHandler *handlers;
59 * List of arrays of message handlers.
64 * This is a linked list.
66 struct NotifyList *next;
71 GNUNET_SERVER_DisconnectCallback callback;
74 * Closure for callback.
81 * @brief handle for a server
83 struct GNUNET_SERVER_Handle
88 struct GNUNET_SCHEDULER_Handle *sched;
91 * List of handlers for incoming messages.
93 struct HandlerList *handlers;
96 * List of our current clients.
98 struct GNUNET_SERVER_Client *clients;
101 * Linked list of functions to call on disconnects by clients.
103 struct NotifyList *disconnect_notify_list;
106 * Function to call for access control.
108 GNUNET_CONNECTION_AccessCheck access;
111 * Closure for access.
116 * NULL-terminated array of sockets used to listen for new
119 struct GNUNET_NETWORK_Handle **listen_sockets;
122 * After how long should an idle connection time
125 struct GNUNET_TIME_Relative idle_timeout;
128 * maximum write buffer size for accepted sockets
133 * Task scheduled to do the listening.
135 GNUNET_SCHEDULER_TaskIdentifier listen_task;
138 * Do we ignore messages of types that we do not
139 * understand or do we require that a handler
140 * is found (and if not kill the connection)?
145 * Should all of the clients of this server continue
146 * to process connections as usual even if we get
147 * a shutdown request? (the listen socket always ignores
150 int clients_ignore_shutdown;
156 * @brief handle for a client of the server
158 struct GNUNET_SERVER_Client
162 * Size of the buffer for incoming data. Should be
163 * first so we get nice alignment.
165 char incoming_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
168 * This is a linked list.
170 struct GNUNET_SERVER_Client *next;
173 * Server that this client belongs to.
175 struct GNUNET_SERVER_Handle *server;
178 * Client closure for callbacks.
180 void *client_closure;
183 * Callback to receive from client.
185 GNUNET_SERVER_ReceiveCallback receive;
188 * Callback to cancel receive from client.
190 GNUNET_SERVER_ReceiveCancelCallback receive_cancel;
193 * Callback to ask about transmit-ready notification.
195 GNUNET_SERVER_TransmitReadyCallback notify_transmit_ready;
198 * Callback to ask about transmit-ready notification.
200 GNUNET_SERVER_TransmitReadyCancelCallback notify_transmit_ready_cancel;
203 * Callback to check if client is still valid.
205 GNUNET_SERVER_CheckCallback check;
208 * Callback to destroy client.
210 GNUNET_SERVER_DestroyCallback destroy;
213 * Side-buffer for incoming data used when processing
219 * Number of bytes in the side buffer.
221 size_t side_buf_size;
224 * Last activity on this socket (used to time it out
225 * if reference_count == 0).
227 struct GNUNET_TIME_Absolute last_activity;
230 * How many bytes in the "incoming_buffer" are currently
231 * valid? (starting at offset 0).
236 * Number of external entities with a reference to
237 * this client object.
239 unsigned int reference_count;
242 * Was processing if incoming messages suspended while
243 * we were still processing data already received?
244 * This is a counter saying how often processing was
245 * suspended (once per handler invoked).
247 unsigned int suspended;
250 * Are we currently in the "process_client_buffer" function (and
251 * will hence restart the receive job on exit if suspended == 0 once
252 * we are done?). If this is set, then "receive_done" will
253 * essentially only decrement suspended; if this is not set, then
254 * "receive_done" may need to restart the receive process (either
255 * from the side-buffer or via select/recv).
257 int in_process_client_buffer;
260 * We're about to close down this client due to some serious
269 * Scheduler says our listen socket is ready. Process it!
271 * @param cls handle to our server for which we are processing the listen
273 * @param tc reason why we are running right now
276 process_listen_socket (void *cls,
277 const struct GNUNET_SCHEDULER_TaskContext *tc)
279 struct GNUNET_SERVER_Handle *server = cls;
280 struct GNUNET_CONNECTION_Handle *sock;
281 struct GNUNET_SERVER_Client *client;
282 struct GNUNET_NETWORK_FDSet *r;
285 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
286 r = GNUNET_NETWORK_fdset_create ();
288 while (NULL != server->listen_sockets[i])
289 GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
290 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
292 /* ignore shutdown, someone else will take care of it! */
293 server->listen_task = GNUNET_SCHEDULER_add_select (server->sched,
294 GNUNET_SCHEDULER_PRIORITY_HIGH,
295 GNUNET_SCHEDULER_NO_TASK,
296 GNUNET_TIME_UNIT_FOREVER_REL,
298 &process_listen_socket,
300 GNUNET_NETWORK_fdset_destroy (r);
304 while (NULL != server->listen_sockets[i])
306 if (GNUNET_NETWORK_fdset_isset
307 (tc->read_ready, server->listen_sockets[i]))
310 GNUNET_CONNECTION_create_from_accept (tc->sched, server->access,
312 server->listen_sockets[i],
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Server accepted incoming connection.\n");
320 client = GNUNET_SERVER_connect_socket (server, sock);
321 GNUNET_CONNECTION_ignore_shutdown (sock,
322 server->clients_ignore_shutdown);
323 /* decrement reference count, we don't keep "client" alive */
324 GNUNET_SERVER_client_drop (client);
329 /* listen for more! */
330 server->listen_task = GNUNET_SCHEDULER_add_select (server->sched,
331 GNUNET_SCHEDULER_PRIORITY_HIGH,
332 GNUNET_SCHEDULER_NO_TASK,
333 GNUNET_TIME_UNIT_FOREVER_REL,
335 &process_listen_socket,
337 GNUNET_NETWORK_fdset_destroy (r);
342 * Create and initialize a listen socket for the server.
344 * @return NULL on error, otherwise the listen socket
346 static struct GNUNET_NETWORK_Handle *
347 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
349 const static int on = 1;
350 struct GNUNET_NETWORK_Handle *sock;
354 switch (serverAddr->sa_family)
357 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
360 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
366 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
369 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
374 (GNUNET_NETWORK_socket_setsockopt
375 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK))
376 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
378 /* bind the socket */
379 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
382 if (errno != EADDRINUSE)
384 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
385 fail if we already took the port on IPv6; if both IPv4 and
386 IPv6 binds fail, then our caller will log using the
387 errno preserved in 'eno' */
388 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392 ("`%s' failed for port %d (%s).\n"),
394 (serverAddr->sa_family == AF_INET) ? "IPv4" : "IPv6");
397 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
401 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
403 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
404 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Server starts to listen on port %u.\n", port);
418 * Create a new server.
420 * @param sched scheduler to use
421 * @param access function for access control
422 * @param access_cls closure for access
423 * @param serverAddr address to listen on (including port), NULL terminated array
424 * @param socklen length of serverAddr
425 * @param maxbuf maximum write buffer size for accepted sockets
426 * @param idle_timeout after how long should we timeout idle connections?
427 * @param require_found if YES, connections sending messages of unknown type
429 * @return handle for the new server, NULL on error
430 * (typically, "port" already in use)
432 struct GNUNET_SERVER_Handle *
433 GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched,
434 GNUNET_CONNECTION_AccessCheck access,
436 struct sockaddr *const *serverAddr,
437 const socklen_t * socklen,
439 struct GNUNET_TIME_Relative
440 idle_timeout, int require_found)
442 struct GNUNET_SERVER_Handle *ret;
443 struct GNUNET_NETWORK_Handle **lsocks;
444 struct GNUNET_NETWORK_FDSet *r;
449 while (serverAddr[i] != NULL)
454 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
457 while (serverAddr[i] != NULL)
459 lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
460 if (lsocks[j] != NULL)
467 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
468 GNUNET_free (lsocks);
476 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
478 ret->maxbuf = maxbuf;
479 ret->idle_timeout = idle_timeout;
480 ret->listen_sockets = lsocks;
481 ret->access = access;
482 ret->access_cls = access_cls;
483 ret->require_found = require_found;
487 r = GNUNET_NETWORK_fdset_create ();
489 while (NULL != ret->listen_sockets[i])
490 GNUNET_NETWORK_fdset_set (r, ret->listen_sockets[i++]);
491 ret->listen_task = GNUNET_SCHEDULER_add_select (sched,
492 GNUNET_SCHEDULER_PRIORITY_HIGH,
493 GNUNET_SCHEDULER_NO_TASK,
494 GNUNET_TIME_UNIT_FOREVER_REL,
496 &process_listen_socket,
498 GNUNET_NETWORK_fdset_destroy (r);
505 * Free resources held by this server.
508 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
510 struct GNUNET_SERVER_Client *pos;
511 struct HandlerList *hpos;
512 struct NotifyList *npos;
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
518 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
520 GNUNET_SCHEDULER_cancel (s->sched, s->listen_task);
521 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
523 if (s->listen_sockets != NULL)
526 while (s->listen_sockets[i] != NULL)
527 GNUNET_break (GNUNET_OK ==
528 GNUNET_NETWORK_socket_close (s->listen_sockets[i++]));
529 GNUNET_free (s->listen_sockets);
530 s->listen_sockets = NULL;
532 while (s->clients != NULL)
535 s->clients = pos->next;
538 while (NULL != (hpos = s->handlers))
540 s->handlers = hpos->next;
543 while (NULL != (npos = s->disconnect_notify_list))
545 npos->callback (npos->callback_cls, NULL);
546 s->disconnect_notify_list = npos->next;
554 * Add additional handlers to an existing server.
556 * @param server the server to add handlers to
557 * @param handlers array of message handlers for
558 * incoming messages; the last entry must
559 * have "NULL" for the "callback"; multiple
560 * entries for the same type are allowed,
561 * they will be called in order of occurence.
562 * These handlers can be removed later;
563 * the handlers array must exist until removed
564 * (or server is destroyed).
567 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
568 const struct GNUNET_SERVER_MessageHandler
571 struct HandlerList *p;
573 p = GNUNET_malloc (sizeof (struct HandlerList));
574 p->handlers = handlers;
575 p->next = server->handlers;
576 server->handlers = p;
581 * Inject a message into the server, pretend it came
582 * from the specified client. Delivery of the message
583 * will happen instantly (if a handler is installed;
584 * otherwise the call does nothing).
586 * @param server the server receiving the message
587 * @param sender the "pretended" sender of the message
589 * @param message message to transmit
590 * @return GNUNET_OK if the message was OK and the
591 * connection can stay open
592 * GNUNET_SYSERR if the connection to the
593 * client should be shut down
596 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
597 struct GNUNET_SERVER_Client *sender,
598 const struct GNUNET_MessageHeader *message)
600 struct HandlerList *pos;
601 const struct GNUNET_SERVER_MessageHandler *mh;
607 type = ntohs (message->type);
608 size = ntohs (message->size);
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 "Server schedules transmission of %u-byte message of type %u to client.\n",
614 pos = server->handlers;
619 while (pos->handlers[i].callback != NULL)
621 mh = &pos->handlers[i];
622 if ( (mh->type == type) ||
623 (mh->type == GNUNET_MESSAGE_TYPE_ALL) )
625 if ((mh->expected_size != 0) && (mh->expected_size != size))
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"), type);
643 if (server->require_found == GNUNET_YES)
644 return GNUNET_SYSERR;
651 * We're finished with this client and especially its input
652 * processing. If the RC is zero, free all resources otherwise wait
653 * until RC hits zero to do so.
656 shutdown_incoming_processing (struct GNUNET_SERVER_Client *client)
658 struct GNUNET_SERVER_Client *prev;
659 struct GNUNET_SERVER_Client *pos;
660 struct GNUNET_SERVER_Handle *server;
661 struct NotifyList *n;
664 rc = client->reference_count;
665 if (client->server != NULL)
667 server = client->server;
668 client->server = NULL;
670 pos = server->clients;
671 while ((pos != NULL) && (pos != client))
676 GNUNET_assert (pos != NULL);
678 server->clients = pos->next;
680 prev->next = pos->next;
681 n = server->disconnect_notify_list;
684 n->callback (n->callback_cls, client);
688 /* wait for RC to hit zero, then free */
691 client->destroy (client->client_closure);
692 GNUNET_free (client);
697 * Go over the contents of the client buffer; as long as full messages
698 * are available, pass them on for processing. Update the buffer
699 * accordingly. Handles fatal errors by shutting down the connection.
701 * @param client identifies which client receive buffer to process
704 process_client_buffer (struct GNUNET_SERVER_Client *client)
706 struct GNUNET_SERVER_Handle *server;
707 const struct GNUNET_MessageHeader *hdr;
710 client->in_process_client_buffer = GNUNET_YES;
711 server = client->server;
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Private buffer contains %u bytes; client is %s and we are %s\n",
716 client->suspended ? "suspended" : "up",
717 client->shutdown_now ? "in shutdown" : "running");
719 while ((client->receive_pos >= sizeof (struct GNUNET_MessageHeader)) &&
720 (0 == client->suspended) && (GNUNET_YES != client->shutdown_now))
722 hdr = (const struct GNUNET_MessageHeader *) &client->incoming_buffer;
723 msize = ntohs (hdr->size);
724 if (msize > client->receive_pos)
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Total message size is %u, we only have %u bytes; need more data\n",
729 msize, client->receive_pos);
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "Passing %u bytes to callback for processing\n", msize);
737 if ((msize < sizeof (struct GNUNET_MessageHeader)) ||
738 (GNUNET_OK != GNUNET_SERVER_inject (server, client, hdr)))
740 client->in_process_client_buffer = GNUNET_NO;
741 shutdown_incoming_processing (client);
744 /* FIXME: this is highly inefficient; we should
745 try to avoid this if the new base address is
746 already nicely aligned. See old handler code... */
747 memmove (client->incoming_buffer,
748 &client->incoming_buffer[msize], client->receive_pos - msize);
749 client->receive_pos -= msize;
751 client->in_process_client_buffer = GNUNET_NO;
752 if (GNUNET_YES == client->shutdown_now)
753 shutdown_incoming_processing (client);
758 * We are receiving an incoming message. Process it.
760 * @param cls our closure (handle for the client)
761 * @param buf buffer with data received from network
762 * @param available number of bytes available in buf
763 * @param addr address of the sender
764 * @param addrlen length of addr
765 * @param errCode code indicating errors receiving, 0 for success
768 process_incoming (void *cls,
771 const struct sockaddr *addr, socklen_t addrlen, int errCode)
773 struct GNUNET_SERVER_Client *client = cls;
774 struct GNUNET_SERVER_Handle *server = client->server;
775 const char *cbuf = buf;
782 (client->shutdown_now == GNUNET_YES) ||
783 (GNUNET_YES != client->check (client->client_closure)))
785 /* other side closed connection, error connecting, etc. */
786 shutdown_incoming_processing (client);
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Server receives %u bytes from `%s'.\n",
792 available, GNUNET_a2s (addr, addrlen));
794 GNUNET_SERVER_client_keep (client);
795 client->last_activity = GNUNET_TIME_absolute_get ();
796 /* process data (if available) */
797 while (available > 0)
800 if (maxcpy > sizeof (client->incoming_buffer) - client->receive_pos)
801 maxcpy = sizeof (client->incoming_buffer) - client->receive_pos;
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
804 "Can copy %u bytes to private buffer\n", maxcpy);
806 memcpy (&client->incoming_buffer[client->receive_pos], cbuf, maxcpy);
807 client->receive_pos += maxcpy;
810 if (0 < client->suspended)
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "Client has suspended processing; copying %u bytes to side buffer to be used later.\n",
819 GNUNET_assert (client->side_buf_size == 0);
820 GNUNET_assert (client->side_buf == NULL);
821 client->side_buf_size = available;
822 client->side_buf = GNUNET_malloc (available);
823 memcpy (client->side_buf, cbuf, available);
826 break; /* do not run next client iteration! */
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830 "Now processing messages in private buffer\n");
832 process_client_buffer (client);
834 GNUNET_assert (available == 0);
835 if ((client->suspended == 0) &&
836 (GNUNET_YES != client->shutdown_now) && (client->server != NULL))
838 /* Finally, keep receiving! */
839 client->receive (client->client_closure,
840 GNUNET_SERVER_MAX_MESSAGE_SIZE,
841 server->idle_timeout, &process_incoming, client);
843 if (GNUNET_YES == client->shutdown_now)
844 shutdown_incoming_processing (client);
845 GNUNET_SERVER_client_drop (client);
853 restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
855 struct GNUNET_SERVER_Client *client = cls;
857 GNUNET_SERVER_client_keep (client);
858 process_client_buffer (client);
859 if (0 == client->suspended)
860 client->receive (client->client_closure,
861 GNUNET_SERVER_MAX_MESSAGE_SIZE,
862 client->server->idle_timeout, &process_incoming, client);
863 GNUNET_SERVER_client_drop (client);
868 * Add a client to the set of our clients and
872 add_client (struct GNUNET_SERVER_Handle *server,
873 struct GNUNET_SERVER_Client *client)
875 client->server = server;
876 client->last_activity = GNUNET_TIME_absolute_get ();
877 client->next = server->clients;
878 server->clients = client;
879 client->receive (client->client_closure,
880 GNUNET_SERVER_MAX_MESSAGE_SIZE,
881 server->idle_timeout, &process_incoming, client);
886 * Create a request for receiving data from a socket.
888 * @param cls identifies the socket to receive from
889 * @param max how much data to read at most
890 * @param timeout when should this operation time out
891 * @param receiver function to call for processing
892 * @param receiver_cls closure for receiver
895 sock_receive (void *cls,
897 struct GNUNET_TIME_Relative timeout,
898 GNUNET_CONNECTION_Receiver receiver, void *receiver_cls)
900 GNUNET_CONNECTION_receive (cls, max, timeout, receiver, receiver_cls);
905 * Wrapper to cancel receiving from a socket.
907 * @param cls handle to the GNUNET_CONNECTION_Handle to cancel
910 sock_receive_cancel (void *cls)
912 GNUNET_CONNECTION_receive_cancel (cls);
920 sock_notify_transmit_ready (void *cls,
922 struct GNUNET_TIME_Relative timeout,
923 GNUNET_CONNECTION_TransmitReadyNotify notify,
926 return GNUNET_CONNECTION_notify_transmit_ready (cls, size, timeout, notify,
935 sock_notify_transmit_ready_cancel (void *cls, void *h)
937 GNUNET_CONNECTION_notify_transmit_ready_cancel (h);
942 * Check if socket is still valid (no fatal errors have happened so far).
944 * @param cls the socket
945 * @return GNUNET_YES if valid, GNUNET_NO otherwise
948 sock_check (void *cls)
950 return GNUNET_CONNECTION_check (cls);
955 * Destroy this socket (free resources).
957 * @param cls the socket
960 sock_destroy (void *cls)
962 GNUNET_CONNECTION_destroy (cls);
967 * Add a TCP socket-based connection to the set of handles managed by
968 * this server. Use this function for outgoing (P2P) connections that
969 * we initiated (and where this server should process incoming
972 * @param server the server to use
973 * @param connection the connection to manage (client must
974 * stop using this connection from now on)
975 * @return the client handle (client should call
976 * "client_drop" on the return value eventually)
978 struct GNUNET_SERVER_Client *
979 GNUNET_SERVER_connect_socket (struct
982 struct GNUNET_CONNECTION_Handle *connection)
984 struct GNUNET_SERVER_Client *client;
986 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
987 client->client_closure = connection;
988 client->receive = &sock_receive;
989 client->receive_cancel = &sock_receive_cancel;
990 client->notify_transmit_ready = &sock_notify_transmit_ready;
991 client->notify_transmit_ready_cancel = &sock_notify_transmit_ready_cancel;
992 client->check = &sock_check;
993 client->destroy = &sock_destroy;
994 client->reference_count = 1;
995 add_client (server, client);
1001 * Add an arbitrary connection to the set of handles managed by this
1002 * server. This can be used if a sending and receiving does not
1003 * really go over the network (internal transmission) or for servers
1006 * @param server the server to use
1007 * @param chandle opaque handle for the connection
1008 * @param creceive receive function for the connection
1009 * @param ccancel cancel receive function for the connection
1010 * @param cnotify transmit notification function for the connection
1011 * @param cnotify_cancel transmit notification cancellation function for the connection
1012 * @param ccheck function to test if the connection is still up
1013 * @param cdestroy function to close and free the connection
1014 * @return the client handle (client should call
1015 * "client_drop" on the return value eventually)
1017 struct GNUNET_SERVER_Client *
1018 GNUNET_SERVER_connect_callback (struct
1019 GNUNET_SERVER_Handle
1022 GNUNET_SERVER_ReceiveCallback
1024 GNUNET_SERVER_ReceiveCancelCallback
1026 GNUNET_SERVER_TransmitReadyCallback
1028 GNUNET_SERVER_TransmitReadyCancelCallback
1030 GNUNET_SERVER_CheckCallback
1032 GNUNET_SERVER_DestroyCallback cdestroy)
1034 struct GNUNET_SERVER_Client *client;
1036 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
1037 client->client_closure = chandle;
1038 client->receive = creceive;
1039 client->receive_cancel = ccancel;
1040 client->notify_transmit_ready = cnotify;
1041 client->notify_transmit_ready_cancel = cnotify_cancel;
1042 client->check = ccheck;
1043 client->destroy = cdestroy;
1044 client->reference_count = 1;
1045 add_client (server, client);
1051 * Notify the server that the given client handle should
1052 * be kept (keeps the connection up if possible, increments
1053 * the internal reference counter).
1055 * @param client the client to keep
1058 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1060 client->reference_count++;
1065 * Notify the server that the given client handle is no
1066 * longer required. Decrements the reference counter. If
1067 * that counter reaches zero an inactive connection maybe
1070 * @param client the client to drop
1073 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1075 GNUNET_assert (client->reference_count > 0);
1076 client->reference_count--;
1077 if ((client->server == NULL) && (client->reference_count == 0))
1078 shutdown_incoming_processing (client);
1083 * Obtain the network address of the other party.
1085 * @param client the client to get the address for
1086 * @param addr where to store the address
1087 * @param addrlen where to store the length of the address
1088 * @return GNUNET_OK on success
1091 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1092 void **addr, size_t * addrlen)
1094 if (client->receive != &sock_receive)
1095 return GNUNET_SYSERR; /* not a network client */
1096 return GNUNET_CONNECTION_get_address (client->client_closure,
1102 * Ask the server to notify us whenever a client disconnects.
1103 * This function is called whenever the actual network connection
1104 * is closed; the reference count may be zero or larger than zero
1107 * @param server the server manageing the clients
1108 * @param callback function to call on disconnect
1109 * @param callback_cls closure for callback
1112 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1113 GNUNET_SERVER_DisconnectCallback callback,
1116 struct NotifyList *n;
1118 n = GNUNET_malloc (sizeof (struct NotifyList));
1119 n->callback = callback;
1120 n->callback_cls = callback_cls;
1121 n->next = server->disconnect_notify_list;
1122 server->disconnect_notify_list = n;
1127 * Ask the server to stop notifying us whenever a client disconnects.
1129 * @param server the server manageing the clients
1130 * @param callback function to call on disconnect
1131 * @param callback_cls closure for callback
1134 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1135 GNUNET_SERVER_DisconnectCallback callback,
1138 struct NotifyList *pos;
1139 struct NotifyList *prev;
1142 pos = server->disconnect_notify_list;
1145 if ( (pos->callback == callback) &&
1146 (pos->callback_cls == callback_cls ) )
1157 server->disconnect_notify_list = pos->next;
1159 prev->next = pos->next;
1165 * Ask the server to disconnect from the given client.
1166 * This is the same as returning GNUNET_SYSERR from a message
1167 * handler, except that it allows dropping of a client even
1168 * when not handling a message from that client.
1170 * @param client the client to disconnect from
1173 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1175 if (client->server == NULL)
1176 return; /* already disconnected */
1177 client->receive_cancel (client->client_closure);
1178 shutdown_incoming_processing (client);
1183 * Notify us when the server has enough space to transmit
1184 * a message of the given size to the given client.
1186 * @param client client to transmit message to
1187 * @param size requested amount of buffer space
1188 * @param timeout after how long should we give up (and call
1189 * notify with buf NULL and size 0)?
1190 * @param callback function to call when space is available
1191 * @param callback_cls closure for callback
1192 * @return non-NULL if the notify callback was queued; can be used
1193 * to cancel the request using
1194 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1195 * NULL if we are already going to notify someone else (busy)
1197 struct GNUNET_CONNECTION_TransmitHandle *
1198 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1200 struct GNUNET_TIME_Relative timeout,
1201 GNUNET_CONNECTION_TransmitReadyNotify
1202 callback, void *callback_cls)
1204 return client->notify_transmit_ready (client->client_closure,
1206 timeout, callback, callback_cls);
1211 * Resume receiving from this client, we are done processing the
1212 * current request. This function must be called from within each
1213 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1215 * @param client client we were processing a message of
1216 * @param success GNUNET_OK to keep the connection open and
1217 * continue to receive
1218 * GNUNET_NO to close the connection (normal behavior)
1219 * GNUNET_SYSERR to close the connection (signal
1223 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1229 GNUNET_assert (client->suspended > 0);
1230 client->suspended--;
1231 if (success != GNUNET_OK)
1232 client->shutdown_now = GNUNET_YES;
1233 if (client->suspended > 0)
1235 if (client->in_process_client_buffer == GNUNET_YES)
1237 if (client->side_buf_size > 0)
1239 /* resume processing from side-buf */
1240 sb = client->side_buf;
1241 client->side_buf = NULL;
1242 /* this will also resume the receive job */
1243 if (GNUNET_YES != client->shutdown_now)
1244 process_incoming (client, sb, client->side_buf_size, NULL, 0, 0);
1246 shutdown_incoming_processing (client);
1247 /* finally, free the side-buf */
1251 /* resume receive job */
1252 if (GNUNET_YES != client->shutdown_now)
1254 GNUNET_SCHEDULER_add_continuation (client->server->sched,
1255 &restart_processing,
1257 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1260 shutdown_incoming_processing (client);
1265 * Configure this server's connections to continue handling client
1266 * requests as usual even after we get a shutdown signal. The change
1267 * only applies to clients that connect to the server from the outside
1268 * using TCP after this call. Clients managed previously or those
1269 * added using GNUNET_SERVER_connect_socket and
1270 * GNUNET_SERVER_connect_callback are not affected by this option.
1272 * @param h server handle
1273 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1276 GNUNET_SERVER_ignore_shutdown (struct GNUNET_SERVER_Handle *h, int do_ignore)
1278 h->clients_ignore_shutdown = do_ignore;
1281 /* end of server.c */