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.
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_MessageStreamTokenizer *mst;
252 * Task that warns about missing calls to
253 * #GNUNET_SERVICE_client_continue().
255 struct GNUNET_SCHEDULER_Task *warn_task;
258 * Task run to finish dropping the client after the stack has
261 struct GNUNET_SCHEDULER_Task *drop_task;
264 * Task that receives data from the client to
265 * pass it to the handlers.
267 struct GNUNET_SCHEDULER_Task *recv_task;
270 * Task that transmit data to the client.
272 struct GNUNET_SCHEDULER_Task *send_task;
275 * Pointer to the message to be transmitted by @e send_task.
277 const struct GNUNET_MessageHeader *msg;
280 * User context value, value returned from
281 * the connect callback.
286 * Time when we last gave a message from this client
287 * to the application.
289 struct GNUNET_TIME_Absolute warn_start;
292 * Current position in @e msg at which we are transmitting.
297 * Persist the file handle for this client no matter what happens,
298 * force the OS to close once the process actually dies. Should only
299 * be used in special cases!
304 * Is this client a 'monitor' client that should not be counted
305 * when deciding on destroying the server during soft shutdown?
306 * (see also #GNUNET_SERVICE_start)
311 * Are we waiting for the application to call #GNUNET_SERVICE_client_continue()?
316 * Type of last message processed (for warn_no_receive_done).
323 * Check if any of the clients we have left are unrelated to
326 * @param sh service to check clients for
327 * @return #GNUNET_YES if we have non-monitoring clients left
330 have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh)
332 struct GNUNET_SERVICE_Client *client;
334 for (client = sh->clients_head;NULL != client; client = client->next)
336 if (client->is_monitor)
345 * Shutdown task triggered when a service should be terminated.
346 * This considers active clients and the service options to see
347 * how this specific service is to be terminated, and depending
348 * on this proceeds with the shutdown logic.
350 * @param cls our `struct GNUNET_SERVICE_Handle`
353 service_shutdown (void *cls)
355 struct GNUNET_SERVICE_Handle *sh = cls;
359 case GNUNET_SERVICE_OPTION_NONE:
360 GNUNET_SERVICE_shutdown (sh);
362 case GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN:
363 /* This task should never be run if we are using
364 the manual shutdown. */
367 case GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN:
368 sh->got_shutdown = GNUNET_YES;
369 GNUNET_SERVICE_suspend (sh);
370 if (GNUNET_NO == have_non_monitor_clients (sh))
371 GNUNET_SERVICE_shutdown (sh);
378 * First task run by any service. Initializes our shutdown task,
379 * starts the listening operation on our listen sockets and launches
380 * the custom logic of the application service.
382 * @param cls our `struct GNUNET_SERVICE_Handle`
385 service_main (void *cls)
387 struct GNUNET_SERVICE_Handle *sh = cls;
389 if (GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN != sh->options)
390 GNUNET_SCHEDULER_add_shutdown (&service_shutdown,
392 GNUNET_SERVICE_resume (sh);
393 if (NULL != sh->service_init_cb)
394 sh->service_init_cb (sh->cb_cls,
401 * Parse an IPv4 access control list.
403 * @param ret location where to write the ACL (set)
404 * @param sh service context to use to get the configuration
405 * @param option name of the ACL option to parse
406 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
410 process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret,
411 struct GNUNET_SERVICE_Handle *sh,
416 if (! GNUNET_CONFIGURATION_have_value (sh->cfg,
423 GNUNET_break (GNUNET_OK ==
424 GNUNET_CONFIGURATION_get_value_string (sh->cfg,
428 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv4_policy (opt)))
430 LOG (GNUNET_ERROR_TYPE_WARNING,
431 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
436 return GNUNET_SYSERR;
444 * Parse an IPv6 access control list.
446 * @param ret location where to write the ACL (set)
447 * @param sh service context to use to get the configuration
448 * @param option name of the ACL option to parse
449 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
453 process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret,
454 struct GNUNET_SERVICE_Handle *sh,
459 if (! GNUNET_CONFIGURATION_have_value (sh->cfg,
466 GNUNET_break (GNUNET_OK ==
467 GNUNET_CONFIGURATION_get_value_string (sh->cfg,
471 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv6_policy (opt)))
473 LOG (GNUNET_ERROR_TYPE_WARNING,
474 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
479 return GNUNET_SYSERR;
487 * Add the given UNIX domain path as an address to the
488 * list (as the first entry).
490 * @param saddrs array to update
491 * @param saddrlens where to store the address length
492 * @param unixpath path to add
493 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
494 * parameter is ignore on systems other than LINUX
497 add_unixpath (struct sockaddr **saddrs,
498 socklen_t *saddrlens,
499 const char *unixpath,
503 struct sockaddr_un *un;
505 un = GNUNET_new (struct sockaddr_un);
506 un->sun_family = AF_UNIX;
507 strncpy (un->sun_path,
509 sizeof (un->sun_path) - 1);
511 if (GNUNET_YES == abstract)
512 un->sun_path[0] = '\0';
514 #if HAVE_SOCKADDR_IN_SIN_LEN
515 un->sun_len = (u_char) sizeof (struct sockaddr_un);
517 *saddrs = (struct sockaddr *) un;
518 *saddrlens = sizeof (struct sockaddr_un);
520 /* this function should never be called
521 * unless AF_UNIX is defined! */
528 * Get the list of addresses that a server for the given service
531 * @param service_name name of the service
532 * @param cfg configuration (which specifies the addresses)
533 * @param addrs set (call by reference) to an array of pointers to the
534 * addresses the server should bind to and listen on; the
535 * array will be NULL-terminated (on success)
536 * @param addr_lens set (call by reference) to an array of the lengths
537 * of the respective `struct sockaddr` struct in the @a addrs
539 * @return number of addresses found on success,
540 * #GNUNET_SYSERR if the configuration
541 * did not specify reasonable finding information or
542 * if it specified a hostname that could not be resolved;
543 * #GNUNET_NO if the number of addresses configured is
544 * zero (in this case, `*addrs` and `*addr_lens` will be
548 get_server_addresses (const char *service_name,
549 const struct GNUNET_CONFIGURATION_Handle *cfg,
550 struct sockaddr ***addrs,
551 socklen_t **addr_lens)
554 struct GNUNET_NETWORK_Handle *desc;
555 unsigned long long port;
557 struct addrinfo hints;
558 struct addrinfo *res;
559 struct addrinfo *pos;
560 struct addrinfo *next;
565 struct sockaddr **saddrs;
566 socklen_t *saddrlens;
572 if (GNUNET_CONFIGURATION_have_value (cfg,
578 GNUNET_CONFIGURATION_get_value_yesno (cfg,
581 return GNUNET_SYSERR;
584 disablev6 = GNUNET_NO;
588 /* probe IPv6 support */
589 desc = GNUNET_NETWORK_socket_create (PF_INET6,
594 if ( (ENOBUFS == errno) ||
599 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
601 return GNUNET_SYSERR;
603 LOG (GNUNET_ERROR_TYPE_INFO,
604 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
607 disablev6 = GNUNET_YES;
611 GNUNET_break (GNUNET_OK ==
612 GNUNET_NETWORK_socket_close (desc));
618 if (GNUNET_CONFIGURATION_have_value (cfg,
623 GNUNET_CONFIGURATION_get_value_number (cfg,
628 LOG (GNUNET_ERROR_TYPE_ERROR,
629 _("Require valid port number for service `%s' in configuration!\n"),
634 LOG (GNUNET_ERROR_TYPE_ERROR,
635 _("Require valid port number for service `%s' in configuration!\n"),
637 return GNUNET_SYSERR;
641 if (GNUNET_CONFIGURATION_have_value (cfg,
645 GNUNET_break (GNUNET_OK ==
646 GNUNET_CONFIGURATION_get_value_string (cfg,
655 abstract = GNUNET_NO;
658 GNUNET_CONFIGURATION_have_value (cfg,
662 GNUNET_CONFIGURATION_get_value_filename (cfg,
666 (0 < strlen (unixpath)))
668 /* probe UNIX support */
669 struct sockaddr_un s_un;
671 if (strlen (unixpath) >= sizeof (s_un.sun_path))
673 LOG (GNUNET_ERROR_TYPE_WARNING,
674 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
676 (unsigned long long) sizeof (s_un.sun_path));
677 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
678 LOG (GNUNET_ERROR_TYPE_INFO,
679 _("Using `%s' instead\n"),
683 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
685 "USE_ABSTRACT_SOCKETS");
686 if (GNUNET_SYSERR == abstract)
687 abstract = GNUNET_NO;
689 if ( (GNUNET_YES != abstract) &&
691 GNUNET_DISK_directory_create_for_file (unixpath)) )
692 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
696 if (NULL != unixpath)
698 desc = GNUNET_NETWORK_socket_create (AF_UNIX,
703 if ((ENOBUFS == errno) ||
708 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
710 GNUNET_free_non_null (hostname);
711 GNUNET_free (unixpath);
712 return GNUNET_SYSERR;
714 LOG (GNUNET_ERROR_TYPE_INFO,
715 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
718 GNUNET_free (unixpath);
723 GNUNET_break (GNUNET_OK ==
724 GNUNET_NETWORK_socket_close (desc));
730 if ((0 == port) && (NULL == unixpath))
732 LOG (GNUNET_ERROR_TYPE_ERROR,
733 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
735 GNUNET_free_non_null (hostname);
736 return GNUNET_SYSERR;
740 saddrs = GNUNET_new_array (2,
742 saddrlens = GNUNET_new_array (2,
744 add_unixpath (saddrs,
748 GNUNET_free_non_null (unixpath);
749 GNUNET_free_non_null (hostname);
751 *addr_lens = saddrlens;
755 if (NULL != hostname)
757 LOG (GNUNET_ERROR_TYPE_DEBUG,
758 "Resolving `%s' since that is where `%s' will bind to.\n",
763 sizeof (struct addrinfo));
765 hints.ai_family = AF_INET;
766 hints.ai_protocol = IPPROTO_TCP;
767 if ((0 != (ret = getaddrinfo (hostname,
773 LOG (GNUNET_ERROR_TYPE_ERROR,
774 _("Failed to resolve `%s': %s\n"),
777 GNUNET_free (hostname);
778 GNUNET_free_non_null (unixpath);
779 return GNUNET_SYSERR;
783 while (NULL != (pos = next))
787 (pos->ai_family == AF_INET6) )
793 LOG (GNUNET_ERROR_TYPE_ERROR,
794 _("Failed to find %saddress for `%s'.\n"),
795 disablev6 ? "IPv4 " : "",
798 GNUNET_free (hostname);
799 GNUNET_free_non_null (unixpath);
800 return GNUNET_SYSERR;
803 if (NULL != unixpath)
805 saddrs = GNUNET_new_array (resi + 1,
807 saddrlens = GNUNET_new_array (resi + 1,
810 if (NULL != unixpath)
812 add_unixpath (saddrs,
819 while (NULL != (pos = next))
823 (AF_INET6 == pos->ai_family) )
825 if ( (IPPROTO_TCP != pos->ai_protocol) &&
826 (0 != pos->ai_protocol) )
827 continue; /* not TCP */
828 if ( (SOCK_STREAM != pos->ai_socktype) &&
829 (0 != pos->ai_socktype) )
831 LOG (GNUNET_ERROR_TYPE_DEBUG,
832 "Service `%s' will bind to `%s'\n",
834 GNUNET_a2s (pos->ai_addr,
836 if (AF_INET == pos->ai_family)
838 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
839 saddrlens[i] = pos->ai_addrlen;
840 saddrs[i] = GNUNET_malloc (saddrlens[i]);
841 GNUNET_memcpy (saddrs[i],
844 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
848 GNUNET_assert (AF_INET6 == pos->ai_family);
849 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
850 saddrlens[i] = pos->ai_addrlen;
851 saddrs[i] = GNUNET_malloc (saddrlens[i]);
852 GNUNET_memcpy (saddrs[i],
855 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
859 GNUNET_free (hostname);
865 /* will bind against everything, just set port */
870 if (NULL != unixpath)
873 saddrs = GNUNET_new_array (resi + 1,
875 saddrlens = GNUNET_new_array (resi + 1,
877 if (NULL != unixpath)
879 add_unixpath (saddrs,
885 saddrlens[i] = sizeof (struct sockaddr_in);
886 saddrs[i] = GNUNET_malloc (saddrlens[i]);
887 #if HAVE_SOCKADDR_IN_SIN_LEN
888 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
890 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
891 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
897 if (NULL != unixpath)
899 saddrs = GNUNET_new_array (resi + 1,
901 saddrlens = GNUNET_new_array (resi + 1,
904 if (NULL != unixpath)
906 add_unixpath (saddrs,
912 saddrlens[i] = sizeof (struct sockaddr_in6);
913 saddrs[i] = GNUNET_malloc (saddrlens[i]);
914 #if HAVE_SOCKADDR_IN_SIN_LEN
915 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
917 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
918 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
920 saddrlens[i] = sizeof (struct sockaddr_in);
921 saddrs[i] = GNUNET_malloc (saddrlens[i]);
922 #if HAVE_SOCKADDR_IN_SIN_LEN
923 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
925 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
926 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
929 GNUNET_free_non_null (unixpath);
931 *addr_lens = saddrlens;
938 * Read listen sockets from the parent process (ARM).
940 * @param sh service context to initialize
941 * @return NULL-terminated array of sockets on success,
942 * NULL if not ok (must bind yourself)
944 static struct GNUNET_NETWORK_Handle **
945 receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
947 static struct GNUNET_NETWORK_Handle **lsocks;
954 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
955 if ( (NULL == env_buf) ||
956 (strlen (env_buf) <= 0) )
958 /* Using W32 API directly here, because this pipe will
959 * never be used outside of this function, and it's just too much of a bother
960 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
962 lsocks_pipe = (HANDLE) strtoul (env_buf,
965 if ( (0 == lsocks_pipe) ||
966 (INVALID_HANDLE_VALUE == lsocks_pipe))
975 ret = ReadFile (lsocks_pipe,
981 (sizeof (count) != rd) ||
984 lsocks = GNUNET_new_array (count + 1,
985 struct GNUNET_NETWORK_Handle *);
988 for (i = 0; i < count; i++)
990 WSAPROTOCOL_INFOA pi;
994 ret = ReadFile (lsocks_pipe,
1000 (sizeof (size) != rd) ||
1001 (sizeof (pi) != size) )
1003 ret = ReadFile (lsocks_pipe,
1009 (sizeof (pi) != rd))
1011 s = WSASocketA (pi.iAddressFamily,
1016 WSA_FLAG_OVERLAPPED);
1017 lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1018 if (NULL == lsocks[i])
1020 else if (i == count - 1)
1025 lsocks[count] = NULL;
1029 CloseHandle (lsocks_pipe);
1033 LOG (GNUNET_ERROR_TYPE_ERROR,
1034 _("Could not access a pre-bound socket, will try to bind myself\n"));
1035 for (i = 0; (i < count) && (NULL != lsocks[i]); i++)
1036 GNUNET_break (GNUNET_OK ==
1037 GNUNET_NETWORK_socket_close (lsocks[i]));
1038 GNUNET_free (lsocks);
1047 * Create and initialize a listen socket for the server.
1049 * @param server_addr address to listen on
1050 * @param socklen length of @a server_addr
1051 * @return NULL on error, otherwise the listen socket
1053 static struct GNUNET_NETWORK_Handle *
1054 open_listen_socket (const struct sockaddr *server_addr,
1057 struct GNUNET_NETWORK_Handle *sock;
1061 switch (server_addr->sa_family)
1064 port = ntohs (((const struct sockaddr_in *) server_addr)->sin_port);
1067 port = ntohs (((const struct sockaddr_in6 *) server_addr)->sin6_port);
1077 sock = GNUNET_NETWORK_socket_create (server_addr->sa_family,
1082 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1087 /* bind the socket */
1088 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock,
1093 if (EADDRINUSE != errno)
1095 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
1096 * fail if we already took the port on IPv6; if both IPv4 and
1097 * IPv6 binds fail, then our caller will log using the
1098 * errno preserved in 'eno' */
1099 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1102 LOG (GNUNET_ERROR_TYPE_ERROR,
1103 _("`%s' failed for port %d (%s).\n"),
1106 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1112 LOG (GNUNET_ERROR_TYPE_WARNING,
1113 _("`%s' failed for port %d (%s): address already in use\n"),
1115 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1116 else if (AF_UNIX == server_addr->sa_family)
1118 LOG (GNUNET_ERROR_TYPE_WARNING,
1119 _("`%s' failed for `%s': address already in use\n"),
1121 GNUNET_a2s (server_addr, socklen));
1124 GNUNET_break (GNUNET_OK ==
1125 GNUNET_NETWORK_socket_close (sock));
1129 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock,
1132 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1134 GNUNET_break (GNUNET_OK ==
1135 GNUNET_NETWORK_socket_close (sock));
1140 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "Server starts to listen on port %u.\n",
1148 * Setup service handle
1150 * Configuration may specify:
1151 * - PORT (where to bind to for TCP)
1152 * - UNIXPATH (where to bind to for UNIX domain sockets)
1153 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1154 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1155 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1156 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1157 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1158 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1160 * @param sh service context to initialize
1161 * @return #GNUNET_OK if configuration succeeded
1164 setup_service (struct GNUNET_SERVICE_Handle *sh)
1167 struct GNUNET_NETWORK_Handle **lsocks;
1174 if (GNUNET_CONFIGURATION_have_value
1179 if (GNUNET_SYSERR ==
1181 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1185 LOG (GNUNET_ERROR_TYPE_ERROR,
1186 _("Specified value for `%s' of service `%s' is invalid\n"),
1189 return GNUNET_SYSERR;
1193 tolerant = GNUNET_NO;
1198 if ( (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1203 (cnt < FD_SETSIZE) &&
1204 (cnt + 4 < FD_SETSIZE) )
1206 lsocks = GNUNET_new_array (cnt + 1,
1207 struct GNUNET_NETWORK_Handle *);
1210 flags = fcntl (3 + cnt,
1213 (0 != (flags & FD_CLOEXEC)) ||
1215 (lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1217 LOG (GNUNET_ERROR_TYPE_ERROR,
1218 _("Could not access pre-bound socket %u, will try to bind myself\n"),
1219 (unsigned int) 3 + cnt);
1221 while (NULL != lsocks[cnt])
1222 GNUNET_break (GNUNET_OK ==
1223 GNUNET_NETWORK_socket_close (lsocks[cnt++]));
1224 GNUNET_free (lsocks);
1229 unsetenv ("LISTEN_FDS");
1232 if (NULL != getenv ("GNUNET_OS_READ_LSOCKS"))
1234 lsocks = receive_sockets_from_parent (sh);
1235 putenv ("GNUNET_OS_READ_LSOCKS=");
1241 /* listen only on inherited sockets if we have any */
1242 struct GNUNET_NETWORK_Handle **ls;
1244 for (ls = lsocks; NULL != *ls; ls++)
1246 struct ServiceListenContext *slc;
1248 slc = GNUNET_new (struct ServiceListenContext);
1250 slc->listen_socket = *ls;
1251 GNUNET_CONTAINER_DLL_insert (sh->slc_head,
1255 GNUNET_free (lsocks);
1259 struct sockaddr **addrs;
1260 socklen_t *addrlens;
1263 num = get_server_addresses (sh->service_name,
1267 if (GNUNET_SYSERR == num)
1268 return GNUNET_SYSERR;
1270 for (int i = 0; i < num; i++)
1272 struct ServiceListenContext *slc;
1274 slc = GNUNET_new (struct ServiceListenContext);
1276 slc->listen_socket = open_listen_socket (addrs[i],
1278 GNUNET_break (NULL != slc->listen_socket);
1279 GNUNET_CONTAINER_DLL_insert (sh->slc_head,
1285 sh->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1287 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1291 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1294 process_acl4 (&sh->v4_denied,
1297 process_acl4 (&sh->v4_allowed,
1300 process_acl6 (&sh->v6_denied,
1303 process_acl6 (&sh->v6_allowed,
1311 * Get the name of the user that'll be used
1312 * to provide the service.
1314 * @param sh service context
1315 * @return value of the 'USERNAME' option
1318 get_user_name (struct GNUNET_SERVICE_Handle *sh)
1323 GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1335 * @param sh service context
1336 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1339 set_user_id (struct GNUNET_SERVICE_Handle *sh)
1343 if (NULL == (user = get_user_name (sh)))
1344 return GNUNET_OK; /* keep */
1349 pws = getpwnam (user);
1352 LOG (GNUNET_ERROR_TYPE_ERROR,
1353 _("Cannot obtain information about user `%s': %s\n"),
1355 errno == 0 ? _("No such user") : STRERROR (errno));
1357 return GNUNET_SYSERR;
1359 if ( (0 != setgid (pws->pw_gid)) ||
1360 (0 != setegid (pws->pw_gid)) ||
1362 (0 != initgroups (user,
1365 (0 != setuid (pws->pw_uid)) ||
1366 (0 != seteuid (pws->pw_uid)))
1368 if ((0 != setregid (pws->pw_gid,
1370 (0 != setreuid (pws->pw_uid,
1373 LOG (GNUNET_ERROR_TYPE_ERROR,
1374 _("Cannot change user/group to `%s': %s\n"),
1378 return GNUNET_SYSERR;
1388 * Get the name of the file where we will
1389 * write the PID of the service.
1391 * @param sh service context
1392 * @return name of the file for the process ID
1395 get_pid_file_name (struct GNUNET_SERVICE_Handle *sh)
1400 GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1410 * Delete the PID file that was created by our parent.
1412 * @param sh service context
1415 pid_file_delete (struct GNUNET_SERVICE_Handle *sh)
1417 char *pif = get_pid_file_name (sh);
1420 return; /* no PID file */
1421 if (0 != UNLINK (pif))
1422 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1430 * Detach from terminal.
1432 * @param sh service context
1433 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1436 detach_terminal (struct GNUNET_SERVICE_Handle *sh)
1443 if (0 != PIPE (filedes))
1445 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1447 return GNUNET_SYSERR;
1452 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1454 return GNUNET_SYSERR;
1461 GNUNET_break (0 == CLOSE (filedes[1]));
1463 if (1 != READ (filedes[0],
1466 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1474 LOG (GNUNET_ERROR_TYPE_INFO,
1475 _("Service process failed to initialize\n"));
1478 LOG (GNUNET_ERROR_TYPE_INFO,
1479 _("Service process could not initialize server function\n"));
1482 LOG (GNUNET_ERROR_TYPE_INFO,
1483 _("Service process failed to report status\n"));
1486 exit (1); /* child reported error */
1488 GNUNET_break (0 == CLOSE (0));
1489 GNUNET_break (0 == CLOSE (1));
1490 GNUNET_break (0 == CLOSE (filedes[0]));
1491 nullfd = OPEN ("/dev/null",
1494 return GNUNET_SYSERR;
1495 /* set stdin/stdout to /dev/null */
1496 if ( (dup2 (nullfd, 0) < 0) ||
1497 (dup2 (nullfd, 1) < 0) )
1499 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1501 (void) CLOSE (nullfd);
1502 return GNUNET_SYSERR;
1504 (void) CLOSE (nullfd);
1505 /* Detach from controlling terminal */
1508 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1510 sh->ready_confirm_fd = filedes[1];
1512 /* FIXME: we probably need to do something else
1513 * elsewhere in order to fork the process itself... */
1521 * Creates the "main" function for a GNUnet service. You
1522 * should almost always use the #GNUNET_SERVICE_MAIN macro
1523 * instead of calling this function directly (except
1524 * for ARM, which should call this function directly).
1526 * The function will launch the service with the name @a service_name
1527 * using the @a service_options to configure its shutdown
1528 * behavior. Once the service is ready, the @a init_cb will be called
1529 * for service-specific initialization. @a init_cb will be given the
1530 * service handler which can be used to control the service's
1531 * availability. When clients connect or disconnect, the respective
1532 * @a connect_cb or @a disconnect_cb functions will be called. For
1533 * messages received from the clients, the respective @a handlers will
1534 * be invoked; for the closure of the handlers we use the return value
1535 * from the @a connect_cb invocation of the respective client.
1537 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
1538 * message to receive further messages from this client. If
1539 * #GNUNET_SERVICE_client_continue() is not called within a short
1540 * time, a warning will be logged. If delays are expected, services
1541 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
1542 * disable the warning.
1544 * Clients sending invalid messages (based on @a handlers) will be
1545 * dropped. Additionally, clients can be dropped at any time using
1546 * #GNUNET_SERVICE_client_drop().
1548 * @param argc number of command-line arguments in @a argv
1549 * @param argv array of command-line arguments
1550 * @param service_name name of the service to run
1551 * @param options options controlling shutdown of the service
1552 * @param service_init_cb function to call once the service is ready
1553 * @param connect_cb function to call whenever a client connects
1554 * @param disconnect_cb function to call whenever a client disconnects
1555 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
1556 * @param handlers NULL-terminated array of message handlers for the service,
1557 * the closure will be set to the value returned by
1558 * the @a connect_cb for the respective connection
1559 * @return 0 on success, non-zero on error
1562 GNUNET_SERVICE_ruN_ (int argc,
1564 const char *service_name,
1565 enum GNUNET_SERVICE_Options options,
1566 GNUNET_SERVICE_InitCallback service_init_cb,
1567 GNUNET_SERVICE_ConnectHandler connect_cb,
1568 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
1570 const struct GNUNET_MQ_MessageHandler *handlers)
1572 struct GNUNET_SERVICE_Handle sh;
1574 char *opt_cfg_filename;
1579 unsigned long long skew_offset;
1580 unsigned long long skew_variance;
1581 long long clock_offset;
1582 struct GNUNET_CONFIGURATION_Handle *cfg;
1586 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1587 GNUNET_GETOPT_OPTION_CFG_FILE (&opt_cfg_filename),
1588 {'d', "daemonize", NULL,
1589 gettext_noop ("do daemonize (detach from terminal)"), 0,
1590 GNUNET_GETOPT_set_one, &do_daemonize},
1591 GNUNET_GETOPT_OPTION_HELP (NULL),
1592 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1593 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1594 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1595 GNUNET_GETOPT_OPTION_END
1601 xdg = getenv ("XDG_CONFIG_HOME");
1603 GNUNET_asprintf (&cfg_filename,
1607 GNUNET_OS_project_data_get ()->config_file);
1609 cfg_filename = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
1610 sh.ready_confirm_fd = -1;
1611 sh.options = options;
1612 sh.cfg = cfg = GNUNET_CONFIGURATION_create ();
1613 sh.service_init_cb = service_init_cb;
1614 sh.connect_cb = connect_cb;
1615 sh.disconnect_cb = disconnect_cb;
1617 sh.handlers = handlers;
1618 sh.service_name = service_name;
1620 /* setup subsystems */
1623 opt_cfg_filename = NULL;
1625 ret = GNUNET_GETOPT_run (service_name,
1629 if (GNUNET_SYSERR == ret)
1631 if (GNUNET_NO == ret)
1636 if (GNUNET_OK != GNUNET_log_setup (service_name,
1643 if (NULL == opt_cfg_filename)
1644 opt_cfg_filename = GNUNET_strdup (cfg_filename);
1645 if (GNUNET_YES == GNUNET_DISK_file_test (opt_cfg_filename))
1647 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg,
1650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1651 _("Malformed configuration file `%s', exit ...\n"),
1658 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg,
1661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1662 _("Malformed configuration, exit ...\n"));
1665 if (0 != strcmp (opt_cfg_filename,
1667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1668 _("Could not access configuration file `%s'\n"),
1671 if (GNUNET_OK != setup_service (&sh))
1673 if ( (1 == do_daemonize) &&
1674 (GNUNET_OK != detach_terminal (&sh)) )
1679 if (GNUNET_OK != set_user_id (&sh))
1681 LOG (GNUNET_ERROR_TYPE_DEBUG,
1682 "Service `%s' runs with configuration from `%s'\n",
1686 GNUNET_CONFIGURATION_get_value_number (sh.cfg,
1691 GNUNET_CONFIGURATION_get_value_number (sh.cfg,
1696 clock_offset = skew_offset - skew_variance;
1697 GNUNET_TIME_set_offset (clock_offset);
1698 LOG (GNUNET_ERROR_TYPE_DEBUG,
1699 "Skewing clock by %dll ms\n",
1702 GNUNET_RESOLVER_connect (sh.cfg);
1704 /* actually run service */
1706 GNUNET_SCHEDULER_run (&service_main,
1709 if (1 == do_daemonize)
1710 pid_file_delete (&sh);
1713 if (-1 != sh.ready_confirm_fd)
1715 if (1 != WRITE (sh.ready_confirm_fd,
1718 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1720 GNUNET_break (0 == CLOSE (sh.ready_confirm_fd));
1727 GNUNET_CONFIGURATION_have_value (sh.cfg,
1731 GNUNET_CONFIGURATION_get_value_string (sh.cfg,
1739 GAUGER (service_name,
1743 GNUNET_free (counter);
1747 GNUNET_SPEEDUP_stop_ ();
1748 GNUNET_CONFIGURATION_destroy (cfg);
1750 while (NULL != sh.slc_head)
1752 struct ServiceListenContext *slc = sh.slc_head;
1754 sh.slc_head = slc->next;
1755 if (NULL != slc->listen_task)
1756 GNUNET_SCHEDULER_cancel (slc->listen_task);
1757 GNUNET_break (GNUNET_OK ==
1758 GNUNET_NETWORK_socket_close (slc->listen_socket));
1762 GNUNET_free_non_null (logfile);
1763 GNUNET_free_non_null (loglev);
1764 GNUNET_free (cfg_filename);
1765 GNUNET_free_non_null (opt_cfg_filename);
1766 GNUNET_free_non_null (sh.v4_denied);
1767 GNUNET_free_non_null (sh.v6_denied);
1768 GNUNET_free_non_null (sh.v4_allowed);
1769 GNUNET_free_non_null (sh.v6_allowed);
1771 return err ? GNUNET_SYSERR : sh.ret;
1776 * Suspend accepting connections from the listen socket temporarily.
1777 * Resume activity using #GNUNET_SERVICE_resume.
1779 * @param sh service to stop accepting connections.
1782 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
1784 struct ServiceListenContext *slc;
1786 for (slc = sh->slc_head; NULL != slc; slc = slc->next)
1788 if (NULL != slc->listen_task)
1790 GNUNET_SCHEDULER_cancel (slc->listen_task);
1791 slc->listen_task = NULL;
1798 * Task run when we are ready to transmit data to the
1801 * @param cls the `struct GNUNET_SERVICE_Client *` to send to
1806 struct GNUNET_SERVICE_Client *client = cls;
1811 client->send_task = NULL;
1812 buf = (const char *) client->msg;
1813 left = ntohs (client->msg->size) - client->msg_pos;
1814 ret = GNUNET_NETWORK_socket_send (client->sock,
1815 &buf[client->msg_pos],
1817 GNUNET_assert (ret <= (ssize_t) left);
1820 GNUNET_MQ_inject_error (client->mq,
1821 GNUNET_MQ_ERROR_WRITE);
1826 if ( (EAGAIN == errno) ||
1834 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1836 GNUNET_MQ_inject_error (client->mq,
1837 GNUNET_MQ_ERROR_WRITE);
1841 client->msg_pos += ret;
1845 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1851 GNUNET_MQ_impl_send_continue (client->mq);
1856 * Signature of functions implementing the sending functionality of a
1859 * @param mq the message queue
1860 * @param msg the message to send
1861 * @param impl_state our `struct GNUNET_SERVICE_Client *`
1864 service_mq_send (struct GNUNET_MQ_Handle *mq,
1865 const struct GNUNET_MessageHeader *msg,
1868 struct GNUNET_SERVICE_Client *client = impl_state;
1870 GNUNET_assert (NULL == client->send_task);
1872 client->msg_pos = 0;
1874 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1882 * Implementation function that cancels the currently sent message.
1884 * @param mq message queue
1885 * @param impl_state state specific to the implementation
1888 service_mq_cancel (struct GNUNET_MQ_Handle *mq,
1891 struct GNUNET_SERVICE_Client *client = impl_state;
1893 GNUNET_assert (0); // not implemented
1894 // FIXME: stop transmission! (must be possible, otherwise
1895 // we must have told MQ that the message was sent!)
1900 * Generic error handler, called with the appropriate
1901 * error code and the same closure specified at the creation of
1902 * the message queue.
1903 * Not every message queue implementation supports an error handler.
1905 * @param cls closure with our `struct GNUNET_SERVICE_Client`
1906 * @param error error code
1909 service_mq_error_handler (void *cls,
1910 enum GNUNET_MQ_Error error)
1912 struct GNUNET_SERVICE_Client *client = cls;
1913 struct GNUNET_SERVICE_Handle *sh = client->sh;
1915 if ( (GNUNET_MQ_ERROR_NO_MATCH == error) &&
1916 (GNUNET_NO == sh->require_found) )
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1919 "No handler for message of type %u found\n",
1920 (unsigned int) client->warn_type);
1921 GNUNET_SERVICE_client_continue (client);
1922 return; /* ignore error */
1924 GNUNET_SERVICE_client_drop (client);
1929 * Task run to warn about missing calls to #GNUNET_SERVICE_client_continue().
1931 * @param cls our `struct GNUNET_SERVICE_Client *` to process more requests from
1934 warn_no_client_continue (void *cls)
1936 struct GNUNET_SERVICE_Client *client = cls;
1938 GNUNET_break (0 != client->warn_type); /* type should never be 0 here, as we don't use 0 */
1940 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1941 &warn_no_client_continue,
1943 LOG (GNUNET_ERROR_TYPE_WARNING,
1944 _("Processing code for message of type %u did not call `GNUNET_SERVICE_client_continue' after %s\n"),
1945 (unsigned int) client->warn_type,
1946 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (client->warn_start),
1952 * Functions with this signature are called whenever a
1953 * complete message is received by the tokenizer for a client.
1955 * Do not call #GNUNET_MST_destroy() from within
1956 * the scope of this callback.
1958 * @param cls closure with the `struct GNUNET_SERVICE_Client *`
1959 * @param message the actual message
1960 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the client was dropped
1963 service_client_mst_cb (void *cls,
1964 const struct GNUNET_MessageHeader *message)
1966 struct GNUNET_SERVICE_Client *client = cls;
1968 GNUNET_assert (GNUNET_NO == client->needs_continue);
1969 client->needs_continue = GNUNET_YES;
1970 client->warn_type = ntohs (message->type);
1971 client->warn_start = GNUNET_TIME_absolute_get ();
1972 GNUNET_assert (NULL == client->warn_task);
1974 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1975 &warn_no_client_continue,
1977 GNUNET_MQ_inject_message (client->mq,
1979 if (NULL != client->drop_task)
1980 return GNUNET_SYSERR;
1986 * A client sent us data. Receive and process it. If we are done,
1987 * reschedule this task.
1989 * @param cls the `struct GNUNET_SERVICE_Client` that sent us data.
1992 service_client_recv (void *cls)
1994 struct GNUNET_SERVICE_Client *client = cls;
1997 client->recv_task = NULL;
1998 ret = GNUNET_MST_read (client->mst,
2002 if (GNUNET_SYSERR == ret)
2004 /* client closed connection (or IO error) */
2005 if (NULL == client->drop_task)
2007 GNUNET_assert (GNUNET_NO == client->needs_continue);
2008 GNUNET_SERVICE_client_drop (client);
2012 if (GNUNET_NO == ret)
2013 return; /* more messages in buffer, wait for application
2014 to be done processing */
2015 GNUNET_assert (GNUNET_OK == ret);
2016 if (GNUNET_YES == client->needs_continue)
2018 if (NULL != client->recv_task)
2020 /* MST needs more data, re-schedule read job */
2022 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2024 &service_client_recv,
2030 * We have successfully accepted a connection from a client. Now
2031 * setup the client (with the scheduler) and tell the application.
2033 * @param sh service that accepted the client
2034 * @param sock socket associated with the client
2037 start_client (struct GNUNET_SERVICE_Handle *sh,
2038 struct GNUNET_NETWORK_Handle *csock)
2040 struct GNUNET_SERVICE_Client *client;
2042 client = GNUNET_new (struct GNUNET_SERVICE_Client);
2043 GNUNET_CONTAINER_DLL_insert (sh->clients_head,
2047 client->sock = csock;
2048 client->mq = GNUNET_MQ_queue_for_callbacks (&service_mq_send,
2053 &service_mq_error_handler,
2055 client->mst = GNUNET_MST_create (&service_client_mst_cb,
2057 client->user_context = sh->connect_cb (sh->cb_cls,
2060 GNUNET_MQ_set_handlers_closure (client->mq,
2061 client->user_context);
2063 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2065 &service_client_recv,
2071 * Check if the given IP address is in the list of IP addresses.
2073 * @param list a list of networks
2074 * @param add the IP to check (in network byte order)
2075 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
2078 check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list,
2079 const struct in_addr *add)
2086 while ( (0 != list[i].network.s_addr) ||
2087 (0 != list[i].netmask.s_addr) )
2089 if ((add->s_addr & list[i].netmask.s_addr) ==
2090 (list[i].network.s_addr & list[i].netmask.s_addr))
2099 * Check if the given IP address is in the list of IP addresses.
2101 * @param list a list of networks
2102 * @param ip the IP to check (in network byte order)
2103 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
2106 check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
2107 const struct in6_addr *ip)
2111 struct in6_addr zero;
2117 sizeof (struct in6_addr));
2120 while (0 != memcmp (&zero,
2122 sizeof (struct in6_addr)))
2124 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
2125 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
2126 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
2138 * We have a client. Accept the incoming socket(s) (and reschedule
2141 * @param cls the `struct ServiceListenContext` of the ready listen socket
2144 accept_client (void *cls)
2146 struct ServiceListenContext *slc = cls;
2147 struct GNUNET_SERVICE_Handle *sh = slc->sh;
2149 slc->listen_task = NULL;
2152 struct GNUNET_NETWORK_Handle *sock;
2153 const struct sockaddr_in *v4;
2154 const struct sockaddr_in6 *v6;
2155 struct sockaddr_storage sa;
2159 addrlen = sizeof (sa);
2160 sock = GNUNET_NETWORK_socket_accept (slc->listen_socket,
2161 (struct sockaddr *) &sa,
2165 switch (sa.ss_family)
2168 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
2169 v4 = (const struct sockaddr_in *) &sa;
2170 ok = ( ( (NULL == sh->v4_allowed) ||
2171 (check_ipv4_listed (sh->v4_allowed,
2173 ( (NULL == sh->v4_denied) ||
2174 (! check_ipv4_listed (sh->v4_denied,
2175 &v4->sin_addr)) ) );
2178 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
2179 v6 = (const struct sockaddr_in6 *) &sa;
2180 ok = ( ( (NULL == sh->v6_allowed) ||
2181 (check_ipv6_listed (sh->v6_allowed,
2182 &v6->sin6_addr))) &&
2183 ( (NULL == sh->v6_denied) ||
2184 (! check_ipv6_listed (sh->v6_denied,
2185 &v6->sin6_addr)) ) );
2189 ok = GNUNET_OK; /* controlled using file-system ACL now */
2193 LOG (GNUNET_ERROR_TYPE_WARNING,
2194 _("Unknown address family %d\n"),
2200 LOG (GNUNET_ERROR_TYPE_DEBUG,
2201 "Service rejected incoming connection from %s due to policy.\n",
2202 GNUNET_a2s ((const struct sockaddr *) &sa,
2204 GNUNET_break (GNUNET_OK ==
2205 GNUNET_NETWORK_socket_close (sock));
2208 LOG (GNUNET_ERROR_TYPE_DEBUG,
2209 "Service accepted incoming connection from %s.\n",
2210 GNUNET_a2s ((const struct sockaddr *) &sa,
2212 start_client (slc->sh,
2216 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2224 * Resume accepting connections from the listen socket.
2226 * @param sh service to resume accepting connections.
2229 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh)
2231 struct ServiceListenContext *slc;
2233 for (slc = sh->slc_head; NULL != slc; slc = slc->next)
2235 GNUNET_assert (NULL == slc->listen_task);
2237 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2246 * Task run to resume receiving data from the client after
2247 * the client called #GNUNET_SERVICE_client_continue().
2249 * @param cls our `struct GNUNET_SERVICE_Client`
2252 resume_client_receive (void *cls)
2254 struct GNUNET_SERVICE_Client *c = cls;
2257 c->recv_task = NULL;
2258 /* first, check if there is still something in the buffer */
2259 ret = GNUNET_MST_next (c->mst,
2261 if (GNUNET_SYSERR == ret)
2264 GNUNET_SERVICE_client_drop (c);
2267 if (GNUNET_NO == ret)
2268 return; /* done processing, wait for more later */
2269 GNUNET_assert (GNUNET_OK == ret);
2270 if (GNUNET_YES == c->needs_continue)
2271 return; /* #GNUNET_MST_next() did give a message to the client */
2272 /* need to receive more data from the network first */
2273 if (NULL != c->recv_task)
2276 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2278 &service_client_recv,
2284 * Continue receiving further messages from the given client.
2285 * Must be called after each message received.
2287 * @param c the client to continue receiving from
2290 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c)
2292 GNUNET_assert (GNUNET_YES == c->needs_continue);
2293 GNUNET_assert (NULL == c->recv_task);
2294 c->needs_continue = GNUNET_NO;
2295 if (NULL != c->warn_task)
2297 GNUNET_SCHEDULER_cancel (c->warn_task);
2298 c->warn_task = NULL;
2301 = GNUNET_SCHEDULER_add_now (&resume_client_receive,
2307 * Disable the warning the server issues if a message is not
2308 * acknowledged in a timely fashion. Use this call if a client is
2309 * intentionally delayed for a while. Only applies to the current
2312 * @param c client for which to disable the warning
2315 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c)
2317 GNUNET_break (NULL != c->warn_task);
2318 if (NULL != c->warn_task)
2320 GNUNET_SCHEDULER_cancel (c->warn_task);
2321 c->warn_task = NULL;
2327 * Asynchronously finish dropping the client.
2329 * @param cls the `struct GNUNET_SERVICE_Client`.
2332 finish_client_drop (void *cls)
2334 struct GNUNET_SERVICE_Client *c = cls;
2335 struct GNUNET_SERVICE_Handle *sh = c->sh;
2337 GNUNET_MST_destroy (c->mst);
2338 GNUNET_MQ_destroy (c->mq);
2339 if (GNUNET_NO == c->persist)
2341 GNUNET_break (GNUNET_OK ==
2342 GNUNET_NETWORK_socket_close (c->sock));
2346 GNUNET_NETWORK_socket_free_memory_only_ (c->sock);
2349 if ( (GNUNET_YES == sh->got_shutdown) &&
2350 (GNUNET_NO == have_non_monitor_clients (sh)) )
2351 GNUNET_SERVICE_shutdown (sh);
2356 * Ask the server to disconnect from the given client. This is the
2357 * same as returning #GNUNET_SYSERR within the check procedure when
2358 * handling a message, wexcept that it allows dropping of a client even
2359 * when not handling a message from that client. The `disconnect_cb`
2360 * will be called on @a c even if the application closes the connection
2361 * using this function.
2363 * @param c client to disconnect now
2366 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
2368 struct GNUNET_SERVICE_Handle *sh = c->sh;
2370 if (NULL != c->drop_task)
2372 /* asked to drop twice! */
2376 GNUNET_CONTAINER_DLL_remove (sh->clients_head,
2379 sh->disconnect_cb (sh->cb_cls,
2382 if (NULL != c->warn_task)
2384 GNUNET_SCHEDULER_cancel (c->warn_task);
2385 c->warn_task = NULL;
2387 if (NULL != c->recv_task)
2389 GNUNET_SCHEDULER_cancel (c->recv_task);
2390 c->recv_task = NULL;
2392 if (NULL != c->send_task)
2394 GNUNET_SCHEDULER_cancel (c->send_task);
2395 c->send_task = NULL;
2397 c->drop_task = GNUNET_SCHEDULER_add_now (&finish_client_drop,
2403 * Explicitly stops the service.
2405 * @param sh server to shutdown
2408 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh)
2410 struct GNUNET_SERVICE_Client *client;
2412 GNUNET_SERVICE_suspend (sh);
2413 sh->got_shutdown = GNUNET_NO;
2414 while (NULL != (client = sh->clients_head))
2415 GNUNET_SERVICE_client_drop (client);
2420 * Set the 'monitor' flag on this client. Clients which have been
2421 * marked as 'monitors' won't prevent the server from shutting down
2422 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
2423 * that for "normal" clients we likely want to allow them to process
2424 * their requests; however, monitor-clients are likely to 'never'
2425 * disconnect during shutdown and thus will not be considered when
2426 * determining if the server should continue to exist after
2427 * shutdown has been triggered.
2429 * @param c client to mark as a monitor
2432 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c)
2434 c->is_monitor = GNUNET_YES;
2435 if ( (GNUNET_YES == c->sh->got_shutdown) &&
2436 (GNUNET_NO == have_non_monitor_clients (c->sh)) )
2437 GNUNET_SERVICE_shutdown (c->sh);
2442 * Set the persist option on this client. Indicates that the
2443 * underlying socket or fd should never really be closed. Used for
2444 * indicating process death.
2446 * @param c client to persist the socket (never to be closed)
2449 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c)
2451 c->persist = GNUNET_YES;
2456 * Obtain the message queue of @a c. Convenience function.
2458 * @param c the client to continue receiving from
2459 * @return the message queue of @a c
2461 struct GNUNET_MQ_Handle *
2462 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c)
2468 /* end of service_new.c */