tcp_conn.h
5b532c7f
 /*
  * $Id$
  *
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
5b532c7f
  *
  * This file is part of ser, a free SIP server.
  *
  * ser is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
049f64c2
  *
  *
f6e50f08
  * History:
049f64c2
  * --------
f6e50f08
  *  2003-01-29  tcp buffer size ++-ed to allow for 0-terminator
  *  2003-06-30  added tcp_connection flags & state (andrei) 
59653eb8
  *  2003-10-27  tcp port aliases support added (andrei)
96227c65
  *  2006-10-13  added tcp_req_states for STUN (vlada)
a288ee34
  *  2007-07-26  improved tcp connection hash function; increased aliases
  *               hash size (andrei)
ccb7fda2
  *  2007-11-26  switched to local_timer (andrei)
885b9f62
  *  2007-11-30  buffered write support (andrei)
5b532c7f
  */
 
 
 
 #ifndef _tcp_conn_h
 #define _tcp_conn_h
 
885b9f62
 #include "tcp_options.h"
 
6ee62314
 #include "ip_addr.h"
ab130758
 #include "locking.h"
be7401cc
 #include "atomic_ops.h"
ef9e3860
 #include "timer_ticks.h"
ccb7fda2
 #include "timer.h"
5b532c7f
 
a288ee34
 /* maximum number of port aliases x search wildcard possibilities */
 #define TCP_CON_MAX_ALIASES (4*3) 
59653eb8
 
28260509
 #define TCP_BUF_SIZE	4096 
55d8155e
 #define DEFAULT_TCP_CONNECTION_LIFETIME 120 /* in  seconds */
bc977837
 #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 */
55d8155e
 #define DEFAULT_TCP_MAX_CONNECTIONS 2048 /* maximum connections */
6bc40dea
 #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*/
 #define TCP_CHILD_SELECT_TIMEOUT 2 /* the same as above but for children */
 
 
f6e50f08
 /* tcp connection flags */
 #define F_CONN_NON_BLOCKING 1
fc665070
 #define F_CONN_READ_W       2 /* watched for READ ev. in main */
 #define F_CONN_WRITE_W      4 /* watched for WRITE (main) */
 #define F_CONN_READER       8 /* handled by a tcp reader */
19782e1c
 #define F_CONN_HASHED      16 /* in tcp_main hash */
d22b82a0
 #define F_CONN_FD_CLOSED   32 /* fd was already closed */
 #define F_CONN_PENDING     64 /* pending connect  (fd not known yet in main) */
 #define F_CONN_MAIN_TIMER 128 /* timer active in the tcp_main process */
7498b4dc
 #define F_CONN_EOF_SEEN   256 /* FIN or RST have been received */
 #define F_CONN_FORCE_EOF  512 /* act as if an EOF was received */
 #define F_CONN_OOB_DATA  1024 /* out of band data on the connection */
 #define F_CONN_WR_ERROR  2048 /* write error on the fd */
92c0024c
 #define F_CONN_WANTS_RD  4096  /* conn. should be watched for READ */
 #define F_CONN_WANTS_WR  8192  /* conn. should be watched for WRITE */
f6e50f08
 
 
f2f969dd
 enum tcp_req_errors {	TCP_REQ_INIT, TCP_REQ_OK, TCP_READ_ERROR,
 						TCP_REQ_OVERRUN, TCP_REQ_BAD_LEN };
c7337a27
 enum tcp_req_states {	H_SKIP_EMPTY, H_SKIP_EMPTY_CR_FOUND, H_SKIP_EMPTY_CRLF_FOUND, H_SKIP_EMPTY_CRLFCR_FOUND,
 			H_SKIP, H_LF, H_LFCR,  H_BODY, H_STARTWS,
6a157851
 		H_CONT_LEN1, H_CONT_LEN2, H_CONT_LEN3, H_CONT_LEN4, H_CONT_LEN5,
 		H_CONT_LEN6, H_CONT_LEN7, H_CONT_LEN8, H_CONT_LEN9, H_CONT_LEN10,
 		H_CONT_LEN11, H_CONT_LEN12, H_CONT_LEN13, H_L_COLON, 
96227c65
 		H_CONT_LEN_BODY, H_CONT_LEN_BODY_PARSE,
c7337a27
 		H_STUN_MSG, H_STUN_READ_BODY, H_STUN_FP, H_STUN_END, H_PING_CRLF
6a157851
 	};
