tcp_main.c
5b532c7f
 /*
  * $Id$
  *
  * Copyright (C) 2001-2003 Fhg Fokus
  *
  * 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)
67b1fc53
  *  2003-11-04  always lock before manipulating refcnt; sendchild
  *              does not inc refcnt by itself anymore (andrei)
a65b3026
  *  2003-11-07  different unix sockets are used for fd passing
  *              to/from readers/writers (andrei)
846af213
  *  2003-11-17  handle_new_connect & tcp_connect will close the 
  *              new socket if tcpconn_new return 0 (e.g. out of mem) (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>
5b532c7f
 
 #include <unistd.h>
 
 #include <errno.h>
 #include <string.h>
 
 
 
 #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"
28427aa4
 #ifdef USE_TLS
 #include "tls/tls_server.h"
 #endif
5b532c7f
 
 
6bc40dea
 
8aeb47e2
 
5b532c7f
 #define local_malloc pkg_malloc
 #define local_free   pkg_free
 
 #define MAX_TCP_CHILDREN 100
 
 struct tcp_child{
 	pid_t pid;
6ee07a24
 	int proc_no; /* ser proc_no, for debugging */
a65b3026
 	int unix_sock; /* unix "read child" sock fd */
5b532c7f
 	int busy;
 	int n_reqs; /* number of requests serviced so far */
 };
 
 
 
67b1fc53
 /* connection hash table (after ip&port) */
 struct tcp_connection** tcpconn_addr_hash=0;
8aeb47e2
 /* connection hash table (after connection id) */
 struct tcp_connection** tcpconn_id_hash=0;
0bd53297
 gen_lock_t* tcpconn_lock=0;
8aeb47e2
 
5b532c7f
 struct tcp_child tcp_children[MAX_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
 
b33736bc
 int tcp_proto_no=-1; /* tcp protocol number as returned by getprotobyname */
5b532c7f
 
 
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;
 	}
 	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;
 		c->flags=0;
 		c->timeout=get_ticks()+TCP_CON_TIMEOUT;
 	}
 			
 		
5b532c7f
 	return c;
 	
 error:
f6e50f08
 	if (c) shm_free(c);
5b532c7f
 	return 0;
 }
 
 
 
f15bede1
 
 struct socket_info* find_tcp_si(union sockaddr_union* s)
 {
67b1fc53
 	int r;
f15bede1
 	struct ip_addr ip;
 	
 	su2ip_addr(&ip, s);
67b1fc53
 	for (r=0; r<sock_no; r++)
 		if (ip_addr_cmp(&ip, &tcp_info[r].address)){
f15bede1
 			/* found it, we use first match */
67b1fc53
 			return &tcp_info[r];
f15bede1
 		}
 	return 0; /* no match */
 }
 
 
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;
39546e5f
 	int optval;
846af213
 	struct tcp_connection* con;
b33736bc
 #ifdef DISABLE_NAGLE
 	int flag;
 #endif
0c5da34b
 
 	s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
846af213
 	if (s==-1){
0c5da34b
 		LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
 				errno, strerror(errno));
 		goto error;
 	}
b33736bc
 #ifdef DISABLE_NAGLE
 	flag=1;
39546e5f
 	if ( (tcp_proto_no!=-1) && (setsockopt(s, tcp_proto_no , TCP_NODELAY,
b33736bc
 					&flag, sizeof(flag))<0) ){
 		LOG(L_ERR, "ERROR: tcp_connect: could not disable Nagle: %s\n",
 				strerror(errno));
 	}
 #endif
39546e5f
 	/* tos*/
 	optval=IPTOS_LOWDELAY;
 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval, sizeof(optval)) ==-1){
 		LOG(L_WARN, "WARNING: tcpconn_connect: setsockopt tos: %s\n",
 				strerror(errno));
 		/* continue since this is not critical */
 	}
 
