tcp_conn.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * [email protected]
  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. * 2003-01-29 tcp buffer size ++-ed to allow for 0-terminator
  31. * 2003-06-30 added tcp_connection flags & state (andrei)
  32. * 2003-10-27 tcp port aliases support added (andrei)
  33. * 2006-10-13 added tcp_req_states for STUN (vlada)
  34. * 2007-07-26 improved tcp connection hash function; increased aliases
  35. * hash size (andrei)
  36. * 2007-11-26 switched to local_timer (andrei)
  37. * 2007-11-30 buffered write support (andrei)
  38. */
  39. #ifndef _tcp_conn_h
  40. #define _tcp_conn_h
  41. #include "tcp_init.h"
  42. #include "tcp_options.h"
  43. #include "ip_addr.h"
  44. #include "locking.h"
  45. #include "atomic_ops.h"
  46. #include "timer_ticks.h"
  47. #include "timer.h"
  48. /* maximum number of port aliases x search wildcard possibilities */
  49. #define TCP_CON_MAX_ALIASES (4*3)
  50. #define TCP_CHILD_TIMEOUT 5 /* after 5 seconds, the child "returns"
  51. the connection to the tcp master process */
  52. #define TCP_MAIN_SELECT_TIMEOUT 5 /* how often "tcp main" checks for timeout*/
  53. #define TCP_CHILD_SELECT_TIMEOUT 2 /* the same as above but for children */
  54. /* tcp connection flags */
  55. #define F_CONN_NON_BLOCKING 1
  56. #define F_CONN_READ_W 2 /* watched for READ ev. in main */
  57. #define F_CONN_WRITE_W 4 /* watched for WRITE (main) */
  58. #define F_CONN_READER 8 /* handled by a tcp reader */
  59. #define F_CONN_HASHED 16 /* in tcp_main hash */
  60. #define F_CONN_FD_CLOSED 32 /* fd was already closed */
  61. #define F_CONN_PENDING 64 /* pending connect (fd not known yet in main) */
  62. #define F_CONN_MAIN_TIMER 128 /* timer active in the tcp_main process */
  63. #define F_CONN_EOF_SEEN 256 /* FIN or RST have been received */
  64. #define F_CONN_FORCE_EOF 512 /* act as if an EOF was received */
  65. #define F_CONN_OOB_DATA 1024 /* out of band data on the connection */
  66. #define F_CONN_WR_ERROR 2048 /* write error on the fd */
  67. #define F_CONN_WANTS_RD 4096 /* conn. should be watched for READ */
  68. #define F_CONN_WANTS_WR 8192 /* conn. should be watched for WRITE */
  69. enum tcp_req_errors { TCP_REQ_INIT, TCP_REQ_OK, TCP_READ_ERROR,
  70. TCP_REQ_OVERRUN, TCP_REQ_BAD_LEN };
  71. enum tcp_req_states { H_SKIP_EMPTY, H_SKIP_EMPTY_CR_FOUND, H_SKIP_EMPTY_CRLF_FOUND, H_SKIP_EMPTY_CRLFCR_FOUND,
  72. H_SKIP, H_LF, H_LFCR, H_BODY, H_STARTWS,
  73. H_CONT_LEN1, H_CONT_LEN2, H_CONT_LEN3, H_CONT_LEN4, H_CONT_LEN5,
  74. H_CONT_LEN6, H_CONT_LEN7, H_CONT_LEN8, H_CONT_LEN9, H_CONT_LEN10,
  75. H_CONT_LEN11, H_CONT_LEN12, H_CONT_LEN13, H_L_COLON,
  76. H_CONT_LEN_BODY, H_CONT_LEN_BODY_PARSE,
  77. H_STUN_MSG, H_STUN_READ_BODY, H_STUN_FP, H_STUN_END, H_PING_CRLF
  78. };
  79. enum tcp_conn_states { S_CONN_ERROR=-2, S_CONN_BAD=-1,
  80. S_CONN_OK=0, /* established (write or read) */
  81. S_CONN_INIT, /* initial state (invalid) */
  82. S_CONN_EOF,
  83. S_CONN_ACCEPT, S_CONN_CONNECT
  84. };
  85. /* fd communication commands */
  86. enum conn_cmds { CONN_DESTROY=-3, CONN_ERROR=-2, CONN_EOF=-1, CONN_RELEASE,
  87. CONN_GET_FD, CONN_NEW, CONN_QUEUED_WRITE,
  88. CONN_NEW_PENDING_WRITE, CONN_NEW_COMPLETE };
  89. /* CONN_RELEASE, EOF, ERROR, DESTROY can be used by "reader" processes
  90. * CONN_GET_FD, NEW, ERROR only by writers */
  91. struct tcp_req{
  92. struct tcp_req* next;
  93. /* sockaddr ? */
  94. char* buf; /* bytes read so far (+0-terminator)*/
  95. char* start; /* where the message starts, after all the empty lines are
  96. skipped*/
  97. char* pos; /* current position in buf */
  98. char* parsed; /* last parsed position */
  99. char* body; /* body position */
  100. unsigned int b_size; /* buffer size-1 (extra space for 0-term)*/
  101. int content_len;
  102. unsigned short flags; /* F_TCP_REQ_HAS_CLEN | F_TCP_REQ_COMPLETE */
  103. int bytes_to_go; /* how many bytes we have still to read from the body*/
  104. enum tcp_req_errors error;
  105. enum tcp_req_states state;
  106. };
  107. /* tcp_req flags */
  108. #define F_TCP_REQ_HAS_CLEN 1
  109. #define F_TCP_REQ_COMPLETE 2
  110. #define TCP_REQ_HAS_CLEN(tr) ((tr)->flags & F_TCP_REQ_HAS_CLEN)
  111. #define TCP_REQ_COMPLETE(tr) ((tr)->flags & F_TCP_REQ_COMPLETE)
  112. struct tcp_connection;
  113. /* tcp port alias structure */
  114. struct tcp_conn_alias{
  115. struct tcp_connection* parent;
  116. struct tcp_conn_alias* next;
  117. struct tcp_conn_alias* prev;
  118. unsigned short port; /* alias port */
  119. unsigned short hash; /* hash index in the address hash */
  120. };
  121. #ifdef TCP_ASYNC
  122. struct tcp_wbuffer{
  123. struct tcp_wbuffer* next;
  124. unsigned int b_size;
  125. char buf[1];
  126. };
  127. struct tcp_wbuffer_queue{
  128. struct tcp_wbuffer* first;
  129. struct tcp_wbuffer* last;
  130. ticks_t wr_timeout; /* write timeout*/
  131. unsigned int queued; /* total size */
  132. unsigned int offset; /* offset in the first wbuffer were data
  133. starts */
  134. unsigned int last_used; /* how much of the last buffer is used */
  135. };
  136. #endif
  137. struct tcp_connection{
  138. int s; /*socket, used by "tcp main" */
  139. int fd; /* used only by "children", don't modify it! private data! */
  140. gen_lock_t write_lock;
  141. int id; /* id (unique!) used to retrieve a specific connection when
  142. reply-ing*/
  143. int reader_pid; /* pid of the active reader process */
  144. struct receive_info rcv; /* src & dst ip, ports, proto a.s.o*/
  145. struct tcp_req req; /* request data */
  146. atomic_t refcnt;
  147. enum sip_protos type; /* PROTO_TCP or a protocol over it, e.g. TLS */
  148. int flags; /* connection related flags */
  149. enum tcp_conn_states state; /* connection state */
  150. void* extra_data; /* extra data associated to the connection, 0 for tcp*/
  151. struct timer_ln timer;
  152. ticks_t timeout;/* connection timeout, after this it will be removed*/
  153. unsigned id_hash; /* hash index in the id_hash */
  154. struct tcp_connection* id_next; /* next, prev in id hash table */
  155. struct tcp_connection* id_prev;
  156. struct tcp_connection* c_next; /* child next prev (use locally) */
  157. struct tcp_connection* c_prev;
  158. struct tcp_conn_alias con_aliases[TCP_CON_MAX_ALIASES];
  159. int aliases; /* aliases number, at least 1 */
  160. #ifdef TCP_ASYNC
  161. struct tcp_wbuffer_queue wbuf_q;
  162. #endif
  163. };
  164. /* helper macros */
  165. #define TCP_RCV_INFO(c) (&(c)->rcv)
  166. #define TCP_RCV_LADDR(r) (&((r).dst_ip))
  167. #define TCP_RCV_LPORT(r) ((r).dst_port)
  168. #define TCP_RCV_PADDR(r) (&((r).src_ip))
  169. #define TCP_RCV_PPORT(r) ((r).src_port)
  170. #define TCP_RCV_PSU(r) (&(r).src_su)
  171. #define TCP_RCV_SOCK_INFO(r) ((r).bind_address)
  172. #define TCP_RCV_PROTO(r) ((r).proto)
  173. #ifdef USE_COMP
  174. #define TCP_RCV_COMP(r) ((r).comp)
  175. #else
  176. #define TCP_RCV_COMP(r) 0
  177. #endif /* USE_COMP */
  178. #define TCP_LADDR(c) TCP_RCV_LADDR(c->rcv)
  179. #define TCP_LPORT(c) TCP_RCV_LPORT(c->rcv)
  180. #define TCP_PADDR(c) TCP_RCV_PADDR(c->rcv)
  181. #define TCP_PPORT(c) TCP_RCV_PPORT(c->rcv)
  182. #define TCP_PSU(c) TCP_RCV_PSU(c->rcv)
  183. #define TCP_SOCK_INFO(c) TCP_RCV_SOCK_INFO(c->rcv)
  184. #define TCP_PROTO(c) TCP_RCV_PROTO(c->rcv)
  185. #define TCP_COMP(c) TCP_RCV_COMP(c->rcv)
  186. #define tcpconn_ref(c) atomic_inc(&((c)->refcnt))
  187. #define tcpconn_put(c) atomic_dec_and_test(&((c)->refcnt))
  188. #define init_tcp_req( r, rd_buf, rd_buf_size) \
  189. do{ \
  190. memset( (r), 0, sizeof(struct tcp_req)); \
  191. (r)->buf=(rd_buf) ;\
  192. (r)->b_size=(rd_buf_size)-1; /* space for 0 term. */ \
  193. (r)->parsed=(r)->pos=(r)->start=(r)->buf; \
  194. (r)->error=TCP_REQ_OK;\
  195. (r)->state=H_SKIP_EMPTY; \
  196. }while(0)
  197. /* add a tcpconn to a list*/
  198. /* list head, new element, next member, prev member */
  199. #define tcpconn_listadd(head, c, next, prev) \
  200. do{ \
  201. /* add it at the begining of the list*/ \
  202. (c)->next=(head); \
  203. (c)->prev=0; \
  204. if ((head)) (head)->prev=(c); \
  205. (head)=(c); \
  206. } while(0)
  207. /* remove a tcpconn from a list*/
  208. #define tcpconn_listrm(head, c, next, prev) \
  209. do{ \
  210. if ((head)==(c)) (head)=(c)->next; \
  211. if ((c)->next) (c)->next->prev=(c)->prev; \
  212. if ((c)->prev) (c)->prev->next=(c)->next; \
  213. }while(0)
  214. #define TCPCONN_LOCK lock_get(tcpconn_lock);
  215. #define TCPCONN_UNLOCK lock_release(tcpconn_lock);
  216. #define TCP_ALIAS_HASH_SIZE 4096
  217. #define TCP_ID_HASH_SIZE 1024
  218. /* hash (dst_ip, dst_port, local_ip, local_port) */
  219. static inline unsigned tcp_addr_hash( struct ip_addr* ip,
  220. unsigned short port,
  221. struct ip_addr* l_ip,
  222. unsigned short l_port)
  223. {
  224. unsigned h;
  225. if(ip->len==4)
  226. h=(ip->u.addr32[0]^port)^(l_ip->u.addr32[0]^l_port);
  227. else if (ip->len==16)
  228. h= (ip->u.addr32[0]^ip->u.addr32[1]^ip->u.addr32[2]^
  229. ip->u.addr32[3]^port) ^
  230. (l_ip->u.addr32[0]^l_ip->u.addr32[1]^l_ip->u.addr32[2]^
  231. l_ip->u.addr32[3]^l_port);
  232. else{
  233. LOG(L_CRIT, "tcp_addr_hash: BUG: bad len %d for an ip address\n",
  234. ip->len);
  235. return 0;
  236. }
  237. /* make sure the first bits are influenced by all 32
  238. * (the first log2(TCP_ALIAS_HASH_SIZE) bits should be a mix of all
  239. * 32)*/
  240. h ^= h>>17;
  241. h ^= h>>7;
  242. return h & (TCP_ALIAS_HASH_SIZE-1);
  243. }
  244. #define tcp_id_hash(id) (id&(TCP_ID_HASH_SIZE-1))
  245. struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
  246. union sockaddr_union* local_addr,
  247. ticks_t timeout);
  248. #endif