tcp_main.c
5b532c7f
 /*
  * $Id$
  *
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
5b532c7f
  *
  * This file is part of ser, a free SIP server.
  *
  * ser is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
b33736bc
 /*
  * History:
  * --------
  *  2002-11-29  created by andrei
  *  2002-12-11  added tcp_send (andrei)
  *  2003-01-20  locking fixes, hashtables (andrei)
  *  2003-02-20  s/lock_t/gen_lock_t/ to avoid a conflict on solaris (andrei)
  *  2003-02-25  Nagle is disabled if -DDISABLE_NAGLE (andrei)
e6509c23
  *  2003-03-29  SO_REUSEADDR before calling bind to allow
  *              server restart, Nagle set on the (hopefuly) 
f15bede1
  *              correct socket (jiri)
  *  2003-03-31  always try to find the corresponding tcp listen socket for
  *               a temp. socket and store in in *->bind_address: added
  *               find_tcp_si, modified tcpconn_connect (andrei)
39546e5f
  *  2003-04-14  set sockopts to TOS low delay (andrei)
f6e50f08
  *  2003-06-30  moved tcp new connect checking & handling to
  *               handle_new_connect (andrei)
f535fd9a
  *  2003-07-09  tls_close called before closing the tcp connection (andrei)
9f4c52ce
  *  2003-10-24  converted to the new socket_info lists (andrei)
59653eb8
  *  2003-10-27  tcp port aliases support added (andrei)
06aaa54f
  *  2003-11-04  always lock before manipulating refcnt; sendchild
  *              does not inc refcnt by itself anymore (andrei)
  *  2003-11-07  different unix sockets are used for fd passing
  *              to/from readers/writers (andrei)
76b0ff49
  *  2003-11-17  handle_new_connect & tcp_connect will close the 
  *              new socket if tcpconn_new return 0 (e.g. out of mem) (andrei)
c55f3308
  *  2003-11-28  tcp_blocking_write & tcp_blocking_connect added (andrei)
a35ad52c
  *  2004-11-08  dropped find_tcp_si and replaced with find_si (andrei)
0ba367ec
  *  2005-06-07  new tcp optimized code, supports epoll (LT), sigio + real time
  *               signals, poll & select (andrei)
741a9937
  *  2005-06-26  *bsd kqueue support (andrei)
9eda5956
  *  2005-07-04  solaris /dev/poll support (andrei)
55d8155e
  *  2005-07-08  tcp_max_connections, tcp_connection_lifetime, don't accept
  *               more connections if tcp_max_connections is exceeded (andrei)
32f8b7ed
  *  2005-10-21  cleanup all the open connections on exit
98f3d5e2
  *              decrement the no. of open connections on timeout too    (andrei) *  2006-01-30  queue send_fd request and execute them at the end of the
  *              poll loop  (#ifdef) (andrei)
  *              process all children requests, before attempting to send
  *              them new stuff (fixes some deadlocks) (andrei)
1ce28841
  *  2006-02-03  timers are run only once per s (andrei)
746f7674
  *              tcp children fds can be non-blocking; send fds are queued on
  *              EAGAIN; lots of bug fixes (andrei)
d9515405
  *  2006-02-06  better tcp_max_connections checks, tcp_connections_no moved to
  *              shm (andrei)
b33736bc
  */
5b532c7f
 
 
 #ifdef USE_TCP
 
6bc40dea
 
 #ifndef SHM_MEM
 #error "shared memory support needed (add -DSHM_MEM to Makefile.defs)"
 #endif
 
5b532c7f
 #include <sys/time.h>
 #include <sys/types.h>
b988daef
 #include <sys/select.h>
5b532c7f
 #include <sys/socket.h>
1f2c924e
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
39546e5f
 #include <netinet/ip.h>
b33736bc
 #include <netinet/tcp.h>
ab130758
 #include <sys/uio.h>  /* writev*/
b33736bc
 #include <netdb.h>
0ba367ec
 #include <stdlib.h> /*exit() */
5b532c7f
 
 #include <unistd.h>
 
 #include <errno.h>
 #include <string.h>
 
0ba367ec
 #ifdef HAVE_SELECT
 #include <sys/select.h>
 #endif
 #include <sys/poll.h>
5b532c7f
 
 
 #include "ip_addr.h"
 #include "pass_fd.h"
6bc40dea
 #include "tcp_conn.h"
5b532c7f
 #include "globals.h"
6ee62314
 #include "pt.h"
8aeb47e2
 #include "locking.h"
5b532c7f
 #include "mem/mem.h"
6ee62314
 #include "mem/shm_mem.h"
 #include "timer.h"
7ec958f3
 #include "sr_module.h"
0c5da34b
 #include "tcp_server.h"
f2f969dd
 #include "tcp_init.h"
518c9339
 #include "tsend.h"
746f7674
 #include "timer_ticks.h"
28427aa4
 #ifdef USE_TLS
 #include "tls/tls_server.h"
0ba367ec
 #endif 
d9515405
 #include "tcp_info.h"
8aeb47e2
 
5b532c7f
 #define local_malloc pkg_malloc
 #define local_free   pkg_free
 
0ba367ec
 #define HANDLE_IO_INLINE
 #include "io_wait.h"
 #include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
 
746f7674
 #define TCP_PASS_NEW_CONNECTION_ON_DATA /* don't pass a new connection
 										   immediately to a child, wait for
 										   some data on it first */
1ce28841
 #define TCP_LISTEN_BACKLOG 1024
28260509
 #define SEND_FD_QUEUE /* queue send fd requests on EAGAIN, instead of sending 
 							them immediately */
 #define TCP_CHILD_NON_BLOCKING 
98f3d5e2
 #ifdef SEND_FD_QUEUE
28260509
 #ifndef TCP_CHILD_NON_BLOCKING
 #define TCP_CHILD_NON_BLOCKING
 #endif
746f7674
 #define MAX_SEND_FD_QUEUE_SIZE	tcp_max_fd_no
98f3d5e2
 #define SEND_FD_QUEUE_SIZE		128  /* initial size */
746f7674
 #define MAX_SEND_FD_RETRIES		96	 /* FIXME: not used for now */
 #define SEND_FD_QUEUE_TIMEOUT	MS_TO_TICKS(2000)  /* 2 s */
98f3d5e2
 #endif
0ba367ec
 
 
 enum fd_types { F_NONE, F_SOCKINFO /* a tcp_listen fd */,
 				F_TCPCONN, F_TCPCHILD, F_PROC };
 
5b532c7f
 struct tcp_child{
 	pid_t pid;
6ee07a24
 	int proc_no; /* ser proc_no, for debugging */
06aaa54f
 	int unix_sock; /* unix "read child" sock fd */
5b532c7f
 	int busy;
 	int n_reqs; /* number of requests serviced so far */
 };
 
 
0ba367ec
 
59653eb8
 int tcp_accept_aliases=0; /* by default don't accept aliases */
bc977837
 int tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
 int tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
55d8155e
 int tcp_con_lifetime=DEFAULT_TCP_CONNECTION_LIFETIME;
0ba367ec
 enum poll_types tcp_poll_method=0; /* by default choose the best method */
55d8155e
 int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
 int tcp_max_fd_no=0;
 
d9515405
 static int* tcp_connections_no=0; /* current open connections */
5b532c7f
 
59653eb8
 /* connection hash table (after ip&port) , includes also aliases */
 struct tcp_conn_alias** tcpconn_aliases_hash=0;
8aeb47e2
 /* connection hash table (after connection id) */
 struct tcp_connection** tcpconn_id_hash=0;
0bd53297
 gen_lock_t* tcpconn_lock=0;
8aeb47e2
 
746f7674
 static struct tcp_child* tcp_children;
2719e69b
 static int* connection_id=0; /*  unique for each connection, used for 
0c5da34b
 								quickly finding the corresponding connection
 								for a reply */
 int unix_tcp_sock;
5b532c7f
 
0ba367ec
 static int tcp_proto_no=-1; /* tcp protocol number as returned by
 							   getprotobyname */
 
 static io_wait_h io_h;
5b532c7f
 
 
bc977837
 
 /* set all socket/fd options:  disable nagle, tos lowdelay, non-blocking
  * return -1 on error */
 static int init_sock_opt(int s)
 {
 	int flags;
 	int optval;
 	
 #ifdef DISABLE_NAGLE
 	flags=1;
 	if ( (tcp_proto_no!=-1) && (setsockopt(s, tcp_proto_no , TCP_NODELAY,
 					&flags, sizeof(flags))<0) ){
 		LOG(L_WARN, "WARNING: init_sock_opt: could not disable Nagle: %s\n",
 				strerror(errno));
 	}
 #endif
 	/* tos*/
fe09f315
 	optval = tos;
bc977837
 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){
 		LOG(L_WARN, "WARNING: init_sock_opt: setsockopt tos: %s\n",
 				strerror(errno));
 		/* continue since this is not critical */
 	}
 	/* non-blocking */
 	flags=fcntl(s, F_GETFL);
 	if (flags==-1){
 		LOG(L_ERR, "ERROR: init_sock_opt: fnctl failed: (%d) %s\n",
 				errno, strerror(errno));
 		goto error;
 	}
 	if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
 		LOG(L_ERR, "ERROR: init_sock_opt: fcntl: set non-blocking failed:"
 				" (%d) %s\n", errno, strerror(errno));
 		goto error;
 	}
 	return 0;
 error:
 	return -1;
 }
 
 
 
0ba367ec
 /* blocking connect on a non-blocking fd; it will timeout after
  * tcp_connect_timeout 
  * if BLOCKING_USE_SELECT and HAVE_SELECT are defined it will internally
  * use select() instead of poll (bad if fd > FD_SET_SIZE, poll is preferred)
  */