0c5da34b
 	if (connect(s, &server->s, sockaddru_len(*server))<0){
 		LOG(L_ERR, "ERROR: tcpconn_connect: connect: (%d) %s\n",
 				errno, strerror(errno));
 		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 */
 	}
67b1fc53
 #ifdef USE_TLS
 	if (type==PROTO_TLS)
 		si=find_tls_si(&my_name);
 	else
 #endif
 		si=find_tcp_si(&my_name);
 
f15bede1
 	if (si==0){
 		LOG(L_ERR, "ERROR: tcp_connect: could not find coresponding"
 				" listening socket, using default...\n");
 		if (server->s.sa_family==AF_INET) si=sendipv4_tcp;
 #ifdef USE_IPV6
 		else si=sendipv6_tcp;
 #endif
 	}
846af213
 	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:
846af213
 	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*/
67b1fc53
 		hash=tcp_addr_hash(&c->rcv.src_ip, c->rcv.src_port);
 		c->addr_hash=hash;
 		tcpconn_listadd(tcpconn_addr_hash[hash], c, next, prev);
8aeb47e2
 		hash=tcp_id_hash(c->id);
 		c->id_hash=hash;
 		tcpconn_listadd(tcpconn_id_hash[hash], c, id_next, id_prev);
 		TCPCONN_UNLOCK;
67b1fc53
 		DBG("tcpconn_add: hashes: %d, %d\n", c->addr_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)
 {
67b1fc53
 	tcpconn_listrm(tcpconn_addr_hash[c->addr_hash], c, next, prev);
ab130758
 	tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
 	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)
 {
0c5da34b
 	TCPCONN_LOCK;
67b1fc53
 	tcpconn_listrm(tcpconn_addr_hash[c->addr_hash], c, next, prev);
8aeb47e2
 	tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_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;
8aeb47e2
 	unsigned hash;
0c5da34b
 	
f2e456c3
 #ifdef EXTRA_DEBUG
9c01c860
 	DBG("tcpconn_find: %d  port %d\n",id, port);
67b1fc53
 	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);
67b1fc53
 		for (c=tcpconn_addr_hash[hash]; c; c=c->next){
f2e456c3
 #ifdef EXTRA_DEBUG
67b1fc53
 			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
67b1fc53
 			if ( (c->state!=S_CONN_BAD) && (port==c->rcv.src_port) &&
 					(ip_addr_cmp(ip, &c->rcv.src_ip)) )
 				return c;
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;
 }
 
 
 
67b1fc53
 void tcpconn_ref(struct tcp_connection* c)
59653eb8
 {
 	TCPCONN_LOCK;
67b1fc53
 	c->refcnt++; /* FIXME: atomic_dec */
59653eb8
 	TCPCONN_UNLOCK;
 }
 
 
 
0c5da34b
 void tcpconn_put(struct tcp_connection* c)
 {
67b1fc53
 	TCPCONN_LOCK;
0c5da34b
 	c->refcnt--; /* FIXME: atomic_dec */
67b1fc53
 	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;
d3f69fe0
 	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);
ab130758
 		c=tcpconn_get(id, &ip, port, TCP_CON_SEND_TIMEOUT); 
f2f969dd
 	}else if (id){
ab130758
 		c=tcpconn_get(id, 0, 0, TCP_CON_SEND_TIMEOUT);
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 */
 				c=tcpconn_get(0, &ip, port, TCP_CON_SEND_TIMEOUT);
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
 			}
67b1fc53
 			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;
67b1fc53
 			n=send_all(unix_tcp_sock, response, sizeof(response));
 			if (n<=0){
534092ba
 				LOG(L_ERR, "BUG: tcp_send: failed write: %s (%d)\n",
 						strerror(errno), errno);
67b1fc53
 				n=-1;
534092ba
 				goto end;
 			}	
0c5da34b
 			n=send_fd(unix_tcp_sock, &c, sizeof(c), c->s);
