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