bc977837
 static int tcp_blocking_connect(int fd, const struct sockaddr *servaddr,
 								socklen_t addrlen)
 {
 	int n;
0ba367ec
 #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
bc977837
 	fd_set sel_set;
0ba367ec
 	fd_set orig_set;
bc977837
 	struct timeval timeout;
0ba367ec
 #else
 	struct pollfd pf;
 #endif
 	int elapsed;
 	int to;
bc977837
 	int ticks;
 	int err;
a9fae28b
 	unsigned int err_len;
a31a6e8c
 	int poll_err;
bc977837
 	
a31a6e8c
 	poll_err=0;
0ba367ec
 	to=tcp_connect_timeout;
 	ticks=get_ticks();
bc977837
 again:
 	n=connect(fd, servaddr, addrlen);
 	if (n==-1){
0ba367ec
 		if (errno==EINTR){
 			elapsed=(get_ticks()-ticks)*TIMER_TICK;
 			if (elapsed<to)		goto again;
 			else goto error_timeout;
 		}
bc977837
 		if (errno!=EINPROGRESS && errno!=EALREADY){
 			LOG(L_ERR, "ERROR: tcp_blocking_connect: (%d) %s\n",
 					errno, strerror(errno));
 			goto error;
 		}
 	}else goto end;
 	
0ba367ec
 	/* poll/select loop */
 #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
 		FD_ZERO(&orig_set);
 		FD_SET(fd, &orig_set);
 #else
 		pf.fd=fd;
 		pf.events=POLLOUT;
 #endif
bc977837
 	while(1){
0ba367ec
 		elapsed=(get_ticks()-ticks)*TIMER_TICK;
 		if (elapsed<to)
 			to-=elapsed;
 		else 
 			goto error_timeout;
 #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
 		sel_set=orig_set;
 		timeout.tv_sec=to;
bc977837
 		timeout.tv_usec=0;
 		n=select(fd+1, 0, &sel_set, 0, &timeout);
0ba367ec
 #else
 		n=poll(&pf, 1, to*1000);
 #endif
bc977837
 		if (n<0){
 			if (errno==EINTR) continue;
0ba367ec
 			LOG(L_ERR, "ERROR: tcp_blocking_connect: poll/select failed:"
 					" (%d) %s\n", errno, strerror(errno));
bc977837
 			goto error;
0ba367ec
 		}else if (n==0) /* timeout */ continue;
 #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
a31a6e8c
 		if (FD_ISSET(fd, &sel_set))
0ba367ec
 #else
 		if (pf.revents&(POLLERR|POLLHUP|POLLNVAL)){ 
a31a6e8c
 			LOG(L_ERR, "ERROR: tcp_blocking_connect: poll error: flags %x\n",
0ba367ec
 					pf.revents);
a31a6e8c
 			poll_err=1;
 		}
0ba367ec
 #endif
a31a6e8c
 		{
bc977837
 			err_len=sizeof(err);
 			getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
a31a6e8c
 			if ((err==0) && (poll_err==0)) goto end;
bc977837
 			if (err!=EINPROGRESS && err!=EALREADY){
 				LOG(L_ERR, "ERROR: tcp_blocking_connect: SO_ERROR (%d) %s\n",
 						err, strerror(err));
 				goto error;
 			}
 		}
 	}
0ba367ec
 error_timeout:
 	/* timeout */
 	LOG(L_ERR, "ERROR: tcp_blocking_connect: timeout %d s elapsed from %d s\n",
 			elapsed, tcp_connect_timeout);
bc977837
 error:
 	return -1;
 end:
 	return 0;
 }
 
 
 
518c9339
 #if 0
bc977837
 /* blocking write even on non-blocking sockets 
  * if TCP_TIMEOUT will return with error */
 static int tcp_blocking_write(struct tcp_connection* c, int fd, char* buf,
 								unsigned int len)
 {
 	int n;
 	fd_set sel_set;
 	struct timeval timeout;
 	int ticks;
 	int initial_len;
 	
 	initial_len=len;
 again:
 	
 	n=send(fd, buf, len,
 #ifdef HAVE_MSG_NOSIGNAL
 			MSG_NOSIGNAL
 #else
 			0
 #endif
 		);
 	if (n<0){
 		if (errno==EINTR)	goto again;
 		else if (errno!=EAGAIN && errno!=EWOULDBLOCK){
 			LOG(L_ERR, "tcp_blocking_write: failed to send: (%d) %s\n",
 					errno, strerror(errno));
 			goto error;
 		}
 	}else if (n<len){
 		/* partial write */
 		buf+=n;
 		len-=n;
 	}else{
 		/* success: full write */
 		goto end;
 	}
 	while(1){
 		FD_ZERO(&sel_set);
 		FD_SET(fd, &sel_set);
 		timeout.tv_sec=tcp_send_timeout;
 		timeout.tv_usec=0;
 		ticks=get_ticks();
 		n=select(fd+1, 0, &sel_set, 0, &timeout);
 		if (n<0){
 			if (errno==EINTR) continue; /* signal, ignore */
 			LOG(L_ERR, "ERROR: tcp_blocking_write: select failed: "
 					" (%d) %s\n", errno, strerror(errno));
 			goto error;
 		}else if (n==0){
 			/* timeout */
 			if (get_ticks()-ticks>=tcp_send_timeout){
 				LOG(L_ERR, "ERROR: tcp_blocking_write: send timeout (%d)\n",
 						tcp_send_timeout);
 				goto error;
 			}
 			continue;
 		}
 		if (FD_ISSET(fd, &sel_set)){
 			/* we can write again */
 			goto again;
 		}
 	}
 error:
 		return -1;
 end:
 		return initial_len;
 }
518c9339
 #endif
bc977837
 
 
 
f2f969dd
 struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
f6e50f08
 									struct socket_info* ba, int type, 
 									int state)
5b532c7f
 {
 	struct tcp_connection *c;
 	
6bc40dea
 	c=(struct tcp_connection*)shm_malloc(sizeof(struct tcp_connection));
5b532c7f
 	if (c==0){
f6e50f08
 		LOG(L_ERR, "ERROR: tcpconn_new: mem. allocation failure\n");
5b532c7f
 		goto error;
 	}
59653eb8
 	memset(c, 0, sizeof(struct tcp_connection)); /* zero init */
5b532c7f
 	c->s=sock;
e29d027b
 	c->fd=-1; /* not initialized */
ab130758
 	if (lock_init(&c->write_lock)==0){
f6e50f08
 		LOG(L_ERR, "ERROR: tcpconn_new: init lock failed\n");
ab130758
 		goto error;
 	}
 	
f2f969dd
 	c->rcv.src_su=*su;
 	
5b532c7f
 	c->refcnt=0;
f2f969dd
 	su2ip_addr(&c->rcv.src_ip, su);
 	c->rcv.src_port=su_getport(su);
 	c->rcv.bind_address=ba;
 	if (ba){
 		c->rcv.dst_ip=ba->address;
 		c->rcv.dst_port=ba->port_no;
 	}
9c01c860
 	print_ip("tcpconn_new: new tcp connection: ", &c->rcv.src_ip, "\n");
 	DBG(     "tcpconn_new: on port %d, type %d\n", c->rcv.src_port, type);
6bc40dea
 	init_tcp_req(&c->req);
2719e69b
 	c->id=(*connection_id)++;
f2f969dd
 	c->rcv.proto_reserved1=0; /* this will be filled before receive_message*/
 	c->rcv.proto_reserved2=0;
f6e50f08
 	c->state=state;
 	c->extra_data=0;
 #ifdef USE_TLS
 	if (type==PROTO_TLS){
28427aa4
 		if (tls_tcpconn_init(c, sock)==-1) goto error;
f6e50f08
 	}else
 #endif /* USE_TLS*/
 	{
 		c->type=PROTO_TCP;
 		c->rcv.proto=PROTO_TCP;
55d8155e
 		c->timeout=get_ticks()+tcp_con_lifetime;
f6e50f08
 	}
7564dcab
 	c->flags|=F_CONN_REMOVED;
 	
5b532c7f
 	return c;
 	
 error:
f6e50f08
 	if (c) shm_free(c);
5b532c7f
 	return 0;
 }
 
 
 
f6e50f08
 struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
0c5da34b
 {
 	int s;
f15bede1
 	struct socket_info* si;
 	union sockaddr_union my_name;
56b80d50
 	socklen_t my_name_len;
76b0ff49
 	struct tcp_connection* con;
a35ad52c
 	struct ip_addr ip;
0c5da34b
 
d9515405
 	s=-1;
 	
 	if (*tcp_connections_no >= tcp_max_connections){
 		LOG(L_ERR, "ERROR: tcpconn_connect: maximum number of connections"
 					" exceeded (%d/%d)\n",
 					*tcp_connections_no, tcp_max_connections);
 		goto error;
 	}
0c5da34b
 	s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
76b0ff49
 	if (s==-1){
0c5da34b
 		LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
 				errno, strerror(errno));
 		goto error;
 	}
bc977837
 	if (init_sock_opt(s)<0){
 		LOG(L_ERR, "ERROR: tcpconn_connect: init_sock_opt failed\n");
 		goto error;
39546e5f
 	}
bc977837
 	if (tcp_blocking_connect(s, &server->s, sockaddru_len(*server))<0){
 		LOG(L_ERR, "ERROR: tcpconn_connect: tcp_blocking_connect failed\n");
0c5da34b
 		goto error;
 	}
f15bede1
 	my_name_len=sizeof(my_name);
 	if (getsockname(s, &my_name.s, &my_name_len)!=0){
 		LOG(L_ERR, "ERROR: tcp_connect: getsockname failed: %s(%d)\n",
 				strerror(errno), errno);
 		si=0; /* try to go on */
 	}
a35ad52c
 	su2ip_addr(&ip, &my_name);
06aaa54f
 #ifdef USE_TLS
 	if (type==PROTO_TLS)
a35ad52c
 		si=find_si(&ip, 0, PROTO_TLS);
06aaa54f
 	else
 #endif
a35ad52c
 		si=find_si(&ip, 0, PROTO_TCP);
06aaa54f
 
f15bede1
 	if (si==0){
53c7e0f1
 		LOG(L_ERR, "ERROR: tcp_connect: could not find corresponding"
f15bede1
 				" listening socket, using default...\n");
 		if (server->s.sa_family==AF_INET) si=sendipv4_tcp;
 #ifdef USE_IPV6
 		else si=sendipv6_tcp;
 #endif
 	}
76b0ff49
 	con=tcpconn_new(s, server, si, type, S_CONN_CONNECT);
 	if (con==0){
 		LOG(L_ERR, "ERROR: tcp_connect: tcpconn_new failed, closing the "
 				 " socket\n");
 		goto error;
 	}
 	return con;
f6e50f08
 	/*FIXME: set sock idx! */
0c5da34b
 error:
76b0ff49
 	if (s!=-1) close(s); /* close the opened socket */
