2 This file is part of GNUnet.
3 Copyright (C) 2016 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/service_new.c
23 * @brief functions related to starting services (redesign)
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_resolver_service.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
41 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
43 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
47 * Information the service tracks per listen operation.
49 struct ServiceListenContext
55 struct ServiceListenContext *next;
60 struct ServiceListenContext *prev;
63 * Service this listen context belongs to.
65 struct GNUNET_SERVICE_Handle *sh;
68 * Socket we are listening on.
70 struct GNUNET_NETWORK_Handle *listen_socket;
73 * Task scheduled to do the listening.
75 struct GNUNET_SCHEDULER_Task *listen_task;
81 * Handle to a service.
83 struct GNUNET_SERVICE_Handle
88 const struct GNUNET_CONFIGURATION_Handle *cfg;
91 * Name of our service.
93 const char *service_name;
96 * Main service-specific task to run.
98 GNUNET_SERVICE_InitCallback service_init_cb;
101 * Function to call when clients connect.
103 GNUNET_SERVICE_ConnectHandler connect_cb;
106 * Function to call when clients disconnect / are disconnected.
108 GNUNET_SERVICE_DisconnectHandler disconnect_cb;
111 * Closure for @e service_init_cb, @e connect_cb, @e disconnect_cb.
116 * DLL of listen sockets used to accept new connections.
118 struct ServiceListenContext *slc_head;
121 * DLL of listen sockets used to accept new connections.
123 struct ServiceListenContext *slc_tail;
126 * Our clients, kept in a DLL.
128 struct GNUNET_SERVICE_Client *clients_head;
131 * Our clients, kept in a DLL.
133 struct GNUNET_SERVICE_Client *clients_tail;
136 * Message handlers to use for all clients.
138 const struct GNUNET_MQ_MessageHandler *handlers;
141 * Closure for @e task.
146 * IPv4 addresses that are not allowed to connect.
148 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_denied;
151 * IPv6 addresses that are not allowed to connect.
153 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_denied;
156 * IPv4 addresses that are allowed to connect (if not
157 * set, all are allowed).
159 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_allowed;
162 * IPv6 addresses that are allowed to connect (if not
163 * set, all are allowed).
165 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_allowed;
168 * Do we require a matching UID for UNIX domain socket connections?
169 * #GNUNET_NO means that the UID does not have to match (however,
170 * @e match_gid may still impose other access control checks).
175 * Do we require a matching GID for UNIX domain socket connections?
176 * Ignored if @e match_uid is #GNUNET_YES. Note that this is about
177 * checking that the client's UID is in our group OR that the
178 * client's GID is our GID. If both "match_gid" and @e match_uid are
179 * #GNUNET_NO, all users on the local system have access.
184 * Set to #GNUNET_YES if we got a shutdown signal and terminate
185 * the service if #have_non_monitor_clients() returns #GNUNET_YES.
192 enum GNUNET_SERVICE_Options options;
195 * If we are daemonizing, this FD is set to the
196 * pipe to the parent. Send '.' if we started
197 * ok, '!' if not. -1 if we are not daemonizing.
199 int ready_confirm_fd;
202 * Overall success/failure of the service start.
207 * If GNUNET_YES, consider unknown message types an error where the
208 * client is disconnected.
216 * Handle to a client that is connected to a service.
218 struct GNUNET_SERVICE_Client
224 struct GNUNET_SERVICE_Client *next;
229 struct GNUNET_SERVICE_Client *prev;
232 * Service that this client belongs to.
234 struct GNUNET_SERVICE_Handle *sh;
237 * Socket of this client.
239 struct GNUNET_NETWORK_Handle *sock;
242 * Message queue for the client.
244 struct GNUNET_MQ_Handle *mq;
247 * Tokenizer we use for processing incoming data.
249 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
252 * Task that warns about missing calls to
253 * #GNUNET_SERVICE_client_continue().
255 struct GNUNET_SCHEDULER_Task *warn_task;
258 * Task that receives data from the client to
259 * pass it to the handlers.
261 struct GNUNET_SCHEDULER_Task *recv_task;
264 * Task that transmit data to the client.
266 struct GNUNET_SCHEDULER_Task *send_task;
269 * User context value, value returned from
270 * the connect callback.
275 * Persist the file handle for this client no matter what happens,
276 * force the OS to close once the process actually dies. Should only
277 * be used in special cases!
282 * Is this client a 'monitor' client that should not be counted
283 * when deciding on destroying the server during soft shutdown?
284 * (see also #GNUNET_SERVICE_start)
289 * Type of last message processed (for warn_no_receive_done).
296 * Check if any of the clients we have left are unrelated to
299 * @param sh service to check clients for
300 * @return #GNUNET_YES if we have non-monitoring clients left
303 have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh)
305 struct GNUNET_SERVICE_Client *client;
307 for (client = sh->clients_head;NULL != client; client = client->next)
309 if (client->is_monitor)
318 * Shutdown task triggered when a service should be terminated.
319 * This considers active clients and the service options to see
320 * how this specific service is to be terminated, and depending
321 * on this proceeds with the shutdown logic.
323 * @param cls our `struct GNUNET_SERVICE_Handle`
326 service_shutdown (void *cls)
328 struct GNUNET_SERVICE_Handle *sh = cls;
332 case GNUNET_SERVICE_OPTION_NONE:
333 GNUNET_SERVICE_shutdown (sh);
335 case GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN:
336 /* This task should never be run if we are using
337 the manual shutdown. */
340 case GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN:
341 sh->got_shutdown = GNUNET_YES;
342 GNUNET_SERVICE_suspend (sh);
343 if (GNUNET_NO == have_non_monitor_clients (sh))
344 GNUNET_SERVICE_shutdown (sh);
351 * First task run by any service. Initializes our shutdown task,
352 * starts the listening operation on our listen sockets and launches
353 * the custom logic of the application service.
355 * @param cls our `struct GNUNET_SERVICE_Handle`
358 service_main (void *cls)
360 struct GNUNET_SERVICE_Handle *sh = cls;
362 if (GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN != sh->options)
363 GNUNET_SCHEDULER_add_shutdown (&service_shutdown,
365 GNUNET_SERVICE_resume (sh);
366 sh->service_init_cb (sh->cb_cls,
373 * Parse an IPv4 access control list.
375 * @param ret location where to write the ACL (set)
376 * @param sh service context to use to get the configuration
377 * @param option name of the ACL option to parse
378 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
382 process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret,
383 struct GNUNET_SERVICE_Handle *sh,
388 if (!GNUNET_CONFIGURATION_have_value (sh->cfg, sh->service_name, option))
393 GNUNET_break (GNUNET_OK ==
394 GNUNET_CONFIGURATION_get_value_string (sh->cfg,
397 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv4_policy (opt)))
399 LOG (GNUNET_ERROR_TYPE_WARNING,
400 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
405 return GNUNET_SYSERR;
413 * Parse an IPv6 access control list.
415 * @param ret location where to write the ACL (set)
416 * @param sh service context to use to get the configuration
417 * @param option name of the ACL option to parse
418 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
422 process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret,
423 struct GNUNET_SERVICE_Handle *sh,
428 if (!GNUNET_CONFIGURATION_have_value (sh->cfg, sh->service_name, option))
433 GNUNET_break (GNUNET_OK ==
434 GNUNET_CONFIGURATION_get_value_string (sh->cfg,
437 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv6_policy (opt)))
439 LOG (GNUNET_ERROR_TYPE_WARNING,
440 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
441 opt, sh->service_name, option);
443 return GNUNET_SYSERR;
451 * Add the given UNIX domain path as an address to the
452 * list (as the first entry).
454 * @param saddrs array to update
455 * @param saddrlens where to store the address length
456 * @param unixpath path to add
457 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
458 * parameter is ignore on systems other than LINUX
461 add_unixpath (struct sockaddr **saddrs,
462 socklen_t *saddrlens,
463 const char *unixpath,
467 struct sockaddr_un *un;
469 un = GNUNET_new (struct sockaddr_un);
470 un->sun_family = AF_UNIX;
471 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
473 if (GNUNET_YES == abstract)
474 un->sun_path[0] = '\0';
476 #if HAVE_SOCKADDR_IN_SIN_LEN
477 un->sun_len = (u_char) sizeof (struct sockaddr_un);
479 *saddrs = (struct sockaddr *) un;
480 *saddrlens = sizeof (struct sockaddr_un);
482 /* this function should never be called
483 * unless AF_UNIX is defined! */
490 * Get the list of addresses that a server for the given service
493 * @param service_name name of the service
494 * @param cfg configuration (which specifies the addresses)
495 * @param addrs set (call by reference) to an array of pointers to the
496 * addresses the server should bind to and listen on; the
497 * array will be NULL-terminated (on success)
498 * @param addr_lens set (call by reference) to an array of the lengths
499 * of the respective `struct sockaddr` struct in the @a addrs
501 * @return number of addresses found on success,
502 * #GNUNET_SYSERR if the configuration
503 * did not specify reasonable finding information or
504 * if it specified a hostname that could not be resolved;
505 * #GNUNET_NO if the number of addresses configured is
506 * zero (in this case, `*addrs` and `*addr_lens` will be
510 get_server_addresses (const char *service_name,
511 const struct GNUNET_CONFIGURATION_Handle *cfg,
512 struct sockaddr ***addrs,
513 socklen_t **addr_lens)
516 struct GNUNET_NETWORK_Handle *desc;
517 unsigned long long port;
519 struct addrinfo hints;
520 struct addrinfo *res;
521 struct addrinfo *pos;
522 struct addrinfo *next;
527 struct sockaddr **saddrs;
528 socklen_t *saddrlens;
534 if (GNUNET_CONFIGURATION_have_value (cfg,
540 GNUNET_CONFIGURATION_get_value_yesno (cfg,
543 return GNUNET_SYSERR;
546 disablev6 = GNUNET_NO;
550 /* probe IPv6 support */
551 desc = GNUNET_NETWORK_socket_create (PF_INET6,
556 if ((ENOBUFS == errno) ||
561 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
563 return GNUNET_SYSERR;
565 LOG (GNUNET_ERROR_TYPE_INFO,
566 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
569 disablev6 = GNUNET_YES;
573 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
579 if (GNUNET_CONFIGURATION_have_value (cfg,
584 GNUNET_CONFIGURATION_get_value_number (cfg,
589 LOG (GNUNET_ERROR_TYPE_ERROR,
590 _("Require valid port number for service `%s' in configuration!\n"),
595 LOG (GNUNET_ERROR_TYPE_ERROR,
596 _("Require valid port number for service `%s' in configuration!\n"),
598 return GNUNET_SYSERR;
602 if (GNUNET_CONFIGURATION_have_value (cfg,
606 GNUNET_break (GNUNET_OK ==
607 GNUNET_CONFIGURATION_get_value_string (cfg,
616 abstract = GNUNET_NO;
619 GNUNET_CONFIGURATION_have_value (cfg,
623 GNUNET_CONFIGURATION_get_value_filename (cfg,
627 (0 < strlen (unixpath)))
629 /* probe UNIX support */
630 struct sockaddr_un s_un;
632 if (strlen (unixpath) >= sizeof (s_un.sun_path))
634 LOG (GNUNET_ERROR_TYPE_WARNING,
635 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
637 (unsigned long long) sizeof (s_un.sun_path));
638 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
639 LOG (GNUNET_ERROR_TYPE_INFO,
640 _("Using `%s' instead\n"),
644 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
646 "USE_ABSTRACT_SOCKETS");
647 if (GNUNET_SYSERR == abstract)
648 abstract = GNUNET_NO;
650 if ((GNUNET_YES != abstract)
652 GNUNET_DISK_directory_create_for_file (unixpath)))
653 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
657 if (NULL != unixpath)
659 desc = GNUNET_NETWORK_socket_create (AF_UNIX,
664 if ((ENOBUFS == errno) ||
669 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
671 GNUNET_free_non_null (hostname);
672 GNUNET_free (unixpath);
673 return GNUNET_SYSERR;
675 LOG (GNUNET_ERROR_TYPE_INFO,
676 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
679 GNUNET_free (unixpath);
684 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
690 if ((0 == port) && (NULL == unixpath))
692 LOG (GNUNET_ERROR_TYPE_ERROR,
693 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
695 GNUNET_free_non_null (hostname);
696 return GNUNET_SYSERR;
700 saddrs = GNUNET_new_array (2,
702 saddrlens = GNUNET_new_array (2,
704 add_unixpath (saddrs,
708 GNUNET_free_non_null (unixpath);
709 GNUNET_free_non_null (hostname);
711 *addr_lens = saddrlens;
715 if (NULL != hostname)
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Resolving `%s' since that is where `%s' will bind to.\n",
723 sizeof (struct addrinfo));
725 hints.ai_family = AF_INET;
726 hints.ai_protocol = IPPROTO_TCP;
727 if ((0 != (ret = getaddrinfo (hostname,
733 LOG (GNUNET_ERROR_TYPE_ERROR,
734 _("Failed to resolve `%s': %s\n"),
737 GNUNET_free (hostname);
738 GNUNET_free_non_null (unixpath);
739 return GNUNET_SYSERR;
743 while (NULL != (pos = next))
747 (pos->ai_family == AF_INET6) )
753 LOG (GNUNET_ERROR_TYPE_ERROR,
754 _("Failed to find %saddress for `%s'.\n"),
755 disablev6 ? "IPv4 " : "",
758 GNUNET_free (hostname);
759 GNUNET_free_non_null (unixpath);
760 return GNUNET_SYSERR;
763 if (NULL != unixpath)
765 saddrs = GNUNET_new_array (resi + 1,
767 saddrlens = GNUNET_new_array (resi + 1,
770 if (NULL != unixpath)
772 add_unixpath (saddrs,
779 while (NULL != (pos = next))
783 (AF_INET6 == pos->ai_family) )
785 if ( (IPPROTO_TCP != pos->ai_protocol) &&
786 (0 != pos->ai_protocol) )
787 continue; /* not TCP */
788 if ( (SOCK_STREAM != pos->ai_socktype) &&
789 (0 != pos->ai_socktype) )
791 LOG (GNUNET_ERROR_TYPE_DEBUG,
792 "Service `%s' will bind to `%s'\n",
794 GNUNET_a2s (pos->ai_addr,
796 if (AF_INET == pos->ai_family)
798 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
799 saddrlens[i] = pos->ai_addrlen;
800 saddrs[i] = GNUNET_malloc (saddrlens[i]);
801 GNUNET_memcpy (saddrs[i],
804 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
808 GNUNET_assert (AF_INET6 == pos->ai_family);
809 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
810 saddrlens[i] = pos->ai_addrlen;
811 saddrs[i] = GNUNET_malloc (saddrlens[i]);
812 GNUNET_memcpy (saddrs[i],
815 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
819 GNUNET_free (hostname);
825 /* will bind against everything, just set port */
830 if (NULL != unixpath)
833 saddrs = GNUNET_new_array (resi + 1,
835 saddrlens = GNUNET_new_array (resi + 1,
837 if (NULL != unixpath)
839 add_unixpath (saddrs,
845 saddrlens[i] = sizeof (struct sockaddr_in);
846 saddrs[i] = GNUNET_malloc (saddrlens[i]);
847 #if HAVE_SOCKADDR_IN_SIN_LEN
848 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
850 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
851 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
857 if (NULL != unixpath)
859 saddrs = GNUNET_new_array (resi + 1,
861 saddrlens = GNUNET_new_array (resi + 1,
864 if (NULL != unixpath)
866 add_unixpath (saddrs,
872 saddrlens[i] = sizeof (struct sockaddr_in6);
873 saddrs[i] = GNUNET_malloc (saddrlens[i]);
874 #if HAVE_SOCKADDR_IN_SIN_LEN
875 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
877 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
878 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
880 saddrlens[i] = sizeof (struct sockaddr_in);
881 saddrs[i] = GNUNET_malloc (saddrlens[i]);
882 #if HAVE_SOCKADDR_IN_SIN_LEN
883 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
885 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
886 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
889 GNUNET_free_non_null (unixpath);
891 *addr_lens = saddrlens;
898 * Read listen sockets from the parent process (ARM).
900 * @param sh service context to initialize
901 * @return NULL-terminated array of sockets on success,
902 * NULL if not ok (must bind yourself)
904 static struct GNUNET_NETWORK_Handle **
905 receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
907 static struct GNUNET_NETWORK_Handle **lsocks;
914 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
915 if ( (NULL == env_buf) ||
916 (strlen (env_buf) <= 0) )
918 /* Using W32 API directly here, because this pipe will
919 * never be used outside of this function, and it's just too much of a bother
920 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
922 lsocks_pipe = (HANDLE) strtoul (env_buf,
925 if ( (0 == lsocks_pipe) ||
926 (INVALID_HANDLE_VALUE == lsocks_pipe))
935 ret = ReadFile (lsocks_pipe,
941 (sizeof (count) != rd) ||
944 lsocks = GNUNET_new_array (count + 1,
945 struct GNUNET_NETWORK_Handle *);
948 for (i = 0; i < count; i++)
950 WSAPROTOCOL_INFOA pi;
954 ret = ReadFile (lsocks_pipe,
960 (sizeof (size) != rd) ||
961 (sizeof (pi) != size) )
963 ret = ReadFile (lsocks_pipe,
971 s = WSASocketA (pi.iAddressFamily,
976 WSA_FLAG_OVERLAPPED);
977 lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
978 if (NULL == lsocks[i])
980 else if (i == count - 1)
985 lsocks[count] = NULL;
989 CloseHandle (lsocks_pipe);
993 LOG (GNUNET_ERROR_TYPE_ERROR,
994 _("Could not access a pre-bound socket, will try to bind myself\n"));
995 for (i = 0; (i < count) && (NULL != lsocks[i]); i++)
996 GNUNET_break (0 == GNUNET_NETWORK_socket_close (lsocks[i]));
997 GNUNET_free (lsocks);
1006 * Create and initialize a listen socket for the server.
1008 * @param server_addr address to listen on
1009 * @param socklen length of @a server_addr
1010 * @return NULL on error, otherwise the listen socket
1012 static struct GNUNET_NETWORK_Handle *
1013 open_listen_socket (const struct sockaddr *server_addr,
1016 struct GNUNET_NETWORK_Handle *sock;
1020 switch (server_addr->sa_family)
1023 port = ntohs (((const struct sockaddr_in *) server_addr)->sin_port);
1026 port = ntohs (((const struct sockaddr_in6 *) server_addr)->sin6_port);
1036 sock = GNUNET_NETWORK_socket_create (server_addr->sa_family,
1041 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1046 /* bind the socket */
1047 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock,
1052 if (EADDRINUSE != errno)
1054 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
1055 * fail if we already took the port on IPv6; if both IPv4 and
1056 * IPv6 binds fail, then our caller will log using the
1057 * errno preserved in 'eno' */
1058 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1061 LOG (GNUNET_ERROR_TYPE_ERROR,
1062 _("`%s' failed for port %d (%s).\n"),
1065 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1071 LOG (GNUNET_ERROR_TYPE_WARNING,
1072 _("`%s' failed for port %d (%s): address already in use\n"),
1074 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1075 else if (AF_UNIX == server_addr->sa_family)
1077 LOG (GNUNET_ERROR_TYPE_WARNING,
1078 _("`%s' failed for `%s': address already in use\n"),
1080 GNUNET_a2s (server_addr, socklen));
1083 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
1087 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock,
1090 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1092 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
1097 LOG (GNUNET_ERROR_TYPE_DEBUG,
1098 "Server starts to listen on port %u.\n",
1105 * Setup service handle
1107 * Configuration may specify:
1108 * - PORT (where to bind to for TCP)
1109 * - UNIXPATH (where to bind to for UNIX domain sockets)
1110 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1111 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1112 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1113 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1114 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1115 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1117 * @param sh service context to initialize
1118 * @return #GNUNET_OK if configuration succeeded
1121 setup_service (struct GNUNET_SERVICE_Handle *sh)
1124 struct GNUNET_NETWORK_Handle **lsocks;
1131 if (GNUNET_CONFIGURATION_have_value
1136 if (GNUNET_SYSERR ==
1138 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1142 LOG (GNUNET_ERROR_TYPE_ERROR,
1143 _("Specified value for `%s' of service `%s' is invalid\n"),
1146 return GNUNET_SYSERR;
1150 tolerant = GNUNET_NO;
1155 if ( (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1160 (cnt < FD_SETSIZE) &&
1161 (cnt + 4 < FD_SETSIZE) )
1163 lsocks = GNUNET_new_array (cnt + 1,
1164 struct GNUNET_NETWORK_Handle *);
1167 flags = fcntl (3 + cnt,
1170 (0 != (flags & FD_CLOEXEC)) ||
1172 (lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1174 LOG (GNUNET_ERROR_TYPE_ERROR,
1175 _("Could not access pre-bound socket %u, will try to bind myself\n"),
1176 (unsigned int) 3 + cnt);
1178 while (NULL != lsocks[cnt])
1179 GNUNET_break (0 == GNUNET_NETWORK_socket_close (lsocks[cnt++]));
1180 GNUNET_free (lsocks);
1185 unsetenv ("LISTEN_FDS");
1188 if (NULL != getenv ("GNUNET_OS_READ_LSOCKS"))
1190 lsocks = receive_sockets_from_parent (sh);
1191 putenv ("GNUNET_OS_READ_LSOCKS=");
1197 /* listen only on inherited sockets if we have any */
1198 struct GNUNET_NETWORK_Handle **ls;
1200 for (ls = lsocks; NULL != *ls; ls++)
1202 struct ServiceListenContext *slc;
1204 slc = GNUNET_new (struct ServiceListenContext);
1205 slc->listen_socket = *ls;
1206 GNUNET_CONTAINER_DLL_insert (sh->slc_head,
1210 GNUNET_free (lsocks);
1214 struct sockaddr **addrs;
1215 socklen_t *addrlens;
1218 num = get_server_addresses (sh->service_name,
1222 if (GNUNET_SYSERR == num)
1223 return GNUNET_SYSERR;
1225 for (int i = 0; i < num; i++)
1227 struct ServiceListenContext *slc;
1229 slc = GNUNET_new (struct ServiceListenContext);
1230 slc->listen_socket = open_listen_socket (addrs[i],
1232 GNUNET_break (NULL != slc->listen_socket);
1233 GNUNET_CONTAINER_DLL_insert (sh->slc_head,
1239 sh->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1241 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1245 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1248 process_acl4 (&sh->v4_denied,
1251 process_acl4 (&sh->v4_allowed,
1254 process_acl6 (&sh->v6_denied,
1257 process_acl6 (&sh->v6_allowed,
1265 * Get the name of the user that'll be used
1266 * to provide the service.
1268 * @param sh service context
1269 * @return value of the 'USERNAME' option
1272 get_user_name (struct GNUNET_SERVICE_Handle *sh)
1277 GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1289 * @param sh service context
1290 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1293 set_user_id (struct GNUNET_SERVICE_Handle *sh)
1297 if (NULL == (user = get_user_name (sh)))
1298 return GNUNET_OK; /* keep */
1303 pws = getpwnam (user);
1306 LOG (GNUNET_ERROR_TYPE_ERROR,
1307 _("Cannot obtain information about user `%s': %s\n"),
1309 errno == 0 ? _("No such user") : STRERROR (errno));
1311 return GNUNET_SYSERR;
1313 if ( (0 != setgid (pws->pw_gid)) ||
1314 (0 != setegid (pws->pw_gid)) ||
1316 (0 != initgroups (user,
1319 (0 != setuid (pws->pw_uid)) ||
1320 (0 != seteuid (pws->pw_uid)))
1322 if ((0 != setregid (pws->pw_gid,
1324 (0 != setreuid (pws->pw_uid,
1327 LOG (GNUNET_ERROR_TYPE_ERROR,
1328 _("Cannot change user/group to `%s': %s\n"),
1332 return GNUNET_SYSERR;
1342 * Get the name of the file where we will
1343 * write the PID of the service.
1345 * @param sh service context
1346 * @return name of the file for the process ID
1349 get_pid_file_name (struct GNUNET_SERVICE_Handle *sh)
1354 GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1364 * Delete the PID file that was created by our parent.
1366 * @param sh service context
1369 pid_file_delete (struct GNUNET_SERVICE_Handle *sh)
1371 char *pif = get_pid_file_name (sh);
1374 return; /* no PID file */
1375 if (0 != UNLINK (pif))
1376 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1384 * Detach from terminal.
1386 * @param sh service context
1387 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1390 detach_terminal (struct GNUNET_SERVICE_Handle *sh)
1397 if (0 != PIPE (filedes))
1399 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1401 return GNUNET_SYSERR;
1406 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1408 return GNUNET_SYSERR;
1415 GNUNET_break (0 == CLOSE (filedes[1]));
1417 if (1 != READ (filedes[0],
1420 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1428 LOG (GNUNET_ERROR_TYPE_INFO,
1429 _("Service process failed to initialize\n"));
1432 LOG (GNUNET_ERROR_TYPE_INFO,
1433 _("Service process could not initialize server function\n"));
1436 LOG (GNUNET_ERROR_TYPE_INFO,
1437 _("Service process failed to report status\n"));
1440 exit (1); /* child reported error */
1442 GNUNET_break (0 == CLOSE (0));
1443 GNUNET_break (0 == CLOSE (1));
1444 GNUNET_break (0 == CLOSE (filedes[0]));
1445 nullfd = OPEN ("/dev/null",
1448 return GNUNET_SYSERR;
1449 /* set stdin/stdout to /dev/null */
1450 if ( (dup2 (nullfd, 0) < 0) ||
1451 (dup2 (nullfd, 1) < 0) )
1453 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1455 (void) CLOSE (nullfd);
1456 return GNUNET_SYSERR;
1458 (void) CLOSE (nullfd);
1459 /* Detach from controlling terminal */
1462 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1464 sh->ready_confirm_fd = filedes[1];
1466 /* FIXME: we probably need to do something else
1467 * elsewhere in order to fork the process itself... */
1475 * Creates the "main" function for a GNUnet service. You
1476 * should almost always use the #GNUNET_SERVICE_MAIN macro
1477 * instead of calling this function directly (except
1478 * for ARM, which should call this function directly).
1480 * The function will launch the service with the name @a service_name
1481 * using the @a service_options to configure its shutdown
1482 * behavior. Once the service is ready, the @a init_cb will be called
1483 * for service-specific initialization. @a init_cb will be given the
1484 * service handler which can be used to control the service's
1485 * availability. When clients connect or disconnect, the respective
1486 * @a connect_cb or @a disconnect_cb functions will be called. For
1487 * messages received from the clients, the respective @a handlers will
1488 * be invoked; for the closure of the handlers we use the return value
1489 * from the @a connect_cb invocation of the respective client.
1491 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
1492 * message to receive further messages from this client. If
1493 * #GNUNET_SERVICE_client_continue() is not called within a short
1494 * time, a warning will be logged. If delays are expected, services
1495 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
1496 * disable the warning.
1498 * Clients sending invalid messages (based on @a handlers) will be
1499 * dropped. Additionally, clients can be dropped at any time using
1500 * #GNUNET_SERVICE_client_drop().
1502 * @param argc number of command-line arguments in @a argv
1503 * @param argv array of command-line arguments
1504 * @param service_name name of the service to run
1505 * @param options options controlling shutdown of the service
1506 * @param service_init_cb function to call once the service is ready
1507 * @param connect_cb function to call whenever a client connects
1508 * @param disconnect_cb function to call whenever a client disconnects
1509 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
1510 * @param handlers NULL-terminated array of message handlers for the service,
1511 * the closure will be set to the value returned by
1512 * the @a connect_cb for the respective connection
1513 * @return 0 on success, non-zero on error
1516 GNUNET_SERVICE_ruN_ (int argc,
1518 const char *service_name,
1519 enum GNUNET_SERVICE_Options options,
1520 GNUNET_SERVICE_InitCallback service_init_cb,
1521 GNUNET_SERVICE_ConnectHandler connect_cb,
1522 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
1524 const struct GNUNET_MQ_MessageHandler *handlers)
1526 struct GNUNET_SERVICE_Handle sh;
1528 char *opt_cfg_filename;
1533 unsigned long long skew_offset;
1534 unsigned long long skew_variance;
1535 long long clock_offset;
1536 struct GNUNET_CONFIGURATION_Handle *cfg;
1540 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1541 GNUNET_GETOPT_OPTION_CFG_FILE (&opt_cfg_filename),
1542 {'d', "daemonize", NULL,
1543 gettext_noop ("do daemonize (detach from terminal)"), 0,
1544 GNUNET_GETOPT_set_one, &do_daemonize},
1545 GNUNET_GETOPT_OPTION_HELP (NULL),
1546 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1547 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1548 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1549 GNUNET_GETOPT_OPTION_END
1552 xdg = getenv ("XDG_CONFIG_HOME");
1554 GNUNET_asprintf (&cfg_filename,
1558 GNUNET_OS_project_data_get ()->config_file);
1560 cfg_filename = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
1562 sh.options = options;
1563 sh.cfg = cfg = GNUNET_CONFIGURATION_create ();
1564 sh.service_init_cb = service_init_cb;
1565 sh.connect_cb = connect_cb;
1566 sh.disconnect_cb = disconnect_cb;
1568 sh.handlers = handlers;
1570 /* setup subsystems */
1571 ret = GNUNET_GETOPT_run (service_name,
1575 if (GNUNET_SYSERR == ret)
1577 if (GNUNET_NO == ret)
1582 if (GNUNET_OK != GNUNET_log_setup (service_name,
1589 if (NULL == opt_cfg_filename)
1590 opt_cfg_filename = GNUNET_strdup (cfg_filename);
1591 if (GNUNET_YES == GNUNET_DISK_file_test (opt_cfg_filename))
1593 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg,
1596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1597 _("Malformed configuration file `%s', exit ...\n"),
1604 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg,
1607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1608 _("Malformed configuration, exit ...\n"));
1611 if (0 != strcmp (opt_cfg_filename,
1613 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1614 _("Could not access configuration file `%s'\n"),
1617 if (GNUNET_OK != setup_service (&sh))
1619 if ( (1 == do_daemonize) &&
1620 (GNUNET_OK != detach_terminal (&sh)) )
1625 if (GNUNET_OK != set_user_id (&sh))
1627 LOG (GNUNET_ERROR_TYPE_DEBUG,
1628 "Service `%s' runs with configuration from `%s'\n",
1632 GNUNET_CONFIGURATION_get_value_number (sh.cfg,
1637 GNUNET_CONFIGURATION_get_value_number (sh.cfg,
1642 clock_offset = skew_offset - skew_variance;
1643 GNUNET_TIME_set_offset (clock_offset);
1644 LOG (GNUNET_ERROR_TYPE_DEBUG,
1645 "Skewing clock by %dll ms\n",
1648 /* actually run service */
1650 GNUNET_SCHEDULER_run (&service_main,
1653 if (1 == do_daemonize)
1654 pid_file_delete (&sh);
1657 if (-1 != sh.ready_confirm_fd)
1659 if (1 != WRITE (sh.ready_confirm_fd, err ? "I" : "S", 1))
1660 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1661 GNUNET_break (0 == CLOSE (sh.ready_confirm_fd));
1668 GNUNET_CONFIGURATION_have_value (sh.cfg,
1672 GNUNET_CONFIGURATION_get_value_string (sh.cfg,
1680 GAUGER (service_name,
1684 GNUNET_free (counter);
1688 GNUNET_SPEEDUP_stop_ ();
1689 GNUNET_CONFIGURATION_destroy (cfg);
1691 while (NULL != sh.slc_head)
1693 struct ServiceListenContext *slc = sh.slc_head;
1695 sh.slc_head = slc->next;
1696 // FIXME: destroy slc
1700 GNUNET_free_non_null (logfile);
1701 GNUNET_free_non_null (loglev);
1702 GNUNET_free (cfg_filename);
1703 GNUNET_free_non_null (opt_cfg_filename);
1704 GNUNET_free_non_null (sh.v4_denied);
1705 GNUNET_free_non_null (sh.v6_denied);
1706 GNUNET_free_non_null (sh.v4_allowed);
1707 GNUNET_free_non_null (sh.v6_allowed);
1709 return err ? GNUNET_SYSERR : sh.ret;
1714 * Suspend accepting connections from the listen socket temporarily.
1715 * Resume activity using #GNUNET_SERVICE_resume.
1717 * @param sh service to stop accepting connections.
1720 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
1722 struct ServiceListenContext *slc;
1724 for (slc = sh->slc_head; NULL != slc; slc = slc->next)
1726 if (NULL != slc->listen_task)
1728 GNUNET_SCHEDULER_cancel (slc->listen_task);
1729 slc->listen_task = NULL;
1736 * Signature of functions implementing the sending functionality of a
1739 * @param mq the message queue
1740 * @param msg the message to send
1741 * @param impl_state state of the implementation
1744 service_mq_send (struct GNUNET_MQ_Handle *mq,
1745 const struct GNUNET_MessageHeader *msg,
1748 // struct GNUNET_SERVICE_Client *client = cls;
1750 // FIXME 1: setup "client->send_task" for transmission.
1751 // FIXME 2: I seriously hope we do not need to make a copy of `msg`!
1752 // OPTIMIZATION: ideally, we'd like the ability to peak at the rest of
1753 // the queue and transmit more than one message if possible.
1758 * Implementation function that cancels the currently sent message.
1760 * @param mq message queue
1761 * @param impl_state state specific to the implementation
1764 service_mq_cancel (struct GNUNET_MQ_Handle *mq,
1767 // struct GNUNET_SERVICE_Client *client = cls;
1769 // FIXME: stop transmission! (must be possible, otherwise
1770 // we must have told MQ that the message was sent!)
1775 * Generic error handler, called with the appropriate
1776 * error code and the same closure specified at the creation of
1777 * the message queue.
1778 * Not every message queue implementation supports an error handler.
1780 * @param cls closure
1781 * @param error error code
1784 service_mq_error_handler (void *cls,
1785 enum GNUNET_MQ_Error error)
1787 struct GNUNET_SERVICE_Client *client = cls;
1794 * Functions with this signature are called whenever a
1795 * complete message is received by the tokenizer for a client.
1797 * Do not call #GNUNET_SERVER_mst_destroy() from within
1798 * the scope of this callback.
1800 * @param cls closure with the `struct GNUNET_SERVICE_Client *`
1801 * @param client_cls closure with the `struct GNUNET_SERVICE_Client *`
1802 * @param message the actual message
1803 * @return #GNUNET_OK on success (always)
1806 service_client_mst_cb (void *cls,
1808 const struct GNUNET_MessageHeader *message)
1810 struct GNUNET_SERVICE_Client *client = cls;
1812 GNUNET_MQ_inject_message (client->mq,
1819 * A client sent us data. Receive and process it. If we are done,
1820 * reschedule this task.
1822 * @param cls the `struct GNUNET_SERVICE_Client` that sent us data.
1825 service_client_recv (void *cls)
1827 struct GNUNET_SERVICE_Client *client = cls;
1829 // FIXME: read into buffer, pass to MST, then client->mq inject!
1830 // FIXME: revise MST API to avoid the memcpy!
1831 // i.e.: GNUNET_MST_read (client->sock);
1836 * We have successfully accepted a connection from a client. Now
1837 * setup the client (with the scheduler) and tell the application.
1839 * @param sh service that accepted the client
1840 * @param sock socket associated with the client
1843 start_client (struct GNUNET_SERVICE_Handle *sh,
1844 struct GNUNET_NETWORK_Handle *csock)
1846 struct GNUNET_SERVICE_Client *client;
1848 client = GNUNET_new (struct GNUNET_SERVICE_Client);
1849 GNUNET_CONTAINER_DLL_insert (sh->clients_head,
1853 client->sock = csock;
1854 client->mq = GNUNET_MQ_queue_for_callbacks (&service_mq_send,
1859 &service_mq_error_handler,
1861 client->mst = GNUNET_SERVER_mst_create (&service_client_mst_cb,
1863 client->user_context = sh->connect_cb (sh->cb_cls,
1866 GNUNET_MQ_set_handlers_closure (client->mq,
1867 client->user_context);
1869 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1871 &service_client_recv,
1877 * Check if the given IP address is in the list of IP addresses.
1879 * @param list a list of networks
1880 * @param add the IP to check (in network byte order)
1881 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
1884 check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list,
1885 const struct in_addr *add)
1892 while ( (0 != list[i].network.s_addr) ||
1893 (0 != list[i].netmask.s_addr) )
1895 if ((add->s_addr & list[i].netmask.s_addr) ==
1896 (list[i].network.s_addr & list[i].netmask.s_addr))
1905 * Check if the given IP address is in the list of IP addresses.
1907 * @param list a list of networks
1908 * @param ip the IP to check (in network byte order)
1909 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
1912 check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
1913 const struct in6_addr *ip)
1917 struct in6_addr zero;
1923 sizeof (struct in6_addr));
1926 while (0 != memcmp (&zero,
1928 sizeof (struct in6_addr)))
1930 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
1931 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
1932 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
1944 * We have a client. Accept the incoming socket(s) (and reschedule
1947 * @param cls the `struct ServiceListenContext` of the ready listen socket
1950 accept_client (void *cls)
1952 struct ServiceListenContext *slc = cls;
1953 struct GNUNET_SERVICE_Handle *sh = slc->sh;
1955 slc->listen_task = NULL;
1958 struct GNUNET_NETWORK_Handle *sock;
1959 const struct sockaddr_in *v4;
1960 const struct sockaddr_in6 *v6;
1961 struct sockaddr_storage sa;
1965 addrlen = sizeof (sa);
1966 sock = GNUNET_NETWORK_socket_accept (slc->listen_socket,
1967 (struct sockaddr *) &sa,
1971 switch (sa.ss_family)
1974 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1975 v4 = (const struct sockaddr_in *) &sa;
1976 ok = ( ( (NULL == sh->v4_allowed) ||
1977 (check_ipv4_listed (sh->v4_allowed,
1979 ( (NULL == sh->v4_denied) ||
1980 (! check_ipv4_listed (sh->v4_denied,
1981 &v4->sin_addr)) ) );
1984 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1985 v6 = (const struct sockaddr_in6 *) &sa;
1986 ok = ( ( (NULL == sh->v6_allowed) ||
1987 (check_ipv6_listed (sh->v6_allowed,
1988 &v6->sin6_addr))) &&
1989 ( (NULL == sh->v6_denied) ||
1990 (! check_ipv6_listed (sh->v6_denied,
1991 &v6->sin6_addr)) ) );
1995 ok = GNUNET_OK; /* controlled using file-system ACL now */
1999 LOG (GNUNET_ERROR_TYPE_WARNING,
2000 _("Unknown address family %d\n"),
2006 LOG (GNUNET_ERROR_TYPE_DEBUG,
2007 "Service rejected incoming connection from %s due to policy.\n",
2008 GNUNET_a2s ((const struct sockaddr *) &sa,
2010 GNUNET_NETWORK_socket_close (sock);
2013 LOG (GNUNET_ERROR_TYPE_DEBUG,
2014 "Service accepted incoming connection from %s.\n",
2015 GNUNET_a2s ((const struct sockaddr *) &sa,
2017 start_client (slc->sh,
2021 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2029 * Resume accepting connections from the listen socket.
2031 * @param sh service to resume accepting connections.
2034 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh)
2036 struct ServiceListenContext *slc;
2038 for (slc = sh->slc_head; NULL != slc; slc = slc->next)
2040 GNUNET_assert (NULL == slc->listen_task);
2042 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2051 * Continue receiving further messages from the given client.
2052 * Must be called after each message received.
2054 * @param c the client to continue receiving from
2057 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c)
2059 GNUNET_break (0); // not implemented
2064 * Disable the warning the server issues if a message is not
2065 * acknowledged in a timely fashion. Use this call if a client is
2066 * intentionally delayed for a while. Only applies to the current
2069 * @param c client for which to disable the warning
2072 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c)
2074 GNUNET_break (NULL != c->warn_task);
2075 if (NULL != c->warn_task)
2077 GNUNET_SCHEDULER_cancel (c->warn_task);
2078 c->warn_task = NULL;
2084 * Ask the server to disconnect from the given client. This is the
2085 * same as returning #GNUNET_SYSERR within the check procedure when
2086 * handling a message, wexcept that it allows dropping of a client even
2087 * when not handling a message from that client. The `disconnect_cb`
2088 * will be called on @a c even if the application closes the connection
2089 * using this function.
2091 * @param c client to disconnect now
2094 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
2096 struct GNUNET_SERVICE_Handle *sh = c->sh;
2098 GNUNET_CONTAINER_DLL_remove (sh->clients_head,
2101 sh->disconnect_cb (sh->cb_cls,
2104 if (NULL != c->warn_task)
2106 GNUNET_SCHEDULER_cancel (c->warn_task);
2107 c->warn_task = NULL;
2109 if (NULL != c->recv_task)
2111 GNUNET_SCHEDULER_cancel (c->recv_task);
2112 c->recv_task = NULL;
2114 if (NULL != c->send_task)
2116 GNUNET_SCHEDULER_cancel (c->send_task);
2117 c->send_task = NULL;
2119 GNUNET_SERVER_mst_destroy (c->mst);
2120 GNUNET_MQ_destroy (c->mq);
2121 if (GNUNET_NO == c->persist)
2123 GNUNET_NETWORK_socket_close (c->sock);
2127 GNUNET_NETWORK_socket_free_memory_only_ (c->sock);
2130 if ( (GNUNET_YES == sh->got_shutdown) &&
2131 (GNUNET_NO == have_non_monitor_clients (sh)) )
2132 GNUNET_SERVICE_shutdown (sh);
2137 * Explicitly stops the service.
2139 * @param sh server to shutdown
2142 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh)
2144 struct GNUNET_SERVICE_Client *client;
2146 GNUNET_SERVICE_suspend (sh);
2147 sh->got_shutdown = GNUNET_NO;
2148 while (NULL != (client = sh->clients_head))
2149 GNUNET_SERVICE_client_drop (client);
2154 * Set the 'monitor' flag on this client. Clients which have been
2155 * marked as 'monitors' won't prevent the server from shutting down
2156 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
2157 * that for "normal" clients we likely want to allow them to process
2158 * their requests; however, monitor-clients are likely to 'never'
2159 * disconnect during shutdown and thus will not be considered when
2160 * determining if the server should continue to exist after
2161 * shutdown has been triggered.
2163 * @param c client to mark as a monitor
2166 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c)
2168 c->is_monitor = GNUNET_YES;
2169 if ( (GNUNET_YES == c->sh->got_shutdown) &&
2170 (GNUNET_NO == have_non_monitor_clients (c->sh)) )
2171 GNUNET_SERVICE_shutdown (c->sh);
2176 * Set the persist option on this client. Indicates that the
2177 * underlying socket or fd should never really be closed. Used for
2178 * indicating process death.
2180 * @param c client to persist the socket (never to be closed)
2183 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c)
2185 c->persist = GNUNET_YES;
2189 /* end of service_new.c */