1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #include <config.h>
35b528cefcSMark Murray
36b528cefcSMark Murray #include <err.h>
375e9cd1aeSAssar Westerlund #include "roken.h"
38b528cefcSMark Murray
39b528cefcSMark Murray /*
40b528cefcSMark Murray * accept a connection on `s' and pretend it's served by inetd.
41b528cefcSMark Murray */
42b528cefcSMark Murray
43b528cefcSMark Murray static void
accept_it(rk_socket_t s,rk_socket_t * ret_socket)44*ae771770SStanislav Sedov accept_it (rk_socket_t s, rk_socket_t *ret_socket)
45b528cefcSMark Murray {
46*ae771770SStanislav Sedov rk_socket_t as;
47b528cefcSMark Murray
48*ae771770SStanislav Sedov as = accept(s, NULL, NULL);
49*ae771770SStanislav Sedov if(rk_IS_BAD_SOCKET(as))
50b528cefcSMark Murray err (1, "accept");
51*ae771770SStanislav Sedov
52*ae771770SStanislav Sedov if (ret_socket) {
53*ae771770SStanislav Sedov
54*ae771770SStanislav Sedov *ret_socket = as;
55*ae771770SStanislav Sedov
56*ae771770SStanislav Sedov } else {
57*ae771770SStanislav Sedov int fd = socket_to_fd(as, 0);
58*ae771770SStanislav Sedov
59*ae771770SStanislav Sedov /* We would use _O_RDONLY for the socket_to_fd() call for
60*ae771770SStanislav Sedov STDIN, but there are instances where we assume that STDIN
61*ae771770SStanislav Sedov is a r/w socket. */
62*ae771770SStanislav Sedov
63*ae771770SStanislav Sedov dup2(fd, STDIN_FILENO);
64*ae771770SStanislav Sedov dup2(fd, STDOUT_FILENO);
65*ae771770SStanislav Sedov
66*ae771770SStanislav Sedov rk_closesocket(as);
67*ae771770SStanislav Sedov }
68b528cefcSMark Murray }
69b528cefcSMark Murray
70*ae771770SStanislav Sedov /**
71*ae771770SStanislav Sedov * Listen on a specified addresses
72*ae771770SStanislav Sedov *
73*ae771770SStanislav Sedov * Listens on the specified addresses for incoming connections. If
74*ae771770SStanislav Sedov * the \a ret_socket parameter is \a NULL, on return STDIN and STDOUT
75*ae771770SStanislav Sedov * will be connected to an accepted socket. If the \a ret_socket
76*ae771770SStanislav Sedov * parameter is non-NULL, the accepted socket will be returned in
77*ae771770SStanislav Sedov * *ret_socket. In the latter case, STDIN and STDOUT will be left
78*ae771770SStanislav Sedov * unmodified.
79*ae771770SStanislav Sedov *
80*ae771770SStanislav Sedov * This function does not return if there is an error or if no
81*ae771770SStanislav Sedov * connection is established.
82*ae771770SStanislav Sedov *
83*ae771770SStanislav Sedov * @param[in] ai Addresses to listen on
84*ae771770SStanislav Sedov * @param[out] ret_socket If non-NULL receives the accepted socket.
85*ae771770SStanislav Sedov *
86*ae771770SStanislav Sedov * @see mini_inetd()
87b528cefcSMark Murray */
88*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
mini_inetd_addrinfo(struct addrinfo * ai,rk_socket_t * ret_socket)89*ae771770SStanislav Sedov mini_inetd_addrinfo (struct addrinfo *ai, rk_socket_t *ret_socket)
90b528cefcSMark Murray {
918373020dSJacques Vidrine int ret;
928373020dSJacques Vidrine struct addrinfo *a;
9313e3f4d6SMark Murray int n, nalloc, i;
94*ae771770SStanislav Sedov rk_socket_t *fds;
95b528cefcSMark Murray fd_set orig_read_set, read_set;
96*ae771770SStanislav Sedov rk_socket_t max_fd = (rk_socket_t)-1;
97b528cefcSMark Murray
9813e3f4d6SMark Murray for (nalloc = 0, a = ai; a != NULL; a = a->ai_next)
9913e3f4d6SMark Murray ++nalloc;
100b528cefcSMark Murray
10113e3f4d6SMark Murray fds = malloc (nalloc * sizeof(*fds));
102*ae771770SStanislav Sedov if (fds == NULL) {
103b528cefcSMark Murray errx (1, "mini_inetd: out of memory");
104*ae771770SStanislav Sedov UNREACHABLE(return);
105*ae771770SStanislav Sedov }
106b528cefcSMark Murray
107b528cefcSMark Murray FD_ZERO(&orig_read_set);
108b528cefcSMark Murray
10913e3f4d6SMark Murray for (i = 0, a = ai; a != NULL; a = a->ai_next) {
110b528cefcSMark Murray fds[i] = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
111*ae771770SStanislav Sedov if (rk_IS_BAD_SOCKET(fds[i]))
11213e3f4d6SMark Murray continue;
113b528cefcSMark Murray socket_set_reuseaddr (fds[i], 1);
114*ae771770SStanislav Sedov socket_set_ipv6only(fds[i], 1);
115*ae771770SStanislav Sedov if (rk_IS_SOCKET_ERROR(bind (fds[i], a->ai_addr, a->ai_addrlen))) {
1164137ff4cSJacques Vidrine warn ("bind af = %d", a->ai_family);
117*ae771770SStanislav Sedov rk_closesocket(fds[i]);
118*ae771770SStanislav Sedov fds[i] = rk_INVALID_SOCKET;
1194137ff4cSJacques Vidrine continue;
1204137ff4cSJacques Vidrine }
121*ae771770SStanislav Sedov if (rk_IS_SOCKET_ERROR(listen (fds[i], SOMAXCONN))) {
1224137ff4cSJacques Vidrine warn ("listen af = %d", a->ai_family);
123*ae771770SStanislav Sedov rk_closesocket(fds[i]);
124*ae771770SStanislav Sedov fds[i] = rk_INVALID_SOCKET;
1254137ff4cSJacques Vidrine continue;
1264137ff4cSJacques Vidrine }
127*ae771770SStanislav Sedov #ifndef NO_LIMIT_FD_SETSIZE
1285e9cd1aeSAssar Westerlund if (fds[i] >= FD_SETSIZE)
1295e9cd1aeSAssar Westerlund errx (1, "fd too large");
130*ae771770SStanislav Sedov #endif
131b528cefcSMark Murray FD_SET(fds[i], &orig_read_set);
132b528cefcSMark Murray max_fd = max(max_fd, fds[i]);
13313e3f4d6SMark Murray ++i;
134b528cefcSMark Murray }
13513e3f4d6SMark Murray if (i == 0)
13613e3f4d6SMark Murray errx (1, "no sockets");
13713e3f4d6SMark Murray n = i;
138b528cefcSMark Murray
139b528cefcSMark Murray do {
140b528cefcSMark Murray read_set = orig_read_set;
141b528cefcSMark Murray
142b528cefcSMark Murray ret = select (max_fd + 1, &read_set, NULL, NULL, NULL);
143*ae771770SStanislav Sedov if (rk_IS_SOCKET_ERROR(ret) && rk_SOCK_ERRNO != EINTR)
144b528cefcSMark Murray err (1, "select");
145b528cefcSMark Murray } while (ret <= 0);
146b528cefcSMark Murray
147b528cefcSMark Murray for (i = 0; i < n; ++i)
148b528cefcSMark Murray if (FD_ISSET (fds[i], &read_set)) {
149*ae771770SStanislav Sedov accept_it (fds[i], ret_socket);
150*ae771770SStanislav Sedov for (i = 0; i < n; ++i)
151*ae771770SStanislav Sedov rk_closesocket(fds[i]);
152*ae771770SStanislav Sedov free(fds);
153b528cefcSMark Murray return;
154b528cefcSMark Murray }
155b528cefcSMark Murray abort ();
156b528cefcSMark Murray }
1578373020dSJacques Vidrine
158*ae771770SStanislav Sedov /**
159*ae771770SStanislav Sedov * Listen on a specified port
160*ae771770SStanislav Sedov *
161*ae771770SStanislav Sedov * Listens on the specified port for incoming connections. If the \a
162*ae771770SStanislav Sedov * ret_socket parameter is \a NULL, on return STDIN and STDOUT will be
163*ae771770SStanislav Sedov * connected to an accepted socket. If the \a ret_socket parameter is
164*ae771770SStanislav Sedov * non-NULL, the accepted socket will be returned in *ret_socket. In
165*ae771770SStanislav Sedov * the latter case, STDIN and STDOUT will be left unmodified.
166*ae771770SStanislav Sedov *
167*ae771770SStanislav Sedov * This function does not return if there is an error or if no
168*ae771770SStanislav Sedov * connection is established.
169*ae771770SStanislav Sedov *
170*ae771770SStanislav Sedov * @param[in] port Port to listen on
171*ae771770SStanislav Sedov * @param[out] ret_socket If non-NULL receives the accepted socket.
172*ae771770SStanislav Sedov *
173*ae771770SStanislav Sedov * @see mini_inetd_addrinfo()
174*ae771770SStanislav Sedov */
175*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
mini_inetd(int port,rk_socket_t * ret_socket)176*ae771770SStanislav Sedov mini_inetd(int port, rk_socket_t * ret_socket)
1778373020dSJacques Vidrine {
1788373020dSJacques Vidrine int error;
1798373020dSJacques Vidrine struct addrinfo *ai, hints;
1808373020dSJacques Vidrine char portstr[NI_MAXSERV];
1818373020dSJacques Vidrine
1828373020dSJacques Vidrine memset (&hints, 0, sizeof(hints));
1838373020dSJacques Vidrine hints.ai_flags = AI_PASSIVE;
1848373020dSJacques Vidrine hints.ai_socktype = SOCK_STREAM;
1858373020dSJacques Vidrine hints.ai_family = PF_UNSPEC;
1868373020dSJacques Vidrine
1878373020dSJacques Vidrine snprintf (portstr, sizeof(portstr), "%d", ntohs(port));
1888373020dSJacques Vidrine
1898373020dSJacques Vidrine error = getaddrinfo (NULL, portstr, &hints, &ai);
1908373020dSJacques Vidrine if (error)
1918373020dSJacques Vidrine errx (1, "getaddrinfo: %s", gai_strerror (error));
1928373020dSJacques Vidrine
193*ae771770SStanislav Sedov mini_inetd_addrinfo(ai, ret_socket);
1948373020dSJacques Vidrine
1958373020dSJacques Vidrine freeaddrinfo(ai);
1968373020dSJacques Vidrine }
197*ae771770SStanislav Sedov
198