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_NO
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 * Socket used to listen for new connections. Set to
127 * "-1" by GNUNET_SERVER_destroy to initiate shutdown.
129 struct GNUNET_NETWORK_Handle *listen_socket;
132 * Task scheduled to do the listening.
134 GNUNET_SCHEDULER_TaskIdentifier listen_task;
137 * Do we ignore messages of types that we do not
138 * understand or do we require that a handler
139 * is found (and if not kill the connection)?
147 * @brief handle for a client of the server
149 struct GNUNET_SERVER_Client
153 * Size of the buffer for incoming data. Should be
154 * first so we get nice alignment.
156 char incoming_buffer[GNUNET_SERVER_MAX_MESSAGE_SIZE];
159 * This is a linked list.
161 struct GNUNET_SERVER_Client *next;
164 * Server that this client belongs to.
166 struct GNUNET_SERVER_Handle *server;
169 * Client closure for callbacks.
171 void *client_closure;
174 * Callback to receive from client.
176 GNUNET_SERVER_ReceiveCallback receive;
179 * Callback to cancel receive from client.
181 GNUNET_SERVER_ReceiveCancelCallback receive_cancel;
184 * Callback to ask about transmit-ready notification.
186 GNUNET_SERVER_TransmitReadyCallback notify_transmit_ready;
189 * Callback to ask about transmit-ready notification.
191 GNUNET_SERVER_TransmitReadyCancelCallback notify_transmit_ready_cancel;
194 * Callback to check if client is still valid.
196 GNUNET_SERVER_CheckCallback check;
199 * Callback to destroy client.
201 GNUNET_SERVER_DestroyCallback destroy;
204 * Side-buffer for incoming data used when processing
210 * Number of bytes in the side buffer.
212 size_t side_buf_size;
215 * Last activity on this socket (used to time it out
216 * if reference_count == 0).
218 struct GNUNET_TIME_Absolute last_activity;
221 * How many bytes in the "incoming_buffer" are currently
222 * valid? (starting at offset 0).
227 * Number of external entities with a reference to
228 * this client object.
230 unsigned int reference_count;
233 * Was processing if incoming messages suspended while
234 * we were still processing data already received?
235 * This is a counter saying how often processing was
236 * suspended (once per handler invoked).
238 unsigned int suspended;
241 * Are we currently in the "process_client_buffer" function (and
242 * will hence restart the receive job on exit if suspended == 0 once
243 * we are done?). If this is set, then "receive_done" will
244 * essentially only decrement suspended; if this is not set, then
245 * "receive_done" may need to restart the receive process (either
246 * from the side-buffer or via select/recv).
248 int in_process_client_buffer;
251 * We're about to close down this client due to some serious
260 * Scheduler says our listen socket is ready.
264 process_listen_socket (void *cls,
265 const struct GNUNET_SCHEDULER_TaskContext *tc)
267 struct GNUNET_SERVER_Handle *server = cls;
268 struct GNUNET_CONNECTION_Handle *sock;
269 struct GNUNET_SERVER_Client *client;
270 struct GNUNET_NETWORK_FDSet *r;
272 server->listen_task = GNUNET_SCHEDULER_NO_TASK;
273 r = GNUNET_NETWORK_fdset_create ();
274 GNUNET_NETWORK_fdset_set (r, server->listen_socket);
275 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
277 server->listen_task = GNUNET_SCHEDULER_add_select (server->sched,
278 GNUNET_SCHEDULER_PRIORITY_HIGH,
279 GNUNET_SCHEDULER_NO_TASK,
280 GNUNET_TIME_UNIT_FOREVER_REL,
281 r, NULL, &process_listen_socket,
283 GNUNET_NETWORK_fdset_destroy (r);
284 return; /* ignore shutdown, someone else will take care of it! */
286 GNUNET_assert (GNUNET_NETWORK_fdset_isset
287 (tc->read_ready, server->listen_socket));
289 GNUNET_CONNECTION_create_from_accept (tc->sched, server->access,
291 server->listen_socket,
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "Server accepted incoming connection.\n");
299 client = GNUNET_SERVER_connect_socket (server, sock);
300 /* decrement reference count, we don't keep "client" alive */
301 GNUNET_SERVER_client_drop (client);
303 /* listen for more! */
304 server->listen_task = GNUNET_SCHEDULER_add_select (server->sched,
305 GNUNET_SCHEDULER_PRIORITY_HIGH,
306 GNUNET_SCHEDULER_NO_TASK,
307 GNUNET_TIME_UNIT_FOREVER_REL,
308 r, NULL, &process_listen_socket, server);
309 GNUNET_NETWORK_fdset_destroy (r);
314 * Create and initialize a listen socket for the server.
316 * @return NULL on error, otherwise the listen socket
318 static struct GNUNET_NETWORK_Handle *
319 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
321 const static int on = 1;
322 struct GNUNET_NETWORK_Handle *sock;
325 switch (serverAddr->sa_family)
328 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
331 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
337 sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
340 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
343 if (GNUNET_NETWORK_socket_setsockopt
344 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
345 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)
350 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 ("`%s' failed for port %d. Is the service already running?\n"),
355 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
358 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
360 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
361 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Server starts to listen on port %u.\n", port);
373 * Create a new server.
375 * @param sched scheduler to use
376 * @param access function for access control
377 * @param access_cls closure for access
378 * @param serverAddr address to listen on (including port), use NULL
379 * for internal server (no listening)
380 * @param socklen length of serverAddr
381 * @param maxbuf maximum write buffer size for accepted sockets
382 * @param idle_timeout after how long should we timeout idle connections?
383 * @param require_found if YES, connections sending messages of unknown type
385 * @return handle for the new server, NULL on error
386 * (typically, "port" already in use)
388 struct GNUNET_SERVER_Handle *
389 GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched,
390 GNUNET_CONNECTION_AccessCheck access,
392 const struct sockaddr *serverAddr,
395 struct GNUNET_TIME_Relative
396 idle_timeout, int require_found)
398 struct GNUNET_SERVER_Handle *ret;
399 struct GNUNET_NETWORK_Handle *lsock;
400 struct GNUNET_NETWORK_FDSet *r;
403 if (serverAddr != NULL)
405 lsock = open_listen_socket (serverAddr, socklen);
409 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
411 ret->maxbuf = maxbuf;
412 ret->idle_timeout = idle_timeout;
413 ret->listen_socket = lsock;
414 ret->access = access;
415 ret->access_cls = access_cls;
416 ret->require_found = require_found;
419 r = GNUNET_NETWORK_fdset_create ();
420 GNUNET_NETWORK_fdset_set (r, ret->listen_socket);
421 ret->listen_task = GNUNET_SCHEDULER_add_select (sched,
422 GNUNET_SCHEDULER_PRIORITY_HIGH,
423 GNUNET_SCHEDULER_NO_TASK,
424 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
425 &process_listen_socket, ret);
426 GNUNET_NETWORK_fdset_destroy (r);
433 * Free resources held by this server.
436 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
438 struct GNUNET_SERVER_Client *pos;
439 struct HandlerList *hpos;
440 struct NotifyList *npos;
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
445 if (GNUNET_SCHEDULER_NO_TASK != s->listen_task)
447 GNUNET_SCHEDULER_cancel (s->sched,
449 s->listen_task = GNUNET_SCHEDULER_NO_TASK;
451 GNUNET_break (GNUNET_OK ==
452 GNUNET_NETWORK_socket_close (s->listen_socket));
453 s->listen_socket = NULL;
454 while (s->clients != NULL)
457 s->clients = pos->next;
460 while (NULL != (hpos = s->handlers))
462 s->handlers = hpos->next;
465 while (NULL != (npos = s->disconnect_notify_list))
467 s->disconnect_notify_list = npos->next;
476 * Add additional handlers to an existing server.
478 * @param server the server to add handlers to
479 * @param handlers array of message handlers for
480 * incoming messages; the last entry must
481 * have "NULL" for the "callback"; multiple
482 * entries for the same type are allowed,
483 * they will be called in order of occurence.
484 * These handlers can be removed later;
485 * the handlers array must exist until removed
486 * (or server is destroyed).
489 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
490 const struct GNUNET_SERVER_MessageHandler
493 struct HandlerList *p;
495 p = GNUNET_malloc (sizeof (struct HandlerList));
496 p->handlers = handlers;
497 p->next = server->handlers;
498 server->handlers = p;
503 * Inject a message into the server, pretend it came
504 * from the specified client. Delivery of the message
505 * will happen instantly (if a handler is installed;
506 * otherwise the call does nothing).
508 * @param server the server receiving the message
509 * @param sender the "pretended" sender of the message
511 * @param message message to transmit
512 * @return GNUNET_OK if the message was OK and the
513 * connection can stay open
514 * GNUNET_SYSERR if the connection to the
515 * client should be shut down
518 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
519 struct GNUNET_SERVER_Client *sender,
520 const struct GNUNET_MessageHeader *message)
522 struct HandlerList *pos;
523 const struct GNUNET_SERVER_MessageHandler *mh;
529 type = ntohs (message->type);
530 size = ntohs (message->size);
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Server schedules transmission of %u-byte message of type %u to client.\n",
536 pos = server->handlers;
541 while (pos->handlers[i].callback != NULL)
543 mh = &pos->handlers[i];
544 if (mh->type == type)
546 if ((mh->expected_size != 0) && (mh->expected_size != size))
549 return GNUNET_SYSERR;
553 mh->callback (mh->callback_cls, sender, message);
560 if (found == GNUNET_NO)
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
563 _("Received message of unknown type %d\n"), type);
564 if (server->require_found == GNUNET_YES)
565 return GNUNET_SYSERR;
572 * We're finished with this client and especially its input
573 * processing. If the RC is zero, free all resources otherwise wait
574 * until RC hits zero to do so.
577 shutdown_incoming_processing (struct GNUNET_SERVER_Client *client)
579 struct GNUNET_SERVER_Client *prev;
580 struct GNUNET_SERVER_Client *pos;
581 struct GNUNET_SERVER_Handle *server;
582 struct NotifyList *n;
585 rc = client->reference_count;
586 if (client->server != NULL)
588 server = client->server;
589 client->server = NULL;
591 pos = server->clients;
592 while ((pos != NULL) && (pos != client))
597 GNUNET_assert (pos != NULL);
599 server->clients = pos->next;
601 prev->next = pos->next;
602 n = server->disconnect_notify_list;
605 n->callback (n->callback_cls, client);
609 /* wait for RC to hit zero, then free */
612 client->destroy (client->client_closure);
613 GNUNET_free (client);
618 * Go over the contents of the client buffer; as long as full messages
619 * are available, pass them on for processing. Update the buffer
620 * accordingly. Handles fatal errors by shutting down the connection.
622 * @param client identifies which client receive buffer to process
625 process_client_buffer (struct GNUNET_SERVER_Client *client)
627 struct GNUNET_SERVER_Handle *server;
628 const struct GNUNET_MessageHeader *hdr;
631 client->in_process_client_buffer = GNUNET_YES;
632 server = client->server;
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Private buffer contains %u bytes; client is %s and we are %s\n",
637 client->suspended ? "suspended" : "up",
638 client->shutdown_now ? "in shutdown" : "running");
640 while ((client->receive_pos >= sizeof (struct GNUNET_MessageHeader)) &&
641 (0 == client->suspended) && (GNUNET_YES != client->shutdown_now))
643 hdr = (const struct GNUNET_MessageHeader *) &client->incoming_buffer;
644 msize = ntohs (hdr->size);
645 if (msize > client->receive_pos)
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649 "Total message size is %u, we only have %u bytes; need more data\n",
650 msize, client->receive_pos);
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Passing %u bytes to callback for processing\n", msize);
658 if ((msize < sizeof (struct GNUNET_MessageHeader)) ||
659 (GNUNET_OK != GNUNET_SERVER_inject (server, client, hdr)))
661 client->in_process_client_buffer = GNUNET_NO;
662 shutdown_incoming_processing (client);
665 /* FIXME: this is highly inefficient; we should
666 try to avoid this if the new base address is
667 already nicely aligned. See old handler code... */
668 memmove (client->incoming_buffer,
669 &client->incoming_buffer[msize], client->receive_pos - msize);
670 client->receive_pos -= msize;
672 client->in_process_client_buffer = GNUNET_NO;
673 if (GNUNET_YES == client->shutdown_now)
674 shutdown_incoming_processing (client);
679 * We are receiving an incoming message. Process it.
681 * @param cls our closure (handle for the client)
682 * @param buf buffer with data received from network
683 * @param available number of bytes available in buf
684 * @param addr address of the sender
685 * @param addrlen length of addr
686 * @param errCode code indicating errors receiving, 0 for success
689 process_incoming (void *cls,
692 const struct sockaddr *addr, socklen_t addrlen, int errCode)
694 struct GNUNET_SERVER_Client *client = cls;
695 struct GNUNET_SERVER_Handle *server = client->server;
696 const char *cbuf = buf;
703 (client->shutdown_now == GNUNET_YES) ||
704 (GNUNET_YES != client->check (client->client_closure)))
706 /* other side closed connection, error connecting, etc. */
707 shutdown_incoming_processing (client);
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Server receives %u bytes from `%s'.\n",
713 available, GNUNET_a2s (addr, addrlen));
715 GNUNET_SERVER_client_keep (client);
716 client->last_activity = GNUNET_TIME_absolute_get ();
717 /* process data (if available) */
718 while (available > 0)
721 if (maxcpy > sizeof (client->incoming_buffer) - client->receive_pos)
722 maxcpy = sizeof (client->incoming_buffer) - client->receive_pos;
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
725 "Can copy %u bytes to private buffer\n", maxcpy);
727 memcpy (&client->incoming_buffer[client->receive_pos], cbuf, maxcpy);
728 client->receive_pos += maxcpy;
731 if (0 < client->suspended)
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "Client has suspended processing; copying %u bytes to side buffer to be used later.\n",
740 GNUNET_assert (client->side_buf_size == 0);
741 GNUNET_assert (client->side_buf == NULL);
742 client->side_buf_size = available;
743 client->side_buf = GNUNET_malloc (available);
744 memcpy (client->side_buf, cbuf, available);
747 break; /* do not run next client iteration! */
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751 "Now processing messages in private buffer\n");
753 process_client_buffer (client);
755 GNUNET_assert (available == 0);
756 if ((client->suspended == 0) &&
757 (GNUNET_YES != client->shutdown_now) && (client->server != NULL))
759 /* Finally, keep receiving! */
760 client->receive (client->client_closure,
761 GNUNET_SERVER_MAX_MESSAGE_SIZE,
762 server->idle_timeout, &process_incoming, client);
764 if (GNUNET_YES == client->shutdown_now)
765 shutdown_incoming_processing (client);
766 GNUNET_SERVER_client_drop (client);
774 restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
776 struct GNUNET_SERVER_Client *client = cls;
778 process_client_buffer (client);
779 if (0 == client->suspended)
780 client->receive (client->client_closure,
781 GNUNET_SERVER_MAX_MESSAGE_SIZE,
782 client->server->idle_timeout, &process_incoming, client);
787 * Add a client to the set of our clients and
791 add_client (struct GNUNET_SERVER_Handle *server,
792 struct GNUNET_SERVER_Client *client)
794 client->server = server;
795 client->last_activity = GNUNET_TIME_absolute_get ();
796 client->next = server->clients;
797 server->clients = client;
798 client->receive (client->client_closure,
799 GNUNET_SERVER_MAX_MESSAGE_SIZE,
800 server->idle_timeout, &process_incoming, client);
805 * Create a request for receiving data from a socket.
807 * @param cls identifies the socket to receive from
808 * @param max how much data to read at most
809 * @param timeout when should this operation time out
810 * @param receiver function to call for processing
811 * @param receiver_cls closure for receiver
814 sock_receive (void *cls,
816 struct GNUNET_TIME_Relative timeout,
817 GNUNET_CONNECTION_Receiver receiver, void *receiver_cls)
819 GNUNET_CONNECTION_receive (cls, max, timeout, receiver, receiver_cls);
824 * Wrapper to cancel receiving from a socket.
826 * @param cls handle to the GNUNET_CONNECTION_Handle to cancel
829 sock_receive_cancel (void *cls)
831 GNUNET_CONNECTION_receive_cancel (cls);
839 sock_notify_transmit_ready (void *cls,
841 struct GNUNET_TIME_Relative timeout,
842 GNUNET_CONNECTION_TransmitReadyNotify notify,
845 return GNUNET_CONNECTION_notify_transmit_ready (cls, size, timeout, notify,
854 sock_notify_transmit_ready_cancel (void *cls, void *h)
856 GNUNET_CONNECTION_notify_transmit_ready_cancel (h);
861 * Check if socket is still valid (no fatal errors have happened so far).
863 * @param cls the socket
864 * @return GNUNET_YES if valid, GNUNET_NO otherwise
867 sock_check (void *cls)
869 return GNUNET_CONNECTION_check (cls);
874 * Destroy this socket (free resources).
876 * @param cls the socket
879 sock_destroy (void *cls)
881 GNUNET_CONNECTION_destroy (cls);
886 * Add a TCP socket-based connection to the set of handles managed by
887 * this server. Use this function for outgoing (P2P) connections that
888 * we initiated (and where this server should process incoming
891 * @param server the server to use
892 * @param connection the connection to manage (client must
893 * stop using this connection from now on)
894 * @return the client handle (client should call
895 * "client_drop" on the return value eventually)
897 struct GNUNET_SERVER_Client *
898 GNUNET_SERVER_connect_socket (struct
901 struct GNUNET_CONNECTION_Handle *connection)
903 struct GNUNET_SERVER_Client *client;
905 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
906 client->client_closure = connection;
907 client->receive = &sock_receive;
908 client->receive_cancel = &sock_receive_cancel;
909 client->notify_transmit_ready = &sock_notify_transmit_ready;
910 client->notify_transmit_ready_cancel = &sock_notify_transmit_ready_cancel;
911 client->check = &sock_check;
912 client->destroy = &sock_destroy;
913 client->reference_count = 1;
914 add_client (server, client);
920 * Add an arbitrary connection to the set of handles managed by this
921 * server. This can be used if a sending and receiving does not
922 * really go over the network (internal transmission) or for servers
925 * @param server the server to use
926 * @param chandle opaque handle for the connection
927 * @param creceive receive function for the connection
928 * @param ccancel cancel receive function for the connection
929 * @param cnotify transmit notification function for the connection
930 * @param cnotify_cancel transmit notification cancellation function for the connection
931 * @param ccheck function to test if the connection is still up
932 * @param cdestroy function to close and free the connection
933 * @return the client handle (client should call
934 * "client_drop" on the return value eventually)
936 struct GNUNET_SERVER_Client *
937 GNUNET_SERVER_connect_callback (struct
941 GNUNET_SERVER_ReceiveCallback
943 GNUNET_SERVER_ReceiveCancelCallback
945 GNUNET_SERVER_TransmitReadyCallback
947 GNUNET_SERVER_TransmitReadyCancelCallback
949 GNUNET_SERVER_CheckCallback
951 GNUNET_SERVER_DestroyCallback cdestroy)
953 struct GNUNET_SERVER_Client *client;
955 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
956 client->client_closure = chandle;
957 client->receive = creceive;
958 client->receive_cancel = ccancel;
959 client->notify_transmit_ready = cnotify;
960 client->notify_transmit_ready_cancel = cnotify_cancel;
961 client->check = ccheck;
962 client->destroy = cdestroy;
963 client->reference_count = 1;
964 add_client (server, client);
970 * Notify the server that the given client handle should
971 * be kept (keeps the connection up if possible, increments
972 * the internal reference counter).
974 * @param client the client to keep
977 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
979 client->reference_count++;
984 * Notify the server that the given client handle is no
985 * longer required. Decrements the reference counter. If
986 * that counter reaches zero an inactive connection maybe
989 * @param client the client to drop
992 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
994 GNUNET_assert (client->reference_count > 0);
995 client->reference_count--;
996 if ((client->server == NULL) && (client->reference_count == 0))
997 shutdown_incoming_processing (client);
1002 * Obtain the network address of the other party.
1004 * @param client the client to get the address for
1005 * @param addr where to store the address
1006 * @param addrlen where to store the length of the address
1007 * @return GNUNET_OK on success
1010 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1011 void **addr, size_t * addrlen)
1013 if (client->receive != &sock_receive)
1014 return GNUNET_SYSERR; /* not a network client */
1015 return GNUNET_CONNECTION_get_address (client->client_closure,
1021 * Ask the server to notify us whenever a client disconnects.
1022 * This function is called whenever the actual network connection
1023 * is closed; the reference count may be zero or larger than zero
1026 * @param server the server manageing the clients
1027 * @param callback function to call on disconnect
1028 * @param callback_cls closure for callback
1031 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1032 GNUNET_SERVER_DisconnectCallback callback,
1035 struct NotifyList *n;
1037 n = GNUNET_malloc (sizeof (struct NotifyList));
1038 n->callback = callback;
1039 n->callback_cls = callback_cls;
1040 n->next = server->disconnect_notify_list;
1041 server->disconnect_notify_list = n;
1046 * Ask the server to disconnect from the given client.
1047 * This is the same as returning GNUNET_SYSERR from a message
1048 * handler, except that it allows dropping of a client even
1049 * when not handling a message from that client.
1051 * @param client the client to disconnect from
1054 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1056 if (client->server == NULL)
1057 return; /* already disconnected */
1058 client->receive_cancel (client->client_closure);
1059 shutdown_incoming_processing (client);
1064 * Notify us when the server has enough space to transmit
1065 * a message of the given size to the given client.
1067 * @param client client to transmit message to
1068 * @param size requested amount of buffer space
1069 * @param timeout after how long should we give up (and call
1070 * notify with buf NULL and size 0)?
1071 * @param callback function to call when space is available
1072 * @param callback_cls closure for callback
1073 * @return non-NULL if the notify callback was queued; can be used
1074 * to cancel the request using
1075 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1076 * NULL if we are already going to notify someone else (busy)
1078 struct GNUNET_CONNECTION_TransmitHandle *
1079 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1081 struct GNUNET_TIME_Relative timeout,
1082 GNUNET_CONNECTION_TransmitReadyNotify
1083 callback, void *callback_cls)
1085 return client->notify_transmit_ready (client->client_closure,
1087 timeout, callback, callback_cls);
1092 * Resume receiving from this client, we are done processing the
1093 * current request. This function must be called from within each
1094 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1096 * @param client client we were processing a message of
1097 * @param success GNUNET_OK to keep the connection open and
1098 * continue to receive
1099 * GNUNET_SYSERR to close the connection (signal
1103 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1109 GNUNET_assert (client->suspended > 0);
1110 client->suspended--;
1111 if (success != GNUNET_OK)
1112 client->shutdown_now = GNUNET_YES;
1113 if (client->suspended > 0)
1115 if (client->in_process_client_buffer == GNUNET_YES)
1117 if (client->side_buf_size > 0)
1119 /* resume processing from side-buf */
1120 sb = client->side_buf;
1121 client->side_buf = NULL;
1122 /* this will also resume the receive job */
1123 if (GNUNET_YES != client->shutdown_now)
1124 process_incoming (client, sb, client->side_buf_size, NULL, 0, 0);
1126 shutdown_incoming_processing (client);
1127 /* finally, free the side-buf */
1131 /* resume receive job */
1132 if (GNUNET_YES != client->shutdown_now)
1134 GNUNET_SCHEDULER_add_continuation (client->server->sched,
1135 &restart_processing,
1137 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1140 shutdown_incoming_processing (client);
1144 /* end of server.c */