2 * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$Id: mini_inetd.c,v 1.18.2.1 2000/10/10 13:22:33 assar Exp $");
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
47 #ifdef HAVE_SYS_TIME_H
50 #ifdef HAVE_SYS_SOCKET_H
51 #include <sys/socket.h>
53 #ifdef HAVE_NETINET_IN_H
54 #include <netinet/in.h>
56 #ifdef HAVE_NETINET_IN6_H
57 #include <netinet/in6.h>
59 #ifdef HAVE_NETINET6_IN6_H
60 #include <netinet6/in6.h>
69 struct sockaddr_in sa;
72 s = socket(AF_INET, SOCK_STREAM, 0);
79 socket_set_reuseaddr (s, 1);
80 memset(&sa, 0, sizeof(sa));
81 sa.sin_family = AF_INET;
83 sa.sin_addr.s_addr = INADDR_ANY;
84 if(bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0){
88 if(listen(s, SOMAXCONN) < 0){
99 struct sockaddr_in6 sa;
102 s = socket(AF_INET6, SOCK_STREAM, 0);
109 socket_set_reuseaddr (s, 1);
110 memset(&sa, 0, sizeof(sa));
111 sa.sin6_family = AF_INET6;
113 sa.sin6_addr = in6addr_any;
114 if(bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0){
118 if(listen(s, SOMAXCONN) < 0){
124 #endif /* HAVE_IPV6 */
127 * accept a connection on `s' and pretend it's served by inetd.
135 s2 = accept(s, NULL, 0);
141 dup2(s2, STDIN_FILENO);
142 dup2(s2, STDOUT_FILENO);
143 /* dup2(s2, STDERR_FILENO); */
148 * Listen on `port' emulating inetd.
152 mini_inetd (int port)
158 fd_set orig_read_set, read_set;
160 FD_ZERO(&orig_read_set);
162 sock_v4 = listen_v4 (port);
164 max_fd = max(max_fd, sock_v4);
165 if (max_fd >= FD_SETSIZE)
166 errx (1, "fd too large");
167 FD_SET(sock_v4, &orig_read_set);
170 sock_v6 = listen_v6 (port);
172 max_fd = max(max_fd, sock_v6);
173 if (max_fd >= FD_SETSIZE)
174 errx (1, "fd too large");
175 FD_SET(sock_v6, &orig_read_set);
180 read_set = orig_read_set;
182 ret = select (max_fd + 1, &read_set, NULL, NULL, NULL);
183 if (ret < 0 && ret != EINTR) {
189 if (sock_v4 > 0 && FD_ISSET (sock_v4, &read_set)) {
193 if (sock_v6 > 0 && FD_ISSET (sock_v6, &read_set)) {