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"
38 #define DEBUG_SERVER GNUNET_YES
41 * List of arrays of message handlers.
46 * This is a linked list.
48 struct HandlerList *next;
51 * NULL-terminated array of handlers.
53 const struct GNUNET_SERVER_MessageHandler *handlers;
58 * List of arrays of message handlers.
63 * This is a linked list.
65 struct NotifyList *next;
70 GNUNET_SERVER_DisconnectCallback callback;
73 * Closure for callback.
80 * @brief handle for a server
82 struct GNUNET_SERVER_Handle
87 struct GNUNET_SCHEDULER_Handle *sched;
90 * List of handlers for incoming messages.
92 struct HandlerList *handlers;
95 * List of our current clients.
97 struct GNUNET_SERVER_Client *clients;
100 * Linked list of functions to call on disconnects by clients.
102 struct NotifyList *disconnect_notify_list;
105 * Function to call for access control.
107 GNUNET_CONNECTION_AccessCheck access;
110 * Closure for access.
115 * After how long should an idle connection time
118 struct GNUNET_TIME_Relative idle_timeout;
121 * maximum write buffer size for accepted sockets
126 * Pipe used to signal shutdown of the server.
128 struct GNUNET_DISK_PipeHandle *shutpipe;
131 * Socket used to listen for new connections. Set to
132 * "-1" by GNUNET_SERVER_destroy to initiate shutdown.
134 struct GNUNET_NETWORK_Handle *listen_socket;
137 * Set to GNUNET_YES if we are shutting down.
142 * Do we ignore messages of types that we do not
143 * understand or do we require that a handler
144 * is found (and if not kill the connection)?
152 * @brief handle for a client of the server
154 struct GNUNET_SERVER_Client
158 * Size of the buffer for incoming data. Should be
159 * first so we get nice alignment.
161 char incoming_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
164 * This is a linked list.
166 struct GNUNET_SERVER_Client *next;
169 * Server that this client belongs to.
171 struct GNUNET_SERVER_Handle *server;
174 * Client closure for callbacks.
176 void *client_closure;
179 * Callback to receive from client.
181 GNUNET_SERVER_ReceiveCallback receive;
184 * Callback to cancel receive from client.
186 GNUNET_SERVER_ReceiveCancelCallback receive_cancel;
189 * Callback to ask about transmit-ready notification.
191 GNUNET_SERVER_TransmitReadyCallback notify_transmit_ready;
194 * Callback to ask about transmit-ready notification.
196 GNUNET_SERVER_TransmitReadyCancelCallback notify_transmit_ready_cancel;
199 * Callback to check if client is still valid.
201 GNUNET_SERVER_CheckCallback check;
204 * Callback to destroy client.
206 GNUNET_SERVER_DestroyCallback destroy;
209 * Side-buffer for incoming data used when processing
215 * Number of bytes in the side buffer.
217 size_t side_buf_size;
220 * Last activity on this socket (used to time it out
221 * if reference_count == 0).
223 struct GNUNET_TIME_Absolute last_activity;
226 * How many bytes in the "incoming_buffer" are currently
227 * valid? (starting at offset 0).
232 * Number of external entities with a reference to
233 * this client object.
235 unsigned int reference_count;
238 * Was processing if incoming messages suspended while
239 * we were still processing data already received?
240 * This is a counter saying how often processing was
241 * suspended (once per handler invoked).
243 unsigned int suspended;
246 * Are we currently in the "process_client_buffer" function (and
247 * will hence restart the receive job on exit if suspended == 0 once
248 * we are done?). If this is set, then "receive_done" will
249 * essentially only decrement suspended; if this is not set, then
250 * "receive_done" may need to restart the receive process (either
251 * from the side-buffer or via select/recv).
253 int in_process_client_buffer;
256 * We're about to close down this client due to some serious
265 * Server has been asked to shutdown, free resources.
268 destroy_server (struct GNUNET_SERVER_Handle *server)
270 struct GNUNET_SERVER_Client *pos;
271 struct HandlerList *hpos;
272 struct NotifyList *npos;
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Server shutting down.\n");
278 GNUNET_assert (server->listen_socket == NULL);
279 if (GNUNET_OK != GNUNET_DISK_pipe_close (server->shutpipe))
280 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
282 while (server->clients != NULL)
284 pos = server->clients;
285 server->clients = pos->next;
288 while (NULL != (hpos = server->handlers))
290 server->handlers = hpos->next;
293 while (NULL != (npos = server->disconnect_notify_list))
295 server->disconnect_notify_list = npos->next;
298 GNUNET_free (server);
303 * Scheduler says our listen socket is ready.
307 process_listen_socket (void *cls,
308 const struct GNUNET_SCHEDULER_TaskContext *tc)
310 struct GNUNET_SERVER_Handle *server = cls;
311 struct GNUNET_CONNECTION_Handle *sock;
312 struct GNUNET_SERVER_Client *client;
313 struct GNUNET_NETWORK_FDSet *r;
314 const struct GNUNET_DISK_FileHandle *shutpipe;
316 if ((server->do_shutdown) ||
317 ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0))
319 /* shutdown was initiated */
320 GNUNET_assert (server->listen_socket != NULL);
321 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (server->listen_socket));
322 server->listen_socket = NULL;
323 if (server->do_shutdown)
324 destroy_server (server);
327 shutpipe = GNUNET_DISK_pipe_handle (server->shutpipe,
328 GNUNET_DISK_PIPE_END_READ);
329 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_socket));
330 GNUNET_assert (!GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, shutpipe));
331 sock = GNUNET_CONNECTION_create_from_accept (tc->sched,
334 server->listen_socket,
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Server accepted incoming connection.\n");
342 client = GNUNET_SERVER_connect_socket (server, sock);
343 /* decrement reference count, we don't keep "client" alive */
344 GNUNET_SERVER_client_drop (client);
346 /* listen for more! */
347 r = GNUNET_NETWORK_fdset_create ();
348 GNUNET_NETWORK_fdset_set (r, server->listen_socket);
349 GNUNET_NETWORK_fdset_handle_set (r, shutpipe);
350 GNUNET_SCHEDULER_add_select (server->sched,
352 GNUNET_SCHEDULER_PRIORITY_HIGH,
353 GNUNET_SCHEDULER_NO_TASK,
354 GNUNET_TIME_UNIT_FOREVER_REL,
356 &process_listen_socket, server);
357 GNUNET_NETWORK_fdset_destroy (r);
362 * Create and initialize a listen socket for the server.
364 * @return NULL on error, otherwise the listen socket
366 static struct GNUNET_NETWORK_Handle *
367 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
369 const static int on = 1;
370 struct GNUNET_NETWORK_Handle *sock;
373 switch (serverAddr->sa_family)
376 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
379 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
385 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
388 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
391 if (GNUNET_NETWORK_socket_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
392 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
394 /* bind the socket */
395 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) != GNUNET_OK)
397 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
400 ("`%s' failed for port %d. Is the service already running?\n"),
402 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
405 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
407 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
408 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Server starts to listen on port %u.\n",
421 * Create a new server.
423 * @param sched scheduler to use
424 * @param access function for access control
425 * @param access_cls closure for access
426 * @param serverAddr address to listen on (including port), use NULL
427 * for internal server (no listening)
428 * @param socklen length of serverAddr
429 * @param maxbuf maximum write buffer size for accepted sockets
430 * @param idle_timeout after how long should we timeout idle connections?
431 * @param require_found if YES, connections sending messages of unknown type
433 * @return handle for the new server, NULL on error
434 * (typically, "port" already in use)
436 struct GNUNET_SERVER_Handle *
437 GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched,
438 GNUNET_CONNECTION_AccessCheck access,
440 const struct sockaddr *serverAddr,
443 struct GNUNET_TIME_Relative
444 idle_timeout, int require_found)
446 struct GNUNET_SERVER_Handle *ret;
447 struct GNUNET_NETWORK_Handle *lsock;
448 struct GNUNET_NETWORK_FDSet *r;
451 if (serverAddr != NULL)
453 lsock = open_listen_socket (serverAddr, socklen);
457 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
458 if (NULL == (ret->shutpipe = GNUNET_DISK_pipe (GNUNET_NO)))
460 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (lsock));
465 ret->maxbuf = maxbuf;
466 ret->idle_timeout = idle_timeout;
467 ret->listen_socket = lsock;
468 ret->access = access;
469 ret->access_cls = access_cls;
470 ret->require_found = require_found;
473 r = GNUNET_NETWORK_fdset_create ();
474 GNUNET_NETWORK_fdset_set (r, ret->listen_socket);
475 GNUNET_NETWORK_fdset_handle_set (r, GNUNET_DISK_pipe_handle (ret->shutpipe,
476 GNUNET_DISK_PIPE_END_READ));
477 GNUNET_SCHEDULER_add_select (sched,
479 GNUNET_SCHEDULER_PRIORITY_HIGH,
480 GNUNET_SCHEDULER_NO_TASK,
481 GNUNET_TIME_UNIT_FOREVER_REL,
483 NULL, &process_listen_socket, ret);
484 GNUNET_NETWORK_fdset_destroy (r);
491 * Free resources held by this server.
494 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
498 GNUNET_assert (s->do_shutdown == GNUNET_NO);
499 s->do_shutdown = GNUNET_YES;
500 if (s->listen_socket == NULL)
503 GNUNET_break (1 == GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (s->shutpipe,
504 GNUNET_DISK_PIPE_END_WRITE),
511 * Add additional handlers to an existing server.
513 * @param server the server to add handlers to
514 * @param handlers array of message handlers for
515 * incoming messages; the last entry must
516 * have "NULL" for the "callback"; multiple
517 * entries for the same type are allowed,
518 * they will be called in order of occurence.
519 * These handlers can be removed later;
520 * the handlers array must exist until removed
521 * (or server is destroyed).
524 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
525 const struct GNUNET_SERVER_MessageHandler
528 struct HandlerList *p;
530 p = GNUNET_malloc (sizeof (struct HandlerList));
531 p->handlers = handlers;
532 p->next = server->handlers;
533 server->handlers = p;
538 * Inject a message into the server, pretend it came
539 * from the specified client. Delivery of the message
540 * will happen instantly (if a handler is installed;
541 * otherwise the call does nothing).
543 * @param server the server receiving the message
544 * @param sender the "pretended" sender of the message
546 * @param message message to transmit
547 * @return GNUNET_OK if the message was OK and the
548 * connection can stay open
549 * GNUNET_SYSERR if the connection to the
550 * client should be shut down
553 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
554 struct GNUNET_SERVER_Client *sender,
555 const struct GNUNET_MessageHeader *message)
557 struct HandlerList *pos;
558 const struct GNUNET_SERVER_MessageHandler *mh;
564 type = ntohs (message->type);
565 size = ntohs (message->size);
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "Server schedules transmission of %u-byte message of type %u to client.\n",
572 pos = server->handlers;
577 while (pos->handlers[i].callback != NULL)
579 mh = &pos->handlers[i];
580 if (mh->type == type)
582 if ((mh->expected_size != 0) && (mh->expected_size != size))
585 return GNUNET_SYSERR;
589 mh->callback (mh->callback_cls, sender, message);
596 if (found == GNUNET_NO)
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
599 _("Received message of unknown type %d\n"), type);
600 if (server->require_found == GNUNET_YES)
601 return GNUNET_SYSERR;
608 * We're finished with this client and especially its input
609 * processing. If the RC is zero, free all resources otherwise wait
610 * until RC hits zero to do so.
613 shutdown_incoming_processing (struct GNUNET_SERVER_Client *client)
615 struct GNUNET_SERVER_Client *prev;
616 struct GNUNET_SERVER_Client *pos;
617 struct GNUNET_SERVER_Handle *server;
618 struct NotifyList *n;
621 rc = client->reference_count;
622 if (client->server != NULL)
624 server = client->server;
625 client->server = NULL;
627 pos = server->clients;
628 while ((pos != NULL) && (pos != client))
633 GNUNET_assert (pos != NULL);
635 server->clients = pos->next;
637 prev->next = pos->next;
638 n = server->disconnect_notify_list;
641 n->callback (n->callback_cls, client);
645 /* wait for RC to hit zero, then free */
648 client->destroy (client->client_closure);
649 GNUNET_free (client);
654 * Go over the contents of the client buffer; as long as full messages
655 * are available, pass them on for processing. Update the buffer
656 * accordingly. Handles fatal errors by shutting down the connection.
658 * @param client identifies which client receive buffer to process
661 process_client_buffer (struct GNUNET_SERVER_Client *client)
663 struct GNUNET_SERVER_Handle *server;
664 const struct GNUNET_MessageHeader *hdr;
667 client->in_process_client_buffer = GNUNET_YES;
668 server = client->server;
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "Private buffer contains %u bytes; client is %s and we are %s\n",
673 client->suspended ? "suspended" : "up",
674 client->shutdown_now ? "in shutdown" : "running");
676 while ((client->receive_pos >= sizeof (struct GNUNET_MessageHeader)) &&
677 (0 == client->suspended) && (GNUNET_YES != client->shutdown_now))
679 hdr = (const struct GNUNET_MessageHeader *) &client->incoming_buffer;
680 msize = ntohs (hdr->size);
681 if (msize > client->receive_pos)
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Total message size is %u, we only have %u bytes; need more data\n",
687 client->receive_pos);
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Passing %u bytes to callback for processing\n",
696 if ((msize < sizeof (struct GNUNET_MessageHeader)) ||
697 (GNUNET_OK != GNUNET_SERVER_inject (server, client, hdr)))
699 client->in_process_client_buffer = GNUNET_NO;
700 shutdown_incoming_processing (client);
703 /* FIXME: this is highly inefficient; we should
704 try to avoid this if the new base address is
705 already nicely aligned. See old handler code... */
706 memmove (client->incoming_buffer,
707 &client->incoming_buffer[msize], client->receive_pos - msize);
708 client->receive_pos -= msize;
710 client->in_process_client_buffer = GNUNET_NO;
711 if (GNUNET_YES == client->shutdown_now)
712 shutdown_incoming_processing (client);
717 * We are receiving an incoming message. Process it.
719 * @param cls our closure (handle for the client)
720 * @param buf buffer with data received from network
721 * @param available number of bytes available in buf
722 * @param addr address of the sender
723 * @param addrlen length of addr
724 * @param errCode code indicating errors receiving, 0 for success
727 process_incoming (void *cls,
730 const struct sockaddr *addr,
734 struct GNUNET_SERVER_Client *client = cls;
735 struct GNUNET_SERVER_Handle *server = client->server;
736 const char *cbuf = buf;
743 (client->shutdown_now == GNUNET_YES) ||
744 (GNUNET_YES != client->check (client->client_closure)))
746 /* other side closed connection, error connecting, etc. */
747 shutdown_incoming_processing (client);
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Server receives %u bytes from `%s'.\n",
754 GNUNET_a2s(addr, addrlen));
756 GNUNET_SERVER_client_keep (client);
757 client->last_activity = GNUNET_TIME_absolute_get ();
758 /* process data (if available) */
759 while (available > 0)
762 if (maxcpy > sizeof (client->incoming_buffer) - client->receive_pos)
763 maxcpy = sizeof (client->incoming_buffer) - client->receive_pos;
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Can copy %u bytes to private buffer\n",
769 memcpy (&client->incoming_buffer[client->receive_pos], cbuf, maxcpy);
770 client->receive_pos += maxcpy;
773 if (0 < client->suspended)
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Client has suspended processing; copying %u bytes to side buffer to be used later.\n",
782 GNUNET_assert (client->side_buf_size == 0);
783 GNUNET_assert (client->side_buf == NULL);
784 client->side_buf_size = available;
785 client->side_buf = GNUNET_malloc (available);
786 memcpy (client->side_buf, cbuf, available);
789 break; /* do not run next client iteration! */
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 "Now processing messages in private buffer\n");
795 process_client_buffer (client);
797 GNUNET_assert (available == 0);
798 if ((client->suspended == 0) &&
799 (GNUNET_YES != client->shutdown_now) && (client->server != NULL))
801 /* Finally, keep receiving! */
802 client->receive (client->client_closure,
803 GNUNET_SERVER_MAX_MESSAGE_SIZE,
804 server->idle_timeout,
805 &process_incoming, client);
807 if (GNUNET_YES == client->shutdown_now)
808 shutdown_incoming_processing (client);
809 GNUNET_SERVER_client_drop (client);
817 restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
819 struct GNUNET_SERVER_Client *client = cls;
821 process_client_buffer (client);
822 if (0 == client->suspended)
823 client->receive (client->client_closure,
824 GNUNET_SERVER_MAX_MESSAGE_SIZE,
825 client->server->idle_timeout,
826 &process_incoming, client);
831 * Add a client to the set of our clients and
835 add_client (struct GNUNET_SERVER_Handle *server,
836 struct GNUNET_SERVER_Client *client)
838 client->server = server;
839 client->last_activity = GNUNET_TIME_absolute_get ();
840 client->next = server->clients;
841 server->clients = client;
842 client->receive (client->client_closure,
843 GNUNET_SERVER_MAX_MESSAGE_SIZE,
844 server->idle_timeout,
845 &process_incoming, client);
850 * Create a request for receiving data from a socket.
852 * @param cls identifies the socket to receive from
853 * @param max how much data to read at most
854 * @param timeout when should this operation time out
855 * @param receiver function to call for processing
856 * @param receiver_cls closure for receiver
859 sock_receive (void *cls,
861 struct GNUNET_TIME_Relative timeout,
862 GNUNET_CONNECTION_Receiver receiver, void *receiver_cls)
864 GNUNET_CONNECTION_receive (cls, max, timeout, receiver, receiver_cls);
869 * Wrapper to cancel receiving from a socket.
871 * @param cls handle to the GNUNET_CONNECTION_Handle to cancel
874 sock_receive_cancel (void *cls)
876 GNUNET_CONNECTION_receive_cancel (cls);
884 sock_notify_transmit_ready (void *cls,
886 struct GNUNET_TIME_Relative timeout,
887 GNUNET_CONNECTION_TransmitReadyNotify notify,
890 return GNUNET_CONNECTION_notify_transmit_ready (cls, size, timeout, notify,
899 sock_notify_transmit_ready_cancel (void *cls, void *h)
901 GNUNET_CONNECTION_notify_transmit_ready_cancel (h);
906 * Check if socket is still valid (no fatal errors have happened so far).
908 * @param cls the socket
909 * @return GNUNET_YES if valid, GNUNET_NO otherwise
912 sock_check (void *cls)
914 return GNUNET_CONNECTION_check (cls);
919 * Destroy this socket (free resources).
921 * @param cls the socket
924 sock_destroy (void *cls)
926 GNUNET_CONNECTION_destroy (cls);
931 * Add a TCP socket-based connection to the set of handles managed by
932 * this server. Use this function for outgoing (P2P) connections that
933 * we initiated (and where this server should process incoming
936 * @param server the server to use
937 * @param connection the connection to manage (client must
938 * stop using this connection from now on)
939 * @return the client handle (client should call
940 * "client_drop" on the return value eventually)
942 struct GNUNET_SERVER_Client *
943 GNUNET_SERVER_connect_socket (struct
946 struct GNUNET_CONNECTION_Handle *connection)
948 struct GNUNET_SERVER_Client *client;
950 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
951 client->client_closure = connection;
952 client->receive = &sock_receive;
953 client->receive_cancel = &sock_receive_cancel;
954 client->notify_transmit_ready = &sock_notify_transmit_ready;
955 client->notify_transmit_ready_cancel = &sock_notify_transmit_ready_cancel;
956 client->check = &sock_check;
957 client->destroy = &sock_destroy;
958 client->reference_count = 1;
959 add_client (server, client);
965 * Add an arbitrary connection to the set of handles managed by this
966 * server. This can be used if a sending and receiving does not
967 * really go over the network (internal transmission) or for servers
970 * @param server the server to use
971 * @param chandle opaque handle for the connection
972 * @param creceive receive function for the connection
973 * @param ccancel cancel receive function for the connection
974 * @param cnotify transmit notification function for the connection
975 * @param cnotify_cancel transmit notification cancellation function for the connection
976 * @param ccheck function to test if the connection is still up
977 * @param cdestroy function to close and free the connection
978 * @return the client handle (client should call
979 * "client_drop" on the return value eventually)
981 struct GNUNET_SERVER_Client *
982 GNUNET_SERVER_connect_callback (struct
986 GNUNET_SERVER_ReceiveCallback
988 GNUNET_SERVER_ReceiveCancelCallback
990 GNUNET_SERVER_TransmitReadyCallback
992 GNUNET_SERVER_TransmitReadyCancelCallback
994 GNUNET_SERVER_CheckCallback
996 GNUNET_SERVER_DestroyCallback cdestroy)
998 struct GNUNET_SERVER_Client *client;
1000 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
1001 client->client_closure = chandle;
1002 client->receive = creceive;
1003 client->receive_cancel = ccancel;
1004 client->notify_transmit_ready = cnotify;
1005 client->notify_transmit_ready_cancel = cnotify_cancel;
1006 client->check = ccheck;
1007 client->destroy = cdestroy;
1008 client->reference_count = 1;
1009 add_client (server, client);
1015 * Notify the server that the given client handle should
1016 * be kept (keeps the connection up if possible, increments
1017 * the internal reference counter).
1019 * @param client the client to keep
1022 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1024 client->reference_count++;
1029 * Notify the server that the given client handle is no
1030 * longer required. Decrements the reference counter. If
1031 * that counter reaches zero an inactive connection maybe
1034 * @param client the client to drop
1037 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1039 GNUNET_assert (client->reference_count > 0);
1040 client->reference_count--;
1041 if ((client->server == NULL) && (client->reference_count == 0))
1042 shutdown_incoming_processing (client);
1047 * Obtain the network address of the other party.
1049 * @param client the client to get the address for
1050 * @param addr where to store the address
1051 * @param addrlen where to store the length of the address
1052 * @return GNUNET_OK on success
1055 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1056 void **addr, size_t * addrlen)
1058 if (client->receive != &sock_receive)
1059 return GNUNET_SYSERR; /* not a network client */
1060 return GNUNET_CONNECTION_get_address (client->client_closure,
1066 * Ask the server to notify us whenever a client disconnects.
1067 * This function is called whenever the actual network connection
1068 * is closed; the reference count may be zero or larger than zero
1071 * @param server the server manageing the clients
1072 * @param callback function to call on disconnect
1073 * @param callback_cls closure for callback
1076 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1077 GNUNET_SERVER_DisconnectCallback callback,
1080 struct NotifyList *n;
1082 n = GNUNET_malloc (sizeof (struct NotifyList));
1083 n->callback = callback;
1084 n->callback_cls = callback_cls;
1085 n->next = server->disconnect_notify_list;
1086 server->disconnect_notify_list = n;
1091 * Ask the server to disconnect from the given client.
1092 * This is the same as returning GNUNET_SYSERR from a message
1093 * handler, except that it allows dropping of a client even
1094 * when not handling a message from that client.
1096 * @param client the client to disconnect from
1099 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1101 if (client->server == NULL)
1102 return; /* already disconnected */
1103 client->receive_cancel (client->client_closure);
1104 shutdown_incoming_processing (client);
1109 * Notify us when the server has enough space to transmit
1110 * a message of the given size to the given client.
1112 * @param client client to transmit message to
1113 * @param size requested amount of buffer space
1114 * @param timeout after how long should we give up (and call
1115 * notify with buf NULL and size 0)?
1116 * @param callback function to call when space is available
1117 * @param callback_cls closure for callback
1118 * @return non-NULL if the notify callback was queued; can be used
1119 * to cancel the request using
1120 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1121 * NULL if we are already going to notify someone else (busy)
1123 struct GNUNET_CONNECTION_TransmitHandle *
1124 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1126 struct GNUNET_TIME_Relative timeout,
1127 GNUNET_CONNECTION_TransmitReadyNotify
1128 callback, void *callback_cls)
1130 return client->notify_transmit_ready (client->client_closure,
1132 timeout, callback, callback_cls);
1137 * Resume receiving from this client, we are done processing the
1138 * current request. This function must be called from within each
1139 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1141 * @param client client we were processing a message of
1142 * @param success GNUNET_OK to keep the connection open and
1143 * continue to receive
1144 * GNUNET_SYSERR to close the connection (signal
1148 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1154 GNUNET_assert (client->suspended > 0);
1155 client->suspended--;
1156 if (success != GNUNET_OK)
1157 client->shutdown_now = GNUNET_YES;
1158 if (client->suspended > 0)
1160 if (client->in_process_client_buffer == GNUNET_YES)
1162 if (client->side_buf_size > 0)
1164 /* resume processing from side-buf */
1165 sb = client->side_buf;
1166 client->side_buf = NULL;
1167 /* this will also resume the receive job */
1168 if (GNUNET_YES != client->shutdown_now)
1169 process_incoming (client, sb, client->side_buf_size, NULL, 0, 0);
1171 shutdown_incoming_processing (client);
1172 /* finally, free the side-buf */
1176 /* resume receive job */
1177 if (GNUNET_YES != client->shutdown_now)
1179 GNUNET_SCHEDULER_add_continuation (client->server->sched,
1181 &restart_processing,
1183 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1186 shutdown_incoming_processing (client);
1190 /* end of server.c */