forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 1
/
msg_translator.c
2127 lines (1952 loc) · 58.2 KB
/
msg_translator.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) 2006 Andreas Granig <[email protected]>
* ( covers insert_path_as_route() )
*
* 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-01-20 bug_fix: use of return value of snprintf aligned to C99 (jiri)
* 2003-01-23 added rport patches, contributed by
* Maxim Sobolev <[email protected]> and heavily modified by me
* (andrei)
* 2003-01-24 added i param to via of outgoing requests (used by tcp),
* modified via_builder params (andrei)
* 2003-01-27 more rport fixes (make use of new via_param->start) (andrei)
* 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
* 2003-01-29 scratchpad removed (jiri)
* 2003-02-28 scratchpad compatibility abandoned (jiri)
* 2003-03-01 VOICE_MAIL defs removed (jiri)
* 2003-03-06 totags in outgoing replies bookmarked to enable
* ACK/200 tag matching (andrei)
* 2003-03-18 killed the build_warning snprintf (andrei)
* 2003-03-31 added subst lump support (andrei)
* 2003-04-01 added opt (conditional) lump support (andrei)
* 2003-04-02 added more subst lumps: SUBST_{SND,RCV}_ALL
* => ip:port;transport=proto (andrei)
* 2003-04-12 added FL_FORCE_RPORT support (andrei)
* 2003-04-13 updated warning builder -- fixed (andrei)
* 2003-07-10 check_via_address knows now how to compare with ipv6 address
* references (e.g [::1]) (andrei)
* build_req_fomr_sip_req no longer adds 1 for ipv6 via parameter
* position calculations ([] are part of host.s now) (andrei)
* 2003-10-02 via+lump dst address/port can be set to preset values (andrei)
* 2003-10-08 receive_test function-alized (jiri)
* 2003-10-20 added body_lump list (sip_msg), adjust_clen (andrei & jan)
* 2003-11-11 type of rpl_lumps replaced by flags (bogdan)
* 2007-02-22 insert_path_as_route() imported from TM as we need it for
* stateless processing also; contributed by Andreas Granig
* (bogdan)
*/
/*!
* \file
* \brief Create and translate SIP messages/ message contents
* - \ref ViaSpecialParams
*/
/*! \page ViaSpecialParams Via header special parameters
*
* Via special params:
*
* \section requests Requests:
* - if the address in via is different from the src_ip or an existing
* received=something is found, received=src_ip is added (and any preexisting
* received is deleted). received is added as the first via parameter if no
* receive is previously present or over the old receive.
* - if the original via contains rport / rport=something or msg->msg_flags
* FL_FORCE_RPORT is set (e.g. script force_rport() cmd) rport=src_port
* is added (over previous rport / as first via param or after received
* if no received was present and received is added too)
* \section localreplies Local replies:
* (see also sl_send_reply)
* - rport and received are added in mostly the same way as for requests, but
* in the reverse order (first rport and then received). See also
* limitations.
* - the local reply is sent to the message source ip address. The
* destination port is set to the source port if rport is present or
* FL_FORCE_RPORT flag is set, to the via port or to
* the default sip port (5060) if neither rport or via port are present.
* \section normalreplies "Normal" replies:
* - if received is present the message is sent to the received address else
* if no port is present (neither a normal via port or rport) a dns srv
* lookup is performed on the host part and the reply is sent to the
* resulting ip. If a port is present or the host part is an ip address
* the dns lookup will be a "normal" one (A or AAAA).
* - if rport is present, it's value will be used as the destination port
* (and this will also disable srv lookups)
* - if no port is present the destination port will be taken from the srv
* lookup. If the srv lookup fails or is not performed (e.g. ip address
* in host) the destination port will be set to the default sip port (5060).
*
* \section limitations Known limitations:
* - when locally replying to a message, rport and received will be appended to
* the via header parameters (for forwarded requests they are inserted at the
* beginning).
* - a locally generated reply might get two received via parameters if a
* received is already present in the original message (this should not
* happen though, but ...)
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "msg_translator.h"
#include "globals.h"
#include "error.h"
#include "mem/mem.h"
#include "dprint.h"
#include "config.h"
#include "md5utils.h"
#include "data_lump.h"
#include "data_lump_rpl.h"
#include "ip_addr.h"
#include "resolve.h"
#include "ut.h"
#include "pt.h"
int disable_503_translation = 0;
#define append_str(_dest,_src,_len) \
do{\
memcpy( (_dest) , (_src) , (_len) );\
(_dest) += (_len) ;\
}while(0);
#define append_str_trans(_dest,_src,_len,_msg) \
append_str( (_dest), (_src), (_len) );
extern char version[];
extern int version_len;
/*! \brief check if IP address in Via != source IP address of signaling */
int received_test( struct sip_msg *msg )
{
int rcvd;
if(msg->via1->received !=NULL)
return 1;
if(msg->via1->maddr){
rcvd = check_ip_address(&msg->rcv.src_ip, &msg->via1->maddr->value,
msg->via1->port, msg->via1->proto, received_dns);
} else {
rcvd = check_ip_address(&msg->rcv.src_ip,
&msg->via1->host, msg->via1->port, msg->via1->proto, received_dns);
}
return rcvd;
}
static char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
{
static char buf[MAX_WARNING_LEN];
str *foo;
int print_len, l, clen;
char* t;
#define str_print(string, string_len) \
do{ \
l=(string_len); \
if ((clen+l)>MAX_WARNING_LEN) \
goto error_overflow; \
memcpy(buf+clen, (string), l); \
clen+=l; \
}while(0)
#define str_lenpair_print(string, string_len, string2, string2_len) \
do{ \
str_print(string, string_len); \
str_print(string2, string2_len);\
}while(0)
#define str_pair_print( string, string2, string2_len) \
str_lenpair_print((string), strlen((string)), (string2), (string2_len))
#define str_int_print(string, intval)\
do{\
t=int2str((intval), &print_len); \
str_pair_print(string, t, print_len);\
} while(0)
#define str_ipaddr_print(string, ipaddr_val)\
do{\
t=ip_addr2a((ipaddr_val)); \
print_len=strlen(t); \
str_pair_print(string, t, print_len);\
} while(0)
clen=0;
str_lenpair_print(WARNING, WARNING_LEN,
msg->rcv.bind_address->name.s,
msg->rcv.bind_address->name.len);
str_lenpair_print(":", 1, msg->rcv.bind_address->port_no_str.s,
msg->rcv.bind_address->port_no_str.len);
str_print(WARNING_PHRASE, WARNING_PHRASE_LEN);
/*adding out_uri*/
if (msg->new_uri.s)
foo=&(msg->new_uri);
else
foo=&(msg->first_line.u.request.uri);
/* pid= */
str_int_print(" pid=", my_pid());
/* req_src_ip= */
str_ipaddr_print(" req_src_ip=", &msg->rcv.src_ip);
str_int_print(" req_src_port=", msg->rcv.src_port);
str_pair_print(" in_uri=", msg->first_line.u.request.uri.s,
msg->first_line.u.request.uri.len);
str_pair_print(" out_uri=", foo->s, foo->len);
str_pair_print(" via_cnt",
(msg->parsed_flag & HDR_EOH_F)==HDR_EOH_F ? "=" : ">", 1);
str_int_print("=", via_cnt);
if (clen<MAX_WARNING_LEN){ buf[clen]='"'; clen++; }
else goto error_overflow;
*returned_len=clen;
return buf;
error_overflow:
LM_ERR("buffer size exceeded\n");
*returned_len=0;
return 0;
}
char* received_builder(struct sip_msg *msg, unsigned int *received_len)
{
char *buf, *tmp;
int len, tmp_len;
struct ip_addr *source_ip;
source_ip=&msg->rcv.src_ip;
buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
if (buf==0){
ser_error=E_OUT_OF_MEM;
LM_ERR("out of pkg memory\n");
return 0;
}
memcpy(buf, RECEIVED, RECEIVED_LEN);
if ( (tmp=ip_addr2a(source_ip))==0)
return 0; /* error*/
tmp_len=strlen(tmp);
len=RECEIVED_LEN+tmp_len;
memcpy(buf+RECEIVED_LEN, tmp, tmp_len);
buf[len]=0; /*null terminate it */
*received_len = len;
return buf;
}
char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
{
char* buf, * tmp;
int len, tmp_len;
tmp_len=0;
tmp=int2str(msg->rcv.src_port, &tmp_len);
len=RPORT_LEN+tmp_len;
buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
if (buf==0){
ser_error=E_OUT_OF_MEM;
LM_ERR("out of pkg memory\n");
return 0;
}
memcpy(buf, RPORT, RPORT_LEN);
memcpy(buf+RPORT_LEN, tmp, tmp_len);
buf[len]=0; /*null terminate it*/
*rport_len=len;
return buf;
}
char* id_builder(struct sip_msg* msg, unsigned int *id_len)
{
char* buf, *p;
int len, value_len, size;
char revhex[sizeof(int)*2];
size=sizeof(int)*2;
p=&revhex[0];
if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
LM_CRIT("not enough space for id\n");
return 0;
}
value_len=p-&revhex[0];
len=ID_PARAM_LEN+value_len;
buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
if (buf==0){
ser_error=E_OUT_OF_MEM;
LM_ERR("out of pkg memory\n");
return 0;
}
memcpy(buf, ID_PARAM, ID_PARAM_LEN);
memcpy(buf+ID_PARAM_LEN, revhex, value_len);
buf[len]=0; /* null terminate it */
*id_len=len;
return buf;
}
char* clen_builder(struct sip_msg* msg, int *clen_len, int diff)
{
char *buf, * value_s;
int len, value, value_len;
str body;
if ( (get_body(msg,&body)!=0) ) {
ser_error=E_BAD_REQ;
LM_ERR("no message body found (missing crlf?)");
return 0;
}
value = body.len + diff;
value_s=int2str(value, &value_len);
LM_DBG("content-length: %d (%s)\n", value, value_s);
len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
buf=pkg_malloc(sizeof(char)*(len+1));
if (buf==0){
ser_error=E_OUT_OF_MEM;
LM_ERR("out of pkg memory\n");
return 0;
}
memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
buf[len]=0; /* null terminate it */
*clen_len=len;
return buf;
}
/*! \brief* checks if a lump opt condition
* returns 1 if cond is true, 0 if false */
static inline int lump_check_opt( struct lump *l,
struct sip_msg* msg,
struct socket_info* snd_s
)
{
struct ip_addr* ip;
unsigned short port;
int proto;
#define get_ip_port_proto \
if (snd_s==0){ \
LM_CRIT("null send socket\n"); \
return 1; /* we presume they are different :-) */ \
} \
if (msg->rcv.bind_address){ \
ip=&msg->rcv.bind_address->address; \
port=msg->rcv.bind_address->port_no; \
proto=msg->rcv.bind_address->proto; \
}else{ \
ip=&msg->rcv.dst_ip; \
port=msg->rcv.dst_port; \
proto=msg->rcv.proto; \
} \
switch(l->u.cond){
case COND_FALSE:
return 0;
case COND_TRUE:
l->flags |= LUMPFLAG_COND_TRUE;
return 1;
case COND_IF_DIFF_REALMS:
get_ip_port_proto;
/* faster tests first */
if ((port==snd_s->port_no)&&(proto==snd_s->proto)&&
(ip_addr_cmp(ip, &snd_s->address))) {
l->flags &= ~LUMPFLAG_COND_TRUE;
return 0;
}
l->flags |= LUMPFLAG_COND_TRUE;
return 1;
case COND_IF_DIFF_AF:
get_ip_port_proto;
if (ip->af==snd_s->address.af) {
l->flags &= ~LUMPFLAG_COND_TRUE;
return 0;
}
l->flags |= LUMPFLAG_COND_TRUE;
return 1;
case COND_IF_DIFF_PROTO:
get_ip_port_proto;
if (proto==snd_s->proto) {
l->flags &= ~LUMPFLAG_COND_TRUE;
return 0;
}
l->flags |= LUMPFLAG_COND_TRUE;
return 1;
case COND_IF_DIFF_PORT:
get_ip_port_proto;
if (port==snd_s->port_no) {
l->flags &= ~LUMPFLAG_COND_TRUE;
return 0;
}
l->flags |= LUMPFLAG_COND_TRUE;
return 1;
case COND_IF_DIFF_IP:
get_ip_port_proto;
if (ip_addr_cmp(ip, &snd_s->address)) {
l->flags &= ~LUMPFLAG_COND_TRUE;
return 0;
}
l->flags |= LUMPFLAG_COND_TRUE;
return 1;
default:
LM_CRIT("unknown lump condition %d\n", l->u.cond);
}
return 0; /* false */
}
/*! \brief computes the "unpacked" len of a lump list,
code moved from build_req_from_req */
int lumps_len(struct sip_msg* msg, struct lump* lumps,
struct socket_info* send_sock)
{
unsigned int s_offset, new_len;
unsigned int last_del;
struct lump *t, *r;
str *send_address_str, *send_port_str;
str *rcv_address_str=NULL;
str *rcv_port_str=NULL;
#define SUBST_LUMP_LEN(subst_l) \
switch((subst_l)->u.subst){ \
case SUBST_RCV_IP: \
if (msg->rcv.bind_address){ \
new_len+=rcv_address_str->len; \
} else \
report_programming_bug("null bind adress 1"); \
break; \
case SUBST_RCV_PORT: \
if (msg->rcv.bind_address){ \
new_len+=rcv_port_str->len; \
} else \
report_programming_bug("null bind adress 2"); \
break; \
case SUBST_RCV_PROTO: \
if (msg->rcv.bind_address){ \
switch(msg->rcv.bind_address->proto){ \
case PROTO_NONE: \
case PROTO_UDP: \
case PROTO_TCP: \
case PROTO_TLS: \
new_len+=3; \
break; \
case PROTO_SCTP: \
new_len+=4; \
break; \
default: \
LM_CRIT("unknown proto %d\n", \
msg->rcv.bind_address->proto); \
}\
} else \
report_programming_bug("null bind adress 3"); \
break; \
case SUBST_RCV_ALL: \
if (msg->rcv.bind_address){ \
new_len+=rcv_address_str->len; \
if (msg->rcv.bind_address->port_no!=SIP_PORT || (rcv_port_str!=&(msg->rcv.bind_address->port_no_str))){ \
/* add :port_no */ \
new_len+=1+rcv_port_str->len; \
}\
/*add;transport=xxx*/ \
switch(msg->rcv.bind_address->proto){ \
case PROTO_NONE: \
case PROTO_UDP: \
break; /* udp is the default */ \
case PROTO_TCP: \
case PROTO_TLS: \
new_len+=TRANSPORT_PARAM_LEN+3; \
break; \
case PROTO_SCTP: \
new_len+=TRANSPORT_PARAM_LEN+4; \
break; \
default: \
LM_CRIT("unknown proto %d\n", \
msg->rcv.bind_address->proto); \
}\
} else \
report_programming_bug("null bind adress 4"); \
break; \
case SUBST_SND_IP: \
if (send_sock){ \
new_len+=send_address_str->len; \
} else \
report_programming_bug("null send_socket 1"); \
break; \
case SUBST_SND_PORT: \
if (send_sock){ \
new_len+=send_port_str->len; \
} else \
report_programming_bug("null send_socket 2"); \
break; \
case SUBST_SND_PROTO: \
if (send_sock){ \
switch(send_sock->proto){ \
case PROTO_NONE: \
case PROTO_UDP: \
case PROTO_TCP: \
case PROTO_TLS: \
new_len+=3; \
break; \
case PROTO_SCTP: \
new_len+=4; \
break; \
default: \
LM_CRIT("unknown proto %d\n", \
send_sock->proto); \
}\
} else \
report_programming_bug("null send_socket 3"); \
break; \
case SUBST_SND_ALL: \
if (send_sock){ \
new_len+=send_address_str->len; \
if ((send_sock->port_no!=SIP_PORT) || \
(send_port_str!=&(send_sock->port_no_str))){ \
/* add :port_no */ \
new_len+=1+send_port_str->len; \
}\
/*add;transport=xxx*/ \
switch(send_sock->proto){ \
case PROTO_NONE: \
case PROTO_UDP: \
break; /* udp is the default */ \
case PROTO_TCP: \
case PROTO_TLS: \
new_len+=TRANSPORT_PARAM_LEN+3; \
break; \
case PROTO_SCTP: \
new_len+=TRANSPORT_PARAM_LEN+4; \
break; \
default: \
LM_CRIT("unknown proto %d\n", \
send_sock->proto); \
}\
} else \
report_programming_bug("null send_socket 4"); \
break; \
case SUBST_NOP: /* do nothing */ \
break; \
default: \
LM_CRIT("unknown subst type %d\n", \
(subst_l)->u.subst); \
}
s_offset=0;
new_len=0;
last_del=0;
/* init send_address_str & send_port_str */
if(send_sock && send_sock->adv_name_str.len)
send_address_str=&(send_sock->adv_name_str);
else if (msg->set_global_address.s)
send_address_str=&(msg->set_global_address);
else if (default_global_address.s)
send_address_str=&default_global_address;
else
send_address_str=&(send_sock->address_str);
if(send_sock && send_sock->adv_port_str.len)
send_port_str=&(send_sock->adv_port_str);
else if (msg->set_global_port.s)
send_port_str=&(msg->set_global_port);
else if (default_global_port.s)
send_port_str=&default_global_port;
else
send_port_str=&(send_sock->port_no_str);
/* init rcv_address_str & rcv_port_str */
if(msg->rcv.bind_address) {
if(msg->rcv.bind_address->adv_name_str.len)
rcv_address_str=&(msg->rcv.bind_address->adv_name_str);
else
rcv_address_str=&(msg->rcv.bind_address->address_str);
if(msg->rcv.bind_address->adv_port_str.len)
rcv_port_str=&(msg->rcv.bind_address->adv_port_str);
else
rcv_port_str=&(msg->rcv.bind_address->port_no_str);
}
for (t = lumps; t; t = t->next) {
/* is this lump still valid? (it must not be anchored in a deleted area */
if (t->u.offset < s_offset && t->u.offset != last_del) {
LM_DBG("skip a %d, buffer offset=%d, lump offset=%d, last_del=%d\n",
t->op,s_offset, t->u.offset,last_del);
continue;
}
for (r = t->before; r; r = r->before) {
switch (r->op) {
case LUMP_ADD:
new_len += r->len;
break;
case LUMP_ADD_SUBST:
SUBST_LUMP_LEN(r);
break;
case LUMP_ADD_OPT:
/* skip if this is an OPT lump and the condition is
* not satisfied */
if (!lump_check_opt(r, msg, send_sock))
goto skip_before;
break;
case LUMP_SKIP:
/* if a SKIP lump, go to the last in the list*/
if (!r->before || !r->before->before)
continue;
for (; r->before->before; r = r->before)
;
break;
default:
/* only ADD allowed for before/after */
LM_CRIT("invalid op for data lump (%x)\n", r->op);
}
}
skip_before:
switch (t->op) {
case LUMP_DEL:
last_del=t->u.offset;
if (t->u.offset < s_offset) {
if (t->u.offset + t->len > s_offset) {
new_len -= t->len - (s_offset - t->u.offset);
s_offset = t->u.offset + t->len;
}
} else {
new_len -= t->len;
s_offset = t->u.offset + t->len;
}
break;
case LUMP_NOP:
/* do nothing */
break;
case LUMP_ADD:
/* FIXME: inconsistent with process_lumps() */
new_len += t->len;
break;
case LUMP_ADD_OPT:
report_programming_bug("LUMP_ADD_OPT");
/* we don't do anything here, it's only a condition for
* before & after */
break;
case LUMP_SKIP:
report_programming_bug("LUMP_SKIP");
/* we don't do anything here, it's only a condition for
* before & after */
break;
case LUMP_ADD_SUBST:
report_programming_bug("LUMP_ADD_SUBST");
SUBST_LUMP_LEN(t);
break;
default:
report_programming_bug("op for data lump (%x)", r->op);
}
for (r = t->after; r; r = r->after) {
switch (r->op) {
case LUMP_ADD:
new_len+=r->len;
break;
case LUMP_ADD_SUBST:
SUBST_LUMP_LEN(r);
break;
case LUMP_ADD_OPT:
/* skip if this is an OPT lump and the condition is
* not satisfied */
if (!lump_check_opt(r, msg, send_sock))
goto skip_after;
break;
case LUMP_SKIP:
/* if a SKIP lump, go to the last in the list*/
if (!r->after || !r->after->after)
continue;
for (; r->after->after; r = r->after)
;
break;
default:
/* only ADD allowed for before/after */
LM_CRIT("invalid op for data lump (%x)\n", r->op);
}
}
skip_after:
; /* to make gcc 3.* happy */
}
return new_len;
}
/*! \brief another helper functions, adds/Removes the lump,
code moved form build_req_from_req */
void process_lumps( struct sip_msg* msg,
struct lump* lumps,
char* new_buf,
unsigned int* new_buf_offs,
unsigned int* orig_offs,
struct socket_info* send_sock)
{
struct lump *t, *r;
char* orig;
unsigned int size, offset, s_offset;
unsigned int last_del;
str *send_address_str, *send_port_str;
str *rcv_address_str=NULL;
str *rcv_port_str=NULL;
#define SUBST_LUMP(subst_l) \
switch((subst_l)->u.subst){ \
case SUBST_RCV_IP: \
if (msg->rcv.bind_address){ \
memcpy(new_buf+offset, rcv_address_str->s, \
rcv_address_str->len); \
offset+=rcv_address_str->len; \
}else{ \
/*FIXME*/ \
LM_CRIT("null bind_address\n"); \
}; \
break; \
case SUBST_RCV_PORT: \
if (msg->rcv.bind_address){ \
memcpy(new_buf+offset, rcv_port_str->s, \
rcv_port_str->len); \
offset+=rcv_port_str->len; \
}else{ \
/*FIXME*/ \
LM_CRIT("null bind_address\n"); \
}; \
break; \
case SUBST_RCV_ALL: \
if (msg->rcv.bind_address){ \
/* address */ \
memcpy(new_buf+offset, rcv_address_str->s, \
rcv_address_str->len); \
offset+=rcv_address_str->len; \
/* :port */ \
if (msg->rcv.bind_address->port_no!=SIP_PORT || (rcv_port_str!=&(msg->rcv.bind_address->port_no_str))){ \
new_buf[offset]=':'; offset++; \
memcpy(new_buf+offset, \
rcv_port_str->s, \
rcv_port_str->len); \
offset+=rcv_port_str->len; \
}\
switch(msg->rcv.bind_address->proto){ \
case PROTO_NONE: \
case PROTO_UDP: \
break; /* nothing to do, udp is default*/ \
case PROTO_TCP: \
memcpy(new_buf+offset, TRANSPORT_PARAM, \
TRANSPORT_PARAM_LEN); \
offset+=TRANSPORT_PARAM_LEN; \
memcpy(new_buf+offset, "tcp", 3); \
offset+=3; \
break; \
case PROTO_TLS: \
memcpy(new_buf+offset, TRANSPORT_PARAM, \
TRANSPORT_PARAM_LEN); \
offset+=TRANSPORT_PARAM_LEN; \
memcpy(new_buf+offset, "tls", 3); \
offset+=3; \
break; \
case PROTO_SCTP: \
memcpy(new_buf+offset, TRANSPORT_PARAM, \
TRANSPORT_PARAM_LEN); \
offset+=TRANSPORT_PARAM_LEN; \
memcpy(new_buf+offset, "sctp", 4); \
offset+=4; \
break; \
default: \
LM_CRIT("unknown proto %d\n", \
msg->rcv.bind_address->proto); \
} \
}else{ \
/*FIXME*/ \
LM_CRIT("null bind_address\n"); \
}; \
break; \
case SUBST_SND_IP: \
if (send_sock){ \
memcpy(new_buf+offset, send_address_str->s, \
send_address_str->len); \
offset+=send_address_str->len; \
}else{ \
/*FIXME*/ \
LM_CRIT("called with null send_sock\n"); \
}; \
break; \
case SUBST_SND_PORT: \
if (send_sock){ \
memcpy(new_buf+offset, send_port_str->s, \
send_port_str->len); \
offset+=send_port_str->len; \
}else{ \
/*FIXME*/ \
LM_CRIT("called with null send_sock\n"); \
}; \
break; \
case SUBST_SND_ALL: \
if (send_sock){ \
/* address */ \
memcpy(new_buf+offset, send_address_str->s, \
send_address_str->len); \
offset+=send_address_str->len; \
/* :port */ \
if ((send_sock->port_no!=SIP_PORT) || \
(send_port_str!=&(send_sock->port_no_str))){ \
new_buf[offset]=':'; offset++; \
memcpy(new_buf+offset, send_port_str->s, \
send_port_str->len); \
offset+=send_port_str->len; \
}\
switch(send_sock->proto){ \
case PROTO_NONE: \
case PROTO_UDP: \
break; /* nothing to do, udp is default*/ \
case PROTO_TCP: \
memcpy(new_buf+offset, TRANSPORT_PARAM, \
TRANSPORT_PARAM_LEN); \
offset+=TRANSPORT_PARAM_LEN; \
memcpy(new_buf+offset, "tcp", 3); \
offset+=3; \
break; \
case PROTO_TLS: \
memcpy(new_buf+offset, TRANSPORT_PARAM, \
TRANSPORT_PARAM_LEN); \
offset+=TRANSPORT_PARAM_LEN; \
memcpy(new_buf+offset, "tls", 3); \
offset+=3; \
break; \
case PROTO_SCTP: \
memcpy(new_buf+offset, TRANSPORT_PARAM, \
TRANSPORT_PARAM_LEN); \
offset+=TRANSPORT_PARAM_LEN; \
memcpy(new_buf+offset, "sctp", 4); \
offset+=4; \
break; \
default: \
LM_CRIT("unknown proto %d\n", \
send_sock->proto); \
} \
}else{ \
/*FIXME*/ \
LM_CRIT("null bind_address\n"); \
}; \
break; \
case SUBST_RCV_PROTO: \
if (msg->rcv.bind_address){ \
switch(msg->rcv.bind_address->proto){ \
case PROTO_NONE: \
case PROTO_UDP: \
memcpy(new_buf+offset, "udp", 3); \
offset+=3; \
break; \
case PROTO_TCP: \
memcpy(new_buf+offset, "tcp", 3); \
offset+=3; \
break; \
case PROTO_TLS: \
memcpy(new_buf+offset, "tls", 3); \
offset+=3; \
break; \
case PROTO_SCTP: \
memcpy(new_buf+offset, "sctp", 4); \
offset+=4; \
break; \
default: \
LM_CRIT("unknown proto %d\n", \
msg->rcv.bind_address->proto); \
} \
}else{ \
/*FIXME*/ \
LM_CRIT("called with null send_sock \n"); \
}; \
break; \
case SUBST_SND_PROTO: \
if (send_sock){ \
switch(send_sock->proto){ \
case PROTO_NONE: \
case PROTO_UDP: \
memcpy(new_buf+offset, "udp", 3); \
offset+=3; \
break; \
case PROTO_TCP: \
memcpy(new_buf+offset, "tcp", 3); \
offset+=3; \
break; \
case PROTO_TLS: \
memcpy(new_buf+offset, "tls", 3); \
offset+=3; \
break; \
case PROTO_SCTP: \
memcpy(new_buf+offset, "sctp", 4); \
offset+=4; \
break; \
default: \
LM_CRIT("unknown proto %d\n", \
send_sock->proto); \
} \
}else{ \
/*FIXME*/ \
LM_CRIT("called with null send_sock \n"); \
}; \
break; \
default: \
LM_CRIT("unknown subst type %d\n", \
(subst_l)->u.subst); \
} \
\
/* init send_address_str & send_port_str */
if(send_sock && send_sock->adv_name_str.len)
send_address_str=&(send_sock->adv_name_str);
else if (msg->set_global_address.len)
send_address_str=&(msg->set_global_address);
else if (default_global_address.s)
send_address_str=&default_global_address;
else
send_address_str=&(send_sock->address_str);
if(send_sock && send_sock->adv_port_str.len)
send_port_str=&(send_sock->adv_port_str);
else if (msg->set_global_port.len)
send_port_str=&(msg->set_global_port);
else if (default_global_port.s)
send_port_str=&default_global_port;
else
send_port_str=&(send_sock->port_no_str);
/* init rcv_address_str & rcv_port_str */
if(msg->rcv.bind_address) {
if(msg->rcv.bind_address->adv_name_str.len)
rcv_address_str=&(msg->rcv.bind_address->adv_name_str);
else
rcv_address_str=&(msg->rcv.bind_address->address_str);
if(msg->rcv.bind_address->adv_port_str.len)
rcv_port_str=&(msg->rcv.bind_address->adv_port_str);
else
rcv_port_str=&(msg->rcv.bind_address->port_no_str);
}
orig=msg->buf;
offset=*new_buf_offs;
s_offset=*orig_offs;
last_del=0;
for (t = lumps; t; t = t->next) {
/* skip this lump if the "offset" is still in a "deleted" area */
if (t->u.offset < s_offset && t->u.offset != last_del) {
LM_DBG("skip a %d, buffer offset=%d, lump offset=%d, last_del=%d\n",
t->op,s_offset, t->u.offset,last_del);
continue;
}
switch (t->op) {
case LUMP_NOP:
case LUMP_DEL:
/* copy till offset (if any) */
if (s_offset < t->u.offset) {
size = t->u.offset-s_offset;
memcpy(new_buf+offset, orig+s_offset, size);
offset += size;
s_offset += size;
}
if (t->op == LUMP_DEL)
last_del = t->u.offset;
/* process before */
for (r = t->before; r; r = r->before) {
switch (r->op) {
case LUMP_ADD:
/*just add it here*/