From 99e65e1526f7720ffc861dd874eb74464e7c12ad Mon Sep 17 00:00:00 2001 From: Camille Oudot Date: Thu, 19 Feb 2015 17:51:36 +0100 Subject: [PATCH] tcpops: new function set_connection_lifetime() --- modules/tcpops/doc/functions.xml | 40 ++++++++++++++++++++++++++ modules/tcpops/tcpops.c | 15 ++++++++++ modules/tcpops/tcpops.h | 3 ++ modules/tcpops/tcpops_mod.c | 48 ++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) diff --git a/modules/tcpops/doc/functions.xml b/modules/tcpops/doc/functions.xml index e8d125f88ed..f95933f7025 100644 --- a/modules/tcpops/doc/functions.xml +++ b/modules/tcpops/doc/functions.xml @@ -104,4 +104,44 @@ onreply_route { ]]> +
+ + <function>set_connection_lifetime([conid], lifetime)</function> + + + Sets the connection lifetime of a connection (TCP). + + Meaning of the parameters is as follows: + + + conid (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. + + + + lifetime (seconds): the new connection lifetime. + + + + Retuns 1 on success, -1 on failure. + + <function>set_connection_lifetime</function> usage + + +
diff --git a/modules/tcpops/tcpops.c b/modules/tcpops/tcpops.c index 4104ce5d2ac..c7ea53cb875 100644 --- a/modules/tcpops/tcpops.c +++ b/modules/tcpops/tcpops.c @@ -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 @@ -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; +} diff --git a/modules/tcpops/tcpops.h b/modules/tcpops/tcpops.h index 30201d06249..c9a07716c9e 100644 --- a/modules/tcpops/tcpops.h +++ b/modules/tcpops/tcpops.h @@ -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_ */ diff --git a/modules/tcpops/tcpops_mod.c b/modules/tcpops/tcpops_mod.c index d4b90c422d7..bd1e10730af 100644 --- a/modules/tcpops/tcpops_mod.c +++ b/modules/tcpops/tcpops_mod.c @@ -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); @@ -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} }; @@ -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; +} + /** * */