0c5da34b
 	return 0;
 }
 
 
 
 struct tcp_connection*  tcpconn_add(struct tcp_connection *c)
 {
8aeb47e2
 	unsigned hash;
 
 	if (c){
 		TCPCONN_LOCK;
 		/* add it at the begining of the list*/
 		hash=tcp_id_hash(c->id);
 		c->id_hash=hash;
 		tcpconn_listadd(tcpconn_id_hash[hash], c, id_next, id_prev);
59653eb8
 		
 		hash=tcp_addr_hash(&c->rcv.src_ip, c->rcv.src_port);
 		/* set the first alias */
 		c->con_aliases[0].port=c->rcv.src_port;
 		c->con_aliases[0].hash=hash;
 		c->con_aliases[0].parent=c;
 		tcpconn_listadd(tcpconn_aliases_hash[hash], &c->con_aliases[0],
 						next, prev);
 		c->aliases++;
8aeb47e2
 		TCPCONN_UNLOCK;
59653eb8
 		DBG("tcpconn_add: hashes: %d, %d\n", hash, c->id_hash);
8aeb47e2
 		return c;
 	}else{
 		LOG(L_CRIT, "tcpconn_add: BUG: null connection pointer\n");
 		return 0;
 	}
0c5da34b
 }
 
 
ab130758
 /* unsafe tcpconn_rm version (nolocks) */
 void _tcpconn_rm(struct tcp_connection* c)
 {
59653eb8
 	int r;
ab130758
 	tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
59653eb8
 	/* remove all the aliases */
 	for (r=0; r<c->aliases; r++)
 		tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash], 
 						&c->con_aliases[r], next, prev);
ab130758
 	lock_destroy(&c->write_lock);
f6e50f08
 #ifdef USE_TLS
744a2341
 	if (c->type==PROTO_TLS) tls_tcpconn_clean(c);
f6e50f08
 #endif
ab130758
 	shm_free(c);
 }
 
 
0c5da34b
 
5b532c7f
 void tcpconn_rm(struct tcp_connection* c)
 {
59653eb8
 	int r;
0c5da34b
 	TCPCONN_LOCK;
8aeb47e2
 	tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
59653eb8
 	/* remove all the aliases */
 	for (r=0; r<c->aliases; r++)
 		tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash], 
 						&c->con_aliases[r], next, prev);
0c5da34b
 	TCPCONN_UNLOCK;
ab130758
 	lock_destroy(&c->write_lock);
f6e50f08
 #ifdef USE_TLS
 	if ((c->type==PROTO_TLS)&&(c->extra_data)) tls_tcpconn_clean(c);
 #endif
6bc40dea
 	shm_free(c);
 }
 
 
6eacb2bc
 /* finds a connection, if id=0 uses the ip addr & port (host byte order)
8aeb47e2
  * WARNING: unprotected (locks) use tcpconn_get unless you really
  * know what you are doing */
 struct tcp_connection* _tcpconn_find(int id, struct ip_addr* ip, int port)
0c5da34b
 {
 
 	struct tcp_connection *c;
59653eb8
 	struct tcp_conn_alias* a;
8aeb47e2
 	unsigned hash;
0c5da34b
 	
f2e456c3
 #ifdef EXTRA_DEBUG
9c01c860
 	DBG("tcpconn_find: %d  port %d\n",id, port);
59653eb8
 	if (ip) print_ip("tcpconn_find: ip ", ip, "\n");
f2e456c3
 #endif
8aeb47e2
 	if (id){
 		hash=tcp_id_hash(id);
 		for (c=tcpconn_id_hash[hash]; c; c=c->id_next){
f2e456c3
 #ifdef EXTRA_DEBUG
9c01c860
 			DBG("c=%p, c->id=%d, port=%d\n",c, c->id, c->rcv.src_port);
 			print_ip("ip=", &c->rcv.src_ip, "\n");
f2e456c3
 #endif
f6e50f08
 			if ((id==c->id)&&(c->state!=S_CONN_BAD)) return c;
8aeb47e2
 		}
 	}else if (ip){
 		hash=tcp_addr_hash(ip, port);
59653eb8
 		for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
f2e456c3
 #ifdef EXTRA_DEBUG
59653eb8
 			DBG("a=%p, c=%p, c->id=%d, alias port= %d port=%d\n", a, a->parent,
 					a->parent->id, a->port, a->parent->rcv.src_port);
 			print_ip("ip=",&a->parent->rcv.src_ip,"\n");
f2e456c3
 #endif
59653eb8
 			if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
 					(ip_addr_cmp(ip, &a->parent->rcv.src_ip)) )
 				return a->parent;
8aeb47e2
 		}
0c5da34b
 	}
 	return 0;
 }
 
 
 
ab130758
 /* _tcpconn_find with locks and timeout */
 struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
 									int timeout)
0c5da34b
 {
 	struct tcp_connection* c;
 	TCPCONN_LOCK;
8aeb47e2
 	c=_tcpconn_find(id, ip, port);
ab130758
 	if (c){ 
 			c->refcnt++;
 			c->timeout=get_ticks()+timeout;
 	}
0c5da34b
 	TCPCONN_UNLOCK;
 	return c;
 }
 
 
 
59653eb8
 /* add port as an alias for the "id" connection
  * returns 0 on success,-1 on failure */
 int tcpconn_add_alias(int id, int port, int proto)
 {
 	struct tcp_connection* c;
 	unsigned hash;
 	struct tcp_conn_alias* a;
 	
 	a=0;
 	/* fix the port */
 	port=port?port:((proto==PROTO_TLS)?SIPS_PORT:SIP_PORT);
 	TCPCONN_LOCK;
 	/* check if alias already exists */
 	c=_tcpconn_find(id, 0, 0);
 	if (c){
 		hash=tcp_addr_hash(&c->rcv.src_ip, port);
 		/* search the aliases for an already existing one */
 		for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
 			if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
 					(ip_addr_cmp(&c->rcv.src_ip, &a->parent->rcv.src_ip)) ){
 				/* found */
 				if (a->parent!=c) goto error_sec;
 				else goto ok;
 			}
 		}
 		if (c->aliases>=TCP_CON_MAX_ALIASES) goto error_aliases;
 		c->con_aliases[c->aliases].parent=c;
 		c->con_aliases[c->aliases].port=port;
 		c->con_aliases[c->aliases].hash=hash;
 		tcpconn_listadd(tcpconn_aliases_hash[hash], 
 								&c->con_aliases[c->aliases], next, prev);
 		c->aliases++;
 	}else goto error_not_found;
 ok:
 	TCPCONN_UNLOCK;
 #ifdef EXTRA_DEBUG
 	if (a) DBG("tcpconn_add_alias: alias already present\n");
 	else   DBG("tcpconn_add_alias: alias port %d for hash %d, id %d\n",
 			port, hash, c->id);
 #endif
 	return 0;
 error_aliases:
 	TCPCONN_UNLOCK;
 	LOG(L_ERR, "ERROR: tcpconn_add_alias: too many aliases for connection %p"
 				" (%d)\n", c, c->id);
 	return -1;
 error_not_found:
 	TCPCONN_UNLOCK;
 	LOG(L_ERR, "ERROR: tcpconn_add_alias: no connection found for id %d\n",id);
 	return -1;
 error_sec:
 	TCPCONN_UNLOCK;
53c7e0f1
 	LOG(L_ERR, "ERROR: tcpconn_add_alias: possible port hijack attempt\n");
59653eb8
 	LOG(L_ERR, "ERROR: tcpconn_add_alias: alias already present and points"
 			" to another connection (%d : %d and %d : %d)\n",
 			a->parent->id,  port, c->id, port);
 	return -1;
 }
 
 
 
06aaa54f
 void tcpconn_ref(struct tcp_connection* c)
 {
 	TCPCONN_LOCK;
 	c->refcnt++; /* FIXME: atomic_dec */
 	TCPCONN_UNLOCK;
 }
 
 
 
0c5da34b
 void tcpconn_put(struct tcp_connection* c)
 {
06aaa54f
 	TCPCONN_LOCK;
0c5da34b
 	c->refcnt--; /* FIXME: atomic_dec */
06aaa54f
 	TCPCONN_UNLOCK;
0c5da34b
 }
 
 
 
 /* finds a tcpconn & sends on it */
e9b02e8e
 int tcp_send(int type, char* buf, unsigned len, union sockaddr_union* to,
 				int id)
