diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000000..de87c76fda6 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,11 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.3.0 + hooks: + - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v15.0.7 + hooks: + - id: clang-format diff --git a/src/core/action.c b/src/core/action.c index 6767c943b2f..4a29a73b06e 100644 --- a/src/core/action.c +++ b/src/core/action.c @@ -28,7 +28,6 @@ */ - #include "comp_defs.h" #include "action.h" @@ -74,7 +73,7 @@ #include #endif -int _last_returned_code = 0; +int _last_returned_code = 0; /* current action executed from config file */ static cfg_action_t *_cfg_crt_action = 0; @@ -96,7 +95,7 @@ cfg_action_t *get_cfg_crt_action(void) /* return line in config for current executed action */ int get_cfg_crt_line(void) { - if(_cfg_crt_action==0) + if(_cfg_crt_action == 0) return 0; return _cfg_crt_action->cline; } @@ -104,7 +103,7 @@ int get_cfg_crt_line(void) /* return name of config for current executed action */ char *get_cfg_crt_file_name(void) { - if(_cfg_crt_action==0) + if(_cfg_crt_action == 0) return 0; return _cfg_crt_action->cfile; } @@ -112,7 +111,7 @@ char *get_cfg_crt_file_name(void) /* return name of routing block for current executed action */ char *get_cfg_crt_route_name(void) { - if(_cfg_crt_action==0) + if(_cfg_crt_action == 0) return 0; return _cfg_crt_action->rname; } @@ -123,37 +122,37 @@ char *get_cfg_crt_route_name(void) * @param ret - module function (v0 or v2) retcode * Side-effects: sets _last_returned_code */ -#define MODF_HANDLE_RETCODE(h, ret) \ - do { \ +#define MODF_HANDLE_RETCODE(h, ret) \ + do { \ /* if (unlikely((ret)==0)) (h)->run_flags|=EXIT_R_F; */ \ - (h)->run_flags |= EXIT_R_F & (((ret) != 0) -1); \ - (h)->last_retcode=(ret); \ - _last_returned_code = (h)->last_retcode; \ + (h)->run_flags |= EXIT_R_F & (((ret) != 0) - 1); \ + (h)->last_retcode = (ret); \ + _last_returned_code = (h)->last_retcode; \ } while(0) - /* frees parameters converted using MODF_RVE_PARAM_CONVERT() from dst. * (used internally in do_action()) * Assumes src is unchanged. * Side-effects: clobbers i (int). */ -#define MODF_RVE_PARAM_FREE(cmd, src, dst) \ - for (i=0; i < (dst)[1].u.number; i++) { \ - if ((src)[i+2].type == RVE_ST && (dst)[i+2].u.data) { \ - if ((dst)[i+2].type == RVE_FREE_FIXUP_ST) {\ - /* call free_fixup (which should restore the original - string) */ \ - (void)call_fixup((cmd)->free_fixup, &(dst)[i+2].u.data, i+1); \ - } else if ((dst)[i+2].type == FPARAM_DYN_ST) {\ - /* completely frees fparam and restore original string */\ - fparam_free_restore(&(dst)[i+2].u.data); \ - } \ - /* free allocated string */ \ - pkg_free((dst)[i+2].u.data); \ - (dst)[i+2].u.data = 0; \ - } \ - } +#define MODF_RVE_PARAM_FREE(cmd, src, dst) \ + for(i = 0; i < (dst)[1].u.number; i++) { \ + if((src)[i + 2].type == RVE_ST && (dst)[i + 2].u.data) { \ + if((dst)[i + 2].type == RVE_FREE_FIXUP_ST) { \ + /* call free_fixup (which should restore the original + string) */ \ + (void)call_fixup( \ + (cmd)->free_fixup, &(dst)[i + 2].u.data, i + 1); \ + } else if((dst)[i + 2].type == FPARAM_DYN_ST) { \ + /* completely frees fparam and restore original string */ \ + fparam_free_restore(&(dst)[i + 2].u.data); \ + } \ + /* free allocated string */ \ + pkg_free((dst)[i + 2].u.data); \ + (dst)[i + 2].u.data = 0; \ + } \ + } /* fills dst from src, converting RVE_ST params to STRING_ST. @@ -163,63 +162,64 @@ char *get_cfg_crt_route_name(void) * WARNING: dst must be cleaned when done, use MODULE_RVE_PARAM_FREE() * Side-effects: clobbers i (int), s (str), rv (rvalue*), might jump to error. */ -#define MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst) \ - do { \ - (dst)[1]=(src)[1]; \ - for (i=0; i < (src)[1].u.number; i++) { \ - if ((src)[2+i].type == RVE_ST) { \ - rv=rval_expr_eval((h), (msg), (src)[i+2].u.data); \ - if (unlikely(rv == 0 || \ - rval_get_str((h), (msg), &s, rv, 0) < 0)) { \ - rval_destroy(rv); \ - ERR("failed to convert RVE to string\n"); \ - (dst)[1].u.number = i; \ - MODF_RVE_PARAM_FREE(cmd, src, dst); \ - goto error; \ - } \ - (dst)[i+2].type = STRING_RVE_ST; \ - (dst)[i+2].u.string = s.s; \ - (dst)[i+2].u.str.len = s.len; \ - rval_destroy(rv); \ - if ((cmd)->fixup) {\ - if ((cmd)->free_fixup) {\ - if (likely( call_fixup((cmd)->fixup, \ - &(dst)[i+2].u.data, i+1) >= 0) ) { \ - /* success => mark it for calling free fixup */ \ - if (likely((dst)[i+2].u.data != s.s)) \ - (dst)[i+2].type = RVE_FREE_FIXUP_ST; \ - } else { \ +#define MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst) \ + do { \ + (dst)[1] = (src)[1]; \ + for(i = 0; i < (src)[1].u.number; i++) { \ + if((src)[2 + i].type == RVE_ST) { \ + rv = rval_expr_eval((h), (msg), (src)[i + 2].u.data); \ + if(unlikely(rv == 0 \ + || rval_get_str((h), (msg), &s, rv, 0) < 0)) { \ + rval_destroy(rv); \ + ERR("failed to convert RVE to string\n"); \ + (dst)[1].u.number = i; \ + MODF_RVE_PARAM_FREE(cmd, src, dst); \ + goto error; \ + } \ + (dst)[i + 2].type = STRING_RVE_ST; \ + (dst)[i + 2].u.string = s.s; \ + (dst)[i + 2].u.str.len = s.len; \ + rval_destroy(rv); \ + if((cmd)->fixup) { \ + if((cmd)->free_fixup) { \ + if(likely(call_fixup((cmd)->fixup, \ + &(dst)[i + 2].u.data, i + 1) \ + >= 0)) { \ + /* success => mark it for calling free fixup */ \ + if(likely((dst)[i + 2].u.data != s.s)) \ + (dst)[i + 2].type = RVE_FREE_FIXUP_ST; \ + } else { \ /* error calling fixup => mark conv. parameter \ and return error */ \ - (dst)[1].u.number = i; \ - ERR("runtime fixup failed for %s param %d\n", \ - (cmd)->name, i+1); \ - MODF_RVE_PARAM_FREE(cmd, src, dst); \ - goto error; \ - } \ - } else if ((cmd)->fixup_flags & FIXUP_F_FPARAM_RVE) { \ - if (likely( call_fixup((cmd)->fixup, \ - &(dst)[i+2].u.data, i+1) >= 0)) { \ - if ((dst)[i+2].u.data != s.s) \ - (dst)[i+2].type = FPARAM_DYN_ST; \ - } else { \ + (dst)[1].u.number = i; \ + ERR("runtime fixup failed for %s param %d\n", \ + (cmd)->name, i + 1); \ + MODF_RVE_PARAM_FREE(cmd, src, dst); \ + goto error; \ + } \ + } else if((cmd)->fixup_flags & FIXUP_F_FPARAM_RVE) { \ + if(likely(call_fixup((cmd)->fixup, \ + &(dst)[i + 2].u.data, i + 1) \ + >= 0)) { \ + if((dst)[i + 2].u.data != s.s) \ + (dst)[i + 2].type = FPARAM_DYN_ST; \ + } else { \ /* error calling fixup => mark conv. parameter \ and return error */ \ - (dst)[1].u.number = i; \ - ERR("runtime fixup failed for %s param %d\n", \ - (cmd)->name, i+1); \ - MODF_RVE_PARAM_FREE(cmd, src, dst); \ - goto error; \ - }\ - } \ - } \ - } else \ - (dst)[i+2]=(src)[i+2]; \ - } \ + (dst)[1].u.number = i; \ + ERR("runtime fixup failed for %s param %d\n", \ + (cmd)->name, i + 1); \ + MODF_RVE_PARAM_FREE(cmd, src, dst); \ + goto error; \ + } \ + } \ + } \ + } else \ + (dst)[i + 2] = (src)[i + 2]; \ + } \ } while(0) - /* call a module function with normal STRING_ST params. * (used internally in do_action()) * @param f_type - cmd_function type @@ -233,23 +233,22 @@ char *get_cfg_crt_route_name(void) * */ #ifdef __SUNPRO_C -#define MODF_CALL(f_type, h, msg, src, ...) \ - do { \ - cmd=(src)[0].u.data; \ - ret=((f_type)cmd->function)((msg), __VA_ARGS__); \ - MODF_HANDLE_RETCODE(h, ret); \ - } while (0) -#else /* ! __SUNPRO_C (gcc, icc a.s.o) */ -#define MODF_CALL(f_type, h, msg, src, params...) \ - do { \ - cmd=(src)[0].u.data; \ - ret=((f_type)cmd->function)((msg), ## params ); \ - MODF_HANDLE_RETCODE(h, ret); \ - } while (0) +#define MODF_CALL(f_type, h, msg, src, ...) \ + do { \ + cmd = (src)[0].u.data; \ + ret = ((f_type)cmd->function)((msg), __VA_ARGS__); \ + MODF_HANDLE_RETCODE(h, ret); \ + } while(0) +#else /* ! __SUNPRO_C (gcc, icc a.s.o) */ +#define MODF_CALL(f_type, h, msg, src, params...) \ + do { \ + cmd = (src)[0].u.data; \ + ret = ((f_type)cmd->function)((msg), ##params); \ + MODF_HANDLE_RETCODE(h, ret); \ + } while(0) #endif /* __SUNPRO_C */ - /* call a module function with possible RVE params. * (used internally in do_action()) * @param f_type - cmd_function type @@ -266,55 +265,55 @@ char *get_cfg_crt_route_name(void) * */ #ifdef __SUNPRO_C -#define MODF_RVE_CALL(f_type, h, msg, src, dst, ...) \ - do { \ - cmd=(src)[0].u.data; \ - MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst); \ - ret=((f_type)cmd->function)((msg), __VA_ARGS__); \ - MODF_HANDLE_RETCODE(h, ret); \ - /* free strings allocated by us or fixups */ \ - MODF_RVE_PARAM_FREE(cmd, src, dst); \ - } while (0) -#else /* ! __SUNPRO_C (gcc, icc a.s.o) */ +#define MODF_RVE_CALL(f_type, h, msg, src, dst, ...) \ + do { \ + cmd = (src)[0].u.data; \ + MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst); \ + ret = ((f_type)cmd->function)((msg), __VA_ARGS__); \ + MODF_HANDLE_RETCODE(h, ret); \ + /* free strings allocated by us or fixups */ \ + MODF_RVE_PARAM_FREE(cmd, src, dst); \ + } while(0) +#else /* ! __SUNPRO_C (gcc, icc a.s.o) */ #define MODF_RVE_CALL(f_type, h, msg, src, dst, params...) \ - do { \ - cmd=(src)[0].u.data; \ - MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst); \ - ret=((f_type)cmd->function)((msg), ## params ); \ - MODF_HANDLE_RETCODE(h, ret); \ - /* free strings allocated by us or fixups */ \ - MODF_RVE_PARAM_FREE(cmd, src, dst); \ - } while (0) + do { \ + cmd = (src)[0].u.data; \ + MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst); \ + ret = ((f_type)cmd->function)((msg), ##params); \ + MODF_HANDLE_RETCODE(h, ret); \ + /* free strings allocated by us or fixups */ \ + MODF_RVE_PARAM_FREE(cmd, src, dst); \ + } while(0) #endif /* __SUNPRO_C */ /* ret= 0! if action -> end of list(e.g DROP), > 0 to continue processing next actions and <0 on error */ -int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg) +int do_action(struct run_act_ctx *h, struct action *a, struct sip_msg *msg) { int ret; long li; long v; struct dest_info dst; - char* tmp; + char *tmp; char *new_uri, *end, *crt; - ksr_cmd_export_t* cmd; + ksr_cmd_export_t *cmd; int len; int user; struct sip_uri uri, next_hop; struct sip_uri *u; unsigned short port; - str* dst_host; + str *dst_host; int i, flags; - avp_t* avp; + avp_t *avp; struct search_state st; - struct switch_cond_table* sct; - struct switch_jmp_table* sjt; - struct rval_expr* rve; - struct match_cond_table* mct; - struct rvalue* rv; - struct rvalue* rv1; + struct switch_cond_table *sct; + struct switch_jmp_table *sjt; + struct rval_expr *rve; + struct match_cond_table *mct; + struct rvalue *rv; + struct rvalue *rv1; struct rval_cache c1; str s; void *srevp[2]; @@ -332,45 +331,43 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg) functions to return with error (status<0) and not setting it leaving there previous error; cache the previous value though for functions which want to process it */ - prev_ser_error=ser_error; - ser_error=E_UNSPEC; + prev_ser_error = ser_error; + ser_error = E_UNSPEC; /* hook for every executed action (in use by cfg debugger) */ - if(unlikely(sr_event_enabled(SREV_CFG_RUN_ACTION))) - { - srevp[0] = (void*)a; - srevp[1] = (void*)msg; - evp.data = (void*)srevp; + if(unlikely(sr_event_enabled(SREV_CFG_RUN_ACTION))) { + srevp[0] = (void *)a; + srevp[1] = (void *)msg; + evp.data = (void *)srevp; sr_event_exec(SREV_CFG_RUN_ACTION, &evp); } - ret=E_BUG; - switch ((unsigned char)a->type){ + ret = E_BUG; + switch((unsigned char)a->type) { case DROP_T: - switch(a->val[0].type){ - case NUMBER_ST: - ret=(int) a->val[0].u.number; - break; - case RVE_ST: - rve=(struct rval_expr*)a->val[0].u.data; - if(rval_expr_eval_long(h, msg, &li, rve)<0) { - LM_WARN("failed to eval int expression\n"); - } - ret = (int)li; - break; - case RETCODE_ST: - ret=h->last_retcode; - break; - default: - BUG("unexpected subtype %d in DROP_T\n", - a->val[0].type); - ret=0; - goto error; - } - h->run_flags|=(unsigned int)a->val[1].u.number; - if(unlikely((ret==0) && (h->run_flags&RETURN_R_F))) { - h->run_flags |= EXIT_R_F; - } + switch(a->val[0].type) { + case NUMBER_ST: + ret = (int)a->val[0].u.number; + break; + case RVE_ST: + rve = (struct rval_expr *)a->val[0].u.data; + if(rval_expr_eval_long(h, msg, &li, rve) < 0) { + LM_WARN("failed to eval int expression\n"); + } + ret = (int)li; + break; + case RETCODE_ST: + ret = h->last_retcode; + break; + default: + BUG("unexpected subtype %d in DROP_T\n", a->val[0].type); + ret = 0; + goto error; + } + h->run_flags |= (unsigned int)a->val[1].u.number; + if(unlikely((ret == 0) && (h->run_flags & RETURN_R_F))) { + h->run_flags |= EXIT_R_F; + } break; case FORWARD_T: #ifdef USE_TCP @@ -385,59 +382,63 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg) case FORWARD_UDP_T: /* init dst */ init_dest_info(&dst); - if (a->type==FORWARD_UDP_T) dst.proto=PROTO_UDP; + if(a->type == FORWARD_UDP_T) + dst.proto = PROTO_UDP; #ifdef USE_TCP - else if (a->type==FORWARD_TCP_T) { - dst.proto= PROTO_TCP; + else if(a->type == FORWARD_TCP_T) { + dst.proto = PROTO_TCP; if(msg->msg_flags & FL_USE_OTCPID) { dst.id = msg->otcpid; } } #endif #ifdef USE_TLS - else if (a->type==FORWARD_TLS_T) { - dst.proto= PROTO_TLS; + else if(a->type == FORWARD_TLS_T) { + dst.proto = PROTO_TLS; if(msg->msg_flags & FL_USE_OTCPID) { dst.id = msg->otcpid; } } #endif #ifdef USE_SCTP - else if (a->type==FORWARD_SCTP_T) dst.proto=PROTO_SCTP; + else if(a->type == FORWARD_SCTP_T) + dst.proto = PROTO_SCTP; #endif - else dst.proto=PROTO_NONE; - if (a->val[0].type==URIHOST_ST){ + else + dst.proto = PROTO_NONE; + if(a->val[0].type == URIHOST_ST) { /*parse uri*/ - if (msg->dst_uri.len) { - ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len, - &next_hop); + if(msg->dst_uri.len) { + ret = parse_uri( + msg->dst_uri.s, msg->dst_uri.len, &next_hop); u = &next_hop; } else { ret = parse_sip_msg_uri(msg); u = &msg->parsed_uri; } - if (ret<0) { + if(ret < 0) { LM_ERR("forward: bad_uri dropping packet\n"); goto error; } - switch (a->val[1].type){ + switch(a->val[1].type) { case URIPORT_ST: - port=u->port_no; - break; + port = u->port_no; + break; case NUMBER_ST: - port=a->val[1].u.number; - break; + port = a->val[1].u.number; + break; default: - LM_CRIT("bad forward 2nd param type (%d)\n", a->val[1].type); - ret=E_UNSPEC; - goto error_fwd_uri; + LM_CRIT("bad forward 2nd param type (%d)\n", + a->val[1].type); + ret = E_UNSPEC; + goto error_fwd_uri; } - if (dst.proto == PROTO_NONE){ /* only if proto not set get it + if(dst.proto == PROTO_NONE) { /* only if proto not set get it from the uri */ - switch(u->proto){ + switch(u->proto) { case PROTO_NONE: /*dst.proto=PROTO_UDP; */ /* no proto, try to get it from the dns */ @@ -454,171 +455,171 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg) #ifdef USE_SCTP case PROTO_SCTP: #endif - dst.proto=u->proto; + dst.proto = u->proto; break; default: LM_ERR("forward: bad uri transport %d\n", u->proto); - ret=E_BAD_PROTO; + ret = E_BAD_PROTO; goto error_fwd_uri; } #ifdef USE_TLS - if (u->type==SIPS_URI_T){ - if (u->proto==PROTO_UDP){ - LM_ERR("forward: secure uri incompatible with transport %d\n", + if(u->type == SIPS_URI_T) { + if(u->proto == PROTO_UDP) { + LM_ERR("forward: secure uri incompatible with " + "transport %d\n", u->proto); - ret=E_BAD_PROTO; + ret = E_BAD_PROTO; goto error_fwd_uri; - } else if (u->proto!=PROTO_WSS) - dst.proto=PROTO_TLS; + } else if(u->proto != PROTO_WSS) + dst.proto = PROTO_TLS; else - dst.proto=PROTO_WSS; + dst.proto = PROTO_WSS; } #endif } #ifdef HONOR_MADDR - if (u->maddr_val.s && u->maddr_val.len) - dst_host=&u->maddr_val; + if(u->maddr_val.s && u->maddr_val.len) + dst_host = &u->maddr_val; else #endif - dst_host=&u->host; + dst_host = &u->host; #ifdef USE_COMP - dst.comp=u->comp; + dst.comp = u->comp; #endif - ret=forward_request(msg, dst_host, port, &dst); - if (ret>=0){ - ret=1; + ret = forward_request(msg, dst_host, port, &dst); + if(ret >= 0) { + ret = 1; } - }else if ((a->val[0].type==PROXY_ST) && (a->val[1].type==NUMBER_ST)){ - if (dst.proto==PROTO_NONE) - dst.proto=msg->rcv.proto; - proxy2su(&dst.to, (struct proxy_l*)a->val[0].u.data); - ret=forward_request(msg, 0, 0, &dst); - if (ret>=0){ - ret=1; - proxy_mark((struct proxy_l*)a->val[0].u.data, ret); - }else if (ser_error!=E_OK){ - proxy_mark((struct proxy_l*)a->val[0].u.data, ret); + } else if((a->val[0].type == PROXY_ST) + && (a->val[1].type == NUMBER_ST)) { + if(dst.proto == PROTO_NONE) + dst.proto = msg->rcv.proto; + proxy2su(&dst.to, (struct proxy_l *)a->val[0].u.data); + ret = forward_request(msg, 0, 0, &dst); + if(ret >= 0) { + ret = 1; + proxy_mark((struct proxy_l *)a->val[0].u.data, ret); + } else if(ser_error != E_OK) { + proxy_mark((struct proxy_l *)a->val[0].u.data, ret); } - }else{ - LM_CRIT("bad forward() types %d, %d\n", - a->val[0].type, a->val[1].type); - ret=E_BUG; + } else { + LM_CRIT("bad forward() types %d, %d\n", a->val[0].type, + a->val[1].type); + ret = E_BUG; goto error; } break; case LOG_T: - if ((a->val[0].type!=NUMBER_ST)|(a->val[1].type!=STRING_ST)){ - LM_CRIT("bad log() types %d, %d\n", - a->val[0].type, a->val[1].type); - ret=E_BUG; + if((a->val[0].type != NUMBER_ST) | (a->val[1].type != STRING_ST)) { + LM_CRIT("bad log() types %d, %d\n", a->val[0].type, + a->val[1].type); + ret = E_BUG; goto error; } LOG_FN(DEFAULT_FACILITY, a->val[0].u.number, "