317b19227ed5c67a7ce0ae4872e411ec35357be9
[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         struct switch_cond_table* sct;
306         struct switch_jmp_table*  sjt;
307         struct rval_expr* rve;
308         struct match_cond_table* mct;
309         struct rvalue* rv;
310         struct rvalue* rv1;
311         struct rval_cache c1;
312         str s;
313         void *srevp[2];
314         /* temporary storage space for a struct action.val[] working copy
315          (needed to transform RVE intro STRING before calling module
316            functions). [0] is not used (corresp. to the module export pointer),
317            [1] contains the number of params, and [2..] the param values.
318            We need [1], because some fixup function use it
319           (see fixup_get_param_count()).  */
320         static action_u_t mod_f_params[MAX_ACTIONS];
321
322         /* reset the value of error to E_UNSPEC so avoid unknowledgable
323            functions to return with error (status<0) and not setting it
324            leaving there previous error; cache the previous value though
325            for functions which want to process it */
326         prev_ser_error=ser_error;
327         ser_error=E_UNSPEC;
328
329         /* hook for every executed action (in use by cfg debugger) */
330         if(unlikely(sr_event_enabled(SREV_CFG_RUN_ACTION)))
331         {
332                 srevp[0] = (void*)a;
333                 srevp[1] = (void*)msg;
334                 sr_event_exec(SREV_CFG_RUN_ACTION, (void*)srevp);
335         }
336
337         ret=E_BUG;
338         switch ((unsigned char)a->type){
339                 case DROP_T:
340                                 switch(a->val[0].type){
341                                         case NUMBER_ST:
342                                                 ret=(int) a->val[0].u.number;
343                                                 break;
344                                         case RVE_ST:
345                                                 rve=(struct rval_expr*)a->val[0].u.data;
346                                                 rval_expr_eval_int(h, msg, &ret, rve);
347                                                 break;
348                                         case RETCODE_ST:
349                                                 ret=h->last_retcode;
350                                                 break;
351                                         default:
352                                                 BUG("unexpected subtype %d in DROP_T\n",
353                                                                 a->val[0].type);
354                                                 ret=0;
355                                                 goto error;
356                                 }
357                                 h->run_flags|=(unsigned int)a->val[1].u.number;
358                         break;
359                 case FORWARD_T:
360 #ifdef USE_TCP
361                 case FORWARD_TCP_T:
362 #endif
363 #ifdef USE_TLS
364                 case FORWARD_TLS_T:
365 #endif
366 #ifdef USE_SCTP
367                 case FORWARD_SCTP_T:
368 #endif
369                 case FORWARD_UDP_T:
370                         /* init dst */
371                         init_dest_info(&dst);
372                         if (a->type==FORWARD_UDP_T) dst.proto=PROTO_UDP;
373 #ifdef USE_TCP
374                         else if (a->type==FORWARD_TCP_T) dst.proto= PROTO_TCP;
375 #endif
376 #ifdef USE_TLS
377                         else if (a->type==FORWARD_TLS_T) dst.proto= PROTO_TLS;
378 #endif
379 #ifdef USE_SCTP
380                         else if (a->type==FORWARD_SCTP_T) dst.proto=PROTO_SCTP;
381 #endif
382                         else dst.proto=PROTO_NONE;
383                         if (a->val[0].type==URIHOST_ST){
384                                 /*parse uri*/
385
386                                 if (msg->dst_uri.len) {
387                                         ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len,
388                                                                         &next_hop);
389                                         u = &next_hop;
390                                 } else {
391                                         ret = parse_sip_msg_uri(msg);
392                                         u = &msg->parsed_uri;
393                                 }
394
395                                 if (ret<0) {
396                                         LOG(L_ERR, "ERROR: do_action: forward: bad_uri "
397                                                                 " dropping packet\n");
398                                         goto error;
399                                 }
400
401                                 switch (a->val[1].type){
402                                         case URIPORT_ST:
403                                                                         port=u->port_no;
404                                                                         break;
405                                         case NUMBER_ST:
406                                                                         port=a->val[1].u.number;
407                                                                         break;
408                                         default:
409                                                         LOG(L_CRIT, "BUG: do_action bad forward 2nd"
410                                                                                 " param type (%d)\n", a->val[1].type);
411                                                         ret=E_UNSPEC;
412                                                         goto error_fwd_uri;
413                                 }
414                                 if (dst.proto == PROTO_NONE){ /* only if proto not set get it
415                                                                                          from the uri */
416                                         switch(u->proto){
417                                                 case PROTO_NONE:
418                                                         /*dst.proto=PROTO_UDP; */
419                                                         /* no proto, try to get it from the dns */
420                                                         break;
421                                                 case PROTO_UDP:
422 #ifdef USE_TCP
423                                                 case PROTO_TCP:
424 #endif
425 #ifdef USE_TLS
426                                                 case PROTO_TLS:
427 #endif
428 #ifdef USE_SCTP
429                                                 case PROTO_SCTP:
430 #endif
431                                                         dst.proto=u->proto;
432                                                         break;
433                                                 default:
434                                                         LOG(L_ERR,"ERROR: do action: forward: bad uri"
435                                                                         " transport %d\n", u->proto);
436                                                         ret=E_BAD_PROTO;
437                                                         goto error_fwd_uri;
438                                         }
439 #ifdef USE_TLS
440                                         if (u->type==SIPS_URI_T){
441                                                 if (u->proto==PROTO_UDP){
442                                                         LOG(L_ERR, "ERROR: do_action: forward: secure uri"
443                                                                         " incompatible with transport %d\n",
444                                                                         u->proto);
445                                                         ret=E_BAD_PROTO;
446                                                         goto error_fwd_uri;
447                                                 }
448                                                 dst.proto=PROTO_TLS;
449                                         }
450 #endif
451                                 }
452
453 #ifdef HONOR_MADDR
454                                 if (u->maddr_val.s && u->maddr_val.len)
455                                         dst_host=&u->maddr_val;
456                                 else
457 #endif
458                                         dst_host=&u->host;
459 #ifdef USE_COMP
460                                 dst.comp=u->comp;
461 #endif
462                                 ret=forward_request(msg, dst_host, port, &dst);
463                                 if (ret>=0){
464                                         ret=1;
465                                 }
466                         }else if ((a->val[0].type==PROXY_ST) && (a->val[1].type==NUMBER_ST)){
467                                 if (dst.proto==PROTO_NONE)
468                                         dst.proto=msg->rcv.proto;
469                                 proxy2su(&dst.to,  (struct proxy_l*)a->val[0].u.data);
470                                 ret=forward_request(msg, 0, 0, &dst);
471                                 if (ret>=0){
472                                         ret=1;
473                                         proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
474                                 }else if (ser_error!=E_OK){
475                                         proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
476                                 }
477                         }else{
478                                 LOG(L_CRIT, "BUG: do_action: bad forward() types %d, %d\n",
479                                                 a->val[0].type, a->val[1].type);
480                                 ret=E_BUG;
481                                 goto error;
482                         }
483                         break;
484                 case SEND_T:
485                 case SEND_TCP_T:
486                         if ((a->val[0].type!= PROXY_ST)|(a->val[1].type!=NUMBER_ST)){
487                                 LOG(L_CRIT, "BUG: do_action: bad send() types %d, %d\n",
488                                                 a->val[0].type, a->val[1].type);
489                                 ret=E_BUG;
490                                 goto error;
491                         }
492                         /* init dst */
493                         init_dest_info(&dst);
494                         ret=proxy2su(&dst.to,  (struct proxy_l*)a->val[0].u.data);
495                         if (ret==0){
496                                 if (p_onsend){
497                                         tmp=p_onsend->buf;
498                                         len=p_onsend->len;
499                                 }else{
500                                         tmp=msg->buf;
501                                         len=msg->len;
502                                 }
503                                 if (a->type==SEND_T){
504                                         /*udp*/
505                                         dst.proto=PROTO_UDP; /* not really needed for udp_send */
506                                         dst.send_sock=get_send_socket(msg, &dst.to, PROTO_UDP);
507                                         if (dst.send_sock!=0){
508                                                 ret=udp_send(&dst, tmp, len);
509                                         }else{
510                                                 ret=-1;
511                                         }
512                                 }
513 #ifdef USE_TCP
514                                         else{
515                                                 /*tcp*/
516                                                 dst.proto=PROTO_TCP;
517                                                 dst.id=0;
518                                                 ret=tcp_send(&dst, 0, tmp, len);
519                                 }
520 #endif
521                         }else{
522                                 ret=E_BUG;
523                                 goto error;
524                         }
525                         proxy_mark((struct proxy_l*)a->val[0].u.data, ret);
526                         if (ret>=0) ret=1;
527
528                         break;
529                 case LOG_T:
530                         if ((a->val[0].type!=NUMBER_ST)|(a->val[1].type!=STRING_ST)){
531                                 LOG(L_CRIT, "BUG: do_action: bad log() types %d, %d\n",
532                                                 a->val[0].type, a->val[1].type);
533                                 ret=E_BUG;
534                                 goto error;
535                         }
536                         LOG_(DEFAULT_FACILITY, a->val[0].u.number, "<script>: ", "%s", 
537                                  a->val[1].u.string);
538                         ret=1;
539                         break;
540
541                 /* jku -- introduce a new branch */
542                 case APPEND_BRANCH_T:
543                         if (unlikely(a->val[0].type!=STR_ST)) {
544                                 LOG(L_CRIT, "BUG: do_action: bad append_branch_t %d\n",
545                                         a->val[0].type );
546                                 ret=E_BUG;
547                                 goto error;
548                         }
549                         getbflagsval(0, (flag_t*)&flags);
550                         ret=append_branch(msg, &a->val[0].u.str, &msg->dst_uri,
551                                                                 &msg->path_vec, a->val[1].u.number,
552                                                                 (flag_t)flags, msg->force_send_socket);
553                         break;
554
555                 /* jku begin: is_length_greater_than */
556                 case LEN_GT_T:
557                         if (a->val[0].type!=NUMBER_ST) {
558                                 LOG(L_CRIT, "BUG: do_action: bad len_gt type %d\n",
559                                         a->val[0].type );
560                                 ret=E_BUG;
561                                 goto error;
562                         }
563                         /* DBG("XXX: message length %d, max %d\n",
564                                 msg->len, a->val[0].u.number ); */
565                         ret = msg->len >= a->val[0].u.number ? 1 : -1;
566                         break;
567                 /* jku end: is_length_greater_than */
568
569                 /* jku - begin : flag processing */
570
571                 case SETFLAG_T:
572                         if (a->val[0].type!=NUMBER_ST) {
573                                 LOG(L_CRIT, "BUG: do_action: bad setflag() type %d\n",
574                                         a->val[0].type );
575                                 ret=E_BUG;
576                                 goto error;
577                         }
578                         if (!flag_in_range( a->val[0].u.number )) {
579                                 ret=E_CFG;
580                                 goto error;
581                         }
582                         setflag( msg, a->val[0].u.number );
583                         ret=1;
584                         break;
585
586                 case RESETFLAG_T:
587                         if (a->val[0].type!=NUMBER_ST) {
588                                 LOG(L_CRIT, "BUG: do_action: bad resetflag() type %d\n",
589                                         a->val[0].type );
590                                 ret=E_BUG;
591                                 goto error;
592                         }
593                         if (!flag_in_range( a->val[0].u.number )) {
594                                 ret=E_CFG;
595                                 goto error;
596                         }
597                         resetflag( msg, a->val[0].u.number );
598                         ret=1;
599                         break;
600
601                 case ISFLAGSET_T:
602                         if (a->val[0].type!=NUMBER_ST) {
603                                 LOG(L_CRIT, "BUG: do_action: bad isflagset() type %d\n",
604                                         a->val[0].type );
605                                 ret=E_BUG;
606                                 goto error;
607                         }
608                         if (!flag_in_range( a->val[0].u.number )) {
609                                 ret=E_CFG;
610                                 goto error;
611                         }
612                         ret=isflagset( msg, a->val[0].u.number );
613                         break;
614                 /* jku - end : flag processing */
615
616                 case AVPFLAG_OPER_T:  {
617                         struct search_state st;
618                         avp_t* avp;
619                         int flag;
620                         ret = 0;
621                         flag = a->val[1].u.number;
622                         if ((a->val[0].u.attr->type & AVP_INDEX_ALL) == AVP_INDEX_ALL || (a->val[0].u.attr->type & AVP_NAME_RE)!=0) {
623                                 for (avp=search_first_avp(a->val[0].u.attr->type, a->val[0].u.attr->name, NULL, &st); avp; avp = search_next_avp(&st, NULL)) {
624                                         switch (a->val[2].u.number) {   /* oper: 0..reset, 1..set, -1..no change */
625                                                 case 0:
626                                                         avp->flags &= ~(avp_flags_t)a->val[1].u.number;
627                                                         break;
628                                                 case 1:
629                                                         avp->flags |= (avp_flags_t)a->val[1].u.number;
630                                                         break;
631                                                 default:;
632                                         }
633                                         ret = ret || ((avp->flags & (avp_flags_t)a->val[1].u.number) != 0);
634                                 }
635                         }
636                         else {
637                                 avp = search_avp_by_index(a->val[0].u.attr->type, a->val[0].u.attr->name, NULL, a->val[0].u.attr->index);
638                                 if (avp) {
639                                         switch (a->val[2].u.number) {   /* oper: 0..reset, 1..set, -1..no change */
640                                                 case 0:
641                                                         avp->flags &= ~(avp_flags_t)a->val[1].u.number;
642                                                         break;
643                                                 case 1:
644                                                         avp->flags |= (avp_flags_t)a->val[1].u.number;
645                                                         break;
646                                                 default:;
647                                         }
648                                         ret = (avp->flags & (avp_flags_t)a->val[1].u.number) != 0;
649                                 }
650                         }
651                         if (ret==0)
652                                 ret = -1;
653                         break;
654                 }
655                 case ERROR_T:
656                         if ((a->val[0].type!=STRING_ST)|(a->val[1].type!=STRING_ST)){
657                                 LOG(L_CRIT, "BUG: do_action: bad error() types %d, %d\n",
658                                                 a->val[0].type, a->val[1].type);
659                                 ret=E_BUG;
660                                 goto error;
661                         }
662                         LOG(L_NOTICE, "WARNING: do_action: error(\"%s\", \"%s\") "
663                                         "not implemented yet\n", a->val[0].u.string, a->val[1].u.string);
664                         ret=1;
665                         break;
666                 case ROUTE_T:
667                         if (likely(a->val[0].type == NUMBER_ST))
668                                 i = a->val[0].u.number;
669                         else if (a->val[0].type == RVE_ST) {
670                                 rv = rval_expr_eval(h, msg, a->val[0].u.data);
671                                 rval_cache_init(&c1);
672                                 if (unlikely(rv == 0 ||
673                                                 rval_get_tmp_str(h, msg, &s, rv, 0, &c1) < 0)) {
674                                         rval_destroy(rv);
675                                         rval_cache_clean(&c1);
676                                         ERR("failed to convert RVE to string\n");
677                                         ret = E_UNSPEC;
678                                         goto error;
679                                 }
680                                 i = route_lookup(&main_rt, s.s);
681                                 if (unlikely(i < 0)) {
682                                         ERR("route \"%s\" not found at %s:%d\n",
683                                                         s.s, (a->cfile)?a->cfile:"line", a->cline);
684                                         rval_cache_clean(&c1);
685                                         s.s = 0;
686                                         ret = E_SCRIPT;
687                                         goto error;
688                                 }
689                                 rval_cache_clean(&c1);
690                                 s.s = 0;
691                         } else {
692                                 LOG(L_CRIT, "BUG: do_action: bad route() type %d\n",
693                                                 a->val[0].type);
694                                 ret=E_BUG;
695                                 goto error;
696                         }
697                         if (unlikely((i>=main_rt.idx)||(i<0))){
698                                 LOG(L_ERR, "ERROR: invalid routing table number in"
699                                                         "route(%lu) at %s:%d\n", a->val[0].u.number,
700                                                         (a->cfile)?a->cfile:"line", a->cline);
701                                 ret=E_CFG;
702                                 goto error;
703                         }
704                         /*ret=((ret=run_actions(rlist[a->val[0].u.number],msg))<0)?ret:1;*/
705                         ret=run_actions(h, main_rt.rlist[i], msg);
706                         h->last_retcode=ret;
707                         _last_returned_code = h->last_retcode;
708                         h->run_flags&=~(RETURN_R_F|BREAK_R_F); /* absorb return & break */
709                         break;
710                 case EXEC_T:
711                         if (a->val[0].type!=STRING_ST){
712                                 LOG(L_CRIT, "BUG: do_action: bad exec() type %d\n",
713                                                 a->val[0].type);
714                                 ret=E_BUG;
715                                 goto error;
716                         }
717                         LOG(L_NOTICE, "WARNING: exec(\"%s\") not fully implemented,"
718                                                 " using dumb version...\n", a->val[0].u.string);
719                         ret=system(a->val[0].u.string);
720                         if (ret!=0){
721                                 LOG(L_NOTICE, "WARNING: exec() returned %d\n", ret);
722                         }
723                         ret=1;
724                         break;
725                 case REVERT_URI_T:
726                         if (msg->new_uri.s) {
727                                 pkg_free(msg->new_uri.s);
728                                 msg->new_uri.len=0;
729                                 msg->new_uri.s=0;
730                                 msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
731                                 ruri_mark_new(); /* available for forking */
732                         };
733                         ret=1;
734                         break;
735                 case SET_HOST_T:
736                 case SET_HOSTPORT_T:
737                 case SET_HOSTPORTTRANS_T:
738                 case SET_HOSTALL_T:
739                 case SET_USER_T:
740                 case SET_USERPASS_T:
741                 case SET_PORT_T:
742                 case SET_URI_T:
743                 case PREFIX_T:
744                 case STRIP_T:
745                 case STRIP_TAIL_T:
746                 case SET_USERPHONE_T:
747                                 user=0;
748                                 if (a->type==STRIP_T || a->type==STRIP_TAIL_T) {
749                                         if (a->val[0].type!=NUMBER_ST) {
750                                                 LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
751                                                         a->val[0].type);
752                                                 ret=E_BUG;
753                                                 goto error;
754                                         }
755                                 } else if (a->type!=SET_USERPHONE_T) {
756                                         if (a->val[0].type!=STRING_ST) {
757                                                 LOG(L_CRIT, "BUG: do_action: bad set*() type %d\n",
758                                                         a->val[0].type);
759                                                 ret=E_BUG;
760                                                 goto error;
761                                         }
762                                 }
763                                 if (a->type==SET_URI_T){
764                                         if (msg->new_uri.s) {
765                                                         pkg_free(msg->new_uri.s);
766                                                         msg->new_uri.len=0;
767                                         }
768                                         msg->parsed_uri_ok=0;
769                                         len=strlen(a->val[0].u.string);
770                                         msg->new_uri.s=pkg_malloc(len+1);
771                                         if (msg->new_uri.s==0){
772                                                 LOG(L_ERR, "ERROR: do_action: memory allocation"
773                                                                 " failure\n");
774                                                 ret=E_OUT_OF_MEM;
775                                                 goto error;
776                                         }
777                                         memcpy(msg->new_uri.s, a->val[0].u.string, len);
778                                         msg->new_uri.s[len]=0;
779                                         msg->new_uri.len=len;
780                                         ruri_mark_new(); /* available for forking */
781
782                                         ret=1;
783                                         break;
784                                 }
785                                 if (msg->parsed_uri_ok==0) {
786                                         if (msg->new_uri.s) {
787                                                 tmp=msg->new_uri.s;
788                                                 len=msg->new_uri.len;
789                                         }else{
790                                                 tmp=msg->first_line.u.request.uri.s;
791                                                 len=msg->first_line.u.request.uri.len;
792                                         }
793                                         if (parse_uri(tmp, len, &uri)<0){
794                                                 LOG(L_ERR, "ERROR: do_action: bad uri <%s>, dropping"
795                                                                         " packet\n", tmp);
796                                                 ret=E_UNSPEC;
797                                                 goto error;
798                                         }
799                                 } else {
800                                         uri=msg->parsed_uri;
801                                 }
802
803                                 /* skip SET_USERPHONE_T action if the URI is already
804                                  * a tel: or tels: URI, or contains the user=phone param */
805                                 if ((a->type==SET_USERPHONE_T) 
806                                         && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T)
807                                                 || ((uri.user_param_val.len==5) && (memcmp(uri.user_param_val.s, "phone", 5)==0)))
808                                 ) {
809                                         ret=1;
810                                         break;
811                                 }
812                                 /* SET_PORT_T does not work with tel: URIs */
813                                 if ((a->type==SET_PORT_T)
814                                         && ((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
815                                         && ((uri.flags & URI_SIP_USER_PHONE)==0)
816                                 ) {
817                                         LOG(L_ERR, "ERROR: do_action: port number of a tel: URI cannot be set\n");
818                                         ret=E_UNSPEC;
819                                         goto error;
820                                 }
821
822                                 new_uri=pkg_malloc(MAX_URI_SIZE);
823                                 if (new_uri==0){
824                                         LOG(L_ERR, "ERROR: do_action: memory allocation "
825                                                                 " failure\n");
826                                         ret=E_OUT_OF_MEM;
827                                         goto error;
828                                 }
829                                 end=new_uri+MAX_URI_SIZE;
830                                 crt=new_uri;
831                                 /* begin copying */
832                                 /* Preserve the URI scheme unless the host part needs
833                                  * to be rewritten, and the shceme is tel: or tels: */
834                                 switch (uri.type) {
835                                 case SIP_URI_T:
836                                         len=s_sip.len;
837                                         tmp=s_sip.s;
838                                         break;
839
840                                 case SIPS_URI_T:
841                                         len=s_sips.len;
842                                         tmp=s_sips.s;
843                                         break;
844
845                                 case TEL_URI_T:
846                                         if ((uri.flags & URI_SIP_USER_PHONE)
847                                                 || (a->type==SET_HOST_T)
848                                                 || (a->type==SET_HOSTPORT_T)
849                                                 || (a->type==SET_HOSTPORTTRANS_T)
850                                         ) {
851                                                 len=s_sip.len;
852                                                 tmp=s_sip.s;
853                                                 break;
854                                         }
855                                         len=s_tel.len;
856                                         tmp=s_tel.s;
857                                         break;
858
859                                 case TELS_URI_T:
860                                         if ((uri.flags & URI_SIP_USER_PHONE)
861                                                 || (a->type==SET_HOST_T)
862                                                 || (a->type==SET_HOSTPORT_T)
863                                                 || (a->type==SET_HOSTPORTTRANS_T)
864                                         ) {
865                                                 len=s_sips.len;
866                                                 tmp=s_sips.s;
867                                                 break;
868                                         }
869                                         len=s_tels.len;
870                                         tmp=s_tels.s;
871                                         break;
872
873                                 default:
874                                         LOG(L_ERR, "ERROR: Unsupported URI scheme (%d), "
875                                                 "reverted to sip:\n",
876                                                 uri.type);
877                                         len=s_sip.len;
878                                         tmp=s_sip.s;
879                                 }
880                                 if(crt+len+1 /* colon */ >end) goto error_uri;
881                                 memcpy(crt,tmp,len);crt+=len;
882                                 *crt=':'; crt++;
883
884                                 /* user */
885
886                                 /* prefix (-jiri) */
887                                 if (a->type==PREFIX_T) {
888                                         tmp=a->val[0].u.string;
889                                         len=strlen(tmp); if(crt+len>end) goto error_uri;
890                                         memcpy(crt,tmp,len);crt+=len;
891                                         /* whatever we had before, with prefix we have username
892                                            now */
893                                         user=1;
894                                 }
895
896                                 if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
897                                         tmp=a->val[0].u.string;
898                                         len=strlen(tmp);
899                                 } else if (a->type==STRIP_T) {
900                                         if (a->val[0].u.number>uri.user.len) {
901                                                 LOG(L_WARN, "Error: too long strip asked; "
902                                                                         " deleting username: %lu of <%.*s>\n",
903                                                                         a->val[0].u.number, uri.user.len, uri.user.s );
904                                                 len=0;
905                                         } else if (a->val[0].u.number==uri.user.len) {
906                                                 len=0;
907                                         } else {
908                                                 tmp=uri.user.s + a->val[0].u.number;
909                                                 len=uri.user.len - a->val[0].u.number;
910                                         }
911                                 } else if (a->type==STRIP_TAIL_T) {
912                                         if (a->val[0].u.number>uri.user.len) {
913                                                 LOG(L_WARN, "WARNING: too long strip_tail 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;
921                                                 len=uri.user.len - a->val[0].u.number;
922                                         }
923                                 } else {
924                                         tmp=uri.user.s;
925                                         len=uri.user.len;
926                                 }
927
928                                 if (len){
929                                         if(crt+len>end) goto error_uri;
930                                         memcpy(crt,tmp,len);crt+=len;
931                                         user=1; /* we have an user field so mark it */
932                                 }
933
934                                 if (a->type==SET_USERPASS_T) tmp=0;
935                                 else tmp=uri.passwd.s;
936                                 /* passwd */
937                                 if (tmp){
938                                         len=uri.passwd.len; if(crt+len+1>end) goto error_uri;
939                                         *crt=':'; crt++;
940                                         memcpy(crt,tmp,len);crt+=len;
941                                 }
942                                 /* tel: URI parameters */
943                                 if ((uri.type==TEL_URI_T)
944                                         || (uri.type==TELS_URI_T)
945                                 ) {
946                                         tmp=uri.params.s;
947                                         if (tmp){
948                                                 len=uri.params.len; if(crt+len+1>end) goto error_uri;
949                                                 *crt=';'; crt++;
950                                                 memcpy(crt,tmp,len);crt+=len;
951                                         }
952                                 }
953                                 /* host */
954                                 if ((a->type==SET_HOST_T)
955                                                 || (a->type==SET_HOSTPORT_T)
956                                                 || (a->type==SET_HOSTALL_T)
957                                                 || (a->type==SET_HOSTPORTTRANS_T)
958                                 ) {
959                                         tmp=a->val[0].u.string;
960                                         if (tmp) len = strlen(tmp);
961                                         else len=0;
962                                 } else if ((uri.type==SIP_URI_T)
963                                         || (uri.type==SIPS_URI_T)
964                                         || (uri.flags & URI_SIP_USER_PHONE)
965                                 ) {
966                                         tmp=uri.host.s;
967                                         len=uri.host.len;
968                                 } else {
969                                         tmp=0;
970                                 }
971                                 if (tmp){
972                                         if (user) { /* add @ */
973                                                 if(crt+1>end) goto error_uri;
974                                                 *crt='@'; crt++;
975                                         }
976                                         if(crt+len>end) goto error_uri;
977                                         memcpy(crt,tmp,len);crt+=len;
978                                 }
979                                 if(a->type==SET_HOSTALL_T)
980                                         goto done_seturi;
981                                 /* port */
982                                 if ((a->type==SET_HOSTPORT_T)
983                                                 || (a->type==SET_HOSTPORTTRANS_T))
984                                         tmp=0;
985                                 else if (a->type==SET_PORT_T) {
986                                         tmp=a->val[0].u.string;
987                                         if (tmp) len = strlen(tmp);
988                                         else len = 0;
989                                 } else {
990                                         tmp=uri.port.s;
991                                         len = uri.port.len;
992                                 }
993                                 if (tmp){
994                                         if(crt+len+1>end) goto error_uri;
995                                         *crt=':'; crt++;
996                                         memcpy(crt,tmp,len);crt+=len;
997                                 }
998                                 /* params */
999                                 if ((a->type==SET_HOSTPORTTRANS_T)
1000                                         && uri.sip_params.s
1001                                         && uri.transport.s
1002                                 ) {
1003                                         /* bypass the transport parameter */
1004                                         if (uri.sip_params.s < uri.transport.s) {
1005                                                 /* there are parameters before transport */
1006                                                 len = uri.transport.s - uri.sip_params.s - 1;
1007                                                         /* ignore the ';' at the end */
1008                                                 if (crt+len+1>end) goto error_uri;
1009                                                 *crt=';'; crt++;
1010                                                 memcpy(crt,uri.sip_params.s,len);crt+=len;
1011                                         }
1012                                         len = (uri.sip_params.s + uri.sip_params.len) -
1013                                                 (uri.transport.s + uri.transport.len);
1014                                         if (len > 0) {
1015                                                 /* there are parameters after transport */
1016                                                 if (crt+len>end) goto error_uri;
1017                                                 tmp = uri.transport.s + uri.transport.len;
1018                                                 memcpy(crt,tmp,len);crt+=len;
1019                                         }
1020                                 } else {
1021                                         tmp=uri.sip_params.s;
1022                                         if (tmp){
1023                                                 len=uri.sip_params.len; if(crt+len+1>end) goto error_uri;
1024                                                 *crt=';'; crt++;
1025                                                 memcpy(crt,tmp,len);crt+=len;
1026                                         }
1027                                 }
1028                                 /* Add the user=phone param if a tel: or tels:
1029                                  * URI was converted to sip: or sips:.
1030                                  * (host part of a tel/tels URI was set.)
1031                                  * Or in case of sip: URI and SET_USERPHONE_T action */
1032                                 if (((((uri.type==TEL_URI_T) || (uri.type==TELS_URI_T))
1033                                         && ((uri.flags & URI_SIP_USER_PHONE)==0))
1034                                         && ((a->type==SET_HOST_T)
1035                                                 || (a->type==SET_HOSTPORT_T)
1036                                                 || (a->type==SET_HOSTPORTTRANS_T)))
1037                                         || (a->type==SET_USERPHONE_T)
1038                                 ) {
1039                                         tmp=";user=phone";
1040                                         len=strlen(tmp);
1041                                         if(crt+len>end) goto error_uri;
1042                                         memcpy(crt,tmp,len);crt+=len;
1043                                 }
1044                                 /* headers */
1045                                 tmp=uri.headers.s;
1046                                 if (tmp){
1047                                         len=uri.headers.len; if(crt+len+1>end) goto error_uri;
1048                                         *crt='?'; crt++;
1049                                         memcpy(crt,tmp,len);crt+=len;
1050                                 }
1051         done_seturi:
1052                                 *crt=0; /* null terminate the thing */
1053                                 /* copy it to the msg */
1054                                 if (msg->new_uri.s) pkg_free(msg->new_uri.s);
1055                                 msg->new_uri.s=new_uri;
1056                                 msg->new_uri.len=crt-new_uri;
1057                                 msg->parsed_uri_ok=0;
1058                                 ruri_mark_new(); /* available for forking */
1059                                 ret=1;
1060                                 break;
1061                 case IF_T:
1062                                         rve=(struct rval_expr*)a->val[0].u.data;
1063                                         if (unlikely(rval_expr_eval_int(h, msg, &v, rve) != 0)){
1064                                                 ERR("if expression evaluation failed (%d,%d-%d,%d)\n",
1065                                                                 rve->fpos.s_line, rve->fpos.s_col,
1066                                                                 rve->fpos.e_line, rve->fpos.e_col);
1067                                                 v=0; /* false */
1068                                         }
1069                                         if (unlikely(h->run_flags & EXIT_R_F)){
1070                                                 ret=0;
1071                                                 break;
1072                                         }
1073                                         h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
1074                                                                                                                             break in expr*/
1075                                         ret=1;  /*default is continue */
1076                                         if (v>0) {
1077                                                 if ((a->val[1].type==ACTIONS_ST)&&a->val[1].u.data){
1078                                                         ret=run_actions(h,
1079                                                                                 (struct action*)a->val[1].u.data, msg);
1080                                                 }
1081                                         }else if ((a->val[2].type==ACTIONS_ST)&&a->val[2].u.data){
1082                                                         ret=run_actions(h,
1083                                                                                 (struct action*)a->val[2].u.data, msg);
1084                                         }
1085                         break;
1086                 case MODULE0_T:
1087                         MODF_CALL(cmd_function, h, msg, a->val, 0, 0);
1088                         break;
1089                 /* instead of using the parameter number, we use different names
1090                  * for calls to functions with 3, 4, 5, 6 or variable number of
1091                  * parameters due to performance reasons */
1092                 case MODULE1_T:
1093                         MODF_CALL(cmd_function, h, msg, a->val,
1094                                                                                 (char*)a->val[2].u.data,
1095                                                                                 0
1096                                         );
1097                         break;
1098                 case MODULE2_T:
1099                         MODF_CALL(cmd_function, h, msg, a->val,
1100                                                                                 (char*)a->val[2].u.data,
1101                                                                                 (char*)a->val[3].u.data
1102                                         );
1103                         break;
1104                 case MODULE3_T:
1105                         MODF_CALL(cmd_function3, h, msg, a->val,
1106                                                                                 (char*)a->val[2].u.data,
1107                                                                                 (char*)a->val[3].u.data,
1108                                                                                 (char*)a->val[4].u.data
1109                                         );
1110                         break;
1111                 case MODULE4_T:
1112                         MODF_CALL(cmd_function4, h, msg, a->val,
1113                                                                                 (char*)a->val[2].u.data,
1114                                                                                 (char*)a->val[3].u.data,
1115                                                                                 (char*)a->val[4].u.data,
1116                                                                                 (char*)a->val[5].u.data
1117                                         );
1118                         break;
1119                 case MODULE5_T:
1120                         MODF_CALL(cmd_function5, h, msg, a->val,
1121                                                                                 (char*)a->val[2].u.data,
1122                                                                                 (char*)a->val[3].u.data,
1123                                                                                 (char*)a->val[4].u.data,
1124                                                                                 (char*)a->val[5].u.data,
1125                                                                                 (char*)a->val[6].u.data
1126                                         );
1127                         break;
1128                 case MODULE6_T:
1129                         MODF_CALL(cmd_function6, h, msg, a->val,
1130                                                                                 (char*)a->val[2].u.data,
1131                                                                                 (char*)a->val[3].u.data,
1132                                                                                 (char*)a->val[4].u.data,
1133                                                                                 (char*)a->val[5].u.data,
1134                                                                                 (char*)a->val[6].u.data,
1135                                                                                 (char*)a->val[7].u.data
1136                                         );
1137                         break;
1138                 case MODULEX_T:
1139                         MODF_CALL(cmd_function_var, h, msg, a->val,
1140                                                         a->val[1].u.number, &a->val[2]);
1141                         break;
1142                 case MODULE1_RVE_T:
1143                         MODF_RVE_CALL(cmd_function, h, msg, a->val, mod_f_params,
1144                                                                                         (char*)mod_f_params[2].u.data,
1145                                                                                         0
1146                                         );
1147                         break;
1148                 case MODULE2_RVE_T:
1149                         MODF_RVE_CALL(cmd_function, h, msg, a->val, mod_f_params,
1150                                                                                         (char*)mod_f_params[2].u.data,
1151                                                                                         (char*)mod_f_params[3].u.data
1152                                         );
1153                         break;
1154                 case MODULE3_RVE_T:
1155                         MODF_RVE_CALL(cmd_function3, h, msg, a->val, mod_f_params,
1156                                                                                         (char*)mod_f_params[2].u.data,
1157                                                                                         (char*)mod_f_params[3].u.data,
1158                                                                                         (char*)mod_f_params[4].u.data
1159                                         );
1160                         break;
1161                 case MODULE4_RVE_T:
1162                         MODF_RVE_CALL(cmd_function4, h, msg, a->val, mod_f_params,
1163                                                                                         (char*)mod_f_params[2].u.data,
1164                                                                                         (char*)mod_f_params[3].u.data,
1165                                                                                         (char*)mod_f_params[4].u.data,
1166                                                                                         (char*)mod_f_params[5].u.data
1167                                         );
1168                         break;
1169                 case MODULE5_RVE_T:
1170                         MODF_RVE_CALL(cmd_function5, h, msg, a->val, mod_f_params,
1171                                                                                         (char*)mod_f_params[2].u.data,
1172                                                                                         (char*)mod_f_params[3].u.data,
1173                                                                                         (char*)mod_f_params[4].u.data,
1174                                                                                         (char*)mod_f_params[5].u.data,
1175                                                                                         (char*)mod_f_params[6].u.data
1176                                         );
1177                         break;
1178                 case MODULE6_RVE_T:
1179                         MODF_RVE_CALL(cmd_function6, h, msg, a->val, mod_f_params,
1180                                                                                         (char*)mod_f_params[2].u.data,
1181                                                                                         (char*)mod_f_params[3].u.data,
1182                                                                                         (char*)mod_f_params[4].u.data,
1183                                                                                         (char*)mod_f_params[5].u.data,
1184                                                                                         (char*)mod_f_params[6].u.data,
1185                                                                                         (char*)mod_f_params[7].u.data
1186                                         );
1187                         break;
1188                 case MODULEX_RVE_T:
1189                         MODF_RVE_CALL(cmd_function_var, h, msg, a->val, mod_f_params,
1190                                                         a->val[1].u.number, &mod_f_params[2]);
1191                         break;
1192                 case EVAL_T:
1193                         /* only eval the expression to account for possible
1194                            side-effect */
1195                         rval_expr_eval_int(h, msg, &v,
1196                                         (struct rval_expr*)a->val[0].u.data);
1197                         if (h->run_flags & EXIT_R_F){
1198                                 ret=0;
1199                                 break;
1200                         }
1201                         h->run_flags &= ~RETURN_R_F|BREAK_R_F; /* catch return & break in
1202                                                                                                           expr */
1203                         ret=1; /* default is continue */
1204                         break;
1205                 case SWITCH_COND_T:
1206                         sct=(struct switch_cond_table*)a->val[1].u.data;
1207                         if (unlikely( rval_expr_eval_int(h, msg, &v,
1208                                                                         (struct rval_expr*)a->val[0].u.data) <0)){
1209                                 /* handle error in expression => use default */
1210                                 ret=-1;
1211                                 goto sw_cond_def;
1212                         }
1213                         if (h->run_flags & EXIT_R_F){
1214                                 ret=0;
1215                                 break;
1216                         }
1217                         h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
1218                                                                                                             in expr */
1219                         ret=1; /* default is continue */
1220                         for(i=0; i<sct->n; i++)
1221                                 if (sct->cond[i]==v){
1222                                         if (likely(sct->jump[i])){
1223                                                 ret=run_actions(h, sct->jump[i], msg);
1224                                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1225                                                                                                            returns passthrough */
1226                                         }
1227                                         goto skip;
1228                                 }
1229 sw_cond_def:
1230                         if (sct->def){
1231                                 ret=run_actions(h, sct->def, msg);
1232                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1233                                                                                            returns passthrough */
1234                         }
1235                         break;
1236                 case SWITCH_JT_T:
1237                         sjt=(struct switch_jmp_table*)a->val[1].u.data;
1238                         if (unlikely( rval_expr_eval_int(h, msg, &v,
1239                                                                         (struct rval_expr*)a->val[0].u.data) <0)){
1240                                 /* handle error in expression => use default */
1241                                 ret=-1;
1242                                 goto sw_jt_def;
1243                         }
1244                         if (h->run_flags & EXIT_R_F){
1245                                 ret=0;
1246                                 break;
1247                         }
1248                         h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
1249                                                                                                             in expr */
1250                         ret=1; /* default is continue */
1251                         if (likely(v >= sjt->first && v <= sjt->last)){
1252                                 if (likely(sjt->tbl[v - sjt->first])){
1253                                         ret=run_actions(h, sjt->tbl[v - sjt->first], msg);
1254                                         h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1255                                                                                                    returns passthrough */
1256                                 }
1257                                 break; 
1258                         }else{
1259                                 for(i=0; i<sjt->rest.n; i++)
1260                                         if (sjt->rest.cond[i]==v){
1261                                                 if (likely(sjt->rest.jump[i])){
1262                                                         ret=run_actions(h, sjt->rest.jump[i], msg);
1263                                                         h->run_flags &= ~BREAK_R_F; /* catch breaks, but 
1264                                                                                                                    let returns pass */
1265                                                 }
1266                                                 goto skip;
1267                                         }
1268                         }
1269                         /* not found => try default */
1270 sw_jt_def:
1271                         if (sjt->rest.def){
1272                                 ret=run_actions(h, sjt->rest.def, msg);
1273                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1274                                                                                            returns passthrough */
1275                         }
1276                         break;
1277                 case BLOCK_T:
1278                         if (likely(a->val[0].u.data)){
1279                                 ret=run_actions(h, (struct action*)a->val[0].u.data, msg);
1280                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1281                                                                                            returns passthrough */
1282                         }
1283                         break;
1284                 case MATCH_COND_T:
1285                         mct=(struct match_cond_table*)a->val[1].u.data;
1286                         rval_cache_init(&c1);
1287                         rv=0;
1288                         rv1=0;
1289                         ret=rval_expr_eval_rvint(h, msg, &rv, &v, 
1290                                                                         (struct rval_expr*)a->val[0].u.data, &c1);
1291                                                                         
1292                         if (unlikely( ret<0)){
1293                                 /* handle error in expression => use default */
1294                                 ret=-1;
1295                                 goto match_cond_def;
1296                         }
1297                         if (h->run_flags & EXIT_R_F){
1298                                 ret=0;
1299                                 break;
1300                         }
1301                         h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return & break
1302                                                                                                             in expr */
1303                         if (likely(rv)){
1304                                 rv1=rval_convert(h, msg, RV_STR, rv, &c1);
1305                                 if (unlikely(rv1==0)){
1306                                         ret=-1;
1307                                         goto match_cond_def;
1308                                 }
1309                                 s=rv1->v.s;
1310                         }else{
1311                                 /* int result in v */
1312                                 rval_cache_clean(&c1);
1313                                 s.s=sint2str(v, &s.len);
1314                         }
1315                         ret=1; /* default is continue */
1316                         for(i=0; i<mct->n; i++)
1317                                 if (( mct->match[i].type==MATCH_STR &&
1318                                                 mct->match[i].l.s.len==s.len &&
1319                                                 memcmp(mct->match[i].l.s.s, s.s, s.len) == 0 ) ||
1320                                          ( mct->match[i].type==MATCH_RE &&
1321                                           regexec(mct->match[i].l.regex, s.s, 0, 0, 0) == 0)
1322                                         ){
1323                                         if (likely(mct->jump[i])){
1324                                                 ret=run_actions(h, mct->jump[i], msg);
1325                                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1326                                                                                                            returns passthrough */
1327                                         }
1328                                         goto match_cleanup;
1329                                 }
1330 match_cond_def:
1331                         if (mct->def){
1332                                 ret=run_actions(h, mct->def, msg);
1333                                 h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1334                                                                                            returns passthrough */
1335                         }
1336 match_cleanup:
1337                         if (rv1){
1338                                 rval_destroy(rv1);
1339                                 rval_destroy(rv);
1340                                 rval_cache_clean(&c1);
1341                         }else if (rv){
1342                                 rval_destroy(rv);
1343                                 rval_cache_clean(&c1);
1344                         }
1345                         break;
1346                 case WHILE_T:
1347                         i=0;
1348                         flags=0;
1349                         rve=(struct rval_expr*)a->val[0].u.data;
1350                         ret=1;
1351                         while(!(flags & (BREAK_R_F|RETURN_R_F|EXIT_R_F)) &&
1352                                         (rval_expr_eval_int(h, msg, &v, rve) == 0) && v){
1353                                 i++;
1354                                 if (unlikely(i > cfg_get(core, core_cfg, max_while_loops))){
1355                                         LOG(L_ERR, "ERROR: runaway while (%d, %d): more then"
1356                                                                 " %d loops\n", 
1357                                                                 rve->fpos.s_line, rve->fpos.s_col,
1358                                                                 cfg_get(core, core_cfg, max_while_loops));
1359                                         ret=-1;
1360                                         goto error;
1361                                 }
1362                                 if (likely(a->val[1].u.data)){
1363                                         ret=run_actions(h, (struct action*)a->val[1].u.data, msg);
1364                                         flags|=h->run_flags;
1365                                         h->run_flags &= ~BREAK_R_F; /* catch breaks, but let
1366                                                                                                    returns pass-through */
1367                                 }
1368                         }
1369                         break;
1370                 case FORCE_RPORT_T:
1371                         msg->msg_flags|=FL_FORCE_RPORT;
1372                         ret=1; /* continue processing */
1373                         break;
1374                 case ADD_LOCAL_RPORT_T:
1375                         msg->msg_flags|=FL_ADD_LOCAL_RPORT;
1376                         ret=1; /* continue processing */
1377                         break;
1378                 case UDP_MTU_TRY_PROTO_T:
1379                         msg->msg_flags|= (unsigned int)a->val[0].u.number & FL_MTU_FB_MASK;
1380                         ret=1; /* continue processing */
1381                         break;
1382                 case SET_ADV_ADDR_T:
1383                         if (a->val[0].type!=STR_ST){
1384                                 LOG(L_CRIT, "BUG: do_action: bad set_advertised_address() "
1385                                                 "type %d\n", a->val[0].type);
1386                                 ret=E_BUG;
1387                                 goto error;
1388                         }
1389                         msg->set_global_address=*((str*)a->val[0].u.data);
1390                         ret=1; /* continue processing */
1391                         break;
1392                 case SET_ADV_PORT_T:
1393                         if (a->val[0].type!=STR_ST){
1394                                 LOG(L_CRIT, "BUG: do_action: bad set_advertised_port() "
1395                                                 "type %d\n", a->val[0].type);
1396                                 ret=E_BUG;
1397                                 goto error;
1398                         }
1399                         msg->set_global_port=*((str*)a->val[0].u.data);
1400                         ret=1; /* continue processing */
1401                         break;
1402 #ifdef USE_TCP
1403                 case FORCE_TCP_ALIAS_T:
1404                         if ( msg->rcv.proto==PROTO_TCP
1405 #ifdef USE_TLS
1406                                         || msg->rcv.proto==PROTO_TLS
1407 #endif
1408                            ){
1409
1410                                 if (a->val[0].type==NOSUBTYPE)  port=msg->via1->port;
1411                                 else if (a->val[0].type==NUMBER_ST) port=(int)a->val[0].u.number;
1412                                 else{
1413                                         LOG(L_CRIT, "BUG: do_action: bad force_tcp_alias"
1414                                                         " port type %d\n", a->val[0].type);
1415                                         ret=E_BUG;
1416                                         goto error;
1417                                 }
1418
1419                                 if (tcpconn_add_alias(msg->rcv.proto_reserved1, port,
1420                                                                         msg->rcv.proto)!=0){
1421                                         LOG(L_ERR, " ERROR: receive_msg: tcp alias failed\n");
1422                                         ret=E_UNSPEC;
1423                                         goto error;
1424                                 }
1425                         }
1426 #endif
1427                         ret=1; /* continue processing */
1428                         break;
1429                 case FORCE_SEND_SOCKET_T:
1430                         if (a->val[0].type!=SOCKETINFO_ST){
1431                                 LOG(L_CRIT, "BUG: do_action: bad force_send_socket argument"
1432                                                 " type: %d\n", a->val[0].type);
1433                                 ret=E_BUG;
1434                                 goto error;
1435                         }
1436                         set_force_socket(msg, (struct socket_info*)a->val[0].u.data);
1437                         ret=1; /* continue processing */
1438                         break;
1439
1440                 case ADD_T:
1441                 case ASSIGN_T:
1442                         v=lval_assign(h, msg, (struct lvalue*)a->val[0].u.data,
1443                                                                   (struct rval_expr*)a->val[1].u.data);
1444                         if (likely(v>=0))
1445                                 ret = 1;
1446                         else if (unlikely (v == EXPR_DROP)) /* hack to quit on DROP*/
1447                                 ret=0;
1448                         else
1449                                 ret=v;
1450                         break;
1451                 case SET_FWD_NO_CONNECT_T:
1452                         msg->fwd_send_flags.f|= SND_F_FORCE_CON_REUSE;
1453                         ret=1; /* continue processing */
1454                         break;
1455                 case SET_RPL_NO_CONNECT_T:
1456                         msg->rpl_send_flags.f|= SND_F_FORCE_CON_REUSE;
1457                         ret=1; /* continue processing */
1458                         break;
1459                 case SET_FWD_CLOSE_T:
1460                         msg->fwd_send_flags.f|= SND_F_CON_CLOSE;
1461                         ret=1; /* continue processing */
1462                         break;
1463                 case SET_RPL_CLOSE_T:
1464                         msg->rpl_send_flags.f|= SND_F_CON_CLOSE;
1465                         ret=1; /* continue processing */
1466                         break;
1467                 case CFG_SELECT_T:
1468                         if (a->val[0].type != CFG_GROUP_ST) {
1469                                 BUG("unsupported parameter in CFG_SELECT_T: %d\n",
1470                                                 a->val[0].type);
1471                                 ret=-1;
1472                                 goto error;
1473                         }
1474                         switch(a->val[1].type) {
1475                                 case NUMBER_ST:
1476                                         v=(int)a->val[1].u.number;
1477                                         break;
1478                                 case RVE_ST:
1479                                         if (rval_expr_eval_int(h, msg, &v, (struct rval_expr*)a->val[1].u.data) < 0) {
1480                                                 ret=-1;
1481                                                 goto error;
1482                                         }
1483                                         break;
1484                                 default:
1485                                         BUG("unsupported group id type in CFG_SELECT_T: %d\n",
1486                                                         a->val[1].type);
1487                                         ret=-1;
1488                                         goto error;
1489                         }
1490                         ret=(cfg_select((cfg_group_t*)a->val[0].u.data, v) == 0) ? 1 : -1;
1491                         break;
1492                 case CFG_RESET_T:
1493                         if (a->val[0].type != CFG_GROUP_ST) {
1494                                 BUG("unsupported parameter in CFG_RESET_T: %d\n",
1495                                                 a->val[0].type);
1496                                 ret=-1;
1497                                 goto error;
1498                         }
1499                         ret=(cfg_reset((cfg_group_t*)a->val[0].u.data) == 0) ? 1 : -1;
1500                         break;
1501 /*
1502                 default:
1503                         LOG(L_CRIT, "BUG: do_action: unknown type %d\n", a->type);
1504 */
1505         }
1506 skip:
1507         return ret;
1508
1509 error_uri:
1510         LOG(L_ERR, "ERROR: do_action: set*: uri too long\n");
1511         if (new_uri) pkg_free(new_uri);
1512         LM_ERR("run action error at: %s:%d\n", (a->cfile)?a->cfile:"", a->cline);
1513         return E_UNSPEC;
1514 error_fwd_uri:
1515         /*free_uri(&uri); -- not needed anymore, using msg->parsed_uri*/
1516 error:
1517         LM_ERR("run action error at: %s:%d\n", (a->cfile)?a->cfile:"", a->cline);
1518         return ret;
1519 }
1520
1521
1522
1523 /* returns: 0, or 1 on success, <0 on error */
1524 /* (0 if drop or break encountered, 1 if not ) */
1525 int run_actions(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
1526 {
1527         struct action* t;
1528         int ret;
1529         struct sr_module *mod;
1530
1531         ret=E_UNSPEC;
1532         h->rec_lev++;
1533         if (unlikely(h->rec_lev>ROUTE_MAX_REC_LEV)){
1534                 LOG(L_ERR, "WARNING: too many recursive routing table lookups (%d)"
1535                                         " giving up!\n", h->rec_lev);
1536                 ret=E_UNSPEC;
1537                 goto error;
1538         }
1539         if (unlikely(h->rec_lev==1)){
1540                 h->run_flags=0;
1541                 h->last_retcode=0;
1542                 _last_returned_code = h->last_retcode;
1543 #ifdef USE_LONGJMP
1544                 if (unlikely(setjmp(h->jmp_env))){
1545                         h->rec_lev=0;
1546                         ret=h->last_retcode;
1547                         goto end;
1548                 }
1549 #endif
1550         }
1551
1552         if (unlikely(a==0)){
1553                 DBG("DEBUG: run_actions: null action list (rec_level=%d)\n",
1554                                 h->rec_lev);
1555                 ret=1;
1556         }
1557
1558         for (t=a; t!=0; t=t->next){
1559                 ret=do_action(h, t, msg);
1560                 /* break, return or drop/exit stop execution of the current
1561                    block */
1562                 if (unlikely(h->run_flags & (BREAK_R_F|RETURN_R_F|EXIT_R_F))){
1563                         if (unlikely(h->run_flags & EXIT_R_F)) {
1564                                 h->last_retcode=ret;
1565                                 _last_returned_code = h->last_retcode;
1566 #ifdef USE_LONGJMP
1567                                 longjmp(h->jmp_env, ret);
1568 #endif
1569                         }
1570                         break;
1571                 }
1572                 /* ignore error returns */
1573         }
1574
1575         h->rec_lev--;
1576 end:
1577         /* process module onbreak handlers if present */
1578         if (unlikely(h->rec_lev==0 && ret==0))
1579                 for (mod=modules;mod;mod=mod->next)
1580                         if (unlikely(mod->exports.onbreak_f)) {
1581                                 mod->exports.onbreak_f( msg );
1582                         }
1583         return ret;
1584
1585
1586 error:
1587         h->rec_lev--;
1588         return ret;
1589 }
1590
1591
1592 int run_top_route(struct action* a, sip_msg_t* msg, struct run_act_ctx *c)
1593 {
1594         struct run_act_ctx ctx;
1595         struct run_act_ctx *p;
1596         int ret;
1597         flag_t sfbk;
1598
1599         p = (c)?c:&ctx;
1600         sfbk = getsflags();
1601         setsflagsval(0);
1602         reset_static_buffer();
1603         init_run_actions_ctx(p);
1604         ret = run_actions(p, a, msg);
1605         setsflagsval(sfbk);
1606         return ret;
1607 }