1f08c3bdfSopenharmony_ci/******************************************************************************/ 2f08c3bdfSopenharmony_ci/* */ 3f08c3bdfSopenharmony_ci/* Copyright (c) International Business Machines Corp., 2005 */ 4f08c3bdfSopenharmony_ci/* */ 5f08c3bdfSopenharmony_ci/* This program is free software; you can redistribute it and/or modify */ 6f08c3bdfSopenharmony_ci/* it under the terms of the GNU General Public License as published by */ 7f08c3bdfSopenharmony_ci/* the Free Software Foundation; either version 2 of the License, or */ 8f08c3bdfSopenharmony_ci/* (at your option) any later version. */ 9f08c3bdfSopenharmony_ci/* */ 10f08c3bdfSopenharmony_ci/* This program is distributed in the hope that it will be useful, */ 11f08c3bdfSopenharmony_ci/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 12f08c3bdfSopenharmony_ci/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */ 13f08c3bdfSopenharmony_ci/* the GNU General Public License for more details. */ 14f08c3bdfSopenharmony_ci/* */ 15f08c3bdfSopenharmony_ci/* You should have received a copy of the GNU General Public License */ 16f08c3bdfSopenharmony_ci/* along with this program; if not, write to the Free Software */ 17f08c3bdfSopenharmony_ci/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ 18f08c3bdfSopenharmony_ci/* */ 19f08c3bdfSopenharmony_ci/******************************************************************************/ 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci/* 22f08c3bdfSopenharmony_ci * File: 23f08c3bdfSopenharmony_ci * ns-tcpserver.c 24f08c3bdfSopenharmony_ci * 25f08c3bdfSopenharmony_ci * Description: 26f08c3bdfSopenharmony_ci * This is TCP traffic server. 27f08c3bdfSopenharmony_ci * Accept connections from the clients, then send tcp segments to clients 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * Author: 30f08c3bdfSopenharmony_ci * Mitsuru Chinen <mitch@jp.ibm.com> 31f08c3bdfSopenharmony_ci * 32f08c3bdfSopenharmony_ci * History: 33f08c3bdfSopenharmony_ci * Oct 19 2005 - Created (Mitsuru Chinen) 34f08c3bdfSopenharmony_ci *---------------------------------------------------------------------------*/ 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci#include "ns-traffic.h" 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci/* 39f08c3bdfSopenharmony_ci * Standard Include Files 40f08c3bdfSopenharmony_ci */ 41f08c3bdfSopenharmony_ci#include <stdio.h> 42f08c3bdfSopenharmony_ci#include <stdlib.h> 43f08c3bdfSopenharmony_ci#include <string.h> 44f08c3bdfSopenharmony_ci#include <errno.h> 45f08c3bdfSopenharmony_ci#include <fcntl.h> 46f08c3bdfSopenharmony_ci#include <netdb.h> 47f08c3bdfSopenharmony_ci#include <time.h> 48f08c3bdfSopenharmony_ci#include <unistd.h> 49f08c3bdfSopenharmony_ci#include <sys/select.h> 50f08c3bdfSopenharmony_ci#include <sys/socket.h> 51f08c3bdfSopenharmony_ci#include <sys/stat.h> 52f08c3bdfSopenharmony_ci#include <sys/types.h> 53f08c3bdfSopenharmony_ci#include <sys/wait.h> 54f08c3bdfSopenharmony_ci#include <netinet/in.h> 55f08c3bdfSopenharmony_ci#include <netinet/tcp.h> 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci/* 58f08c3bdfSopenharmony_ci * Gloval variables 59f08c3bdfSopenharmony_ci */ 60f08c3bdfSopenharmony_cistruct sigaction handler; /* Behavior for a signal */ 61f08c3bdfSopenharmony_ciint catch_sighup; /* When catch the SIGHUP, set to non-zero */ 62f08c3bdfSopenharmony_ciint catch_sigpipe; /* When catch the SIGPIPE, set to non-zero */ 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci/* 65f08c3bdfSopenharmony_ci * Structure: server_info 66f08c3bdfSopenharmony_ci * 67f08c3bdfSopenharmony_ci * Description: 68f08c3bdfSopenharmony_ci * This structure stores the information of a server 69f08c3bdfSopenharmony_ci */ 70f08c3bdfSopenharmony_cistruct server_info { 71f08c3bdfSopenharmony_ci sa_family_t family; /* protocol family */ 72f08c3bdfSopenharmony_ci char *portnum; /* port number */ 73f08c3bdfSopenharmony_ci int listen_sd; /* socket descriptor for listening */ 74f08c3bdfSopenharmony_ci int concurrent; /* if non-zero, act as a concurrent server */ 75f08c3bdfSopenharmony_ci size_t current_connection; /* number of the current connection */ 76f08c3bdfSopenharmony_ci size_t max_connection; /* maximum connection number */ 77f08c3bdfSopenharmony_ci size_t lost_connection; /* number of lost connection */ 78f08c3bdfSopenharmony_ci size_t small_sending; /* if non-zero, in the small sending mode */ 79f08c3bdfSopenharmony_ci size_t window_scaling; /* if non-zero, in the window scaling mode */ 80f08c3bdfSopenharmony_ci}; 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci/* 83f08c3bdfSopenharmony_ci * Function: usage() 84f08c3bdfSopenharmony_ci * 85f08c3bdfSopenharmony_ci * Descripton: 86f08c3bdfSopenharmony_ci * Print the usage of this program. Then, terminate this program with 87f08c3bdfSopenharmony_ci * the specified exit value. 88f08c3bdfSopenharmony_ci * 89f08c3bdfSopenharmony_ci * Argument: 90f08c3bdfSopenharmony_ci * exit_value: exit value 91f08c3bdfSopenharmony_ci * 92f08c3bdfSopenharmony_ci * Return value: 93f08c3bdfSopenharmony_ci * This function does not return. 94f08c3bdfSopenharmony_ci */ 95f08c3bdfSopenharmony_civoid usage(char *program_name, int exit_value) 96f08c3bdfSopenharmony_ci{ 97f08c3bdfSopenharmony_ci FILE *stream = stdout; /* stream where the usage is output */ 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci if (exit_value == EXIT_FAILURE) 100f08c3bdfSopenharmony_ci stream = stderr; 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_ci fprintf(stream, "%s [OPTION]\n" 103f08c3bdfSopenharmony_ci "\t-f\tprotocol family\n" 104f08c3bdfSopenharmony_ci "\t\t 4 : IPv4\n" 105f08c3bdfSopenharmony_ci "\t\t 6 : IPv6\n" 106f08c3bdfSopenharmony_ci "\t-p\tport number\n" 107f08c3bdfSopenharmony_ci "\t-b\twork in the background\n" 108f08c3bdfSopenharmony_ci "\t-c\twork in the concurrent server mode\n" 109f08c3bdfSopenharmony_ci "\t-s\twork in the small sending mode\n" 110f08c3bdfSopenharmony_ci "\t-w\twork in the window scaling mode\n" 111f08c3bdfSopenharmony_ci "\t-o\tfilename where the server infomation is outputted\n" 112f08c3bdfSopenharmony_ci "\t-d\twork in the debug mode\n" 113f08c3bdfSopenharmony_ci "\t-h\tdisplay this usage\n" 114f08c3bdfSopenharmony_ci "" "*) Server works till it receives SIGHUP\n", program_name); 115f08c3bdfSopenharmony_ci exit(exit_value); 116f08c3bdfSopenharmony_ci} 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_ci/* 119f08c3bdfSopenharmony_ci * Function: set_signal_flag() 120f08c3bdfSopenharmony_ci * 121f08c3bdfSopenharmony_ci * Description: 122f08c3bdfSopenharmony_ci * This function sets global variable according to the signal. 123f08c3bdfSopenharmony_ci * Once a signal is caught, the signal is ignored after that. 124f08c3bdfSopenharmony_ci * 125f08c3bdfSopenharmony_ci * Argument: 126f08c3bdfSopenharmony_ci * type: type of signal 127f08c3bdfSopenharmony_ci * 128f08c3bdfSopenharmony_ci * Return value: 129f08c3bdfSopenharmony_ci * None 130f08c3bdfSopenharmony_ci */ 131f08c3bdfSopenharmony_civoid set_signal_flag(int type) 132f08c3bdfSopenharmony_ci{ 133f08c3bdfSopenharmony_ci /* Set SIG_IGN against the caught signal */ 134f08c3bdfSopenharmony_ci handler.sa_handler = SIG_IGN; 135f08c3bdfSopenharmony_ci if (sigaction(type, &handler, NULL) < 0) 136f08c3bdfSopenharmony_ci fatal_error("sigaction()"); 137f08c3bdfSopenharmony_ci 138f08c3bdfSopenharmony_ci if (debug) 139f08c3bdfSopenharmony_ci fprintf(stderr, "Catch signal. type is %d\n", type); 140f08c3bdfSopenharmony_ci 141f08c3bdfSopenharmony_ci switch (type) { 142f08c3bdfSopenharmony_ci case SIGHUP: 143f08c3bdfSopenharmony_ci catch_sighup = 1; 144f08c3bdfSopenharmony_ci break; 145f08c3bdfSopenharmony_ci case SIGPIPE: 146f08c3bdfSopenharmony_ci catch_sigpipe = 1; 147f08c3bdfSopenharmony_ci break; 148f08c3bdfSopenharmony_ci default: 149f08c3bdfSopenharmony_ci fprintf(stderr, "Unexpected signal (%d) is caught\n", type); 150f08c3bdfSopenharmony_ci exit(EXIT_FAILURE); 151f08c3bdfSopenharmony_ci } 152f08c3bdfSopenharmony_ci} 153f08c3bdfSopenharmony_ci 154f08c3bdfSopenharmony_ci/* 155f08c3bdfSopenharmony_ci * Function: delete_zombies() 156f08c3bdfSopenharmony_ci * 157f08c3bdfSopenharmony_ci * Descripton: 158f08c3bdfSopenharmony_ci * Delete the zombies 159f08c3bdfSopenharmony_ci * 160f08c3bdfSopenharmony_ci * Argument: 161f08c3bdfSopenharmony_ci * info_p: pointer to a server infomation 162f08c3bdfSopenharmony_ci * 163f08c3bdfSopenharmony_ci * Return value: 164f08c3bdfSopenharmony_ci * None 165f08c3bdfSopenharmony_ci */ 166f08c3bdfSopenharmony_civoid delete_zombies(struct server_info *info_p) 167f08c3bdfSopenharmony_ci{ 168f08c3bdfSopenharmony_ci int status; /* exit value of a child */ 169f08c3bdfSopenharmony_ci pid_t zombie_pid; /* process id of a zombie */ 170f08c3bdfSopenharmony_ci 171f08c3bdfSopenharmony_ci while (info_p->current_connection) { 172f08c3bdfSopenharmony_ci zombie_pid = waitpid((pid_t) - 1, &status, WNOHANG); 173f08c3bdfSopenharmony_ci if (zombie_pid == (pid_t) - 1) 174f08c3bdfSopenharmony_ci fatal_error("waitpid()"); 175f08c3bdfSopenharmony_ci else if (zombie_pid == (pid_t) 0) 176f08c3bdfSopenharmony_ci break; 177f08c3bdfSopenharmony_ci else { 178f08c3bdfSopenharmony_ci --info_p->current_connection; 179f08c3bdfSopenharmony_ci if (status != EXIT_SUCCESS) { 180f08c3bdfSopenharmony_ci ++info_p->lost_connection; 181f08c3bdfSopenharmony_ci if (debug) 182f08c3bdfSopenharmony_ci fprintf(stderr, 183f08c3bdfSopenharmony_ci "The number of lost conncections is %zu\n", 184f08c3bdfSopenharmony_ci info_p->lost_connection); 185f08c3bdfSopenharmony_ci } 186f08c3bdfSopenharmony_ci } 187f08c3bdfSopenharmony_ci } 188f08c3bdfSopenharmony_ci} 189f08c3bdfSopenharmony_ci 190f08c3bdfSopenharmony_ci/* 191f08c3bdfSopenharmony_ci * Function: create_listen_socket() 192f08c3bdfSopenharmony_ci * 193f08c3bdfSopenharmony_ci * Descripton: 194f08c3bdfSopenharmony_ci * Create a socket to listen for connections on a socket. 195f08c3bdfSopenharmony_ci * The socket discripter is stored info_p->listen_sd. 196f08c3bdfSopenharmony_ci * 197f08c3bdfSopenharmony_ci * Argument: 198f08c3bdfSopenharmony_ci * info_p: pointer to a server infomation 199f08c3bdfSopenharmony_ci * 200f08c3bdfSopenharmony_ci * Return value: 201f08c3bdfSopenharmony_ci * None 202f08c3bdfSopenharmony_ci */ 203f08c3bdfSopenharmony_civoid create_listen_socket(struct server_info *info_p) 204f08c3bdfSopenharmony_ci{ 205f08c3bdfSopenharmony_ci int on; /* on/off at an socket option */ 206f08c3bdfSopenharmony_ci int err; /* return value of getaddrinfo */ 207f08c3bdfSopenharmony_ci struct addrinfo hints; /* hints for getaddrinfo() */ 208f08c3bdfSopenharmony_ci struct addrinfo *res; /* pointer to addrinfo */ 209f08c3bdfSopenharmony_ci 210f08c3bdfSopenharmony_ci /* Set the hints to addrinfo() */ 211f08c3bdfSopenharmony_ci memset(&hints, '\0', sizeof(struct addrinfo)); 212f08c3bdfSopenharmony_ci hints.ai_family = info_p->family; 213f08c3bdfSopenharmony_ci hints.ai_socktype = SOCK_STREAM; 214f08c3bdfSopenharmony_ci hints.ai_protocol = IPPROTO_TCP; 215f08c3bdfSopenharmony_ci hints.ai_flags = AI_PASSIVE; 216f08c3bdfSopenharmony_ci 217f08c3bdfSopenharmony_ci /* Translate the network and service information of the server */ 218f08c3bdfSopenharmony_ci err = getaddrinfo(NULL, info_p->portnum, &hints, &res); 219f08c3bdfSopenharmony_ci if (err) { 220f08c3bdfSopenharmony_ci fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(err)); 221f08c3bdfSopenharmony_ci exit(EXIT_FAILURE); 222f08c3bdfSopenharmony_ci } 223f08c3bdfSopenharmony_ci if (res->ai_next) { 224f08c3bdfSopenharmony_ci fprintf(stderr, "getaddrinfo(): multiple address is found."); 225f08c3bdfSopenharmony_ci exit(EXIT_FAILURE); 226f08c3bdfSopenharmony_ci } 227f08c3bdfSopenharmony_ci 228f08c3bdfSopenharmony_ci /* Create a socket for listening. */ 229f08c3bdfSopenharmony_ci info_p->listen_sd = socket(res->ai_family, 230f08c3bdfSopenharmony_ci res->ai_socktype, res->ai_protocol); 231f08c3bdfSopenharmony_ci if (info_p->listen_sd < 0) 232f08c3bdfSopenharmony_ci fatal_error("socket()"); 233f08c3bdfSopenharmony_ci 234f08c3bdfSopenharmony_ci#ifdef IPV6_V6ONLY 235f08c3bdfSopenharmony_ci /* Don't accept IPv4 mapped address if the protocol family is IPv6 */ 236f08c3bdfSopenharmony_ci if (res->ai_family == PF_INET6) { 237f08c3bdfSopenharmony_ci on = 1; 238f08c3bdfSopenharmony_ci if (setsockopt(info_p->listen_sd, 239f08c3bdfSopenharmony_ci IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(int))) 240f08c3bdfSopenharmony_ci fatal_error("setsockopt()"); 241f08c3bdfSopenharmony_ci } 242f08c3bdfSopenharmony_ci#endif 243f08c3bdfSopenharmony_ci 244f08c3bdfSopenharmony_ci /* Enable to reuse the socket */ 245f08c3bdfSopenharmony_ci on = 1; 246f08c3bdfSopenharmony_ci if (setsockopt(info_p->listen_sd, 247f08c3bdfSopenharmony_ci SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int))) 248f08c3bdfSopenharmony_ci fatal_error("setsockopt()"); 249f08c3bdfSopenharmony_ci 250f08c3bdfSopenharmony_ci /* Disable the Nagle algorithm, when small sending mode */ 251f08c3bdfSopenharmony_ci if (info_p->small_sending) { 252f08c3bdfSopenharmony_ci on = 1; 253f08c3bdfSopenharmony_ci if (setsockopt(info_p->listen_sd, 254f08c3bdfSopenharmony_ci IPPROTO_TCP, TCP_NODELAY, &on, sizeof(int))) 255f08c3bdfSopenharmony_ci fatal_error("setsockopt()"); 256f08c3bdfSopenharmony_ci if (debug) { 257f08c3bdfSopenharmony_ci fprintf(stderr, "small sending[on]\n"); 258f08c3bdfSopenharmony_ci } 259f08c3bdfSopenharmony_ci } 260f08c3bdfSopenharmony_ci 261f08c3bdfSopenharmony_ci /* Maximize socket buffer, when window scaling mode */ 262f08c3bdfSopenharmony_ci if (info_p->window_scaling) 263f08c3bdfSopenharmony_ci maximize_sockbuf(info_p->listen_sd); 264f08c3bdfSopenharmony_ci 265f08c3bdfSopenharmony_ci /* Bind to the local address */ 266f08c3bdfSopenharmony_ci if (bind(info_p->listen_sd, res->ai_addr, res->ai_addrlen) < 0) 267f08c3bdfSopenharmony_ci fatal_error("bind()"); 268f08c3bdfSopenharmony_ci freeaddrinfo(res); 269f08c3bdfSopenharmony_ci 270f08c3bdfSopenharmony_ci /* Start to listen for connections */ 271f08c3bdfSopenharmony_ci if (listen(info_p->listen_sd, 5) < 0) 272f08c3bdfSopenharmony_ci fatal_error("listen()"); 273f08c3bdfSopenharmony_ci} 274f08c3bdfSopenharmony_ci 275f08c3bdfSopenharmony_ci/* 276f08c3bdfSopenharmony_ci * Function: communicate_client() 277f08c3bdfSopenharmony_ci * 278f08c3bdfSopenharmony_ci * Descripton: 279f08c3bdfSopenharmony_ci * Communicate with the connected client. 280f08c3bdfSopenharmony_ci * Currently, this function sends tcp segment in the specified second 281f08c3bdfSopenharmony_ci * or recevie SIGHUP 282f08c3bdfSopenharmony_ci * 283f08c3bdfSopenharmony_ci * Argument: 284f08c3bdfSopenharmony_ci * sock_fd: socket descriptor to communicate with client 285f08c3bdfSopenharmony_ci * info_p: pointer to a server infomation 286f08c3bdfSopenharmony_ci * 287f08c3bdfSopenharmony_ci * Return value: 288f08c3bdfSopenharmony_ci * 0: success 289f08c3bdfSopenharmony_ci * other: fail 290f08c3bdfSopenharmony_ci */ 291f08c3bdfSopenharmony_ciint communicate_client(struct server_info *info_p, int sock_fd) 292f08c3bdfSopenharmony_ci{ 293f08c3bdfSopenharmony_ci char *sendmsg; /* pointer to the message to send */ 294f08c3bdfSopenharmony_ci int sndbuf_size; /* size of the send buffer */ 295f08c3bdfSopenharmony_ci socklen_t sock_optlen; /* size of the result parameter */ 296f08c3bdfSopenharmony_ci ssize_t sntbyte_size; /* size of the sent byte */ 297f08c3bdfSopenharmony_ci int ret = EXIT_SUCCESS; /* The return value of this function */ 298f08c3bdfSopenharmony_ci 299f08c3bdfSopenharmony_ci if (info_p->small_sending) { /* small sending mode */ 300f08c3bdfSopenharmony_ci sndbuf_size = 1; 301f08c3bdfSopenharmony_ci } else { 302f08c3bdfSopenharmony_ci sock_optlen = sizeof(sndbuf_size); 303f08c3bdfSopenharmony_ci if (getsockopt 304f08c3bdfSopenharmony_ci (sock_fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, 305f08c3bdfSopenharmony_ci &sock_optlen) < 0) { 306f08c3bdfSopenharmony_ci perror("getsockopt()"); 307f08c3bdfSopenharmony_ci if (close(sock_fd)) 308f08c3bdfSopenharmony_ci fatal_error("close()"); 309f08c3bdfSopenharmony_ci return EXIT_FAILURE; 310f08c3bdfSopenharmony_ci } 311f08c3bdfSopenharmony_ci } 312f08c3bdfSopenharmony_ci if (debug) 313f08c3bdfSopenharmony_ci fprintf(stderr, "sndbuf size is %d\n", sndbuf_size); 314f08c3bdfSopenharmony_ci 315f08c3bdfSopenharmony_ci /* Define the message */ 316f08c3bdfSopenharmony_ci sendmsg = malloc(sndbuf_size); 317f08c3bdfSopenharmony_ci if (sendmsg == NULL) { 318f08c3bdfSopenharmony_ci fprintf(stderr, "malloc() is failed.\n"); 319f08c3bdfSopenharmony_ci if (close(sock_fd)) 320f08c3bdfSopenharmony_ci fatal_error("close()"); 321f08c3bdfSopenharmony_ci return EXIT_FAILURE; 322f08c3bdfSopenharmony_ci } 323f08c3bdfSopenharmony_ci 324f08c3bdfSopenharmony_ci /* Set a signal handler against SIGHUP and SIGPIPE */ 325f08c3bdfSopenharmony_ci handler.sa_handler = set_signal_flag; 326f08c3bdfSopenharmony_ci if (sigaction(SIGHUP, &handler, NULL) < 0) 327f08c3bdfSopenharmony_ci fatal_error("sigaction()"); 328f08c3bdfSopenharmony_ci if (sigaction(SIGPIPE, &handler, NULL) < 0) 329f08c3bdfSopenharmony_ci fatal_error("sigaction()"); 330f08c3bdfSopenharmony_ci 331f08c3bdfSopenharmony_ci /* Send the message */ 332f08c3bdfSopenharmony_ci for (;;) { 333f08c3bdfSopenharmony_ci sntbyte_size = send(sock_fd, sendmsg, sndbuf_size, 0); 334f08c3bdfSopenharmony_ci 335f08c3bdfSopenharmony_ci /* Catch SIGPIPE */ 336f08c3bdfSopenharmony_ci if (catch_sigpipe) { 337f08c3bdfSopenharmony_ci if (debug) 338f08c3bdfSopenharmony_ci fprintf(stderr, 339f08c3bdfSopenharmony_ci "The client closed the connection.\n"); 340f08c3bdfSopenharmony_ci break; 341f08c3bdfSopenharmony_ci } 342f08c3bdfSopenharmony_ci 343f08c3bdfSopenharmony_ci /* Catch SIGHUP */ 344f08c3bdfSopenharmony_ci if (catch_sighup) 345f08c3bdfSopenharmony_ci break; 346f08c3bdfSopenharmony_ci 347f08c3bdfSopenharmony_ci if (sntbyte_size < (ssize_t) 0) { 348f08c3bdfSopenharmony_ci if (errno == EPIPE) { 349f08c3bdfSopenharmony_ci if (debug) 350f08c3bdfSopenharmony_ci fprintf(stderr, 351f08c3bdfSopenharmony_ci "The client closed the connection.\n"); 352f08c3bdfSopenharmony_ci } else { 353f08c3bdfSopenharmony_ci printf("errno=%d\n", errno); 354f08c3bdfSopenharmony_ci perror("send()"); 355f08c3bdfSopenharmony_ci ret = EXIT_FAILURE; 356f08c3bdfSopenharmony_ci } 357f08c3bdfSopenharmony_ci break; 358f08c3bdfSopenharmony_ci } 359f08c3bdfSopenharmony_ci } 360f08c3bdfSopenharmony_ci 361f08c3bdfSopenharmony_ci free(sendmsg); 362f08c3bdfSopenharmony_ci if (close(sock_fd)) 363f08c3bdfSopenharmony_ci fatal_error("close()"); 364f08c3bdfSopenharmony_ci return ret; 365f08c3bdfSopenharmony_ci} 366f08c3bdfSopenharmony_ci 367f08c3bdfSopenharmony_ci/* 368f08c3bdfSopenharmony_ci * Function: handle_client() 369f08c3bdfSopenharmony_ci * 370f08c3bdfSopenharmony_ci * Descripton: 371f08c3bdfSopenharmony_ci * Accept a connection from a client, then fork to communicate the client 372f08c3bdfSopenharmony_ci * 373f08c3bdfSopenharmony_ci * Argument: 374f08c3bdfSopenharmony_ci * info_p: pointer to a server infomation 375f08c3bdfSopenharmony_ci * 376f08c3bdfSopenharmony_ci * Return value: 377f08c3bdfSopenharmony_ci * 0: success 378f08c3bdfSopenharmony_ci * other: fail 379f08c3bdfSopenharmony_ci */ 380f08c3bdfSopenharmony_ciint handle_client(struct server_info *info_p) 381f08c3bdfSopenharmony_ci{ 382f08c3bdfSopenharmony_ci int ret = EXIT_SUCCESS; /* return value of this function */ 383f08c3bdfSopenharmony_ci int do_accept = 1; /* if non-zero, accept connection */ 384f08c3bdfSopenharmony_ci fd_set read_fds; /* list of file descriptor for reading */ 385f08c3bdfSopenharmony_ci int max_read_fd = 0; /* maximum number in the read fds */ 386f08c3bdfSopenharmony_ci 387f08c3bdfSopenharmony_ci info_p->current_connection = 0; 388f08c3bdfSopenharmony_ci FD_ZERO(&read_fds); 389f08c3bdfSopenharmony_ci FD_SET(info_p->listen_sd, &read_fds); 390f08c3bdfSopenharmony_ci max_read_fd = info_p->listen_sd; 391f08c3bdfSopenharmony_ci 392f08c3bdfSopenharmony_ci /* Catch SIGHUP */ 393f08c3bdfSopenharmony_ci handler.sa_handler = set_signal_flag; 394f08c3bdfSopenharmony_ci if (sigaction(SIGHUP, &handler, NULL) < 0) 395f08c3bdfSopenharmony_ci fatal_error("sigaction()"); 396f08c3bdfSopenharmony_ci 397f08c3bdfSopenharmony_ci /* Loop to wait a new connection */ 398f08c3bdfSopenharmony_ci for (;;) { 399f08c3bdfSopenharmony_ci if (do_accept) { 400f08c3bdfSopenharmony_ci int data_sd; /* socket descriptor for send/recv data */ 401f08c3bdfSopenharmony_ci socklen_t client_addr_len; /* length of `client_addr' */ 402f08c3bdfSopenharmony_ci struct sockaddr_storage client_addr; /* address of a client */ 403f08c3bdfSopenharmony_ci int select_ret; /* return value of select() */ 404f08c3bdfSopenharmony_ci fd_set active_fds; /* list of the active file descriptor */ 405f08c3bdfSopenharmony_ci struct timeval select_timeout; /* timeout for select() */ 406f08c3bdfSopenharmony_ci 407f08c3bdfSopenharmony_ci /* When catch SIGHUP, no more connection is acceptted. */ 408f08c3bdfSopenharmony_ci if (catch_sighup) { 409f08c3bdfSopenharmony_ci do_accept = 0; 410f08c3bdfSopenharmony_ci if (close(info_p->listen_sd)) 411f08c3bdfSopenharmony_ci fatal_error("close()"); 412f08c3bdfSopenharmony_ci continue; 413f08c3bdfSopenharmony_ci } 414f08c3bdfSopenharmony_ci 415f08c3bdfSopenharmony_ci /* Check a connection is requested */ 416f08c3bdfSopenharmony_ci active_fds = read_fds; 417f08c3bdfSopenharmony_ci select_timeout.tv_sec = 0; /* 0.5 sec */ 418f08c3bdfSopenharmony_ci select_timeout.tv_usec = 500000; 419f08c3bdfSopenharmony_ci 420f08c3bdfSopenharmony_ci select_ret = select(max_read_fd + 1, 421f08c3bdfSopenharmony_ci &active_fds, NULL, NULL, 422f08c3bdfSopenharmony_ci &select_timeout); 423f08c3bdfSopenharmony_ci if (select_ret < 0) { 424f08c3bdfSopenharmony_ci do_accept = 0; 425f08c3bdfSopenharmony_ci if (!catch_sighup) { 426f08c3bdfSopenharmony_ci perror("select()"); 427f08c3bdfSopenharmony_ci ret = EXIT_FAILURE; 428f08c3bdfSopenharmony_ci } 429f08c3bdfSopenharmony_ci if (close(info_p->listen_sd)) 430f08c3bdfSopenharmony_ci fatal_error("close()"); 431f08c3bdfSopenharmony_ci continue; 432f08c3bdfSopenharmony_ci } else if (select_ret == 0) { /* select() is timeout */ 433f08c3bdfSopenharmony_ci if (info_p->concurrent) 434f08c3bdfSopenharmony_ci delete_zombies(info_p); 435f08c3bdfSopenharmony_ci continue; 436f08c3bdfSopenharmony_ci } 437f08c3bdfSopenharmony_ci 438f08c3bdfSopenharmony_ci /* Accetpt a client connection */ 439f08c3bdfSopenharmony_ci if (FD_ISSET(info_p->listen_sd, &active_fds)) { 440f08c3bdfSopenharmony_ci client_addr_len = 441f08c3bdfSopenharmony_ci sizeof(struct sockaddr_storage); 442f08c3bdfSopenharmony_ci data_sd = 443f08c3bdfSopenharmony_ci accept(info_p->listen_sd, 444f08c3bdfSopenharmony_ci (struct sockaddr *)&client_addr, 445f08c3bdfSopenharmony_ci &client_addr_len); 446f08c3bdfSopenharmony_ci if (data_sd < 0) { 447f08c3bdfSopenharmony_ci do_accept = 0; 448f08c3bdfSopenharmony_ci if (!catch_sighup) { 449f08c3bdfSopenharmony_ci perror("accept()"); 450f08c3bdfSopenharmony_ci ret = EXIT_FAILURE; 451f08c3bdfSopenharmony_ci } 452f08c3bdfSopenharmony_ci if (close(info_p->listen_sd)) 453f08c3bdfSopenharmony_ci fatal_error("close()"); 454f08c3bdfSopenharmony_ci continue; 455f08c3bdfSopenharmony_ci } 456f08c3bdfSopenharmony_ci if (debug) 457f08c3bdfSopenharmony_ci fprintf(stderr, 458f08c3bdfSopenharmony_ci "called accept(). data_sd=%d\n", 459f08c3bdfSopenharmony_ci data_sd); 460f08c3bdfSopenharmony_ci 461f08c3bdfSopenharmony_ci /* Handle clients */ 462f08c3bdfSopenharmony_ci if (info_p->concurrent) { /* concurrent server. */ 463f08c3bdfSopenharmony_ci pid_t child_pid; 464f08c3bdfSopenharmony_ci child_pid = fork(); 465f08c3bdfSopenharmony_ci if (child_pid < 0) { /* fork() is failed. */ 466f08c3bdfSopenharmony_ci perror("fork()"); 467f08c3bdfSopenharmony_ci if (close(data_sd)) 468f08c3bdfSopenharmony_ci fatal_error("close()"); 469f08c3bdfSopenharmony_ci if (close(info_p->listen_sd)) 470f08c3bdfSopenharmony_ci fatal_error("close()"); 471f08c3bdfSopenharmony_ci do_accept = 0; 472f08c3bdfSopenharmony_ci continue; 473f08c3bdfSopenharmony_ci } else if (child_pid == 0) { /* case of a child */ 474f08c3bdfSopenharmony_ci int exit_value; 475f08c3bdfSopenharmony_ci if (close(info_p->listen_sd)) 476f08c3bdfSopenharmony_ci fatal_error("close()"); 477f08c3bdfSopenharmony_ci exit_value = 478f08c3bdfSopenharmony_ci communicate_client(info_p, 479f08c3bdfSopenharmony_ci data_sd); 480f08c3bdfSopenharmony_ci if (debug) 481f08c3bdfSopenharmony_ci fprintf(stderr, 482f08c3bdfSopenharmony_ci "child(%d) exits. value is %d\n", 483f08c3bdfSopenharmony_ci getpid(), 484f08c3bdfSopenharmony_ci exit_value); 485f08c3bdfSopenharmony_ci exit(exit_value); 486f08c3bdfSopenharmony_ci } else { /* case of the parent */ 487f08c3bdfSopenharmony_ci if (close(data_sd)) 488f08c3bdfSopenharmony_ci fatal_error("close()"); 489f08c3bdfSopenharmony_ci 490f08c3bdfSopenharmony_ci ++info_p->current_connection; 491f08c3bdfSopenharmony_ci if (info_p->max_connection < 492f08c3bdfSopenharmony_ci info_p-> 493f08c3bdfSopenharmony_ci current_connection) { 494f08c3bdfSopenharmony_ci info_p->max_connection = 495f08c3bdfSopenharmony_ci info_p-> 496f08c3bdfSopenharmony_ci current_connection; 497f08c3bdfSopenharmony_ci if (debug) 498f08c3bdfSopenharmony_ci fprintf(stderr, 499f08c3bdfSopenharmony_ci "The maximum connection is updated. The number is %zu.\n", 500f08c3bdfSopenharmony_ci info_p-> 501f08c3bdfSopenharmony_ci max_connection); 502f08c3bdfSopenharmony_ci } 503f08c3bdfSopenharmony_ci delete_zombies(info_p); 504f08c3bdfSopenharmony_ci } 505f08c3bdfSopenharmony_ci } else { /* repeat server */ 506f08c3bdfSopenharmony_ci ret = 507f08c3bdfSopenharmony_ci communicate_client(info_p, data_sd); 508f08c3bdfSopenharmony_ci if (ret != EXIT_SUCCESS) 509f08c3bdfSopenharmony_ci if (close(info_p->listen_sd)) 510f08c3bdfSopenharmony_ci fatal_error("close()"); 511f08c3bdfSopenharmony_ci break; 512f08c3bdfSopenharmony_ci } 513f08c3bdfSopenharmony_ci } 514f08c3bdfSopenharmony_ci } else { 515f08c3bdfSopenharmony_ci /* case where new connection isn't accepted. */ 516f08c3bdfSopenharmony_ci if (info_p->concurrent) 517f08c3bdfSopenharmony_ci delete_zombies(info_p); 518f08c3bdfSopenharmony_ci if (info_p->current_connection == 0) 519f08c3bdfSopenharmony_ci break; 520f08c3bdfSopenharmony_ci } 521f08c3bdfSopenharmony_ci } 522f08c3bdfSopenharmony_ci return ret; 523f08c3bdfSopenharmony_ci} 524f08c3bdfSopenharmony_ci 525f08c3bdfSopenharmony_ci/* 526f08c3bdfSopenharmony_ci * 527f08c3bdfSopenharmony_ci * Function: main() 528f08c3bdfSopenharmony_ci * 529f08c3bdfSopenharmony_ci */ 530f08c3bdfSopenharmony_ciint main(int argc, char *argv[]) 531f08c3bdfSopenharmony_ci{ 532f08c3bdfSopenharmony_ci char *program_name = argv[0]; 533f08c3bdfSopenharmony_ci int optc; /* option */ 534f08c3bdfSopenharmony_ci struct server_info server; /* server information */ 535f08c3bdfSopenharmony_ci int ret = EXIT_SUCCESS; /* exit value */ 536f08c3bdfSopenharmony_ci int background = 0; /* If non-zero work in the background */ 537f08c3bdfSopenharmony_ci FILE *info_fp = stdout; /* FILE pointer to a information file */ 538f08c3bdfSopenharmony_ci 539f08c3bdfSopenharmony_ci debug = 0; 540f08c3bdfSopenharmony_ci 541f08c3bdfSopenharmony_ci /* Initilalize the server information */ 542f08c3bdfSopenharmony_ci memset(&server, '\0', sizeof(struct server_info)); 543f08c3bdfSopenharmony_ci server.family = PF_UNSPEC; 544f08c3bdfSopenharmony_ci server.portnum = NULL; 545f08c3bdfSopenharmony_ci 546f08c3bdfSopenharmony_ci /* Retrieve the options */ 547f08c3bdfSopenharmony_ci while ((optc = getopt(argc, argv, "f:p:bcswo:dh")) != EOF) { 548f08c3bdfSopenharmony_ci switch (optc) { 549f08c3bdfSopenharmony_ci case 'f': 550f08c3bdfSopenharmony_ci if (strncmp(optarg, "4", 1) == 0) 551f08c3bdfSopenharmony_ci server.family = PF_INET; /* IPv4 */ 552f08c3bdfSopenharmony_ci else if (strncmp(optarg, "6", 1) == 0) 553f08c3bdfSopenharmony_ci server.family = PF_INET6; /* IPv6 */ 554f08c3bdfSopenharmony_ci else { 555f08c3bdfSopenharmony_ci fprintf(stderr, 556f08c3bdfSopenharmony_ci "protocol family should be 4 or 6.\n"); 557f08c3bdfSopenharmony_ci usage(program_name, EXIT_FAILURE); 558f08c3bdfSopenharmony_ci } 559f08c3bdfSopenharmony_ci break; 560f08c3bdfSopenharmony_ci 561f08c3bdfSopenharmony_ci case 'p': 562f08c3bdfSopenharmony_ci { 563f08c3bdfSopenharmony_ci unsigned long int num; 564f08c3bdfSopenharmony_ci num = strtoul(optarg, NULL, 0); 565f08c3bdfSopenharmony_ci if (num < PORTNUMMIN || PORTNUMMAX < num) { 566f08c3bdfSopenharmony_ci fprintf(stderr, 567f08c3bdfSopenharmony_ci "The range of port is from %u to %u\n", 568f08c3bdfSopenharmony_ci PORTNUMMIN, PORTNUMMAX); 569f08c3bdfSopenharmony_ci usage(program_name, EXIT_FAILURE); 570f08c3bdfSopenharmony_ci } 571f08c3bdfSopenharmony_ci server.portnum = strdup(optarg); 572f08c3bdfSopenharmony_ci } 573f08c3bdfSopenharmony_ci break; 574f08c3bdfSopenharmony_ci 575f08c3bdfSopenharmony_ci case 'b': 576f08c3bdfSopenharmony_ci background = 1; 577f08c3bdfSopenharmony_ci break; 578f08c3bdfSopenharmony_ci 579f08c3bdfSopenharmony_ci case 'c': 580f08c3bdfSopenharmony_ci server.concurrent = 1; 581f08c3bdfSopenharmony_ci break; 582f08c3bdfSopenharmony_ci 583f08c3bdfSopenharmony_ci case 's': 584f08c3bdfSopenharmony_ci server.small_sending = 1; 585f08c3bdfSopenharmony_ci break; 586f08c3bdfSopenharmony_ci 587f08c3bdfSopenharmony_ci case 'w': 588f08c3bdfSopenharmony_ci server.window_scaling = 1; 589f08c3bdfSopenharmony_ci break; 590f08c3bdfSopenharmony_ci 591f08c3bdfSopenharmony_ci case 'o': 592f08c3bdfSopenharmony_ci if ((info_fp = fopen(optarg, "w")) == NULL) { 593f08c3bdfSopenharmony_ci fprintf(stderr, "Cannot open %s\n", optarg); 594f08c3bdfSopenharmony_ci exit(EXIT_FAILURE); 595f08c3bdfSopenharmony_ci } 596f08c3bdfSopenharmony_ci break; 597f08c3bdfSopenharmony_ci 598f08c3bdfSopenharmony_ci case 'd': 599f08c3bdfSopenharmony_ci debug = 1; 600f08c3bdfSopenharmony_ci break; 601f08c3bdfSopenharmony_ci 602f08c3bdfSopenharmony_ci case 'h': 603f08c3bdfSopenharmony_ci usage(program_name, EXIT_SUCCESS); 604f08c3bdfSopenharmony_ci break; 605f08c3bdfSopenharmony_ci 606f08c3bdfSopenharmony_ci default: 607f08c3bdfSopenharmony_ci usage(program_name, EXIT_FAILURE); 608f08c3bdfSopenharmony_ci } 609f08c3bdfSopenharmony_ci } 610f08c3bdfSopenharmony_ci 611f08c3bdfSopenharmony_ci /* Check the family is spefied. */ 612f08c3bdfSopenharmony_ci if (server.family == PF_UNSPEC) { 613f08c3bdfSopenharmony_ci fprintf(stderr, "protocol family should be specified.\n"); 614f08c3bdfSopenharmony_ci usage(program_name, EXIT_FAILURE); 615f08c3bdfSopenharmony_ci } 616f08c3bdfSopenharmony_ci 617f08c3bdfSopenharmony_ci /* Check the port number is specfied. */ 618f08c3bdfSopenharmony_ci if (server.portnum == NULL) { 619f08c3bdfSopenharmony_ci server.portnum = (char *)calloc(6, sizeof(char)); 620f08c3bdfSopenharmony_ci sprintf(server.portnum, "%u", PORTNUMMIN); 621f08c3bdfSopenharmony_ci } 622f08c3bdfSopenharmony_ci 623f08c3bdfSopenharmony_ci /* If -b option is specified, work as a daemon */ 624f08c3bdfSopenharmony_ci if (background) 625f08c3bdfSopenharmony_ci if (daemon(0, 0) < 0) 626f08c3bdfSopenharmony_ci fatal_error("daemon()"); 627f08c3bdfSopenharmony_ci 628f08c3bdfSopenharmony_ci /* At first, SIGHUP is ignored. default with SIGPIPE */ 629f08c3bdfSopenharmony_ci handler.sa_handler = SIG_IGN; 630f08c3bdfSopenharmony_ci if (sigfillset(&handler.sa_mask) < 0) 631f08c3bdfSopenharmony_ci fatal_error("sigfillset()"); 632f08c3bdfSopenharmony_ci handler.sa_flags = 0; 633f08c3bdfSopenharmony_ci 634f08c3bdfSopenharmony_ci if (sigaction(SIGHUP, &handler, NULL) < 0) 635f08c3bdfSopenharmony_ci fatal_error("sigaction()"); 636f08c3bdfSopenharmony_ci 637f08c3bdfSopenharmony_ci /* Create a listen socket */ 638f08c3bdfSopenharmony_ci create_listen_socket(&server); 639f08c3bdfSopenharmony_ci 640f08c3bdfSopenharmony_ci /* Output any server information to the information file */ 641f08c3bdfSopenharmony_ci fprintf(info_fp, "PID: %u\n", getpid()); 642f08c3bdfSopenharmony_ci fflush(info_fp); 643f08c3bdfSopenharmony_ci if (info_fp != stdout) 644f08c3bdfSopenharmony_ci if (fclose(info_fp)) 645f08c3bdfSopenharmony_ci fatal_error("fclose()"); 646f08c3bdfSopenharmony_ci 647f08c3bdfSopenharmony_ci /* Handle one or more tcp clients. */ 648f08c3bdfSopenharmony_ci ret = handle_client(&server); 649f08c3bdfSopenharmony_ci exit(ret); 650f08c3bdfSopenharmony_ci} 651