core: fix another bunch of 'no real prototype' warnings, add doxygen docs
[sip-router] / action.c
1
2 /*
3  * $Id$
4  *
5  * Copyright (C) 2001-2003 FhG Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
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
13  *
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:
17  *    info@iptel.org
18  *
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.
23  *
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
27  *
28  * History:
29  * ---------
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
46  *              (andrei)
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)
54  *  2010-06-01  special hack/support for fparam fixups so that they can handle
55  *               variable RVEs (andrei)
56  */
57
58 /*!
59  * \file
60  * \brief SIP-router core :: 
61  * \ingroup core
62  * Module: \ref core
63  */
64
65
66
67 #include "comp_defs.h"
68
69 #include "action.h"
70 #include "config.h"
71 #include "error.h"
72 #include "dprint.h"
73 #include "proxy.h"
74 #include "forward.h"
75 #include "udp_server.h"
76 #include "route.h"
77 #include "parser/msg_parser.h"
78 #include "parser/parse_uri.h"
79 #include "ut.h"
80 #include "lvalue.h"
81 #include "sr_module.h"
82 #include "select_buf.h"
83 #include "mem/mem.h"
84 #include "globals.h"
85 #include "dset.h"
86 #include "onsend.h"
87 #include "resolve.h"
88 #ifdef USE_TCP
89 #include "tcp_server.h"
90 #endif
91 #ifdef USE_SCTP
92 #include "sctp_server.h"
93 #endif
94 #include "switch.h"
95 #include "events.h"
96 #include "cfg/cfg_struct.h"
97
98 #include <sys/types.h>
99 #include <sys/socket.h>
100 #include <netdb.h>
101 #include <stdlib.h>
102 #include <netinet/in.h>
103 #include <arpa/inet.h>
104 #include <string.h>
105
106
107 #ifdef DEBUG_DMALLOC
108 #include <dmalloc.h>
109 #endif
110
111 int _last_returned_code  = 0;
112 struct onsend_info* p_onsend=0; /* onsend route send info */
113
114
115
116 /* handle the exit code of a module function call.
117  * (used internally in do_action())
118  * @param h - script handle (h->last_retcode and h->run_flags will be set).
119  * @param ret - module function (v0 or v2) retcode
120  * Side-effects: sets _last_returned_code
121  */
122 #define MODF_HANDLE_RETCODE(h, ret) \
123         do { \
124                 /* if (unlikely((ret)==0)) (h)->run_flags|=EXIT_R_F; */ \
125                 (h)->run_flags |= EXIT_R_F & (((ret) != 0) -1); \
126                 (h)->last_retcode=(ret); \
127                 _last_returned_code = (h)->last_retcode; \
128         } while(0)
129
130
131
132 /* frees parameters converted using MODF_RVE_PARAM_CONVERT() from dst.
133  * (used internally in do_action())
134  * Assumes src is unchanged.
135  * Side-effects: clobbers i (int).
136  */
137 #define MODF_RVE_PARAM_FREE(cmd, src, dst) \
138                 for (i=0; i < (dst)[1].u.number; i++) { \
139                         if ((src)[i+2].type == RVE_ST && (dst)[i+2].u.data) { \
140                                 if ((dst)[i+2].type == RVE_FREE_FIXUP_ST) {\
141                                         /* call free_fixup (which should restore the original
142                                            string) */ \
143                                         call_fixup((cmd)->free_fixup, &(dst)[i+2].u.data, i+1); \
144                                 } else if ((dst)[i+2].type == FPARAM_DYN_ST) {\
145                                         /* completely frees fparam and restore original string */\
146                                         fparam_free_restore(&(dst)[i+2].u.data); \
147                                 } \
148                                 /* free allocated string */ \
149                                 pkg_free((dst)[i+2].u.data); \
150                                 (dst)[i+2].u.data = 0; \
151                         } \
152                 }
153
154
155 /* fills dst from src, converting RVE_ST params to STRING_ST.
156  * (used internally in do_action())
157  * @param src - source action_u_t array, as in the action structure
158  * @param dst - destination action_u_t array, will be filled from src.
159  * WARNING: dst must be cleaned when done, use MODULE_RVE_PARAM_FREE()
160  * Side-effects: clobbers i (int), s (str), rv (rvalue*), might jump to error.
161  */
162 #define MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst) \
163         do { \
164                 (dst)[1]=(src)[1]; \
165                 for (i=0; i < (src)[1].u.number; i++) { \
166                         if ((src)[2+i].type == RVE_ST) { \
167                                 rv=rval_expr_eval((h), (msg), (src)[i+2].u.data); \
168                                 if (unlikely(rv == 0 || \
169                                         rval_get_str((h), (msg), &s, rv, 0) < 0)) { \
170                                         rval_destroy(rv); \
171                                         ERR("failed to convert RVE to string\n"); \
172                                         (dst)[1].u.number = i; \
173                                         MODF_RVE_PARAM_FREE(cmd, src, dst); \
174                                         goto error; \
175                                 } \
176                                 (dst)[i+2].type = STRING_RVE_ST; \
177                                 (dst)[i+2].u.string = s.s; \
178                                 (dst)[i+2].u.str.len = s.len; \
179                                 rval_destroy(rv); \
180                                 if ((cmd)->fixup) {\
181                                         if ((cmd)->free_fixup) {\
182                                                 if (likely( call_fixup((cmd)->fixup, \
183                                                                                 &(dst)[i+2].u.data, i+1) >= 0) ) { \
184                                                         /* success => mark it for calling free fixup */ \
185                                                         if (likely((dst)[i+2].u.data != s.s)) \
186                                                                 (dst)[i+2].type = RVE_FREE_FIXUP_ST; \
187                                                 } else { \
188                                                         /* error calling fixup => mark conv. parameter \
189                                                            and return error */ \
190                                                         (dst)[1].u.number = i; \
191                                                         ERR("runtime fixup failed for %s param %d\n", \
192                                                                         (cmd)->name, i+1); \
193                                                         MODF_RVE_PARAM_FREE(cmd, src, dst); \
194                                                         goto error; \
195                                                 } \
196                                         } else if ((cmd)->fixup_flags & FIXUP_F_FPARAM_RVE) { \
197                                                 if (likely( call_fixup((cmd)->fixup, \
198                                                                                 &(dst)[i+2].u.data, i+1) >= 0)) { \
199                                                         if ((dst)[i+2].u.data != s.s) \
200                                                                 (dst)[i+2].type = FPARAM_DYN_ST; \
201                                                 } else { \
202                                                         /* error calling fixup => mark conv. parameter \
203                                                            and return error */ \
204                                                         (dst)[1].u.number = i; \
205                                                         ERR("runtime fixup failed for %s param %d\n", \
206                                                                         (cmd)->name, i+1); \
207                                                         MODF_RVE_PARAM_FREE(cmd, src, dst); \
208                                                         goto error; \
209                                                 }\
210                                         } \
211                                 } \
212                         } else \
213                                 (dst)[i+2]=(src)[i+2]; \
214                 } \
215         } while(0)
216
217
218
219 /* call a module function with normal STRING_ST params.
220  * (used internally in do_action())
221  * @param f_type - cmd_function type
222  * @param h
223  * @param msg
224  * @param src - source action_u_t array (e.g. action->val)
225  * @param params... - variable list of parameters, passed to the module
226  *               function
227  * Side-effects: sets ret, clobbers i (int), s (str), rv (rvalue*), cmd,
228  *               might jump to error.
229  *
230  */
231 #ifdef __SUNPRO_C
232 #define MODF_CALL(f_type, h, msg, src, ...) \
233         do { \
234                 cmd=(src)[0].u.data; \
235                 ret=((f_type)cmd->function)((msg), __VAR_ARGS__); \
236                 MODF_HANDLE_RETCODE(h, ret); \
237         } while (0)
238 #else  /* ! __SUNPRO_C  (gcc, icc a.s.o) */
239 #define MODF_CALL(f_type, h, msg, src, params...) \
240         do { \
241                 cmd=(src)[0].u.data; \
242                 ret=((f_type)cmd->function)((msg), ## params ); \
243                 MODF_HANDLE_RETCODE(h, ret); \
244         } while (0)
245 #endif /* __SUNPRO_C */
246
247
248
249 /* call a module function with possible RVE params.
250  * (used internally in do_action())
251  * @param f_type - cmd_function type
252  * @param h
253  * @param msg
254  * @param src - source action_u_t array (e.g. action->val)
255  * @param dst - temporary action_u_t array used for conversions. It can be
256  *              used for the function parameters. It's contents it's not
257  *              valid after the call.
258  * @param params... - variable list of parameters, passed to the module
259  *               function
260  * Side-effects: sets ret, clobbers i (int), s (str), rv (rvalue*), f, dst,
261  *               might jump to error.
262  *
263  */
264 #ifdef __SUNPRO_C
265 #define MODF_RVE_CALL(f_type, h, msg, src, dst, ...) \
266         do { \
267                 cmd=(src)[0].u.data; \
268                 MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst); \
269                 ret=((f_type)cmd->function)((msg), __VAR_ARGS__); \
270                 MODF_HANDLE_RETCODE(h, ret); \
271                 /* free strings allocated by us or fixups */ \
272                 MODF_RVE_PARAM_FREE(cmd, src, dst); \
273         } while (0)
274 #else  /* ! __SUNPRO_C  (gcc, icc a.s.o) */
275 #define MODF_RVE_CALL(f_type, h, msg, src, dst, params...) \
276         do { \
277                 cmd=(src)[0].u.data; \
278                 MODF_RVE_PARAM_CONVERT(h, msg, cmd, src, dst); \
279                 ret=((f_type)cmd->function)((msg), ## params ); \
280                 MODF_HANDLE_RETCODE(h, ret); \
281                 /* free strings allocated by us or fixups */ \
282                 MODF_RVE_PARAM_FREE(cmd, src, dst); \
283         } while (0)
284 #endif /* __SUNPRO_C */
285
286
287 /* ret= 0! if action -> end of list(e.g DROP),
288       > 0 to continue processing next actions
289    and <0 on error */
290 int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
291 {
292         int ret;
293         int v;
294         struct dest_info dst;
295         char* tmp;
296         char *new_uri, *end, *crt;
297         sr31_cmd_export_t* cmd;
298         int len;
299         int user;
300         struct sip_uri uri, next_hop;
301         struct sip_uri *u;
302         unsigned short port;
303         str* dst_host;
304         int i, flags;
305         avp_t* avp;
306         struct search_state st;
307         struct switch_cond_table* sct;
308         struct switch_jmp_table*  sjt;
309         struct rval_expr* rve;
310         struct match_cond_table* mct;
311         struct rvalue* rv;
312         struct rvalue* rv1;
313         struct rval_cache c1;
314         str s;
315         void *srevp[2];
316         /* temporary storage space for a struct action.val[] working copy
317          (needed to transform RVE intro STRING before calling module
318            functions). [0] is not used (corresp. to the module export pointer),
319            [1] contains the number of params, and [2..] the param values.
320            We need [1], because some fixup function use it
321           (see fixup_get_param_count()).  */
322         static action_u_t mod_f_params[MAX_ACTIONS];
323
324         /* reset the value of error to E_UNSPEC so avoid unknowledgable
325            functions to return with error (status<0) and not setting it
326            leaving there previous error; cache the previous value though
327            for functions which want to process it */
328         prev_ser_error=ser_error;
329         ser_error=E_UNSPEC;
330
331         /* hook for every executed action (in use by cfg debugger) */
332         if(unlikely(sr_event_enabled(SREV_CFG_RUN_ACTION)))
333         {
334                 srevp[0] = (void*)a;
335                 srevp[1] = (void*)msg;
336                 sr_event_exec(SREV_CFG_RUN_ACTION, (void*)srevp);
337         }
338
339         ret=E_BUG;
340         switch ((unsigned char)a->type){
341                 case DROP_T:
342                                 switch(a->val[0].type){
343                                         case NUMBER_ST:
344                                                 ret=(int) a->val[0].u.number;
345                                                 break;
346                                         case RVE_ST:
347                                                 rve=(struct rval_expr*)a->val[0].u.data;
348                                                 rval_expr_eval_int(h, msg, &ret, rve);
349                                                 break;
350                                         case RETCODE_ST:
351                                                 ret=h->last_retcode;
352                                                 break;
353                                         default:
354                                                 BUG("unexpected subtype %d in DROP_T\n",
355                                                                 a->val[0].type);
356                                                 ret=0;
357                                                 goto error;
358                                 }
359                                 h->run_flags|=(unsigned int)a->val[1].u.number;
360                         break;
361                 case FORWARD_T:
362 #ifdef USE_TCP
363                 case FORWARD_TCP_T:
364 #endif
365 #ifdef USE_TLS
366                 case FORWARD_TLS_T:
367 #endif
368 #ifdef USE_SCTP
369                 case FORWARD_SCTP_T:
370 #endif
371                 case FORWARD_UDP_T:
372                         /* init dst */
373                         init_dest_info(&dst);
374                         if (a->type==FORWARD_UDP_T) dst.proto=PROTO_UDP;
375 #ifdef USE_TCP
376                         else if (a->type==FORWARD_TCP_T) dst.proto= PROTO_TCP;
377 #endif
378 #ifdef USE_TLS
379                         else if (a->type==FORWARD_TLS_T) dst.proto= PROTO_TLS;
380 #endif
381 #ifdef USE_SCTP
382                         else if (a->type==FORWARD_SCTP_T) dst.proto=PROTO_SCTP;
383 #endif
384                         else dst.proto=PROTO_NONE;
385                         if (a->val[0].type==URIHOST_ST){
386                                 /*parse uri*/
387
388                                 if (msg->dst_uri.len) {
389                                         ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len,
390                                                                         &next_hop);
391                                         u = &next_hop;
392                                 } else {
393                                         ret = parse_sip_msg_uri(msg);
394                                         u = &msg->parsed_uri;
395                                 }
396
397                                 if (ret<0) {
398                                         LOG(L_ERR, "ERROR: do_action: forward: bad_uri "
399                                                                 " dropping packet\n");
400                                         goto error;
401                                 }
402
403                                 switch (a->val[1].type){
404                                         case URIPORT_ST:
405                                                                         port=u->port_no;
406                                                                         break;
407                                         case NUMBER_ST:
408                                                                         port=a->val[1].u.number;
409                                                                         break;
410                                         default:
411                                                         LOG(L_CRIT, "BUG: do_action bad forward 2nd"
412                                                                                 " param type (%d)\n", a->val[1].type);
413                                                         ret=E_UNSPEC;
414                                                         goto error_fwd_uri;
415                                 }
416                                 if (dst.proto == PROTO_NONE){ /* only if proto not set get it
417                                                                                          from the uri */
418                                         switch(u->proto){
419                                                 case PROTO_NONE:
420                                                         /*dst.proto=PROTO_UDP; */
421                                                         /* no proto, try to get it from the dns */
422                                                         break;
423                                                 case PROTO_UDP:
424 #ifdef USE_TCP
425                                                 case PROTO_TCP:
426 #endif
427 #ifdef USE_TLS
428                                                 case PROTO_TLS:
429 #endif
430 #ifdef USE_SCTP
431                                                 case PROTO_SCTP:
432 #endif
433                                                         dst.proto=u->proto;
434                                                         break;
435                                                 default:
436                                                         LOG(L_ERR,"ERROR: do action: forward: bad uri"
437                                                                         " transport %d\n", u->proto);
438                                                         ret=E_BAD_PROTO;
439                                                         goto error_fwd_uri;
440                                         }
441 #ifdef USE_TLS
442                                         if (u->type==SIPS_URI_T){
443                                                 if (u->proto==PROTO_UDP){
444                                                         LOG(L_ERR, "ERROR: do_action: forward: secure uri"
445                                                                         " incompatible with transport %d\n",
446                                                                         u->proto);
447                                                         ret=E_BAD_PROTO;
448                                                         goto error_fwd_uri;
449                                                 }
450                                                 dst.proto=PROTO_TLS;
451                                         }
452 #endif
453                                 }
454
455 #ifdef HONOR_MADDR
456                                 if (u->maddr_val.s && u->maddr_val.len)
457                                         dst_host=&u->maddr_val;
458                                 else
459 #endif
460                                         dst_host=&u->host;
461 #ifdef USE_COMP
462                                 dst.comp=u->comp;
463 #endif
464                                 ret=forward_request(msg, dst_host, port, &dst);
465                                 if (ret>=0){
466                                         ret=1;
467                                 }
468                         }else if ((a->val[0].type==PROXY_ST) && (a->val[1].type==NUMBER_ST)){
469                                 if (dst.proto==PROTO_NONE)
470                                         dst.proto=msg->rcv.proto;
471                                 proxy2su(&dst.to,  (struct proxy_l*)a->val[0].u.data);
472                                 ret=forward_request(msg, 0, 0, &dst);
473                                 if (ret>=0){
474                                         ret=1;
475                                         proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
476                                 }else if (ser_error!=E_OK){
477                                         proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
478                                 }
479                         }else{
480                                 LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
481                                                 a->val[0].type, a->val[1].type);
482                                 ret=E_BUG;
483                                 goto error;
484                         }
485                         break;
486                 case SEND_T:
487                 case SEND_TCP_T:
488                         if ((a->val[0].type!= PROXY_ST)|(a->val[1].type!=NUMBER_ST)){
489                                 LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
490                                                 a->val[0].type, a->val[1].type);
491                                 ret=E_BUG;
492                                 goto error;
493                         }
494                         /* init dst */
495                         init_dest_info(&dst);
496                         ret=proxy2su(&dst.to,  (struct proxy_l*)a->val[0].u.data);
497                         if (ret==0){
498                                 if (p_onsend){
499                                         tmp=p_onsend->buf;
500                                         len=p_onsend->len;
501                                 }else{
502                                         tmp=msg->buf;
503                                         len=msg->len;
504                                 }
505                                 if (a->type==SEND_T){
506                                         /*udp*/
507                                         dst.proto=PROTO_UDP; /* not really needed for udp_send */
508                                         dst.send_sock=get_send_socket(msg, &dst.to, PROTO_UDP);
509                                         if (dst.send_sock!=0){
510                                                 ret=udp_send(&dst, tmp, len);
511                                         }else{
512                                                 ret=-1;
513                                         }
514                                 }
515 #ifdef USE_TCP
516                                         else{
517                                                 /*tcp*/
518                                                 dst.proto=PROTO_TCP;
519                                                 dst.id=0;
520                                                 ret=tcp_send(&dst, 0, tmp, len);
521                                 }
522 #endif
523                         }else{
524                                 ret=E_BUG;
525                                 goto error;
526                         }
527                         proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
528                         if (ret>=0) ret=1;
529
530                         break;
531                 case LOG_T:
532                         if ((a->val[0].type!=NUMBER_ST)|(a->val[1].type!=STRING_ST)){
533                                 LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
534                                                 a->val[0].type, a->val[1].type);
535                                 ret=E_BUG;
536                                 goto error;
537                         }
538                         LOG_(DEFAULT_FACILITY, a->val[0].u.number, "<script>: ", "%s", 
539                                  a->val[1].u.string);
540                         ret=1;
541                         break;
542
543                 /* jku -- introduce a new branch */
544                 case APPEND_BRANCH_T:
545                         if (unlikely(a->val[0].type!=STR_ST)) {
546                                 LOG(L_CRIT, "BUG: do_action: bad append_branch_t %d\n",
547                                         a->val[0].type );
548                                 ret=E_BUG;
549                                 goto error;
550                         }
551                         getbflagsval(0, (flag_t*)&flags);
552                         ret=append_branch(msg, &a->val[0].u.str, &msg->dst_uri,
553                                                                 &msg->path_vec, a->val[1].u.number,
554                                                                 (flag_t)flags, msg->force_send_socket);
555                         /* if the uri is the ruri and q was also not changed, mark
556                            ruri as consumed, to avoid having an identical branch */
557                         if ((a->val[0].u.str.s == 0 || a->val[0].u.str.len == 0) &&
558                                         a->val[1].u.number == Q_UNSPECIFIED)
559                                 ruri_mark_consumed();
560                         break;
561
562                 /* jku begin: is_length_greater_than */
563                 case LEN_GT_T:
564                         if (a->val[0].type!=NUMBER_ST) {
565                                 LOG(L_CRIT, "BUG: do_action: bad len_gt type %d\n",
566                                         a->val[0].type );
567                                 ret=E_BUG;
568                                 goto error;
569                         }
570                         /* DBG("XXX: message length %d, max %d\n",
571                                 msg->len, a->val[0].u.number ); */
572                         ret = msg->len >= a->val[0].u.number ? 1 : -1;
573                         break;
574                 /* jku end: is_length_greater_than */
575
576                 /* jku - begin : flag processing */
577
578                 case SETFLAG_T:
579                         if (a->val[0].type!=NUMBER_ST) {
580                                 LOG(L_CRIT, "BUG: do_action: bad setflag() type %d\n",
581                                         a->val[0].type );
582                                 ret=E_BUG;
583                                 goto error;
584                         }
585                         if (!flag_in_range( a->val[0].u.number )) {
586                                 ret=E_CFG;
587                                 goto error;
588                         }
589                         setflag( msg, a->val[0].u.number );
590                         ret=1;
591                         break;
592
593                 case RESETFLAG_T:
594                         if (a->val[0].type!=NUMBER_ST) {
595                                 LOG(L_CRIT, "BUG: do_action: bad resetflag() type %d\n",
596                                         a->val[0].type );
597                                 ret=E_BUG;
598                                 goto error;
599                         }
600                         if (!flag_in_range( a->val[0].u.number )) {
601                                 ret=E_CFG;
602                                 goto error;
603                         }
604                         resetflag( msg, a->val[0].u.number );
605                         ret=1;
606                         break;
607
608                 case ISFLAGSET_T:
609                         if (a->val[0].type!=NUMBER_ST) {
610                                 LOG(L_CRIT, "BUG: do_action: bad isflagset() type %d\n",
611                                         a->val[0].type );
612                                 ret=E_BUG;
613                                 goto error;
614                         }
615                         if (!flag_in_range( a->val[0].u.number )) {
616                                 ret=E_CFG;
617                                 goto error;
618                         }
619                         ret=isflagset( msg, a->val[0].u.number );
620                         break;
621                 /* jku - end : flag processing */
622
623                 case AVPFLAG_OPER_T:
624                         ret = 0;
625                         if ((a->val[0].u.attr->type & AVP_INDEX_ALL) == AVP_INDEX_ALL ||
626                                         (a->val[0].u.attr->type & AVP_NAME_RE)!=0) {
627                                 for (avp=search_first_avp(a->val[0].u.attr->type,
628                                                         a->val[0].u.attr->name, NULL, &st);
629                                                 avp;
630                                                 avp = search_next_avp(&st, NULL)) {
631                                         switch (a->val[2].u.number) {
632                                                 /* oper: 0..reset, 1..set, -1..no change */
633                                                 case 0:
634                                                         avp->flags &= ~(avp_flags_t)a->val[1].u.number;
635                                                         break;
636                                                 case 1:
637                                                         avp->flags |= (avp_flags_t)a->val[1].u.number;
638                                                         break;
639                                                 default:;
640                                         }
641                                         ret = ret ||
642                                                 ((avp->flags & (avp_flags_t)a->val[1].u.number) != 0);
643                                 }
644                         } else {
645                                 avp = search_avp_by_index(a->val[0].u.attr->type,
646                                                                                         a->val[0].u.attr->name, NULL,
647                                                                                         a->val[0].u.attr->index);
648                                 if (avp) {
649                                         switch (a->val[2].u.number) {
650                                                 /* oper: 0..reset, 1..set, -1..no change */
651                                                 case 0:
652                                                         avp->flags &= ~(avp_flags_t)a->val[1].u.number;
653                                                         break;
654                                                 case 1:
655                                                         avp->flags |= (avp_flags_t)a->val[1].u.number;
656                                                         break;
657                                                 default:;
658                                         }
659                                         ret = (avp->flags & (avp_flags_t)a->val[1].u.number) != 0;
660                                 }
661                         }
662                         if (ret==0)
663                                 ret = -1;
664                         break;
665                 case ERROR_T:
666                         if ((a->val[0].type!=STRING_ST)|(a->val[1].type!=STRING_ST)){
667                                 LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
668                                                 a->val[0].type, a->val[1].type);
669                                 ret=E_BUG;
670                                 goto error;
671                         }
672                         LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
673                                         "not implemented yet\n", a->val[0].u.string, a->val[1].u.string);
674                         ret=1;
675                         break;
676                 case ROUTE_T:
677                         if (likely(a->val[0].type == NUMBER_ST))
678                                 i = a->val[0].u.number;
679                         else if (a->val[0].type == RVE_ST) {
680                                 rv = rval_expr_eval(h, msg, a->val[0].u.data);
681                                 rval_cache_init(&c1);
682                                 if (unlikely(rv == 0 ||
683                                                 rval_get_tmp_str(h, msg, &s, rv, 0, &c1) < 0)) {
684                                         rval_destroy(rv);
685                                         rval_cache_clean(&c1);
686                                         ERR("failed to convert RVE to string\n");
687                                         ret = E_UNSPEC;
688                                         goto error;
689                                 }
690                                 i = route_lookup(&main_rt, s.s);
691                                 if (unlikely(i < 0)) {
692                                         ERR("route \"%s\" not found at %s:%d\n",
693                                                         s.s, (a->cfile)?a->cfile:"line", a->cline);
694                                         rval_destroy(rv);
695                                         rval_cache_clean(&c1);
696                                         s.s = 0;
697                                         ret = E_SCRIPT;
698                                         goto error;
699                                 }
700                                 rval_destroy(rv);
701                                 rval_cache_clean(&c1);
702                                 s.s = 0;
703                         } else {
704                                 LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
705                                                 a->val[0].type);
706                                 ret=E_BUG;
707                                 goto error;
708                         }
709                         if (unlikely((i>=main_rt.idx)||(i<0))){
710                                 LOG(L_ERR, "ERROR: invalid routing table number in"
711                                                         "route(%lu) at %s:%d\n", a->val[0].u.number,
712                                                         (a->cfile)?a->cfile:"line", a->cline);
713                                 ret=E_CFG;
714                                 goto error;
715                         }
716                         /*ret=((ret=run_actions(rlist[a->val[0].u.number],msg))<0)?ret:1;*/
717                         ret=run_actions(h, main_rt.rlist[i], msg);
718                         h->last_retcode=ret;
719                         _last_returned_code = h->last_retcode;
720                         h->run_flags&=~(RETURN_R_F|BREAK_R_F); /* absorb return & break */
721                         break;
722                 case EXEC_T:
723                         if (a->val[0].type!=STRING_ST){
724                                 LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
725                                                 a->val[0].type);
726                                 ret=E_BUG;
727                                 goto error;
728                         }
729                         LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
730                                                 " using dumb version...\n", a->val[0].u.string);
731                         ret=system(a->val[0].u.string);
732                         if (ret!=0){
733                                 LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
734                         }
735                         ret=1;
736                         break;
737                 case REVERT_URI_T:
738                         if (msg->new_uri.s) {
739                                 pkg_free(msg->new_uri.s);
740                                 msg->new_uri.len=0;
741                                 msg->new_uri.s=0;
742                                 msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
743                                 ruri_mark_new(); /* available for forking */
744                         };
745                         ret=1;
746                         break;
747                 case SET_HOST_T:
748                 case SET_HOSTPORT_T:
749                 case SET_HOSTPORTTRANS_T:
750                 case SET_HOSTALL_T:
751                 case SET_USER_T:
752                 case SET_USERPASS_T:
753                 case SET_PORT_T:
754                 case SET_URI_T:
755                 case PREFIX_T:
756                 case STRIP_T:
757                 case STRIP_TAIL_T:
758                 case SET_USERPHONE_T:
759                                 user=0;
760                                 if (a->type==STRIP_T || a->type==STRIP_TAIL_T) {
761                                         if (a->val[0].type!=NUMBER_ST) {
762                                                 LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
763                                                         a->val[0].type);
764                                                 ret=E_BUG;
765                                                 goto error;
766                                         }
767                                 } else if (a->type!=SET_USERPHONE_T) {
768                                         if (a->val[0].type!=STRING_ST) {
769                                                 LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
770                                                         a->val[0].type);
771                                                 ret=E_BUG;
772                                                 goto error;
773                                         }
774                                 }
775                                 if (a->type==SET_URI_T){
776                                         if (msg->new_uri.s) {
777                                                         pkg_free(msg->new_uri.s);
778                                                         msg->new_uri.len=0;
779                                         }
780                                         msg->parsed_uri_ok=0;
781                                         len=strlen(a->val[0].u.string);
782                                         msg->new_uri.s=pkg_malloc(len+1);
783                                         if (msg->new_uri.s==0){
784                                                 LOG(L_ERR, "ERROR: do_action: memory allocation"
785                                                                 " failure\n");
786                                                 ret=E_OUT_OF_MEM;
787                                                 goto error;
788                                         }
789                                         memcpy(msg->new_uri.s, a->val[0].u.string, len);
790                                         msg->new_uri.s[len]=0;
791                                         msg->new_uri.len=len;
792                                         ruri_mark_new(); /* available for forking */
793
794                                         ret=1;
795                                         break;
796                                 }
797                                 if (msg->parsed_uri_ok==0) {
798                                         if (msg->new_uri.s) {
799                                                 tmp=msg->new_uri.s;
800                                                 len=msg->new_uri.len;
801                                         }else{
802                                                 tmp=msg->first_line.u.request.uri.s;
803                                                 len=msg->first_line.u.request.uri.len;
804                                         }
805                                         if (parse_uri(tmp, len, &uri)<0){
806                                                 LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
807                                                                         " packet\n", tmp);
808                                                 ret=E_UNSPEC;
809                                                 goto error;
810                                         }
811                                 } else {
812                                         uri=msg->parsed_uri;
813                                 }
814
815                                 /* skip SET_USERPHONE_T action if the URI is already
816                                  * a tel: or tels: URI, or contains the user=phone param */
817                                 if ((a->type==SET_USERPHONE_T) 
818                                         && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T)
819                                                 || ((uri.user_param_val.len==5) && (memcmp(uri.user_param_val.s, "phone", 5)==0)))
820                                 ) {
821                                         ret=1;
822                                         break;
823                                 }
824                                 /* SET_PORT_T does not work with tel: URIs */
825                                 if ((a->type==SET_PORT_T)
826                                         && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
827                                         && ((uri.flags & URI_SIP_USER_PHONE)==0)
828                                 ) {
829                                         LOG(L_ERR, "ERROR: do_action: port number of a tel: URI cannot be set\n");
830                                         ret=E_UNSPEC;
831                                         goto error;
832                                 }
833
834                                 new_uri=pkg_malloc(MAX_URI_SIZE);
835                                 if (new_uri==0){
836                                         LOG(L_ERR, "ERROR: do_action: memory allocation "
837                                                                 " failure\n");
838                                         ret=E_OUT_OF_MEM;
839                                         goto error;
840                                 }
841                                 end=new_uri+MAX_URI_SIZE;
842                                 crt=new_uri;
843                                 /* begin copying */
844                                 /* Preserve the URI scheme unless the host part needs
845                                  * to be rewritten, and the shceme is tel: or tels: */
846                                 switch (uri.type) {
847                                 case SIP_URI_T:
848                                         len=s_sip.len;
849                                         tmp=s_sip.s;
850                                         break;
851
852                                 case SIPS_URI_T:
853                                         len=s_sips.len;
854                                         tmp=s_sips.s;
855                                         break;
856
857                                 case TEL_URI_T:
858                                         if ((uri.flags & URI_SIP_USER_PHONE)
859                                                 || (a->type==SET_HOST_T)
860                                                 || (a->type==SET_HOSTPORT_T)
861                                                 || (a->type==SET_HOSTPORTTRANS_T)
862                                         ) {
863                                                 len=s_sip.len;
864                                                 tmp=s_sip.s;
865                                                 break;
866                                         }
867                                         len=s_tel.len;
868                                         tmp=s_tel.s;
869                                         break;
870
871                                 case TELS_URI_T:
872                                         if ((uri.flags & URI_SIP_USER_PHONE)
873                                                 || (a->type==SET_HOST_T)
874                                                 || (a->type==SET_HOSTPORT_T)
875                                                 || (a->type==SET_HOSTPORTTRANS_T)
876                                         ) {
877                                                 len=s_sips.len;
878                                                 tmp=s_sips.s;
879                                                 break;
880                                         }
881                                         len=s_tels.len;
882                                         tmp=s_tels.s;
883                                         break;
884
885                                 default:
886                                         LOG(L_ERR, "ERROR: Unsupported URI scheme (%d), "
887                                                 "reverted to sip:\n",
888                                                 uri.type);
889                                         len=s_sip.len;
890                                         tmp=s_sip.s;
891                                 }
892                                 if(crt+len+1 /* colon */ >end) goto error_uri;
893                                 memcpy(crt,tmp,len);crt+=len;
894                                 *crt=':'; crt++;
895
896                                 /* user */
897
898                                 /* prefix (-jiri) */
899                                 if (a->type==PREFIX_T) {
900                                         tmp=a->val[0].u.string;
901                                         len=strlen(tmp); if(crt+len>end) goto error_uri;
902                                         memcpy(crt,tmp,len);crt+=len;
903                                         /* whatever we had before, with prefix we have username
904                                            now */
905                                         user=1;
906                                 }
907
908                                 if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
909                                         tmp=a->val[0].u.string;
910                                         len=strlen(tmp);
911                                 } else if (a->type==STRIP_T) {
912                                         if (a->val[0].u.number>uri.user.len) {
913                                                 LOG(L_WARN, "Error: too long strip asked; "
914                                                                         " deleting username: %lu of <%.*s>\n",
915                                                                         a->val[0].u.number, uri.user.len, uri.user.s );
916                                                 len=0;
917                                         } else if (a->val[0].u.number==uri.user.len) {
918                                                 len=0;
919                                         } else {
920                                                 tmp=uri.user.s + a->val[0].u.number;
921                                                 len=uri.user.len - a->val[0].u.number;
922                                         }
923                                 } else if (a->type==STRIP_TAIL_T) {
924                                         if (a->val[0].u.number>uri.user.len) {
925                                                 LOG(L_WARN, "WARNING: too long strip_tail asked; "
926                                                                         " deleting username: %lu of <%.*s>\n",
927                                                                         a->val[0].u.number, uri.user.len, uri.user.s );
928                                                 len=0;
929                                         } else if (a->val[0].u.number==uri.user.len) {
930                                                 len=0;
931                                         } else {
932                                                 tmp=uri.user.s;
933                                                 len=uri.user.len - a->val[0].u.number;
934                                         }
935                                 } else {
936                                         tmp=uri.user.s;
937                                         len=uri.user.len;
938                                 }
939
940                                 if (len){
941                                         if(crt+len>end) goto error_uri;
942                                         memcpy(crt,tmp,len);crt+=len;
943                                         user=1; /* we have an user field so mark it */
944                                 }
945
946                                 if (a->type==SET_USERPASS_T) tmp=0;
947                                 else tmp=uri.passwd.s;
948                                 /* passwd */
949                                 if (tmp){
950                                         len=uri.passwd.len; if(crt+len+1>end) goto error_uri;
951                                         *crt=':'; crt++;
952                                         memcpy(crt,tmp,len);crt+=len;
953                                 }
954                                 /* tel: URI parameters */
955                                 if ((uri.type==TEL_URI_T)
956                                         || (uri.type==TELS_URI_T)
957                                 ) {
958                                         tmp=uri.params.s;
959                                         if (tmp){
960                                                 len=uri.params.len; if(crt+len+1>end) goto error_uri;
961                                                 *crt=';'; crt++;
962                                                 memcpy(crt,tmp,len);crt+=len;
963                                         }
964                                 }
965                                 /* host */
966                                 if ((a->type==SET_HOST_T)
967                                                 || (a->type==SET_HOSTPORT_T)
968                                                 || (a->type==SET_HOSTALL_T)
969                                                 || (a->type==SET_HOSTPORTTRANS_T)
970                                 ) {
971                                         tmp=a->val[0].u.string;
972                                         if (tmp) len = strlen(tmp);
973                                         else len=0;
974                                 } else if ((uri.type==SIP_URI_T)
975                                         || (uri.type==SIPS_URI_T)
976                                         || (uri.flags & URI_SIP_USER_PHONE)
977                                 ) {
978                                         tmp=uri.host.s;
979                                         len=uri.host.len;
980                                 } else {
981                                         tmp=0;
982                                 }
983                                 if (tmp){
984                                         if (user) { /* add @ */
985                                                 if(crt+1>end) goto error_uri;
986                                                 *crt='@'; crt++;
987                                         }
988                                         if(crt+len>end) goto error_uri;
989                                         memcpy(crt,tmp,len);crt+=len;
990                                 }
991                                 if(a->type==SET_HOSTALL_T)
992                                         goto done_seturi;
993                                 /* port */
994                                 if ((a->type==SET_HOSTPORT_T)
995                                                 || (a->type==SET_HOSTPORTTRANS_T))
996                                         tmp=0;
997                                 else if (a->type==SET_PORT_T) {
998                                         tmp=a->val[0].u.string;
999                                         if (tmp) {
1000                                                 len = strlen(tmp);
1001                                                 if(len==0) tmp = 0;
1002                                         } else len = 0;
1003                                 } else {
1004                                         tmp=uri.port.s;
1005                                         len = uri.port.len;
1006                                 }
1007                                 if (tmp){
1008                                         if(crt+len+1>end) goto error_uri;
1009                                         *crt=':'; crt++;
1010                                         memcpy(crt,tmp,len);crt+=len;
1011                                 }
1012                                 /* params */
1013                                 if ((a->type==SET_HOSTPORTTRANS_T)
1014                                         && uri.sip_params.s
1015                                         && uri.transport.s
1016                                 ) {
1017                                         /* bypass the transport parameter */
1018                                         if (uri.sip_params.s < uri.transport.s) {
1019                                                 /* there are parameters before transport */
1020                                                 len = uri.transport.s - uri.sip_params.s - 1;
1021                                                         /* ignore the ';' at the end */
1022                                                 if (crt+len+1>end) goto error_uri;
1023                                                 *crt=';'; crt++;
1024                                                 memcpy(crt,uri.sip_params.s,len);crt+=len;
1025                                         }
1026                                         len = (uri.sip_params.s + uri.sip_params.len) -
1027                                                 (uri.transport.s + uri.transport.len);
1028                                         if (len > 0) {
1029                                                 /* there are parameters after transport */
1030                                                 if (crt+len>end) goto error_uri;
1031                                                 tmp = uri.transport.s + uri.transport.len;
1032                                                 memcpy(crt,tmp,len);crt+=len;
1033                                         }
1034                                 } else {
1035                                         tmp=uri.sip_params.s;
1036                                         if (tmp){
1037                                                 len=uri.sip_params.len; if(crt+len+1>end) goto error_uri;
1038                                                 *crt=';'; crt++;
1039                                                 memcpy(crt,tmp,len);crt+=len;
1040                                         }
1041                                 }
1042                                 /* Add the user=phone param if a tel: or tels:
1043                                  * URI was converted to sip: or sips:.
1044                                  * (host part of a tel/tels URI was set.)
1045                                  * Or in case of sip: URI and SET_USERPHONE_T action */
1046                                 if (((((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
1047                                         && ((uri.flags & URI_SIP_USER_PHONE)==0))
1048                                         && ((a->type==SET_HOST_T)
1049                                                 || (a->type==SET_HOSTPORT_T)
1050                                                 || (a->type==SET_HOSTPORTTRANS_T)))
1051                                         || (a->type==SET_USERPHONE_T)
1052                                 ) {
1053                                         tmp=";user=phone";
1054                                         len=strlen(tmp);
1055                                         if(crt+len>end) goto error_uri;
1056                                         memcpy(crt,tmp,len);crt+=len;
1057                                 }
1058                                 /* headers */
1059                                 tmp=uri.headers.s;
1060                                 if (tmp){
1061                                         len=uri.headers.len; if(crt+len+1>end) goto error_uri;
1062                                         *crt='?'; crt++;
1063                                         memcpy(crt,tmp,len);crt+=len;
1064                                 }
1065         done_seturi:
1066                                 *crt=0; /* null terminate the thing */
1067                                 /* copy it to the msg */
1068                                 if (msg->new_uri.s) pkg_free(msg->new_uri.s);
1069                                 msg->new_uri.s=new_uri;
1070                                 msg->new_uri.len=crt-new_uri;
1071                                 msg->parsed_uri_ok=0;
1072                                 ruri_mark_new(); /* available for forking */
1073                                 ret=1;
1074                                 break;
1075                 case IF_T:
1076                                         rve=(struct rval_expr*)a->val[0].u.data;
1077                                         if (unlikely(rval_expr_eval_int(h, msg, &v, rve) != 0)){
1078                                                 ERR("if expression evaluation failed (%d,%d-%d,%d)\n",
1079                                                                 rve->fpos.s_line, rve->fpos.s_col,
1080                                                                 rve->fpos.e_line, rve->fpos.e_col);
1081                                                 v=0; /* false */
1082                                         }
1083                                         if (unlikely(h->run_flags & EXIT_R_F)){
1084                                                 ret=0;
1085                                                 break;
1086                                         }
1087                                         h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
1088                                                                                                                             break in expr*/
1089                                         ret=1;  /*default is continue */
1090                                         if (v>0) {
1091                                                 if ((a->val[1].type==ACTIONS_ST)&&a->val[1].u.data){
1092                                                         ret=run_actions(h,
1093                                                                                 (struct action*)a->val[1].u.data, msg);
1094                                                 }
1095                                         }else if ((a->val[2].type==ACTIONS_ST)&&a->val[2].u.data){
1096                                                         ret=run_actions(h,
1097                                                                                 (struct action*)a->val[2].u.data, msg);
1098                                         }
1099                         break;
1100                 case MODULE0_T:
1101                         MODF_CALL(cmd_function, h, msg, a->val, 0, 0);
1102                         break;
1103                 /* instead of using the parameter number, we use different names
1104                  * for calls to functions with 3, 4, 5, 6 or variable number of
1105                  * parameters due to performance reasons */
1106                 case MODULE1_T:
1107                         MODF_CALL(cmd_function, h, msg, a->val,
1108                                                                                 (char*)a->val[2].u.data,
1109                                                                                 0
1110                                         );
1111                         break;
1112                 case MODULE2_T:
1113                         MODF_CALL(cmd_function, h, msg, a->val,
1114                                                                                 (char*)a->val[2].u.data,
1115                                                                                 (char*)a->val[3].u.data
1116                                         );
1117                         break;
1118                 case MODULE3_T:
1119                         MODF_CALL(cmd_function3, h, msg, a->val,
1120                                                                                 (char*)a->val[2].u.data,
1121                                                                                 (char*)a->val[3].u.data,
1122                                                                                 (char*)a->val[4].u.data
1123                                         );
1124                         break;
1125                 case MODULE4_T:
1126                         MODF_CALL(cmd_function4, h, msg, a->val,
1127                                                                                 (char*)a->val[2].u.data,
1128                                                                                 (char*)a->val[3].u.data,
1129                                                                                 (char*)a->val[4].u.data,
1130                                                                                 (char*)a->val[5].u.data
1131                                         );
1132                         break;
1133                 case MODULE5_T:
1134                         MODF_CALL(cmd_function5, h, msg, a->val,
1135                                                                                 (char*)a->val[2].u.data,
1136                                                                                 (char*)a->val[3].u.data,
1137                                                                                 (char*)a->val[4].u.data,
1138                                                                                 (char*)a->val[5].u.data,
1139                                                                                 (char*)a->val[6].u.data
1140                                         );
1141                         break;
1142                 case MODULE6_T:
1143                         MODF_CALL(cmd_function6, h, msg, a->val,
1144                                                                                 (char*)a->val[2].u.data,
1145                                                                                 (char*)a->val[3].u.data,
1146                                                                                 (char*)a->val[4].u.data,
1147                                                                                 (char*)a->val[5].u.data,
1148                                                                                 (char*)a->val[6].u.data,
1149                                                                                 (char*)a->val[7].u.data
1150                                         );
1151                         break;
1152                 case MODULEX_T:
1153                         MODF_CALL(cmd_function_var, h, msg, a->val,
1154                                                         a->val[1].u.number, &a->val[2]);
1155                         break;
1156                 case MODULE1_RVE_T:
1157                         MODF_RVE_CALL(cmd_function, h, msg, a->val, mod_f_params,
1158                                                                                         (char*)mod_f_params[2].u.data,
1159                                                                                         0
1160                                         );
1161                         break;
1162                 case MODULE2_RVE_T:
1163                         MODF_RVE_CALL(cmd_function, h, msg, a->val, mod_f_params,
1164                                                                                         (char*)mod_f_params[2].u.data,
1165                                                                                         (char*)mod_f_params[3].u.data
1166                                         );
1167                         break;
1168                 case MODULE3_RVE_T:
1169                         MODF_RVE_CALL(cmd_function3, h, msg, a->val, mod_f_params,
1170                                                                                         (char*)mod_f_params[2].u.data,
1171                                                                                         (char*)mod_f_params[3].u.data,
1172                                                                                         (char*)mod_f_params[4].u.data
1173                                         );
1174                         break;
1175                 case MODULE4_RVE_T:
1176                         MODF_RVE_CALL(cmd_function4, h, msg, a->val, mod_f_params,
1177                                                                                         (char*)mod_f_params[2].u.data,
1178                                                                                         (char*)mod_f_params[3].u.data,
1179                                                                                         (char*)mod_f_params[4].u.data,
1180                                                                                         (char*)mod_f_params[5].u.data
1181                                         );
1182                         break;
1183                 case MODULE5_RVE_T:
1184                         MODF_RVE_CALL(cmd_function5, h, msg, a->val, mod_f_params,
1185                                                                                         (char*)mod_f_params[2].u.data,
1186                                                                                         (char*)mod_f_params[3].u.data,
1187                                                                                         (char*)mod_f_params[4].u.data,
1188                                                                                         (char*)mod_f_params[5].u.data,
1189                                                                                         (char*)mod_f_params[6].u.data
1190                                         );
1191                         break;
1192                 case MODULE6_RVE_T:
1193                         MODF_RVE_CALL(cmd_function6, h, msg, a->val, mod_f_params,
1194                                                                                         (char*)mod_f_params[2].u.data,
1195                                                                                         (char*)mod_f_params[3].u.data,
1196                                                                                         (char*)mod_f_params[4].u.data,
1197                                                                                         (char*)mod_f_params[5].u.data,
1198                                                                                         (char*)mod_f_params[6].u.data,
1199                                                                                         (char*)mod_f_params[7].u.data
1200                                         );
1201                         break;
1202                 case MODULEX_RVE_T:
1203                         MODF_RVE_CALL(cmd_function_var, h, msg, a->val, mod_f_params,
1204                                                         a->val[1].u.number, &mod_f_params[2]);
1205                         break;
1206                 case EVAL_T:
1207                         /* only eval the expression to account for possible
1208                            side-effect */
1209                         rval_expr_eval_int(h, msg, &v,
1210                                         (struct rval_expr*)a->val[0].u.data);
1211                         if (h->run_flags & EXIT_R_F){
1212                                 ret=0;
1213                                 break;
1214                         }
1215                         h->run_flags &= ~RETURN_R_F|BREAK_R_F; /* catch return & break in
1216                                                                                                           expr */
1217                         ret=1; /* default is continue */
1218                         break;
1219                 case SWITCH_COND_T:
1220                         sct=(struct switch_cond_table*)a->val[1].u.data;
1221                         if (unlikely( rval_expr_eval_int(h, msg, &v,
1222                                                                         (struct rval_expr*)a->val[0].u.data) <0)){
1223                                 /* handle error in expression => use default */
1224                                 ret=-1;
1225                                 goto sw_cond_def;
1226                         }
1227                         if (h->run_flags & EXIT_R_F){
1228                                 ret=0;
1229                                 break;
1230                         }
1231                         h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
1232                                                                                                             in expr */
1233                         ret=1; /* default is continue */
1234                         for(i=0; i<sct->n; i++)
1235                                 if (sct->cond[i]==v){
1236                                         if (likely(sct->jump[i])){
1237                                                 ret=run_actions(h, sct->jump[i], msg);
1238                                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1239                                                                                                            returns passthrough */
1240                                         }
1241                                         goto skip;
1242                                 }
1243 sw_cond_def:
1244                         if (sct->def){
1245                                 ret=run_actions(h, sct->def, msg);
1246                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1247                                                                                            returns passthrough */
1248                         }
1249                         break;
1250                 case SWITCH_JT_T:
1251                         sjt=(struct switch_jmp_table*)a->val[1].u.data;
1252                         if (unlikely( rval_expr_eval_int(h, msg, &v,
1253                                                                         (struct rval_expr*)a->val[0].u.data) <0)){
1254                                 /* handle error in expression => use default */
1255                                 ret=-1;
1256                                 goto sw_jt_def;
1257                         }
1258                         if (h->run_flags & EXIT_R_F){
1259                                 ret=0;
1260                                 break;
1261                         }
1262                         h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
1263                                                                                                             in expr */
1264                         ret=1; /* default is continue */
1265                         if (likely(v >= sjt->first && v <= sjt->last)){
1266                                 if (likely(sjt->tbl[v - sjt->first])){
1267                                         ret=run_actions(h, sjt->tbl[v - sjt->first], msg);
1268                                         h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1269                                                                                                    returns passthrough */
1270                                 }
1271                                 break; 
1272                         }else{
1273                                 for(i=0; i<sjt->rest.n; i++)
1274                                         if (sjt->rest.cond[i]==v){
1275                                                 if (likely(sjt->rest.jump[i])){
1276                                                         ret=run_actions(h, sjt->rest.jump[i], msg);
1277                                                         h->run_flags &= ~BREAK_R_F; /* catch breaks, but 
1278                                                                                                                    let returns pass */
1279                                                 }
1280                                                 goto skip;
1281                                         }
1282                         }
1283                         /* not found => try default */
1284 sw_jt_def:
1285                         if (sjt->rest.def){
1286                                 ret=run_actions(h, sjt->rest.def, msg);
1287                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1288                                                                                            returns passthrough */
1289                         }
1290                         break;
1291                 case BLOCK_T:
1292                         if (likely(a->val[0].u.data)){
1293                                 ret=run_actions(h, (struct action*)a->val[0].u.data, msg);
1294                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1295                                                                                            returns passthrough */
1296                         }
1297                         break;
1298                 case MATCH_COND_T:
1299                         mct=(struct match_cond_table*)a->val[1].u.data;
1300                         rval_cache_init(&c1);
1301                         rv=0;
1302                         rv1=0;
1303                         ret=rval_expr_eval_rvint(h, msg, &rv, &v, 
1304                                                                         (struct rval_expr*)a->val[0].u.data, &c1);
1305                                                                         
1306                         if (unlikely( ret<0)){
1307                                 /* handle error in expression => use default */
1308                                 ret=-1;
1309                                 goto match_cond_def;
1310                         }
1311                         if (h->run_flags & EXIT_R_F){
1312                                 ret=0;
1313                                 break;
1314                         }
1315                         h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
1316                                                                                                             in expr */
1317                         if (likely(rv)){
1318                                 rv1=rval_convert(h, msg, RV_STR, rv, &c1);
1319                                 if (unlikely(rv1==0)){
1320                                         ret=-1;
1321                                         goto match_cond_def;
1322                                 }
1323                                 s=rv1->v.s;
1324                         }else{
1325                                 /* int result in v */
1326                                 rval_cache_clean(&c1);
1327                                 s.s=sint2str(v, &s.len);
1328                         }
1329                         ret=1; /* default is continue */
1330                         for(i=0; i<mct->n; i++)
1331                                 if (( mct->match[i].type==MATCH_STR &&
1332                                                 mct->match[i].l.s.len==s.len &&
1333                                                 memcmp(mct->match[i].l.s.s, s.s, s.len) == 0 ) ||
1334                                          ( mct->match[i].type==MATCH_RE &&
1335                                           regexec(mct->match[i].l.regex, s.s, 0, 0, 0) == 0)
1336                                         ){
1337                                         if (likely(mct->jump[i])){
1338                                                 /* make sure we cleanup first, in case run_actions()
1339                                                    exits the script directly via longjmp() */
1340                                                 if (rv1){
1341                                                         rval_destroy(rv1);
1342                                                         rval_destroy(rv);
1343                                                         rval_cache_clean(&c1);
1344                                                 }else if (rv){
1345                                                         rval_destroy(rv);
1346                                                         rval_cache_clean(&c1);
1347                                                 }
1348                                                 ret=run_actions(h, mct->jump[i], msg);
1349                                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1350                                                                                                            returns passthrough */
1351                                                 goto skip;
1352                                         }
1353                                         goto match_cleanup;
1354                                 }
1355 match_cond_def:
1356                         if (mct->def){
1357                                 /* make sure we cleanup first, in case run_actions()
1358                                    exits the script directly via longjmp() */
1359                                 if (rv1){
1360                                         rval_destroy(rv1);
1361                                         rval_destroy(rv);
1362                                         rval_cache_clean(&c1);
1363                                 }else if (rv){
1364                                         rval_destroy(rv);
1365                                         rval_cache_clean(&c1);
1366                                 }
1367                                 ret=run_actions(h, mct->def, msg);
1368                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1369                                                                                            returns passthrough */
1370                                 break;
1371                         }
1372 match_cleanup:
1373                         if (rv1){
1374                                 rval_destroy(rv1);
1375                                 rval_destroy(rv);
1376                                 rval_cache_clean(&c1);
1377                         }else if (rv){
1378                                 rval_destroy(rv);
1379                                 rval_cache_clean(&c1);
1380                         }
1381                         break;
1382                 case WHILE_T:
1383                         i=0;
1384                         flags=0;
1385                         rve=(struct rval_expr*)a->val[0].u.data;
1386                         ret=1;
1387                         while(!(flags & (BREAK_R_F|RETURN_R_F|EXIT_R_F)) &&
1388                                         (rval_expr_eval_int(h, msg, &v, rve) == 0) && v){
1389                                 i++;
1390                                 if (unlikely(i > cfg_get(core, core_cfg, max_while_loops))){
1391                                         LOG(L_ERR, "ERROR: runaway while (%d, %d): more then"
1392                                                                 " %d loops\n", 
1393                                                                 rve->fpos.s_line, rve->fpos.s_col,
1394                                                                 cfg_get(core, core_cfg, max_while_loops));
1395                                         ret=-1;
1396                                         goto error;
1397                                 }
1398                                 if (likely(a->val[1].u.data)){
1399                                         ret=run_actions(h, (struct action*)a->val[1].u.data, msg);
1400                                         flags|=h->run_flags;
1401                                         h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1402                                                                                                    returns pass-through */
1403                                 }
1404                         }
1405                         break;
1406                 case FORCE_RPORT_T:
1407                         msg->msg_flags|=FL_FORCE_RPORT;
1408                         ret=1; /* continue processing */
1409                         break;
1410                 case ADD_LOCAL_RPORT_T:
1411                         msg->msg_flags|=FL_ADD_LOCAL_RPORT;
1412                         ret=1; /* continue processing */
1413                         break;
1414                 case UDP_MTU_TRY_PROTO_T:
1415                         msg->msg_flags|= (unsigned int)a->val[0].u.number & FL_MTU_FB_MASK;
1416                         ret=1; /* continue processing */
1417                         break;
1418                 case SET_ADV_ADDR_T:
1419                         if (a->val[0].type!=STR_ST){
1420                                 LOG(L_CRIT, "BUG: do_action: bad set_advertised_address() "
1421                                                 "type %d\n", a->val[0].type);
1422                                 ret=E_BUG;
1423                                 goto error;
1424                         }
1425                         msg->set_global_address=*((str*)a->val[0].u.data);
1426                         ret=1; /* continue processing */
1427                         break;
1428                 case SET_ADV_PORT_T:
1429                         if (a->val[0].type!=STR_ST){
1430                                 LOG(L_CRIT, "BUG: do_action: bad set_advertised_port() "
1431                                                 "type %d\n", a->val[0].type);
1432                                 ret=E_BUG;
1433                                 goto error;
1434                         }
1435                         msg->set_global_port=*((str*)a->val[0].u.data);
1436                         ret=1; /* continue processing */
1437                         break;
1438 #ifdef USE_TCP
1439                 case FORCE_TCP_ALIAS_T:
1440                         if ( msg->rcv.proto==PROTO_TCP
1441 #ifdef USE_TLS
1442                                         || msg->rcv.proto==PROTO_TLS
1443 #endif
1444                            ){
1445
1446                                 if (a->val[0].type==NOSUBTYPE)  port=msg->via1->port;
1447                                 else if (a->val[0].type==NUMBER_ST) port=(int)a->val[0].u.number;
1448                                 else{
1449                                         LOG(L_CRIT, "BUG: do_action: bad force_tcp_alias"
1450                                                         " port type %d\n", a->val[0].type);
1451                                         ret=E_BUG;
1452                                         goto error;
1453                                 }
1454
1455                                 if (tcpconn_add_alias(msg->rcv.proto_reserved1, port,
1456                                                                         msg->rcv.proto)!=0){
1457                                         LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n");
1458                                         ret=E_UNSPEC;
1459                                         goto error;
1460                                 }
1461                         }
1462 #endif
1463                         ret=1; /* continue processing */
1464                         break;
1465                 case FORCE_SEND_SOCKET_T:
1466                         if (a->val[0].type!=SOCKETINFO_ST){
1467                                 LOG(L_CRIT, "BUG: do_action: bad force_send_socket argument"
1468                                                 " type: %d\n", a->val[0].type);
1469                                 ret=E_BUG;
1470                                 goto error;
1471                         }
1472                         set_force_socket(msg, (struct socket_info*)a->val[0].u.data);
1473                         ret=1; /* continue processing */
1474                         break;
1475
1476                 case ADD_T:
1477                 case ASSIGN_T:
1478                         v=lval_assign(h, msg, (struct lvalue*)a->val[0].u.data,
1479                                                                   (struct rval_expr*)a->val[1].u.data);
1480                         if (likely(v>=0))
1481                                 ret = 1;
1482                         else if (unlikely (v == EXPR_DROP)) /* hack to quit on DROP*/
1483                                 ret=0;
1484                         else
1485                                 ret=v;
1486                         break;
1487                 case SET_FWD_NO_CONNECT_T:
1488                         msg->fwd_send_flags.f|= SND_F_FORCE_CON_REUSE;
1489                         ret=1; /* continue processing */
1490                         break;
1491                 case SET_RPL_NO_CONNECT_T:
1492                         msg->rpl_send_flags.f|= SND_F_FORCE_CON_REUSE;
1493                         ret=1; /* continue processing */
1494                         break;
1495                 case SET_FWD_CLOSE_T:
1496                         msg->fwd_send_flags.f|= SND_F_CON_CLOSE;
1497                         ret=1; /* continue processing */
1498                         break;
1499                 case SET_RPL_CLOSE_T:
1500                         msg->rpl_send_flags.f|= SND_F_CON_CLOSE;
1501                         ret=1; /* continue processing */
1502                         break;
1503                 case CFG_SELECT_T:
1504                         if (a->val[0].type != CFG_GROUP_ST) {
1505                                 BUG("unsupported parameter in CFG_SELECT_T: %d\n",
1506                                                 a->val[0].type);
1507                                 ret=-1;
1508                                 goto error;
1509                         }
1510                         switch(a->val[1].type) {
1511                                 case NUMBER_ST:
1512                                         v=(int)a->val[1].u.number;
1513                                         break;
1514                                 case RVE_ST:
1515                                         if (rval_expr_eval_int(h, msg, &v, (struct rval_expr*)a->val[1].u.data) < 0) {
1516                                                 ret=-1;
1517                                                 goto error;
1518                                         }
1519                                         break;
1520                                 default:
1521                                         BUG("unsupported group id type in CFG_SELECT_T: %d\n",
1522                                                         a->val[1].type);
1523                                         ret=-1;
1524                                         goto error;
1525                         }
1526                         ret=(cfg_select((cfg_group_t*)a->val[0].u.data, v) == 0) ? 1 : -1;
1527                         break;
1528                 case CFG_RESET_T:
1529                         if (a->val[0].type != CFG_GROUP_ST) {
1530                                 BUG("unsupported parameter in CFG_RESET_T: %d\n",
1531                                                 a->val[0].type);
1532                                 ret=-1;
1533                                 goto error;
1534                         }
1535                         ret=(cfg_reset((cfg_group_t*)a->val[0].u.data) == 0) ? 1 : -1;
1536                         break;
1537 /*
1538                 default:
1539                         LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
1540 */
1541         }
1542 skip:
1543         return ret;
1544
1545 error_uri:
1546         LOG(L_ERR, "ERROR: do_action: set*: uri too long\n");
1547         if (new_uri) pkg_free(new_uri);
1548         LM_ERR("run action error at: %s:%d\n", (a->cfile)?a->cfile:"", a->cline);
1549         return E_UNSPEC;
1550 error_fwd_uri:
1551         /*free_uri(&uri); -- not needed anymore, using msg->parsed_uri*/
1552 error:
1553         LM_ERR("run action error at: %s:%d\n", (a->cfile)?a->cfile:"", a->cline);
1554         return ret;
1555 }
1556
1557
1558
1559 /* returns: 0, or 1 on success, <0 on error */
1560 /* (0 if drop or break encountered, 1 if not ) */
1561 int run_actions(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
1562 {
1563         struct action* t;
1564         int ret;
1565         struct sr_module *mod;
1566
1567         ret=E_UNSPEC;
1568         h->rec_lev++;
1569         if (unlikely(h->rec_lev>ROUTE_MAX_REC_LEV)){
1570                 LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
1571                                         " giving up!\n", h->rec_lev);
1572                 ret=E_UNSPEC;
1573                 goto error;
1574         }
1575         if (unlikely(h->rec_lev==1)){
1576                 h->run_flags=0;
1577                 h->last_retcode=0;
1578                 _last_returned_code = h->last_retcode;
1579 #ifdef USE_LONGJMP
1580                 if (unlikely(setjmp(h->jmp_env))){
1581                         h->rec_lev=0;
1582                         ret=h->last_retcode;
1583                         goto end;
1584                 }
1585 #endif
1586         }
1587
1588         if (unlikely(a==0)){
1589                 DBG("DEBUG: run_actions: null action list (rec_level=%d)\n",
1590                                 h->rec_lev);
1591                 ret=1;
1592         }
1593
1594         for (t=a; t!=0; t=t->next){
1595                 ret=do_action(h, t, msg);
1596                 /* break, return or drop/exit stop execution of the current
1597                    block */
1598                 if (unlikely(h->run_flags & (BREAK_R_F|RETURN_R_F|EXIT_R_F))){
1599                         if (unlikely(h->run_flags & EXIT_R_F)) {
1600                                 h->last_retcode=ret;
1601                                 _last_returned_code = h->last_retcode;
1602 #ifdef USE_LONGJMP
1603                                 longjmp(h->jmp_env, ret);
1604 #endif
1605                         }
1606                         break;
1607                 }
1608                 /* ignore error returns */
1609         }
1610
1611         h->rec_lev--;
1612 end:
1613         /* process module onbreak handlers if present */
1614         if (unlikely(h->rec_lev==0 && ret==0 &&
1615                                         !(h->run_flags & IGNORE_ON_BREAK_R_F)))
1616                 for (mod=modules;mod;mod=mod->next)
1617                         if (unlikely(mod->exports.onbreak_f)) {
1618                                 mod->exports.onbreak_f( msg );
1619                         }
1620         return ret;
1621
1622
1623 error:
1624         h->rec_lev--;
1625         return ret;
1626 }
1627
1628
1629
1630 #ifdef USE_LONGJMP
1631 /** safe version of run_actions().
1632  * It always return (it doesn't longjmp on forced script end).
1633  * @returns 0, or 1 on success, <0 on error
1634  * (0 if drop or break encountered, 1 if not ) */
1635 int run_actions_safe(struct run_act_ctx* h, struct action* a,
1636                                                 struct sip_msg* msg)
1637 {
1638         struct run_act_ctx ctx;
1639         int ret;
1640         int ign_on_break;
1641         
1642         /* start with a fresh action context */
1643         init_run_actions_ctx(&ctx);
1644         ctx.last_retcode = h->last_retcode;
1645         ign_on_break = h->run_flags & IGNORE_ON_BREAK_R_F;
1646         ctx.run_flags = h->run_flags | IGNORE_ON_BREAK_R_F;
1647         ret = run_actions(&ctx, a, msg);
1648         h->last_retcode = ctx.last_retcode;
1649         h->run_flags = (ctx.run_flags & ~IGNORE_ON_BREAK_R_F) | ign_on_break;
1650         return ret;
1651 }
1652 #endif /* USE_LONGJMP */
1653
1654
1655
1656 int run_top_route(struct action* a, sip_msg_t* msg, struct run_act_ctx *c)
1657 {
1658         struct run_act_ctx ctx;
1659         struct run_act_ctx *p;
1660         int ret;
1661         flag_t sfbk;
1662
1663         p = (c)?c:&ctx;
1664         sfbk = getsflags();
1665         setsflagsval(0);
1666         reset_static_buffer();
1667         init_run_actions_ctx(p);
1668         ret = run_actions(p, a, msg);
1669         setsflagsval(sfbk);
1670         return ret;
1671 }