2 This file is part of GNUnet.
3 (C) 2009, 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/service.c
23 * @brief functions related to starting services
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_resolver_service.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
42 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
45 /* ******************* access control ******************** */
48 * Start task that may speed up our system clock artificially
50 * @param cfg configuration to use
51 * @return GNUNET_OK on success, GNUNET_SYSERR if the speedup was not configured
54 GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg);
57 * Stop tasks that modify clock behavior.
60 GNUNET_SPEEDUP_stop_ (void);
64 * Check if the given IP address is in the list of IP addresses.
66 * @param list a list of networks
67 * @param add the IP to check (in network byte order)
68 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
71 check_ipv4_listed (const struct GNUNET_STRINGS_IPv4NetworkPolicy *list,
72 const struct in_addr *add)
79 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
81 if ((add->s_addr & list[i].netmask.s_addr) ==
82 (list[i].network.s_addr & list[i].netmask.s_addr))
91 * Check if the given IP address is in the list of IP addresses.
93 * @param list a list of networks
94 * @param ip the IP to check (in network byte order)
95 * @return #GNUNET_NO if the IP is not in the list, #GNUNET_YES if it it is
98 check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
99 const struct in6_addr *ip)
103 struct in6_addr zero;
107 memset (&zero, 0, sizeof (struct in6_addr));
110 while (0 != memcmp (&zero, &list[i].network, sizeof (struct in6_addr)))
112 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
113 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
114 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
125 /* ****************** service struct ****************** */
129 * Context for "service_task".
131 struct GNUNET_SERVICE_Context
136 const struct GNUNET_CONFIGURATION_Handle *cfg;
139 * Handle for the server.
141 struct GNUNET_SERVER_Handle *server;
144 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
147 struct sockaddr **addrs;
150 * Name of our service.
152 const char *service_name;
155 * Main service-specific task to run.
157 GNUNET_SERVICE_Main task;
165 * IPv4 addresses that are not allowed to connect.
167 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_denied;
170 * IPv6 addresses that are not allowed to connect.
172 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_denied;
175 * IPv4 addresses that are allowed to connect (if not
176 * set, all are allowed).
178 struct GNUNET_STRINGS_IPv4NetworkPolicy *v4_allowed;
181 * IPv6 addresses that are allowed to connect (if not
182 * set, all are allowed).
184 struct GNUNET_STRINGS_IPv6NetworkPolicy *v6_allowed;
187 * My (default) message handlers. Adjusted copy
190 struct GNUNET_SERVER_MessageHandler *my_handlers;
193 * Array of the lengths of the entries in addrs.
198 * NULL-terminated array of listen sockets we should take over.
200 struct GNUNET_NETWORK_Handle **lsocks;
203 * Task ID of the shutdown task.
205 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
208 * Idle timeout for server.
210 struct GNUNET_TIME_Relative timeout;
213 * Overall success/failure of the service start.
218 * If we are daemonizing, this FD is set to the
219 * pipe to the parent. Send '.' if we started
220 * ok, '!' if not. -1 if we are not daemonizing.
222 int ready_confirm_fd;
225 * Do we close connections if we receive messages
226 * for which we have no handler?
231 * Do we require a matching UID for UNIX domain socket connections?
232 * #GNUNET_NO means that the UID does not have to match (however,
233 * @e match_gid may still impose other access control checks).
238 * Do we require a matching GID for UNIX domain socket connections?
239 * Ignored if @e match_uid is #GNUNET_YES. Note that this is about
240 * checking that the client's UID is in our group OR that the
241 * client's GID is our GID. If both "match_gid" and @e match_uid are
242 * #GNUNET_NO, all users on the local system have access.
249 enum GNUNET_SERVICE_Options options;
254 /* ****************** message handlers ****************** */
257 * Send a 'TEST' message back to the client.
259 * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
260 * @param size number of bytes available in 'buf'
261 * @param buf where to copy the message
262 * @return number of bytes written to 'buf'
265 write_test (void *cls, size_t size, void *buf)
267 struct GNUNET_SERVER_Client *client = cls;
268 struct GNUNET_MessageHeader *msg;
270 if (size < sizeof (struct GNUNET_MessageHeader))
272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
273 return 0; /* client disconnected */
275 msg = (struct GNUNET_MessageHeader *) buf;
276 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
277 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
278 GNUNET_SERVER_receive_done (client, GNUNET_OK);
279 return sizeof (struct GNUNET_MessageHeader);
284 * Handler for TEST message.
286 * @param cls closure (refers to service)
287 * @param client identification of the client
288 * @param message the actual message
291 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
292 const struct GNUNET_MessageHeader *message)
294 /* simply bounce message back to acknowledge */
296 GNUNET_SERVER_notify_transmit_ready (client,
297 sizeof (struct GNUNET_MessageHeader),
298 GNUNET_TIME_UNIT_FOREVER_REL,
299 &write_test, client))
300 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
305 * Default handlers for all services. Will be copied and the
306 * "callback_cls" fields will be replaced with the specific service
309 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
310 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
311 sizeof (struct GNUNET_MessageHeader)},
316 /* ****************** service core routines ************** */
320 * Check if access to the service is allowed from the given address.
323 * @param uc credentials, if available, otherwise NULL
324 * @param addr address
325 * @param addrlen length of address
326 * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
327 * for unknown address family (will be denied).
330 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
331 const struct sockaddr *addr, socklen_t addrlen)
333 struct GNUNET_SERVICE_Context *sctx = cls;
334 const struct sockaddr_in *i4;
335 const struct sockaddr_in6 *i6;
338 switch (addr->sa_family)
341 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
342 i4 = (const struct sockaddr_in *) addr;
343 ret = ((NULL == sctx->v4_allowed) ||
344 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
345 ((NULL == sctx->v4_denied) ||
346 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
349 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
350 i6 = (const struct sockaddr_in6 *) addr;
351 ret = ((NULL == sctx->v6_allowed) ||
352 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
353 ((NULL == sctx->v6_denied) ||
354 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
358 ret = GNUNET_OK; /* controlled using file-system ACL now */
362 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
364 return GNUNET_SYSERR;
366 if (GNUNET_OK != ret)
368 LOG (GNUNET_ERROR_TYPE_WARNING,
369 _("Access from `%s' denied to service `%s'\n"),
370 GNUNET_a2s (addr, addrlen),
378 * Get the name of the file where we will
379 * write the PID of the service.
381 * @param sctx service context
382 * @return name of the file for the process ID
385 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
390 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
398 * Parse an IPv4 access control list.
400 * @param ret location where to write the ACL (set)
401 * @param sctx service context to use to get the configuration
402 * @param option name of the ACL option to parse
403 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
407 process_acl4 (struct GNUNET_STRINGS_IPv4NetworkPolicy **ret, struct GNUNET_SERVICE_Context *sctx,
412 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
417 GNUNET_break (GNUNET_OK ==
418 GNUNET_CONFIGURATION_get_value_string (sctx->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"),
425 opt, sctx->service_name, option);
427 return GNUNET_SYSERR;
435 * Parse an IPv6 access control list.
437 * @param ret location where to write the ACL (set)
438 * @param sctx service context to use to get the configuration
439 * @param option name of the ACL option to parse
440 * @return #GNUNET_SYSERR on parse error, #GNUNET_OK on success (including
444 process_acl6 (struct GNUNET_STRINGS_IPv6NetworkPolicy **ret, struct GNUNET_SERVICE_Context *sctx,
449 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
454 GNUNET_break (GNUNET_OK ==
455 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
458 if (NULL == (*ret = GNUNET_STRINGS_parse_ipv6_policy (opt)))
460 LOG (GNUNET_ERROR_TYPE_WARNING,
461 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
462 opt, sctx->service_name, option);
464 return GNUNET_SYSERR;
472 * Add the given UNIX domain path as an address to the
473 * list (as the first entry).
475 * @param saddrs array to update
476 * @param saddrlens where to store the address length
477 * @param unixpath path to add
478 * @param abstract GNUNET_YES to add an abstract UNIX domain socket. This
479 * parameter is ignore on systems other than LINUX
482 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
483 const char *unixpath,
487 struct sockaddr_un *un;
489 un = GNUNET_new (struct sockaddr_un);
490 un->sun_family = AF_UNIX;
491 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
493 if (GNUNET_YES == abstract)
494 un->sun_path[0] = '\0';
496 #if HAVE_SOCKADDR_IN_SIN_LEN
497 un->sun_len = (u_char) sizeof (struct sockaddr_un);
499 *saddrs = (struct sockaddr *) un;
500 *saddrlens = sizeof (struct sockaddr_un);
502 /* this function should never be called
503 * unless AF_UNIX is defined! */
510 * Get the list of addresses that a server for the given service
513 * @param service_name name of the service
514 * @param cfg configuration (which specifies the addresses)
515 * @param addrs set (call by reference) to an array of pointers to the
516 * addresses the server should bind to and listen on; the
517 * array will be NULL-terminated (on success)
518 * @param addr_lens set (call by reference) to an array of the lengths
519 * of the respective 'struct sockaddr' struct in the 'addrs'
521 * @return number of addresses found on success,
522 * #GNUNET_SYSERR if the configuration
523 * did not specify reasonable finding information or
524 * if it specified a hostname that could not be resolved;
525 * #GNUNET_NO if the number of addresses configured is
526 * zero (in this case, `*addrs` and `*addr_lens` will be
530 GNUNET_SERVICE_get_server_addresses (const char *service_name,
531 const struct GNUNET_CONFIGURATION_Handle
532 *cfg, struct sockaddr ***addrs,
533 socklen_t ** addr_lens)
536 struct GNUNET_NETWORK_Handle *desc;
537 unsigned long long port;
539 struct addrinfo hints;
540 struct addrinfo *res;
541 struct addrinfo *pos;
542 struct addrinfo *next;
547 struct sockaddr **saddrs;
548 socklen_t *saddrlens;
554 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
558 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
559 return GNUNET_SYSERR;
562 disablev6 = GNUNET_NO;
566 /* probe IPv6 support */
567 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
570 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
573 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
574 return GNUNET_SYSERR;
576 LOG (GNUNET_ERROR_TYPE_INFO,
578 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
579 service_name, STRERROR (errno));
580 disablev6 = GNUNET_YES;
584 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
590 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
593 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
596 LOG (GNUNET_ERROR_TYPE_ERROR,
597 _("Require valid port number for service `%s' in configuration!\n"),
602 LOG (GNUNET_ERROR_TYPE_ERROR,
603 _("Require valid port number for service `%s' in configuration!\n"),
605 return GNUNET_SYSERR;
609 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
611 GNUNET_break (GNUNET_OK ==
612 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
613 "BINDTO", &hostname));
619 abstract = GNUNET_NO;
622 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
624 GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH",
626 (0 < strlen (unixpath)))
628 /* probe UNIX support */
629 struct sockaddr_un s_un;
631 if (strlen (unixpath) >= sizeof (s_un.sun_path))
633 LOG (GNUNET_ERROR_TYPE_WARNING,
634 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
635 (unsigned long long) sizeof (s_un.sun_path));
636 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
637 LOG (GNUNET_ERROR_TYPE_INFO,
638 _("Using `%s' instead\n"), unixpath);
641 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
643 "USE_ABSTRACT_SOCKETS");
644 if (GNUNET_SYSERR == abstract)
645 abstract = GNUNET_NO;
647 if ((GNUNET_YES != abstract)
649 GNUNET_DISK_directory_create_for_file (unixpath)))
650 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
654 if (NULL != unixpath)
656 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
659 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
662 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
663 GNUNET_free_non_null (hostname);
664 GNUNET_free (unixpath);
665 return GNUNET_SYSERR;
667 LOG (GNUNET_ERROR_TYPE_INFO,
668 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
669 service_name, STRERROR (errno));
670 GNUNET_free (unixpath);
675 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
681 if ((0 == port) && (NULL == unixpath))
683 LOG (GNUNET_ERROR_TYPE_ERROR,
684 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
686 GNUNET_free_non_null (hostname);
687 return GNUNET_SYSERR;
691 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
692 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
693 add_unixpath (saddrs, saddrlens, unixpath, abstract);
694 GNUNET_free_non_null (unixpath);
695 GNUNET_free_non_null (hostname);
697 *addr_lens = saddrlens;
701 if (NULL != hostname)
703 LOG (GNUNET_ERROR_TYPE_DEBUG,
704 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
706 memset (&hints, 0, sizeof (struct addrinfo));
708 hints.ai_family = AF_INET;
709 hints.ai_protocol = IPPROTO_TCP;
710 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
713 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
715 GNUNET_free (hostname);
716 GNUNET_free_non_null (unixpath);
717 return GNUNET_SYSERR;
721 while (NULL != (pos = next))
724 if ((disablev6) && (pos->ai_family == AF_INET6))
730 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
731 disablev6 ? "IPv4 " : "", hostname);
733 GNUNET_free (hostname);
734 GNUNET_free_non_null (unixpath);
735 return GNUNET_SYSERR;
738 if (NULL != unixpath)
740 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
741 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
743 if (NULL != unixpath)
745 add_unixpath (saddrs, saddrlens, unixpath, abstract);
749 while (NULL != (pos = next))
752 if ((disablev6) && (AF_INET6 == pos->ai_family))
754 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
755 continue; /* not TCP */
756 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
758 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
759 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
760 if (AF_INET == pos->ai_family)
762 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
763 saddrlens[i] = pos->ai_addrlen;
764 saddrs[i] = GNUNET_malloc (saddrlens[i]);
765 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
766 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
770 GNUNET_assert (AF_INET6 == pos->ai_family);
771 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
772 saddrlens[i] = pos->ai_addrlen;
773 saddrs[i] = GNUNET_malloc (saddrlens[i]);
774 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
775 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
779 GNUNET_free (hostname);
785 /* will bind against everything, just set port */
790 if (NULL != unixpath)
793 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
794 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
795 if (NULL != unixpath)
797 add_unixpath (saddrs, saddrlens, unixpath, abstract);
800 saddrlens[i] = sizeof (struct sockaddr_in);
801 saddrs[i] = GNUNET_malloc (saddrlens[i]);
802 #if HAVE_SOCKADDR_IN_SIN_LEN
803 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
805 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
806 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
812 if (NULL != unixpath)
814 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
815 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
817 if (NULL != unixpath)
819 add_unixpath (saddrs, saddrlens, unixpath, abstract);
822 saddrlens[i] = sizeof (struct sockaddr_in6);
823 saddrs[i] = GNUNET_malloc (saddrlens[i]);
824 #if HAVE_SOCKADDR_IN_SIN_LEN
825 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
827 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
828 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
830 saddrlens[i] = sizeof (struct sockaddr_in);
831 saddrs[i] = GNUNET_malloc (saddrlens[i]);
832 #if HAVE_SOCKADDR_IN_SIN_LEN
833 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
835 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
836 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
839 GNUNET_free_non_null (unixpath);
841 *addr_lens = saddrlens;
848 * Read listen sockets from the parent process (ARM).
850 * @param sctx service context to initialize
851 * @return #GNUNET_YES if ok, #GNUNET_NO if not ok (must bind yourself),
852 * and #GNUNET_SYSERR on error.
855 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
863 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
864 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
866 /* Using W32 API directly here, because this pipe will
867 * never be used outside of this function, and it's just too much of a bother
868 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
870 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
871 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
880 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
881 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
884 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
887 for (i = 0; i < count; i++)
889 WSAPROTOCOL_INFOA pi;
893 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
894 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
896 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
897 if ( (0 == ret) || (sizeof (pi) != rd))
899 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
900 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
901 if (NULL == sctx->lsocks[i])
903 else if (i == count - 1)
908 sctx->lsocks[count] = NULL;
913 CloseHandle (lsocks_pipe);
917 LOG (GNUNET_ERROR_TYPE_ERROR,
918 _("Could not access a pre-bound socket, will try to bind myself\n"));
919 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
920 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
921 GNUNET_free_non_null (sctx->lsocks);
931 * Setup addr, addrlen, idle_timeout
932 * based on configuration!
934 * Configuration may specify:
935 * - PORT (where to bind to for TCP)
936 * - UNIXPATH (where to bind to for UNIX domain sockets)
937 * - TIMEOUT (after how many ms does an inactive service timeout);
938 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
939 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
940 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
941 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
942 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
943 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
945 * @param sctx service context to initialize
946 * @return #GNUNET_OK if configuration succeeded
949 setup_service (struct GNUNET_SERVICE_Context *sctx)
951 struct GNUNET_TIME_Relative idleout;
960 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, "TIMEOUT"))
963 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->service_name,
964 "TIMEOUT", &idleout))
966 LOG (GNUNET_ERROR_TYPE_ERROR,
967 _("Specified value for `%s' of service `%s' is invalid\n"),
968 "TIMEOUT", sctx->service_name);
969 return GNUNET_SYSERR;
971 sctx->timeout = idleout;
974 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
976 if (GNUNET_CONFIGURATION_have_value
977 (sctx->cfg, sctx->service_name, "TOLERANT"))
981 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
984 LOG (GNUNET_ERROR_TYPE_ERROR,
985 _("Specified value for `%s' of service `%s' is invalid\n"),
986 "TOLERANT", sctx->service_name);
987 return GNUNET_SYSERR;
991 tolerant = GNUNET_NO;
995 if ((NULL != (nfds = getenv ("LISTEN_FDS"))) &&
996 (1 == SSCANF (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
997 (cnt + 4 < FD_SETSIZE))
1000 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1003 flags = fcntl (3 + cnt, F_GETFD);
1004 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1006 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1008 LOG (GNUNET_ERROR_TYPE_ERROR,
1010 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1011 (unsigned int) 3 + cnt);
1013 while (sctx->lsocks[cnt] != NULL)
1014 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1015 GNUNET_free (sctx->lsocks);
1016 sctx->lsocks = NULL;
1020 unsetenv ("LISTEN_FDS");
1023 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1025 receive_sockets_from_parent (sctx);
1026 putenv ("GNUNET_OS_READ_LSOCKS=");
1030 if ((NULL == sctx->lsocks) &&
1032 GNUNET_SERVICE_get_server_addresses (sctx->service_name, sctx->cfg,
1033 &sctx->addrs, &sctx->addrlens)))
1034 return GNUNET_SYSERR;
1035 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1037 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1040 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1042 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1043 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1044 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1045 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1052 * Get the name of the user that'll be used
1053 * to provide the service.
1055 * @param sctx service context
1056 * @return value of the 'USERNAME' option
1059 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1064 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
1074 * @param sctx service context
1075 * @param pid PID to write (should be equal to 'getpid()'
1076 * @return #GNUNET_OK on success (including no work to be done)
1079 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1087 if (NULL == (pif = get_pid_file_name (sctx)))
1088 return GNUNET_OK; /* no file desired */
1089 user = get_user_name (sctx);
1090 rdir = GNUNET_strdup (pif);
1091 len = strlen (rdir);
1092 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1095 if (0 != ACCESS (rdir, F_OK))
1097 /* we get to create a directory -- and claim it
1099 (void) GNUNET_DISK_directory_create (rdir);
1100 if ((NULL != user) && (0 < strlen (user)))
1101 GNUNET_DISK_file_change_owner (rdir, user);
1103 if (0 != ACCESS (rdir, W_OK | X_OK))
1105 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1107 GNUNET_free_non_null (user);
1109 return GNUNET_SYSERR;
1112 pidfd = FOPEN (pif, "w");
1115 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1117 GNUNET_free_non_null (user);
1118 return GNUNET_SYSERR;
1120 if (0 > FPRINTF (pidfd, "%u", pid))
1121 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1122 GNUNET_break (0 == FCLOSE (pidfd));
1123 if ((NULL != user) && (0 < strlen (user)))
1124 GNUNET_DISK_file_change_owner (pif, user);
1125 GNUNET_free_non_null (user);
1132 * Task run during shutdown. Stops the server/service.
1134 * @param cls the `struct GNUNET_SERVICE_Context`
1138 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1140 struct GNUNET_SERVICE_Context *service = cls;
1141 struct GNUNET_SERVER_Handle *server = service->server;
1143 service->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1144 if (0 != (service->options & GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN))
1145 GNUNET_SERVER_stop_listening (server);
1147 GNUNET_SERVER_destroy (server);
1152 * Initial task for the service.
1154 * @param cls service context
1158 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1160 struct GNUNET_SERVICE_Context *sctx = cls;
1163 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1165 (void) GNUNET_SPEEDUP_start_ (sctx->cfg);
1166 GNUNET_RESOLVER_connect (sctx->cfg);
1167 if (NULL != sctx->lsocks)
1169 = GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1170 sctx->timeout, sctx->require_found);
1173 = GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1174 sctx->timeout, sctx->require_found);
1175 if (NULL == sctx->server)
1177 if (NULL != sctx->addrs)
1178 for (i = 0; NULL != sctx->addrs[i]; i++)
1179 LOG (GNUNET_ERROR_TYPE_INFO,
1180 _("Failed to start `%s' at `%s'\n"),
1181 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1182 sctx->ret = GNUNET_SYSERR;
1186 if (NULL != sctx->addrs)
1187 for (i = 0; NULL != sctx->addrs[i]; i++)
1188 if ((AF_UNIX == sctx->addrs[i]->sa_family)
1189 && ('\0' != ((const struct sockaddr_un *)sctx->addrs[i])->sun_path[0]))
1190 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sctx->addrs[i])->sun_path,
1196 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1198 /* install a task that will kill the server
1199 * process if the scheduler ever gets a shutdown signal */
1200 sctx->shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1204 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1205 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1207 while (NULL != sctx->my_handlers[i].callback)
1208 sctx->my_handlers[i++].callback_cls = sctx;
1209 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1210 if (-1 != sctx->ready_confirm_fd)
1212 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1213 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1214 sctx->ready_confirm_fd = -1;
1215 write_pid_file (sctx, getpid ());
1217 if (NULL != sctx->addrs)
1220 while (NULL != sctx->addrs[i])
1222 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1223 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1227 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1232 * Detach from terminal.
1234 * @param sctx service context
1235 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1238 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1245 if (0 != PIPE (filedes))
1247 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1248 return GNUNET_SYSERR;
1253 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1254 return GNUNET_SYSERR;
1261 GNUNET_break (0 == CLOSE (filedes[1]));
1263 if (1 != READ (filedes[0], &c, sizeof (char)))
1264 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1271 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1274 LOG (GNUNET_ERROR_TYPE_INFO,
1275 _("Service process could not initialize server function\n"));
1278 LOG (GNUNET_ERROR_TYPE_INFO,
1279 _("Service process failed to report status\n"));
1282 exit (1); /* child reported error */
1284 GNUNET_break (0 == CLOSE (0));
1285 GNUNET_break (0 == CLOSE (1));
1286 GNUNET_break (0 == CLOSE (filedes[0]));
1287 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1289 return GNUNET_SYSERR;
1290 /* set stdin/stdout to /dev/null */
1291 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1293 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1294 (void) CLOSE (nullfd);
1295 return GNUNET_SYSERR;
1297 (void) CLOSE (nullfd);
1298 /* Detach from controlling terminal */
1301 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1302 sctx->ready_confirm_fd = filedes[1];
1304 /* FIXME: we probably need to do something else
1305 * elsewhere in order to fork the process itself... */
1315 * @param sctx service context
1316 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1319 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1323 if (NULL == (user = get_user_name (sctx)))
1324 return GNUNET_OK; /* keep */
1329 pws = getpwnam (user);
1332 LOG (GNUNET_ERROR_TYPE_ERROR,
1333 _("Cannot obtain information about user `%s': %s\n"), user,
1334 errno == 0 ? _("No such user") : STRERROR (errno));
1336 return GNUNET_SYSERR;
1338 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1340 (0 != initgroups (user, pws->pw_gid)) ||
1342 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1344 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1345 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1347 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1348 user, STRERROR (errno));
1350 return GNUNET_SYSERR;
1360 * Delete the PID file that was created by our parent.
1362 * @param sctx service context
1365 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1367 char *pif = get_pid_file_name (sctx);
1370 return; /* no PID file */
1371 if (0 != UNLINK (pif))
1372 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1378 * Run a standard GNUnet service startup sequence (initialize loggers
1379 * and configuration, parse options).
1381 * @param argc number of command line arguments
1382 * @param argv command line arguments
1383 * @param service_name our service name
1384 * @param options service options
1385 * @param task main task of the service
1386 * @param task_cls closure for @a task
1387 * @return #GNUNET_SYSERR on error, #GNUNET_OK
1388 * if we shutdown nicely
1391 GNUNET_SERVICE_run (int argc, char *const *argv,
1392 const char *service_name,
1393 enum GNUNET_SERVICE_Options options,
1394 GNUNET_SERVICE_Main task,
1397 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1407 unsigned long long skew_offset;
1408 unsigned long long skew_variance;
1409 long long clock_offset;
1410 struct GNUNET_SERVICE_Context sctx;
1411 struct GNUNET_CONFIGURATION_Handle *cfg;
1414 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1415 GNUNET_GETOPT_OPTION_CFG_FILE (&opt_cfg_fn),
1416 {'d', "daemonize", NULL,
1417 gettext_noop ("do daemonize (detach from terminal)"), 0,
1418 GNUNET_GETOPT_set_one, &do_daemonize},
1419 GNUNET_GETOPT_OPTION_HELP (NULL),
1420 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1421 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1422 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1423 GNUNET_GETOPT_OPTION_END
1430 xdg = getenv ("XDG_CONFIG_HOME");
1432 GNUNET_asprintf (&cfg_fn,
1438 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1439 memset (&sctx, 0, sizeof (sctx));
1440 sctx.options = options;
1441 sctx.ready_confirm_fd = -1;
1442 sctx.ret = GNUNET_OK;
1443 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1445 sctx.task_cls = task_cls;
1446 sctx.service_name = service_name;
1447 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1449 /* setup subsystems */
1450 ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
1451 if (GNUNET_SYSERR == ret)
1453 if (GNUNET_NO == ret)
1458 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
1460 if (NULL == opt_cfg_fn)
1461 opt_cfg_fn = GNUNET_strdup (cfg_fn);
1462 if (GNUNET_YES == GNUNET_DISK_file_test (opt_cfg_fn))
1464 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, opt_cfg_fn))
1466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1467 _("Malformed configuration file `%s', exit ...\n"),
1474 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, NULL))
1476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1477 _("Malformed configuration, exit ...\n"));
1480 if (0 != strcmp (opt_cfg_fn, cfg_fn))
1481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1482 _("Could not access configuration file `%s'\n"),
1485 if (GNUNET_OK != setup_service (&sctx))
1487 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1489 if (GNUNET_OK != set_user_id (&sctx))
1491 LOG (GNUNET_ERROR_TYPE_DEBUG,
1492 "Service `%s' runs with configuration from `%s'\n",
1496 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1497 "SKEW_OFFSET", &skew_offset)) &&
1499 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1500 "SKEW_VARIANCE", &skew_variance)))
1502 clock_offset = skew_offset - skew_variance;
1503 GNUNET_TIME_set_offset (clock_offset);
1504 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1506 /* actually run service */
1508 GNUNET_SCHEDULER_run (&service_task, &sctx);
1510 if ((1 == do_daemonize) && (NULL != sctx.server))
1511 pid_file_delete (&sctx);
1512 GNUNET_free_non_null (sctx.my_handlers);
1515 if (-1 != sctx.ready_confirm_fd)
1517 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1518 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1519 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1526 GNUNET_CONFIGURATION_have_value (sctx.cfg, service_name,
1529 GNUNET_CONFIGURATION_get_value_string (sctx.cfg, service_name,
1536 GAUGER (service_name, counter, mi.usmblks, "blocks");
1537 GNUNET_free (counter);
1541 GNUNET_SPEEDUP_stop_ ();
1542 GNUNET_CONFIGURATION_destroy (cfg);
1544 if (NULL != sctx.addrs)
1545 while (NULL != sctx.addrs[i])
1546 GNUNET_free (sctx.addrs[i++]);
1547 GNUNET_free_non_null (sctx.addrs);
1548 GNUNET_free_non_null (sctx.addrlens);
1549 GNUNET_free_non_null (logfile);
1550 GNUNET_free_non_null (loglev);
1551 GNUNET_free (cfg_fn);
1552 GNUNET_free_non_null (opt_cfg_fn);
1553 GNUNET_free_non_null (sctx.v4_denied);
1554 GNUNET_free_non_null (sctx.v6_denied);
1555 GNUNET_free_non_null (sctx.v4_allowed);
1556 GNUNET_free_non_null (sctx.v6_allowed);
1558 return err ? GNUNET_SYSERR : sctx.ret;
1563 * Run a service startup sequence within an existing
1564 * initialized system.
1566 * @param service_name our service name
1567 * @param cfg configuration to use
1568 * @param options service options
1569 * @return NULL on error, service handle
1571 struct GNUNET_SERVICE_Context *
1572 GNUNET_SERVICE_start (const char *service_name,
1573 const struct GNUNET_CONFIGURATION_Handle *cfg,
1574 enum GNUNET_SERVICE_Options options)
1577 struct GNUNET_SERVICE_Context *sctx;
1579 sctx = GNUNET_new (struct GNUNET_SERVICE_Context);
1580 sctx->ready_confirm_fd = -1; /* no daemonizing */
1581 sctx->ret = GNUNET_OK;
1582 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1583 sctx->service_name = service_name;
1585 sctx->options = options;
1587 /* setup subsystems */
1588 if (GNUNET_OK != setup_service (sctx))
1590 GNUNET_SERVICE_stop (sctx);
1593 if (NULL != sctx->lsocks)
1595 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1596 sctx->timeout, sctx->require_found);
1599 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1600 sctx->timeout, sctx->require_found);
1602 if (NULL == sctx->server)
1604 GNUNET_SERVICE_stop (sctx);
1608 if (NULL != sctx->addrs)
1609 for (i = 0; NULL != sctx->addrs[i]; i++)
1610 if ((AF_UNIX == sctx->addrs[i]->sa_family)
1611 && ('\0' != ((const struct sockaddr_un *)sctx->addrs[i])->sun_path[0]))
1612 GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sctx->addrs[i])->sun_path,
1616 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1617 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1619 while ((sctx->my_handlers[i].callback != NULL))
1620 sctx->my_handlers[i++].callback_cls = sctx;
1621 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1627 * Obtain the server used by a service. Note that the server must NOT
1628 * be destroyed by the caller.
1630 * @param ctx the service context returned from the start function
1631 * @return handle to the server for this service, NULL if there is none
1633 struct GNUNET_SERVER_Handle *
1634 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1641 * Stop a service that was started with "GNUNET_SERVICE_start".
1643 * @param sctx the service context returned from the start function
1646 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1655 GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name,
1658 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, sctx->service_name,
1665 GAUGER (sctx->service_name, counter, mi.usmblks, "blocks");
1666 GNUNET_free (counter);
1670 if (GNUNET_SCHEDULER_NO_TASK != sctx->shutdown_task)
1672 GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
1673 sctx->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1675 if (NULL != sctx->server)
1676 GNUNET_SERVER_destroy (sctx->server);
1677 GNUNET_free_non_null (sctx->my_handlers);
1678 if (NULL != sctx->addrs)
1681 while (NULL != sctx->addrs[i])
1682 GNUNET_free (sctx->addrs[i++]);
1683 GNUNET_free (sctx->addrs);
1685 GNUNET_free_non_null (sctx->addrlens);
1686 GNUNET_free_non_null (sctx->v4_denied);
1687 GNUNET_free_non_null (sctx->v6_denied);
1688 GNUNET_free_non_null (sctx->v4_allowed);
1689 GNUNET_free_non_null (sctx->v6_allowed);
1694 /* end of service.c */