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 * Scheduler for the server.
435 struct GNUNET_SCHEDULER_Handle *sched;
438 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
441 struct sockaddr **addrs;
444 * Name of our service.
446 const char *serviceName;
449 * Main service-specific task to run.
451 GNUNET_SERVICE_Main task;
459 * IPv4 addresses that are not allowed to connect.
461 struct IPv4NetworkSet *v4_denied;
464 * IPv6 addresses that are not allowed to connect.
466 struct IPv6NetworkSet *v6_denied;
469 * IPv4 addresses that are allowed to connect (if not
470 * set, all are allowed).
472 struct IPv4NetworkSet *v4_allowed;
475 * IPv6 addresses that are allowed to connect (if not
476 * set, all are allowed).
478 struct IPv6NetworkSet *v6_allowed;
481 * My (default) message handlers. Adjusted copy
484 struct GNUNET_SERVER_MessageHandler *my_handlers;
487 * Array of the lengths of the entries in addrs.
489 socklen_t * addrlens;
492 * NULL-terminated array of listen sockets we should take over.
494 struct GNUNET_NETWORK_Handle **lsocks;
497 * Idle timeout for server.
499 struct GNUNET_TIME_Relative timeout;
502 * Maximum buffer size for the server.
507 * Overall success/failure of the service start.
512 * If we are daemonizing, this FD is set to the
513 * pipe to the parent. Send '.' if we started
514 * ok, '!' if not. -1 if we are not daemonizing.
516 int ready_confirm_fd;
519 * Do we close connections if we receive messages
520 * for which we have no handler?
527 enum GNUNET_SERVICE_Options options;
532 /* ****************** message handlers ****************** */
535 write_test (void *cls, size_t size, void *buf)
537 struct GNUNET_SERVER_Client *client = cls;
538 struct GNUNET_MessageHeader *msg;
540 if (size < sizeof (struct GNUNET_MessageHeader))
542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
543 return 0; /* client disconnected */
545 msg = (struct GNUNET_MessageHeader *) buf;
546 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
547 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
548 GNUNET_SERVER_receive_done (client, GNUNET_OK);
549 return sizeof (struct GNUNET_MessageHeader);
553 * Handler for TEST message.
555 * @param cls closure (refers to service)
556 * @param client identification of the client
557 * @param message the actual message
560 handle_test (void *cls,
561 struct GNUNET_SERVER_Client *client,
562 const struct GNUNET_MessageHeader *message)
564 /* simply bounce message back to acknowledge */
565 if (NULL == GNUNET_SERVER_notify_transmit_ready (client,
567 GNUNET_MessageHeader),
568 GNUNET_TIME_UNIT_FOREVER_REL,
569 &write_test, client))
570 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
575 * Default handlers for all services. Will be copied and the
576 * "callback_cls" fields will be replaced with the specific service
579 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
580 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
581 sizeof (struct GNUNET_MessageHeader)},
587 /* ****************** service core routines ************** */
591 * Check if access to the service is allowed from the given address.
594 check_access (void *cls, const struct sockaddr *addr, socklen_t addrlen)
596 struct GNUNET_SERVICE_Context *sctx = cls;
597 const struct sockaddr_in *i4;
598 const struct sockaddr_in6 *i6;
601 switch (addr->sa_family)
604 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
605 i4 = (const struct sockaddr_in *) addr;
606 ret = ((sctx->v4_allowed == NULL) ||
607 (check_ipv4_listed (sctx->v4_allowed,
609 && ((sctx->v4_denied == NULL) ||
610 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
613 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
614 i6 = (const struct sockaddr_in6 *) addr;
615 ret = ((sctx->v6_allowed == NULL) ||
616 (check_ipv6_listed (sctx->v6_allowed,
618 && ((sctx->v6_denied == NULL) ||
619 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
622 /* FIXME: support checking UID/GID in the future... */
623 ret = GNUNET_OK; /* always OK for now */
626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
627 _("Unknown address family %d\n"), addr->sa_family);
628 return GNUNET_SYSERR;
630 if (ret != GNUNET_OK)
632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
633 _("Access from `%s' denied to service `%s'\n"),
634 GNUNET_a2s (addr, addrlen), sctx->serviceName);
641 * Get the name of the file where we will
642 * write the PID of the service.
645 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
651 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
660 * Parse an IPv4 access control list.
663 process_acl4 (struct IPv4NetworkSet **ret,
664 struct GNUNET_SERVICE_Context *sctx, const char *option)
668 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
670 GNUNET_break (GNUNET_OK ==
671 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
674 if (NULL == (*ret = parse_ipv4_specification (opt)))
676 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
678 ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
679 opt, sctx->serviceName, option);
681 return GNUNET_SYSERR;
689 * Parse an IPv4 access control list.
692 process_acl6 (struct IPv6NetworkSet **ret,
693 struct GNUNET_SERVICE_Context *sctx, const char *option)
696 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
698 GNUNET_break (GNUNET_OK ==
699 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
702 if (NULL == (*ret = parse_ipv6_specification (opt)))
704 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
706 ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
707 opt, sctx->serviceName, option);
709 return GNUNET_SYSERR;
716 * Add the given UNIX domain path as an address to the
717 * list (as the first entry).
719 * @param saddrs array to update
720 * @param saddrlens where to store the address length
721 * @param unixpath path to add
724 add_unixpath (struct sockaddr **saddrs,
725 socklen_t *saddrlens,
726 const char *unixpath)
729 struct sockaddr_un *un;
732 un = GNUNET_malloc (sizeof (struct sockaddr_un));
733 un->sun_family = AF_UNIX;
734 slen = strlen (unixpath) + 1;
735 if (slen >= sizeof (un->sun_path))
736 slen = sizeof (un->sun_path) - 1;
737 memcpy (un->sun_path,
740 un->sun_path[slen] = '\0';
742 un->sun_path[0] = '\0';
743 slen = sizeof (struct sockaddr_un);
745 slen += sizeof (sa_family_t);
747 *saddrs = (struct sockaddr*) un;
750 /* this function should never be called
751 unless AF_UNIX is defined! */
758 * Get the list of addresses that a server for the given service
761 * @param serviceName name of the service
762 * @param cfg configuration (which specifies the addresses)
763 * @param addrs set (call by reference) to an array of pointers to the
764 * addresses the server should bind to and listen on; the
765 * array will be NULL-terminated (on success)
766 * @param addr_lens set (call by reference) to an array of the lengths
767 * of the respective 'struct sockaddr' struct in the 'addrs'
769 * @return number of addresses found on success,
770 * GNUNET_SYSERR if the configuration
771 * did not specify reasonable finding information or
772 * if it specified a hostname that could not be resolved;
773 * GNUNET_NO if the number of addresses configured is
774 * zero (in this case, '*addrs' and '*addr_lens' will be
778 GNUNET_SERVICE_get_server_addresses (const char *serviceName,
779 const struct GNUNET_CONFIGURATION_Handle *cfg,
780 struct sockaddr ***addrs,
781 socklen_t **addr_lens)
784 struct GNUNET_NETWORK_Handle *desc;
785 unsigned long long port;
787 struct addrinfo hints;
788 struct addrinfo *res;
789 struct addrinfo *pos;
790 struct addrinfo *next;
794 struct sockaddr **saddrs;
795 socklen_t *saddrlens;
801 if (GNUNET_CONFIGURATION_have_value (cfg,
802 serviceName, "DISABLEV6"))
805 (disablev6 = GNUNET_CONFIGURATION_get_value_yesno (cfg,
808 return GNUNET_SYSERR;
811 disablev6 = GNUNET_NO;
815 /* probe IPv6 support */
816 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
819 if ((errno == ENOBUFS) ||
820 (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
822 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
823 return GNUNET_SYSERR;
825 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
827 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
828 serviceName, STRERROR (errno));
829 disablev6 = GNUNET_YES;
833 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
839 if (GNUNET_CONFIGURATION_have_value (cfg,
840 serviceName, "PORT"))
842 GNUNET_break (GNUNET_OK ==
843 GNUNET_CONFIGURATION_get_value_number (cfg,
849 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
851 ("Require valid port number for service `%s' in configuration!\n"),
853 return GNUNET_SYSERR;
857 if (GNUNET_CONFIGURATION_have_value (cfg,
858 serviceName, "BINDTO"))
860 GNUNET_break (GNUNET_OK ==
861 GNUNET_CONFIGURATION_get_value_string (cfg,
870 if (GNUNET_CONFIGURATION_have_value (cfg,
871 serviceName, "UNIXPATH"))
873 GNUNET_break (GNUNET_OK ==
874 GNUNET_CONFIGURATION_get_value_string (cfg,
879 /* probe UNIX support */
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);
908 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
909 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
912 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
913 GNUNET_free_non_null(hostname);
914 return GNUNET_SYSERR;
917 if (hostname != NULL)
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Resolving `%s' since that is where `%s' will bind to.\n",
925 memset (&hints, 0, sizeof (struct addrinfo));
927 hints.ai_family = AF_INET;
928 if ((0 != (ret = getaddrinfo (hostname,
929 NULL, &hints, &res))) || (res == NULL))
931 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
932 _("Failed to resolve `%s': %s\n"),
933 hostname, gai_strerror (ret));
934 GNUNET_free (hostname);
935 GNUNET_free (unixpath);
937 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
938 return GNUNET_SYSERR;
942 while (NULL != (pos = next))
945 if ( (disablev6) && (pos->ai_family == AF_INET6))
951 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
952 _("Failed to find %saddress for `%s'.\n"),
953 disablev6 ? "IPv4 " : "", hostname);
955 GNUNET_free (hostname);
956 GNUNET_free (unixpath);
957 return GNUNET_SYSERR;
960 if (NULL != unixpath)
962 saddrs = GNUNET_malloc ((resi+1) * sizeof(struct sockaddr*));
963 saddrlens = GNUNET_malloc ((resi+1) * sizeof (socklen_t));
965 if (NULL != unixpath)
967 add_unixpath (saddrs, saddrlens, unixpath);
971 while (NULL != (pos = next))
974 if ( (disablev6) && (pos->ai_family == AF_INET6))
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Service `%s' will bind to `%s'\n",
980 GNUNET_a2s (pos->ai_addr,
983 if (pos->ai_family == AF_INET)
985 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
986 saddrlens[i] = pos->ai_addrlen;
987 saddrs[i] = GNUNET_malloc (saddrlens[i]);
988 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
989 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
993 GNUNET_assert (pos->ai_family == AF_INET6);
994 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
995 saddrlens[i] = pos->ai_addrlen;
996 saddrs[i] = GNUNET_malloc (saddrlens[i]);
997 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
998 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1002 GNUNET_free (hostname);
1007 /* will bind against everything, just set port */
1012 if (NULL != unixpath)
1015 saddrs = GNUNET_malloc ((resi+1) * sizeof(struct sockaddr*));
1016 saddrlens = GNUNET_malloc ((resi+1) * sizeof (socklen_t));
1017 if (NULL != unixpath)
1019 add_unixpath (saddrs, saddrlens, unixpath);
1022 saddrlens[i] = sizeof (struct sockaddr_in);
1023 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1024 #if HAVE_SOCKADDR_IN_SIN_LEN
1025 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1027 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1028 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1034 if (NULL != unixpath)
1036 saddrs = GNUNET_malloc ((resi+1) * sizeof(struct sockaddr*));
1037 saddrlens = GNUNET_malloc ((resi+1) * sizeof (socklen_t));
1039 if (NULL != unixpath)
1041 add_unixpath (saddrs, saddrlens, unixpath);
1044 saddrlens[i] = sizeof (struct sockaddr_in6);
1045 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1046 #if HAVE_SOCKADDR_IN_SIN_LEN
1047 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1049 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1050 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1052 saddrlens[i] = sizeof (struct sockaddr_in);
1053 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1054 #if HAVE_SOCKADDR_IN_SIN_LEN
1055 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1057 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1058 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1061 GNUNET_free_non_null (unixpath);
1063 *addr_lens = saddrlens;
1065 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1071 * Setup addr, addrlen, maxbuf, idle_timeout
1072 * based on configuration!
1074 * Configuration may specify:
1075 * - PORT (where to bind to for TCP)
1076 * - UNIXPATH (where to bind to for UNIX domain sockets)
1077 * - TIMEOUT (after how many ms does an inactive service timeout);
1078 * - MAXBUF (maximum incoming message size supported)
1079 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1080 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1081 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1082 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1083 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1084 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1086 * @return GNUNET_OK if configuration succeeded
1089 setup_service (struct GNUNET_SERVICE_Context *sctx)
1091 unsigned long long maxbuf;
1092 struct GNUNET_TIME_Relative idleout;
1100 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1101 sctx->serviceName, "TIMEOUT"))
1104 GNUNET_CONFIGURATION_get_value_time (sctx->cfg,
1106 "TIMEOUT", &idleout))
1108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1109 _("Specified value for `%s' of service `%s' is invalid\n"),
1112 return GNUNET_SYSERR;
1114 sctx->timeout = idleout;
1117 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1118 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1119 sctx->serviceName, "MAXBUF"))
1122 GNUNET_CONFIGURATION_get_value_number (sctx->cfg,
1126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1127 _("Specified value for `%s' of service `%s' is invalid\n"),
1130 return GNUNET_SYSERR;
1134 maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1136 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1137 sctx->serviceName, "TOLERANT"))
1139 if (GNUNET_SYSERR ==
1140 (tolerant = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
1144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1145 _("Specified value for `%s' of service `%s' is invalid\n"),
1148 return GNUNET_SYSERR;
1152 tolerant = GNUNET_NO;
1156 if ( (NULL != (lpid = getenv ("LISTEN_PID"))) &&
1157 (1 == sscanf ("%u", lpid, &pid)) &&
1158 (getpid () == (pid_t) pid) &&
1159 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1160 (1 == sscanf ("%u", nfds, &cnt)) &&
1163 sctx->lsocks = GNUNET_malloc (sizeof(struct GNUNET_NETWORK_Handle*) * (cnt+1));
1166 flags = fcntl (3 + cnt, F_GETFD);
1168 (0 != (flags & FD_CLOEXEC)) ||
1169 (NULL == (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))) )
1171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1172 _("Could not access pre-bound socket %u, will try to bind myself\n"),
1173 (unsigned int) 3 +cnt);
1175 while (sctx->lsocks[cnt] != NULL)
1176 GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]);
1177 GNUNET_free (sctx->lsocks);
1178 sctx->lsocks = NULL;
1182 unsetenv ("LISTEN_PID");
1183 unsetenv ("LISTEN_FDS");
1187 if ( (sctx->lsocks == NULL) &&
1189 GNUNET_SERVICE_get_server_addresses (sctx->serviceName,
1193 return GNUNET_SYSERR;
1194 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1195 sctx->maxbuf = (size_t) maxbuf;
1196 if (sctx->maxbuf != maxbuf)
1198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1200 ("Value in configuration for `%s' and service `%s' too large!\n"),
1201 "MAXBUF", sctx->serviceName);
1202 return GNUNET_SYSERR;
1205 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1206 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1207 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1208 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1215 * Get the name of the user that'll be used
1216 * to provide the service.
1219 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1225 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
1236 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1244 if (NULL == (pif = get_pid_file_name (sctx)))
1245 return GNUNET_OK; /* no file desired */
1246 user = get_user_name (sctx);
1247 rdir = GNUNET_strdup (pif);
1248 len = strlen (rdir);
1249 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1252 if (0 != ACCESS (rdir, F_OK))
1254 /* we get to create a directory -- and claim it
1256 GNUNET_DISK_directory_create (rdir);
1257 if ((user != NULL) && (0 < strlen (user)))
1258 GNUNET_DISK_file_change_owner (rdir, user);
1260 if (0 != ACCESS (rdir, W_OK | X_OK))
1262 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1264 GNUNET_free_non_null (user);
1266 return GNUNET_SYSERR;
1269 pidfd = FOPEN (pif, "w");
1272 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1274 GNUNET_free_non_null (user);
1275 return GNUNET_SYSERR;
1277 if (0 > FPRINTF (pidfd, "%u", pid))
1278 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1279 GNUNET_break (0 == fclose (pidfd));
1280 if ((user != NULL) && (0 < strlen (user)))
1281 GNUNET_DISK_file_change_owner (pif, user);
1282 GNUNET_free_non_null (user);
1289 * Task run during shutdown.
1295 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1297 struct GNUNET_SERVER_Handle *server = cls;
1299 GNUNET_SERVER_destroy (server);
1304 * Initial task for the service.
1307 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1309 struct GNUNET_SERVICE_Context *sctx = cls;
1312 sctx->sched = tc->sched;
1313 if (sctx->lsocks != NULL)
1314 sctx->server = GNUNET_SERVER_create_with_sockets (tc->sched,
1319 sctx->timeout, sctx->require_found);
1321 sctx->server = GNUNET_SERVER_create (tc->sched,
1327 sctx->timeout, sctx->require_found);
1328 if (sctx->server == NULL)
1330 if (sctx->addrs != NULL)
1333 while (sctx->addrs[i] != NULL)
1335 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1336 _("Failed to start `%s' at `%s'\n"),
1338 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1342 sctx->ret = GNUNET_SYSERR;
1345 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1347 /* install a task that will kill the server
1348 process if the scheduler ever gets a shutdown signal */
1349 GNUNET_SCHEDULER_add_delayed (tc->sched,
1350 GNUNET_TIME_UNIT_FOREVER_REL,
1351 &shutdown_task, sctx->server);
1353 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1354 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1356 while ((sctx->my_handlers[i].callback != NULL))
1357 sctx->my_handlers[i++].callback_cls = sctx;
1358 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1359 if (sctx->ready_confirm_fd != -1)
1361 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1362 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1363 sctx->ready_confirm_fd = -1;
1364 write_pid_file (sctx, getpid ());
1366 if (sctx->addrs != NULL)
1369 while (sctx->addrs[i] != NULL)
1371 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1372 _("Service `%s' runs at %s\n"),
1374 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1378 sctx->task (sctx->task_cls, tc->sched, sctx->server, sctx->cfg);
1383 * Detach from terminal.
1386 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1393 if (0 != PIPE (filedes))
1395 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
1396 return GNUNET_SYSERR;
1401 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
1402 return GNUNET_SYSERR;
1409 GNUNET_break (0 == CLOSE (filedes[1]));
1411 if (1 != READ (filedes[0], &c, sizeof (char)))
1412 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1419 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1420 _("Service process failed to initialize\n"));
1423 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1425 ("Service process could not initialize server function\n"));
1428 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1429 _("Service process failed to report status\n"));
1432 exit (1); /* child reported error */
1434 GNUNET_break (0 == CLOSE (0));
1435 GNUNET_break (0 == CLOSE (1));
1436 GNUNET_break (0 == CLOSE (filedes[0]));
1437 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1439 return GNUNET_SYSERR;
1440 /* set stdin/stdout to /dev/null */
1441 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1443 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
1444 return GNUNET_SYSERR;
1446 /* Detach from controlling terminal */
1449 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsid");
1450 sctx->ready_confirm_fd = filedes[1];
1452 /* FIXME: we probably need to do something else
1453 elsewhere in order to fork the process itself... */
1464 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1468 if (NULL == (user = get_user_name (sctx)))
1469 return GNUNET_OK; /* keep */
1474 pws = getpwnam (user);
1477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1478 _("Cannot obtain information about user `%s': %s\n"),
1479 user, errno == 0 ? _("No such user") : STRERROR (errno));
1481 return GNUNET_SYSERR;
1483 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1485 (0 != initgroups (user, pws->pw_gid)) ||
1487 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1489 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1490 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1493 _("Cannot change user/group to `%s': %s\n"), user,
1496 return GNUNET_SYSERR;
1506 * Delete the PID file that was created by our parent.
1509 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1511 char *pif = get_pid_file_name (sctx);
1513 return; /* no PID file */
1514 if (0 != UNLINK (pif))
1515 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1521 * Run a standard GNUnet service startup sequence (initialize loggers
1522 * and configuration, parse options).
1524 * @param argc number of command line arguments
1525 * @param argv command line arguments
1526 * @param serviceName our service name
1527 * @param opt service options
1528 * @param task main task of the service
1529 * @param task_cls closure for task
1530 * @return GNUNET_SYSERR on error, GNUNET_OK
1531 * if we shutdown nicely
1534 GNUNET_SERVICE_run (int argc,
1536 const char *serviceName,
1537 enum GNUNET_SERVICE_Options opt,
1538 GNUNET_SERVICE_Main task, void *task_cls)
1540 #define HANDLE_ERROR do { err = 1; GNUNET_break (0); goto shutdown; } while (0)
1548 struct GNUNET_SERVICE_Context sctx;
1549 struct GNUNET_CONFIGURATION_Handle *cfg;
1550 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1551 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1552 {'d', "daemonize", NULL,
1553 gettext_noop ("do daemonize (detach from terminal)"), 0,
1554 GNUNET_GETOPT_set_one, &do_daemonize},
1555 GNUNET_GETOPT_OPTION_HELP (serviceName),
1556 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1557 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1558 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1559 GNUNET_GETOPT_OPTION_END
1564 loglev = GNUNET_strdup ("WARNING");
1565 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1566 memset (&sctx, 0, sizeof (sctx));
1568 sctx.ready_confirm_fd = -1;
1569 sctx.ret = GNUNET_OK;
1570 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1571 sctx.maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1573 sctx.serviceName = serviceName;
1574 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1575 /* setup subsystems */
1576 if (GNUNET_SYSERR == GNUNET_GETOPT_run (serviceName, service_options, argc,
1579 if (GNUNET_OK != GNUNET_log_setup (serviceName, loglev, logfile))
1581 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1583 if (GNUNET_OK != setup_service (&sctx))
1585 if ( (do_daemonize == 1) && (GNUNET_OK != detach_terminal (&sctx)))
1587 if (GNUNET_OK != set_user_id (&sctx))
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "Service `%s' runs with configuration from `%s'\n",
1592 serviceName, cfg_fn);
1594 /* actually run service */
1595 GNUNET_SCHEDULER_run (&service_task, &sctx);
1598 if ((do_daemonize == 1) && (sctx.server != NULL))
1599 pid_file_delete (&sctx);
1600 GNUNET_free_non_null (sctx.my_handlers);
1603 if (sctx.ready_confirm_fd != -1)
1605 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1606 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1607 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1610 GNUNET_CONFIGURATION_destroy (cfg);
1612 if (sctx.addrs != NULL)
1613 while (sctx.addrs[i] != NULL)
1614 GNUNET_free (sctx.addrs[i++]);
1615 GNUNET_free_non_null (sctx.addrs);
1616 GNUNET_free_non_null (sctx.addrlens);
1617 GNUNET_free_non_null (logfile);
1618 GNUNET_free (loglev);
1619 GNUNET_free (cfg_fn);
1620 GNUNET_free_non_null (sctx.v4_denied);
1621 GNUNET_free_non_null (sctx.v6_denied);
1622 GNUNET_free_non_null (sctx.v4_allowed);
1623 GNUNET_free_non_null (sctx.v6_allowed);
1625 return err ? GNUNET_SYSERR : sctx.ret;
1630 * Run a service startup sequence within an existing
1631 * initialized system.
1633 * @param serviceName our service name
1634 * @param sched scheduler to use
1635 * @param cfg configuration to use
1636 * @return NULL on error, service handle
1638 struct GNUNET_SERVICE_Context *
1639 GNUNET_SERVICE_start (const char *serviceName,
1640 struct GNUNET_SCHEDULER_Handle *sched,
1641 const struct GNUNET_CONFIGURATION_Handle *cfg)
1644 struct GNUNET_SERVICE_Context *sctx;
1646 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1647 sctx->ready_confirm_fd = -1; /* no daemonizing */
1648 sctx->ret = GNUNET_OK;
1649 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1650 sctx->maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1651 sctx->serviceName = serviceName;
1653 sctx->sched = sched;
1655 /* setup subsystems */
1656 if (GNUNET_OK != setup_service (sctx))
1658 GNUNET_SERVICE_stop (sctx);
1661 if (sctx->lsocks != NULL)
1662 sctx->server = GNUNET_SERVER_create_with_sockets (sched,
1667 sctx->timeout, sctx->require_found);
1669 sctx->server = GNUNET_SERVER_create (sched,
1676 sctx->require_found);
1678 if (NULL == sctx->server)
1680 GNUNET_SERVICE_stop (sctx);
1683 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1684 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1686 while ((sctx->my_handlers[i].callback != NULL))
1687 sctx->my_handlers[i++].callback_cls = sctx;
1688 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1693 * Obtain the server used by a service. Note that the server must NOT
1694 * be destroyed by the caller.
1696 * @param ctx the service context returned from the start function
1697 * @return handle to the server for this service, NULL if there is none
1699 struct GNUNET_SERVER_Handle *
1700 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1707 * Stop a service that was started with "GNUNET_SERVICE_start".
1709 * @param sctx the service context returned from the start function
1712 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1715 if (NULL != sctx->server)
1716 GNUNET_SERVER_destroy (sctx->server);
1717 GNUNET_free_non_null (sctx->my_handlers);
1718 if (sctx->addrs != NULL)
1721 while (sctx->addrs[i] != NULL)
1722 GNUNET_free (sctx->addrs[i++]);
1723 GNUNET_free (sctx->addrs);
1725 GNUNET_free_non_null (sctx->addrlens);
1726 GNUNET_free_non_null (sctx->v4_denied);
1727 GNUNET_free_non_null (sctx->v6_denied);
1728 GNUNET_free_non_null (sctx->v4_allowed);
1729 GNUNET_free_non_null (sctx->v6_allowed);
1734 /* end of service.c */