67b1fc53
 			if (n<=0){
534092ba
 				LOG(L_ERR, "BUG: tcp_send: failed send_fd: %s (%d)\n",
 						strerror(errno), errno);
67b1fc53
 				n=-1;
534092ba
 				goto end;
 			}
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 */
67b1fc53
 			DBG("tcp_send: tcp connection found (%p), acquiring fd\n", c);
0c5da34b
 			/* get the fd */
 			response[0]=(long)c;
 			response[1]=CONN_GET_FD;
67b1fc53
 			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);
67b1fc53
 				n=-1;
534092ba
 				goto release_c;
 			}
f2f969dd
 			DBG("tcp_send, c= %p, n=%d\n", c, n);
d3f69fe0
 			tmp=c;
e29d027b
 			n=receive_fd(unix_tcp_sock, &c, sizeof(c), &fd);
67b1fc53
 			if (n<=0){
534092ba
 				LOG(L_ERR, "BUG: tcp_send: failed to get fd(receive_fd):"
 							" %s (%d)\n", strerror(errno), errno);
67b1fc53
 				n=-1;
534092ba
 				goto release_c;
 			}
d3f69fe0
 			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 */
846af213
 				goto end;
d3f69fe0
 			}
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
 		n=send(fd, buf, len,
b988daef
 #ifdef HAVE_MSG_NOSIGNAL
 			MSG_NOSIGNAL
 #else
 			0
 #endif
 			);
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){
4290c43f
 		if (errno==EINTR) goto send_it; /* interrupted write, try again*/
 										/* keep the lock or lock/unlock again?*/
ab130758
 		LOG(L_ERR, "ERROR: tcpsend: failed to send, n=%d: %s (%d)\n",
 				n, strerror(errno), errno);
 		/* 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;
67b1fc53
 		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);
67b1fc53
 			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;
 }
 
 
 
8aeb47e2
 /* very ineficient for now - FIXME*/
0c5da34b
 void tcpconn_timeout(fd_set* set)
6bc40dea
 {
 	struct tcp_connection *c, *next;
8aeb47e2
 	int ticks;
744a2341
 	unsigned h;
 	int fd;
5b532c7f
 	
6bc40dea
 	
6ee62314
 	ticks=get_ticks();
ab130758
 	TCPCONN_LOCK; /* fixme: we can lock only on delete IMO */
67b1fc53
 	for(h=0; h<TCP_ADDR_HASH_SIZE; h++){
 		c=tcpconn_addr_hash[h];
8aeb47e2
 		while(c){
67b1fc53
 			next=c->next;
8aeb47e2
 			if ((c->refcnt==0) && (ticks>c->timeout)) {
 				DBG("tcpconn_timeout: timeout for hash=%d - %p (%d > %d)\n",
 						h, c, ticks, c->timeout);
744a2341
 				fd=c->s;
f535fd9a
 #ifdef USE_TLS
 				if (c->type==PROTO_TLS)
 					tls_close(c, fd);
 #endif
ab130758
 				_tcpconn_rm(c);
744a2341
 				if (fd>0) {
 					FD_CLR(fd, set);
 					close(fd);
 				}
0c5da34b
 			}
8aeb47e2
 			c=next;
6bc40dea
 		}
 	}
ab130758
 	TCPCONN_UNLOCK;
5b532c7f
 }
 
 
 
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;
67b1fc53
 	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 */
 	optval=IPTOS_LOWDELAY;
 	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){
 		LOG(L_ERR, "ERROR: tcp_init: bind(%x, %p, %d) on %s: %s\n",
 				sock_info->socket, &addr->s, 
 				sockaddru_len(*addr),
 				sock_info->address_str.s,
 				strerror(errno));
 		goto error;
 	}
 	if (listen(sock_info->socket, 10)==-1){
 		LOG(L_ERR, "ERROR: tcp_init: listen(%x, %p, %d) on %s: %s\n",
 				sock_info->socket, &addr->s, 
 				sockaddru_len(*addr),
 				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;
 }
 
 
 
 static int send2child(struct tcp_connection* tcpconn)
 {
 	int i;
6ee62314
 	int min_busy;
 	int idx;
5b532c7f
 	
6ee62314
 	min_busy=tcp_children[0].busy;
 	idx=0;
5b532c7f
 	for (i=0; i<tcp_children_no; i++){
 		if (!tcp_children[i].busy){
6ee62314
 			idx=i;
 			min_busy=0;
 			break;
 		}else if (min_busy>tcp_children[i].busy){
 			min_busy=tcp_children[i].busy;
 			idx=i;
5b532c7f
 		}
 	}
6ee62314
 	
 	tcp_children[idx].busy++;
 	tcp_children[idx].n_reqs++;
 	if (min_busy){
67b1fc53
 		LOG(L_WARN, "WARNING: send2child: no free tcp receiver, "
6ee62314
 				" connection passed to the least busy one (%d)\n",
 				min_busy);
5b532c7f
 	}
6ee07a24
 	DBG("send2child: to tcp child %d %d(%d), %p\n", idx, 
 					tcp_children[idx].proc_no,
 					tcp_children[idx].pid, tcpconn);
67b1fc53
 	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;
 	}
6ee62314
 	
fc35aa1c
 	return 0;
5b532c7f
 }
 
 
