Selaa lähdekoodia

tcp: use dynamic config framework, part 2

- added tcp_connect_timeout, tcp_send_timeout,
  tcp_connection_lifetime, tcp_max_connections, tcp_accept_aliases
  to the config framework.

- added 2 new tcp related variables: tcp alias_flags and tcp
  new_conn_alias_flags
Andrei Pelinescu-Onciul 16 vuotta sitten
vanhempi
commit
3dc4f620d8
9 muutettua tiedostoa jossa 154 lisäystä ja 88 poistoa
  1. 4 4
      cfg.y
  2. 1 5
      globals.h
  3. 3 2
      receive.c
  4. 1 6
      tcp_conn.h
  5. 1 1
      tcp_info.h
  6. 10 0
      tcp_init.h
  7. 54 63
      tcp_main.c
  8. 69 5
      tcp_options.c
  9. 11 2
      tcp_options.h

+ 4 - 4
cfg.y

@@ -764,7 +764,7 @@ assign_stm:
 	| DISABLE_TCP EQUAL error { yyerror("boolean value expected"); }
 	| TCP_ACCEPT_ALIASES EQUAL NUMBER {
 		#ifdef USE_TCP
-			tcp_accept_aliases=$3;
+			tcp_default_cfg.accept_aliases=$3;
 		#else
 			warn("tcp support not compiled in");
 		#endif
@@ -780,7 +780,7 @@ assign_stm:
 	| TCP_CHILDREN EQUAL error { yyerror("number expected"); }
 	| TCP_CONNECT_TIMEOUT EQUAL NUMBER {
 		#ifdef USE_TCP
-			tcp_connect_timeout=$3;
+			tcp_default_cfg.connect_timeout_s=$3;
 		#else
 			warn("tcp support not compiled in");
 		#endif
@@ -788,7 +788,7 @@ assign_stm:
 	| TCP_CONNECT_TIMEOUT EQUAL error { yyerror("number expected"); }
 	| TCP_SEND_TIMEOUT EQUAL NUMBER {
 		#ifdef USE_TCP
-			tcp_send_timeout=$3;
+			tcp_default_cfg.send_timeout_s=$3;
 		#else
 			warn("tcp support not compiled in");
 		#endif
@@ -796,7 +796,7 @@ assign_stm:
 	| TCP_SEND_TIMEOUT EQUAL error { yyerror("number expected"); }
 	| TCP_CON_LIFETIME EQUAL NUMBER {
 		#ifdef USE_TCP
-			tcp_con_lifetime=$3;
+			tcp_default_cfg.con_lifetime_s=$3;
 		#else
 			warn("tcp support not compiled in");
 		#endif

+ 1 - 5
globals.h

@@ -87,12 +87,8 @@ extern int children_no;
 extern int tcp_main_pid;
 extern int tcp_children_no;
 extern int tcp_disable;
-extern int tcp_accept_aliases;
-extern int tcp_connect_timeout;
-extern int tcp_send_timeout;
-extern int tcp_con_lifetime; /* connection lifetime */
 extern enum poll_types tcp_poll_method;
-extern int tcp_max_connections;
+extern int tcp_max_connections; /* maximum connections, hard limit */
 #endif
 #ifdef USE_TLS
 extern int tls_disable;

+ 3 - 2
receive.c

@@ -62,7 +62,8 @@
 #include "select_buf.h"
 
 #include "tcp_server.h" /* for tcpconn_add_alias */
-
+#include "tcp_options.h" /* for access to tcp_accept_aliases*/
+#include "cfg/cfg.h"
 
 #ifdef DEBUG_DMALLOC
 #include <mem/dmalloc.h>
@@ -138,7 +139,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
 		/* check if necessary to add receive?->moved to forward_req */
 		/* check for the alias stuff */
 #ifdef USE_TCP
-		if (msg->via1->alias && tcp_accept_aliases && 
+		if (msg->via1->alias && cfg_get(tcp, tcp_cfg, accept_aliases) && 
 				(((rcv_info->proto==PROTO_TCP) && !tcp_disable)
 #ifdef USE_TLS
 					|| ((rcv_info->proto==PROTO_TLS) && !tls_disable)

+ 1 - 6
tcp_conn.h

@@ -42,6 +42,7 @@
 #ifndef _tcp_conn_h
 #define _tcp_conn_h
 
+#include "tcp_init.h"
 #include "tcp_options.h"
 
 #include "ip_addr.h"
@@ -54,12 +55,6 @@
 #define TCP_CON_MAX_ALIASES (4*3) 
 
 #define TCP_BUF_SIZE	4096 
-#define DEFAULT_TCP_CONNECTION_LIFETIME 120 /* in  seconds */
-#define DEFAULT_TCP_SEND_TIMEOUT 10 /* if a send can't write for more then 10s,
-									   timeout */
-#define DEFAULT_TCP_CONNECT_TIMEOUT 10 /* if a connect doesn't complete in this
-										  time, timeout */
-#define DEFAULT_TCP_MAX_CONNECTIONS 2048 /* maximum connections */
 #define TCP_CHILD_TIMEOUT 5 /* after 5 seconds, the child "returns" 
 							 the connection to the tcp master process */
 #define TCP_MAIN_SELECT_TIMEOUT 5 /* how often "tcp main" checks for timeout*/

+ 1 - 1
tcp_info.h

@@ -33,7 +33,7 @@
 
 struct tcp_gen_info{
 	int tcp_readers;
-	int tcp_max_connections;
+	int tcp_max_connections; /* startup connection limit, cannot be exceeded*/
 	int tcp_connections_no; /* crt. number */
 	int tcp_write_queued; /* total bytes queued for write, 0 if no
 							 write queued support is enabled */

+ 10 - 0
tcp_init.h

@@ -29,6 +29,16 @@
 #define tcp_init_h
 #include "ip_addr.h"
 
+#define DEFAULT_TCP_CONNECTION_LIFETIME_S 120 /* in  seconds */
+/* maximum accepted lifetime in ticks (maximum possible is  ~ MAXINT/2) */
+#define MAX_TCP_CON_LIFETIME	((1U<<(sizeof(ticks_t)*8-1))-1)
+
+#define DEFAULT_TCP_SEND_TIMEOUT 10 /* if a send can't write for more then 10s,
+									   timeout */
+#define DEFAULT_TCP_CONNECT_TIMEOUT 10 /* if a connect doesn't complete in this
+										  time, timeout */
+#define DEFAULT_TCP_MAX_CONNECTIONS 2048 /* maximum connections */
+
 struct tcp_child{
 	pid_t pid;
 	int proc_no; /* ser proc_no, for debugging */

+ 54 - 63
tcp_main.c

@@ -203,8 +203,6 @@
 #define SEND_FD_QUEUE_TIMEOUT	MS_TO_TICKS(2000)  /* 2 s */
 #endif
 
-/* maximum accepted lifetime (maximum possible is  ~ MAXINT/2) */
-#define MAX_TCP_CON_LIFETIME	((1U<<(sizeof(ticks_t)*8-1))-1)
 /* minimum interval local_timer_run() is allowed to run, in ticks */
 #define TCPCONN_TIMEOUT_MIN_RUN 1  /* once per tick */
 #define TCPCONN_WAIT_TIMEOUT 1 /* 1 tick */
@@ -235,17 +233,10 @@ static struct fd_cache_entry fd_cache[TCP_FD_CACHE_SIZE];
 
 static int is_tcp_main=0;
 
-int tcp_accept_aliases=0; /* by default don't accept aliases */
-/* flags used for adding new aliases */
-int tcp_alias_flags=TCP_ALIAS_FORCE_ADD;
-/* flags used for adding the default aliases of a new tcp connection */
-int tcp_new_conn_alias_flags=TCP_ALIAS_REPLACE;
-int tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
-int tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
-int tcp_con_lifetime=DEFAULT_TCP_CONNECTION_LIFETIME;
+
 enum poll_types tcp_poll_method=0; /* by default choose the best method */
-int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
 int tcp_main_max_fd_no=0;
+int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
 
 static union sockaddr_union tcp_source_ipv4_addr; /* saved bind/srv v4 addr. */
 static union sockaddr_union* tcp_source_ipv4=0;
@@ -500,7 +491,7 @@ static int tcp_blocking_connect(int fd, int type,
 	int poll_err;
 	
 	poll_err=0;
-	to=tcp_connect_timeout;
+	to=cfg_get(tcp, tcp_cfg, connect_timeout_s);
 	ticks=get_ticks();
 again:
 	n=connect(fd, servaddr, addrlen);
@@ -594,7 +585,7 @@ error_timeout:
 #endif /* USE_DST_BLACKLIST */
 	LOG(L_ERR, "ERROR: tcp_blocking_connect %s: timeout %d s elapsed "
 				"from %d s\n", su2a((union sockaddr_union*)servaddr, addrlen),
-				elapsed, tcp_connect_timeout);
+				elapsed, cfg_get(tcp, tcp_cfg, connect_timeout_s));
 error:
 	return -1;
 end:
@@ -976,7 +967,7 @@ struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
 	{
 		c->type=PROTO_TCP;
 		c->rcv.proto=PROTO_TCP;
-		c->timeout=get_ticks_raw()+tcp_con_lifetime;
+		c->timeout=get_ticks_raw()+cfg_get(tcp, tcp_cfg, con_lifetime);
 	}
 	
 	return c;
@@ -1118,10 +1109,11 @@ struct tcp_connection* tcpconn_connect( union sockaddr_union* server,
 
 	s=-1;
 	
-	if (*tcp_connections_no >= tcp_max_connections){
+	if (*tcp_connections_no >= cfg_get(tcp, tcp_cfg, max_connections)){
 		LOG(L_ERR, "ERROR: tcpconn_connect: maximum number of connections"
 					" exceeded (%d/%d)\n",
-					*tcp_connections_no, tcp_max_connections);
+					*tcp_connections_no,
+					cfg_get(tcp, tcp_cfg, max_connections));
 		goto error;
 	}
 	s=tcp_do_connect(server, from, type, &my_name, &si, &state);
@@ -1155,6 +1147,7 @@ int tcpconn_finish_connect( struct tcp_connection* c,
 	struct socket_info* si;
 	enum tcp_conn_states state;
 	struct tcp_conn_alias* a;
+	int new_conn_alias_flags;
 	
 	s=tcp_do_connect(&c->rcv.src_su, from, c->type, &local_addr, &si, &state);
 	if (unlikely(s==-1)){
@@ -1168,14 +1161,16 @@ int tcpconn_finish_connect( struct tcp_connection* c,
 	c->rcv.dst_port=su_getport(&local_addr);
 	/* update aliases if needed */
 	if (likely(from==0)){
+		new_conn_alias_flags=cfg_get(tcp, tcp_cfg, new_conn_alias_flags);
 		/* add aliases */
 		TCPCONN_LOCK;
 		_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip, 0,
-													tcp_new_conn_alias_flags);
+													new_conn_alias_flags);
 		_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip,
-									c->rcv.dst_port, tcp_new_conn_alias_flags);
+									c->rcv.dst_port, new_conn_alias_flags);
 		TCPCONN_UNLOCK;
 	}else if (su_cmp(from, &local_addr)!=1){
+		new_conn_alias_flags=cfg_get(tcp, tcp_cfg, new_conn_alias_flags);
 		TCPCONN_LOCK;
 			/* remove all the aliases except the first one and re-add them
 			 * (there shouldn't be more then the 3 default aliases at this 
@@ -1187,9 +1182,9 @@ int tcpconn_finish_connect( struct tcp_connection* c,
 			c->aliases=1;
 			/* add the local_ip:0 and local_ip:local_port aliases */
 			_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip,
-												0, tcp_new_conn_alias_flags);
+												0, new_conn_alias_flags);
 			_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip,
-									c->rcv.dst_port, tcp_new_conn_alias_flags);
+									c->rcv.dst_port, new_conn_alias_flags);
 		TCPCONN_UNLOCK;
 	}
 	
@@ -1204,11 +1199,13 @@ int tcpconn_finish_connect( struct tcp_connection* c,
 inline static struct tcp_connection*  tcpconn_add(struct tcp_connection *c)
 {
 	struct ip_addr zero_ip;
+	int new_conn_alias_flags;
 
 	if (likely(c)){
 		ip_addr_mk_any(c->rcv.src_ip.af, &zero_ip);
 		c->id_hash=tcp_id_hash(c->id);
 		c->aliases=0;
+		new_conn_alias_flags=cfg_get(tcp, tcp_cfg, new_conn_alias_flags);
 		TCPCONN_LOCK;
 		c->flags|=F_CONN_HASHED;
 		/* add it at the begining of the list*/
@@ -1221,12 +1218,12 @@ inline static struct tcp_connection*  tcpconn_add(struct tcp_connection *c)
 		 * the third alias is for (peer_ip, peer_port, local_addr, local_port) 
 		 *   -- for finding if a fully specified connection exists */
 		_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &zero_ip, 0,
-													tcp_new_conn_alias_flags);
+													new_conn_alias_flags);
 		if (likely(c->rcv.dst_ip.af && ! ip_addr_any(&c->rcv.dst_ip))){
 			_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip, 0,
-													tcp_new_conn_alias_flags);
+													new_conn_alias_flags);
 			_tcpconn_add_alias_unsafe(c, c->rcv.src_port, &c->rcv.dst_ip,
-									c->rcv.dst_port, tcp_new_conn_alias_flags);
+									c->rcv.dst_port, new_conn_alias_flags);
 		}
 		/* ignore add_alias errors, there are some valid cases when one
 		 *  of the add_alias would fail (e.g. first add_alias for 2 connections
@@ -1501,6 +1498,7 @@ int tcpconn_add_alias(int id, int port, int proto)
 	int ret;
 	struct ip_addr zero_ip;
 	int r;
+	int alias_flags;
 	
 	/* fix the port */
 	port=port?port:((proto==PROTO_TLS)?SIPS_PORT:SIP_PORT);
@@ -1509,18 +1507,18 @@ int tcpconn_add_alias(int id, int port, int proto)
 	c=_tcpconn_find(id, 0, 0, 0, 0);
 	if (likely(c)){
 		ip_addr_mk_any(c->rcv.src_ip.af, &zero_ip);
-		
+		alias_flags=cfg_get(tcp, tcp_cfg, alias_flags);
 		/* alias src_ip:port, 0, 0 */
 		ret=_tcpconn_add_alias_unsafe(c, port,  &zero_ip, 0, 
-										tcp_alias_flags);
+										alias_flags);
 		if (ret<0 && ret!=-3) goto error;
 		/* alias src_ip:port, local_ip, 0 */
 		ret=_tcpconn_add_alias_unsafe(c, port,  &c->rcv.dst_ip, 0, 
-										tcp_alias_flags);
+										alias_flags);
 		if (ret<0 && ret!=-3) goto error;
 		/* alias src_ip:port, local_ip, local_port */
 		ret=_tcpconn_add_alias_unsafe(c, port, &c->rcv.dst_ip, c->rcv.dst_port,
-										tcp_alias_flags);
+										alias_flags);
 		if (unlikely(ret<0)) goto error;
 	}else goto error_not_found;
 	TCPCONN_UNLOCK;
