forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.c
2260 lines (2044 loc) · 52.6 KB
/
route.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SIP routing engine
*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2005-2006 Voice Sistem S.R.L.
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* --------
* 2003-01-28 scratchpad removed, src_port introduced (jiri)
* 2003-02-28 scratchpad compatibility abandoned (jiri)
* 2003-03-10 updated to the new module exports format (andrei)
* 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
* 2003-04-01 added dst_port, proto, af; renamed comp_port to comp_no,
* inlined all the comp_* functions (andrei)
* 2003-04-05 s/reply_route/failure_route, onreply_route introduced (jiri)
* 2003-05-23 comp_ip fixed, now it will resolve its operand and compare
* the ip with all the addresses (andrei)
* 2003-10-10 added more operators support to comp_* (<,>,<=,>=,!=) (andrei)
* 2004-10-19 added from_uri & to_uri (andrei)
* 2006-03-02 MODULE_T action points to a cmd_export_t struct instead to
* a function address - more info is accessible (bogdan)
* Fixup failure reports the config line (bogdan)
* 2006-12-22 support for script and branch flags added (bogdan)
*/
/*!
* \file
* \brief SIP routing engine
*/
#include <stdlib.h>
#include <sys/types.h>
#include <regex.h>
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "route.h"
#include "forward.h"
#include "dprint.h"
#include "proxy.h"
#include "action.h"
#include "sr_module.h"
#include "ip_addr.h"
#include "resolve.h"
#include "socket_info.h"
#include "blacklists.h"
#include "mem/mem.h"
#include "xlog.h"
#include "evi/evi_modules.h"
#include "mod_fix.h"
/* instance of script routes used for script interpreting */
struct os_script_routes *sroutes = NULL;
int route_type = REQUEST_ROUTE;
static struct script_route_ref *sroute_refs = NULL;
static int fix_actions(struct action* a); /*fwd declaration*/
extern int return_code;
str str_route = str_init("route");
str str_request_route = str_init("request_route");
str str_failure_route = str_init("failure_route");
str str_onreply_route = str_init("onreply_route");
str str_branch_route = str_init("branch_route");
str str_error_route = str_init("error_route");
str str_local_route = str_init("local_route");
str str_startup_route = str_init("startup_route");
str str_timer_route = str_init("timer_route");
str str_event_route = str_init("event_route");
/*!
* \brief Allocates and initializes a new routing list holder
*/
struct os_script_routes* new_sroutes_holder( int inc_ver )
{
static unsigned int sr_version = 1;
struct os_script_routes *sr;
sr = (struct os_script_routes *) pkg_malloc
( sizeof(struct os_script_routes) );
if ( sr==NULL) {
LM_ERR("failed to allocate table for script routes\n");
return NULL;
}
memset( sr, 0, sizeof(struct os_script_routes) );
sr->request[DEFAULT_RT].name = "0";
sr->onreply[DEFAULT_RT].name = "0";
sr->version = inc_ver ? ++sr_version : sr_version ;
return sr;
}
/*!
* \brief Frees a routing list holder
*/
void free_route_lists(struct os_script_routes *sr)
{
int i;
for( i=0 ; i<RT_NO ; i++ )
if (sr->request[i].a)
free_action_list(sr->request[i].a);
for( i=0 ; i<ONREPLY_RT_NO ; i++ )
if (sr->onreply[i].a)
free_action_list(sr->onreply[i].a);
for( i=0 ; i<FAILURE_RT_NO ; i++ )
if (sr->failure[i].a)
free_action_list(sr->failure[i].a);
for( i=0 ; i<BRANCH_RT_NO ; i++ )
if (sr->branch[i].a)
free_action_list(sr->branch[i].a);
if (sr->local.a)
free_action_list(sr->local.a);
if (sr->error.a)
free_action_list(sr->error.a);
if (sr->startup.a)
free_action_list(sr->startup.a);
for( i=0 ; i<TIMER_RT_NO ; i++ )
if (sr->timer[i].a)
free_action_list(sr->timer[i].a);
for( i=0 ; i<EVENT_RT_NO ; i++ )
if (sr->event[i].a)
free_action_list(sr->event[i].a);
}
/************************** Generic functions ***********************/
int get_script_route_ID_by_name(char *name, struct script_route *sr, int size)
{
unsigned int i;
for(i=1;i<size;i++) {
if (sr[i].name==0)
return -1;
if (strcmp(sr[i].name,name)==0 )
return i;
}
return -1;
}
int get_script_route_ID_by_name_str(str *name, struct script_route *sr, int size)
{
unsigned int i;
for(i=1;i<size;i++) {
if (sr[i].name==0)
return -1;
if (strlen(sr[i].name)==name->len &&
strncmp(sr[i].name, name->s, name->len) == 0)
return i;
}
return -1;
}
static struct script_route_ref * __ref_script_route_by_name(char *name, int l,
struct script_route *sr, int size, int type, int in_shm)
{
struct script_route_ref *ref;
unsigned int i;
/* first check if a reference to the route already exists */
for( ref=sroute_refs ; ref ; ref=ref->next ) {
if (ref->type==type && ref->name.len==l
&& strncmp(ref->name.s,name,l)==0) {
/* we found an already exists reference */
ref->u.refcnt++;
LM_DBG("returning existing %p [%.*s] with idx %d, "
"ver/cnt %d\n", ref,
ref->name.len, ref->name.s, ref->idx, ref->u.refcnt);
/* note that the returned reference may point to
* no route, there is no guarantee to be a working one */
return ref;
}
}
/* no reference found, create a new one */
ref = in_shm
? shm_malloc( sizeof(struct script_route_ref) + l + 1 )
: pkg_malloc( sizeof(struct script_route_ref) + l + 1 );
if (ref==NULL) {
LM_ERR("failed to pkg allocate new sroute reference\n");
return NULL;
}
ref->name.s = (char*)(ref+1);
ref->name.len = l;
memcpy( ref->name.s, name, l);
ref->name.s[l] = 0; // make it NULL terminated also
ref->type = type;
ref->idx = -1;
/* see if we find its idx */
for( i=1 ; i<size && sr[i].name ; i++ ) {
if (strcmp(sr[i].name,ref->name.s)==0 )
ref->idx = i;
}
if (in_shm) {
ref->u.version = sroutes->version;
ref->next = NULL;
} else {
ref->u.refcnt = 1;
/* link it */
ref->next = sroute_refs;
sroute_refs = ref;
}
LM_DBG("returning new %p [%.*s] with idx %d, ver/xnt %d\n", ref,
ref->name.len, ref->name.s, ref->idx, ref->u.version);
return ref;
}
struct script_route_ref * ref_script_route_by_name(char *name,
struct script_route *sr, int size, int type, int in_shm)
{
return __ref_script_route_by_name( name, strlen(name),
sr, size, type, in_shm);
}
struct script_route_ref * ref_script_route_by_name_str(str *name,
struct script_route *sr, int size, int type, int in_shm)
{
return __ref_script_route_by_name( name->s, name->len,
sr, size, type, in_shm);
}
void unref_script_route(struct script_route_ref *ref)
{
struct script_route_ref *it;
if (!ref)
return;
//LM_DBG("xXx--- unrefing %p [%.*s] with idx %d, ver/cnt\n", ref,
// ref->name.len, ref->name.s, ref->idx, ref->u.version);
if (--ref->u.refcnt==0) {
/* not used by anyone else, destroy it */
LM_DBG("freeing %p [%.*s] with idx %d\n", ref,
ref->name.len, ref->name.s, ref->idx);
if (ref==sroute_refs) {
sroute_refs = ref->next;
} else {
for( it=sroute_refs ; it && it->next!=ref ; it=it->next );
if (it==NULL) {
LM_BUG("removing sroute reference <%.*s> from empty "
"list :(\n",ref->name.len, ref->name.s);
} else {
it->next = ref->next;
}
pkg_free(ref);
}
}
}
int update_script_route_ref(struct script_route_ref *ref)
{
LM_DBG("updating %p [%.*s], idx=%d, ver/cnt %d\n",
ref, ref->name.len, ref->name.s, ref->idx, ref->u.version);
switch (ref->type) {
case REQUEST_ROUTE:
ref->idx = get_script_route_ID_by_name_str( &ref->name,
sroutes->request, RT_NO);
LM_DBG("---------found idx=%d\n", ref->idx);
return 0;
case ONREPLY_ROUTE:
ref->idx = get_script_route_ID_by_name_str( &ref->name,
sroutes->onreply, ONREPLY_RT_NO);
LM_DBG("---------found idx=%d\n", ref->idx);
return 0;
case FAILURE_ROUTE:
ref->idx = get_script_route_ID_by_name_str( &ref->name,
sroutes->failure, FAILURE_RT_NO);
return 0;
LM_DBG("---------found idx=%d\n", ref->idx);
case BRANCH_ROUTE:
ref->idx = get_script_route_ID_by_name_str( &ref->name,
sroutes->branch, BRANCH_RT_NO);
LM_DBG("---------found idx=%d\n", ref->idx);
return 0;
case EVENT_ROUTE:
ref->idx = get_script_route_ID_by_name_str( &ref->name,
sroutes->event, EVENT_RT_NO);
LM_DBG("---------found idx=%d\n", ref->idx);
return 0;
default:
LM_BUG("unsupported route type %d [%s]\n",
ref->type, ref->name.s);
ref->idx = -1;
}
return -1;
}
void update_all_script_route_refs(void)
{
struct script_route_ref *it;
for( it=sroute_refs ; it ; it=it->next ) {
update_script_route_ref( it );
if (it->idx==-1)
LM_WARN("route [%s] not found when updating refs\n",it->name.s);
}
}
struct script_route_ref *dup_ref_script_route_in_shm(
struct script_route_ref *ref, int from_shm)
{
struct script_route_ref *s_ref;
s_ref = (struct script_route_ref *) shm_malloc(
sizeof(struct script_route_ref) + ref->name.len + 1);
if (s_ref==NULL) {
LM_ERR("failed to dup script route in shm\n");
} else {
memcpy( s_ref, ref,
sizeof(struct script_route_ref) + ref->name.len + 1 );
s_ref->name.s = (char*)(s_ref+1);
s_ref->u.version = from_shm ? ref->u.version : sroutes->version;
s_ref->next = NULL;
}
LM_DBG("dupping %p [%.*s], idx %d, ver/cnt %d, to "
"new %p [%.*s], idx %d, ver/cnt %d\n",
ref, ref->name.len, ref->name.s, ref->idx, ref->u.version,
s_ref, s_ref->name.len, s_ref->name.s, s_ref->idx, s_ref->u.version);
return s_ref;
}
void print_script_route_refs(void)
{
struct script_route_ref *it;
for( it=sroute_refs ; it ; it=it->next )
LM_DBG(" rt_ref - [%s] idx %d, ver/cnt %d\n", it->name.s,
it->idx, it->u.refcnt);
}
/********************** Interpreter related functions ***********************/
/*! \brief comp_scriptvar helping function */
inline static int comp_ip(int op, str *ip_str, struct net *ipnet)
{
struct ip_addr *ip_tmp = NULL;
ip_tmp = str2ip(ip_str);
if (!ip_tmp) {
ip_tmp = str2ip6(ip_str);
if (!ip_tmp) {
LM_DBG("Var value is not an IP\n");
return -1;
}
}
if (op == EQUAL_OP) {
return (matchnet(ip_tmp, ipnet) == 1);
} else if (op == DIFF_OP) {
return (matchnet(ip_tmp, ipnet) != 1);
} else {
LM_CRIT("invalid operator %d\n", op);
return -1;
}
}
/*! \brief compare str to str */
inline static int comp_s2s(int op, str *s1, str *s2)
{
#define make_nt_copy(_sd,_so) \
do { \
if (pkg_str_extend(_sd, (_so)->len+1)<0) \
return -1; \
memcpy((_sd)->s, (_so)->s, (_so)->len);\
(_sd)->s[(_so)->len] = '\0'; \
} while(0)
static str cp1 = {NULL,0};
static str cp2 = {NULL,0};
int rt;
int ret;
regex_t* re;
ret = -1;
/* check the input values :
* s1 - must be a non-empty string
* s2 - must be a non-empty string or a regexp* for [NOT]MATCH_OP */
if ( s1->s==NULL )
return 0;
switch(op) {
case EQUAL_OP:
if ( s2->s==NULL) return 0;
ret = str_casematch(s1, s2);
break;
case DIFF_OP:
if ( s2->s==NULL ) return 0;
ret = !str_casematch(s1, s2);
break;
case GT_OP:
if ( s2->s==NULL ) return 0;
rt = str_strcasecmp(s1, s2);
if (rt>0)
ret = 1;
else if(rt==0 && s1->len>s2->len)
ret = 1;
else ret = 0;
break;
case GTE_OP:
if ( s2->s==NULL ) return 0;
rt = str_strcasecmp(s1, s2);
if (rt>0)
ret = 1;
else if(rt==0 && s1->len>=s2->len)
ret = 1;
else ret = 0;
break;
case LT_OP:
if ( s2->s==NULL ) return 0;
rt = str_strcasecmp(s1, s2);
if (rt<0)
ret = 1;
else if(rt==0 && s1->len<s2->len)
ret = 1;
else ret = 0;
break;
case LTE_OP:
if ( s2->s==NULL ) return 0;
rt = str_strcasecmp(s1, s2);
if (rt<0)
ret = 1;
else if(rt==0 && s1->len<=s2->len)
ret = 1;
else ret = 0;
break;
case MATCH_OP:
if ( s2==NULL ) return 0;
make_nt_copy( &cp1, s1);
ret=(regexec((regex_t*)s2, cp1.s, 0, 0, 0)==0);
break;
case NOTMATCH_OP:
if ( s2==NULL ) return 1;
make_nt_copy( &cp1, s1);
ret=(regexec((regex_t*)s2, cp1.s, 0, 0, 0)!=0);
break;
case MATCHD_OP:
case NOTMATCHD_OP:
if ( s2==NULL || s2->s==NULL)
return (op == MATCHD_OP? 0 : 1);
re=(regex_t*)pkg_malloc(sizeof(regex_t));
if (re==0) {
LM_CRIT("pkg memory allocation failure\n");
return -1;
}
make_nt_copy( &cp1, s1);
make_nt_copy( &cp2, s2);
if (regcomp(re, cp2.s, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
pkg_free(re);
return -1;
}
if(op==MATCHD_OP)
ret=(regexec(re, cp1.s, 0, 0, 0)==0);
else
ret=(regexec(re, cp1.s, 0, 0, 0)!=0);
regfree(re);
pkg_free(re);
break;
default:
LM_CRIT("unknown op %d\n", op);
}
return ret;
}
/*! \brief compare nr to nr */
inline static int comp_n2n(int op, int n1, int n2)
{
switch(op) {
case EQUAL_OP:
case MATCH_OP:
case MATCHD_OP:
if(n1 == n2)
return 1;
return 0;
case NOTMATCH_OP:
case NOTMATCHD_OP:
case DIFF_OP:
if(n1 != n2)
return 1;
return 0;
case GT_OP:
if(n1 > n2)
return 1;
return 0;
case GTE_OP:
if(n1 >= n2)
return 1;
return 0;
case LT_OP:
if(n1 < n2)
return 1;
return 0;
case LTE_OP:
if(n1 <= n2)
return 1;
return 0;
default:
LM_CRIT("unknown op %d\n", op);
}
return -1;
}
static inline const char *op_id_2_string(int op_id)
{
switch (op_id) {
case EQUAL_OP:
return "EQUAL";
case MATCH_OP:
return "REGEXP_MATCH";
case NOTMATCH_OP:
return "REGEXP_NO_MATCH";
case MATCHD_OP:
return "DYN_REGEXP_MATCH";
case NOTMATCHD_OP:
return "DYN_REGEXP_NO_MATCH";
case GT_OP:
return "GREATER_THAN";
case LT_OP:
return "LESS_THAN";
case GTE_OP:
return "GREATER_OR_EQUAL";
case LTE_OP:
return "LESS_OR_EQUAL";
case DIFF_OP:
return "DIFFERENT_THAN";
case VALUE_OP:
return "VALUE";
case NO_OP:
default:
return "NONE";
}
}
static inline const char *expr_type_2_string(int expr_type)
{
switch (expr_type) {
case STRING_ST:
return "STRING";
case NET_ST:
return "NET_MASK";
case NUMBER_ST:
return "NUMBER";
case IP_ST:
return "IP";
case RE_ST:
return "REGEXP";
case PROXY_ST:
return "PROXY";
case EXPR_ST:
return "EXPRESSION";
case ACTIONS_ST:
return "ACTION";
case CMD_ST:
return "FUNCTION";
case MODFIXUP_ST:
return "MOD_FIXUP";
case STR_ST:
return "STR";
case SOCKID_ST:
return "SOCKET";
case SOCKETINFO_ST:
return "SOCKET_INFO";
case SCRIPTVAR_ST:
return "VARIABLE";
case NULLV_ST:
return "NULL";
case BLACKLIST_ST:
return "BLACKLIST";
case SCRIPTVAR_ELEM_ST:
return "VARIABLE_ELEMENT";
case NOSUBTYPE:
default:
return"NONE";
}
}
static inline const char *val_type_2_string(int val_type)
{
if (val_type&PV_VAL_STR)
return "STRING_VAL";
if (val_type&PV_VAL_INT)
return "INTEGER_VAL";
if (val_type&PV_VAL_NULL)
return "NULL_VAL";
if (val_type&PV_VAL_EMPTY)
return "EMPTY_VAL";
return "NO_VAL";
}
inline static int comp_scriptvar(struct sip_msg *msg, int op, operand_t *left,
operand_t *right)
{
str lstr;
str rstr;
int ln;
int rn;
pv_value_t lvalue;
pv_value_t rvalue;
int type;
struct net *rnet;
lstr.s = 0; lstr.len = 0;
rstr.s = 0; rstr.len = 0;
ln = 0; rn =0;
if(pv_get_spec_value(msg, left->v.spec, &lvalue)!=0)
{
LM_ERR("cannot get left var value\n");
goto error;
}
if(right->type==NULLV_ST)
{
if(op==EQUAL_OP)
{
if(lvalue.flags&PV_VAL_NULL)
return 1;
return 0;
} else {
if(lvalue.flags&PV_VAL_NULL)
return 0;
return 1;
}
}
lstr = lvalue.rs;
ln = lvalue.ri;
type = 0;
rvalue.flags = 0; /*just for err printing purposes */
if(right->type == SCRIPTVAR_ST)
{
if(pv_get_spec_value(msg, right->v.spec, &rvalue)!=0)
{
LM_ERR("cannot get right var value\n");
goto error;
}
if(rvalue.flags&PV_VAL_NULL || lvalue.flags&PV_VAL_NULL ) {
if (rvalue.flags&PV_VAL_NULL && lvalue.flags&PV_VAL_NULL )
return (op==EQUAL_OP || op==MATCH_OP || op==MATCHD_OP)?1:0;
return (op==DIFF_OP || op==NOTMATCH_OP || op==NOTMATCHD_OP)?1:0;
}
if(op==MATCH_OP||op==NOTMATCH_OP)
{
if(!((rvalue.flags&PV_VAL_STR) && (lvalue.flags&PV_VAL_STR)))
goto error_op;
if(op==MATCH_OP)
return comp_s2s(MATCHD_OP, &lstr, &rvalue.rs);
else
return comp_s2s(NOTMATCHD_OP, &lstr, &rvalue.rs);
}
if((rvalue.flags&PV_VAL_INT) && (lvalue.flags&PV_VAL_INT)) {
/* comparing int */
rn = rvalue.ri;
type = 2;
} else if((rvalue.flags&PV_VAL_STR) && (lvalue.flags&PV_VAL_STR)) {
/* comparing string */
rstr = rvalue.rs;
type = 1;
} else
goto error_op;
} else {
/* null against a not-null constant */
if(lvalue.flags&PV_VAL_NULL)
return (op==DIFF_OP || op==NOTMATCH_OP || op==NOTMATCHD_OP)?1:0;
if(right->type == STR_ST) {
if(!(lvalue.flags&PV_VAL_STR))
goto error_op;
/* comparing string */
type = 1;
rstr = right->v.s;
} else if(right->type == NUMBER_ST) {
if(!(lvalue.flags&PV_VAL_INT))
goto error_op;
/* comparing int */
type = 2;
rn = right->v.n;
} else if (right->type == NET_ST) {
if(!(lvalue.flags&PV_VAL_STR))
goto error_op;
/* comparing IP */
type = 3;
rnet = (struct net*)right->v.data;
} else {
if(op==MATCH_OP || op==NOTMATCH_OP)
{
if(!(lvalue.flags&PV_VAL_STR) || right->type != RE_ST)
goto error_op;
return comp_s2s(op, &lstr, (str*)right->v.expr);
}
/* comparing others */
type = 0;
}
}
switch (type) {
case 1: /* compare str */
LM_DBG("str %d: %.*s\n", op, lstr.len, lstr.s);
return comp_s2s(op, &lstr, &rstr);
case 2:
LM_DBG("int %d: %d / %d\n", op, ln, rn);
return comp_n2n(op, ln, rn);
case 3:
LM_DBG("ip %d: %.*s\n", op, lstr.len, lstr.s);
return comp_ip(op, &lstr, rnet);
}
/* default is error */
error_op:
LM_WARN("invalid %s operation: left is %s/%s, right is %s/%s\n",
op_id_2_string(op),
expr_type_2_string(left->type), val_type_2_string(lvalue.flags),
expr_type_2_string(right->type), val_type_2_string(rvalue.flags) );
error:
return -1;
}
/*! \brief
* \return 0/1 (false/true) or -1 on error, -127 EXPR_DROP
*/
static int eval_elem(struct expr* e, struct sip_msg* msg, pv_value_t *val)
{
int ret;
/* int retl;
int retr; */
int ival;
pv_value_t lval;
pv_value_t rval;
char *p;
int i,n;
ret=E_BUG;
if (e->type!=ELEM_T){
LM_CRIT("invalid type\n");
goto error;
}
if(val) memset(val, 0, sizeof(pv_value_t));
switch(e->left.type){
case NUMBER_O:
ret=!(!e->right.v.n); /* !! to transform it in {0,1} */
break;
case ACTION_O:
ret=run_action_list( (struct action*)e->right.v.data, msg);
if(val)
{
val->flags = PV_TYPE_INT|PV_VAL_INT;
val->ri = ret;
}
if (ret<=0) ret=(ret==0)?EXPR_DROP:0;
else ret=1;
return ret;
case EXPR_O:
/* retl = retr = 0; */
memset(&lval, 0, sizeof(pv_value_t));
memset(&rval, 0, sizeof(pv_value_t));
if(e->left.v.data)
eval_expr((struct expr*)e->left.v.data,msg,&lval);
/* XXX why is retl used here ?? */
/* retl=eval_expr((struct expr*)e->left.v.data,msg,&lval); */
if(lval.flags == PV_VAL_NONE)
{
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
}
if(e->op == BNOT_OP)
{
if(lval.flags&PV_VAL_INT)
{
if(val!=NULL)
{
val->flags = PV_TYPE_INT|PV_VAL_INT;
val->ri = ~lval.ri;
}
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return (val->ri)?1:0;
}
LM_ERR("binary NOT on non-numeric value\n");
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
}
if(e->right.v.data)
eval_expr((struct expr*)e->right.v.data,msg,&rval);
/* retr=eval_expr((struct expr*)e->right.v.data,msg,&rval); */
if(lval.flags&PV_TYPE_INT)
{
if( (rval.flags&PV_VAL_NULL) )
{
rval.ri = 0;
} else if(!(rval.flags&PV_VAL_INT))
{
LM_ERR("invalid numeric operands\n");
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
}
if(val!=NULL)
val->flags = PV_TYPE_INT|PV_VAL_INT;
ival = 0;
switch(e->op) {
case PLUS_OP:
ival = lval.ri + rval.ri;
break;
case MINUS_OP:
ival = lval.ri - rval.ri;
break;
case DIV_OP:
if(rval.ri==0)
{
LM_ERR("divide by 0\n");
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
} else
ival = lval.ri / rval.ri;
break;
case MULT_OP:
ival = lval.ri * rval.ri;
break;
case MODULO_OP:
if(rval.ri==0)
{
LM_ERR("divide by 0\n");
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
} else
ival = lval.ri % rval.ri;
break;
case BAND_OP:
ival = lval.ri & rval.ri;
break;
case BOR_OP:
ival = lval.ri | rval.ri;
break;
case BXOR_OP:
ival = lval.ri ^ rval.ri;
break;
case BLSHIFT_OP:
ival = lval.ri << rval.ri;
break;
case BRSHIFT_OP:
ival = lval.ri >> rval.ri;
break;
default:
LM_ERR("invalid int op %d\n", e->op);
if(val!=NULL) val->ri = 0;
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
}
pv_value_destroy(&lval);
pv_value_destroy(&rval);
if(val!=NULL) val->ri = ival;
return (ival)?1:0;
} else if (e->op == PLUS_OP) {
if (!val) {
ret = (lval.rs.len>0 || rval.rs.len>0);
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return ret;
}
if (rval.flags & PV_VAL_NULL) {
pv_value_destroy(&rval);
rval.flags = PV_VAL_STR;
}
if(!(rval.flags&PV_VAL_STR))
{
LM_ERR("invalid string operands\n");
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
}
val->rs.s=(char*)pkg_malloc((lval.rs.len+rval.rs.len+1)
*sizeof(char));
if(val->rs.s==0)
{
LM_ERR("no more memory\n");
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
}
val->flags = PV_VAL_PKG|PV_VAL_STR;
memcpy(val->rs.s, lval.rs.s, lval.rs.len);
memcpy(val->rs.s+lval.rs.len, rval.rs.s, rval.rs.len);
val->rs.len = lval.rs.len + rval.rs.len;
val->rs.s[val->rs.len] = '\0';
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 1;
} else if ((lval.flags & PV_VAL_STR) && (rval.flags & PV_VAL_STR)) {
if (lval.rs.len != rval.rs.len)
{
LM_ERR("Different length string operands\n");
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
}
n = lval.rs.len;
val->rs.s = pkg_malloc(n+1);
if (!val->rs.s)
{
LM_ERR("no more memory\n");
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
}
switch(e->op) {
case BAND_OP:
for (i=0;i<n;i++)
val->rs.s[i] = lval.rs.s[i] & rval.rs.s[i];
break;
case BOR_OP:
for (i=0;i<n;i++)
val->rs.s[i] = lval.rs.s[i] | rval.rs.s[i];
break;
case BXOR_OP:
for (i=0;i<n;i++)
val->rs.s[i] = lval.rs.s[i] ^ rval.rs.s[i];
break;
default:
LM_ERR("Only bitwise operations can be applied on strings\n");
val->ri = 0;
pv_value_destroy(&lval);
pv_value_destroy(&rval);
return 0;
}
val->flags = PV_VAL_PKG|PV_VAL_STR;