f6e50f08
 /* handle a new connection, called internally by tcp_main_loop */
 static inline void handle_new_connect(struct socket_info* si,
 										fd_set* sel_set, int* n)
 {
 	union sockaddr_union su;
 	struct tcp_connection* tcpconn;
 	socklen_t su_len;
 	int new_sock;
 	
 	if ((FD_ISSET(si->socket, sel_set))){
 		/* got a connection on r */
 		su_len=sizeof(su);
 		new_sock=accept(si->socket, &(su.s), &su_len);
af6af90f
 		(*n)--;
846af213
 		if (new_sock==-1){
f6e50f08
 			LOG(L_ERR,  "WARNING: tcp_main_loop: error while accepting"
 					" connection(%d): %s\n", errno, strerror(errno));
 			return;
 		}
 		
 		/* add socket to list */
 		tcpconn=tcpconn_new(new_sock, &su, si, si->proto, S_CONN_ACCEPT);
 		if (tcpconn){
67b1fc53
 			tcpconn->refcnt++; /* safe, not yet available to the
 								  outside world */
f6e50f08
 			tcpconn_add(tcpconn);
 			DBG("tcp_main_loop: new connection: %p %d\n",
 				tcpconn, tcpconn->s);
 			/* pass it to a child */
 			if(send2child(tcpconn)<0){
 				LOG(L_ERR,"ERROR: tcp_main_loop: no children "
 						"available\n");
 				TCPCONN_LOCK;
67b1fc53
 				tcpconn->refcnt--;
f6e50f08
 				if (tcpconn->refcnt==0){
 					close(tcpconn->s);
 					_tcpconn_rm(tcpconn);
 				}else tcpconn->timeout=0; /* force expire */
 				TCPCONN_UNLOCK;
 			}
846af213
 		}else{ /*tcpconn==0 */
 			LOG(L_ERR, "ERROR: tcp_main_loop: tcpconn_new failed, "
 					"closing socket\n");
 			close(new_sock);
f6e50f08
 		}
 	}
 }
 
 
a65b3026
 /* 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("tcp_main_loop: destroying connection\n");
 		fd=tcpconn->s;
 #ifdef USE_TLS
 		/*FIXME: lock ->writelock ? */
 		if (tcpconn->type==PROTO_TLS)
 			tls_close(tcpconn, fd);
 #endif
 		_tcpconn_rm(tcpconn);
 		close(fd);
 	}else{
 		/* force timeout */
 		tcpconn->timeout=0;
 		tcpconn->state=S_CONN_BAD;
 		DBG("tcp_main_loop: delaying ...\n");
 		
 	}
 	TCPCONN_UNLOCK;
 }
 
 
