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 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 * Pointer to the message to be transmitted by @e send_task.
271 const struct GNUNET_MessageHeader *msg;
274 * User context value, value returned from
275 * the connect callback.
280 * Time when we last gave a message from this client
281 * to the application.
283 struct GNUNET_TIME_Absolute warn_start;
286 * Current position in @e msg at which we are transmitting.
291 * Persist the file handle for this client no matter what happens,
292 * force the OS to close once the process actually dies. Should only
293 * be used in special cases!
298 * Is this client a 'monitor' client that should not be counted
299 * when deciding on destroying the server during soft shutdown?
300 * (see also #GNUNET_SERVICE_start)
305 * Are we waiting for the application to call #GNUNET_SERVICE_client_continue()?
310 * Type of last message processed (for warn_no_receive_done).
317 * Check if any of the clients we have left are unrelated to
320 * @param sh service to check clients for
321 * @return #GNUNET_YES if we have non-monitoring clients left
324 have_non_monitor_clients (struct GNUNET_SERVICE_Handle *sh)
326 struct GNUNET_SERVICE_Client *client;
328 for (client = sh->clients_head;NULL != client; client = client->next)
330 if (client->is_monitor)
339 * Shutdown task triggered when a service should be terminated.
340 * This considers active clients and the service options to see
341 * how this specific service is to be terminated, and depending
342 * on this proceeds with the shutdown logic.
344 * @param cls our `struct GNUNET_SERVICE_Handle`
347 service_shutdown (void *cls)
349 struct GNUNET_SERVICE_Handle *sh = cls;
353 case GNUNET_SERVICE_OPTION_NONE:
354 GNUNET_SERVICE_shutdown (sh);
356 case GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN:
357 /* This task should never be run if we are using
358 the manual shutdown. */
361 case GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN:
362 sh->got_shutdown = GNUNET_YES;
363 GNUNET_SERVICE_suspend (sh);
364 if (GNUNET_NO == have_non_monitor_clients (sh))
365 GNUNET_SERVICE_shutdown (sh);
372 * First task run by any service. Initializes our shutdown task,
373 * starts the listening operation on our listen sockets and launches
374 * the custom logic of the application service.
376 * @param cls our `struct GNUNET_SERVICE_Handle`
379 service_main (void *cls)
381 struct GNUNET_SERVICE_Handle *sh = cls;
383 if (GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN != sh->options)
384 GNUNET_SCHEDULER_add_shutdown (&service_shutdown,
386 GNUNET_SERVICE_resume (sh);
387 sh->service_init_cb (sh->cb_cls,
394 * Parse an IPv4 access control list.
396 * @param ret location where to write the ACL (set)
397 * @param sh service context to use to get the configuration
398 * @param option name of the ACL option to parse
399 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
403 process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret,
404 struct GNUNET_SERVICE_Handle *sh,
409 if (! GNUNET_CONFIGURATION_have_value (sh->cfg,
416 GNUNET_break (GNUNET_OK ==
417 GNUNET_CONFIGURATION_get_value_string (sh->cfg,
421 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv4_policy (opt)))
423 LOG (GNUNET_ERROR_TYPE_WARNING,
424 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
429 return GNUNET_SYSERR;
437 * Parse an IPv6 access control list.
439 * @param ret location where to write the ACL (set)
440 * @param sh service context to use to get the configuration
441 * @param option name of the ACL option to parse
442 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
446 process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret,
447 struct GNUNET_SERVICE_Handle *sh,
452 if (! GNUNET_CONFIGURATION_have_value (sh->cfg,
459 GNUNET_break (GNUNET_OK ==
460 GNUNET_CONFIGURATION_get_value_string (sh->cfg,
464 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv6_policy (opt)))
466 LOG (GNUNET_ERROR_TYPE_WARNING,
467 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
472 return GNUNET_SYSERR;
480 * Add the given UNIX domain path as an address to the
481 * list (as the first entry).
483 * @param saddrs array to update
484 * @param saddrlens where to store the address length
485 * @param unixpath path to add
486 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
487 * parameter is ignore on systems other than LINUX
490 add_unixpath (struct sockaddr **saddrs,
491 socklen_t *saddrlens,
492 const char *unixpath,
496 struct sockaddr_un *un;
498 un = GNUNET_new (struct sockaddr_un);
499 un->sun_family = AF_UNIX;
500 strncpy (un->sun_path,
502 sizeof (un->sun_path) - 1);
504 if (GNUNET_YES == abstract)
505 un->sun_path[0] = '\0';
507 #if HAVE_SOCKADDR_IN_SIN_LEN
508 un->sun_len = (u_char) sizeof (struct sockaddr_un);
510 *saddrs = (struct sockaddr *) un;
511 *saddrlens = sizeof (struct sockaddr_un);
513 /* this function should never be called
514 * unless AF_UNIX is defined! */
521 * Get the list of addresses that a server for the given service
524 * @param service_name name of the service
525 * @param cfg configuration (which specifies the addresses)
526 * @param addrs set (call by reference) to an array of pointers to the
527 * addresses the server should bind to and listen on; the
528 * array will be NULL-terminated (on success)
529 * @param addr_lens set (call by reference) to an array of the lengths
530 * of the respective `struct sockaddr` struct in the @a addrs
532 * @return number of addresses found on success,
533 * #GNUNET_SYSERR if the configuration
534 * did not specify reasonable finding information or
535 * if it specified a hostname that could not be resolved;
536 * #GNUNET_NO if the number of addresses configured is
537 * zero (in this case, `*addrs` and `*addr_lens` will be
541 get_server_addresses (const char *service_name,
542 const struct GNUNET_CONFIGURATION_Handle *cfg,
543 struct sockaddr ***addrs,
544 socklen_t **addr_lens)
547 struct GNUNET_NETWORK_Handle *desc;
548 unsigned long long port;
550 struct addrinfo hints;
551 struct addrinfo *res;
552 struct addrinfo *pos;
553 struct addrinfo *next;
558 struct sockaddr **saddrs;
559 socklen_t *saddrlens;
565 if (GNUNET_CONFIGURATION_have_value (cfg,
571 GNUNET_CONFIGURATION_get_value_yesno (cfg,
574 return GNUNET_SYSERR;
577 disablev6 = GNUNET_NO;
581 /* probe IPv6 support */
582 desc = GNUNET_NETWORK_socket_create (PF_INET6,
587 if ( (ENOBUFS == errno) ||
592 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
594 return GNUNET_SYSERR;
596 LOG (GNUNET_ERROR_TYPE_INFO,
597 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
600 disablev6 = GNUNET_YES;
604 GNUNET_break (GNUNET_OK ==
605 GNUNET_NETWORK_socket_close (desc));
611 if (GNUNET_CONFIGURATION_have_value (cfg,
616 GNUNET_CONFIGURATION_get_value_number (cfg,
621 LOG (GNUNET_ERROR_TYPE_ERROR,
622 _("Require valid port number for service `%s' in configuration!\n"),
627 LOG (GNUNET_ERROR_TYPE_ERROR,
628 _("Require valid port number for service `%s' in configuration!\n"),
630 return GNUNET_SYSERR;
634 if (GNUNET_CONFIGURATION_have_value (cfg,
638 GNUNET_break (GNUNET_OK ==
639 GNUNET_CONFIGURATION_get_value_string (cfg,
648 abstract = GNUNET_NO;
651 GNUNET_CONFIGURATION_have_value (cfg,
655 GNUNET_CONFIGURATION_get_value_filename (cfg,
659 (0 < strlen (unixpath)))
661 /* probe UNIX support */
662 struct sockaddr_un s_un;
664 if (strlen (unixpath) >= sizeof (s_un.sun_path))
666 LOG (GNUNET_ERROR_TYPE_WARNING,
667 _("UNIXPATH `%s' too long, maximum length is %llu\n"),
669 (unsigned long long) sizeof (s_un.sun_path));
670 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
671 LOG (GNUNET_ERROR_TYPE_INFO,
672 _("Using `%s' instead\n"),
676 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
678 "USE_ABSTRACT_SOCKETS");
679 if (GNUNET_SYSERR == abstract)
680 abstract = GNUNET_NO;
682 if ( (GNUNET_YES != abstract) &&
684 GNUNET_DISK_directory_create_for_file (unixpath)) )
685 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
689 if (NULL != unixpath)
691 desc = GNUNET_NETWORK_socket_create (AF_UNIX,
696 if ((ENOBUFS == errno) ||
701 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
703 GNUNET_free_non_null (hostname);
704 GNUNET_free (unixpath);
705 return GNUNET_SYSERR;
707 LOG (GNUNET_ERROR_TYPE_INFO,
708 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
711 GNUNET_free (unixpath);
716 GNUNET_break (GNUNET_OK ==
717 GNUNET_NETWORK_socket_close (desc));
723 if ((0 == port) && (NULL == unixpath))
725 LOG (GNUNET_ERROR_TYPE_ERROR,
726 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
728 GNUNET_free_non_null (hostname);
729 return GNUNET_SYSERR;
733 saddrs = GNUNET_new_array (2,
735 saddrlens = GNUNET_new_array (2,
737 add_unixpath (saddrs,
741 GNUNET_free_non_null (unixpath);
742 GNUNET_free_non_null (hostname);
744 *addr_lens = saddrlens;
748 if (NULL != hostname)
750 LOG (GNUNET_ERROR_TYPE_DEBUG,
751 "Resolving `%s' since that is where `%s' will bind to.\n",
756 sizeof (struct addrinfo));
758 hints.ai_family = AF_INET;
759 hints.ai_protocol = IPPROTO_TCP;
760 if ((0 != (ret = getaddrinfo (hostname,
766 LOG (GNUNET_ERROR_TYPE_ERROR,
767 _("Failed to resolve `%s': %s\n"),
770 GNUNET_free (hostname);
771 GNUNET_free_non_null (unixpath);
772 return GNUNET_SYSERR;
776 while (NULL != (pos = next))
780 (pos->ai_family == AF_INET6) )
786 LOG (GNUNET_ERROR_TYPE_ERROR,
787 _("Failed to find %saddress for `%s'.\n"),
788 disablev6 ? "IPv4 " : "",
791 GNUNET_free (hostname);
792 GNUNET_free_non_null (unixpath);
793 return GNUNET_SYSERR;
796 if (NULL != unixpath)
798 saddrs = GNUNET_new_array (resi + 1,
800 saddrlens = GNUNET_new_array (resi + 1,
803 if (NULL != unixpath)
805 add_unixpath (saddrs,
812 while (NULL != (pos = next))
816 (AF_INET6 == pos->ai_family) )
818 if ( (IPPROTO_TCP != pos->ai_protocol) &&
819 (0 != pos->ai_protocol) )
820 continue; /* not TCP */
821 if ( (SOCK_STREAM != pos->ai_socktype) &&
822 (0 != pos->ai_socktype) )
824 LOG (GNUNET_ERROR_TYPE_DEBUG,
825 "Service `%s' will bind to `%s'\n",
827 GNUNET_a2s (pos->ai_addr,
829 if (AF_INET == pos->ai_family)
831 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
832 saddrlens[i] = pos->ai_addrlen;
833 saddrs[i] = GNUNET_malloc (saddrlens[i]);
834 GNUNET_memcpy (saddrs[i],
837 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
841 GNUNET_assert (AF_INET6 == pos->ai_family);
842 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
843 saddrlens[i] = pos->ai_addrlen;
844 saddrs[i] = GNUNET_malloc (saddrlens[i]);
845 GNUNET_memcpy (saddrs[i],
848 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
852 GNUNET_free (hostname);
858 /* will bind against everything, just set port */
863 if (NULL != unixpath)
866 saddrs = GNUNET_new_array (resi + 1,
868 saddrlens = GNUNET_new_array (resi + 1,
870 if (NULL != unixpath)
872 add_unixpath (saddrs,
878 saddrlens[i] = sizeof (struct sockaddr_in);
879 saddrs[i] = GNUNET_malloc (saddrlens[i]);
880 #if HAVE_SOCKADDR_IN_SIN_LEN
881 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
883 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
884 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
890 if (NULL != unixpath)
892 saddrs = GNUNET_new_array (resi + 1,
894 saddrlens = GNUNET_new_array (resi + 1,
897 if (NULL != unixpath)
899 add_unixpath (saddrs,
905 saddrlens[i] = sizeof (struct sockaddr_in6);
906 saddrs[i] = GNUNET_malloc (saddrlens[i]);
907 #if HAVE_SOCKADDR_IN_SIN_LEN
908 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
910 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
911 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
913 saddrlens[i] = sizeof (struct sockaddr_in);
914 saddrs[i] = GNUNET_malloc (saddrlens[i]);
915 #if HAVE_SOCKADDR_IN_SIN_LEN
916 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
918 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
919 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
922 GNUNET_free_non_null (unixpath);
924 *addr_lens = saddrlens;
931 * Read listen sockets from the parent process (ARM).
933 * @param sh service context to initialize
934 * @return NULL-terminated array of sockets on success,
935 * NULL if not ok (must bind yourself)
937 static struct GNUNET_NETWORK_Handle **
938 receive_sockets_from_parent (struct GNUNET_SERVICE_Handle *sh)
940 static struct GNUNET_NETWORK_Handle **lsocks;
947 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
948 if ( (NULL == env_buf) ||
949 (strlen (env_buf) <= 0) )
951 /* Using W32 API directly here, because this pipe will
952 * never be used outside of this function, and it's just too much of a bother
953 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
955 lsocks_pipe = (HANDLE) strtoul (env_buf,
958 if ( (0 == lsocks_pipe) ||
959 (INVALID_HANDLE_VALUE == lsocks_pipe))
968 ret = ReadFile (lsocks_pipe,
974 (sizeof (count) != rd) ||
977 lsocks = GNUNET_new_array (count + 1,
978 struct GNUNET_NETWORK_Handle *);
981 for (i = 0; i < count; i++)
983 WSAPROTOCOL_INFOA pi;
987 ret = ReadFile (lsocks_pipe,
993 (sizeof (size) != rd) ||
994 (sizeof (pi) != size) )
996 ret = ReadFile (lsocks_pipe,
1002 (sizeof (pi) != rd))
1004 s = WSASocketA (pi.iAddressFamily,
1009 WSA_FLAG_OVERLAPPED);
1010 lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1011 if (NULL == lsocks[i])
1013 else if (i == count - 1)
1018 lsocks[count] = NULL;
1022 CloseHandle (lsocks_pipe);
1026 LOG (GNUNET_ERROR_TYPE_ERROR,
1027 _("Could not access a pre-bound socket, will try to bind myself\n"));
1028 for (i = 0; (i < count) && (NULL != lsocks[i]); i++)
1029 GNUNET_break (GNUNET_OK ==
1030 GNUNET_NETWORK_socket_close (lsocks[i]));
1031 GNUNET_free (lsocks);
1040 * Create and initialize a listen socket for the server.
1042 * @param server_addr address to listen on
1043 * @param socklen length of @a server_addr
1044 * @return NULL on error, otherwise the listen socket
1046 static struct GNUNET_NETWORK_Handle *
1047 open_listen_socket (const struct sockaddr *server_addr,
1050 struct GNUNET_NETWORK_Handle *sock;
1054 switch (server_addr->sa_family)
1057 port = ntohs (((const struct sockaddr_in *) server_addr)->sin_port);
1060 port = ntohs (((const struct sockaddr_in6 *) server_addr)->sin6_port);
1070 sock = GNUNET_NETWORK_socket_create (server_addr->sa_family,
1075 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1080 /* bind the socket */
1081 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock,
1086 if (EADDRINUSE != errno)
1088 /* we don't log 'EADDRINUSE' here since an IPv4 bind may
1089 * fail if we already took the port on IPv6; if both IPv4 and
1090 * IPv6 binds fail, then our caller will log using the
1091 * errno preserved in 'eno' */
1092 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1095 LOG (GNUNET_ERROR_TYPE_ERROR,
1096 _("`%s' failed for port %d (%s).\n"),
1099 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1105 LOG (GNUNET_ERROR_TYPE_WARNING,
1106 _("`%s' failed for port %d (%s): address already in use\n"),
1108 (AF_INET == server_addr->sa_family) ? "IPv4" : "IPv6");
1109 else if (AF_UNIX == server_addr->sa_family)
1111 LOG (GNUNET_ERROR_TYPE_WARNING,
1112 _("`%s' failed for `%s': address already in use\n"),
1114 GNUNET_a2s (server_addr, socklen));
1117 GNUNET_break (GNUNET_OK ==
1118 GNUNET_NETWORK_socket_close (sock));
1122 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock,
1125 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1127 GNUNET_break (GNUNET_OK ==
1128 GNUNET_NETWORK_socket_close (sock));
1133 LOG (GNUNET_ERROR_TYPE_DEBUG,
1134 "Server starts to listen on port %u.\n",
1141 * Setup service handle
1143 * Configuration may specify:
1144 * - PORT (where to bind to for TCP)
1145 * - UNIXPATH (where to bind to for UNIX domain sockets)
1146 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1147 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1148 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1149 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1150 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1151 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1153 * @param sh service context to initialize
1154 * @return #GNUNET_OK if configuration succeeded
1157 setup_service (struct GNUNET_SERVICE_Handle *sh)
1160 struct GNUNET_NETWORK_Handle **lsocks;
1167 if (GNUNET_CONFIGURATION_have_value
1172 if (GNUNET_SYSERR ==
1174 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1178 LOG (GNUNET_ERROR_TYPE_ERROR,
1179 _("Specified value for `%s' of service `%s' is invalid\n"),
1182 return GNUNET_SYSERR;
1186 tolerant = GNUNET_NO;
1191 if ( (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1196 (cnt < FD_SETSIZE) &&
1197 (cnt + 4 < FD_SETSIZE) )
1199 lsocks = GNUNET_new_array (cnt + 1,
1200 struct GNUNET_NETWORK_Handle *);
1203 flags = fcntl (3 + cnt,
1206 (0 != (flags & FD_CLOEXEC)) ||
1208 (lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1210 LOG (GNUNET_ERROR_TYPE_ERROR,
1211 _("Could not access pre-bound socket %u, will try to bind myself\n"),
1212 (unsigned int) 3 + cnt);
1214 while (NULL != lsocks[cnt])
1215 GNUNET_break (GNUNET_OK ==
1216 GNUNET_NETWORK_socket_close (lsocks[cnt++]));
1217 GNUNET_free (lsocks);
1222 unsetenv ("LISTEN_FDS");
1225 if (NULL != getenv ("GNUNET_OS_READ_LSOCKS"))
1227 lsocks = receive_sockets_from_parent (sh);
1228 putenv ("GNUNET_OS_READ_LSOCKS=");
1234 /* listen only on inherited sockets if we have any */
1235 struct GNUNET_NETWORK_Handle **ls;
1237 for (ls = lsocks; NULL != *ls; ls++)
1239 struct ServiceListenContext *slc;
1241 slc = GNUNET_new (struct ServiceListenContext);
1243 slc->listen_socket = *ls;
1244 GNUNET_CONTAINER_DLL_insert (sh->slc_head,
1248 GNUNET_free (lsocks);
1252 struct sockaddr **addrs;
1253 socklen_t *addrlens;
1256 num = get_server_addresses (sh->service_name,
1260 if (GNUNET_SYSERR == num)
1261 return GNUNET_SYSERR;
1263 for (int i = 0; i < num; i++)
1265 struct ServiceListenContext *slc;
1267 slc = GNUNET_new (struct ServiceListenContext);
1269 slc->listen_socket = open_listen_socket (addrs[i],
1271 GNUNET_break (NULL != slc->listen_socket);
1272 GNUNET_CONTAINER_DLL_insert (sh->slc_head,
1278 sh->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1280 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1284 GNUNET_CONFIGURATION_get_value_yesno (sh->cfg,
1287 process_acl4 (&sh->v4_denied,
1290 process_acl4 (&sh->v4_allowed,
1293 process_acl6 (&sh->v6_denied,
1296 process_acl6 (&sh->v6_allowed,
1304 * Get the name of the user that'll be used
1305 * to provide the service.
1307 * @param sh service context
1308 * @return value of the 'USERNAME' option
1311 get_user_name (struct GNUNET_SERVICE_Handle *sh)
1316 GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1328 * @param sh service context
1329 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1332 set_user_id (struct GNUNET_SERVICE_Handle *sh)
1336 if (NULL == (user = get_user_name (sh)))
1337 return GNUNET_OK; /* keep */
1342 pws = getpwnam (user);
1345 LOG (GNUNET_ERROR_TYPE_ERROR,
1346 _("Cannot obtain information about user `%s': %s\n"),
1348 errno == 0 ? _("No such user") : STRERROR (errno));
1350 return GNUNET_SYSERR;
1352 if ( (0 != setgid (pws->pw_gid)) ||
1353 (0 != setegid (pws->pw_gid)) ||
1355 (0 != initgroups (user,
1358 (0 != setuid (pws->pw_uid)) ||
1359 (0 != seteuid (pws->pw_uid)))
1361 if ((0 != setregid (pws->pw_gid,
1363 (0 != setreuid (pws->pw_uid,
1366 LOG (GNUNET_ERROR_TYPE_ERROR,
1367 _("Cannot change user/group to `%s': %s\n"),
1371 return GNUNET_SYSERR;
1381 * Get the name of the file where we will
1382 * write the PID of the service.
1384 * @param sh service context
1385 * @return name of the file for the process ID
1388 get_pid_file_name (struct GNUNET_SERVICE_Handle *sh)
1393 GNUNET_CONFIGURATION_get_value_filename (sh->cfg,
1403 * Delete the PID file that was created by our parent.
1405 * @param sh service context
1408 pid_file_delete (struct GNUNET_SERVICE_Handle *sh)
1410 char *pif = get_pid_file_name (sh);
1413 return; /* no PID file */
1414 if (0 != UNLINK (pif))
1415 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
1423 * Detach from terminal.
1425 * @param sh service context
1426 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1429 detach_terminal (struct GNUNET_SERVICE_Handle *sh)
1436 if (0 != PIPE (filedes))
1438 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1440 return GNUNET_SYSERR;
1445 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1447 return GNUNET_SYSERR;
1454 GNUNET_break (0 == CLOSE (filedes[1]));
1456 if (1 != READ (filedes[0],
1459 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1467 LOG (GNUNET_ERROR_TYPE_INFO,
1468 _("Service process failed to initialize\n"));
1471 LOG (GNUNET_ERROR_TYPE_INFO,
1472 _("Service process could not initialize server function\n"));
1475 LOG (GNUNET_ERROR_TYPE_INFO,
1476 _("Service process failed to report status\n"));
1479 exit (1); /* child reported error */
1481 GNUNET_break (0 == CLOSE (0));
1482 GNUNET_break (0 == CLOSE (1));
1483 GNUNET_break (0 == CLOSE (filedes[0]));
1484 nullfd = OPEN ("/dev/null",
1487 return GNUNET_SYSERR;
1488 /* set stdin/stdout to /dev/null */
1489 if ( (dup2 (nullfd, 0) < 0) ||
1490 (dup2 (nullfd, 1) < 0) )
1492 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1494 (void) CLOSE (nullfd);
1495 return GNUNET_SYSERR;
1497 (void) CLOSE (nullfd);
1498 /* Detach from controlling terminal */
1501 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1503 sh->ready_confirm_fd = filedes[1];
1505 /* FIXME: we probably need to do something else
1506 * elsewhere in order to fork the process itself... */
1514 * Creates the "main" function for a GNUnet service. You
1515 * should almost always use the #GNUNET_SERVICE_MAIN macro
1516 * instead of calling this function directly (except
1517 * for ARM, which should call this function directly).
1519 * The function will launch the service with the name @a service_name
1520 * using the @a service_options to configure its shutdown
1521 * behavior. Once the service is ready, the @a init_cb will be called
1522 * for service-specific initialization. @a init_cb will be given the
1523 * service handler which can be used to control the service's
1524 * availability. When clients connect or disconnect, the respective
1525 * @a connect_cb or @a disconnect_cb functions will be called. For
1526 * messages received from the clients, the respective @a handlers will
1527 * be invoked; for the closure of the handlers we use the return value
1528 * from the @a connect_cb invocation of the respective client.
1530 * Each handler MUST call #GNUNET_SERVICE_client_continue() after each
1531 * message to receive further messages from this client. If
1532 * #GNUNET_SERVICE_client_continue() is not called within a short
1533 * time, a warning will be logged. If delays are expected, services
1534 * should call #GNUNET_SERVICE_client_disable_continue_warning() to
1535 * disable the warning.
1537 * Clients sending invalid messages (based on @a handlers) will be
1538 * dropped. Additionally, clients can be dropped at any time using
1539 * #GNUNET_SERVICE_client_drop().
1541 * @param argc number of command-line arguments in @a argv
1542 * @param argv array of command-line arguments
1543 * @param service_name name of the service to run
1544 * @param options options controlling shutdown of the service
1545 * @param service_init_cb function to call once the service is ready
1546 * @param connect_cb function to call whenever a client connects
1547 * @param disconnect_cb function to call whenever a client disconnects
1548 * @param cls closure argument for @a service_init_cb, @a connect_cb and @a disconnect_cb
1549 * @param handlers NULL-terminated array of message handlers for the service,
1550 * the closure will be set to the value returned by
1551 * the @a connect_cb for the respective connection
1552 * @return 0 on success, non-zero on error
1555 GNUNET_SERVICE_ruN_ (int argc,
1557 const char *service_name,
1558 enum GNUNET_SERVICE_Options options,
1559 GNUNET_SERVICE_InitCallback service_init_cb,
1560 GNUNET_SERVICE_ConnectHandler connect_cb,
1561 GNUNET_SERVICE_DisconnectHandler disconnect_cb,
1563 const struct GNUNET_MQ_MessageHandler *handlers)
1565 struct GNUNET_SERVICE_Handle sh;
1567 char *opt_cfg_filename;
1572 unsigned long long skew_offset;
1573 unsigned long long skew_variance;
1574 long long clock_offset;
1575 struct GNUNET_CONFIGURATION_Handle *cfg;
1579 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1580 GNUNET_GETOPT_OPTION_CFG_FILE (&opt_cfg_filename),
1581 {'d', "daemonize", NULL,
1582 gettext_noop ("do daemonize (detach from terminal)"), 0,
1583 GNUNET_GETOPT_set_one, &do_daemonize},
1584 GNUNET_GETOPT_OPTION_HELP (NULL),
1585 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1586 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1587 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1588 GNUNET_GETOPT_OPTION_END
1594 xdg = getenv ("XDG_CONFIG_HOME");
1596 GNUNET_asprintf (&cfg_filename,
1600 GNUNET_OS_project_data_get ()->config_file);
1602 cfg_filename = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
1603 sh.ready_confirm_fd = -1;
1604 sh.options = options;
1605 sh.cfg = cfg = GNUNET_CONFIGURATION_create ();
1606 sh.service_init_cb = service_init_cb;
1607 sh.connect_cb = connect_cb;
1608 sh.disconnect_cb = disconnect_cb;
1610 sh.handlers = handlers;
1611 sh.service_name = service_name;
1613 /* setup subsystems */
1616 opt_cfg_filename = NULL;
1618 ret = GNUNET_GETOPT_run (service_name,
1622 if (GNUNET_SYSERR == ret)
1624 if (GNUNET_NO == ret)
1629 if (GNUNET_OK != GNUNET_log_setup (service_name,
1636 if (NULL == opt_cfg_filename)
1637 opt_cfg_filename = GNUNET_strdup (cfg_filename);
1638 if (GNUNET_YES == GNUNET_DISK_file_test (opt_cfg_filename))
1640 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg,
1643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1644 _("Malformed configuration file `%s', exit ...\n"),
1651 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg,
1654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1655 _("Malformed configuration, exit ...\n"));
1658 if (0 != strcmp (opt_cfg_filename,
1660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1661 _("Could not access configuration file `%s'\n"),
1664 if (GNUNET_OK != setup_service (&sh))
1666 if ( (1 == do_daemonize) &&
1667 (GNUNET_OK != detach_terminal (&sh)) )
1672 if (GNUNET_OK != set_user_id (&sh))
1674 LOG (GNUNET_ERROR_TYPE_DEBUG,
1675 "Service `%s' runs with configuration from `%s'\n",
1679 GNUNET_CONFIGURATION_get_value_number (sh.cfg,
1684 GNUNET_CONFIGURATION_get_value_number (sh.cfg,
1689 clock_offset = skew_offset - skew_variance;
1690 GNUNET_TIME_set_offset (clock_offset);
1691 LOG (GNUNET_ERROR_TYPE_DEBUG,
1692 "Skewing clock by %dll ms\n",
1695 /* actually run service */
1697 GNUNET_SCHEDULER_run (&service_main,
1700 if (1 == do_daemonize)
1701 pid_file_delete (&sh);
1704 if (-1 != sh.ready_confirm_fd)
1706 if (1 != WRITE (sh.ready_confirm_fd,
1709 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
1711 GNUNET_break (0 == CLOSE (sh.ready_confirm_fd));
1718 GNUNET_CONFIGURATION_have_value (sh.cfg,
1722 GNUNET_CONFIGURATION_get_value_string (sh.cfg,
1730 GAUGER (service_name,
1734 GNUNET_free (counter);
1738 GNUNET_SPEEDUP_stop_ ();
1739 GNUNET_CONFIGURATION_destroy (cfg);
1741 while (NULL != sh.slc_head)
1743 struct ServiceListenContext *slc = sh.slc_head;
1745 sh.slc_head = slc->next;
1746 if (NULL != slc->listen_task)
1747 GNUNET_SCHEDULER_cancel (slc->listen_task);
1748 GNUNET_break (GNUNET_OK ==
1749 GNUNET_NETWORK_socket_close (slc->listen_socket));
1753 GNUNET_free_non_null (logfile);
1754 GNUNET_free_non_null (loglev);
1755 GNUNET_free (cfg_filename);
1756 GNUNET_free_non_null (opt_cfg_filename);
1757 GNUNET_free_non_null (sh.v4_denied);
1758 GNUNET_free_non_null (sh.v6_denied);
1759 GNUNET_free_non_null (sh.v4_allowed);
1760 GNUNET_free_non_null (sh.v6_allowed);
1762 return err ? GNUNET_SYSERR : sh.ret;
1767 * Suspend accepting connections from the listen socket temporarily.
1768 * Resume activity using #GNUNET_SERVICE_resume.
1770 * @param sh service to stop accepting connections.
1773 GNUNET_SERVICE_suspend (struct GNUNET_SERVICE_Handle *sh)
1775 struct ServiceListenContext *slc;
1777 for (slc = sh->slc_head; NULL != slc; slc = slc->next)
1779 if (NULL != slc->listen_task)
1781 GNUNET_SCHEDULER_cancel (slc->listen_task);
1782 slc->listen_task = NULL;
1789 * Task run when we are ready to transmit data to the
1792 * @param cls the `struct GNUNET_SERVICE_Client *` to send to
1797 struct GNUNET_SERVICE_Client *client = cls;
1802 client->send_task = NULL;
1803 buf = (const char *) client->msg;
1804 left = ntohs (client->msg->size) - client->msg_pos;
1805 ret = GNUNET_NETWORK_socket_send (client->sock,
1806 &buf[client->msg_pos],
1808 GNUNET_assert (ret <= (ssize_t) left);
1811 GNUNET_MQ_inject_error (client->mq,
1812 GNUNET_MQ_ERROR_WRITE);
1817 if ( (EAGAIN == errno) ||
1825 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1827 GNUNET_MQ_inject_error (client->mq,
1828 GNUNET_MQ_ERROR_WRITE);
1832 client->msg_pos += ret;
1836 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1842 GNUNET_MQ_impl_send_continue (client->mq);
1847 * Signature of functions implementing the sending functionality of a
1850 * @param mq the message queue
1851 * @param msg the message to send
1852 * @param impl_state our `struct GNUNET_SERVICE_Client *`
1855 service_mq_send (struct GNUNET_MQ_Handle *mq,
1856 const struct GNUNET_MessageHeader *msg,
1859 struct GNUNET_SERVICE_Client *client = impl_state;
1861 GNUNET_assert (NULL == client->send_task);
1863 client->msg_pos = 0;
1865 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1873 * Implementation function that cancels the currently sent message.
1875 * @param mq message queue
1876 * @param impl_state state specific to the implementation
1879 service_mq_cancel (struct GNUNET_MQ_Handle *mq,
1882 struct GNUNET_SERVICE_Client *client = impl_state;
1884 GNUNET_assert (0); // not implemented
1885 // FIXME: stop transmission! (must be possible, otherwise
1886 // we must have told MQ that the message was sent!)
1891 * Generic error handler, called with the appropriate
1892 * error code and the same closure specified at the creation of
1893 * the message queue.
1894 * Not every message queue implementation supports an error handler.
1896 * @param cls closure with our `struct GNUNET_SERVICE_Client`
1897 * @param error error code
1900 service_mq_error_handler (void *cls,
1901 enum GNUNET_MQ_Error error)
1903 struct GNUNET_SERVICE_Client *client = cls;
1904 struct GNUNET_SERVICE_Handle *sh = client->sh;
1906 if ( (GNUNET_MQ_ERROR_NO_MATCH == error) &&
1907 (GNUNET_NO == sh->require_found) )
1908 return; /* ignore error */
1909 GNUNET_SERVICE_client_drop (client);
1914 * Task run to warn about missing calls to #GNUNET_SERVICE_client_continue().
1916 * @param cls our `struct GNUNET_SERVICE_Client *` to process more requests from
1919 warn_no_client_continue (void *cls)
1921 struct GNUNET_SERVICE_Client *client = cls;
1923 GNUNET_break (0 != client->warn_type); /* type should never be 0 here, as we don't use 0 */
1925 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1926 &warn_no_client_continue,
1928 LOG (GNUNET_ERROR_TYPE_WARNING,
1929 _("Processing code for message of type %u did not call `GNUNET_SERVICE_client_continue' after %s\n"),
1930 (unsigned int) client->warn_type,
1931 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (client->warn_start),
1937 * Functions with this signature are called whenever a
1938 * complete message is received by the tokenizer for a client.
1940 * Do not call #GNUNET_MST_destroy() from within
1941 * the scope of this callback.
1943 * @param cls closure with the `struct GNUNET_SERVICE_Client *`
1944 * @param message the actual message
1945 * @return #GNUNET_OK on success (always)
1948 service_client_mst_cb (void *cls,
1949 const struct GNUNET_MessageHeader *message)
1951 struct GNUNET_SERVICE_Client *client = cls;
1953 GNUNET_assert (GNUNET_NO == client->needs_continue);
1954 client->needs_continue = GNUNET_YES;
1955 client->warn_type = ntohs (message->type);
1956 client->warn_start = GNUNET_TIME_absolute_get ();
1958 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1959 &warn_no_client_continue,
1961 GNUNET_MQ_inject_message (client->mq,
1968 * A client sent us data. Receive and process it. If we are done,
1969 * reschedule this task.
1971 * @param cls the `struct GNUNET_SERVICE_Client` that sent us data.
1974 service_client_recv (void *cls)
1976 struct GNUNET_SERVICE_Client *client = cls;
1979 client->recv_task = NULL;
1980 ret = GNUNET_MST_read (client->mst,
1984 if (GNUNET_SYSERR == ret)
1986 /* client closed connection (or IO error) */
1987 GNUNET_assert (GNUNET_NO == client->needs_continue);
1988 GNUNET_SERVICE_client_drop (client);
1991 if (GNUNET_NO == ret)
1992 return; /* more messages in buffer, wait for application
1993 to be done processing */
1994 GNUNET_assert (GNUNET_OK == ret);
1995 if (GNUNET_YES == client->needs_continue)
1997 if (NULL != client->recv_task)
1999 /* MST needs more data, re-schedule read job */
2001 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2003 &service_client_recv,
2009 * We have successfully accepted a connection from a client. Now
2010 * setup the client (with the scheduler) and tell the application.
2012 * @param sh service that accepted the client
2013 * @param sock socket associated with the client
2016 start_client (struct GNUNET_SERVICE_Handle *sh,
2017 struct GNUNET_NETWORK_Handle *csock)
2019 struct GNUNET_SERVICE_Client *client;
2021 client = GNUNET_new (struct GNUNET_SERVICE_Client);
2022 GNUNET_CONTAINER_DLL_insert (sh->clients_head,
2026 client->sock = csock;
2027 client->mq = GNUNET_MQ_queue_for_callbacks (&service_mq_send,
2032 &service_mq_error_handler,
2034 client->mst = GNUNET_MST_create (&service_client_mst_cb,
2036 client->user_context = sh->connect_cb (sh->cb_cls,
2039 GNUNET_MQ_set_handlers_closure (client->mq,
2040 client->user_context);
2042 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2044 &service_client_recv,
2050 * Check if the given IP address is in the list of IP addresses.
2052 * @param list a list of networks
2053 * @param add the IP to check (in network byte order)
2054 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
2057 check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list,
2058 const struct in_addr *add)
2065 while ( (0 != list[i].network.s_addr) ||
2066 (0 != list[i].netmask.s_addr) )
2068 if ((add->s_addr & list[i].netmask.s_addr) ==
2069 (list[i].network.s_addr & list[i].netmask.s_addr))
2078 * Check if the given IP address is in the list of IP addresses.
2080 * @param list a list of networks
2081 * @param ip the IP to check (in network byte order)
2082 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
2085 check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
2086 const struct in6_addr *ip)
2090 struct in6_addr zero;
2096 sizeof (struct in6_addr));
2099 while (0 != memcmp (&zero,
2101 sizeof (struct in6_addr)))
2103 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
2104 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
2105 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
2117 * We have a client. Accept the incoming socket(s) (and reschedule
2120 * @param cls the `struct ServiceListenContext` of the ready listen socket
2123 accept_client (void *cls)
2125 struct ServiceListenContext *slc = cls;
2126 struct GNUNET_SERVICE_Handle *sh = slc->sh;
2128 slc->listen_task = NULL;
2131 struct GNUNET_NETWORK_Handle *sock;
2132 const struct sockaddr_in *v4;
2133 const struct sockaddr_in6 *v6;
2134 struct sockaddr_storage sa;
2138 addrlen = sizeof (sa);
2139 sock = GNUNET_NETWORK_socket_accept (slc->listen_socket,
2140 (struct sockaddr *) &sa,
2144 switch (sa.ss_family)
2147 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
2148 v4 = (const struct sockaddr_in *) &sa;
2149 ok = ( ( (NULL == sh->v4_allowed) ||
2150 (check_ipv4_listed (sh->v4_allowed,
2152 ( (NULL == sh->v4_denied) ||
2153 (! check_ipv4_listed (sh->v4_denied,
2154 &v4->sin_addr)) ) );
2157 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
2158 v6 = (const struct sockaddr_in6 *) &sa;
2159 ok = ( ( (NULL == sh->v6_allowed) ||
2160 (check_ipv6_listed (sh->v6_allowed,
2161 &v6->sin6_addr))) &&
2162 ( (NULL == sh->v6_denied) ||
2163 (! check_ipv6_listed (sh->v6_denied,
2164 &v6->sin6_addr)) ) );
2168 ok = GNUNET_OK; /* controlled using file-system ACL now */
2172 LOG (GNUNET_ERROR_TYPE_WARNING,
2173 _("Unknown address family %d\n"),
2179 LOG (GNUNET_ERROR_TYPE_DEBUG,
2180 "Service rejected incoming connection from %s due to policy.\n",
2181 GNUNET_a2s ((const struct sockaddr *) &sa,
2183 GNUNET_break (GNUNET_OK ==
2184 GNUNET_NETWORK_socket_close (sock));
2187 LOG (GNUNET_ERROR_TYPE_DEBUG,
2188 "Service accepted incoming connection from %s.\n",
2189 GNUNET_a2s ((const struct sockaddr *) &sa,
2191 start_client (slc->sh,
2195 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2203 * Resume accepting connections from the listen socket.
2205 * @param sh service to resume accepting connections.
2208 GNUNET_SERVICE_resume (struct GNUNET_SERVICE_Handle *sh)
2210 struct ServiceListenContext *slc;
2212 for (slc = sh->slc_head; NULL != slc; slc = slc->next)
2214 GNUNET_assert (NULL == slc->listen_task);
2216 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2225 * Task run to resume receiving data from the client after
2226 * the client called #GNUNET_SERVICE_client_continue().
2228 * @param cls our `struct GNUNET_SERVICE_Client`
2231 resume_client_receive (void *cls)
2233 struct GNUNET_SERVICE_Client *c = cls;
2236 c->recv_task = NULL;
2237 /* first, check if there is still something in the buffer */
2238 ret = GNUNET_MST_next (c->mst,
2240 if (GNUNET_SYSERR == ret)
2243 GNUNET_SERVICE_client_drop (c);
2246 if (GNUNET_NO == ret)
2247 return; /* done processing, wait for more later */
2248 GNUNET_assert (GNUNET_OK == ret);
2249 if (GNUNET_YES == c->needs_continue)
2250 return; /* #GNUNET_MST_next() did give a message to the client */
2251 /* need to receive more data from the network first */
2253 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2255 &service_client_recv,
2261 * Continue receiving further messages from the given client.
2262 * Must be called after each message received.
2264 * @param c the client to continue receiving from
2267 GNUNET_SERVICE_client_continue (struct GNUNET_SERVICE_Client *c)
2269 GNUNET_assert (GNUNET_YES == c->needs_continue);
2270 GNUNET_assert (NULL == c->recv_task);
2271 c->needs_continue = GNUNET_NO;
2272 if (NULL != c->warn_task)
2274 GNUNET_SCHEDULER_cancel (c->warn_task);
2275 c->warn_task = NULL;
2278 = GNUNET_SCHEDULER_add_now (&resume_client_receive,
2284 * Disable the warning the server issues if a message is not
2285 * acknowledged in a timely fashion. Use this call if a client is
2286 * intentionally delayed for a while. Only applies to the current
2289 * @param c client for which to disable the warning
2292 GNUNET_SERVICE_client_disable_continue_warning (struct GNUNET_SERVICE_Client *c)
2294 GNUNET_break (NULL != c->warn_task);
2295 if (NULL != c->warn_task)
2297 GNUNET_SCHEDULER_cancel (c->warn_task);
2298 c->warn_task = NULL;
2304 * Ask the server to disconnect from the given client. This is the
2305 * same as returning #GNUNET_SYSERR within the check procedure when
2306 * handling a message, wexcept that it allows dropping of a client even
2307 * when not handling a message from that client. The `disconnect_cb`
2308 * will be called on @a c even if the application closes the connection
2309 * using this function.
2311 * @param c client to disconnect now
2314 GNUNET_SERVICE_client_drop (struct GNUNET_SERVICE_Client *c)
2316 struct GNUNET_SERVICE_Handle *sh = c->sh;
2318 GNUNET_CONTAINER_DLL_remove (sh->clients_head,
2321 sh->disconnect_cb (sh->cb_cls,
2324 if (NULL != c->warn_task)
2326 GNUNET_SCHEDULER_cancel (c->warn_task);
2327 c->warn_task = NULL;
2329 if (NULL != c->recv_task)
2331 GNUNET_SCHEDULER_cancel (c->recv_task);
2332 c->recv_task = NULL;
2334 if (NULL != c->send_task)
2336 GNUNET_SCHEDULER_cancel (c->send_task);
2337 c->send_task = NULL;
2339 GNUNET_MST_destroy (c->mst);
2340 GNUNET_MQ_destroy (c->mq);
2341 if (GNUNET_NO == c->persist)
2343 GNUNET_break (GNUNET_OK ==
2344 GNUNET_NETWORK_socket_close (c->sock));
2348 GNUNET_NETWORK_socket_free_memory_only_ (c->sock);
2351 if ( (GNUNET_YES == sh->got_shutdown) &&
2352 (GNUNET_NO == have_non_monitor_clients (sh)) )
2353 GNUNET_SERVICE_shutdown (sh);
2358 * Explicitly stops the service.
2360 * @param sh server to shutdown
2363 GNUNET_SERVICE_shutdown (struct GNUNET_SERVICE_Handle *sh)
2365 struct GNUNET_SERVICE_Client *client;
2367 GNUNET_SERVICE_suspend (sh);
2368 sh->got_shutdown = GNUNET_NO;
2369 while (NULL != (client = sh->clients_head))
2370 GNUNET_SERVICE_client_drop (client);
2375 * Set the 'monitor' flag on this client. Clients which have been
2376 * marked as 'monitors' won't prevent the server from shutting down
2377 * once #GNUNET_SERVICE_stop_listening() has been invoked. The idea is
2378 * that for "normal" clients we likely want to allow them to process
2379 * their requests; however, monitor-clients are likely to 'never'
2380 * disconnect during shutdown and thus will not be considered when
2381 * determining if the server should continue to exist after
2382 * shutdown has been triggered.
2384 * @param c client to mark as a monitor
2387 GNUNET_SERVICE_client_mark_monitor (struct GNUNET_SERVICE_Client *c)
2389 c->is_monitor = GNUNET_YES;
2390 if ( (GNUNET_YES == c->sh->got_shutdown) &&
2391 (GNUNET_NO == have_non_monitor_clients (c->sh)) )
2392 GNUNET_SERVICE_shutdown (c->sh);
2397 * Set the persist option on this client. Indicates that the
2398 * underlying socket or fd should never really be closed. Used for
2399 * indicating process death.
2401 * @param c client to persist the socket (never to be closed)
2404 GNUNET_SERVICE_client_persist (struct GNUNET_SERVICE_Client *c)
2406 c->persist = GNUNET_YES;
2411 * Obtain the message queue of @a c. Convenience function.
2413 * @param c the client to continue receiving from
2414 * @return the message queue of @a c
2416 struct GNUNET_MQ_Handle *
2417 GNUNET_SERVICE_client_get_mq (struct GNUNET_SERVICE_Client *c)
2423 /* end of service_new.c */