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.
440 struct sockaddr **addrs;
443 * Name of our service.
445 const char *serviceName;
448 * Main service-specific task to run.
450 GNUNET_SERVICE_Main task;
458 * IPv4 addresses that are not allowed to connect.
460 struct IPv4NetworkSet *v4_denied;
463 * IPv6 addresses that are not allowed to connect.
465 struct IPv6NetworkSet *v6_denied;
468 * IPv4 addresses that are allowed to connect (if not
469 * set, all are allowed).
471 struct IPv4NetworkSet *v4_allowed;
474 * IPv6 addresses that are allowed to connect (if not
475 * set, all are allowed).
477 struct IPv6NetworkSet *v6_allowed;
480 * My (default) message handlers. Adjusted copy
483 struct GNUNET_SERVER_MessageHandler *my_handlers;
486 * Idle timeout for server.
488 struct GNUNET_TIME_Relative timeout;
491 * Maximum buffer size for the server.
496 * Overall success/failure of the service start.
501 * If we are daemonizing, this FD is set to the
502 * pipe to the parent. Send '.' if we started
503 * ok, '!' if not. -1 if we are not daemonizing.
505 int ready_confirm_fd;
508 * Do we close connections if we receive messages
509 * for which we have no handler?
514 * Can clients ask us to initiate a shutdown?
521 enum GNUNET_SERVICE_Options options;
524 * Array of the lengths of the entries in addrs.
526 socklen_t * addrlens;
531 /* ****************** message handlers ****************** */
534 write_test (void *cls, size_t size, void *buf)
536 struct GNUNET_SERVER_Client *client = cls;
537 struct GNUNET_MessageHeader *msg;
539 if (size < sizeof (struct GNUNET_MessageHeader))
541 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
542 return 0; /* client disconnected */
544 msg = (struct GNUNET_MessageHeader *) buf;
545 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
546 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
547 GNUNET_SERVER_receive_done (client, GNUNET_OK);
548 return sizeof (struct GNUNET_MessageHeader);
552 * Handler for TEST message.
554 * @param cls closure (refers to service)
555 * @param client identification of the client
556 * @param message the actual message
559 handle_test (void *cls,
560 struct GNUNET_SERVER_Client *client,
561 const struct GNUNET_MessageHeader *message)
563 /* simply bounce message back to acknowledge */
564 if (NULL == GNUNET_SERVER_notify_transmit_ready (client,
566 GNUNET_MessageHeader),
567 GNUNET_TIME_UNIT_FOREVER_REL,
568 &write_test, client))
569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
574 transmit_shutdown_deny (void *cls, size_t size, void *buf)
576 struct GNUNET_SERVER_Client *client = cls;
577 struct GNUNET_MessageHeader *msg;
579 if (size < sizeof (struct GNUNET_MessageHeader))
581 return 0; /* client disconnected */
582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
584 msg = (struct GNUNET_MessageHeader *) buf;
585 msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN_REFUSE);
586 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
587 GNUNET_SERVER_receive_done (client, GNUNET_OK);
588 GNUNET_SERVER_client_drop(client);
589 return sizeof (struct GNUNET_MessageHeader);
593 transmit_shutdown_ack (void *cls, size_t size, void *buf)
595 struct GNUNET_SERVER_Client *client = cls;
596 struct GNUNET_MessageHeader *msg;
598 if (size < sizeof (struct GNUNET_MessageHeader))
600 return 0; /* client disconnected */
601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
604 msg = (struct GNUNET_MessageHeader *) buf;
605 msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN_ACK);
606 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
607 GNUNET_SERVER_receive_done (client, GNUNET_OK);
608 GNUNET_SERVER_client_drop(client);
609 return sizeof (struct GNUNET_MessageHeader);
613 * Handler for SHUTDOWN message.
615 * @param cls closure (refers to service)
616 * @param client identification of the client
617 * @param message the actual message
620 handle_shutdown (void *cls,
621 struct GNUNET_SERVER_Client *client,
622 const struct GNUNET_MessageHeader *message)
624 struct GNUNET_SERVICE_Context *service = cls;
626 /* FIXME: why is this call necessary???? */
627 GNUNET_SERVER_client_keep(client);
628 if (!service->allow_shutdown)
630 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
632 ("Received shutdown request, but configured to ignore!\n"));
633 GNUNET_SERVER_notify_transmit_ready (client,
634 sizeof(struct GNUNET_MessageHeader),
635 GNUNET_TIME_UNIT_FOREVER_REL,
636 &transmit_shutdown_deny, client);
639 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
640 _("Initiating shutdown as requested by client.\n"));
642 GNUNET_SERVER_notify_transmit_ready (client,
643 sizeof(struct GNUNET_MessageHeader),
644 GNUNET_TIME_UNIT_FOREVER_REL,
645 &transmit_shutdown_ack, client);
647 GNUNET_assert (service->sched != NULL);
648 GNUNET_SERVER_client_persist_ (client);
649 GNUNET_SCHEDULER_shutdown (service->sched);
654 * Default handlers for all services. Will be copied and the
655 * "callback_cls" fields will be replaced with the specific service
658 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
659 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
660 sizeof (struct GNUNET_MessageHeader)},
661 {&handle_shutdown, NULL, GNUNET_MESSAGE_TYPE_SHUTDOWN,
662 sizeof (struct GNUNET_MessageHeader)},
668 /* ****************** service core routines ************** */
672 * Check if access to the service is allowed from the given address.
675 check_access (void *cls, const struct sockaddr *addr, socklen_t addrlen)
677 struct GNUNET_SERVICE_Context *sctx = cls;
678 const struct sockaddr_in *i4;
679 const struct sockaddr_in6 *i6;
682 switch (addr->sa_family)
685 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
686 i4 = (const struct sockaddr_in *) addr;
687 ret = ((sctx->v4_allowed == NULL) ||
688 (check_ipv4_listed (sctx->v4_allowed,
690 && ((sctx->v4_denied == NULL) ||
691 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
694 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
695 i6 = (const struct sockaddr_in6 *) addr;
696 ret = ((sctx->v6_allowed == NULL) ||
697 (check_ipv6_listed (sctx->v6_allowed,
699 && ((sctx->v6_denied == NULL) ||
700 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
704 _("Unknown address family %d\n"), addr->sa_family);
705 return GNUNET_SYSERR;
707 if (ret != GNUNET_OK)
709 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
710 _("Access from `%s' denied to service `%s'\n"),
711 GNUNET_a2s (addr, addrlen), sctx->serviceName);
718 * Get the name of the file where we will
719 * write the PID of the service.
722 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
728 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
737 * Parse an IPv4 access control list.
740 process_acl4 (struct IPv4NetworkSet **ret,
741 struct GNUNET_SERVICE_Context *sctx, const char *option)
745 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
747 GNUNET_break (GNUNET_OK ==
748 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
751 if (NULL == (*ret = parse_ipv4_specification (opt)))
753 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
755 ("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
756 opt, sctx->serviceName, option);
758 return GNUNET_SYSERR;
766 * Parse an IPv4 access control list.
769 process_acl6 (struct IPv6NetworkSet **ret,
770 struct GNUNET_SERVICE_Context *sctx, const char *option)
773 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
775 GNUNET_break (GNUNET_OK ==
776 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
779 if (NULL == (*ret = parse_ipv6_specification (opt)))
781 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
783 ("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
784 opt, sctx->serviceName, option);
786 return GNUNET_SYSERR;
794 * Get the list of addresses that a server for the given service
797 * @param serviceName name of the service
798 * @param cfg configuration (which specifies the addresses)
799 * @param addrs set (call by reference) to an array of pointers to the
800 * addresses the server should bind to and listen on; the
801 * array will be NULL-terminated (on success)
802 * @param addr_lens set (call by reference) to an array of the lengths
803 * of the respective 'struct sockaddr' struct in the 'addrs'
805 * @return number of addresses found on success,
806 * GNUNET_SYSERR if the configuration
807 * did not specify reasonable finding information or
808 * if it specified a hostname that could not be resolved;
809 * GNUNET_NO if the number of addresses configured is
810 * zero (in this case, '*addrs' and '*addr_lens' will be
814 GNUNET_SERVICE_get_server_addresses (const char *serviceName,
815 const struct GNUNET_CONFIGURATION_Handle *cfg,
816 struct sockaddr ***addrs,
817 socklen_t **addr_lens)
820 struct GNUNET_NETWORK_Handle *desc;
821 unsigned long long port;
822 struct addrinfo hints;
823 struct addrinfo *res;
824 struct addrinfo *pos;
825 struct addrinfo *next;
829 struct sockaddr **saddrs;
830 socklen_t *saddrlens;
835 if (GNUNET_CONFIGURATION_have_value (cfg,
836 serviceName, "DISABLEV6"))
839 (disablev6 = GNUNET_CONFIGURATION_get_value_yesno (cfg,
842 return GNUNET_SYSERR;
845 disablev6 = GNUNET_NO;
849 /* probe IPv6 support */
850 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
853 if ((errno == ENOBUFS) ||
854 (errno == ENOMEM) || (errno == ENFILE) || (errno == EACCES))
856 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
857 return GNUNET_SYSERR;
859 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
861 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
862 serviceName, STRERROR (errno));
863 disablev6 = GNUNET_YES;
867 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
873 GNUNET_CONFIGURATION_get_value_number (cfg,
876 &port)) || (port > 65535))
878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
880 ("Require valid port number for service `%s' in configuration!\n"),
882 return GNUNET_SYSERR;
884 if (GNUNET_CONFIGURATION_have_value (cfg,
885 serviceName, "BINDTO"))
887 GNUNET_break (GNUNET_OK ==
888 GNUNET_CONFIGURATION_get_value_string (cfg,
896 if (hostname != NULL)
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Resolving `%s' since that is where `%s' will bind to.\n",
904 memset (&hints, 0, sizeof (struct addrinfo));
906 hints.ai_family = AF_INET;
907 if ((0 != (ret = getaddrinfo (hostname,
908 NULL, &hints, &res))) || (res == NULL))
910 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
911 _("Failed to resolve `%s': %s\n"),
912 hostname, gai_strerror (ret));
913 GNUNET_free (hostname);
914 return GNUNET_SYSERR;
918 while (NULL != (pos = next))
921 if ( (disablev6) && (pos->ai_family == AF_INET6))
927 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
928 _("Failed to find %saddress for `%s'.\n"),
929 disablev6 ? "IPv4 " : "", hostname);
931 GNUNET_free (hostname);
932 return GNUNET_SYSERR;
935 saddrs = GNUNET_malloc ((i+1) * sizeof(struct sockaddr*));
936 saddrlens = GNUNET_malloc ((i+1) * sizeof (socklen_t));
939 while (NULL != (pos = next))
942 if ( (disablev6) && (pos->ai_family == AF_INET6))
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "Service `%s' will bind to `%s'\n",
948 GNUNET_a2s (pos->ai_addr,
951 if (pos->ai_family == AF_INET)
953 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
954 saddrlens[i] = pos->ai_addrlen;
955 saddrs[i] = GNUNET_malloc (saddrlens[i]);
956 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
957 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
961 GNUNET_assert (pos->ai_family == AF_INET6);
962 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
963 saddrlens[i] = pos->ai_addrlen;
964 saddrs[i] = GNUNET_malloc (saddrlens[i]);
965 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
966 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
970 GNUNET_free (hostname);
975 /* will bind against everything, just set port */
980 saddrs = GNUNET_malloc (2 * sizeof(struct sockaddr*));
981 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
982 saddrlens[0] = sizeof (struct sockaddr_in);
983 saddrs[0] = GNUNET_malloc (saddrlens[0]);
984 #if HAVE_SOCKADDR_IN_SIN_LEN
985 ((struct sockaddr_in *) saddrs[0])->sin_len = saddrlens[0];
987 ((struct sockaddr_in *) saddrs[0])->sin_family = AF_INET;
988 ((struct sockaddr_in *) saddrs[0])->sin_port = htons (port);
994 saddrs = GNUNET_malloc (3 * sizeof(struct sockaddr*));
995 saddrlens = GNUNET_malloc (3 * sizeof (socklen_t));
997 saddrlens[0] = sizeof (struct sockaddr_in6);
998 saddrs[0] = GNUNET_malloc (saddrlens[0]);
999 #if HAVE_SOCKADDR_IN_SIN_LEN
1000 ((struct sockaddr_in6 *) saddrs[0])->sin6_len = saddrlens[0];
1002 ((struct sockaddr_in6 *) saddrs[0])->sin6_family = AF_INET6;
1003 ((struct sockaddr_in6 *) saddrs[0])->sin6_port = htons (port);
1005 saddrlens[1] = sizeof (struct sockaddr_in);
1006 saddrs[1] = GNUNET_malloc (saddrlens[1]);
1007 #if HAVE_SOCKADDR_IN_SIN_LEN
1008 ((struct sockaddr_in *) saddrs[1])->sin_len = saddrlens[1];
1010 ((struct sockaddr_in *) saddrs[1])->sin_family = AF_INET;
1011 ((struct sockaddr_in *) saddrs[1])->sin_port = htons (port);
1016 *addr_lens = saddrlens;
1022 * Setup addr, addrlen, maxbuf, idle_timeout
1023 * based on configuration!
1025 * Configuration must specify a "PORT". It may
1027 * - TIMEOUT (after how many ms does an inactive service timeout);
1028 * - MAXBUF (maximum incoming message size supported)
1029 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1030 * - ALLOW_SHUTDOWN (allow clients to shutdown this service)
1031 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1032 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1033 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1034 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1035 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1037 * @return GNUNET_OK if configuration succeeded
1040 setup_service (struct GNUNET_SERVICE_Context *sctx)
1042 unsigned long long maxbuf;
1043 struct GNUNET_TIME_Relative idleout;
1046 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1047 sctx->serviceName, "TIMEOUT"))
1050 GNUNET_CONFIGURATION_get_value_time (sctx->cfg,
1052 "TIMEOUT", &idleout))
1054 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1055 _("Specified value for `%s' of service `%s' is invalid\n"),
1058 return GNUNET_SYSERR;
1060 sctx->timeout = idleout;
1063 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1064 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1065 sctx->serviceName, "MAXBUF"))
1068 GNUNET_CONFIGURATION_get_value_number (sctx->cfg,
1072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1073 _("Specified value for `%s' of service `%s' is invalid\n"),
1076 return GNUNET_SYSERR;
1080 maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1081 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1082 sctx->serviceName, "ALLOW_SHUTDOWN"))
1084 if (GNUNET_SYSERR ==
1085 (sctx->allow_shutdown =
1086 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1089 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1090 _("Specified value for `%s' of service `%s' is invalid\n"),
1093 return GNUNET_SYSERR;
1097 sctx->allow_shutdown = GNUNET_NO;
1100 if (GNUNET_CONFIGURATION_have_value (sctx->cfg,
1101 sctx->serviceName, "TOLERANT"))
1103 if (GNUNET_SYSERR ==
1104 (tolerant = GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg,
1108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1109 _("Specified value for `%s' of service `%s' is invalid\n"),
1112 return GNUNET_SYSERR;
1116 tolerant = GNUNET_NO;
1118 if (GNUNET_SYSERR ==
1119 GNUNET_SERVICE_get_server_addresses (sctx->serviceName,
1123 return GNUNET_SYSERR;
1124 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1125 sctx->maxbuf = (size_t) maxbuf;
1126 if (sctx->maxbuf != maxbuf)
1128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1130 ("Value in configuration for `%s' and service `%s' too large!\n"),
1131 "MAXBUF", sctx->serviceName);
1132 return GNUNET_SYSERR;
1135 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1136 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1137 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1138 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1145 * Get the name of the user that'll be used
1146 * to provide the service.
1149 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1155 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg,
1166 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1174 if (NULL == (pif = get_pid_file_name (sctx)))
1175 return GNUNET_OK; /* no file desired */
1176 user = get_user_name (sctx);
1177 rdir = GNUNET_strdup (pif);
1178 len = strlen (rdir);
1179 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1182 if (0 != ACCESS (rdir, F_OK))
1184 /* we get to create a directory -- and claim it
1186 GNUNET_DISK_directory_create (rdir);
1187 if ((user != NULL) && (0 < strlen (user)))
1188 GNUNET_DISK_file_change_owner (rdir, user);
1190 if (0 != ACCESS (rdir, W_OK | X_OK))
1192 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1194 GNUNET_free_non_null (user);
1196 return GNUNET_SYSERR;
1199 pidfd = FOPEN (pif, "w");
1202 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1204 GNUNET_free_non_null (user);
1205 return GNUNET_SYSERR;
1207 if (0 > FPRINTF (pidfd, "%u", pid))
1208 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1209 GNUNET_break (0 == fclose (pidfd));
1210 if ((user != NULL) && (0 < strlen (user)))
1211 GNUNET_DISK_file_change_owner (pif, user);
1212 GNUNET_free_non_null (user);
1219 * Task run during shutdown.
1225 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1227 struct GNUNET_SERVER_Handle *server = cls;
1229 GNUNET_SERVER_destroy (server);
1234 * Initial task for the service.
1237 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1239 struct GNUNET_SERVICE_Context *sctx = cls;
1242 sctx->sched = tc->sched;
1243 sctx->server = GNUNET_SERVER_create (tc->sched,
1249 sctx->timeout, sctx->require_found);
1250 if (sctx->server == NULL)
1253 while (sctx->addrs[i] != NULL)
1255 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1256 _("Failed to start `%s' at `%s'\n"),
1258 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1261 sctx->ret = GNUNET_SYSERR;
1264 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1266 /* install a task that will kill the server
1267 process if the scheduler ever gets a shutdown signal */
1268 GNUNET_SCHEDULER_add_delayed (tc->sched,
1269 GNUNET_TIME_UNIT_FOREVER_REL,
1270 &shutdown_task, sctx->server);
1272 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1273 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1275 while ((sctx->my_handlers[i].callback != NULL))
1276 sctx->my_handlers[i++].callback_cls = sctx;
1277 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1278 if (sctx->ready_confirm_fd != -1)
1280 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1281 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1282 sctx->ready_confirm_fd = -1;
1283 write_pid_file (sctx, getpid ());
1286 while (sctx->addrs[i] != NULL)
1288 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1289 _("Service `%s' runs at %s\n"),
1291 GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1294 sctx->task (sctx->task_cls, tc->sched, sctx->server, sctx->cfg);
1299 * Detach from terminal.
1302 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1309 if (0 != PIPE (filedes))
1311 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
1312 return GNUNET_SYSERR;
1317 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "fork");
1318 return GNUNET_SYSERR;
1325 GNUNET_break (0 == CLOSE (filedes[1]));
1327 if (1 != READ (filedes[0], &c, sizeof (char)))
1328 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1335 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1336 _("Service process failed to initialize\n"));
1339 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1341 ("Service process could not initialize server function\n"));
1344 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1345 _("Service process failed to report status\n"));
1348 exit (1); /* child reported error */
1350 GNUNET_break (0 == CLOSE (0));
1351 GNUNET_break (0 == CLOSE (1));
1352 GNUNET_break (0 == CLOSE (filedes[0]));
1353 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1355 return GNUNET_SYSERR;
1356 /* set stdin/stdout to /dev/null */
1357 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1359 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup2");
1360 return GNUNET_SYSERR;
1362 /* Detach from controlling terminal */
1365 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "setsid");
1366 sctx->ready_confirm_fd = filedes[1];
1368 /* FIXME: we probably need to do something else
1369 elsewhere in order to fork the process itself... */
1380 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1384 if (NULL == (user = get_user_name (sctx)))
1385 return GNUNET_OK; /* keep */
1390 pws = getpwnam (user);
1393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1394 _("Cannot obtain information about user `%s': %s\n"),
1395 user, errno == 0 ? _("No such user") : STRERROR (errno));
1397 return GNUNET_SYSERR;
1399 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1401 (0 != initgroups (user, pws->pw_gid)) ||
1403 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1405 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1406 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1409 _("Cannot change user/group to `%s': %s\n"), user,
1412 return GNUNET_SYSERR;
1422 * Delete the PID file that was created by our parent.
1425 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1427 char *pif = get_pid_file_name (sctx);
1429 return; /* no PID file */
1430 if (0 != UNLINK (pif))
1431 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1437 * Run a standard GNUnet service startup sequence (initialize loggers
1438 * and configuration, parse options).
1440 * @param argc number of command line arguments
1441 * @param argv command line arguments
1442 * @param serviceName our service name
1443 * @param opt service options
1444 * @param task main task of the service
1445 * @param task_cls closure for task
1446 * @return GNUNET_SYSERR on error, GNUNET_OK
1447 * if we shutdown nicely
1450 GNUNET_SERVICE_run (int argc,
1452 const char *serviceName,
1453 enum GNUNET_SERVICE_Options opt,
1454 GNUNET_SERVICE_Main task, void *task_cls)
1456 #define HANDLE_ERROR do { err = 1; GNUNET_break (0); goto shutdown; } while (0)
1464 struct GNUNET_SERVICE_Context sctx;
1465 struct GNUNET_CONFIGURATION_Handle *cfg;
1466 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1467 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1468 {'d', "daemonize", NULL,
1469 gettext_noop ("do daemonize (detach from terminal)"), 0,
1470 GNUNET_GETOPT_set_one, &do_daemonize},
1471 GNUNET_GETOPT_OPTION_HELP (serviceName),
1472 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1473 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1474 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1475 GNUNET_GETOPT_OPTION_END
1480 loglev = GNUNET_strdup ("WARNING");
1481 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1482 memset (&sctx, 0, sizeof (sctx));
1484 sctx.ready_confirm_fd = -1;
1485 sctx.ret = GNUNET_OK;
1486 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1487 sctx.maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1489 sctx.serviceName = serviceName;
1490 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1491 /* setup subsystems */
1492 if (GNUNET_SYSERR == GNUNET_GETOPT_run (serviceName, service_options, argc,
1495 if (GNUNET_OK != GNUNET_log_setup (serviceName, loglev, logfile))
1497 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1499 if (GNUNET_OK != setup_service (&sctx))
1501 if ( (do_daemonize == 1) && (GNUNET_OK != detach_terminal (&sctx)))
1503 if (GNUNET_OK != set_user_id (&sctx))
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "Service `%s' runs with configuration from `%s'\n",
1508 serviceName, cfg_fn);
1510 /* actually run service */
1511 GNUNET_SCHEDULER_run (&service_task, &sctx);
1514 if ((do_daemonize == 1) && (sctx.server != NULL))
1515 pid_file_delete (&sctx);
1516 GNUNET_free_non_null (sctx.my_handlers);
1519 if (sctx.ready_confirm_fd != -1)
1521 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1522 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1523 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1526 GNUNET_CONFIGURATION_destroy (cfg);
1528 if (sctx.addrs != NULL)
1529 while (sctx.addrs[i] != NULL)
1530 GNUNET_free (sctx.addrs[i++]);
1531 GNUNET_free_non_null (sctx.addrs);
1532 GNUNET_free_non_null (sctx.addrlens);
1533 GNUNET_free_non_null (logfile);
1534 GNUNET_free (loglev);
1535 GNUNET_free (cfg_fn);
1536 GNUNET_free_non_null (sctx.v4_denied);
1537 GNUNET_free_non_null (sctx.v6_denied);
1538 GNUNET_free_non_null (sctx.v4_allowed);
1539 GNUNET_free_non_null (sctx.v6_allowed);
1541 return err ? GNUNET_SYSERR : sctx.ret;
1546 * Run a service startup sequence within an existing
1547 * initialized system.
1549 * @param serviceName our service name
1550 * @param sched scheduler to use
1551 * @param cfg configuration to use
1552 * @return NULL on error, service handle
1554 struct GNUNET_SERVICE_Context *
1555 GNUNET_SERVICE_start (const char *serviceName,
1556 struct GNUNET_SCHEDULER_Handle *sched,
1557 const struct GNUNET_CONFIGURATION_Handle *cfg)
1560 struct GNUNET_SERVICE_Context *sctx;
1562 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1563 sctx->ready_confirm_fd = -1; /* no daemonizing */
1564 sctx->ret = GNUNET_OK;
1565 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1566 sctx->maxbuf = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1567 sctx->serviceName = serviceName;
1569 sctx->sched = sched;
1571 /* setup subsystems */
1572 if ((GNUNET_OK != setup_service (sctx)) ||
1573 (NULL == (sctx->server = GNUNET_SERVER_create (sched,
1580 sctx->require_found))))
1582 GNUNET_SERVICE_stop (sctx);
1585 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1586 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1588 while ((sctx->my_handlers[i].callback != NULL))
1589 sctx->my_handlers[i++].callback_cls = sctx;
1590 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1595 * Obtain the server used by a service. Note that the server must NOT
1596 * be destroyed by the caller.
1598 * @param ctx the service context returned from the start function
1599 * @return handle to the server for this service, NULL if there is none
1601 struct GNUNET_SERVER_Handle *
1602 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1609 * Stop a service that was started with "GNUNET_SERVICE_start".
1611 * @param sctx the service context returned from the start function
1614 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1617 if (NULL != sctx->server)
1618 GNUNET_SERVER_destroy (sctx->server);
1619 GNUNET_free_non_null (sctx->my_handlers);
1621 while (sctx->addrs[i] != NULL)
1622 GNUNET_free (sctx->addrs[i++]);
1623 GNUNET_free_non_null (sctx->addrs);
1624 GNUNET_free_non_null (sctx->addrlens);
1625 GNUNET_free_non_null (sctx->v4_denied);
1626 GNUNET_free_non_null (sctx->v6_denied);
1627 GNUNET_free_non_null (sctx->v4_allowed);
1628 GNUNET_free_non_null (sctx->v6_allowed);
1633 /* end of service.c */