Module: kamailio Branch: master Commit: ac55488bc2fac333b0b777d38875b7516c430925 URL: https://github.com/kamailio/kamailio/commit/ac55488bc2fac333b0b777d38875b751...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: 2021-06-22T19:01:02+02:00
tcpops: added tcp_close_connection([conid])
- close connection for current sip message or by conid
---
Modified: src/modules/tcpops/tcpops_mod.c
---
Diff: https://github.com/kamailio/kamailio/commit/ac55488bc2fac333b0b777d38875b751... Patch: https://github.com/kamailio/kamailio/commit/ac55488bc2fac333b0b777d38875b751...
---
diff --git a/src/modules/tcpops/tcpops_mod.c b/src/modules/tcpops/tcpops_mod.c index 23036fd771..8590e4b6b6 100644 --- a/src/modules/tcpops/tcpops_mod.c +++ b/src/modules/tcpops/tcpops_mod.c @@ -39,6 +39,7 @@ #include "../../core/mod_fix.h" #include "../../core/events.h" #include "../../core/kemi.h" +#include "../../core/pass_fd.h"
#include "tcpops.h"
@@ -63,6 +64,8 @@ static int w_tcp_conid_alive(sip_msg_t* msg, char* con, char *p2); static int w_tcp_get_conid(sip_msg_t* msg, char *paddr, char *pvn); static int w_tcp_set_otcpid(sip_msg_t* msg, char* conid, char *p2); static int w_tcp_set_otcpid_flag(sip_msg_t* msg, char* mode, char *p2); +static int w_tcp_close_connection(sip_msg_t* msg, char* p1, char *p2); +static int w_tcp_close_connection_id(sip_msg_t* msg, char* pconid, char *p2);
str tcpops_event_callback = STR_NULL;
@@ -103,6 +106,10 @@ static cmd_export_t cmds[]={ fixup_igp_all, fixup_free_igp_all, ANY_ROUTE}, {"tcp_set_otcpid_flag", (cmd_function)w_tcp_set_otcpid_flag, 1, fixup_igp_all, fixup_free_igp_all, ANY_ROUTE}, + {"tcp_close_connection", (cmd_function)w_tcp_close_connection, 0, + 0, 0, ANY_ROUTE}, + {"tcp_close_connection", (cmd_function)w_tcp_close_connection_id, 1, + fixup_igp_all, fixup_free_igp_all, ANY_ROUTE}, {0, 0, 0, 0, 0, 0} };
@@ -665,6 +672,65 @@ static int w_tcp_set_otcpid_flag(sip_msg_t* msg, char* mode, char *p2) return ki_tcp_set_otcpid_flag(msg, vmode); }
+/*! + * \brief Close a TCP connection + * + * Requests the TCP main process to close the specified TCP connection + * \param conid the internal connection ID + */ +static int ki_tcp_close_connection_id(sip_msg_t *msg, int conid) +{ + struct tcp_connection *con; + long mcmd[2]; + int n; + + if ((con = tcpconn_get(conid, 0, 0, 0, 0))) { + mcmd[0] = (long)con; + mcmd[1] = CONN_EOF; + + con->send_flags.f |= SND_F_CON_CLOSE; + con->flags |= F_CONN_FORCE_EOF; + + n = send_all(unix_tcp_sock, mcmd, sizeof(mcmd)); + if (unlikely(n <= 0)){ + LM_ERR("failed to send close request: %s (%d)\n", strerror(errno), errno); + return -2; + } + return 1; + } + return -1; +} + +/** + * + */ +static int ki_tcp_close_connection(sip_msg_t *msg) +{ + return ki_tcp_close_connection_id(msg, msg->rcv.proto_reserved1); +} + +/** + * + */ +static int w_tcp_close_connection_id(sip_msg_t* msg, char* pconid, char *p2) +{ + int conid = 0; + + if(fixup_get_ivalue(msg, (gparam_t*)pconid, &conid)<0) { + LM_ERR("failed to get conid parameter\n"); + return -1; + } + return ki_tcp_close_connection_id(msg, conid); +} + +/** + * + */ +static int w_tcp_close_connection(sip_msg_t* msg, char* p1, char *p2) +{ + return ki_tcp_close_connection_id(msg, msg->rcv.proto_reserved1); +} + /** * */ @@ -799,6 +865,16 @@ static sr_kemi_t sr_kemi_tcpops_exports[] = { { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } }, + { str_init("tcpops"), str_init("tcp_close_connection"), + SR_KEMIP_INT, ki_tcp_close_connection, + { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE, + SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } + }, + { str_init("tcpops"), str_init("tcp_close_connection_id"), + SR_KEMIP_INT, ki_tcp_close_connection_id, + { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE, + SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } + },
{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } } };