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 GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg);
85 GNUNET_SPEEDUP_stop_ (void);
89 * Parse a network specification. The argument specifies
90 * a list of networks. The format is
91 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
92 * with a semicolon). The network must be given in dotted-decimal
93 * notation. The netmask can be given in CIDR notation (/16) or
94 * in dotted-decimal (/255.255.0.0).
96 * @param routeList a string specifying the forbidden networks
97 * @return the converted list, NULL if the synatx is flawed
99 static struct IPv4NetworkSet *
100 parse_ipv4_specification (const char *routeList)
108 unsigned int temps[8];
110 struct IPv4NetworkSet *result;
112 if (NULL == routeList)
114 len = strlen (routeList);
118 for (i = 0; i < len; i++)
119 if (routeList[i] == ';')
121 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
127 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
128 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
129 &temps[6], &temps[7]);
132 for (j = 0; j < 8; j++)
135 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
137 GNUNET_free (result);
140 result[i].network.s_addr =
141 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
143 result[i].netmask.s_addr =
144 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
146 while (routeList[pos] != ';')
152 /* try second notation */
154 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
155 &temps[2], &temps[3], &slash);
158 for (j = 0; j < 4; j++)
161 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
163 GNUNET_free (result);
166 result[i].network.s_addr =
167 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
169 if ((slash <= 32) && (slash >= 0))
171 result[i].netmask.s_addr = 0;
174 result[i].netmask.s_addr =
175 (result[i].netmask.s_addr >> 1) + 0x80000000;
178 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
179 while (';' != routeList[pos])
187 LOG (GNUNET_ERROR_TYPE_ERROR,
188 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
190 GNUNET_free (result);
191 return NULL; /* error */
194 /* try third notation */
197 SSCANF (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
198 &temps[2], &temps[3]);
201 for (j = 0; j < 4; j++)
204 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
206 GNUNET_free (result);
209 result[i].network.s_addr =
210 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
212 result[i].netmask.s_addr = 0;
215 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
218 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
219 while (routeList[pos] != ';')
225 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
227 GNUNET_free (result);
228 return NULL; /* error */
230 if (pos < strlen (routeList))
232 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
234 GNUNET_free (result);
235 return NULL; /* oops */
237 return result; /* ok */
242 * Parse a network specification. The argument specifies
243 * a list of networks. The format is
244 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
245 * with a semicolon). The network must be given in colon-hex
246 * notation. The netmask must be given in CIDR notation (/16) or
247 * can be omitted to specify a single host.
249 * @param routeListX a string specifying the forbidden networks
250 * @return the converted list, NULL if the synatx is flawed
252 static struct IPv6NetworkSet *
253 parse_ipv6_specification (const char *routeListX)
263 struct IPv6NetworkSet *result;
268 if (NULL == routeListX)
270 len = strlen (routeListX);
273 routeList = GNUNET_strdup (routeListX);
275 for (i = 0; i < len; i++)
276 if (';' == routeList[i])
278 if (';' != routeList[len - 1])
280 LOG (GNUNET_ERROR_TYPE_ERROR,
281 _("Invalid network notation (does not end with ';': `%s')\n"),
283 GNUNET_free (routeList);
287 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
293 while (';' != routeList[pos])
296 while ((slash >= start) && (routeList[slash] != '/'))
300 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
305 routeList[pos] = '\0';
306 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
310 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
313 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"),
314 &routeList[slash + 1]);
318 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
320 GNUNET_free (result);
321 GNUNET_free (routeList);
327 result[i].netmask.s6_addr[off++] = 0xFF;
332 result[i].netmask.s6_addr[off] =
333 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
338 routeList[slash] = '\0';
339 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
343 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"),
344 &routeList[slash + 1]);
346 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
347 GNUNET_free (result);
348 GNUNET_free (routeList);
354 GNUNET_free (routeList);
360 * Check if the given IP address is in the list of IP addresses.
362 * @param list a list of networks
363 * @param add the IP to check (in network byte order)
364 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
367 check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
374 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
376 if ((add->s_addr & list[i].netmask.s_addr) ==
377 (list[i].network.s_addr & list[i].netmask.s_addr))
386 * Check if the given IP address is in the list of IP addresses.
388 * @param list a list of networks
389 * @param ip the IP to check (in network byte order)
390 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
393 check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
397 struct in6_addr zero;
401 memset (&zero, 0, sizeof (struct in6_addr));
404 while (0 != memcmp (&zero, &list[i].network, sizeof (struct in6_addr)))
406 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
407 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
408 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
419 /* ****************** service struct ****************** */
423 * Context for "service_task".
425 struct GNUNET_SERVICE_Context
430 const struct GNUNET_CONFIGURATION_Handle *cfg;
433 * Handle for the server.
435 struct GNUNET_SERVER_Handle *server;
438 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
441 struct sockaddr **addrs;
444 * Name of our service.
446 const char *service_name;
449 * Main service-specific task to run.
451 GNUNET_SERVICE_Main task;
459 * IPv4 addresses that are not allowed to connect.
461 struct IPv4NetworkSet *v4_denied;
464 * IPv6 addresses that are not allowed to connect.
466 struct IPv6NetworkSet *v6_denied;
469 * IPv4 addresses that are allowed to connect (if not
470 * set, all are allowed).
472 struct IPv4NetworkSet *v4_allowed;
475 * IPv6 addresses that are allowed to connect (if not
476 * set, all are allowed).
478 struct IPv6NetworkSet *v6_allowed;
481 * My (default) message handlers. Adjusted copy
484 struct GNUNET_SERVER_MessageHandler *my_handlers;
487 * Array of the lengths of the entries in addrs.
492 * NULL-terminated array of listen sockets we should take over.
494 struct GNUNET_NETWORK_Handle **lsocks;
497 * Task ID of the shutdown task.
499 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
502 * Idle timeout for server.
504 struct GNUNET_TIME_Relative timeout;
507 * Overall success/failure of the service start.
512 * If we are daemonizing, this FD is set to the
513 * pipe to the parent. Send '.' if we started
514 * ok, '!' if not. -1 if we are not daemonizing.
516 int ready_confirm_fd;
519 * Do we close connections if we receive messages
520 * for which we have no handler?
525 * Do we require a matching UID for UNIX domain socket connections?
526 * GNUNET_NO means that the UID does not have to match (however,
527 * "match_gid" may still impose other access control checks).
532 * Do we require a matching GID for UNIX domain socket connections?
533 * Ignored if "match_uid" is GNUNET_YES. Note that this is about
534 * checking that the client's UID is in our group OR that the
535 * client's GID is our GID. If both "match_gid" and "match_uid" are
536 * "GNUNET_NO", all users on the local system have access.
543 enum GNUNET_SERVICE_Options options;
548 /* ****************** message handlers ****************** */
551 * Send a 'TEST' message back to the client.
553 * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
554 * @param size number of bytes available in 'buf'
555 * @param buf where to copy the message
556 * @return number of bytes written to 'buf'
559 write_test (void *cls, size_t size, void *buf)
561 struct GNUNET_SERVER_Client *client = cls;
562 struct GNUNET_MessageHeader *msg;
564 if (size < sizeof (struct GNUNET_MessageHeader))
566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
567 return 0; /* client disconnected */
569 msg = (struct GNUNET_MessageHeader *) buf;
570 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
571 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
572 GNUNET_SERVER_receive_done (client, GNUNET_OK);
573 return sizeof (struct GNUNET_MessageHeader);
578 * Handler for TEST message.
580 * @param cls closure (refers to service)
581 * @param client identification of the client
582 * @param message the actual message
585 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
586 const struct GNUNET_MessageHeader *message)
588 /* simply bounce message back to acknowledge */
590 GNUNET_SERVER_notify_transmit_ready (client,
591 sizeof (struct GNUNET_MessageHeader),
592 GNUNET_TIME_UNIT_FOREVER_REL,
593 &write_test, client))
594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
599 * Default handlers for all services. Will be copied and the
600 * "callback_cls" fields will be replaced with the specific service
603 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
604 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
605 sizeof (struct GNUNET_MessageHeader)},
610 /* ****************** service core routines ************** */
614 * Check if access to the service is allowed from the given address.
617 * @param uc credentials, if available, otherwise NULL
618 * @param addr address
619 * @param addrlen length of address
620 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
621 * for unknown address family (will be denied).
624 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
625 const struct sockaddr *addr, socklen_t addrlen)
627 struct GNUNET_SERVICE_Context *sctx = cls;
628 const struct sockaddr_in *i4;
629 const struct sockaddr_in6 *i6;
632 switch (addr->sa_family)
635 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
636 i4 = (const struct sockaddr_in *) addr;
637 ret = ((NULL == sctx->v4_allowed) ||
638 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
639 ((NULL == sctx->v4_denied) ||
640 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
643 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
644 i6 = (const struct sockaddr_in6 *) addr;
645 ret = ((NULL == sctx->v6_allowed) ||
646 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
647 ((NULL == sctx->v6_denied) ||
648 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
652 ret = GNUNET_OK; /* always OK for now */
653 if (GNUNET_YES == sctx->match_uid)
655 /* UID match required */
656 ret = (NULL != uc) && (uc->uid == geteuid ());
658 else if ( (GNUNET_YES == sctx->match_gid) &&
659 ( (NULL == uc) || (uc->uid != geteuid ()) ) )
661 /* group match required and UID does not match */
664 /* no credentials, group match not possible */
672 if (uc->gid != getegid())
674 /* default group did not match, but maybe the user is in our group, let's check */
675 grp = getgrgid (getegid ());
678 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getgrgid");
682 for (i=0; NULL != grp->gr_mem[i]; i++)
684 struct passwd *nam = getpwnam (grp->gr_mem[i]);
686 continue; /* name in group that is not in user DB !? */
687 if (nam->pw_uid == uc->uid)
689 /* yes, uid is in our group, allow! */
697 if (GNUNET_NO == ret)
698 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
699 (NULL == uc) ? -1 : uc->uid, (NULL == uc) ? -1 : uc->gid);
703 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
705 return GNUNET_SYSERR;
707 if (GNUNET_OK != ret)
709 LOG (GNUNET_ERROR_TYPE_WARNING,
710 _("Access from `%s' denied to service `%s'\n"),
711 GNUNET_a2s (addr, addrlen),
719 * Get the name of the file where we will
720 * write the PID of the service.
722 * @param sctx service context
723 * @return name of the file for the process ID
726 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
731 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
739 * Parse an IPv4 access control list.
741 * @param ret location where to write the ACL (set)
742 * @param sctx service context to use to get the configuration
743 * @param option name of the ACL option to parse
744 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
748 process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
753 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
758 GNUNET_break (GNUNET_OK ==
759 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
762 if (NULL == (*ret = parse_ipv4_specification (opt)))
764 LOG (GNUNET_ERROR_TYPE_WARNING,
765 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
766 opt, sctx->service_name, option);
768 return GNUNET_SYSERR;
776 * Parse an IPv6 access control list.
778 * @param ret location where to write the ACL (set)
779 * @param sctx service context to use to get the configuration
780 * @param option name of the ACL option to parse
781 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
785 process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
790 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
795 GNUNET_break (GNUNET_OK ==
796 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
799 if (NULL == (*ret = parse_ipv6_specification (opt)))
801 LOG (GNUNET_ERROR_TYPE_WARNING,
802 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
803 opt, sctx->service_name, option);
805 return GNUNET_SYSERR;
813 * Add the given UNIX domain path as an address to the
814 * list (as the first entry).
816 * @param saddrs array to update
817 * @param saddrlens where to store the address length
818 * @param unixpath path to add
821 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
822 const char *unixpath)
825 struct sockaddr_un *un;
828 un = GNUNET_malloc (sizeof (struct sockaddr_un));
829 un->sun_family = AF_UNIX;
830 slen = strlen (unixpath) + 1;
831 if (slen >= sizeof (un->sun_path))
832 slen = sizeof (un->sun_path) - 1;
833 memcpy (un->sun_path, unixpath, slen);
834 un->sun_path[slen] = '\0';
835 slen = sizeof (struct sockaddr_un);
837 un->sun_path[0] = '\0';
839 #if HAVE_SOCKADDR_IN_SIN_LEN
840 un->sun_len = (u_char) slen;
842 *saddrs = (struct sockaddr *) un;
845 /* this function should never be called
846 * unless AF_UNIX is defined! */
853 * Get the list of addresses that a server for the given service
856 * @param service_name name of the service
857 * @param cfg configuration (which specifies the addresses)
858 * @param addrs set (call by reference) to an array of pointers to the
859 * addresses the server should bind to and listen on; the
860 * array will be NULL-terminated (on success)
861 * @param addr_lens set (call by reference) to an array of the lengths
862 * of the respective 'struct sockaddr' struct in the 'addrs'
864 * @return number of addresses found on success,
865 * GNUNET_SYSERR if the configuration
866 * did not specify reasonable finding information or
867 * if it specified a hostname that could not be resolved;
868 * GNUNET_NO if the number of addresses configured is
869 * zero (in this case, '*addrs' and '*addr_lens' will be
873 GNUNET_SERVICE_get_server_addresses (const char *service_name,
874 const struct GNUNET_CONFIGURATION_Handle
875 *cfg, struct sockaddr ***addrs,
876 socklen_t ** addr_lens)
879 struct GNUNET_NETWORK_Handle *desc;
880 unsigned long long port;
882 struct addrinfo hints;
883 struct addrinfo *res;
884 struct addrinfo *pos;
885 struct addrinfo *next;
889 struct sockaddr **saddrs;
890 socklen_t *saddrlens;
896 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
900 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
901 return GNUNET_SYSERR;
904 disablev6 = GNUNET_NO;
908 /* probe IPv6 support */
909 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
912 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
915 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
916 return GNUNET_SYSERR;
918 LOG (GNUNET_ERROR_TYPE_INFO,
920 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
921 service_name, STRERROR (errno));
922 disablev6 = GNUNET_YES;
926 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
932 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
935 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
938 LOG (GNUNET_ERROR_TYPE_ERROR,
939 _("Require valid port number for service `%s' in configuration!\n"),
944 LOG (GNUNET_ERROR_TYPE_ERROR,
945 _("Require valid port number for service `%s' in configuration!\n"),
947 return GNUNET_SYSERR;
951 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
953 GNUNET_break (GNUNET_OK ==
954 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
955 "BINDTO", &hostname));
963 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
965 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
967 (0 < strlen (unixpath)))
969 /* probe UNIX support */
970 struct sockaddr_un s_un;
972 if (strlen (unixpath) >= sizeof (s_un.sun_path))
974 LOG (GNUNET_ERROR_TYPE_WARNING,
975 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
976 sizeof (s_un.sun_path));
977 GNUNET_free_non_null (hostname);
978 GNUNET_free (unixpath);
979 return GNUNET_SYSERR;
982 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
985 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
988 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
989 GNUNET_free_non_null (hostname);
990 GNUNET_free (unixpath);
991 return GNUNET_SYSERR;
993 LOG (GNUNET_ERROR_TYPE_INFO,
995 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
996 service_name, STRERROR (errno));
997 GNUNET_free (unixpath);
1002 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1008 if ((0 == port) && (NULL == unixpath))
1010 LOG (GNUNET_ERROR_TYPE_ERROR,
1012 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
1014 GNUNET_free_non_null (hostname);
1015 return GNUNET_SYSERR;
1019 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
1020 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
1021 add_unixpath (saddrs, saddrlens, unixpath);
1022 GNUNET_free_non_null (unixpath);
1023 GNUNET_free_non_null (hostname);
1025 *addr_lens = saddrlens;
1029 if (NULL != hostname)
1031 LOG (GNUNET_ERROR_TYPE_DEBUG,
1032 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
1034 memset (&hints, 0, sizeof (struct addrinfo));
1036 hints.ai_family = AF_INET;
1037 hints.ai_protocol = IPPROTO_TCP;
1038 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1041 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
1042 gai_strerror (ret));
1043 GNUNET_free (hostname);
1044 GNUNET_free_non_null (unixpath);
1045 return GNUNET_SYSERR;
1049 while (NULL != (pos = next))
1051 next = pos->ai_next;
1052 if ((disablev6) && (pos->ai_family == AF_INET6))
1058 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
1059 disablev6 ? "IPv4 " : "", hostname);
1061 GNUNET_free (hostname);
1062 GNUNET_free_non_null (unixpath);
1063 return GNUNET_SYSERR;
1066 if (NULL != unixpath)
1068 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1069 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1071 if (NULL != unixpath)
1073 add_unixpath (saddrs, saddrlens, unixpath);
1077 while (NULL != (pos = next))
1079 next = pos->ai_next;
1080 if ((disablev6) && (AF_INET6 == pos->ai_family))
1082 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1083 continue; /* not TCP */
1084 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1085 continue; /* huh? */
1086 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1087 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1088 if (AF_INET == pos->ai_family)
1090 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
1091 saddrlens[i] = pos->ai_addrlen;
1092 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1093 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1094 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1098 GNUNET_assert (AF_INET6 == pos->ai_family);
1099 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
1100 saddrlens[i] = pos->ai_addrlen;
1101 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1102 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1103 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1107 GNUNET_free (hostname);
1113 /* will bind against everything, just set port */
1118 if (NULL != unixpath)
1121 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1122 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1123 if (NULL != unixpath)
1125 add_unixpath (saddrs, saddrlens, unixpath);
1128 saddrlens[i] = sizeof (struct sockaddr_in);
1129 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1130 #if HAVE_SOCKADDR_IN_SIN_LEN
1131 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1133 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1134 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1140 if (NULL != unixpath)
1142 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1143 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1145 if (NULL != unixpath)
1147 add_unixpath (saddrs, saddrlens, unixpath);
1150 saddrlens[i] = sizeof (struct sockaddr_in6);
1151 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1152 #if HAVE_SOCKADDR_IN_SIN_LEN
1153 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1155 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1156 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1158 saddrlens[i] = sizeof (struct sockaddr_in);
1159 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1160 #if HAVE_SOCKADDR_IN_SIN_LEN
1161 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1163 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1164 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1167 GNUNET_free_non_null (unixpath);
1169 *addr_lens = saddrlens;
1176 * Read listen sockets from the parent process (ARM).
1178 * @param sctx service context to initialize
1179 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1180 * and GNUNET_SYSERR on error.
1183 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1185 const char *env_buf;
1191 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1192 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
1194 /* Using W32 API directly here, because this pipe will
1195 * never be used outside of this function, and it's just too much of a bother
1196 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1198 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1199 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
1208 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1209 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
1212 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
1215 for (i = 0; i < count; i++)
1217 WSAPROTOCOL_INFOA pi;
1221 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1222 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
1224 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1225 if ( (0 == ret) || (sizeof (pi) != rd))
1227 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1228 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1229 if (NULL == sctx->lsocks[i])
1231 else if (i == count - 1)
1236 sctx->lsocks[count] = NULL;
1241 CloseHandle (lsocks_pipe);
1245 LOG (GNUNET_ERROR_TYPE_ERROR,
1246 _("Could not access a pre-bound socket, will try to bind myself\n"));
1247 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
1248 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1249 GNUNET_free_non_null (sctx->lsocks);
1250 sctx->lsocks = NULL;
1259 * Setup addr, addrlen, idle_timeout
1260 * based on configuration!
1262 * Configuration may specify:
1263 * - PORT (where to bind to for TCP)
1264 * - UNIXPATH (where to bind to for UNIX domain sockets)
1265 * - TIMEOUT (after how many ms does an inactive service timeout);
1266 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1267 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1268 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1269 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1270 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1271 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1273 * @param sctx service context to initialize
1274 * @return GNUNET_OK if configuration succeeded
1277 setup_service (struct GNUNET_SERVICE_Context *sctx)
1279 struct GNUNET_TIME_Relative idleout;
1290 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, "TIMEOUT"))
1293 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->service_name,
1294 "TIMEOUT", &idleout))
1296 LOG (GNUNET_ERROR_TYPE_ERROR,
1297 _("Specified value for `%s' of service `%s' is invalid\n"),
1298 "TIMEOUT", sctx->service_name);
1299 return GNUNET_SYSERR;
1301 sctx->timeout = idleout;
1304 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1306 if (GNUNET_CONFIGURATION_have_value
1307 (sctx->cfg, sctx->service_name, "TOLERANT"))
1309 if (GNUNET_SYSERR ==
1311 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1314 LOG (GNUNET_ERROR_TYPE_ERROR,
1315 _("Specified value for `%s' of service `%s' is invalid\n"),
1316 "TOLERANT", sctx->service_name);
1317 return GNUNET_SYSERR;
1321 tolerant = GNUNET_NO;
1325 if ((NULL != (lpid = getenv ("LISTEN_PID"))) &&
1326 (1 == SSCANF (lpid, "%u", &pid)) && (getpid () == (pid_t) pid) &&
1327 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1328 (1 == SSCANF (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1329 (cnt + 4 < FD_SETSIZE))
1332 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1335 flags = fcntl (3 + cnt, F_GETFD);
1336 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1338 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1340 LOG (GNUNET_ERROR_TYPE_ERROR,
1342 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1343 (unsigned int) 3 + cnt);
1345 while (sctx->lsocks[cnt] != NULL)
1346 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1347 GNUNET_free (sctx->lsocks);
1348 sctx->lsocks = NULL;
1352 unsetenv ("LISTEN_PID");
1353 unsetenv ("LISTEN_FDS");
1356 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1358 receive_sockets_from_parent (sctx);
1359 putenv ("GNUNET_OS_READ_LSOCKS=");
1363 if ((NULL == sctx->lsocks) &&
1365 GNUNET_SERVICE_get_server_addresses (sctx->service_name, sctx->cfg,
1366 &sctx->addrs, &sctx->addrlens)))
1367 return GNUNET_SYSERR;
1368 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1370 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1373 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1375 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1376 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1377 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1378 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1385 * Get the name of the user that'll be used
1386 * to provide the service.
1388 * @param sctx service context
1389 * @return value of the 'USERNAME' option
1392 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1397 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
1406 * @param sctx service context
1407 * @param pid PID to write (should be equal to 'getpid()'
1408 * @return GNUNET_OK on success (including no work to be done)
1411 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1419 if (NULL == (pif = get_pid_file_name (sctx)))
1420 return GNUNET_OK; /* no file desired */
1421 user = get_user_name (sctx);
1422 rdir = GNUNET_strdup (pif);
1423 len = strlen (rdir);
1424 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1427 if (0 != ACCESS (rdir, F_OK))
1429 /* we get to create a directory -- and claim it
1431 GNUNET_DISK_directory_create (rdir);
1432 if ((NULL != user) && (0 < strlen (user)))
1433 GNUNET_DISK_file_change_owner (rdir, user);
1435 if (0 != ACCESS (rdir, W_OK | X_OK))
1437 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1439 GNUNET_free_non_null (user);
1441 return GNUNET_SYSERR;
1444 pidfd = FOPEN (pif, "w");
1447 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1449 GNUNET_free_non_null (user);
1450 return GNUNET_SYSERR;
1452 if (0 > FPRINTF (pidfd, "%u", pid))
1453 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1454 GNUNET_break (0 == FCLOSE (pidfd));
1455 if ((NULL != user) && (0 < strlen (user)))
1456 GNUNET_DISK_file_change_owner (pif, user);
1457 GNUNET_free_non_null (user);
1464 * Task run during shutdown. Stops the server/service.
1466 * @param cls the 'struct GNUNET_SERVICE_Context'
1470 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1472 struct GNUNET_SERVICE_Context *service = cls;
1473 struct GNUNET_SERVER_Handle *server = service->server;
1475 service->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1476 if (0 != (service->options & GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN))
1477 GNUNET_SERVER_stop_listening (server);
1479 GNUNET_SERVER_destroy (server);
1484 * Initial task for the service.
1486 * @param cls service context
1490 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1492 struct GNUNET_SERVICE_Context *sctx = cls;
1495 GNUNET_RESOLVER_connect (sctx->cfg);
1496 if (NULL != sctx->lsocks)
1498 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1499 sctx->timeout, sctx->require_found);
1502 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1503 sctx->timeout, sctx->require_found);
1504 if (NULL == sctx->server)
1506 if (NULL != sctx->addrs)
1509 while (NULL != sctx->addrs[i])
1511 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1512 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1516 sctx->ret = GNUNET_SYSERR;
1519 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1521 /* install a task that will kill the server
1522 * process if the scheduler ever gets a shutdown signal */
1523 sctx->shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1526 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1527 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1529 while (NULL != sctx->my_handlers[i].callback)
1530 sctx->my_handlers[i++].callback_cls = sctx;
1531 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1532 if (-1 != sctx->ready_confirm_fd)
1534 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1535 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1536 sctx->ready_confirm_fd = -1;
1537 write_pid_file (sctx, getpid ());
1539 if (NULL != sctx->addrs)
1542 while (NULL != sctx->addrs[i])
1544 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1545 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1549 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1554 * Detach from terminal.
1556 * @param sctx service context
1557 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1560 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1567 if (0 != PIPE (filedes))
1569 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1570 return GNUNET_SYSERR;
1575 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1576 return GNUNET_SYSERR;
1583 GNUNET_break (0 == CLOSE (filedes[1]));
1585 if (1 != READ (filedes[0], &c, sizeof (char)))
1586 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1593 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1596 LOG (GNUNET_ERROR_TYPE_INFO,
1597 _("Service process could not initialize server function\n"));
1600 LOG (GNUNET_ERROR_TYPE_INFO,
1601 _("Service process failed to report status\n"));
1604 exit (1); /* child reported error */
1606 GNUNET_break (0 == CLOSE (0));
1607 GNUNET_break (0 == CLOSE (1));
1608 GNUNET_break (0 == CLOSE (filedes[0]));
1609 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1611 return GNUNET_SYSERR;
1612 /* set stdin/stdout to /dev/null */
1613 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1615 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1616 (void) CLOSE (nullfd);
1617 return GNUNET_SYSERR;
1619 (void) CLOSE (nullfd);
1620 /* Detach from controlling terminal */
1623 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1624 sctx->ready_confirm_fd = filedes[1];
1626 /* FIXME: we probably need to do something else
1627 * elsewhere in order to fork the process itself... */
1637 * @param sctx service context
1638 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1641 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1645 if (NULL == (user = get_user_name (sctx)))
1646 return GNUNET_OK; /* keep */
1651 pws = getpwnam (user);
1654 LOG (GNUNET_ERROR_TYPE_ERROR,
1655 _("Cannot obtain information about user `%s': %s\n"), user,
1656 errno == 0 ? _("No such user") : STRERROR (errno));
1658 return GNUNET_SYSERR;
1660 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1662 (0 != initgroups (user, pws->pw_gid)) ||
1664 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1666 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1667 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1669 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1670 user, STRERROR (errno));
1672 return GNUNET_SYSERR;
1682 * Delete the PID file that was created by our parent.
1684 * @param sctx service context
1687 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1689 char *pif = get_pid_file_name (sctx);
1692 return; /* no PID file */
1693 if (0 != UNLINK (pif))
1694 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1700 * Run a standard GNUnet service startup sequence (initialize loggers
1701 * and configuration, parse options).
1703 * @param argc number of command line arguments
1704 * @param argv command line arguments
1705 * @param service_name our service name
1706 * @param options service options
1707 * @param task main task of the service
1708 * @param task_cls closure for task
1709 * @return GNUNET_SYSERR on error, GNUNET_OK
1710 * if we shutdown nicely
1713 GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
1714 enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main task,
1717 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1725 unsigned long long skew_offset;
1726 unsigned long long skew_variance;
1727 long long clock_offset;
1728 struct GNUNET_SERVICE_Context sctx;
1729 struct GNUNET_CONFIGURATION_Handle *cfg;
1731 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1732 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1733 {'d', "daemonize", NULL,
1734 gettext_noop ("do daemonize (detach from terminal)"), 0,
1735 GNUNET_GETOPT_set_one, &do_daemonize},
1736 GNUNET_GETOPT_OPTION_HELP (NULL),
1737 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1738 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1739 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1740 GNUNET_GETOPT_OPTION_END
1746 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1747 memset (&sctx, 0, sizeof (sctx));
1748 sctx.options = options;
1749 sctx.ready_confirm_fd = -1;
1750 sctx.ret = GNUNET_OK;
1751 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1753 sctx.task_cls = task_cls;
1754 sctx.service_name = service_name;
1755 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1756 /* setup subsystems */
1757 if (GNUNET_SYSERR ==
1758 GNUNET_GETOPT_run (service_name, service_options, argc, argv))
1760 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
1762 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1764 if (GNUNET_OK != setup_service (&sctx))
1766 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1768 if (GNUNET_OK != set_user_id (&sctx))
1770 LOG (GNUNET_ERROR_TYPE_DEBUG,
1771 "Service `%s' runs with configuration from `%s'\n", service_name, cfg_fn);
1773 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1774 "SKEW_OFFSET", &skew_offset)) &&
1776 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1777 "SKEW_VARIANCE", &skew_variance)))
1779 clock_offset = skew_offset - skew_variance;
1780 GNUNET_TIME_set_offset (clock_offset);
1781 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1783 /* actually run service */
1785 GNUNET_SCHEDULER_run (&service_task, &sctx);
1786 GNUNET_SPEEDUP_start_ (cfg);
1788 if ((1 == do_daemonize) && (NULL != sctx.server))
1789 pid_file_delete (&sctx);
1790 GNUNET_free_non_null (sctx.my_handlers);
1793 if (-1 != sctx.ready_confirm_fd)
1795 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1796 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1797 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1800 GNUNET_SPEEDUP_stop_ ();
1801 GNUNET_CONFIGURATION_destroy (cfg);
1803 if (NULL != sctx.addrs)
1804 while (NULL != sctx.addrs[i])
1805 GNUNET_free (sctx.addrs[i++]);
1806 GNUNET_free_non_null (sctx.addrs);
1807 GNUNET_free_non_null (sctx.addrlens);
1808 GNUNET_free_non_null (logfile);
1809 GNUNET_free_non_null (loglev);
1810 GNUNET_free (cfg_fn);
1811 GNUNET_free_non_null (sctx.v4_denied);
1812 GNUNET_free_non_null (sctx.v6_denied);
1813 GNUNET_free_non_null (sctx.v4_allowed);
1814 GNUNET_free_non_null (sctx.v6_allowed);
1816 return err ? GNUNET_SYSERR : sctx.ret;
1821 * Run a service startup sequence within an existing
1822 * initialized system.
1824 * @param service_name our service name
1825 * @param cfg configuration to use
1826 * @param options service options
1827 * @return NULL on error, service handle
1829 struct GNUNET_SERVICE_Context *
1830 GNUNET_SERVICE_start (const char *service_name,
1831 const struct GNUNET_CONFIGURATION_Handle *cfg,
1832 enum GNUNET_SERVICE_Options options)
1835 struct GNUNET_SERVICE_Context *sctx;
1837 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1838 sctx->ready_confirm_fd = -1; /* no daemonizing */
1839 sctx->ret = GNUNET_OK;
1840 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1841 sctx->service_name = service_name;
1843 sctx->options = options;
1845 /* setup subsystems */
1846 if (GNUNET_OK != setup_service (sctx))
1848 GNUNET_SERVICE_stop (sctx);
1851 if (NULL != sctx->lsocks)
1853 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1854 sctx->timeout, sctx->require_found);
1857 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1858 sctx->timeout, sctx->require_found);
1860 if (NULL == sctx->server)
1862 GNUNET_SERVICE_stop (sctx);
1865 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1866 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1868 while ((sctx->my_handlers[i].callback != NULL))
1869 sctx->my_handlers[i++].callback_cls = sctx;
1870 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1876 * Obtain the server used by a service. Note that the server must NOT
1877 * be destroyed by the caller.
1879 * @param ctx the service context returned from the start function
1880 * @return handle to the server for this service, NULL if there is none
1882 struct GNUNET_SERVER_Handle *
1883 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1890 * Stop a service that was started with "GNUNET_SERVICE_start".
1892 * @param sctx the service context returned from the start function
1895 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1899 if (GNUNET_SCHEDULER_NO_TASK != sctx->shutdown_task)
1901 GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
1902 sctx->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1904 if (NULL != sctx->server)
1905 GNUNET_SERVER_destroy (sctx->server);
1906 GNUNET_free_non_null (sctx->my_handlers);
1907 if (NULL != sctx->addrs)
1910 while (NULL != sctx->addrs[i])
1911 GNUNET_free (sctx->addrs[i++]);
1912 GNUNET_free (sctx->addrs);
1914 GNUNET_free_non_null (sctx->addrlens);
1915 GNUNET_free_non_null (sctx->v4_denied);
1916 GNUNET_free_non_null (sctx->v6_denied);
1917 GNUNET_free_non_null (sctx->v4_allowed);
1918 GNUNET_free_non_null (sctx->v6_allowed);
1923 /* end of service.c */