/src/opensips/net/tcp_conn.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2001-2003 FhG Fokus |
3 | | * |
4 | | * This file is part of opensips, a free SIP server. |
5 | | * |
6 | | * opensips 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 | | * opensips 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 | | * History: |
22 | | * -------- |
23 | | * 2003-01-29 tcp buffer size ++-ed to allow for 0-terminator |
24 | | * 2003-06-30 added tcp_connection flags & state (andrei) |
25 | | * 2003-10-27 tcp port aliases support added (andrei) |
26 | | * 2012-01-19 added TCP keepalive support |
27 | | */ |
28 | | |
29 | | /*! |
30 | | * \file |
31 | | * \brief TCP protocol support |
32 | | */ |
33 | | |
34 | | |
35 | | |
36 | | #ifndef _NET_tcp_conn_h |
37 | | #define _NET_tcp_conn_h |
38 | | |
39 | | #include "../locking.h" |
40 | | #include "tcp_conn_defs.h" |
41 | | |
42 | | |
43 | | /*!< TCP connection lifetime, in seconds */ |
44 | | #define DEFAULT_TCP_CONNECTION_LIFETIME 120 |
45 | | /*!< TCP socket backlog count */ |
46 | | #define DEFAULT_TCP_SOCKET_BACKLOG 10 |
47 | | /*!< If a connect doesn't complete in more than 100ms, timeout */ |
48 | | #define DEFAULT_TCP_CONNECT_TIMEOUT 100 |
49 | | /*!< Maximum number of connections */ |
50 | | #define DEFAULT_TCP_MAX_CONNECTIONS 2048 |
51 | | /*!< After 5 seconds, the child "returns" |
52 | | the connection to the tcp master process */ |
53 | | #define TCP_CHILD_TIMEOUT 5 |
54 | | /*!< how often "tcp main" checks for timeout*/ |
55 | | #define TCP_MAIN_SELECT_TIMEOUT 5 |
56 | | /*!< the same as above but for children */ |
57 | | #define TCP_CHILD_SELECT_TIMEOUT 2 |
58 | | |
59 | | |
60 | | /* fd communication commands - internal usage ONLY */ |
61 | | enum conn_cmds { CONN_DESTROY=-4, CONN_ERROR_TCPW=-3,CONN_ERROR_GENW=-2, |
62 | | CONN_EOF=-1, CONN_RELEASE, CONN_GET_FD, CONN_NEW, ASYNC_CONNECT, |
63 | | ASYNC_WRITE_TCPW, ASYNC_WRITE_GENW, CONN_RELEASE_WRITE }; |
64 | | /* CONN_RELEASE[_WRITE], EOF, ERROR_TCPW, ASYNC_WRITE_TCPW, DESTROY can be |
65 | | * used by TCP "reader" workers/processes |
66 | | * CONN_GET_FD, NEW, CONNECT, ERROR_GENW, ASYNC_WRITE_GENW only by generic |
67 | | * writer workers/processes */ |
68 | | |
69 | | #ifdef TCP_DEBUG_CONN |
70 | | #define tcpconn_check_add(c) \ |
71 | | do { \ |
72 | | if ((c)->proc_id > 0) { \ |
73 | | LM_CRIT("add: conn=%p already in process %d\n", \ |
74 | | (c), (c)->proc_id); \ |
75 | | abort(); \ |
76 | | } \ |
77 | | if ((c)->c_next || ((c)->c_prev)) { \ |
78 | | LM_CRIT("add: conn=%p already linked somewhere else " \ |
79 | | "prev=%p next=%p\n", (c), (c)->c_prev, (c)->c_next); \ |
80 | | abort(); \ |
81 | | } \ |
82 | | } while(0) |
83 | | |
84 | | #define tcpconn_check_del(c) \ |
85 | | do { \ |
86 | | if ((c)->proc_id != process_no) { \ |
87 | | if ((c)->proc_id != -1) { \ |
88 | | LM_CRIT("del: conn=%p already in process %d\n", \ |
89 | | (c), (c)->proc_id); \ |
90 | | abort(); \ |
91 | | } else { \ |
92 | | LM_WARN("del: conn=%p removed before proc was assigned\n", (c)); \ |
93 | | } \ |
94 | | } \ |
95 | | } while(0) |
96 | | #else |
97 | | #define tcpconn_check_add(c) |
98 | | #define tcpconn_check_del(c) |
99 | | #endif |
100 | | |
101 | | |
102 | | /*! \brief add a tcpconn to a list |
103 | | * list head, new element, next member, prev member */ |
104 | | #define tcpconn_listadd(head, c, next, prev) \ |
105 | 0 | do{ \ |
106 | 0 | /* add it at the beginning of the list*/ \ |
107 | 0 | (c)->next=(head); \ |
108 | 0 | (c)->prev=0; \ |
109 | 0 | if ((head)) (head)->prev=(c); \ |
110 | 0 | (head)=(c); \ |
111 | 0 | } while(0) |
112 | | |
113 | | |
114 | | /*! \brief remove a tcpconn from a list*/ |
115 | | #define tcpconn_listrm(head, c, next, prev) \ |
116 | 0 | do{ \ |
117 | 0 | if ((head)==(c)) (head)=(c)->next; \ |
118 | 0 | if ((c)->next) (c)->next->prev=(c)->prev; \ |
119 | 0 | if ((c)->prev) (c)->prev->next=(c)->next; \ |
120 | 0 | (c)->prev = (c)->next = NULL; \ |
121 | 0 | }while(0) |
122 | | |
123 | | /*! \brief look up a tcpconn in a list */ |
124 | | static inline int tcpconn_list_find(struct tcp_connection *con, |
125 | | struct tcp_connection *list) |
126 | 0 | { |
127 | 0 | for (; list; list = list->c_next) { |
128 | 0 | if (con == list) { |
129 | 0 | return 1; |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | 0 | return 0; |
134 | 0 | } Unexecuted instantiation: net_tcp_proc.c:tcpconn_list_find Unexecuted instantiation: net_tcp.c:tcpconn_list_find |
135 | | |
136 | 0 | #define TCPCONN_GET_PART(_id) (_id%TCP_PARTITION_SIZE) |
137 | 0 | #define TCP_PART(_id) (tcp_parts[TCPCONN_GET_PART(_id)]) |
138 | | |
139 | | #define TCPCONN_LOCK(_id) \ |
140 | 0 | lock_get(tcp_parts[TCPCONN_GET_PART(_id)].tcpconn_lock); |
141 | | #define TCPCONN_UNLOCK(_id) \ |
142 | 0 | lock_release(tcp_parts[TCPCONN_GET_PART(_id)].tcpconn_lock); |
143 | | |
144 | 0 | #define TCP_ALIAS_HASH_SIZE 1024 |
145 | 0 | #define TCP_ID_HASH_SIZE 1024 |
146 | | |
147 | | static inline unsigned tcp_addr_hash(struct ip_addr* ip, unsigned short port) |
148 | 0 | { |
149 | 0 | if(ip->len==4) return (ip->u.addr32[0]^port)&(TCP_ALIAS_HASH_SIZE-1); |
150 | 0 | else if (ip->len==16) |
151 | 0 | return (ip->u.addr32[0]^ip->u.addr32[1]^ip->u.addr32[2]^ |
152 | 0 | ip->u.addr32[3]^port) & (TCP_ALIAS_HASH_SIZE-1); |
153 | 0 | else{ |
154 | 0 | LM_CRIT("bad len %d for an ip address\n", ip->len); |
155 | 0 | return 0; |
156 | 0 | } |
157 | 0 | } Unexecuted instantiation: net_tcp_proc.c:tcp_addr_hash Unexecuted instantiation: net_tcp.c:tcp_addr_hash |
158 | | |
159 | 0 | #define tcp_id_hash(id) (id&(TCP_ID_HASH_SIZE-1)) |
160 | | |
161 | | void tcpconn_put(struct tcp_connection* c); |
162 | | |
163 | | |
164 | | #endif |
165 | | |