Coverage Report

Created: 2025-07-18 06:32

/src/opensips/route.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SIP routing engine
3
 *
4
 * Copyright (C) 2001-2003 FhG Fokus
5
 * Copyright (C) 2005-2006 Voice Sistem S.R.L.
6
 *
7
 * This file is part of opensips, a free SIP server.
8
 *
9
 * opensips 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
 * opensips is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
22
 *
23
 * History:
24
 * --------
25
 *  2003-01-28  scratchpad removed, src_port introduced (jiri)
26
 *  2003-02-28  scratchpad compatibility abandoned (jiri)
27
 *  2003-03-10  updated to the new module exports format (andrei)
28
 *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
29
 *  2003-04-01  added dst_port, proto, af; renamed comp_port to comp_no,
30
 *               inlined all the comp_* functions (andrei)
31
 *  2003-04-05  s/reply_route/failure_route, onreply_route introduced (jiri)
32
 *  2003-05-23  comp_ip fixed, now it will resolve its operand and compare
33
 *              the ip with all the addresses (andrei)
34
 *  2003-10-10  added more operators support to comp_* (<,>,<=,>=,!=) (andrei)
35
 *  2004-10-19  added from_uri & to_uri (andrei)
36
 *  2006-03-02  MODULE_T action points to a cmd_export_t struct instead to
37
 *               a function address - more info is accessible (bogdan)
38
 *              Fixup failure reports the config line (bogdan)
39
 *  2006-12-22  support for script and branch flags added (bogdan)
40
 */
41
42
43
/*!
44
 * \file
45
 * \brief SIP routing engine
46
 */
47
48
49
50
#include <stdlib.h>
51
#include <sys/types.h>
52
#include <regex.h>
53
#include <netdb.h>
54
#include <string.h>
55
#include <sys/socket.h>
56
#include <netinet/in.h>
57
#include <arpa/inet.h>
58
#include <netdb.h>
59
60
#include "route.h"
61
#include "forward.h"
62
#include "dprint.h"
63
#include "proxy.h"
64
#include "action.h"
65
#include "sr_module.h"
66
#include "ip_addr.h"
67
#include "resolve.h"
68
#include "socket_info.h"
69
#include "blacklists.h"
70
#include "mem/mem.h"
71
#include "xlog.h"
72
#include "evi/evi_modules.h"
73
#include "mod_fix.h"
74
75
/* instance of script routes used for script interpreting */
76
struct os_script_routes *sroutes = NULL;
77
78
int route_type = REQUEST_ROUTE;
79
80
static struct script_route_ref *sroute_refs = NULL;
81
82
83
static int fix_actions(struct action* a); /*fwd declaration*/
84
85
extern int return_code;
86
87
str str_route = str_init("route");
88
str str_request_route = str_init("request_route");
89
str str_failure_route = str_init("failure_route");
90
str str_onreply_route = str_init("onreply_route");
91
str str_branch_route  = str_init("branch_route");
92
str str_error_route   = str_init("error_route");
93
str str_local_route   = str_init("local_route");
94
str str_startup_route = str_init("startup_route");
95
str str_timer_route   = str_init("timer_route");
96
str str_event_route   = str_init("event_route");
97
98
99
/*!
100
 * \brief Allocates and initializes a new routing list holder
101
 */
102
struct os_script_routes* new_sroutes_holder( int inc_ver )
103
0
{
104
0
  static unsigned int sr_version = 1;
105
0
  struct os_script_routes *sr;
106
107
0
  sr = (struct os_script_routes *) pkg_malloc
108
0
    ( sizeof(struct os_script_routes) );
109
0
  if ( sr==NULL) {
110
0
    LM_ERR("failed to allocate table for script routes\n");
111
0
    return NULL;
112
0
  }
113
0
  memset( sr, 0, sizeof(struct os_script_routes) );
114
115
0
  sr->request[DEFAULT_RT].name = "0";
116
0
  sr->onreply[DEFAULT_RT].name = "0";
117
118
0
  sr->version = inc_ver ? ++sr_version : sr_version ;
119
120
0
  return sr;
121
0
}
122
123
124
/*!
125
 * \brief Frees a routing list holder
126
 */
127
void free_route_lists(struct os_script_routes *sr)
128
0
{
129
0
  int i;
130
131
0
  for( i=0 ; i<RT_NO ; i++ )
132
0
    if (sr->request[i].a)
133
0
      free_action_list(sr->request[i].a);
134
135
0
  for( i=0 ; i<ONREPLY_RT_NO ; i++ )
136
0
    if (sr->onreply[i].a)
137
0
      free_action_list(sr->onreply[i].a);
138
139
0
  for( i=0 ; i<FAILURE_RT_NO ; i++ )
140
0
    if (sr->failure[i].a)
141
0
      free_action_list(sr->failure[i].a);
142
143
0
  for( i=0 ; i<BRANCH_RT_NO ; i++ )
144
0
    if (sr->branch[i].a)
145
0
      free_action_list(sr->branch[i].a);
146
147
0
  if (sr->local.a)
148
0
    free_action_list(sr->local.a);
149
150
0
  if (sr->error.a)
151
0
    free_action_list(sr->error.a);
152
153
0
  if (sr->startup.a)
154
0
    free_action_list(sr->startup.a);
155
156
0
  for( i=0 ; i<TIMER_RT_NO ; i++ )
157
0
    if (sr->timer[i].a)
158
0
      free_action_list(sr->timer[i].a);
159
160
0
  for( i=0 ; i<EVENT_RT_NO ; i++ )
161
0
    if (sr->event[i].a)
162
0
      free_action_list(sr->event[i].a);
163
164
0
}
165
166
167
/************************** Generic functions ***********************/
168
169
int get_script_route_ID_by_name(char *name, struct script_route *sr, int size)
170
0
{
171
0
  unsigned int i;
172
173
0
  for(i=1;i<size;i++) {
174
0
    if (sr[i].name==0)
175
0
      return -1;
176
0
    if (strcmp(sr[i].name,name)==0 )
177
0
      return i;
178
0
  }
179
0
  return -1;
180
0
}
181
182
int get_script_route_ID_by_name_str(str *name, struct script_route *sr, int size)
183
0
{
184
0
  unsigned int i;
185
186
0
  for(i=1;i<size;i++) {
187
0
    if (sr[i].name==0)
188
0
      return -1;
189
0
    if (strlen(sr[i].name)==name->len &&
190
0
        strncmp(sr[i].name, name->s, name->len) == 0)
191
0
      return i;
192
0
  }
193
0
  return -1;
194
0
}
195
196
197
static struct script_route_ref * __ref_script_route_by_name(char *name, int l,
198
          struct script_route *sr, int size, int type, int in_shm)
199
0
{
200
0
  struct script_route_ref *ref;
201
0
  unsigned int i;
202
203
0
  if (!in_shm) {
204
    /* first check if a reference to the route already exists */
205
0
    for( ref=sroute_refs ; ref ; ref=ref->next ) {
206
0
      if (ref->type==type && ref->name.len==l
207
0
      && strncmp(ref->name.s,name,l)==0) {
208
        /* we found an already exists reference */
209
0
        ref->u.refcnt++;
210
0
        LM_DBG("returning existing %p [%.*s] with idx %d, "
211
0
          "ver/cnt %d\n", ref,
212
0
          ref->name.len, ref->name.s, ref->idx, ref->u.refcnt);
213
        /* note that the returned reference may point to
214
         * no route, there is no guarantee to be a working one */
215
0
        return ref;
216
0
      }
217
0
    }
218
219
    /* no reference found, create a new one */
220
0
    ref = pkg_malloc( sizeof *ref + l + 1 );
221
0
  } else {
222
0
    ref = shm_malloc( sizeof *ref + l + 1 );
223
0
  }
224
225
0
  if (ref==NULL) {
226
0
    LM_ERR("failed to allocate new sroute reference\n");
227
0
    return NULL;
228
0
  }
229
0
  ref->name.s = (char*)(ref+1);
230
0
  ref->name.len = l;
231
0
  memcpy( ref->name.s, name, l);
232
0
  ref->name.s[l] = 0; // make it NULL terminated also
233
0
  ref->type = type;
234
0
  ref->idx = -1;
235
236
  /* see if we find its idx */
237
0
  for( i=1 ; i<size && sr[i].name ; i++ ) {
238
0
    if (strcmp(sr[i].name,ref->name.s)==0 )
239
0
      ref->idx = i;
240
0
  }
241
242
0
  if (in_shm) {
243
0
    ref->u.version = sroutes->version;
244
0
    ref->next = NULL;
245
0
  } else {
246
0
    ref->u.refcnt = 1;
247
    /* link it */
248
0
    ref->next = sroute_refs;
249
0
    sroute_refs = ref;
250
0
  }
251
252
0
  LM_DBG("returning new %p [%.*s] with idx %d, ver/xnt %d\n", ref,
253
0
    ref->name.len, ref->name.s, ref->idx, ref->u.version);
254
0
  return ref;
255
0
}
256
257
258
struct script_route_ref * ref_script_route_by_name(char *name,
259
          struct script_route *sr, int size, int type, int in_shm)
260
0
{
261
0
  return __ref_script_route_by_name( name, strlen(name),
262
0
    sr, size, type, in_shm);
263
0
}
264
265
266
struct script_route_ref * ref_script_route_by_name_str(str *name,
267
          struct script_route *sr, int size, int type, int in_shm)
268
0
{
269
0
  return __ref_script_route_by_name( name->s, name->len,
270
0
    sr, size, type, in_shm);
271
0
}
272
273
274
void unref_script_route(struct script_route_ref *ref)
275
0
{
276
0
  struct script_route_ref *it;
277
278
0
  if (!ref)
279
0
    return;
280
281
  //LM_DBG("xXx--- unrefing %p [%.*s] with idx %d, ver/cnt\n", ref,
282
  //  ref->name.len, ref->name.s, ref->idx, ref->u.version);
283
284
0
  if (--ref->u.refcnt==0) {
285
    /* not used by anyone else, destroy it */
286
0
    LM_DBG("freeing %p [%.*s] with idx %d\n", ref,
287
0
      ref->name.len, ref->name.s, ref->idx);
288
0
    if (ref==sroute_refs) {
289
0
      sroute_refs = ref->next;
290
0
    } else {
291
0
      for( it=sroute_refs ; it && it->next!=ref ; it=it->next );
292
0
      if (it==NULL) {
293
0
        LM_BUG("removing sroute reference <%.*s> from empty "
294
0
          "list :(\n",ref->name.len, ref->name.s);
295
0
      } else {
296
0
        it->next = ref->next;
297
0
      }
298
0
      pkg_free(ref);
299
0
    }
300
0
  }
301
0
}
302
303
304
int update_script_route_ref(struct script_route_ref *ref)
305
0
{
306
0
  LM_DBG("updating %p [%.*s], idx=%d, ver/cnt %d\n",
307
0
    ref, ref->name.len, ref->name.s, ref->idx, ref->u.version);
308
309
0
  switch (ref->type) {
310
0
    case REQUEST_ROUTE:
311
0
      ref->idx = get_script_route_ID_by_name_str( &ref->name,
312
0
        sroutes->request, RT_NO);
313
0
      LM_DBG("---------found idx=%d\n", ref->idx);
314
0
      return 0;
315
0
    case ONREPLY_ROUTE:
316
0
      ref->idx = get_script_route_ID_by_name_str( &ref->name,
317
0
        sroutes->onreply, ONREPLY_RT_NO);
318
0
      LM_DBG("---------found idx=%d\n", ref->idx);
319
0
      return 0;
320
0
    case FAILURE_ROUTE:
321
0
      ref->idx = get_script_route_ID_by_name_str( &ref->name,
322
0
        sroutes->failure, FAILURE_RT_NO);
323
0
      return 0;
324
0
      LM_DBG("---------found idx=%d\n", ref->idx);
325
0
    case BRANCH_ROUTE:
326
0
      ref->idx = get_script_route_ID_by_name_str( &ref->name,
327
0
        sroutes->branch, BRANCH_RT_NO);
328
0
      LM_DBG("---------found idx=%d\n", ref->idx);
329
0
      return 0;
330
0
    case EVENT_ROUTE:
331
0
      ref->idx = get_script_route_ID_by_name_str( &ref->name,
332
0
        sroutes->event, EVENT_RT_NO);
333
0
      LM_DBG("---------found idx=%d\n", ref->idx);
334
0
      return 0;
335
0
    default:
336
0
      LM_BUG("unsupported route type %d [%s]\n",
337
0
        ref->type, ref->name.s);
338
0
      ref->idx = -1;
339
0
  }
340
0
  return -1;
341
0
}
342
343
344
void update_all_script_route_refs(void)
345
0
{
346
0
  struct script_route_ref *it;
347
348
0
  for( it=sroute_refs ; it ; it=it->next ) {
349
0
    update_script_route_ref( it );
350
0
    if (it->idx==-1)
351
0
      LM_WARN("route [%s] not found when updating refs\n",it->name.s);
352
0
  }
353
0
}
354
355
356
struct script_route_ref *dup_ref_script_route_in_shm(
357
                struct script_route_ref *ref, int from_shm)
