Browse code

tcpops: new function set_connection_lifetime()

Camille Oudot authored on 19/02/2015 16:51:36
Showing 4 changed files
... ...
@@ -104,4 +104,44 @@ onreply_route {
104 104
 			]]></programlisting>
105 105
 		</example>
106 106
 	</section>
107
+	<section id="tcpops.f.set_connection_lifetime">
108
+                <title>
109
+                        <function>set_connection_lifetime([conid], lifetime)</function>
110
+                </title>
111
+                <para>
112
+                                Sets the connection lifetime of a connection (TCP).
113
+                </para>
114
+                <para>Meaning of the parameters is as follows:</para>
115
+                <itemizedlist>
116
+                        <listitem>
117
+                                <para><emphasis>conid</emphasis> (optionnal): the kamailio internal
118
+                                connection id on which to set the new lifetime. If no parameter
119
+                                is given, it will be set on the current message source connection.
120
+                                </para>
121
+                        </listitem>
122
+                        <listitem>
123
+                                <para><emphasis>lifetime</emphasis> (seconds): the new connection lifetime.
124
+                                </para>
125
+                        </listitem>
126
+                </itemizedlist>
127
+                <para>Retuns 1 on success, -1 on failure.</para>
128
+                <example>
129
+                        <title><function>set_connection_lifetime</function> usage</title>
130
+                        <programlisting><![CDATA[
131
+...
132
+# use 10s as default lifetime
133
+tcp_connection_lifetime=10 
134
+...
135
+
136
+request_route {
137
+        ...
138
+        if (is_method("REGISTER") && pv_www_authenticate("$td", "xxx", "0")) {
139
+                # raise the TCP lifetime to a bigger value
140
+                set_connection_lifetime("3605");
141
+        }
142
+        ...
143
+}
144
+                        ]]></programlisting>
145
+                </example>
146
+        </section>
107 147
 </section>
... ...
@@ -30,6 +30,7 @@
30 30
 #include "../../tcp_conn.h"
31 31
 #include "../../globals.h"
32 32
 #include "../../pass_fd.h"
33
+#include "../../timer.h"
33 34
 
34 35
 /**
35 36
  * gets the fd of the current message source connection
... ...
@@ -169,3 +170,17 @@ int tcpops_keepalive_disable(int fd, int closefd)
169 169
 }
170 170
 
171 171
 #endif
172
+
173
+int tcpops_set_connection_lifetime(struct tcp_connection* con, int time) {
174
+	if (unlikely(con == NULL)) {
175
+		LM_CRIT("BUG: con == NULL");
176
+	}
177
+	if (unlikely(time < 0)) {
178
+		LM_ERR("Invalid timeout value, %d, must be >= 0\n", time);
179
+		return -1;
180
+	}
181
+	con->lifetime = S_TO_TICKS(time);
182
+	con->timeout = get_ticks_raw() + con->lifetime;
183
+	LM_DBG("new connection lifetime for conid=%d: %d\n", con->id, con->timeout);
184
+	return 1;
185
+}
... ...
@@ -24,9 +24,12 @@
24 24
 #ifndef TCP_KEEPALIVE_H_
25 25
 #define TCP_KEEPALIVE_H_
26 26
 
27
+#include "../../tcp_conn.h"
28
+
27 29
 int tcpops_get_current_fd(int conid, int *fd);
28 30
 int tcpops_acquire_fd_from_tcpmain(int conid, int *fd);
29 31
 int tcpops_keepalive_enable(int fd, int idle, int count, int interval, int closefd);
30 32
 int tcpops_keepalive_disable(int fd, int closefd);
33
+int tcpops_set_connection_lifetime(struct tcp_connection* con, int time);
31 34
 
32 35
 #endif /* TCP_KEEPALIVE_H_ */
... ...
@@ -44,6 +44,8 @@ static int w_tcp_keepalive_enable4(sip_msg_t* msg, char* con, char* idle, char *
44 44
 static int w_tcp_keepalive_enable3(sip_msg_t* msg, char* idle, char *cnt, char *intvl);
45 45
 static int w_tcp_keepalive_disable1(sip_msg_t* msg, char* con);
46 46
 static int w_tcp_keepalive_disable0(sip_msg_t* msg);
47
+static int w_tcpops_set_connection_lifetime2(sip_msg_t* msg, char* con, char* time);
48
+static int w_tcpops_set_connection_lifetime1(sip_msg_t* msg, char* time);
47 49
 
48 50
 static int fixup_numpv(void** param, int param_no);
49 51
 
... ...
@@ -57,6 +59,10 @@ static cmd_export_t cmds[]={
57 57
 		0, ANY_ROUTE},
58 58
 	{"tcp_keepalive_disable", (cmd_function)w_tcp_keepalive_disable0, 0, 0,
59 59
 		0, REQUEST_ROUTE|ONREPLY_ROUTE},
60
+	{"set_connection_lifetime", (cmd_function)w_tcpops_set_connection_lifetime2, 2, fixup_numpv,
61
+		0, ANY_ROUTE},
62
+	{"set_connection_lifetime", (cmd_function)w_tcpops_set_connection_lifetime1, 1, fixup_numpv,
63
+		0, REQUEST_ROUTE|ONREPLY_ROUTE},
60 64
 	{0, 0, 0, 0, 0, 0}
61 65
 };
62 66
 
... ...
@@ -210,6 +216,48 @@ static int w_tcp_keepalive_disable0(sip_msg_t* msg)
210 210
 	return tcpops_keepalive_disable(fd, 0);
211 211
 }
212 212
 
213
+
214
+static int w_tcpops_set_connection_lifetime2(sip_msg_t* msg, char* conid, char* time)
215
+{
216
+	struct tcp_connection *s_con;
217
+	int ret = -1;
218
+
219
+	_IVALUE (conid)
220
+	_IVALUE (time)
221
+
222
+	if (unlikely((s_con = tcpconn_get(i_conid, 0, 0, 0, 0)) == NULL)) {
223
+		LM_ERR("invalid connection id %d, (must be a TCP connid)\n", i_conid);
224
+		return 0;
225
+	} else {
226
+		ret = tcpops_set_connection_lifetime(s_con, i_time);
227
+		tcpconn_put(s_con);
228
+	}
229
+	return ret;
230
+}
231
+
232
+
233
+static int w_tcpops_set_connection_lifetime1(sip_msg_t* msg, char* time)
234
+{
235
+	struct tcp_connection *s_con;
236
+	int ret = -1;
237
+
238
+	_IVALUE (time)
239
+
240
+	if(unlikely(msg->rcv.proto != PROTO_TCP && msg->rcv.proto != PROTO_TLS && msg->rcv.proto != PROTO_WS && msg->rcv.proto != PROTO_WSS))
241
+	{
242
+		LM_ERR("the current message does not come from a TCP connection\n");
243
+		return -1;
244
+	}
245
+
246
+	if (unlikely((s_con = tcpconn_get(msg->rcv.proto_reserved1, 0, 0, 0, 0)) == NULL)) {
247
+		return -1;
248
+	} else {
249
+		ret = tcpops_set_connection_lifetime(s_con, i_time);
250
+		tcpconn_put(s_con);
251
+	}
252
+	return ret;
253
+}
254
+
213 255
 /**
214 256
  *
215 257
  */