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 *serviceName;
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 ****************** */
546 write_test (void *cls, size_t size, void *buf)
548 struct GNUNET_SERVER_Client *client = cls;
549 struct GNUNET_MessageHeader *msg;
551 if (size < sizeof (struct GNUNET_MessageHeader))
553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
554 return 0; /* client disconnected */
556 msg = (struct GNUNET_MessageHeader *) buf;
557 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
558 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
559 GNUNET_SERVER_receive_done (client, GNUNET_OK);
560 return sizeof (struct GNUNET_MessageHeader);
565 * Handler for TEST message.
567 * @param cls closure (refers to service)
568 * @param client identification of the client
569 * @param message the actual message
572 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
573 const struct GNUNET_MessageHeader *message)
575 /* simply bounce message back to acknowledge */
577 GNUNET_SERVER_notify_transmit_ready (client,
578 sizeof (struct GNUNET_MessageHeader),
579 GNUNET_TIME_UNIT_FOREVER_REL,
580 &write_test, client))
581 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
586 * Default handlers for all services. Will be copied and the
587 * "callback_cls" fields will be replaced with the specific service
590 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
591 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
592 sizeof (struct GNUNET_MessageHeader)},
597 /* ****************** service core routines ************** */
601 * Check if access to the service is allowed from the given address.
604 * @param uc credentials, if available, otherwise NULL
605 * @param addr address
606 * @param addrlen length of address
607 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
608 * for unknown address family (will be denied).
611 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
612 const struct sockaddr *addr, socklen_t addrlen)
614 struct GNUNET_SERVICE_Context *sctx = cls;
615 const struct sockaddr_in *i4;
616 const struct sockaddr_in6 *i6;
619 switch (addr->sa_family)
622 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
623 i4 = (const struct sockaddr_in *) addr;
624 ret = ((NULL == sctx->v4_allowed) ||
625 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
626 ((NULL == sctx->v4_denied) ||
627 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
630 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
631 i6 = (const struct sockaddr_in6 *) addr;
632 ret = ((NULL == sctx->v6_allowed) ||
633 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
634 ((NULL == sctx->v6_denied) ||
635 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
639 ret = GNUNET_OK; /* always OK for now */
640 if (GNUNET_YES == sctx->match_uid)
642 /* UID match required */
643 ret = (NULL != uc) && (uc->uid == geteuid ());
645 else if ( (GNUNET_YES == sctx->match_gid) &&
646 ( (NULL == uc) || (uc->uid != geteuid ()) ) )
648 /* group match required and UID does not match */
651 /* no credentials, group match not possible */
659 if (uc->gid != getegid())
661 /* default group did not match, but maybe the user is in our group, let's check */
662 grp = getgrgid (getegid ());
665 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getgrgid");
669 for (i=0; NULL != grp->gr_mem[i]; i++)
671 struct passwd *nam = getpwnam (grp->gr_mem[i]);
673 continue; /* name in group that is not in user DB !? */
674 if (nam->pw_uid == uc->uid)
676 /* yes, uid is in our group, allow! */
684 if (GNUNET_NO == ret)
685 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
686 (NULL == uc) ? -1 : uc->uid, (NULL == uc) ? -1 : uc->gid);
690 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
692 return GNUNET_SYSERR;
694 if (GNUNET_OK != ret)
696 LOG (GNUNET_ERROR_TYPE_WARNING,
697 _("Access from `%s' denied to service `%s'\n"),
698 GNUNET_a2s (addr, addrlen),
706 * Get the name of the file where we will
707 * write the PID of the service.
709 * @param sctx service context
710 * @return name of the file for the process ID
713 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
718 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
726 * Parse an IPv4 access control list.
734 process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
739 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
741 GNUNET_break (GNUNET_OK ==
742 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
745 if (NULL == (*ret = parse_ipv4_specification (opt)))
747 LOG (GNUNET_ERROR_TYPE_WARNING,
748 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
749 opt, sctx->serviceName, option);
751 return GNUNET_SYSERR;
759 * Parse an IPv6 access control list.
767 process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
772 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
774 GNUNET_break (GNUNET_OK ==
775 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
778 if (NULL == (*ret = parse_ipv6_specification (opt)))
780 LOG (GNUNET_ERROR_TYPE_WARNING,
781 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
782 opt, sctx->serviceName, option);
784 return GNUNET_SYSERR;
792 * Add the given UNIX domain path as an address to the
793 * list (as the first entry).
795 * @param saddrs array to update
796 * @param saddrlens where to store the address length
797 * @param unixpath path to add
800 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
801 const char *unixpath)
804 struct sockaddr_un *un;
807 un = GNUNET_malloc (sizeof (struct sockaddr_un));
808 un->sun_family = AF_UNIX;
809 slen = strlen (unixpath) + 1;
810 if (slen >= sizeof (un->sun_path))
811 slen = sizeof (un->sun_path) - 1;
812 memcpy (un->sun_path, unixpath, slen);
813 un->sun_path[slen] = '\0';
814 slen = sizeof (struct sockaddr_un);
816 un->sun_path[0] = '\0';
818 #if HAVE_SOCKADDR_IN_SIN_LEN
819 un->sun_len = (u_char) slen;
821 *saddrs = (struct sockaddr *) un;
824 /* this function should never be called
825 * unless AF_UNIX is defined! */
832 * Get the list of addresses that a server for the given service
835 * @param serviceName name of the service
836 * @param cfg configuration (which specifies the addresses)
837 * @param addrs set (call by reference) to an array of pointers to the
838 * addresses the server should bind to and listen on; the
839 * array will be NULL-terminated (on success)
840 * @param addr_lens set (call by reference) to an array of the lengths
841 * of the respective 'struct sockaddr' struct in the 'addrs'
843 * @return number of addresses found on success,
844 * GNUNET_SYSERR if the configuration
845 * did not specify reasonable finding information or
846 * if it specified a hostname that could not be resolved;
847 * GNUNET_NO if the number of addresses configured is
848 * zero (in this case, '*addrs' and '*addr_lens' will be
852 GNUNET_SERVICE_get_server_addresses (const char *serviceName,
853 const struct GNUNET_CONFIGURATION_Handle
854 *cfg, struct sockaddr ***addrs,
855 socklen_t ** addr_lens)
858 struct GNUNET_NETWORK_Handle *desc;
859 unsigned long long port;
861 struct addrinfo hints;
862 struct addrinfo *res;
863 struct addrinfo *pos;
864 struct addrinfo *next;
868 struct sockaddr **saddrs;
869 socklen_t *saddrlens;
875 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "DISABLEV6"))
879 GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName, "DISABLEV6")))
880 return GNUNET_SYSERR;
883 disablev6 = GNUNET_NO;
887 /* probe IPv6 support */
888 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
891 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
894 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
895 return GNUNET_SYSERR;
897 LOG (GNUNET_ERROR_TYPE_INFO,
899 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
900 serviceName, STRERROR (errno));
901 disablev6 = GNUNET_YES;
905 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
911 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
913 GNUNET_break (GNUNET_OK ==
914 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
918 LOG (GNUNET_ERROR_TYPE_ERROR,
919 _("Require valid port number for service `%s' in configuration!\n"),
921 return GNUNET_SYSERR;
925 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
927 GNUNET_break (GNUNET_OK ==
928 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
929 "BINDTO", &hostname));
937 GNUNET_CONFIGURATION_have_value (cfg, serviceName, "UNIXPATH")) &&
939 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, "UNIXPATH",
941 (0 < strlen (unixpath)))
943 /* probe UNIX support */
944 struct sockaddr_un s_un;
946 if (strlen (unixpath) >= sizeof (s_un.sun_path))
948 LOG (GNUNET_ERROR_TYPE_WARNING,
949 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
950 sizeof (s_un.sun_path));
951 GNUNET_free_non_null (hostname);
952 GNUNET_free (unixpath);
953 return GNUNET_SYSERR;
956 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
959 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
962 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
963 GNUNET_free_non_null (hostname);
964 GNUNET_free (unixpath);
965 return GNUNET_SYSERR;
967 LOG (GNUNET_ERROR_TYPE_INFO,
969 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
970 serviceName, STRERROR (errno));
971 GNUNET_free (unixpath);
976 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
982 if ((0 == port) && (NULL == unixpath))
984 LOG (GNUNET_ERROR_TYPE_ERROR,
986 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
988 GNUNET_free_non_null (hostname);
989 return GNUNET_SYSERR;
993 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
994 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
995 add_unixpath (saddrs, saddrlens, unixpath);
996 GNUNET_free_non_null (unixpath);
997 GNUNET_free_non_null (hostname);
999 *addr_lens = saddrlens;
1003 if (NULL != hostname)
1005 LOG (GNUNET_ERROR_TYPE_DEBUG,
1006 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
1008 memset (&hints, 0, sizeof (struct addrinfo));
1010 hints.ai_family = AF_INET;
1011 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1014 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
1015 gai_strerror (ret));
1016 GNUNET_free (hostname);
1017 GNUNET_free_non_null (unixpath);
1018 return GNUNET_SYSERR;
1022 while (NULL != (pos = next))
1024 next = pos->ai_next;
1025 if ((disablev6) && (pos->ai_family == AF_INET6))
1031 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
1032 disablev6 ? "IPv4 " : "", hostname);
1034 GNUNET_free (hostname);
1035 GNUNET_free_non_null (unixpath);
1036 return GNUNET_SYSERR;
1039 if (NULL != unixpath)
1041 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1042 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1044 if (NULL != unixpath)
1046 add_unixpath (saddrs, saddrlens, unixpath);
1050 while (NULL != (pos = next))
1052 next = pos->ai_next;
1053 if ((disablev6) && (AF_INET6 == pos->ai_family))
1055 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1056 continue; /* not TCP */
1057 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1058 continue; /* huh? */
1059 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1060 serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1061 if (AF_INET == pos->ai_family)
1063 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
1064 saddrlens[i] = pos->ai_addrlen;
1065 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1066 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1067 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1071 GNUNET_assert (AF_INET6 == pos->ai_family);
1072 GNUNET_assert (sizeof (struct sockaddr_in6) == 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_in6 *) saddrs[i])->sin6_port = htons (port);
1080 GNUNET_free (hostname);
1086 /* will bind against everything, just set port */
1091 if (NULL != unixpath)
1094 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1095 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1096 if (NULL != unixpath)
1098 add_unixpath (saddrs, saddrlens, unixpath);
1101 saddrlens[i] = sizeof (struct sockaddr_in);
1102 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1103 #if HAVE_SOCKADDR_IN_SIN_LEN
1104 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1106 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1107 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1113 if (NULL != unixpath)
1115 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1116 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1118 if (NULL != unixpath)
1120 add_unixpath (saddrs, saddrlens, unixpath);
1123 saddrlens[i] = sizeof (struct sockaddr_in6);
1124 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1125 #if HAVE_SOCKADDR_IN_SIN_LEN
1126 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1128 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1129 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1131 saddrlens[i] = sizeof (struct sockaddr_in);
1132 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1133 #if HAVE_SOCKADDR_IN_SIN_LEN
1134 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1136 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1137 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1140 GNUNET_free_non_null (unixpath);
1142 *addr_lens = saddrlens;
1152 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1153 * and GNUNET_SYSERR on error.
1156 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1158 const char *env_buf;
1164 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1165 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
1167 /* Using W32 API directly here, because this pipe will
1168 * never be used outside of this function, and it's just too much of a bother
1169 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1171 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1172 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
1181 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1182 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
1185 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
1188 for (i = 0; i < count; i++)
1190 WSAPROTOCOL_INFOA pi;
1194 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1195 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
1197 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1198 if ( (0 == ret) || (sizeof (pi) != rd))
1200 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1201 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1202 if (NULL == sctx->lsocks[i])
1204 else if (i == count - 1)
1209 sctx->lsocks[count] = NULL;
1214 CloseHandle (lsocks_pipe);
1218 LOG (GNUNET_ERROR_TYPE_ERROR,
1219 _("Could not access a pre-bound socket, will try to bind myself\n"));
1220 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
1221 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1222 GNUNET_free_non_null (sctx->lsocks);
1223 sctx->lsocks = NULL;
1232 * Setup addr, addrlen, idle_timeout
1233 * based on configuration!
1235 * Configuration may specify:
1236 * - PORT (where to bind to for TCP)
1237 * - UNIXPATH (where to bind to for UNIX domain sockets)
1238 * - TIMEOUT (after how many ms does an inactive service timeout);
1239 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1240 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1241 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1242 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1243 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1244 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1247 * @return GNUNET_OK if configuration succeeded
1250 setup_service (struct GNUNET_SERVICE_Context *sctx)
1252 struct GNUNET_TIME_Relative idleout;
1263 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, "TIMEOUT"))
1266 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName,
1267 "TIMEOUT", &idleout))
1269 LOG (GNUNET_ERROR_TYPE_ERROR,
1270 _("Specified value for `%s' of service `%s' is invalid\n"),
1271 "TIMEOUT", sctx->serviceName);
1272 return GNUNET_SYSERR;
1274 sctx->timeout = idleout;
1277 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1279 if (GNUNET_CONFIGURATION_have_value
1280 (sctx->cfg, sctx->serviceName, "TOLERANT"))
1282 if (GNUNET_SYSERR ==
1284 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1287 LOG (GNUNET_ERROR_TYPE_ERROR,
1288 _("Specified value for `%s' of service `%s' is invalid\n"),
1289 "TOLERANT", sctx->serviceName);
1290 return GNUNET_SYSERR;
1294 tolerant = GNUNET_NO;
1298 if ((NULL != (lpid = getenv ("LISTEN_PID"))) &&
1299 (1 == SSCANF (lpid, "%u", &pid)) && (getpid () == (pid_t) pid) &&
1300 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1301 (1 == SSCANF (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1302 (cnt + 4 < FD_SETSIZE))
1305 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1308 flags = fcntl (3 + cnt, F_GETFD);
1309 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1311 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1313 LOG (GNUNET_ERROR_TYPE_ERROR,
1315 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1316 (unsigned int) 3 + cnt);
1318 while (sctx->lsocks[cnt] != NULL)
1319 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1320 GNUNET_free (sctx->lsocks);
1321 sctx->lsocks = NULL;
1325 unsetenv ("LISTEN_PID");
1326 unsetenv ("LISTEN_FDS");
1329 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1331 receive_sockets_from_parent (sctx);
1332 putenv ("GNUNET_OS_READ_LSOCKS=");
1336 if ((NULL == sctx->lsocks) &&
1338 GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg,
1339 &sctx->addrs, &sctx->addrlens)))
1340 return GNUNET_SYSERR;
1341 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1343 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1346 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1348 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1349 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1350 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1351 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1358 * Get the name of the user that'll be used
1359 * to provide the service.
1365 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1370 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
1384 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1392 if (NULL == (pif = get_pid_file_name (sctx)))
1393 return GNUNET_OK; /* no file desired */
1394 user = get_user_name (sctx);
1395 rdir = GNUNET_strdup (pif);
1396 len = strlen (rdir);
1397 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1400 if (0 != ACCESS (rdir, F_OK))
1402 /* we get to create a directory -- and claim it
1404 GNUNET_DISK_directory_create (rdir);
1405 if ((NULL != user) && (0 < strlen (user)))
1406 GNUNET_DISK_file_change_owner (rdir, user);
1408 if (0 != ACCESS (rdir, W_OK | X_OK))
1410 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1412 GNUNET_free_non_null (user);
1414 return GNUNET_SYSERR;
1417 pidfd = FOPEN (pif, "w");
1420 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1422 GNUNET_free_non_null (user);
1423 return GNUNET_SYSERR;
1425 if (0 > FPRINTF (pidfd, "%u", pid))
1426 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1427 GNUNET_break (0 == FCLOSE (pidfd));
1428 if ((NULL != user) && (0 < strlen (user)))
1429 GNUNET_DISK_file_change_owner (pif, user);
1430 GNUNET_free_non_null (user);
1437 * Task run during shutdown.
1443 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1445 struct GNUNET_SERVER_Handle *server = cls;
1447 // FIXME: we should not unconditionally destroy the server
1448 // here (often only stopping 'listening' would be better)
1449 GNUNET_SERVER_destroy (server);
1454 * Initial task for the service.
1456 * @param cls service context
1460 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1462 struct GNUNET_SERVICE_Context *sctx = cls;
1465 GNUNET_RESOLVER_connect (sctx->cfg);
1466 if (NULL != sctx->lsocks)
1468 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1469 sctx->timeout, sctx->require_found);
1472 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1473 sctx->timeout, sctx->require_found);
1474 if (NULL == sctx->server)
1476 if (NULL != sctx->addrs)
1479 while (NULL != sctx->addrs[i])
1481 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1482 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1486 sctx->ret = GNUNET_SYSERR;
1489 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1491 /* install a task that will kill the server
1492 * process if the scheduler ever gets a shutdown signal */
1493 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1496 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1497 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1499 while (NULL != sctx->my_handlers[i].callback)
1500 sctx->my_handlers[i++].callback_cls = sctx;
1501 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1502 if (-1 != sctx->ready_confirm_fd)
1504 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1505 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1506 sctx->ready_confirm_fd = -1;
1507 write_pid_file (sctx, getpid ());
1509 if (NULL != sctx->addrs)
1512 while (NULL != sctx->addrs[i])
1514 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1515 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1519 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1524 * Detach from terminal.
1526 * @param sctx service context
1530 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1537 if (0 != PIPE (filedes))
1539 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1540 return GNUNET_SYSERR;
1545 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1546 return GNUNET_SYSERR;
1553 GNUNET_break (0 == CLOSE (filedes[1]));
1555 if (1 != READ (filedes[0], &c, sizeof (char)))
1556 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1563 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1566 LOG (GNUNET_ERROR_TYPE_INFO,
1567 _("Service process could not initialize server function\n"));
1570 LOG (GNUNET_ERROR_TYPE_INFO,
1571 _("Service process failed to report status\n"));
1574 exit (1); /* child reported error */
1576 GNUNET_break (0 == CLOSE (0));
1577 GNUNET_break (0 == CLOSE (1));
1578 GNUNET_break (0 == CLOSE (filedes[0]));
1579 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1581 return GNUNET_SYSERR;
1582 /* set stdin/stdout to /dev/null */
1583 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1585 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1586 (void) CLOSE (nullfd);
1587 return GNUNET_SYSERR;
1589 (void) CLOSE (nullfd);
1590 /* Detach from controlling terminal */
1593 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1594 sctx->ready_confirm_fd = filedes[1];
1596 /* FIXME: we probably need to do something else
1597 * elsewhere in order to fork the process itself... */
1607 * @param sctx service context
1611 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1615 if (NULL == (user = get_user_name (sctx)))
1616 return GNUNET_OK; /* keep */
1621 pws = getpwnam (user);
1624 LOG (GNUNET_ERROR_TYPE_ERROR,
1625 _("Cannot obtain information about user `%s': %s\n"), user,
1626 errno == 0 ? _("No such user") : STRERROR (errno));
1628 return GNUNET_SYSERR;
1630 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1632 (0 != initgroups (user, pws->pw_gid)) ||
1634 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1636 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1637 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1639 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1640 user, STRERROR (errno));
1642 return GNUNET_SYSERR;
1652 * Delete the PID file that was created by our parent.
1654 * @param sctx service context
1657 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1659 char *pif = get_pid_file_name (sctx);
1662 return; /* no PID file */
1663 if (0 != UNLINK (pif))
1664 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1670 * Run a standard GNUnet service startup sequence (initialize loggers
1671 * and configuration, parse options).
1673 * @param argc number of command line arguments
1674 * @param argv command line arguments
1675 * @param serviceName our service name
1676 * @param opt service options
1677 * @param task main task of the service
1678 * @param task_cls closure for task
1679 * @return GNUNET_SYSERR on error, GNUNET_OK
1680 * if we shutdown nicely
1683 GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1684 enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task,
1687 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1695 unsigned long long skew_offset;
1696 unsigned long long skew_variance;
1697 long long clock_offset;
1698 struct GNUNET_SERVICE_Context sctx;
1699 struct GNUNET_CONFIGURATION_Handle *cfg;
1701 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1702 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1703 {'d', "daemonize", NULL,
1704 gettext_noop ("do daemonize (detach from terminal)"), 0,
1705 GNUNET_GETOPT_set_one, &do_daemonize},
1706 GNUNET_GETOPT_OPTION_HELP (NULL),
1707 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1708 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1709 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1710 GNUNET_GETOPT_OPTION_END
1716 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1717 memset (&sctx, 0, sizeof (sctx));
1719 sctx.ready_confirm_fd = -1;
1720 sctx.ret = GNUNET_OK;
1721 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1723 sctx.task_cls = task_cls;
1724 sctx.serviceName = serviceName;
1725 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1726 /* setup subsystems */
1727 if (GNUNET_SYSERR ==
1728 GNUNET_GETOPT_run (serviceName, service_options, argc, argv))
1730 if (GNUNET_OK != GNUNET_log_setup (serviceName, loglev, logfile))
1732 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1734 if (GNUNET_OK != setup_service (&sctx))
1736 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1738 if (GNUNET_OK != set_user_id (&sctx))
1740 LOG (GNUNET_ERROR_TYPE_DEBUG,
1741 "Service `%s' runs with configuration from `%s'\n", serviceName, cfg_fn);
1743 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1744 "SKEW_OFFSET", &skew_offset)) &&
1746 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1747 "SKEW_VARIANCE", &skew_variance)))
1749 clock_offset = skew_offset - skew_variance;
1750 GNUNET_TIME_set_offset (clock_offset);
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1753 /* actually run service */
1755 GNUNET_SCHEDULER_run (&service_task, &sctx);
1758 if ((1 == do_daemonize) && (NULL != sctx.server))
1759 pid_file_delete (&sctx);
1760 GNUNET_free_non_null (sctx.my_handlers);
1763 if (-1 != sctx.ready_confirm_fd)
1765 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1766 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1767 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1770 GNUNET_CONFIGURATION_destroy (cfg);
1772 if (NULL != sctx.addrs)
1773 while (NULL != sctx.addrs[i])
1774 GNUNET_free (sctx.addrs[i++]);
1775 GNUNET_free_non_null (sctx.addrs);
1776 GNUNET_free_non_null (sctx.addrlens);
1777 GNUNET_free_non_null (logfile);
1778 GNUNET_free_non_null (loglev);
1779 GNUNET_free (cfg_fn);
1780 GNUNET_free_non_null (sctx.v4_denied);
1781 GNUNET_free_non_null (sctx.v6_denied);
1782 GNUNET_free_non_null (sctx.v4_allowed);
1783 GNUNET_free_non_null (sctx.v6_allowed);
1785 return err ? GNUNET_SYSERR : sctx.ret;
1790 * Run a service startup sequence within an existing
1791 * initialized system.
1793 * @param serviceName our service name
1794 * @param cfg configuration to use
1795 * @return NULL on error, service handle
1797 struct GNUNET_SERVICE_Context *
1798 GNUNET_SERVICE_start (const char *serviceName,
1799 const struct GNUNET_CONFIGURATION_Handle *cfg)
1802 struct GNUNET_SERVICE_Context *sctx;
1804 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1805 sctx->ready_confirm_fd = -1; /* no daemonizing */
1806 sctx->ret = GNUNET_OK;
1807 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1808 sctx->serviceName = serviceName;
1811 /* setup subsystems */
1812 if (GNUNET_OK != setup_service (sctx))
1814 GNUNET_SERVICE_stop (sctx);
1817 if (NULL != sctx->lsocks)
1819 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1820 sctx->timeout, sctx->require_found);
1823 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1824 sctx->timeout, sctx->require_found);
1826 if (NULL == sctx->server)
1828 GNUNET_SERVICE_stop (sctx);
1831 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1832 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1834 while ((sctx->my_handlers[i].callback != NULL))
1835 sctx->my_handlers[i++].callback_cls = sctx;
1836 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1842 * Obtain the server used by a service. Note that the server must NOT
1843 * be destroyed by the caller.
1845 * @param ctx the service context returned from the start function
1846 * @return handle to the server for this service, NULL if there is none
1848 struct GNUNET_SERVER_Handle *
1849 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1856 * Stop a service that was started with "GNUNET_SERVICE_start".
1858 * @param sctx the service context returned from the start function
1861 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1865 if (NULL != sctx->server)
1866 GNUNET_SERVER_destroy (sctx->server);
1867 GNUNET_free_non_null (sctx->my_handlers);
1868 if (NULL != sctx->addrs)
1871 while (NULL != sctx->addrs[i])
1872 GNUNET_free (sctx->addrs[i++]);
1873 GNUNET_free (sctx->addrs);
1875 GNUNET_free_non_null (sctx->addrlens);
1876 GNUNET_free_non_null (sctx->v4_denied);
1877 GNUNET_free_non_null (sctx->v6_denied);
1878 GNUNET_free_non_null (sctx->v4_allowed);
1879 GNUNET_free_non_null (sctx->v6_allowed);
1884 /* end of service.c */