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 * Task ID of the shutdown task.
492 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
495 * Idle timeout for server.
497 struct GNUNET_TIME_Relative timeout;
500 * Overall success/failure of the service start.
505 * If we are daemonizing, this FD is set to the
506 * pipe to the parent. Send '.' if we started
507 * ok, '!' if not. -1 if we are not daemonizing.
509 int ready_confirm_fd;
512 * Do we close connections if we receive messages
513 * for which we have no handler?
518 * Do we require a matching UID for UNIX domain socket connections?
519 * GNUNET_NO means that the UID does not have to match (however,
520 * "match_gid" may still impose other access control checks).
525 * Do we require a matching GID for UNIX domain socket connections?
526 * Ignored if "match_uid" is GNUNET_YES. Note that this is about
527 * checking that the client's UID is in our group OR that the
528 * client's GID is our GID. If both "match_gid" and "match_uid" are
529 * "GNUNET_NO", all users on the local system have access.
536 enum GNUNET_SERVICE_Options options;
541 /* ****************** message handlers ****************** */
544 * Send a 'TEST' message back to the client.
546 * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
547 * @param size number of bytes available in 'buf'
548 * @param buf where to copy the message
549 * @return number of bytes written to 'buf'
552 write_test (void *cls, size_t size, void *buf)
554 struct GNUNET_SERVER_Client *client = cls;
555 struct GNUNET_MessageHeader *msg;
557 if (size < sizeof (struct GNUNET_MessageHeader))
559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
560 return 0; /* client disconnected */
562 msg = (struct GNUNET_MessageHeader *) buf;
563 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
564 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
565 GNUNET_SERVER_receive_done (client, GNUNET_OK);
566 return sizeof (struct GNUNET_MessageHeader);
571 * Handler for TEST message.
573 * @param cls closure (refers to service)
574 * @param client identification of the client
575 * @param message the actual message
578 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
579 const struct GNUNET_MessageHeader *message)
581 /* simply bounce message back to acknowledge */
583 GNUNET_SERVER_notify_transmit_ready (client,
584 sizeof (struct GNUNET_MessageHeader),
585 GNUNET_TIME_UNIT_FOREVER_REL,
586 &write_test, client))
587 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
592 * Default handlers for all services. Will be copied and the
593 * "callback_cls" fields will be replaced with the specific service
596 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
597 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
598 sizeof (struct GNUNET_MessageHeader)},
603 /* ****************** service core routines ************** */
607 * Check if access to the service is allowed from the given address.
610 * @param uc credentials, if available, otherwise NULL
611 * @param addr address
612 * @param addrlen length of address
613 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
614 * for unknown address family (will be denied).
617 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
618 const struct sockaddr *addr, socklen_t addrlen)
620 struct GNUNET_SERVICE_Context *sctx = cls;
621 const struct sockaddr_in *i4;
622 const struct sockaddr_in6 *i6;
625 switch (addr->sa_family)
628 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
629 i4 = (const struct sockaddr_in *) addr;
630 ret = ((NULL == sctx->v4_allowed) ||
631 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
632 ((NULL == sctx->v4_denied) ||
633 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
636 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
637 i6 = (const struct sockaddr_in6 *) addr;
638 ret = ((NULL == sctx->v6_allowed) ||
639 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
640 ((NULL == sctx->v6_denied) ||
641 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
645 ret = GNUNET_OK; /* always OK for now */
646 if (GNUNET_YES == sctx->match_uid)
648 /* UID match required */
649 ret = (NULL != uc) && (uc->uid == geteuid ());
651 else if ( (GNUNET_YES == sctx->match_gid) &&
652 ( (NULL == uc) || (uc->uid != geteuid ()) ) )
654 /* group match required and UID does not match */
657 /* no credentials, group match not possible */
665 if (uc->gid != getegid())
667 /* default group did not match, but maybe the user is in our group, let's check */
668 grp = getgrgid (getegid ());
671 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getgrgid");
675 for (i=0; NULL != grp->gr_mem[i]; i++)
677 struct passwd *nam = getpwnam (grp->gr_mem[i]);
679 continue; /* name in group that is not in user DB !? */
680 if (nam->pw_uid == uc->uid)
682 /* yes, uid is in our group, allow! */
690 if (GNUNET_NO == ret)
691 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
692 (NULL == uc) ? -1 : uc->uid, (NULL == uc) ? -1 : uc->gid);
696 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
698 return GNUNET_SYSERR;
700 if (GNUNET_OK != ret)
702 LOG (GNUNET_ERROR_TYPE_WARNING,
703 _("Access from `%s' denied to service `%s'\n"),
704 GNUNET_a2s (addr, addrlen),
712 * Get the name of the file where we will
713 * write the PID of the service.
715 * @param sctx service context
716 * @return name of the file for the process ID
719 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
724 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
732 * Parse an IPv4 access control list.
734 * @param ret location where to write the ACL (set)
735 * @param sctx service context to use to get the configuration
736 * @param option name of the ACL option to parse
737 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
741 process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
746 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
751 GNUNET_break (GNUNET_OK ==
752 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
755 if (NULL == (*ret = parse_ipv4_specification (opt)))
757 LOG (GNUNET_ERROR_TYPE_WARNING,
758 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
759 opt, sctx->service_name, option);
761 return GNUNET_SYSERR;
769 * Parse an IPv6 access control list.
771 * @param ret location where to write the ACL (set)
772 * @param sctx service context to use to get the configuration
773 * @param option name of the ACL option to parse
774 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
778 process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
783 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
788 GNUNET_break (GNUNET_OK ==
789 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
792 if (NULL == (*ret = parse_ipv6_specification (opt)))
794 LOG (GNUNET_ERROR_TYPE_WARNING,
795 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
796 opt, sctx->service_name, option);
798 return GNUNET_SYSERR;
806 * Add the given UNIX domain path as an address to the
807 * list (as the first entry).
809 * @param saddrs array to update
810 * @param saddrlens where to store the address length
811 * @param unixpath path to add
814 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
815 const char *unixpath)
818 struct sockaddr_un *un;
821 un = GNUNET_malloc (sizeof (struct sockaddr_un));
822 un->sun_family = AF_UNIX;
823 slen = strlen (unixpath) + 1;
824 if (slen >= sizeof (un->sun_path))
825 slen = sizeof (un->sun_path) - 1;
826 memcpy (un->sun_path, unixpath, slen);
827 un->sun_path[slen] = '\0';
828 slen = sizeof (struct sockaddr_un);
830 un->sun_path[0] = '\0';
832 #if HAVE_SOCKADDR_IN_SIN_LEN
833 un->sun_len = (u_char) slen;
835 *saddrs = (struct sockaddr *) un;
838 /* this function should never be called
839 * unless AF_UNIX is defined! */
846 * Get the list of addresses that a server for the given service
849 * @param service_name name of the service
850 * @param cfg configuration (which specifies the addresses)
851 * @param addrs set (call by reference) to an array of pointers to the
852 * addresses the server should bind to and listen on; the
853 * array will be NULL-terminated (on success)
854 * @param addr_lens set (call by reference) to an array of the lengths
855 * of the respective 'struct sockaddr' struct in the 'addrs'
857 * @return number of addresses found on success,
858 * GNUNET_SYSERR if the configuration
859 * did not specify reasonable finding information or
860 * if it specified a hostname that could not be resolved;
861 * GNUNET_NO if the number of addresses configured is
862 * zero (in this case, '*addrs' and '*addr_lens' will be
866 GNUNET_SERVICE_get_server_addresses (const char *service_name,
867 const struct GNUNET_CONFIGURATION_Handle
868 *cfg, struct sockaddr ***addrs,
869 socklen_t ** addr_lens)
872 struct GNUNET_NETWORK_Handle *desc;
873 unsigned long long port;
875 struct addrinfo hints;
876 struct addrinfo *res;
877 struct addrinfo *pos;
878 struct addrinfo *next;
882 struct sockaddr **saddrs;
883 socklen_t *saddrlens;
889 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
893 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
894 return GNUNET_SYSERR;
897 disablev6 = GNUNET_NO;
901 /* probe IPv6 support */
902 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
905 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
908 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
909 return GNUNET_SYSERR;
911 LOG (GNUNET_ERROR_TYPE_INFO,
913 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
914 service_name, STRERROR (errno));
915 disablev6 = GNUNET_YES;
919 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
925 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
927 GNUNET_break (GNUNET_OK ==
928 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
932 LOG (GNUNET_ERROR_TYPE_ERROR,
933 _("Require valid port number for service `%s' in configuration!\n"),
935 return GNUNET_SYSERR;
939 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
941 GNUNET_break (GNUNET_OK ==
942 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
943 "BINDTO", &hostname));
951 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
953 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
955 (0 < strlen (unixpath)))
957 /* probe UNIX support */
958 struct sockaddr_un s_un;
960 if (strlen (unixpath) >= sizeof (s_un.sun_path))
962 LOG (GNUNET_ERROR_TYPE_WARNING,
963 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
964 sizeof (s_un.sun_path));
965 GNUNET_free_non_null (hostname);
966 GNUNET_free (unixpath);
967 return GNUNET_SYSERR;
970 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
973 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
976 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
977 GNUNET_free_non_null (hostname);
978 GNUNET_free (unixpath);
979 return GNUNET_SYSERR;
981 LOG (GNUNET_ERROR_TYPE_INFO,
983 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
984 service_name, STRERROR (errno));
985 GNUNET_free (unixpath);
990 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
996 if ((0 == port) && (NULL == unixpath))
998 LOG (GNUNET_ERROR_TYPE_ERROR,
1000 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
1002 GNUNET_free_non_null (hostname);
1003 return GNUNET_SYSERR;
1007 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
1008 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
1009 add_unixpath (saddrs, saddrlens, unixpath);
1010 GNUNET_free_non_null (unixpath);
1011 GNUNET_free_non_null (hostname);
1013 *addr_lens = saddrlens;
1017 if (NULL != hostname)
1019 LOG (GNUNET_ERROR_TYPE_DEBUG,
1020 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
1022 memset (&hints, 0, sizeof (struct addrinfo));
1024 hints.ai_family = AF_INET;
1025 hints.ai_protocol = IPPROTO_TCP;
1026 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1029 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
1030 gai_strerror (ret));
1031 GNUNET_free (hostname);
1032 GNUNET_free_non_null (unixpath);
1033 return GNUNET_SYSERR;
1037 while (NULL != (pos = next))
1039 next = pos->ai_next;
1040 if ((disablev6) && (pos->ai_family == AF_INET6))
1046 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
1047 disablev6 ? "IPv4 " : "", hostname);
1049 GNUNET_free (hostname);
1050 GNUNET_free_non_null (unixpath);
1051 return GNUNET_SYSERR;
1054 if (NULL != unixpath)
1056 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1057 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1059 if (NULL != unixpath)
1061 add_unixpath (saddrs, saddrlens, unixpath);
1065 while (NULL != (pos = next))
1067 next = pos->ai_next;
1068 if ((disablev6) && (AF_INET6 == pos->ai_family))
1070 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1071 continue; /* not TCP */
1072 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1073 continue; /* huh? */
1074 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1075 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1076 if (AF_INET == pos->ai_family)
1078 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
1079 saddrlens[i] = pos->ai_addrlen;
1080 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1081 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1082 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1086 GNUNET_assert (AF_INET6 == pos->ai_family);
1087 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
1088 saddrlens[i] = pos->ai_addrlen;
1089 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1090 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1091 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1095 GNUNET_free (hostname);
1101 /* will bind against everything, just set port */
1106 if (NULL != unixpath)
1109 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1110 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1111 if (NULL != unixpath)
1113 add_unixpath (saddrs, saddrlens, unixpath);
1116 saddrlens[i] = sizeof (struct sockaddr_in);
1117 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1118 #if HAVE_SOCKADDR_IN_SIN_LEN
1119 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1121 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1122 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1128 if (NULL != unixpath)
1130 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1131 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1133 if (NULL != unixpath)
1135 add_unixpath (saddrs, saddrlens, unixpath);
1138 saddrlens[i] = sizeof (struct sockaddr_in6);
1139 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1140 #if HAVE_SOCKADDR_IN_SIN_LEN
1141 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1143 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1144 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1146 saddrlens[i] = sizeof (struct sockaddr_in);
1147 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1148 #if HAVE_SOCKADDR_IN_SIN_LEN
1149 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1151 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1152 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1155 GNUNET_free_non_null (unixpath);
1157 *addr_lens = saddrlens;
1164 * Read listen sockets from the parent process (ARM).
1166 * @param sctx service context to initialize
1167 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1168 * and GNUNET_SYSERR on error.
1171 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1173 const char *env_buf;
1179 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1180 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
1182 /* Using W32 API directly here, because this pipe will
1183 * never be used outside of this function, and it's just too much of a bother
1184 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1186 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1187 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
1196 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1197 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
1200 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
1203 for (i = 0; i < count; i++)
1205 WSAPROTOCOL_INFOA pi;
1209 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1210 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
1212 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1213 if ( (0 == ret) || (sizeof (pi) != rd))
1215 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1216 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1217 if (NULL == sctx->lsocks[i])
1219 else if (i == count - 1)
1224 sctx->lsocks[count] = NULL;
1229 CloseHandle (lsocks_pipe);
1233 LOG (GNUNET_ERROR_TYPE_ERROR,
1234 _("Could not access a pre-bound socket, will try to bind myself\n"));
1235 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
1236 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1237 GNUNET_free_non_null (sctx->lsocks);
1238 sctx->lsocks = NULL;
1247 * Setup addr, addrlen, idle_timeout
1248 * based on configuration!
1250 * Configuration may specify:
1251 * - PORT (where to bind to for TCP)
1252 * - UNIXPATH (where to bind to for UNIX domain sockets)
1253 * - TIMEOUT (after how many ms does an inactive service timeout);
1254 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1255 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1256 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1257 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1258 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1259 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1261 * @param sctx service context to initialize
1262 * @return GNUNET_OK if configuration succeeded
1265 setup_service (struct GNUNET_SERVICE_Context *sctx)
1267 struct GNUNET_TIME_Relative idleout;
1278 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, "TIMEOUT"))
1281 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->service_name,
1282 "TIMEOUT", &idleout))
1284 LOG (GNUNET_ERROR_TYPE_ERROR,
1285 _("Specified value for `%s' of service `%s' is invalid\n"),
1286 "TIMEOUT", sctx->service_name);
1287 return GNUNET_SYSERR;
1289 sctx->timeout = idleout;
1292 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1294 if (GNUNET_CONFIGURATION_have_value
1295 (sctx->cfg, sctx->service_name, "TOLERANT"))
1297 if (GNUNET_SYSERR ==
1299 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1302 LOG (GNUNET_ERROR_TYPE_ERROR,
1303 _("Specified value for `%s' of service `%s' is invalid\n"),
1304 "TOLERANT", sctx->service_name);
1305 return GNUNET_SYSERR;
1309 tolerant = GNUNET_NO;
1313 if ((NULL != (lpid = getenv ("LISTEN_PID"))) &&
1314 (1 == SSCANF (lpid, "%u", &pid)) && (getpid () == (pid_t) pid) &&
1315 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1316 (1 == SSCANF (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1317 (cnt + 4 < FD_SETSIZE))
1320 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1323 flags = fcntl (3 + cnt, F_GETFD);
1324 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1326 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1328 LOG (GNUNET_ERROR_TYPE_ERROR,
1330 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1331 (unsigned int) 3 + cnt);
1333 while (sctx->lsocks[cnt] != NULL)
1334 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1335 GNUNET_free (sctx->lsocks);
1336 sctx->lsocks = NULL;
1340 unsetenv ("LISTEN_PID");
1341 unsetenv ("LISTEN_FDS");
1344 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1346 receive_sockets_from_parent (sctx);
1347 putenv ("GNUNET_OS_READ_LSOCKS=");
1351 if ((NULL == sctx->lsocks) &&
1353 GNUNET_SERVICE_get_server_addresses (sctx->service_name, sctx->cfg,
1354 &sctx->addrs, &sctx->addrlens)))
1355 return GNUNET_SYSERR;
1356 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1358 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1361 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1363 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1364 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1365 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1366 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1373 * Get the name of the user that'll be used
1374 * to provide the service.
1376 * @param sctx service context
1377 * @return value of the 'USERNAME' option
1380 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1385 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
1394 * @param sctx service context
1395 * @param pid PID to write (should be equal to 'getpid()'
1396 * @return GNUNET_OK on success (including no work to be done)
1399 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1407 if (NULL == (pif = get_pid_file_name (sctx)))
1408 return GNUNET_OK; /* no file desired */
1409 user = get_user_name (sctx);
1410 rdir = GNUNET_strdup (pif);
1411 len = strlen (rdir);
1412 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1415 if (0 != ACCESS (rdir, F_OK))
1417 /* we get to create a directory -- and claim it
1419 GNUNET_DISK_directory_create (rdir);
1420 if ((NULL != user) && (0 < strlen (user)))
1421 GNUNET_DISK_file_change_owner (rdir, user);
1423 if (0 != ACCESS (rdir, W_OK | X_OK))
1425 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1427 GNUNET_free_non_null (user);
1429 return GNUNET_SYSERR;
1432 pidfd = FOPEN (pif, "w");
1435 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1437 GNUNET_free_non_null (user);
1438 return GNUNET_SYSERR;
1440 if (0 > FPRINTF (pidfd, "%u", pid))
1441 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1442 GNUNET_break (0 == FCLOSE (pidfd));
1443 if ((NULL != user) && (0 < strlen (user)))
1444 GNUNET_DISK_file_change_owner (pif, user);
1445 GNUNET_free_non_null (user);
1452 * Task run during shutdown. Stops the server/service.
1454 * @param cls the 'struct GNUNET_SERVICE_Context'
1458 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1460 struct GNUNET_SERVICE_Context *service = cls;
1461 struct GNUNET_SERVER_Handle *server = service->server;
1463 service->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1464 if (0 != (service->options & GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN))
1465 GNUNET_SERVER_stop_listening (server);
1467 GNUNET_SERVER_destroy (server);
1472 * Initial task for the service.
1474 * @param cls service context
1478 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1480 struct GNUNET_SERVICE_Context *sctx = cls;
1483 GNUNET_RESOLVER_connect (sctx->cfg);
1484 if (NULL != sctx->lsocks)
1486 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1487 sctx->timeout, sctx->require_found);
1490 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1491 sctx->timeout, sctx->require_found);
1492 if (NULL == sctx->server)
1494 if (NULL != sctx->addrs)
1497 while (NULL != sctx->addrs[i])
1499 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1500 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1504 sctx->ret = GNUNET_SYSERR;
1507 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1509 /* install a task that will kill the server
1510 * process if the scheduler ever gets a shutdown signal */
1511 sctx->shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1514 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1515 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1517 while (NULL != sctx->my_handlers[i].callback)
1518 sctx->my_handlers[i++].callback_cls = sctx;
1519 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1520 if (-1 != sctx->ready_confirm_fd)
1522 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1523 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1524 sctx->ready_confirm_fd = -1;
1525 write_pid_file (sctx, getpid ());
1527 if (NULL != sctx->addrs)
1530 while (NULL != sctx->addrs[i])
1532 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1533 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1537 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1542 * Detach from terminal.
1544 * @param sctx service context
1545 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1548 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1555 if (0 != PIPE (filedes))
1557 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1558 return GNUNET_SYSERR;
1563 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1564 return GNUNET_SYSERR;
1571 GNUNET_break (0 == CLOSE (filedes[1]));
1573 if (1 != READ (filedes[0], &c, sizeof (char)))
1574 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1581 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1584 LOG (GNUNET_ERROR_TYPE_INFO,
1585 _("Service process could not initialize server function\n"));
1588 LOG (GNUNET_ERROR_TYPE_INFO,
1589 _("Service process failed to report status\n"));
1592 exit (1); /* child reported error */
1594 GNUNET_break (0 == CLOSE (0));
1595 GNUNET_break (0 == CLOSE (1));
1596 GNUNET_break (0 == CLOSE (filedes[0]));
1597 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1599 return GNUNET_SYSERR;
1600 /* set stdin/stdout to /dev/null */
1601 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1603 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1604 (void) CLOSE (nullfd);
1605 return GNUNET_SYSERR;
1607 (void) CLOSE (nullfd);
1608 /* Detach from controlling terminal */
1611 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1612 sctx->ready_confirm_fd = filedes[1];
1614 /* FIXME: we probably need to do something else
1615 * elsewhere in order to fork the process itself... */
1625 * @param sctx service context
1626 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1629 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1633 if (NULL == (user = get_user_name (sctx)))
1634 return GNUNET_OK; /* keep */
1639 pws = getpwnam (user);
1642 LOG (GNUNET_ERROR_TYPE_ERROR,
1643 _("Cannot obtain information about user `%s': %s\n"), user,
1644 errno == 0 ? _("No such user") : STRERROR (errno));
1646 return GNUNET_SYSERR;
1648 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1650 (0 != initgroups (user, pws->pw_gid)) ||
1652 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1654 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1655 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1657 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1658 user, STRERROR (errno));
1660 return GNUNET_SYSERR;
1670 * Delete the PID file that was created by our parent.
1672 * @param sctx service context
1675 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1677 char *pif = get_pid_file_name (sctx);
1680 return; /* no PID file */
1681 if (0 != UNLINK (pif))
1682 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1688 * Run a standard GNUnet service startup sequence (initialize loggers
1689 * and configuration, parse options).
1691 * @param argc number of command line arguments
1692 * @param argv command line arguments
1693 * @param service_name our service name
1694 * @param options service options
1695 * @param task main task of the service
1696 * @param task_cls closure for task
1697 * @return GNUNET_SYSERR on error, GNUNET_OK
1698 * if we shutdown nicely
1701 GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
1702 enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main task,
1705 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1713 unsigned long long skew_offset;
1714 unsigned long long skew_variance;
1715 long long clock_offset;
1716 struct GNUNET_SERVICE_Context sctx;
1717 struct GNUNET_CONFIGURATION_Handle *cfg;
1719 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1720 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1721 {'d', "daemonize", NULL,
1722 gettext_noop ("do daemonize (detach from terminal)"), 0,
1723 GNUNET_GETOPT_set_one, &do_daemonize},
1724 GNUNET_GETOPT_OPTION_HELP (NULL),
1725 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1726 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1727 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1728 GNUNET_GETOPT_OPTION_END
1734 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1735 memset (&sctx, 0, sizeof (sctx));
1736 sctx.options = options;
1737 sctx.ready_confirm_fd = -1;
1738 sctx.ret = GNUNET_OK;
1739 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1741 sctx.task_cls = task_cls;
1742 sctx.service_name = service_name;
1743 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1744 /* setup subsystems */
1745 if (GNUNET_SYSERR ==
1746 GNUNET_GETOPT_run (service_name, service_options, argc, argv))
1748 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
1750 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1752 if (GNUNET_OK != setup_service (&sctx))
1754 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1756 if (GNUNET_OK != set_user_id (&sctx))
1758 LOG (GNUNET_ERROR_TYPE_DEBUG,
1759 "Service `%s' runs with configuration from `%s'\n", service_name, cfg_fn);
1761 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1762 "SKEW_OFFSET", &skew_offset)) &&
1764 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1765 "SKEW_VARIANCE", &skew_variance)))
1767 clock_offset = skew_offset - skew_variance;
1768 GNUNET_TIME_set_offset (clock_offset);
1769 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1771 /* actually run service */
1773 GNUNET_SCHEDULER_run (&service_task, &sctx);
1776 if ((1 == do_daemonize) && (NULL != sctx.server))
1777 pid_file_delete (&sctx);
1778 GNUNET_free_non_null (sctx.my_handlers);
1781 if (-1 != sctx.ready_confirm_fd)
1783 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1784 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1785 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1788 GNUNET_CONFIGURATION_destroy (cfg);
1790 if (NULL != sctx.addrs)
1791 while (NULL != sctx.addrs[i])
1792 GNUNET_free (sctx.addrs[i++]);
1793 GNUNET_free_non_null (sctx.addrs);
1794 GNUNET_free_non_null (sctx.addrlens);
1795 GNUNET_free_non_null (logfile);
1796 GNUNET_free_non_null (loglev);
1797 GNUNET_free (cfg_fn);
1798 GNUNET_free_non_null (sctx.v4_denied);
1799 GNUNET_free_non_null (sctx.v6_denied);
1800 GNUNET_free_non_null (sctx.v4_allowed);
1801 GNUNET_free_non_null (sctx.v6_allowed);
1803 return err ? GNUNET_SYSERR : sctx.ret;
1808 * Run a service startup sequence within an existing
1809 * initialized system.
1811 * @param service_name our service name
1812 * @param cfg configuration to use
1813 * @param options service options
1814 * @return NULL on error, service handle
1816 struct GNUNET_SERVICE_Context *
1817 GNUNET_SERVICE_start (const char *service_name,
1818 const struct GNUNET_CONFIGURATION_Handle *cfg,
1819 enum GNUNET_SERVICE_Options options)
1822 struct GNUNET_SERVICE_Context *sctx;
1824 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1825 sctx->ready_confirm_fd = -1; /* no daemonizing */
1826 sctx->ret = GNUNET_OK;
1827 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1828 sctx->service_name = service_name;
1830 sctx->options = options;
1832 /* setup subsystems */
1833 if (GNUNET_OK != setup_service (sctx))
1835 GNUNET_SERVICE_stop (sctx);
1838 if (NULL != sctx->lsocks)
1840 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1841 sctx->timeout, sctx->require_found);
1844 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1845 sctx->timeout, sctx->require_found);
1847 if (NULL == sctx->server)
1849 GNUNET_SERVICE_stop (sctx);
1852 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1853 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1855 while ((sctx->my_handlers[i].callback != NULL))
1856 sctx->my_handlers[i++].callback_cls = sctx;
1857 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1863 * Obtain the server used by a service. Note that the server must NOT
1864 * be destroyed by the caller.
1866 * @param ctx the service context returned from the start function
1867 * @return handle to the server for this service, NULL if there is none
1869 struct GNUNET_SERVER_Handle *
1870 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1877 * Stop a service that was started with "GNUNET_SERVICE_start".
1879 * @param sctx the service context returned from the start function
1882 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1886 if (GNUNET_SCHEDULER_NO_TASK != sctx->shutdown_task)
1888 GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
1889 sctx->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1891 if (NULL != sctx->server)
1892 GNUNET_SERVER_destroy (sctx->server);
1893 GNUNET_free_non_null (sctx->my_handlers);
1894 if (NULL != sctx->addrs)
1897 while (NULL != sctx->addrs[i])
1898 GNUNET_free (sctx->addrs[i++]);
1899 GNUNET_free (sctx->addrs);
1901 GNUNET_free_non_null (sctx->addrlens);
1902 GNUNET_free_non_null (sctx->v4_denied);
1903 GNUNET_free_non_null (sctx->v6_denied);
1904 GNUNET_free_non_null (sctx->v4_allowed);
1905 GNUNET_free_non_null (sctx->v6_allowed);
1910 /* end of service.c */