358
0
{
359
0
  struct script_route_ref *s_ref;
360
361
0
  s_ref = (struct script_route_ref *) shm_malloc(
362
0
    sizeof(struct script_route_ref) + ref->name.len + 1);
363
0
  if (s_ref==NULL) {
364
0
    LM_ERR("failed to dup script route in shm\n");
365
0
    return NULL;
366
0
  } else {
367
0
    memcpy( s_ref, ref,
368
0
      sizeof(struct script_route_ref) + ref->name.len + 1 );
369
0
    s_ref->name.s = (char*)(s_ref+1);
370
0
    s_ref->u.version = from_shm ? ref->u.version : sroutes->version;
371
0
    s_ref->next = NULL;
372
0
  }
373
374
0
  LM_DBG("dupping %p [%.*s], idx %d, ver/cnt %d, to "
375
0
    "new %p [%.*s], idx %d, ver/cnt %d\n",
376
0
    ref, ref->name.len, ref->name.s, ref->idx, ref->u.version,
377
0
    s_ref, s_ref->name.len, s_ref->name.s, s_ref->idx, s_ref->u.version);
378
0
  return s_ref;
379
0
}
380
381
382
void print_script_route_refs(void)
383
0
{
384
0
  struct script_route_ref *it;
385
386
0
  for( it=sroute_refs ; it ; it=it->next )
387
0
    LM_DBG(" rt_ref - [%s] idx %d, ver/cnt %d\n", it->name.s,
388
0
      it->idx, it->u.refcnt);
389
0
}
390
391
392
/********************** Interpreter related functions ***********************/
393
394
/*! \brief comp_scriptvar helping function */
395
inline static int comp_ip(int op, str *ip_str, struct net *ipnet)
396
0
{
397
0
  struct ip_addr *ip_tmp = NULL;
398
399
0
  ip_tmp = str2ip(ip_str);
400
0
  if (!ip_tmp) {
401
0
    ip_tmp = str2ip6(ip_str);
402
0
    if (!ip_tmp) {
403
0
      LM_DBG("Var value is not an IP\n");
404
0
      return -1;
405
0
    }
406
0
  }
407
408
0
  if (op == EQUAL_OP) {
409
0
    return (matchnet(ip_tmp, ipnet) == 1);
410
0
  } else if (op == DIFF_OP) {
411
0
    return (matchnet(ip_tmp, ipnet) != 1);
412
0
  } else {
413
0
    LM_CRIT("invalid operator %d\n", op);
414
0
    return -1;
415
0
  }
416
0
}
417
418
/*! \brief compare str to str */
419
inline static int comp_s2s(int op, str *s1, str *s2)
420
0
{
421
0
#define make_nt_copy(_sd,_so) \
422
0
  do { \
423
0
    if (pkg_str_extend(_sd, (_so)->len+1)<0) \
424
0
      return -1; \
425
0
    memcpy((_sd)->s, (_so)->s, (_so)->len);\
426
0
    (_sd)->s[(_so)->len] = '\0'; \
427
0
  } while(0)
428
0
  static str cp1 = {NULL,0};
429
0
  static str cp2 = {NULL,0};
430
0
  int rt;
431
0
  int ret;
432
0
  regex_t* re;
433
434
0
  ret = -1;
435
  /* check the input values :
436
   *  s1 - must be a non-empty string
437
   *  s2 - must be a non-empty string or a regexp* for [NOT]MATCH_OP */
438
0
  if ( s1->s==NULL )
439
0
    return 0;
440
441
0
  switch(op) {
442
0
    case EQUAL_OP:
443
0
      if ( s2->s==NULL) return 0;
444
0
      ret = str_casematch(s1, s2);
445
0
    break;
446
0
    case DIFF_OP:
447
0
      if ( s2->s==NULL ) return 0;
448
0
      ret = !str_casematch(s1, s2);
449
0
      break;
450
0
    case GT_OP:
451
0
      if ( s2->s==NULL ) return 0;
452
0
      rt = str_strcasecmp(s1, s2);
453
0
      if (rt>0)
454
0
        ret = 1;
455
0
      else if(rt==0 && s1->len>s2->len)
456
0
        ret = 1;
457
0
      else ret = 0;
458
0
      break;
459
0
    case GTE_OP:
460
0
      if ( s2->s==NULL ) return 0;
461
0
      rt = str_strcasecmp(s1, s2);
462
0
      if (rt>0)
463
0
        ret = 1;
464
0
      else if(rt==0 && s1->len>=s2->len)
465
0
        ret = 1;
466
0
      else ret = 0;
467
0
      break;
468
0
    case LT_OP:
469
0
      if ( s2->s==NULL ) return 0;
470
0
      rt = str_strcasecmp(s1, s2);
471
0
      if (rt<0)
472
0
        ret = 1;
473
0
      else if(rt==0 && s1->len<s2->len)
474
0
        ret = 1;
475
0
      else ret = 0;
476
0
      break;
477
0
    case LTE_OP:
478
0
      if ( s2->s==NULL ) return 0;
479
0
      rt = str_strcasecmp(s1, s2);
480
0
      if (rt<0)
481
0
        ret = 1;
482
0
      else if(rt==0 && s1->len<=s2->len)
483
0
        ret = 1;
484
0
      else ret = 0;
485
0
      break;
486
0
    case MATCH_OP:
487
0
      if ( s2==NULL ) return 0;
488
0
      make_nt_copy( &cp1, s1);
489
0
      ret=(regexec((regex_t*)s2, cp1.s, 0, 0, 0)==0);
490
0
      break;
491
0
    case NOTMATCH_OP:
492
0
      if ( s2==NULL ) return 1;
493
0
      make_nt_copy( &cp1, s1);
494
0
      ret=(regexec((regex_t*)s2, cp1.s, 0, 0, 0)!=0);
495
0
      break;
496
0
    case MATCHD_OP:
497
0
    case NOTMATCHD_OP:
498
0
      if ( s2==NULL || s2->s==NULL)
499
0
        return (op == MATCHD_OP? 0 : 1);
500
0
      re=(regex_t*)pkg_malloc(sizeof(regex_t));
501
0
      if (re==0) {
502
0
        LM_CRIT("pkg memory allocation failure\n");
503
0
        return -1;
504
0
      }
505
506
0
      make_nt_copy( &cp1, s1);
507
0
      make_nt_copy( &cp2, s2);
508
509
0
      if (regcomp(re, cp2.s, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
510
0
        pkg_free(re);
511
0
        return -1;
512
0
      }
513
0
      if(op==MATCHD_OP)
514
0
        ret=(regexec(re, cp1.s, 0, 0, 0)==0);
515
0
      else
516
0
        ret=(regexec(re, cp1.s, 0, 0, 0)!=0);
517
0
      regfree(re);
518
0
      pkg_free(re);
519
0
      break;
520
0
    default:
521
0
      LM_CRIT("unknown op %d\n", op);
522
0
  }
523
0
  return ret;
524
0
}
525
526
/*! \brief compare nr to nr */
527
inline static int comp_n2n(int op, int n1, int n2)
528
0
{
529
0
  switch(op) {
530
0
    case EQUAL_OP:
531
0
    case MATCH_OP:
532
0
    case MATCHD_OP:
533
0
      if(n1 == n2)
534
0
        return 1;
535
0
      return 0;
536
0
    case NOTMATCH_OP:
537
0
    case NOTMATCHD_OP:
538
0
    case DIFF_OP:
539
0
      if(n1 != n2)
540
0
        return 1;
541
0
      return 0;
542
0
    case GT_OP:
543
0
      if(n1 > n2)
544
0
        return 1;
545
0
      return 0;
546
0
    case GTE_OP:
547
0
      if(n1 >= n2)
548
0
        return 1;
549
0
      return 0;
550
0
    case LT_OP:
551
0
      if(n1 < n2)
552
0
        return 1;
553
0
      return 0;
554
0
    case LTE_OP:
555
0
      if(n1 <= n2)
556
0
        return 1;
557
0
      return 0;
558
0
    default:
559
0
      LM_CRIT("unknown op %d\n", op);
560
0
  }
561
0
  return -1;
562
0
}
563
564
565
static inline const char *op_id_2_string(int op_id)
566
0
{
567
0
  switch (op_id) {
568
0
    case EQUAL_OP:
569
0
      return "EQUAL";
570
0
    case MATCH_OP:
571
0
      return "REGEXP_MATCH";
572
0
    case NOTMATCH_OP:
573
0
      return "REGEXP_NO_MATCH";
574
0
    case MATCHD_OP:
575
0
      return "DYN_REGEXP_MATCH";
576
0
    case NOTMATCHD_OP:
577
0
      return "DYN_REGEXP_NO_MATCH";
578
0
    case GT_OP:
579
0
      return "GREATER_THAN";
580
0
    case LT_OP:
581
0
      return "LESS_THAN";
582
0
    case GTE_OP:
583
0
      return "GREATER_OR_EQUAL";
584
0
    case LTE_OP:
585
0
      return "LESS_OR_EQUAL";
586
0
    case DIFF_OP:
587
0
      return "DIFFERENT_THAN";
588
0
    case VALUE_OP:
589
0
      return "VALUE";
590
0
    case NO_OP:
591
0
    default:
592
0
      return "NONE";
593
0
  }
594
0
}
595
596
597
static inline const char *expr_type_2_string(int expr_type)
598
0
{
599
0
  switch (expr_type) {
600
0
    case STRING_ST:
601
0
      return "STRING";
602
0
    case NET_ST:
603
0
      return "NET_MASK";
604
0
    case NUMBER_ST:
605
0
      return "NUMBER";
606
0
    case IP_ST:
607
0
      return "IP";
608
0
    case RE_ST:
609
0
      return "REGEXP";
610
0
    case PROXY_ST:
611
0
      return "PROXY";
612
0
    case EXPR_ST:
613
0
      return "EXPRESSION";
614
0
    case ACTIONS_ST:
615
0
      return "ACTION";
616
0
    case CMD_ST:
617
0
      return "FUNCTION";
618
0
    case MODFIXUP_ST:
619
0
      return "MOD_FIXUP";
620
0
    case STR_ST:
621
0
      return "STR";
622
0
    case SOCKID_ST:
623
0
      return "SOCKET";
624
0
    case SOCKETINFO_ST:
625
0
      return "SOCKET_INFO";
626
0
    case SCRIPTVAR_ST:
627
0
      return "VARIABLE";
628
0
    case NULLV_ST:
629
0
      return "NULL";
630
0
    case BLACKLIST_ST:
631
0
      return "BLACKLIST";
632
0
    case SCRIPTVAR_ELEM_ST:
633
0
      return "VARIABLE_ELEMENT";
634
0
    case NOSUBTYPE:
635
0
    default:
636
0
      return"NONE";
637
0
  }
638
0
}
639
640
static inline const char *val_type_2_string(int val_type)
641
0
{
642
0
  if (val_type&PV_VAL_STR)
643
0
    return "STRING_VAL";
644
0
  if (val_type&PV_VAL_INT)
645
0
    return "INTEGER_VAL";
646
0
  if (val_type&PV_VAL_NULL)
647
0
    return "NULL_VAL";
648
0
  if (val_type&PV_VAL_EMPTY)
649
0
    return "EMPTY_VAL";
650
0
  return "NO_VAL";
651
0
}
652
653
654
inline static int comp_scriptvar(struct sip_msg *msg, int op, operand_t *left,
655
    operand_t *right)
656
0
{
657
0
  str lstr;
658
0
  str rstr;
659
0
  int ln;
660
0
  int rn;
661
0
  pv_value_t lvalue;
662
0
  pv_value_t rvalue;
663
0
  int type;
664
0
  struct net *rnet;
665
666
0
  lstr.s = 0; lstr.len = 0;
667
0
  rstr.s = 0; rstr.len = 0;
668
0
  ln = 0; rn =0;
669
670
0
  if(pv_get_spec_value(msg, left->v.spec, &lvalue)!=0)
671
0
  {
672
0
    LM_ERR("cannot get left var value\n");
673
0
    goto error;
674
0
  }
675
0
  if(right->type==NULLV_ST)
676
0
  {
677
0
    if(op==EQUAL_OP)
678
0
    {
679
0
      if(lvalue.flags&PV_VAL_NULL)
680
0
        return 1;
681
0
      return 0;
682
0
    } else {
683
0
      if(lvalue.flags&PV_VAL_NULL)
684
0
        return 0;
685
0
      return 1;
686
0
    }
687
0
  }
688
689
0
  lstr = lvalue.rs;
690
0
  ln   = lvalue.ri;
691
0
  type = 0;
692
0
  rvalue.flags = 0; /*just for err printing purposes */
693
694
0
  if(right->type == SCRIPTVAR_ST)
695
0
  {
696
0
    if(pv_get_spec_value(msg, right->v.spec, &rvalue)!=0)
697
0
    {
698
0
      LM_ERR("cannot get right var value\n");
699
0
      goto error;
700
0
    }
701
0
    if(rvalue.flags&PV_VAL_NULL || lvalue.flags&PV_VAL_NULL ) {
702
0
      if (rvalue.flags&PV_VAL_NULL && lvalue.flags&PV_VAL_NULL )
703
0
        return (op==EQUAL_OP || op==MATCH_OP || op==MATCHD_OP)?1:0;
704
0
      return (op==DIFF_OP || op==NOTMATCH_OP || op==NOTMATCHD_OP)?1:0;
705
0
    }
706
707
0
    if(op==MATCH_OP||op==NOTMATCH_OP)
708
0
    {
709
0
      if(!((rvalue.flags&PV_VAL_STR) && (lvalue.flags&PV_VAL_STR)))
710
0
        goto error_op;
711
0
      if(op==MATCH_OP)
712
0
        return comp_s2s(MATCHD_OP, &lstr, &rvalue.rs);
713
0
      else
714
0
        return comp_s2s(NOTMATCHD_OP, &lstr, &rvalue.rs);
715
0
    }
716
717
0
    if((rvalue.flags&PV_VAL_INT) && (lvalue.flags&PV_VAL_INT)) {
718
      /* comparing int */
719
0
      rn = rvalue.ri;
720
0
      type = 2;
721
0
    } else if((rvalue.flags&PV_VAL_STR) && (lvalue.flags&PV_VAL_STR)) {
722
      /* comparing string */
723
0
      rstr = rvalue.rs;
724
0
      type = 1;
725
0
    } else
726
0
      goto error_op;
727
0
  } else {
728
    /* null against a not-null constant */
729
0
    if(lvalue.flags&PV_VAL_NULL)
730
0
      return (op==DIFF_OP || op==NOTMATCH_OP || op==NOTMATCHD_OP)?1:0;
731
732
0
    if(right->type == STR_ST) {
733
0
      if(!(lvalue.flags&PV_VAL_STR))
734
0
        goto error_op;
735
      /* comparing string */
736
0
      type = 1;
737
0
      rstr = right->v.s;
738
0
    } else if(right->type == NUMBER_ST) {
739
0
      if(!(lvalue.flags&PV_VAL_INT))
740
0
        goto error_op;
741
      /* comparing int */
742
0
      type = 2;
743
0
      rn = right->v.n;
744
0
    } else if (right->type == NET_ST) {
745
0
      if(!(lvalue.flags&PV_VAL_STR))
746
0
        goto error_op;
747
      /* comparing IP */
748
0
      type = 3;
749
0
      rnet = (struct net*)right->v.data;
750
0
    } else {
751
0
      if(op==MATCH_OP || op==NOTMATCH_OP)
752
0
      {
753
0
        if(!(lvalue.flags&PV_VAL_STR) || right->type != RE_ST)
754
0
          goto error_op;
755
0
        return comp_s2s(op, &lstr, (str*)right->v.expr);
756
0
      }
757
      /* comparing others */
758
0
      type = 0;
759
0
    }
760
0
  }
761
762
0
  switch (type) {
763
0
  case 1: /* compare str */
764
0
    LM_DBG("str %d: %.*s\n", op, lstr.len, lstr.s);
765
0
    return comp_s2s(op, &lstr, &rstr);
766
0
  case 2:
767
0
    LM_DBG("int %d: %d / %d\n", op, ln, rn);
768
0
    return comp_n2n(op, ln, rn);
769
0
  case 3:
770
0
    LM_DBG("ip %d: %.*s\n", op, lstr.len, lstr.s);
771
0
    return comp_ip(op, &lstr, rnet);
772
0
  }
773
774
  /* default is error */
775
776
0
error_op:
777
0
  LM_WARN("invalid %s operation: left is %s/%s, right is %s/%s\n",
778
0
    op_id_2_string(op),
779
0
    expr_type_2_string(left->type), val_type_2_string(lvalue.flags),
780
0
    expr_type_2_string(right->type), val_type_2_string(rvalue.flags) );
781
0
error:
782
0
  return -1;
783
0
}
784
785
786
/*! \brief
787
 * \return 0/1 (false/true) or -1 on error, -127 EXPR_DROP
788
 */
789
static int eval_elem(struct expr* e, struct sip_msg* msg, pv_value_t *val)
790
0
{
791
0
  int ret;
792
/*  int retl;
793
  int retr; */
794
0
  int ival;
795
0
  pv_value_t lval;
796
0
  pv_value_t rval;
797
0
  char *p;
798
0
  int i,n;
799
800
0
  ret=E_BUG;
801
0
  if (e->type!=ELEM_T){
802
0
    LM_CRIT("invalid type\n");
803
0
    goto error;
804
0
  }
805
806
0
  if(val) memset(val, 0, sizeof(pv_value_t));
807
808
0
  switch(e->left.type){
809
0
    case NUMBER_O:
810
0
        ret=!(!e->right.v.n); /* !! to transform it in {0,1} */
811
0
        break;
812
0
    case ACTION_O:
813
0
        ret=run_action_list( (struct action*)e->right.v.data, msg);
814
0
        if(val)
815
0
        {
816
0
          val->flags = PV_TYPE_INT|PV_VAL_INT;
817
0
          val->ri = ret;
818
0
        }
819
0
        if (ret<=0) ret=(ret==0)?EXPR_DROP:0;
820
0
        else ret=1;
821
0
        return ret;
822
0
    case EXPR_O:
823
        /* retl = retr = 0; */
824
0
        memset(&lval, 0, sizeof(pv_value_t));
825
0
        memset(&rval, 0, sizeof(pv_value_t));
826
0
        if(e->left.v.data)
827
0
          eval_expr((struct expr*)e->left.v.data,msg,&lval);
828
          /* XXX why is retl used here ?? */
829
          /* retl=eval_expr((struct expr*)e->left.v.data,msg,&lval); */
830
0
        if(lval.flags == PV_VAL_NONE)
831
0
        {
832
0
          pv_value_destroy(&lval);
833
0
          pv_value_destroy(&rval);
834
0
          return 0;
835
0
        }
836
0
        if(e->op == BNOT_OP)
837
0
        {
838
0
          if(lval.flags&PV_VAL_INT)
839
0
          {
840
0
            if(val!=NULL)
841
0
            {
842
0
              val->flags = PV_TYPE_INT|PV_VAL_INT;
843
0
              val->ri = ~lval.ri;
844
0
            }
845
0
            pv_value_destroy(&lval);
846
0
            pv_value_destroy(&rval);
847
0
            return (val->ri)?1:0;
848
0
          }
849
0
          LM_ERR("binary NOT on non-numeric value\n");
850
0
          pv_value_destroy(&lval);
851
0
          pv_value_destroy(&rval);
852
0
          return 0;
853
0
        }
854
0
        if(e->right.v.data)
855
0
          eval_expr((struct expr*)e->right.v.data,msg,&rval);
856
          /* retr=eval_expr((struct expr*)e->right.v.data,msg,&rval); */
857
858
0
        if(lval.flags&PV_TYPE_INT)
859
0
        {
860
0
          if( (rval.flags&PV_VAL_NULL) )
861
0
          {
862
0
            rval.ri = 0;
863
0
          } else if(!(rval.flags&PV_VAL_INT))
864
0
          {
865
0
            LM_ERR("invalid numeric operands\n");
866
0
            pv_value_destroy(&lval);
867
0
            pv_value_destroy(&rval);
868
0
            return 0;
869
0
          }
870
0
          if(val!=NULL)
871
0
            val->flags = PV_TYPE_INT|PV_VAL_INT;
872
873
0
          ival = 0;
874
0
          switch(e->op) {
875
0
            case PLUS_OP:
876
0
              ival = lval.ri + rval.ri;
877
0
              break;
878
0
            case MINUS_OP:
879
0
              ival = lval.ri - rval.ri;
880
0
              break;
881
0
            case DIV_OP:
882
0
              if(rval.ri==0)
883
0
              {
884
0
                LM_ERR("divide by 0\n");
885
0
                pv_value_destroy(&lval);
886
0
                pv_value_destroy(&rval);
887
0
                return 0;
888
0
              } else
889
0
                ival = lval.ri / rval.ri;
890
0
              break;
891
0
            case MULT_OP:
892
0
              ival = lval.ri * rval.ri;
893
0
              break;
894
0
            case MODULO_OP:
895
0
              if(rval.ri==0)
896
0
              {
897
0
                LM_ERR("divide by 0\n");
898
0
                pv_value_destroy(&lval);
899
0
                pv_value_destroy(&rval);
900
0
                return 0;
901
0
              } else
902
0
                ival = lval.ri % rval.ri;
903
0
              break;
904
0
            case BAND_OP:
905
0
              ival = lval.ri & rval.ri;
906
0
              break;
907
0
            case BOR_OP:
908
0
              ival = lval.ri | rval.ri;
909
0
              break;
910
0
            case BXOR_OP:
911
0
              ival = lval.ri ^ rval.ri;
912
0
              break;
913
0
            case BLSHIFT_OP:
914
0
              ival = lval.ri << rval.ri;
915
0
              break;
916
0
            case BRSHIFT_OP:
917
0
              ival = lval.ri >> rval.ri;
918
0
              break;
919
0
            default:
920
0
              LM_ERR("invalid int op %d\n", e->op);
921
0
              if(val!=NULL) val->ri = 0;
922
0
              pv_value_destroy(&lval);
923
0
              pv_value_destroy(&rval);
924
0
              return 0;
925
0
          }
926
0
          pv_value_destroy(&lval);
927
0
          pv_value_destroy(&rval);
928
0
          if(val!=NULL) val->ri = ival;
929
0
          return (ival)?1:0;
930
0
        } else if (e->op == PLUS_OP) {
931
0
          if (!val) {
932
0
            ret = (lval.rs.len>0 || rval.rs.len>0);
933
0
            pv_value_destroy(&lval);
934
0
            pv_value_destroy(&rval);
935
0
            return ret;
936
0
          }
937
938
0
          if (rval.flags & PV_VAL_NULL) {
939
0
            pv_value_destroy(&rval);
940
0
            rval.flags = PV_VAL_STR;
941
0
          }
942
943
0
          if(!(rval.flags&PV_VAL_STR))
944
0
          {
945
0
            LM_ERR("invalid string operands\n");
946
0
            pv_value_destroy(&lval);
947
0
            pv_value_destroy(&rval);
948
0
            return 0;
949
0
          }
950
0
          val->rs.s=(char*)pkg_malloc((lval.rs.len+rval.rs.len+1)
951
0
              *sizeof(char));
952
0
          if(val->rs.s==0)
953
0
          {
954
0
            LM_ERR("no more memory\n");
955
0
            pv_value_destroy(&lval);
956
0
            pv_value_destroy(&rval);
957
0
            return 0;
958
0
          }
959
0
          val->flags = PV_VAL_PKG|PV_VAL_STR;
960
0
          memcpy(val->rs.s, lval.rs.s, lval.rs.len);
961
0
          memcpy(val->rs.s+lval.rs.len, rval.rs.s, rval.rs.len);
962
0
          val->rs.len = lval.rs.len + rval.rs.len;
963
0
          val->rs.s[val->rs.len] = '\0';
964
0
          pv_value_destroy(&lval);
965
0
          pv_value_destroy(&rval);
966
0
          return 1;
967
0
        } else if ((lval.flags & PV_VAL_STR) && (rval.flags & PV_VAL_STR)) {
968
0
          if (lval.rs.len != rval.rs.len)
969
0
          {
970
0
            LM_ERR("Different length string operands\n");
971
0
            pv_value_destroy(&lval);
972
0
            pv_value_destroy(&rval);
973
0
            return 0;
974
0
          }
975
0
          n = lval.rs.len;
976
0
          val->rs.s = pkg_malloc(n+1);
977
0
          if (!val->rs.s)
978
0
          {
979
0
            LM_ERR("no more memory\n");
980
0
            pv_value_destroy(&lval);
981
0
            pv_value_destroy(&rval);
982
0
            return 0;
983
0
          }
984
0
          switch(e->op) {
985
0
            case BAND_OP:
986
0
              for (i=0;i<n;i++)
987
0
                val->rs.s[i] = lval.rs.s[i] & rval.rs.s[i];
988
0
              break;
989
0
            case BOR_OP:
990
0
              for (i=0;i<n;i++)
991
0
                val->rs.s[i] = lval.rs.s[i] | rval.rs.s[i];
992
0
              break;
993
0
            case BXOR_OP:
994
0
              for (i=0;i<n;i++)
995
0
                val->rs.s[i] = lval.rs.s[i] ^ rval.rs.s[i];
996
0
              break;
997
0
            default:
998
0
              LM_ERR("Only bitwise operations can be applied on strings\n");
999
0
              val->ri = 0;
1000
0
              pv_value_destroy(&lval);
1001
0
              pv_value_destroy(&rval);
1002
0
              return 0;
1003
0
          }
1004
0
          val->flags = PV_VAL_PKG|PV_VAL_STR;
1005
0
          val->rs.len = n;
1006
0
          val->rs.s[n] = '\0';
1007
0
          pv_value_destroy(&lval);
1008
0
          pv_value_destroy(&rval);
1009
0
          return 1;
1010
0
        }
1011
0
        else {
1012
0
          LM_ERR("Invalid operator : %d \n",e->op);
1013
0
          pv_value_destroy(&lval);
1014
0
          pv_value_destroy(&rval);
1015
0
          return 0;
1016
0
        }
1017
0
        break;
1018
0
    case STRINGV_O:
1019
0
        if(val) {
1020
0
          val->flags = PV_VAL_STR;
1021
0
          val->rs = e->left.v.s;
1022
0
        }
1023
        /* optimization for no dup ?!?! */
1024
0
        return (e->left.v.s.len>0)?1:0;
1025
0
    case NUMBERV_O:
1026
0
        if(val) {
1027
0
          val->flags = PV_TYPE_INT|PV_VAL_INT;
1028
0
          val->ri = e->left.v.n;
1029
0
        }
1030
0
        ret=!(!e->left.v.n); /* !! to transform it in {0,1} */
1031
0
        return ret;
1032
0
    case SCRIPTVAR_O:
1033
0
        if(e->op==NO_OP)
1034
0
        {
1035
0
          memset(&rval, 0, sizeof(pv_value_t));
1036
0
          if(pv_get_spec_value(msg, e->right.v.spec, &rval)==0)
1037
0
          {
1038
0
            if(rval.flags==PV_VAL_NONE || (rval.flags&PV_VAL_NULL)
1039
0
                || (rval.flags&PV_VAL_EMPTY)
1040
0
                || ((rval.flags&PV_TYPE_INT)&&rval.ri==0))
1041
0
            {
1042
0
              pv_value_destroy(&rval);
1043
0
              return 0;
1044
0
            }
1045
0
            if(rval.flags&PV_TYPE_INT)
1046
0
            {
1047
0
              pv_value_destroy(&rval);
1048
0
              return 1;
1049
0
            }
1050
0
            if(rval.rs.len!=0)
1051
0
            {
1052
0
              pv_value_destroy(&rval);
1053
0
              return 1;
1054
0
            }
1055
0
            pv_value_destroy(&rval);
1056
0
          }
1057
0
          return 0;
1058
0
        }
1059
0
        if(e->op==VALUE_OP)
1060
0
        {
1061
0
          if(pv_get_spec_value(msg, e->left.v.spec, &lval)==0)
1062
0
          {
1063
0
            if(val!=NULL)
1064
0
              memcpy(val, &lval, sizeof(pv_value_t));
1065
0
            if(lval.flags&PV_VAL_STR)
1066
0
            {
1067
0
              if(!((lval.flags&PV_VAL_PKG)
1068
0
                  || (lval.flags&PV_VAL_SHM)))
1069
0
              {
1070
0
                if(val!=NULL)
1071
0
                {
1072
                  /* do pkg duplicate */
1073
0
                  p = (char*)pkg_malloc((val->rs.len+1)
1074
0
                      *sizeof(char));
1075
0
                  if(p==0)
1076
0
                  {
1077
0
                    LM_ERR("no more pkg memory\n");
1078
0
                    memset(val, 0, sizeof(pv_value_t));
1079
0
                    return 0;
1080
0
                  }
1081
0
                  memcpy(p, val->rs.s, val->rs.len);
1082
0
                  p[val->rs.len] = 0;
1083
0
                  val->rs.s = p;
1084
0
                  val->flags|= PV_VAL_PKG;
1085
0
                }
1086
0
              }
1087
0
              return 1;
1088
0
            }
1089
0
            if(lval.flags==PV_VAL_NONE
1090
0
                || (lval.flags & PV_VAL_NULL)
1091
0
                || (lval.flags & PV_VAL_EMPTY))
1092
0
              return 0;
1093
0
            if(lval.flags&PV_TYPE_INT)
1094
0
              return (lval.ri!=0);
1095
0
            else
1096
0
              return (lval.rs.len>0);
1097
0
          }
1098
0
          return 0;
1099
0
        }
1100
1101
0
        ret=comp_scriptvar(msg, e->op, &e->left, &e->right);
1102
0
        break;
1103
0
    default:
1104
0
        LM_CRIT("invalid operand %d\n", e->left.type);
1105
0
  }
1106
0
  if(val)
1107
0
  {
1108
0
    val->flags = PV_TYPE_INT|PV_VAL_INT;
1109
0
    val->ri = ret;
1110
0
  }
1111
0
  return ret;
1112
0
error:
1113
0
  if(val)
1114
0
  {
1115
0
    val->flags = PV_TYPE_INT|PV_VAL_INT;
1116
0
    val->ri = -1;
1117
0
  }
1118
0
  return -1;
1119
0
}
1120
1121
1122
1123
/*! \return ret= 0/1 (false/true) ,  -1 on error or EXPR_DROP (-127)  */
1124
int eval_expr(struct expr* e, struct sip_msg* msg, pv_value_t *val)
1125
0
{
1126
0
  static int rec_lev=0;
1127
0
  int ret;
1128
1129
0
  rec_lev++;
1130
0
  if (rec_lev>MAX_REC_LEV){
1131
0
    LM_CRIT("too many expressions (%d)\n", rec_lev);
1132
0
    ret=-1;
1133
0
    goto skip;
1134
0
  }
1135
1136
0
  if (e->type==ELEM_T){
1137
0
    ret=eval_elem(e, msg, val);
1138
0
  }else if (e->type==EXP_T){
1139
0
    switch(e->op){
1140
0
      case AND_OP:
1141
0
        ret=eval_expr(e->left.v.expr, msg, val);
1142
        /* if error or false stop evaluating the rest */
1143
0
        if (ret!=1) break;
1144
0
        ret=eval_expr(e->right.v.expr, msg, val); /*ret1 is 1*/
1145
0
        break;
1146
0
      case OR_OP:
1147
0
        ret=eval_expr(e->left.v.expr, msg, val);
1148
        /* if true or error stop evaluating the rest */
1149
0
        if (ret!=0) break;
1150
0
        ret=eval_expr(e->right.v.expr, msg, val); /* ret1 is 0 */
1151
0
        break;
1152
0
      case NOT_OP:
1153
0
        ret=eval_expr(e->left.v.expr, msg, val);
1154
0
        if (ret<0) break;
1155
0
        ret= ! ret;
1156
0
        break;
1157
0
      case EVAL_OP:
1158
0
        ret=eval_expr(e->left.v.expr, msg, val);
1159
0
        break;
1160
0
      default:
1161
0
        LM_CRIT("unknown op %d\n", e->op);
1162
0
        ret=-1;
1163
0
    }
1164
0
  }else{
1165
0
    LM_CRIT("unknown type %d\n", e->type);
1166
0
    ret=-1;
1167
0
  }
1168
1169
0
skip:
1170
0
  rec_lev--;
1171
0
  return ret;
1172
0
}
1173
1174
1175
int run_startup_route(void)
1176
0
{
1177
0
  struct sip_msg *req;
1178
0
  int ret, old_route_type;
1179
1180
0
  req = get_dummy_sip_msg();
1181
0
  if(req == NULL) {
1182
0
    LM_ERR("No more memory\n");
1183
0
    return -1;
1184
0
  }
1185
1186
0
  swap_route_type(old_route_type, STARTUP_ROUTE);
1187
  /* run the route */
1188
0
  ret = run_top_route( sroutes->startup, req);
1189
0
  set_route_type(old_route_type);
1190
1191
  /* clean whatever extra structures were added by script functions */
1192
0
  release_dummy_sip_msg(req);
1193
1194
0
  return ret;
1195
0
}
1196
1197
1198
/********************* Parsing/fixing related functions *********************/
1199
1200
1201
int get_script_route_idx( char* name,struct script_route *sr, int size,int set)
1202
0
{
1203
0
  unsigned int i;
1204
1205
0
  for(i=1;i<size;i++) {
1206
0
    if (sr[i].name==NULL) {
1207
      /* name not found -> allocate it now */
1208
0
      sr[i].name = name;
1209
0
      return i;
1210
0
    }
1211
0
    if (strcmp(sr[i].name,name)==0 ) {
1212
      /* name found */
1213
0
      if (sr[i].a && set) {
1214
0
        LM_ERR("Script route <%s> is redefined\n",name);
1215
0
        return -1;
1216
0
      }
1217
0
      return i;
1218
0
    }
1219
0
  }
1220
0
  LM_ERR("Too many routes - no slot left for <%s>\n",name);
1221
0
  return -1;
1222
0
}
1223
1224
1225
/*! \brief traverses an expression tree and compiles the REs where necessary)
1226
 * \return 0 for ok, <0 if errors
1227
 */
1228
static int fix_expr(struct expr* exp)
1229
0
{
1230
0
  regex_t* re;
1231
0
  int ret;
1232
1233
0
  ret=E_BUG;
1234
0
  if (exp==0){
1235
0
    LM_CRIT("null pointer\n");
1236
0
    return E_BUG;
1237
0
  }
1238
0
  if (exp->type==EXP_T){
1239
0
    switch(exp->op){
1240
0
      case AND_OP:
1241
0
      case OR_OP:
1242
0
          if ((ret=fix_expr(exp->left.v.expr))!=0)
1243
0
            return ret;
1244
0
          ret=fix_expr(exp->right.v.expr);
1245
0
          break;
1246
0
      case NOT_OP:
1247
0
      case EVAL_OP:
1248
0
          ret=fix_expr(exp->left.v.expr);
1249
0
          break;
1250
0
      default:
1251
0
          LM_CRIT("unknown op %d\n", exp->op);
1252
0
    }
1253
0
  }else if (exp->type==ELEM_T){
1254
0
      if (exp->op==MATCH_OP || exp->op==NOTMATCH_OP){
1255
0
        if (exp->right.type==STR_ST){
1256
0
          re=(regex_t*)pkg_malloc(sizeof(regex_t));
1257
0
          if (re==0){
1258
0
            LM_CRIT("out of pkg memory\n");
1259
0
            return E_OUT_OF_MEM;
1260
0
          }
1261
0
          if (regcomp(re, (char*) exp->right.v.data,
1262
0
                REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
1263
0
            LM_CRIT("bad re \"%s\"\n", (char*) exp->right.v.data);
1264
0
            pkg_free(re);
1265
0
            return E_BAD_RE;
1266
0
          }
1267
          /* replace the string with the re */
1268
0
          pkg_free(exp->right.v.data);
1269
0
          exp->right.v.data=re;
1270
0
          exp->right.type=RE_ST;
1271
0
        }else if (exp->right.type!=RE_ST
1272
0
            && exp->right.type!=SCRIPTVAR_ST){
1273
0
          LM_CRIT("invalid type for match\n");
1274
0
          return E_BUG;
1275
0
        }
1276
0
      }
1277
0
      if (exp->left.type==ACTION_O){
1278
0
        ret=fix_actions((struct action*)exp->right.v.data);
1279
0
        if (ret!=0){
1280
0
          LM_CRIT("fix_actions error\n");
1281
0
          return ret;
1282
0
        }
1283
0
      }
1284
0
      if (exp->left.type==EXPR_O){
1285
0
        ret=fix_expr(exp->left.v.expr);
1286
0
        if (ret!=0){
1287
0
          LM_CRIT("fix left exp error\n");
1288
0
          return ret;
1289
0
        }
1290
0
      }
1291
0
      if (exp->right.type==EXPR_ST){
1292
0
        ret=fix_expr(exp->right.v.expr);
1293
0
        if (ret!=0){
1294
0
          LM_CRIT("fix right exp error\n");
1295
0
          return ret;
1296
0
        }
1297
0
      }
1298
0
      ret=0;
1299
0
  }
1300
0
  return ret;
1301
0
}
1302
1303
1304
/*! \brief Adds the proxies in the proxy list & resolves the hostnames
1305
 * \return 0 if ok, <0 on error */
1306
static int fix_actions(struct action* a)
1307
0
{
1308
0
  struct action *t;
1309
0
  int ret;
1310
0
  const cmd_export_t* cmd;
1311
0
  const acmd_export_t* acmd;
1312
0
  str s;
1313
0
  pv_elem_t *model=NULL;
1314
0
  xl_level_p xlp;
1315
0
  struct script_route_ref *rt_ref;
1316
1317
0
  if (a==0){
1318
0
    LM_CRIT("null pointer\n");
1319
0
    return E_BUG;
1320
0
  }
1321
0
  for(t=a; t!=0; t=t->next){
1322
0
    switch(t->type){
1323
0
      case ROUTE_T:
1324
0
        ret = 0;
1325
0
        switch (t->elem[0].type) {
1326
0
          case SCRIPTVAR_ST:
1327
0
          case SCRIPTVAR_ELEM_ST:
1328
0
            break;
1329
0
          case NUMBER_ST:
1330
0
            if ((t->elem[0].u.number>RT_NO)||(t->elem[0].u.number<0)){
1331
0
              LM_ALERT("invalid routing table number in"
1332
0
                  "route(%lu)\n", t->elem[0].u.number);
1333
0
              ret = -1;
1334
0
            }
1335
0
            if (sroutes->request[t->elem[0].u.number].a==NULL) {
1336
0
              LM_ERR("called route [%s] (id=%d) is not defined\n",
1337
0
                  sroutes->request[t->elem[0].u.number].name,
1338
0
                  (int)t->elem[0].u.number);
1339
0
              ret = -1;
1340
0
            }
1341
0
            break;
1342
0
          default:
1343
0
            ret = -1;
1344
0
            break;
1345
0
        }
1346
0
        if (ret == -1) {
1347
0
          LM_ERR("failed to validate a route() statement (type %d)\n",
1348
0
                     t->elem[0].type);
1349
0
          ret = E_CFG;
1350
0
          goto error;
1351
0
        }
1352
0
        if (t->elem[1].type != 0) {
1353
0
          if (t->elem[1].type != NUMBER_ST ||
1354
0
              t->elem[2].type != SCRIPTVAR_ST) {
1355
0
            LM_ALERT("BUG in route() type %d/%d\n",
1356
0
                 t->elem[1].type, t->elem[2].type);
1357
0
            ret=E_BUG;
1358
0
            goto error;
1359
0
          }
1360
0
          if (t->elem[1].u.number >= MAX_ACTION_ELEMS ||
1361
0
              t->elem[1].u.number <= 0) {
1362
0
            LM_ALERT("BUG in number of route parameters %d\n",
1363
0
                 (int)t->elem[1].u.number);
1364
0
            ret=E_BUG;
1365
0
            goto error;
1366
0
          }
1367
0
        }
1368
0
        break;
1369
0
      case ASSERT_T:
1370
0
        if (t->elem[0].type!=EXPR_ST){
1371
0
          LM_CRIT("invalid subtype %d for assert (should be expr)\n",
1372
0
                t->elem[0].type);
1373
0
          ret = E_BUG;
1374
0
          goto error;
1375
0
        }
1376
0
        if (t->elem[0].u.data)
1377
0
          if ((ret=fix_expr((struct expr*)t->elem[0].u.data))<0)
1378
0
            return ret;
1379
0
        break;
1380
0
      case IF_T:
1381
0
        if (t->elem[0].type!=EXPR_ST){
1382
0
          LM_CRIT("invalid subtype %d for if (should be expr)\n",
1383
0
                t->elem[0].type);
1384
0
          ret = E_BUG;
1385
0
          goto error;
1386
0
        }else if( (t->elem[1].type!=ACTIONS_ST)
1387
0
            &&(t->elem[1].type!=NOSUBTYPE) ){
1388
0
          LM_CRIT("invalid subtype %d for if() {...} (should be"
1389
0
                "action)\n", t->elem[1].type);
1390
0
          ret = E_BUG;
1391
0
          goto error;
1392
0
        }else if( (t->elem[2].type!=ACTIONS_ST)
1393
0
            &&(t->elem[2].type!=NOSUBTYPE) ){
1394
0
          LM_CRIT("invalid subtype %d for if() {} else{...}(should"
1395
0
              "be action)\n", t->elem[2].type);
1396
0
          ret = E_BUG;
1397
0
          goto error;
1398
0
        }
1399
0
        if (t->elem[0].u.data){
1400
0
          if ((ret=fix_expr((struct expr*)t->elem[0].u.data))<0)
1401
0
            return ret;
1402
0
        }
1403
0
        if ( (t->elem[1].type==ACTIONS_ST)&&(t->elem[1].u.data) ){
1404
0
          if ((ret=fix_actions((struct action*)t->elem[1].u.data))<0)
1405
0
            return ret;
1406
0
        }
1407
0
        if ( (t->elem[2].type==ACTIONS_ST)&&(t->elem[2].u.data) ){
1408
0
          if((ret=fix_actions((struct action*)t->elem[2].u.data))<0)
1409
0
            return ret;
1410
0
        }
1411
0
        break;
1412
0
      case WHILE_T:
1413
0
        if (t->elem[0].type!=EXPR_ST){
1414
0
          LM_CRIT("invalid subtype %d for while (should be expr)\n",
1415
0
                t->elem[0].type);
1416
0
          ret = E_BUG;
1417
0
          goto error;
1418
0
        }else if( (t->elem[1].type!=ACTIONS_ST)
1419
0
            &&(t->elem[1].type!=NOSUBTYPE) ){
1420
0
          LM_CRIT("invalid subtype %d for while() {...} (should be"
1421
0
                "action)\n", t->elem[1].type);
1422
0
          ret = E_BUG;
1423
0
          goto error;
1424
0
        }
1425
0
        if (t->elem[0].u.data){
1426
0
          if ((ret=fix_expr((struct expr*)t->elem[0].u.data))<0)
1427
0
            return ret;
1428
0
        }
1429
0
        if ( (t->elem[1].type==ACTIONS_ST)&&(t->elem[1].u.data) ){
1430
0
          if ((ret=fix_actions((struct action*)t->elem[1].u.data))<0)
1431
0
            return ret;
1432
0
        }
1433
0
        break;
1434
0
      case FOR_EACH_T:
1435
0
        if (t->elem[2].type != ACTIONS_ST) {
1436
0
          LM_CRIT("bad subtype %d in for-each (should be actions)\n",
1437
0
                     t->elem[2].type);
1438
0
          ret = E_BUG;
1439
0
          goto error;
1440
0
        }
1441
1442
0
        if (t->elem[2].u.data) {
1443
0
          if ((ret=fix_actions((struct action*)t->elem[2].u.data))<0)
1444
0
            return ret;
1445
0
        }
1446
0
        break;
1447
0
      case SWITCH_T:
1448
0
        if ( (t->elem[1].type==ACTIONS_ST)&&(t->elem[1].u.data) ){
1449
0
          if ((ret=fix_actions((struct action*)t->elem[1].u.data))<0)
1450
0
            return ret;
1451
0
        }
1452
0
        break;
1453
0
      case CASE_T:
1454
0
        if ( (t->elem[1].type==ACTIONS_ST)&&(t->elem[1].u.data) ){
1455
0
          if ((ret=fix_actions((struct action*)t->elem[1].u.data))<0)
1456
0
            return ret;
1457
0
        }
1458
0
        break;
1459
0
      case DEFAULT_T:
1460
0
        if ( (t->elem[0].type==ACTIONS_ST)&&(t->elem[0].u.data) ){
1461
0
          if ((ret=fix_actions((struct action*)t->elem[0].u.data))<0)
1462
0
            return ret;
1463
0
        }
1464
0
        break;
1465
0
      case CMD_T:
1466
0
        cmd = (const cmd_export_t*)t->elem[0].u.data_const;
1467
0
        LM_DBG("fixing %s, %s:%d\n", cmd->name, t->file, t->line);
1468
1469
0
        if ((ret = fix_cmd(cmd->params, t->elem)) < 0) {
1470
0
          LM_ERR("Failed to fix command <%s>\n", cmd->name);
1471
0
          goto error;
1472
0
        }
1473
0
        break;
1474
0
      case ASYNC_T:
1475
0
      case LAUNCH_T:
1476
0
        if ( (t->elem[0].type==ACTIONS_ST)&&(t->elem[0].u.data) ){
1477
0
          if ((ret=fix_actions((struct action*)t->elem[0].u.data))<0)
1478
0
            return ret;
1479
0
        }
1480
0
        rt_ref = (struct script_route_ref*)(t->elem[1].u.data);
1481
0
        if (rt_ref && update_script_route_ref( rt_ref ) !=0 ) {
1482
0
          LM_ERR("async/launch resume route [%s] is not defined\n",
1483
0
            rt_ref->name.s);
1484
0
          ret = E_CFG;
1485
0
          goto error;
1486
0
        }
1487
        /* LAUNCH may have a third param, a FMT'ed string */
1488
0
        if ( (t->elem[2].type==STRING_ST) &&
1489
0
        (s.s=(char*)t->elem[2].u.data)!=NULL) {
1490
0
          if ( (s.len=strlen(s.s))==0 ) {
1491
0
            t->elem[2].type = NOSUBTYPE;
1492
0
          } else {
1493
0
            if(pv_parse_format(&s ,&model) || model==NULL) {
1494
0
              LM_ERR("wrong format [%s] for value param!\n",s.s);
1495
0
              ret=E_BUG;
1496
0
              goto error;
1497
0
            }
1498
0
            t->elem[2].u.data = (void*)model;
1499
0
            t->elem[2].type = SCRIPTVAR_ELEM_ST;
1500
0
          }
1501
0
        } else {
1502
0
          t->elem[2].type = NOSUBTYPE;
1503
0
        }
1504
0
        break;
1505
0
      case AMODULE_T:
1506
0
        acmd = (const acmd_export_t*)t->elem[0].u.data_const;
1507
0
        LM_DBG("fixing async %s, %s:%d\n", acmd->name, t->file, t->line);
1508
1509
0
        if ((ret = fix_cmd(acmd->params, t->elem)) < 0) {
1510
0
          LM_ERR("Failed to fix command <%s>\n", acmd->name);
1511
0
          goto error;
1512
0
        }
1513
0
        break;
1514
0
      case EQ_T:
1515
0
      case COLONEQ_T:
1516
0
      case PLUSEQ_T:
1517
0
      case MINUSEQ_T:
1518
0
      case DIVEQ_T:
1519
0
      case MULTEQ_T:
1520
0
      case MODULOEQ_T:
1521
0
      case BANDEQ_T:
1522
0
      case BOREQ_T:
1523
0
      case BXOREQ_T:
1524
0
        if (t->elem[1].u.data){
1525
0
          if ((ret=fix_expr((struct expr*)t->elem[1].u.data))<0)
1526
0
            return ret;
1527
0
        }
1528
0
        break;
1529
0
      case XDBG_T:
1530
0
      case XLOG_T:
1531
0
        s.s = (char*)t->elem[1].u.data;
1532
0
        if (s.s == NULL)
1533
0
        {
1534
          /* commands have only one parameter */
1535
0
          s.s = (char *)t->elem[0].u.data;
1536
0
          s.len = strlen(s.s);
1537
0
          if(s.len==0)
1538
0
          {
1539
0
            LM_ERR("param is empty string!\n");
1540
0
            return E_CFG;
1541
0
          }
1542
1543
0
          if(pv_parse_format(&s ,&model) || model==NULL)
1544
0
          {
1545
0
            LM_ERR("wrong format [%s] for value param!\n", s.s);
1546
0
            ret=E_BUG;
1547
0
            goto error;
1548
0
          }
1549
1550
0
          t->elem[0].u.data = (void*)model;
1551
0
          t->elem[0].type = SCRIPTVAR_ELEM_ST;
1552
0
        }
1553
0
        else
1554
0
        {
1555
          /* there are two parameters */
1556
0
          s.s = (char *)t->elem[0].u.data;
1557
0
          s.len = strlen(s.s);
1558
0
          if (s.len == 0)
1559
0
          {
1560
0
            LM_ERR("param is empty string\n");
1561
0
            return E_CFG;
1562
0
          }
1563
0
          xlp = (xl_level_p)pkg_malloc(sizeof(xl_level_t));
1564
0
          if(xlp == NULL)
1565
0
          {
1566
0
            LM_ERR("no more memory\n");
1567
0
            return E_UNSPEC;
1568
0
          }
1569
1570
0
          memset(xlp, 0, sizeof(xl_level_t));
1571
0
          if(s.s[0]==PV_MARKER)
1572
0
          {
1573
0
            xlp->type = 1;
1574
0
            if(pv_parse_spec(&s, &xlp->v.sp)==NULL)
1575
0
            {
1576
0
              LM_ERR("invalid level param\n");
1577
0
              return E_UNSPEC;
1578
0
            }
1579
0
          }
1580
0
          else
1581
0
          {
1582
0
            xlp->type = 0;
1583
0
            switch(s.s[2])
1584
0
            {
1585
0
              case 'A': xlp->v.level = L_ALERT; break;
1586
0
              case 'C': xlp->v.level = L_CRIT; break;
1587
0
              case 'E': xlp->v.level = L_ERR; break;
1588
0
              case 'W': xlp->v.level = L_WARN; break;
1589
0
              case 'N': xlp->v.level = L_NOTICE; break;
1590
0
              case 'I': xlp->v.level = L_INFO; break;
1591
0
              case 'D': xlp->v.level = L_DBG; break;
1592
0
              default:
1593
0
                LM_ERR("unknown log level\n");
1594
0
                return E_UNSPEC;
1595
0
            }
1596
0
          }
1597
0
          t->elem[0].u.data = xlp;
1598
1599
0
          s.s = t->elem[1].u.data;
1600
0
          s.len = strlen(s.s);
1601
0
          if (pv_parse_format(&s, &model) || model == NULL)
1602
0
          {
1603
0
            LM_ERR("wrong format [%s] for value param\n",s.s);
1604
0
            ret=E_BUG;
1605
0
            goto error;
1606
0
          }
1607
1608
0
          t->elem[1].u.data = model;
1609
0
          t->elem[1].type = SCRIPTVAR_ELEM_ST;
1610
0
        }
1611
0
        break;
1612
0
    }
1613
0
  }
1614
0
  return 0;
1615
0
error:
1616
0
  LM_ERR("fixing failed (code=%d) at %s:%d\n", ret, t->file, t->line);
1617
0
  return ret;
1618
0
}
1619
1620
1621
/*! \brief adds an action list to head; a must be null terminated (last a->next=0))
1622
 */
1623
void push(struct action* a, struct action** head)
1624
0
{
1625
0
  struct action *t;
1626
0
  if (*head==0){
1627
0
    *head=a;
1628
0
    return;
1629
0
  }
1630
0
  for (t=*head; t->next;t=t->next);
1631
0
  t->next=a;
1632
0
}
1633
1634
1635
/*! \brief fixes all action tables
1636
 * \return 0 if ok , <0 on error
1637
 */
1638
int fix_rls(void)
1639
0
{
1640
0
  int i, ret;
1641
1642
0
  for(i=0;i<RT_NO;i++){
1643
0
    if(sroutes->request[i].a){
1644
0
      if ((ret=fix_actions(sroutes->request[i].a))!=0){
1645
0
        return ret;
1646
0
      }
1647
0
    }
1648
0
  }
1649
0
  for(i=0;i<ONREPLY_RT_NO;i++){
1650
0
    if(sroutes->onreply[i].a){
1651
0
      if ((ret=fix_actions(sroutes->onreply[i].a))!=0){
1652
0
        return ret;
1653
0
      }
1654
0
    }
1655
0
  }
1656
0
  for(i=0;i<FAILURE_RT_NO;i++){
1657
0
    if(sroutes->failure[i].a){
1658
0
      if ((ret=fix_actions(sroutes->failure[i].a))!=0){
1659
0
        return ret;
1660
0
      }
1661
0
    }
1662
0
  }
1663
0
  for(i=0;i<BRANCH_RT_NO;i++){
1664
0
    if(sroutes->branch[i].a){
1665
0
      if ((ret=fix_actions(sroutes->branch[i].a))!=0){
1666
0
        return ret;
1667
0
      }
1668
0
    }
1669
0
  }
1670
0
  if(sroutes->error.a){
1671
0
    if ((ret=fix_actions(sroutes->error.a))!=0){
1672
0
      return ret;
1673
0
    }
1674
0
  }
1675
0
  if(sroutes->local.a){
1676
0
    if ((ret=fix_actions(sroutes->local.a))!=0){
1677
0
      return ret;
1678
0
    }
1679
0
  }
1680
0
  if(sroutes->startup.a){
1681
0
    if ((ret=fix_actions(sroutes->startup.a))!=0){
1682
0
      return ret;
1683
0
    }
1684
0
  }
1685
1686
0
  for(i = 0; i< TIMER_RT_NO; i++) {
1687
0
    if(sroutes->timer[i].a == NULL)
1688
0
      break;
1689
1690
0
    if ((ret=fix_actions(sroutes->timer[i].a))!=0){
1691
0
      return ret;
1692
0
    }
1693
0
  }
1694
1695
0
  for(i = 1; i< EVENT_RT_NO; i++) {
1696
0
    if(sroutes->event[i].a == NULL)
1697
0
      break;
1698
1699
0
    if ((ret=fix_actions(sroutes->event[i].a))!=0){
1700
0
      return ret;
1701
0
    }
1702
0
  }
1703
1704
0
  return 0;
1705
0
}
1706
1707
1708
static int rcheck_stack[RT_NO];
1709
static int rcheck_stack_p = 0;
1710
static int rcheck_status = 0;
1711
1712
static int check_expr(struct expr* exp, int r_type);
1713
1714
static int check_actions(struct action *a, int r_type)
1715
0
{
1716
0
  struct action *aitem;
1717
0
  const cmd_export_t  *fct;
1718
0
  int n;
1719
1720
0
  for( ; a ; a=a->next ) {
1721
0
    switch (a->type) {
1722
0
      case ROUTE_T:
1723
        /* this route is already on the current path ? */
1724
0
        for( n=0 ; n<rcheck_stack_p ; n++ ) {
1725
0
          if (rcheck_stack[n]==(int)a->elem[0].u.number)
1726
0
            break;
1727
0
        }
1728
0
        if (n!=rcheck_stack_p)
1729
0
          break;
1730
0
        if (++rcheck_stack_p==RT_NO) {
1731
0
          LM_CRIT("stack overflow (%d)\n", rcheck_stack_p);
1732
0
          goto error;
1733
0
        }
1734
0
        rcheck_stack[rcheck_stack_p] = a->elem[0].u.number;
1735
0
        if (check_actions(sroutes->request[a->elem[0].u.number].a,
1736
0
        r_type)!=0)
1737
0
          goto error;
1738
0
        rcheck_stack_p--;
1739
0
        break;
1740
0
      case IF_T:
1741
0
        if (check_expr((struct expr*)a->elem[0].u.data, r_type) < 0)
1742
0
          goto error;
1743
0
        if (check_actions((struct action*)a->elem[1].u.data, r_type)!=0)
1744
0
          goto error;
1745
0
        if (check_actions((struct action*)a->elem[2].u.data, r_type)!=0)
1746
0
          goto error;
1747
0
        break;
1748
0
      case WHILE_T:
1749
0
        if (check_actions((struct action*)a->elem[1].u.data, r_type)!=0)
1750
0
          goto error;
1751
0
        break;
1752
0
      case FOR_EACH_T:
1753
0
        if (check_actions((struct action*)a->elem[2].u.data, r_type)!=0)
1754
0
          goto error;
1755
0
        break;
1756
0
      case SWITCH_T:
1757
0
        aitem = (struct action*)a->elem[1].u.data;
1758
0
        for( ; aitem ; aitem=aitem->next ) {
1759
0
          n = check_actions((struct action*)aitem->elem[1].u.data,
1760
0
              r_type);
1761
0
          if (n!=0) goto error;
1762
0
        }
1763
0
        break;
1764
0
      case CMD_T:
1765
        /* do check :D */
1766
0
        fct = (const cmd_export_t*)(a->elem[0].u.data_const);
1767
0
        if ( (fct->flags&r_type)!=r_type ) {
1768
0
          rcheck_status = -1;
1769
0
          LM_ERR("script function "
1770
0
            "\"%s\" (types=%d) does not support route type "
1771
0
            "(%d)\n",fct->name, fct->flags, r_type);
1772
0
          for( n=rcheck_stack_p-1; n>=0 ; n-- ) {
1773
0
            LM_ERR("route stack[%d]=%d\n",n,rcheck_stack[n]);
1774
0
          }
1775
0
        }
1776
1777
0
        if (check_cmd(fct->params, a->elem) < 0) {
1778
0
          LM_ERR("check failed for function <%s>, %s:%d\n", fct->name,
1779
0
            a->file, a->line);
1780
0
          rcheck_status = -1;
1781
0
        }
1782
0
        break;
1783
0
      default:
1784
0
        break;
1785
0
    }
1786
0
  }
1787
1788
0
  return 0;
1789
0
error:
1790
0
  return -1;
1791
0
}
1792
1793
static int check_expr(struct expr* exp, int r_type)
1794
0
{
1795
0
  int ret = -1;
1796
1797
0
  if (exp==0) {
1798
0
    LM_CRIT("null pointer\n");
1799
0
    return -1;
1800
0
  }
1801
1802
0
  if (exp->type==EXP_T){
1803
0
    switch(exp->op){
1804
0
      case AND_OP:
1805
0
      case OR_OP:
1806
0
        if (check_expr(exp->left.v.expr, r_type) < 0)
1807
0
          return -1;
1808
0
        return check_expr(exp->right.v.expr, r_type);
1809
0
      case NOT_OP:
1810
0
      case EVAL_OP:
1811
0
        return check_expr(exp->left.v.expr, r_type);
1812
0
      default:
1813
0
        LM_CRIT("unknown op %d\n", exp->op);
1814
0
        return -1;
1815
0
    }
1816
0
  } else if (exp->type==ELEM_T){
1817
0
      if (exp->left.type==ACTION_O){
1818
0
        ret=check_actions((struct action*)exp->right.v.data, r_type);
1819
0
        if (ret!=0){
1820
0
          LM_CRIT("check_actions error\n");
1821
0
          return ret;
1822
0
        }
1823
0
      }
1824
0
      if (exp->left.type==EXPR_O){
1825
0
        ret=check_expr(exp->left.v.expr, r_type);
1826
0
        if (ret!=0){
1827
0
          LM_CRIT("check left exp error\n");
1828
0
          return ret;
1829
0
        }
1830
0
      }
1831
0
      if (exp->right.type==EXPR_ST){
1832
0
        ret=check_expr(exp->right.v.expr, r_type);
1833
0
        if (ret!=0){
1834
0
          LM_CRIT("fix right exp error\n");
1835
0
          return ret;
1836
0
        }
1837
0
      }
1838
0
      ret=0;
1839
0
  }
1840
1841
0
  return ret;
1842
0
}
1843
1844
1845
/*! \brief check all parsed routing tables for compatiblity between
1846
 * route types and called module functions;
1847
 * \return 0 if ok , <0 on error
1848
 */
1849
int check_rls(void)
1850
0
{
1851
0
  int i,ret;
1852
1853
0
  rcheck_status = 0;
1854
1855
0
  if(sroutes->request[0].a){
1856
0
    if ((ret=check_actions(sroutes->request[0].a,
1857
0
    REQUEST_ROUTE))!=0){
1858
0
      LM_ERR("check failed for main request route\n");
1859
0
      return ret;
1860
0
    }
1861
0
  }
1862
0
  for(i=0;i<ONREPLY_RT_NO;i++){
1863
0
    if(sroutes->onreply[i].a){
1864
0
      if ((ret=check_actions(sroutes->onreply[i].a,
1865
0
      ONREPLY_ROUTE))!=0){
1866
0
        LM_ERR("check failed for onreply_route[%d]\n",i);
1867
0
        return ret;
1868
0
      }
1869
0
    }
1870
0
  }
1871
0
  for(i=0;i<FAILURE_RT_NO;i++){
1872
0
    if(sroutes->failure[i].a){
1873
0
      if ((ret=check_actions(sroutes->failure[i].a,
1874
0
      FAILURE_ROUTE))!=0){
1875
0
        LM_ERR("check failed for failure_route[%d]\n",i);
1876
0
        return ret;
1877
0
      }
1878
0
    }
1879
0
  }
1880
0
  for(i=0;i<BRANCH_RT_NO;i++){
1881
0
    if(sroutes->branch[i].a){
1882
0
      if ((ret=check_actions(sroutes->branch[i].a,
1883
0
      BRANCH_ROUTE))!=0){
1884
0
        LM_ERR("check failed for branch_route[%d]\n",i);
1885
0
        return ret;
1886
0
      }
1887
0
    }
1888
0
  }
1889
0
  if(sroutes->error.a){
1890
0
    if ((ret=check_actions(sroutes->error.a,ERROR_ROUTE))!=0){
1891
0
      LM_ERR("check failed for error_route\n");
1892
0
      return ret;
1893
0
    }
1894
0
  }
1895
0
  if(sroutes->local.a){
1896
0
    if ((ret=check_actions(sroutes->local.a,LOCAL_ROUTE))!=0){
1897
0
      LM_ERR("check failed for local_route\n");
1898
0
      return ret;
1899
0
    }
1900
0
  }
1901
0
  if(sroutes->startup.a){
1902
0
    if ((ret=check_actions(sroutes->startup.a,STARTUP_ROUTE))!=0){
1903
0
      LM_ERR("check failed for startup_route\n");
1904
0
      return ret;
1905
0
    }
1906
0
  }
1907
1908
0
  for(i = 0; i< TIMER_RT_NO; i++) {
1909
0
    if(sroutes->timer[i].a == NULL)
1910
0
      break;
1911
1912
0
    if ((ret=check_actions(sroutes->timer[i].a,TIMER_ROUTE))!=0){
1913
0
      LM_ERR("check failed for timer_route\n");
1914
0
      return ret;
1915
0
    }
1916
0
  }
1917
1918
0
  for(i = 1; i< EVENT_RT_NO; i++) {
1919
0
    if(sroutes->event[i].a == NULL)
1920
0
      break;
1921
1922
0
    if ((ret=check_actions(sroutes->event[i].a,EVENT_ROUTE))!=0){
1923
0
      LM_ERR("check failed for event_route\n");
1924
0
      return ret;
1925
0
    }
1926
0
  }
1927
1928
0
  return rcheck_status;
1929
0
}
1930
1931
1932
/*! \brief debug function, prints main routing table */
1933
void print_rl(struct os_script_routes *srs)
1934
0
{
1935
0
#define dump_script_routes( route, max, desc) \
1936
0
  { \
1937
0
    int __j; \
1938
0
    for (__j = 0; __j < max; __j++) { \
1939
0
      if (!(route)[__j].a) \
1940
0
        continue; \
1941
0
      LM_GEN1(L_DBG, desc " routing block %d:\n", __j); \
1942
0
      print_actions((route)[__j].a); \
1943
0
      LM_GEN1(L_DBG, "\n\n"); \
1944
0
    } \
1945
0
  }
1946
1947
0
  dump_script_routes(srs->request,  RT_NO,         "main");
1948
0
  dump_script_routes(srs->onreply,  ONREPLY_RT_NO, "onreply");
1949
0
  dump_script_routes(srs->failure,  FAILURE_RT_NO, "failure");
1950
0
  dump_script_routes(srs->branch,   BRANCH_RT_NO,  "branch");
1951
0
  dump_script_routes(&srs->local,   1,             "local");
1952
0
  dump_script_routes(&srs->error,   1,             "error");
1953
0
  dump_script_routes(&srs->startup, 1,             "startup");
1954
0
  dump_script_routes(srs->timer,    TIMER_RT_NO,   "timer");
1955
0
  dump_script_routes(srs->event,    EVENT_RT_NO,   "event");
1956
0
}
1957
1958
1959
void get_top_route_type(str *type, int *has_name)
1960
0
{
1961
0
  switch (route_type) {
1962
0
  case REQUEST_ROUTE:
1963
0
    *type = str_route;
1964
0
    if (!route_stack[route_stack_start])
1965
0
      goto out_noname;
1966
0
    break;
1967
1968
0
  case ONREPLY_ROUTE:
1969
0
    *type = str_onreply_route;
1970
0
    if (!route_stack[route_stack_start])
1971
0
      goto out_noname;
1972
0
    break;
1973
1974
0
  case FAILURE_ROUTE:
1975
0
    *type = str_failure_route;
1976
0
    break;
1977
1978
0
  case BRANCH_ROUTE:
1979
0
    *type = str_branch_route;
1980
0
    break;
1981
1982
0
  case ERROR_ROUTE:
1983
0
    *type = str_error_route;
1984
0
    goto out_noname;
1985
0
    break;
1986
1987
0
  case LOCAL_ROUTE:
1988
0
    *type = str_local_route;
1989
0
    goto out_noname;
1990
0
    break;
1991
1992
0
  case STARTUP_ROUTE:
1993
0
    *type = str_startup_route;
1994
0
    goto out_noname;
1995
0
    break;
1996
1997
0
  case TIMER_ROUTE:
1998
0
    *type = str_timer_route;
1999
0
    break;
2000
2001
0
  case EVENT_ROUTE:
2002
0
    *type = str_event_route;
2003
0
    break;
2004
2005
0
  default:
2006
0
    *type = str_init("");
2007
0
    goto out_noname;
2008
0
  }
2009
2010
0
  *has_name = 1;
2011
0
  return;
2012
2013
0
out_noname:
2014
0
  *has_name = 0;
2015
0
}
2016
2017
2018
void get_route_type(int idx, str *type)
2019
0
{
2020
0
  int _;
2021
2022
0
  if (idx == 0)
2023
0
    return get_top_route_type(type, &_);
2024
0
  else
2025
0
    init_str(type, "route");
2026
0
}
2027
2028
2029
void get_route_name(int idx, str *name)
2030
0
{
2031
0
  switch (route_type) {
2032
0
  case REQUEST_ROUTE:
2033
0
  case ONREPLY_ROUTE:
2034
0
  case FAILURE_ROUTE:
2035
0
  case BRANCH_ROUTE:
2036
0
  case ERROR_ROUTE:
2037
0
  case LOCAL_ROUTE:
2038
0
  case STARTUP_ROUTE:
2039
0
  case TIMER_ROUTE:
2040
0
  case EVENT_ROUTE:
2041
0
    if (!route_stack[idx] || route_stack[idx][0] == '!')
2042
0
      init_str(name, "");
2043
0
    else
2044
0
      init_str(name, route_stack[idx]);
2045
0
    break;
2046
2047
0
  default:
2048
0
    LM_ERR("bad route type: %d\n", route_type);
2049
0
    init_str(name, "");
2050
0
  }
2051
0
}
2052
2053
2054
int is_script_func_used(const char *name, int param_no)
2055
0
{
2056
0
  unsigned int i;
2057
2058
0
  for( i=0; i<RT_NO ; i++ )
2059
0
    if (sroutes->request[i].a &&
2060
0
    is_mod_func_used(sroutes->request[i].a,name,param_no) )
2061
0
      return 1;
2062
2063
0
  for( i=0; i<ONREPLY_RT_NO ; i++ )
2064
0
    if (sroutes->onreply[i].a &&
2065
0
    is_mod_func_used(sroutes->onreply[i].a,name,param_no) )
2066
0
      return 1;
2067
2068
0
  for( i=0; i<FAILURE_RT_NO ; i++ )
2069
0
    if (sroutes->failure[i].a &&
2070
0
    is_mod_func_used(sroutes->failure[i].a,name,param_no) )
2071
0
      return 1;
2072
2073
0
  for( i=0; i<BRANCH_RT_NO ; i++ )
2074
0
    if (sroutes->branch[i].a &&
2075
0
    is_mod_func_used(sroutes->branch[i].a,name,param_no) )
2076
0
      return 1;
2077
2078
0
  for( i=0; i<TIMER_RT_NO ; i++ )
2079
0
    if (sroutes->timer[i].a &&
2080
0
    is_mod_func_used(sroutes->timer[i].a,name,param_no) )
2081
0
      return 1;
2082
2083
0
  for( i=0; i<EVENT_RT_NO ; i++ )
2084
0
    if (sroutes->event[i].a &&
2085
0
    is_mod_func_used(sroutes->event[i].a,name,param_no) )
2086
0
      return 1;
2087
2088
0
  if (sroutes->error.a &&
2089
0
  is_mod_func_used(sroutes->error.a,name,param_no) )
2090
0
    return 1;
2091
2092
0
  if (sroutes->local.a &&
2093
0
  is_mod_func_used(sroutes->local.a,name,param_no) )
2094
0
    return 1;
2095
2096
0
  if (sroutes->startup.a &&
2097
0
  is_mod_func_used(sroutes->startup.a,name,param_no) )
2098
0
    return 1;
2099
2100
0
  return 0;
2101
0
}
2102
2103
int is_script_async_func_used(const char *name, int param_no)
2104
0
{
2105
0
  unsigned int i;
2106
2107
0
  for( i=0; i<RT_NO ; i++ )
2108
0
    if (sroutes->request[i].a &&
2109
0
    is_mod_async_func_used(sroutes->request[i].a,name,param_no) )
2110
0
      return 1;
2111
2112
0
  for( i=0; i<ONREPLY_RT_NO ; i++ )
2113
0
    if (sroutes->onreply[i].a &&
2114
0
    is_mod_async_func_used(sroutes->onreply[i].a,name,param_no) )
2115
0
      return 1;
2116
2117
0
  for( i=0; i<FAILURE_RT_NO ; i++ )
2118
0
    if (sroutes->failure[i].a &&
2119
0
    is_mod_async_func_used(sroutes->failure[i].a,name,param_no) )
2120
0
      return 1;
2121
2122
0
  for( i=0; i<BRANCH_RT_NO ; i++ )
2123
0
    if (sroutes->branch[i].a &&
2124
0
    is_mod_async_func_used(sroutes->branch[i].a,name,param_no) )
2125
0
      return 1;
2126
2127
0
  for( i=0; i<TIMER_RT_NO ; i++ )
2128
0
    if (sroutes->timer[i].a &&
2129
0
    is_mod_async_func_used(sroutes->timer[i].a,name,param_no) )
2130
0
      return 1;
2131
2132
0
  for( i=0; i<EVENT_RT_NO ; i++ )
2133
0
    if (sroutes->event[i].a &&
2134
0
    is_mod_async_func_used(sroutes->event[i].a,name,param_no) )
2135
0
      return 1;
2136
2137
0
  if (sroutes->error.a &&
2138
0
  is_mod_async_func_used(sroutes->error.a,name,param_no) )
2139
0
    return 1;
2140
2141
0
  if (sroutes->local.a &&
2142
0
  is_mod_async_func_used(sroutes->local.a,name,param_no) )
2143
0
    return 1;
2144
2145
0
  if (sroutes->startup.a &&
2146
0
  is_mod_async_func_used(sroutes->startup.a,name,param_no) )
2147
0
    return 1;
2148
2149
0
  return 0;
2150
0
}
2151
2152
static int script_return_level = 0;
2153
static struct script_return_value **script_return_values = NULL, *script_return_last;
2154
2155
int script_return_push(void)
2156
0
{
2157
0
  struct script_return_value **tmp;
2158
0
  tmp = pkg_realloc(script_return_values, (script_return_level + 1) * sizeof *tmp);
2159
0
  if (!tmp) {
2160
0
    LM_ERR("could not add another return level (current=%d\n", script_return_level);
2161
0
    return -1;
2162
0
  }
2163
0
  script_return_values = tmp;
2164
0
  script_return_values[script_return_level] = NULL;
2165
0
  if (script_return_last)
2166
0
    script_return_free(&script_return_last);
2167
0
  return script_return_level++;
2168
0
}
2169
2170
void script_return_pop(int level)
2171
0
{
2172
  /* we leave this here, just to have a way of popping the last level */
2173
0
  if (script_return_last)
2174
0
    script_return_free(&script_return_last);
2175
0
  if (script_return_level < 0)
2176
0
    return;
2177
0
  if (level != script_return_level - 1) {
2178
0
    LM_BUG("cannot return level %d vs %d\n", level, script_return_level);
2179
0
    return;
2180
0
  }
2181
0
  script_return_last = script_return_values[level];
2182
0
  script_return_values = pkg_realloc(script_return_values,
2183
0
      (--script_return_level) * sizeof *script_return_values);
2184
0
}
2185
2186
void script_return_free(struct script_return_value **values)
2187
0
{
2188
0
  struct script_return_value *v, *n;
2189
0
  if (*values == NULL)
2190
0
    return;
2191
0
  for (v = *values; v; v = n) {
2192
0
    n = v->next;
2193
0
    pkg_free(v);
2194
0
  }
2195
0
  *values = NULL;
2196
0
}
2197
2198
void script_return_set(struct sip_msg *msg, struct script_return_param *params)
2199
0
{
2200
0
  pv_value_t val;
2201
0
  struct script_return_param *p;
2202
0
  struct script_return_value *v, *ret = NULL;
2203
0
  if (script_return_level <= 0) {
2204
0
    LM_ERR("no return level initialized\n");
2205
0
    return;
2206
0
  }
2207
  /* itertate through each parameter to store it's value */
2208
0
  for (p = params; p; p = p->next) {
2209
0
    memset(&val, 0, sizeof val);
2210
0
    switch (p->type) {
2211
0
      case SCRIPT_ROUTE_RET_INT:
2212
0
        val.ri = p->rint;
2213
0
        val.flags = PV_VAL_INT;
2214
0
        break;
2215
0
      case SCRIPT_ROUTE_RET_STR:
2216
0
        val.rs = p->rstr;
2217
0
        val.flags = PV_VAL_STR;
2218
0
        break;
2219
0
      case SCRIPT_ROUTE_RET_VAR:
2220
0
        if (pv_get_spec_value(msg, p->rspec, &val) != 0) {
2221
0
          LM_ERR("cannot get return value\n");
2222
0
          pv_get_null(NULL, NULL, &val);
2223
0
        }
2224
0
        break;
2225
0
      case SCRIPT_ROUTE_RET_NULL:
2226
0
        pv_get_null(NULL, NULL, &val);
2227
0
        break;
2228
0
      default:
2229
0
        LM_BUG("unhandled return type %d\n", p->type);
2230
0
        script_return_free(&ret);
2231
0
        return;
2232
0
    }
2233
0
    v = pkg_malloc(sizeof(*v) + ((val.flags & PV_VAL_STR)?val.rs.len:0));
2234
0
    if (v) {
2235
0
      v->val = val;
2236
0
      if (val.flags & PV_VAL_STR) {
2237
0
        v->val.rs.s = v->buf;
2238
0
        memcpy(v->val.rs.s, val.rs.s, val.rs.len);
2239
0
      }
2240
0
      v->next = ret;
2241
0
      ret = v;
2242
0
    } else {
2243
0
      LM_ERR("could not allocate return value\n");
2244
0
      script_return_free(&ret);
2245
0
      return;
2246
0
    }
2247
0
  }
2248
0
  if (script_return_values[script_return_level - 1])
2249
0
    script_return_free(&script_return_values[script_return_level - 1]);
2250
0
  script_return_values[script_return_level - 1] = ret;
2251
0
}
2252
2253
int script_return_get(pv_value_t *res, int index)
2254
0
{
2255
0
  struct script_return_value *v;
2256
0
  pv_get_null(NULL, NULL, res);
2257
0
  if (index < 0 || !script_return_last)
2258
0
    return 0;
2259
0
  for (v = script_return_last; v && index > 0; v = v->next, index--);
2260
0
  if (!v)
2261
0
    return 0;
2262
0
  *res = v->val;
2263
0
  return 1;
2264
0
}