0c5da34b
 {
 	struct tcp_connection *c;
06aaa54f
 	struct tcp_connection *tmp;
0c5da34b
 	struct ip_addr ip;
 	int port;
e29d027b
 	int fd;
0c5da34b
 	long response[2];
 	int n;
 	
f2f969dd
 	port=0;
 	if (to){
 		su2ip_addr(&ip, to);
 		port=su_getport(to);
55d8155e
 		c=tcpconn_get(id, &ip, port, tcp_con_lifetime); 
f2f969dd
 	}else if (id){
55d8155e
 		c=tcpconn_get(id, 0, 0, tcp_con_lifetime);
f2f969dd
 	}else{
 		LOG(L_CRIT, "BUG: tcp_send called with null id & to\n");
 		return -1;
 	}
0c5da34b
 	
 	if (id){
 		if (c==0) {
f2f969dd
 			if (to){
ab130758
 				/* try again w/o id */
55d8155e
 				c=tcpconn_get(0, &ip, port, tcp_con_lifetime);
f2f969dd
 				goto no_id;
 			}else{
 				LOG(L_ERR, "ERROR: tcp_send: id %d not found, dropping\n",
 						id);
 				return -1;
 			}
 		}else goto get_fd;
 	}
 no_id:
0c5da34b
 		if (c==0){
 			DBG("tcp_send: no open tcp connection found, opening new one\n");
 			/* create tcp connection */
e9b02e8e
 			if ((c=tcpconn_connect(to, type))==0){
0c5da34b
 				LOG(L_ERR, "ERROR: tcp_send: connect failed\n");
534092ba
 				return -1;
0c5da34b
 			}
06aaa54f
 			c->refcnt++; /* safe to do it w/o locking, it's not yet
 							available to the rest of the world */
534092ba
 			fd=c->s;
0c5da34b
 			
 			/* send the new tcpconn to "tcp main" */
 			response[0]=(long)c;
 			response[1]=CONN_NEW;
0ba367ec
 			n=send_fd(unix_tcp_sock, response, sizeof(response), c->s);
06aaa54f
 			if (n<=0){
534092ba
 				LOG(L_ERR, "BUG: tcp_send: failed send_fd: %s (%d)\n",
 						strerror(errno), errno);
06aaa54f
 				n=-1;
534092ba
 				goto end;
0ba367ec
 			}	
f2f969dd
 			goto send_it;
 		}
 get_fd:
e29d027b
 			/* todo: see if this is not the same process holding
 			 *  c  and if so send directly on c->fd */
06aaa54f
 			DBG("tcp_send: tcp connection found (%p), acquiring fd\n", c);
0c5da34b
 			/* get the fd */
 			response[0]=(long)c;
 			response[1]=CONN_GET_FD;
06aaa54f
 			n=send_all(unix_tcp_sock, response, sizeof(response));
 			if (n<=0){
534092ba
 				LOG(L_ERR, "BUG: tcp_send: failed to get fd(write):%s (%d)\n",
 						strerror(errno), errno);
06aaa54f
 				n=-1;
534092ba
 				goto release_c;
 			}
f2f969dd
 			DBG("tcp_send, c= %p, n=%d\n", c, n);
06aaa54f
 			tmp=c;
0ba367ec
 			n=receive_fd(unix_tcp_sock, &c, sizeof(c), &fd, MSG_WAITALL);
06aaa54f
 			if (n<=0){
534092ba
 				LOG(L_ERR, "BUG: tcp_send: failed to get fd(receive_fd):"
 							" %s (%d)\n", strerror(errno), errno);
06aaa54f
 				n=-1;
 				goto release_c;
 			}
 			if (c!=tmp){
 				LOG(L_CRIT, "BUG: tcp_send: get_fd: got different connection:"
 						"  %p (id= %d, refcnt=%d state=%d != "
 						"  %p (id= %d, refcnt=%d state=%d (n=%d)\n",
 						  c,   c->id,   c->refcnt,   c->state,
 						  tmp, tmp->id, tmp->refcnt, tmp->state, n
 				   );
 				n=-1; /* fail */
76b0ff49
 				goto end;
534092ba
 			}
e29d027b
 			DBG("tcp_send: after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
f2f969dd
 		
0c5da34b
 	
f2f969dd
 	
 send_it:
0c5da34b
 	DBG("tcp_send: sending...\n");
ab130758
 	lock_get(&c->write_lock);
e9b02e8e
 #ifdef USE_TLS
 	if (c->type==PROTO_TLS)
 		n=tls_blocking_write(c, fd, buf, len);
 	else
 #endif
518c9339
 		/* n=tcp_blocking_write(c, fd, buf, len); */
 		n=tsend_stream(fd, buf, len, tcp_send_timeout*1000); 
ab130758
 	lock_release(&c->write_lock);
e29d027b
 	DBG("tcp_send: after write: c= %p n=%d fd=%d\n",c, n, fd);
ab130758
 	DBG("tcp_send: buf=\n%.*s\n", (int)len, buf);
 	if (n<0){
bc977837
 		LOG(L_ERR, "ERROR: tcp_send: failed to send\n");
ab130758
 		/* error on the connection , mark it as bad and set 0 timeout */
f6e50f08
 		c->state=S_CONN_BAD;
ab130758
 		c->timeout=0;
 		/* tell "main" it should drop this (optional it will t/o anyway?)*/
 		response[0]=(long)c;
 		response[1]=CONN_ERROR;
06aaa54f
 		n=send_all(unix_tcp_sock, response, sizeof(response));
 		/* CONN_ERROR will auto-dec refcnt => we must not call tcpconn_put !!*/
 		if (n<=0){
1c93f767
 			LOG(L_ERR, "BUG: tcp_send: error return failed (write):%s (%d)\n",
ab130758
 					strerror(errno), errno);
06aaa54f
 			n=-1;
ab130758
 		}
1c93f767
 		close(fd);
 		return n; /* error return, no tcpconn_put */
ab130758
 	}
534092ba
 end:
e29d027b
 	close(fd);
534092ba
 release_c:
0c5da34b
 	tcpconn_put(c); /* release c (lock; dec refcnt; unlock) */
 	return n;
 }
 
 
 
f2f969dd
 int tcp_init(struct socket_info* sock_info)
5b532c7f
 {
 	union sockaddr_union* addr;
e6509c23
 	int optval;
b33736bc
 #ifdef DISABLE_NAGLE
 	int flag;
 	struct protoent* pe;
7ec958f3
 
 	if (tcp_proto_no==-1){ /* if not already set */
 		pe=getprotobyname("tcp");
 		if (pe==0){
 			LOG(L_ERR, "ERROR: tcp_init: could not get TCP protocol number\n");
 			tcp_proto_no=-1;
 		}else{
 			tcp_proto_no=pe->p_proto;
 		}
b33736bc
 	}
 #endif
5b532c7f
 	
 	addr=&sock_info->su;
faa66933
 	/* sock_info->proto=PROTO_TCP; */
6eacb2bc
 	if (init_su(addr, &sock_info->address, sock_info->port_no)<0){
5b532c7f
 		LOG(L_ERR, "ERROR: tcp_init: could no init sockaddr_union\n");
 		goto error;
 	}
 	sock_info->socket=socket(AF2PF(addr->s.sa_family), SOCK_STREAM, 0);
 	if (sock_info->socket==-1){
 		LOG(L_ERR, "ERROR: tcp_init: socket: %s\n", strerror(errno));
 		goto error;
 	}
b33736bc
 #ifdef DISABLE_NAGLE
 	flag=1;
 	if ( (tcp_proto_no!=-1) &&
 		 (setsockopt(sock_info->socket, tcp_proto_no , TCP_NODELAY,
 					 &flag, sizeof(flag))<0) ){
 		LOG(L_ERR, "ERROR: tcp_init: could not disable Nagle: %s\n",
 				strerror(errno));
 	}
 #endif
e6509c23
 
 
39546e5f
 #if  !defined(TCP_DONT_REUSEADDR) 
e6509c23
 	/* Stevens, "Network Programming", Section 7.5, "Generic Socket
      * Options": "...server started,..a child continues..on existing
 	 * connection..listening server is restarted...call to bind fails
 	 * ... ALL TCP servers should specify the SO_REUSEADDRE option 
 	 * to allow the server to be restarted in this situation
 	 *
 	 * Indeed, without this option, the server can't restart.
 	 *   -jiri
 	 */
 	optval=1;
 	if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR,
 				(void*)&optval, sizeof(optval))==-1) {
 		LOG(L_ERR, "ERROR: tcp_init: setsockopt %s\n",
 			strerror(errno));
 		goto error;
 	}
 #endif
39546e5f
 	/* tos */
fe09f315
 	optval = tos;
39546e5f
 	if (setsockopt(sock_info->socket, IPPROTO_IP, IP_TOS, (void*)&optval, 
 				sizeof(optval)) ==-1){
 		LOG(L_WARN, "WARNING: tcp_init: setsockopt tos: %s\n", strerror(errno));
 		/* continue since this is not critical */
 	}
5b532c7f
 	if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
0ba367ec
 		LOG(L_ERR, "ERROR: tcp_init: bind(%x, %p, %d) on %s:%d : %s\n",
 				sock_info->socket,  &addr->s, 
4d080f49
 				(unsigned)sockaddru_len(*addr),
5b532c7f
 				sock_info->address_str.s,
0ba367ec
 				sock_info->port_no,
5b532c7f
 				strerror(errno));
 		goto error;
 	}
1ce28841
 	if (listen(sock_info->socket, TCP_LISTEN_BACKLOG)==-1){
5b532c7f
 		LOG(L_ERR, "ERROR: tcp_init: listen(%x, %p, %d) on %s: %s\n",
 				sock_info->socket, &addr->s, 
4d080f49
 				(unsigned)sockaddru_len(*addr),
5b532c7f
 				sock_info->address_str.s,
 				strerror(errno));
 		goto error;
 	}
 	
 	return 0;
 error:
 	if (sock_info->socket!=-1){
 		close(sock_info->socket);
 		sock_info->socket=-1;
 	}
 	return -1;
 }
 
 
 
746f7674
 /* used internally by tcp_main_loop() */
 static void tcpconn_destroy(struct tcp_connection* tcpconn)
 {
 	int fd;
 
 	TCPCONN_LOCK; /*avoid races w/ tcp_send*/
 	tcpconn->refcnt--;
 	if (tcpconn->refcnt==0){ 
 		DBG("tcpconn_destroy: destroying connection %p, flags %04x\n",
 				tcpconn, tcpconn->flags);
 		fd=tcpconn->s;
 #ifdef USE_TLS
 		/*FIXME: lock ->writelock ? */
 		if (tcpconn->type==PROTO_TLS)
 			tls_close(tcpconn, fd);
 #endif
 		_tcpconn_rm(tcpconn);
 		close(fd);
d9515405
 		(*tcp_connections_no)--;
746f7674
 	}else{
 		/* force timeout */
 		tcpconn->timeout=0;
 		tcpconn->state=S_CONN_BAD;
 		DBG("tcpconn_destroy: delaying (%p, flags %04x) ...\n",
 				tcpconn, tcpconn->flags);
 		
 	}
 	TCPCONN_UNLOCK;
 }
 
 
 
98f3d5e2
 #ifdef SEND_FD_QUEUE
 struct send_fd_info{
 	struct tcp_connection* tcp_conn;
746f7674
 	ticks_t expire;
98f3d5e2
 	int unix_sock;
746f7674
 	unsigned int retries; /* debugging */
98f3d5e2
 };
 
 struct tcp_send_fd_q{
 	struct send_fd_info* data; /* buffer */
 	struct send_fd_info* crt;  /* pointer inside the buffer */
 	struct send_fd_info* end;  /* points after the last valid position */
 };
 
 
 static struct tcp_send_fd_q send2child_q;
 
 
 
 static int send_fd_queue_init(struct tcp_send_fd_q *q, unsigned int size)
5b532c7f
 {
98f3d5e2
 	q->data=pkg_malloc(size*sizeof(struct send_fd_info));
 	if (q->data==0){
 		LOG(L_ERR, "ERROR: send_fd_queue_init: out of memory\n");
06aaa54f
 		return -1;
 	}
98f3d5e2
 	q->crt=&q->data[0];
 	q->end=&q->data[size];
06aaa54f
 	return 0;
5b532c7f
 }
 
