tcp_conn.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #define F_CONN_PASSIVE 16384 /* conn. created via accept() and not connect()*/
  70. enum tcp_req_errors { TCP_REQ_INIT, TCP_REQ_OK, TCP_READ_ERROR,
  71. TCP_REQ_OVERRUN, TCP_REQ_BAD_LEN };
  72. enum tcp_req_states { H_SKIP_EMPTY, H_SKIP_EMPTY_CR_FOUND, H_SKIP_EMPTY_CRLF_FOUND, H_SKIP_EMPTY_CRLFCR_FOUND,
  73. H_SKIP, H_LF, H_LFCR, H_BODY, H_STARTWS,
  74. H_CONT_LEN1, H_CONT_LEN2, H_CONT_LEN3, H_CONT_LEN4, H_CONT_LEN5,
  75. H_CONT_LEN6, H_CONT_LEN7, H_CONT_LEN8, H_CONT_LEN9, H_CONT_LEN10,
  76. H_CONT_LEN11, H_CONT_LEN12, H_CONT_LEN13, H_L_COLON,
  77. H_CONT_LEN_BODY, H_CONT_LEN_BODY_PARSE,
  78. H_STUN_MSG, H_STUN_READ_BODY, H_STUN_FP, H_STUN_END, H_PING_CRLF
  79. };
  80. enum tcp_conn_states { S_CONN_ERROR=-2, S_CONN_BAD=-1,
  81. S_CONN_OK=0, /* established (write or read) */
  82. S_CONN_INIT, /* initial state (invalid) */
  83. S_CONN_EOF,
  84. S_CONN_ACCEPT, S_CONN_CONNECT
  85. };
  86. /* fd communication commands */
  87. enum conn_cmds { CONN_DESTROY=-3, CONN_ERROR=-2, CONN_EOF=-1, CONN_RELEASE,
  88. CONN_GET_FD, CONN_NEW, CONN_QUEUED_WRITE,
  89. CONN_NEW_PENDING_WRITE, CONN_NEW_COMPLETE };
  90. /* CONN_RELEASE, EOF, ERROR, DESTROY can be used by "reader" processes
  91. * CONN_GET_FD, NEW, ERROR only by writers */
  92. struct tcp_req{
  93. struct tcp_req* next;
  94. /* sockaddr ? */
  95. char* buf; /* bytes read so far (+0-terminator)*/
  96. char* start; /* where the message starts, after all the empty lines are
  97. skipped*/
  98. char* pos; /* current position in buf */
  99. char* parsed; /* last parsed position */
  100. char* body; /* body position */
  101. unsigned int b_size; /* buffer size-1 (extra space for 0-term)*/
  102. int content_len;
  103. unsigned short flags; /* F_TCP_REQ_HAS_CLEN | F_TCP_REQ_COMPLETE */
  104. int bytes_to_go; /* how many bytes we have still to read from the body*/
  105. enum tcp_req_errors error;
  106. enum tcp_req_states state;
  107. };
  108. /* tcp_req flags */
  109. #define F_TCP_REQ_HAS_CLEN 1
  110. #define F_TCP_REQ_COMPLETE 2
  111. #define TCP_REQ_HAS_CLEN(tr) ((tr)->flags & F_TCP_REQ_HAS_CLEN)
  112. #define TCP_REQ_COMPLETE(tr) ((tr)->flags & F_TCP_REQ_COMPLETE)
  113. struct tcp_connection;
  114. /* tcp port alias structure */
  115. struct tcp_conn_alias{
  116. struct tcp_connection* parent;
  117. struct tcp_conn_alias* next;
  118. struct tcp_conn_alias* prev;
  119. unsigned short port; /* alias port */
  120. unsigned short hash; /* hash index in the address hash */
  121. };
  122. #ifdef TCP_ASYNC
  123. struct tcp_wbuffer{
  124. struct tcp_wbuffer* next;
  125. unsigned int b_size;
  126. char buf[1];
  127. };
  128. struct tcp_wbuffer_queue{
  129. struct tcp_wbuffer* first;
  130. struct tcp_wbuffer* last;
  131. ticks_t wr_timeout; /* write timeout*/
  132. unsigned int queued; /* total size */
  133. unsigned int offset; /* offset in the first wbuffer were data
  134. starts */
  135. unsigned int last_used; /* how much of the last buffer is used */
  136. };
  137. #endif
  138. struct tcp_connection{
  139. int s; /*socket, used by "tcp main" */
  140. int fd; /* used only by "children", don't modify it! private data! */
  141. gen_lock_t write_lock;
  142. int id; /* id (unique!) used to retrieve a specific connection when
  143. reply-ing*/
  144. int reader_pid; /* pid of the active reader process */
  145. struct receive_info rcv; /* src & dst ip, ports, proto a.s.o*/
  146. struct tcp_req req; /* request data */
  147. atomic_t refcnt;
  148. enum sip_protos type; /* PROTO_TCP or a protocol over it, e.g. TLS */
  149. unsigned short flags; /* connection related flags */
  150. snd_flags_t send_flags; /* special send flags */
  151. enum tcp_conn_states state; /* connection state */
  152. void* extra_data; /* extra data associated to the connection, 0 for tcp*/
  153. struct timer_ln timer;
  154. ticks_t timeout;/* connection timeout, after this it will be removed*/
  155. unsigned id_hash; /* hash index in the id_hash */
  156. struct tcp_connection* id_next; /* next, prev in id hash table */
  157. struct tcp_connection* id_prev;
  158. struct tcp_connection* c_next; /* child next prev (use locally) */
  159. struct tcp_connection* c_prev;
  160. struct tcp_conn_alias con_aliases[TCP_CON_MAX_ALIASES];
  161. int aliases; /* aliases number, at least 1 */
  162. #ifdef TCP_ASYNC
  163. struct tcp_wbuffer_queue wbuf_q;
  164. #endif
  165. };
  166. /* helper macros */
  167. #define tcpconn_set_send_flags(c, snd_flags) \
  168. SND_FLAGS_OR(&(c)->send_flags, &(c)->send_flags, &(snd_flags))
  169. #define tcpconn_close_after_send(c) ((c)->send_flags.f & SND_F_CON_CLOSE)
  170. #define TCP_RCV_INFO(c) (&(c)->rcv)
  171. #define TCP_RCV_LADDR(r) (&((r).dst_ip))
  172. #define TCP_RCV_LPORT(r) ((r).dst_port)
  173. #define TCP_RCV_PADDR(r) (&((r).src_ip))
  174. #define TCP_RCV_PPORT(r) ((r).src_port)
  175. #define TCP_RCV_PSU(r) (&(r).src_su)
  176. #define TCP_RCV_SOCK_INFO(r) ((r).bind_address)
  177. #define TCP_RCV_PROTO(r) ((r).proto)
  178. #ifdef USE_COMP
  179. #define TCP_RCV_COMP(r) ((r).comp)
  180. #else
  181. #define TCP_RCV_COMP(r) 0
  182. #endif /* USE_COMP */
  183. #define TCP_LADDR(c) TCP_RCV_LADDR(c->rcv)
  184. #define TCP_LPORT(c) TCP_RCV_LPORT(c->rcv)
  185. #define TCP_PADDR(c) TCP_RCV_PADDR(c->rcv)
  186. #define TCP_PPORT(c) TCP_RCV_PPORT(c->rcv)
  187. #define TCP_PSU(c) TCP_RCV_PSU(c->rcv)
  188. #define TCP_SOCK_INFO(c) TCP_RCV_SOCK_INFO(c->rcv)
  189. #define TCP_PROTO(c) TCP_RCV_PROTO(c->rcv)
  190. #define TCP_COMP(c) TCP_RCV_COMP(c->rcv)
  191. #define tcpconn_ref(c) atomic_inc(&((c)->refcnt))
  192. #define tcpconn_put(c) atomic_dec_and_test(&((c)->refcnt))
  193. #define init_tcp_req( r, rd_buf, rd_buf_size) \
  194. do{ \
  195. memset( (r), 0, sizeof(struct tcp_req)); \
  196. (r)->buf=(rd_buf) ;\
  197. (r)->b_size=(rd_buf_size)-1; /* space for 0 term. */ \
  198. (r)->parsed=(r)->pos=(r)->start=(r)->buf; \
  199. (r)->error=TCP_REQ_OK;\
  200. (r)->state=H_SKIP_EMPTY; \
  201. }while(0)
  202. /* add a tcpconn to a list*/
  203. /* list head, new element, next member, prev member */
  204. #define tcpconn_listadd(head, c, next, prev) \
  205. do{ \
  206. /* add it at the begining of the list*/ \
  207. (c)->next=(head); \
  208. (c)->prev=0; \
  209. if ((head)) (head)->prev=(c); \
  210. (head)=(c); \
  211. } while(0)
  212. /* remove a tcpconn from a list*/
  213. #define tcpconn_listrm(head, c, next, prev) \
  214. do{ \
  215. if ((head)==(c)) (head)=(c)->next; \
  216. if ((c)->next) (c)->next->prev=(c)->prev; \
  217. if ((c)->prev) (c)->prev->next=(c)->next; \
  218. }while(0)
  219. #define TCPCONN_LOCK lock_get(tcpconn_lock);
  220. #define TCPCONN_UNLOCK lock_release(tcpconn_lock);
  221. #define TCP_ALIAS_HASH_SIZE 4096
  222. #define TCP_ID_HASH_SIZE 1024
  223. /* hash (dst_ip, dst_port, local_ip, local_port) */
  224. static inline unsigned tcp_addr_hash( struct ip_addr* ip,
  225. unsigned short port,
  226. struct ip_addr* l_ip,
  227. unsigned short l_port)
  228. {
  229. unsigned h;
  230. if(ip->len==4)
  231. h=(ip->u.addr32[0]^port)^(l_ip->u.addr32[0]^l_port);
  232. else if (ip->len==16)
  233. h= (ip->u.addr32[0]^ip->u.addr32[1]^ip->u.addr32[2]^
  234. ip->u.addr32[3]^port) ^
  235. (l_ip->u.addr32[0]^l_ip->u.addr32[1]^l_ip->u.addr32[2]^
  236. l_ip->u.addr32[3]^l_port);
  237. else{
  238. LOG(L_CRIT, "tcp_addr_hash: BUG: bad len %d for an ip address\n",
  239. ip->len);
  240. return 0;
  241. }
  242. /* make sure the first bits are influenced by all 32
  243. * (the first log2(TCP_ALIAS_HASH_SIZE) bits should be a mix of all
  244. * 32)*/
  245. h ^= h>>17;
  246. h ^= h>>7;
  247. return h & (TCP_ALIAS_HASH_SIZE-1);
  248. }
  249. #define tcp_id_hash(id) (id&(TCP_ID_HASH_SIZE-1))
  250. struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
  251. union sockaddr_union* local_addr,
  252. ticks_t timeout);
  253. #endif