Skip to content

Commit

Permalink
usrloc: new param to close TCP connections
Browse files Browse the repository at this point in the history
the new `close_expired_tcp`, if set, forces Kamailio to close the TCP
connections of expired contacts
  • Loading branch information
camilleoudot committed Mar 5, 2015
1 parent ec11b9f commit c3f9e82
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
21 changes: 21 additions & 0 deletions modules/usrloc/doc/usrloc_admin.xml
Expand Up @@ -896,6 +896,27 @@ modparam("usrloc", "handle_lost_tcp", 1)
</example>
</section>

<section id="usrloc.p.close_expired_tcp">
<title><varname>close_expired_tcp</varname> (int)</title>
<para>
If set to 1, Kamailio will close the TCP connection when a contact
has expired, if the corresponding transport is TCP/TLS/WS/WSS.
</para>
<para>
<emphasis>
Default value is <quote>0</quote>.
</emphasis>
</para>
<example>
<title>Set <varname>close_expired_tcp</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("usrloc", "close_expired_tcp", 1)
...
</programlisting>
</example>
</section>

<section id="usrloc.p.expires_type">
<title><varname>expires_type</varname> (int)</title>
<para>
Expand Down
2 changes: 2 additions & 0 deletions modules/usrloc/ul_mod.c
Expand Up @@ -152,6 +152,7 @@ int db_mode = 0; /*!< Database sync scheme: 0-no db, 1-write through,
int use_domain = 0; /*!< Whether usrloc should use domain part of aor */
int desc_time_order = 0; /*!< By default do not enable timestamp ordering */
int handle_lost_tcp = 0; /*!< By default do not remove contacts before expiration time */
int close_expired_tcp = 0; /*!< By default do not close TCP connections for expired contacts */

int ul_fetch_rows = 2000; /*!< number of rows to fetch from result */
int ul_hash_size = 10;
Expand Down Expand Up @@ -207,6 +208,7 @@ static param_export_t params[] = {
{"hash_size", INT_PARAM, &ul_hash_size },
{"nat_bflag", INT_PARAM, &nat_bflag },
{"handle_lost_tcp", INT_PARAM, &handle_lost_tcp },
{"close_expired_tcp", INT_PARAM, &close_expired_tcp },
{"preload", PARAM_STRING|USE_FUNC_PARAM, (void*)ul_preload_param},
{"db_update_as_insert", INT_PARAM, &ul_db_update_as_insert},
{"timer_procs", INT_PARAM, &ul_timer_procs},
Expand Down
2 changes: 2 additions & 0 deletions modules/usrloc/ul_mod.h
Expand Up @@ -81,6 +81,8 @@ extern int ul_db_update_as_insert;
extern int ul_db_check_update;
extern int ul_keepalive_timeout;
extern int handle_lost_tcp;
extern int close_expired_tcp;


/*! nat branch flag */
extern unsigned int nat_bflag;
Expand Down
39 changes: 39 additions & 0 deletions modules/usrloc/urecord.c
Expand Up @@ -34,6 +34,7 @@
#include "../../ut.h"
#include "../../hashes.h"
#include "../../tcp_conn.h"
#include "../../pass_fd.h"
#include "ul_mod.h"
#include "usrloc.h"
#include "utime.h"
Expand Down Expand Up @@ -236,6 +237,31 @@ static inline int is_tcp_alive(ucontact_t *c)
return rc;
}

/*!
* \brief Close a TCP connection
*
* Requests the TCP main process to close the specified TCP connection
* \param conid the internal connection ID
*/
static inline int close_connection(int conid) {
struct tcp_connection *con;
long msg[2];
int n;
if ((con = tcpconn_get(conid, 0, 0, 0, 0))) {
msg[0] = (long)con;
msg[1] = CONN_EOF;

n = send_all(unix_tcp_sock, msg, sizeof(msg));
tcpconn_put(con);
if (unlikely(n <= 0)){
LM_ERR("failed to send close request: %s (%d)\n", strerror(errno), errno);
return 0;
}
return 1;
}
return 0;
}

/*!
* \brief Expires timer for NO_DB db_mode
*
Expand All @@ -247,6 +273,7 @@ static inline void nodb_timer(urecord_t* _r)
{
ucontact_t* ptr, *t;


ptr = _r->contacts;

while(ptr) {
Expand All @@ -264,6 +291,10 @@ static inline void nodb_timer(urecord_t* _r)
ptr->aor->len, ZSW(ptr->aor->s),
ptr->c.len, ZSW(ptr->c.s));

if (close_expired_tcp && is_valid_tcpconn(ptr)) {
close_connection(ptr->tcpconn_id);
}

t = ptr;
ptr = ptr->next;

Expand Down Expand Up @@ -301,6 +332,10 @@ static inline void wt_timer(urecord_t* _r)
ptr->aor->len, ZSW(ptr->aor->s),
ptr->c.len, ZSW(ptr->c.s));

if (close_expired_tcp && is_valid_tcpconn(ptr)) {
close_connection(ptr->tcpconn_id);
}

t = ptr;
ptr = ptr->next;

Expand Down Expand Up @@ -350,6 +385,10 @@ static inline void wb_timer(urecord_t* _r)
ptr->c.len, ZSW(ptr->c.s));
update_stat( _r->slot->d->expires, 1);

if (close_expired_tcp && is_valid_tcpconn(ptr)) {
close_connection(ptr->tcpconn_id);
}

t = ptr;
ptr = ptr->next;

Expand Down

0 comments on commit c3f9e82

Please sign in to comment.