From c218e3597e734bfe4308ce1911437080637107bd Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Sat, 27 May 2017 19:57:41 +0200 Subject: [PATCH] pipelimit: exported functions to kemi framework --- src/modules/pipelimit/pipelimit.c | 115 +++++++++++++++++++++++------- 1 file changed, 90 insertions(+), 25 deletions(-) diff --git a/src/modules/pipelimit/pipelimit.c b/src/modules/pipelimit/pipelimit.c index 32dec4a55f4..702d22f879a 100644 --- a/src/modules/pipelimit/pipelimit.c +++ b/src/modules/pipelimit/pipelimit.c @@ -53,6 +53,7 @@ #include "../../core/rpc.h" #include "../../core/rpc_lookup.h" #include "../../core/rand/kam_rand.h" +#include "../../core/kemi.h" #include "pl_statistics.h" #include "pl_ht.h" @@ -640,6 +641,36 @@ static int w_pl_check(struct sip_msg* msg, char *p1, char *p2) } +/** + * limit checking with creation of pipe if it doesn't exist + */ +static int pl_check_limit(sip_msg_t* msg, str *pipeid, str *alg, int limit) +{ + pl_pipe_t *pipe = NULL; + + pipe = pl_pipe_get(pipeid, 1); + if(pipe==NULL) { + LM_DBG("pipe not found [%.*s] - trying to add it\n", + pipeid->len, pipeid->s); + if(pl_pipe_add(pipeid, alg, limit)<0) { + LM_ERR("failed to add pipe [%.*s]\n", + pipeid->len, pipeid->s); + return -2; + } + pipe = pl_pipe_get(pipeid, 0); + if(pipe==NULL) { + LM_ERR("failed to retrieve pipe [%.*s]\n", + pipeid->len, pipeid->s); + return -2; + } + } else { + if(limit>0) pipe->limit = limit; + pl_pipe_release(&pipe->name); + } + + return pl_check(msg, pipeid); +} + /** * limit checking with creation of pipe if it doesn't exist */ @@ -649,7 +680,6 @@ static int w_pl_check3(struct sip_msg* msg, char *p1pipe, char *p2alg, int limit; str pipeid = {0, 0}; str alg = {0, 0}; - pl_pipe_t *pipe = NULL; if(msg==NULL) return -1; @@ -674,30 +704,7 @@ static int w_pl_check3(struct sip_msg* msg, char *p1pipe, char *p2alg, return -1; } - pipe = pl_pipe_get(&pipeid, 1); - if(pipe==NULL) - { - LM_DBG("pipe not found [%.*s] - trying to add it\n", - pipeid.len, pipeid.s); - if(pl_pipe_add(&pipeid, &alg, limit)<0) - { - LM_ERR("failed to add pipe [%.*s]\n", - pipeid.len, pipeid.s); - return -2; - } - pipe = pl_pipe_get(&pipeid, 0); - if(pipe==NULL) - { - LM_ERR("failed to retrieve pipe [%.*s]\n", - pipeid.len, pipeid.s); - return -2; - } - } else { - if(limit>0) pipe->limit = limit; - pl_pipe_release(&pipe->name); - } - - return pl_check(msg, &pipeid); + return pl_check_limit(msg, &pipeid, &alg, limit); } static int fixup_pl_check3(void** param, int param_no) @@ -805,3 +812,61 @@ static rpc_export_t rpc_methods[] = { {0, 0, 0, 0} }; +static int ki_pl_drop(sip_msg_t* msg) +{ + return pl_drop(msg, 0, 0); +} + +static int ki_pl_drop_retry(sip_msg_t* msg, int rafter) +{ + return pl_drop(msg, rafter, rafter); +} + +static int ki_pl_drop_range(sip_msg_t* msg, int rmin, int rmax) +{ + return pl_drop(msg, rmin, rmax); +} + +/** + * + */ +/* clang-format off */ +static sr_kemi_t sr_kemi_pipelimit_exports[] = { + { str_init("pipelimit"), str_init("pl_check"), + SR_KEMIP_INT, pl_check, + { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE, + SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } + }, + { str_init("pipelimit"), str_init("pl_check_limit"), + SR_KEMIP_INT, pl_check_limit, + { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT, + SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } + }, + { str_init("pipelimit"), str_init("pl_drop"), + SR_KEMIP_INT, ki_pl_drop, + { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE, + SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } + }, + { str_init("pipelimit"), str_init("pl_drop_retry"), + SR_KEMIP_INT, ki_pl_drop_retry, + { SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE, + SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } + }, + { str_init("pipelimit"), str_init("pl_drop_range"), + SR_KEMIP_INT, ki_pl_drop_range, + { SR_KEMIP_INT, SR_KEMIP_INT, SR_KEMIP_NONE, + SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE } + }, + + { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } } +}; +/* clang-format on */ + +/** + * + */ +int mod_register(char *path, int *dlflags, void *p1, void *p2) +{ + sr_kemi_modules_add(sr_kemi_pipelimit_exports); + return 0; +} \ No newline at end of file