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
25 * @author Florian Dold
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_resolver_service.h"
40 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
42 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
44 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
48 * Information the service tracks per listen operation.
50 struct ServiceListenContext
56 struct ServiceListenContext *next;
61 struct ServiceListenContext *prev;
64 * Service this listen context belongs to.
66 struct GNUNET_SERVICE_Handle *sh;
69 * Socket we are listening on.
71 struct GNUNET_NETWORK_Handle *listen_socket;
74 * Task scheduled to do the listening.
76 struct GNUNET_SCHEDULER_Task *listen_task;
82 * Handle to a service.
84 struct GNUNET_SERVICE_Handle
89 const struct GNUNET_CONFIGURATION_Handle *cfg;
92 * Name of our service.
94 const char *service_name;
97 * Main service-specific task to run.
99 GNUNET_SERVICE_InitCallback service_init_cb;
102 * Function to call when clients connect.
104 GNUNET_SERVICE_ConnectHandler connect_cb;
107 * Function to call when clients disconnect / are disconnected.
109 GNUNET_SERVICE_DisconnectHandler disconnect_cb;
112 * Closure for @e service_init_cb, @e connect_cb, @e disconnect_cb.
117 * DLL of listen sockets used to accept new connections.
119 struct ServiceListenContext *slc_head;
122 * DLL of listen sockets used to accept new connections.
124 struct ServiceListenContext *slc_tail;
127 * Our clients, kept in a DLL.
129 struct GNUNET_SERVICE_Client *clients_head;
132 * Our clients, kept in a DLL.
134 struct GNUNET_SERVICE_Client *clients_tail;
137 * Message handlers to use for all clients.
139 const struct GNUNET_MQ_MessageHandler *handlers;
142 * Closure for @e task.
147 * IPv4 addresses that are not allowed to connect.
149 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_denied;
152 * IPv6 addresses that are not allowed to connect.
154 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_denied;
157 * IPv4 addresses that are allowed to connect (if not
158 * set, all are allowed).
160 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_allowed;
163 * IPv6 addresses that are allowed to connect (if not
164 * set, all are allowed).
166 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_allowed;
169 * Do we require a matching UID for UNIX domain socket connections?
170 * #GNUNET_NO means that the UID does not have to match (however,
171 * @e match_gid may still impose other access control checks).
176 * Do we require a matching GID for UNIX domain socket connections?
177 * Ignored if @e match_uid is #GNUNET_YES. Note that this is about
178 * checking that the client's UID is in our group OR that the
179 * client's GID is our GID. If both "match_gid" and @e match_uid are
180 * #GNUNET_NO, all users on the local system have access.
185 * Set to #GNUNET_YES if we got a shutdown signal and terminate
186 * the service if #have_non_monitor_clients() returns #GNUNET_YES.
193 enum GNUNET_SERVICE_Options options;
196 * If we are daemonizing, this FD is set to the
197 * pipe to the parent. Send '.' if we started
198 * ok, '!' if not. -1 if we are not daemonizing.
200 int ready_confirm_fd;
203 * Overall success/failure of the service start.
208 * If GNUNET_YES, consider unknown message types an error where the
209 * client is disconnected.
217 * Handle to a client that is connected to a service.
219 struct GNUNET_SERVICE_Client
225 struct GNUNET_SERVICE_Client *next;
230 struct GNUNET_SERVICE_Client *prev;
233 * Service that this client belongs to.
235 struct GNUNET_SERVICE_Handle *sh;
238 * Socket of this client.
240 struct GNUNET_NETWORK_Handle *sock;
243 * Message queue for the client.
245 struct GNUNET_MQ_Handle *mq;
248 * Tokenizer we use for processing incoming data.
250 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
253 * Task that warns about missing calls to
254 * #GNUNET_SERVICE_client_continue().
256 struct GNUNET_SCHEDULER_Task *warn_task;
259 * Task that receives data from the client to
260 * pass it to the handlers.
262 struct GNUNET_SCHEDULER_Task *recv_task;
265 * Task that transmit data to the client.
267 struct GNUNET_SCHEDULER_Task *send_task;
270 * User context value, value returned from
271 * the connect callback.
276 * Persist the file handle for this client no matter what happens,
277 * force the OS to close once the process actually dies. Should only
278 * be used in special cases!
283 * Is this client a 'monitor' client that should not be counted
284 * when deciding on destroying the server during soft shutdown?
285 * (see also #GNUNET_SERVICE_start)
290 * Type of last message processed (for warn_no_receive_done).
297 * Check if any of the clients we have left are unrelated to
300 * @param sh service to check clients for
301 * @return #GNUNET_YES if we have non-monitoring clients left
304 have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh)
306 struct GNUNET_SERVICE_Client *client;
308 for (client = sh->clients_head;NULL != client; client = client->next)
310 if (client->is_monitor)
319 * Shutdown task triggered when a service should be terminated.
320 * This considers active clients and the service options to see
321 * how this specific service is to be terminated, and depending
322 * on this proceeds with the shutdown logic.
324 * @param cls our `struct GNUNET_SERVICE_Handle`
327 service_shutdown (void *cls)
329 struct GNUNET_SERVICE_Handle *sh = cls;
333 case GNUNET_SERVICE_OPTION_NONE:
334 GNUNET_SERVICE_shutdown (sh);
336 case GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN:
337 /* This task should never be run if we are using
338 the manual shutdown. */
341 case GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN:
342 sh->got_shutdown = GNUNET_YES;
343 GNUNET_SERVICE_suspend (sh);
344 if (GNUNET_NO == have_non_monitor_clients (sh))
345 GNUNET_SERVICE_shutdown (sh);
352 * First task run by any service. Initializes our shutdown task,
353 * starts the listening operation on our listen sockets and launches
354 * the custom logic of the application service.
356 * @param cls our `struct GNUNET_SERVICE_Handle`
359 service_main (void *cls)
361 struct GNUNET_SERVICE_Handle *sh = cls;
363 if (GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN != sh->options)
364 GNUNET_SCHEDULER_add_shutdown (&service_shutdown,
366 GNUNET_SERVICE_resume (sh);
367 sh->service_init_cb (sh->cb_cls,
374 * Parse an IPv4 access control list.
376 * @param ret location where to write the ACL (set)
377 * @param sh service context to use to get the configuration
378 * @param option name of the ACL option to parse
379 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
383 process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret,
384 struct GNUNET_SERVICE_Handle *sh,
389 if (!GNUNET_CONFIGURATION_have_value (sh->cfg, sh->service_name, option))
394 GNUNET_break (GNUNET_OK ==
395 GNUNET_CONFIGURATION_get_value_string (sh->cfg,
398 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv4_policy (opt)))
400 LOG (GNUNET_ERROR_TYPE_WARNING,
401 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
406 return GNUNET_SYSERR;
414 * Parse an IPv6 access control list.
416 * @param ret location where to write the ACL (set)
417 * @param sh service context to use to get the configuration
418 * @param option name of the ACL option to parse
419 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
423 process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret,
424 struct GNUNET_SERVICE_Handle *sh,
429 if (!GNUNET_CONFIGURATION_have_value (sh->cfg, sh->service_name, option))
434 GNUNET_break (GNUNET_OK ==
435 GNUNET_CONFIGURATION_get_value_string (sh->cfg,
438 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv6_policy (opt)))
440 LOG (GNUNET_ERROR_TYPE_WARNING,
441 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
442 opt, sh->service_name, option);
444 return GNUNET_SYSERR;
452 * Add the given UNIX domain path as an address to the
453 * list (as the first entry).
455 * @param saddrs array to update
456 * @param saddrlens where to store the address length
457 * @param unixpath path to add
458 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
459 * parameter is ignore on systems other than LINUX
462 add_unixpath (struct sockaddr **saddrs,
463 socklen_t *saddrlens,
464 const char *unixpath,
468 struct sockaddr_un *un;
470 un = GNUNET_new (struct sockaddr_un);
471 un->sun_family = AF_UNIX;
472 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
474 if (GNUNET_YES == abstract)
475 un->sun_path[0] = '\0';
477 #if HAVE_SOCKADDR_IN_SIN_LEN
478 un->sun_len = (u_char) sizeof (struct sockaddr_un);
480 *saddrs = (struct sockaddr *) un;
481 *saddrlens = sizeof (struct sockaddr_un);
483 /* this function should never be called
484 * unless AF_UNIX is defined! */
491 * Get the list of addresses that a server for the given service
494 * @param service_name name of the service
495 * @param cfg configuration (which specifies the addresses)
496 * @param addrs set (call by reference) to an array of pointers to the
497 * addresses the server should bind to and listen on; the
498 * array will be NULL-terminated (on success)
499 * @param addr_lens set (call by reference) to an array of the lengths
500 * of the respective `struct sockaddr` struct in the @a addrs
502 * @return number of addresses found on success,
503 * #GNUNET_SYSERR if the configuration
504 * did not specify reasonable finding information or
505 * if it specified a hostname that could not be resolved;
506 * #GNUNET_NO if the number of addresses configured is
507 * zero (in this case, `*addrs` and `*addr_lens` will be
511 get_server_addresses (const char *service_name,
512 const struct GNUNET_CONFIGURATION_Handle *cfg,
513 struct sockaddr ***addrs,
514 socklen_t **addr_lens)
517 struct GNUNET_NETWORK_Handle *desc;
518 unsigned long long port;
520 struct addrinfo hints;
521 struct addrinfo *res;
522 struct addrinfo *pos;
523 struct addrinfo *next;
528 struct sockaddr **saddrs;
529 socklen_t *saddrlens;
535 if (GNUNET_CONFIGURATION_have_value (cfg,
541 GNUNET_CONFIGURATION_get_value_yesno (cfg,
544 return GNUNET_SYSERR;
547 disablev6 = GNUNET_NO;
551 /* probe IPv6 support */
552 desc = GNUNET_NETWORK_socket_create (PF_INET6,
557 if ((ENOBUFS == errno) ||
562 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
564 return GNUNET_SYSERR;
566 LOG (GNUNET_ERROR_TYPE_INFO,
567 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
570 disablev6 = GNUNET_YES;
574 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
580 if (GNUNET_CONFIGURATION_have_value (cfg,
585 GNUNET_CONFIGURATION_get_value_number (cfg,
590 LOG (GNUNET_ERROR_TYPE_ERROR,
591 _("Require valid port number for service `%s' in configuration!\n"),
596 LOG (GNUNET_ERROR_TYPE_ERROR,
597 _("Require valid port number for service `%s' in configuration!\n"),
599 return GNUNET_SYSERR;
603 if (GNUNET_CONFIGURATION_have_value (cfg,
607 GNUNET_break (GNUNET_OK ==
608 GNUNET_CONFIGURATION_get_value_string (cfg,
617 abstract = GNUNET_NO;
620 GNUNET_CONFIGURATION_have_value (cfg,
624 GNUNET_CONFIGURATION_get_value_filename (cfg,
628 (0 < strlen (unixpath)))
630 /* probe UNIX support */
631 struct sockaddr_un s_un;
633 if (strlen (unixpath) >= sizeof (s_un.sun_path))
635 LOG (GNUNET_ERROR_TYPE_WARNING,
636 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
638 (unsigned long long) sizeof (s_un.sun_path));
639 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
640 LOG (GNUNET_ERROR_TYPE_INFO,
641 _("Using `%s' instead\n"),
645 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
647 "USE_ABSTRACT_SOCKETS");
648 if (GNUNET_SYSERR == abstract)
649 abstract = GNUNET_NO;
651 if ((GNUNET_YES != abstract)
653 GNUNET_DISK_directory_create_for_file (unixpath)))
654 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
658 if (NULL != unixpath)
660 desc = GNUNET_NETWORK_socket_create (AF_UNIX,
665 if ((ENOBUFS == errno) ||
670 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
672 GNUNET_free_non_null (hostname);
673 GNUNET_free (unixpath);
674 return GNUNET_SYSERR;
676 LOG (GNUNET_ERROR_TYPE_INFO,
677 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
680 GNUNET_free (unixpath);
685 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
691 if ((0 == port) && (NULL == unixpath))
693 LOG (GNUNET_ERROR_TYPE_ERROR,
694 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
696 GNUNET_free_non_null (hostname);
697 return GNUNET_SYSERR;
701 saddrs = GNUNET_new_array (2,
703 saddrlens = GNUNET_new_array (2,
705 add_unixpath (saddrs,
709 GNUNET_free_non_null (unixpath);
710 GNUNET_free_non_null (hostname);
712 *addr_lens = saddrlens;
716 if (NULL != hostname)
718 LOG (GNUNET_ERROR_TYPE_DEBUG,
719 "Resolving `%s' since that is where `%s' will bind to.\n",
724 sizeof (struct addrinfo));
726 hints.ai_family = AF_INET;
727 hints.ai_protocol = IPPROTO_TCP;
728 if ((0 != (ret = getaddrinfo (hostname,
734 LOG (GNUNET_ERROR_TYPE_ERROR,
735 _("Failed to resolve `%s': %s\n"),
738 GNUNET_free (hostname);
739 GNUNET_free_non_null (unixpath);
740 return GNUNET_SYSERR;
744 while (NULL != (pos = next))
748 (pos->ai_family == AF_INET6) )
754 LOG (GNUNET_ERROR_TYPE_ERROR,
755 _("Failed to find %saddress for `%s'.\n"),
756 disablev6 ? "IPv4 " : "",
759 GNUNET_free (hostname);
760 GNUNET_free_non_null (unixpath);
761 return GNUNET_SYSERR;
764 if (NULL != unixpath)
766 saddrs = GNUNET_new_array (resi + 1,
768 saddrlens = GNUNET_new_array (resi + 1,
771 if (NULL != unixpath)
773 add_unixpath (saddrs,
780 while (NULL != (pos = next))
784 (AF_INET6 == pos->ai_family) )
786 if ( (IPPROTO_TCP != pos->ai_protocol) &&
787 (0 != pos->ai_protocol) )
788 continue; /* not TCP */
789 if ( (SOCK_STREAM != pos->ai_socktype) &&
790 (0 != pos->ai_socktype) )
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "Service `%s' will bind to `%s'\n",
795 GNUNET_a2s (pos->ai_addr,
797 if (AF_INET == pos->ai_family)
799 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
800 saddrlens[i] = pos->ai_addrlen;
801 saddrs[i] = GNUNET_malloc (saddrlens[i]);
802 GNUNET_memcpy (saddrs[i],
805 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
809 GNUNET_assert (AF_INET6 == pos->ai_family);
810 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
811 saddrlens[i] = pos->ai_addrlen;
812 saddrs[i] = GNUNET_malloc (saddrlens[i]);
813 GNUNET_memcpy (saddrs[i],
816 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
820 GNUNET_free (hostname);
826 /* will bind against everything, just set port */
831 if (NULL != unixpath)
834 saddrs = GNUNET_new_array (resi + 1,
836 saddrlens = GNUNET_new_array (resi + 1,
838 if (NULL != unixpath)
840 add_unixpath (saddrs,
846 saddrlens[i] = sizeof (struct sockaddr_in);
847 saddrs[i] = GNUNET_malloc (saddrlens[i]);
848 #if HAVE_SOCKADDR_IN_SIN_LEN
849 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
851 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
852 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
858 if (NULL != unixpath)
860 saddrs = GNUNET_new_array (resi + 1,
862 saddrlens = GNUNET_new_array (resi + 1,
865 if (NULL != unixpath)
867 add_unixpath (saddrs,
873 saddrlens[i] = sizeof (struct sockaddr_in6);
874 saddrs[i] = GNUNET_malloc (saddrlens[i]);
875 #if HAVE_SOCKADDR_IN_SIN_LEN
876 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
878 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
879 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
881 saddrlens[i] = sizeof (struct sockaddr_in);
882 saddrs[i] = GNUNET_malloc (saddrlens[i]);
883 #if HAVE_SOCKADDR_IN_SIN_LEN
884 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
886 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
887 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
890 GNUNET_free_non_null (unixpath);
892 *addr_lens = saddrlens;
899 * Read listen sockets from the parent process (ARM).
901 * @param sh service context to initialize
902 * @return NULL-terminated array of sockets on success,
903 * NULL if not ok (must bind yourself)
905 static struct GNUNET_NETWORK_Handle **
906 receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
908 static struct GNUNET_NETWORK_Handle **lsocks;
915 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
916 if ( (NULL == env_buf) ||
917 (strlen (env_buf) <= 0) )
919 /* Using W32 API directly here, because this pipe will
920 * never be used outside of this function, and it's just too much of a bother
921 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
923 lsocks_pipe = (HANDLE) strtoul (env_buf,
926 if ( (0 == lsocks_pipe) ||
927 (INVALID_HANDLE_VALUE == lsocks_pipe))
936 ret = ReadFile (lsocks_pipe,
942 (sizeof (count) != rd) ||
945 lsocks = GNUNET_new_array (count + 1,
946 struct GNUNET_NETWORK_Handle *);
949 for (i = 0; i < count; i++)
951 WSAPROTOCOL_INFOA pi;
955 ret = ReadFile (lsocks_pipe,
961 (sizeof (size) != rd) ||
962 (sizeof (pi) != size) )
964 ret = ReadFile (lsocks_pipe,
972 s = WSASocketA (pi.iAddressFamily,
977 WSA_FLAG_OVERLAPPED);
978 lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
979 if (NULL == lsocks[i])
981 else if (i == count - 1)
986 lsocks[count] = NULL;
990 CloseHandle (lsocks_pipe);
994 LOG (GNUNET_ERROR_TYPE_ERROR,
995 _("Could not access a pre-bound socket, will try to bind myself\n"));
996 for (i = 0; (i < count) && (NULL != lsocks[i]); i++)
997 GNUNET_break (0 == GNUNET_NETWORK_socket_close (lsocks[i]));
998 GNUNET_free (lsocks);
1007 * Create and initialize a listen socket for the server.
1009 * @param server_addr address to listen on
1010 * @param socklen length of @a server_addr
1011 * @return NULL on error, otherwise the listen socket
1013 static struct GNUNET_NETWORK_Handle *
1014 open_listen_socket (const struct sockaddr *server_addr,
1017 struct GNUNET_NETWORK_Handle *sock;
1021 switch (server_addr->sa_family)
1024 port = ntohs (((const struct sockaddr_in *) server_addr)->sin_port);
1027 port = ntohs (((const struct sockaddr_in6 *) server_addr)->sin6_port);
1037 sock = GNUNET_NETWORK_socket_create (server_addr->sa_family,
1042 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1047 /* bind the socket */
1048 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock,
1053 if (EADDRINUSE != errno)
1055 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
1056 * fail if we already took the port on IPv6; if both IPv4 and
1057 * IPv6 binds fail, then our caller will log using the
1058 * errno preserved in 'eno' */
1059 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1062 LOG (GNUNET_ERROR_TYPE_ERROR,
1063 _("`%s' failed for port %d (%s).\n"),
1066 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1072 LOG (GNUNET_ERROR_TYPE_WARNING,
1073 _("`%s' failed for port %d (%s): address already in use\n"),
1075 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1076 else if (AF_UNIX == server_addr->sa_family)
1078 LOG (GNUNET_ERROR_TYPE_WARNING,
1079 _("`%s' failed for `%s': address already in use\n"),
1081 GNUNET_a2s (server_addr, socklen));
1084 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
1088 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock,
1091 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1093 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
1098 LOG (GNUNET_ERROR_TYPE_DEBUG,
1099 "Server starts to listen on port %u.\n",
1106 * Setup service handle
1108 * Configuration may specify:
1109 * - PORT (where to bind to for TCP)
1110 * - UNIXPATH (where to bind to for UNIX domain sockets)
1111 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1112 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1113 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1114 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1115 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1116 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1118 * @param sh service context to initialize
1119 * @return #GNUNET_OK if configuration succeeded
1122 setup_service (struct GNUNET_SERVICE_Handle *sh)
1125 struct GNUNET_NETWORK_Handle **lsocks;
1132 if (GNUNET_CONFIGURATION_have_value
1137 if (GNUNET_SYSERR ==
1139 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1143 LOG (GNUNET_ERROR_TYPE_ERROR,
1144 _("Specified value for `%s' of service `%s' is invalid\n"),
1147 return GNUNET_SYSERR;
1151 tolerant = GNUNET_NO;
1156 if ( (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1161 (cnt < FD_SETSIZE) &&
1162 (cnt + 4 < FD_SETSIZE) )
1164 lsocks = GNUNET_new_array (cnt + 1,
1165 struct GNUNET_NETWORK_Handle *);
1168 flags = fcntl (3 + cnt,
1171 (0 != (flags & FD_CLOEXEC)) ||
1173 (lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1175 LOG (GNUNET_ERROR_TYPE_ERROR,
1176 _("Could not access pre-bound socket %u, will try to bind myself\n"),
1177 (unsigned int) 3 + cnt);
1179 while (NULL != lsocks[cnt])
1180 GNUNET_break (0 == GNUNET_NETWORK_socket_close (lsocks[cnt++]));
1181 GNUNET_free (lsocks);
1186 unsetenv ("LISTEN_FDS");
1189 if (NULL != getenv ("GNUNET_OS_READ_LSOCKS"))
1191 lsocks = receive_sockets_from_parent (sh);
1192 putenv ("GNUNET_OS_READ_LSOCKS=");
1198 /* listen only on inherited sockets if we have any */
1199 struct GNUNET_NETWORK_Handle **ls;
1201 for (ls = lsocks; NULL != *ls; ls++)
1203 struct ServiceListenContext *slc;
1205 slc = GNUNET_new (struct ServiceListenContext);
1206 slc->listen_socket = *ls;
1207 GNUNET_CONTAINER_DLL_insert (sh->slc_head,
1211 GNUNET_free (lsocks);
1215 struct sockaddr **addrs;
1216 socklen_t *addrlens;
1219 num = get_server_addresses (sh->service_name,
1223 if (GNUNET_SYSERR == num)
1224 return GNUNET_SYSERR;
1226 for (int i = 0; i < num; i++)
1228 struct ServiceListenContext *slc;
1230 slc = GNUNET_new (struct ServiceListenContext);
1231 slc->listen_socket = open_listen_socket (addrs[i],
1233 GNUNET_break (NULL != slc->listen_socket);
1234 GNUNET_CONTAINER_DLL_insert (sh->slc_head,
1240 sh->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1242 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1246 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1249 process_acl4 (&sh->v4_denied,
1252 process_acl4 (&sh->v4_allowed,
1255 process_acl6 (&sh->v6_denied,
1258 process_acl6 (&sh->v6_allowed,
1266 * Get the name of the user that'll be used
1267 * to provide the service.
1269 * @param sh service context
1270 * @return value of the 'USERNAME' option
1273 get_user_name (struct GNUNET_SERVICE_Handle *sh)
1278 GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1290 * @param sh service context
1291 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1294 set_user_id (struct GNUNET_SERVICE_Handle *sh)
1298 if (NULL == (user = get_user_name (sh)))
1299 return GNUNET_OK; /* keep */
1304 pws = getpwnam (user);
1307 LOG (GNUNET_ERROR_TYPE_ERROR,
1308 _("Cannot obtain information about user `%s': %s\n"),
1310 errno == 0 ? _("No such user") : STRERROR (errno));
1312 return GNUNET_SYSERR;
1314 if ( (0 != setgid (pws->pw_gid)) ||
1315 (0 != setegid (pws->pw_gid)) ||
1317 (0 != initgroups (user,
1320 (0 != setuid (pws->pw_uid)) ||
1321 (0 != seteuid (pws->pw_uid)))
1323 if ((0 != setregid (pws->pw_gid,
1325 (0 != setreuid (pws->pw_uid,
1328 LOG (GNUNET_ERROR_TYPE_ERROR,
1329 _("Cannot change user/group to `%s': %s\n"),
1333 return GNUNET_SYSERR;
1343 * Get the name of the file where we will
1344 * write the PID of the service.
1346 * @param sh service context
1347 * @return name of the file for the process ID
1350 get_pid_file_name (struct GNUNET_SERVICE_Handle *sh)
1355 GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1365 * Delete the PID file that was created by our parent.
1367 * @param sh service context
1370 pid_file_delete (struct GNUNET_SERVICE_Handle *sh)
1372 char *pif = get_pid_file_name (sh);
1375 return; /* no PID file */
1376 if (0 != UNLINK (pif))
1377 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1385 * Detach from terminal.
1387 * @param sh service context
1388 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1391 detach_terminal (struct GNUNET_SERVICE_Handle *sh)
1398 if (0 != PIPE (filedes))
1400 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1402 return GNUNET_SYSERR;
1407 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1409 return GNUNET_SYSERR;
1416 GNUNET_break (0 == CLOSE (filedes[1]));
1418 if (1 != READ (filedes[0],
1421 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1429 LOG (GNUNET_ERROR_TYPE_INFO,
1430 _("Service process failed to initialize\n"));
1433 LOG (GNUNET_ERROR_TYPE_INFO,
1434 _("Service process could not initialize server function\n"));
1437 LOG (GNUNET_ERROR_TYPE_INFO,
1438 _("Service process failed to report status\n"));
1441 exit (1); /* child reported error */
1443 GNUNET_break (0 == CLOSE (0));
1444 GNUNET_break (0 == CLOSE (1));
1445 GNUNET_break (0 == CLOSE (filedes[0]));
1446 nullfd = OPEN ("/dev/null",
1449 return GNUNET_SYSERR;
1450 /* set stdin/stdout to /dev/null */
1451 if ( (dup2 (nullfd, 0) < 0) ||
1452 (dup2 (nullfd, 1) < 0) )
1454 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1456 (void) CLOSE (nullfd);
1457 return GNUNET_SYSERR;
1459 (void) CLOSE (nullfd);
1460 /* Detach from controlling terminal */
1463 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1465 sh->ready_confirm_fd = filedes[1];
1467 /* FIXME: we probably need to do something else
1468 * elsewhere in order to fork the process itself... */
1476 * Creates the "main" function for a GNUnet service. You
1477 * should almost always use the #GNUNET_SERVICE_MAIN macro
1478 * instead of calling this function directly (except
1479 * for ARM, which should call this function directly).
1481 * The function will launch the service with the name @a service_name
1482 * using the @a service_options to configure its shutdown
1483 * behavior. Once the service is ready, the @a init_cb will be called
1484 * for service-specific initialization. @a init_cb will be given the
1485 * service handler which can be used to control the service's
1486 * availability. When clients connect or disconnect, the respective
1487 * @a connect_cb or @a disconnect_cb functions will be called. For
1488 * messages received from the clients, the respective @a handlers will
1489 * be invoked; for the closure of the handlers we use the return value
1490 * from the @a connect_cb invocation of the respective client.
1492 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
1493 * message to receive further messages from this client. If
1494 * #GNUNET_SERVICE_client_continue() is not called within a short
1495 * time, a warning will be logged. If delays are expected, services
1496 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
1497 * disable the warning.
1499 * Clients sending invalid messages (based on @a handlers) will be
1500 * dropped. Additionally, clients can be dropped at any time using
1501 * #GNUNET_SERVICE_client_drop().
1503 * @param argc number of command-line arguments in @a argv
1504 * @param argv array of command-line arguments
1505 * @param service_name name of the service to run
1506 * @param options options controlling shutdown of the service
1507 * @param service_init_cb function to call once the service is ready
1508 * @param connect_cb function to call whenever a client connects
1509 * @param disconnect_cb function to call whenever a client disconnects
1510 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
1511 * @param handlers NULL-terminated array of message handlers for the service,
1512 * the closure will be set to the value returned by
1513 * the @a connect_cb for the respective connection
1514 * @return 0 on success, non-zero on error
1517 GNUNET_SERVICE_ruN_ (int argc,
1519 const char *service_name,
1520 enum GNUNET_SERVICE_Options options,
1521 GNUNET_SERVICE_InitCallback service_init_cb,
1522 GNUNET_SERVICE_ConnectHandler connect_cb,
1523 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
1525 const struct GNUNET_MQ_MessageHandler *handlers)
1527 struct GNUNET_SERVICE_Handle sh;
1529 char *opt_cfg_filename;
1534 unsigned long long skew_offset;
1535 unsigned long long skew_variance;
1536 long long clock_offset;
1537 struct GNUNET_CONFIGURATION_Handle *cfg;
1541 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1542 GNUNET_GETOPT_OPTION_CFG_FILE (&opt_cfg_filename),
1543 {'d', "daemonize", NULL,
1544 gettext_noop ("do daemonize (detach from terminal)"), 0,
1545 GNUNET_GETOPT_set_one, &do_daemonize},
1546 GNUNET_GETOPT_OPTION_HELP (NULL),
1547 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1548 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1549 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1550 GNUNET_GETOPT_OPTION_END
1553 xdg = getenv ("XDG_CONFIG_HOME");
1555 GNUNET_asprintf (&cfg_filename,
1559 GNUNET_OS_project_data_get ()->config_file);
1561 cfg_filename = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
1563 sh.options = options;
1564 sh.cfg = cfg = GNUNET_CONFIGURATION_create ();
1565 sh.service_init_cb = service_init_cb;
1566 sh.connect_cb = connect_cb;
1567 sh.disconnect_cb = disconnect_cb;
1569 sh.handlers = handlers;
1571 /* setup subsystems */
1572 ret = GNUNET_GETOPT_run (service_name,
1576 if (GNUNET_SYSERR == ret)
1578 if (GNUNET_NO == ret)
1583 if (GNUNET_OK != GNUNET_log_setup (service_name,
1590 if (NULL == opt_cfg_filename)
1591 opt_cfg_filename = GNUNET_strdup (cfg_filename);
1592 if (GNUNET_YES == GNUNET_DISK_file_test (opt_cfg_filename))
1594 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg,
1597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1598 _("Malformed configuration file `%s', exit ...\n"),
1605 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg,
1608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1609 _("Malformed configuration, exit ...\n"));
1612 if (0 != strcmp (opt_cfg_filename,
1614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1615 _("Could not access configuration file `%s'\n"),
1618 if (GNUNET_OK != setup_service (&sh))
1620 if ( (1 == do_daemonize) &&
1621 (GNUNET_OK != detach_terminal (&sh)) )
1626 if (GNUNET_OK != set_user_id (&sh))
1628 LOG (GNUNET_ERROR_TYPE_DEBUG,
1629 "Service `%s' runs with configuration from `%s'\n",
1633 GNUNET_CONFIGURATION_get_value_number (sh.cfg,
1638 GNUNET_CONFIGURATION_get_value_number (sh.cfg,
1643 clock_offset = skew_offset - skew_variance;
1644 GNUNET_TIME_set_offset (clock_offset);
1645 LOG (GNUNET_ERROR_TYPE_DEBUG,
1646 "Skewing clock by %dll ms\n",
1649 /* actually run service */
1651 GNUNET_SCHEDULER_run (&service_main,
1654 if (1 == do_daemonize)
1655 pid_file_delete (&sh);
1658 if (-1 != sh.ready_confirm_fd)
1660 if (1 != WRITE (sh.ready_confirm_fd, err ? "I" : "S", 1))
1661 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1662 GNUNET_break (0 == CLOSE (sh.ready_confirm_fd));
1669 GNUNET_CONFIGURATION_have_value (sh.cfg,
1673 GNUNET_CONFIGURATION_get_value_string (sh.cfg,
1681 GAUGER (service_name,
1685 GNUNET_free (counter);
1689 GNUNET_SPEEDUP_stop_ ();
1690 GNUNET_CONFIGURATION_destroy (cfg);
1692 while (NULL != sh.slc_head)
1694 struct ServiceListenContext *slc = sh.slc_head;
1696 sh.slc_head = slc->next;
1697 // FIXME: destroy slc
1701 GNUNET_free_non_null (logfile);
1702 GNUNET_free_non_null (loglev);
1703 GNUNET_free (cfg_filename);
1704 GNUNET_free_non_null (opt_cfg_filename);
1705 GNUNET_free_non_null (sh.v4_denied);
1706 GNUNET_free_non_null (sh.v6_denied);
1707 GNUNET_free_non_null (sh.v4_allowed);
1708 GNUNET_free_non_null (sh.v6_allowed);
1710 return err ? GNUNET_SYSERR : sh.ret;
1715 * Suspend accepting connections from the listen socket temporarily.
1716 * Resume activity using #GNUNET_SERVICE_resume.
1718 * @param sh service to stop accepting connections.
1721 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
1723 struct ServiceListenContext *slc;
1725 for (slc = sh->slc_head; NULL != slc; slc = slc->next)
1727 if (NULL != slc->listen_task)
1729 GNUNET_SCHEDULER_cancel (slc->listen_task);
1730 slc->listen_task = NULL;
1737 * Signature of functions implementing the sending functionality of a
1740 * @param mq the message queue
1741 * @param msg the message to send
1742 * @param impl_state state of the implementation
1745 service_mq_send (struct GNUNET_MQ_Handle *mq,
1746 const struct GNUNET_MessageHeader *msg,
1749 // struct GNUNET_SERVICE_Client *client = cls;
1751 // FIXME 1: setup "client->send_task" for transmission.
1752 // FIXME 2: I seriously hope we do not need to make a copy of `msg`!
1753 // OPTIMIZATION: ideally, we'd like the ability to peak at the rest of
1754 // the queue and transmit more than one message if possible.
1759 * Implementation function that cancels the currently sent message.
1761 * @param mq message queue
1762 * @param impl_state state specific to the implementation
1765 service_mq_cancel (struct GNUNET_MQ_Handle *mq,
1768 // struct GNUNET_SERVICE_Client *client = cls;
1770 // FIXME: stop transmission! (must be possible, otherwise
1771 // we must have told MQ that the message was sent!)
1776 * Generic error handler, called with the appropriate
1777 * error code and the same closure specified at the creation of
1778 * the message queue.
1779 * Not every message queue implementation supports an error handler.
1781 * @param cls closure
1782 * @param error error code
1785 service_mq_error_handler (void *cls,
1786 enum GNUNET_MQ_Error error)
1788 struct GNUNET_SERVICE_Client *client = cls;
1795 * Functions with this signature are called whenever a
1796 * complete message is received by the tokenizer for a client.
1798 * Do not call #GNUNET_SERVER_mst_destroy() from within
1799 * the scope of this callback.
1801 * @param cls closure with the `struct GNUNET_SERVICE_Client *`
1802 * @param client_cls closure with the `struct GNUNET_SERVICE_Client *`
1803 * @param message the actual message
1804 * @return #GNUNET_OK on success (always)
1807 service_client_mst_cb (void *cls,
1809 const struct GNUNET_MessageHeader *message)
1811 struct GNUNET_SERVICE_Client *client = cls;
1813 GNUNET_MQ_inject_message (client->mq,
1820 * A client sent us data. Receive and process it. If we are done,
1821 * reschedule this task.
1823 * @param cls the `struct GNUNET_SERVICE_Client` that sent us data.
1826 service_client_recv (void *cls)
1828 struct GNUNET_SERVICE_Client *client = cls;
1830 // FIXME: read into buffer, pass to MST, then client->mq inject!
1831 // FIXME: revise MST API to avoid the memcpy!
1832 // i.e.: GNUNET_MST_read (client->sock);
1837 * We have successfully accepted a connection from a client. Now
1838 * setup the client (with the scheduler) and tell the application.
1840 * @param sh service that accepted the client
1841 * @param sock socket associated with the client
1844 start_client (struct GNUNET_SERVICE_Handle *sh,
1845 struct GNUNET_NETWORK_Handle *csock)
1847 struct GNUNET_SERVICE_Client *client;
1849 client = GNUNET_new (struct GNUNET_SERVICE_Client);
1850 GNUNET_CONTAINER_DLL_insert (sh->clients_head,
1854 client->sock = csock;
1855 client->mq = GNUNET_MQ_queue_for_callbacks (&service_mq_send,
1860 &service_mq_error_handler,
1862 client->mst = GNUNET_SERVER_mst_create (&service_client_mst_cb,
1864 client->user_context = sh->connect_cb (sh->cb_cls,
1867 GNUNET_MQ_set_handlers_closure (client->mq,
1868 client->user_context);
1870 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1872 &service_client_recv,
1878 * Check if the given IP address is in the list of IP addresses.
1880 * @param list a list of networks
1881 * @param add the IP to check (in network byte order)
1882 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
1885 check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list,
1886 const struct in_addr *add)
1893 while ( (0 != list[i].network.s_addr) ||
1894 (0 != list[i].netmask.s_addr) )
1896 if ((add->s_addr & list[i].netmask.s_addr) ==
1897 (list[i].network.s_addr & list[i].netmask.s_addr))
1906 * Check if the given IP address is in the list of IP addresses.
1908 * @param list a list of networks
1909 * @param ip the IP to check (in network byte order)
1910 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
1913 check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
1914 const struct in6_addr *ip)
1918 struct in6_addr zero;
1924 sizeof (struct in6_addr));
1927 while (0 != memcmp (&zero,
1929 sizeof (struct in6_addr)))
1931 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
1932 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
1933 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
1945 * We have a client. Accept the incoming socket(s) (and reschedule
1948 * @param cls the `struct ServiceListenContext` of the ready listen socket
1951 accept_client (void *cls)
1953 struct ServiceListenContext *slc = cls;
1954 struct GNUNET_SERVICE_Handle *sh = slc->sh;
1956 slc->listen_task = NULL;
1959 struct GNUNET_NETWORK_Handle *sock;
1960 const struct sockaddr_in *v4;
1961 const struct sockaddr_in6 *v6;
1962 struct sockaddr_storage sa;
1966 addrlen = sizeof (sa);
1967 sock = GNUNET_NETWORK_socket_accept (slc->listen_socket,
1968 (struct sockaddr *) &sa,
1972 switch (sa.ss_family)
1975 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1976 v4 = (const struct sockaddr_in *) &sa;
1977 ok = ( ( (NULL == sh->v4_allowed) ||
1978 (check_ipv4_listed (sh->v4_allowed,
1980 ( (NULL == sh->v4_denied) ||
1981 (! check_ipv4_listed (sh->v4_denied,
1982 &v4->sin_addr)) ) );
1985 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1986 v6 = (const struct sockaddr_in6 *) &sa;
1987 ok = ( ( (NULL == sh->v6_allowed) ||
1988 (check_ipv6_listed (sh->v6_allowed,
1989 &v6->sin6_addr))) &&
1990 ( (NULL == sh->v6_denied) ||
1991 (! check_ipv6_listed (sh->v6_denied,
1992 &v6->sin6_addr)) ) );
1996 ok = GNUNET_OK; /* controlled using file-system ACL now */
2000 LOG (GNUNET_ERROR_TYPE_WARNING,
2001 _("Unknown address family %d\n"),
2007 LOG (GNUNET_ERROR_TYPE_DEBUG,
2008 "Service rejected incoming connection from %s due to policy.\n",
2009 GNUNET_a2s ((const struct sockaddr *) &sa,
2011 GNUNET_NETWORK_socket_close (sock);
2014 LOG (GNUNET_ERROR_TYPE_DEBUG,
2015 "Service accepted incoming connection from %s.\n",
2016 GNUNET_a2s ((const struct sockaddr *) &sa,
2018 start_client (slc->sh,
2022 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2030 * Resume accepting connections from the listen socket.
2032 * @param sh service to resume accepting connections.
2035 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh)
2037 struct ServiceListenContext *slc;
2039 for (slc = sh->slc_head; NULL != slc; slc = slc->next)
2041 GNUNET_assert (NULL == slc->listen_task);
2043 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2052 * Continue receiving further messages from the given client.
2053 * Must be called after each message received.
2055 * @param c the client to continue receiving from
2058 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c)
2060 GNUNET_break (0); // not implemented
2065 * Disable the warning the server issues if a message is not
2066 * acknowledged in a timely fashion. Use this call if a client is
2067 * intentionally delayed for a while. Only applies to the current
2070 * @param c client for which to disable the warning
2073 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c)
2075 GNUNET_break (NULL != c->warn_task);
2076 if (NULL != c->warn_task)
2078 GNUNET_SCHEDULER_cancel (c->warn_task);
2079 c->warn_task = NULL;
2085 * Ask the server to disconnect from the given client. This is the
2086 * same as returning #GNUNET_SYSERR within the check procedure when
2087 * handling a message, wexcept that it allows dropping of a client even
2088 * when not handling a message from that client. The `disconnect_cb`
2089 * will be called on @a c even if the application closes the connection
2090 * using this function.
2092 * @param c client to disconnect now
2095 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
2097 struct GNUNET_SERVICE_Handle *sh = c->sh;
2099 GNUNET_CONTAINER_DLL_remove (sh->clients_head,
2102 sh->disconnect_cb (sh->cb_cls,
2105 if (NULL != c->warn_task)
2107 GNUNET_SCHEDULER_cancel (c->warn_task);
2108 c->warn_task = NULL;
2110 if (NULL != c->recv_task)
2112 GNUNET_SCHEDULER_cancel (c->recv_task);
2113 c->recv_task = NULL;
2115 if (NULL != c->send_task)
2117 GNUNET_SCHEDULER_cancel (c->send_task);
2118 c->send_task = NULL;
2120 GNUNET_SERVER_mst_destroy (c->mst);
2121 GNUNET_MQ_destroy (c->mq);
2122 if (GNUNET_NO == c->persist)
2124 GNUNET_NETWORK_socket_close (c->sock);
2128 GNUNET_NETWORK_socket_free_memory_only_ (c->sock);
2131 if ( (GNUNET_YES == sh->got_shutdown) &&
2132 (GNUNET_NO == have_non_monitor_clients (sh)) )
2133 GNUNET_SERVICE_shutdown (sh);
2138 * Explicitly stops the service.
2140 * @param sh server to shutdown
2143 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh)
2145 struct GNUNET_SERVICE_Client *client;
2147 GNUNET_SERVICE_suspend (sh);
2148 sh->got_shutdown = GNUNET_NO;
2149 while (NULL != (client = sh->clients_head))
2150 GNUNET_SERVICE_client_drop (client);
2155 * Set the 'monitor' flag on this client. Clients which have been
2156 * marked as 'monitors' won't prevent the server from shutting down
2157 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
2158 * that for "normal" clients we likely want to allow them to process
2159 * their requests; however, monitor-clients are likely to 'never'
2160 * disconnect during shutdown and thus will not be considered when
2161 * determining if the server should continue to exist after
2162 * shutdown has been triggered.
2164 * @param c client to mark as a monitor
2167 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c)
2169 c->is_monitor = GNUNET_YES;
2170 if ( (GNUNET_YES == c->sh->got_shutdown) &&
2171 (GNUNET_NO == have_non_monitor_clients (c->sh)) )
2172 GNUNET_SERVICE_shutdown (c->sh);
2177 * Set the persist option on this client. Indicates that the
2178 * underlying socket or fd should never really be closed. Used for
2179 * indicating process death.
2181 * @param c client to persist the socket (never to be closed)
2184 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c)
2186 c->persist = GNUNET_YES;
2190 /* end of service_new.c */