@@ -1622,6 +1620,7 @@ int tcp_send(struct dest_info* dst, union sockaddr_union* from,
 	long response[2];
 	int n;
 	int do_close_fd;
+	ticks_t con_lifetime;
 #ifdef TCP_BUF_WRITE
 	int enable_write_watch;
 #endif /* TCP_BUF_WRITE */
@@ -1634,11 +1633,12 @@ int tcp_send(struct dest_info* dst, union sockaddr_union* from,
 #endif /* TCP_FD_CACHE */
 	do_close_fd=1; /* close the fd on exit */
 	port=su_getport(&dst->to);
+	con_lifetime=cfg_get(tcp, tcp_cfg, con_lifetime);
 	if (likely(port)){
 		su2ip_addr(&ip, &dst->to);
-		c=tcpconn_get(dst->id, &ip, port, from, tcp_con_lifetime); 
+		c=tcpconn_get(dst->id, &ip, port, from, con_lifetime); 
 	}else if (likely(dst->id)){
-		c=tcpconn_get(dst->id, 0, 0, 0, tcp_con_lifetime);
+		c=tcpconn_get(dst->id, 0, 0, 0, con_lifetime);
 	}else{
 		LOG(L_CRIT, "BUG: tcp_send called with null id & to\n");
 		return -1;
@@ -1648,7 +1648,7 @@ int tcp_send(struct dest_info* dst, union sockaddr_union* from,
 		if (unlikely(c==0)) {
 			if (likely(port)){
 				/* try again w/o id */
-				c=tcpconn_get(0, &ip, port, from, tcp_con_lifetime);
+				c=tcpconn_get(0, &ip, port, from, con_lifetime);
 				goto no_id;
 			}else{
 				LOG(L_ERR, "ERROR: tcp_send: id %d not found, dropping\n",
@@ -1680,11 +1680,13 @@ no_id:
 #if defined(TCP_CONNECT_WAIT) && defined(TCP_BUF_WRITE)
 			if (likely(cfg_get(tcp, tcp_cfg, tcp_connect_wait) && 
 						cfg_get(tcp, tcp_cfg, tcp_buf_write) )){
-				if (unlikely(*tcp_connections_no >= tcp_max_connections)){
+				if (unlikely(*tcp_connections_no >=
+								cfg_get(tcp, tcp_cfg, max_connections))){
 					LOG(L_ERR, "ERROR: tcp_send %s: maximum number of"
 								" connections exceeded (%d/%d)\n",
 								su2a(&dst->to, sizeof(dst->to)),
-								*tcp_connections_no, tcp_max_connections);
+								*tcp_connections_no,
+								cfg_get(tcp, tcp_cfg, max_connections));
 					return -1;
 				}
 				c=tcpconn_new(-1, &dst->to, from, 0, dst->proto,
@@ -1932,7 +1934,8 @@ send_it:
 	else
 #endif
 		/* n=tcp_blocking_write(c, fd, buf, len); */
-		n=tsend_stream(fd, buf, len, tcp_send_timeout*1000); 
+		n=tsend_stream(fd, buf, len,
+							cfg_get(tcp, tcp_cfg, send_timeout_s)*1000);
 #ifdef TCP_BUF_WRITE
 	}
 #else /* ! TCP_BUF_WRITE */
@@ -2574,6 +2577,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
 	int n;
 	ticks_t t;
 	ticks_t crt_timeout;
+	ticks_t con_lifetime;
 	
 	if (unlikely(tcp_c->unix_sock<=0)){
 		/* (we can't have a fd==0, 0 is never closed )*/
@@ -2650,8 +2654,9 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
 			}
 			/* update the timeout*/
 			t=get_ticks_raw();
-			tcpconn->timeout=t+tcp_con_lifetime;
-			crt_timeout=tcp_con_lifetime;
+			con_lifetime=cfg_get(tcp, tcp_cfg, con_lifetime);
+			tcpconn->timeout=t+con_lifetime;
+			crt_timeout=con_lifetime;
 #ifdef TCP_BUF_WRITE
 			if (unlikely(cfg_get(tcp, tcp_cfg, tcp_buf_write) && 
 							_wbufq_non_empty(tcpconn) )){
@@ -2676,7 +2681,7 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
 						tcpconn_put_destroy(tcpconn);
 					break;
 				}else{
-					crt_timeout=MIN_unsigned(tcp_con_lifetime,
+					crt_timeout=MIN_unsigned(con_lifetime,
 											tcpconn->wbuf_q.wr_timeout-t);
 				}
 			}
@@ -2766,6 +2771,7 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
 	int fd;
 	int flags;
 	ticks_t t;
+	ticks_t con_lifetime;
 #ifdef TCP_BUF_WRITE
 	ticks_t nxt_timeout;
 #endif /* TCP_BUF_WRITE */
@@ -2874,11 +2880,12 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
 			tcpconn_add(tcpconn);
 			/* update the timeout*/
 			t=get_ticks_raw();
-			tcpconn->timeout=t+tcp_con_lifetime;
+			con_lifetime=cfg_get(tcp, tcp_cfg, con_lifetime);
+			tcpconn->timeout=t+con_lifetime;
 			/* activate the timer (already properly init. in tcpconn_new())
 			 * no need for reinit */
 			local_timer_add(&tcp_main_ltimer, &tcpconn->timer, 
-								tcp_con_lifetime, t);
+								con_lifetime, t);
 			tcpconn->flags|=(F_CONN_MAIN_TIMER|F_CONN_READ_W|F_CONN_WANTS_RD)
 #ifdef TCP_BUF_WRITE
 					/* not used for now, the connection is sent to tcp_main
@@ -2989,8 +2996,9 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
 			tcpconn->s=fd;
 			/* update the timeout*/
 			t=get_ticks_raw();
-			tcpconn->timeout=t+tcp_con_lifetime;
-			nxt_timeout=tcp_con_lifetime;
+			con_lifetime=cfg_get(tcp, tcp_cfg, con_lifetime);
+			tcpconn->timeout=t+con_lifetime;
+			nxt_timeout=con_lifetime;
 			if (unlikely(cmd==CONN_NEW_COMPLETE)){
 				tcpconn->state=S_CONN_OK;
 				/* check if needs to be watched for write */
@@ -3157,9 +3165,10 @@ static inline int handle_new_connect(struct socket_info* si)
 				" connection(%d): %s\n", errno, strerror(errno));
 		return -1;
 	}
-	if (unlikely(*tcp_connections_no>=tcp_max_connections)){
+	if (unlikely(*tcp_connections_no>=cfg_get(tcp, tcp_cfg, max_connections))){
 		LOG(L_ERR, "ERROR: maximum number of connections exceeded: %d/%d\n",
-					*tcp_connections_no, tcp_max_connections);
+					*tcp_connections_no,
+					cfg_get(tcp, tcp_cfg, max_connections));
 		close(new_sock);
 		return 1; /* success, because the accept was succesfull */
 	}
@@ -3192,7 +3201,8 @@ static inline int handle_new_connect(struct socket_info* si)
 		tcpconn_add(tcpconn);
 		/* activate the timer */
 		local_timer_add(&tcp_main_ltimer, &tcpconn->timer, 
-								tcp_con_lifetime, get_ticks_raw());
+								cfg_get(tcp, tcp_cfg, con_lifetime),
+								get_ticks_raw());
 		tcpconn->flags|=(F_CONN_MAIN_TIMER|F_CONN_READ_W|F_CONN_WANTS_RD);
 		if (unlikely(io_watch_add(&io_h, tcpconn->s, POLLIN, 
 													F_TCPCONN, tcpconn)<0)){
@@ -3843,26 +3853,7 @@ int init_tcp()
 			TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*));
 	
 	/* fix config variables */
-	if (tcp_connect_timeout<0)
-		tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
-	if (tcp_send_timeout<0)
-		tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
-	if (tcp_con_lifetime<0){
-		/* set to max value (~ 1/2 MAX_INT) */
-		tcp_con_lifetime=MAX_TCP_CON_LIFETIME;
-	}else{
-		if ((unsigned)tcp_con_lifetime > 
-				(unsigned)TICKS_TO_S(MAX_TCP_CON_LIFETIME)){
-			LOG(L_WARN, "init_tcp: tcp_con_lifetime too big (%u s), "
-					" the maximum value is %u\n", tcp_con_lifetime,
-					TICKS_TO_S(MAX_TCP_CON_LIFETIME));
-			tcp_con_lifetime=MAX_TCP_CON_LIFETIME;
-		}else{
-			tcp_con_lifetime=S_TO_TICKS(tcp_con_lifetime);
-		}
-	}
-	
-		poll_err=check_poll_method(tcp_poll_method);
+	poll_err=check_poll_method(tcp_poll_method);
 	
 	/* set an appropriate poll method */
 	if (poll_err || (tcp_poll_method==0)){

+ 69 - 5
tcp_options.c

@@ -29,6 +29,7 @@
 #include "globals.h"
 #include "timer_ticks.h"
 #include "cfg/cfg.h"
+#include "tcp_init.h" /* DEFAULT* */
 
 
 
@@ -68,7 +69,18 @@ struct cfg_group_tcp tcp_default_cfg;
 static cfg_def_t tcp_cfg_def[] = {
 	/*   name        , type |input type| chg type, min, max, fixup, proc. cbk 
 	      description */
-	{ "fd_cache",     CFG_VAR_INT | CFG_READONLY,    0,   1,     0,         0,
+	{ "connect_timeout", CFG_VAR_INT | CFG_READONLY, -1,
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME),         0,         0,
+		"used only in non-async mode, in seconds"},
+	{ "send_timeout", CFG_VAR_INT | CFG_READONLY,   -1,
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME),         0,         0,
+		"in seconds"},
+	{ "connection_lifetime", CFG_VAR_INT | CFG_READONLY,   -1,
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME),         0,         0,
+		"connection lifetime (in seconds)"},
+	{ "max_connections", CFG_VAR_INT | CFG_READONLY, 0,  (1U<<31)-1, 0,      0,
+		"maximum connection number, soft limit"},
+	{ "fd_cache",     CFG_VAR_INT | CFG_READONLY,    0,   1,      0,         0,
 		"file descriptor cache for tcp_send"},
 	/* tcp async options */
 	{ "async",        CFG_VAR_INT | CFG_READONLY,    0,   1,      0,         0,
@@ -79,8 +91,7 @@ static cfg_def_t tcp_cfg_def[] = {
 		"maximum bytes queued for write per connection (depends on async)"},
 	{ "wq_max",       CFG_VAR_INT | CFG_READONLY,    0,  1<<30,    0,        0,
 		"maximum bytes queued for write allowed globally (depends on async)"},
-	{ "wq_timeout",   CFG_VAR_INT | CFG_READONLY,    1,  1<<30,    0,        0,
-		"timeout for queued writes (in ticks, use send_timeout for seconds)"},
+	/* see also wq_timeout below */
 	/* tcp socket options */
 	{ "defer_accept", CFG_VAR_INT | CFG_READONLY,    0,   3600,   0,         0,
 		"0/1 on linux, seconds on freebsd (see docs)"},
@@ -101,6 +112,20 @@ static cfg_def_t tcp_cfg_def[] = {
 	/* other options */
 	{ "crlf_ping",   CFG_VAR_INT | CFG_READONLY,    0,        1,  0,         0,
 		"enable responding to CRLF SIP-level keepalives "},
+	{ "accept_aliases", CFG_VAR_INT | CFG_READONLY, 0,        1,  0,         0,
+		"turn on/off tcp aliases (see tcp_accept_aliases) "},
+	{ "alias_flags", CFG_VAR_INT | CFG_READONLY,    0,        0,  0,         0,
+		"flags used for adding new aliases (FORCE_ADD:1 , REPLACE:2) "},
+	{ "new_conn_alias_flags", CFG_VAR_INT | CFG_READONLY, 0,  0,  0,         0,
+		"flags for the def. aliases for a new conn. (FORCE_ADD:1, REPLACE:2 "},
+	/* internal and/or "fixed" versions of some vars
+	   (not supposed to be writeable, read will provide only debugging value*/
+	{ "wq_timeout_ticks",   CFG_VAR_INT | CFG_READONLY, 0,
+									MAX_TCP_CON_LIFETIME,         0,         0,
+		"internal send_timeout value in ticks, used in async. mode"},
+	{ "con_lifetime_ticks", CFG_VAR_INT | CFG_READONLY, 0,
+									MAX_TCP_CON_LIFETIME,         0,         0,
+		"internal connection_lifetime value, converted to ticks"},
 	{0, 0, 0, 0, 0, 0, 0}
 };
 
@@ -110,11 +135,15 @@ void* tcp_cfg; /* tcp config handle */
 /* set defaults */
 void init_tcp_options()
 {
+	tcp_default_cfg.connect_timeout_s=DEFAULT_TCP_CONNECT_TIMEOUT;
+	tcp_default_cfg.send_timeout_s=DEFAULT_TCP_SEND_TIMEOUT;
+	tcp_default_cfg.con_lifetime_s=DEFAULT_TCP_CONNECTION_LIFETIME_S;
+	tcp_default_cfg.max_connections=tcp_max_connections;
 #ifdef TCP_BUF_WRITE
 	tcp_default_cfg.tcp_buf_write=0;
 	tcp_default_cfg.tcpconn_wq_max=32*1024; /* 32 k */
 	tcp_default_cfg.tcp_wq_max=10*1024*1024; /* 10 MB */
-	tcp_default_cfg.tcp_wq_timeout=S_TO_TICKS(tcp_send_timeout);
+	tcp_default_cfg.tcp_wq_timeout=S_TO_TICKS(tcp_default_cfg.send_timeout_s);
 #ifdef TCP_CONNECT_WAIT
 	tcp_default_cfg.tcp_connect_wait=1;
 #endif /* TCP_CONNECT_WAIT */
@@ -134,6 +163,11 @@ void init_tcp_options()
 	tcp_default_cfg.delayed_ack=1;
 #endif
 	tcp_default_cfg.crlf_ping=1;
+	tcp_default_cfg.accept_aliases=0; /* don't accept aliases by default */
+	/* flags used for adding new aliases */
+	tcp_default_cfg.alias_flags=TCP_ALIAS_FORCE_ADD;
+	/* flags used for adding the default aliases of a new tcp connection */
+	tcp_default_cfg.new_conn_alias_flags=TCP_ALIAS_REPLACE;
 }
 
 
@@ -155,6 +189,20 @@ void init_tcp_options()
 	}
 
 
+
+/* if *to<0 to=default_val, else if to>max_val to=max_val */
+static void fix_timeout(char* name, int* to, int default_val, unsigned max_val)
+{
+	if (*to < 0) *to=default_val;
+	else if ((unsigned)*to > max_val){
+		WARN("%s: timeout too big (%u), the maximum value is %u\n",
+				name, *to, max_val);
+		*to=max_val;
+	}
+}
+
+
+
 /* checks & warns if some tcp_option cannot be enabled */
 void tcp_options_check()
 {
@@ -196,7 +244,7 @@ void tcp_options_check()
 #ifndef HAVE_TCP_KEEPCNT
 	W_OPT_NS(keepcnt);
 #endif
-	if (tcp_default_cfg.keepintvl || tcp_default_cfg.keepidle || 
+	if (tcp_default_cfg.keepintvl || tcp_default_cfg.keepidle ||
 			tcp_default_cfg.keepcnt){
 		tcp_default_cfg.keepalive=1; /* force on */
 	}
@@ -206,6 +254,22 @@ void tcp_options_check()
 #ifndef HAVE_TCP_QUICKACK
 	W_OPT_NS(delayed_ack);
 #endif
+	/* fix various timeouts */
+	fix_timeout("tcp_connect_timeout", &tcp_default_cfg.connect_timeout_s,
+						DEFAULT_TCP_CONNECT_TIMEOUT,
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME));
+	fix_timeout("tcp_send_timeout", &tcp_default_cfg.send_timeout_s,
+						DEFAULT_TCP_SEND_TIMEOUT,
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME));
+	fix_timeout("tcp_connection_lifetime", &tcp_default_cfg.con_lifetime_s,
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME),
+						TICKS_TO_S(MAX_TCP_CON_LIFETIME));
+	/* compute timeout in ticks */
+#ifdef TCP_BUF_WRITE
+	tcp_default_cfg.tcp_wq_timeout=S_TO_TICKS(tcp_default_cfg.send_timeout_s);
+#endif /* TCP_BUF_WRITE */
+	tcp_default_cfg.con_lifetime=S_TO_TICKS(tcp_default_cfg.con_lifetime_s);
+	tcp_default_cfg.max_connections=tcp_max_connections;
 }
 
 

+ 11 - 2
tcp_options.h

@@ -110,14 +110,17 @@
 #endif /* USE_TCP */
 
 struct cfg_group_tcp{
-	/* ser tcp options */
+	/* ser tcp options, low level */
+	int connect_timeout_s; /* in s, used only in non-async mode */
+	int send_timeout_s; /* in s */
+	int con_lifetime_s; /* in s */
+	int max_connections;
 	int fd_cache; /* on /off */
 	/* tcp async options */
 	int tcp_buf_write; /* on / off */
 	int tcp_connect_wait; /* on / off, depends on tcp_buf_write */
 	unsigned int tcpconn_wq_max; /* maximum queue len per connection */
 	unsigned int tcp_wq_max; /* maximum overall queued bytes */
-	unsigned int tcp_wq_timeout;      /* timeout for queued writes */
 
 	/* tcp socket options */
 	int defer_accept; /* on / off */
@@ -131,6 +134,12 @@ struct cfg_group_tcp{
 	
 	/* other options */
 	int crlf_ping;  /* on/off - reply to double CRLF keepalives */
+	int accept_aliases;
+	int alias_flags;
+	int new_conn_alias_flags;
+	/* internal, "fixed" vars */
+	unsigned int tcp_wq_timeout; /* in ticks, timeout for queued writes */
+	unsigned int con_lifetime; /* in ticks, see con_lifetime_s */
 };
 
 extern struct cfg_group_tcp tcp_default_cfg;