tcp_conn.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef _tcp_conn_h
  28. #define _tcp_conn_h
  29. #include "ip_addr.h"
  30. #define TCP_BUF_SIZE 65535
  31. #define TCP_CON_TIMEOUT 60 /* in seconds */
  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. enum tcp_req_errors { TCP_REQ_INIT, TCP_REQ_OK, TCP_READ_ERROR,
  37. TCP_REQ_OVERRUN, TCP_REQ_BAD_LEN };
  38. enum tcp_req_states { H_SKIP_EMPTY, H_SKIP, H_LF, H_LFCR, H_BODY, H_STARTWS,
  39. H_CONT_LEN1, H_CONT_LEN2, H_CONT_LEN3, H_CONT_LEN4, H_CONT_LEN5,
  40. H_CONT_LEN6, H_CONT_LEN7, H_CONT_LEN8, H_CONT_LEN9, H_CONT_LEN10,
  41. H_CONT_LEN11, H_CONT_LEN12, H_CONT_LEN13, H_L_COLON,
  42. H_CONT_LEN_BODY, H_CONT_LEN_BODY_PARSE
  43. };
  44. /* fd communication commands */
  45. enum conn_cmds { CONN_DESTROY=-3, CONN_ERROR=-2, CONN_EOF=-1, CONN_RELEASE,
  46. CONN_GET_FD, CONN_NEW };
  47. struct tcp_req{
  48. struct tcp_req* next;
  49. /* sockaddr ? */
  50. char buf[TCP_BUF_SIZE]; /* bytes read so far*/
  51. char* start; /* where the message starts, after alll the empty lines are
  52. skipped*/
  53. char* pos; /* current position in buf */
  54. char* parsed; /* last parsed position */
  55. char* body; /* body position */
  56. int content_len;
  57. int has_content_len; /* 1 if content_length was parsed ok*/
  58. int complete; /* 1 if one req has been fully read, 0 otherwise*/
  59. int bytes_to_go; /* how many bytes we have still to read from the body*/
  60. enum tcp_req_errors error;
  61. enum tcp_req_states state;
  62. };
  63. struct tcp_connection{
  64. int s; /*socket, used by "tcp main" */
  65. int fd; /* used only by "children" */
  66. int id; /* id (unique!) used to retrieve a specific connection when
  67. reply-ing*/
  68. struct receive_info rcv; /* src & dst ip, ports, proto a.s.o*/
  69. #if 0
  70. struct ip_addr ip; /* peer ip */
  71. int port; /* peer port */
  72. int sock_idx; /* receiving socket index in the tcp_info array */
  73. union sockaddr_union su;
  74. #endif
  75. struct tcp_req req; /* request data */
  76. int refcnt;
  77. int timeout; /* connection timeout, after this it will be removed*/
  78. struct tcp_connection* next; /* next, prev in hash table, used by "main" */
  79. struct tcp_connection* prev;
  80. struct tcp_connection* c_next; /* child next prev (use locally) */
  81. struct tcp_connection* c_prev;
  82. };
  83. #define init_tcp_req( r) \
  84. do{ \
  85. memset( (r), 0, sizeof(struct tcp_req)); \
  86. (r)->parsed=(r)->pos=(r)->start=(r)->buf; \
  87. (r)->error=TCP_REQ_OK;\
  88. (r)->state=H_SKIP_EMPTY; \
  89. }while(0)
  90. /* add a tcpconn to a list*/
  91. /* list head, new element, next member, prev member */
  92. #define tcpconn_listadd(head, c, next, prev) \
  93. do{ \
  94. /* add it at the begining of the list*/ \
  95. (c)->next=(head); \
  96. (c)->prev=0; \
  97. if ((head)) (head)->prev=(c); \
  98. (head)=(c); \
  99. } while(0)
  100. /* remove a tcpconn from a list*/
  101. #define tcpconn_listrm(head, c, next, prev) \
  102. do{ \
  103. if ((head)==(c)) (head)=(c)->next; \
  104. if ((c)->next) (c)->next->prev=(c)->prev; \
  105. if ((c)->prev) (c)->prev->next=(c)->next; \
  106. }while(0)
  107. #define TCPCONN_LOCK LOG(L_CRIT, "LOCK not implemented yet: %s : %d: %s\n", \
  108. __FILE__, __LINE__, __FUNCTION__);
  109. #define TCPCONN_UNLOCK LOG(L_CRIT, "UNLOCK not implemented yet: %s: %d: %s\n",\
  110. __FILE__, __LINE__, __FUNCTION__);
  111. #endif