98f3d5e2
 static void send_fd_queue_destroy(struct tcp_send_fd_q *q)
 {
 	if (q->data){
 		pkg_free(q->data);
 		q->data=0;
 		q->crt=q->end=0;
 	}
 }
5b532c7f
 
98f3d5e2
 
 
 static int init_send_fd_queues()
f6e50f08
 {
98f3d5e2
 	if (send_fd_queue_init(&send2child_q, SEND_FD_QUEUE_SIZE)!=0)
 		goto error;
 	return 0;
 error:
 	LOG(L_ERR, "ERROR: init_send_fd_queues: init failed\n");
 	return -1;
 }
 
 
 
 static void destroy_send_fd_queues()
 {
 	send_fd_queue_destroy(&send2child_q);
 }
 
 
 
 
 inline static int send_fd_queue_add(	struct tcp_send_fd_q* q, 
 										int unix_sock,
 										struct tcp_connection *t)
 {
 	struct send_fd_info* tmp;
 	unsigned long new_size;
f6e50f08
 	
98f3d5e2
 	if (q->crt>=q->end){
 		new_size=q->end-&q->data[0];
 		if (new_size< MAX_SEND_FD_QUEUE_SIZE/2){
 			new_size*=2;
 		}else new_size=MAX_SEND_FD_QUEUE_SIZE;
 		if (q->crt>=&q->data[new_size]){
 			LOG(L_ERR, "ERROR: send_fd_queue_add: queue full: %ld/%ld\n",
d9515405
 					(long)(q->crt-&q->data[0]-1), new_size);
98f3d5e2
 			goto error;
 		}
 		LOG(L_CRIT, "INFO: send_fd_queue: queue full: %ld, extending to %ld\n",
d9515405
 				(long)(q->end-&q->data[0]), new_size);
98f3d5e2
 		tmp=pkg_realloc(q->data, new_size*sizeof(struct send_fd_info));
 		if (tmp==0){
 			LOG(L_ERR, "ERROR: send_fd_queue_add: out of memory\n");
 			goto error;
 		}
 		q->crt=(q->crt-&q->data[0])+tmp;
 		q->data=tmp;
 		q->end=&q->data[new_size];
0ba367ec
 	}
98f3d5e2
 	q->crt->tcp_conn=t;
 	q->crt->unix_sock=unix_sock;
746f7674
 	q->crt->expire=get_ticks_raw()+SEND_FD_QUEUE_TIMEOUT;
98f3d5e2
 	q->crt->retries=0;
 	q->crt++;
 	return 0;
 error:
 	return -1;
 }
 
 
 
 inline static void send_fd_queue_run(struct tcp_send_fd_q* q)
 {
 	struct send_fd_info* p;
 	struct send_fd_info* t;
0ba367ec
 	
98f3d5e2
 	for (p=t=&q->data[0]; p<q->crt; p++){
 		if (send_fd(p->unix_sock, &(p->tcp_conn),
 					sizeof(struct tcp_connection*), p->tcp_conn->s)<=0){
28260509
 			if ( ((errno==EAGAIN)||(errno==EWOULDBLOCK)) && 
746f7674
 							((s_ticks_t)(p->expire-get_ticks_raw())>0)){
98f3d5e2
 				/* leave in queue for a future try */
 				*t=*p;
 				t->retries++;
 				t++;
 			}else{
28260509
 				LOG(L_ERR, "ERROR: run_send_fd_queue: send_fd failed"
746f7674
 						   " on socket %d , queue entry %ld, retries %d,"
 						   " connection %p, tcp socket %d, errno=%d (%s) \n",
d9515405
 						   p->unix_sock, (long)(p-&q->data[0]), p->retries,
746f7674
 						   p->tcp_conn, p->tcp_conn->s, errno,
 						   strerror(errno));
 				tcpconn_destroy(p->tcp_conn);
98f3d5e2
 			}
bc977837
 		}
f6e50f08
 	}
98f3d5e2
 	q->crt=t;
f6e50f08
 }
98f3d5e2
 #else
 #define send_fd_queue_run(q)
 #endif
f6e50f08
 
 
0ba367ec
 
 /* handles io from a tcp child process
  * params: tcp_c - pointer in the tcp_children array, to the entry for
  *                 which an io event was detected 
  *         fd_i  - fd index in the fd_array (usefull for optimizing
  *                 io_watch_deletes)
  * returns:  handle_* return convention: -1 on error, 0 on EAGAIN (no more
  *           io events queued), >0 on success. success/error refer only to
  *           the reads from the fd.
  */
 inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
5b532c7f
 {
 	struct tcp_connection* tcpconn;
 	long response[2];
0c5da34b
 	int cmd;
6a157851
 	int bytes;
0ba367ec
 	
 	if (tcp_c->unix_sock<=0){
 		/* (we can't have a fd==0, 0 is never closed )*/
 		LOG(L_CRIT, "BUG: handle_tcp_child: fd %d for %d "
 				"(pid %d, ser no %d)\n", tcp_c->unix_sock,
 				(int)(tcp_c-&tcp_children[0]), tcp_c->pid, tcp_c->proc_no);
 		goto error;
 	}
 	/* read until sizeof(response)
 	 * (this is a SOCK_STREAM so read is not atomic) */
 	bytes=recv_all(tcp_c->unix_sock, response, sizeof(response), MSG_DONTWAIT);
 	if (bytes<(int)sizeof(response)){
 		if (bytes==0){
 			/* EOF -> bad, child has died */
 			DBG("DBG: handle_tcp_child: dead tcp child %d (pid %d, no %d)"
 					" (shutting down?)\n", (int)(tcp_c-&tcp_children[0]), 
 					tcp_c->pid, tcp_c->proc_no );
741a9937
 			/* don't listen on it any more */
 			io_watch_del(&io_h, tcp_c->unix_sock, fd_i, 0); 
0ba367ec
 			goto error; /* eof. so no more io here, it's ok to return error */
 		}else if (bytes<0){
 			/* EAGAIN is ok if we try to empty the buffer
 			 * e.g.: SIGIO_RT overflow mode or EPOLL ET */
 			if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
2b04f2a8
 				LOG(L_CRIT, "ERROR: handle_tcp_child: read from tcp child %ld "
0ba367ec
 						" (pid %d, no %d) %s [%d]\n",
2b04f2a8
 						(long)(tcp_c-&tcp_children[0]), tcp_c->pid,
 						tcp_c->proc_no, strerror(errno), errno );
0ba367ec
 			}else{
 				bytes=0;
 			}
 			/* try to ignore ? */
 			goto end;
 		}else{
 			/* should never happen */
 			LOG(L_CRIT, "BUG: handle_tcp_child: too few bytes received (%d)\n",
 					bytes );
 			bytes=0; /* something was read so there is no error; otoh if
 					  receive_fd returned less then requested => the receive
 					  buffer is empty => no more io queued on this fd */
 			goto end;
 		}
 	}
 	
 	DBG("handle_tcp_child: reader response= %lx, %ld from %d \n",
 					response[0], response[1], (int)(tcp_c-&tcp_children[0]));
 	cmd=response[1];
 	tcpconn=(struct tcp_connection*)response[0];
 	if (tcpconn==0){
 		/* should never happen */
 		LOG(L_CRIT, "BUG: handle_tcp_child: null tcpconn pointer received"
 				 " from tcp child %d (pid %d): %lx, %lx\n",
 				 	(int)(tcp_c-&tcp_children[0]), tcp_c->pid,
 					response[0], response[1]) ;
 		goto end;
 	}
 	switch(cmd){
 		case CONN_RELEASE:
 			tcp_c->busy--;
 			if (tcpconn->state==S_CONN_BAD){ 
 				tcpconn_destroy(tcpconn);
 				break;
 			}
 			/* update the timeout*/
55d8155e
 			tcpconn->timeout=get_ticks()+tcp_con_lifetime;
0ba367ec
 			tcpconn_put(tcpconn);
3135b4bb
 			/* must be after the de-ref*/
 			io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
06c04bc2
 			tcpconn->flags&=~F_CONN_REMOVED;
0ba367ec
 			DBG("handle_tcp_child: CONN_RELEASE  %p refcnt= %d\n", 
 											tcpconn, tcpconn->refcnt);
 			break;
 		case CONN_ERROR:
 		case CONN_DESTROY:
 		case CONN_EOF:
 			/* WARNING: this will auto-dec. refcnt! */
 				tcp_c->busy--;
 				/* main doesn't listen on it => we don't have to delete it
 				 if (tcpconn->s!=-1)
741a9937
 					io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
0ba367ec
 				*/
741a9937
 				tcpconn_destroy(tcpconn); /* closes also the fd */
0ba367ec
 				break;
 		default:
 				LOG(L_CRIT, "BUG: handle_tcp_child:  unknown cmd %d"
 									" from tcp reader %d\n",
 									cmd, (int)(tcp_c-&tcp_children[0]));
 	}
 end:
 	return bytes;
 error:
 	return -1;
 }
 
 
 
 /* handles io from a "generic" ser process (get fd or new_fd from a tcp_send)
  * 
  * params: p     - pointer in the ser processes array (pt[]), to the entry for
  *                 which an io event was detected
  *         fd_i  - fd index in the fd_array (usefull for optimizing
  *                 io_watch_deletes)
  * returns:  handle_* return convention:
  *          -1 on error reading from the fd,
  *           0 on EAGAIN  or when no  more io events are queued 
  *             (receive buffer empty),
  *           >0 on successfull reads from the fd (the receive buffer might
  *             be non-empty).
  */
 inline static int handle_ser_child(struct process_table* p, int fd_i)
 {
 	struct tcp_connection* tcpconn;
 	long response[2];
 	int cmd;
 	int bytes;
 	int ret;
744a2341
 	int fd;
0ba367ec
 	
 	ret=-1;
 	if (p->unix_sock<=0){
 		/* (we can't have a fd==0, 0 is never closed )*/
 		LOG(L_CRIT, "BUG: handle_ser_child: fd %d for %d "
 				"(pid %d)\n", p->unix_sock, (int)(p-&pt[0]), p->pid);
 		goto error;
 	}
 			
 	/* get all bytes and the fd (if transmitted)
 	 * (this is a SOCK_STREAM so read is not atomic) */
 	bytes=receive_fd(p->unix_sock, response, sizeof(response), &fd,
 						MSG_DONTWAIT);
 	if (bytes<(int)sizeof(response)){
 		/* too few bytes read */
 		if (bytes==0){
 			/* EOF -> bad, child has died */
 			DBG("DBG: handle_ser_child: dead child %d, pid %d"
 					" (shutting down?)\n", (int)(p-&pt[0]), p->pid);
 			/* don't listen on it any more */
741a9937
 			io_watch_del(&io_h, p->unix_sock, fd_i, 0);
0ba367ec
 			goto error; /* child dead => no further io events from it */
 		}else if (bytes<0){
 			/* EAGAIN is ok if we try to empty the buffer
 			 * e.g: SIGIO_RT overflow mode or EPOLL ET */
 			if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
 				LOG(L_CRIT, "ERROR: handle_ser_child: read from child %d  "
 						"(pid %d):  %s [%d]\n", (int)(p-&pt[0]), p->pid,
 						strerror(errno), errno);
 				ret=-1;
 			}else{
 				ret=0;
 			}
 			/* try to ignore ? */
 			goto end;
 		}else{
 			/* should never happen */
 			LOG(L_CRIT, "BUG: handle_ser_child: too few bytes received (%d)\n",
 					bytes );
 			ret=0; /* something was read so there is no error; otoh if
 					  receive_fd returned less then requested => the receive
 					  buffer is empty => no more io queued on this fd */
 			goto end;
 		}
 	}
 	ret=1; /* something was received, there might be more queued */
 	DBG("handle_ser_child: read response= %lx, %ld, fd %d from %d (%d)\n",
 					response[0], response[1], fd, (int)(p-&pt[0]), p->pid);
 	cmd=response[1];
 	tcpconn=(struct tcp_connection*)response[0];
 	if (tcpconn==0){
 		LOG(L_CRIT, "BUG: handle_ser_child: null tcpconn pointer received"
 				 " from child %d (pid %d): %lx, %lx\n",
 				 	(int)(p-&pt[0]), p->pid, response[0], response[1]) ;
 		goto end;
 	}
 	switch(cmd){
 		case CONN_ERROR:
06c04bc2
 			if (!(tcpconn->flags & F_CONN_REMOVED) && (tcpconn->s!=-1)){
741a9937
 				io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
06c04bc2
 				tcpconn->flags|=F_CONN_REMOVED;
 			}
741a9937
 			tcpconn_destroy(tcpconn); /* will close also the fd */
0ba367ec
 			break;
 		case CONN_GET_FD:
 			/* send the requested FD  */
 			/* WARNING: take care of setting refcnt properly to
 			 * avoid race condition */
 			if (send_fd(p->unix_sock, &tcpconn, sizeof(tcpconn),
 							tcpconn->s)<=0){
 				LOG(L_ERR, "ERROR: handle_ser_child: send_fd failed\n");
 			}
 			break;
 		case CONN_NEW:
 			/* update the fd in the requested tcpconn*/
 			/* WARNING: take care of setting refcnt properly to
 			 * avoid race condition */
 			if (fd==-1){
 				LOG(L_CRIT, "BUG: handle_ser_child: CONN_NEW:"
 							" no fd received\n");
 				break;
 			}
d9515405
 			(*tcp_connections_no)++;
0ba367ec
 			tcpconn->s=fd;
 			/* add tcpconn to the list*/
 			tcpconn_add(tcpconn);
 			/* update the timeout*/
55d8155e
 			tcpconn->timeout=get_ticks()+tcp_con_lifetime;
3135b4bb
 			io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
06c04bc2
 			tcpconn->flags&=~F_CONN_REMOVED;
0ba367ec
 			break;
 		default:
 			LOG(L_CRIT, "BUG: handle_ser_child: unknown cmd %d\n", cmd);
 	}
 end:
 	return ret;
 error:
 	return -1;
 }
 
 
 
