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