2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/service.c
23 * @brief functions related to starting services
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_directories.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_service_lib.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
41 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
43 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
45 #define DEBUG_SERVICE GNUNET_EXTRA_LOGGING
47 /* ******************* access control ******************** */
50 * @brief IPV4 network in CIDR notation.
54 struct in_addr network;
55 struct in_addr netmask;
59 * @brief network in CIDR notation for IPV6.
63 struct in6_addr network;
64 struct in6_addr netmask;
69 * Parse a network specification. The argument specifies
70 * a list of networks. The format is
71 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
72 * with a semicolon). The network must be given in dotted-decimal
73 * notation. The netmask can be given in CIDR notation (/16) or
74 * in dotted-decimal (/255.255.0.0).
76 * @param routeList a string specifying the forbidden networks
77 * @return the converted list, NULL if the synatx is flawed
79 static struct IPv4NetworkSet *
80 parse_ipv4_specification (const char *routeList)
88 unsigned int temps[8];
90 struct IPv4NetworkSet *result;
92 if (routeList == NULL)
94 len = strlen (routeList);
98 for (i = 0; i < len; i++)
99 if (routeList[i] == ';')
101 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
102 /* add termination */
103 memset (result, 0, sizeof (struct IPv4NetworkSet) * (count + 1));
109 sscanf (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
110 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
111 &temps[6], &temps[7]);
114 for (j = 0; j < 8; j++)
117 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
119 GNUNET_free (result);
122 result[i].network.s_addr =
123 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
125 result[i].netmask.s_addr =
126 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
128 while (routeList[pos] != ';')
134 /* try second notation */
136 sscanf (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
137 &temps[2], &temps[3], &slash);
140 for (j = 0; j < 4; j++)
143 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
145 GNUNET_free (result);
148 result[i].network.s_addr =
149 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
151 if ((slash <= 32) && (slash >= 0))
153 result[i].netmask.s_addr = 0;
156 result[i].netmask.s_addr =
157 (result[i].netmask.s_addr >> 1) + 0x80000000;
160 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
161 while (routeList[pos] != ';')
169 LOG (GNUNET_ERROR_TYPE_ERROR,
170 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
172 GNUNET_free (result);
173 return NULL; /* error */
176 /* try third notation */
179 sscanf (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
180 &temps[2], &temps[3]);
183 for (j = 0; j < 4; j++)
186 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
188 GNUNET_free (result);
191 result[i].network.s_addr =
192 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
194 result[i].netmask.s_addr = 0;
197 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
200 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
201 while (routeList[pos] != ';')
207 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
209 GNUNET_free (result);
210 return NULL; /* error */
212 if (pos < strlen (routeList))
214 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
216 GNUNET_free (result);
217 return NULL; /* oops */
219 return result; /* ok */
224 * Parse a network specification. The argument specifies
225 * a list of networks. The format is
226 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
227 * with a semicolon). The network must be given in colon-hex
228 * notation. The netmask must be given in CIDR notation (/16) or
229 * can be omitted to specify a single host.
231 * @param routeListX a string specifying the forbidden networks
232 * @return the converted list, NULL if the synatx is flawed
234 static struct IPv6NetworkSet *
235 parse_ipv6_specification (const char *routeListX)
245 struct IPv6NetworkSet *result;
250 if (routeListX == NULL)
252 len = strlen (routeListX);
255 routeList = GNUNET_strdup (routeListX);
257 for (i = 0; i < len; i++)
258 if (routeList[i] == ';')
260 if (routeList[len - 1] != ';')
262 LOG (GNUNET_ERROR_TYPE_ERROR,
263 _("Invalid network notation (does not end with ';': `%s')\n"),
265 GNUNET_free (routeList);
269 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
270 memset (result, 0, sizeof (struct IPv6NetworkSet) * (count + 1));
276 while (routeList[pos] != ';')
279 while ((slash >= start) && (routeList[slash] != '/'))
283 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
288 routeList[pos] = '\0';
289 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
293 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
296 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"),
297 &routeList[slash + 1]);
301 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
303 GNUNET_free (result);
304 GNUNET_free (routeList);
310 result[i].netmask.s6_addr[off++] = 0xFF;
315 result[i].netmask.s6_addr[off] =
316 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
321 routeList[slash] = '\0';
322 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
326 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"),
327 &routeList[slash + 1]);
329 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
330 GNUNET_free (result);
331 GNUNET_free (routeList);
337 GNUNET_free (routeList);
343 * Check if the given IP address is in the list of IP addresses.
345 * @param list a list of networks
346 * @param add the IP to check (in network byte order)
347 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
350 check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
358 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
360 if ((add->s_addr & list[i].netmask.s_addr) ==
361 (list[i].network.s_addr & list[i].netmask.s_addr))
369 * Check if the given IP address is in the list of IP addresses.
371 * @param list a list of networks
372 * @param ip the IP to check (in network byte order)
373 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
376 check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
380 struct in6_addr zero;
385 memset (&zero, 0, sizeof (struct in6_addr));
388 while (memcmp (&zero, &list[i].network, sizeof (struct in6_addr)) != 0)
390 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
391 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
392 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
403 /* ****************** service struct ****************** */
407 * Context for "service_task".
409 struct GNUNET_SERVICE_Context
414 const struct GNUNET_CONFIGURATION_Handle *cfg;
417 * Handle for the server.
419 struct GNUNET_SERVER_Handle *server;
422 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
425 struct sockaddr **addrs;
428 * Name of our service.
430 const char *serviceName;
433 * Main service-specific task to run.
435 GNUNET_SERVICE_Main task;
443 * IPv4 addresses that are not allowed to connect.
445 struct IPv4NetworkSet *v4_denied;
448 * IPv6 addresses that are not allowed to connect.
450 struct IPv6NetworkSet *v6_denied;
453 * IPv4 addresses that are allowed to connect (if not
454 * set, all are allowed).
456 struct IPv4NetworkSet *v4_allowed;
459 * IPv6 addresses that are allowed to connect (if not
460 * set, all are allowed).
462 struct IPv6NetworkSet *v6_allowed;
465 * My (default) message handlers. Adjusted copy
468 struct GNUNET_SERVER_MessageHandler *my_handlers;
471 * Array of the lengths of the entries in addrs.
476 * NULL-terminated array of listen sockets we should take over.
478 struct GNUNET_NETWORK_Handle **lsocks;
481 * Idle timeout for server.
483 struct GNUNET_TIME_Relative timeout;
486 * Overall success/failure of the service start.
491 * If we are daemonizing, this FD is set to the
492 * pipe to the parent. Send '.' if we started
493 * ok, '!' if not. -1 if we are not daemonizing.
495 int ready_confirm_fd;
498 * Do we close connections if we receive messages
499 * for which we have no handler?
504 * Do we require a matching UID for UNIX domain socket
510 * Do we require a matching GID for UNIX domain socket
518 enum GNUNET_SERVICE_Options options;
523 /* ****************** message handlers ****************** */
526 write_test (void *cls, size_t size, void *buf)
528 struct GNUNET_SERVER_Client *client = cls;
529 struct GNUNET_MessageHeader *msg;
531 if (size < sizeof (struct GNUNET_MessageHeader))
533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
534 return 0; /* client disconnected */
536 msg = (struct GNUNET_MessageHeader *) buf;
537 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
538 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
539 GNUNET_SERVER_receive_done (client, GNUNET_OK);
540 return sizeof (struct GNUNET_MessageHeader);
544 * Handler for TEST message.
546 * @param cls closure (refers to service)
547 * @param client identification of the client
548 * @param message the actual message
551 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
552 const struct GNUNET_MessageHeader *message)
554 /* simply bounce message back to acknowledge */
556 GNUNET_SERVER_notify_transmit_ready (client,
557 sizeof (struct 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 * @param uc credentials, if available, otherwise NULL
585 * @param addr address
586 * @param addrlen length of address
587 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
588 * for unknown address family (will be denied).
591 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
592 const struct sockaddr *addr, socklen_t addrlen)
594 struct GNUNET_SERVICE_Context *sctx = cls;
595 const struct sockaddr_in *i4;
596 const struct sockaddr_in6 *i6;
599 switch (addr->sa_family)
602 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
603 i4 = (const struct sockaddr_in *) addr;
604 ret = ((sctx->v4_allowed == NULL) ||
605 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
606 ((sctx->v4_denied == NULL) ||
607 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
610 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
611 i6 = (const struct sockaddr_in6 *) addr;
612 ret = ((sctx->v6_allowed == NULL) ||
613 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
614 ((sctx->v6_denied == NULL) ||
615 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
619 ret = GNUNET_OK; /* always OK for now */
620 if ((sctx->match_uid == GNUNET_YES) || (sctx->match_gid == GNUNET_YES))
623 ((sctx->match_uid != GNUNET_YES) || (uc->uid == geteuid ()) ||
624 (uc->uid == getuid ())) && ((sctx->match_gid != GNUNET_YES) ||
625 (uc->gid == getegid ()) ||
626 (uc->gid == getgid ())))
629 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
630 (uc == NULL) ? -1 : uc->uid, (uc == NULL) ? -1 : uc->gid);
634 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
636 return GNUNET_SYSERR;
638 if (ret != GNUNET_OK)
640 LOG (GNUNET_ERROR_TYPE_WARNING,
641 _("Access from `%s' denied to service `%s'\n"), GNUNET_a2s (addr,
650 * Get the name of the file where we will
651 * write the PID of the service.
654 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
660 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
668 * Parse an IPv4 access control list.
671 process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
676 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
678 GNUNET_break (GNUNET_OK ==
679 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
682 if (NULL == (*ret = parse_ipv4_specification (opt)))
684 LOG (GNUNET_ERROR_TYPE_WARNING,
685 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
686 opt, sctx->serviceName, option);
688 return GNUNET_SYSERR;
696 * Parse an IPv4 access control list.
699 process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
704 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, option))
706 GNUNET_break (GNUNET_OK ==
707 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
710 if (NULL == (*ret = parse_ipv6_specification (opt)))
712 LOG (GNUNET_ERROR_TYPE_WARNING,
713 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
714 opt, sctx->serviceName, option);
716 return GNUNET_SYSERR;
723 * Add the given UNIX domain path as an address to the
724 * list (as the first entry).
726 * @param saddrs array to update
727 * @param saddrlens where to store the address length
728 * @param unixpath path to add
731 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
732 const char *unixpath)
735 struct sockaddr_un *un;
738 un = GNUNET_malloc (sizeof (struct sockaddr_un));
739 un->sun_family = AF_UNIX;
740 slen = strlen (unixpath) + 1;
741 if (slen >= sizeof (un->sun_path))
742 slen = sizeof (un->sun_path) - 1;
743 memcpy (un->sun_path, unixpath, slen);
744 un->sun_path[slen] = '\0';
745 slen = sizeof (struct sockaddr_un);
747 un->sun_path[0] = '\0';
749 #if HAVE_SOCKADDR_IN_SIN_LEN
750 un->sun_len = (u_char) slen;
752 *saddrs = (struct sockaddr *) un;
755 /* this function should never be called
756 * unless AF_UNIX is defined! */
763 * Get the list of addresses that a server for the given service
766 * @param serviceName name of the service
767 * @param cfg configuration (which specifies the addresses)
768 * @param addrs set (call by reference) to an array of pointers to the
769 * addresses the server should bind to and listen on; the
770 * array will be NULL-terminated (on success)
771 * @param addr_lens set (call by reference) to an array of the lengths
772 * of the respective 'struct sockaddr' struct in the 'addrs'
774 * @return number of addresses found on success,
775 * GNUNET_SYSERR if the configuration
776 * did not specify reasonable finding information or
777 * if it specified a hostname that could not be resolved;
778 * GNUNET_NO if the number of addresses configured is
779 * zero (in this case, '*addrs' and '*addr_lens' will be
783 GNUNET_SERVICE_get_server_addresses (const char *serviceName,
784 const struct GNUNET_CONFIGURATION_Handle
785 *cfg, struct sockaddr ***addrs,
786 socklen_t ** addr_lens)
789 struct GNUNET_NETWORK_Handle *desc;
790 unsigned long long port;
792 struct addrinfo hints;
793 struct addrinfo *res;
794 struct addrinfo *pos;
795 struct addrinfo *next;
799 struct sockaddr **saddrs;
800 socklen_t *saddrlens;
806 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "DISABLEV6"))
810 GNUNET_CONFIGURATION_get_value_yesno (cfg, serviceName, "DISABLEV6")))
811 return GNUNET_SYSERR;
814 disablev6 = GNUNET_NO;
818 /* probe IPv6 support */
819 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
822 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
825 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
826 return GNUNET_SYSERR;
828 LOG (GNUNET_ERROR_TYPE_INFO,
830 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
831 serviceName, STRERROR (errno));
832 disablev6 = GNUNET_YES;
836 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
842 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
844 GNUNET_break (GNUNET_OK ==
845 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
849 LOG (GNUNET_ERROR_TYPE_ERROR,
850 _("Require valid port number for service `%s' in configuration!\n"),
852 return GNUNET_SYSERR;
856 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
858 GNUNET_break (GNUNET_OK ==
859 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
860 "BINDTO", &hostname));
868 GNUNET_CONFIGURATION_have_value (cfg, serviceName, "UNIXPATH")) &&
870 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName, "UNIXPATH",
872 (0 < strlen (unixpath)))
874 /* probe UNIX support */
875 struct sockaddr_un s_un;
877 if (strlen (unixpath) >= sizeof (s_un.sun_path))
879 LOG (GNUNET_ERROR_TYPE_WARNING,
880 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
881 sizeof (s_un.sun_path));
882 GNUNET_free_non_null (hostname);
883 GNUNET_free (unixpath);
884 return GNUNET_SYSERR;
887 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
890 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
893 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
894 GNUNET_free_non_null (hostname);
895 GNUNET_free (unixpath);
896 return GNUNET_SYSERR;
898 LOG (GNUNET_ERROR_TYPE_INFO,
900 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
901 serviceName, STRERROR (errno));
902 GNUNET_free (unixpath);
907 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
913 if ((port == 0) && (unixpath == NULL))
915 LOG (GNUNET_ERROR_TYPE_ERROR,
917 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
919 GNUNET_free_non_null (hostname);
920 return GNUNET_SYSERR;
924 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
925 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
926 add_unixpath (saddrs, saddrlens, unixpath);
927 GNUNET_free_non_null (unixpath);
928 GNUNET_free_non_null (hostname);
930 *addr_lens = saddrlens;
934 if (hostname != NULL)
937 LOG (GNUNET_ERROR_TYPE_DEBUG,
938 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
941 memset (&hints, 0, sizeof (struct addrinfo));
943 hints.ai_family = AF_INET;
944 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
947 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
949 GNUNET_free (hostname);
950 GNUNET_free_non_null (unixpath);
951 return GNUNET_SYSERR;
955 while (NULL != (pos = next))
958 if ((disablev6) && (pos->ai_family == AF_INET6))
964 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
965 disablev6 ? "IPv4 " : "", hostname);
967 GNUNET_free (hostname);
968 GNUNET_free_non_null (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))
988 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
989 continue; /* not TCP */
990 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
993 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
994 serviceName, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
996 if (pos->ai_family == AF_INET)
998 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
999 saddrlens[i] = pos->ai_addrlen;
1000 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1001 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1002 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1006 GNUNET_assert (pos->ai_family == AF_INET6);
1007 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
1008 saddrlens[i] = pos->ai_addrlen;
1009 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1010 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1011 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1015 GNUNET_free (hostname);
1021 /* will bind against everything, just set port */
1026 if (NULL != unixpath)
1029 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1030 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1031 if (NULL != unixpath)
1033 add_unixpath (saddrs, saddrlens, unixpath);
1036 saddrlens[i] = sizeof (struct sockaddr_in);
1037 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1038 #if HAVE_SOCKADDR_IN_SIN_LEN
1039 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1041 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1042 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1048 if (NULL != unixpath)
1050 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1051 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1053 if (NULL != unixpath)
1055 add_unixpath (saddrs, saddrlens, unixpath);
1058 saddrlens[i] = sizeof (struct sockaddr_in6);
1059 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1060 #if HAVE_SOCKADDR_IN_SIN_LEN
1061 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1063 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1064 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1066 saddrlens[i] = sizeof (struct sockaddr_in);
1067 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1068 #if HAVE_SOCKADDR_IN_SIN_LEN
1069 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1071 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1072 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1075 GNUNET_free_non_null (unixpath);
1077 *addr_lens = saddrlens;
1084 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1085 * and GNUNET_SYSERR on error.
1088 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1090 const char *env_buf;
1095 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1096 if ((env_buf == NULL) || (strlen (env_buf) <= 0))
1100 /* Using W32 API directly here, because this pipe will
1101 * never be used outside of this function, and it's just too much of a bother
1102 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1104 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1105 if (lsocks_pipe == 0 || lsocks_pipe == INVALID_HANDLE_VALUE)
1115 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1116 if (ret == 0 || rd != sizeof (count) || count == 0)
1119 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
1122 for (i = 0; i < count; i++)
1124 WSAPROTOCOL_INFOA pi;
1127 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1128 if (ret == 0 || rd != sizeof (size) || size != sizeof (pi))
1130 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1131 if (ret == 0 || rd != sizeof (pi))
1133 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1134 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1135 if (sctx->lsocks[i] == NULL)
1137 else if (i == count - 1)
1142 sctx->lsocks[count] = NULL;
1147 CloseHandle (lsocks_pipe);
1151 LOG (GNUNET_ERROR_TYPE_ERROR,
1152 _("Could not access a pre-bound socket, will try to bind myself\n"));
1153 for (i = 0; i < count && sctx->lsocks[i] != NULL; i++)
1154 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1155 GNUNET_free_non_null (sctx->lsocks);
1156 sctx->lsocks = NULL;
1166 * Setup addr, addrlen, idle_timeout
1167 * based on configuration!
1169 * Configuration may specify:
1170 * - PORT (where to bind to for TCP)
1171 * - UNIXPATH (where to bind to for UNIX domain sockets)
1172 * - TIMEOUT (after how many ms does an inactive service timeout);
1173 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1174 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1175 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1176 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1177 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1178 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1180 * @return GNUNET_OK if configuration succeeded
1183 setup_service (struct GNUNET_SERVICE_Context *sctx)
1185 struct GNUNET_TIME_Relative idleout;
1196 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->serviceName, "TIMEOUT"))
1199 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->serviceName,
1200 "TIMEOUT", &idleout))
1202 LOG (GNUNET_ERROR_TYPE_ERROR,
1203 _("Specified value for `%s' of service `%s' is invalid\n"),
1204 "TIMEOUT", sctx->serviceName);
1205 return GNUNET_SYSERR;
1207 sctx->timeout = idleout;
1210 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1212 if (GNUNET_CONFIGURATION_have_value
1213 (sctx->cfg, sctx->serviceName, "TOLERANT"))
1215 if (GNUNET_SYSERR ==
1217 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1220 LOG (GNUNET_ERROR_TYPE_ERROR,
1221 _("Specified value for `%s' of service `%s' is invalid\n"),
1222 "TOLERANT", sctx->serviceName);
1223 return GNUNET_SYSERR;
1227 tolerant = GNUNET_NO;
1231 if ((NULL != (lpid = getenv ("LISTEN_PID"))) &&
1232 (1 == sscanf (lpid, "%u", &pid)) && (getpid () == (pid_t) pid) &&
1233 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1234 (1 == sscanf (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1235 (cnt + 4 < FD_SETSIZE))
1238 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1241 flags = fcntl (3 + cnt, F_GETFD);
1242 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1244 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1246 LOG (GNUNET_ERROR_TYPE_ERROR,
1248 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1249 (unsigned int) 3 + cnt);
1251 while (sctx->lsocks[cnt] != NULL)
1252 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1253 GNUNET_free (sctx->lsocks);
1254 sctx->lsocks = NULL;
1258 unsetenv ("LISTEN_PID");
1259 unsetenv ("LISTEN_FDS");
1262 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1264 receive_sockets_from_parent (sctx);
1265 putenv ("GNUNET_OS_READ_LSOCKS=");
1269 if ((sctx->lsocks == NULL) &&
1271 GNUNET_SERVICE_get_server_addresses (sctx->serviceName, sctx->cfg,
1272 &sctx->addrs, &sctx->addrlens)))
1273 return GNUNET_SYSERR;
1274 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1276 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1279 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->serviceName,
1281 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1282 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1283 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1284 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1291 * Get the name of the user that'll be used
1292 * to provide the service.
1295 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1301 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->serviceName,
1311 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1319 if (NULL == (pif = get_pid_file_name (sctx)))
1320 return GNUNET_OK; /* no file desired */
1321 user = get_user_name (sctx);
1322 rdir = GNUNET_strdup (pif);
1323 len = strlen (rdir);
1324 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1327 if (0 != ACCESS (rdir, F_OK))
1329 /* we get to create a directory -- and claim it
1331 GNUNET_DISK_directory_create (rdir);
1332 if ((user != NULL) && (0 < strlen (user)))
1333 GNUNET_DISK_file_change_owner (rdir, user);
1335 if (0 != ACCESS (rdir, W_OK | X_OK))
1337 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1339 GNUNET_free_non_null (user);
1341 return GNUNET_SYSERR;
1344 pidfd = FOPEN (pif, "w");
1347 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1349 GNUNET_free_non_null (user);
1350 return GNUNET_SYSERR;
1352 if (0 > FPRINTF (pidfd, "%u", pid))
1353 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1354 GNUNET_break (0 == fclose (pidfd));
1355 if ((user != NULL) && (0 < strlen (user)))
1356 GNUNET_DISK_file_change_owner (pif, user);
1357 GNUNET_free_non_null (user);
1364 * Task run during shutdown.
1370 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1372 struct GNUNET_SERVER_Handle *server = cls;
1374 GNUNET_SERVER_destroy (server);
1379 * Initial task for the service.
1382 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1384 struct GNUNET_SERVICE_Context *sctx = cls;
1387 GNUNET_RESOLVER_connect (sctx->cfg);
1388 if (sctx->lsocks != NULL)
1390 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1391 sctx->timeout, sctx->require_found);
1394 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1395 sctx->timeout, sctx->require_found);
1396 if (sctx->server == NULL)
1398 if (sctx->addrs != NULL)
1401 while (sctx->addrs[i] != NULL)
1403 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1404 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1408 sctx->ret = GNUNET_SYSERR;
1411 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1413 /* install a task that will kill the server
1414 * process if the scheduler ever gets a shutdown signal */
1415 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1418 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1419 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1421 while ((sctx->my_handlers[i].callback != NULL))
1422 sctx->my_handlers[i++].callback_cls = sctx;
1423 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1424 if (sctx->ready_confirm_fd != -1)
1426 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1427 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1428 sctx->ready_confirm_fd = -1;
1429 write_pid_file (sctx, getpid ());
1431 if (sctx->addrs != NULL)
1434 while (sctx->addrs[i] != NULL)
1436 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1437 sctx->serviceName, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1441 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1446 * Detach from terminal.
1449 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1456 if (0 != PIPE (filedes))
1458 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1459 return GNUNET_SYSERR;
1464 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1465 return GNUNET_SYSERR;
1472 GNUNET_break (0 == CLOSE (filedes[1]));
1474 if (1 != READ (filedes[0], &c, sizeof (char)))
1475 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1482 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1485 LOG (GNUNET_ERROR_TYPE_INFO,
1486 _("Service process could not initialize server function\n"));
1489 LOG (GNUNET_ERROR_TYPE_INFO,
1490 _("Service process failed to report status\n"));
1493 exit (1); /* child reported error */
1495 GNUNET_break (0 == CLOSE (0));
1496 GNUNET_break (0 == CLOSE (1));
1497 GNUNET_break (0 == CLOSE (filedes[0]));
1498 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1500 return GNUNET_SYSERR;
1501 /* set stdin/stdout to /dev/null */
1502 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1504 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1505 (void) CLOSE (nullfd);
1506 return GNUNET_SYSERR;
1508 (void) CLOSE (nullfd);
1509 /* Detach from controlling terminal */
1512 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1513 sctx->ready_confirm_fd = filedes[1];
1515 /* FIXME: we probably need to do something else
1516 * elsewhere in order to fork the process itself... */
1527 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1531 if (NULL == (user = get_user_name (sctx)))
1532 return GNUNET_OK; /* keep */
1537 pws = getpwnam (user);
1540 LOG (GNUNET_ERROR_TYPE_ERROR,
1541 _("Cannot obtain information about user `%s': %s\n"), user,
1542 errno == 0 ? _("No such user") : STRERROR (errno));
1544 return GNUNET_SYSERR;
1546 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1548 (0 != initgroups (user, pws->pw_gid)) ||
1550 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1552 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1553 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1555 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1556 user, STRERROR (errno));
1558 return GNUNET_SYSERR;
1568 * Delete the PID file that was created by our parent.
1571 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1573 char *pif = get_pid_file_name (sctx);
1576 return; /* no PID file */
1577 if (0 != UNLINK (pif))
1578 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1584 * Run a standard GNUnet service startup sequence (initialize loggers
1585 * and configuration, parse options).
1587 * @param argc number of command line arguments
1588 * @param argv command line arguments
1589 * @param serviceName our service name
1590 * @param opt service options
1591 * @param task main task of the service
1592 * @param task_cls closure for task
1593 * @return GNUNET_SYSERR on error, GNUNET_OK
1594 * if we shutdown nicely
1597 GNUNET_SERVICE_run (int argc, char *const *argv, const char *serviceName,
1598 enum GNUNET_SERVICE_Options opt, GNUNET_SERVICE_Main task,
1601 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1609 unsigned long long skew_offset;
1610 unsigned long long skew_variance;
1611 long long clock_offset;
1612 struct GNUNET_SERVICE_Context sctx;
1613 struct GNUNET_CONFIGURATION_Handle *cfg;
1615 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1616 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1617 {'d', "daemonize", NULL,
1618 gettext_noop ("do daemonize (detach from terminal)"), 0,
1619 GNUNET_GETOPT_set_one, &do_daemonize},
1620 GNUNET_GETOPT_OPTION_HELP (serviceName),
1621 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1622 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1623 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION),
1624 GNUNET_GETOPT_OPTION_END
1630 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1631 memset (&sctx, 0, sizeof (sctx));
1633 sctx.ready_confirm_fd = -1;
1634 sctx.ret = GNUNET_OK;
1635 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1637 sctx.task_cls = task_cls;
1638 sctx.serviceName = serviceName;
1639 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1640 /* setup subsystems */
1641 if (GNUNET_SYSERR ==
1642 GNUNET_GETOPT_run (serviceName, service_options, argc, argv))
1644 if (GNUNET_OK != GNUNET_log_setup (serviceName, loglev, logfile))
1646 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_fn))
1648 if (GNUNET_OK != setup_service (&sctx))
1650 if ((do_daemonize == 1) && (GNUNET_OK != detach_terminal (&sctx)))
1652 if (GNUNET_OK != set_user_id (&sctx))
1655 LOG (GNUNET_ERROR_TYPE_DEBUG,
1656 "Service `%s' runs with configuration from `%s'\n", serviceName, cfg_fn);
1659 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1660 "SKEW_OFFSET", &skew_offset)) &&
1662 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1663 "SKEW_VARIANCE", &skew_variance)))
1665 clock_offset = skew_offset - skew_variance;
1666 GNUNET_TIME_set_offset (clock_offset);
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1671 /* actually run service */
1673 GNUNET_SCHEDULER_run (&service_task, &sctx);
1676 if ((do_daemonize == 1) && (sctx.server != NULL))
1677 pid_file_delete (&sctx);
1678 GNUNET_free_non_null (sctx.my_handlers);
1681 if (sctx.ready_confirm_fd != -1)
1683 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1684 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1685 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1688 GNUNET_CONFIGURATION_destroy (cfg);
1690 if (sctx.addrs != NULL)
1691 while (sctx.addrs[i] != NULL)
1692 GNUNET_free (sctx.addrs[i++]);
1693 GNUNET_free_non_null (sctx.addrs);
1694 GNUNET_free_non_null (sctx.addrlens);
1695 GNUNET_free_non_null (logfile);
1696 GNUNET_free_non_null (loglev);
1697 GNUNET_free (cfg_fn);
1698 GNUNET_free_non_null (sctx.v4_denied);
1699 GNUNET_free_non_null (sctx.v6_denied);
1700 GNUNET_free_non_null (sctx.v4_allowed);
1701 GNUNET_free_non_null (sctx.v6_allowed);
1703 return err ? GNUNET_SYSERR : sctx.ret;
1708 * Run a service startup sequence within an existing
1709 * initialized system.
1711 * @param serviceName our service name
1712 * @param cfg configuration to use
1713 * @return NULL on error, service handle
1715 struct GNUNET_SERVICE_Context *
1716 GNUNET_SERVICE_start (const char *serviceName,
1717 const struct GNUNET_CONFIGURATION_Handle *cfg)
1720 struct GNUNET_SERVICE_Context *sctx;
1722 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1723 sctx->ready_confirm_fd = -1; /* no daemonizing */
1724 sctx->ret = GNUNET_OK;
1725 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1726 sctx->serviceName = serviceName;
1729 /* setup subsystems */
1730 if (GNUNET_OK != setup_service (sctx))
1732 GNUNET_SERVICE_stop (sctx);
1735 if (sctx->lsocks != NULL)
1737 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1738 sctx->timeout, sctx->require_found);
1741 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1742 sctx->timeout, sctx->require_found);
1744 if (NULL == sctx->server)
1746 GNUNET_SERVICE_stop (sctx);
1749 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1750 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1752 while ((sctx->my_handlers[i].callback != NULL))
1753 sctx->my_handlers[i++].callback_cls = sctx;
1754 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1759 * Obtain the server used by a service. Note that the server must NOT
1760 * be destroyed by the caller.
1762 * @param ctx the service context returned from the start function
1763 * @return handle to the server for this service, NULL if there is none
1765 struct GNUNET_SERVER_Handle *
1766 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1773 * Stop a service that was started with "GNUNET_SERVICE_start".
1775 * @param sctx the service context returned from the start function
1778 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1782 if (NULL != sctx->server)
1783 GNUNET_SERVER_destroy (sctx->server);
1784 GNUNET_free_non_null (sctx->my_handlers);
1785 if (sctx->addrs != NULL)
1788 while (sctx->addrs[i] != NULL)
1789 GNUNET_free (sctx->addrs[i++]);
1790 GNUNET_free (sctx->addrs);
1792 GNUNET_free_non_null (sctx->addrlens);
1793 GNUNET_free_non_null (sctx->v4_denied);
1794 GNUNET_free_non_null (sctx->v6_denied);
1795 GNUNET_free_non_null (sctx->v4_allowed);
1796 GNUNET_free_non_null (sctx->v6_allowed);
1801 /* end of service.c */