98f3d5e2
 /* sends a tcpconn + fd to a choosen child */
 inline static int send2child(struct tcp_connection* tcpconn)
 {
 	int i;
 	int min_busy;
 	int idx;
746f7674
 	static int crt=0; /* current child */
 	int last;
98f3d5e2
 	
 	min_busy=tcp_children[0].busy;
 	idx=0;
746f7674
 	last=crt+tcp_children_no;
 	for (; crt<last; crt++){
 		i=crt%tcp_children_no;
98f3d5e2
 		if (!tcp_children[i].busy){
 			idx=i;
 			min_busy=0;
 			break;
 		}else if (min_busy>tcp_children[i].busy){
 			min_busy=tcp_children[i].busy;
 			idx=i;
 		}
 	}
746f7674
 	crt=idx+1; /* next time we start with crt%tcp_children_no */
98f3d5e2
 	
 	tcp_children[idx].busy++;
 	tcp_children[idx].n_reqs++;
 	if (min_busy){
 		DBG("WARNING: send2child: no free tcp receiver, "
 				" connection passed to the least busy one (%d)\n",
 				min_busy);
 	}
 	DBG("send2child: to tcp child %d %d(%d), %p\n", idx, 
 					tcp_children[idx].proc_no,
 					tcp_children[idx].pid, tcpconn);
 	/* first make sure this child doesn't have pending request for
 	 * tcp_main (to avoid a possible deadlock: e.g. child wants to
 	 * send a release command, but the master fills its socket buffer
 	 * with new connection commands => deadlock) */
 	/* answer tcp_send requests first */
 	while(handle_ser_child(&pt[tcp_children[idx].proc_no], -1)>0);
 	/* process tcp readers requests */
 	while(handle_tcp_child(&tcp_children[idx], -1)>0);
 		
 #ifdef SEND_FD_QUEUE
28260509
 	/* if queue full, try to queue the io */
 	if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
 			tcpconn->s)<=0){
 		if ((errno==EAGAIN)||(errno==EWOULDBLOCK)){
 			/* FIXME: remove after debugging */
746f7674
 			 LOG(L_CRIT, "INFO: tcp child %d, socket %d: queue full,"
 					 	" %d requests queued (total handled %d)\n",
 					idx, tcp_children[idx].unix_sock, min_busy,
 					tcp_children[idx].n_reqs-1);
28260509
 			if (send_fd_queue_add(&send2child_q, tcp_children[idx].unix_sock, 
98f3d5e2
 						tcpconn)!=0){
28260509
 				LOG(L_ERR, "ERROR: send2child: queue send op. failed\n");
 				return -1;
 			}
 		}else{
 			LOG(L_ERR, "ERROR: send2child: send_fd failed\n");
746f7674
 			return -1;
28260509
 		}
98f3d5e2
 	}
 #else
 	if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
 			tcpconn->s)<=0){
 		LOG(L_ERR, "ERROR: send2child: send_fd failed\n");
 		return -1;
 	}
 #endif
 	
 	return 0;
 }
 
 
 
 /* handles a new connection, called internally by tcp_main_loop/handle_io.
  * params: si - pointer to one of the tcp socket_info structures on which
  *              an io event was detected (connection attempt)
  * returns:  handle_* return convention: -1 on error, 0 on EAGAIN (no more
  *           io events queued), >0 on success. success/error refer only to
  *           the accept.
  */
 static inline int handle_new_connect(struct socket_info* si)
 {
 	union sockaddr_union su;
 	struct tcp_connection* tcpconn;
 	socklen_t su_len;
 	int new_sock;
 	
 	/* got a connection on r */
 	su_len=sizeof(su);
 	new_sock=accept(si->socket, &(su.s), &su_len);
 	if (new_sock==-1){
 		if ((errno==EAGAIN)||(errno==EWOULDBLOCK))
 			return 0;
 		LOG(L_ERR,  "WARNING: handle_new_connect: error while accepting"
 				" connection(%d): %s\n", errno, strerror(errno));
 		return -1;
 	}
d9515405
 	if (*tcp_connections_no>=tcp_max_connections){
98f3d5e2
 		LOG(L_ERR, "ERROR: maximum number of connections exceeded: %d/%d\n",
d9515405
 					*tcp_connections_no, tcp_max_connections);
98f3d5e2
 		close(new_sock);
 		return 1; /* success, because the accept was succesfull */
 	}
d9515405
 	(*tcp_connections_no)++;
98f3d5e2
 	if (init_sock_opt(new_sock)<0){
 		LOG(L_ERR, "ERROR: handle_new_connect: init_sock_opt failed\n");
 		close(new_sock);
 		return 1; /* success, because the accept was succesfull */
 	}
 	
 	/* add socket to list */
 	tcpconn=tcpconn_new(new_sock, &su, si, si->proto, S_CONN_ACCEPT);
 	if (tcpconn){
746f7674
 #ifdef TCP_PASS_NEW_CONNECTION_ON_DATA
 		io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
 		tcpconn->flags&=~F_CONN_REMOVED;
 		tcpconn_add(tcpconn);
 #else
98f3d5e2
 		tcpconn->refcnt++; /* safe, not yet available to the
 							  outside world */
 		tcpconn_add(tcpconn);
 		DBG("handle_new_connect: new connection: %p %d flags: %04x\n",
 			tcpconn, tcpconn->s, tcpconn->flags);
 		/* pass it to a child */
 		if(send2child(tcpconn)<0){
 			LOG(L_ERR,"ERROR: handle_new_connect: no children "
 					"available\n");
746f7674
 			tcpconn_destroy(tcpconn);
98f3d5e2
 		}
746f7674
 #endif
98f3d5e2
 	}else{ /*tcpconn==0 */
 		LOG(L_ERR, "ERROR: handle_new_connect: tcpconn_new failed, "
 				"closing socket\n");
 		close(new_sock);
 		
 	}
 	return 1; /* accept() was succesfull */
 }
 
 
 
 /* handles an io event on one of the watched tcp connections
  * 
  * params: tcpconn - pointer to the tcp_connection for which we have an io ev.
  *         fd_i    - index in the fd_array table (needed for delete)
  * returns:  handle_* return convention, but on success it always returns 0
  *           (because it's one-shot, after a succesfull execution the fd is
  *            removed from tcp_main's watch fd list and passed to a child =>
  *            tcp_main is not interested in further io events that might be
  *            queued for this fd)
  */
 inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, int fd_i)
 {
 	/*  is refcnt!=0 really necessary? 
 	 *  No, in fact it's a bug: I can have the following situation: a send only
 	 *   tcp connection used by n processes simultaneously => refcnt = n. In 
 	 *   the same time I can have a read event and this situation is perfectly
 	 *   valid. -- andrei
 	 */
 #if 0
 	if ((tcpconn->refcnt!=0)){
 		/* FIXME: might be valid for sigio_rt iff fd flags are not cleared
 		 *        (there is a short window in which it could generate a sig
 		 *         that would be catched by tcp_main) */
 		LOG(L_CRIT, "BUG: handle_tcpconn_ev: io event on referenced"
 					" tcpconn (%p), refcnt=%d, fd=%d\n",
 					tcpconn, tcpconn->refcnt, tcpconn->s);
 		return -1;
 	}
 #endif
 	/* pass it to child, so remove it from the io watch list */
 	DBG("handle_tcpconn_ev: data available on %p %d\n", tcpconn, tcpconn->s);
 	if (io_watch_del(&io_h, tcpconn->s, fd_i, 0)==-1) goto error;
 	tcpconn->flags|=F_CONN_REMOVED;
 	tcpconn_ref(tcpconn); /* refcnt ++ */
 	if (send2child(tcpconn)<0){
 		LOG(L_ERR,"ERROR: handle_tcpconn_ev: no children available\n");
746f7674
 		tcpconn_destroy(tcpconn);
 #if 0
98f3d5e2
 		TCPCONN_LOCK;
 		tcpconn->refcnt--;
 		if (tcpconn->refcnt==0){
 			fd=tcpconn->s;
 			_tcpconn_rm(tcpconn);
 			close(fd);
 		}else tcpconn->timeout=0; /* force expire*/
 		TCPCONN_UNLOCK;
746f7674
 #endif
98f3d5e2
 	}
 	return 0; /* we are not interested in possibly queued io events, 
 				 the fd was either passed to a child, or closed */
 error:
 	return -1;
 }
 
 
 