5b532c7f
 
f6e50f08
 enum tcp_conn_states { S_CONN_ERROR=-2, S_CONN_BAD=-1, S_CONN_OK=0, 
28427aa4
 						S_CONN_INIT, S_CONN_EOF, 
d22b82a0
 						S_CONN_ACCEPT, S_CONN_CONNECT, S_CONN_PENDING };
f6e50f08
 
 
0c5da34b
 /* fd communication commands */
f2f969dd
 enum conn_cmds { CONN_DESTROY=-3, CONN_ERROR=-2, CONN_EOF=-1, CONN_RELEASE, 
d22b82a0
 					CONN_GET_FD, CONN_NEW, CONN_QUEUED_WRITE,
 					CONN_NEW_PENDING_WRITE, CONN_NEW_COMPLETE };
06aaa54f
 /* CONN_RELEASE, EOF, ERROR, DESTROY can be used by "reader" processes
  * CONN_GET_FD, NEW, ERROR only by writers */
0c5da34b
 
5b532c7f
 struct tcp_req{
 	struct tcp_req* next;
 	/* sockaddr ? */
049f64c2
 	char buf[TCP_BUF_SIZE+1]; /* bytes read so far (+0-terminator)*/
53c7e0f1
 	char* start; /* where the message starts, after all the empty lines are
c0c6207a
 					skipped*/
5b532c7f
 	char* pos; /* current position in buf */
 	char* parsed; /* last parsed position */
 	char* body; /* body position */
6a157851
 	int content_len;
 	int has_content_len; /* 1 if content_length was parsed ok*/
 	int complete; /* 1 if one req has been fully read, 0 otherwise*/
 	int bytes_to_go; /* how many bytes we have still to read from the body*/
f2f969dd
 	enum tcp_req_errors error;
 	enum tcp_req_states state;
5b532c7f
 };
 
 
6bc40dea
 
59653eb8
 struct tcp_connection;
 
 /* tcp port alias structure */
 struct tcp_conn_alias{
 	struct tcp_connection* parent;
 	struct tcp_conn_alias* next;
 	struct tcp_conn_alias* prev;
 	unsigned short port; /* alias port */
 	unsigned short hash; /* hash index in the address hash */
 };
 
 
885b9f62
 #ifdef TCP_BUF_WRITE
 	struct tcp_wbuffer{
 		struct tcp_wbuffer* next;
 		unsigned int b_size;
 		char buf[1];
 	};
 
 	struct tcp_wbuffer_queue{
 		struct tcp_wbuffer* first;
 		struct tcp_wbuffer* last;
20863813
 		ticks_t wr_timeout; /* write timeout*/
885b9f62
 		unsigned int queued; /* total size */
 		unsigned int offset; /* offset in the first wbuffer were data
 								starts */
 		unsigned int last_used; /* how much of the last buffer is used */
 	};
 #endif
 
6bc40dea
 
 struct tcp_connection{
 	int s; /*socket, used by "tcp main" */
e29d027b
 	int fd; /* used only by "children", don't modify it! private data! */
0bd53297
 	gen_lock_t write_lock;
0c5da34b
 	int id; /* id (unique!) used to retrieve a specific connection when
 	           reply-ing*/
5c5cd736
 	int reader_pid; /* pid of the active reader process */
f2f969dd
 	struct receive_info rcv; /* src & dst ip, ports, proto a.s.o*/
6bc40dea
 	struct tcp_req req; /* request data */
be7401cc
 	atomic_t refcnt;
1c93f767
 	enum sip_protos type; /* PROTO_TCP or a protocol over it, e.g. TLS */
f6e50f08
 	int flags; /* connection related flags */
1c93f767
 	enum tcp_conn_states state; /* connection state */
f6e50f08
 	void* extra_data; /* extra data associated to the connection, 0 for tcp*/
ccb7fda2
 	struct timer_ln timer;
885b9f62
 	ticks_t timeout;/* connection timeout, after this it will be removed*/
59653eb8
 	unsigned id_hash; /* hash index in the id_hash */
8aeb47e2
 	struct tcp_connection* id_next; /* next, prev in id hash table */
 	struct tcp_connection* id_prev;
6ee62314
 	struct tcp_connection* c_next; /* child next prev (use locally) */
 	struct tcp_connection* c_prev;
7564dcab
 	struct tcp_conn_alias con_aliases[TCP_CON_MAX_ALIASES];
 	int aliases; /* aliases number, at least 1 */
885b9f62
 #ifdef TCP_BUF_WRITE
 	struct tcp_wbuffer_queue wbuf_q;
 #endif
6bc40dea
 };
 
 
 
 
