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_NETWORK_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_Descriptor *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 * Current task identifier for the receive call
227 * (or GNUNET_SCHEDULER_NO_TASK for none).
229 GNUNET_SCHEDULER_TaskIdentifier my_receive;
232 * How many bytes in the "incoming_buffer" are currently
233 * valid? (starting at offset 0).
238 * Number of external entities with a reference to
239 * this client object.
241 unsigned int reference_count;
244 * Was processing if incoming messages suspended while
245 * we were still processing data already received?
246 * This is a counter saying how often processing was
247 * suspended (once per handler invoked).
249 unsigned int suspended;
252 * Are we currently in the "process_client_buffer" function (and
253 * will hence restart the receive job on exit if suspended == 0 once
254 * we are done?). If this is set, then "receive_done" will
255 * essentially only decrement suspended; if this is not set, then
256 * "receive_done" may need to restart the receive process (either
257 * from the side-buffer or via select/recv).
259 int in_process_client_buffer;
262 * We're about to close down this client due to some serious
271 * Server has been asked to shutdown, free resources.
274 destroy_server (struct GNUNET_SERVER_Handle *server)
276 struct GNUNET_SERVER_Client *pos;
277 struct HandlerList *hpos;
278 struct NotifyList *npos;
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282 "Server shutting down.\n");
284 GNUNET_assert (server->listen_socket == NULL);
285 GNUNET_break (GNUNET_YES == GNUNET_DISK_pipe_close (server->shutpipe));
286 while (server->clients != NULL)
288 pos = server->clients;
289 server->clients = pos->next;
292 while (NULL != (hpos = server->handlers))
294 server->handlers = hpos->next;
297 while (NULL != (npos = server->disconnect_notify_list))
299 server->disconnect_notify_list = npos->next;
302 GNUNET_free (server);
307 * Scheduler says our listen socket is ready.
311 process_listen_socket (void *cls,
312 const struct GNUNET_SCHEDULER_TaskContext *tc)
314 struct GNUNET_SERVER_Handle *server = cls;
315 struct GNUNET_CONNECTION_Handle *sock;
316 struct GNUNET_SERVER_Client *client;
317 struct GNUNET_NETWORK_FDSet *r;
318 const struct GNUNET_DISK_FileHandle *shutpipe;
320 if ((server->do_shutdown) ||
321 ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0))
323 /* shutdown was initiated */
324 GNUNET_assert (server->listen_socket != NULL);
325 GNUNET_break (0 == GNUNET_NETWORK_socket_close (server->listen_socket));
326 server->listen_socket = NULL;
327 if (server->do_shutdown)
328 destroy_server (server);
331 shutpipe = GNUNET_DISK_pipe_handle (server->shutpipe, 0);
332 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_socket));
333 GNUNET_assert (!GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, shutpipe));
334 sock = GNUNET_CONNECTION_create_from_accept (tc->sched,
337 server->listen_socket,
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Server accepted incoming connection.\n");
345 client = GNUNET_SERVER_connect_socket (server, sock);
346 /* decrement reference count, we don't keep "client" alive */
347 GNUNET_SERVER_client_drop (client);
349 /* listen for more! */
350 r = GNUNET_NETWORK_fdset_create ();
351 GNUNET_NETWORK_fdset_set (r, server->listen_socket);
352 GNUNET_NETWORK_fdset_handle_set (r, shutpipe);
353 GNUNET_SCHEDULER_add_select (server->sched,
355 GNUNET_SCHEDULER_PRIORITY_HIGH,
356 GNUNET_SCHEDULER_NO_TASK,
357 GNUNET_TIME_UNIT_FOREVER_REL,
359 &process_listen_socket, server);
360 GNUNET_NETWORK_fdset_destroy (r);
365 * Create and initialize a listen socket for the server.
367 * @return NULL on error, otherwise the listen socket
369 static struct GNUNET_NETWORK_Descriptor *
370 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
372 const static int on = 1;
373 struct GNUNET_NETWORK_Descriptor *sock;
376 switch (serverAddr->sa_family)
379 port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
382 port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
388 sock = GNUNET_NETWORK_socket_socket (serverAddr->sa_family, SOCK_STREAM, 0);
391 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
395 if (GNUNET_OK != GNUNET_NETWORK_socket_set_inheritable (sock))
396 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
399 if (GNUNET_NETWORK_socket_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0)
400 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
402 /* bind the socket */
403 if (GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen) < 0)
405 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
408 ("`%s' failed for port %d. Is the service already running?\n"),
410 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock));
413 if (0 != GNUNET_NETWORK_socket_listen (sock, 5))
415 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
416 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sock));
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Server starts to listen on port %u.\n",
429 * Create a new server.
431 * @param sched scheduler to use
432 * @param access function for access control
433 * @param access_cls closure for access
434 * @param serverAddr address to listen on (including port), use NULL
435 * for internal server (no listening)
436 * @param socklen length of serverAddr
437 * @param maxbuf maximum write buffer size for accepted sockets
438 * @param idle_timeout after how long should we timeout idle connections?
439 * @param require_found if YES, connections sending messages of unknown type
441 * @return handle for the new server, NULL on error
442 * (typically, "port" already in use)
444 struct GNUNET_SERVER_Handle *
445 GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched,
446 GNUNET_NETWORK_AccessCheck access,
448 const struct sockaddr *serverAddr,
451 struct GNUNET_TIME_Relative
452 idle_timeout, int require_found)
454 struct GNUNET_SERVER_Handle *ret;
455 struct GNUNET_NETWORK_Descriptor *lsock;
456 struct GNUNET_NETWORK_FDSet *r;
458 lsock = NULL; // FIXME NILS: this was -2, does that have a special meaning?
459 if (serverAddr != NULL)
461 lsock = open_listen_socket (serverAddr, socklen);
465 ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
466 ret->shutpipe = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileDescriptor *[2]));
467 if ((ret->shutpipe = GNUNET_DISK_pipe (GNUNET_NO)) == NULL)
469 GNUNET_break (0 == GNUNET_NETWORK_socket_close (lsock));
470 GNUNET_free (ret->shutpipe);
475 ret->maxbuf = maxbuf;
476 ret->idle_timeout = idle_timeout;
477 ret->listen_socket = lsock;
478 ret->access = access;
479 ret->access_cls = access_cls;
480 ret->require_found = require_found;
483 r = GNUNET_NETWORK_fdset_create ();
484 GNUNET_NETWORK_fdset_set (r, ret->listen_socket);
485 GNUNET_NETWORK_fdset_handle_set (r, GNUNET_DISK_pipe_handle (ret->shutpipe, 0));
486 GNUNET_SCHEDULER_add_select (sched,
488 GNUNET_SCHEDULER_PRIORITY_HIGH,
489 GNUNET_SCHEDULER_NO_TASK,
490 GNUNET_TIME_UNIT_FOREVER_REL,
492 NULL, &process_listen_socket, ret);
493 GNUNET_NETWORK_fdset_destroy (r);
500 * Free resources held by this server.
503 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
507 GNUNET_assert (s->do_shutdown == GNUNET_NO);
508 s->do_shutdown = GNUNET_YES;
509 if (s->listen_socket == NULL)
512 GNUNET_break (1 == GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (s->shutpipe, 1), &c, 1));
517 * Add additional handlers to an existing server.
519 * @param server the server to add handlers to
520 * @param handlers array of message handlers for
521 * incoming messages; the last entry must
522 * have "NULL" for the "callback"; multiple
523 * entries for the same type are allowed,
524 * they will be called in order of occurence.
525 * These handlers can be removed later;
526 * the handlers array must exist until removed
527 * (or server is destroyed).
530 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
531 const struct GNUNET_SERVER_MessageHandler
534 struct HandlerList *p;
536 p = GNUNET_malloc (sizeof (struct HandlerList));
537 p->handlers = handlers;
538 p->next = server->handlers;
539 server->handlers = p;
544 * Inject a message into the server, pretend it came
545 * from the specified client. Delivery of the message
546 * will happen instantly (if a handler is installed;
547 * otherwise the call does nothing).
549 * @param server the server receiving the message
550 * @param sender the "pretended" sender of the message
552 * @param message message to transmit
553 * @return GNUNET_OK if the message was OK and the
554 * connection can stay open
555 * GNUNET_SYSERR if the connection to the
556 * client should be shut down
559 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
560 struct GNUNET_SERVER_Client *sender,
561 const struct GNUNET_MessageHeader *message)
563 struct HandlerList *pos;
564 const struct GNUNET_SERVER_MessageHandler *mh;
570 type = ntohs (message->type);
571 size = ntohs (message->size);
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "Server schedules transmission of %u-byte message of type %u to client.\n",
578 pos = server->handlers;
583 while (pos->handlers[i].callback != NULL)
585 mh = &pos->handlers[i];
586 if (mh->type == type)
588 if ((mh->expected_size != 0) && (mh->expected_size != size))
591 return GNUNET_SYSERR;
595 mh->callback (mh->callback_cls, sender, message);
602 if (found == GNUNET_NO)
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
605 _("Received message of unknown type %d\n"), type);
606 if (server->require_found == GNUNET_YES)
607 return GNUNET_SYSERR;
614 * We're finished with this client and especially its input
615 * processing. If the RC is zero, free all resources otherwise wait
616 * until RC hits zero to do so.
619 shutdown_incoming_processing (struct GNUNET_SERVER_Client *client)
621 struct GNUNET_SERVER_Client *prev;
622 struct GNUNET_SERVER_Client *pos;
623 struct GNUNET_SERVER_Handle *server;
624 struct NotifyList *n;
627 GNUNET_assert (client->my_receive == GNUNET_SCHEDULER_NO_TASK);
628 rc = client->reference_count;
629 if (client->server != NULL)
631 server = client->server;
632 client->server = NULL;
634 pos = server->clients;
635 while ((pos != NULL) && (pos != client))
640 GNUNET_assert (pos != NULL);
642 server->clients = pos->next;
644 prev->next = pos->next;
645 n = server->disconnect_notify_list;
648 n->callback (n->callback_cls, client);
652 /* wait for RC to hit zero, then free */
655 client->destroy (client->client_closure);
656 GNUNET_free (client);
661 * Go over the contents of the client buffer; as long as full messages
662 * are available, pass them on for processing. Update the buffer
663 * accordingly. Handles fatal errors by shutting down the connection.
665 * @param client identifies which client receive buffer to process
668 process_client_buffer (struct GNUNET_SERVER_Client *client)
670 struct GNUNET_SERVER_Handle *server;
671 const struct GNUNET_MessageHeader *hdr;
674 client->in_process_client_buffer = GNUNET_YES;
675 server = client->server;
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "Private buffer contains %u bytes; client is %s and we are %s\n",
680 client->suspended ? "suspended" : "up",
681 client->shutdown_now ? "in shutdown" : "running");
683 while ((client->receive_pos >= sizeof (struct GNUNET_MessageHeader)) &&
684 (0 == client->suspended) && (GNUNET_YES != client->shutdown_now))
686 hdr = (const struct GNUNET_MessageHeader *) &client->incoming_buffer;
687 msize = ntohs (hdr->size);
688 if (msize > client->receive_pos)
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "Total message size is %u, we only have %u bytes; need more data\n",
694 client->receive_pos);
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "Passing %u bytes to callback for processing\n",
703 if ((msize < sizeof (struct GNUNET_MessageHeader)) ||
704 (GNUNET_OK != GNUNET_SERVER_inject (server, client, hdr)))
706 client->in_process_client_buffer = GNUNET_NO;
707 shutdown_incoming_processing (client);
710 /* FIXME: this is highly inefficient; we should
711 try to avoid this if the new base address is
712 already nicely aligned. See old handler code... */
713 memmove (client->incoming_buffer,
714 &client->incoming_buffer[msize], client->receive_pos - msize);
715 client->receive_pos -= msize;
717 client->in_process_client_buffer = GNUNET_NO;
718 if (GNUNET_YES == client->shutdown_now)
719 shutdown_incoming_processing (client);
724 * We are receiving an incoming message. Process it.
726 * @param cls our closure (handle for the client)
727 * @param buf buffer with data received from network
728 * @param available number of bytes available in buf
729 * @param addr address of the sender
730 * @param addrlen length of addr
731 * @param errCode code indicating errors receiving, 0 for success
734 process_incoming (void *cls,
737 const struct sockaddr *addr,
741 struct GNUNET_SERVER_Client *client = cls;
742 struct GNUNET_SERVER_Handle *server = client->server;
743 const char *cbuf = buf;
746 client->my_receive = GNUNET_SCHEDULER_NO_TASK;
751 (client->shutdown_now == GNUNET_YES) ||
752 (GNUNET_YES != client->check (client->client_closure)))
754 /* other side closed connection, error connecting, etc. */
755 shutdown_incoming_processing (client);
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Server receives %u bytes from `%s'.\n",
762 GNUNET_a2s(addr, addrlen));
764 GNUNET_SERVER_client_keep (client);
765 client->last_activity = GNUNET_TIME_absolute_get ();
766 /* process data (if available) */
767 while (available > 0)
770 if (maxcpy > sizeof (client->incoming_buffer) - client->receive_pos)
771 maxcpy = sizeof (client->incoming_buffer) - client->receive_pos;
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "Can copy %u bytes to private buffer\n",
777 memcpy (&client->incoming_buffer[client->receive_pos], cbuf, maxcpy);
778 client->receive_pos += maxcpy;
781 if (0 < client->suspended)
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787 "Client has suspended processing; copying %u bytes to side buffer to be used later.\n",
790 GNUNET_assert (client->side_buf_size == 0);
791 GNUNET_assert (client->side_buf == NULL);
792 client->side_buf_size = available;
793 client->side_buf = GNUNET_malloc (available);
794 memcpy (client->side_buf, cbuf, available);
797 break; /* do not run next client iteration! */
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Now processing messages in private buffer\n");
803 process_client_buffer (client);
805 GNUNET_assert (available == 0);
806 if ((client->suspended == 0) &&
807 (GNUNET_YES != client->shutdown_now) && (client->server != NULL))
809 /* Finally, keep receiving! */
810 client->my_receive = client->receive (client->client_closure,
811 GNUNET_SERVER_MAX_MESSAGE_SIZE,
812 server->idle_timeout,
813 &process_incoming, client);
815 if (GNUNET_YES == client->shutdown_now)
816 shutdown_incoming_processing (client);
817 GNUNET_SERVER_client_drop (client);
825 restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
827 struct GNUNET_SERVER_Client *client = cls;
829 process_client_buffer (client);
830 if (0 == client->suspended)
831 client->my_receive = client->receive (client->client_closure,
832 GNUNET_SERVER_MAX_MESSAGE_SIZE,
833 client->server->idle_timeout,
834 &process_incoming, client);
839 * Add a client to the set of our clients and
843 add_client (struct GNUNET_SERVER_Handle *server,
844 struct GNUNET_SERVER_Client *client)
846 client->server = server;
847 client->last_activity = GNUNET_TIME_absolute_get ();
848 client->next = server->clients;
849 server->clients = client;
850 client->my_receive = client->receive (client->client_closure,
851 GNUNET_SERVER_MAX_MESSAGE_SIZE,
852 server->idle_timeout,
853 &process_incoming, client);
858 * Create a request for receiving data from a socket.
860 * @param cls identifies the socket to receive from
861 * @param max how much data to read at most
862 * @param timeout when should this operation time out
863 * @param receiver function to call for processing
864 * @param receiver_cls closure for receiver
865 * @return task identifier that can be used to cancel the operation
867 static GNUNET_SCHEDULER_TaskIdentifier
868 sock_receive (void *cls,
870 struct GNUNET_TIME_Relative timeout,
871 GNUNET_NETWORK_Receiver receiver, void *receiver_cls)
873 return GNUNET_CONNECTION_receive (cls, max, timeout, receiver, receiver_cls);
878 * Wrapper to cancel receiving from a socket.
880 * @param cls handle to the GNUNET_CONNECTION_Handle to cancel
881 * @param tc task ID that was returned by GNUNET_CONNECTION_receive
884 sock_receive_cancel (void *cls, GNUNET_SCHEDULER_TaskIdentifier ti)
886 GNUNET_CONNECTION_receive_cancel (cls, ti);
894 sock_notify_transmit_ready (void *cls,
896 struct GNUNET_TIME_Relative timeout,
897 GNUNET_NETWORK_TransmitReadyNotify notify,
900 return GNUNET_CONNECTION_notify_transmit_ready (cls, size, timeout, notify,
909 sock_notify_transmit_ready_cancel (void *cls, void *h)
911 GNUNET_CONNECTION_notify_transmit_ready_cancel (h);
916 * Check if socket is still valid (no fatal errors have happened so far).
918 * @param cls the socket
919 * @return GNUNET_YES if valid, GNUNET_NO otherwise
922 sock_check (void *cls)
924 return GNUNET_CONNECTION_check (cls);
929 * Destroy this socket (free resources).
931 * @param cls the socket
934 sock_destroy (void *cls)
936 GNUNET_CONNECTION_destroy (cls);
941 * Add a TCP socket-based connection to the set of handles managed by
942 * this server. Use this function for outgoing (P2P) connections that
943 * we initiated (and where this server should process incoming
946 * @param server the server to use
947 * @param connection the connection to manage (client must
948 * stop using this connection from now on)
949 * @return the client handle (client should call
950 * "client_drop" on the return value eventually)
952 struct GNUNET_SERVER_Client *
953 GNUNET_SERVER_connect_socket (struct
956 struct GNUNET_CONNECTION_Handle *connection)
958 struct GNUNET_SERVER_Client *client;
960 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
961 client->client_closure = connection;
962 client->receive = &sock_receive;
963 client->receive_cancel = &sock_receive_cancel;
964 client->notify_transmit_ready = &sock_notify_transmit_ready;
965 client->notify_transmit_ready_cancel = &sock_notify_transmit_ready_cancel;
966 client->check = &sock_check;
967 client->destroy = &sock_destroy;
968 client->reference_count = 1;
969 add_client (server, client);
975 * Add an arbitrary connection to the set of handles managed by this
976 * server. This can be used if a sending and receiving does not
977 * really go over the network (internal transmission) or for servers
980 * @param server the server to use
981 * @param chandle opaque handle for the connection
982 * @param creceive receive function for the connection
983 * @param ccancel cancel receive function for the connection
984 * @param cnotify transmit notification function for the connection
985 * @param cnotify_cancel transmit notification cancellation function for the connection
986 * @param ccheck function to test if the connection is still up
987 * @param cdestroy function to close and free the connection
988 * @return the client handle (client should call
989 * "client_drop" on the return value eventually)
991 struct GNUNET_SERVER_Client *
992 GNUNET_SERVER_connect_callback (struct
996 GNUNET_SERVER_ReceiveCallback
998 GNUNET_SERVER_ReceiveCancelCallback
1000 GNUNET_SERVER_TransmitReadyCallback
1002 GNUNET_SERVER_TransmitReadyCancelCallback
1004 GNUNET_SERVER_CheckCallback
1006 GNUNET_SERVER_DestroyCallback cdestroy)
1008 struct GNUNET_SERVER_Client *client;
1010 client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
1011 client->client_closure = chandle;
1012 client->receive = creceive;
1013 client->receive_cancel = ccancel;
1014 client->notify_transmit_ready = cnotify;
1015 client->notify_transmit_ready_cancel = cnotify_cancel;
1016 client->check = ccheck;
1017 client->destroy = cdestroy;
1018 client->reference_count = 1;
1019 add_client (server, client);
1025 * Notify the server that the given client handle should
1026 * be kept (keeps the connection up if possible, increments
1027 * the internal reference counter).
1029 * @param client the client to keep
1032 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1034 client->reference_count++;
1039 * Notify the server that the given client handle is no
1040 * longer required. Decrements the reference counter. If
1041 * that counter reaches zero an inactive connection maybe
1044 * @param client the client to drop
1047 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1049 GNUNET_assert (client->reference_count > 0);
1050 client->reference_count--;
1051 if ((client->server == NULL) && (client->reference_count == 0))
1052 shutdown_incoming_processing (client);
1057 * Obtain the network address of the other party.
1059 * @param client the client to get the address for
1060 * @param addr where to store the address
1061 * @param addrlen where to store the length of the address
1062 * @return GNUNET_OK on success
1065 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1066 void **addr, size_t * addrlen)
1068 if (client->receive != &sock_receive)
1069 return GNUNET_SYSERR; /* not a network client */
1070 return GNUNET_CONNECTION_get_address (client->client_closure,
1076 * Ask the server to notify us whenever a client disconnects.
1077 * This function is called whenever the actual network connection
1078 * is closed; the reference count may be zero or larger than zero
1081 * @param server the server manageing the clients
1082 * @param callback function to call on disconnect
1083 * @param callback_cls closure for callback
1086 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1087 GNUNET_SERVER_DisconnectCallback callback,
1090 struct NotifyList *n;
1092 n = GNUNET_malloc (sizeof (struct NotifyList));
1093 n->callback = callback;
1094 n->callback_cls = callback_cls;
1095 n->next = server->disconnect_notify_list;
1096 server->disconnect_notify_list = n;
1101 * Ask the server to disconnect from the given client.
1102 * This is the same as returning GNUNET_SYSERR from a message
1103 * handler, except that it allows dropping of a client even
1104 * when not handling a message from that client.
1106 * @param client the client to disconnect from
1109 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1111 if (client->server == NULL)
1112 return; /* already disconnected */
1113 GNUNET_assert (client->my_receive != GNUNET_SCHEDULER_NO_TASK);
1114 client->receive_cancel (client->client_closure, client->my_receive);
1115 client->my_receive = GNUNET_SCHEDULER_NO_TASK;
1116 shutdown_incoming_processing (client);
1121 * Notify us when the server has enough space to transmit
1122 * a message of the given size to the given client.
1124 * @param server the server to use
1125 * @param client client to transmit message to
1126 * @param size requested amount of buffer space
1127 * @param timeout after how long should we give up (and call
1128 * notify with buf NULL and size 0)?
1129 * @param callback function to call when space is available
1130 * @param callback_cls closure for callback
1131 * @return non-NULL if the notify callback was queued; can be used
1132 * to cancel the request using
1133 * GNUNET_CONNECTION_notify_transmit_ready_cancel.
1134 * NULL if we are already going to notify someone else (busy)
1136 struct GNUNET_NETWORK_TransmitHandle *
1137 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1139 struct GNUNET_TIME_Relative timeout,
1140 GNUNET_NETWORK_TransmitReadyNotify
1141 callback, void *callback_cls)
1143 return client->notify_transmit_ready (client->client_closure,
1145 timeout, callback, callback_cls);
1150 * Resume receiving from this client, we are done processing the
1151 * current request. This function must be called from within each
1152 * GNUNET_SERVER_MessageCallback (or its respective continuations).
1154 * @param client client we were processing a message of
1155 * @param success GNUNET_OK to keep the connection open and
1156 * continue to receive
1157 * GNUNET_SYSERR to close the connection (signal
1161 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1167 GNUNET_assert (client->suspended > 0);
1168 client->suspended--;
1169 if (success != GNUNET_OK)
1170 client->shutdown_now = GNUNET_YES;
1171 if (client->suspended > 0)
1173 if (client->in_process_client_buffer == GNUNET_YES)
1175 if (client->side_buf_size > 0)
1177 /* resume processing from side-buf */
1178 sb = client->side_buf;
1179 client->side_buf = NULL;
1180 /* this will also resume the receive job */
1181 if (GNUNET_YES != client->shutdown_now)
1182 process_incoming (client, sb, client->side_buf_size, NULL, 0, 0);
1184 shutdown_incoming_processing (client);
1185 /* finally, free the side-buf */
1189 /* resume receive job */
1190 if (GNUNET_YES != client->shutdown_now)
1192 GNUNET_SCHEDULER_add_continuation (client->server->sched,
1194 &restart_processing,
1196 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1199 shutdown_incoming_processing (client);
1203 /* end of server.c */