1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* Copyright (c) 1997, 98, 2000, 01
* Motoyuki Kasahara
*
* Copyright (c) 2001
* IPv6 support by UMENO Takashi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* This program requires the following Autoconf macros:
* AC_C_CONST
* AC_TYPE_SIZE_T
* AC_HEADER_STDC
* AC_CHECK_HEADERS(string.h, memory.h, unistd.h)
* AC_CHECK_FUNCS(memcpy)
* AC_HEADER_TIME
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <syslog.h>
#include <errno.h>
#include <signal.h>
#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
#endif /* not STDC_HEADERS and not HAVE_STRING_H */
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#else
#include <sys/file.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else /* not TIME_WITH_SYS_TIME */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else /* not HAVE_SYS_TIME_H */
#include <time.h>
#endif /* not HAVE_SYS_TIME_H */
#endif /* not TIME_WITH_SYS_TIME */
#include "dummyin6.h"
#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
#include "getaddrinfo.h"
#endif
#include "serverport.h"
#include "fdset.h"
#ifdef USE_FAKELOG
#include "fakelog.h"
#endif
#ifndef HAVE_MEMCPY
#define memcpy(d, s, n) bcopy((s), (d), (n))
#ifdef __STDC__
void *memchr(const void *, int, size_t);
int memcmp(const void *, const void *, size_t);
void *memmove(void *, const void *, size_t);
void *memset(void *, int, size_t);
#else /* not __STDC__ */
char *memchr();
int memcmp();
char *memmove();
char *memset();
#endif /* not __STDC__ */
#endif /* not HAVE_MEMCPY */
#undef LISTEN_NONBLOCK_SOCKETS
/*
* Target address families.
*/
#ifdef ENABLE_IPV6
static int target_families[] = {AF_INET6, AF_INET};
#define TARGET_FAMILY_COUNT 2
#else
static int target_families[] = {AF_INET};
#define TARGET_FAMILY_COUNT 1
#endif
/*
* Bind and listen to a TCP port.
*
* `port' is a port number to which the server listens.
* `backlog' is a maximum queue length of pending connections.
*
* If successful, the file descriptor of the socket is returned.
* Otherwise, -1 is returned.
*/
int
set_server_ports(port, backlog, listening_files, max_file)
int port;
int backlog;
fd_set *listening_files;
int *max_file;
{
struct addrinfo hints;
struct addrinfo *info_list;
struct addrinfo *info;
int socket_options;
char address_string[INET6_ADDRSTRLEN];
char port_string[16];
int gai_error_code;
int file;
int i;
initialize_fdset(listening_files, max_file);
/*
* Get address information.
*/
sprintf(port_string, "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
gai_error_code = getaddrinfo(NULL, port_string, &hints, &info_list);
if (gai_error_code != 0) {
syslog(LOG_DEBUG, "debug: getaddrinfo fail, %s",
gai_strerror(gai_error_code));
goto failed;
}
for (i = 0; i < TARGET_FAMILY_COUNT; i++) {
for (info = info_list; info != NULL; info = info->ai_next) {
if (info->ai_family != target_families[i])
continue;
info->ai_addr->sa_family = info->ai_family; /* for AIX. */
gai_error_code = getnameinfo(info->ai_addr, info->ai_addrlen,
address_string, sizeof(address_string), NULL, 0,
NI_NUMERICHOST | NI_NUMERICSERV);
if (gai_error_code != 0)
continue;
/*
* Create a socket.
*/
file = socket(info->ai_family, info->ai_socktype,
info->ai_protocol);
if (file < 0)
continue;
#ifdef SO_REUSEADDR
socket_options = 1;
if (setsockopt(file, SOL_SOCKET, SO_REUSEADDR,
(char *)&socket_options, sizeof(socket_options)) < 0) {
syslog(LOG_ERR, "setsockopt() failed, %s: %s", strerror(errno),
address_string);
goto failed;
}
#endif
/*
* Bind the socket.
*/
if (bind(file, info->ai_addr, info->ai_addrlen) < 0) {
syslog(LOG_ERR, "bind() failed, %s: %s", strerror(errno),
address_string);
close(file);
continue;
}
/*
* Listen to the port.
*/
if (listen(file, backlog) < 0) {
syslog(LOG_ERR, "listen() failed, %s: %s", strerror(errno),
address_string);
goto failed;
}
add_fd_to_fdset(listening_files, max_file, file);
#ifdef LISTEN_NONBLOCK_SOCKETS
fcntl(file, F_GETFL, 0);
fcntl(file, F_SETFL, O_NONBLOCK);
#endif
syslog(LOG_DEBUG, "debug: set server port: %s", address_string);
}
}
freeaddrinfo(info_list);
if (*max_file < 0)
syslog(LOG_ERR, "no server port available: %d/tcp", port);
return *max_file;
/*
* An error occurs...
*/
failed:
freeaddrinfo(info_list);
syslog(LOG_ERR, "failed to listen to the port: %d/tcp", port);
return -1;
}
/*
* Wait until a new connection comes.
*
* `file' is a socket to wait a connection.
* The system call listen() must have been called for `file'.
* (A file created by set_server_port() is suitable for `file'.)
*
* The function never returns until a new connection comes, or an error
* occurs. If an error occurs, -1 is returned. Otherwise 0 is returned.
*/
int
wait_server_ports(listening_files, select_files, max_file)
fd_set *listening_files;
fd_set *select_files;
int *max_file;
{
int status;
memcpy(select_files, listening_files, sizeof(fd_set));
for (;;) {
/*
* Wait until a new connection comes.
*/
status = select(*max_file + 1, select_files, NULL, NULL, NULL);
if (0 < status)
break;
else if (errno == EINTR) {
/*
* When a child process exits, select() returns -1 and `errno'
* is set to EINTR.
*/
return -1;
} else {
syslog(LOG_ERR, "select() failed, %s", strerror(errno));
goto failed;
}
}
syslog(LOG_DEBUG, "debug: catch a new connection");
return 0;
/*
* An error occurs...
*/
failed:
syslog(LOG_ERR, "failed to catch a new connection");
return -1;
}
|