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_server_lib.h"
36 #include "gnunet_service_lib.h"
38 #define DEBUG_SERVICE GNUNET_NO
40 /* ******************* access control ******************** */
43 * @brief IPV4 network in CIDR notation.
47 struct in_addr network;
48 struct in_addr netmask;
52 * @brief network in CIDR notation for IPV6.
56 struct in6_addr network;
57 struct in6_addr netmask;
62 * Parse a network specification. The argument specifies
63 * a list of networks. The format is
64 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
65 * with a semicolon). The network must be given in dotted-decimal
66 * notation. The netmask can be given in CIDR notation (/16) or
67 * in dotted-decimal (/255.255.0.0).
69 * @param routeList a string specifying the forbidden networks
70 * @return the converted list, NULL if the synatx is flawed
72 static struct IPv4NetworkSet *
73 parse_ipv4_specification (const char *routeList)
81 unsigned int temps[8];
83 struct IPv4NetworkSet *result;
85 if (routeList == NULL)
87 len = strlen (routeList);
91 for (i = 0; i < len; i++)
92 if (routeList[i] == ';')
94 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
96 memset (result, 0, sizeof (struct IPv4NetworkSet) * (count + 1));
101 cnt = sscanf (&routeList[pos],
102 "%u.%u.%u.%u/%u.%u.%u.%u;",
106 &temps[3], &temps[4], &temps[5], &temps[6], &temps[7]);
109 for (j = 0; j < 8; j++)
112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
113 _("Invalid format for IP: `%s'\n"),
115 GNUNET_free (result);
118 result[i].network.s_addr
120 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
122 result[i].netmask.s_addr =
123 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
125 while (routeList[pos] != ';')
131 /* try second notation */
132 cnt = sscanf (&routeList[pos],
134 &temps[0], &temps[1], &temps[2], &temps[3], &slash);
137 for (j = 0; j < 4; j++)
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141 _("Invalid format for IP: `%s'\n"),
143 GNUNET_free (result);
146 result[i].network.s_addr
148 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
150 if ((slash <= 32) && (slash >= 0))
152 result[i].netmask.s_addr = 0;
155 result[i].netmask.s_addr
156 = (result[i].netmask.s_addr >> 1) + 0x80000000;
159 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
160 while (routeList[pos] != ';')
168 GNUNET_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 */
178 cnt = sscanf (&routeList[pos],
180 &temps[0], &temps[1], &temps[2], &temps[3]);
183 for (j = 0; j < 4; j++)
186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187 _("Invalid format for IP: `%s'\n"),
189 GNUNET_free (result);
192 result[i].network.s_addr
194 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
196 result[i].netmask.s_addr = 0;
199 result[i].netmask.s_addr
200 = (result[i].netmask.s_addr >> 1) + 0x80000000;
203 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
204 while (routeList[pos] != ';')
210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
212 GNUNET_free (result);
213 return NULL; /* error */
215 if (pos < strlen (routeList))
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 _("Invalid format for IP: `%s'\n"), &routeList[pos]);
219 GNUNET_free (result);
220 return NULL; /* oops */
222 return result; /* ok */
227 * Parse a network specification. The argument specifies
228 * a list of networks. The format is
229 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
230 * with a semicolon). The network must be given in colon-hex
231 * notation. The netmask must be given in CIDR notation (/16) or
232 * can be omitted to specify a single host.
234 * @param routeListX a string specifying the forbidden networks
235 * @return the converted list, NULL if the synatx is flawed
237 static struct IPv6NetworkSet *
238 parse_ipv6_specification (const char *routeListX)
248 struct IPv6NetworkSet *result;
253 if (routeListX == NULL)
255 len = strlen (routeListX);
258 routeList = GNUNET_strdup (routeListX);
260 for (i = 0; i < len; i++)
261 if (routeList[i] == ';')
263 if (routeList[len - 1] != ';')
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267 ("Invalid network notation (does not end with ';': `%s')\n"),
269 GNUNET_free (routeList);
273 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
274 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1));
280 while (routeList[pos] != ';')
283 while ((slash >= start) && (routeList[slash] != '/'))
287 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
292 routeList[pos] = '\0';
293 ret = inet_pton (AF_INET6,
294 &routeList[slash + 1], &result[i].netmask);
298 if ((1 != SSCANF (&routeList[slash + 1],
299 "%u", &bits)) || (bits >= 128))
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
303 _("Wrong format `%s' for netmask\n"),
304 &routeList[slash + 1]);
308 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
311 GNUNET_free (result);
312 GNUNET_free (routeList);
318 result[i].netmask.s6_addr[off++] = 0xFF;
323 result[i].netmask.s6_addr[off]
324 = (result[i].netmask.s6_addr[off] >> 1) + 0x80;
329 routeList[slash] = '\0';
330 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 _("Wrong format `%s' for network\n"),
336 &routeList[slash + 1]);
338 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "inet_pton");
339 GNUNET_free (result);
340 GNUNET_free (routeList);
346 GNUNET_free (routeList);
352 * Check if the given IP address is in the list of IP addresses.
354 * @param list a list of networks
355 * @param add the IP to check (in network byte order)
356 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
359 check_ipv4_listed (const struct IPv4NetworkSet *list,
360 const struct in_addr *add)
368 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
370 if ((add->s_addr & list[i].netmask.s_addr) ==
371 (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,
387 const struct in6_addr *ip)
391 struct in6_addr zero;
396 memset (&zero, 0, sizeof (struct in6_addr));
399 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0)
401 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
402 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
403 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
414 /* ****************** service struct ****************** */
418 * Context for "service_task".
420 struct GNUNET_SERVICE_Context
425 const struct GNUNET_CONFIGURATION_Handle *cfg;
428 * Handle for the server.
430 struct GNUNET_SERVER_Handle *server;
433 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
436 struct sockaddr **addrs;
439 * Name of our service.
441 const char *serviceName;
444 * Main service-specific task to run.
446 GNUNET_SERVICE_Main task;
454 * IPv4 addresses that are not allowed to connect.
456 struct IPv4NetworkSet *v4_denied;
459 * IPv6 addresses that are not allowed to connect.
461 struct IPv6NetworkSet *v6_denied;
464 * IPv4 addresses that are allowed to connect (if not
465 * set, all are allowed).
467 struct IPv4NetworkSet *v4_allowed;
470 * IPv6 addresses that are allowed to connect (if not
471 * set, all are allowed).
473 struct IPv6NetworkSet *v6_allowed;
476 * My (default) message handlers. Adjusted copy
479 struct GNUNET_SERVER_MessageHandler *my_handlers;
482 * Array of the lengths of the entries in addrs.
484 socklen_t * addrlens;
487 * NULL-terminated array of listen sockets we should take over.
489 struct GNUNET_NETWORK_Handle **lsocks;
492 * Idle timeout for server.
494 struct GNUNET_TIME_Relative timeout;
497 * Overall success/failure of the service start.
502 * If we are daemonizing, this FD is set to the
503 * pipe to the parent. Send '.' if we started
504 * ok, '!' if not. -1 if we are not daemonizing.
506 int ready_confirm_fd;
509 * Do we close connections if we receive messages
510 * for which we have no handler?
517 enum GNUNET_SERVICE_Options options;
522 /* ****************** message handlers ****************** */
525 write_test (void *cls, size_t size, void *buf)
527 struct GNUNET_SERVER_Client *client = cls;
528 struct GNUNET_MessageHeader *msg;
530 if (size < sizeof (struct GNUNET_MessageHeader))
532 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
533 return 0; /* client disconnected */
535 msg = (struct GNUNET_MessageHeader *) buf;
536 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
537 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
538 GNUNET_SERVER_receive_done (client, GNUNET_OK);
539 return sizeof (struct GNUNET_MessageHeader);
543 * Handler for TEST message.
545 * @param cls closure (refers to service)
546 * @param client identification of the client
547 * @param message the actual message
550 handle_test (void *cls,
551 struct GNUNET_SERVER_Client *client,
552 const struct GNUNET_MessageHeader *message)
554 /* simply bounce message back to acknowledge */
555 if (NULL == GNUNET_SERVER_notify_transmit_ready (client,
557 GNUNET_MessageHeader),
558 GNUNET_TIME_UNIT_FOREVER_REL,
559 &write_test, client))
560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
565 * Default handlers for all services. Will be copied and the
566 * "callback_cls" fields will be replaced with the specific service
569 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
570 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
571 sizeof (struct GNUNET_MessageHeader)},
577 /* ****************** service core routines ************** */
581 * Check if access to the service is allowed from the given address.
584 check_access (void *cls, const struct sockaddr *addr, socklen_t addrlen)
586 struct GNUNET_SERVICE_Context *sctx = cls;
587 const struct sockaddr_in *i4;
588 const struct sockaddr_in6 *i6;
591 switch (addr->sa_family)
594 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
595 i4 = (const struct sockaddr_in *) addr;
596 ret = ((sctx->v4_allowed == NULL) ||
597 (check_ipv4_listed (sctx->v4_allowed,
599 && ((sctx->v4_denied == NULL) ||
600 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
603 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
604 i6 = (const struct sockaddr_in6 *) addr;
605 ret = ((sctx->v6_allowed == NULL) ||
606 (check_ipv6_listed (sctx->v6_allowed,
608 && ((sctx->v6_denied == NULL) ||
609 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
612 /* FIXME: support checking UID/GID in the future... */
613 ret = GNUNET_OK; /* always OK for now */
616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
617 _("Unknown address family %d\n"), addr->sa_family);
618 return GNUNET_SYSERR;
620 if (ret != GNUNET_OK)
622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
623 _("Access from `%s' denied to service `%s'\n"),
624 GNUNET_a2s (addr, addrlen), sctx->serviceName);
631 * Get the name of the file where we will
632 * write the PID of the service.
635 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
641 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
650 * Parse an IPv4 access control list.
653 process_acl4 (struct IPv4NetworkSet **ret,
654 struct GNUNET_SERVICE_Context *sctx, const char *option)
658 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
660 GNUNET_break (GNUNET_OK ==
661 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
664 if (NULL == (*ret = parse_ipv4_specification (opt)))
666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
668 ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
669 opt, sctx->serviceName, option);
671 return GNUNET_SYSERR;
679 * Parse an IPv4 access control list.
682 process_acl6 (struct IPv6NetworkSet **ret,
683 struct GNUNET_SERVICE_Context *sctx, const char *option)
686 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
688 GNUNET_break (GNUNET_OK ==
689 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
692 if (NULL == (*ret = parse_ipv6_specification (opt)))
694 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
696 ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
697 opt, sctx->serviceName, option);
699 return GNUNET_SYSERR;
706 * Add the given UNIX domain path as an address to the
707 * list (as the first entry).
709 * @param saddrs array to update
710 * @param saddrlens where to store the address length
711 * @param unixpath path to add
714 add_unixpath (struct sockaddr **saddrs,
715 socklen_t *saddrlens,
716 const char *unixpath)
719 struct sockaddr_un *un;
722 un = GNUNET_malloc (sizeof (struct sockaddr_un));
723 un->sun_family = AF_UNIX;
724 slen = strlen (unixpath) + 1;
725 if (slen >= sizeof (un->sun_path))
726 slen = sizeof (un->sun_path) - 1;
727 memcpy (un->sun_path,
730 un->sun_path[slen] = '\0';
732 un->sun_path[0] = '\0';
733 slen = sizeof (struct sockaddr_un);
735 slen += sizeof (sa_family_t);
737 *saddrs = (struct sockaddr*) un;
740 /* this function should never be called
741 unless AF_UNIX is defined! */
748 * Get the list of addresses that a server for the given service
751 * @param serviceName name of the service
752 * @param cfg configuration (which specifies the addresses)
753 * @param addrs set (call by reference) to an array of pointers to the
754 * addresses the server should bind to and listen on; the
755 * array will be NULL-terminated (on success)
756 * @param addr_lens set (call by reference) to an array of the lengths
757 * of the respective 'struct sockaddr' struct in the 'addrs'
759 * @return number of addresses found on success,
760 * GNUNET_SYSERR if the configuration
761 * did not specify reasonable finding information or
762 * if it specified a hostname that could not be resolved;
763 * GNUNET_NO if the number of addresses configured is
764 * zero (in this case, '*addrs' and '*addr_lens' will be
768 GNUNET_SERVICE_get_server_addresses (const char *serviceName,
769 const struct GNUNET_CONFIGURATION_Handle *cfg,
770 struct sockaddr ***addrs,
771 socklen_t **addr_lens)
774 struct GNUNET_NETWORK_Handle *desc;
775 unsigned long long port;
777 struct addrinfo hints;
778 struct addrinfo *res;
779 struct addrinfo *pos;
780 struct addrinfo *next;
784 struct sockaddr **saddrs;
785 socklen_t *saddrlens;
791 if (GNUNET_CONFIGURATION_have_value (cfg,
792 serviceName, "DISABLEV6"))
795 (disablev6 = GNUNET_CONFIGURATION_get_value_yesno (cfg,
798 return GNUNET_SYSERR;
801 disablev6 = GNUNET_NO;
805 /* probe IPv6 support */
806 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
809 if ((errno == ENOBUFS) ||
810 (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
812 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
813 return GNUNET_SYSERR;
815 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
817 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
818 serviceName, STRERROR (errno));
819 disablev6 = GNUNET_YES;
823 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
829 if (GNUNET_CONFIGURATION_have_value (cfg,
830 serviceName, "PORT"))
832 GNUNET_break (GNUNET_OK ==
833 GNUNET_CONFIGURATION_get_value_number (cfg,
839 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
841 ("Require valid port number for service `%s' in configuration!\n"),
843 return GNUNET_SYSERR;
847 if (GNUNET_CONFIGURATION_have_value (cfg,
848 serviceName, "BINDTO"))
850 GNUNET_break (GNUNET_OK ==
851 GNUNET_CONFIGURATION_get_value_string (cfg,
860 if (GNUNET_CONFIGURATION_have_value (cfg,
861 serviceName, "UNIXPATH"))
863 GNUNET_break (GNUNET_OK ==
864 GNUNET_CONFIGURATION_get_value_string (cfg,
869 /* probe UNIX support */
870 struct sockaddr_un s_un;
871 if (strlen(unixpath) >= sizeof(s_un.sun_path))
873 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
874 _("UNIXPATH `%s' too long, maximum length is %llu\n"),unixpath, sizeof(s_un.sun_path));
875 GNUNET_free_non_null (hostname);
876 GNUNET_free (unixpath);
877 return GNUNET_SYSERR;
880 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
883 if ((errno == ENOBUFS) ||
884 (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
886 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
887 GNUNET_free_non_null (hostname);
888 GNUNET_free (unixpath);
889 return GNUNET_SYSERR;
891 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
893 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
894 serviceName, STRERROR (errno));
895 GNUNET_free (unixpath);
900 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
914 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
916 GNUNET_free_non_null(hostname);
917 return GNUNET_SYSERR;
921 saddrs = GNUNET_malloc (2 * sizeof(struct sockaddr*));
922 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
923 add_unixpath (saddrs, saddrlens, unixpath);
924 GNUNET_free_non_null (unixpath);
925 GNUNET_free_non_null(hostname);
927 *addr_lens = saddrlens;
931 if (hostname != NULL)
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
935 "Resolving `%s' since that is where `%s' will bind to.\n",
939 memset (&hints, 0, sizeof (struct addrinfo));
941 hints.ai_family = AF_INET;
942 if ((0 != (ret = getaddrinfo (hostname,
943 NULL, &hints, &res))) || (res == NULL))
945 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
946 _("Failed to resolve `%s': %s\n"),
947 hostname, gai_strerror (ret));
948 GNUNET_free (hostname);
949 GNUNET_free (unixpath);
950 return GNUNET_SYSERR;
954 while (NULL != (pos = next))
957 if ( (disablev6) && (pos->ai_family == AF_INET6))
963 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
964 _("Failed to find %saddress for `%s'.\n"),
965 disablev6 ? "IPv4 " : "", hostname);
967 GNUNET_free (hostname);
968 GNUNET_free (unixpath);
969 return GNUNET_SYSERR;
972 if (NULL != unixpath)
974 saddrs = GNUNET_malloc ((resi+1) * sizeof(struct sockaddr*));
975 saddrlens = GNUNET_malloc ((resi+1) * sizeof (socklen_t));
977 if (NULL != unixpath)
979 add_unixpath (saddrs, saddrlens, unixpath);
983 while (NULL != (pos = next))
986 if ( (disablev6) && (pos->ai_family == AF_INET6))
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990 "Service `%s' will bind to `%s'\n",
992 GNUNET_a2s (pos->ai_addr,
995 if (pos->ai_family == AF_INET)
997 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
998 saddrlens[i] = pos->ai_addrlen;
999 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1000 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1001 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1005 GNUNET_assert (pos->ai_family == AF_INET6);
1006 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1007 saddrlens[i] = pos->ai_addrlen;
1008 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1009 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1010 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1014 GNUNET_free (hostname);
1019 /* will bind against everything, just set port */
1024 if (NULL != unixpath)
1027 saddrs = GNUNET_malloc ((resi+1) * sizeof(struct sockaddr*));
1028 saddrlens = GNUNET_malloc ((resi+1) * sizeof (socklen_t));
1029 if (NULL != unixpath)
1031 add_unixpath (saddrs, saddrlens, unixpath);
1034 saddrlens[i] = sizeof (struct sockaddr_in);
1035 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1036 #if HAVE_SOCKADDR_IN_SIN_LEN
1037 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1039 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1040 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1046 if (NULL != unixpath)
1048 saddrs = GNUNET_malloc ((resi+1) * sizeof(struct sockaddr*));
1049 saddrlens = GNUNET_malloc ((resi+1) * sizeof (socklen_t));
1051 if (NULL != unixpath)
1053 add_unixpath (saddrs, saddrlens, unixpath);
1056 saddrlens[i] = sizeof (struct sockaddr_in6);
1057 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1058 #if HAVE_SOCKADDR_IN_SIN_LEN
1059 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1061 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1062 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1064 saddrlens[i] = sizeof (struct sockaddr_in);
1065 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1066 #if HAVE_SOCKADDR_IN_SIN_LEN
1067 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1069 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1070 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1073 GNUNET_free_non_null (unixpath);
1075 *addr_lens = saddrlens;
1081 * Setup addr, addrlen, idle_timeout
1082 * based on configuration!
1084 * Configuration may specify:
1085 * - PORT (where to bind to for TCP)
1086 * - UNIXPATH (where to bind to for UNIX domain sockets)
1087 * - TIMEOUT (after how many ms does an inactive service timeout);
1088 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1089 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1090 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1091 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1092 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1093 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1095 * @return GNUNET_OK if configuration succeeded
1098 setup_service (struct GNUNET_SERVICE_Context *sctx)
1100 struct GNUNET_TIME_Relative idleout;
1110 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1111 sctx->serviceName, "TIMEOUT"))
1114 GNUNET_CONFIGURATION_get_value_time (sctx->cfg,
1116 "TIMEOUT", &idleout))
1118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1119 _("Specified value for `%s' of service `%s' is invalid\n"),
1122 return GNUNET_SYSERR;
1124 sctx->timeout = idleout;
1127 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1129 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1130 sctx->serviceName, "TOLERANT"))
1132 if (GNUNET_SYSERR ==
1133 (tolerant = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
1137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1138 _("Specified value for `%s' of service `%s' is invalid\n"),
1141 return GNUNET_SYSERR;
1145 tolerant = GNUNET_NO;
1149 if ( (NULL != (lpid = getenv ("LISTEN_PID"))) &&
1150 (1 == sscanf (lpid, "%u", &pid)) &&
1151 (getpid () == (pid_t) pid) &&
1152 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1153 (1 == sscanf (nfds, "%u", &cnt)) &&
1155 (cnt < FD_SETSIZE) &&
1156 (cnt + 4 < FD_SETSIZE) )
1158 sctx->lsocks = GNUNET_malloc (sizeof(struct GNUNET_NETWORK_Handle*) * (cnt+1));
1161 flags = fcntl (3 + cnt, F_GETFD);
1163 (0 != (flags & FD_CLOEXEC)) ||
1164 (NULL == (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))) )
1166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1167 _("Could not access pre-bound socket %u, will try to bind myself\n"),
1168 (unsigned int) 3 +cnt);
1170 while (sctx->lsocks[cnt] != NULL)
1171 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1172 GNUNET_free (sctx->lsocks);
1173 sctx->lsocks = NULL;
1177 unsetenv ("LISTEN_PID");
1178 unsetenv ("LISTEN_FDS");
1182 if ( (sctx->lsocks == NULL) &&
1184 GNUNET_SERVICE_get_server_addresses (sctx->serviceName,
1188 return GNUNET_SYSERR;
1189 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1191 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1192 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1193 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1194 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1201 * Get the name of the user that'll be used
1202 * to provide the service.
1205 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1211 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
1222 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1230 if (NULL == (pif = get_pid_file_name (sctx)))
1231 return GNUNET_OK; /* no file desired */
1232 user = get_user_name (sctx);
1233 rdir = GNUNET_strdup (pif);
1234 len = strlen (rdir);
1235 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1238 if (0 != ACCESS (rdir, F_OK))
1240 /* we get to create a directory -- and claim it
1242 GNUNET_DISK_directory_create (rdir);
1243 if ((user != NULL) && (0 < strlen (user)))
1244 GNUNET_DISK_file_change_owner (rdir, user);
1246 if (0 != ACCESS (rdir, W_OK | X_OK))
1248 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1250 GNUNET_free_non_null (user);
1252 return GNUNET_SYSERR;
1255 pidfd = FOPEN (pif, "w");
1258 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1260 GNUNET_free_non_null (user);
1261 return GNUNET_SYSERR;
1263 if (0 > FPRINTF (pidfd, "%u", pid))
1264 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1265 GNUNET_break (0 == fclose (pidfd));
1266 if ((user != NULL) && (0 < strlen (user)))
1267 GNUNET_DISK_file_change_owner (pif, user);
1268 GNUNET_free_non_null (user);
1275 * Task run during shutdown.
1281 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1283 struct GNUNET_SERVER_Handle *server = cls;
1285 GNUNET_SERVER_destroy (server);
1290 * Initial task for the service.
1293 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1295 struct GNUNET_SERVICE_Context *sctx = cls;
1298 if (sctx->lsocks != NULL)
1299 sctx->server = GNUNET_SERVER_create_with_sockets (&check_access,
1302 sctx->timeout, sctx->require_found);
1304 sctx->server = GNUNET_SERVER_create (&check_access,
1308 sctx->timeout, sctx->require_found);
1309 if (sctx->server == NULL)
1311 if (sctx->addrs != NULL)
1314 while (sctx->addrs[i] != NULL)
1316 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1317 _("Failed to start `%s' at `%s'\n"),
1319 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1323 sctx->ret = GNUNET_SYSERR;
1326 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1328 /* install a task that will kill the server
1329 process if the scheduler ever gets a shutdown signal */
1330 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1331 &shutdown_task, sctx->server);
1333 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1334 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1336 while ((sctx->my_handlers[i].callback != NULL))
1337 sctx->my_handlers[i++].callback_cls = sctx;
1338 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1339 if (sctx->ready_confirm_fd != -1)
1341 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1342 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1343 sctx->ready_confirm_fd = -1;
1344 write_pid_file (sctx, getpid ());
1346 if (sctx->addrs != NULL)
1349 while (sctx->addrs[i] != NULL)
1351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1352 _("Service `%s' runs at %s\n"),
1354 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1358 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1363 * Detach from terminal.
1366 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1373 if (0 != PIPE (filedes))
1375 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
1376 return GNUNET_SYSERR;
1381 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
1382 return GNUNET_SYSERR;
1389 GNUNET_break (0 == CLOSE (filedes[1]));
1391 if (1 != READ (filedes[0], &c, sizeof (char)))
1392 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1399 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1400 _("Service process failed to initialize\n"));
1403 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1405 ("Service process could not initialize server function\n"));
1408 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1409 _("Service process failed to report status\n"));
1412 exit (1); /* child reported error */
1414 GNUNET_break (0 == CLOSE (0));
1415 GNUNET_break (0 == CLOSE (1));
1416 GNUNET_break (0 == CLOSE (filedes[0]));
1417 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1419 return GNUNET_SYSERR;
1420 /* set stdin/stdout to /dev/null */
1421 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1423 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
1424 (void) CLOSE (nullfd);
1425 return GNUNET_SYSERR;
1427 (void) CLOSE (nullfd);
1428 /* Detach from controlling terminal */
1431 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsid");
1432 sctx->ready_confirm_fd = filedes[1];
1434 /* FIXME: we probably need to do something else
1435 elsewhere in order to fork the process itself... */
1446 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1450 if (NULL == (user = get_user_name (sctx)))
1451 return GNUNET_OK; /* keep */
1456 pws = getpwnam (user);
1459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1460 _("Cannot obtain information about user `%s': %s\n"),
1461 user, errno == 0 ? _("No such user") : STRERROR (errno));
1463 return GNUNET_SYSERR;
1465 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1467 (0 != initgroups (user, pws->pw_gid)) ||
1469 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1471 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1472 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1475 _("Cannot change user/group to `%s': %s\n"), user,
1478 return GNUNET_SYSERR;
1488 * Delete the PID file that was created by our parent.
1491 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1493 char *pif = get_pid_file_name (sctx);
1495 return; /* no PID file */
1496 if (0 != UNLINK (pif))
1497 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1503 * Run a standard GNUnet service startup sequence (initialize loggers
1504 * and configuration, parse options).
1506 * @param argc number of command line arguments
1507 * @param argv command line arguments
1508 * @param serviceName our service name
1509 * @param opt service options
1510 * @param task main task of the service
1511 * @param task_cls closure for task
1512 * @return GNUNET_SYSERR on error, GNUNET_OK
1513 * if we shutdown nicely
1516 GNUNET_SERVICE_run (int argc,
1518 const char *serviceName,
1519 enum GNUNET_SERVICE_Options opt,
1520 GNUNET_SERVICE_Main task, void *task_cls)
1522 #define HANDLE_ERROR do { err = 1; GNUNET_break (0); goto shutdown; } while (0)
1530 struct GNUNET_SERVICE_Context sctx;
1531 struct GNUNET_CONFIGURATION_Handle *cfg;
1532 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1533 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1534 {'d', "daemonize", NULL,
1535 gettext_noop ("do daemonize (detach from terminal)"), 0,
1536 GNUNET_GETOPT_set_one, &do_daemonize},
1537 GNUNET_GETOPT_OPTION_HELP (serviceName),
1538 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1539 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1540 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1541 GNUNET_GETOPT_OPTION_END
1546 loglev = GNUNET_strdup ("WARNING");
1547 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1548 memset (&sctx, 0, sizeof (sctx));
1550 sctx.ready_confirm_fd = -1;
1551 sctx.ret = GNUNET_OK;
1552 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1554 sctx.task_cls = task_cls;
1555 sctx.serviceName = serviceName;
1556 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1557 /* setup subsystems */
1558 if (GNUNET_SYSERR == GNUNET_GETOPT_run (serviceName, service_options, argc,
1561 if (GNUNET_OK != GNUNET_log_setup (serviceName, loglev, logfile))
1563 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1565 if (GNUNET_OK != setup_service (&sctx))
1567 if ( (do_daemonize == 1) && (GNUNET_OK != detach_terminal (&sctx)))
1569 if (GNUNET_OK != set_user_id (&sctx))
1572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1573 "Service `%s' runs with configuration from `%s'\n",
1574 serviceName, cfg_fn);
1576 /* actually run service */
1577 GNUNET_SCHEDULER_run (&service_task, &sctx);
1580 if ((do_daemonize == 1) && (sctx.server != NULL))
1581 pid_file_delete (&sctx);
1582 GNUNET_free_non_null (sctx.my_handlers);
1585 if (sctx.ready_confirm_fd != -1)
1587 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1588 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1589 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1592 GNUNET_CONFIGURATION_destroy (cfg);
1594 if (sctx.addrs != NULL)
1595 while (sctx.addrs[i] != NULL)
1596 GNUNET_free (sctx.addrs[i++]);
1597 GNUNET_free_non_null (sctx.addrs);
1598 GNUNET_free_non_null (sctx.addrlens);
1599 GNUNET_free_non_null (logfile);
1600 GNUNET_free (loglev);
1601 GNUNET_free (cfg_fn);
1602 GNUNET_free_non_null (sctx.v4_denied);
1603 GNUNET_free_non_null (sctx.v6_denied);
1604 GNUNET_free_non_null (sctx.v4_allowed);
1605 GNUNET_free_non_null (sctx.v6_allowed);
1607 return err ? GNUNET_SYSERR : sctx.ret;
1612 * Run a service startup sequence within an existing
1613 * initialized system.
1615 * @param serviceName our service name
1616 * @param cfg configuration to use
1617 * @return NULL on error, service handle
1619 struct GNUNET_SERVICE_Context *
1620 GNUNET_SERVICE_start (const char *serviceName,
1621 const struct GNUNET_CONFIGURATION_Handle *cfg)
1624 struct GNUNET_SERVICE_Context *sctx;
1626 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1627 sctx->ready_confirm_fd = -1; /* no daemonizing */
1628 sctx->ret = GNUNET_OK;
1629 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1630 sctx->serviceName = serviceName;
1633 /* setup subsystems */
1634 if (GNUNET_OK != setup_service (sctx))
1636 GNUNET_SERVICE_stop (sctx);
1639 if (sctx->lsocks != NULL)
1640 sctx->server = GNUNET_SERVER_create_with_sockets (&check_access,
1643 sctx->timeout, sctx->require_found);
1645 sctx->server = GNUNET_SERVER_create (&check_access,
1650 sctx->require_found);
1652 if (NULL == sctx->server)
1654 GNUNET_SERVICE_stop (sctx);
1657 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1658 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1660 while ((sctx->my_handlers[i].callback != NULL))
1661 sctx->my_handlers[i++].callback_cls = sctx;
1662 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1667 * Obtain the server used by a service. Note that the server must NOT
1668 * be destroyed by the caller.
1670 * @param ctx the service context returned from the start function
1671 * @return handle to the server for this service, NULL if there is none
1673 struct GNUNET_SERVER_Handle *
1674 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1681 * Stop a service that was started with "GNUNET_SERVICE_start".
1683 * @param sctx the service context returned from the start function
1686 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1689 if (NULL != sctx->server)
1690 GNUNET_SERVER_destroy (sctx->server);
1691 GNUNET_free_non_null (sctx->my_handlers);
1692 if (sctx->addrs != NULL)
1695 while (sctx->addrs[i] != NULL)
1696 GNUNET_free (sctx->addrs[i++]);
1697 GNUNET_free (sctx->addrs);
1699 GNUNET_free_non_null (sctx->addrlens);
1700 GNUNET_free_non_null (sctx->v4_denied);
1701 GNUNET_free_non_null (sctx->v6_denied);
1702 GNUNET_free_non_null (sctx->v4_allowed);
1703 GNUNET_free_non_null (sctx->v6_allowed);
1708 /* end of service.c */