forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_cmds.c
1263 lines (1093 loc) · 30.8 KB
/
core_cmds.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
/*
* Copyright (C) 2019 OpenSIPS Solutions
*
* 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
*
*/
/*
* Script functions exported by the OpenSIPS core
*/
#include "action.h"
#include "dprint.h"
#include "proxy.h"
#include "forward.h"
#include "parser/msg_parser.h"
#include "parser/parse_uri.h"
#include "ut.h"
#include "mem/mem.h"
#include "globals.h"
#include "dset.h"
#include "flags.h"
#include "serialize.h"
#include "blacklists.h"
#include "status_report.h"
#include "cachedb/cachedb.h"
#include "msg_translator.h"
/* needed by tcpconn_add_alias() */
#include "net/tcp_conn_defs.h"
static int fixup_forward_dest(void** param);
static int fixup_destination(void** param);
static int fixup_free_destination(void** param);
static int fixup_mflag(void** param);
static int fixup_bflag(void** param);
static int fixup_qvalue(void** param);
static int fixup_f_send_sock(void** param);
static int fixup_blacklist(void** param);
static int fixup_check_wrvar(void** param);
static int fixup_avp_list(void** param);
static int fixup_check_avp(void** param);
static int fixup_event_name(void** param);
static int fixup_format_string(void** param);
static int fixup_nt_string(void** param);
static int w_forward(struct sip_msg *msg, struct proxy_l *dest);
static int w_send(struct sip_msg *msg, struct proxy_l *dest, str *headers);
static int w_setflag(struct sip_msg *msg, void *flag);
static int w_resetflag(struct sip_msg *msg, void *flag);
static int w_isflagset(struct sip_msg *msg, void *flag);
static int w_setbflag(struct sip_msg *msg, void *flag, int *branch_idx);
static int w_resetbflag(struct sip_msg *msg, void *flag, int *branch_idx);
static int w_isbflagset(struct sip_msg *msg, void *flag, int *branch_idx);
static int w_sethost(struct sip_msg *msg, str *host);
static int w_sethostport(struct sip_msg *msg, str *hostport);
static int w_setuser(struct sip_msg *msg, str *user);
static int w_setuserpass(struct sip_msg *msg, str *userpass);
static int w_setport(struct sip_msg *msg, str *port);
static int w_seturi(struct sip_msg *msg, str *uri);
static int w_prefix(struct sip_msg *msg, str *prefix);
static int w_strip(struct sip_msg *msg, int *nchars);
static int w_strip_tail(struct sip_msg *msg, int *nchars);
static int w_append_branch(struct sip_msg *msg, str *uri, int *qvalue);
static int w_remove_branch(struct sip_msg *msg, int *branch);
static int w_pv_printf(struct sip_msg *msg, pv_spec_t *var, str *fmt_str);
static int w_revert_uri(struct sip_msg *msg);
static int w_setdsturi(struct sip_msg *msg, str *uri);
static int w_resetdsturi(struct sip_msg *msg);
static int w_isdsturiset(struct sip_msg *msg);
static int w_force_rport(struct sip_msg *msg);
static int w_add_local_rport(struct sip_msg *msg);
static int w_force_tcp_alias(struct sip_msg *msg, int *port);
static int w_set_adv_address(struct sip_msg *msg, str *adv_addr);
static int w_set_adv_port(struct sip_msg *msg, str *adv_port);
static int w_f_send_sock(struct sip_msg *msg, struct socket_info *si);
static int w_serialize_branches(struct sip_msg *msg, int *clear_prev,
int *keep_ord);
static int w_next_branches(struct sip_msg *msg);
static int w_use_blacklist(struct sip_msg *msg, struct bl_head *list);
static int w_unuse_blacklist(struct sip_msg *msg, struct bl_head *list);
static int w_cache_store(struct sip_msg *msg, str *id, str *attr, str *val,
int *expire);
static int w_cache_remove(struct sip_msg *msg, str *id, str *attr);
static int w_cache_fetch(struct sip_msg *msg, str *id, str *attr,
pv_spec_t *res);
static int w_cache_counter_fetch(struct sip_msg *msg, str *id, str *attr,
pv_spec_t *res);
static int w_cache_add(struct sip_msg *msg, str *id, str *attr,
int *inc, int *expire, pv_spec_t *new_val);
static int w_cache_sub(struct sip_msg *msg, str *id, str *attr,
int *dec, int *expire, pv_spec_t *new_val);
static int w_cache_raw_query(struct sip_msg *msg, str *id, str *raw_query,
pvname_list_t *avp_list);
static int w_raise_event(struct sip_msg *msg, void *ev_id, pv_spec_t *attrs_avp,
pv_spec_t *vals_avp);
static int w_subscribe_event(struct sip_msg *msg, str *name, str *socket,
int *expire);
static int w_construct_uri(struct sip_msg *msg, str *proto, str *user,
str *domain, str *port, str *extra, pv_spec_t *result);
static int w_get_timestamp(struct sip_msg *msg, pv_spec_t *sec_avp,
pv_spec_t *usec_avp);
static int w_script_trace(struct sip_msg *msg, int *log_level,
pv_elem_t *fmt_string, void *info_str);
static int w_is_myself(struct sip_msg *msg, str *host, int *port);
static cmd_export_t core_cmds[]={
{"forward", (cmd_function)w_forward, {
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_FIX_NULL,
fixup_forward_dest, fixup_free_destination}, {0,0,0}},
ALL_ROUTES},
{"send", (cmd_function)w_send, {
{CMD_PARAM_STR, fixup_destination, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"setflag", (cmd_function)w_setflag, {
{CMD_PARAM_STR|CMD_PARAM_STATIC, fixup_mflag, 0}, {0,0,0}},
ALL_ROUTES},
{"resetflag", (cmd_function)w_resetflag, {
{CMD_PARAM_STR|CMD_PARAM_STATIC, fixup_mflag, 0}, {0,0,0}},
ALL_ROUTES},
{"isflagset", (cmd_function)w_isflagset, {
{CMD_PARAM_STR|CMD_PARAM_STATIC, fixup_mflag, 0}, {0,0,0}},
ALL_ROUTES},
{"setbflag", (cmd_function)w_setbflag, {
{CMD_PARAM_STR|CMD_PARAM_STATIC, fixup_bflag, 0},
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"resetbflag", (cmd_function)w_resetbflag, {
{CMD_PARAM_STR|CMD_PARAM_STATIC, fixup_bflag, 0},
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"isbflagset", (cmd_function)w_isbflagset, {
{CMD_PARAM_STR|CMD_PARAM_STATIC, fixup_bflag, 0},
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"sethost", (cmd_function)w_sethost, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"sethostport", (cmd_function)w_sethostport, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"setuser", (cmd_function)w_setuser, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"setuserpass", (cmd_function)w_setuserpass, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"setport", (cmd_function)w_setport, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"seturi", (cmd_function)w_seturi, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"prefix", (cmd_function)w_prefix, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"strip", (cmd_function)w_strip, {
{CMD_PARAM_INT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"strip_tail", (cmd_function)w_strip_tail, {
{CMD_PARAM_INT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"append_branch", (cmd_function)w_append_branch, {
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_FIX_NULL,
fixup_qvalue, 0}, {0,0,0}},
ALL_ROUTES},
{"remove_branch", (cmd_function)w_remove_branch, {
{CMD_PARAM_INT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"pv_printf", (cmd_function)w_pv_printf, {
{CMD_PARAM_VAR, 0, 0},
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"revert_uri", (cmd_function)w_revert_uri, {
{0, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"setdsturi", (cmd_function)w_setdsturi, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"resetdsturi", (cmd_function)w_resetdsturi, {
{0, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"isdsturiset", (cmd_function)w_isdsturiset, {
{0, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"force_rport", (cmd_function)w_force_rport, {
{0, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"add_local_rport", (cmd_function)w_add_local_rport, {
{0, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"force_tcp_alias", (cmd_function)w_force_tcp_alias, {
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"set_advertised_address", (cmd_function)w_set_adv_address, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"set_advertised_port", (cmd_function)w_set_adv_port, {
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"force_send_socket", (cmd_function)w_f_send_sock, {
{CMD_PARAM_STR, fixup_f_send_sock, 0}, {0,0,0}},
ALL_ROUTES},
{"serialize_branches", (cmd_function)w_serialize_branches, {
{CMD_PARAM_INT, 0, 0},
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"next_branches", (cmd_function)w_next_branches, {
{0, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"use_blacklist", (cmd_function)w_use_blacklist, {
{CMD_PARAM_STR, fixup_blacklist, 0}, {0,0,0}},
ALL_ROUTES},
{"unuse_blacklist", (cmd_function)w_unuse_blacklist, {
{CMD_PARAM_STR, fixup_blacklist, 0}, {0,0,0}},
ALL_ROUTES},
{"cache_store", (cmd_function)w_cache_store, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"cache_remove", (cmd_function)w_cache_remove, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"cache_fetch", (cmd_function)w_cache_fetch, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_VAR, fixup_check_wrvar, 0}, {0,0,0}},
ALL_ROUTES},
{"cache_counter_fetch", (cmd_function)w_cache_counter_fetch, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_VAR, fixup_check_wrvar, 0}, {0,0,0}},
ALL_ROUTES},
{"cache_add", (cmd_function)w_cache_add, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_INT, 0, 0},
{CMD_PARAM_INT, 0, 0},
{CMD_PARAM_VAR|CMD_PARAM_OPT, fixup_check_wrvar, 0}, {0,0,0}},
ALL_ROUTES},
{"cache_sub", (cmd_function)w_cache_sub, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_INT, 0, 0},
{CMD_PARAM_INT, 0, 0},
{CMD_PARAM_VAR|CMD_PARAM_OPT, fixup_check_wrvar, 0}, {0,0,0}},
ALL_ROUTES},
{"cache_raw_query", (cmd_function)w_cache_raw_query, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_NO_EXPAND, fixup_avp_list, 0}, {0,0,0}},
ALL_ROUTES},
{"raise_event", (cmd_function)w_raise_event, {
{CMD_PARAM_STR, fixup_event_name, 0},
{CMD_PARAM_VAR|CMD_PARAM_OPT, fixup_check_avp, 0},
{CMD_PARAM_VAR|CMD_PARAM_OPT, fixup_check_avp, 0}, {0,0,0}},
ALL_ROUTES},
{"subscribe_event", (cmd_function)w_subscribe_event, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"construct_uri", (cmd_function)w_construct_uri, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_VAR, fixup_check_avp, 0}, {0,0,0}},
ALL_ROUTES},
{"get_timestamp", (cmd_function)w_get_timestamp, {
{CMD_PARAM_VAR, fixup_check_avp, 0},
{CMD_PARAM_VAR, fixup_check_avp, 0}, {0,0,0}},
ALL_ROUTES},
{"script_trace", (cmd_function)w_script_trace, {
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_NO_EXPAND, fixup_format_string, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT|CMD_PARAM_STATIC, fixup_nt_string, 0},
{0,0,0}},
ALL_ROUTES},
{"is_myself", (cmd_function)w_is_myself, {
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{"sr_check_status", (cmd_function)w_sr_check_status, {
{CMD_PARAM_STR, fixup_sr_group, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
ALL_ROUTES},
{0,0,{{0,0,0}},0}
};
cmd_export_t* find_core_cmd_export_t(char* name, int flags)
{
cmd_export_t* cmd;
for(cmd=core_cmds; cmd && cmd->name; cmd++){
if((strcmp(name, cmd->name)==0)&&((cmd->flags & flags) == flags)){
LM_DBG("found <%s> core function\n", name);
return cmd;
}
}
LM_DBG("<%s> not found \n", name);
return 0;
}
static int fixup_destination(void** param)
{
str *s = (str*)*param;
str host;
int proto=PROTO_NONE, port;
if (parse_phostport(s->s, s->len, &host.s, &host.len, &port, &proto) != 0) {
LM_ERR("Failed to parse destination\n");
return E_CFG;
}
*param = mk_proxy(&host,(unsigned short)port, proto, 0);
if (*param==0) {
LM_ERR("Failed to create proxy struct\n");
return E_CFG;
}
return 0;
}
static int fixup_free_destination(void** param)
{
free_proxy(*param);
pkg_free(*param);
return 0;
}
static int fixup_forward_dest(void** param)
{
if (sl_fwd_disabled>0) {
LM_ERR("stateless forwarding disabled, but forward() "
"is used!!\n");
return E_CFG;
}
sl_fwd_disabled = 0;
if (*param == NULL)
return 0;
return fixup_destination(param);
}
static int fixup_mflag(void** param)
{
if ((*param = (void*)(long)fixup_flag(FLAG_TYPE_MSG, (str*)*param)) ==
(void*)(long)NAMED_FLAG_ERROR) {
LM_ERR("Fixup flag failed!\n");
return E_CFG;
}
return 0;
}
static int fixup_bflag(void** param)
{
if ((*param = (void*)(long)fixup_flag(FLAG_TYPE_BRANCH, (str*)*param)) ==
(void*)(long)NAMED_FLAG_ERROR) {
LM_ERR("Fixup flag failed!\n");
return E_CFG;
}
return 0;
}
static int fixup_qvalue(void** param)
{
int rc;
qvalue_t q;
str *s = (str*)*param;
if (s==NULL) {
*param = (void*)(long)Q_UNSPECIFIED;
return 0;
}
if ((rc = str2q(&q, s->s, s->len)) < 0) {
LM_ERR("Bad qvalue (%.*s): %s\n", s->len, s->s, qverr2str(rc));
return E_CFG;
}
*param = (void*)(long)q;
return 0;
}
static int fixup_f_send_sock(void** param)
{
str *s = (str*)*param;
str host, host_nt;
int proto=PROTO_NONE, port;
struct hostent* he;
struct socket_info* si;
struct ip_addr ip;
if (parse_phostport(s->s, s->len, &host.s, &host.len, &port, &proto) != 0) {
LM_ERR("Failed to parse socket\n");
return E_CFG;
}
if (pkg_nt_str_dup(&host_nt, &host) < 0) {
LM_ERR("oom\n");
return E_OUT_OF_MEM;
}
he=resolvehost(host_nt.s,0);
if (he==0){
LM_ERR(" could not resolve %s\n", host_nt.s);
goto error;
}
hostent2ip_addr(&ip, he, 0);
si=find_si(&ip, port, proto);
if (si==0){
LM_ERR("bad force_send_socket"
" argument: %s:%d (opensips doesn't listen on it)\n",
host_nt.s, port);
goto error;
}
pkg_free(host_nt.s);
*param = si;
return 0;
error:
pkg_free(host_nt.s);
return E_BAD_ADDRESS;
}
static int fixup_blacklist(void** param)
{
str *s = (str*)*param;
if (!str_strcasecmp(s, _str("all")))
*param = NULL;
else {
*param = get_bl_head_by_name(s);
if (!*param) {
LM_ERR("[UN]USE_BLACKLIST - list "
"%.*s not configured\n", s->len, s->s);
return E_CFG;
}
}
return 0;
}
static int fixup_check_wrvar(void** param)
{
if (((pv_spec_t *)*param)->setf == NULL) {
LM_ERR("Output parameter must be a writable variable\n");
return E_CFG;
}
return 0;
}
static int fixup_avp_list(void** param)
{
str *s = (str*)*param;
*param = parse_pvname_list(s, PVT_AVP);
if (!*param) {
LM_ERR("Failed to parse AVP list\n");
return E_UNSPEC;
}
return 0;
}
static int fixup_check_avp(void** param)
{
if (((pv_spec_t *)*param)->type != PVT_AVP) {
LM_ERR("Parameter must be an AVP\n");
return E_CFG;
}
return 0;
}
static int fixup_event_name(void** param)
{
str *s = (str*)*param;
event_id_t ev_id;
ev_id = evi_get_id(s);
if (ev_id == EVI_ERROR) {
ev_id = evi_publish_event(*s);
if (ev_id == EVI_ERROR) {
LM_ERR("cannot subscribe event\n");
return E_UNSPEC;
}
}
*param = (void*)(long)ev_id;
return 0;
}
static int fixup_format_string(void** param)
{
str *s = (str*)*param;
if (pv_parse_format(s, (pv_elem_t**)param) < 0) {
LM_ERR("Failed to parse format string\n");
return E_CFG;
}
return 0;
}
static int fixup_nt_string(void** param)
{
str *s = (str*)*param;
str s_nt;
if (pkg_nt_str_dup(&s_nt, s) < 0) {
LM_ERR("oom\n");
return E_OUT_OF_MEM;
}
*param = s_nt.s;
return 0;
}
static int w_forward(struct sip_msg *msg, struct proxy_l *dest)
{
struct sip_uri next_hop, *u;
struct proxy_l* p;
int ret;
if (!dest) {
/* 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");
return E_BAD_ADDRESS;
}
/* 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");
return E_BAD_ADDRESS;
}
} else {
if (0==(p=clone_proxy(dest))) {
LM_ERR("failed to clone proxy, dropping packet\n");
return E_OUT_OF_MEM;
}
}
ret=forward_request(msg, p);
free_proxy(p); /* frees only p content, not p itself */
pkg_free(p);
if (ret==0)
ret=1;
return ret;
}
static int w_send(struct sip_msg *msg, struct proxy_l *dest, str *headers)
{
int ret;
union sockaddr_union* to;
struct proxy_l* p;
int len;
char* tmp;
to=(union sockaddr_union*)
pkg_malloc(sizeof(union sockaddr_union));
if (to==0){
LM_ERR("memory allocation failure\n");
return E_OUT_OF_MEM;
}
if (0==(p=clone_proxy(dest))) {
LM_ERR("failed to clone proxy, dropping packet\n");
return E_OUT_OF_MEM;
}
ret=hostent2su(to, &p->host, p->addr_idx,
(p->port)?p->port:SIP_PORT );
if (ret==0){
if (headers) {
/* build new msg */
tmp = pkg_malloc(msg->len + headers->len);
if (!tmp) {
LM_ERR("memory allocation failure\n");
return E_OUT_OF_MEM;
}
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, headers->s, headers->len);
memcpy(tmp + len + headers->len,
msg->buf + len, msg->len - len);
ret = msg_send(0/*send_sock*/, p->proto, to, 0/*id*/,
tmp, msg->len + headers->len, msg);
pkg_free(tmp);
} else {
ret = msg_send(0/*send_sock*/, p->proto, to, 0/*id*/,
msg->buf, msg->len, msg);
}
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;
return ret;
}
static int w_setflag(struct sip_msg *msg, void *flag)
{
return setflag(msg, (flag_t)(long)flag);
}
static int w_resetflag(struct sip_msg *msg, void *flag)
{
return resetflag(msg, (flag_t)(long)flag);
}
static int w_isflagset(struct sip_msg *msg, void *flag)
{
return isflagset(msg, (flag_t)(long)flag);
}
static int w_setbflag(struct sip_msg *msg, void *flag, int *branch_idx)
{
return setbflag(msg, branch_idx ? *branch_idx : 0, (flag_t)(long)flag);
}
static int w_resetbflag(struct sip_msg *msg, void *flag, int *branch_idx)
{
return resetbflag(msg, branch_idx ? *branch_idx : 0, (flag_t)(long)flag);
}
static int w_isbflagset(struct sip_msg *msg, void *flag, int *branch_idx)
{
return isbflagset(msg, branch_idx ? *branch_idx : 0, (flag_t)(long)flag);
}
static int w_sethost(struct sip_msg *msg, str *host)
{
return rewrite_ruri(msg, host, 0, RW_RURI_HOST) ? -1 : 1;
}
static int w_sethostport(struct sip_msg *msg, str *hostport)
{
return rewrite_ruri(msg, hostport, 0, RW_RURI_HOSTPORT) ? -1 : 1;
}
static int w_setuser(struct sip_msg *msg, str *user)
{
return rewrite_ruri(msg, user, 0, RW_RURI_USER) ? -1 : 1;
}
static int w_setuserpass(struct sip_msg *msg, str *userpass)
{
return rewrite_ruri(msg, userpass, 0, RW_RURI_USERPASS) ? -1 : 1;
}
static int w_setport(struct sip_msg *msg, str *port)
{
return rewrite_ruri(msg, port, 0, RW_RURI_PORT) ? -1 : 1;
}
static int w_seturi(struct sip_msg *msg, str *uri)
{
if (set_ruri(msg, uri) ) {
LM_ERR("failed to set new RURI\n");
return E_OUT_OF_MEM;
}
return 1;
}
static int w_prefix(struct sip_msg *msg, str *prefix)
{
return rewrite_ruri(msg, prefix, 0, RW_RURI_PREFIX) ? -1 : 1;
}
static int w_strip(struct sip_msg *msg, int *nchars)
{
return rewrite_ruri(msg, 0, *nchars, RW_RURI_STRIP) ? -1 : 1;
}
static int w_strip_tail(struct sip_msg *msg, int *nchars)
{
return rewrite_ruri(msg, 0, *nchars, RW_RURI_STRIP_TAIL) ? -1 : 1;
}
static int w_append_branch(struct sip_msg *msg, str *uri, int *qvalue)
{
int ret;
qvalue_t q = (int)(long)qvalue;
if (!uri) {
ret = append_branch(msg, 0, &msg->dst_uri, &msg->path_vec,
(q==Q_UNSPECIFIED) ? get_ruri_q(msg) : q,
getb0flags(msg), msg->force_send_socket);
/* reset all branch info */
msg->force_send_socket = 0;
setb0flags(msg,0);
set_ruri_q(msg,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;
return ret;
} else {
return append_branch(msg, uri, &msg->dst_uri,
&msg->path_vec, q, getb0flags(msg),
msg->force_send_socket);
}
}
static int w_remove_branch(struct sip_msg *msg, int *branch)
{
return (remove_branch(*branch)==0)?1:-1;
}
static int w_pv_printf(struct sip_msg *msg, pv_spec_t *var, str *fmt_str)
{
pv_value_t val;
if(!pv_is_w(var))
{
LM_ERR("read only PV in first parameter of pv_printf\n");
return -1;
}
val.flags = PV_VAL_STR;
val.rs = *fmt_str;
if(pv_set_value(msg, var, EQ_T, &val)<0)
{
LM_ERR("setting PV failed\n");
return -1;
}
return 1;
}
static int w_revert_uri(struct sip_msg *msg)
{
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*/
};
return 1;
}
static int w_setdsturi(struct sip_msg *msg, str *uri)
{
if(set_dst_uri(msg, uri)!=0)
return -1;
else
return 1;
}
static int w_resetdsturi(struct sip_msg *msg)
{
reset_dst_uri(msg);
return 1;
}
static int w_isdsturiset(struct sip_msg *msg)
{
if(msg->dst_uri.s==0 || msg->dst_uri.len<=0)
return -1;
else
return 1;
return 1;
}
static int w_force_rport(struct sip_msg *msg)
{
msg->msg_flags|=FL_FORCE_RPORT;
return 1;
}
static int w_add_local_rport(struct sip_msg *msg)
{
msg->msg_flags|=FL_FORCE_LOCAL_RPORT;
return 1;
}
static int w_force_tcp_alias(struct sip_msg *msg, int *port)
{
unsigned short p;
if (is_tcp_based_proto(msg->rcv.proto)) {
if (!port) p=msg->via1->port;
else
p=*port;
if (tcpconn_add_alias(msg->rcv.proto_reserved1, p,
msg->rcv.proto)!=0){
LM_WARN("tcp alias failed\n");
return E_UNSPEC;
}
}
return 1;
}
static int w_set_adv_address(struct sip_msg *msg, str *adv_addr)
{
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");
return E_OUT_OF_MEM;
}
}
memcpy(msg->set_global_address.s, adv_addr->s, adv_addr->len);
msg->set_global_address.len = adv_addr->len;
return 1;
}
static int w_set_adv_port(struct sip_msg *msg, str *adv_port)
{
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");
return E_OUT_OF_MEM;
}
}
memcpy(msg->set_global_port.s, adv_port->s, adv_port->len);
msg->set_global_port.len = adv_port->len;
return 1;
}
static int w_f_send_sock(struct sip_msg *msg, struct socket_info *si)
{
msg->force_send_socket=si;
return 1;
}
static int w_serialize_branches(struct sip_msg *msg, int *clear_prev,
int *keep_ord)
{
if (serialize_branches(msg,*clear_prev,
keep_ord ? *keep_ord : 0)!=0) {
LM_ERR("serialize_branches failed\n");
return E_UNSPEC;
}
return 1;
}
static int w_next_branches(struct sip_msg *msg)
{
int ret;
if ((ret = next_branches(msg)) < 0)
LM_DBG("no more branches\n");
return ret;
}
static int w_use_blacklist(struct sip_msg *msg, struct bl_head *list)
{
mark_for_search(list, 1);
return 1;
}
static int w_unuse_blacklist(struct sip_msg *msg, struct bl_head *list)
{
mark_for_search(list, 0);
return 1;
}
static int w_cache_store(struct sip_msg *msg, str *id, str *attr, str *val,
int *expire)
{
if (!attr->s || !attr->len) {
LM_ERR("attribute cannot be empty\n");
return E_UNSPEC;
}
if (!attr->s || !attr->len) {
LM_ERR("value cannot be empty\n");
return E_UNSPEC;
}
return cachedb_store(id, attr, val, expire ? *expire : 0);
}
static int w_cache_remove(struct sip_msg *msg, str *id, str *attr)
{
if (!attr->s || !attr->len) {
LM_ERR("attribute cannot be empty\n");
return E_UNSPEC;
}
return cachedb_remove(id, attr);
}
static int w_cache_fetch(struct sip_msg *msg, str *id, str *attr,
pv_spec_t *res)
{
str aux = {0, 0};
int ret;
pv_value_t val;
if (!attr->s || !attr->len) {
LM_ERR("attribute cannot be empty\n");
return E_UNSPEC;
}
ret = cachedb_fetch(id, attr, &aux);
if(ret > 0)
{
val.rs = aux;
val.flags = PV_VAL_STR;
fix_val_str_flags(val);
if (pv_set_value(msg, res, 0, &val) < 0) {
LM_ERR("cannot set the variable value\n");
pkg_free(aux.s);
return -1;
}
pkg_free(aux.s);
}
return ret;
}
static int w_cache_counter_fetch(struct sip_msg *msg, str *id, str *attr,
pv_spec_t *res)
{
int aux_counter;
int ret;
pv_value_t val;
if (!attr->s || !attr->len) {
LM_ERR("attribute cannot be empty\n");
return E_UNSPEC;