155682Smarkm/*
2233294Sstas * Copyright (c) 1995 - 2001 Kungliga Tekniska H��gskolan
355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
455682Smarkm * All rights reserved.
5233294Sstas *
655682Smarkm * Redistribution and use in source and binary forms, with or without
755682Smarkm * modification, are permitted provided that the following conditions
855682Smarkm * are met:
9233294Sstas *
1055682Smarkm * 1. Redistributions of source code must retain the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer.
12233294Sstas *
1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1455682Smarkm *    notice, this list of conditions and the following disclaimer in the
1555682Smarkm *    documentation and/or other materials provided with the distribution.
16233294Sstas *
1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors
1855682Smarkm *    may be used to endorse or promote products derived from this software
1955682Smarkm *    without specific prior written permission.
20233294Sstas *
2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155682Smarkm * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include <config.h>
3555682Smarkm
3655682Smarkm#include <err.h>
3772445Sassar#include "roken.h"
3855682Smarkm
3955682Smarkm/*
4055682Smarkm * accept a connection on `s' and pretend it's served by inetd.
4155682Smarkm */
4255682Smarkm
4355682Smarkmstatic void
44233294Sstasaccept_it (rk_socket_t s, rk_socket_t *ret_socket)
4555682Smarkm{
46233294Sstas    rk_socket_t as;
4755682Smarkm
48233294Sstas    as = accept(s, NULL, NULL);
49233294Sstas    if(rk_IS_BAD_SOCKET(as))
5055682Smarkm	err (1, "accept");
51233294Sstas
52233294Sstas    if (ret_socket) {
53233294Sstas
54233294Sstas	*ret_socket = as;
55233294Sstas
56233294Sstas    } else {
57233294Sstas	int fd = socket_to_fd(as, 0);
58233294Sstas
59233294Sstas	/* We would use _O_RDONLY for the socket_to_fd() call for
60233294Sstas	   STDIN, but there are instances where we assume that STDIN
61233294Sstas	   is a r/w socket. */
62233294Sstas
63233294Sstas	dup2(fd, STDIN_FILENO);
64233294Sstas	dup2(fd, STDOUT_FILENO);
65233294Sstas
66233294Sstas	rk_closesocket(as);
67233294Sstas    }
6855682Smarkm}
6955682Smarkm
70233294Sstas/**
71233294Sstas * Listen on a specified addresses
72233294Sstas *
73233294Sstas * Listens on the specified addresses for incoming connections.  If
74233294Sstas * the \a ret_socket parameter is \a NULL, on return STDIN and STDOUT
75233294Sstas * will be connected to an accepted socket.  If the \a ret_socket
76233294Sstas * parameter is non-NULL, the accepted socket will be returned in
77233294Sstas * *ret_socket.  In the latter case, STDIN and STDOUT will be left
78233294Sstas * unmodified.
79233294Sstas *
80233294Sstas * This function does not return if there is an error or if no
81233294Sstas * connection is established.
82233294Sstas *
83233294Sstas * @param[in] ai Addresses to listen on
84233294Sstas * @param[out] ret_socket If non-NULL receives the accepted socket.
85233294Sstas *
86233294Sstas * @see mini_inetd()
8755682Smarkm */
88233294SstasROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
89233294Sstasmini_inetd_addrinfo (struct addrinfo *ai, rk_socket_t *ret_socket)
9055682Smarkm{
91102644Snectar    int ret;
92102644Snectar    struct addrinfo *a;
9357416Smarkm    int n, nalloc, i;
94233294Sstas    rk_socket_t *fds;
9555682Smarkm    fd_set orig_read_set, read_set;
96233294Sstas    rk_socket_t max_fd = (rk_socket_t)-1;
9755682Smarkm
9857416Smarkm    for (nalloc = 0, a = ai; a != NULL; a = a->ai_next)
9957416Smarkm	++nalloc;
10055682Smarkm
10157416Smarkm    fds = malloc (nalloc * sizeof(*fds));
102233294Sstas    if (fds == NULL) {
10355682Smarkm	errx (1, "mini_inetd: out of memory");
104233294Sstas	UNREACHABLE(return);
105233294Sstas    }
10655682Smarkm
10755682Smarkm    FD_ZERO(&orig_read_set);
10855682Smarkm
10957416Smarkm    for (i = 0, a = ai; a != NULL; a = a->ai_next) {
11055682Smarkm	fds[i] = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
111233294Sstas	if (rk_IS_BAD_SOCKET(fds[i]))
11257416Smarkm	    continue;
11355682Smarkm	socket_set_reuseaddr (fds[i], 1);
114233294Sstas	socket_set_ipv6only(fds[i], 1);
115233294Sstas	if (rk_IS_SOCKET_ERROR(bind (fds[i], a->ai_addr, a->ai_addrlen))) {
11690926Snectar	    warn ("bind af = %d", a->ai_family);
117233294Sstas	    rk_closesocket(fds[i]);
118233294Sstas	    fds[i] = rk_INVALID_SOCKET;
11990926Snectar	    continue;
12090926Snectar	}
121233294Sstas	if (rk_IS_SOCKET_ERROR(listen (fds[i], SOMAXCONN))) {
12290926Snectar	    warn ("listen af = %d", a->ai_family);
123233294Sstas	    rk_closesocket(fds[i]);
124233294Sstas	    fds[i] = rk_INVALID_SOCKET;
12590926Snectar	    continue;
12690926Snectar	}
127233294Sstas#ifndef NO_LIMIT_FD_SETSIZE
12872445Sassar	if (fds[i] >= FD_SETSIZE)
12972445Sassar	    errx (1, "fd too large");
130233294Sstas#endif
13155682Smarkm	FD_SET(fds[i], &orig_read_set);
13255682Smarkm	max_fd = max(max_fd, fds[i]);
13357416Smarkm	++i;
13455682Smarkm    }
13557416Smarkm    if (i == 0)
13657416Smarkm	errx (1, "no sockets");
13757416Smarkm    n = i;
13855682Smarkm
13955682Smarkm    do {
14055682Smarkm	read_set = orig_read_set;
14155682Smarkm
14255682Smarkm	ret = select (max_fd + 1, &read_set, NULL, NULL, NULL);
143233294Sstas	if (rk_IS_SOCKET_ERROR(ret) && rk_SOCK_ERRNO != EINTR)
14455682Smarkm	    err (1, "select");
14555682Smarkm    } while (ret <= 0);
14655682Smarkm
14755682Smarkm    for (i = 0; i < n; ++i)
14855682Smarkm	if (FD_ISSET (fds[i], &read_set)) {
149233294Sstas	    accept_it (fds[i], ret_socket);
150233294Sstas	    for (i = 0; i < n; ++i)
151233294Sstas	      rk_closesocket(fds[i]);
152233294Sstas	    free(fds);
15355682Smarkm	    return;
15455682Smarkm	}
15555682Smarkm    abort ();
15655682Smarkm}
157102644Snectar
158233294Sstas/**
159233294Sstas * Listen on a specified port
160233294Sstas *
161233294Sstas * Listens on the specified port for incoming connections.  If the \a
162233294Sstas * ret_socket parameter is \a NULL, on return STDIN and STDOUT will be
163233294Sstas * connected to an accepted socket.  If the \a ret_socket parameter is
164233294Sstas * non-NULL, the accepted socket will be returned in *ret_socket.  In
165233294Sstas * the latter case, STDIN and STDOUT will be left unmodified.
166233294Sstas *
167233294Sstas * This function does not return if there is an error or if no
168233294Sstas * connection is established.
169233294Sstas *
170233294Sstas * @param[in] port Port to listen on
171233294Sstas * @param[out] ret_socket If non-NULL receives the accepted socket.
172233294Sstas *
173233294Sstas * @see mini_inetd_addrinfo()
174233294Sstas */
175233294SstasROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
176233294Sstasmini_inetd(int port, rk_socket_t * ret_socket)
177102644Snectar{
178102644Snectar    int error;
179102644Snectar    struct addrinfo *ai, hints;
180102644Snectar    char portstr[NI_MAXSERV];
181102644Snectar
182102644Snectar    memset (&hints, 0, sizeof(hints));
183102644Snectar    hints.ai_flags    = AI_PASSIVE;
184102644Snectar    hints.ai_socktype = SOCK_STREAM;
185102644Snectar    hints.ai_family   = PF_UNSPEC;
186102644Snectar
187102644Snectar    snprintf (portstr, sizeof(portstr), "%d", ntohs(port));
188102644Snectar
189102644Snectar    error = getaddrinfo (NULL, portstr, &hints, &ai);
190102644Snectar    if (error)
191102644Snectar	errx (1, "getaddrinfo: %s", gai_strerror (error));
192102644Snectar
193233294Sstas    mini_inetd_addrinfo(ai, ret_socket);
194233294Sstas
195102644Snectar    freeaddrinfo(ai);
196102644Snectar}
197233294Sstas
198