-
Notifications
You must be signed in to change notification settings - Fork 564
/
topo_hiding_logic.c
1992 lines (1723 loc) · 51.3 KB
/
topo_hiding_logic.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
/**
* Topology Hiding Module
*
* Copyright (C) 2015 OpenSIPS Foundation
*
* 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
* -------
* 2015-02-17 initial version (Vlad Paiu)
*/
#include "topo_hiding_logic.h"
extern int force_dialog;
extern struct tm_binds tm_api;
extern struct rr_binds rr_api;
extern struct dlg_binds dlg_api;
extern str topo_hiding_prefix;
extern str topo_hiding_seed;
extern str topo_hiding_ct_encode_pw;
extern str th_contact_encode_param;
struct th_ct_params {
str param_name;
struct th_ct_params *next;
};
static struct th_ct_params *th_param_list=NULL;
static struct th_ct_params *th_hdr_param_list=NULL;
static int topo_hiding_with_dlg(struct sip_msg *req,struct cell* t,struct dlg_cell* dlg,int extra_flags);
static int topo_hiding_no_dlg(struct sip_msg *req,struct cell* t,int extra_flags);
static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg);
static int topo_delete_vias(struct sip_msg* req);
static int topo_delete_record_routes(struct sip_msg *req);
static struct lump* delete_existing_contact(struct sip_msg *msg);
static int topo_parse_passed_params(str *params,struct th_ct_params **lst);
static void topo_dlg_onroute (struct dlg_cell* dlg, int type,
struct dlg_cb_params * params);
static void topo_dlg_initial_reply (struct dlg_cell* dlg, int type,
struct dlg_cb_params * params);
static void th_down_onreply(struct cell* t, int type,struct tmcb_params *param);
static void th_up_onreply(struct cell* t, int type, struct tmcb_params *param);
static void th_no_dlg_onreply(struct cell* t, int type, struct tmcb_params *param);
static void th_no_dlg_user_onreply(struct cell* t, int type, struct tmcb_params *param);
static int topo_no_dlg_encode_contact(struct sip_msg *req,int flags);
static int topo_no_dlg_seq_handling(struct sip_msg *msg,str *info);
static int dlg_th_onreply(struct dlg_cell *dlg, struct sip_msg *rpl, struct sip_msg *req,
int init_req, int dir);
/* exposed logic below */
int topology_hiding(struct sip_msg *req,int extra_flags)
{
struct dlg_cell *dlg;
struct cell* t;
str tag;
/* we should only initialize topology hiding for initial requests */
if (!req->to && parse_headers(req, HDR_TO_F,0)==-1) {
LM_ERR("To parsing failed\n");
return -1;
}
if (!req->to) {
LM_ERR("no To\n");
return -1;
}
tag=get_to(req)->tag_value;
if (tag.len>0) {
LM_WARN("SCRIPT ERROR - trying to initialize topology hiding for sequential request \n");
return -1;
}
t = tm_api.t_gett();
if (t == T_UNDEFINED)
t=NULL;
if (dlg_api.get_dlg) {
/* we have dialog module loaded */
dlg = dlg_api.get_dlg();
if (!dlg) {
if (force_dialog) {
if(dlg_api.create_dlg(req, 0) < 0) {
LM_ERR("Failed to create dialog\n");
return -1;
}
/* double check if the dialog can be retrieved */
if (!(dlg = dlg_api.get_dlg())) {
LM_ERR("failed to get dialog\n");
return -1;
}
}
}
if (!dlg)
return topo_hiding_no_dlg(req,t,extra_flags);
else
return topo_hiding_with_dlg(req,t,dlg,extra_flags);
}
return topo_hiding_no_dlg(req,t,extra_flags);
}
int topo_parse_passed_ct_params(str *params)
{
return topo_parse_passed_params(params,&th_param_list);
}
int topo_parse_passed_hdr_ct_params(str *params)
{
return topo_parse_passed_params(params,&th_hdr_param_list);
}
int topology_hiding_match(struct sip_msg *msg)
{
struct sip_uri *r_uri;
int i;
if (parse_sip_msg_uri(msg)<0) {
LM_ERR("Failed to parse request URI\n");
return -1;
}
if (parse_headers(msg, HDR_ROUTE_F, 0) == -1) {
LM_ERR("failed to parse route headers\n");
}
r_uri = &msg->parsed_uri;
if (check_self(&r_uri->host,r_uri->port_no ? r_uri->port_no : SIP_PORT, 0) == 1 && msg->route == NULL) {
/* Seems we are in the topo hiding case :
* we are in the R-URI and there are no other route headers */
for (i=0;i<r_uri->u_params_no;i++)
if (r_uri->u_name[i].len == th_contact_encode_param.len &&
memcmp(th_contact_encode_param.s,r_uri->u_name[i].s,th_contact_encode_param.len)==0) {
LM_DBG("We found param in R-URI with value of %.*s\n",
r_uri->u_val[i].len,r_uri->u_val[i].s);
/* pass the param value to the matching funcs */
return topo_no_dlg_seq_handling(msg,&r_uri->u_val[i]);
}
}
return -1;
}
/* internal functionality */
#define init_new_ct_node(start,len,list) \
do { \
el = pkg_malloc(sizeof(struct th_ct_params));\
if (!el) { \
LM_ERR("No more pkg mem\n"); \
return -1; \
} \
el->param_name.len = len; \
el->param_name.s = start; \
el->next = *list; \
*list = el; \
} while (0)
static int topo_parse_passed_params(str *params,struct th_ct_params **lst)
{
char *p,*s,*end;
struct th_ct_params* el;
int len;
p = params->s;
end = p+params->len;
while (1) {
s = memchr(p,';',end-p);
if (!s) {
len = end-p;
if (len > 0)
init_new_ct_node(p,len,lst);
break;
}
len = s-p;
if (len > 0)
init_new_ct_node(p,len,lst);
p=s+1;
}
return 0;
}
static int topo_delete_record_routes(struct sip_msg *req)
{
struct lump* lump, *crt, *prev_crt =0, *a, *foo;
struct hdr_field *it;
char* buf;
/* FIXME - we will be losing uac_replace_from/to in case of no dialog */
/* delete also the added record route and the did param */
for(crt=req->add_rm; crt;) {
if ((crt->type==HDR_RECORDROUTE_T) && (crt->op==LUMP_NOP) ) {
/* lump found */
lump = crt;
crt = crt->next;
a=lump->before;
while(a) {
foo=a; a=a->before;
if (!(foo->flags&LUMPFLAG_SHMEM))
free_lump(foo);
if (!(foo->flags&LUMPFLAG_SHMEM))
pkg_free(foo);
}
a=lump->after;
while(a) {
foo=a; a=a->after;
if (!(foo->flags&LUMPFLAG_SHMEM))
free_lump(foo);
if (!(foo->flags&LUMPFLAG_SHMEM))
pkg_free(foo);
}
if(lump == req->add_rm)
req->add_rm = lump->next;
else
prev_crt->next = lump->next;
if (!(lump->flags&LUMPFLAG_SHMEM))
free_lump(lump);
if (!(lump->flags&LUMPFLAG_SHMEM))
pkg_free(lump);
continue;
}
prev_crt = crt;
crt= crt->next;
}
buf = req->buf;
/* delete record-route headers */
for (it=req->record_route;it;it=it->sibling) {
if (del_lump(req,it->name.s - buf,it->len, 0) == 0) {
LM_ERR("del_lump failed - while deleting record-route\n");
return -1;
}
}
return 0;
}
static int topo_delete_vias(struct sip_msg* req)
{
struct hdr_field *it;
char *buf;
/* parse all headers to be sure that all VIAs are found */
if (parse_headers(req, HDR_EOH_F, 0)< 0) {
LM_ERR("Failed to parse reply\n");
return -1;
}
buf = req->buf;
it = req->h_via1;
if(it) {
/* delete first via1 to set the type (the build_req_buf_from_sip_req will know not to add lump in via1)*/
if (del_lump(req,it->name.s - buf,it->len, 0) == 0) {
LM_ERR("del_lump failed\n");
return -1;
}
LM_DBG("Delete via [%.*s]\n", it->len, it->name.s);
for (it=it->sibling; it; it=it->sibling) {
if (del_lump(req,it->name.s - buf,it->len, 0) == 0) {
LM_ERR("del_lump failed\n");
return -1;
}
LM_DBG("Delete via [%.*s]\n", it->len, it->name.s);
}
}
return 0;
}
static struct lump* delete_existing_contact(struct sip_msg *msg)
{
int offset;
int len;
struct lump* lump, *crt, *prev_crt =0, *a, *foo;
offset = msg->contact->body.s - msg->buf;
len = msg->contact->body.len;
for (crt = msg->add_rm;crt;) {
if (crt->type == HDR_CONTACT_T && crt->op == LUMP_DEL &&
crt->u.offset >= offset && crt->u.offset <= offset + len) {
lump = crt;
crt = crt->next;
a=lump->before;
while(a) {
foo=a; a=a->before;
if (!(foo->flags&LUMPFLAG_SHMEM))
free_lump(foo);
if (!(foo->flags&LUMPFLAG_SHMEM))
pkg_free(foo);
}
a=lump->after;
while(a) {
foo=a; a=a->after;
if (!(foo->flags&LUMPFLAG_SHMEM))
free_lump(foo);
if (!(foo->flags&LUMPFLAG_SHMEM))
pkg_free(foo);
}
if(lump == msg->add_rm)
msg->add_rm = lump->next;
else
prev_crt->next = lump->next;
if (!(lump->flags&LUMPFLAG_SHMEM))
free_lump(lump);
if (!(lump->flags&LUMPFLAG_SHMEM))
pkg_free(lump);
continue;
}
prev_crt = crt;
crt= crt->next;
}
if ((lump = del_lump(msg, msg->contact->body.s - msg->buf, msg->contact->body.len,HDR_CONTACT_T)) == 0) {
LM_ERR("del_lump failed\n");
return NULL;
}
return lump;
}
static int topo_dlg_replace_contact(struct sip_msg* msg, struct dlg_cell* dlg)
{
char *prefix=NULL,*suffix=NULL,*p,*p_init,*ct_username=NULL;
int prefix_len,suffix_len,ct_username_len=0,n,i;
struct sip_uri ctu;
str contact;
struct th_ct_params* el;
param_t *it;
str *rr_param;
struct lump* lump;
if(!msg->contact)
{
if(parse_headers(msg, HDR_CONTACT_F, 0)< 0)
{
LM_ERR("Failed to parse headers\n");
return -1;
}
if(!msg->contact)
return 0;
}
prefix_len = 5; /* <sip: */
if (dlg_api.is_mod_flag_set(dlg,TOPOH_KEEP_USER)) {
if ( parse_contact(msg->contact)<0 ||
((contact_body_t *)msg->contact->parsed)->contacts==NULL ||
((contact_body_t *)msg->contact->parsed)->contacts->next!=NULL ) {
LM_ERR("bad Contact HDR\n");
} else {
contact = ((contact_body_t *)msg->contact->parsed)->contacts->uri;
if(parse_uri(contact.s, contact.len, &ctu) < 0) {
LM_ERR("Bad Contact URI\n");
} else {
ct_username = ctu.user.s;
ct_username_len = ctu.user.len;
LM_DBG("Trying to propagate username [%.*s]\n",ct_username_len,
ct_username);
if (ct_username_len > 0)
prefix_len += 1 + /* @ */ + ct_username_len;
}
}
}
prefix = pkg_malloc(prefix_len);
if (!prefix) {
LM_ERR("no more pkg\n");
goto error;
}
suffix_len = RR_DLG_PARAM_SIZE+1; /* > */
if (th_param_list) {
if ( parse_contact(msg->contact)<0 ||
((contact_body_t *)msg->contact->parsed)->contacts==NULL ||
((contact_body_t *)msg->contact->parsed)->contacts->next!=NULL ) {
LM_ERR("bad Contact HDR\n");
} else {
contact = ((contact_body_t *)msg->contact->parsed)->contacts->uri;
if(parse_uri(contact.s, contact.len, &ctu) < 0) {
LM_ERR("Bad Contact URI\n");
} else {
for (el=th_param_list;el;el=el->next) {
/* we just iterate over the unknown params */
for (i=0;i<ctu.u_params_no;i++) {
if (el->param_name.len == ctu.u_name[i].len &&
(memcmp(el->param_name.s,ctu.u_name[i].s,
el->param_name.len) == 0)) {
if (ctu.u_val[i].len)
suffix_len += 1 /* ; */ + ctu.u_name[i].len +
ctu.u_val[i].len + 1; /* = and value */
else
suffix_len += 1 /* ; */ + ctu.u_name[i].len;
}
}
}
}
}
}
if (th_hdr_param_list) {
if ( parse_contact(msg->contact)<0 ||
((contact_body_t *)msg->contact->parsed)->contacts==NULL ||
((contact_body_t *)msg->contact->parsed)->contacts->next!=NULL ) {
LM_ERR("bad Contact HDR\n");
} else {
for (el=th_hdr_param_list;el;el=el->next) {
for (it=((contact_body_t *)msg->contact->parsed)->contacts->params;it;it=it->next) {
if (it->name.len == el->param_name.len &&
(memcmp(it->name.s,el->param_name.s,it->name.len) == 0)) {
if (it->body.len)
suffix_len += 1 /* ; */ + it->name.len +
it->body.len + 1; /* = and value */
else
suffix_len += 1 /* ; */ + it->name.len;
}
}
}
}
}
suffix = pkg_malloc(suffix_len);
if (!suffix) {
LM_ERR("no more pkg\n");
goto error;
}
memcpy(prefix,"<sip:",prefix_len);
if (dlg_api.is_mod_flag_set(dlg,TOPOH_KEEP_USER) && ct_username_len > 0) {
memcpy(prefix+5,ct_username,ct_username_len);
prefix[prefix_len-1] = '@';
}
rr_param = dlg_api.get_rr_param();
p_init = p = suffix;
*p++ = ';';
memcpy(p,rr_param->s,rr_param->len);
p+=rr_param->len;
*p++ = '=';
n = RR_DLG_PARAM_SIZE - (p-p_init);
if (int2reverse_hex( &p, &n, dlg->h_entry)==-1)
return -1;
*(p++) = DLG_SEPARATOR;
n = RR_DLG_PARAM_SIZE - (p-p_init);
if (int2reverse_hex( &p, &n, dlg->h_id)==-1)
return -1;
if (th_param_list) {
for (el=th_param_list;el;el=el->next) {
/* we just iterate over the unknown params */
for (i=0;i<ctu.u_params_no;i++) {
if (el->param_name.len == ctu.u_name[i].len &&
memcmp(el->param_name.s,ctu.u_name[i].s,
el->param_name.len) == 0) {
*p++ = ';';
memcpy(p,ctu.u_name[i].s,ctu.u_name[i].len);
p+=ctu.u_name[i].len;
if (ctu.u_val[i].len) {
*p++ = '=';
memcpy(p,ctu.u_val[i].s,ctu.u_val[i].len);
p+=ctu.u_val[i].len;
}
}
}
}
}
*p++ = '>';
if (th_hdr_param_list) {
if ( parse_contact(msg->contact)<0 ||
((contact_body_t *)msg->contact->parsed)->contacts==NULL ||
((contact_body_t *)msg->contact->parsed)->contacts->next!=NULL ) {
LM_ERR("bad Contact HDR\n");
} else {
for (el=th_hdr_param_list;el;el=el->next) {
for (it=((contact_body_t *)msg->contact->parsed)->contacts->params;it;it=it->next) {
if (it->name.len == el->param_name.len &&
(memcmp(it->name.s,el->param_name.s,it->name.len) == 0)) {
*p++ = ';';
memcpy(p,it->name.s,it->name.len);
p += it->name.len;
if (it->body.len) {
*p++ = '=';
memcpy(p,it->body.s,it->body.len);
p += it->body.len;
}
}
}
}
}
}
suffix_len = p - p_init;
if (!(lump = delete_existing_contact(msg))){
LM_ERR("Failed removing existing contact \n");
goto error;
}
if ((lump = insert_new_lump_after(lump,prefix,prefix_len,HDR_CONTACT_T)) == 0) {
LM_ERR("failed inserting '<sip:'\n");
goto error;
}
/* make sure we do not free this string in case of a further error */
prefix = NULL;
if ((lump = insert_subst_lump_after(lump, SUBST_SND_ALL, HDR_CONTACT_T)) == 0) {
LM_ERR("failed inserting SUBST_SND buf\n");
goto error;
}
if ((lump = insert_new_lump_after(lump,suffix,suffix_len,HDR_CONTACT_T)) == 0) {
LM_ERR("failed inserting '<sip:'\n");
goto error;
}
return 0;
error:
if (prefix) pkg_free(prefix);
if (suffix) pkg_free(suffix);
return -1;
}
struct lump* restore_vias_from_req(struct sip_msg *req,struct sip_msg *rpl)
{
struct lump* lmp;
struct hdr_field *it;
str via_str;
char *p,*buf = rpl->buf;
char *received_buf=0,*rport_buf=0;
unsigned int rport_len=0,received_len=0;
int size;
lmp = anchor_lump(rpl,rpl->headers->name.s - buf,0);
if (lmp == 0)
{
LM_ERR("failed anchoring new lump\n");
return NULL;
}
if ((req->msg_flags&FL_FORCE_RPORT)||(req->via1->rport)) {
if ((received_buf=received_builder(req,&received_len))==0){
LM_ERR("received_builder failed\n");
return NULL;
}
if ((rport_buf=rport_builder(req, &rport_len))==0){
LM_ERR("rport_builder failed\n");
return NULL;
}
/* take care of via1 + rest of VIA headers in h_via1 */
via_str.len = rport_len + received_len + req->h_via1->len;
LM_DBG("via len = %d\n",via_str.len);
if (req->via1->received) {
via_str.len -= req->via1->received->size+1;
LM_DBG(" have received will remove %d \n",req->via1->received->size+1);
}
if (req->via1->rport) {
via_str.len -= req->via1->rport->size+1;
LM_DBG(" have rport will remove %d \n",req->via1->rport->size+1);
}
/* copy rest of VIA headers */
it = req->h_via1->sibling;
while (it) {
via_str.len += it->len;
it = it->sibling;
}
via_str.s = pkg_malloc(via_str.len);
if (!via_str.s) {
LM_ERR("No more pkg mem\n");
goto err_free_rport;
}
/* take care of via1 + rest of VIA headers in h_via1 */
if (req->via1->params.s){
size= req->via1->params.s-req->via1->hdr.s-1; /*compensate for ';' */
}else{
size= req->via1->host.s-req->via1->hdr.s+req->via1->host.len;
if (req->via1->port!=0){
size += req->via1->port_str.len + 1; /* +1 for ':'*/
}
}
p = via_str.s;
memcpy(p,req->via1->hdr.s,size);
p += size;
memcpy(p,received_buf,received_len);
p += received_len;
memcpy(p,rport_buf,rport_len);
p += rport_len;
int bytes_before = 0;
int bytes_after = 0;
int bytes_between = 0;
char *between = NULL;
char *after = NULL;
if (req->via1->received) {
if (!req->via1->rport) {
bytes_before = req->via1->received->start-req->via1->hdr.s-size-1;
memcpy(p,
req->via1->hdr.s+size,
bytes_before);
p += bytes_before;
bytes_after = req->h_via1->len - size - req->via1->received->size -
bytes_before - 1;
memcpy(p,
req->via1->received->start+req->via1->received->size,
bytes_after);
p += bytes_after;
} else {
/* we have both :( */
if (req->via1->rport->start > req->via1->received->start) {
bytes_before = req->via1->received->start-req->via1->hdr.s-size-1;
bytes_between = req->via1->rport->start - req->via1->received->start - req->via1->received->size - 1;
between = req->via1->received->start + req->via1->received->size;
after = req->via1->rport->start+req->via1->rport->size;
bytes_after = req->h_via1->len - size - req->via1->rport->size -
bytes_before - 1 - bytes_between - req->via1->received->size - 1;
LM_DBG("1 both , before = %d, between = %d, after = %d\n",bytes_before,bytes_between,bytes_after);
} else {
bytes_before = req->via1->rport->start-req->via1->hdr.s-size-1;
bytes_between = req->via1->received->start - req->via1->rport->start - req->via1->rport->size - 1;
between = req->via1->rport->start + req->via1->rport->size;
after = req->via1->received->start+req->via1->received->size;
bytes_after = req->h_via1->len - size - req->via1->rport->size -
bytes_before - 1 - bytes_between - req->via1->received->size -1 ;
LM_DBG("2 both , before = %d, between = %d, after = %d\n",bytes_before,bytes_between,bytes_after);
}
memcpy(p,
req->via1->hdr.s+size,
bytes_before);
p += bytes_before;
memcpy(p,
between,
bytes_between);
p += bytes_between;
memcpy(p,
after,
bytes_after);
p += bytes_after;
}
} else if (req->via1->rport) {
if (!req->via1->received) {
bytes_before = req->via1->rport->start-req->via1->hdr.s-size-1;
memcpy(p,
req->via1->hdr.s+size,
bytes_before);
p += bytes_before;
bytes_after = req->h_via1->len - size - req->via1->rport->size -
bytes_before - 1;
memcpy(p,
req->via1->rport->start+req->via1->rport->size,
bytes_after);
p += bytes_after;
}
} else {
/* no rport or received already present */
memcpy(p,req->via1->hdr.s+size,req->h_via1->len-size);
p+= req->h_via1->len-size;
}
/* copy rest of VIA headers */
it = req->h_via1->sibling;
while (it) {
memcpy(p,it->name.s,it->len);
p+=it->len;
it = it->sibling;
}
LM_DBG("built [%.*s], %d %d\n",(int)(p-via_str.s),via_str.s,(int)(p-via_str.s),via_str.len);
if ((lmp = insert_new_lump_after(lmp, via_str.s, via_str.len, 0)) == 0) {
LM_ERR("failed inserting new old vias\n");
pkg_free(via_str.s);
goto err_free_rport;
}
pkg_free(rport_buf);
pkg_free(received_buf);
} else {
/* no need to add received/rport , just copy the headers altogether */
it = req->h_via1;
via_str.len = 0;
while (it) {
via_str.len += it->len;
it = it->sibling;
}
LM_DBG("via len = %d\n",via_str.len);
if (via_str.len == 0)
return lmp;
via_str.s = pkg_malloc(via_str.len);
if (!via_str.s) {
LM_ERR("no more pkg mem\n");
return NULL;
}
LM_DBG("allocated via_str %p\n",via_str.s);
it = req->h_via1;
p = via_str.s;
while (it) {
memcpy(p,it->name.s,it->len);
p+=it->len;
it = it->sibling;
}
LM_DBG("inserting via headers - [%.*s]\n",via_str.len,via_str.s);
if ((lmp = insert_new_lump_after(lmp, via_str.s, via_str.len, 0)) == 0) {
LM_ERR("failed inserting new old vias\n");
pkg_free(via_str.s);
return NULL;
}
}
return lmp;
err_free_rport:
pkg_free(rport_buf);
pkg_free(received_buf);
return NULL;
}
#define RECORD_ROUTE "Record-Route: "
#define RECORD_ROUTE_LEN (sizeof(RECORD_ROUTE)-1)
static void _th_no_dlg_onreply(struct cell* t, int type, struct tmcb_params *param,int flags)
{
struct lump* lmp;
str rr_set;
struct sip_msg *req = param->req;
struct sip_msg *rpl = param->rpl;
char *route;
int size;
/* parse all headers to be sure that all RR and Contact hdrs are found */
if (parse_headers(rpl, HDR_EOH_F, 0)< 0) {
LM_ERR("Failed to parse reply\n");
return;
}
if (topo_delete_record_routes(rpl) < 0) {
LM_ERR("Failed to remove Record Route header \n");
return;
}
if(topo_delete_vias(rpl) < 0) {
LM_ERR("Failed to remove via headers\n");
return;
}
if (topo_no_dlg_encode_contact(rpl,flags) < 0) {
LM_ERR("Failed to encode contact header \n");
return;
}
if (!(lmp = restore_vias_from_req(req,rpl))) {
LM_ERR("Failed to restore VIA headers from request \n");
return ;
}
/* pass record route headers */
if(req->record_route){
if(print_rr_body(req->record_route, &rr_set, 0,
0) != 0 ){
LM_ERR("failed to print route records \n");
return;
}
size = rr_set.len + RECORD_ROUTE_LEN + CRLF_LEN;
route = pkg_malloc(size);
if (route == NULL) {
LM_ERR("no more pkg memory\n");
pkg_free(rr_set.s);
return;
}
memcpy(route, RECORD_ROUTE, RECORD_ROUTE_LEN);
memcpy(route+RECORD_ROUTE_LEN, rr_set.s, rr_set.len);
memcpy(route+RECORD_ROUTE_LEN+rr_set.len, CRLF, CRLF_LEN);
/* put after Via */
if ((lmp = insert_new_lump_after(lmp, route, size, HDR_RECORDROUTE_T)) == 0) {
LM_ERR("failed inserting new route set\n");
pkg_free(route);
pkg_free(rr_set.s);
return;
}
LM_DBG("Added record route [%.*s]\n", size, route);
pkg_free(rr_set.s);
}
return;
}
static void th_no_dlg_onreply(struct cell* t, int type, struct tmcb_params *param)
{
_th_no_dlg_onreply(t,type,param,0);
}
static void th_no_dlg_user_onreply(struct cell* t, int type, struct tmcb_params *param)
{
_th_no_dlg_onreply(t,type,param,TOPOH_KEEP_USER);
}
static int topo_hiding_no_dlg(struct sip_msg *req,struct cell* t,int extra_flags)
{
transaction_cb* used_cb;
/* parse all headers to be sure that all RR and Contact hdrs are found */
if (parse_headers(req, HDR_EOH_F, 0)< 0) {
LM_ERR("Failed to parse reply\n");
return -1;
}
if (topo_delete_record_routes(req) < 0) {
LM_ERR("Failed to remove Record Route header \n");
return -1;
}
if(topo_delete_vias(req) < 0) {
LM_ERR("Failed to remove via headers\n");
return -1;
}
if (topo_no_dlg_encode_contact(req,extra_flags) < 0) {
LM_ERR("Failed to encode contact header \n");
return -1;
}
if (extra_flags & TOPOH_KEEP_USER)
used_cb = th_no_dlg_user_onreply;
else
used_cb = th_no_dlg_onreply;
if (tm_api.register_tmcb( req, 0, TMCB_RESPONSE_FWDED,
used_cb,NULL, NULL)<0 ) {
LM_ERR("failed to register TMCB\n");
return -1;
}
return 1;
}
static int topo_hiding_with_dlg(struct sip_msg *req,struct cell* t,struct dlg_cell* dlg,int extra_flags)
{
dlg_api.set_mod_flag(dlg, TOPOH_ONGOING | extra_flags );
/* parse all headers to be sure that all RR and Contact hdrs are found */
if (parse_headers(req, HDR_EOH_F, 0)< 0) {
LM_ERR("Failed to parse reply\n");
return -1;
}
if (topo_delete_record_routes(req) < 0) {
LM_ERR("Failed to remove Record Route header \n");
return -1;
}
if(topo_delete_vias(req) < 0) {
LM_ERR("Failed to remove via headers\n");
return -1;
}
if(topo_dlg_replace_contact(req, dlg) < 0) {
LM_ERR("Failed to replace contact\n");
return -1;
}
if (dlg_api.register_dlgcb(dlg, DLGCB_RESPONSE_FWDED, topo_dlg_initial_reply, NULL, NULL)) {
LM_ERR("cannot register callback for fwded relies in dialog\n");
return -1;
}
if (dlg_api.register_dlgcb(dlg, DLGCB_TERMINATED | DLGCB_REQ_WITHIN,
topo_dlg_onroute, NULL , NULL)) {
LM_ERR("cannot register callback for sequential requests\n");
return -1;
}
return 1;
}
static void topo_unref_dialog(void *dialog)
{
dlg_api.unref_dlg((struct dlg_cell*)dialog, 1);
}
static void topo_dlg_initial_reply (struct dlg_cell* dlg, int type,
struct dlg_cb_params * params)
{
struct cell *t;
if (dlg==0)
return;
if (params->msg==FAKED_REPLY)
return;
t = tm_api.t_gett();
if (t == T_UNDEFINED || t == NULL)
return;
if(dlg_th_onreply(dlg, params->msg, t->uas.request, 1, DLG_DIR_UPSTREAM) < 0)
LM_ERR("Failed to transform the reply for topology hiding\n");
}
static void topo_dlg_onroute (struct dlg_cell* dlg, int type,
struct dlg_cb_params * params)
{
int dir = params->direction;
struct sip_msg *req = params->msg;
if (!req) {
LM_ERR("Called with NULL SIP message \n");
return;
}
/* parse all headers to be sure that all RR and Contact hdrs are found */
if (parse_headers(req, HDR_EOH_F, 0)< 0) {
LM_ERR("Failed to parse reply\n");
return;
}
/* delete vias */
if(topo_delete_vias(req) < 0) {
LM_ERR("Failed to remove via headers\n");
return;
}
/* delete record route */
if (topo_delete_record_routes(req) < 0) {
LM_ERR("Failed to remove record route headers \n");
return;
}
/* add route headers */
if (dlg_api.fix_route_dialog(req, dlg) < 0) {
LM_ERR("Failed to fix the SIP request according to dialog info \n");
return;
}
/* replace contact*/
if(topo_dlg_replace_contact(req, dlg) < 0) {
LM_ERR("Failed to replace contact\n");
return;
}
/* register tm callback for response in */
dlg_api.ref_dlg(dlg,1);
if (tm_api.register_tmcb( req, 0, TMCB_RESPONSE_FWDED,
(dir==DLG_DIR_UPSTREAM)?th_down_onreply:th_up_onreply,
(void*)dlg, topo_unref_dialog)<0 ) {
LM_ERR("failed to register TMCB\n");
dlg_api.unref_dlg(dlg,1);
return;
}
if (dir == DLG_DIR_UPSTREAM) {