-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.c
2122 lines (1965 loc) · 53.9 KB
/
action.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$
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* ---------
* 2003-02-28 scratchpad compatibility abandoned (jiri)
* 2003-01-29 removed scratchpad (jiri)
* 2003-03-19 fixed set* len calculation bug & simplified a little the code
* (should be a little faster now) (andrei)
* replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
* 2003-04-01 Added support for loose routing in forward (janakj)
* 2003-04-12 FORCE_RPORT_T added (andrei)
* 2003-04-22 strip_tail added (jiri)
* 2003-10-02 added SET_ADV_ADDR_T & SET_ADV_PORT_T (andrei)
* 2003-10-29 added FORCE_TCP_ALIAS_T (andrei)
* 2004-11-30 added FORCE_SEND_SOCKET_T (andrei)
* 2005-11-29 added serialize_branches and next_branches (bogdan)
* 2006-03-02 MODULE_T action points to a cmd_export_t struct instead to
* a function address - more info is accessible (bogdan)
* 2006-05-22 forward(_udp,_tcp,_tls) and send(_tcp) merged in forward() and
* send() (bogdan)
* 2006-12-22 functions for script and branch flags added (bogdan)
*/
/*!
* \file
* \brief OpenSIPS Generic functions
*/
#include "action.h"
#include "config.h"
#include "error.h"
#include "dprint.h"
#include "proxy.h"
#include "forward.h"
#include "udp_server.h"
#include "route.h"
#include "parser/msg_parser.h"
#include "parser/parse_uri.h"
#include "ut.h"
#include "sr_module.h"
#include "mem/mem.h"
#include "globals.h"
#include "dset.h"
#include "flags.h"
#include "errinfo.h"
#include "serialize.h"
#include "blacklists.h"
#include "cachedb/cachedb.h"
#include "msg_translator.h"
#include "mod_fix.h"
#ifdef USE_TCP
#include "tcp_server.h"
#endif
#include "script_var.h"
#include "xlog.h"
#include "evi/evi_modules.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#ifdef DEBUG_DMALLOC
#include <dmalloc.h>
#endif
int action_flags = 0;
int return_code = 0;
int max_while_loops = 100;
/* script tracing options */
int use_script_trace = 0;
int script_trace_log_level = L_ALERT;
char *script_trace_info = NULL;
pv_elem_t script_trace_elem;
static int rec_lev=0;
extern err_info_t _oser_err_info;
action_time longest_action[LONGEST_ACTION_SIZE];
int min_action_time=0;
action_elem_p route_params[MAX_REC_LEV];
int route_params_number[MAX_REC_LEV];
int route_rec_level = -1;
static int for_each_handler(struct sip_msg *msg, struct action *a);
/* run actions from a route */
/* returns: 0, or 1 on success, <0 on error */
/* (0 if drop or break encountered, 1 if not ) */
static inline int run_actions(struct action* a, struct sip_msg* msg)
{
int ret;
rec_lev++;
if (rec_lev>ROUTE_MAX_REC_LEV){
LM_ERR("too many recursive routing table lookups (%d) giving up!\n",
rec_lev);
ret=E_UNSPEC;
goto error;
}
if (a==0){
LM_WARN("null action list (rec_level=%d)\n",
rec_lev);
ret=1;
goto error;
}
ret=run_action_list(a, msg);
/* if 'return', reset the flag */
if(action_flags&ACT_FL_RETURN)
action_flags &= ~ACT_FL_RETURN;
rec_lev--;
return ret;
error:
rec_lev--;
return ret;
}
/* run the error route with correct handling - simpler wrapper to
allow the usage from other parts of the code */
void run_error_route(struct sip_msg* msg, int force_reset)
{
int old_route;
LM_DBG("triggering\n");
swap_route_type(old_route, ERROR_ROUTE);
run_actions(error_rlist.a, msg);
/* reset error info */
init_err_info();
set_route_type(old_route);
}
/* run a list of actions */
int run_action_list(struct action* a, struct sip_msg* msg)
{
int ret=E_UNSPEC;
struct action* t;
for (t=a; t!=0; t=t->next){
ret=do_action(t, msg);
/* if action returns 0, then stop processing the script */
if(ret==0)
action_flags |= ACT_FL_EXIT;
/* check for errors */
if (_oser_err_info.eclass!=0 && error_rlist.a!=NULL &&
(route_type&(ERROR_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE))==0 )
run_error_route(msg,0);
/* continue or not ? */
if( action_flags&(ACT_FL_RETURN|ACT_FL_EXIT) )
break;
}
return ret;
}
int run_top_route(struct action* a, struct sip_msg* msg)
{
static unsigned int bl_last_msg_id = 0;
int bk_action_flags;
int bk_rec_lev;
int ret;
bk_action_flags = action_flags;
bk_rec_lev = rec_lev;
action_flags = 0;
rec_lev = 0;
init_err_info();
if (bl_last_msg_id != msg->id) {
bl_last_msg_id = msg->id;
reset_bl_markers();
}
resetsflag( (unsigned int)-1 );
run_actions(a, msg);
ret = action_flags;
action_flags = bk_action_flags;
rec_lev = bk_rec_lev;
/* reset script tracing */
use_script_trace = 0;
return ret;
}
/* execute assignment operation */
int do_assign(struct sip_msg* msg, struct action* a)
{
int ret;
pv_value_t val;
pv_spec_p dspec;
ret = -1;
dspec = (pv_spec_p)a->elem[0].u.data;
if(!pv_is_w(dspec))
{
LM_ERR("read only PV in left expression\n");
goto error;
}
memset(&val, 0, sizeof(pv_value_t));
if(a->elem[1].type != NULLV_ST)
{
ret = eval_expr((struct expr*)a->elem[1].u.data, msg, &val);
if(!((val.flags&PV_VAL_STR)||(val.flags&PV_VAL_INT))) {
LM_WARN("no value in right expression on line %s:%d\n",
a->file, a->line);
goto error2;
}
}
switch ((unsigned char)a->type){
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:
script_trace("assign",
(unsigned char)a->type == EQ_T ? "equal" :
(unsigned char)a->type == COLONEQ_T ? "colon-eq" :
(unsigned char)a->type == PLUSEQ_T ? "plus-eq" :
(unsigned char)a->type == MINUSEQ_T ? "minus-eq" :
(unsigned char)a->type == DIVEQ_T ? "div-eq" :
(unsigned char)a->type == MULTEQ_T ? "mult-eq" :
(unsigned char)a->type == MODULOEQ_T? "modulo-eq" :
(unsigned char)a->type == BANDEQ_T ? "b-and-eq" :
(unsigned char)a->type == BOREQ_T ? "b-or-eq":"b-xor-eq",
msg, a->file, a->line);
if(a->elem[1].type == NULLV_ST)
{
if(pv_set_value(msg, dspec, (int)a->type, 0)<0)
{
LM_ERR("setting PV failed\n");
goto error;
}
} else {
if(pv_set_value(msg, dspec, (int)a->type, &val)<0)
{
LM_ERR("setting PV failed\n");
goto error;
}
}
ret = 1;
break;
default:
LM_ALERT("BUG -> unknown op type %d\n", a->type);
goto error;
}
pv_value_destroy(&val);
return ret;
error:
LM_ERR("error at %s:%d\n", a->file, a->line);
error2:
pv_value_destroy(&val);
return -1;
}
static int do_action_set_adv_address(struct sip_msg *msg, struct action *a)
{
str adv_addr;
int ret = 1; /* continue processing */
if (a->elem[0].type != STR_ST) {
report_programming_bug("set_advertised_address type %d", a->elem[0].type);
ret = E_BUG;
goto out;
}
if (pv_printf_s(msg, (pv_elem_t *)a->elem[0].u.data, &adv_addr) != 0
|| adv_addr.len <= 0) {
LM_WARN("cannot get string for value (%s:%d)\n",a->file,a->line);
ret = E_BUG;
goto out;
}
LM_DBG("setting adv address = [%.*s]\n", adv_addr.len, adv_addr.s);
/* duplicate the advertised address into private memory */
if (adv_addr.len > msg->set_global_address.len) {
msg->set_global_address.s = pkg_realloc(msg->set_global_address.s,
adv_addr.len);
if (!msg->set_global_address.s) {
LM_ERR("out of pkg mem\n");
ret = E_OUT_OF_MEM;
goto out;
}
}
memcpy(msg->set_global_address.s, adv_addr.s, adv_addr.len);
msg->set_global_address.len = adv_addr.len;
out:
return ret;
}
static int do_action_set_adv_port(struct sip_msg *msg, struct action *a)
{
str adv_port;
int ret = 1;
if (a->elem[0].type != STR_ST) {
report_programming_bug("set_advertised_port type %d", a->elem[0].type);
ret = E_BUG;
goto out;
}
if (pv_printf_s(msg, (pv_elem_t *)a->elem[0].u.data, &adv_port) != 0
|| adv_port.len <= 0) {
LM_WARN("cannot get string for value (%s:%d)\n", a->file,a->line);
ret = E_BUG;
goto out;
}
LM_DBG("setting adv port '%.*s'\n", adv_port.len, adv_port.s);
/* duplicate the advertised port into private memory */
if (adv_port.len > msg->set_global_port.len) {
msg->set_global_port.s = pkg_realloc(msg->set_global_port.s,
adv_port.len);
if (!msg->set_global_port.s) {
LM_ERR("out of pkg mem\n");
ret = E_OUT_OF_MEM;
goto out;
}
}
memcpy(msg->set_global_port.s, adv_port.s, adv_port.len);
msg->set_global_port.len = adv_port.len;
out:
return ret;
}
#define should_skip_updating(action_type) \
(action_type == IF_T || action_type == ROUTE_T || \
action_type == WHILE_T || action_type == FOR_EACH_T)
#define update_longest_action(a) do { \
if (execmsgthreshold && !should_skip_updating((unsigned char)(a)->type)) { \
end_time = get_time_diff(&start); \
if (end_time > min_action_time) { \
for (i=0;i<LONGEST_ACTION_SIZE;i++) { \
if (longest_action[i].a_time < end_time) { \
memmove(longest_action+i+1,longest_action+i, \
(LONGEST_ACTION_SIZE-i-1)*sizeof(action_time)); \
longest_action[i].a_time=end_time; \
longest_action[i].a = a; \
min_action_time = longest_action[LONGEST_ACTION_SIZE-1].a_time; \
break; \
} \
} \
} \
} \
} while(0)
/* ret= 0! if action -> end of list(e.g DROP),
> 0 to continue processing next actions
and <0 on error */
int do_action(struct action* a, struct sip_msg* msg)
{
int ret;
int v;
int sec,usec;
union sockaddr_union* to;
struct proxy_l* p;
char* tmp;
char *new_uri, *end, *crt;
int len,i;
int user = 0;
int expires = 0;
str vals[5];
str result;
struct sip_uri uri, next_hop;
struct sip_uri *u;
unsigned short port;
int cmatch;
struct action *aitem;
struct action *adefault;
pv_spec_t *spec;
pv_elem_p model;
pv_value_t val;
pv_elem_t *pve;
str name_s;
struct timeval start;
int end_time;
int aux_counter;
/* reset the value of error to E_UNSPEC so avoid unknowledgable
functions to return with error (status<0) and not setting it
leaving there previous error; cache the previous value though
for functions which want to process it */
prev_ser_error=ser_error;
ser_error=E_UNSPEC;
start_expire_timer(start,execmsgthreshold);
ret=E_BUG;
switch ((unsigned char)a->type){
case DROP_T:
script_trace("core", "drop", msg, a->file, a->line) ;
action_flags |= ACT_FL_DROP;
case EXIT_T:
script_trace("core", "exit", msg, a->file, a->line) ;
ret=0;
action_flags |= ACT_FL_EXIT;
break;
case RETURN_T:
script_trace("core", "return", msg, a->file, a->line) ;
if (a->elem[0].type == SCRIPTVAR_ST)
{
spec = (pv_spec_t*)a->elem[0].u.data;
if(pv_get_spec_value(msg, spec, &val)!=0
|| (val.flags&PV_VAL_NULL))
{
ret=-1;
} else {
if(!(val.flags&PV_VAL_INT))
ret = 1;
else
ret = val.ri;
}
pv_value_destroy(&val);
} else {
ret=a->elem[0].u.number;
}
action_flags |= ACT_FL_RETURN;
break;
case FORWARD_T:
script_trace("core", "forward", msg, a->file, a->line) ;
if (a->elem[0].type==NOSUBTYPE){
/* parse uri and build a proxy */
if (msg->dst_uri.len) {
ret = parse_uri(msg->dst_uri.s, msg->dst_uri.len,
&next_hop);
u = &next_hop;
} else {
ret = parse_sip_msg_uri(msg);
u = &msg->parsed_uri;
}
if (ret<0) {
LM_ERR("forward: bad_uri dropping packet\n");
break;
}
/* create a temporary proxy*/
p=mk_proxy(u->maddr_val.len?&u->maddr_val:&u->host,
u->port_no, u->proto, (u->type==SIPS_URI_T)?1:0 );
if (p==0){
LM_ERR("bad host name in uri, dropping packet\n");
ret=E_BAD_ADDRESS;
goto error_fwd_uri;
}
ret=forward_request(msg, p);
free_proxy(p); /* frees only p content, not p itself */
pkg_free(p);
if (ret==0) ret=1;
}else if ((a->elem[0].type==PROXY_ST)) {
if (0==(p=clone_proxy((struct proxy_l*)a->elem[0].u.data))) {
LM_ERR("failed to clone proxy, dropping packet\n");
ret=E_OUT_OF_MEM;
goto error_fwd_uri;
}
ret=forward_request(msg, p);
free_proxy(p); /* frees only p content, not p itself */
pkg_free(p);
if (ret==0) ret=1;
}else{
LM_ALERT("BUG in forward() types %d, %d\n",
a->elem[0].type, a->elem[1].type);
ret=E_BUG;
}
break;
case SEND_T:
script_trace("core", "send", msg, a->file, a->line) ;
if (a->elem[0].type!= PROXY_ST){
LM_ALERT("BUG in send() type %d\n", a->elem[0].type);
ret=E_BUG;
break;
}
if (a->elem[1].u.data) {
if (a->elem[1].type != SCRIPTVAR_ELEM_ST){
LM_ALERT("BUG in send() header type %d\n",a->elem[1].type);
ret=E_BUG;
break;
} else {
pve = (pv_elem_t *)a->elem[1].u.data;
}
} else {
pve = NULL;
}
to=(union sockaddr_union*)
pkg_malloc(sizeof(union sockaddr_union));
if (to==0){
LM_ERR("memory allocation failure\n");
ret=E_OUT_OF_MEM;
break;
}
if (0==(p=clone_proxy((struct proxy_l*)a->elem[0].u.data))) {
LM_ERR("failed to clone proxy, dropping packet\n");
ret=E_OUT_OF_MEM;
break;
}
ret=hostent2su(to, &p->host, p->addr_idx,
(p->port)?p->port:SIP_PORT );
if (ret==0){
if (pve) {
if ( pv_printf_s(msg, pve, &name_s)!=0 ||
name_s.len == 0 || name_s.s == NULL) {
LM_WARN("cannot get string for value (%s:%d)\n",
a->file,a->line);
ret=E_UNSPEC;
break;
}
/* build new msg */
tmp = pkg_malloc(msg->len + name_s.len);
if (!tmp) {
LM_ERR("memory allocation failure\n");
ret = E_OUT_OF_MEM;
break;
}
LM_DBG("searching for first line %d\n",
msg->first_line.len);
/* search first line of previous msg */
/* copy headers */
len = msg->first_line.len;
memcpy(tmp, msg->buf, len);
memcpy(tmp + len, name_s.s, name_s.len);
memcpy(tmp + len + name_s.len,
msg->buf + len, msg->len - len);
ret = msg_send(0/*send_sock*/, p->proto, to, 0/*id*/,
tmp, msg->len + name_s.len);
pkg_free(tmp);
} else {
ret = msg_send(0/*send_sock*/, p->proto, to, 0/*id*/,
msg->buf, msg->len);
}
if (ret!=0 && p->host.h_addr_list[p->addr_idx+1])
p->addr_idx++;
}
free_proxy(p); /* frees only p content, not p itself */
pkg_free(p);
pkg_free(to);
if (ret==0)
ret=1;
break;
case LOG_T:
script_trace("core", "log", msg, a->file, a->line) ;
if ((a->elem[0].type!=NUMBER_ST)|(a->elem[1].type!=STRING_ST)){
LM_ALERT("BUG in log() types %d, %d\n",
a->elem[0].type, a->elem[1].type);
ret=E_BUG;
break;
}
LM_GEN1(a->elem[0].u.number, "%s", a->elem[1].u.string);
ret=1;
break;
case APPEND_BRANCH_T:
script_trace("core", "append_branch", msg, a->file, a->line) ;
if ((a->elem[0].type!=STR_ST)) {
LM_ALERT("BUG in append_branch %d\n",
a->elem[0].type );
ret=E_BUG;
break;
}
if (a->elem[0].u.s.s==NULL) {
ret = append_branch(msg, 0, &msg->dst_uri, &msg->path_vec,
get_ruri_q(), getb0flags(), msg->force_send_socket);
/* reset all branch info */
msg->force_send_socket = 0;
setb0flags(0);
set_ruri_q(Q_UNSPECIFIED);
if(msg->dst_uri.s!=0)
pkg_free(msg->dst_uri.s);
msg->dst_uri.s = 0;
msg->dst_uri.len = 0;
if(msg->path_vec.s!=0)
pkg_free(msg->path_vec.s);
msg->path_vec.s = 0;
msg->path_vec.len = 0;
} else {
ret = append_branch(msg, &a->elem[0].u.s, &msg->dst_uri,
&msg->path_vec, a->elem[1].u.number, getb0flags(),
msg->force_send_socket);
}
break;
case REMOVE_BRANCH_T:
script_trace("core", "remove_branch", msg, a->file, a->line) ;
if (a->elem[0].type == SCRIPTVAR_ST) {
spec = (pv_spec_t*)a->elem[0].u.data;
if( pv_get_spec_value(msg, spec, &val)!=0
|| (val.flags&PV_VAL_NULL) || !(val.flags&PV_VAL_INT) ) {
ret=-1;
break;
}
i = val.ri;
} else {
i=a->elem[0].u.number;
}
ret = (remove_branch((unsigned int)i)==0)?1:-1;
break;
case LEN_GT_T:
script_trace("core", "len_gt", msg, a->file, a->line) ;
if (a->elem[0].type!=NUMBER_ST) {
LM_ALERT("BUG in len_gt type %d\n",
a->elem[0].type );
ret=E_BUG;
break;
}
ret = (msg->len >= (unsigned int)a->elem[0].u.number) ? 1 : -1;
break;
case SET_DEBUG_T:
script_trace("core", "set_debug", msg, a->file, a->line) ;
if (a->elem[0].type==NUMBER_ST)
set_proc_debug_level(a->elem[0].u.number);
else
reset_proc_debug_level();
ret = 1;
break;
case SETFLAG_T:
script_trace("core", "setflag", msg, a->file, a->line) ;
ret = setflag( msg, a->elem[0].u.number );
break;
case RESETFLAG_T:
script_trace("core", "resetflag", msg, a->file, a->line) ;
ret = resetflag( msg, a->elem[0].u.number );
break;
case ISFLAGSET_T:
script_trace("core", "isflagset", msg, a->file, a->line) ;
ret = isflagset( msg, a->elem[0].u.number );
break;
case SETSFLAG_T:
script_trace("core", "setsflag", msg, a->file, a->line) ;
ret = setsflag( a->elem[0].u.number );
break;
case RESETSFLAG_T:
script_trace("core", "resetsflag", msg, a->file, a->line) ;
ret = resetsflag( a->elem[0].u.number );
break;
case ISSFLAGSET_T:
script_trace("core", "issflagset", msg, a->file, a->line) ;
ret = issflagset( a->elem[0].u.number );
break;
case SETBFLAG_T:
script_trace("core", "setbflag", msg, a->file, a->line) ;
ret = setbflag( a->elem[0].u.number, a->elem[1].u.number );
break;
case RESETBFLAG_T:
script_trace("core", "resetbflag", msg, a->file, a->line) ;
ret = resetbflag( a->elem[0].u.number, a->elem[1].u.number );
break;
case ISBFLAGSET_T:
script_trace("core", "isbflagset", msg, a->file, a->line) ;
ret = isbflagset( a->elem[0].u.number, a->elem[1].u.number );
break;
case ERROR_T:
script_trace("core", "error", msg, a->file, a->line) ;
if ((a->elem[0].type!=STRING_ST)|(a->elem[1].type!=STRING_ST)){
LM_ALERT("BUG in error() types %d, %d\n",
a->elem[0].type, a->elem[1].type);
ret=E_BUG;
break;
}
LM_ERR("error(\"%s\", \"%s\") not implemented yet\n",
a->elem[0].u.string, a->elem[1].u.string);
ret=1;
break;
case ROUTE_T:
script_trace("route", rlist[a->elem[0].u.number].name, msg, a->file, a->line) ;
if (a->elem[0].type!=NUMBER_ST){
LM_ALERT("BUG in route() type %d\n",
a->elem[0].type);
ret=E_BUG;
break;
}
if ((a->elem[0].u.number>RT_NO)||(a->elem[0].u.number<0)){
LM_ALERT("BUG - invalid routing table number in"
"route(%lu)\n", a->elem[0].u.number);
ret=E_CFG;
break;
}
/* check if the route has parameters */
if (a->elem[1].type != 0) {
if (a->elem[1].type != NUMBER_ST || a->elem[2].type != SCRIPTVAR_ELEM_ST) {
LM_ALERT("BUG in route() type %d/%d\n",
a->elem[1].type, a->elem[2].type);
ret=E_BUG;
break;
}
route_rec_level++;
route_params[route_rec_level] = (action_elem_t *)a->elem[2].u.data;
route_params_number[route_rec_level] = a->elem[1].u.number;
return_code=run_actions(rlist[a->elem[0].u.number].a, msg);
route_rec_level--;
} else {
route_rec_level++;
route_params[route_rec_level] = NULL;
route_params_number[route_rec_level] = 0;
return_code=run_actions(rlist[a->elem[0].u.number].a, msg);
route_rec_level--;
}
ret=return_code;
break;
case REVERT_URI_T:
script_trace("core", "revert_uri", msg, a->file, a->line) ;
if (msg->new_uri.s) {
pkg_free(msg->new_uri.s);
msg->new_uri.len=0;
msg->new_uri.s=0;
msg->parsed_uri_ok=0; /* invalidate current parsed uri*/
};
ret=1;
break;
case SET_HOST_T:
case SET_HOSTPORT_T:
case SET_USER_T:
case SET_USERPASS_T:
case SET_PORT_T:
case SET_URI_T:
case PREFIX_T:
case STRIP_T:
case STRIP_TAIL_T:
script_trace("core",
(unsigned char)a->type == SET_HOST_T ? "set_host" :
(unsigned char)a->type == SET_HOSTPORT_T ? "set_hostport" :
(unsigned char)a->type == SET_USER_T ? "set_user" :
(unsigned char)a->type == SET_USERPASS_T ? "set_userpass" :
(unsigned char)a->type == SET_PORT_T ? "set_port" :
(unsigned char)a->type == SET_URI_T ? "set_uri" :
(unsigned char)a->type == PREFIX_T ? "prefix" :
(unsigned char)a->type == STRIP_T ? "strip" : "strip_tail",
msg, a->file, a->line);
user=0;
if (a->type==STRIP_T || a->type==STRIP_TAIL_T) {
if (a->elem[0].type!=NUMBER_ST) {
LM_ALERT("BUG in set*() type %d\n",
a->elem[0].type);
break;
}
} else if (a->elem[0].type!=STR_ST){
LM_ALERT("BUG in set*() type %d\n",
a->elem[0].type);
ret=E_BUG;
break;
}
if (a->type==SET_URI_T) {
if (set_ruri( msg, &a->elem[0].u.s) ) {
LM_ERR("failed to set new RURI\n");
ret=E_OUT_OF_MEM;
break;
}
ret=1;
break;
}
if (msg->new_uri.s) {
tmp=msg->new_uri.s;
len=msg->new_uri.len;
}else{
tmp=msg->first_line.u.request.uri.s;
len=msg->first_line.u.request.uri.len;
}
if (parse_uri(tmp, len, &uri)<0){
LM_ERR("bad uri <%.*s>, dropping packet\n", len, tmp);
ret=E_UNSPEC;
break;
}
new_uri=pkg_malloc(MAX_URI_SIZE);
if (new_uri==0){
LM_ERR("memory allocation failure\n");
ret=E_OUT_OF_MEM;
break;
}
end=new_uri+MAX_URI_SIZE;
crt=new_uri;
/* begin copying */
len = (uri.user.len?uri.user.s:uri.host.s) - tmp;
if (crt+len>end) goto error_uri;
memcpy(crt,tmp,len);crt+=len;
if (a->type==PREFIX_T) {
if (crt+a->elem[0].u.s.len>end) goto error_uri;
memcpy( crt, a->elem[0].u.s.s, a->elem[0].u.s.len);
crt+=a->elem[0].u.s.len;
/* whatever we had before, with prefix we have username
now */
user=1;
}
if ((a->type==SET_USER_T)||(a->type==SET_USERPASS_T)) {
tmp=a->elem[0].u.s.s;
len=a->elem[0].u.s.len;
} else if (a->type==STRIP_T) {
if (a->elem[0].u.number>uri.user.len) {
LM_WARN("too long strip asked; "
" deleting username: %lu of <%.*s>\n",
a->elem[0].u.number, uri.user.len, uri.user.s);
len=0;
} else if (a->elem[0].u.number==uri.user.len) {
len=0;
} else {
tmp=uri.user.s + a->elem[0].u.number;
len=uri.user.len - a->elem[0].u.number;
}
} else if (a->type==STRIP_TAIL_T) {
if (a->elem[0].u.number>uri.user.len) {
LM_WARN("too long strip_tail asked;"
" deleting username: %lu of <%.*s>\n",
a->elem[0].u.number, uri.user.len, uri.user.s);
len=0;
} else if (a->elem[0].u.number==uri.user.len) {
len=0;
} else {
tmp=uri.user.s;
len=uri.user.len - a->elem[0].u.number;
}
} else {
tmp=uri.user.s;
len=uri.user.len;
}
if (len){
if(crt+len>end) goto error_uri;
memcpy(crt,tmp,len);crt+=len;
user=1; /* we have an user field so mark it */
}
if (a->type==SET_USERPASS_T) tmp=0;
else tmp=uri.passwd.s;
/* passwd */
if (tmp){
len=uri.passwd.len; if(crt+len+1>end) goto error_uri;
*crt=':'; crt++;
memcpy(crt,tmp,len);crt+=len;
}
/* host */
if (user || tmp){ /* add @ */
if(crt+1>end) goto error_uri;
*crt='@'; crt++;
}
if ((a->type==SET_HOST_T) ||(a->type==SET_HOSTPORT_T)) {
tmp=a->elem[0].u.s.s;
len=a->elem[0].u.s.len;
} else {
tmp=uri.host.s;
len = uri.host.len;
}
if (tmp){
if(crt+len>end) goto error_uri;
memcpy(crt,tmp,len);crt+=len;
}
/* port */
if (a->type==SET_HOSTPORT_T) tmp=0;
else if (a->type==SET_PORT_T) {
tmp=a->elem[0].u.s.s;
len=a->elem[0].u.s.len;
} else {
tmp=uri.port.s;
len = uri.port.len;
}
if (tmp && len>0){
if(crt+len+1>end) goto error_uri;
*crt=':'; crt++;
memcpy(crt,tmp,len);crt+=len;
}
/* params */
tmp=uri.params.s;
if (tmp){
/* include in param string the starting ';' */
len=uri.params.len+1;
tmp--;
if(crt+len+1>end) goto error_uri;
/* if a maddr param is present, strip it out */
if (uri.maddr.len &&
(a->type==SET_HOSTPORT_T || a->type==SET_HOST_T)) {
memcpy(crt,tmp,uri.maddr.s-tmp-1);
crt+=uri.maddr.s-tmp-1;
memcpy(crt,uri.maddr_val.s+uri.maddr_val.len,
tmp+len-uri.maddr_val.s-uri.maddr_val.len);
crt+=tmp+len-uri.maddr_val.s-uri.maddr_val.len;
} else {
memcpy(crt,tmp,len);crt+=len;
}
}
/* headers */
tmp=uri.headers.s;
if (tmp){
len=uri.headers.len; if(crt+len+1>end) goto error_uri;
*crt='?'; crt++;
memcpy(crt,tmp,len);crt+=len;
}
*crt=0; /* null terminate the thing */
/* copy it to the msg */
if (msg->new_uri.s) pkg_free(msg->new_uri.s);
msg->new_uri.s=new_uri;
msg->new_uri.len=crt-new_uri;
msg->parsed_uri_ok=0;
ret=1;
break;
case SET_DSTURI_T:
script_trace("core", "set_dsturi", msg, a->file, a->line) ;
if (a->elem[0].type!=STR_ST){
LM_ALERT("BUG in setdsturi() type %d\n",
a->elem[0].type);
ret=E_BUG;
break;
}
if(set_dst_uri(msg, &a->elem[0].u.s)!=0)
ret = -1;
else
ret = 1;
break;
case SET_DSTHOST_T:
case SET_DSTPORT_T:
script_trace("core", (unsigned char) a->type == SET_DSTHOST_T ?
"set_dsturi" : "set_dstport", msg, a->file, a->line);
if (a->elem[0].type!=STR_ST){
LM_ALERT("BUG in domain setting type %d\n",
a->elem[0].type);
ret=E_BUG;
break;
}
tmp = msg->dst_uri.s;
len = msg->dst_uri.len;
if (tmp == NULL || len == 0) {
LM_ERR("failure - null uri\n");
ret = E_UNSPEC;
break;
}
if (a->type == SET_DSTHOST_T &&
(a->elem[0].u.s.s == NULL || a->elem[0].u.s.len == 0)) {
LM_ERR("cannot set a null uri domain\n");
break;
}
if (parse_uri(tmp, len, &uri)<0) {
LM_ERR("bad uri <%.*s>, dropping packet\n", len, tmp);
break;
}
new_uri=pkg_malloc(MAX_URI_SIZE);
if (new_uri == NULL) {
LM_ERR("memory allocation failure\n");
ret=E_OUT_OF_MEM;
break;
}
end=new_uri+MAX_URI_SIZE;
crt=new_uri;
len = (uri.user.len?uri.user.s:uri.host.s) - tmp;
if (crt+len>end) goto error_uri;
memcpy(crt,tmp,len);
crt += len;
/* user */
tmp = uri.user.s;
len = uri.user.len;
if (tmp) {