- applied patch from Dragos Vingarzan <vingarzan@fokus.fraunhofer.de> which
[sip-router] / tcp_main.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2001-2003 FhG Fokus
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27 /*
28  * History:
29  * --------
30  *  2002-11-29  created by andrei
31  *  2002-12-11  added tcp_send (andrei)
32  *  2003-01-20  locking fixes, hashtables (andrei)
33  *  2003-02-20  s/lock_t/gen_lock_t/ to avoid a conflict on solaris (andrei)
34  *  2003-02-25  Nagle is disabled if -DDISABLE_NAGLE (andrei)
35  *  2003-03-29  SO_REUSEADDR before calling bind to allow
36  *              server restart, Nagle set on the (hopefuly) 
37  *              correct socket (jiri)
38  *  2003-03-31  always try to find the corresponding tcp listen socket for
39  *               a temp. socket and store in in *->bind_address: added
40  *               find_tcp_si, modified tcpconn_connect (andrei)
41  *  2003-04-14  set sockopts to TOS low delay (andrei)
42  *  2003-06-30  moved tcp new connect checking & handling to
43  *               handle_new_connect (andrei)
44  *  2003-07-09  tls_close called before closing the tcp connection (andrei)
45  *  2003-10-24  converted to the new socket_info lists (andrei)
46  *  2003-10-27  tcp port aliases support added (andrei)
47  *  2003-11-04  always lock before manipulating refcnt; sendchild
48  *              does not inc refcnt by itself anymore (andrei)
49  *  2003-11-07  different unix sockets are used for fd passing
50  *              to/from readers/writers (andrei)
51  *  2003-11-17  handle_new_connect & tcp_connect will close the 
52  *              new socket if tcpconn_new return 0 (e.g. out of mem) (andrei)
53  *  2003-11-28  tcp_blocking_write & tcp_blocking_connect added (andrei)
54  *  2004-11-08  dropped find_tcp_si and replaced with find_si (andrei)
55  *  2005-06-07  new tcp optimized code, supports epoll (LT), sigio + real time
56  *               signals, poll & select (andrei)
57  *  2005-06-26  *bsd kqueue support (andrei)
58  *  2005-07-04  solaris /dev/poll support (andrei)
59  *  2005-07-08  tcp_max_connections, tcp_connection_lifetime, don't accept
60  *               more connections if tcp_max_connections is exceeded (andrei)
61  *  2005-10-21  cleanup all the open connections on exit
62  *              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
63  *              poll loop  (#ifdef) (andrei)
64  *              process all children requests, before attempting to send
65  *              them new stuff (fixes some deadlocks) (andrei)
66  *  2006-02-03  timers are run only once per s (andrei)
67  *              tcp children fds can be non-blocking; send fds are queued on
68  *              EAGAIN; lots of bug fixes (andrei)
69  *  2006-02-06  better tcp_max_connections checks, tcp_connections_no moved to
70  *              shm (andrei)
71  *  2006-04-12  tcp_send() changed to use struct dest_info (andrei)
72  */
73
74
75 #ifdef USE_TCP
76
77
78 #ifndef SHM_MEM
79 #error "shared memory support needed (add -DSHM_MEM to Makefile.defs)"
80 #endif
81
82 #include <sys/time.h>
83 #include <sys/types.h>
84 #include <sys/select.h>
85 #include <sys/socket.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/tcp.h>
90 #include <sys/uio.h>  /* writev*/
91 #include <netdb.h>
92 #include <stdlib.h> /*exit() */
93
94 #include <unistd.h>
95
96 #include <errno.h>
97 #include <string.h>
98
99 #ifdef HAVE_SELECT
100 #include <sys/select.h>
101 #endif
102 #include <sys/poll.h>
103
104
105 #include "ip_addr.h"
106 #include "pass_fd.h"
107 #include "tcp_conn.h"
108 #include "globals.h"
109 #include "pt.h"
110 #include "locking.h"
111 #include "mem/mem.h"
112 #include "mem/shm_mem.h"
113 #include "timer.h"
114 #include "sr_module.h"
115 #include "tcp_server.h"
116 #include "tcp_init.h"
117 #include "tsend.h"
118 #include "timer_ticks.h"
119 #ifdef USE_TLS
120 #include "tls/tls_server.h"
121 #endif 
122 #include "tcp_info.h"
123
124 #define local_malloc pkg_malloc
125 #define local_free   pkg_free
126
127 #define HANDLE_IO_INLINE
128 #include "io_wait.h"
129 #include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
130
131 #define TCP_PASS_NEW_CONNECTION_ON_DATA /* don't pass a new connection
132                                                                                    immediately to a child, wait for
133                                                                                    some data on it first */
134 #define TCP_LISTEN_BACKLOG 1024
135 #define SEND_FD_QUEUE /* queue send fd requests on EAGAIN, instead of sending 
136                                                         them immediately */
137 #define TCP_CHILD_NON_BLOCKING 
138 #ifdef SEND_FD_QUEUE
139 #ifndef TCP_CHILD_NON_BLOCKING
140 #define TCP_CHILD_NON_BLOCKING
141 #endif
142 #define MAX_SEND_FD_QUEUE_SIZE  tcp_max_fd_no
143 #define SEND_FD_QUEUE_SIZE              128  /* initial size */
144 #define MAX_SEND_FD_RETRIES             96       /* FIXME: not used for now */
145 #define SEND_FD_QUEUE_TIMEOUT   MS_TO_TICKS(2000)  /* 2 s */
146 #endif
147
148
149 enum fd_types { F_NONE, F_SOCKINFO /* a tcp_listen fd */,
150                                 F_TCPCONN, F_TCPCHILD, F_PROC };
151
152
153
154 int tcp_accept_aliases=0; /* by default don't accept aliases */
155 int tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
156 int tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
157 int tcp_con_lifetime=DEFAULT_TCP_CONNECTION_LIFETIME;
158 enum poll_types tcp_poll_method=0; /* by default choose the best method */
159 int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
160 int tcp_max_fd_no=0;
161
162 static int* tcp_connections_no=0; /* current open connections */
163
164 /* connection hash table (after ip&port) , includes also aliases */
165 struct tcp_conn_alias** tcpconn_aliases_hash=0;
166 /* connection hash table (after connection id) */
167 struct tcp_connection** tcpconn_id_hash=0;
168 gen_lock_t* tcpconn_lock=0;
169
170 struct tcp_child* tcp_children;
171 static int* connection_id=0; /*  unique for each connection, used for 
172                                                                 quickly finding the corresponding connection
173                                                                 for a reply */
174 int unix_tcp_sock;
175
176 static int tcp_proto_no=-1; /* tcp protocol number as returned by
177                                                            getprotobyname */
178
179 static io_wait_h io_h;
180
181
182
183 /* set all socket/fd options:  disable nagle, tos lowdelay, non-blocking
184  * return -1 on error */
185 static int init_sock_opt(int s)
186 {
187         int flags;
188         int optval;
189         
190 #ifdef DISABLE_NAGLE
191         flags=1;
192         if ( (tcp_proto_no!=-1) && (setsockopt(s, tcp_proto_no , TCP_NODELAY,
193                                         &flags, sizeof(flags))<0) ){
194                 LOG(L_WARN, "WARNING: init_sock_opt: could not disable Nagle: %s\n",
195                                 strerror(errno));
196         }
197 #endif
198         /* tos*/
199         optval = tos;
200         if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){
201                 LOG(L_WARN, "WARNING: init_sock_opt: setsockopt tos: %s\n",
202                                 strerror(errno));
203                 /* continue since this is not critical */
204         }
205         /* non-blocking */
206         flags=fcntl(s, F_GETFL);
207         if (flags==-1){
208                 LOG(L_ERR, "ERROR: init_sock_opt: fnctl failed: (%d) %s\n",
209                                 errno, strerror(errno));
210                 goto error;
211         }
212         if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
213                 LOG(L_ERR, "ERROR: init_sock_opt: fcntl: set non-blocking failed:"
214                                 " (%d) %s\n", errno, strerror(errno));
215                 goto error;
216         }
217         return 0;
218 error:
219         return -1;
220 }
221
222
223
224 /* blocking connect on a non-blocking fd; it will timeout after
225  * tcp_connect_timeout 
226  * if BLOCKING_USE_SELECT and HAVE_SELECT are defined it will internally
227  * use select() instead of poll (bad if fd > FD_SET_SIZE, poll is preferred)
228  */
229 static int tcp_blocking_connect(int fd, const struct sockaddr *servaddr,
230                                                                 socklen_t addrlen)
231 {
232         int n;
233 #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
234         fd_set sel_set;
235         fd_set orig_set;
236         struct timeval timeout;
237 #else
238         struct pollfd pf;
239 #endif
240         int elapsed;
241         int to;
242         int ticks;
243         int err;
244         unsigned int err_len;
245         int poll_err;
246         
247         poll_err=0;
248         to=tcp_connect_timeout;
249         ticks=get_ticks();
250 again:
251         n=connect(fd, servaddr, addrlen);
252         if (n==-1){
253                 if (errno==EINTR){
254                         elapsed=(get_ticks()-ticks)*TIMER_TICK;
255                         if (elapsed<to)         goto again;
256                         else goto error_timeout;
257                 }
258                 if (errno!=EINPROGRESS && errno!=EALREADY){
259                         LOG(L_ERR, "ERROR: tcp_blocking_connect: (%d) %s\n",
260                                         errno, strerror(errno));
261                         goto error;
262                 }
263         }else goto end;
264         
265         /* poll/select loop */
266 #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
267                 FD_ZERO(&orig_set);
268                 FD_SET(fd, &orig_set);
269 #else
270                 pf.fd=fd;
271                 pf.events=POLLOUT;
272 #endif
273         while(1){
274                 elapsed=(get_ticks()-ticks)*TIMER_TICK;
275                 if (elapsed<to)
276                         to-=elapsed;
277                 else 
278                         goto error_timeout;
279 #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
280                 sel_set=orig_set;
281                 timeout.tv_sec=to;
282                 timeout.tv_usec=0;
283                 n=select(fd+1, 0, &sel_set, 0, &timeout);
284 #else
285                 n=poll(&pf, 1, to*1000);
286 #endif
287                 if (n<0){
288                         if (errno==EINTR) continue;
289                         LOG(L_ERR, "ERROR: tcp_blocking_connect: poll/select failed:"
290                                         " (%d) %s\n", errno, strerror(errno));
291                         goto error;
292                 }else if (n==0) /* timeout */ continue;
293 #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
294                 if (FD_ISSET(fd, &sel_set))
295 #else
296                 if (pf.revents&(POLLERR|POLLHUP|POLLNVAL)){ 
297                         LOG(L_ERR, "ERROR: tcp_blocking_connect: poll error: flags %x\n",
298                                         pf.revents);
299                         poll_err=1;
300                 }
301 #endif
302                 {
303                         err_len=sizeof(err);
304                         getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
305                         if ((err==0) && (poll_err==0)) goto end;
306                         if (err!=EINPROGRESS && err!=EALREADY){
307                                 LOG(L_ERR, "ERROR: tcp_blocking_connect: SO_ERROR (%d) %s\n",
308                                                 err, strerror(err));
309                                 goto error;
310                         }
311                 }
312         }
313 error_timeout:
314         /* timeout */
315         LOG(L_ERR, "ERROR: tcp_blocking_connect: timeout %d s elapsed from %d s\n",
316                         elapsed, tcp_connect_timeout);
317 error:
318         return -1;
319 end:
320         return 0;
321 }
322
323
324
325 #if 0
326 /* blocking write even on non-blocking sockets 
327  * if TCP_TIMEOUT will return with error */
328 static int tcp_blocking_write(struct tcp_connection* c, int fd, char* buf,
329                                                                 unsigned int len)
330 {
331         int n;
332         fd_set sel_set;
333         struct timeval timeout;
334         int ticks;
335         int initial_len;
336         
337         initial_len=len;
338 again:
339         
340         n=send(fd, buf, len,
341 #ifdef HAVE_MSG_NOSIGNAL
342                         MSG_NOSIGNAL
343 #else
344                         0
345 #endif
346                 );
347         if (n<0){
348                 if (errno==EINTR)       goto again;
349                 else if (errno!=EAGAIN && errno!=EWOULDBLOCK){
350                         LOG(L_ERR, "tcp_blocking_write: failed to send: (%d) %s\n",
351                                         errno, strerror(errno));
352                         goto error;
353                 }
354         }else if (n<len){
355                 /* partial write */
356                 buf+=n;
357                 len-=n;
358         }else{
359                 /* success: full write */
360                 goto end;
361         }
362         while(1){
363                 FD_ZERO(&sel_set);
364                 FD_SET(fd, &sel_set);
365                 timeout.tv_sec=tcp_send_timeout;
366                 timeout.tv_usec=0;
367                 ticks=get_ticks();
368                 n=select(fd+1, 0, &sel_set, 0, &timeout);
369                 if (n<0){
370                         if (errno==EINTR) continue; /* signal, ignore */
371                         LOG(L_ERR, "ERROR: tcp_blocking_write: select failed: "
372                                         " (%d) %s\n", errno, strerror(errno));
373                         goto error;
374                 }else if (n==0){
375                         /* timeout */
376                         if (get_ticks()-ticks>=tcp_send_timeout){
377                                 LOG(L_ERR, "ERROR: tcp_blocking_write: send timeout (%d)\n",
378                                                 tcp_send_timeout);
379                                 goto error;
380                         }
381                         continue;
382                 }
383                 if (FD_ISSET(fd, &sel_set)){
384                         /* we can write again */
385                         goto again;
386                 }
387         }
388 error:
389                 return -1;
390 end:
391                 return initial_len;
392 }
393 #endif
394
395
396
397 struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
398                                                                         struct socket_info* ba, int type, 
399                                                                         int state)
400 {
401         struct tcp_connection *c;
402         
403         c=(struct tcp_connection*)shm_malloc(sizeof(struct tcp_connection));
404         if (c==0){
405                 LOG(L_ERR, "ERROR: tcpconn_new: mem. allocation failure\n");
406                 goto error;
407         }
408         memset(c, 0, sizeof(struct tcp_connection)); /* zero init */
409         c->s=sock;
410         c->fd=-1; /* not initialized */
411         if (lock_init(&c->write_lock)==0){
412                 LOG(L_ERR, "ERROR: tcpconn_new: init lock failed\n");
413                 goto error;
414         }
415         
416         c->rcv.src_su=*su;
417         
418         c->refcnt=0;
419         su2ip_addr(&c->rcv.src_ip, su);
420         c->rcv.src_port=su_getport(su);
421         c->rcv.bind_address=ba;
422         if (ba){
423                 c->rcv.dst_ip=ba->address;
424                 c->rcv.dst_port=ba->port_no;
425         }
426         print_ip("tcpconn_new: new tcp connection: ", &c->rcv.src_ip, "\n");
427         DBG(     "tcpconn_new: on port %d, type %d\n", c->rcv.src_port, type);
428         init_tcp_req(&c->req);
429         c->id=(*connection_id)++;
430         c->rcv.proto_reserved1=0; /* this will be filled before receive_message*/
431         c->rcv.proto_reserved2=0;
432         c->state=state;
433         c->extra_data=0;
434 #ifdef USE_TLS
435         if (type==PROTO_TLS){
436                 if (tls_tcpconn_init(c, sock)==-1) goto error;
437         }else
438 #endif /* USE_TLS*/
439         {
440                 c->type=PROTO_TCP;
441                 c->rcv.proto=PROTO_TCP;
442                 c->timeout=get_ticks()+tcp_con_lifetime;
443         }
444         c->flags|=F_CONN_REMOVED;
445         
446         return c;
447         
448 error:
449         if (c) shm_free(c);
450         return 0;
451 }
452
453
454
455 struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
456 {
457         int s;
458         struct socket_info* si;
459         union sockaddr_union my_name;
460         socklen_t my_name_len;
461         struct tcp_connection* con;
462         struct ip_addr ip;
463
464         s=-1;
465         
466         if (*tcp_connections_no >= tcp_max_connections){
467                 LOG(L_ERR, "ERROR: tcpconn_connect: maximum number of connections"
468                                         " exceeded (%d/%d)\n",
469                                         *tcp_connections_no, tcp_max_connections);
470                 goto error;
471         }
472         s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
473         if (s==-1){
474                 LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
475                                 errno, strerror(errno));
476                 goto error;
477         }
478         if (init_sock_opt(s)<0){
479                 LOG(L_ERR, "ERROR: tcpconn_connect: init_sock_opt failed\n");
480                 goto error;
481         }
482         if (tcp_blocking_connect(s, &server->s, sockaddru_len(*server))<0){
483                 LOG(L_ERR, "ERROR: tcpconn_connect: tcp_blocking_connect failed\n");
484                 goto error;
485         }
486         my_name_len=sizeof(my_name);
487         if (getsockname(s, &my_name.s, &my_name_len)!=0){
488                 LOG(L_ERR, "ERROR: tcp_connect: getsockname failed: %s(%d)\n",
489                                 strerror(errno), errno);
490                 si=0; /* try to go on */
491         }
492         su2ip_addr(&ip, &my_name);
493 #ifdef USE_TLS
494         if (type==PROTO_TLS)
495                 si=find_si(&ip, 0, PROTO_TLS);
496         else
497 #endif
498                 si=find_si(&ip, 0, PROTO_TCP);
499
500         if (si==0){
501                 LOG(L_ERR, "ERROR: tcp_connect: could not find corresponding"
502                                 " listening socket, using default...\n");
503                 if (server->s.sa_family==AF_INET) si=sendipv4_tcp;
504 #ifdef USE_IPV6
505                 else si=sendipv6_tcp;
506 #endif
507         }
508         con=tcpconn_new(s, server, si, type, S_CONN_CONNECT);
509         if (con==0){
510                 LOG(L_ERR, "ERROR: tcp_connect: tcpconn_new failed, closing the "
511                                  " socket\n");
512                 goto error;
513         }
514         return con;
515         /*FIXME: set sock idx! */
516 error:
517         if (s!=-1) close(s); /* close the opened socket */
518         return 0;
519 }
520
521
522
523 struct tcp_connection*  tcpconn_add(struct tcp_connection *c)
524 {
525         unsigned hash;
526
527         if (c){
528                 TCPCONN_LOCK;
529                 /* add it at the begining of the list*/
530                 hash=tcp_id_hash(c->id);
531                 c->id_hash=hash;
532                 tcpconn_listadd(tcpconn_id_hash[hash], c, id_next, id_prev);
533                 
534                 hash=tcp_addr_hash(&c->rcv.src_ip, c->rcv.src_port);
535                 /* set the first alias */
536                 c->con_aliases[0].port=c->rcv.src_port;
537                 c->con_aliases[0].hash=hash;
538                 c->con_aliases[0].parent=c;
539                 tcpconn_listadd(tcpconn_aliases_hash[hash], &c->con_aliases[0],
540                                                 next, prev);
541                 c->aliases++;
542                 TCPCONN_UNLOCK;
543                 DBG("tcpconn_add: hashes: %d, %d\n", hash, c->id_hash);
544                 return c;
545         }else{
546                 LOG(L_CRIT, "tcpconn_add: BUG: null connection pointer\n");
547                 return 0;
548         }
549 }
550
551
552 /* unsafe tcpconn_rm version (nolocks) */
553 void _tcpconn_rm(struct tcp_connection* c)
554 {
555         int r;
556         tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
557         /* remove all the aliases */
558         for (r=0; r<c->aliases; r++)
559                 tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash], 
560                                                 &c->con_aliases[r], next, prev);
561         lock_destroy(&c->write_lock);
562 #ifdef USE_TLS
563         if (c->type==PROTO_TLS) tls_tcpconn_clean(c);
564 #endif
565         shm_free(c);
566 }
567
568
569
570 void tcpconn_rm(struct tcp_connection* c)
571 {
572         int r;
573         TCPCONN_LOCK;
574         tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
575         /* remove all the aliases */
576         for (r=0; r<c->aliases; r++)
577                 tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash], 
578                                                 &c->con_aliases[r], next, prev);
579         TCPCONN_UNLOCK;
580         lock_destroy(&c->write_lock);
581 #ifdef USE_TLS
582         if ((c->type==PROTO_TLS)&&(c->extra_data)) tls_tcpconn_clean(c);
583 #endif
584         shm_free(c);
585 }
586
587
588 /* finds a connection, if id=0 uses the ip addr & port (host byte order)
589  * WARNING: unprotected (locks) use tcpconn_get unless you really
590  * know what you are doing */
591 struct tcp_connection* _tcpconn_find(int id, struct ip_addr* ip, int port)
592 {
593
594         struct tcp_connection *c;
595         struct tcp_conn_alias* a;
596         unsigned hash;
597         
598 #ifdef EXTRA_DEBUG
599         DBG("tcpconn_find: %d  port %d\n",id, port);
600         if (ip) print_ip("tcpconn_find: ip ", ip, "\n");
601 #endif
602         if (id){
603                 hash=tcp_id_hash(id);
604                 for (c=tcpconn_id_hash[hash]; c; c=c->id_next){
605 #ifdef EXTRA_DEBUG
606                         DBG("c=%p, c->id=%d, port=%d\n",c, c->id, c->rcv.src_port);
607                         print_ip("ip=", &c->rcv.src_ip, "\n");
608 #endif
609                         if ((id==c->id)&&(c->state!=S_CONN_BAD)) return c;
610                 }
611         }else if (ip){
612                 hash=tcp_addr_hash(ip, port);
613                 for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
614 #ifdef EXTRA_DEBUG
615                         DBG("a=%p, c=%p, c->id=%d, alias port= %d port=%d\n", a, a->parent,
616                                         a->parent->id, a->port, a->parent->rcv.src_port);
617                         print_ip("ip=",&a->parent->rcv.src_ip,"\n");
618 #endif
619                         if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
620                                         (ip_addr_cmp(ip, &a->parent->rcv.src_ip)) )
621                                 return a->parent;
622                 }
623         }
624         return 0;
625 }
626
627
628
629 /* _tcpconn_find with locks and timeout */
630 struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
631                                                                         int timeout)
632 {
633         struct tcp_connection* c;
634         TCPCONN_LOCK;
635         c=_tcpconn_find(id, ip, port);
636         if (c){ 
637                         c->refcnt++;
638                         c->timeout=get_ticks()+timeout;
639         }
640         TCPCONN_UNLOCK;
641         return c;
642 }
643
644
645
646 /* add port as an alias for the "id" connection
647  * returns 0 on success,-1 on failure */
648 int tcpconn_add_alias(int id, int port, int proto)
649 {
650         struct tcp_connection* c;
651         unsigned hash;
652         struct tcp_conn_alias* a;
653         
654         a=0;
655         /* fix the port */
656         port=port?port:((proto==PROTO_TLS)?SIPS_PORT:SIP_PORT);
657         TCPCONN_LOCK;
658         /* check if alias already exists */
659         c=_tcpconn_find(id, 0, 0);
660         if (c){
661                 hash=tcp_addr_hash(&c->rcv.src_ip, port);
662                 /* search the aliases for an already existing one */
663                 for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
664                         if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
665                                         (ip_addr_cmp(&c->rcv.src_ip, &a->parent->rcv.src_ip)) ){
666                                 /* found */
667                                 if (a->parent!=c) goto error_sec;
668                                 else goto ok;
669                         }
670                 }
671                 if (c->aliases>=TCP_CON_MAX_ALIASES) goto error_aliases;
672                 c->con_aliases[c->aliases].parent=c;
673                 c->con_aliases[c->aliases].port=port;
674                 c->con_aliases[c->aliases].hash=hash;
675                 tcpconn_listadd(tcpconn_aliases_hash[hash], 
676                                                                 &c->con_aliases[c->aliases], next, prev);
677                 c->aliases++;
678         }else goto error_not_found;
679 ok:
680         TCPCONN_UNLOCK;
681 #ifdef EXTRA_DEBUG
682         if (a) DBG("tcpconn_add_alias: alias already present\n");
683         else   DBG("tcpconn_add_alias: alias port %d for hash %d, id %d\n",
684                         port, hash, c->id);
685 #endif
686         return 0;
687 error_aliases:
688         TCPCONN_UNLOCK;
689         LOG(L_ERR, "ERROR: tcpconn_add_alias: too many aliases for connection %p"
690                                 " (%d)\n", c, c->id);
691         return -1;
692 error_not_found:
693         TCPCONN_UNLOCK;
694         LOG(L_ERR, "ERROR: tcpconn_add_alias: no connection found for id %d\n",id);
695         return -1;
696 error_sec:
697         TCPCONN_UNLOCK;
698         LOG(L_ERR, "ERROR: tcpconn_add_alias: possible port hijack attempt\n");
699         LOG(L_ERR, "ERROR: tcpconn_add_alias: alias already present and points"
700                         " to another connection (%d : %d and %d : %d)\n",
701                         a->parent->id,  port, c->id, port);
702         return -1;
703 }
704
705
706
707 void tcpconn_ref(struct tcp_connection* c)
708 {
709         TCPCONN_LOCK;
710         c->refcnt++; /* FIXME: atomic_dec */
711         TCPCONN_UNLOCK;
712 }
713
714
715
716 void tcpconn_put(struct tcp_connection* c)
717 {
718         TCPCONN_LOCK;
719         c->refcnt--; /* FIXME: atomic_dec */
720         TCPCONN_UNLOCK;
721 }
722
723
724
725 /* finds a tcpconn & sends on it
726  * uses the dst members to, proto (TCP|TLS) and id
727  * returns: number of bytes written (>=0) on success
728  *          <0 on error */
729 int tcp_send(struct dest_info* dst, char* buf, unsigned len)
730 {
731         struct tcp_connection *c;
732         struct tcp_connection *tmp;
733         struct ip_addr ip;
734         int port;
735         int fd;
736         long response[2];
737         int n;
738         
739         port=su_getport(&dst->to);
740         if (port){
741                 su2ip_addr(&ip, &dst->to);
742                 c=tcpconn_get(dst->id, &ip, port, tcp_con_lifetime); 
743         }else if (dst->id){
744                 c=tcpconn_get(dst->id, 0, 0, tcp_con_lifetime);
745         }else{
746                 LOG(L_CRIT, "BUG: tcp_send called with null id & to\n");
747                 return -1;
748         }
749         
750         if (dst->id){
751                 if (c==0) {
752                         if (port){
753                                 /* try again w/o id */
754                                 c=tcpconn_get(0, &ip, port, tcp_con_lifetime);
755                                 goto no_id;
756                         }else{
757                                 LOG(L_ERR, "ERROR: tcp_send: id %d not found, dropping\n",
758                                                 dst->id);
759                                 return -1;
760                         }
761                 }else goto get_fd;
762         }
763 no_id:
764                 if (c==0){
765                         DBG("tcp_send: no open tcp connection found, opening new one\n");
766                         /* create tcp connection */
767                         if ((c=tcpconn_connect(&dst->to, dst->proto))==0){
768                                 LOG(L_ERR, "ERROR: tcp_send: connect failed\n");
769                                 return -1;
770                         }
771                         c->refcnt++; /* safe to do it w/o locking, it's not yet
772                                                         available to the rest of the world */
773                         fd=c->s;
774                         
775                         /* send the new tcpconn to "tcp main" */
776                         response[0]=(long)c;
777                         response[1]=CONN_NEW;
778                         n=send_fd(unix_tcp_sock, response, sizeof(response), c->s);
779                         if (n<=0){
780                                 LOG(L_ERR, "BUG: tcp_send: failed send_fd: %s (%d)\n",
781                                                 strerror(errno), errno);
782                                 n=-1;
783                                 goto end;
784                         }       
785                         goto send_it;
786                 }
787 get_fd:
788                         /* todo: see if this is not the same process holding
789                          *  c  and if so send directly on c->fd */
790                         DBG("tcp_send: tcp connection found (%p), acquiring fd\n", c);
791                         /* get the fd */
792                         response[0]=(long)c;
793                         response[1]=CONN_GET_FD;
794                         n=send_all(unix_tcp_sock, response, sizeof(response));
795                         if (n<=0){
796                                 LOG(L_ERR, "BUG: tcp_send: failed to get fd(write):%s (%d)\n",
797                                                 strerror(errno), errno);
798                                 n=-1;
799                                 goto release_c;
800                         }
801                         DBG("tcp_send, c= %p, n=%d\n", c, n);
802                         tmp=c;
803                         n=receive_fd(unix_tcp_sock, &c, sizeof(c), &fd, MSG_WAITALL);
804                         if (n<=0){
805                                 LOG(L_ERR, "BUG: tcp_send: failed to get fd(receive_fd):"
806                                                         " %s (%d)\n", strerror(errno), errno);
807                                 n=-1;
808                                 goto release_c;
809                         }
810                         if (c!=tmp){
811                                 LOG(L_CRIT, "BUG: tcp_send: get_fd: got different connection:"
812                                                 "  %p (id= %d, refcnt=%d state=%d != "
813                                                 "  %p (id= %d, refcnt=%d state=%d (n=%d)\n",
814                                                   c,   c->id,   c->refcnt,   c->state,
815                                                   tmp, tmp->id, tmp->refcnt, tmp->state, n
816                                    );
817                                 n=-1; /* fail */
818                                 goto end;
819                         }
820                         DBG("tcp_send: after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
821                 
822         
823         
824 send_it:
825         DBG("tcp_send: sending...\n");
826         lock_get(&c->write_lock);
827 #ifdef USE_TLS
828         if (c->type==PROTO_TLS)
829                 n=tls_blocking_write(c, fd, buf, len);
830         else
831 #endif
832                 /* n=tcp_blocking_write(c, fd, buf, len); */
833                 n=tsend_stream(fd, buf, len, tcp_send_timeout*1000); 
834         lock_release(&c->write_lock);
835         DBG("tcp_send: after write: c= %p n=%d fd=%d\n",c, n, fd);
836         DBG("tcp_send: buf=\n%.*s\n", (int)len, buf);
837         if (n<0){
838                 LOG(L_ERR, "ERROR: tcp_send: failed to send\n");
839                 /* error on the connection , mark it as bad and set 0 timeout */
840                 c->state=S_CONN_BAD;
841                 c->timeout=0;
842                 /* tell "main" it should drop this (optional it will t/o anyway?)*/
843                 response[0]=(long)c;
844                 response[1]=CONN_ERROR;
845                 if (send_all(unix_tcp_sock, response, sizeof(response))<=0){
846                         LOG(L_ERR, "BUG: tcp_send: error return failed (write):%s (%d)\n",
847                                         strerror(errno), errno);
848                         tcpconn_put(c); /* deref. it manually */
849                         n=-1;
850                 }
851                 /* CONN_ERROR will auto-dec refcnt => we must not call tcpconn_put 
852                  * if it succeeds */
853                 close(fd);
854                 return n; /* error return, no tcpconn_put */
855         }
856 end:
857         close(fd);
858 release_c:
859         tcpconn_put(c); /* release c (lock; dec refcnt; unlock) */
860         return n;
861 }
862
863
864
865 int tcp_init(struct socket_info* sock_info)
866 {
867         union sockaddr_union* addr;
868         int optval;
869 #ifdef DISABLE_NAGLE
870         int flag;
871         struct protoent* pe;
872
873         if (tcp_proto_no==-1){ /* if not already set */
874                 pe=getprotobyname("tcp");
875                 if (pe==0){
876                         LOG(L_ERR, "ERROR: tcp_init: could not get TCP protocol number\n");
877                         tcp_proto_no=-1;
878                 }else{
879                         tcp_proto_no=pe->p_proto;
880                 }
881         }
882 #endif
883         
884         addr=&sock_info->su;
885         /* sock_info->proto=PROTO_TCP; */
886         if (init_su(addr, &sock_info->address, sock_info->port_no)<0){
887                 LOG(L_ERR, "ERROR: tcp_init: could no init sockaddr_union\n");
888                 goto error;
889         }
890         sock_info->socket=socket(AF2PF(addr->s.sa_family), SOCK_STREAM, 0);
891         if (sock_info->socket==-1){
892                 LOG(L_ERR, "ERROR: tcp_init: socket: %s\n", strerror(errno));
893                 goto error;
894         }
895 #ifdef DISABLE_NAGLE
896         flag=1;
897         if ( (tcp_proto_no!=-1) &&
898                  (setsockopt(sock_info->socket, tcp_proto_no , TCP_NODELAY,
899                                          &flag, sizeof(flag))<0) ){
900                 LOG(L_ERR, "ERROR: tcp_init: could not disable Nagle: %s\n",
901                                 strerror(errno));
902         }
903 #endif
904
905
906 #if  !defined(TCP_DONT_REUSEADDR) 
907         /* Stevens, "Network Programming", Section 7.5, "Generic Socket
908      * Options": "...server started,..a child continues..on existing
909          * connection..listening server is restarted...call to bind fails
910          * ... ALL TCP servers should specify the SO_REUSEADDRE option 
911          * to allow the server to be restarted in this situation
912          *
913          * Indeed, without this option, the server can't restart.
914          *   -jiri
915          */
916         optval=1;
917         if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR,
918                                 (void*)&optval, sizeof(optval))==-1) {
919                 LOG(L_ERR, "ERROR: tcp_init: setsockopt %s\n",
920                         strerror(errno));
921                 goto error;
922         }
923 #endif
924         /* tos */
925         optval = tos;
926         if (setsockopt(sock_info->socket, IPPROTO_IP, IP_TOS, (void*)&optval, 
927                                 sizeof(optval)) ==-1){
928                 LOG(L_WARN, "WARNING: tcp_init: setsockopt tos: %s\n", strerror(errno));
929                 /* continue since this is not critical */
930         }
931         if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
932                 LOG(L_ERR, "ERROR: tcp_init: bind(%x, %p, %d) on %s:%d : %s\n",
933                                 sock_info->socket,  &addr->s, 
934                                 (unsigned)sockaddru_len(*addr),
935                                 sock_info->address_str.s,
936                                 sock_info->port_no,
937                                 strerror(errno));
938                 goto error;
939         }
940         if (listen(sock_info->socket, TCP_LISTEN_BACKLOG)==-1){
941                 LOG(L_ERR, "ERROR: tcp_init: listen(%x, %p, %d) on %s: %s\n",
942                                 sock_info->socket, &addr->s, 
943                                 (unsigned)sockaddru_len(*addr),
944                                 sock_info->address_str.s,
945                                 strerror(errno));
946                 goto error;
947         }
948         
949         return 0;
950 error:
951         if (sock_info->socket!=-1){
952                 close(sock_info->socket);
953                 sock_info->socket=-1;
954         }
955         return -1;
956 }
957
958
959
960 /* used internally by tcp_main_loop() */
961 static void tcpconn_destroy(struct tcp_connection* tcpconn)
962 {
963         int fd;
964
965         TCPCONN_LOCK; /*avoid races w/ tcp_send*/
966         tcpconn->refcnt--;
967         if (tcpconn->refcnt==0){ 
968                 DBG("tcpconn_destroy: destroying connection %p, flags %04x\n",
969                                 tcpconn, tcpconn->flags);
970                 fd=tcpconn->s;
971 #ifdef USE_TLS
972                 /*FIXME: lock ->writelock ? */
973                 if (tcpconn->type==PROTO_TLS)
974                         tls_close(tcpconn, fd);
975 #endif
976                 _tcpconn_rm(tcpconn);
977                 close(fd);
978                 (*tcp_connections_no)--;
979         }else{
980                 /* force timeout */
981                 tcpconn->timeout=0;
982                 tcpconn->state=S_CONN_BAD;
983                 DBG("tcpconn_destroy: delaying (%p, flags %04x) ...\n",
984                                 tcpconn, tcpconn->flags);
985                 
986         }
987         TCPCONN_UNLOCK;
988 }
989
990
991
992 #ifdef SEND_FD_QUEUE
993 struct send_fd_info{
994         struct tcp_connection* tcp_conn;
995         ticks_t expire;
996         int unix_sock;
997         unsigned int retries; /* debugging */
998 };
999
1000 struct tcp_send_fd_q{
1001         struct send_fd_info* data; /* buffer */
1002         struct send_fd_info* crt;  /* pointer inside the buffer */
1003         struct send_fd_info* end;  /* points after the last valid position */
1004 };
1005
1006
1007 static struct tcp_send_fd_q send2child_q;
1008
1009
1010
1011 static int send_fd_queue_init(struct tcp_send_fd_q *q, unsigned int size)
1012 {
1013         q->data=pkg_malloc(size*sizeof(struct send_fd_info));
1014         if (q->data==0){
1015                 LOG(L_ERR, "ERROR: send_fd_queue_init: out of memory\n");
1016                 return -1;
1017         }
1018         q->crt=&q->data[0];
1019         q->end=&q->data[size];
1020         return 0;
1021 }
1022
1023 static void send_fd_queue_destroy(struct tcp_send_fd_q *q)
1024 {
1025         if (q->data){
1026                 pkg_free(q->data);
1027                 q->data=0;
1028                 q->crt=q->end=0;
1029         }
1030 }
1031
1032
1033
1034 static int init_send_fd_queues()
1035 {
1036         if (send_fd_queue_init(&send2child_q, SEND_FD_QUEUE_SIZE)!=0)
1037                 goto error;
1038         return 0;
1039 error:
1040         LOG(L_ERR, "ERROR: init_send_fd_queues: init failed\n");
1041         return -1;
1042 }
1043
1044
1045
1046 static void destroy_send_fd_queues()
1047 {
1048         send_fd_queue_destroy(&send2child_q);
1049 }
1050
1051
1052
1053
1054 inline static int send_fd_queue_add(    struct tcp_send_fd_q* q, 
1055                                                                                 int unix_sock,
1056                                                                                 struct tcp_connection *t)
1057 {
1058         struct send_fd_info* tmp;
1059         unsigned long new_size;
1060         
1061         if (q->crt>=q->end){
1062                 new_size=q->end-&q->data[0];
1063                 if (new_size< MAX_SEND_FD_QUEUE_SIZE/2){
1064                         new_size*=2;
1065                 }else new_size=MAX_SEND_FD_QUEUE_SIZE;
1066                 if (q->crt>=&q->data[new_size]){
1067                         LOG(L_ERR, "ERROR: send_fd_queue_add: queue full: %ld/%ld\n",
1068                                         (long)(q->crt-&q->data[0]-1), new_size);
1069                         goto error;
1070                 }
1071                 LOG(L_CRIT, "INFO: send_fd_queue: queue full: %ld, extending to %ld\n",
1072                                 (long)(q->end-&q->data[0]), new_size);
1073                 tmp=pkg_realloc(q->data, new_size*sizeof(struct send_fd_info));
1074                 if (tmp==0){
1075                         LOG(L_ERR, "ERROR: send_fd_queue_add: out of memory\n");
1076                         goto error;
1077                 }
1078                 q->crt=(q->crt-&q->data[0])+tmp;
1079                 q->data=tmp;
1080                 q->end=&q->data[new_size];
1081         }
1082         q->crt->tcp_conn=t;
1083         q->crt->unix_sock=unix_sock;
1084         q->crt->expire=get_ticks_raw()+SEND_FD_QUEUE_TIMEOUT;
1085         q->crt->retries=0;
1086         q->crt++;
1087         return 0;
1088 error:
1089         return -1;
1090 }
1091
1092
1093
1094 inline static void send_fd_queue_run(struct tcp_send_fd_q* q)
1095 {
1096         struct send_fd_info* p;
1097         struct send_fd_info* t;
1098         
1099         for (p=t=&q->data[0]; p<q->crt; p++){
1100                 if (send_fd(p->unix_sock, &(p->tcp_conn),
1101                                         sizeof(struct tcp_connection*), p->tcp_conn->s)<=0){
1102                         if ( ((errno==EAGAIN)||(errno==EWOULDBLOCK)) && 
1103                                                         ((s_ticks_t)(p->expire-get_ticks_raw())>0)){
1104                                 /* leave in queue for a future try */
1105                                 *t=*p;
1106                                 t->retries++;
1107                                 t++;
1108                         }else{
1109                                 LOG(L_ERR, "ERROR: run_send_fd_queue: send_fd failed"
1110                                                    " on socket %d , queue entry %ld, retries %d,"
1111                                                    " connection %p, tcp socket %d, errno=%d (%s) \n",
1112                                                    p->unix_sock, (long)(p-&q->data[0]), p->retries,
1113                                                    p->tcp_conn, p->tcp_conn->s, errno,
1114                                                    strerror(errno));
1115                                 tcpconn_destroy(p->tcp_conn);
1116                         }
1117                 }
1118         }
1119         q->crt=t;
1120 }
1121 #else
1122 #define send_fd_queue_run(q)
1123 #endif
1124
1125
1126
1127 /* handles io from a tcp child process
1128  * params: tcp_c - pointer in the tcp_children array, to the entry for
1129  *                 which an io event was detected 
1130  *         fd_i  - fd index in the fd_array (usefull for optimizing
1131  *                 io_watch_deletes)
1132  * returns:  handle_* return convention: -1 on error, 0 on EAGAIN (no more
1133  *           io events queued), >0 on success. success/error refer only to
1134  *           the reads from the fd.
1135  */
1136 inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
1137 {
1138         struct tcp_connection* tcpconn;
1139         long response[2];
1140         int cmd;
1141         int bytes;
1142         
1143         if (tcp_c->unix_sock<=0){
1144                 /* (we can't have a fd==0, 0 is never closed )*/
1145                 LOG(L_CRIT, "BUG: handle_tcp_child: fd %d for %d "
1146                                 "(pid %d, ser no %d)\n", tcp_c->unix_sock,
1147                                 (int)(tcp_c-&tcp_children[0]), tcp_c->pid, tcp_c->proc_no);
1148                 goto error;
1149         }
1150         /* read until sizeof(response)
1151          * (this is a SOCK_STREAM so read is not atomic) */
1152         bytes=recv_all(tcp_c->unix_sock, response, sizeof(response), MSG_DONTWAIT);
1153         if (bytes<(int)sizeof(response)){
1154                 if (bytes==0){
1155                         /* EOF -> bad, child has died */
1156                         DBG("DBG: handle_tcp_child: dead tcp child %d (pid %d, no %d)"
1157                                         " (shutting down?)\n", (int)(tcp_c-&tcp_children[0]), 
1158                                         tcp_c->pid, tcp_c->proc_no );
1159                         /* don't listen on it any more */
1160                         io_watch_del(&io_h, tcp_c->unix_sock, fd_i, 0); 
1161                         goto error; /* eof. so no more io here, it's ok to return error */
1162                 }else if (bytes<0){
1163                         /* EAGAIN is ok if we try to empty the buffer
1164                          * e.g.: SIGIO_RT overflow mode or EPOLL ET */
1165                         if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
1166                                 LOG(L_CRIT, "ERROR: handle_tcp_child: read from tcp child %ld "
1167                                                 " (pid %d, no %d) %s [%d]\n",
1168                                                 (long)(tcp_c-&tcp_children[0]), tcp_c->pid,
1169                                                 tcp_c->proc_no, strerror(errno), errno );
1170                         }else{
1171                                 bytes=0;
1172                         }
1173                         /* try to ignore ? */
1174                         goto end;
1175                 }else{
1176                         /* should never happen */
1177                         LOG(L_CRIT, "BUG: handle_tcp_child: too few bytes received (%d)\n",
1178                                         bytes );
1179                         bytes=0; /* something was read so there is no error; otoh if
1180                                           receive_fd returned less then requested => the receive
1181                                           buffer is empty => no more io queued on this fd */
1182                         goto end;
1183                 }
1184         }
1185         
1186         DBG("handle_tcp_child: reader response= %lx, %ld from %d \n",
1187                                         response[0], response[1], (int)(tcp_c-&tcp_children[0]));
1188         cmd=response[1];
1189         tcpconn=(struct tcp_connection*)response[0];
1190         if (tcpconn==0){
1191                 /* should never happen */
1192                 LOG(L_CRIT, "BUG: handle_tcp_child: null tcpconn pointer received"
1193                                  " from tcp child %d (pid %d): %lx, %lx\n",
1194                                         (int)(tcp_c-&tcp_children[0]), tcp_c->pid,
1195                                         response[0], response[1]) ;
1196                 goto end;
1197         }
1198         switch(cmd){
1199                 case CONN_RELEASE:
1200                         tcp_c->busy--;
1201                         if (tcpconn->state==S_CONN_BAD){ 
1202                                 tcpconn_destroy(tcpconn);
1203                                 break;
1204                         }
1205                         /* update the timeout*/
1206                         tcpconn->timeout=get_ticks()+tcp_con_lifetime;
1207                         tcpconn_put(tcpconn);
1208                         /* must be after the de-ref*/
1209                         io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
1210                         tcpconn->flags&=~F_CONN_REMOVED;
1211                         DBG("handle_tcp_child: CONN_RELEASE  %p refcnt= %d\n", 
1212                                                                                         tcpconn, tcpconn->refcnt);
1213                         break;
1214                 case CONN_ERROR:
1215                 case CONN_DESTROY:
1216                 case CONN_EOF:
1217                         /* WARNING: this will auto-dec. refcnt! */
1218                                 tcp_c->busy--;
1219                                 /* main doesn't listen on it => we don't have to delete it
1220                                  if (tcpconn->s!=-1)
1221                                         io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
1222                                 */
1223                                 tcpconn_destroy(tcpconn); /* closes also the fd */
1224                                 break;
1225                 default:
1226                                 LOG(L_CRIT, "BUG: handle_tcp_child:  unknown cmd %d"
1227                                                                         " from tcp reader %d\n",
1228                                                                         cmd, (int)(tcp_c-&tcp_children[0]));
1229         }
1230 end:
1231         return bytes;
1232 error:
1233         return -1;
1234 }
1235
1236
1237
1238 /* handles io from a "generic" ser process (get fd or new_fd from a tcp_send)
1239  * 
1240  * params: p     - pointer in the ser processes array (pt[]), to the entry for
1241  *                 which an io event was detected
1242  *         fd_i  - fd index in the fd_array (usefull for optimizing
1243  *                 io_watch_deletes)
1244  * returns:  handle_* return convention:
1245  *          -1 on error reading from the fd,
1246  *           0 on EAGAIN  or when no  more io events are queued 
1247  *             (receive buffer empty),
1248  *           >0 on successfull reads from the fd (the receive buffer might
1249  *             be non-empty).
1250  */
1251 inline static int handle_ser_child(struct process_table* p, int fd_i)
1252 {
1253         struct tcp_connection* tcpconn;
1254         long response[2];
1255         int cmd;
1256         int bytes;
1257         int ret;
1258         int fd;
1259         
1260         ret=-1;
1261         if (p->unix_sock<=0){
1262                 /* (we can't have a fd==0, 0 is never closed )*/
1263                 LOG(L_CRIT, "BUG: handle_ser_child: fd %d for %d "
1264                                 "(pid %d)\n", p->unix_sock, (int)(p-&pt[0]), p->pid);
1265                 goto error;
1266         }
1267                         
1268         /* get all bytes and the fd (if transmitted)
1269          * (this is a SOCK_STREAM so read is not atomic) */
1270         bytes=receive_fd(p->unix_sock, response, sizeof(response), &fd,
1271                                                 MSG_DONTWAIT);
1272         if (bytes<(int)sizeof(response)){
1273                 /* too few bytes read */
1274                 if (bytes==0){
1275                         /* EOF -> bad, child has died */
1276                         DBG("DBG: handle_ser_child: dead child %d, pid %d"
1277                                         " (shutting down?)\n", (int)(p-&pt[0]), p->pid);
1278                         /* don't listen on it any more */
1279                         io_watch_del(&io_h, p->unix_sock, fd_i, 0);
1280                         goto error; /* child dead => no further io events from it */
1281                 }else if (bytes<0){
1282                         /* EAGAIN is ok if we try to empty the buffer
1283                          * e.g: SIGIO_RT overflow mode or EPOLL ET */
1284                         if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
1285                                 LOG(L_CRIT, "ERROR: handle_ser_child: read from child %d  "
1286                                                 "(pid %d):  %s [%d]\n", (int)(p-&pt[0]), p->pid,
1287                                                 strerror(errno), errno);
1288                                 ret=-1;
1289                         }else{
1290                                 ret=0;
1291                         }
1292                         /* try to ignore ? */
1293                         goto end;
1294                 }else{
1295                         /* should never happen */
1296                         LOG(L_CRIT, "BUG: handle_ser_child: too few bytes received (%d)\n",
1297                                         bytes );
1298                         ret=0; /* something was read so there is no error; otoh if
1299                                           receive_fd returned less then requested => the receive
1300                                           buffer is empty => no more io queued on this fd */
1301                         goto end;
1302                 }
1303         }
1304         ret=1; /* something was received, there might be more queued */
1305         DBG("handle_ser_child: read response= %lx, %ld, fd %d from %d (%d)\n",
1306                                         response[0], response[1], fd, (int)(p-&pt[0]), p->pid);
1307         cmd=response[1];
1308         tcpconn=(struct tcp_connection*)response[0];
1309         if (tcpconn==0){
1310                 LOG(L_CRIT, "BUG: handle_ser_child: null tcpconn pointer received"
1311                                  " from child %d (pid %d): %lx, %lx\n",
1312                                         (int)(p-&pt[0]), p->pid, response[0], response[1]) ;
1313                 goto end;
1314         }
1315         switch(cmd){
1316                 case CONN_ERROR:
1317                         if (!(tcpconn->flags & F_CONN_REMOVED) && (tcpconn->s!=-1)){
1318                                 io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
1319                                 tcpconn->flags|=F_CONN_REMOVED;
1320                         }
1321                         tcpconn_destroy(tcpconn); /* will close also the fd */
1322                         break;
1323                 case CONN_GET_FD:
1324                         /* send the requested FD  */
1325                         /* WARNING: take care of setting refcnt properly to
1326                          * avoid race condition */
1327                         if (send_fd(p->unix_sock, &tcpconn, sizeof(tcpconn),
1328                                                         tcpconn->s)<=0){
1329                                 LOG(L_ERR, "ERROR: handle_ser_child: send_fd failed\n");
1330                         }
1331                         break;
1332                 case CONN_NEW:
1333                         /* update the fd in the requested tcpconn*/
1334                         /* WARNING: take care of setting refcnt properly to
1335                          * avoid race condition */
1336                         if (fd==-1){
1337                                 LOG(L_CRIT, "BUG: handle_ser_child: CONN_NEW:"
1338                                                         " no fd received\n");
1339                                 break;
1340                         }
1341                         (*tcp_connections_no)++;
1342                         tcpconn->s=fd;
1343                         /* add tcpconn to the list*/
1344                         tcpconn_add(tcpconn);
1345                         /* update the timeout*/
1346                         tcpconn->timeout=get_ticks()+tcp_con_lifetime;
1347                         io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
1348                         tcpconn->flags&=~F_CONN_REMOVED;
1349                         break;
1350                 default:
1351                         LOG(L_CRIT, "BUG: handle_ser_child: unknown cmd %d\n", cmd);
1352         }
1353 end:
1354         return ret;
1355 error:
1356         return -1;
1357 }
1358
1359
1360
1361 /* sends a tcpconn + fd to a choosen child */
1362 inline static int send2child(struct tcp_connection* tcpconn)
1363 {
1364         int i;
1365         int min_busy;
1366         int idx;
1367         static int crt=0; /* current child */
1368         int last;
1369         
1370         min_busy=tcp_children[0].busy;
1371         idx=0;
1372         last=crt+tcp_children_no;
1373         for (; crt<last; crt++){
1374                 i=crt%tcp_children_no;
1375                 if (!tcp_children[i].busy){
1376                         idx=i;
1377                         min_busy=0;
1378                         break;
1379                 }else if (min_busy>tcp_children[i].busy){
1380                         min_busy=tcp_children[i].busy;
1381                         idx=i;
1382                 }
1383         }
1384         crt=idx+1; /* next time we start with crt%tcp_children_no */
1385         
1386         tcp_children[idx].busy++;
1387         tcp_children[idx].n_reqs++;
1388         if (min_busy){
1389                 DBG("WARNING: send2child: no free tcp receiver, "
1390                                 " connection passed to the least busy one (%d)\n",
1391                                 min_busy);
1392         }
1393         DBG("send2child: to tcp child %d %d(%d), %p\n", idx, 
1394                                         tcp_children[idx].proc_no,
1395                                         tcp_children[idx].pid, tcpconn);
1396         /* first make sure this child doesn't have pending request for
1397          * tcp_main (to avoid a possible deadlock: e.g. child wants to
1398          * send a release command, but the master fills its socket buffer
1399          * with new connection commands => deadlock) */
1400         /* answer tcp_send requests first */
1401         while(handle_ser_child(&pt[tcp_children[idx].proc_no], -1)>0);
1402         /* process tcp readers requests */
1403         while(handle_tcp_child(&tcp_children[idx], -1)>0);
1404                 
1405 #ifdef SEND_FD_QUEUE
1406         /* if queue full, try to queue the io */
1407         if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
1408                         tcpconn->s)<=0){
1409                 if ((errno==EAGAIN)||(errno==EWOULDBLOCK)){
1410                         /* FIXME: remove after debugging */
1411                          LOG(L_CRIT, "INFO: tcp child %d, socket %d: queue full,"
1412                                                 " %d requests queued (total handled %d)\n",
1413                                         idx, tcp_children[idx].unix_sock, min_busy,
1414                                         tcp_children[idx].n_reqs-1);
1415                         if (send_fd_queue_add(&send2child_q, tcp_children[idx].unix_sock, 
1416                                                 tcpconn)!=0){
1417                                 LOG(L_ERR, "ERROR: send2child: queue send op. failed\n");
1418                                 return -1;
1419                         }
1420                 }else{
1421                         LOG(L_ERR, "ERROR: send2child: send_fd failed\n");
1422                         return -1;
1423                 }
1424         }
1425 #else
1426         if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
1427                         tcpconn->s)<=0){
1428                 LOG(L_ERR, "ERROR: send2child: send_fd failed\n");
1429                 return -1;
1430         }
1431 #endif
1432         
1433         return 0;
1434 }
1435
1436
1437
1438 /* handles a new connection, called internally by tcp_main_loop/handle_io.
1439  * params: si - pointer to one of the tcp socket_info structures on which
1440  *              an io event was detected (connection attempt)
1441  * returns:  handle_* return convention: -1 on error, 0 on EAGAIN (no more
1442  *           io events queued), >0 on success. success/error refer only to
1443  *           the accept.
1444  */
1445 static inline int handle_new_connect(struct socket_info* si)
1446 {
1447         union sockaddr_union su;
1448         struct tcp_connection* tcpconn;
1449         socklen_t su_len;
1450         int new_sock;
1451         
1452         /* got a connection on r */
1453         su_len=sizeof(su);
1454         new_sock=accept(si->socket, &(su.s), &su_len);
1455         if (new_sock==-1){
1456                 if ((errno==EAGAIN)||(errno==EWOULDBLOCK))
1457                         return 0;
1458                 LOG(L_ERR,  "WARNING: handle_new_connect: error while accepting"
1459                                 " connection(%d): %s\n", errno, strerror(errno));
1460                 return -1;
1461         }
1462         if (*tcp_connections_no>=tcp_max_connections){
1463                 LOG(L_ERR, "ERROR: maximum number of connections exceeded: %d/%d\n",
1464                                         *tcp_connections_no, tcp_max_connections);
1465                 close(new_sock);
1466                 return 1; /* success, because the accept was succesfull */
1467         }
1468         if (init_sock_opt(new_sock)<0){
1469                 LOG(L_ERR, "ERROR: handle_new_connect: init_sock_opt failed\n");
1470                 close(new_sock);
1471                 return 1; /* success, because the accept was succesfull */
1472         }
1473         (*tcp_connections_no)++;
1474         
1475         /* add socket to list */
1476         tcpconn=tcpconn_new(new_sock, &su, si, si->proto, S_CONN_ACCEPT);
1477         if (tcpconn){
1478 #ifdef TCP_PASS_NEW_CONNECTION_ON_DATA
1479                 io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
1480                 tcpconn->flags&=~F_CONN_REMOVED;
1481                 tcpconn_add(tcpconn);
1482 #else
1483                 tcpconn->refcnt++; /* safe, not yet available to the
1484                                                           outside world */
1485                 tcpconn_add(tcpconn);
1486                 DBG("handle_new_connect: new connection: %p %d flags: %04x\n",
1487                         tcpconn, tcpconn->s, tcpconn->flags);
1488                 /* pass it to a child */
1489                 if(send2child(tcpconn)<0){
1490                         LOG(L_ERR,"ERROR: handle_new_connect: no children "
1491                                         "available\n");
1492                         tcpconn_destroy(tcpconn);
1493                 }
1494 #endif
1495         }else{ /*tcpconn==0 */
1496                 LOG(L_ERR, "ERROR: handle_new_connect: tcpconn_new failed, "
1497                                 "closing socket\n");
1498                 close(new_sock);
1499                 (*tcp_connections_no)--;
1500         }
1501         return 1; /* accept() was succesfull */
1502 }
1503
1504
1505
1506 /* handles an io event on one of the watched tcp connections
1507  * 
1508  * params: tcpconn - pointer to the tcp_connection for which we have an io ev.
1509  *         fd_i    - index in the fd_array table (needed for delete)
1510  * returns:  handle_* return convention, but on success it always returns 0
1511  *           (because it's one-shot, after a succesfull execution the fd is
1512  *            removed from tcp_main's watch fd list and passed to a child =>
1513  *            tcp_main is not interested in further io events that might be
1514  *            queued for this fd)
1515  */
1516 inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, int fd_i)
1517 {
1518         /*  is refcnt!=0 really necessary? 
1519          *  No, in fact it's a bug: I can have the following situation: a send only
1520          *   tcp connection used by n processes simultaneously => refcnt = n. In 
1521          *   the same time I can have a read event and this situation is perfectly
1522          *   valid. -- andrei
1523          */
1524 #if 0
1525         if ((tcpconn->refcnt!=0)){
1526                 /* FIXME: might be valid for sigio_rt iff fd flags are not cleared
1527                  *        (there is a short window in which it could generate a sig
1528                  *         that would be catched by tcp_main) */
1529                 LOG(L_CRIT, "BUG: handle_tcpconn_ev: io event on referenced"
1530                                         " tcpconn (%p), refcnt=%d, fd=%d\n",
1531                                         tcpconn, tcpconn->refcnt, tcpconn->s);
1532                 return -1;
1533         }
1534 #endif
1535         /* pass it to child, so remove it from the io watch list */
1536         DBG("handle_tcpconn_ev: data available on %p %d\n", tcpconn, tcpconn->s);
1537         if (io_watch_del(&io_h, tcpconn->s, fd_i, 0)==-1) goto error;
1538         tcpconn->flags|=F_CONN_REMOVED;
1539         tcpconn_ref(tcpconn); /* refcnt ++ */
1540         if (send2child(tcpconn)<0){
1541                 LOG(L_ERR,"ERROR: handle_tcpconn_ev: no children available\n");
1542                 tcpconn_destroy(tcpconn);
1543 #if 0
1544                 TCPCONN_LOCK;
1545                 tcpconn->refcnt--;
1546                 if (tcpconn->refcnt==0){
1547                         fd=tcpconn->s;
1548                         _tcpconn_rm(tcpconn);
1549                         close(fd);
1550                 }else tcpconn->timeout=0; /* force expire*/
1551                 TCPCONN_UNLOCK;
1552 #endif
1553         }
1554         return 0; /* we are not interested in possibly queued io events, 
1555                                  the fd was either passed to a child, or closed */
1556 error:
1557         return -1;
1558 }
1559
1560
1561
1562 /* generic handle io routine, it will call the appropiate
1563  *  handle_xxx() based on the fd_map type
1564  *
1565  * params:  fm  - pointer to a fd hash entry
1566  *          idx - index in the fd_array (or -1 if not known)
1567  * return: -1 on error
1568  *          0 on EAGAIN or when by some other way it is known that no more 
1569  *            io events are queued on the fd (the receive buffer is empty).
1570  *            Usefull to detect when there are no more io events queued for
1571  *            sigio_rt, epoll_et, kqueue.
1572  *         >0 on successfull read from the fd (when there might be more io
1573  *            queued -- the receive buffer might still be non-empty)
1574  */
1575 inline static int handle_io(struct fd_map* fm, int idx)
1576 {       
1577         int ret;
1578         
1579         switch(fm->type){
1580                 case F_SOCKINFO:
1581                         ret=handle_new_connect((struct socket_info*)fm->data);
1582                         break;
1583                 case F_TCPCONN:
1584                         ret=handle_tcpconn_ev((struct tcp_connection*)fm->data, idx);
1585                         break;
1586                 case F_TCPCHILD:
1587                         ret=handle_tcp_child((struct tcp_child*)fm->data, idx);
1588                         break;
1589                 case F_PROC:
1590                         ret=handle_ser_child((struct process_table*)fm->data, idx);
1591                         break;
1592                 case F_NONE:
1593                         LOG(L_CRIT, "BUG: handle_io: empty fd map\n");
1594                         goto error;
1595                 default:
1596                         LOG(L_CRIT, "BUG: handle_io: uknown fd type %d\n", fm->type); 
1597                         goto error;
1598         }
1599         return ret;
1600 error:
1601         return -1;
1602 }
1603
1604
1605
1606 /* very inefficient for now - FIXME
1607  * keep in sync with tcpconn_destroy, the "delete" part should be
1608  * the same except for io_watch_del..*/
1609 static inline void tcpconn_timeout(int force)
1610 {
1611         static int prev_ticks=0;
1612         struct tcp_connection *c, *next;
1613         unsigned int ticks;
1614         unsigned h;
1615         int fd;
1616         
1617         
1618         ticks=get_ticks();
1619         if ((ticks==prev_ticks) && !force) return;
1620         prev_ticks=ticks;
1621         TCPCONN_LOCK; /* fixme: we can lock only on delete IMO */
1622         for(h=0; h<TCP_ID_HASH_SIZE; h++){
1623                 c=tcpconn_id_hash[h];
1624                 while(c){
1625                         next=c->id_next;
1626                         if (force ||((c->refcnt==0) && ((int)(ticks-c->timeout)>=0))){
1627                                 if (!force)
1628                                         DBG("tcpconn_timeout: timeout for hash=%d - %p"
1629                                                         " (%d > %d)\n", h, c, ticks, c->timeout);
1630                                 fd=c->s;
1631 #ifdef USE_TLS
1632                                 if (c->type==PROTO_TLS)
1633                                         tls_close(c, fd);
1634 #endif
1635                                 _tcpconn_rm(c);
1636                                 if ((fd>0)&&(c->refcnt==0)) {
1637                                         if (!(c->flags & F_CONN_REMOVED)){
1638                                                 io_watch_del(&io_h, fd, -1, IO_FD_CLOSING);
1639                                                 c->flags|=F_CONN_REMOVED;
1640                                         }
1641                                         close(fd);
1642                                 }
1643                                 (*tcp_connections_no)--;
1644                         }
1645                         c=next;
1646                 }
1647         }
1648         TCPCONN_UNLOCK;
1649 }
1650
1651
1652
1653 /* tcp main loop */
1654 void tcp_main_loop()
1655 {
1656
1657         struct socket_info* si;
1658         int r;
1659         
1660         /* init send fd queues (here because we want mem. alloc only in the tcp
1661          *  process */
1662 #ifdef SEND_FD_QUEUE
1663         if (init_send_fd_queues()<0){
1664                 LOG(L_CRIT, "ERROR: init_tcp: could not init send fd queues\n");
1665                 goto error;
1666         }
1667 #endif
1668         /* init io_wait (here because we want the memory allocated only in
1669          * the tcp_main process) */
1670         
1671         /* FIXME: TODO: make tcp_max_fd_no a config param */
1672         if  (init_io_wait(&io_h, tcp_max_fd_no, tcp_poll_method)<0)
1673                 goto error;
1674         /* init: start watching all the fds*/
1675         
1676         /* add all the sockets we listens on for connections */
1677         for (si=tcp_listen; si; si=si->next){
1678                 if ((si->proto==PROTO_TCP) &&(si->socket!=-1)){
1679                         if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
1680                                 LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
1681                                                         "listen socket to the fd list\n");
1682                                 goto error;
1683                         }
1684                 }else{
1685                         LOG(L_CRIT, "BUG: tcp_main_loop: non tcp address in tcp_listen\n");
1686                 }
1687         }
1688 #ifdef USE_TLS
1689         if (!tls_disable){
1690                 for (si=tls_listen; si; si=si->next){
1691                         if ((si->proto==PROTO_TLS) && (si->socket!=-1)){
1692                                 if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
1693                                         LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
1694                                                         "tls listen socket to the fd list\n");
1695                                         goto error;
1696                                 }
1697                         }else{
1698                                 LOG(L_CRIT, "BUG: tcp_main_loop: non tls address"
1699                                                 " in tls_listen\n");
1700                         }
1701                 }
1702         }
1703 #endif
1704         /* add all the unix sockets used for communcation with other ser processes
1705          *  (get fd, new connection a.s.o) */
1706         for (r=1; r<process_no; r++){
1707                 if (pt[r].unix_sock>0) /* we can't have 0, we never close it!*/
1708                         if (io_watch_add(&io_h, pt[r].unix_sock, F_PROC, &pt[r])<0){
1709                                         LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
1710                                                         "process %d unix socket to the fd list\n", r);
1711                                         goto error;
1712                         }
1713         }
1714         /* add all the unix sokets used for communication with the tcp childs */
1715         for (r=0; r<tcp_children_no; r++){
1716                 if (tcp_children[r].unix_sock>0)/*we can't have 0, we never close it!*/
1717                         if (io_watch_add(&io_h, tcp_children[r].unix_sock, F_TCPCHILD,
1718                                                         &tcp_children[r]) <0){
1719                                 LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
1720                                                 "tcp child %d unix socket to the fd list\n", r);
1721                                 goto error;
1722                         }
1723         }
1724         
1725         /* main loop */
1726         switch(io_h.poll_method){
1727                 case POLL_POLL:
1728                         while(1){
1729                                 /* wait and process IO */
1730                                 io_wait_loop_poll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0); 
1731                                 send_fd_queue_run(&send2child_q); /* then new io */
1732                                 /* remove old connections */
1733                                 tcpconn_timeout(0);
1734                         }
1735                         break;
1736 #ifdef HAVE_SELECT
1737                 case POLL_SELECT:
1738                         while(1){
1739                                 io_wait_loop_select(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
1740                                 send_fd_queue_run(&send2child_q); /* then new io */
1741                                 tcpconn_timeout(0);
1742                         }
1743                         break;
1744 #endif
1745 #ifdef HAVE_SIGIO_RT
1746                 case POLL_SIGIO_RT:
1747                         while(1){
1748                                 io_wait_loop_sigio_rt(&io_h, TCP_MAIN_SELECT_TIMEOUT);
1749                                 send_fd_queue_run(&send2child_q); /* then new io */
1750                                 tcpconn_timeout(0);
1751                         }
1752                         break;
1753 #endif
1754 #ifdef HAVE_EPOLL
1755                 case POLL_EPOLL_LT:
1756                         while(1){
1757                                 io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
1758                                 send_fd_queue_run(&send2child_q); /* then new io */
1759                                 tcpconn_timeout(0);
1760                         }
1761                         break;
1762                 case POLL_EPOLL_ET:
1763                         while(1){
1764                                 io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 1);
1765                                 send_fd_queue_run(&send2child_q); /* then new io */
1766                                 tcpconn_timeout(0);
1767                         }
1768                         break;
1769 #endif
1770 #ifdef HAVE_KQUEUE
1771                 case POLL_KQUEUE:
1772                         while(1){
1773                                 io_wait_loop_kqueue(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
1774                                 send_fd_queue_run(&send2child_q); /* then new io */
1775                                 tcpconn_timeout(0);
1776                         }
1777                         break;
1778 #endif
1779 #ifdef HAVE_DEVPOLL
1780                 case POLL_DEVPOLL:
1781                         while(1){
1782                                 io_wait_loop_devpoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
1783                                 send_fd_queue_run(&send2child_q); /* then new io */
1784                                 tcpconn_timeout(0);
1785                         }
1786                         break;
1787 #endif
1788                 default:
1789                         LOG(L_CRIT, "BUG: tcp_main_loop: no support for poll method "
1790                                         " %s (%d)\n", 
1791                                         poll_method_name(io_h.poll_method), io_h.poll_method);
1792                         goto error;
1793         }
1794 error:
1795 #ifdef SEND_FD_QUEUE
1796         destroy_send_fd_queues();
1797 #endif
1798         destroy_io_wait(&io_h);
1799         LOG(L_CRIT, "ERROR: tcp_main_loop: exiting...");
1800         exit(-1);
1801 }
1802
1803
1804
1805 /* cleanup before exit */
1806 void destroy_tcp()
1807 {
1808                 if (tcpconn_id_hash){
1809                         tcpconn_timeout(1); /* force close/expire for all active tcpconns*/
1810                         shm_free(tcpconn_id_hash);
1811                         tcpconn_id_hash=0;
1812                 }
1813                 if (tcp_connections_no){
1814                         shm_free(tcp_connections_no);
1815                         tcp_connections_no=0;
1816                 }
1817                 if (connection_id){
1818                         shm_free(connection_id);
1819                         connection_id=0;
1820                 }
1821                 if (tcpconn_aliases_hash){
1822                         shm_free(tcpconn_aliases_hash);
1823                         tcpconn_aliases_hash=0;
1824                 }
1825                 if (tcpconn_lock){
1826                         lock_destroy(tcpconn_lock);
1827                         lock_dealloc((void*)tcpconn_lock);
1828                         tcpconn_lock=0;
1829                 }
1830                 if (tcp_children){
1831                         pkg_free(tcp_children);
1832                         tcp_children=0;
1833                 }
1834 }
1835
1836
1837
1838 int init_tcp()
1839 {
1840         char* poll_err;
1841         
1842         /* init lock */
1843         tcpconn_lock=lock_alloc();
1844         if (tcpconn_lock==0){
1845                 LOG(L_CRIT, "ERROR: init_tcp: could not alloc lock\n");
1846                 goto error;
1847         }
1848         if (lock_init(tcpconn_lock)==0){
1849                 LOG(L_CRIT, "ERROR: init_tcp: could not init lock\n");
1850                 lock_dealloc((void*)tcpconn_lock);
1851                 tcpconn_lock=0;
1852                 goto error;
1853         }
1854         /* init globals */
1855         tcp_connections_no=shm_malloc(sizeof(int));
1856         if (tcp_connections_no==0){
1857                 LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
1858                 goto error;
1859         }
1860         *tcp_connections_no=0;
1861         connection_id=shm_malloc(sizeof(int));
1862         if (connection_id==0){
1863                 LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
1864                 goto error;
1865         }
1866         *connection_id=1;
1867         /* alloc hashtables*/
1868         tcpconn_aliases_hash=(struct tcp_conn_alias**)
1869                         shm_malloc(TCP_ALIAS_HASH_SIZE* sizeof(struct tcp_conn_alias*));
1870         if (tcpconn_aliases_hash==0){
1871                 LOG(L_CRIT, "ERROR: init_tcp: could not alloc address hashtable\n");
1872                 goto error;
1873         }
1874         tcpconn_id_hash=(struct tcp_connection**)shm_malloc(TCP_ID_HASH_SIZE*
1875                                                                 sizeof(struct tcp_connection*));
1876         if (tcpconn_id_hash==0){
1877                 LOG(L_CRIT, "ERROR: init_tcp: could not alloc id hashtable\n");
1878                 goto error;
1879         }
1880         /* init hashtables*/
1881         memset((void*)tcpconn_aliases_hash, 0, 
1882                         TCP_ALIAS_HASH_SIZE * sizeof(struct tcp_conn_alias*));
1883         memset((void*)tcpconn_id_hash, 0, 
1884                         TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*));
1885         
1886         /* fix config variables */
1887         /* they can have only positive values due the config parser so we can
1888          * ignore most of them */
1889                 poll_err=check_poll_method(tcp_poll_method);
1890         
1891         /* set an appropiate poll method */
1892         if (poll_err || (tcp_poll_method==0)){
1893                 tcp_poll_method=choose_poll_method();
1894                 if (poll_err){
1895                         LOG(L_ERR, "ERROR: init_tcp: %s, using %s instead\n",
1896                                         poll_err, poll_method_name(tcp_poll_method));
1897                 }else{
1898                         LOG(L_INFO, "init_tcp: using %s as the io watch method"
1899                                         " (auto detected)\n", poll_method_name(tcp_poll_method));
1900                 }
1901         }else{
1902                         LOG(L_INFO, "init_tcp: using %s io watch method (config)\n",
1903                                         poll_method_name(tcp_poll_method));
1904         }
1905         
1906         return 0;
1907 error:
1908         /* clean-up */
1909         destroy_tcp();
1910         return -1;
1911 }
1912
1913
1914 #ifdef TCP_CHILD_NON_BLOCKING
1915 /* returns -1 on error */
1916 static int set_non_blocking(int s)
1917 {
1918         int flags;
1919         /* non-blocking */
1920         flags=fcntl(s, F_GETFL);
1921         if (flags==-1){
1922                 LOG(L_ERR, "ERROR: set_non_blocking: fnctl failed: (%d) %s\n",
1923                                 errno, strerror(errno));
1924                 goto error;
1925         }
1926         if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
1927                 LOG(L_ERR, "ERROR: set_non_blocking: fcntl: set non-blocking failed:"
1928                                 " (%d) %s\n", errno, strerror(errno));
1929                 goto error;
1930         }
1931         return 0;
1932 error:
1933         return -1;
1934 }
1935
1936 #endif
1937
1938
1939 /*  returns -1 on error, 0 on success */
1940 int tcp_fix_child_sockets(int* fd)
1941 {
1942 #ifdef TCP_CHILD_NON_BLOCKING
1943         if ((set_non_blocking(fd[0])<0) ||
1944                 (set_non_blocking(fd[1])<0)){
1945                 return -1;
1946         }
1947 #endif
1948         return 0;
1949 }
1950
1951
1952
1953 /* starts the tcp processes */
1954 int tcp_init_children()
1955 {
1956         int r;
1957         int reader_fd_1; /* for comm. with the tcp children read  */
1958         pid_t pid;
1959         struct socket_info *si;
1960         
1961         /* estimate max fd. no:
1962          * 1 tcp send unix socket/all_proc, 
1963          *  + 1 udp sock/udp proc + 1 tcp_child sock/tcp child*
1964          *  + no_listen_tcp */
1965         for(r=0, si=tcp_listen; si; si=si->next, r++);
1966 #ifdef USE_TLS
1967         if (! tls_disable)
1968                 for (si=tls_listen; si; si=si->next, r++);
1969 #endif
1970         
1971         tcp_max_fd_no=get_max_procs()*2 +r-1 /* timer */ +3; /* stdin/out/err*/
1972         /* max connections can be temporarily exceeded with estimated_process_count
1973          * - tcp_main (tcpconn_connect called simultaneously in all all the 
1974          *  processes) */
1975         tcp_max_fd_no+=tcp_max_connections+get_max_procs()-1 /* tcp main */;
1976         
1977         /* alloc the children array */
1978         tcp_children=pkg_malloc(sizeof(struct tcp_child)*tcp_children_no);
1979         if (tcp_children==0){
1980                         LOG(L_ERR, "ERROR: tcp_init_children: out of memory\n");
1981                         goto error;
1982         }
1983         /* create the tcp sock_info structures */
1984         /* copy the sockets --moved to main_loop*/
1985         
1986         /* fork children & create the socket pairs*/
1987         for(r=0; r<tcp_children_no; r++){
1988                 child_rank++;
1989                 pid=fork_tcp_process(child_rank,"tcp receiver",1,&reader_fd_1);
1990                 if (pid<0){
1991                         LOG(L_ERR, "ERROR: tcp_main: fork failed: %s\n",
1992                                         strerror(errno));
1993                         goto error;
1994                 }else if (pid>0){
1995                         /* parent */
1996                 }else{
1997                         /* child */
1998                         bind_address=0; /* force a SEGFAULT if someone uses a non-init.
1999                                                            bind address on tcp */
2000                         tcp_receive_loop(reader_fd_1);
2001                 }
2002         }
2003         return 0;
2004 error:
2005         return -1;
2006 }
2007
2008
2009
2010 void tcp_get_info(struct tcp_gen_info *ti)
2011 {
2012         ti->tcp_readers=tcp_children_no;
2013         ti->tcp_max_connections=tcp_max_connections;
2014         ti->tcp_connections_no=*tcp_connections_no;
2015 }
2016
2017 #endif