5b532c7f
 void tcp_main_loop()
 {
 	int r;
 	int n;
 	fd_set master_set;
 	fd_set sel_set;
 	int maxfd;
 	struct tcp_connection* tcpconn;
8aeb47e2
 	unsigned h;
5b532c7f
 	long response[2];
0c5da34b
 	int cmd;
6a157851
 	int bytes;
6bc40dea
 	struct timeval timeout;
744a2341
 	int fd;
5b532c7f
 
 	/*init */
 	maxfd=0;
 	FD_ZERO(&master_set);
 	/* set all the listen addresses */
67b1fc53
 	for (r=0; r<sock_no; r++){
 		if ((tcp_info[r].proto==PROTO_TCP) &&(tcp_info[r].socket!=-1)){
 			FD_SET(tcp_info[r].socket, &master_set);
 			if (tcp_info[r].socket>maxfd) maxfd=tcp_info[r].socket;
5b532c7f
 		}
f6e50f08
 #ifdef USE_TLS
67b1fc53
 		if ((!tls_disable)&&(tls_info[r].proto==PROTO_TLS) &&
 				(tls_info[r].socket!=-1)){
 			FD_SET(tls_info[r].socket, &master_set);
 			if (tls_info[r].socket>maxfd) maxfd=tls_info[r].socket;
f6e50f08
 		}
9f4c52ce
 #endif
67b1fc53
 	}
5b532c7f
 	/* set all the unix sockets used for child comm */
f2f969dd
 	for (r=1; r<process_no; r++){
 		if (pt[r].unix_sock>0){ /* we can't have 0, we never close it!*/
0c5da34b
 			FD_SET(pt[r].unix_sock, &master_set);
 			if (pt[r].unix_sock>maxfd) maxfd=pt[r].unix_sock;
5b532c7f
 		}
 	}
a65b3026
 	for (r=0; r<tcp_children_no; r++){
 		if (tcp_children[r].unix_sock>0){ /* we can't have 0, 
 											 we never close it!*/
 			FD_SET(tcp_children[r].unix_sock, &master_set);
 			if (tcp_children[r].unix_sock>maxfd)
 				maxfd=tcp_children[r].unix_sock;
 		}
 	}
5b532c7f
 	
 	
 	/* main loop*/
 	
 	while(1){
 		sel_set=master_set;
6ee62314
 		timeout.tv_sec=TCP_MAIN_SELECT_TIMEOUT;
 		timeout.tv_usec=0;
6bc40dea
 		n=select(maxfd+1, &sel_set, 0 ,0 , &timeout);
5b532c7f
 		if (n<0){
6bc40dea
 			if (errno==EINTR) continue; /* just a signal */
5b532c7f
 			/* errors */
6bc40dea
 			LOG(L_ERR, "ERROR: tcp_main_loop: select:(%d) %s\n", errno,
 					strerror(errno));
0c5da34b
 			n=0;
5b532c7f
 		}
 		
67b1fc53
 		for (r=0; r<sock_no && n; r++){
 			handle_new_connect(&tcp_info[r], &sel_set, &n);
f6e50f08
 #ifdef USE_TLS
 			if (!tls_disable)
67b1fc53
 				handle_new_connect(&tls_info[r], &sel_set, &n);
f6e50f08
 #endif
67b1fc53
 		}
5b532c7f
 		
8aeb47e2
 		/* check all the read fds (from the tcpconn_addr_hash ) */
67b1fc53
 		for (h=0; h<TCP_ADDR_HASH_SIZE; h++){
 			for(tcpconn=tcpconn_addr_hash[h]; tcpconn && n; 
 					tcpconn=tcpconn->next){
 				/* FIXME: is refcnt==0 really necessary? */
8aeb47e2
 				if ((tcpconn->refcnt==0)&&(FD_ISSET(tcpconn->s, &sel_set))){
 					/* new data available */
 					n--;
 					/* pass it to child, so remove it from select list */
 					DBG("tcp_main_loop: data available on %p [h:%d] %d\n",
 							tcpconn, h, tcpconn->s);
 					FD_CLR(tcpconn->s, &master_set);
67b1fc53
 					tcpconn_ref(tcpconn); /* refcnt ++ */
8aeb47e2
 					if (send2child(tcpconn)<0){
 						LOG(L_ERR,"ERROR: tcp_main_loop: no "
 									"children available\n");
ab130758
 						TCPCONN_LOCK;
67b1fc53
 						tcpconn->refcnt--;
ab130758
 						if (tcpconn->refcnt==0){
744a2341
 							fd=tcpconn->s;
ab130758
 							_tcpconn_rm(tcpconn);
744a2341
 							close(fd);
ab130758
 						}else tcpconn->timeout=0; /* force expire*/
 						TCPCONN_UNLOCK;
8aeb47e2
 					}
5b532c7f
 				}
 			}
 		}
 		/* check unix sockets & listen | destroy connections */
a65b3026
 		/* tcp_children readers first */
 		for (r=0; r<tcp_children_no && n; r++){
 			if ( (tcp_children[r].unix_sock>0) && 
 					FD_ISSET(tcp_children[r].unix_sock, &sel_set)){
8e807134
 				/* (we can't have a fd==0, 0 is never closed )*/
5b532c7f
 				n--;
67b1fc53
 				/* read until sizeof(response)
 				 * (this is a SOCK_STREAM so read is not atomic */
a65b3026
 				bytes=recv_all(tcp_children[r].unix_sock, response,
 								sizeof(response));
6a157851
 				if (bytes==0){
6ee62314
 					/* EOF -> bad, child has died */
a65b3026
 					LOG(L_CRIT, "BUG: tcp_main_loop: dead tcp child %d\n", r);
0c5da34b
 					/* don't listen on it any more */
a65b3026
 					FD_CLR(tcp_children[r].unix_sock, &master_set);
75e2f1a6
 					/*exit(-1);*/
67b1fc53
 					continue; /* skip this and try the next one */
6a157851
 				}else if (bytes<0){
a65b3026
 					LOG(L_CRIT, "ERROR: tcp_main_loop: read from tcp child %d "
 							"%s\n", r, strerror(errno));
67b1fc53
 					/* try to ignore ? */
 					continue; /* skip this and try the next one */
6a157851
 				}
 					
a65b3026
 				DBG("tcp_main_loop: reader response= %lx, %ld from %d \n",
 						response[0], response[1], r);
0c5da34b
 				cmd=response[1];
a65b3026
 				tcpconn=(struct tcp_connection*)response[0];
0c5da34b
 				switch(cmd){
 					case CONN_RELEASE:
a65b3026
 						tcp_children[r].busy--;
0c5da34b
 						if (tcpconn){
a65b3026
 								if (tcpconn->state==S_CONN_BAD){ 
 									tcpconn_destroy(tcpconn);
 									break;
 								}
0c5da34b
 								FD_SET(tcpconn->s, &master_set);
 								if (maxfd<tcpconn->s) maxfd=tcpconn->s;
 								/* update the timeout*/
 								tcpconn->timeout=get_ticks()+TCP_CON_TIMEOUT;
ab130758
 								tcpconn_put(tcpconn);
a65b3026
 								DBG("tcp_main_loop: CONN_RELEASE  %p"
 										" refcnt= %d\n", 
 										tcpconn, tcpconn->refcnt);
0c5da34b
 						}
 						break;
 					case CONN_ERROR:
 					case CONN_DESTROY:
 					case CONN_EOF:
1c93f767
 						/* WARNING: this will auto-dec. refcnt! */
a65b3026
 						tcp_children[pt[r].idx].busy--;
 						if (tcpconn){
 							if (tcpconn->s!=-1)
 								FD_CLR(tcpconn->s, &master_set);
 							tcpconn_destroy(tcpconn);
0c5da34b
 						}
a65b3026
 						break;
 					default:
 							LOG(L_CRIT, "BUG: tcp_main_loop:  unknown cmd %d"
 										" from tcp reader %d\n",
 									cmd, r);
 				}
 			}
 		}
 		/* check "send" unix sockets & listen | destroy connections */
 		/* start from 1, the "main" process does not transmit anything*/
 		for (r=1; r<process_no && n; r++){
 			if ( (pt[r].unix_sock>0) && FD_ISSET(pt[r].unix_sock, &sel_set)){
 				/* (we can't have a fd==0, 0 is never closed )*/
 				n--;
 				/* read until sizeof(response)
 				 * (this is a SOCK_STREAM so read is not atomic */
 				bytes=recv_all(pt[r].unix_sock, response, sizeof(response));
 				if (bytes==0){
 					/* EOF -> bad, child has died */
 					LOG(L_CRIT, "BUG: tcp_main_loop: dead child %d\n", r);
 					/* don't listen on it any more */
 					FD_CLR(pt[r].unix_sock, &master_set);
 					/*exit(-1);*/
 					continue; /* skip this and try the next one */
 				}else if (bytes<0){
 					LOG(L_CRIT, "ERROR: tcp_main_loop: read from child:  %s\n",
 							strerror(errno));
 					/* try to ignore ? */
 					continue; /* skip this and try the next one */
 				}
 					
 				DBG("tcp_main_loop: read response= %lx, %ld from %d (%d)\n",
 						response[0], response[1], r, pt[r].pid);
 				cmd=response[1];
 				tcpconn=(struct tcp_connection*)response[0];
 				switch(cmd){
 					case CONN_ERROR:
0c5da34b
 						if (tcpconn){
ab130758
 							if (tcpconn->s!=-1)
 								FD_CLR(tcpconn->s, &master_set);
a65b3026
 							tcpconn_destroy(tcpconn);
0c5da34b
 						}
 						break;
 					case CONN_GET_FD:
 						/* send the requested FD  */
 						/* WARNING: take care of setting refcnt properly to
 						 * avoid race condition */
 						if (tcpconn){
67b1fc53
 							if (send_fd(pt[r].unix_sock, &tcpconn,
 										sizeof(tcpconn), tcpconn->s)<=0){
 								LOG(L_ERR, "ERROR: tcp_main_loop:"
 										"send_fd failed\n");
 							}
0c5da34b
 						}else{
 							LOG(L_CRIT, "BUG: tcp_main_loop: null pointer\n");
 						}
 						break;
 					case CONN_NEW:
 						/* update the fd in the requested tcpconn*/
 						/* WARNING: take care of setting refcnt properly to
 						 * avoid race condition */
 						if (tcpconn){
67b1fc53
 							bytes=receive_fd(pt[r].unix_sock, &tcpconn,
 									sizeof(tcpconn), &tcpconn->s);
 								if (bytes<sizeof(tcpconn)){
 									if (bytes<0){
 										LOG(L_CRIT, "BUG: tcp_main_loop:"
 												" CONN_NEW: receive_fd "
 												"failed\n");
 									}else{
 										LOG(L_CRIT, "BUG: tcp_main_loop:"
 												" CONN_NEW: to few bytes "
 												"received (%d)\n", bytes );
 									}
 									break; /* try to ignore */
 								}
0c5da34b
 							/* add tcpconn to the list*/
 							tcpconn_add(tcpconn);
5b532c7f
 							FD_SET(tcpconn->s, &master_set);
 							if (maxfd<tcpconn->s) maxfd=tcpconn->s;
6bc40dea
 							/* update the timeout*/
 							tcpconn->timeout=get_ticks()+TCP_CON_TIMEOUT;
5b532c7f
 						}else{
0c5da34b
 							LOG(L_CRIT, "BUG: tcp_main_loop: null pointer\n");
5b532c7f
 						}
0c5da34b
 						break;
 					default:
 							LOG(L_CRIT, "BUG: tcp_main_loop: unknown cmd %d\n",
 									cmd);
5b532c7f
 				}
 			}
67b1fc53
 		} /* for */
6bc40dea
 		
 		/* remove old connections */
0c5da34b
 		tcpconn_timeout(&master_set);
5b532c7f
 	
 	}
 }
 
 
 
