tcp_server.c
author viric@llimona
Fri, 16 Jun 2006 18:45:24 +0200
changeset 62 39bf7ecd7b21
parent 61 4cd174a9b698
permissions -rw-r--r--
IPV6_V6ONLY setsockopt added for the udp server.
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);
55
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
    21
static int sendlog(const int socket);
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    22
55
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
    23
/* Globals */
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
    24
static int tcp_server_pid = 0;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
    25
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
    26
/* Code */
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    27
/* 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
    28
int init_tcp_server()
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    29
{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    30
	int pid;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    31
	int port;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    32
	char service[MAX_STRING];
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    33
	enum {
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    34
		SUCCESS=0,
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    35
		ERROR=-1,
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    36
		DISABLED=-2
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    37
	} ret;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    38
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
	/* Get the configuration */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    41
	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
    42
	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
    43
		return DISABLED;
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
	/* Check the configuration */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    46
	port = atoi(service);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    47
	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
    48
		return ERROR;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    49
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    50
	/* Create the server process */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    51
	pid = child_fork();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    52
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    53
	if (pid == 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    54
	{
52
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
    55
		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
    56
		/* Child */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    57
		close(0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    58
		close(1);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    59
		//close(2);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    60
		if(port < 0)
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
			listen_tcp(-port, true);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    63
		} else
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    64
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    65
			listen_tcp(port, false);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    66
		}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    67
		/* Should be Not reachable */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    68
		exit(0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    69
	} else if (pid > 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    70
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    71
		/* Parent */
55
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
    72
		tcp_server_pid = pid;
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    73
		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
    74
			port, pid);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    75
		ret = SUCCESS;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    76
	} else
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
		ret = ERROR;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    79
	}
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
	return ret;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    82
}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    83
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    84
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    85
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
    86
{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    87
	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
    88
	fd_set listen_sockets;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    89
	int result;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    90
	int high_socket;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    91
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    92
	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
    93
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    94
	if (ipv6enabled)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    95
		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
    96
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    97
	/* 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
    98
	high_socket = socket_ipv4;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
    99
	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
   100
		high_socket = socket_ipv6;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   101
		
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   102
	high_socket += 1;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   103
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   104
	result = 0;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   105
	while (result >= 0)
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
		/* 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
   108
		FD_ZERO(&listen_sockets);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   109
		if (ipv6enabled)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   110
			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
   111
		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
   112
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   113
		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
   114
			NULL);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   115
		if (result == 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   116
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   117
			/* 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
   118
			continue;
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
		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
   121
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   122
			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
   123
				strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   124
			abort();
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
		/* Algun FD té dades... */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   128
		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
   129
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   130
			accept_connection(socket_ipv4);
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
		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
   134
		{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   135
			accept_connection(socket_ipv6);
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
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   138
}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   139
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   140
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   141
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
   142
{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   143
	int socket_ipv6;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   144
	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
   145
	int result;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   146
	int on=1;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   147
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
	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
   150
	if (socket_ipv6 == -1)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   151
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   152
		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
   153
		abort();
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
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   156
	/* 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
   157
	   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
   158
	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
   159
	                         (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
   160
	{
61
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   161
		fprintf(stderr, "IPv6 setsockopt() failed for SO_REUSEADDR: %s.\n",
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   162
			strerror(errno));
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   163
		abort();
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   164
	}
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   165
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   166
	/* Es necessita per a que linux no fagi Bind del port IPv4 alhora */
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   167
	if (setsockopt(socket_ipv6, IPPROTO_IPV6, IPV6_V6ONLY,
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   168
	                         (char *)&on,sizeof(on)) < 0)
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   169
	{
4cd174a9b698 Added the setsockopt for IPV6_V6ONLY, in order to have IPv6 and IPv4 in two
viric@llimona
parents: 58
diff changeset
   170
		fprintf(stderr, "IPv6 setsockopt() failed for IPV6_V6ONLY: %s.\n",
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   171
			strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   172
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   173
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   174
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   175
	/* 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
   176
	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
   177
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   178
		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
   179
			strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   180
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   181
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   182
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   183
	/* IPv6 listen address */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   184
	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
   185
	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
   186
	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
   187
	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
   188
	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
   189
	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
   190
		sizeof(source_ipv6));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   191
	assert(result == 0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   192
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   193
	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
   194
	if (result != 0)
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
		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
   197
		abort();
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
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   200
	return socket_ipv6;
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
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
   204
{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   205
	int socket_ipv4;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   206
	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
   207
	int result;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   208
	int on=1;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   209
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   210
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   211
	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
   212
	if (socket_ipv4 == -1)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   213
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   214
		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
   215
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   216
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   217
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   218
	/* 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
   219
	   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
   220
	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
   221
	                         (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
   222
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   223
		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
   224
			strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   225
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   226
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   227
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   228
	/* 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
   229
	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
   230
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   231
		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
   232
			strerror(errno));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   233
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   234
	}
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
	/* IPv4 listen address */
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   237
	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
   238
	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
   239
	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
   240
	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
   241
	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
   242
		sizeof(source_ipv4));
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   243
	assert(result == 0);
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   244
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   245
	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
   246
	if (result != 0)
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   247
	{
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   248
		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
   249
		abort();
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   250
	}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   251
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   252
	return socket_ipv4;
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   253
}
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   254
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   255
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   256
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
   257
{
52
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   258
	int newsock, pid;
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   259
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   260
	newsock = accept(socket, NULL, NULL);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   261
	if (newsock == -1)
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   262
		return 0;
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   263
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   264
	/* 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
   265
	pid = fork();
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   266
52
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   267
	if (pid == 0)
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   268
	{
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   269
		/* Child */
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   270
		close(socket);
55
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   271
		sendlog(newsock);
52
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   272
		close(newsock);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   273
		exit(0);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   274
		/* Unreachable */
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   275
	} else if (pid > 0)
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   276
	{
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   277
		/* Parent */
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   278
		close(newsock);
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   279
	}
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   280
	else
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   281
		return -1;
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   282
3af277b9f73b Removed the signal handler for the TCP server.
viric@llimona
parents: 51
diff changeset
   283
	return 0;
51
a01abd65856a The tcp server listen() started working. By now the connections are closed at once.
viric@llimona
parents:
diff changeset
   284
}
55
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   285
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   286
/* Return -2 when the socket is not enabled. Otherwise, the output of close().*/
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   287
void close_out_tcp()
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   288
{
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   289
	if (tcp_server_pid != 0)
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   290
	{
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   291
		fprintf(stderr,"close_out_tcp: Sending SIGTERM to %i\n", tcp_server_pid);
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   292
		kill(tcp_server_pid, SIGTERM);
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   293
		tcp_server_pid = 0;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   294
	}
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   295
	return;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   296
}
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   297
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   298
static int sendlog(const int socket)
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   299
{
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   300
	int fh;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   301
	int bytes, res;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   302
	enum config
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   303
	{
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   304
		BUFFER_SIZE=500
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   305
	};
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   306
	char buffer[BUFFER_SIZE];
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   307
	char *buffer2;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   308
	char name[MAX_STRING];
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   309
58
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   310
	if (get_config(LOG_FILE, name, MAX_STRING) < 0)
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   311
	{
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   312
		fprintf(stderr, "The tcp_server received a connection, but"
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   313
			" there isn't any log file.");
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   314
		return -1;
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   315
	}
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   316
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   317
	if (strncmp(name, "disabled", MAX_STRING) == 0)
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   318
	{
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   319
		fprintf(stderr, "The tcp_server received a connection, but"
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   320
			" there isn't any log file.");
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   321
		return -1;
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   322
	}
c03287b2c3c1 Now the tcp_server handles well the situation where there isn't a log file.
viric@llimona
parents: 56
diff changeset
   323
55
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   324
56
48b8acd6e878 The open() is done without mode_t mode.
viric@llimona
parents: 55
diff changeset
   325
	fh = open(name, O_RDONLY);
55
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   326
	if (fh == -1)
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   327
	{
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   328
		fprintf(stderr, "Error opening the log file for read: %s\n",
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   329
				strerror(errno));
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   330
		return -1;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   331
	}
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   332
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   333
	while((bytes = read(fh, buffer, BUFFER_SIZE)) > 0)
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   334
	{
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   335
		buffer2 = buffer;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   336
		while(bytes > 0)
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   337
		{
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   338
			res = send(socket, buffer2, bytes, 0);
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   339
			if (res == 0)
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   340
				sleep(1);
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   341
			else if (res < 0)
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   342
			{
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   343
				close(fh);
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   344
				return -1;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   345
			}
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   346
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   347
			bytes = bytes - res;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   348
			buffer2 = buffer2 + res;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   349
		}
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   350
	}
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   351
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   352
	close(fh);
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   353
	return bytes;
c72dbd390cf2 Now the tcp_server sends the log file.
viric@llimona
parents: 52
diff changeset
   354
}