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 2, 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_common.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_directories.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_service_lib.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
41 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
43 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
46 /* ******************* access control ******************** */
49 * @brief IPV4 network in CIDR notation.
56 struct in_addr network;
61 struct in_addr netmask;
65 * @brief network in CIDR notation for IPV6.
72 struct in6_addr network;
77 struct in6_addr netmask;
82 * Parse a network specification. The argument specifies
83 * a list of networks. The format is
84 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
85 * with a semicolon). The network must be given in dotted-decimal
86 * notation. The netmask can be given in CIDR notation (/16) or
87 * in dotted-decimal (/255.255.0.0).
89 * @param routeList a string specifying the forbidden networks
90 * @return the converted list, NULL if the synatx is flawed
92 static struct IPv4NetworkSet *
93 parse_ipv4_specification (const char *routeList)
101 unsigned int temps[8];
103 struct IPv4NetworkSet *result;
105 if (NULL == routeList)
107 len = strlen (routeList);
111 for (i = 0; i < len; i++)
112 if (routeList[i] == ';')
114 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
120 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
121 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
122 &temps[6], &temps[7]);
125 for (j = 0; j < 8; j++)
128 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
130 GNUNET_free (result);
133 result[i].network.s_addr =
134 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
136 result[i].netmask.s_addr =
137 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
139 while (routeList[pos] != ';')
145 /* try second notation */
147 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
148 &temps[2], &temps[3], &slash);
151 for (j = 0; j < 4; j++)
154 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
156 GNUNET_free (result);
159 result[i].network.s_addr =
160 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
162 if ((slash <= 32) && (slash >= 0))
164 result[i].netmask.s_addr = 0;
167 result[i].netmask.s_addr =
168 (result[i].netmask.s_addr >> 1) + 0x80000000;
171 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
172 while (';' != routeList[pos])
180 LOG (GNUNET_ERROR_TYPE_ERROR,
181 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
183 GNUNET_free (result);
184 return NULL; /* error */
187 /* try third notation */
190 SSCANF (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
191 &temps[2], &temps[3]);
194 for (j = 0; j < 4; j++)
197 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
199 GNUNET_free (result);
202 result[i].network.s_addr =
203 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
205 result[i].netmask.s_addr = 0;
208 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
211 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
212 while (routeList[pos] != ';')
218 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
220 GNUNET_free (result);
221 return NULL; /* error */
223 if (pos < strlen (routeList))
225 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
227 GNUNET_free (result);
228 return NULL; /* oops */
230 return result; /* ok */
235 * Parse a network specification. The argument specifies
236 * a list of networks. The format is
237 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
238 * with a semicolon). The network must be given in colon-hex
239 * notation. The netmask must be given in CIDR notation (/16) or
240 * can be omitted to specify a single host.
242 * @param routeListX a string specifying the forbidden networks
243 * @return the converted list, NULL if the synatx is flawed
245 static struct IPv6NetworkSet *
246 parse_ipv6_specification (const char *routeListX)
256 struct IPv6NetworkSet *result;
261 if (NULL == routeListX)
263 len = strlen (routeListX);
266 routeList = GNUNET_strdup (routeListX);
268 for (i = 0; i < len; i++)
269 if (';' == routeList[i])
271 if (';' != routeList[len - 1])
273 LOG (GNUNET_ERROR_TYPE_ERROR,
274 _("Invalid network notation (does not end with ';': `%s')\n"),
276 GNUNET_free (routeList);
280 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
286 while (';' != routeList[pos])
289 while ((slash >= start) && (routeList[slash] != '/'))
293 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
298 routeList[pos] = '\0';
299 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
303 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
306 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"),
307 &routeList[slash + 1]);
311 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
313 GNUNET_free (result);
314 GNUNET_free (routeList);
320 result[i].netmask.s6_addr[off++] = 0xFF;
325 result[i].netmask.s6_addr[off] =
326 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
331 routeList[slash] = '\0';
332 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
336 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"),
337 &routeList[slash + 1]);
339 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
340 GNUNET_free (result);
341 GNUNET_free (routeList);
347 GNUNET_free (routeList);
353 * Check if the given IP address is in the list of IP addresses.
355 * @param list a list of networks
356 * @param add the IP to check (in network byte order)
357 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
360 check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
367 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
369 if ((add->s_addr & list[i].netmask.s_addr) ==
370 (list[i].network.s_addr & list[i].netmask.s_addr))
379 * Check if the given IP address is in the list of IP addresses.
381 * @param list a list of networks
382 * @param ip the IP to check (in network byte order)
383 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
386 check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
390 struct in6_addr zero;
394 memset (&zero, 0, sizeof (struct in6_addr));
397 while (0 != memcmp (&zero, &list[i].network, sizeof (struct in6_addr)))
399 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
400 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
401 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
412 /* ****************** service struct ****************** */
416 * Context for "service_task".
418 struct GNUNET_SERVICE_Context
423 const struct GNUNET_CONFIGURATION_Handle *cfg;
426 * Handle for the server.
428 struct GNUNET_SERVER_Handle *server;
431 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
434 struct sockaddr **addrs;
437 * Name of our service.
439 const char *service_name;
442 * Main service-specific task to run.
444 GNUNET_SERVICE_Main task;
452 * IPv4 addresses that are not allowed to connect.
454 struct IPv4NetworkSet *v4_denied;
457 * IPv6 addresses that are not allowed to connect.
459 struct IPv6NetworkSet *v6_denied;
462 * IPv4 addresses that are allowed to connect (if not
463 * set, all are allowed).
465 struct IPv4NetworkSet *v4_allowed;
468 * IPv6 addresses that are allowed to connect (if not
469 * set, all are allowed).
471 struct IPv6NetworkSet *v6_allowed;
474 * My (default) message handlers. Adjusted copy
477 struct GNUNET_SERVER_MessageHandler *my_handlers;
480 * Array of the lengths of the entries in addrs.
485 * NULL-terminated array of listen sockets we should take over.
487 struct GNUNET_NETWORK_Handle **lsocks;
490 * Idle timeout for server.
492 struct GNUNET_TIME_Relative timeout;
495 * Overall success/failure of the service start.
500 * If we are daemonizing, this FD is set to the
501 * pipe to the parent. Send '.' if we started
502 * ok, '!' if not. -1 if we are not daemonizing.
504 int ready_confirm_fd;
507 * Do we close connections if we receive messages
508 * for which we have no handler?
513 * Do we require a matching UID for UNIX domain socket connections?
514 * GNUNET_NO means that the UID does not have to match (however,
515 * "match_gid" may still impose other access control checks).
520 * Do we require a matching GID for UNIX domain socket connections?
521 * Ignored if "match_uid" is GNUNET_YES. Note that this is about
522 * checking that the client's UID is in our group OR that the
523 * client's GID is our GID. If both "match_gid" and "match_uid" are
524 * "GNUNET_NO", all users on the local system have access.
531 enum GNUNET_SERVICE_Options options;
536 /* ****************** message handlers ****************** */
539 * Send a 'TEST' message back to the client.
541 * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
542 * @param size number of bytes available in 'buf'
543 * @param buf where to copy the message
544 * @return number of bytes written to 'buf'
547 write_test (void *cls, size_t size, void *buf)
549 struct GNUNET_SERVER_Client *client = cls;
550 struct GNUNET_MessageHeader *msg;
552 if (size < sizeof (struct GNUNET_MessageHeader))
554 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
555 return 0; /* client disconnected */
557 msg = (struct GNUNET_MessageHeader *) buf;
558 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
559 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
560 GNUNET_SERVER_receive_done (client, GNUNET_OK);
561 return sizeof (struct GNUNET_MessageHeader);
566 * Handler for TEST message.
568 * @param cls closure (refers to service)
569 * @param client identification of the client
570 * @param message the actual message
573 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
574 const struct GNUNET_MessageHeader *message)
576 /* simply bounce message back to acknowledge */
578 GNUNET_SERVER_notify_transmit_ready (client,
579 sizeof (struct GNUNET_MessageHeader),
580 GNUNET_TIME_UNIT_FOREVER_REL,
581 &write_test, client))
582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
587 * Default handlers for all services. Will be copied and the
588 * "callback_cls" fields will be replaced with the specific service
591 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
592 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
593 sizeof (struct GNUNET_MessageHeader)},
598 /* ****************** service core routines ************** */
602 * Check if access to the service is allowed from the given address.
605 * @param uc credentials, if available, otherwise NULL
606 * @param addr address
607 * @param addrlen length of address
608 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
609 * for unknown address family (will be denied).
612 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
613 const struct sockaddr *addr, socklen_t addrlen)
615 struct GNUNET_SERVICE_Context *sctx = cls;
616 const struct sockaddr_in *i4;
617 const struct sockaddr_in6 *i6;
620 switch (addr->sa_family)
623 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
624 i4 = (const struct sockaddr_in *) addr;
625 ret = ((NULL == sctx->v4_allowed) ||
626 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
627 ((NULL == sctx->v4_denied) ||
628 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
631 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
632 i6 = (const struct sockaddr_in6 *) addr;
633 ret = ((NULL == sctx->v6_allowed) ||
634 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
635 ((NULL == sctx->v6_denied) ||
636 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
640 ret = GNUNET_OK; /* always OK for now */
641 if (GNUNET_YES == sctx->match_uid)
643 /* UID match required */
644 ret = (NULL != uc) && (uc->uid == geteuid ());
646 else if ( (GNUNET_YES == sctx->match_gid) &&
647 ( (NULL == uc) || (uc->uid != geteuid ()) ) )
649 /* group match required and UID does not match */
652 /* no credentials, group match not possible */
660 if (uc->gid != getegid())
662 /* default group did not match, but maybe the user is in our group, let's check */
663 grp = getgrgid (getegid ());
666 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getgrgid");
670 for (i=0; NULL != grp->gr_mem[i]; i++)
672 struct passwd *nam = getpwnam (grp->gr_mem[i]);
674 continue; /* name in group that is not in user DB !? */
675 if (nam->pw_uid == uc->uid)
677 /* yes, uid is in our group, allow! */
685 if (GNUNET_NO == ret)
686 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
687 (NULL == uc) ? -1 : uc->uid, (NULL == uc) ? -1 : uc->gid);
691 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
693 return GNUNET_SYSERR;
695 if (GNUNET_OK != ret)
697 LOG (GNUNET_ERROR_TYPE_WARNING,
698 _("Access from `%s' denied to service `%s'\n"),
699 GNUNET_a2s (addr, addrlen),
707 * Get the name of the file where we will
708 * write the PID of the service.
710 * @param sctx service context
711 * @return name of the file for the process ID
714 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
719 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
727 * Parse an IPv4 access control list.
729 * @param ret location where to write the ACL (set)
730 * @param sctx service context to use to get the configuration
731 * @param option name of the ACL option to parse
732 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
736 process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
741 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
746 GNUNET_break (GNUNET_OK ==
747 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
750 if (NULL == (*ret = parse_ipv4_specification (opt)))
752 LOG (GNUNET_ERROR_TYPE_WARNING,
753 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
754 opt, sctx->service_name, option);
756 return GNUNET_SYSERR;
764 * Parse an IPv6 access control list.
766 * @param ret location where to write the ACL (set)
767 * @param sctx service context to use to get the configuration
768 * @param option name of the ACL option to parse
769 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
773 process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
778 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
783 GNUNET_break (GNUNET_OK ==
784 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
787 if (NULL == (*ret = parse_ipv6_specification (opt)))
789 LOG (GNUNET_ERROR_TYPE_WARNING,
790 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
791 opt, sctx->service_name, option);
793 return GNUNET_SYSERR;
801 * Add the given UNIX domain path as an address to the
802 * list (as the first entry).
804 * @param saddrs array to update
805 * @param saddrlens where to store the address length
806 * @param unixpath path to add
809 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
810 const char *unixpath)
813 struct sockaddr_un *un;
816 un = GNUNET_malloc (sizeof (struct sockaddr_un));
817 un->sun_family = AF_UNIX;
818 slen = strlen (unixpath) + 1;
819 if (slen >= sizeof (un->sun_path))
820 slen = sizeof (un->sun_path) - 1;
821 memcpy (un->sun_path, unixpath, slen);
822 un->sun_path[slen] = '\0';
823 slen = sizeof (struct sockaddr_un);
825 un->sun_path[0] = '\0';
827 #if HAVE_SOCKADDR_IN_SIN_LEN
828 un->sun_len = (u_char) slen;
830 *saddrs = (struct sockaddr *) un;
833 /* this function should never be called
834 * unless AF_UNIX is defined! */
841 * Get the list of addresses that a server for the given service
844 * @param service_name name of the service
845 * @param cfg configuration (which specifies the addresses)
846 * @param addrs set (call by reference) to an array of pointers to the
847 * addresses the server should bind to and listen on; the
848 * array will be NULL-terminated (on success)
849 * @param addr_lens set (call by reference) to an array of the lengths
850 * of the respective 'struct sockaddr' struct in the 'addrs'
852 * @return number of addresses found on success,
853 * GNUNET_SYSERR if the configuration
854 * did not specify reasonable finding information or
855 * if it specified a hostname that could not be resolved;
856 * GNUNET_NO if the number of addresses configured is
857 * zero (in this case, '*addrs' and '*addr_lens' will be
861 GNUNET_SERVICE_get_server_addresses (const char *service_name,
862 const struct GNUNET_CONFIGURATION_Handle
863 *cfg, struct sockaddr ***addrs,
864 socklen_t ** addr_lens)
867 struct GNUNET_NETWORK_Handle *desc;
868 unsigned long long port;
870 struct addrinfo hints;
871 struct addrinfo *res;
872 struct addrinfo *pos;
873 struct addrinfo *next;
877 struct sockaddr **saddrs;
878 socklen_t *saddrlens;
884 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
888 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
889 return GNUNET_SYSERR;
892 disablev6 = GNUNET_NO;
896 /* probe IPv6 support */
897 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
900 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
903 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
904 return GNUNET_SYSERR;
906 LOG (GNUNET_ERROR_TYPE_INFO,
908 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
909 service_name, STRERROR (errno));
910 disablev6 = GNUNET_YES;
914 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
920 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
922 GNUNET_break (GNUNET_OK ==
923 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
927 LOG (GNUNET_ERROR_TYPE_ERROR,
928 _("Require valid port number for service `%s' in configuration!\n"),
930 return GNUNET_SYSERR;
934 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
936 GNUNET_break (GNUNET_OK ==
937 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
938 "BINDTO", &hostname));
946 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
948 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
950 (0 < strlen (unixpath)))
952 /* probe UNIX support */
953 struct sockaddr_un s_un;
955 if (strlen (unixpath) >= sizeof (s_un.sun_path))
957 LOG (GNUNET_ERROR_TYPE_WARNING,
958 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
959 sizeof (s_un.sun_path));
960 GNUNET_free_non_null (hostname);
961 GNUNET_free (unixpath);
962 return GNUNET_SYSERR;
965 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
968 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
971 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
972 GNUNET_free_non_null (hostname);
973 GNUNET_free (unixpath);
974 return GNUNET_SYSERR;
976 LOG (GNUNET_ERROR_TYPE_INFO,
978 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
979 service_name, STRERROR (errno));
980 GNUNET_free (unixpath);
985 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
991 if ((0 == port) && (NULL == unixpath))
993 LOG (GNUNET_ERROR_TYPE_ERROR,
995 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
997 GNUNET_free_non_null (hostname);
998 return GNUNET_SYSERR;
1002 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
1003 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
1004 add_unixpath (saddrs, saddrlens, unixpath);
1005 GNUNET_free_non_null (unixpath);
1006 GNUNET_free_non_null (hostname);
1008 *addr_lens = saddrlens;
1012 if (NULL != hostname)
1014 LOG (GNUNET_ERROR_TYPE_DEBUG,
1015 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
1017 memset (&hints, 0, sizeof (struct addrinfo));
1019 hints.ai_family = AF_INET;
1020 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1023 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
1024 gai_strerror (ret));
1025 GNUNET_free (hostname);
1026 GNUNET_free_non_null (unixpath);
1027 return GNUNET_SYSERR;
1031 while (NULL != (pos = next))
1033 next = pos->ai_next;
1034 if ((disablev6) && (pos->ai_family == AF_INET6))
1040 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
1041 disablev6 ? "IPv4 " : "", hostname);
1043 GNUNET_free (hostname);
1044 GNUNET_free_non_null (unixpath);
1045 return GNUNET_SYSERR;
1048 if (NULL != unixpath)
1050 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1051 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1053 if (NULL != unixpath)
1055 add_unixpath (saddrs, saddrlens, unixpath);
1059 while (NULL != (pos = next))
1061 next = pos->ai_next;
1062 if ((disablev6) && (AF_INET6 == pos->ai_family))
1064 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1065 continue; /* not TCP */
1066 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1067 continue; /* huh? */
1068 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1069 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1070 if (AF_INET == pos->ai_family)
1072 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
1073 saddrlens[i] = pos->ai_addrlen;
1074 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1075 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1076 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1080 GNUNET_assert (AF_INET6 == pos->ai_family);
1081 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
1082 saddrlens[i] = pos->ai_addrlen;
1083 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1084 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1085 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1089 GNUNET_free (hostname);
1095 /* will bind against everything, just set port */
1100 if (NULL != unixpath)
1103 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1104 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1105 if (NULL != unixpath)
1107 add_unixpath (saddrs, saddrlens, unixpath);
1110 saddrlens[i] = sizeof (struct sockaddr_in);
1111 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1112 #if HAVE_SOCKADDR_IN_SIN_LEN
1113 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1115 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1116 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1122 if (NULL != unixpath)
1124 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1125 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1127 if (NULL != unixpath)
1129 add_unixpath (saddrs, saddrlens, unixpath);
1132 saddrlens[i] = sizeof (struct sockaddr_in6);
1133 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1134 #if HAVE_SOCKADDR_IN_SIN_LEN
1135 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1137 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1138 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1140 saddrlens[i] = sizeof (struct sockaddr_in);
1141 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1142 #if HAVE_SOCKADDR_IN_SIN_LEN
1143 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1145 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1146 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1149 GNUNET_free_non_null (unixpath);
1151 *addr_lens = saddrlens;
1158 * Read listen sockets from the parent process (ARM).
1160 * @param sctx service context to initialize
1161 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1162 * and GNUNET_SYSERR on error.
1165 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1167 const char *env_buf;
1173 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1174 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
1176 /* Using W32 API directly here, because this pipe will
1177 * never be used outside of this function, and it's just too much of a bother
1178 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1180 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1181 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
1190 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1191 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
1194 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
1197 for (i = 0; i < count; i++)
1199 WSAPROTOCOL_INFOA pi;
1203 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1204 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
1206 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1207 if ( (0 == ret) || (sizeof (pi) != rd))
1209 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1210 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1211 if (NULL == sctx->lsocks[i])
1213 else if (i == count - 1)
1218 sctx->lsocks[count] = NULL;
1223 CloseHandle (lsocks_pipe);
1227 LOG (GNUNET_ERROR_TYPE_ERROR,
1228 _("Could not access a pre-bound socket, will try to bind myself\n"));
1229 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
1230 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1231 GNUNET_free_non_null (sctx->lsocks);
1232 sctx->lsocks = NULL;
1241 * Setup addr, addrlen, idle_timeout
1242 * based on configuration!
1244 * Configuration may specify:
1245 * - PORT (where to bind to for TCP)
1246 * - UNIXPATH (where to bind to for UNIX domain sockets)
1247 * - TIMEOUT (after how many ms does an inactive service timeout);
1248 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1249 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1250 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1251 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1252 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1253 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1255 * @param sctx service context to initialize
1256 * @return GNUNET_OK if configuration succeeded
1259 setup_service (struct GNUNET_SERVICE_Context *sctx)
1261 struct GNUNET_TIME_Relative idleout;
1272 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, "TIMEOUT"))
1275 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->service_name,
1276 "TIMEOUT", &idleout))
1278 LOG (GNUNET_ERROR_TYPE_ERROR,
1279 _("Specified value for `%s' of service `%s' is invalid\n"),
1280 "TIMEOUT", sctx->service_name);
1281 return GNUNET_SYSERR;
1283 sctx->timeout = idleout;
1286 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1288 if (GNUNET_CONFIGURATION_have_value
1289 (sctx->cfg, sctx->service_name, "TOLERANT"))
1291 if (GNUNET_SYSERR ==
1293 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1296 LOG (GNUNET_ERROR_TYPE_ERROR,
1297 _("Specified value for `%s' of service `%s' is invalid\n"),
1298 "TOLERANT", sctx->service_name);
1299 return GNUNET_SYSERR;
1303 tolerant = GNUNET_NO;
1307 if ((NULL != (lpid = getenv ("LISTEN_PID"))) &&
1308 (1 == SSCANF (lpid, "%u", &pid)) && (getpid () == (pid_t) pid) &&
1309 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1310 (1 == SSCANF (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1311 (cnt + 4 < FD_SETSIZE))
1314 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1317 flags = fcntl (3 + cnt, F_GETFD);
1318 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1320 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1322 LOG (GNUNET_ERROR_TYPE_ERROR,
1324 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1325 (unsigned int) 3 + cnt);
1327 while (sctx->lsocks[cnt] != NULL)
1328 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1329 GNUNET_free (sctx->lsocks);
1330 sctx->lsocks = NULL;
1334 unsetenv ("LISTEN_PID");
1335 unsetenv ("LISTEN_FDS");
1338 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1340 receive_sockets_from_parent (sctx);
1341 putenv ("GNUNET_OS_READ_LSOCKS=");
1345 if ((NULL == sctx->lsocks) &&
1347 GNUNET_SERVICE_get_server_addresses (sctx->service_name, sctx->cfg,
1348 &sctx->addrs, &sctx->addrlens)))
1349 return GNUNET_SYSERR;
1350 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1352 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1355 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1357 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1358 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1359 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1360 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1367 * Get the name of the user that'll be used
1368 * to provide the service.
1370 * @param sctx service context
1371 * @return value of the 'USERNAME' option
1374 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1379 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
1388 * @param sctx service context
1389 * @param pid PID to write (should be equal to 'getpid()'
1390 * @return GNUNET_OK on success (including no work to be done)
1393 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1401 if (NULL == (pif = get_pid_file_name (sctx)))
1402 return GNUNET_OK; /* no file desired */
1403 user = get_user_name (sctx);
1404 rdir = GNUNET_strdup (pif);
1405 len = strlen (rdir);
1406 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1409 if (0 != ACCESS (rdir, F_OK))
1411 /* we get to create a directory -- and claim it
1413 GNUNET_DISK_directory_create (rdir);
1414 if ((NULL != user) && (0 < strlen (user)))
1415 GNUNET_DISK_file_change_owner (rdir, user);
1417 if (0 != ACCESS (rdir, W_OK | X_OK))
1419 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1421 GNUNET_free_non_null (user);
1423 return GNUNET_SYSERR;
1426 pidfd = FOPEN (pif, "w");
1429 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1431 GNUNET_free_non_null (user);
1432 return GNUNET_SYSERR;
1434 if (0 > FPRINTF (pidfd, "%u", pid))
1435 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1436 GNUNET_break (0 == FCLOSE (pidfd));
1437 if ((NULL != user) && (0 < strlen (user)))
1438 GNUNET_DISK_file_change_owner (pif, user);
1439 GNUNET_free_non_null (user);
1446 * Task run during shutdown. Stops the server/service.
1448 * @param cls the 'struct GNUNET_SERVICE_Context'
1452 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1454 struct GNUNET_SERVICE_Context *service = cls;
1455 struct GNUNET_SERVER_Handle *server = service->server;
1457 if (0 != (service->options & GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN))
1458 GNUNET_SERVER_stop_listening (server);
1460 GNUNET_SERVER_destroy (server);
1465 * Initial task for the service.
1467 * @param cls service context
1471 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1473 struct GNUNET_SERVICE_Context *sctx = cls;
1476 GNUNET_RESOLVER_connect (sctx->cfg);
1477 if (NULL != sctx->lsocks)
1479 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1480 sctx->timeout, sctx->require_found);
1483 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1484 sctx->timeout, sctx->require_found);
1485 if (NULL == sctx->server)
1487 if (NULL != sctx->addrs)
1490 while (NULL != sctx->addrs[i])
1492 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1493 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1497 sctx->ret = GNUNET_SYSERR;
1500 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1502 /* install a task that will kill the server
1503 * process if the scheduler ever gets a shutdown signal */
1504 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1507 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1508 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1510 while (NULL != sctx->my_handlers[i].callback)
1511 sctx->my_handlers[i++].callback_cls = sctx;
1512 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1513 if (-1 != sctx->ready_confirm_fd)
1515 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1516 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1517 sctx->ready_confirm_fd = -1;
1518 write_pid_file (sctx, getpid ());
1520 if (NULL != sctx->addrs)
1523 while (NULL != sctx->addrs[i])
1525 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1526 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1530 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1535 * Detach from terminal.
1537 * @param sctx service context
1538 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1541 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1548 if (0 != PIPE (filedes))
1550 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1551 return GNUNET_SYSERR;
1556 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1557 return GNUNET_SYSERR;
1564 GNUNET_break (0 == CLOSE (filedes[1]));
1566 if (1 != READ (filedes[0], &c, sizeof (char)))
1567 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1574 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1577 LOG (GNUNET_ERROR_TYPE_INFO,
1578 _("Service process could not initialize server function\n"));
1581 LOG (GNUNET_ERROR_TYPE_INFO,
1582 _("Service process failed to report status\n"));
1585 exit (1); /* child reported error */
1587 GNUNET_break (0 == CLOSE (0));
1588 GNUNET_break (0 == CLOSE (1));
1589 GNUNET_break (0 == CLOSE (filedes[0]));
1590 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1592 return GNUNET_SYSERR;
1593 /* set stdin/stdout to /dev/null */
1594 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1596 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1597 (void) CLOSE (nullfd);
1598 return GNUNET_SYSERR;
1600 (void) CLOSE (nullfd);
1601 /* Detach from controlling terminal */
1604 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1605 sctx->ready_confirm_fd = filedes[1];
1607 /* FIXME: we probably need to do something else
1608 * elsewhere in order to fork the process itself... */
1618 * @param sctx service context
1619 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1622 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1626 if (NULL == (user = get_user_name (sctx)))
1627 return GNUNET_OK; /* keep */
1632 pws = getpwnam (user);
1635 LOG (GNUNET_ERROR_TYPE_ERROR,
1636 _("Cannot obtain information about user `%s': %s\n"), user,
1637 errno == 0 ? _("No such user") : STRERROR (errno));
1639 return GNUNET_SYSERR;
1641 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1643 (0 != initgroups (user, pws->pw_gid)) ||
1645 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1647 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1648 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1650 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1651 user, STRERROR (errno));
1653 return GNUNET_SYSERR;
1663 * Delete the PID file that was created by our parent.
1665 * @param sctx service context
1668 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1670 char *pif = get_pid_file_name (sctx);
1673 return; /* no PID file */
1674 if (0 != UNLINK (pif))
1675 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1681 * Run a standard GNUnet service startup sequence (initialize loggers
1682 * and configuration, parse options).
1684 * @param argc number of command line arguments
1685 * @param argv command line arguments
1686 * @param service_name our service name
1687 * @param options service options
1688 * @param task main task of the service
1689 * @param task_cls closure for task
1690 * @return GNUNET_SYSERR on error, GNUNET_OK
1691 * if we shutdown nicely
1694 GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
1695 enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main task,
1698 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1706 unsigned long long skew_offset;
1707 unsigned long long skew_variance;
1708 long long clock_offset;
1709 struct GNUNET_SERVICE_Context sctx;
1710 struct GNUNET_CONFIGURATION_Handle *cfg;
1712 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1713 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1714 {'d', "daemonize", NULL,
1715 gettext_noop ("do daemonize (detach from terminal)"), 0,
1716 GNUNET_GETOPT_set_one, &do_daemonize},
1717 GNUNET_GETOPT_OPTION_HELP (NULL),
1718 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1719 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1720 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1721 GNUNET_GETOPT_OPTION_END
1727 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1728 memset (&sctx, 0, sizeof (sctx));
1729 sctx.options = options;
1730 sctx.ready_confirm_fd = -1;
1731 sctx.ret = GNUNET_OK;
1732 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1734 sctx.task_cls = task_cls;
1735 sctx.service_name = service_name;
1736 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1737 /* setup subsystems */
1738 if (GNUNET_SYSERR ==
1739 GNUNET_GETOPT_run (service_name, service_options, argc, argv))
1741 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
1743 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1745 if (GNUNET_OK != setup_service (&sctx))
1747 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1749 if (GNUNET_OK != set_user_id (&sctx))
1751 LOG (GNUNET_ERROR_TYPE_DEBUG,
1752 "Service `%s' runs with configuration from `%s'\n", service_name, cfg_fn);
1754 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1755 "SKEW_OFFSET", &skew_offset)) &&
1757 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1758 "SKEW_VARIANCE", &skew_variance)))
1760 clock_offset = skew_offset - skew_variance;
1761 GNUNET_TIME_set_offset (clock_offset);
1762 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1764 /* actually run service */
1766 GNUNET_SCHEDULER_run (&service_task, &sctx);
1769 if ((1 == do_daemonize) && (NULL != sctx.server))
1770 pid_file_delete (&sctx);
1771 GNUNET_free_non_null (sctx.my_handlers);
1774 if (-1 != sctx.ready_confirm_fd)
1776 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1777 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1778 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1781 GNUNET_CONFIGURATION_destroy (cfg);
1783 if (NULL != sctx.addrs)
1784 while (NULL != sctx.addrs[i])
1785 GNUNET_free (sctx.addrs[i++]);
1786 GNUNET_free_non_null (sctx.addrs);
1787 GNUNET_free_non_null (sctx.addrlens);
1788 GNUNET_free_non_null (logfile);
1789 GNUNET_free_non_null (loglev);
1790 GNUNET_free (cfg_fn);
1791 GNUNET_free_non_null (sctx.v4_denied);
1792 GNUNET_free_non_null (sctx.v6_denied);
1793 GNUNET_free_non_null (sctx.v4_allowed);
1794 GNUNET_free_non_null (sctx.v6_allowed);
1796 return err ? GNUNET_SYSERR : sctx.ret;
1801 * Run a service startup sequence within an existing
1802 * initialized system.
1804 * @param service_name our service name
1805 * @param cfg configuration to use
1806 * @param options service options
1807 * @return NULL on error, service handle
1809 struct GNUNET_SERVICE_Context *
1810 GNUNET_SERVICE_start (const char *service_name,
1811 const struct GNUNET_CONFIGURATION_Handle *cfg,
1812 enum GNUNET_SERVICE_Options options)
1815 struct GNUNET_SERVICE_Context *sctx;
1817 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1818 sctx->ready_confirm_fd = -1; /* no daemonizing */
1819 sctx->ret = GNUNET_OK;
1820 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1821 sctx->service_name = service_name;
1823 sctx->options = options;
1825 /* setup subsystems */
1826 if (GNUNET_OK != setup_service (sctx))
1828 GNUNET_SERVICE_stop (sctx);
1831 if (NULL != sctx->lsocks)
1833 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1834 sctx->timeout, sctx->require_found);
1837 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1838 sctx->timeout, sctx->require_found);
1840 if (NULL == sctx->server)
1842 GNUNET_SERVICE_stop (sctx);
1845 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1846 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1848 while ((sctx->my_handlers[i].callback != NULL))
1849 sctx->my_handlers[i++].callback_cls = sctx;
1850 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1856 * Obtain the server used by a service. Note that the server must NOT
1857 * be destroyed by the caller.
1859 * @param ctx the service context returned from the start function
1860 * @return handle to the server for this service, NULL if there is none
1862 struct GNUNET_SERVER_Handle *
1863 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1870 * Stop a service that was started with "GNUNET_SERVICE_start".
1872 * @param sctx the service context returned from the start function
1875 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1879 if (NULL != sctx->server)
1880 GNUNET_SERVER_destroy (sctx->server);
1881 GNUNET_free_non_null (sctx->my_handlers);
1882 if (NULL != sctx->addrs)
1885 while (NULL != sctx->addrs[i])
1886 GNUNET_free (sctx->addrs[i++]);
1887 GNUNET_free (sctx->addrs);
1889 GNUNET_free_non_null (sctx->addrlens);
1890 GNUNET_free_non_null (sctx->v4_denied);
1891 GNUNET_free_non_null (sctx->v6_denied);
1892 GNUNET_free_non_null (sctx->v4_allowed);
1893 GNUNET_free_non_null (sctx->v6_allowed);
1898 /* end of service.c */