0c5da34b
 int init_tcp()
 {
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 */
 	connection_id=(int*)shm_malloc(sizeof(int));
 	if (connection_id==0){
 		LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
 		lock_destroy(tcpconn_lock);
 		lock_dealloc((void*)tcpconn_lock);
 		tcpconn_lock=0;
 		goto error;
 	}
 	*connection_id=1;
8aeb47e2
 	/* alloc hashtables*/
67b1fc53
 	tcpconn_addr_hash=(struct tcp_connection**)shm_malloc(TCP_ADDR_HASH_SIZE*
 								sizeof(struct tcp_connection*));
 
 	if (tcpconn_addr_hash==0){
8aeb47e2
 		LOG(L_CRIT, "ERROR: init_tcp: could not alloc address hashtable\n");
2719e69b
 		shm_free(connection_id);
 		connection_id=0;
8aeb47e2
 		lock_destroy(tcpconn_lock);
 		lock_dealloc((void*)tcpconn_lock);
 		tcpconn_lock=0;
 		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");
2719e69b
 		shm_free(connection_id);
 		connection_id=0;
67b1fc53
 		shm_free(tcpconn_addr_hash);
 		tcpconn_addr_hash=0;
8aeb47e2
 		lock_destroy(tcpconn_lock);
 		lock_dealloc((void*)tcpconn_lock);
 		tcpconn_lock=0;
0c5da34b
 		goto error;
 	}
8aeb47e2
 	/* init hashtables*/
67b1fc53
 	memset((void*)tcpconn_addr_hash, 0, 
 			TCP_ADDR_HASH_SIZE * sizeof(struct tcp_connection*));
8aeb47e2
 	memset((void*)tcpconn_id_hash, 0, 
 			TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*));