0ba367ec
 /* generic handle io routine, it will call the appropiate
  *  handle_xxx() based on the fd_map type
  *
  * params:  fm  - pointer to a fd hash entry
  *          idx - index in the fd_array (or -1 if not known)
  * return: -1 on error
  *          0 on EAGAIN or when by some other way it is known that no more 
  *            io events are queued on the fd (the receive buffer is empty).
  *            Usefull to detect when there are no more io events queued for
  *            sigio_rt, epoll_et, kqueue.
  *         >0 on successfull read from the fd (when there might be more io
  *            queued -- the receive buffer might still be non-empty)
  */
 inline static int handle_io(struct fd_map* fm, int idx)
 {	
 	int ret;
 	
 	switch(fm->type){
 		case F_SOCKINFO:
 			ret=handle_new_connect((struct socket_info*)fm->data);
 			break;
 		case F_TCPCONN:
 			ret=handle_tcpconn_ev((struct tcp_connection*)fm->data, idx);
 			break;
 		case F_TCPCHILD:
 			ret=handle_tcp_child((struct tcp_child*)fm->data, idx);
 			break;
 		case F_PROC:
 			ret=handle_ser_child((struct process_table*)fm->data, idx);
 			break;
 		case F_NONE:
 			LOG(L_CRIT, "BUG: handle_io: empty fd map\n");
 			goto error;
 		default:
 			LOG(L_CRIT, "BUG: handle_io: uknown fd type %d\n", fm->type); 
 			goto error;
 	}
 	return ret;
 error:
 	return -1;
 }
 
 
 
32f8b7ed
 /* very inefficient for now - FIXME
  * keep in sync with tcpconn_destroy, the "delete" part should be
  * the same except for io_watch_del..*/
 static inline void tcpconn_timeout(int force)
0ba367ec
 {
1ce28841
 	static int prev_ticks=0;
0ba367ec
 	struct tcp_connection *c, *next;
1ce28841
 	unsigned int ticks;
0ba367ec
 	unsigned h;
 	int fd;
 	
 	
 	ticks=get_ticks();
1ce28841
 	if ((ticks==prev_ticks) && !force) return;
 	prev_ticks=ticks;
0ba367ec
 	TCPCONN_LOCK; /* fixme: we can lock only on delete IMO */
 	for(h=0; h<TCP_ID_HASH_SIZE; h++){
 		c=tcpconn_id_hash[h];
 		while(c){
 			next=c->id_next;
1ce28841
 			if (force ||((c->refcnt==0) && ((int)(ticks-c->timeout)>=0))){
32f8b7ed
 				if (!force)
 					DBG("tcpconn_timeout: timeout for hash=%d - %p"
 							" (%d > %d)\n", h, c, ticks, c->timeout);
0ba367ec
 				fd=c->s;
 #ifdef USE_TLS
 				if (c->type==PROTO_TLS)
 					tls_close(c, fd);
 #endif
 				_tcpconn_rm(c);
32f8b7ed
 				if ((fd>0)&&(c->refcnt==0)) {
06c04bc2
 					if (!(c->flags & F_CONN_REMOVED)){
 						io_watch_del(&io_h, fd, -1, IO_FD_CLOSING);
 						c->flags|=F_CONN_REMOVED;
 					}
0ba367ec
 					close(fd);
 				}
d9515405
 				(*tcp_connections_no)--;
0ba367ec
 			}
 			c=next;
 		}
 	}
 	TCPCONN_UNLOCK;
 }
 
5b532c7f
 
0ba367ec
 
 /* tcp main loop */
 void tcp_main_loop()
 {
 
 	struct socket_info* si;
 	int r;
 	
746f7674
 	/* init send fd queues (here because we want mem. alloc only in the tcp
 	 *  process */
 #ifdef SEND_FD_QUEUE
 	if (init_send_fd_queues()<0){
 		LOG(L_CRIT, "ERROR: init_tcp: could not init send fd queues\n");
 		goto error;
 	}
 #endif
0ba367ec
 	/* init io_wait (here because we want the memory allocated only in
 	 * the tcp_main process) */
 	
 	/* FIXME: TODO: make tcp_max_fd_no a config param */
 	if  (init_io_wait(&io_h, tcp_max_fd_no, tcp_poll_method)<0)
 		goto error;
 	/* init: start watching all the fds*/
 	
 	/* add all the sockets we listens on for connections */
9f4c52ce
 	for (si=tcp_listen; si; si=si->next){
 		if ((si->proto==PROTO_TCP) &&(si->socket!=-1)){
0ba367ec
 			if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
 				LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
 							"listen socket to the fd list\n");
 				goto error;
 			}
9f4c52ce
 		}else{
 			LOG(L_CRIT, "BUG: tcp_main_loop: non tcp address in tcp_listen\n");
5b532c7f
 		}
9f4c52ce
 	}
f6e50f08
 #ifdef USE_TLS
9f4c52ce
 	if (!tls_disable){
 		for (si=tls_listen; si; si=si->next){
 			if ((si->proto==PROTO_TLS) && (si->socket!=-1)){
0ba367ec
 				if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
 					LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
 							"tls listen socket to the fd list\n");
 					goto error;
 				}
9f4c52ce
 			}else{
faa66933
 				LOG(L_CRIT, "BUG: tcp_main_loop: non tls address"
 						" in tls_listen\n");
 			}
f6e50f08
 		}
1d9e67a8
 	}
9f4c52ce
 #endif
0ba367ec
 	/* add all the unix sockets used for communcation with other ser processes
 	 *  (get fd, new connection a.s.o) */
f2f969dd
 	for (r=1; r<process_no; r++){
0ba367ec
 		if (pt[r].unix_sock>0) /* we can't have 0, we never close it!*/
 			if (io_watch_add(&io_h, pt[r].unix_sock, F_PROC, &pt[r])<0){
 					LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
 							"process %d unix socket to the fd list\n", r);
 					goto error;
 			}
5b532c7f
 	}
0ba367ec
 	/* add all the unix sokets used for communication with the tcp childs */
06aaa54f
 	for (r=0; r<tcp_children_no; r++){
0ba367ec
 		if (tcp_children[r].unix_sock>0)/*we can't have 0, we never close it!*/
 			if (io_watch_add(&io_h, tcp_children[r].unix_sock, F_TCPCHILD,
 							&tcp_children[r]) <0){
 				LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
 						"tcp child %d unix socket to the fd list\n", r);
 				goto error;
 			}
06aaa54f
 	}
5b532c7f
 	
0ba367ec
 	/* main loop */
 	switch(io_h.poll_method){
 		case POLL_POLL:
 			while(1){
 				/* wait and process IO */
 				io_wait_loop_poll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0); 
98f3d5e2
 				send_fd_queue_run(&send2child_q); /* then new io */
0ba367ec
 				/* remove old connections */
32f8b7ed
 				tcpconn_timeout(0);
0ba367ec
 			}
 			break;
 #ifdef HAVE_SELECT
 		case POLL_SELECT:
 			while(1){
 				io_wait_loop_select(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
98f3d5e2
 				send_fd_queue_run(&send2child_q); /* then new io */
32f8b7ed
 				tcpconn_timeout(0);
0ba367ec
 			}
 			break;
f6e50f08
 #endif
0ba367ec
 #ifdef HAVE_SIGIO_RT
 		case POLL_SIGIO_RT:
 			while(1){
 				io_wait_loop_sigio_rt(&io_h, TCP_MAIN_SELECT_TIMEOUT);
98f3d5e2
 				send_fd_queue_run(&send2child_q); /* then new io */
32f8b7ed
 				tcpconn_timeout(0);
5b532c7f
 			}
0ba367ec
 			break;
 #endif
 #ifdef HAVE_EPOLL
 		case POLL_EPOLL_LT:
 			while(1){
 				io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
98f3d5e2
 				send_fd_queue_run(&send2child_q); /* then new io */
32f8b7ed
 				tcpconn_timeout(0);
06aaa54f
 			}
0ba367ec
 			break;
 		case POLL_EPOLL_ET:
 			while(1){
 				io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 1);
98f3d5e2
 				send_fd_queue_run(&send2child_q); /* then new io */
32f8b7ed
 				tcpconn_timeout(0);
5b532c7f
 			}
0ba367ec
 			break;
741a9937
 #endif
 #ifdef HAVE_KQUEUE
 		case POLL_KQUEUE:
 			while(1){
 				io_wait_loop_kqueue(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
98f3d5e2
 				send_fd_queue_run(&send2child_q); /* then new io */
32f8b7ed
 				tcpconn_timeout(0);
741a9937
 			}
 			break;
9eda5956
 #endif
 #ifdef HAVE_DEVPOLL
 		case POLL_DEVPOLL:
 			while(1){
 				io_wait_loop_devpoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
98f3d5e2
 				send_fd_queue_run(&send2child_q); /* then new io */
32f8b7ed
 				tcpconn_timeout(0);
9eda5956
 			}
 			break;
0ba367ec
 #endif
 		default:
 			LOG(L_CRIT, "BUG: tcp_main_loop: no support for poll method "
 					" %s (%d)\n", 
 					poll_method_name(io_h.poll_method), io_h.poll_method);
 			goto error;
5b532c7f
 	}
