5 * Copyright (C) 2001-2003 FhG Fokus
7 * This file is part of ser, a free SIP server.
9 * ser is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version
14 * For a license to use the ser software under conditions
15 * other than those described here, or to purchase support for this
16 * software, please contact iptel.org by e-mail at the following addresses:
19 * ser is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * 2003-02-28 scratchpad compatibility abandoned (jiri)
31 * 2003-01-29 removed scratchpad (jiri)
32 * 2003-03-19 fixed set* len calculation bug & simplified a little the code
33 * (should be a little faster now) (andrei)
34 * replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
35 * 2003-04-01 Added support for loose routing in forward (janakj)
36 * 2003-04-12 FORCE_RPORT_T added (andrei)
37 * 2003-04-22 strip_tail added (jiri)
38 * 2003-10-02 added SET_ADV_ADDR_T & SET_ADV_PORT_T (andrei)
39 * 2003-10-29 added FORCE_TCP_ALIAS_T (andrei)
40 * 2004-11-30 added FORCE_SEND_SOCKET_T (andrei)
41 * 2005-12-12 return & drop/exit differentiation (andrei)
42 * 2005-12-19 select framework (mma)
43 * 2006-04-12 updated *_send() calls to use a struct dest_info (andrei)
44 * 2006-07-27 dns cache and dns based send address failover support (andrei)
45 * 2006-12-06 on popular request last_retcode set also by module functions
47 * 2007-06-14 run_actions & do_action need a ctx or handle now, no more
48 * static vars (andrei)
49 * 2008-11-18 support for variable parameter module functions (andrei)
50 * 2008-12-03 use lvalues/rvalues for assignments (andrei)
51 * 2008-12-17 added UDP_MTU_TRY_PROTO_T (andrei)
52 * 2009-05-04 switched IF_T to rval_expr (andrei)
53 * 2009-09-15 added SET_{FWD,RPL}_NO_CONNECT, SET_{FWD,RPL}_CLOSE (andrei)
58 * \brief SIP-router core ::
65 #include "comp_defs.h"
73 #include "udp_server.h"
75 #include "parser/msg_parser.h"
76 #include "parser/parse_uri.h"
79 #include "sr_module.h"
80 #include "select_buf.h"
87 #include "tcp_server.h"
90 #include "sctp_server.h"
95 #include <sys/types.h>
96 #include <sys/socket.h>
99 #include <netinet/in.h>
100 #include <arpa/inet.h>
108 int _last_returned_code = 0;
109 struct onsend_info* p_onsend=0; /* onsend route send info */
113 /* handle the exit code of a module function call.
114 * (used internally in do_action())
115 * @param h - script handle (h->last_retcode and h->run_flags will be set).
116 * @param ret - module function (v0 or v2) retcode
117 * Side-effects: sets _last_returned_code
119 #define MODF_HANDLE_RETCODE(h, ret) \
121 /* if (unlikely((ret)==0)) (h)->run_flags|=EXIT_R_F; */ \
122 (h)->run_flags |= EXIT_R_F & (((ret) != 0) -1); \
123 (h)->last_retcode=(ret); \
124 _last_returned_code = (h)->last_retcode; \
129 /* frees parameters converted using MODF_RVE_PARAM_CONVERT() from dst.
130 * (used internally in do_action())
131 * Assumes src is unchanged.
132 * Side-effects: clobbers i (int).
134 #define MODF_RVE_PARAM_FREE(src, dst) \
135 for (i=0; i < (dst)[1].u.number; i++) { \
136 if ((src)[i+2].type == RVE_ST && (dst)[i+2].u.string) { \
137 pkg_free((dst)[i+2].u.string); \
138 (dst)[i+2].u.string = 0; \
143 /* fills dst from src, converting RVE_ST params to STRING_ST.
144 * (used internally in do_action())
145 * @param src - source action_u_t array, as in the action structure
146 * @param dst - destination action_u_t array, will be filled from src.
147 * WARNING: dst must be cleaned when done, use MODULE_RVE_PARAM_FREE()
148 * Side-effects: clobbers i (int), s (str), rv (rvalue*), might jump to error.
150 #define MODF_RVE_PARAM_CONVERT(h, msg, src, dst) \
153 for (i=0; i < (src)[1].u.number; i++) { \
154 if ((src)[2+i].type == RVE_ST) { \
155 rv=rval_expr_eval((h), (msg), (src)[i+2].u.data); \
156 if (unlikely(rv == 0 || \
157 rval_get_str((h), (msg), &s, rv, 0) < 0)) { \
159 ERR("failed to convert RVE to string\n"); \
160 (dst)[1].u.number = i; \
161 MODF_RVE_PARAM_FREE(src, dst); \
164 (dst)[i+2].type = STRING_ST; \
165 (dst)[i+2].u.string = s.s; \
166 (dst)[i+2].u.str.len = s.len; \
169 (dst)[i+2]=(src)[i+2]; \
175 /* call a module function with normal STRING_ST params.
176 * (used internally in do_action())
177 * @param f_type - cmd_function type
180 * @param src - source action_u_t array (e.g. action->val)
181 * @param params... - variable list of parameters, passed to the module
183 * Side-effects: sets ret, clobbers i (int), s (str), rv (rvalue*), f,
184 * might jump to error.
188 #define MODF_CALL(f_type, h, msg, src, ...) \
190 f=((union cmd_export_u*)(src)[0].u.data)->c.function; \
191 ret=((f_type)f)((msg), __VAR_ARGS__); \
192 MODF_HANDLE_RETCODE(h, ret); \
194 #else /* ! __SUNPRO_C (gcc, icc a.s.o) */
195 #define MODF_CALL(f_type, h, msg, src, params...) \
197 f=((union cmd_export_u*)(src)[0].u.data)->c.function; \
198 ret=((f_type)f)((msg), ## params ); \
199 MODF_HANDLE_RETCODE(h, ret); \
201 #endif /* __SUNPRO_C */
205 /* call a module function with possible RVE params.
206 * (used internally in do_action())
207 * @param f_type - cmd_function type
210 * @param src - source action_u_t array (e.g. action->val)
211 * @param dst - temporary action_u_t array used for conversions. It can be
212 * used for the function parameters. It's contents it's not
213 * valid after the call.
214 * @param params... - variable list of parameters, passed to the module
216 * Side-effects: sets ret, clobbers i (int), s (str), rv (rvalue*), f, dst,
217 * might jump to error.
221 #define MODF_RVE_CALL(f_type, h, msg, src, dst, ...) \
223 f=((union cmd_export_u*)(src)[0].u.data)->c.function; \
224 MODF_RVE_PARAM_CONVERT(h, msg, src, dst); \
225 ret=((f_type)f)((msg), __VAR_ARGS__); \
226 MODF_HANDLE_RETCODE(h, ret); \
227 /* free strings allocated by us */ \
228 MODF_RVE_PARAM_FREE(src, dst); \
230 #else /* ! __SUNPRO_C (gcc, icc a.s.o) */
231 #define MODF_RVE_CALL(f_type, h, msg, src, dst, params...) \
233 f=((union cmd_export_u*)(src)[0].u.data)->c.function; \
234 MODF_RVE_PARAM_CONVERT(h, msg, src, dst); \
235 ret=((f_type)f)((msg), ## params ); \
236 MODF_HANDLE_RETCODE(h, ret); \
237 /* free strings allocated by us */ \
238 MODF_RVE_PARAM_FREE(src, dst); \
240 #endif /* __SUNPRO_C */
243 /* ret= 0! if action -> end of list(e.g DROP),
244 > 0 to continue processing next actions
246 int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
250 struct dest_info dst;
252 char *new_uri, *end, *crt;
256 struct sip_uri uri, next_hop;
261 struct switch_cond_table* sct;
262 struct switch_jmp_table* sjt;
263 struct rval_expr* rve;
264 struct match_cond_table* mct;
267 struct rval_cache c1;
270 /* temporary storage space for a struct action.val[] working copy
271 (needed to transform RVE intro STRING before calling module
272 functions). [0] is not used (corresp. to the module export pointer),
273 [1] contains the number of params, and [2..] the param values.
274 We need [1], because some fixup function use it
275 (see fixup_get_param_count()). */
276 static action_u_t mod_f_params[MAX_ACTIONS];
278 /* reset the value of error to E_UNSPEC so avoid unknowledgable
279 functions to return with error (status<0) and not setting it
280 leaving there previous error; cache the previous value though
281 for functions which want to process it */
282 prev_ser_error=ser_error;
285 /* hook for every executed action (in use by cfg debugger) */
286 if(unlikely(sr_event_enabled(SREV_CFG_RUN_ACTION)))
289 srevp[1] = (void*)msg;
290 sr_event_exec(SREV_CFG_RUN_ACTION, (void*)srevp);
294 switch ((unsigned char)a->type){
296 switch(a->val[0].type){
298 ret=(int) a->val[0].u.number;
301 rve=(struct rval_expr*)a->val[0].u.data;
302 rval_expr_eval_int(h, msg, &ret, rve);
308 BUG("unexpected subtype %d in DROP_T\n",
313 h->run_flags|=(unsigned int)a->val[1].u.number;
327 init_dest_info(&dst);
328 if (a->type==FORWARD_UDP_T) dst.proto=PROTO_UDP;
330 else if (a->type==FORWARD_TCP_T) dst.proto= PROTO_TCP;
333 else if (a->type==FORWARD_TLS_T) dst.proto= PROTO_TLS;
336 else if (a->type==FORWARD_SCTP_T) dst.proto=PROTO_SCTP;
338 else dst.proto=PROTO_NONE;
339 if (a->val[0].type==URIHOST_ST){
342 if (msg->dst_uri.len) {
343 ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len,
347 ret = parse_sip_msg_uri(msg);
348 u = &msg->parsed_uri;
352 LOG(L_ERR, "ERROR: do_action: forward: bad_uri "
353 " dropping packet\n");
357 switch (a->val[1].type){
362 port=a->val[1].u.number;
365 LOG(L_CRIT, "BUG: do_action bad forward 2nd"
366 " param type (%d)\n", a->val[1].type);
370 if (dst.proto == PROTO_NONE){ /* only if proto not set get it
374 /*dst.proto=PROTO_UDP; */
375 /* no proto, try to get it from the dns */
390 LOG(L_ERR,"ERROR: do action: forward: bad uri"
391 " transport %d\n", u->proto);
396 if (u->type==SIPS_URI_T){
397 if (u->proto==PROTO_UDP){
398 LOG(L_ERR, "ERROR: do_action: forward: secure uri"
399 " incompatible with transport %d\n",
410 if (u->maddr_val.s && u->maddr_val.len)
411 dst_host=&u->maddr_val;
418 ret=forward_request(msg, dst_host, port, &dst);
422 }else if ((a->val[0].type==PROXY_ST) && (a->val[1].type==NUMBER_ST)){
423 if (dst.proto==PROTO_NONE)
424 dst.proto=msg->rcv.proto;
425 proxy2su(&dst.to, (struct proxy_l*)a->val[0].u.data);
426 ret=forward_request(msg, 0, 0, &dst);
429 proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
430 }else if (ser_error!=E_OK){
431 proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
434 LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
435 a->val[0].type, a->val[1].type);
442 if ((a->val[0].type!= PROXY_ST)|(a->val[1].type!=NUMBER_ST)){
443 LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
444 a->val[0].type, a->val[1].type);
449 init_dest_info(&dst);
450 ret=proxy2su(&dst.to, (struct proxy_l*)a->val[0].u.data);
459 if (a->type==SEND_T){
461 dst.proto=PROTO_UDP; /* not really needed for udp_send */
462 dst.send_sock=get_send_socket(msg, &dst.to, PROTO_UDP);
463 if (dst.send_sock!=0){
464 ret=udp_send(&dst, tmp, len);
474 ret=tcp_send(&dst, 0, tmp, len);
481 proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
486 if ((a->val[0].type!=NUMBER_ST)|(a->val[1].type!=STRING_ST)){
487 LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
488 a->val[0].type, a->val[1].type);
492 LOG_(DEFAULT_FACILITY, a->val[0].u.number, "<script>: ", "%s",
497 /* jku -- introduce a new branch */
498 case APPEND_BRANCH_T:
499 if (unlikely(a->val[0].type!=STR_ST)) {
500 LOG(L_CRIT, "BUG: do_action: bad append_branch_t %d\n",
505 getbflagsval(0, (flag_t*)&flags);
506 ret=append_branch(msg, &a->val[0].u.str, &msg->dst_uri,
507 &msg->path_vec, a->val[1].u.number,
508 (flag_t)flags, msg->force_send_socket);
511 /* jku begin: is_length_greater_than */
513 if (a->val[0].type!=NUMBER_ST) {
514 LOG(L_CRIT, "BUG: do_action: bad len_gt type %d\n",
519 /* DBG("XXX: message length %d, max %d\n",
520 msg->len, a->val[0].u.number ); */
521 ret = msg->len >= a->val[0].u.number ? 1 : -1;
523 /* jku end: is_length_greater_than */
525 /* jku - begin : flag processing */
528 if (a->val[0].type!=NUMBER_ST) {
529 LOG(L_CRIT, "BUG: do_action: bad setflag() type %d\n",
534 if (!flag_in_range( a->val[0].u.number )) {
538 setflag( msg, a->val[0].u.number );
543 if (a->val[0].type!=NUMBER_ST) {
544 LOG(L_CRIT, "BUG: do_action: bad resetflag() type %d\n",
549 if (!flag_in_range( a->val[0].u.number )) {
553 resetflag( msg, a->val[0].u.number );
558 if (a->val[0].type!=NUMBER_ST) {
559 LOG(L_CRIT, "BUG: do_action: bad isflagset() type %d\n",
564 if (!flag_in_range( a->val[0].u.number )) {
568 ret=isflagset( msg, a->val[0].u.number );
570 /* jku - end : flag processing */
572 case AVPFLAG_OPER_T: {
573 struct search_state st;
577 flag = a->val[1].u.number;
578 if ((a->val[0].u.attr->type & AVP_INDEX_ALL) == AVP_INDEX_ALL || (a->val[0].u.attr->type & AVP_NAME_RE)!=0) {
579 for (avp=search_first_avp(a->val[0].u.attr->type, a->val[0].u.attr->name, NULL, &st); avp; avp = search_next_avp(&st, NULL)) {
580 switch (a->val[2].u.number) { /* oper: 0..reset, 1..set, -1..no change */
582 avp->flags &= ~(avp_flags_t)a->val[1].u.number;
585 avp->flags |= (avp_flags_t)a->val[1].u.number;
589 ret = ret || ((avp->flags & (avp_flags_t)a->val[1].u.number) != 0);
593 avp = search_avp_by_index(a->val[0].u.attr->type, a->val[0].u.attr->name, NULL, a->val[0].u.attr->index);
595 switch (a->val[2].u.number) { /* oper: 0..reset, 1..set, -1..no change */
597 avp->flags &= ~(avp_flags_t)a->val[1].u.number;
600 avp->flags |= (avp_flags_t)a->val[1].u.number;
604 ret = (avp->flags & (avp_flags_t)a->val[1].u.number) != 0;
612 if ((a->val[0].type!=STRING_ST)|(a->val[1].type!=STRING_ST)){
613 LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
614 a->val[0].type, a->val[1].type);
618 LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
619 "not implemented yet\n", a->val[0].u.string, a->val[1].u.string);
623 if (a->val[0].type!=NUMBER_ST){
624 LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
629 if ((a->val[0].u.number>=main_rt.idx)||(a->val[0].u.number<0)){
630 LOG(L_ERR, "ERROR: invalid routing table number in"
631 "route(%lu)\n", a->val[0].u.number);
635 /*ret=((ret=run_actions(rlist[a->val[0].u.number], msg))<0)?ret:1;*/
636 ret=run_actions(h, main_rt.rlist[a->val[0].u.number], msg);
638 _last_returned_code = h->last_retcode;
639 h->run_flags&=~(RETURN_R_F|BREAK_R_F); /* absorb return & break */
642 if (a->val[0].type!=STRING_ST){
643 LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
648 LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
649 " using dumb version...\n", a->val[0].u.string);
650 ret=system(a->val[0].u.string);
652 LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
657 if (msg->new_uri.s) {
658 pkg_free(msg->new_uri.s);
661 msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
667 case SET_HOSTPORTTRANS_T:
676 case SET_USERPHONE_T:
678 if (a->type==STRIP_T || a->type==STRIP_TAIL_T) {
679 if (a->val[0].type!=NUMBER_ST) {
680 LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
685 } else if (a->type!=SET_USERPHONE_T) {
686 if (a->val[0].type!=STRING_ST) {
687 LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
693 if (a->type==SET_URI_T){
694 if (msg->new_uri.s) {
695 pkg_free(msg->new_uri.s);
698 msg->parsed_uri_ok=0;
699 len=strlen(a->val[0].u.string);
700 msg->new_uri.s=pkg_malloc(len+1);
701 if (msg->new_uri.s==0){
702 LOG(L_ERR, "ERROR: do_action: memory allocation"
707 memcpy(msg->new_uri.s, a->val[0].u.string, len);
708 msg->new_uri.s[len]=0;
709 msg->new_uri.len=len;
714 if (msg->parsed_uri_ok==0) {
715 if (msg->new_uri.s) {
717 len=msg->new_uri.len;
719 tmp=msg->first_line.u.request.uri.s;
720 len=msg->first_line.u.request.uri.len;
722 if (parse_uri(tmp, len, &uri)<0){
723 LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
732 /* skip SET_USERPHONE_T action if the URI is already
733 * a tel: or tels: URI, or contains the user=phone param */
734 if ((a->type==SET_USERPHONE_T)
735 && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T)
736 || ((uri.user_param_val.len==5) && (memcmp(uri.user_param_val.s, "phone", 5)==0)))
741 /* SET_PORT_T does not work with tel: URIs */
742 if ((a->type==SET_PORT_T)
743 && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
744 && ((uri.flags & URI_SIP_USER_PHONE)==0)
746 LOG(L_ERR, "ERROR: do_action: port number of a tel: URI cannot be set\n");
751 new_uri=pkg_malloc(MAX_URI_SIZE);
753 LOG(L_ERR, "ERROR: do_action: memory allocation "
758 end=new_uri+MAX_URI_SIZE;
761 /* Preserve the URI scheme unless the host part needs
762 * to be rewritten, and the shceme is tel: or tels: */
775 if ((uri.flags & URI_SIP_USER_PHONE)
776 || (a->type==SET_HOST_T)
777 || (a->type==SET_HOSTPORT_T)
778 || (a->type==SET_HOSTPORTTRANS_T)
789 if ((uri.flags & URI_SIP_USER_PHONE)
790 || (a->type==SET_HOST_T)
791 || (a->type==SET_HOSTPORT_T)
792 || (a->type==SET_HOSTPORTTRANS_T)
803 LOG(L_ERR, "ERROR: Unsupported URI scheme (%d), "
804 "reverted to sip:\n",
809 if(crt+len+1 /* colon */ >end) goto error_uri;
810 memcpy(crt,tmp,len);crt+=len;
816 if (a->type==PREFIX_T) {
817 tmp=a->val[0].u.string;
818 len=strlen(tmp); if(crt+len>end) goto error_uri;
819 memcpy(crt,tmp,len);crt+=len;
820 /* whatever we had before, with prefix we have username
825 if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
826 tmp=a->val[0].u.string;
828 } else if (a->type==STRIP_T) {
829 if (a->val[0].u.number>uri.user.len) {
830 LOG(L_WARN, "Error: too long strip asked; "
831 " deleting username: %lu of <%.*s>\n",
832 a->val[0].u.number, uri.user.len, uri.user.s );
834 } else if (a->val[0].u.number==uri.user.len) {
837 tmp=uri.user.s + a->val[0].u.number;
838 len=uri.user.len - a->val[0].u.number;
840 } else if (a->type==STRIP_TAIL_T) {
841 if (a->val[0].u.number>uri.user.len) {
842 LOG(L_WARN, "WARNING: too long strip_tail asked; "
843 " deleting username: %lu of <%.*s>\n",
844 a->val[0].u.number, uri.user.len, uri.user.s );
846 } else if (a->val[0].u.number==uri.user.len) {
850 len=uri.user.len - a->val[0].u.number;
858 if(crt+len>end) goto error_uri;
859 memcpy(crt,tmp,len);crt+=len;
860 user=1; /* we have an user field so mark it */
863 if (a->type==SET_USERPASS_T) tmp=0;
864 else tmp=uri.passwd.s;
867 len=uri.passwd.len; if(crt+len+1>end) goto error_uri;
869 memcpy(crt,tmp,len);crt+=len;
871 /* tel: URI parameters */
872 if ((uri.type==TEL_URI_T)
873 || (uri.type==TELS_URI_T)
877 len=uri.params.len; if(crt+len+1>end) goto error_uri;
879 memcpy(crt,tmp,len);crt+=len;
883 if ((a->type==SET_HOST_T)
884 || (a->type==SET_HOSTPORT_T)
885 || (a->type==SET_HOSTALL_T)
886 || (a->type==SET_HOSTPORTTRANS_T)
888 tmp=a->val[0].u.string;
889 if (tmp) len = strlen(tmp);
891 } else if ((uri.type==SIP_URI_T)
892 || (uri.type==SIPS_URI_T)
893 || (uri.flags & URI_SIP_USER_PHONE)
901 if (user) { /* add @ */
902 if(crt+1>end) goto error_uri;
905 if(crt+len>end) goto error_uri;
906 memcpy(crt,tmp,len);crt+=len;
908 if(a->type==SET_HOSTALL_T)
911 if ((a->type==SET_HOSTPORT_T)
912 || (a->type==SET_HOSTPORTTRANS_T))
914 else if (a->type==SET_PORT_T) {
915 tmp=a->val[0].u.string;
916 if (tmp) len = strlen(tmp);
923 if(crt+len+1>end) goto error_uri;
925 memcpy(crt,tmp,len);crt+=len;
928 if ((a->type==SET_HOSTPORTTRANS_T)
932 /* bypass the transport parameter */
933 if (uri.sip_params.s < uri.transport.s) {
934 /* there are parameters before transport */
935 len = uri.transport.s - uri.sip_params.s - 1;
936 /* ignore the ';' at the end */
937 if (crt+len+1>end) goto error_uri;
939 memcpy(crt,uri.sip_params.s,len);crt+=len;
941 len = (uri.sip_params.s + uri.sip_params.len) -
942 (uri.transport.s + uri.transport.len);
944 /* there are parameters after transport */
945 if (crt+len>end) goto error_uri;
946 tmp = uri.transport.s + uri.transport.len;
947 memcpy(crt,tmp,len);crt+=len;
950 tmp=uri.sip_params.s;
952 len=uri.sip_params.len; if(crt+len+1>end) goto error_uri;
954 memcpy(crt,tmp,len);crt+=len;
957 /* Add the user=phone param if a tel: or tels:
958 * URI was converted to sip: or sips:.
959 * (host part of a tel/tels URI was set.)
960 * Or in case of sip: URI and SET_USERPHONE_T action */
961 if (((((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
962 && ((uri.flags & URI_SIP_USER_PHONE)==0))
963 && ((a->type==SET_HOST_T)
964 || (a->type==SET_HOSTPORT_T)
965 || (a->type==SET_HOSTPORTTRANS_T)))
966 || (a->type==SET_USERPHONE_T)
970 if(crt+len>end) goto error_uri;
971 memcpy(crt,tmp,len);crt+=len;
976 len=uri.headers.len; if(crt+len+1>end) goto error_uri;
978 memcpy(crt,tmp,len);crt+=len;
981 *crt=0; /* null terminate the thing */
982 /* copy it to the msg */
983 if (msg->new_uri.s) pkg_free(msg->new_uri.s);
984 msg->new_uri.s=new_uri;
985 msg->new_uri.len=crt-new_uri;
986 msg->parsed_uri_ok=0;
990 rve=(struct rval_expr*)a->val[0].u.data;
991 if (unlikely(rval_expr_eval_int(h, msg, &v, rve) != 0)){
992 ERR("if expression evaluation failed (%d,%d-%d,%d)\n",
993 rve->fpos.s_line, rve->fpos.s_col,
994 rve->fpos.e_line, rve->fpos.e_col);
997 if (unlikely(h->run_flags & EXIT_R_F)){
1001 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
1003 ret=1; /*default is continue */
1005 if ((a->val[1].type==ACTIONS_ST)&&a->val[1].u.data){
1007 (struct action*)a->val[1].u.data, msg);
1009 }else if ((a->val[2].type==ACTIONS_ST)&&a->val[2].u.data){
1011 (struct action*)a->val[2].u.data, msg);
1015 MODF_CALL(cmd_function, h, msg, a->val, 0, 0);
1017 /* instead of using the parameter number, we use different names
1018 * for calls to functions with 3, 4, 5, 6 or variable number of
1019 * parameters due to performance reasons */
1021 MODF_CALL(cmd_function, h, msg, a->val,
1022 (char*)a->val[2].u.data,
1027 MODF_CALL(cmd_function, h, msg, a->val,
1028 (char*)a->val[2].u.data,
1029 (char*)a->val[3].u.data
1033 MODF_CALL(cmd_function3, h, msg, a->val,
1034 (char*)a->val[2].u.data,
1035 (char*)a->val[3].u.data,
1036 (char*)a->val[4].u.data
1040 MODF_CALL(cmd_function4, h, msg, a->val,
1041 (char*)a->val[2].u.data,
1042 (char*)a->val[3].u.data,
1043 (char*)a->val[4].u.data,
1044 (char*)a->val[5].u.data
1048 MODF_CALL(cmd_function5, h, msg, a->val,
1049 (char*)a->val[2].u.data,
1050 (char*)a->val[3].u.data,
1051 (char*)a->val[4].u.data,
1052 (char*)a->val[5].u.data,
1053 (char*)a->val[6].u.data
1057 MODF_CALL(cmd_function6, h, msg, a->val,
1058 (char*)a->val[2].u.data,
1059 (char*)a->val[3].u.data,
1060 (char*)a->val[4].u.data,
1061 (char*)a->val[5].u.data,
1062 (char*)a->val[6].u.data,
1063 (char*)a->val[7].u.data
1067 MODF_CALL(cmd_function_var, h, msg, a->val,
1068 a->val[1].u.number, &a->val[2]);
1071 MODF_RVE_CALL(cmd_function, h, msg, a->val, mod_f_params,
1072 (char*)mod_f_params[2].u.data,
1077 MODF_RVE_CALL(cmd_function, h, msg, a->val, mod_f_params,
1078 (char*)mod_f_params[2].u.data,
1079 (char*)mod_f_params[3].u.data
1083 MODF_RVE_CALL(cmd_function3, h, msg, a->val, mod_f_params,
1084 (char*)mod_f_params[2].u.data,
1085 (char*)mod_f_params[3].u.data,
1086 (char*)mod_f_params[4].u.data
1090 MODF_RVE_CALL(cmd_function4, h, msg, a->val, mod_f_params,
1091 (char*)mod_f_params[2].u.data,
1092 (char*)mod_f_params[3].u.data,
1093 (char*)mod_f_params[4].u.data,
1094 (char*)mod_f_params[5].u.data
1098 MODF_RVE_CALL(cmd_function5, h, msg, a->val, mod_f_params,
1099 (char*)mod_f_params[2].u.data,
1100 (char*)mod_f_params[3].u.data,
1101 (char*)mod_f_params[4].u.data,
1102 (char*)mod_f_params[5].u.data,
1103 (char*)mod_f_params[6].u.data
1107 MODF_RVE_CALL(cmd_function6, h, msg, a->val, mod_f_params,
1108 (char*)mod_f_params[2].u.data,
1109 (char*)mod_f_params[3].u.data,
1110 (char*)mod_f_params[4].u.data,
1111 (char*)mod_f_params[5].u.data,
1112 (char*)mod_f_params[6].u.data,
1113 (char*)mod_f_params[7].u.data
1117 MODF_RVE_CALL(cmd_function_var, h, msg, a->val, mod_f_params,
1118 a->val[1].u.number, &mod_f_params[2]);
1121 /* only eval the expression to account for possible
1123 rval_expr_eval_int(h, msg, &v,
1124 (struct rval_expr*)a->val[0].u.data);
1125 if (h->run_flags & EXIT_R_F){
1129 h->run_flags &= ~RETURN_R_F|BREAK_R_F; /* catch return & break in
1131 ret=1; /* default is continue */
1134 sct=(struct switch_cond_table*)a->val[1].u.data;
1135 if (unlikely( rval_expr_eval_int(h, msg, &v,
1136 (struct rval_expr*)a->val[0].u.data) <0)){
1137 /* handle error in expression => use default */
1141 if (h->run_flags & EXIT_R_F){
1145 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
1147 ret=1; /* default is continue */
1148 for(i=0; i<sct->n; i++)
1149 if (sct->cond[i]==v){
1150 if (likely(sct->jump[i])){
1151 ret=run_actions(h, sct->jump[i], msg);
1152 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1153 returns passthrough */
1159 ret=run_actions(h, sct->def, msg);
1160 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1161 returns passthrough */
1165 sjt=(struct switch_jmp_table*)a->val[1].u.data;
1166 if (unlikely( rval_expr_eval_int(h, msg, &v,
1167 (struct rval_expr*)a->val[0].u.data) <0)){
1168 /* handle error in expression => use default */
1172 if (h->run_flags & EXIT_R_F){
1176 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
1178 ret=1; /* default is continue */
1179 if (likely(v >= sjt->first && v <= sjt->last)){
1180 if (likely(sjt->tbl[v - sjt->first])){
1181 ret=run_actions(h, sjt->tbl[v - sjt->first], msg);
1182 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1183 returns passthrough */
1187 for(i=0; i<sjt->rest.n; i++)
1188 if (sjt->rest.cond[i]==v){
1189 if (likely(sjt->rest.jump[i])){
1190 ret=run_actions(h, sjt->rest.jump[i], msg);
1191 h->run_flags &= ~BREAK_R_F; /* catch breaks, but
1197 /* not found => try default */
1200 ret=run_actions(h, sjt->rest.def, msg);
1201 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1202 returns passthrough */
1206 if (likely(a->val[0].u.data)){
1207 ret=run_actions(h, (struct action*)a->val[0].u.data, msg);
1208 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1209 returns passthrough */
1213 mct=(struct match_cond_table*)a->val[1].u.data;
1214 rval_cache_init(&c1);
1217 ret=rval_expr_eval_rvint(h, msg, &rv, &v,
1218 (struct rval_expr*)a->val[0].u.data, &c1);
1220 if (unlikely( ret<0)){
1221 /* handle error in expression => use default */
1223 goto match_cond_def;
1225 if (h->run_flags & EXIT_R_F){
1229 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
1232 rv1=rval_convert(h, msg, RV_STR, rv, &c1);
1233 if (unlikely(rv1==0)){
1235 goto match_cond_def;
1239 /* int result in v */
1240 rval_cache_clean(&c1);
1241 s.s=sint2str(v, &s.len);
1243 ret=1; /* default is continue */
1244 for(i=0; i<mct->n; i++)
1245 if (( mct->match[i].type==MATCH_STR &&
1246 mct->match[i].l.s.len==s.len &&
1247 memcmp(mct->match[i].l.s.s, s.s, s.len) == 0 ) ||
1248 ( mct->match[i].type==MATCH_RE &&
1249 regexec(mct->match[i].l.regex, s.s, 0, 0, 0) == 0)
1251 if (likely(mct->jump[i])){
1252 ret=run_actions(h, mct->jump[i], msg);
1253 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1254 returns passthrough */
1260 ret=run_actions(h, mct->def, msg);
1261 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1262 returns passthrough */
1268 rval_cache_clean(&c1);
1271 rval_cache_clean(&c1);
1277 rve=(struct rval_expr*)a->val[0].u.data;
1279 while(!(flags & BREAK_R_F) &&
1280 (rval_expr_eval_int(h, msg, &v, rve) == 0) && v){
1282 if (unlikely(i > cfg_get(core, core_cfg, max_while_loops))){
1283 LOG(L_ERR, "ERROR: runaway while (%d, %d): more then"
1285 rve->fpos.s_line, rve->fpos.s_col,
1286 cfg_get(core, core_cfg, max_while_loops));
1290 if (likely(a->val[1].u.data)){
1291 ret=run_actions(h, (struct action*)a->val[1].u.data, msg);
1292 flags|=h->run_flags;
1293 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1294 returns passthrough */
1299 msg->msg_flags|=FL_FORCE_RPORT;
1300 ret=1; /* continue processing */
1302 case ADD_LOCAL_RPORT_T:
1303 msg->msg_flags|=FL_ADD_LOCAL_RPORT;
1304 ret=1; /* continue processing */
1306 case UDP_MTU_TRY_PROTO_T:
1307 msg->msg_flags|= (unsigned int)a->val[0].u.number & FL_MTU_FB_MASK;
1308 ret=1; /* continue processing */
1310 case SET_ADV_ADDR_T:
1311 if (a->val[0].type!=STR_ST){
1312 LOG(L_CRIT, "BUG: do_action: bad set_advertised_address() "
1313 "type %d\n", a->val[0].type);
1317 msg->set_global_address=*((str*)a->val[0].u.data);
1318 ret=1; /* continue processing */
1320 case SET_ADV_PORT_T:
1321 if (a->val[0].type!=STR_ST){
1322 LOG(L_CRIT, "BUG: do_action: bad set_advertised_port() "
1323 "type %d\n", a->val[0].type);
1327 msg->set_global_port=*((str*)a->val[0].u.data);
1328 ret=1; /* continue processing */
1331 case FORCE_TCP_ALIAS_T:
1332 if ( msg->rcv.proto==PROTO_TCP
1334 || msg->rcv.proto==PROTO_TLS
1338 if (a->val[0].type==NOSUBTYPE) port=msg->via1->port;
1339 else if (a->val[0].type==NUMBER_ST) port=(int)a->val[0].u.number;
1341 LOG(L_CRIT, "BUG: do_action: bad force_tcp_alias"
1342 " port type %d\n", a->val[0].type);
1347 if (tcpconn_add_alias(msg->rcv.proto_reserved1, port,
1348 msg->rcv.proto)!=0){
1349 LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n");
1355 ret=1; /* continue processing */
1357 case FORCE_SEND_SOCKET_T:
1358 if (a->val[0].type!=SOCKETINFO_ST){
1359 LOG(L_CRIT, "BUG: do_action: bad force_send_socket argument"
1360 " type: %d\n", a->val[0].type);
1364 set_force_socket(msg, (struct socket_info*)a->val[0].u.data);
1365 ret=1; /* continue processing */
1370 v=lval_assign(h, msg, (struct lvalue*)a->val[0].u.data,
1371 (struct rval_expr*)a->val[1].u.data);
1374 else if (unlikely (v == EXPR_DROP)) /* hack to quit on DROP*/
1379 case SET_FWD_NO_CONNECT_T:
1380 msg->fwd_send_flags.f|= SND_F_FORCE_CON_REUSE;
1381 ret=1; /* continue processing */
1383 case SET_RPL_NO_CONNECT_T:
1384 msg->rpl_send_flags.f|= SND_F_FORCE_CON_REUSE;
1385 ret=1; /* continue processing */
1387 case SET_FWD_CLOSE_T:
1388 msg->fwd_send_flags.f|= SND_F_CON_CLOSE;
1389 ret=1; /* continue processing */
1391 case SET_RPL_CLOSE_T:
1392 msg->rpl_send_flags.f|= SND_F_CON_CLOSE;
1393 ret=1; /* continue processing */
1397 LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
1404 LOG(L_ERR, "ERROR: do_action: set*: uri too long\n");
1405 if (new_uri) pkg_free(new_uri);
1406 LM_ERR("run action error at: %s:%d\n", (a->cfile)?a->cfile:"", a->cline);
1409 /*free_uri(&uri); -- not needed anymore, using msg->parsed_uri*/
1411 LM_ERR("run action error at: %s:%d\n", (a->cfile)?a->cfile:"", a->cline);
1417 /* returns: 0, or 1 on success, <0 on error */
1418 /* (0 if drop or break encountered, 1 if not ) */
1419 int run_actions(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
1423 struct sr_module *mod;
1427 if (h->rec_lev>ROUTE_MAX_REC_LEV){
1428 LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
1429 " giving up!\n", h->rec_lev);
1436 _last_returned_code = h->last_retcode;
1438 if (setjmp(h->jmp_env)){
1440 ret=h->last_retcode;
1447 DBG("DEBUG: run_actions: null action list (rec_level=%d)\n",
1452 for (t=a; t!=0; t=t->next){
1453 ret=do_action(h, t, msg);
1454 /* break, return or drop/exit stop execution of the current
1456 if (h->run_flags & (BREAK_R_F|RETURN_R_F|EXIT_R_F)){
1457 if (h->run_flags & EXIT_R_F){
1459 h->last_retcode=ret;
1460 _last_returned_code = h->last_retcode;
1461 longjmp(h->jmp_env, ret);
1466 /* ignore error returns */
1471 /* process module onbreak handlers if present */
1472 if (h->rec_lev==0 && ret==0)
1473 for (mod=modules;mod;mod=mod->next)
1474 if ((mod->mod_interface_ver==0) && mod->exports &&
1475 mod->exports->v0.onbreak_f) {
1476 mod->exports->v0.onbreak_f( msg );
1477 DBG("DEBUG: %s onbreak handler called\n",
1478 mod->exports->c.name);
1489 int run_top_route(struct action* a, sip_msg_t* msg, struct run_act_ctx *c)
1491 struct run_act_ctx ctx;
1492 struct run_act_ctx *p;
1499 reset_static_buffer();
1500 init_run_actions_ctx(p);
1501 ret = run_actions(p, a, msg);