forked from eliasbaixas/openser-xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.c
1630 lines (1538 loc) · 37.9 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
/*
* $Id: route.c 2785 2007-09-17 17:07:01Z bogdan_iancu $
*
* SIP routing engine
*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2005-2006 Voice Sistem S.R.L.
*
* This file is part of openser, a free SIP server.
*
* openser 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
*
* openser 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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)
*/
#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 "parser/parse_uri.h"
#include "parser/parse_from.h"
#include "parser/parse_to.h"
#include "mem/mem.h"
/* main routing script table */
struct action* rlist[RT_NO];
/* reply routing table */
struct action* onreply_rlist[ONREPLY_RT_NO];
struct action* failure_rlist[FAILURE_RT_NO];
struct action* branch_rlist[BRANCH_RT_NO];
struct action* error_rlist;
int route_type = REQUEST_ROUTE;
static int fix_actions(struct action* a); /*fwd declaration*/
extern int return_code;
/*
*
*/
void init_route_lists(void)
{
memset(rlist, 0, sizeof(rlist));
memset(onreply_rlist, 0, sizeof(onreply_rlist));
memset(failure_rlist, 0, sizeof(failure_rlist));
memset(branch_rlist, 0, sizeof(branch_rlist));
error_rlist = 0;
}
/* traverses an expr tree and compiles the REs where necessary)
* returns: 0 for ok, <0 if errors */
static int fix_expr(struct expr* exp)
{
regex_t* re;
int ret;
ret=E_BUG;
if (exp==0){
LM_CRIT("null pointer\n");
return E_BUG;
}
if (exp->type==EXP_T){
switch(exp->op){
case AND_OP:
case OR_OP:
if ((ret=fix_expr(exp->left.v.expr))!=0)
return ret;
ret=fix_expr(exp->right.v.expr);
break;
case NOT_OP:
case EVAL_OP:
ret=fix_expr(exp->left.v.expr);
break;
default:
LM_CRIT("unknown op %d\n", exp->op);
}
}else if (exp->type==ELEM_T){
if (exp->op==MATCH_OP || exp->op==NOTMATCH_OP){
if (exp->right.type==STRING_ST){
re=(regex_t*)pkg_malloc(sizeof(regex_t));
if (re==0){
LM_CRIT("out of pkg memory\n");
return E_OUT_OF_MEM;
}
if (regcomp(re, (char*) exp->right.v.data,
REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
LM_CRIT("bad re \"%s\"\n", (char*) exp->right.v.data);
pkg_free(re);
return E_BAD_RE;
}
/* replace the string with the re */
pkg_free(exp->right.v.data);
exp->right.v.data=re;
exp->right.type=RE_ST;
}else if (exp->right.type!=RE_ST
&& exp->right.type!=SCRIPTVAR_ST){
LM_CRIT("invalid type for match\n");
return E_BUG;
}
}
if (exp->left.type==ACTION_O){
ret=fix_actions((struct action*)exp->right.v.data);
if (ret!=0){
LM_CRIT("fix_actions error\n");
return ret;
}
}
if (exp->left.type==EXPR_O){
ret=fix_expr(exp->left.v.expr);
if (ret!=0){
LM_CRIT("fix left exp error\n");
return ret;
}
}
if (exp->right.type==EXPR_ST){
ret=fix_expr(exp->right.v.expr);
if (ret!=0){
LM_CRIT("fix rigth exp error\n");
return ret;
}
}
ret=0;
}
return ret;
}
/* adds the proxies in the proxy list & resolves the hostnames */
/* returns 0 if ok, <0 on error */
static int fix_actions(struct action* a)
{
struct action *t;
int ret;
cmd_export_t* cmd;
struct hostent* he;
struct ip_addr ip;
struct socket_info* si;
str host;
int proto=PROTO_NONE, port;
struct proxy_l *p;
struct bl_head *blh;
if (a==0){
LM_CRIT("null pointer\n");
return E_BUG;
}
for(t=a; t!=0; t=t->next){
switch(t->type){
case FORWARD_T:
if (t->elem[0].type==NOSUBTYPE)
break;
case SEND_T:
if (t->elem[0].type!=STRING_ST) {
LM_CRIT("invalid type %d (should be string)\n", t->type);
return E_BUG;
}
ret = parse_phostport( t->elem[0].u.string,
strlen(t->elem[0].u.string),
&host.s, &host.len, &port, &proto);
if (ret!=0) {
LM_ERR("ERROR:fix_actions: FORWARD/SEND bad "
"argument\n");
return E_CFG;
}
p = add_proxy( &host,(unsigned short)port, proto);
if (p==0) {
LM_ERR("forward/send failed to add proxy");
return E_CFG;
}
t->elem[0].type = PROXY_ST;
t->elem[0].u.data = (void*)p;
break;
case IF_T:
if (t->elem[0].type!=EXPR_ST){
LM_CRIT("invalid subtype %d for if (should be expr)\n",
t->elem[0].type);
return E_BUG;
}else if( (t->elem[1].type!=ACTIONS_ST)
&&(t->elem[1].type!=NOSUBTYPE) ){
LM_CRIT("invalid subtype %d for if() {...} (should be"
"action)\n", t->elem[1].type);
return E_BUG;
}else if( (t->elem[2].type!=ACTIONS_ST)
&&(t->elem[2].type!=NOSUBTYPE) ){
LM_CRIT("invalid subtype %d for if() {} else{...}(should"
"be action)\n", t->elem[2].type);
return E_BUG;
}
if (t->elem[0].u.data){
if ((ret=fix_expr((struct expr*)t->elem[0].u.data))<0)
return ret;
}
if ( (t->elem[1].type==ACTIONS_ST)&&(t->elem[1].u.data) ){
if ((ret=fix_actions((struct action*)t->elem[1].u.data))<0)
return ret;
}
if ( (t->elem[2].type==ACTIONS_ST)&&(t->elem[2].u.data) ){
if((ret=fix_actions((struct action*)t->elem[2].u.data))<0)
return ret;
}
break;
case WHILE_T:
if (t->elem[0].type!=EXPR_ST){
LM_CRIT("invalid subtype %d for while (should be expr)\n",
t->elem[0].type);
return E_BUG;
}else if( (t->elem[1].type!=ACTIONS_ST)
&&(t->elem[1].type!=NOSUBTYPE) ){
LM_CRIT("invalid subtype %d for while() {...} (should be"
"action)\n", t->elem[1].type);
return E_BUG;
}
if (t->elem[0].u.data){
if ((ret=fix_expr((struct expr*)t->elem[0].u.data))<0)
return ret;
}
if ( (t->elem[1].type==ACTIONS_ST)&&(t->elem[1].u.data) ){
if ((ret=fix_actions((struct action*)t->elem[1].u.data))<0)
return ret;
}
break;
case SWITCH_T:
if ( (t->elem[1].type==ACTIONS_ST)&&(t->elem[1].u.data) ){
if ((ret=fix_actions((struct action*)t->elem[1].u.data))<0)
return ret;
}
break;
case CASE_T:
if ( (t->elem[1].type==ACTIONS_ST)&&(t->elem[1].u.data) ){
if ((ret=fix_actions((struct action*)t->elem[1].u.data))<0)
return ret;
}
break;
case DEFAULT_T:
if ( (t->elem[0].type==ACTIONS_ST)&&(t->elem[0].u.data) ){
if ((ret=fix_actions((struct action*)t->elem[0].u.data))<0)
return ret;
}
break;
case MODULE_T:
cmd = (cmd_export_t*)t->elem[0].u.data;
LM_DBG("fixing %s, line %d\n", cmd->name, t->line);
if (cmd->fixup){
if (cmd->param_no>0){
ret=cmd->fixup(&t->elem[1].u.data, 1);
t->elem[1].type=MODFIXUP_ST;
if (ret<0) goto error;
}
if (cmd->param_no>1){
ret=cmd->fixup(&t->elem[2].u.data, 2);
t->elem[2].type=MODFIXUP_ST;
if (ret<0) goto error;
}
if (cmd->param_no==0){
ret=cmd->fixup( 0, 0);
if (ret<0) goto error;
}
}
break;
case FORCE_SEND_SOCKET_T:
if (t->elem[0].type!=SOCKID_ST){
LM_CRIT("invalid subtype %d for force_send_socket\n",
t->elem[0].type);
return E_BUG;
}
he=resolvehost(((struct socket_id*)t->elem[0].u.data)->name,0);
if (he==0){
LM_ERR(" could not resolve %s\n",
((struct socket_id*)t->elem[0].u.data)->name);
ret = E_BAD_ADDRESS;
goto error;
}
hostent2ip_addr(&ip, he, 0);
si=find_si(&ip, ((struct socket_id*)t->elem[0].u.data)->port,
((struct socket_id*)t->elem[0].u.data)->proto);
if (si==0){
LM_ERR("bad force_send_socket"
" argument: %s:%d (ser doesn't listen on it)\n",
((struct socket_id*)t->elem[0].u.data)->name,
((struct socket_id*)t->elem[0].u.data)->port);
ret = E_BAD_ADDRESS;
goto error;
}
t->elem[0].u.data=si;
t->elem[0].type=SOCKETINFO_ST;
break;
case SET_DEBUG_T:
if (t->elem[0].type==NOSUBTYPE)
break;
if (t->elem[0].type!=NUMBER_ST) {
LM_CRIT("fix_actions: BUG in setdebug() type %d\n",
t->elem[0].type );
ret=E_BUG;
goto error;
}
/* normalize the value */
if (t->elem[0].u.number>L_DBG)
t->elem[0].u.number = L_DBG;
else if (t->elem[0].u.number<L_ALERT)
t->elem[0].u.number = L_ALERT;
break;
case SETFLAG_T:
case RESETFLAG_T:
case ISFLAGSET_T:
if (t->elem[0].type!=NUMBER_ST) {
LM_CRIT("bad xxxflag() type %d\n", t->elem[0].type );
ret=E_BUG;
goto error;
}
if (!flag_in_range( t->elem[0].u.number )) {
ret=E_CFG;
goto error;
}
break;
case SETSFLAG_T:
case RESETSFLAG_T:
case ISSFLAGSET_T:
if (t->elem[0].type!=NUMBER_ST) {
LM_CRIT("bad xxxsflag() type %d\n", t->elem[0].type );
ret=E_BUG;
goto error;
}
t->elem[0].u.number = fixup_flag( t->elem[0].u.number );
if (t->elem[0].u.data==0) {
ret=E_CFG;
goto error;
}
break;
case SETBFLAG_T:
case RESETBFLAG_T:
case ISBFLAGSET_T:
if (t->elem[0].type!=NUMBER_ST || t->elem[1].type!=NUMBER_ST) {
LM_CRIT("bad xxxbflag() type "
"%d,%d\n", t->elem[0].type, t->elem[0].type);
ret=E_BUG;
goto error;
}
t->elem[1].u.number = fixup_flag( t->elem[1].u.number );
if (t->elem[1].u.data==0) {
ret=E_CFG;
goto error;
}
break;
case EQ_T:
case COLONEQ_T:
case PLUSEQ_T:
case MINUSEQ_T:
case DIVEQ_T:
case MULTEQ_T:
case MODULOEQ_T:
case BANDEQ_T:
case BOREQ_T:
case BXOREQ_T:
if (t->elem[1].u.data){
if ((ret=fix_expr((struct expr*)t->elem[1].u.data))<0)
return ret;
}
break;
case USE_BLACKLIST_T:
if (t->elem[0].type!=STRING_ST) {
LM_CRIT("bad USE_BLACKLIST type %d\n", t->elem[0].type);
ret=E_BUG;
goto error;
}
host.s = t->elem[0].u.string;
host.len = strlen(host.s);
blh = get_bl_head_by_name(&host);
if (blh==NULL) {
LM_ERR("USE_BLACKLIST - list "
"%s not configured\n", t->elem[0].u.string);
ret=E_CFG;
goto error;
}
t->elem[0].type = BLACKLIST_ST;
t->elem[0].u.data = blh;
break;
}
}
return 0;
error:
LM_ERR("fixing failed (code=%d) at cfg line %d\n", ret, t->line);
return ret;
}
inline static int comp_no( int port, void *param, int op, int subtype )
{
if (subtype!=NUMBER_ST) {
LM_CRIT("number expected: %d\n", subtype );
return E_BUG;
}
switch (op){
case EQUAL_OP:
return port==(long)param;
case DIFF_OP:
return port!=(long)param;
case GT_OP:
return port>(long)param;
case LT_OP:
return port<(long)param;
case GTE_OP:
return port>=(long)param;
case LTE_OP:
return port<=(long)param;
default:
LM_CRIT("unknown operator: %d\n", op );
return E_BUG;
}
}
/* eval_elem helping function, returns str op param */
inline static int comp_strval(struct sip_msg *msg, int op, str* ival,
operand_t *opd)
{
int ret;
regex_t* re;
char backup;
char backup2;
str res;
pv_value_t value;
if(ival==NULL || ival->s==NULL)
goto error;
res.s = 0; res.len = 0;
if(opd->type == SCRIPTVAR_ST)
{
if(pv_get_spec_value(msg, opd->v.spec, &value)!=0)
{
LM_CRIT("cannot get var value\n");
goto error;
}
if(value.flags&PV_VAL_STR)
{
res = value.rs;
} else {
res.s = sint2str(value.ri, &res.len);
}
} else if(opd->type == NUMBER_ST) {
res.s = sint2str(opd->v.n, &res.len);
}else if(opd->type == STRING_ST) {
res = opd->v.s;
} else {
if((op!=MATCH_OP && op!=NOTMATCH_OP) || opd->type != RE_ST)
{
LM_CRIT("invalid operation %d/%d\n", op, opd->type);
goto error;
}
}
ret=-1;
switch(op){
case EQUAL_OP:
if(ival->len != res.len) return 0;
ret=(strncasecmp(ival->s, res.s, ival->len)==0);
break;
case DIFF_OP:
if(ival->len != res.len) return 1;
ret=(strncasecmp(ival->s, res.s, ival->len)!=0);
break;
case MATCH_OP:
case NOTMATCH_OP:
backup=ival->s[ival->len];ival->s[ival->len]='\0';
if(opd->type == SCRIPTVAR_ST) {
re=(regex_t*)pkg_malloc(sizeof(regex_t));
if (re==0){
LM_CRIT("pkg memory allocation failure\n");
ival->s[ival->len]=backup;
goto error;
}
backup2 = res.s[res.len];res.s[res.len] = '\0';
if (regcomp(re, res.s, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
pkg_free(re);
res.s[res.len] = backup2;
ival->s[ival->len]=backup;
goto error;
}
ret=(regexec(re, ival->s, 0, 0, 0)==0);
regfree(re);
pkg_free(re);
res.s[res.len] = backup2;
} else {
ret=(regexec((regex_t*)opd->v.data, ival->s, 0, 0, 0)==0);
}
ival->s[ival->len]=backup;
if(op==NOTMATCH_OP)
ret = !ret;
break;
default:
LM_CRIT("unknown op %d\n", op);
goto error;
}
return ret;
error:
return -1;
}
/* eval_elem helping function, returns str op param */
inline static int comp_str(char* str, void* param, int op, int subtype)
{
int ret;
ret=-1;
switch(op){
case EQUAL_OP:
if (subtype!=STRING_ST){
LM_CRIT("bad type %d, string expected\n", subtype);
goto error;
}
ret=(strcasecmp(str, (char*)param)==0);
break;
case DIFF_OP:
if (subtype!=STRING_ST){
LM_CRIT("bad type %d, string expected\n", subtype);
goto error;
}
ret=(strcasecmp(str, (char*)param)!=0);
break;
case MATCH_OP:
if (subtype!=RE_ST){
LM_CRIT("bad type %d, RE expected\n", subtype);
goto error;
}
ret=(regexec((regex_t*)param, str, 0, 0, 0)==0);
break;
case NOTMATCH_OP:
if (subtype!=RE_ST){
LM_CRIT("bad type %d, RE expected!\n", subtype);
goto error;
}
ret=(regexec((regex_t*)param, str, 0, 0, 0)!=0);
break;
default:
LM_CRIT("unknown op %d\n", op);
goto error;
}
return ret;
error:
return -1;
}
/* check_self wrapper -- it checks also for the op */
inline static int check_self_op(int op, str* s, unsigned short p)
{
int ret;
ret=check_self(s, p, 0);
switch(op){
case EQUAL_OP:
break;
case DIFF_OP:
if (ret>=0) ret=!ret;
break;
default:
LM_CRIT("invalid operator %d\n", op);
ret=-1;
}
return ret;
}
/* eval_elem helping function, returns an op param */
inline static int comp_ip(struct sip_msg *msg, int op, struct ip_addr* ip,
operand_t *opd)
{
struct hostent* he;
char ** h;
int ret;
str tmp;
ret=-1;
switch(opd->type){
case NET_ST:
switch(op){
case EQUAL_OP:
ret=(matchnet(ip, (struct net*)opd->v.data)==1);
break;
case DIFF_OP:
ret=(matchnet(ip, (struct net*)opd->v.data)!=1);
break;
default:
goto error_op;
}
break;
case STRING_ST:
case RE_ST:
switch(op){
case EQUAL_OP:
case MATCH_OP:
/* 1: compare with ip2str*/
ret=comp_str(ip_addr2a(ip), opd->v.data, op, opd->type);
if (ret==1) break;
/* 2: resolve (name) & compare w/ all the ips */
if (opd->type==STRING_ST){
he=resolvehost((char*)opd->v.data,0);
if (he==0){
LM_DBG("could not resolve %s\n",(char*)opd->v.data);
}else if (he->h_addrtype==(int)ip->af){
for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
ret=(memcmp(ip->u.addr, *h, ip->len)==0);
}
if (ret==1) break;
}
}
/* 3: (slow) rev dns the address
* and compare with all the aliases
* !!??!! review: remove this? */
he=rev_resolvehost(ip);
if (he==0){
print_ip( "comp_ip: could not rev_resolve ip address:"
" ", ip, "\n");
ret=0;
}else{
/* compare with primary host name */
ret=comp_str(he->h_name, opd->v.data, op, opd->type);
/* compare with all the aliases */
for(h=he->h_aliases; (ret!=1) && (*h); h++){
ret=comp_str(*h, opd->v.data, op, opd->type);
}
}
break;
case DIFF_OP:
ret=comp_ip(msg, op, ip, opd);
if (ret>=0) ret=!ret;
break;
default:
goto error_op;
}
break;
case MYSELF_ST: /* check if it's one of our addresses*/
tmp.s=ip_addr2a(ip);
tmp.len=strlen(tmp.s);
ret=check_self_op(op, &tmp, 0);
break;
default:
LM_CRIT("invalid type for src_ip or dst_ip (%d)\n", opd->type);
ret=-1;
}
return ret;
error_op:
LM_CRIT("invalid operator %d\n", op);
return -1;
}
/* compare str to str */
inline static int comp_s2s(int op, str *s1, str *s2)
{
char backup;
char backup2;
int n;
int rt;
int ret;
regex_t* re;
ret = -1;
/* if any of values is NULL return FALSE */
if(s1->s==NULL || s2->s==NULL)
return 0;
switch(op) {
case EQUAL_OP:
if(s1->len != s2->len) return 0;
ret=(strncasecmp(s1->s, s2->s, s2->len)==0);
break;
case DIFF_OP:
if(s1->len != s2->len) return 1;
ret=(strncasecmp(s1->s, s2->s, s2->len)!=0);
break;
case GT_OP:
n = (s1->len>=s2->len)?s1->len:s2->len;
rt = strncasecmp(s1->s,s2->s, n);
if (rt>0)
ret = 1;
else if(rt==0 && s1->len>s1->len)
ret = 1;
else ret = 0;
break;
case GTE_OP:
n = (s1->len>=s2->len)?s1->len:s2->len;
rt = strncasecmp(s1->s,s2->s, n);
if (rt>0)
ret = 1;
else if(rt==0 && s1->len>=s1->len)
ret = 1;
else ret = 0;
break;
case LT_OP:
n = (s1->len>=s2->len)?s1->len:s2->len;
rt = strncasecmp(s1->s,s2->s, n);
if (rt<0)
ret = 1;
else if(rt==0 && s1->len<s1->len)
ret = 1;
else ret = 0;
break;
case LTE_OP:
n = (s1->len>=s2->len)?s1->len:s2->len;
rt = strncasecmp(s1->s,s2->s, n);
if (rt<0)
ret = 1;
else if(rt==0 && s1->len<=s1->len)
ret = 1;
else ret = 0;
break;
case MATCH_OP:
backup = s1->s[s1->len]; s1->s[s1->len] = '\0';
ret=(regexec((regex_t*)s2, s1->s, 0, 0, 0)==0);
s1->s[s1->len] = backup;
break;
case NOTMATCH_OP:
backup = s1->s[s1->len]; s1->s[s1->len] = '\0';
ret=(regexec((regex_t*)s2, s1->s, 0, 0, 0)!=0);
s1->s[s1->len] = backup;
break;
case MATCHD_OP:
case NOTMATCHD_OP:
re=(regex_t*)pkg_malloc(sizeof(regex_t));
if (re==0) {
LM_CRIT("pkg memory allocation failure\n");
return -1;
}
backup = s1->s[s1->len]; s1->s[s1->len] = '\0';
backup2 = s2->s[s2->len]; s2->s[s2->len] = '\0';
if (regcomp(re, s2->s, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
pkg_free(re);
s2->s[s2->len] = backup2;
s1->s[s1->len] = backup;
return -1;
}
if(op==MATCHD_OP)
ret=(regexec(re, s1->s, 0, 0, 0)==0);
else
ret=(regexec(re, s1->s, 0, 0, 0)!=0);
regfree(re);
pkg_free(re);
s2->s[s2->len] = backup2;
s1->s[s1->len] = backup;
break;
default:
LM_CRIT("unknown op %d\n", op);
}
return ret;
}
/* 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;
}
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;
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_CRIT("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;
if(right->type == SCRIPTVAR_ST)
{
if(pv_get_spec_value(msg, right->v.spec, &rvalue)!=0)
{
LM_CRIT("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)?1:0;
return (op==DIFF_OP)?1:0;
}
if(op==MATCH_OP||op==NOTMATCH_OP)
{
if(!((rvalue.flags&PV_VAL_STR) && (lvalue.flags&PV_VAL_STR)))
{
LM_CRIT("invalid operation %d/%d\n", op, right->type);
goto error;
}
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 {
LM_CRIT("invalid operation %d/%d!\n", op,
right->type);
goto error;
}
} 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 == NUMBER_ST) {
if(!(lvalue.flags&PV_VAL_INT))
{
LM_CRIT("invalid operation %d/%d/%d!!\n", op,
right->type, lvalue.flags);
goto error;
}
/* comparing int */
type =2;
rn = right->v.n;
} else if(right->type == STRING_ST) {
if(!(lvalue.flags&PV_VAL_STR))
{
LM_CRIT("invalid operation %d/%d!!!\n", op, right->type);
goto error;
}
/* comparing string */
type =1;
rstr = right->v.s;
} else {
if(op==MATCH_OP || op==NOTMATCH_OP)
{
if(!(lvalue.flags&PV_VAL_STR) || right->type != RE_ST)
{
LM_CRIT("invalid operation %d/%d\n", op, right->type);
goto error;
}
return comp_s2s(op, &lstr, (str*)right->v.expr);
}
/* comparing others */
type = 0;
}
}
if(type==1) { /* compare str */
LM_DBG("str %d : %.*s\n", op, lstr.len, ZSW(lstr.s));
return comp_s2s(op, &lstr, &rstr);
} else if(type==2) {
LM_DBG("int %d : %d / %d\n", op, ln, rn);
return comp_n2n(op, ln, rn);
} else {
LM_CRIT("invalid operation %d/%d\n", op, right->type);
}
error:
return -1;
}
/* returns: 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)
{
struct sip_uri uri;
int ret;
int retl;
int retr;
int ival;
pv_value_t lval;
pv_value_t rval;
char *p;
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 METHOD_O:
ret=comp_strval(msg, e->op, &msg->first_line.u.request.method,
&e->right);
break;
case URI_O:
if(msg->new_uri.s){
if (e->right.type==MYSELF_ST){