Skip to content

Commit

Permalink
tcpops: new function set_connection_lifetime()
Browse files Browse the repository at this point in the history
  • Loading branch information
camilleoudot committed Feb 20, 2015
1 parent 0ec36ae commit 99e65e1
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
40 changes: 40 additions & 0 deletions modules/tcpops/doc/functions.xml
Expand Up @@ -104,4 +104,44 @@ onreply_route {
]]></programlisting>
</example>
</section>
<section id="tcpops.f.set_connection_lifetime">
<title>
<function>set_connection_lifetime([conid], lifetime)</function>
</title>
<para>
Sets the connection lifetime of a connection (TCP).
</para>
<para>Meaning of the parameters is as follows:</para>
<itemizedlist>
<listitem>
<para><emphasis>conid</emphasis> (optionnal): the kamailio internal
connection id on which to set the new lifetime. If no parameter
is given, it will be set on the current message source connection.
</para>
</listitem>
<listitem>
<para><emphasis>lifetime</emphasis> (seconds): the new connection lifetime.
</para>
</listitem>
</itemizedlist>
<para>Retuns 1 on success, -1 on failure.</para>
<example>
<title><function>set_connection_lifetime</function> usage</title>
<programlisting><![CDATA[
...
# use 10s as default lifetime
tcp_connection_lifetime=10
...
request_route {
...
if (is_method("REGISTER") && pv_www_authenticate("$td", "xxx", "0")) {
# raise the TCP lifetime to a bigger value
set_connection_lifetime("3605");
}
...
}
]]></programlisting>
</example>
</section>
</section>
15 changes: 15 additions & 0 deletions modules/tcpops/tcpops.c
Expand Up @@ -30,6 +30,7 @@
#include "../../tcp_conn.h"
#include "../../globals.h"
#include "../../pass_fd.h"
#include "../../timer.h"

/**
* gets the fd of the current message source connection
Expand Down Expand Up @@ -169,3 +170,17 @@ int tcpops_keepalive_disable(int fd, int closefd)
}

#endif

int tcpops_set_connection_lifetime(struct tcp_connection* con, int time) {
if (unlikely(con == NULL)) {
LM_CRIT("BUG: con == NULL");
}
if (unlikely(time < 0)) {
LM_ERR("Invalid timeout value, %d, must be >= 0\n", time);
return -1;
}
con->lifetime = S_TO_TICKS(time);
con->timeout = get_ticks_raw() + con->lifetime;
LM_DBG("new connection lifetime for conid=%d: %d\n", con->id, con->timeout);
return 1;
}
3 changes: 3 additions & 0 deletions modules/tcpops/tcpops.h
Expand Up @@ -24,9 +24,12 @@
#ifndef TCP_KEEPALIVE_H_
#define TCP_KEEPALIVE_H_

#include "../../tcp_conn.h"

int tcpops_get_current_fd(int conid, int *fd);
int tcpops_acquire_fd_from_tcpmain(int conid, int *fd);
int tcpops_keepalive_enable(int fd, int idle, int count, int interval, int closefd);
int tcpops_keepalive_disable(int fd, int closefd);
int tcpops_set_connection_lifetime(struct tcp_connection* con, int time);

#endif /* TCP_KEEPALIVE_H_ */
48 changes: 48 additions & 0 deletions modules/tcpops/tcpops_mod.c
Expand Up @@ -44,6 +44,8 @@ static int w_tcp_keepalive_enable4(sip_msg_t* msg, char* con, char* idle, char *
static int w_tcp_keepalive_enable3(sip_msg_t* msg, char* idle, char *cnt, char *intvl);
static int w_tcp_keepalive_disable1(sip_msg_t* msg, char* con);
static int w_tcp_keepalive_disable0(sip_msg_t* msg);
static int w_tcpops_set_connection_lifetime2(sip_msg_t* msg, char* con, char* time);
static int w_tcpops_set_connection_lifetime1(sip_msg_t* msg, char* time);

static int fixup_numpv(void** param, int param_no);

Expand All @@ -57,6 +59,10 @@ static cmd_export_t cmds[]={
0, ANY_ROUTE},
{"tcp_keepalive_disable", (cmd_function)w_tcp_keepalive_disable0, 0, 0,
0, REQUEST_ROUTE|ONREPLY_ROUTE},
{"set_connection_lifetime", (cmd_function)w_tcpops_set_connection_lifetime2, 2, fixup_numpv,
0, ANY_ROUTE},
{"set_connection_lifetime", (cmd_function)w_tcpops_set_connection_lifetime1, 1, fixup_numpv,
0, REQUEST_ROUTE|ONREPLY_ROUTE},
{0, 0, 0, 0, 0, 0}
};

Expand Down Expand Up @@ -210,6 +216,48 @@ static int w_tcp_keepalive_disable0(sip_msg_t* msg)
return tcpops_keepalive_disable(fd, 0);
}


static int w_tcpops_set_connection_lifetime2(sip_msg_t* msg, char* conid, char* time)
{
struct tcp_connection *s_con;
int ret = -1;

_IVALUE (conid)
_IVALUE (time)

if (unlikely((s_con = tcpconn_get(i_conid, 0, 0, 0, 0)) == NULL)) {
LM_ERR("invalid connection id %d, (must be a TCP connid)\n", i_conid);
return 0;
} else {
ret = tcpops_set_connection_lifetime(s_con, i_time);
tcpconn_put(s_con);
}
return ret;
}


static int w_tcpops_set_connection_lifetime1(sip_msg_t* msg, char* time)
{
struct tcp_connection *s_con;
int ret = -1;

_IVALUE (time)

if(unlikely(msg->rcv.proto != PROTO_TCP && msg->rcv.proto != PROTO_TLS && msg->rcv.proto != PROTO_WS && msg->rcv.proto != PROTO_WSS))
{
LM_ERR("the current message does not come from a TCP connection\n");
return -1;
}

if (unlikely((s_con = tcpconn_get(msg->rcv.proto_reserved1, 0, 0, 0, 0)) == NULL)) {
return -1;
} else {
ret = tcpops_set_connection_lifetime(s_con, i_time);
tcpconn_put(s_con);
}
return ret;
}

/**
*
*/
Expand Down

0 comments on commit 99e65e1

Please sign in to comment.