59653eb8
 
be7401cc
 #define tcpconn_ref(c) atomic_inc(&((c)->refcnt))
19782e1c
 #define tcpconn_put(c) atomic_dec_and_test(&((c)->refcnt))
be7401cc
 
59653eb8
 
6bc40dea
 #define init_tcp_req( r) \
5b532c7f
 	do{ \
 		memset( (r), 0, sizeof(struct tcp_req)); \
c0c6207a
 		(r)->parsed=(r)->pos=(r)->start=(r)->buf; \
5b532c7f
 		(r)->error=TCP_REQ_OK;\
c0c6207a
 		(r)->state=H_SKIP_EMPTY; \
5b532c7f
 	}while(0)
 
 
6bc40dea
 /* add a tcpconn to a list*/
6ee62314
 /* list head, new element, next member, prev member */
 #define tcpconn_listadd(head, c, next, prev) \
6bc40dea
 	do{ \
 		/* add it at the begining of the list*/ \
 		(c)->next=(head); \
 		(c)->prev=0; \
 		if ((head)) (head)->prev=(c); \
 		(head)=(c); \
 	} while(0)
 
5b532c7f
 
6bc40dea
 /* remove a tcpconn from a list*/
6ee62314
 #define tcpconn_listrm(head, c, next, prev) \
6bc40dea
 	do{ \
 		if ((head)==(c)) (head)=(c)->next; \
 		if ((c)->next) (c)->next->prev=(c)->prev; \
 		if ((c)->prev) (c)->prev->next=(c)->next; \
 	}while(0)
5b532c7f
 
 
8aeb47e2
 #define TCPCONN_LOCK lock_get(tcpconn_lock);
 #define TCPCONN_UNLOCK lock_release(tcpconn_lock);
5b532c7f
 
a288ee34
 #define TCP_ALIAS_HASH_SIZE 4096
8aeb47e2
 #define TCP_ID_HASH_SIZE 1024
 
a288ee34
 /* hash (dst_ip, dst_port, local_ip, local_port) */
 static inline unsigned tcp_addr_hash(	struct ip_addr* ip, 
 										unsigned short port,
 										struct ip_addr* l_ip,
 										unsigned short l_port)
8aeb47e2
 {
a288ee34
 	unsigned h;
 
 	if(ip->len==4)
 		h=(ip->u.addr32[0]^port)^(l_ip->u.addr32[0]^l_port);
8aeb47e2
 	else if (ip->len==16) 
a288ee34
 		h= (ip->u.addr32[0]^ip->u.addr32[1]^ip->u.addr32[2]^
 				ip->u.addr32[3]^port) ^
 			(l_ip->u.addr32[0]^l_ip->u.addr32[1]^l_ip->u.addr32[2]^
 				l_ip->u.addr32[3]^l_port);
8aeb47e2
 	else{
 		LOG(L_CRIT, "tcp_addr_hash: BUG: bad len %d for an ip address\n",
 				ip->len);
 		return 0;
 	}
a288ee34
 	/* make sure the first bits are influenced by all 32
 	 * (the first log2(TCP_ALIAS_HASH_SIZE) bits should be a mix of all
 	 *  32)*/
 	h ^= h>>17;
 	h ^= h>>7;
 	return h & (TCP_ALIAS_HASH_SIZE-1);
8aeb47e2
 }
 
 #define tcp_id_hash(id) (id&(TCP_ID_HASH_SIZE-1))
5b532c7f
 
ef9e3860
 struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
a288ee34
 									union sockaddr_union* local_addr,
ef9e3860
 									ticks_t timeout);
5b532c7f
 
 #endif