tcp_server.c
author viric@llimona
Tue, 30 May 2006 01:10:00 +0200
changeset 53 667cd5966695
parent 52 3af277b9f73b
child 55 c72dbd390cf2
permissions -rw-r--r--
Finer message processing - now only at file and screen output appear '\n'.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
     1
#include <unistd.h> // Per les crides a fitxer
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
     2
#include <stdio.h>  // Per l'I/O de stdin/stdout
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
     3
#include <errno.h>  // Pels errors de les crides a fitxer
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
     4
#include <string.h> // Per strerror()
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
     5
#include <stdlib.h> // Per abort()
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
     6
#include <fcntl.h> // Per fcntl()
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
     7
#include <netinet/in.h>
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
     8
#include <arpa/inet.h>
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
     9
#include <stdbool.h>
52
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
    10
#include <signal.h>
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    11
#include <assert.h> // Per assert()
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    12
#include <sys/select.h> // Pel select()
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    13
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    14
#include "syslog.h"
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    15
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    16
/* Prototypes */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    17
static void listen_tcp(const int port, const bool ipv6enabled);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    18
static int listen_tcp_ipv6(int port);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    19
static int listen_tcp_ipv4(int port);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    20
static int accept_connection(int socket);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    21
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    22
/* Returns 0 on success. Returns -1 on error. */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    23
int init_tcp_server()
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    24
{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    25
	int pid;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    26
	int port;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    27
	char service[MAX_STRING];
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    28
	enum {
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    29
		SUCCESS=0,
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    30
		ERROR=-1,
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    31
		DISABLED=-2
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    32
	} ret;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    33
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    34
	
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    35
	/* Get the configuration */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    36
	get_config(TCP_MANAGER, service, MAX_STRING);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    37
	if (strncmp(service, "disabled", MAX_STRING) == 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    38
		return DISABLED;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    39
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    40
	/* Check the configuration */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    41
	port = atoi(service);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    42
	if (port < -65535 || port > 65535)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    43
		return ERROR;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    44
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    45
	/* Create the server process */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    46
	pid = child_fork();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    47
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    48
	if (pid == 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    49
	{
52
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
    50
		program_child_handler(SIG_DFL);
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    51
		/* Child */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    52
		close(0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    53
		close(1);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    54
		//close(2);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    55
		if(port < 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    56
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    57
			listen_tcp(-port, true);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    58
		} else
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    59
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    60
			listen_tcp(port, false);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    61
		}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    62
		/* Should be Not reachable */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    63
		exit(0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    64
	} else if (pid > 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    65
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    66
		/* Parent */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    67
		fprintf(stderr, "Child forked (tcp server on port %i): %i\n",
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    68
			port, pid);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    69
		ret = SUCCESS;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    70
	} else
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    71
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    72
		ret = ERROR;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    73
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    74
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    75
	return ret;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    76
}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    77
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    78
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    79
static void listen_tcp(const int port, const bool ipv6enabled)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    80
{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    81
	int socket_ipv6, socket_ipv4;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    82
	fd_set listen_sockets;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    83
	int result;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    84
	int high_socket;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    85
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    86
	socket_ipv4 = listen_tcp_ipv4(port);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    87
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    88
	if (ipv6enabled)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    89
		socket_ipv6 = listen_tcp_ipv6(port);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    90
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    91
	/* Mirem quin és el socket més alt pel select() */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    92
	high_socket = socket_ipv4;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    93
	if (ipv6enabled && (high_socket < socket_ipv6) )
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    94
		high_socket = socket_ipv6;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    95
		
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    96
	high_socket += 1;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    97
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    98
	result = 0;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    99
	while (result >= 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   100
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   101
		/* Establim els FDs que volem esperar al select() */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   102
		FD_ZERO(&listen_sockets);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   103
		if (ipv6enabled)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   104
			FD_SET(socket_ipv6, &listen_sockets);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   105
		FD_SET(socket_ipv4, &listen_sockets);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   106
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   107
		result = select(high_socket, &listen_sockets, NULL, NULL,
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   108
			NULL);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   109
		if (result == 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   110
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   111
			/* Això no hauria de passar, no tenim timeout fixat. */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   112
			continue;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   113
		}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   114
		else if (result == -1 && errno != EINTR)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   115
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   116
			fprintf(stderr, "Error in select(): %s\n",
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   117
				strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   118
			abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   119
		}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   120
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   121
		/* Algun FD té dades... */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   122
		if (FD_ISSET(socket_ipv4, &listen_sockets))
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   123
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   124
			accept_connection(socket_ipv4);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   125
		}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   126
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   127
		if (ipv6enabled && FD_ISSET(socket_ipv6, &listen_sockets))
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   128
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   129
			accept_connection(socket_ipv6);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   130
		}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   131
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   132
}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   133
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   134
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   135
static int listen_tcp_ipv6(int port)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   136
{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   137
	int socket_ipv6;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   138
	struct sockaddr_in6 source_ipv6;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   139
	int result;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   140
	int on=1;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   141
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   142
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   143
	socket_ipv6 = socket(PF_INET6, SOCK_STREAM, 0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   144
	if (socket_ipv6 == -1)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   145
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   146
		fprintf(stderr, "IPv6 socket failed.\n");
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   147
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   148
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   149
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   150
	/* Fem que poguem reutilitzar l'adreça encara que no s'hagi acabat
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   151
	   el timeout després de tancar-se la connexió */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   152
	if (setsockopt(socket_ipv6, SOL_SOCKET, SO_REUSEADDR,
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   153
	                         (char *)&on,sizeof(on)) < 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   154
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   155
		fprintf(stderr, "IPv6 setsockopt() failed: %s.\n",
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   156
			strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   157
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   158
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   159
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   160
	/* No volem que aquest socket bloquegi. */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   161
	if (fcntl(socket_ipv6, F_SETFL, O_NONBLOCK) == -1)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   162
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   163
		fprintf(stderr, "IPv6 fcntl() failed: %s.\n",
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   164
			strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   165
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   166
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   167
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   168
	/* IPv6 listen address */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   169
	memset(&source_ipv6, 0, sizeof(source_ipv6));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   170
	source_ipv6.sin6_family = AF_INET6;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   171
	source_ipv6.sin6_flowinfo = 0;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   172
	source_ipv6.sin6_port = htons(port);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   173
	source_ipv6.sin6_addr = in6addr_any;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   174
	result = bind(socket_ipv6, (struct sockaddr *) &source_ipv6,
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   175
		sizeof(source_ipv6));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   176
	assert(result == 0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   177
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   178
	result = listen(socket_ipv6, TCP_LISTEN_QUEUE);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   179
	if (result != 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   180
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   181
		fprintf(stderr,"Error in listen() ipv6: %s\n", strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   182
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   183
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   184
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   185
	return socket_ipv6;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   186
}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   187
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   188
static int listen_tcp_ipv4(int port)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   189
{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   190
	int socket_ipv4;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   191
	struct sockaddr_in source_ipv4;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   192
	int result;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   193
	int on=1;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   194
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   195
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   196
	socket_ipv4 = socket(PF_INET, SOCK_STREAM, 0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   197
	if (socket_ipv4 == -1)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   198
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   199
		fprintf(stderr, "IPv4 socket not supported.\n");
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   200
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   201
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   202
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   203
	/* Fem que poguem reutilitzar l'adreça encara que no s'hagi acabat
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   204
	   el timeout després de tancar-se la connexió */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   205
	if (setsockopt(socket_ipv4, SOL_SOCKET, SO_REUSEADDR,
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   206
	                         (char *)&on,sizeof(on)) < 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   207
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   208
		fprintf(stderr, "IPv4 setsockopt() failed: %s.\n",
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   209
			strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   210
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   211
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   212
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   213
	/* No volem que aquest socket bloquegi. */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   214
	if (fcntl(socket_ipv4, F_SETFL, O_NONBLOCK) == -1)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   215
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   216
		fprintf(stderr, "IPv6 fcntl() failed: %s.\n",
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   217
			strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   218
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   219
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   220
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   221
	/* IPv4 listen address */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   222
	memset(&source_ipv4, 0, sizeof(source_ipv4));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   223
	source_ipv4.sin_family = AF_INET;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   224
	source_ipv4.sin_port = htons(port);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   225
	source_ipv4.sin_addr.s_addr = htonl(INADDR_ANY);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   226
	result = bind(socket_ipv4, (struct sockaddr *) &source_ipv4,
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   227
		sizeof(source_ipv4));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   228
	assert(result == 0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   229
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   230
	result = listen(socket_ipv4, TCP_LISTEN_QUEUE);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   231
	if (result != 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   232
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   233
		fprintf(stderr,"Error in listen() ipv4: %s\n", strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   234
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   235
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   236
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   237
	return socket_ipv4;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   238
}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   239
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   240
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   241
static int accept_connection(int socket)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   242
{
52
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   243
	int newsock, pid;
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   244
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   245
	newsock = accept(socket, NULL, NULL);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   246
	if (newsock == -1)
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   247
		return 0;
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   248
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   249
	/* We don't use child_fork(), as it's only for the _main parent_ */
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   250
	pid = fork();
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   251
52
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   252
	if (pid == 0)
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   253
	{
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   254
		/* Child */
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   255
		close(socket);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   256
		send(newsock, "hola\n", 5, 0);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   257
		close(newsock);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   258
		exit(0);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   259
		/* Unreachable */
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   260
	} else if (pid > 0)
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   261
	{
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   262
		/* Parent */
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   263
		close(newsock);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   264
	}
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   265
	else
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   266
		return -1;
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   267
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   268
	return 0;
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   269
}