0ba367ec
 error:
746f7674
 #ifdef SEND_FD_QUEUE
 	destroy_send_fd_queues();
 #endif
0ba367ec
 	destroy_io_wait(&io_h);
 	LOG(L_CRIT, "ERROR: tcp_main_loop: exiting...");
 	exit(-1);
 }
 
 
 
 /* cleanup before exit */
 void destroy_tcp()
 {
 		if (tcpconn_id_hash){
32f8b7ed
 			tcpconn_timeout(1); /* force close/expire for all active tcpconns*/
0ba367ec
 			shm_free(tcpconn_id_hash);
 			tcpconn_id_hash=0;
 		}
d9515405
 		if (tcp_connections_no){
 			shm_free(tcp_connections_no);
 			tcp_connections_no=0;
 		}
0ba367ec
 		if (connection_id){
 			shm_free(connection_id);
 			connection_id=0;
 		}
 		if (tcpconn_aliases_hash){
 			shm_free(tcpconn_aliases_hash);
 			tcpconn_aliases_hash=0;
 		}
 		if (tcpconn_lock){
 			lock_destroy(tcpconn_lock);
 			lock_dealloc((void*)tcpconn_lock);
 			tcpconn_lock=0;
 		}
746f7674
 		if (tcp_children){
 			pkg_free(tcp_children);
 			tcp_children=0;
 		}
5b532c7f
 }
 
 
 
0c5da34b
 int init_tcp()
 {
55d8155e
 	char* poll_err;
 	
8aeb47e2
 	/* init lock */
 	tcpconn_lock=lock_alloc();
 	if (tcpconn_lock==0){
 		LOG(L_CRIT, "ERROR: init_tcp: could not alloc lock\n");
 		goto error;
 	}
 	if (lock_init(tcpconn_lock)==0){
 		LOG(L_CRIT, "ERROR: init_tcp: could not init lock\n");
 		lock_dealloc((void*)tcpconn_lock);
 		tcpconn_lock=0;
 		goto error;
 	}
2719e69b
 	/* init globals */
d9515405
 	tcp_connections_no=shm_malloc(sizeof(int));
 	if (tcp_connections_no==0){
 		LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
 		goto error;
 	}
 	*tcp_connections_no=0;
 	connection_id=shm_malloc(sizeof(int));
2719e69b
 	if (connection_id==0){
 		LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
 		goto error;
 	}
 	*connection_id=1;
8aeb47e2
 	/* alloc hashtables*/
59653eb8
 	tcpconn_aliases_hash=(struct tcp_conn_alias**)
 			shm_malloc(TCP_ALIAS_HASH_SIZE* sizeof(struct tcp_conn_alias*));
 	if (tcpconn_aliases_hash==0){
8aeb47e2
 		LOG(L_CRIT, "ERROR: init_tcp: could not alloc address hashtable\n");
 		goto error;
 	}
 	tcpconn_id_hash=(struct tcp_connection**)shm_malloc(TCP_ID_HASH_SIZE*
 								sizeof(struct tcp_connection*));
 	if (tcpconn_id_hash==0){
 		LOG(L_CRIT, "ERROR: init_tcp: could not alloc id hashtable\n");
0c5da34b
 		goto error;
 	}
8aeb47e2
 	/* init hashtables*/
59653eb8
 	memset((void*)tcpconn_aliases_hash, 0, 
 			TCP_ALIAS_HASH_SIZE * sizeof(struct tcp_conn_alias*));
8aeb47e2
 	memset((void*)tcpconn_id_hash, 0, 
 			TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*));
0ba367ec
 	
55d8155e
 	/* fix config variables */
 	/* they can have only positive values due the config parser so we can
 	 * ignore most of them */
 		poll_err=check_poll_method(tcp_poll_method);
 	
 	/* set an appropiate poll method */
 	if (poll_err || (tcp_poll_method==0)){
 		tcp_poll_method=choose_poll_method();
 		if (poll_err){
 			LOG(L_ERR, "ERROR: init_tcp: %s, using %s instead\n",
 					poll_err, poll_method_name(tcp_poll_method));
 		}else{
 			LOG(L_INFO, "init_tcp: using %s as the io watch method"
 					" (auto detected)\n", poll_method_name(tcp_poll_method));
 		}
 	}else{
 			LOG(L_INFO, "init_tcp: using %s io watch method (config)\n",
 					poll_method_name(tcp_poll_method));
 	}
0ba367ec
 	
0c5da34b
 	return 0;
 error:
0ba367ec
 	/* clean-up */
 	destroy_tcp();
 	return -1;
8aeb47e2
 }
 
 
 
28260509
 /* returns -1 on error */
 static int set_non_blocking(int s)
 {
 	int flags;
 	/* non-blocking */
 	flags=fcntl(s, F_GETFL);
 	if (flags==-1){
 		LOG(L_ERR, "ERROR: set_non_blocking: fnctl failed: (%d) %s\n",
 				errno, strerror(errno));
 		goto error;
 	}
 	if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
 		LOG(L_ERR, "ERROR: set_non_blocking: fcntl: set non-blocking failed:"
 				" (%d) %s\n", errno, strerror(errno));
 		goto error;
 	}
 	return 0;
 error:
 	return -1;
 }
 
 
 
5b532c7f
 /* starts the tcp processes */
6ee62314
 int tcp_init_children()
5b532c7f
 {
 	int r;
 	int sockfd[2];
06aaa54f
 	int reader_fd[2]; /* for comm. with the tcp children read  */
5b532c7f
 	pid_t pid;
55d8155e
 	struct socket_info *si;
 	
 	/* estimate max fd. no:
 	 * 1 tcp send unix socket/all_proc, 
 	 *  + 1 udp sock/udp proc + 1 tcp_child sock/tcp child*
 	 *  + no_listen_tcp */
 	for(r=0, si=tcp_listen; si; si=si->next, r++);
 #ifdef USE_TLS
 	if (! tls_disable)
d3ab3df2
 		for (si=tls_listen; si; si=si->next, r++);
55d8155e
 #endif
5b532c7f
 	
55a45269
 	tcp_max_fd_no=process_count*2 +r-1 /* timer */ +3; /* stdin/out/err*/
d9515405
 	/* max connections can be temporarily exceeded with process_count
 	 * - tcp_main (tcpconn_connect called simultaneously in all all the 
 	 *  processes) */
 	tcp_max_fd_no+=tcp_max_connections+process_count-1 /* tcp main */;
5b532c7f
 	
746f7674
 	/* alloc the children array */
 	tcp_children=pkg_malloc(sizeof(struct tcp_child)*tcp_children_no);
 	if (tcp_children==0){
 			LOG(L_ERR, "ERROR: tcp_init_children: out of memory\n");
 			goto error;
 	}
5b532c7f
 	/* create the tcp sock_info structures */
f2f969dd
 	/* copy the sockets --moved to main_loop*/
5b532c7f
 	
 	/* fork children & create the socket pairs*/
 	for(r=0; r<tcp_children_no; r++){
6c6659cb
 		if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
5b532c7f
 			LOG(L_ERR, "ERROR: tcp_main: socketpair failed: %s\n",
 					strerror(errno));
 			goto error;
 		}
06aaa54f
 		if (socketpair(AF_UNIX, SOCK_STREAM, 0, reader_fd)<0){
 			LOG(L_ERR, "ERROR: tcp_main: socketpair failed: %s\n",
 					strerror(errno));
 			goto error;
 		}
28260509
 #ifdef TCP_CHILD_NON_BLOCKING
 		if ((set_non_blocking(reader_fd[0])<0) || 
 			(set_non_blocking(reader_fd[1])<0)){
 			LOG(L_ERR, "ERROR: tcp_main: failed to set non blocking"
 						"on child sockets\n");
 			/* continue, it's not critical (it will go slower under
 			 * very high connection rates) */
 		}
 #endif
5b532c7f
 		
6ee62314
 		process_no++;
3167c744
 		child_rank++;
5b532c7f
 		pid=fork();
 		if (pid<0){
 			LOG(L_ERR, "ERROR: tcp_main: fork failed: %s\n",
 					strerror(errno));
 			goto error;
 		}else if (pid>0){
 			/* parent */
 			close(sockfd[1]);
06aaa54f
 			close(reader_fd[1]);
5b532c7f
 			tcp_children[r].pid=pid;
6ee07a24
 			tcp_children[r].proc_no=process_no;
5b532c7f
 			tcp_children[r].busy=0;
 			tcp_children[r].n_reqs=0;
06aaa54f
 			tcp_children[r].unix_sock=reader_fd[0];
6ee62314
 			pt[process_no].pid=pid;
0c5da34b
 			pt[process_no].unix_sock=sockfd[0];
 			pt[process_no].idx=r;
6ee62314
 			strncpy(pt[process_no].desc, "tcp receiver", MAX_PT_DESC);
5b532c7f
 		}else{
 			/* child */
 			close(sockfd[0]);
0c5da34b
 			unix_tcp_sock=sockfd[1];
7ec958f3
 			bind_address=0; /* force a SEGFAULT if someone uses a non-init.
 							   bind address on tcp */
0ba367ec
 			/* record pid twice to avoid the child using it, before
 			 * parent gets a chance to set it*/
 			pt[process_no].pid=getpid();
3167c744
 			if (init_child(child_rank) < 0) {
cb87691a
 				LOG(L_ERR, "init_children failed\n");
7ec958f3
 				goto error;
 			}
06aaa54f
 			tcp_receive_loop(reader_fd[1]);
5b532c7f
 		}
 	}
6ee62314
 	return 0;
5b532c7f
 error:
 	return -1;
 }
 
d9515405
 
 
 void tcp_get_info(struct tcp_gen_info *ti)
 {
 	ti->tcp_readers=tcp_children_no;
 	ti->tcp_max_connections=tcp_max_connections;
 	ti->tcp_connections_no=*tcp_connections_no;
 }
 
5b532c7f
 #endif