2 This file is part of GNUnet.
3 (C) 2009 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)
45 #define DEBUG_SERVICE GNUNET_EXTRA_LOGGING
47 /* ******************* access control ******************** */
50 * @brief IPV4 network in CIDR notation.
54 struct in_addr network;
55 struct in_addr netmask;
59 * @brief network in CIDR notation for IPV6.
63 struct in6_addr network;
64 struct in6_addr netmask;
69 * Parse a network specification. The argument specifies
70 * a list of networks. The format is
71 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
72 * with a semicolon). The network must be given in dotted-decimal
73 * notation. The netmask can be given in CIDR notation (/16) or
74 * in dotted-decimal (/255.255.0.0).
76 * @param routeList a string specifying the forbidden networks
77 * @return the converted list, NULL if the synatx is flawed
79 static struct IPv4NetworkSet *
80 parse_ipv4_specification (const char *routeList)
88 unsigned int temps[8];
90 struct IPv4NetworkSet *result;
92 if (routeList == NULL)
94 len = strlen (routeList);
98 for (i = 0; i < len; i++)
99 if (routeList[i] == ';')
101 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
102 /* add termination */
103 memset (result, 0, sizeof (struct IPv4NetworkSet) * (count + 1));
109 sscanf (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
110 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
111 &temps[6], &temps[7]);
114 for (j = 0; j < 8; j++)
117 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
119 GNUNET_free (result);
122 result[i].network.s_addr =
123 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
125 result[i].netmask.s_addr =
126 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
128 while (routeList[pos] != ';')
134 /* try second notation */
136 sscanf (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
137 &temps[2], &temps[3], &slash);
140 for (j = 0; j < 4; j++)
143 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
145 GNUNET_free (result);
148 result[i].network.s_addr =
149 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
151 if ((slash <= 32) && (slash >= 0))
153 result[i].netmask.s_addr = 0;
156 result[i].netmask.s_addr =
157 (result[i].netmask.s_addr >> 1) + 0x80000000;
160 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
161 while (routeList[pos] != ';')
169 LOG (GNUNET_ERROR_TYPE_ERROR,
170 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
172 GNUNET_free (result);
173 return NULL; /* error */
176 /* try third notation */
179 sscanf (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
180 &temps[2], &temps[3]);
183 for (j = 0; j < 4; j++)
186 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
188 GNUNET_free (result);
191 result[i].network.s_addr =
192 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
194 result[i].netmask.s_addr = 0;
197 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
200 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
201 while (routeList[pos] != ';')
207 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
209 GNUNET_free (result);
210 return NULL; /* error */
212 if (pos < strlen (routeList))
214 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
216 GNUNET_free (result);
217 return NULL; /* oops */
219 return result; /* ok */
224 * Parse a network specification. The argument specifies
225 * a list of networks. The format is
226 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
227 * with a semicolon). The network must be given in colon-hex
228 * notation. The netmask must be given in CIDR notation (/16) or
229 * can be omitted to specify a single host.
231 * @param routeListX a string specifying the forbidden networks
232 * @return the converted list, NULL if the synatx is flawed
234 static struct IPv6NetworkSet *
235 parse_ipv6_specification (const char *routeListX)
245 struct IPv6NetworkSet *result;
250 if (routeListX == NULL)
252 len = strlen (routeListX);
255 routeList = GNUNET_strdup (routeListX);
257 for (i = 0; i < len; i++)
258 if (routeList[i] == ';')
260 if (routeList[len - 1] != ';')
262 LOG (GNUNET_ERROR_TYPE_ERROR,
263 _("Invalid network notation (does not end with ';': `%s')\n"),
265 GNUNET_free (routeList);
269 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
270 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1));
276 while (routeList[pos] != ';')
279 while ((slash >= start) && (routeList[slash] != '/'))
283 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
288 routeList[pos] = '\0';
289 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
293 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
296 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"),
297 &routeList[slash + 1]);
301 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
303 GNUNET_free (result);
304 GNUNET_free (routeList);
310 result[i].netmask.s6_addr[off++] = 0xFF;
315 result[i].netmask.s6_addr[off] =
316 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
321 routeList[slash] = '\0';
322 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
326 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"),
327 &routeList[slash + 1]);
329 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
330 GNUNET_free (result);
331 GNUNET_free (routeList);
337 GNUNET_free (routeList);
343 * Check if the given IP address is in the list of IP addresses.
345 * @param list a list of networks
346 * @param add the IP to check (in network byte order)
347 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
350 check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
358 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
360 if ((add->s_addr & list[i].netmask.s_addr) ==
361 (list[i].network.s_addr & list[i].netmask.s_addr))
369 * Check if the given IP address is in the list of IP addresses.
371 * @param list a list of networks
372 * @param ip the IP to check (in network byte order)
373 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
376 check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
380 struct in6_addr zero;
385 memset (&zero, 0, sizeof (struct in6_addr));
388 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0)
390 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
391 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
392 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
403 /* ****************** service struct ****************** */
407 * Context for "service_task".
409 struct GNUNET_SERVICE_Context
414 const struct GNUNET_CONFIGURATION_Handle *cfg;
417 * Handle for the server.
419 struct GNUNET_SERVER_Handle *server;
422 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
425 struct sockaddr **addrs;
428 * Name of our service.
430 const char *serviceName;
433 * Main service-specific task to run.
435 GNUNET_SERVICE_Main task;
443 * IPv4 addresses that are not allowed to connect.
445 struct IPv4NetworkSet *v4_denied;
448 * IPv6 addresses that are not allowed to connect.
450 struct IPv6NetworkSet *v6_denied;
453 * IPv4 addresses that are allowed to connect (if not
454 * set, all are allowed).
456 struct IPv4NetworkSet *v4_allowed;
459 * IPv6 addresses that are allowed to connect (if not
460 * set, all are allowed).
462 struct IPv6NetworkSet *v6_allowed;
465 * My (default) message handlers. Adjusted copy
468 struct GNUNET_SERVER_MessageHandler *my_handlers;
471 * Array of the lengths of the entries in addrs.
476 * NULL-terminated array of listen sockets we should take over.
478 struct GNUNET_NETWORK_Handle **lsocks;
481 * Idle timeout for server.
483 struct GNUNET_TIME_Relative timeout;
486 * Overall success/failure of the service start.
491 * If we are daemonizing, this FD is set to the
492 * pipe to the parent. Send '.' if we started
493 * ok, '!' if not. -1 if we are not daemonizing.
495 int ready_confirm_fd;
498 * Do we close connections if we receive messages
499 * for which we have no handler?
504 * Do we require a matching UID for UNIX domain socket connections?
505 * GNUNET_NO means that the UID does not have to match (however,
506 * "match_gid" may still impose other access control checks).
511 * Do we require a matching GID for UNIX domain socket connections?
512 * Ignored if "match_uid" is GNUNET_YES. Note that this is about
513 * checking that the client's UID is in our group OR that the
514 * client's GID is our GID. If both "match_gid" and "match_uid" are
515 * "GNUNET_NO", all users on the local system have access.
522 enum GNUNET_SERVICE_Options options;
527 /* ****************** message handlers ****************** */
530 write_test (void *cls, size_t size, void *buf)
532 struct GNUNET_SERVER_Client *client = cls;
533 struct GNUNET_MessageHeader *msg;
535 if (size < sizeof (struct GNUNET_MessageHeader))
537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
538 return 0; /* client disconnected */
540 msg = (struct GNUNET_MessageHeader *) buf;
541 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
542 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
544 return sizeof (struct GNUNET_MessageHeader);
548 * Handler for TEST message.
550 * @param cls closure (refers to service)
551 * @param client identification of the client
552 * @param message the actual message
555 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
556 const struct GNUNET_MessageHeader *message)
558 /* simply bounce message back to acknowledge */
560 GNUNET_SERVER_notify_transmit_ready (client,
561 sizeof (struct GNUNET_MessageHeader),
562 GNUNET_TIME_UNIT_FOREVER_REL,
563 &write_test, client))
564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
569 * Default handlers for all services. Will be copied and the
570 * "callback_cls" fields will be replaced with the specific service
573 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
574 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
575 sizeof (struct GNUNET_MessageHeader)},
581 /* ****************** service core routines ************** */
585 * Check if access to the service is allowed from the given address.
588 * @param uc credentials, if available, otherwise NULL
589 * @param addr address
590 * @param addrlen length of address
591 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
592 * for unknown address family (will be denied).
595 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
596 const struct sockaddr *addr, socklen_t addrlen)
598 struct GNUNET_SERVICE_Context *sctx = cls;
599 const struct sockaddr_in *i4;
600 const struct sockaddr_in6 *i6;
603 switch (addr->sa_family)
606 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
607 i4 = (const struct sockaddr_in *) addr;
608 ret = ((sctx->v4_allowed == NULL) ||
609 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
610 ((sctx->v4_denied == NULL) ||
611 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
614 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
615 i6 = (const struct sockaddr_in6 *) addr;
616 ret = ((sctx->v6_allowed == NULL) ||
617 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
618 ((sctx->v6_denied == NULL) ||
619 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
623 ret = GNUNET_OK; /* always OK for now */
624 if (sctx->match_uid == GNUNET_YES)
626 /* UID match required */
627 ret = (uc != NULL) && (uc->uid == geteuid ());
629 else if (sctx->match_gid == GNUNET_YES)
631 /* group match required */
634 /* no credentials, group match not possible */
642 if (uc->gid != getegid())
644 /* default group did not match, but maybe the user is in our group, let's check */
645 grp = getgrgid (getegid ());
648 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getgrgid");
652 for (i=0; NULL != grp->gr_mem[i]; i++)
654 struct passwd *nam = getpwnam (grp->gr_mem[i]);
656 continue; /* name in group that is not in user DB !? */
657 if (nam->pw_uid == uc->uid)
659 /* yes, uid is in our group, allow! */
667 if (GNUNET_NO == ret)
668 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
669 (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid);
673 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
675 return GNUNET_SYSERR;
677 if (ret != GNUNET_OK)
679 LOG (GNUNET_ERROR_TYPE_WARNING,
680 _("Access from `%s' denied to service `%s'\n"), GNUNET_a2s (addr,
689 * Get the name of the file where we will
690 * write the PID of the service.
693 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
699 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
707 * Parse an IPv4 access control list.
710 process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
715 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
717 GNUNET_break (GNUNET_OK ==
718 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
721 if (NULL == (*ret = parse_ipv4_specification (opt)))
723 LOG (GNUNET_ERROR_TYPE_WARNING,
724 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
725 opt, sctx->serviceName, option);
727 return GNUNET_SYSERR;
735 * Parse an IPv4 access control list.
738 process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
743 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
745 GNUNET_break (GNUNET_OK ==
746 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
749 if (NULL == (*ret = parse_ipv6_specification (opt)))
751 LOG (GNUNET_ERROR_TYPE_WARNING,
752 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
753 opt, sctx->serviceName, option);
755 return GNUNET_SYSERR;
762 * Add the given UNIX domain path as an address to the
763 * list (as the first entry).
765 * @param saddrs array to update
766 * @param saddrlens where to store the address length
767 * @param unixpath path to add
770 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
771 const char *unixpath)
774 struct sockaddr_un *un;
777 un = GNUNET_malloc (sizeof (struct sockaddr_un));
778 un->sun_family = AF_UNIX;
779 slen = strlen (unixpath) + 1;
780 if (slen >= sizeof (un->sun_path))
781 slen = sizeof (un->sun_path) - 1;
782 memcpy (un->sun_path, unixpath, slen);
783 un->sun_path[slen] = '\0';
784 slen = sizeof (struct sockaddr_un);
786 un->sun_path[0] = '\0';
788 #if HAVE_SOCKADDR_IN_SIN_LEN
789 un->sun_len = (u_char) slen;
791 *saddrs = (struct sockaddr *) un;
794 /* this function should never be called
795 * unless AF_UNIX is defined! */
802 * Get the list of addresses that a server for the given service
805 * @param serviceName name of the service
806 * @param cfg configuration (which specifies the addresses)
807 * @param addrs set (call by reference) to an array of pointers to the
808 * addresses the server should bind to and listen on; the
809 * array will be NULL-terminated (on success)
810 * @param addr_lens set (call by reference) to an array of the lengths
811 * of the respective 'struct sockaddr' struct in the 'addrs'
813 * @return number of addresses found on success,
814 * GNUNET_SYSERR if the configuration
815 * did not specify reasonable finding information or
816 * if it specified a hostname that could not be resolved;
817 * GNUNET_NO if the number of addresses configured is
818 * zero (in this case, '*addrs' and '*addr_lens' will be
822 GNUNET_SERVICE_get_server_addresses (const char *serviceName,
823 const struct GNUNET_CONFIGURATION_Handle
824 *cfg, struct sockaddr ***addrs,
825 socklen_t ** addr_lens)
828 struct GNUNET_NETWORK_Handle *desc;
829 unsigned long long port;
831 struct addrinfo hints;
832 struct addrinfo *res;
833 struct addrinfo *pos;
834 struct addrinfo *next;
838 struct sockaddr **saddrs;
839 socklen_t *saddrlens;
845 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "DISABLEV6"))
849 GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName, "DISABLEV6")))
850 return GNUNET_SYSERR;
853 disablev6 = GNUNET_NO;
857 /* probe IPv6 support */
858 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
861 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
864 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
865 return GNUNET_SYSERR;
867 LOG (GNUNET_ERROR_TYPE_INFO,
869 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
870 serviceName, STRERROR (errno));
871 disablev6 = GNUNET_YES;
875 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
881 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
883 GNUNET_break (GNUNET_OK ==
884 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
888 LOG (GNUNET_ERROR_TYPE_ERROR,
889 _("Require valid port number for service `%s' in configuration!\n"),
891 return GNUNET_SYSERR;
895 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
897 GNUNET_break (GNUNET_OK ==
898 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
899 "BINDTO", &hostname));
907 GNUNET_CONFIGURATION_have_value (cfg, serviceName, "UNIXPATH")) &&
909 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, "UNIXPATH",
911 (0 < strlen (unixpath)))
913 /* probe UNIX support */
914 struct sockaddr_un s_un;
916 if (strlen (unixpath) >= sizeof (s_un.sun_path))
918 LOG (GNUNET_ERROR_TYPE_WARNING,
919 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
920 sizeof (s_un.sun_path));
921 GNUNET_free_non_null (hostname);
922 GNUNET_free (unixpath);
923 return GNUNET_SYSERR;
926 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
929 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
932 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
933 GNUNET_free_non_null (hostname);
934 GNUNET_free (unixpath);
935 return GNUNET_SYSERR;
937 LOG (GNUNET_ERROR_TYPE_INFO,
939 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
940 serviceName, STRERROR (errno));
941 GNUNET_free (unixpath);
946 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
952 if ((port == 0) && (unixpath == NULL))
954 LOG (GNUNET_ERROR_TYPE_ERROR,
956 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
958 GNUNET_free_non_null (hostname);
959 return GNUNET_SYSERR;
963 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
964 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
965 add_unixpath (saddrs, saddrlens, unixpath);
966 GNUNET_free_non_null (unixpath);
967 GNUNET_free_non_null (hostname);
969 *addr_lens = saddrlens;
973 if (hostname != NULL)
976 LOG (GNUNET_ERROR_TYPE_DEBUG,
977 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
980 memset (&hints, 0, sizeof (struct addrinfo));
982 hints.ai_family = AF_INET;
983 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
986 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
988 GNUNET_free (hostname);
989 GNUNET_free_non_null (unixpath);
990 return GNUNET_SYSERR;
994 while (NULL != (pos = next))
997 if ((disablev6) && (pos->ai_family == AF_INET6))
1003 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
1004 disablev6 ? "IPv4 " : "", hostname);
1006 GNUNET_free (hostname);
1007 GNUNET_free_non_null (unixpath);
1008 return GNUNET_SYSERR;
1011 if (NULL != unixpath)
1013 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1014 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1016 if (NULL != unixpath)
1018 add_unixpath (saddrs, saddrlens, unixpath);
1022 while (NULL != (pos = next))
1024 next = pos->ai_next;
1025 if ((disablev6) && (pos->ai_family == AF_INET6))
1027 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
1028 continue; /* not TCP */
1029 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
1030 continue; /* huh? */
1032 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1033 serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1035 if (pos->ai_family == AF_INET)
1037 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
1038 saddrlens[i] = pos->ai_addrlen;
1039 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1040 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1041 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1045 GNUNET_assert (pos->ai_family == AF_INET6);
1046 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1047 saddrlens[i] = pos->ai_addrlen;
1048 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1049 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1050 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1054 GNUNET_free (hostname);
1060 /* will bind against everything, just set port */
1065 if (NULL != unixpath)
1068 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1069 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1070 if (NULL != unixpath)
1072 add_unixpath (saddrs, saddrlens, unixpath);
1075 saddrlens[i] = sizeof (struct sockaddr_in);
1076 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1077 #if HAVE_SOCKADDR_IN_SIN_LEN
1078 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1080 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1081 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1087 if (NULL != unixpath)
1089 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1090 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1092 if (NULL != unixpath)
1094 add_unixpath (saddrs, saddrlens, unixpath);
1097 saddrlens[i] = sizeof (struct sockaddr_in6);
1098 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1099 #if HAVE_SOCKADDR_IN_SIN_LEN
1100 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1102 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1103 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1105 saddrlens[i] = sizeof (struct sockaddr_in);
1106 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1107 #if HAVE_SOCKADDR_IN_SIN_LEN
1108 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1110 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1111 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1114 GNUNET_free_non_null (unixpath);
1116 *addr_lens = saddrlens;
1123 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1124 * and GNUNET_SYSERR on error.
1127 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1129 const char *env_buf;
1134 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1135 if ((env_buf == NULL) || (strlen (env_buf) <= 0))
1139 /* Using W32 API directly here, because this pipe will
1140 * never be used outside of this function, and it's just too much of a bother
1141 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1143 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1144 if (lsocks_pipe == 0 || lsocks_pipe == INVALID_HANDLE_VALUE)
1154 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1155 if (ret == 0 || rd != sizeof (count) || count == 0)
1158 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
1161 for (i = 0; i < count; i++)
1163 WSAPROTOCOL_INFOA pi;
1166 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1167 if (ret == 0 || rd != sizeof (size) || size != sizeof (pi))
1169 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1170 if (ret == 0 || rd != sizeof (pi))
1172 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1173 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1174 if (sctx->lsocks[i] == NULL)
1176 else if (i == count - 1)
1181 sctx->lsocks[count] = NULL;
1186 CloseHandle (lsocks_pipe);
1190 LOG (GNUNET_ERROR_TYPE_ERROR,
1191 _("Could not access a pre-bound socket, will try to bind myself\n"));
1192 for (i = 0; i < count && sctx->lsocks[i] != NULL; i++)
1193 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1194 GNUNET_free_non_null (sctx->lsocks);
1195 sctx->lsocks = NULL;
1205 * Setup addr, addrlen, idle_timeout
1206 * based on configuration!
1208 * Configuration may specify:
1209 * - PORT (where to bind to for TCP)
1210 * - UNIXPATH (where to bind to for UNIX domain sockets)
1211 * - TIMEOUT (after how many ms does an inactive service timeout);
1212 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1213 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1214 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1215 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1216 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1217 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1219 * @return GNUNET_OK if configuration succeeded
1222 setup_service (struct GNUNET_SERVICE_Context *sctx)
1224 struct GNUNET_TIME_Relative idleout;
1235 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, "TIMEOUT"))
1238 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName,
1239 "TIMEOUT", &idleout))
1241 LOG (GNUNET_ERROR_TYPE_ERROR,
1242 _("Specified value for `%s' of service `%s' is invalid\n"),
1243 "TIMEOUT", sctx->serviceName);
1244 return GNUNET_SYSERR;
1246 sctx->timeout = idleout;
1249 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1251 if (GNUNET_CONFIGURATION_have_value
1252 (sctx->cfg, sctx->serviceName, "TOLERANT"))
1254 if (GNUNET_SYSERR ==
1256 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1259 LOG (GNUNET_ERROR_TYPE_ERROR,
1260 _("Specified value for `%s' of service `%s' is invalid\n"),
1261 "TOLERANT", sctx->serviceName);
1262 return GNUNET_SYSERR;
1266 tolerant = GNUNET_NO;
1270 if ((NULL != (lpid = getenv ("LISTEN_PID"))) &&
1271 (1 == sscanf (lpid, "%u", &pid)) && (getpid () == (pid_t) pid) &&
1272 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1273 (1 == sscanf (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1274 (cnt + 4 < FD_SETSIZE))
1277 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1280 flags = fcntl (3 + cnt, F_GETFD);
1281 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1283 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1285 LOG (GNUNET_ERROR_TYPE_ERROR,
1287 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1288 (unsigned int) 3 + cnt);
1290 while (sctx->lsocks[cnt] != NULL)
1291 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1292 GNUNET_free (sctx->lsocks);
1293 sctx->lsocks = NULL;
1297 unsetenv ("LISTEN_PID");
1298 unsetenv ("LISTEN_FDS");
1301 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1303 receive_sockets_from_parent (sctx);
1304 putenv ("GNUNET_OS_READ_LSOCKS=");
1308 if ((sctx->lsocks == NULL) &&
1310 GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg,
1311 &sctx->addrs, &sctx->addrlens)))
1312 return GNUNET_SYSERR;
1313 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1315 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1318 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1320 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1321 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1322 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1323 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1330 * Get the name of the user that'll be used
1331 * to provide the service.
1334 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1340 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
1350 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1358 if (NULL == (pif = get_pid_file_name (sctx)))
1359 return GNUNET_OK; /* no file desired */
1360 user = get_user_name (sctx);
1361 rdir = GNUNET_strdup (pif);
1362 len = strlen (rdir);
1363 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1366 if (0 != ACCESS (rdir, F_OK))
1368 /* we get to create a directory -- and claim it
1370 GNUNET_DISK_directory_create (rdir);
1371 if ((user != NULL) && (0 < strlen (user)))
1372 GNUNET_DISK_file_change_owner (rdir, user);
1374 if (0 != ACCESS (rdir, W_OK | X_OK))
1376 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1378 GNUNET_free_non_null (user);
1380 return GNUNET_SYSERR;
1383 pidfd = FOPEN (pif, "w");
1386 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1388 GNUNET_free_non_null (user);
1389 return GNUNET_SYSERR;
1391 if (0 > FPRINTF (pidfd, "%u", pid))
1392 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1393 GNUNET_break (0 == FCLOSE (pidfd));
1394 if ((user != NULL) && (0 < strlen (user)))
1395 GNUNET_DISK_file_change_owner (pif, user);
1396 GNUNET_free_non_null (user);
1403 * Task run during shutdown.
1409 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1411 struct GNUNET_SERVER_Handle *server = cls;
1413 GNUNET_SERVER_destroy (server);
1418 * Initial task for the service.
1421 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1423 struct GNUNET_SERVICE_Context *sctx = cls;
1426 GNUNET_RESOLVER_connect (sctx->cfg);
1427 if (sctx->lsocks != NULL)
1429 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1430 sctx->timeout, sctx->require_found);
1433 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1434 sctx->timeout, sctx->require_found);
1435 if (sctx->server == NULL)
1437 if (sctx->addrs != NULL)
1440 while (sctx->addrs[i] != NULL)
1442 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1443 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1447 sctx->ret = GNUNET_SYSERR;
1450 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1452 /* install a task that will kill the server
1453 * process if the scheduler ever gets a shutdown signal */
1454 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1457 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1458 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1460 while ((sctx->my_handlers[i].callback != NULL))
1461 sctx->my_handlers[i++].callback_cls = sctx;
1462 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1463 if (sctx->ready_confirm_fd != -1)
1465 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1466 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1467 sctx->ready_confirm_fd = -1;
1468 write_pid_file (sctx, getpid ());
1470 if (sctx->addrs != NULL)
1473 while (sctx->addrs[i] != NULL)
1475 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1476 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1480 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1485 * Detach from terminal.
1488 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1495 if (0 != PIPE (filedes))
1497 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1498 return GNUNET_SYSERR;
1503 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1504 return GNUNET_SYSERR;
1511 GNUNET_break (0 == CLOSE (filedes[1]));
1513 if (1 != READ (filedes[0], &c, sizeof (char)))
1514 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1521 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1524 LOG (GNUNET_ERROR_TYPE_INFO,
1525 _("Service process could not initialize server function\n"));
1528 LOG (GNUNET_ERROR_TYPE_INFO,
1529 _("Service process failed to report status\n"));
1532 exit (1); /* child reported error */
1534 GNUNET_break (0 == CLOSE (0));
1535 GNUNET_break (0 == CLOSE (1));
1536 GNUNET_break (0 == CLOSE (filedes[0]));
1537 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1539 return GNUNET_SYSERR;
1540 /* set stdin/stdout to /dev/null */
1541 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1543 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1544 (void) CLOSE (nullfd);
1545 return GNUNET_SYSERR;
1547 (void) CLOSE (nullfd);
1548 /* Detach from controlling terminal */
1551 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1552 sctx->ready_confirm_fd = filedes[1];
1554 /* FIXME: we probably need to do something else
1555 * elsewhere in order to fork the process itself... */
1566 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1570 if (NULL == (user = get_user_name (sctx)))
1571 return GNUNET_OK; /* keep */
1576 pws = getpwnam (user);
1579 LOG (GNUNET_ERROR_TYPE_ERROR,
1580 _("Cannot obtain information about user `%s': %s\n"), user,
1581 errno == 0 ? _("No such user") : STRERROR (errno));
1583 return GNUNET_SYSERR;
1585 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1587 (0 != initgroups (user, pws->pw_gid)) ||
1589 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1591 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1592 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1594 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1595 user, STRERROR (errno));
1597 return GNUNET_SYSERR;
1607 * Delete the PID file that was created by our parent.
1610 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1612 char *pif = get_pid_file_name (sctx);
1615 return; /* no PID file */
1616 if (0 != UNLINK (pif))
1617 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1623 * Run a standard GNUnet service startup sequence (initialize loggers
1624 * and configuration, parse options).
1626 * @param argc number of command line arguments
1627 * @param argv command line arguments
1628 * @param serviceName our service name
1629 * @param opt service options
1630 * @param task main task of the service
1631 * @param task_cls closure for task
1632 * @return GNUNET_SYSERR on error, GNUNET_OK
1633 * if we shutdown nicely
1636 GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1637 enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task,
1640 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1648 unsigned long long skew_offset;
1649 unsigned long long skew_variance;
1650 long long clock_offset;
1651 struct GNUNET_SERVICE_Context sctx;
1652 struct GNUNET_CONFIGURATION_Handle *cfg;
1654 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1655 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1656 {'d', "daemonize", NULL,
1657 gettext_noop ("do daemonize (detach from terminal)"), 0,
1658 GNUNET_GETOPT_set_one, &do_daemonize},
1659 GNUNET_GETOPT_OPTION_HELP (serviceName),
1660 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1661 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1662 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1663 GNUNET_GETOPT_OPTION_END
1669 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1670 memset (&sctx, 0, sizeof (sctx));
1672 sctx.ready_confirm_fd = -1;
1673 sctx.ret = GNUNET_OK;
1674 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1676 sctx.task_cls = task_cls;
1677 sctx.serviceName = serviceName;
1678 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1679 /* setup subsystems */
1680 if (GNUNET_SYSERR ==
1681 GNUNET_GETOPT_run (serviceName, service_options, argc, argv))
1683 if (GNUNET_OK != GNUNET_log_setup (serviceName, loglev, logfile))
1685 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1687 if (GNUNET_OK != setup_service (&sctx))
1689 if ((do_daemonize == 1) && (GNUNET_OK != detach_terminal (&sctx)))
1691 if (GNUNET_OK != set_user_id (&sctx))
1694 LOG (GNUNET_ERROR_TYPE_DEBUG,
1695 "Service `%s' runs with configuration from `%s'\n", serviceName, cfg_fn);
1698 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1699 "SKEW_OFFSET", &skew_offset)) &&
1701 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1702 "SKEW_VARIANCE", &skew_variance)))
1704 clock_offset = skew_offset - skew_variance;
1705 GNUNET_TIME_set_offset (clock_offset);
1707 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1710 /* actually run service */
1712 GNUNET_SCHEDULER_run (&service_task, &sctx);
1715 if ((do_daemonize == 1) && (sctx.server != NULL))
1716 pid_file_delete (&sctx);
1717 GNUNET_free_non_null (sctx.my_handlers);
1720 if (sctx.ready_confirm_fd != -1)
1722 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1723 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1724 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1727 GNUNET_CONFIGURATION_destroy (cfg);
1729 if (sctx.addrs != NULL)
1730 while (sctx.addrs[i] != NULL)
1731 GNUNET_free (sctx.addrs[i++]);
1732 GNUNET_free_non_null (sctx.addrs);
1733 GNUNET_free_non_null (sctx.addrlens);
1734 GNUNET_free_non_null (logfile);
1735 GNUNET_free_non_null (loglev);
1736 GNUNET_free (cfg_fn);
1737 GNUNET_free_non_null (sctx.v4_denied);
1738 GNUNET_free_non_null (sctx.v6_denied);
1739 GNUNET_free_non_null (sctx.v4_allowed);
1740 GNUNET_free_non_null (sctx.v6_allowed);
1742 return err ? GNUNET_SYSERR : sctx.ret;
1747 * Run a service startup sequence within an existing
1748 * initialized system.
1750 * @param serviceName our service name
1751 * @param cfg configuration to use
1752 * @return NULL on error, service handle
1754 struct GNUNET_SERVICE_Context *
1755 GNUNET_SERVICE_start (const char *serviceName,
1756 const struct GNUNET_CONFIGURATION_Handle *cfg)
1759 struct GNUNET_SERVICE_Context *sctx;
1761 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1762 sctx->ready_confirm_fd = -1; /* no daemonizing */
1763 sctx->ret = GNUNET_OK;
1764 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1765 sctx->serviceName = serviceName;
1768 /* setup subsystems */
1769 if (GNUNET_OK != setup_service (sctx))
1771 GNUNET_SERVICE_stop (sctx);
1774 if (sctx->lsocks != NULL)
1776 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1777 sctx->timeout, sctx->require_found);
1780 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1781 sctx->timeout, sctx->require_found);
1783 if (NULL == sctx->server)
1785 GNUNET_SERVICE_stop (sctx);
1788 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1789 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1791 while ((sctx->my_handlers[i].callback != NULL))
1792 sctx->my_handlers[i++].callback_cls = sctx;
1793 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1798 * Obtain the server used by a service. Note that the server must NOT
1799 * be destroyed by the caller.
1801 * @param ctx the service context returned from the start function
1802 * @return handle to the server for this service, NULL if there is none
1804 struct GNUNET_SERVER_Handle *
1805 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1812 * Stop a service that was started with "GNUNET_SERVICE_start".
1814 * @param sctx the service context returned from the start function
1817 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1821 if (NULL != sctx->server)
1822 GNUNET_SERVER_destroy (sctx->server);
1823 GNUNET_free_non_null (sctx->my_handlers);
1824 if (sctx->addrs != NULL)
1827 while (sctx->addrs[i] != NULL)
1828 GNUNET_free (sctx->addrs[i++]);
1829 GNUNET_free (sctx->addrs);
1831 GNUNET_free_non_null (sctx->addrlens);
1832 GNUNET_free_non_null (sctx->v4_denied);
1833 GNUNET_free_non_null (sctx->v6_denied);
1834 GNUNET_free_non_null (sctx->v4_allowed);
1835 GNUNET_free_non_null (sctx->v6_allowed);
1840 /* end of service.c */