From 381dfada3989d86a2afa88d7c13859b073c403d7 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Mon, 15 Jan 2018 12:49:57 +0100 Subject: [PATCH] tcpops: added function to get connection id based on target host:port - tcp_get_connid(hostport, pvname) - connection id is set in pvar paraemter --- src/modules/tcpops/tcpops_mod.c | 94 ++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/src/modules/tcpops/tcpops_mod.c b/src/modules/tcpops/tcpops_mod.c index 8e7fcdcacd6..1e844f5ed89 100644 --- a/src/modules/tcpops/tcpops_mod.c +++ b/src/modules/tcpops/tcpops_mod.c @@ -33,6 +33,8 @@ #include "../../core/globals.h" #include "../../core/sr_module.h" #include "../../core/tcp_options.h" +#include "../../core/resolve.h" +#include "../../core/lvalue.h" #include "../../core/dprint.h" #include "../../core/mod_fix.h" #include "../../core/events.h" @@ -58,7 +60,7 @@ static int w_tcpops_enable_closed_event1(sip_msg_t* msg, char* con, char* p2); static int w_tcpops_enable_closed_event0(sip_msg_t* msg, char* p1, char* p2); static int w_tcp_conid_state(sip_msg_t* msg, char* con, char *p2); 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 fixup_numpv(void** param, int param_no); @@ -81,6 +83,8 @@ static cmd_export_t cmds[]={ 0, 0, REQUEST_ROUTE|ONREPLY_ROUTE}, {"tcp_conid_state", (cmd_function)w_tcp_conid_state, 1, fixup_numpv, 0, ANY_ROUTE}, + {"tcp_get_conid", (cmd_function)w_tcp_get_conid, 2, + fixup_spve_pvar, fixup_free_spve_pvar, ANY_ROUTE}, {"tcp_conid_alive", (cmd_function)w_tcp_conid_alive, 1, fixup_numpv, 0, ANY_ROUTE}, {0, 0, 0, 0, 0, 0} @@ -513,6 +517,94 @@ static int fixup_numpv(void** param, int param_no) return fixup_igp_null(param, 1); } +/** + * + */ +static int ki_tcp_get_conid_helper(sip_msg_t* msg, str *saddr, pv_spec_t *pvs) +{ + int conid = 0; + sip_uri_t uaddr, *u; + dest_info_t dst; + char *p; + int ret; + ticks_t clifetime; + tcp_connection_t *c; + ip_addr_t ip; + int port; + pv_value_t val; + + if(pvs->setf==NULL) { + LM_ERR("output variable is read only\n"); + return -1; + } + + init_dest_info(&dst); + + u = &uaddr; + u->port_no = 5060; + u->host = *saddr; + /* detect ipv6 */ + p = memchr(saddr->s, ']', saddr->len); + if (p) { + p++; + p = memchr(p, ':', saddr->s + saddr->len - p); + } else { + p = memchr(saddr->s, ':', saddr->len); + } + if (p) { + u->host.len = p - saddr->s; + p++; + u->port_no = str2s(p, saddr->len - (p - saddr->s), NULL); + } + + ret = sip_hostport2su(&dst.to, &u->host, u->port_no, &dst.proto); + if(ret!=0) { + LM_ERR("failed to resolve [%.*s]\n", u->host.len, ZSW(u->host.s)); + return E_BUG; + } + + dst.proto = PROTO_TCP; + dst.id = 0; + clifetime = cfg_get(tcp, tcp_cfg, con_lifetime); + su2ip_addr(&ip, &dst.to); + port = su_getport(&dst.to); + c = tcpconn_get(dst.id, &ip, port, NULL, clifetime); + + if (unlikely(c==0)) { + goto setvalue; + } + conid = c->id; + tcpconn_put(c); + +setvalue: + memset(&val, 0, sizeof(pv_value_t)); + val.ri = conid; + val.flags = PV_VAL_INT; + if(pvs->setf(msg, &pvs->pvp, (int)EQ_T, &val)<0) { + LM_ERR("failed to set the output var\n"); + return -1; + } + if(conid==0) { + return -1; + } + return 1; +} + +/** + * + */ +static int w_tcp_get_conid(sip_msg_t* msg, char *paddr, char *pvn) +{ + str saddr; + + if(fixup_get_svalue(msg, (gparam_t*)paddr, &saddr)<0) { + LM_ERR("failed to get address parameter\n"); + return -1; + } + + return ki_tcp_get_conid_helper(msg, &saddr, (pv_spec_t*)pvn); +} + /** * */