tcp_conn.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #ifndef _tcp_conn_h
  22. #define _tcp_conn_h
  23. #include "tcp_init.h"
  24. #include "tcp_options.h"
  25. #include "ip_addr.h"
  26. #include "locking.h"
  27. #include "atomic_ops.h"
  28. #include "timer_ticks.h"
  29. #include "timer.h"
  30. /* maximum number of port aliases x search wildcard possibilities */
  31. #define TCP_CON_MAX_ALIASES (4*3)
  32. #define TCP_CHILD_TIMEOUT 5 /* after 5 seconds, the child "returns"
  33. the connection to the tcp master process */
  34. #define TCP_MAIN_SELECT_TIMEOUT 5 /* how often "tcp main" checks for timeout*/
  35. #define TCP_CHILD_SELECT_TIMEOUT 2 /* the same as above but for children */
  36. /* tcp connection flags */
  37. #define F_CONN_READ_W 2 /* watched for READ ev. in main */
  38. #define F_CONN_WRITE_W 4 /* watched for WRITE (main) */
  39. #define F_CONN_READER 8 /* handled by a tcp reader */
  40. #define F_CONN_HASHED 16 /* in tcp_main hash */
  41. #define F_CONN_FD_CLOSED 32 /* fd was already closed */
  42. #define F_CONN_PENDING 64 /* pending connect (fd not known yet in main) */
  43. #define F_CONN_MAIN_TIMER 128 /* timer active in the tcp_main process */
  44. #define F_CONN_EOF_SEEN 256 /* FIN or RST have been received */
  45. #define F_CONN_FORCE_EOF 512 /* act as if an EOF was received */
  46. #define F_CONN_OOB_DATA 1024 /* out of band data on the connection */
  47. #define F_CONN_WR_ERROR 2048 /* write error on the fd */
  48. #define F_CONN_WANTS_RD 4096 /* conn. should be watched for READ */
  49. #define F_CONN_WANTS_WR 8192 /* conn. should be watched for WRITE */
  50. #define F_CONN_PASSIVE 16384 /* conn. created via accept() and not connect()*/
  51. #ifndef NO_READ_HTTP11
  52. #define READ_HTTP11
  53. #endif
  54. #ifndef NO_READ_MSRP
  55. #define READ_MSRP
  56. #endif
  57. #ifndef NO_READ_WS
  58. #define READ_WS
  59. #endif
  60. enum tcp_req_errors { TCP_REQ_INIT, TCP_REQ_OK, TCP_READ_ERROR,
  61. TCP_REQ_OVERRUN, TCP_REQ_BAD_LEN };
  62. enum tcp_req_states { H_SKIP_EMPTY, H_SKIP_EMPTY_CR_FOUND,
  63. H_SKIP_EMPTY_CRLF_FOUND, H_SKIP_EMPTY_CRLFCR_FOUND,
  64. H_SKIP, H_LF, H_LFCR, H_BODY, H_STARTWS,
  65. H_CONT_LEN1, H_CONT_LEN2, H_CONT_LEN3, H_CONT_LEN4, H_CONT_LEN5,
  66. H_CONT_LEN6, H_CONT_LEN7, H_CONT_LEN8, H_CONT_LEN9, H_CONT_LEN10,
  67. H_CONT_LEN11, H_CONT_LEN12, H_CONT_LEN13, H_L_COLON,
  68. H_CONT_LEN_BODY, H_CONT_LEN_BODY_PARSE,
  69. H_STUN_MSG, H_STUN_READ_BODY, H_STUN_FP, H_STUN_END, H_PING_CRLF
  70. #ifdef READ_HTTP11
  71. , H_HTTP11_CHUNK_START, H_HTTP11_CHUNK_SIZE,
  72. H_HTTP11_CHUNK_BODY, H_HTTP11_CHUNK_END, H_HTTP11_CHUNK_FINISH
  73. #endif
  74. #ifdef READ_MSRP
  75. , H_MSRP_BODY, H_MSRP_BODY_LF, H_MSRP_BODY_END, H_MSRP_FINISH
  76. #endif
  77. };
  78. enum tcp_conn_states { S_CONN_ERROR=-2, S_CONN_BAD=-1,
  79. S_CONN_OK=0, /* established (write or read) */
  80. S_CONN_INIT, /* initial state (invalid) */
  81. S_CONN_EOF,
  82. S_CONN_ACCEPT, S_CONN_CONNECT
  83. };
  84. /* fd communication commands */
  85. enum conn_cmds {
  86. CONN_DESTROY=-3 /* destroy connection & auto-dec. refcnt */,
  87. CONN_ERROR=-2 /* error on connection & auto-dec. refcnt */,
  88. CONN_EOF=-1 /* eof received or conn. closed & auto-dec refcnt */,
  89. CONN_NOP=0 /* do-nothing (invalid for tcp_main) */,
  90. CONN_RELEASE /* release a connection from tcp_read back into tcp_main
  91. & auto-dec refcnt */,
  92. CONN_GET_FD /* request a fd from tcp_main */,
  93. CONN_NEW /* update/set a fd int a new tcp connection; refcnts are
  94. not touched */,
  95. CONN_QUEUED_WRITE /* new write queue: start watching the fd for write &
  96. auto-dec refcnt */,
  97. CONN_NEW_PENDING_WRITE /* like CONN_NEW+CONN_QUEUED_WRITE: set fd and
  98. start watching it for write (write queue
  99. non-empty); refcnts are not touced */,
  100. CONN_NEW_COMPLETE /* like CONN_NEW_PENDING_WRITE, but there is no
  101. pending write (the write queue might be empty) */
  102. };
  103. /* CONN_RELEASE, EOF, ERROR, DESTROY can be used by "reader" processes
  104. * CONN_GET_FD, CONN_NEW*, CONN_QUEUED_WRITE only by writers */
  105. struct tcp_req{
  106. struct tcp_req* next;
  107. /* sockaddr ? */
  108. char* buf; /* bytes read so far (+0-terminator)*/
  109. char* start; /* where the message starts, after all the empty lines are
  110. skipped*/
  111. char* pos; /* current position in buf */
  112. char* parsed; /* last parsed position */
  113. char* body; /* body position */
  114. unsigned int b_size; /* buffer size-1 (extra space for 0-term)*/
  115. int content_len;
  116. #ifdef READ_HTTP11
  117. int chunk_size;
  118. #endif
  119. unsigned short flags; /* F_TCP_REQ_HAS_CLEN | F_TCP_REQ_COMPLETE */
  120. int bytes_to_go; /* how many bytes we have still to read from the body*/
  121. enum tcp_req_errors error;
  122. enum tcp_req_states state;
  123. };
  124. /* tcp_req flags */
  125. #define F_TCP_REQ_HAS_CLEN 1
  126. #define F_TCP_REQ_COMPLETE 2
  127. #ifdef READ_HTTP11
  128. #define F_TCP_REQ_BCHUNKED 4
  129. #endif
  130. #ifdef READ_MSRP
  131. #define F_TCP_REQ_MSRP_NO 8
  132. #define F_TCP_REQ_MSRP_FRAME 16
  133. #define F_TCP_REQ_MSRP_BODY 32
  134. #endif
  135. #define TCP_REQ_HAS_CLEN(tr) ((tr)->flags & F_TCP_REQ_HAS_CLEN)
  136. #define TCP_REQ_COMPLETE(tr) ((tr)->flags & F_TCP_REQ_COMPLETE)
  137. #ifdef READ_HTTP11
  138. #define TCP_REQ_BCHUNKED(tr) ((tr)->flags & F_TCP_REQ_BCHUNKED)
  139. #endif
  140. struct tcp_connection;
  141. /* tcp port alias structure */
  142. struct tcp_conn_alias{
  143. struct tcp_connection* parent;
  144. struct tcp_conn_alias* next;
  145. struct tcp_conn_alias* prev;
  146. unsigned short port; /* alias port */
  147. unsigned short hash; /* hash index in the address hash */
  148. };
  149. #ifdef TCP_ASYNC
  150. struct tcp_wbuffer{
  151. struct tcp_wbuffer* next;
  152. unsigned int b_size;
  153. char buf[1];
  154. };
  155. struct tcp_wbuffer_queue{
  156. struct tcp_wbuffer* first;
  157. struct tcp_wbuffer* last;
  158. ticks_t wr_timeout; /* write timeout*/
  159. unsigned int queued; /* total size */
  160. unsigned int offset; /* offset in the first wbuffer were data
  161. starts */
  162. unsigned int last_used; /* how much of the last buffer is used */
  163. };
  164. #endif
  165. struct tcp_connection{
  166. int s; /*socket, used by "tcp main" */
  167. int fd; /* used only by "children", don't modify it! private data! */
  168. gen_lock_t write_lock;
  169. int id; /* id (unique!) used to retrieve a specific connection when
  170. reply-ing*/
  171. int reader_pid; /* pid of the active reader process */
  172. struct receive_info rcv; /* src & dst ip, ports, proto a.s.o*/
  173. struct tcp_req req; /* request data */
  174. atomic_t refcnt;
  175. enum sip_protos type; /* PROTO_TCP or a protocol over it, e.g. TLS */
  176. unsigned short flags; /* connection related flags */
  177. snd_flags_t send_flags; /* special send flags */
  178. enum tcp_conn_states state; /* connection state */
  179. void* extra_data; /* extra data associated to the connection, 0 for tcp*/
  180. struct timer_ln timer;
  181. ticks_t timeout;/* connection timeout, after this it will be removed*/
  182. ticks_t lifetime;/* connection lifetime */
  183. unsigned id_hash; /* hash index in the id_hash */
  184. struct tcp_connection* id_next; /* next, prev in id hash table */
  185. struct tcp_connection* id_prev;
  186. struct tcp_connection* c_next; /* child next prev (use locally) */
  187. struct tcp_connection* c_prev;
  188. struct tcp_conn_alias con_aliases[TCP_CON_MAX_ALIASES];
  189. int aliases; /* aliases number, at least 1 */
  190. #ifdef TCP_ASYNC
  191. struct tcp_wbuffer_queue wbuf_q;
  192. #endif
  193. };
  194. /* helper macros */
  195. #define tcpconn_set_send_flags(c, snd_flags) \
  196. SND_FLAGS_OR(&(c)->send_flags, &(c)->send_flags, &(snd_flags))
  197. #define tcpconn_close_after_send(c) ((c)->send_flags.f & SND_F_CON_CLOSE)
  198. #define TCP_RCV_INFO(c) (&(c)->rcv)
  199. #define TCP_RCV_LADDR(r) (&((r).dst_ip))
  200. #define TCP_RCV_LPORT(r) ((r).dst_port)
  201. #define TCP_RCV_PADDR(r) (&((r).src_ip))
  202. #define TCP_RCV_PPORT(r) ((r).src_port)
  203. #define TCP_RCV_PSU(r) (&(r).src_su)
  204. #define TCP_RCV_SOCK_INFO(r) ((r).bind_address)
  205. #define TCP_RCV_PROTO(r) ((r).proto)
  206. #ifdef USE_COMP
  207. #define TCP_RCV_COMP(r) ((r).comp)
  208. #else
  209. #define TCP_RCV_COMP(r) 0
  210. #endif /* USE_COMP */
  211. #define TCP_LADDR(c) TCP_RCV_LADDR(c->rcv)
  212. #define TCP_LPORT(c) TCP_RCV_LPORT(c->rcv)
  213. #define TCP_PADDR(c) TCP_RCV_PADDR(c->rcv)
  214. #define TCP_PPORT(c) TCP_RCV_PPORT(c->rcv)
  215. #define TCP_PSU(c) TCP_RCV_PSU(c->rcv)
  216. #define TCP_SOCK_INFO(c) TCP_RCV_SOCK_INFO(c->rcv)
  217. #define TCP_PROTO(c) TCP_RCV_PROTO(c->rcv)
  218. #define TCP_COMP(c) TCP_RCV_COMP(c->rcv)
  219. #define tcpconn_ref(c) atomic_inc(&((c)->refcnt))
  220. #define tcpconn_put(c) atomic_dec_and_test(&((c)->refcnt))
  221. #define init_tcp_req( r, rd_buf, rd_buf_size) \
  222. do{ \
  223. memset( (r), 0, sizeof(struct tcp_req)); \
  224. (r)->buf=(rd_buf) ;\
  225. (r)->b_size=(rd_buf_size)-1; /* space for 0 term. */ \
  226. (r)->parsed=(r)->pos=(r)->start=(r)->buf; \
  227. (r)->error=TCP_REQ_OK;\
  228. (r)->state=H_SKIP_EMPTY; \
  229. }while(0)
  230. /* add a tcpconn to a list*/
  231. /* list head, new element, next member, prev member */
  232. #define tcpconn_listadd(head, c, next, prev) \
  233. do{ \
  234. /* add it at the begining of the list*/ \
  235. (c)->next=(head); \
  236. (c)->prev=0; \
  237. if ((head)) (head)->prev=(c); \
  238. (head)=(c); \
  239. } while(0)
  240. /* remove a tcpconn from a list*/
  241. #define tcpconn_listrm(head, c, next, prev) \
  242. do{ \
  243. if ((head)==(c)) (head)=(c)->next; \
  244. if ((c)->next) (c)->next->prev=(c)->prev; \
  245. if ((c)->prev) (c)->prev->next=(c)->next; \
  246. }while(0)
  247. #define TCPCONN_LOCK lock_get(tcpconn_lock);
  248. #define TCPCONN_UNLOCK lock_release(tcpconn_lock);
  249. #define TCP_ALIAS_HASH_SIZE 4096
  250. #define TCP_ID_HASH_SIZE 1024
  251. /* hash (dst_ip, dst_port, local_ip, local_port) */
  252. static inline unsigned tcp_addr_hash( struct ip_addr* ip,
  253. unsigned short port,
  254. struct ip_addr* l_ip,
  255. unsigned short l_port)
  256. {
  257. unsigned h;
  258. if(ip->len==4)
  259. h=(ip->u.addr32[0]^port)^(l_ip->u.addr32[0]^l_port);
  260. else if (ip->len==16)
  261. h= (ip->u.addr32[0]^ip->u.addr32[1]^ip->u.addr32[2]^
  262. ip->u.addr32[3]^port) ^
  263. (l_ip->u.addr32[0]^l_ip->u.addr32[1]^l_ip->u.addr32[2]^
  264. l_ip->u.addr32[3]^l_port);
  265. else{
  266. LM_CRIT("bad len %d for an ip address\n", ip->len);
  267. return 0;
  268. }
  269. /* make sure the first bits are influenced by all 32
  270. * (the first log2(TCP_ALIAS_HASH_SIZE) bits should be a mix of all
  271. * 32)*/
  272. h ^= h>>17;
  273. h ^= h>>7;
  274. return h & (TCP_ALIAS_HASH_SIZE-1);
  275. }
  276. #define tcp_id_hash(id) (id&(TCP_ID_HASH_SIZE-1))
  277. struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
  278. union sockaddr_union* local_addr,
  279. ticks_t timeout);
  280. typedef struct tcp_event_info {
  281. int type;
  282. char *buf;
  283. unsigned int len;
  284. struct receive_info *rcv;
  285. struct tcp_connection *con;
  286. } tcp_event_info_t;
  287. typedef struct ws_event_info {
  288. int type;
  289. char *buf;
  290. unsigned int len;
  291. int id;
  292. } ws_event_info_t;
  293. #endif