0c5da34b
 	return 0;
 error:
 		return -1;
 }
 
 
 
8aeb47e2
 /* cleanup before exit */
 void destroy_tcp()
 {
 	if (tcpconn_lock){
 		lock_destroy(tcpconn_lock);
 		lock_dealloc((void*)tcpconn_lock);
 		tcpconn_lock=0;
 	}
67b1fc53
 	if(tcpconn_addr_hash){
 		shm_free(tcpconn_addr_hash);
 		tcpconn_addr_hash=0;
8aeb47e2
 	}
 	if(tcpconn_id_hash){
 		shm_free(tcpconn_id_hash);
 		tcpconn_id_hash=0;
 	}
6d44652c
 	
 	if(connection_id){
 		shm_free(connection_id);
 		connection_id=0;
 	}
8aeb47e2
 }
 
 
 
5b532c7f
 /* starts the tcp processes */
6ee62314
 int tcp_init_children()
5b532c7f
 {
 	int r;
 	int sockfd[2];
a65b3026
 	int reader_fd[2]; /* for comm. with the tcp children read  */
5b532c7f
 	pid_t pid;
 	
 	
 	/* 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;
 		}
a65b3026
 		if (socketpair(AF_UNIX, SOCK_STREAM, 0, reader_fd)<0){
 			LOG(L_ERR, "ERROR: tcp_main: socketpair failed: %s\n",
 					strerror(errno));
 			goto error;
 		}
5b532c7f
 		
6ee62314
 		process_no++;
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]);
a65b3026
 			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;
a65b3026
 			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 */
67b1fc53
 			bind_idx=0;
cb87691a
 			if (init_child(r+children_no+1) < 0) {
 				LOG(L_ERR, "init_children failed\n");
7ec958f3
 				goto error;
 			}
a65b3026
 			tcp_receive_loop(reader_fd[1]);
5b532c7f
 		}
 	}
6ee62314
 	return 0;
5b532c7f
 error:
 	return -1;
 }
 
 #endif