f45cb66a41c579d3791cae6457f12ac0bc481ff1
[sip-router] / msg_translator.c
1 /*
2  * $Id$
3  *
4  *
5  * Copyright (C) 2001-2003 FhG Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  *
28  *
29  * History:
30  * --------
31  * 2003-01-20  bug_fix: use of return value of snprintf aligned to C99 (jiri)
32  * 2003-01-23  added rport patches, contributed by
33  *              Maxim Sobolev <sobomax@FreeBSD.org> and heavily modified by me
34  *              (andrei)
35  * 2003-01-24  added i param to via of outgoing requests (used by tcp),
36  *              modified via_builder params (andrei)
37  * 2003-01-27  more rport fixes (make use of new via_param->start)  (andrei)
38  * 2003-01-27  next baby-step to removing ZT - PRESERVE_ZT (jiri)
39  * 2003-01-29  scratchpad removed (jiri)
40  * 2003-02-28  scratchpad compatibility abandoned (jiri)
41  * 2003-03-01  VOICE_MAIL defs removed (jiri)
42  * 2003-03-06  totags in outgoing replies bookmarked to enable
43  *             ACK/200 tag matching (andrei)
44  * 2003-03-18  killed the build_warning snprintf (andrei)
45  * 2003-03-31  added subst lump support (andrei)
46  * 2003-04-01  added opt (conditional) lump support (andrei)
47  * 2003-04-02  added more subst lumps: SUBST_{SND,RCV}_ALL
48  *              => ip:port;transport=proto (andrei)
49  * 2003-04-12  added FL_FORCE_RPORT support (andrei)
50  * 2003-04-13  updated warning builder -- fixed (andrei)
51  * 2003-07-10  check_via_address knows now how to compare with ipv6 address
52  *              references (e.g [::1]) (andrei)
53  *             build_req_fomr_sip_req no longer adds 1 for ipv6 via parameter
54  *              position calculations ([] are part of host.s now) (andrei)
55  * 2003-10-02  via+lump dst address/port can be set to preset values (andrei)
56  * 2003-10-08  receive_test function-alized (jiri)
57  * 2003-10-20  added body_lump list (sip_msg), adjust_clen (andrei & jan)
58  * 2003-11-11  type of rpl_lumps replaced by flags (bogdan)
59  * 2006-04-20  build_req_from_sip_req, via_builder and lump_* functions
60  *              use now struct dest_info; lumps & via comp param support
61  *              (rfc3486) (andrei)
62  * 2007-08-31  id_builder() and via_builder() are grouped into one function:
63  *             create_via_hf() -- tm module needs them as well (Miklos)
64  * 2008-12-17  build_req_from_sip_req() will now fallback to tcp, tls or sctp
65  *              if packet size > udp_mtu and fallback is enabled 
66  *             build_req_from_sip_req() uses now global_req_flags along
67  *               msg->msg_flags  (andrei)
68  *
69  */
70 /* Via special params:
71  * requests:
72  * - if the address in via is different from the src_ip or an existing
73  *   received=something is found, received=src_ip is added (and any preexisting
74  *   received is deleted). received is added as the first via parameter if no
75  *   receive is previously present or over the old receive.
76  * - if the original via contains rport / rport=something or msg->msg_flags
77  *   FL_FORCE_RPORT is set (e.g. script force_rport() cmd) rport=src_port
78  *   is added (over previous rport / as first via param or after received
79  *   if no received was present and received is added too)
80  * local replies:
81  *    (see also sl_send_reply)
82  *  - rport and received are added in mostly the same way as for requests, but
83  *    in the reverse order (first rport and then received). See also
84  *    limitations.
85  *  - if reply_to_via is set (default off) the local reply will be sent to
86  *    the address in via (received is ignored since it was not set by us). The
87  *    destination port is either the message source port if via contains rport
88  *    or the FL_FORCE_RPORT flag is set or the port from the via. If either
89  *    port or rport are present a normal dns lookup (instead of a srv lookup)
90  *    is performed on the address. If no port is present and a srv lookup is
91  *    performed the port is taken from the srv lookup. If the srv lookup failed
92  *    or it was not performed, the port is set to the default sip port (5060).
93  *  - if reply_to_via is off (default) the local reply is sent to the message
94  *    source ip address. The destination port is set to the source port if
95  *    rport is present or FL_FORCE_RPORT flag is set, to the via port or to
96  *    the default sip port (5060) if neither rport or via port are present.
97  * "normal" replies:
98  *  - if received is present the message is sent to the received address else
99  *    if no port is present (neither a normal via port or rport) a dns srv
100  *    lookup is performed on the host part and the reply is sent to the
101  *    resulting ip. If a port is present or the host part is an ip address
102  *    the dns lookup will be a "normal" one (A or AAAA).
103  *  - if rport is present, it's value will be used as the destination port
104  *   (and this will also disable srv lookups)
105  *  - if no port is present the destination port will be taken from the srv
106  *    lookup. If the srv lookup fails or is not performed (e.g. ip address
107  *    in host) the destination port will be set to the default sip port (5060).
108  *
109  * Known limitations:
110  * - when locally replying to a message, rport and received will be appended to
111  *   the via header parameters (for forwarded requests they are inserted at the
112  *   beginning).
113  * - a locally generated reply might get two received via parameters if a
114  *   received is already present in the original message (this should not
115  *   happen though, but ...)
116  *
117  *--andrei
118 */
119
120 /*!
121  * \file
122  * \brief SIP-router core :: 
123  * \ingroup core
124  * Module: \ref core
125  */
126
127 #include <sys/types.h>
128 #include <sys/socket.h>
129 #include <netdb.h>
130 #include <string.h>
131 #include <stdio.h>
132 #include <stdlib.h>
133
134 #include "comp_defs.h"
135 #include "msg_translator.h"
136 #include "globals.h"
137 #include "error.h"
138 #include "mem/mem.h"
139 #include "dprint.h"
140 #include "config.h"
141 #include "md5utils.h"
142 #include "data_lump.h"
143 #include "data_lump_rpl.h"
144 #include "ip_addr.h"
145 #include "resolve.h"
146 #include "ut.h"
147 #include "pt.h"
148 #include "cfg/cfg.h"
149 #include "parser/parse_to.h"
150 #include "forward.h"
151
152
153 #define append_str_trans(_dest,_src,_len,_msg) \
154         append_str( (_dest), (_src), (_len) );
155
156 extern char version[];
157 extern int version_len;
158
159
160
161 /** per process fixup function for global_req_flags.
162   * It should be called from the configuration framework.
163   */
164 void fix_global_req_flags(str* gname, str* name)
165 {
166         global_req_flags=0;
167         switch(cfg_get(core, core_cfg, udp_mtu_try_proto)){
168                 case PROTO_NONE:
169                 case PROTO_UDP:
170                         /* do nothing */
171                         break;
172                 case PROTO_TCP:
173                         global_req_flags|=FL_MTU_TCP_FB;
174                         break;
175                 case PROTO_TLS:
176                         global_req_flags|=FL_MTU_TLS_FB;
177                         break;
178                 case PROTO_SCTP:
179                         global_req_flags|=FL_MTU_SCTP_FB;
180                         break;
181         }
182         if (cfg_get(core, core_cfg, force_rport))
183                 global_req_flags|=FL_FORCE_RPORT;
184 }
185
186
187
188 /* checks if ip is in host(name) and ?host(ip)=name?
189  * ip must be in network byte order!
190  *  resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
191  * return 0 if equal */
192 static int check_via_address(struct ip_addr* ip, str *name,
193                                 unsigned short port, int resolver)
194 {
195         struct hostent* he;
196         int i;
197         char* s;
198         #ifdef USE_IPV6
199         int len;
200         #endif
201
202         /* maybe we are lucky and name it's an ip */
203         s=ip_addr2a(ip);
204         if (s){
205                 DBG("check_via_address(%s, %.*s, %d)\n",
206                         s, name->len, name->s, resolver);
207
208         #ifdef USE_IPV6
209
210                 len=strlen(s);
211
212                 /* check if name->s is an ipv6 address or an ipv6 address ref. */
213                 if ((ip->af==AF_INET6) &&
214                                 (       ((len==name->len)&&(strncasecmp(name->s, s, name->len)==0))
215                                         ||
216                                         ((len==(name->len-2))&&(name->s[0]=='[')&&
217                                                 (name->s[name->len-1]==']')&&
218                                                 (strncasecmp(name->s+1, s, len)==0))
219                                 )
220                    )
221                         return 0;
222                 else
223         #endif
224
225                         if (strncmp(name->s, s, name->len)==0)
226                                 return 0;
227         }else{
228                 LOG(L_CRIT, "check_via_address: BUG: could not convert ip address\n");
229                 return -1;
230         }
231
232         if (port==0) port=SIP_PORT;
233         if (resolver&DO_DNS){
234                 DBG("check_via_address: doing dns lookup\n");
235                 /* try all names ips */
236                 he=sip_resolvehost(name, &port, 0); /* don't use naptr */
237                 if (he && ip->af==he->h_addrtype){
238                         for(i=0;he && he->h_addr_list[i];i++){
239                                 if ( memcmp(&he->h_addr_list[i], ip->u.addr, ip->len)==0)
240                                         return 0;
241                         }
242                 }
243         }
244         if (resolver&DO_REV_DNS){
245                 DBG("check_via_address: doing rev. dns lookup\n");
246                 /* try reverse dns */
247                 he=rev_resolvehost(ip);
248                 if (he && (strncmp(he->h_name, name->s, name->len)==0))
249                         return 0;
250                 for (i=0; he && he->h_aliases[i];i++){
251                         if (strncmp(he->h_aliases[i],name->s, name->len)==0)
252                                 return 0;
253                 }
254         }
255         return -1;
256 }
257
258
259 /* check if IP address in Via != source IP address of signaling */
260 int received_test( struct sip_msg *msg )
261 {
262         int rcvd;
263
264         rcvd=msg->via1->received
265                         || check_via_address(&msg->rcv.src_ip, &msg->via1->host,
266                                                         msg->via1->port, received_dns);
267         return rcvd;
268 }
269
270
271 static char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
272 {
273         static char buf[MAX_WARNING_LEN];
274         str *foo;
275         int print_len, l;
276         int clen;
277         char* t;
278
279 #define str_print(string, string_len) \
280                 do{ \
281                         l=(string_len); \
282                         if ((clen+l)>MAX_WARNING_LEN) \
283                                 goto error_overflow; \
284                         memcpy(buf+clen, (string), l); \
285                         clen+=l; \
286                 }while(0)
287
288 #define str_lenpair_print(string, string_len, string2, string2_len) \
289                 do{ \
290                         str_print(string, string_len); \
291                         str_print(string2, string2_len);\
292                 }while(0)
293
294 #define str_pair_print( string, string2, string2_len) \
295                 str_lenpair_print((string), strlen((string)), (string2), (string2_len))
296
297 #define str_int_print(string, intval)\
298                 do{\
299                         t=int2str((intval), &print_len); \
300                         str_pair_print(string, t, print_len);\
301                 } while(0)
302
303 #define str_su_print(sockaddr)\
304                 do{\
305                         t=su2a(&sockaddr, sizeof(sockaddr)); \
306                         print_len=strlen(t); \
307                         str_print(t, print_len); \
308                 } while(0)
309
310 #define str_ipaddr_print(string, ipaddr_val)\
311                 do{\
312                         t=ip_addr2a((ipaddr_val)); \
313                         print_len=strlen(t); \
314                         str_pair_print(string, t, print_len);\
315                 } while(0)
316
317         clen=0;
318         str_print(WARNING, WARNING_LEN);
319         str_su_print(msg->rcv.bind_address->su);
320         str_print(WARNING_PHRASE, WARNING_PHRASE_LEN);
321
322         /*adding out_uri*/
323         if (msg->new_uri.s)
324                 foo=&(msg->new_uri);
325         else
326                 foo=&(msg->first_line.u.request.uri);
327         /* pid= */
328         str_int_print(" pid=", my_pid());
329         /* req_src_ip= */
330         str_ipaddr_print(" req_src_ip=", &msg->rcv.src_ip);
331         str_int_print(" req_src_port=", msg->rcv.src_port);
332         str_pair_print(" in_uri=", msg->first_line.u.request.uri.s,
333                                                                 msg->first_line.u.request.uri.len);
334         str_pair_print(" out_uri=", foo->s, foo->len);
335         str_pair_print(" via_cnt",
336                                         (msg->parsed_flag & HDR_EOH_F)==HDR_EOH_F ? "=" : ">", 1);
337         str_int_print("=", via_cnt);
338         if (clen<MAX_WARNING_LEN){ buf[clen]='"'; clen++; }
339         else goto error_overflow;
340
341
342         *returned_len=clen;
343         return buf;
344 error_overflow:
345         LOG(L_NOTICE, "NOTICE: warning_builder: buffer size exceeded (probably too long URI)\n");
346         *returned_len=0;
347         return 0;
348 }
349
350
351
352
353 char* received_builder(struct sip_msg *msg, unsigned int *received_len)
354 {
355         char *buf;
356         int  len;
357         struct ip_addr *source_ip;
358         char *tmp;
359         int  tmp_len;
360
361         source_ip=&msg->rcv.src_ip;
362
363         buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
364         if (buf==0){
365                 ser_error=E_OUT_OF_MEM;
366                 LOG(L_ERR, "ERROR: received_builder: out of memory\n");
367                 return 0;
368         }
369         memcpy(buf, RECEIVED, RECEIVED_LEN);
370         if ( (tmp=ip_addr2a(source_ip))==0)
371                 return 0; /* error*/
372         tmp_len=strlen(tmp);
373         len=RECEIVED_LEN+tmp_len;
374
375         memcpy(buf+RECEIVED_LEN, tmp, tmp_len);
376         buf[len]=0; /*null terminate it */
377
378         *received_len = len;
379         return buf;
380 }
381
382
383
384 char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
385 {
386         char* buf;
387         char* tmp;
388         int tmp_len;
389         int len;
390
391         tmp_len=0;
392         tmp=int2str(msg->rcv.src_port, &tmp_len);
393         len=RPORT_LEN+tmp_len;
394         buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
395         if (buf==0){
396                 ser_error=E_OUT_OF_MEM;
397                 LOG(L_ERR, "ERROR: rport_builder: out of memory\n");
398                 return 0;
399         }
400         memcpy(buf, RPORT, RPORT_LEN);
401         memcpy(buf+RPORT_LEN, tmp, tmp_len);
402         buf[len]=0; /*null terminate it*/
403
404         *rport_len=len;
405         return buf;
406 }
407
408
409
410 char* id_builder(struct sip_msg* msg, unsigned int *id_len)
411 {
412         char* buf;
413         int len, value_len;
414         char revhex[sizeof(int)*2];
415         char* p;
416         int size;
417
418         size=sizeof(int)*2;
419         p=&revhex[0];
420         if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
421                 LOG(L_CRIT, "BUG: id_builder: not enough space for id\n");
422                 return 0;
423         }
424         value_len=p-&revhex[0];
425         len=ID_PARAM_LEN+value_len;
426         buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
427         if (buf==0){
428                 ser_error=E_OUT_OF_MEM;
429                 LOG(L_ERR, "ERROR: id_builder: out of memory\n");
430                 return 0;
431         }
432         memcpy(buf, ID_PARAM, ID_PARAM_LEN);
433         memcpy(buf+ID_PARAM_LEN, revhex, value_len);
434         buf[len]=0; /* null terminate it */
435         *id_len=len;
436         return buf;
437 }
438
439
440
441 char* clen_builder(     struct sip_msg* msg, int *clen_len, int diff, 
442                                         int body_only)
443 {
444         char* buf;
445         int len;
446         int value;
447         char* value_s;
448         int value_len;
449         char* body;
450
451
452         body=get_body(msg);
453         if (body==0){
454                 ser_error=E_BAD_REQ;
455                 LOG(L_ERR, "ERROR: clen_builder: no message body found"
456                                         " (missing crlf?)");
457                 return 0;
458         }
459         value=msg->len-(int)(body-msg->buf)+diff;
460         value_s=int2str(value, &value_len);
461         DBG("clen_builder: content-length: %d (%s)\n", value, value_s);
462
463         if (body_only) {
464                 len=value_len;
465         }
466         else {
467                 len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
468         }
469         buf=pkg_malloc(sizeof(char)*(len+1));
470         if (buf==0){
471                 ser_error=E_OUT_OF_MEM;
472                 LOG(L_ERR, "ERROR: clen_builder: out of memory\n");
473                 return 0;
474         }
475         if (body_only) {
476                 memcpy(buf, value_s, value_len);
477         }
478         else {
479                 memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
480                 memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
481                 memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
482         }
483         buf[len]=0; /* null terminate it */
484         *clen_len=len;
485         return buf;
486 }
487
488
489
490 /* checks if a lump opt condition
491  * returns 1 if cond is true, 0 if false */
492 static inline int lump_check_opt(       struct lump *l,
493                                                                         struct sip_msg* msg,
494                                                                         struct dest_info* snd_i
495                                                                         )
496 {
497         struct ip_addr* ip;
498         unsigned short port;
499         int proto;
500
501 #define get_ip_port_proto \
502                         if ((snd_i==0) || (snd_i->send_sock==0)){ \
503                                 LOG(L_CRIT, "ERROR: lump_check_opt: null send socket\n"); \
504                                 return 1; /* we presume they are different :-) */ \
505                         } \
506                         if (msg->rcv.bind_address){ \
507                                 ip=&msg->rcv.bind_address->address; \
508                                 port=msg->rcv.bind_address->port_no; \
509                                 proto=msg->rcv.bind_address->proto; \
510                         }else{ \
511                                 ip=&msg->rcv.dst_ip; \
512                                 port=msg->rcv.dst_port; \
513                                 proto=msg->rcv.proto; \
514                         } \
515
516         switch(l->u.cond){
517                 case COND_FALSE:
518                         return 0;
519                 case COND_TRUE:
520                         LUMP_SET_COND_TRUE(l);
521                         return 1;
522                 case COND_IF_DIFF_REALMS:
523                         get_ip_port_proto;
524                         /* faster tests first */
525                         if ((port==snd_i->send_sock->port_no) && 
526                                         (proto==snd_i->send_sock->proto) &&
527 #ifdef USE_COMP
528                                         (msg->rcv.comp==snd_i->comp) &&
529 #endif
530                                         (ip_addr_cmp(ip, &snd_i->send_sock->address)))
531                                 return 0;
532                         else {
533                                 LUMP_SET_COND_TRUE(l);
534                                 return 1;
535                         }
536                 case COND_IF_DIFF_AF:
537                         get_ip_port_proto;
538                         if (ip->af!=snd_i->send_sock->address.af) {
539                                 LUMP_SET_COND_TRUE(l);
540                                 return 1;
541                         } else return 0;
542                 case COND_IF_DIFF_PROTO:
543                         get_ip_port_proto;
544                         if (proto!=snd_i->send_sock->proto) {
545                                 LUMP_SET_COND_TRUE(l);
546                                 return 1;
547                         } else return 0;
548                 case COND_IF_DIFF_PORT:
549                         get_ip_port_proto;
550                         if (port!=snd_i->send_sock->port_no) {
551                                 LUMP_SET_COND_TRUE(l);
552                                 return 1;
553                         } else return 0;
554                 case COND_IF_DIFF_IP:
555                         get_ip_port_proto;
556                         if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
557                         else {
558                                 LUMP_SET_COND_TRUE(l);
559                                 return 1;
560                         }
561                 case COND_IF_RAND:
562                         if(rand()>=RAND_MAX/2) {
563                                 LUMP_SET_COND_TRUE(l);
564                                 return 1;
565                         } else return 0;
566                 default:
567                         LOG(L_CRIT, "BUG: lump:w_check_opt: unknown lump condition %d\n",
568                                         l->u.cond);
569         }
570         return 0; /* false */
571 }
572
573
574
575 /* computes the "unpacked" len of a lump list,
576    code moved from build_req_from_req */
577 static inline int lumps_len(struct sip_msg* msg, struct lump* lumps, 
578                                                                 struct dest_info* send_info)
579 {
580         int s_offset;
581         int new_len;
582         struct lump* t;
583         struct lump* r;
584         str* send_address_str;
585         str* send_port_str;
586         str* recv_address_str = NULL;
587         str* recv_port_str = NULL;
588         int  recv_port_no = 0;
589         struct socket_info* send_sock;
590         
591
592 #ifdef USE_COMP
593         #define RCVCOMP_LUMP_LEN \
594                                                 /* add;comp=xxx */ \
595                                         switch(msg->rcv.comp){ \
596                                                 case COMP_NONE: \
597                                                                 break; \
598                                                 case COMP_SIGCOMP: \
599                                                                 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
600                                                                 break; \
601                                                 case COMP_SERGZ: \
602                                                                 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
603                                                                 break; \
604                                                 default: \
605                                                 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
606                                                                 msg->rcv.comp); \
607                                         }
608
609         #define SENDCOMP_LUMP_LEN \
610                                         /* add;comp=xxx */ \
611                                         switch(send_info->comp){ \
612                                                 case COMP_NONE: \
613                                                                 break; \
614                                                 case COMP_SIGCOMP: \
615                                                                 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
616                                                                 break; \
617                                                 case COMP_SERGZ: \
618                                                                 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
619                                                                 break; \
620                                                 default: \
621                                                 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
622                                                                 send_info->comp); \
623                                         }
624 #else
625         #define RCVCOMP_LUMP_LEN
626         #define SENDCOMP_LUMP_LEN
627 #endif /*USE_COMP */
628         
629 #define SUBST_LUMP_LEN(subst_l) \
630                 switch((subst_l)->u.subst){ \
631                         case SUBST_RCV_IP: \
632                                 if (msg->rcv.bind_address){ \
633                                         new_len+=recv_address_str->len; \
634                                         if (msg->rcv.bind_address->address.af!=AF_INET) \
635                                                 new_len+=2; \
636                                 }else{ \
637                                         /* FIXME */ \
638                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
639                                 }; \
640                                 break; \
641                         case SUBST_RCV_PORT: \
642                                 if (msg->rcv.bind_address){ \
643                                         new_len+=recv_port_str->len; \
644                                 }else{ \
645                                         /* FIXME */ \
646                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
647                                 }; \
648                                 break; \
649                         case SUBST_RCV_PROTO: \
650                                 if (msg->rcv.bind_address){ \
651                                         switch(msg->rcv.bind_address->proto){ \
652                                                 case PROTO_NONE: \
653                                                 case PROTO_UDP: \
654                                                 case PROTO_TCP: \
655                                                 case PROTO_TLS: \
656                                                                 new_len+=3; \
657                                                                 break; \
658                                                 case PROTO_SCTP: \
659                                                                 new_len+=4; \
660                                                                 break; \
661                                                 default: \
662                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
663                                                                 msg->rcv.bind_address->proto); \
664                                         }\
665                                 }else{ \
666                                         /* FIXME */ \
667                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
668                                 }; \
669                                 break; \
670                         case SUBST_RCV_ALL: \
671                                 if (msg->rcv.bind_address){ \
672                                         new_len+=recv_address_str->len; \
673                                         if (msg->rcv.bind_address->address.af!=AF_INET) \
674                                                 new_len+=2; \
675                                         if (recv_port_no!=SIP_PORT){ \
676                                                 /* add :port_no */ \
677                                                 new_len+=1+recv_port_str->len; \
678                                         }\
679                                                 /*add;transport=xxx*/ \
680                                         switch(msg->rcv.bind_address->proto){ \
681                                                 case PROTO_NONE: \
682                                                 case PROTO_UDP: \
683                                                                 break; /* udp is the default */ \
684                                                 case PROTO_TCP: \
685                                                 case PROTO_TLS: \
686                                                                 new_len+=TRANSPORT_PARAM_LEN+3; \
687                                                                 break; \
688                                                 case PROTO_SCTP: \
689                                                                 new_len+=TRANSPORT_PARAM_LEN+4; \
690                                                                 break; \
691                                                 default: \
692                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
693                                                                 msg->rcv.bind_address->proto); \
694                                         }\
695                                         RCVCOMP_LUMP_LEN \
696                                 }else{ \
697                                         /* FIXME */ \
698                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
699                                 }; \
700                                 break; \
701                         case SUBST_SND_IP: \
702                                 if (send_sock){ \
703                                         new_len+=send_address_str->len; \
704                                         if (send_sock->address.af!=AF_INET && \
705                                                         send_address_str==&(send_sock->address_str)) \
706                                                 new_len+=2; \
707                                 }else{ \
708                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
709                                                         " null send_sock\n"); \
710                                 }; \
711                                 break; \
712                         case SUBST_SND_PORT: \
713                                 if (send_sock){ \
714                                         new_len+=send_port_str->len; \
715                                 }else{ \
716                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
717                                                         " null send_sock\n"); \
718                                 }; \
719                                 break; \
720                         case SUBST_SND_PROTO: \
721                                 if (send_sock){ \
722                                         switch(send_sock->proto){ \
723                                                 case PROTO_NONE: \
724                                                 case PROTO_UDP: \
725                                                 case PROTO_TCP: \
726                                                 case PROTO_TLS: \
727                                                                 new_len+=3; \
728                                                                 break; \
729                                                 case PROTO_SCTP: \
730                                                                 new_len+=4; \
731                                                                 break; \
732                                                 default: \
733                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
734                                                                 send_sock->proto); \
735                                         }\
736                                 }else{ \
737                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
738                                                         " null send_sock\n"); \
739                                 }; \
740                                 break; \
741                         case SUBST_SND_ALL: \
742                                 if (send_sock){ \
743                                         new_len+=send_address_str->len; \
744                                         if ((send_sock->address.af!=AF_INET) && \
745                                                         (send_address_str==&(send_sock->address_str))) \
746                                                 new_len+=2; \
747                                         if ((send_sock->port_no!=SIP_PORT) || \
748                                                         (send_port_str!=&(send_sock->port_no_str))){ \
749                                                 /* add :port_no */ \
750                                                 new_len+=1+send_port_str->len; \
751                                         }\
752                                         /*add;transport=xxx*/ \
753                                         switch(send_sock->proto){ \
754                                                 case PROTO_NONE: \
755                                                 case PROTO_UDP: \
756                                                                 break; /* udp is the default */ \
757                                                 case PROTO_TCP: \
758                                                 case PROTO_TLS: \
759                                                                 new_len+=TRANSPORT_PARAM_LEN+3; \
760                                                                 break; \
761                                                 case PROTO_SCTP: \
762                                                                 new_len+=TRANSPORT_PARAM_LEN+4; \
763                                                                 break; \
764                                                 default: \
765                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
766                                                                 send_sock->proto); \
767                                         }\
768                                         SENDCOMP_LUMP_LEN \
769                                 }else{ \
770                                         /* FIXME */ \
771                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
772                                                         " null send_sock\n"); \
773                                 }; \
774                                 break; \
775                         case SUBST_NOP: /* do nothing */ \
776                                 break; \
777                         default: \
778                                 LOG(L_CRIT, "BUG: unknown subst type %d\n", \
779                                                 (subst_l)->u.subst); \
780                 }
781         
782         if (send_info){
783                 send_sock=send_info->send_sock;
784         }else{
785                 send_sock=0;
786         };
787         s_offset=0;
788         new_len=0;
789         /* init send_address_str & send_port_str */
790         if(send_sock && send_sock->useinfo.name.len>0)
791                 send_address_str=&(send_sock->useinfo.name);
792         else if (msg->set_global_address.len)
793                 send_address_str=&(msg->set_global_address);
794         else
795                 send_address_str=&(send_sock->address_str);
796         if(send_sock && send_sock->useinfo.port_no>0)
797                 send_port_str=&(send_sock->useinfo.port_no_str);
798         else if (msg->set_global_port.len)
799                 send_port_str=&(msg->set_global_port);
800         else
801                 send_port_str=&(send_sock->port_no_str);
802         /* init recv_address_str, recv_port_str & recv_port_no */
803         if(msg->rcv.bind_address) {
804                 if(msg->rcv.bind_address->useinfo.name.len>0)
805                         recv_address_str=&(msg->rcv.bind_address->useinfo.name);
806                 else
807                         recv_address_str=&(msg->rcv.bind_address->address_str);
808                 if(msg->rcv.bind_address->useinfo.port_no>0) {
809                         recv_port_str=&(msg->rcv.bind_address->useinfo.port_no_str);
810                         recv_port_no = msg->rcv.bind_address->useinfo.port_no;
811                 } else {
812                         recv_port_str=&(msg->rcv.bind_address->port_no_str);
813                         recv_port_no = msg->rcv.bind_address->port_no;
814                 }
815         }
816
817         for(t=lumps;t;t=t->next){
818                 /* skip if this is an OPT lump and the condition is not satisfied */
819                 if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t, msg, send_info))
820                         continue;
821                 for(r=t->before;r;r=r->before){
822                         switch(r->op){
823                                 case LUMP_ADD:
824                                         new_len+=r->len;
825                                         break;
826                                 case LUMP_ADD_SUBST:
827                                         SUBST_LUMP_LEN(r);
828                                         break;
829                                 case LUMP_ADD_OPT:
830                                         /* skip if this is an OPT lump and the condition is
831                                          * not satisfied */
832                                         if (!lump_check_opt(r, msg, send_info))
833                                                 goto skip_before;
834                                         break;
835                                 default:
836                                         /* only ADD allowed for before/after */
837                                                 LOG(L_CRIT, "BUG: lumps_len: invalid op "
838                                                         "for data lump (%x)\n", r->op);
839                         }
840                 }
841 skip_before:
842                 switch(t->op){
843                         case LUMP_ADD:
844                                 new_len+=t->len;
845                                 break;
846                         case LUMP_ADD_SUBST:
847                                 SUBST_LUMP_LEN(t);
848                                 break;
849                         case LUMP_ADD_OPT:
850                                 /* we don't do anything here, it's only a condition for
851                                  * before & after */
852                                 break;
853                         case LUMP_DEL:
854                                 /* fix overlapping deleted zones */
855                                 if (t->u.offset < s_offset){
856                                         /* change len */
857                                         if (t->len>s_offset-t->u.offset)
858                                                         t->len-=s_offset-t->u.offset;
859                                         else t->len=0;
860                                         t->u.offset=s_offset;
861                                 }
862                                 s_offset=t->u.offset+t->len;
863                                 new_len-=t->len;
864                                 break;
865                         case LUMP_NOP:
866                                 /* fix offset if overlapping on a deleted zone */
867                                 if (t->u.offset < s_offset){
868                                         t->u.offset=s_offset;
869                                 }else
870                                         s_offset=t->u.offset;
871                                 /* do nothing */
872                                 break;
873                         default:
874                                 LOG(L_CRIT,"BUG:lumps_len: invalid"
875                                                         " op for data lump (%x)\n", r->op);
876                 }
877                 for (r=t->after;r;r=r->after){
878                         switch(r->op){
879                                 case LUMP_ADD:
880                                         new_len+=r->len;
881                                         break;
882                                 case LUMP_ADD_SUBST:
883                                         SUBST_LUMP_LEN(r);
884                                         break;
885                                 case LUMP_ADD_OPT:
886                                         /* skip if this is an OPT lump and the condition is
887                                          * not satisfied */
888                                         if (!lump_check_opt(r, msg, send_info))
889                                                 goto skip_after;
890                                         break;
891                                 default:
892                                         /* only ADD allowed for before/after */
893                                         LOG(L_CRIT, "BUG:lumps_len: invalid"
894                                                                 " op for data lump (%x)\n", r->op);
895                         }
896                 }
897 skip_after:
898                 ; /* to make gcc 3.* happy */
899         }
900         return new_len;
901 #undef RCVCOMP_LUMP_LEN
902 #undef SENDCOMP_LUMP_LEN
903 }
904
905
906
907 /* another helper functions, adds/Removes the lump,
908         code moved form build_req_from_req  */
909
910 static inline void process_lumps(       struct sip_msg* msg,
911                                                                         struct lump* lumps,
912                                                                         char* new_buf,
913                                                                         unsigned int* new_buf_offs,
914                                                                         unsigned int* orig_offs,
915                                                                         struct dest_info* send_info)
916 {
917         struct lump *t;
918         struct lump *r;
919         char* orig;
920         int size;
921         int offset;
922         int s_offset;
923         str* send_address_str;
924         str* send_port_str;
925         str* recv_address_str = NULL;
926         str* recv_port_str = NULL;
927         int  recv_port_no = 0;
928         struct socket_info* send_sock;
929
930 #ifdef USE_COMP
931         #define RCVCOMP_PARAM_ADD \
932                                 /* add ;comp=xxxx */ \
933                                 switch(msg->rcv.comp){ \
934                                         case COMP_NONE: \
935                                                 break; \
936                                         case COMP_SIGCOMP: \
937                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
938                                                 offset+=COMP_PARAM_LEN; \
939                                                 memcpy(new_buf+offset, SIGCOMP_NAME, \
940                                                                 SIGCOMP_NAME_LEN); \
941                                                 offset+=SIGCOMP_NAME_LEN; \
942                                                 break; \
943                                         case COMP_SERGZ: \
944                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
945                                                 offset+=COMP_PARAM_LEN; \
946                                                 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
947                                                 offset+=SERGZ_NAME_LEN; \
948                                                 break;\
949                                         default:\
950                                                 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
951                                                                 msg->rcv.comp); \
952                                 }
953         
954         #define SENDCOMP_PARAM_ADD \
955                                 /* add ;comp=xxxx */ \
956                                 switch(send_info->comp){ \
957                                         case COMP_NONE: \
958                                                 break; \
959                                         case COMP_SIGCOMP: \
960                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
961                                                 offset+=COMP_PARAM_LEN; \
962                                                 memcpy(new_buf+offset, SIGCOMP_NAME, \
963                                                                 SIGCOMP_NAME_LEN); \
964                                                 offset+=SIGCOMP_NAME_LEN; \
965                                                 break; \
966                                         case COMP_SERGZ: \
967                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
968                                                 offset+=COMP_PARAM_LEN; \
969                                                 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
970                                                 offset+=SERGZ_NAME_LEN; \
971                                                 break;\
972                                         default:\
973                                                 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
974                                                                 msg->rcv.comp); \
975                                 } 
976 #else
977         #define RCVCOMP_PARAM_ADD
978         #define SENDCOMP_PARAM_ADD
979 #endif /* USE_COMP */
980
981 #define SUBST_LUMP(subst_l) \
982         switch((subst_l)->u.subst){ \
983                 case SUBST_RCV_IP: \
984                         if (msg->rcv.bind_address){  \
985                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
986                                         new_buf[offset]='['; offset++; \
987                                 }\
988                                 memcpy(new_buf+offset, recv_address_str->s, \
989                                                 recv_address_str->len); \
990                                 offset+=recv_address_str->len; \
991                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
992                                         new_buf[offset]=']'; offset++; \
993                                 }\
994                         }else{  \
995                                 /*FIXME*/ \
996                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
997                         }; \
998                         break; \
999                 case SUBST_RCV_PORT: \
1000                         if (msg->rcv.bind_address){  \
1001                                 memcpy(new_buf+offset, recv_port_str->s, \
1002                                                 recv_port_str->len); \
1003                                 offset+=recv_port_str->len; \
1004                         }else{  \
1005                                 /*FIXME*/ \
1006                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1007                         }; \
1008                         break; \
1009                 case SUBST_RCV_ALL: \
1010                         if (msg->rcv.bind_address){  \
1011                                 /* address */ \
1012                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
1013                                         new_buf[offset]='['; offset++; \
1014                                 }\
1015                                 memcpy(new_buf+offset, recv_address_str->s, \
1016                                                 recv_address_str->len); \
1017                                 offset+=recv_address_str->len; \
1018                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
1019                                         new_buf[offset]=']'; offset++; \
1020                                 }\
1021                                 /* :port */ \
1022                                 if (recv_port_no!=SIP_PORT){ \
1023                                         new_buf[offset]=':'; offset++; \
1024                                         memcpy(new_buf+offset, \
1025                                                         recv_port_str->s, \
1026                                                         recv_port_str->len); \
1027                                         offset+=recv_port_str->len; \
1028                                 }\
1029                                 switch(msg->rcv.bind_address->proto){ \
1030                                         case PROTO_NONE: \
1031                                         case PROTO_UDP: \
1032                                                 break; /* nothing to do, udp is default*/ \
1033                                         case PROTO_TCP: \
1034                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1035                                                                 TRANSPORT_PARAM_LEN); \
1036                                                 offset+=TRANSPORT_PARAM_LEN; \
1037                                                 memcpy(new_buf+offset, "tcp", 3); \
1038                                                 offset+=3; \
1039                                                 break; \
1040                                         case PROTO_TLS: \
1041                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1042                                                                 TRANSPORT_PARAM_LEN); \
1043                                                 offset+=TRANSPORT_PARAM_LEN; \
1044                                                 memcpy(new_buf+offset, "tls", 3); \
1045                                                 offset+=3; \
1046                                                 break; \
1047                                         case PROTO_SCTP: \
1048                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1049                                                                 TRANSPORT_PARAM_LEN); \
1050                                                 offset+=TRANSPORT_PARAM_LEN; \
1051                                                 memcpy(new_buf+offset, "sctp", 4); \
1052                                                 offset+=4; \
1053                                                 break; \
1054                                         default: \
1055                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1056                                                                 msg->rcv.bind_address->proto); \
1057                                 } \
1058                                 RCVCOMP_PARAM_ADD \
1059                         }else{  \
1060                                 /*FIXME*/ \
1061                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1062                         }; \
1063                         break; \
1064                 case SUBST_SND_IP: \
1065                         if (send_sock){  \
1066                                 if ((send_sock->address.af!=AF_INET) && \
1067                                                 (send_address_str==&(send_sock->address_str))){\
1068                                         new_buf[offset]='['; offset++; \
1069                                 }\
1070                                 memcpy(new_buf+offset, send_address_str->s, \
1071                                                                         send_address_str->len); \
1072                                 offset+=send_address_str->len; \
1073                                 if ((send_sock->address.af!=AF_INET) && \
1074                                                 (send_address_str==&(send_sock->address_str))){\
1075                                         new_buf[offset]=']'; offset++; \
1076                                 }\
1077                         }else{  \
1078                                 /*FIXME*/ \
1079                                 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1080                                                         " null send_sock\n"); \
1081                         }; \
1082                         break; \
1083                 case SUBST_SND_PORT: \
1084                         if (send_sock){  \
1085                                 memcpy(new_buf+offset, send_port_str->s, \
1086                                                                         send_port_str->len); \
1087                                 offset+=send_port_str->len; \
1088                         }else{  \
1089                                 /*FIXME*/ \
1090                                 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1091                                                 " null send_sock\n"); \
1092                         }; \
1093                         break; \
1094                 case SUBST_SND_ALL: \
1095                         if (send_sock){  \
1096                                 /* address */ \
1097                                 if ((send_sock->address.af!=AF_INET) && \
1098                                                 (send_address_str==&(send_sock->address_str))){\
1099                                         new_buf[offset]='['; offset++; \
1100                                 }\
1101                                 memcpy(new_buf+offset, send_address_str->s, \
1102                                                 send_address_str->len); \
1103                                 offset+=send_address_str->len; \
1104                                 if ((send_sock->address.af!=AF_INET) && \
1105                                                 (send_address_str==&(send_sock->address_str))){\
1106                                         new_buf[offset]=']'; offset++; \
1107                                 }\
1108                                 /* :port */ \
1109                                 if ((send_sock->port_no!=SIP_PORT) || \
1110                                         (send_port_str!=&(send_sock->port_no_str))){ \
1111                                         new_buf[offset]=':'; offset++; \
1112                                         memcpy(new_buf+offset, send_port_str->s, \
1113                                                         send_port_str->len); \
1114                                         offset+=send_port_str->len; \
1115                                 }\
1116                                 switch(send_sock->proto){ \
1117                                         case PROTO_NONE: \
1118                                         case PROTO_UDP: \
1119                                                 break; /* nothing to do, udp is default*/ \
1120                                         case PROTO_TCP: \
1121                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1122                                                                 TRANSPORT_PARAM_LEN); \
1123                                                 offset+=TRANSPORT_PARAM_LEN; \
1124                                                 memcpy(new_buf+offset, "tcp", 3); \
1125                                                 offset+=3; \
1126                                                 break; \
1127                                         case PROTO_TLS: \
1128                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1129                                                                 TRANSPORT_PARAM_LEN); \
1130                                                 offset+=TRANSPORT_PARAM_LEN; \
1131                                                 memcpy(new_buf+offset, "tls", 3); \
1132                                                 offset+=3; \
1133                                                 break; \
1134                                         case PROTO_SCTP: \
1135                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1136                                                                 TRANSPORT_PARAM_LEN); \
1137                                                 offset+=TRANSPORT_PARAM_LEN; \
1138                                                 memcpy(new_buf+offset, "sctp", 4); \
1139                                                 offset+=4; \
1140                                                 break; \
1141                                         default: \
1142                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1143                                                                 send_sock->proto); \
1144                                 } \
1145                                 SENDCOMP_PARAM_ADD \
1146                         }else{  \
1147                                 /*FIXME*/ \
1148                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1149                         }; \
1150                         break; \
1151                 case SUBST_RCV_PROTO: \
1152                         if (msg->rcv.bind_address){ \
1153                                 switch(msg->rcv.bind_address->proto){ \
1154                                         case PROTO_NONE: \
1155                                         case PROTO_UDP: \
1156                                                 memcpy(new_buf+offset, "udp", 3); \
1157                                                 offset+=3; \
1158                                                 break; \
1159                                         case PROTO_TCP: \
1160                                                 memcpy(new_buf+offset, "tcp", 3); \
1161                                                 offset+=3; \
1162                                                 break; \
1163                                         case PROTO_TLS: \
1164                                                 memcpy(new_buf+offset, "tls", 3); \
1165                                                 offset+=3; \
1166                                                 break; \
1167                                         case PROTO_SCTP: \
1168                                                 memcpy(new_buf+offset, "sctp", 4); \
1169                                                 offset+=4; \
1170                                                 break; \
1171                                         default: \
1172                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1173                                                                 msg->rcv.bind_address->proto); \
1174                                 } \
1175                         }else{  \
1176                                 /*FIXME*/ \
1177                                 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1178                                                         " send_sock \n"); \
1179                         }; \
1180                         break; \
1181                 case  SUBST_SND_PROTO: \
1182                         if (send_sock){ \
1183                                 switch(send_sock->proto){ \
1184                                         case PROTO_NONE: \
1185                                         case PROTO_UDP: \
1186                                                 memcpy(new_buf+offset, "udp", 3); \
1187                                                 offset+=3; \
1188                                                 break; \
1189                                         case PROTO_TCP: \
1190                                                 memcpy(new_buf+offset, "tcp", 3); \
1191                                                 offset+=3; \
1192                                                 break; \
1193                                         case PROTO_TLS: \
1194                                                 memcpy(new_buf+offset, "tls", 3); \
1195                                                 offset+=3; \
1196                                                 break; \
1197                                         case PROTO_SCTP: \
1198                                                 memcpy(new_buf+offset, "sctp", 4); \
1199                                                 offset+=4; \
1200                                                 break; \
1201                                         default: \
1202                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1203                                                                 send_sock->proto); \
1204                                 } \
1205                         }else{  \
1206                                 /*FIXME*/ \
1207                                 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1208                                                         " send_sock \n"); \
1209                         }; \
1210                         break; \
1211                 default: \
1212                                 LOG(L_CRIT, "BUG: process_lumps: unknown subst type %d\n", \
1213                                                         (subst_l)->u.subst); \
1214         }
1215
1216         if (send_info){
1217                 send_sock=send_info->send_sock;
1218         }else{
1219                 send_sock=0;
1220         }
1221         /* init send_address_str & send_port_str */
1222         if (msg->set_global_address.len)
1223                 send_address_str=&(msg->set_global_address);
1224         else
1225                 send_address_str=&(send_sock->address_str);
1226         if (msg->set_global_port.len)
1227                 send_port_str=&(msg->set_global_port);
1228         else
1229                 send_port_str=&(send_sock->port_no_str);
1230         /* init send_address_str & send_port_str */
1231         if(send_sock && send_sock->useinfo.name.len>0)
1232                 send_address_str=&(send_sock->useinfo.name);
1233         else if (msg->set_global_address.len)
1234                 send_address_str=&(msg->set_global_address);
1235         else
1236                 send_address_str=&(send_sock->address_str);
1237         if(send_sock && send_sock->useinfo.port_no>0)
1238                 send_port_str=&(send_sock->useinfo.port_no_str);
1239         else if (msg->set_global_port.len)
1240                 send_port_str=&(msg->set_global_port);
1241         else
1242                 send_port_str=&(send_sock->port_no_str);
1243         /* init recv_address_str, recv_port_str & recv_port_no */
1244         if(msg->rcv.bind_address) {
1245                 if(msg->rcv.bind_address->useinfo.name.len>0)
1246                         recv_address_str=&(msg->rcv.bind_address->useinfo.name);
1247                 else
1248                         recv_address_str=&(msg->rcv.bind_address->address_str);
1249                 if(msg->rcv.bind_address->useinfo.port_no>0) {
1250                         recv_port_str=&(msg->rcv.bind_address->useinfo.port_no_str);
1251                         recv_port_no = msg->rcv.bind_address->useinfo.port_no;
1252                 } else {
1253                         recv_port_str=&(msg->rcv.bind_address->port_no_str);
1254                         recv_port_no = msg->rcv.bind_address->port_no;
1255                 }
1256         }
1257
1258         orig=msg->buf;
1259         offset=*new_buf_offs;
1260         s_offset=*orig_offs;
1261
1262         for (t=lumps;t;t=t->next){
1263                 switch(t->op){
1264                         case LUMP_ADD:
1265                         case LUMP_ADD_SUBST:
1266                         case LUMP_ADD_OPT:
1267                                 /* skip if this is an OPT lump and the condition is
1268                                  * not satisfied */
1269                                 if ((t->op==LUMP_ADD_OPT) &&
1270                                                 (!lump_check_opt(t, msg, send_info)))
1271                                         continue;
1272                                 /* just add it here! */
1273                                 /* process before  */
1274                                 for(r=t->before;r;r=r->before){
1275                                         switch (r->op){
1276                                                 case LUMP_ADD:
1277                                                         /*just add it here*/
1278                                                         memcpy(new_buf+offset, r->u.value, r->len);
1279                                                         offset+=r->len;
1280                                                         break;
1281                                                 case LUMP_ADD_SUBST:
1282                                                         SUBST_LUMP(r);
1283                                                         break;
1284                                                 case LUMP_ADD_OPT:
1285                                                         /* skip if this is an OPT lump and the condition is
1286                                                         * not satisfied */
1287                                                         if (!lump_check_opt(r, msg, send_info))
1288                                                                 goto skip_before;
1289                                                         break;
1290                                                 default:
1291                                                         /* only ADD allowed for before/after */
1292                                                         LOG(L_CRIT, "BUG:process_lumps: "
1293                                                                         "invalid op for data lump (%x)\n", r->op);
1294                                         }
1295                                 }
1296 skip_before:
1297                                 /* copy "main" part */
1298                                 switch(t->op){
1299                                         case LUMP_ADD:
1300                                                 memcpy(new_buf+offset, t->u.value, t->len);
1301                                                 offset+=t->len;
1302                                                 break;
1303                                         case LUMP_ADD_SUBST:
1304                                                 SUBST_LUMP(t);
1305                                                 break;
1306                                         case LUMP_ADD_OPT:
1307                                                 /* do nothing, it's only a condition */
1308                                                 break;
1309                                         default:
1310                                                 /* should not ever get here */
1311                                                 LOG(L_CRIT, "BUG: process_lumps: unhandled data lump "
1312                                                                 " op %d\n", t->op);
1313                                 }
1314                                 /* process after */
1315                                 for(r=t->after;r;r=r->after){
1316                                         switch (r->op){
1317                                                 case LUMP_ADD:
1318                                                         /*just add it here*/
1319                                                         memcpy(new_buf+offset, r->u.value, r->len);
1320                                                         offset+=r->len;
1321                                                         break;
1322                                                 case LUMP_ADD_SUBST:
1323                                                         SUBST_LUMP(r);
1324                                                         break;
1325                                                 case LUMP_ADD_OPT:
1326                                                         /* skip if this is an OPT lump and the condition is
1327                                                         * not satisfied */
1328                                                         if (!lump_check_opt(r, msg, send_info))
1329                                                                 goto skip_after;
1330                                                         break;
1331                                                 default:
1332                                                         /* only ADD allowed for before/after */
1333                                                         LOG(L_CRIT, "BUG:process_lumps: "
1334                                                                         "invalid op for data lump (%x)\n", r->op);
1335                                         }
1336                                 }
1337 skip_after:
1338                                 break;
1339                         case LUMP_NOP:
1340                         case LUMP_DEL:
1341                                 /* copy till offset */
1342                                 if (s_offset>t->u.offset){
1343                                         DBG("Warning: (%d) overlapped lumps offsets,"
1344                                                 " ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
1345                                         /* this should've been fixed above (when computing len) */
1346                                         /* just ignore it*/
1347                                         break;
1348                                 }
1349                                 size=t->u.offset-s_offset;
1350                                 if (size){
1351                                         memcpy(new_buf+offset, orig+s_offset,size);
1352                                         offset+=size;
1353                                         s_offset+=size;
1354                                 }
1355                                 /* process before  */
1356                                 for(r=t->before;r;r=r->before){
1357                                         switch (r->op){
1358                                                 case LUMP_ADD:
1359                                                         /*just add it here*/
1360                                                         memcpy(new_buf+offset, r->u.value, r->len);
1361                                                         offset+=r->len;
1362                                                         break;
1363                                                 case LUMP_ADD_SUBST:
1364                                                         SUBST_LUMP(r);
1365                                                         break;
1366                                                 case LUMP_ADD_OPT:
1367                                                         /* skip if this is an OPT lump and the condition is
1368                                                         * not satisfied */
1369                                                         if (!lump_check_opt(r, msg, send_info))
1370                                                                 goto skip_nop_before;
1371                                                         break;
1372                                                 default:
1373                                                         /* only ADD allowed for before/after */
1374                                                         LOG(L_CRIT, "BUG:process_lumps: "
1375                                                                         "invalid op for data lump (%x)\n",r->op);
1376                                         }
1377                                 }
1378 skip_nop_before:
1379                                 /* process main (del only) */
1380                                 if (t->op==LUMP_DEL){
1381                                         /* skip len bytes from orig msg */
1382                                         s_offset+=t->len;
1383                                 }
1384                                 /* process after */
1385                                 for(r=t->after;r;r=r->after){
1386                                         switch (r->op){
1387                                                 case LUMP_ADD:
1388                                                         /*just add it here*/
1389                                                         memcpy(new_buf+offset, r->u.value, r->len);
1390                                                         offset+=r->len;
1391                                                         break;
1392                                                 case LUMP_ADD_SUBST:
1393                                                         SUBST_LUMP(r);
1394                                                         break;
1395                                                 case LUMP_ADD_OPT:
1396                                                         /* skip if this is an OPT lump and the condition is
1397                                                         * not satisfied */
1398                                                         if (!lump_check_opt(r, msg, send_info))
1399                                                                 goto skip_nop_after;
1400                                                         break;
1401                                                 default:
1402                                                         /* only ADD allowed for before/after */
1403                                                         LOG(L_CRIT, "BUG:process_lumps: "
1404                                                                         "invalid op for data lump (%x)\n", r->op);
1405                                         }
1406                                 }
1407 skip_nop_after:
1408                                 break;
1409                         default:
1410                                         LOG(L_CRIT, "BUG: process_lumps: "
1411                                                         "unknown op (%x)\n", t->op);
1412                 }
1413         }
1414         *new_buf_offs=offset;
1415         *orig_offs=s_offset;
1416 #undef RCVCOMP_PARAM_ADD 
1417 #undef SENDCOMP_PARAM_ADD
1418 }
1419
1420
1421 /*
1422  * Adjust/insert Content-Length if necessary
1423  */
1424 static inline int adjust_clen(struct sip_msg* msg, int body_delta, int proto)
1425 {
1426         struct lump* anchor;
1427         char* clen_buf;
1428         int clen_len, body_only;
1429 #ifdef USE_TCP
1430         char* body;
1431         int comp_clen;
1432 #endif /* USE_TCP */
1433
1434         /* Calculate message length difference caused by lumps modifying message
1435          * body, from this point on the message body must not be modified. Zero
1436          * value indicates that the body hasn't been modified
1437         */
1438
1439         clen_buf = 0;
1440         anchor=0;
1441         body_only=1;
1442
1443         /* check to see if we need to add clen */
1444 #ifdef USE_TCP
1445         if (proto == PROTO_TCP
1446 #ifdef USE_TLS
1447             || proto == PROTO_TLS
1448 #endif
1449             ) {
1450                 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
1451                         LOG(L_ERR, "adjust_clen: error parsing content-length\n");
1452                         goto error;
1453                 }
1454                 if (unlikely(msg->content_length==0)){
1455                         /* not present, we need to add it */
1456                         /* msg->unparsed should point just before the final crlf
1457                          * - whole message was parsed by the above parse_headers
1458                          *   which did not find content-length */
1459                         anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1460                                                                                                 HDR_CONTENTLENGTH_T);
1461                         if (anchor==0){
1462                                 LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1463                                 goto error;
1464                         }
1465                         body_only=0;
1466                 }else{
1467                         /* compute current content length and compare it with the
1468                            one in the message */
1469                         body=get_body(msg);
1470                         if (unlikely(body==0)){
1471                                 ser_error=E_BAD_REQ;
1472                                 LOG(L_ERR, "adjust_clen: no message body found"
1473                                                 " (missing crlf?)");
1474                                 goto error;
1475                         }
1476                         comp_clen=msg->len-(int)(body-msg->buf)+body_delta;
1477                         if (comp_clen!=(int)(long)msg->content_length->parsed){
1478                                 /* note: we don't distinguish here between received with
1479                                    wrong content-length and content-length changed, we just
1480                                    fix it automatically in both cases (the reason being
1481                                    that an error message telling we have received a msg-
1482                                    with wrong content-length is of very little use) */
1483                                 anchor = del_lump(msg, msg->content_length->body.s-msg->buf,
1484                                                                         msg->content_length->body.len,
1485                                                                         HDR_CONTENTLENGTH_T);
1486                                 if (anchor==0) {
1487                                         LOG(L_ERR, "adjust_clen: Can't remove original"
1488                                                                 " Content-Length\n");
1489                                         goto error;
1490                                 }
1491                                 body_only=1;
1492                         }
1493                 }
1494         }else
1495 #endif /* USE_TCP */
1496         if (body_delta){
1497                 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
1498                         LOG(L_ERR, "adjust_clen: Error parsing Content-Length\n");
1499                         goto error;
1500                 }
1501
1502                 /* The body has been changed, try to find
1503                  * existing Content-Length
1504                  */
1505                 /* no need for Content-Length if it's and UDP packet and
1506                  * it hasn't Content-Length already */
1507                 if ((msg->content_length==0)){
1508                     /* content-length doesn't exist, append it */
1509                         /* msg->unparsed should point just before the final crlf
1510                          * - whole message was parsed by the above parse_headers
1511                          *   which did not find content-length */
1512                         if (proto!=PROTO_UDP){
1513                                 anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1514                                                                                                         HDR_CONTENTLENGTH_T);
1515                                 if (anchor==0){
1516                                         LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1517                                         goto error;
1518                                 }
1519                                 body_only=0;
1520                         } /* else
1521                                 DBG("adjust_clen: UDP packet with no clen => "
1522                                                 "not adding one \n"); */
1523                 }else{
1524                         /* Content-Length has been found, remove it */
1525                         anchor = del_lump(      msg, msg->content_length->body.s - msg->buf,
1526                                                                 msg->content_length->body.len,
1527                                                                 HDR_CONTENTLENGTH_T);
1528                         if (anchor==0) {
1529                                 LOG(L_ERR, "adjust_clen: Can't remove original"
1530                                                         " Content-Length\n");
1531                                 goto error;
1532                         }
1533                 }
1534         }
1535
1536         if (anchor){
1537                 clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
1538                 if (!clen_buf) goto error;
1539                 if (insert_new_lump_after(anchor, clen_buf, clen_len,
1540                                         HDR_CONTENTLENGTH_T) == 0)
1541                         goto error;
1542         }
1543         
1544         return 0;
1545 error:
1546         if (clen_buf) pkg_free(clen_buf);
1547         return -1;
1548 }
1549
1550
1551
1552 /** builds a request in memory from another sip request.
1553   *
1554   * Side-effects: - it adds lumps to the msg which are _not_ cleaned.
1555   * The added lumps are HDR_VIA_T (almost always added), HDR_CONTENLENGTH_T
1556   * and HDR_ROUTE_T (when a Route: header is added as a result of a non-null
1557   * msg->path_vec).
1558   *               - it might change send_info->proto and send_info->send_socket
1559   *                 if proto fallback is enabled (see below).
1560   *
1561   * Uses also global_req_flags ( OR'ed with msg->msg_flags, see send_info
1562   * below).
1563   *
1564   * @param msg  - sip message structure, complete with lumps
1565   * @param returned_len - result length (filled in)
1566   * @param send_info  - dest_info structure (value/result), contains where the
1567   *                     packet will be sent to (it's needed for building a 
1568   *                     correct via, fill RR lumps a.s.o.). If MTU based
1569   *                     protocol fall-back is enabled (see flags below),
1570   *                     send_info->proto might be updated with the new
1571   *                     protocol.
1572   *                     msg->msg_flags used:
1573   *                     - FL_TCP_MTU_FB, FL_TLS_MTU_FB and FL_SCTP_MTU_FB -
1574   *                       fallback to the corresp. proto if the built 
1575   *                       message > mtu and send_info->proto==PROTO_UDP. 
1576   *                       It will also update send_info->proto.
1577   *                     - FL_FORCE_RPORT: add rport to via
1578   * @param mode - flags for building the message, can be a combination of:
1579   *                 * BUILD_NO_LOCAL_VIA - don't add a local via
1580   *                 * BUILD_NO_VIA1_UPDATE - don't update first via (rport,
1581   *                    received a.s.o)
1582   *                 * BUILD_NO_PATH - don't add a Route: header with the 
1583   *                   msg->path_vec content.
1584   *                 * BUILD_IN_SHM - build the result in shm memory
1585   *
1586   * @return pointer to the new request (pkg_malloc'ed or shm_malloc'ed,
1587   * depending on the presence of the BUILD_IN_SHM flag, needs freeing when
1588   *   done) and sets returned_len or 0 on error.
1589   */
1590 char * build_req_buf_from_sip_req( struct sip_msg* msg,
1591                                                                 unsigned int *returned_len,
1592                                                                 struct dest_info* send_info,
1593                                                                 unsigned int mode)
1594 {
1595         unsigned int len, new_len, received_len, rport_len, uri_len, via_len,
1596                                  body_delta;
1597         char* line_buf;
1598         char* received_buf;
1599         char* rport_buf;
1600         char* new_buf;
1601         char* buf;
1602         str path_buf;
1603         unsigned int offset, s_offset, size;
1604         struct lump* via_anchor;
1605         struct lump* via_lump;
1606         struct lump* via_insert_param;
1607         struct lump* path_anchor;
1608         struct lump* path_lump;
1609         str branch;
1610         unsigned int flags;
1611         unsigned int udp_mtu;
1612         struct dest_info di;
1613
1614         via_insert_param=0;
1615         uri_len=0;
1616         buf=msg->buf;
1617         len=msg->len;
1618         received_len=0;
1619         rport_len=0;
1620         new_buf=0;
1621         received_buf=0;
1622         rport_buf=0;
1623         via_anchor=0;
1624         line_buf=0;
1625         via_len=0;
1626         path_buf.s=0;
1627         path_buf.len=0;
1628
1629         flags=msg->msg_flags|global_req_flags;
1630         /* Calculate message body difference and adjust Content-Length */
1631         body_delta = lumps_len(msg, msg->body_lumps, send_info);
1632         if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
1633                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1634                                 " Content-Length\n");
1635                 goto error00;
1636         }
1637
1638         if(unlikely(mode&BUILD_NO_LOCAL_VIA))
1639                 goto after_local_via;
1640
1641         /* create the via header */
1642         branch.s=msg->add_to_branch_s;
1643         branch.len=msg->add_to_branch_len;
1644
1645         via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
1646         if (unlikely(via_anchor==0)) goto error00;
1647         line_buf = create_via_hf( &via_len, msg, send_info, &branch);
1648         if (unlikely(!line_buf)){
1649                 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
1650                                         "memory allocation failure\n");
1651                 goto error00;
1652         }
1653 after_local_via:
1654         if(unlikely(mode&BUILD_NO_VIA1_UPDATE))
1655                 goto after_update_via1;
1656         /* check if received needs to be added */
1657         if ( received_test(msg) ) {
1658                 if ((received_buf=received_builder(msg,&received_len))==0){
1659                         LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1660                                                         " received_builder failed\n");
1661                         goto error01;  /* free also line_buf */
1662                 }
1663         }
1664
1665         /* check if rport needs to be updated:
1666          *  - if FL_FORCE_RPORT is set add it (and del. any previous version)
1667          *  - if via already contains an rport add it and overwrite the previous
1668          *  rport value if present (if you don't want to overwrite the previous
1669          *  version remove the comments) */
1670         if ((flags&FL_FORCE_RPORT)||
1671                         (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1672                 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1673                         LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1674                                                         " rport_builder failed\n");
1675                         goto error01; /* free everything */
1676                 }
1677         }
1678
1679         /* find out where the offset of the first parameter that should be added
1680          * (after host:port), needed by add receive & maybe rport */
1681         if (msg->via1->params.s){
1682                         size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
1683                                                                                                                   for ';' */
1684         }else{
1685                         size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
1686                         if (msg->via1->port!=0){
1687                                 /*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
1688                                 size += msg->via1->port_str.len + 1; /* +1 for ':'*/
1689                         }
1690 #if 0
1691                         /* no longer necessary, now hots.s contains [] */
1692                 #ifdef USE_IPV6
1693                         if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
1694                 #endif
1695 #endif
1696         }
1697         /* if received needs to be added, add anchor after host and add it, or
1698          * overwrite the previous one if already present */
1699         if (received_len){
1700                 if (msg->via1->received){ /* received already present => overwrite it*/
1701                         via_insert_param=del_lump(msg,
1702                                                                 msg->via1->received->start-buf-1, /*;*/
1703                                                                 msg->via1->received->size+1, /*;*/ HDR_VIA_T);
1704                 }else if (via_insert_param==0){ /* receive not present, ok */
1705                         via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1706                                                                                         HDR_VIA_T);
1707                 }
1708                 if (via_insert_param==0) goto error02; /* free received_buf */
1709                 if (insert_new_lump_after(via_insert_param, received_buf, received_len,
1710                                         HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
1711         }
1712         /* if rport needs to be updated, delete it if present and add it's value */
1713         if (rport_len){
1714                 if (msg->via1->rport){ /* rport already present */
1715                         via_insert_param=del_lump(msg,
1716                                                                 msg->via1->rport->start-buf-1, /*';'*/
1717                                                                 msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
1718                 }else if (via_insert_param==0){ /*force rport, no rport present */
1719                         /* no rport, add it */
1720                         via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1721                                                                                         HDR_VIA_T);
1722                 }
1723                 if (via_insert_param==0) goto error03; /* free rport_buf */
1724                 if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
1725                                                                         HDR_VIA_T) ==0 )
1726                         goto error03; /* free rport_buf */
1727
1728         }
1729
1730 after_update_via1:
1731         /* add route with path content */
1732         if(unlikely(!(mode&BUILD_NO_PATH) && msg->path_vec.s &&
1733                                         msg->path_vec.len)){
1734                 path_buf.len=ROUTE_PREFIX_LEN+msg->path_vec.len+CRLF_LEN;
1735                 path_buf.s=pkg_malloc(path_buf.len+1);
1736                 if (unlikely(path_buf.s==0)){
1737                         LOG(L_ERR, "out of memory\n");
1738                         ser_error=E_OUT_OF_MEM;
1739                         goto error00;
1740                 }
1741                 memcpy(path_buf.s, ROUTE_PREFIX, ROUTE_PREFIX_LEN);
1742                 memcpy(path_buf.s+ROUTE_PREFIX_LEN, msg->path_vec.s,
1743                                         msg->path_vec.len);
1744                 memcpy(path_buf.s+ROUTE_PREFIX_LEN+msg->path_vec.len, CRLF, CRLF_LEN);
1745                 path_buf.s[path_buf.len]=0;
1746                 /* insert Route header either before the other routes
1747                    (if present & parsed), after the local via or after in front of
1748                     the first via if we don't add a local via*/
1749                 if (msg->route){
1750                         path_anchor=anchor_lump(msg, msg->route->name.s-buf, 0, 
1751                                                                         HDR_ROUTE_T);
1752                 }else if (likely(via_anchor)){
1753                         path_anchor=via_anchor;
1754                 }else if (likely(msg->via1)){
1755                         path_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, 
1756                                                                         HDR_ROUTE_T);
1757                 }else{
1758                         /* if no via1 (theoretically possible for non-sip messages,
1759                            e.g. http xmlrpc) */
1760                         path_anchor=anchor_lump(msg, msg->headers->name.s-buf, 0, 
1761                                                                         HDR_ROUTE_T);
1762                 }
1763                 if (unlikely(path_anchor==0))
1764                         goto error05;
1765                 if (unlikely((path_lump=insert_new_lump_after(path_anchor, path_buf.s,
1766                                                                                                                 path_buf.len,
1767                                                                                                                 HDR_ROUTE_T))==0))
1768                         goto error05;
1769         }
1770         /* compute new msg len and fix overlapping zones*/
1771         new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
1772 #ifdef XL_DEBUG
1773         LOG(L_ERR, "DEBUG: new_len(%d)=len(%d)+lumps_len\n", new_len, len);
1774 #endif
1775         udp_mtu=cfg_get(core, core_cfg, udp_mtu);
1776         di.proto=PROTO_NONE;
1777         if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu && 
1778                                         (flags & FL_MTU_FB_MASK) && (new_len>udp_mtu)
1779                                         && (!(mode&BUILD_NO_LOCAL_VIA)))){
1780
1781                 di=*send_info; /* copy whole struct - will be used in the Via builder */
1782                 di.proto=PROTO_NONE; /* except the proto */
1783 #ifdef USE_TCP
1784                 if (!tcp_disable && (flags & FL_MTU_TCP_FB) &&
1785                                 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TCP))){
1786                         di.proto=PROTO_TCP;
1787                 }
1788         #ifdef USE_TLS
1789                 else if (!tls_disable && (flags & FL_MTU_TLS_FB) &&
1790                                 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TLS))){
1791                         di.proto=PROTO_TLS;
1792                 }
1793         #endif /* USE_TLS */
1794 #endif /* USE_TCP */
1795 #ifdef USE_SCTP
1796         #ifdef USE_TCP
1797                 else
1798         #endif /* USE_TCP */
1799                  if (!sctp_disable && (flags & FL_MTU_SCTP_FB) &&
1800                                 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_SCTP))){
1801                         di.proto=PROTO_SCTP;
1802                  }
1803 #endif /* USE_SCTP */
1804                 
1805                 if (di.proto!=PROTO_NONE){
1806                         new_len-=via_len;
1807                         if(likely(line_buf)) pkg_free(line_buf);
1808                         line_buf = create_via_hf( &via_len, msg, &di, &branch);
1809                         if (!line_buf){
1810                                 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
1811                                                         "memory allocation failure!\n");
1812                                 goto error00;
1813                         }
1814                         new_len+=via_len;
1815                 }
1816         }
1817         /* add via header to the list */
1818         /* try to add it before msg. 1st via */
1819         /* add first via, as an anchor for second via*/
1820         if(likely(line_buf)) {
1821                 if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
1822                                                                                         HDR_VIA_T))==0)
1823                         goto error04;
1824         }
1825         if (msg->new_uri.s){
1826                 uri_len=msg->new_uri.len;
1827                 new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
1828         }
1829         if(unlikely(mode&BUILD_IN_SHM))
1830                 new_buf=(char*)shm_malloc(new_len+1);
1831         else
1832                 new_buf=(char*)pkg_malloc(new_len+1);
1833         if (new_buf==0){
1834                 ser_error=E_OUT_OF_MEM;
1835                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
1836                 goto error00;
1837         }
1838
1839         offset=s_offset=0;
1840         if (msg->new_uri.s){
1841                 /* copy message up to uri */
1842                 size=msg->first_line.u.request.uri.s-buf;
1843                 memcpy(new_buf, buf, size);
1844                 offset+=size;
1845                 s_offset+=size;
1846                 /* add our uri */
1847                 memcpy(new_buf+offset, msg->new_uri.s, uri_len);
1848                 offset+=uri_len;
1849                 s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
1850         }
1851         new_buf[new_len]=0;
1852         /* copy msg adding/removing lumps */
1853         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
1854         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info);
1855         /* copy the rest of the message */
1856         memcpy(new_buf+offset, buf+s_offset, len-s_offset);
1857         new_buf[new_len]=0;
1858
1859         /* update the send_info if udp_mtu affected */
1860         if (di.proto!=PROTO_NONE) { 
1861                 send_info->proto=di.proto;
1862                 send_info->send_sock=di.send_sock;
1863         }
1864
1865 #ifdef DBG_MSG_QA
1866         if (new_buf[new_len-1]==0) {
1867                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: 0 in the end\n");
1868                 abort();
1869         }
1870 #endif
1871
1872         *returned_len=new_len;
1873         return new_buf;
1874
1875 error01:
1876 error02:
1877         if (received_buf) pkg_free(received_buf);
1878 error03:
1879         if (rport_buf) pkg_free(rport_buf);
1880 error04:
1881         if (line_buf) pkg_free(line_buf);
1882 error05:
1883         if (path_buf.s) pkg_free(path_buf.s);
1884 error00:
1885         *returned_len=0;
1886         return 0;
1887 }
1888
1889
1890
1891 char * build_res_buf_from_sip_res( struct sip_msg* msg,
1892                                 unsigned int *returned_len)
1893 {
1894         unsigned int new_len, via_len, body_delta;
1895         char* new_buf;
1896         unsigned offset, s_offset, via_offset;
1897         char* buf;
1898         unsigned int len;
1899
1900         buf=msg->buf;
1901         len=msg->len;
1902         new_buf=0;
1903         /* we must remove the first via */
1904         if (msg->via1->next) {
1905                 via_len=msg->via1->bsize;
1906                 via_offset=msg->h_via1->body.s-buf;
1907         } else {
1908                 via_len=msg->h_via1->len;
1909                 via_offset=msg->h_via1->name.s-buf;
1910         }
1911
1912              /* Calculate message body difference and adjust
1913               * Content-Length
1914               */
1915         body_delta = lumps_len(msg, msg->body_lumps, 0);
1916         if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
1917                         < 0) {
1918                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1919                                 " Content-Length\n");
1920                 goto error;
1921         }
1922
1923         /* remove the first via*/
1924         if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
1925                 LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
1926                                         "via\n");
1927                 goto error;
1928         }
1929
1930         new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
1931                                                                                                                 know the send sock */
1932
1933         DBG(" old size: %d, new size: %d\n", len, new_len);
1934         new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
1935                                                                                          (\0 to print it )*/
1936         if (new_buf==0){
1937                 LOG(L_ERR, "ERROR: build_res_buf_from_sip_res: out of mem\n");
1938                 goto error;
1939         }
1940         new_buf[new_len]=0; /* debug: print the message */
1941         offset=s_offset=0;
1942         /*FIXME: no send sock*/
1943         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0);/*FIXME:*/
1944         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0);
1945         /* copy the rest of the message */
1946         memcpy(new_buf+offset,
1947                 buf+s_offset,
1948                 len-s_offset);
1949          /* send it! */
1950         DBG("build_res_from_sip_res: copied size: orig:%d, new: %d, rest: %d"
1951                         " msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
1952
1953         *returned_len=new_len;
1954         return new_buf;
1955 error:
1956         *returned_len=0;
1957         return 0;
1958 }
1959
1960
1961 char * build_res_buf_from_sip_req( unsigned int code, str *text ,str *new_tag,
1962                 struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
1963 {
1964         char              *buf, *p;
1965         unsigned int      len,foo;
1966         struct hdr_field  *hdr;
1967         struct lump_rpl   *lump;
1968         struct lump_rpl   *body;
1969         int               i;
1970         char*             received_buf;
1971         unsigned int      received_len;
1972         char*             rport_buf;
1973         unsigned int      rport_len;
1974         char*             warning_buf;
1975         unsigned int      warning_len;
1976         char*             content_len_buf;
1977         unsigned int      content_len_len;
1978         char *after_body;
1979         str  to_tag;
1980         char *totags;
1981
1982         body = 0;
1983         buf=0;
1984         received_buf=rport_buf=warning_buf=content_len_buf=0;
1985         received_len=rport_len=warning_len=content_len_len=0;
1986
1987         to_tag.s=0;  /* fixes gcc 4.0 warning */
1988         to_tag.len=0;
1989
1990         /* force parsing all headers -- we want to return all
1991         Via's in the reply and they may be scattered down to the
1992         end of header (non-block Vias are a really poor property
1993         of SIP :( ) */
1994         if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
1995                 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1996                         "alas, parse_headers failed\n");
1997                 goto error00;
1998         }
1999
2000         /*computes the length of the new response buffer*/
2001         len = 0;
2002
2003         /* check if received needs to be added */
2004         if (received_test(msg)) {
2005                 if ((received_buf=received_builder(msg,&received_len))==0) {
2006                         LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
2007                                 "alas, received_builder failed\n");
2008                         goto error00;
2009                 }
2010         }
2011         /* check if rport needs to be updated */
2012         if ( ((msg->msg_flags|global_req_flags)&FL_FORCE_RPORT)||
2013                 (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
2014                 if ((rport_buf=rport_builder(msg, &rport_len))==0){
2015                         LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"
2016                                                         " rport_builder failed\n");
2017                         goto error01; /* free everything */
2018                 }
2019                 if (msg->via1->rport)
2020                         len -= msg->via1->rport->size+1; /* include ';' */
2021         }
2022
2023         /* first line */
2024         len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
2025                 text->len + CRLF_LEN/*new line*/;
2026         /*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
2027         for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
2028                 switch (hdr->type) {
2029                         case HDR_TO_T:
2030                                 if (new_tag && new_tag->len) {
2031                                         to_tag=get_to(msg)->tag_value;
2032                                         if (to_tag.len )
2033                                                 len+=new_tag->len-to_tag.len;
2034                                         else
2035                                                 len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
2036                                 }
2037                                 len += hdr->len;
2038                                 break;
2039                         case HDR_VIA_T:
2040                                 /* we always add CRLF to via*/
2041                                 len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
2042                                 if (hdr==msg->h_via1) len += received_len+rport_len;
2043                                 break;
2044                         case HDR_RECORDROUTE_T:
2045                                 /* RR only for 1xx and 2xx replies */
2046                                 if (code<180 || code>=300)
2047                                         break;
2048                         case HDR_FROM_T:
2049                         case HDR_CALLID_T:
2050                         case HDR_CSEQ_T:
2051                                 /* we keep the original termination for these headers*/
2052                                 len += hdr->len;
2053                                 break;
2054                         default:
2055                                 /* do nothing, we are interested only in the above headers */
2056                                 ;
2057                 }
2058         }
2059         /* lumps length */
2060         for(lump=msg->reply_lump;lump;lump=lump->next) {
2061                 len += lump->text.len;
2062                 if (lump->flags&LUMP_RPL_BODY)
2063                         body = lump;
2064         }
2065         /* server header */
2066         if (server_signature)
2067                 len += server_hdr.len + CRLF_LEN;
2068         /* warning hdr */
2069         if (sip_warning) {
2070                 warning_buf = warning_builder(msg,&warning_len);
2071                 if (warning_buf) len += warning_len + CRLF_LEN;
2072                 else LOG(L_WARN, "WARNING: warning skipped -- too big\n");
2073         }
2074         /* content length hdr */
2075         if (body) {
2076                 content_len_buf = int2str(body->text.len, (int*)&content_len_len);
2077                 len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
2078         } else {
2079                 len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
2080         }
2081         /* end of message */
2082         len += CRLF_LEN; /*new line*/
2083
2084         /*allocating mem*/
2085         buf = (char*) pkg_malloc( len+1 );
2086         if (!buf)
2087         {
2088                 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: out of memory "
2089                         " ; needs %d\n",len);
2090                 goto error01;
2091         }
2092
2093         /* filling the buffer*/
2094         p=buf;
2095         /* first line */
2096         memcpy( p , msg->first_line.u.request.version.s , 
2097                 msg->first_line.u.request.version.len);
2098         p += msg->first_line.u.request.version.len;
2099         *(p++) = ' ' ;
2100         /*code*/
2101         for ( i=2 , foo = code  ;  i>=0  ;  i-- , foo=foo/10 )
2102                 *(p+i) = '0' + foo - ( foo/10 )*10;
2103         p += 3;
2104         *(p++) = ' ' ;
2105         memcpy( p , text->s , text->len );
2106         p += text->len;
2107         memcpy( p, CRLF, CRLF_LEN );
2108         p+=CRLF_LEN;
2109         /* headers*/
2110         for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
2111                 switch (hdr->type)
2112                 {
2113                         case HDR_VIA_T:
2114                                 if (hdr==msg->h_via1){
2115                                         if (rport_buf){
2116                                                 if (msg->via1->rport){ /* delete the old one */
2117                                                         /* copy until rport */
2118                                                         append_str_trans( p, hdr->name.s ,
2119                                                                 msg->via1->rport->start-hdr->name.s-1,msg);
2120                                                         /* copy new rport */
2121                                                         append_str(p, rport_buf, rport_len);
2122                                                         /* copy the rest of the via */
2123                                                         append_str_trans(p, msg->via1->rport->start+
2124                                                                                                 msg->via1->rport->size,
2125                                                                                                 hdr->body.s+hdr->body.len-
2126                                                                                                 msg->via1->rport->start-
2127                                                                                                 msg->via1->rport->size, msg);
2128                                                 }else{ /* just append the new one */
2129                                                         /* normal whole via copy */
2130                                                         append_str_trans( p, hdr->name.s ,
2131                                                                 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2132                                                         append_str(p, rport_buf, rport_len);
2133                                                 }
2134                                         }else{
2135                                                 /* normal whole via copy */
2136                                                 append_str_trans( p, hdr->name.s ,
2137                                                                 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2138                                         }
2139                                         if (received_buf)
2140                                                 append_str( p, received_buf, received_len);
2141                                 }else{
2142                                         /* normal whole via copy */
2143                                         append_str_trans( p, hdr->name.s,
2144                                                         (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2145                                 }
2146                                 append_str( p, CRLF,CRLF_LEN);
2147                                 break;
2148                         case HDR_RECORDROUTE_T:
2149                                 /* RR only for 1xx and 2xx replies */
2150                                 if (code<180 || code>=300) break;
2151                                 append_str(p, hdr->name.s, hdr->len);
2152                                 break;
2153                         case HDR_TO_T:
2154                                 if (new_tag && new_tag->len){
2155                                         if (to_tag.s ) { /* replacement */
2156                                                 /* before to-tag */
2157                                                 append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
2158                                                 /* to tag replacement */
2159                                                 bmark->to_tag_val.s=p;
2160                                                 bmark->to_tag_val.len=new_tag->len;
2161                                                 append_str( p, new_tag->s,new_tag->len);
2162                                                 /* the rest after to-tag */
2163                                                 append_str( p, to_tag.s+to_tag.len,
2164                                                         hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
2165                                         }else{ /* adding a new to-tag */
2166                                                 after_body=hdr->body.s+hdr->body.len;
2167                                                 append_str( p, hdr->name.s, after_body-hdr->name.s);
2168                                                 append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
2169                                                 bmark->to_tag_val.s=p;
2170                                                 bmark->to_tag_val.len=new_tag->len;
2171                                                 append_str( p, new_tag->s,new_tag->len);
2172                                                 append_str( p, after_body,
2173                                                                                 hdr->name.s+hdr->len-after_body);
2174                                         }
2175                                         break;
2176                                 } /* no new to-tag -- proceed to 1:1 copying  */
2177                                 totags=((struct to_body*)(hdr->parsed))->tag_value.s;
2178                                 if (totags) {
2179                                         bmark->to_tag_val.s=p+(totags-hdr->name.s);
2180                                         bmark->to_tag_val.len=
2181                                                         ((struct to_body*)(hdr->parsed))->tag_value.len;
2182                                 }
2183                                 else {
2184                                         bmark->to_tag_val.len = 0;
2185                                         bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
2186                                 }
2187                                 /* no break */
2188                         case HDR_FROM_T:
2189                         case HDR_CALLID_T:
2190                         case HDR_CSEQ_T:
2191                                         append_str(p, hdr->name.s, hdr->len);
2192                                         break;
2193                         default:
2194                                 /* do nothing, we are interested only in the above headers */
2195                                 ;
2196                 } /* end switch */
2197         } /* end for */
2198         /* lumps */
2199         for(lump=msg->reply_lump;lump;lump=lump->next)
2200                 if (lump->flags&LUMP_RPL_HDR){
2201                         memcpy(p,lump->text.s,lump->text.len);
2202                         p += lump->text.len;
2203                 }
2204         /* server header */
2205         if (server_signature) {
2206                 memcpy( p, server_hdr.s, server_hdr.len );
2207                 p+=server_hdr.len;
2208                 memcpy( p, CRLF, CRLF_LEN );
2209                 p+=CRLF_LEN;
2210         }
2211         /* content_length hdr */
2212         if (content_len_len) {
2213                 append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
2214                 append_str( p, content_len_buf, content_len_len );
2215                 append_str( p, CRLF, CRLF_LEN );
2216         } else {
2217                 append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
2218         }
2219         /* warning header */
2220         if (warning_buf) {
2221                 memcpy( p, warning_buf, warning_len);
2222                 p+=warning_len;
2223                 memcpy( p, CRLF, CRLF_LEN);
2224                 p+=CRLF_LEN;
2225         }
2226         /*end of message*/
2227         memcpy( p, CRLF, CRLF_LEN );
2228         p+=CRLF_LEN;
2229         /* body */
2230         if (body) {
2231                 memcpy ( p, body->text.s, body->text.len );
2232                 p+=body->text.len;
2233         }
2234
2235         if (len!=p-buf)
2236                 LOG(L_CRIT,"BUG:build_res_from_sip_req: diff len=%d p-buf=%d\n",
2237                                         len, (int)(p-buf));
2238
2239         *(p) = 0;
2240         *returned_len = len;
2241         /* in req2reply, received_buf is not introduced to lumps and
2242            needs to be deleted here
2243         */
2244         if (received_buf) pkg_free(received_buf);
2245         if (rport_buf) pkg_free(rport_buf);
2246         return buf;
2247
2248 error01:
2249         if (received_buf) pkg_free(received_buf);
2250         if (rport_buf) pkg_free(rport_buf);
2251 error00:
2252         *returned_len=0;
2253         return 0;
2254 }
2255
2256
2257
2258 /* return number of chars printed or 0 if space exceeded;
2259    assumes buffer size of at least MAX_BRANCH_PARAM_LEN
2260  */
2261 int branch_builder( unsigned int hash_index,
2262         /* only either parameter useful */
2263         unsigned int label, char * char_v,
2264         int branch,
2265         char *branch_str, int *len )
2266 {
2267
2268         char *begin;
2269         int size;
2270
2271         /* hash id provided ... start with it */
2272         size=MAX_BRANCH_PARAM_LEN;
2273         begin=branch_str;
2274         *len=0;
2275
2276         memcpy(begin, MCOOKIE, MCOOKIE_LEN );
2277         size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
2278
2279         if (int2reverse_hex( &begin, &size, hash_index)==-1)
2280                 return 0;
2281
2282         if (size) {
2283                 *begin=BRANCH_SEPARATOR;
2284                 begin++; size--;
2285         } else return 0;
2286
2287         /* string with request's characteristic value ... use it ... */
2288         if (char_v) {
2289                 if (memcpy(begin,char_v,MD5_LEN)) {
2290                         begin+=MD5_LEN; size-=MD5_LEN;
2291                 } else return 0;
2292         } else { /* ... use the "label" value otherwise */
2293                 if (int2reverse_hex( &begin, &size, label )==-1)
2294                         return 0;
2295         }
2296
2297         if (size) {
2298                 *begin=BRANCH_SEPARATOR;
2299                 begin++; size--;
2300         } else return 0;
2301
2302         if (int2reverse_hex( &begin, &size, branch)==-1)
2303                 return 0;
2304
2305         *len=MAX_BRANCH_PARAM_LEN-size;
2306         return size;
2307
2308 }
2309
2310
2311
2312 /* uses only the send_info->send_socket, send_info->proto and 
2313  * send_info->comp (so that a send_info used for sending can be passed
2314  * to this function w/o changes and the correct via will be built) */
2315 char* via_builder( unsigned int *len,
2316         struct dest_info* send_info /* where to send the reply */,
2317         str* branch, str* extra_params, struct hostport* hp)
2318 {
2319         unsigned int  via_len, extra_len;
2320         char               *line_buf;
2321         int max_len;
2322         int via_prefix_len;
2323         str* address_str; /* address displayed in via */
2324         str* port_str; /* port no displayed in via */
2325         struct socket_info* send_sock;
2326         int comp_len, comp_name_len;
2327 #ifdef USE_COMP
2328         char* comp_name;
2329 #endif /* USE_COMP */
2330
2331         send_sock=send_info->send_sock;
2332         /* use pre-set address in via, the outbound socket alias or address one */
2333         if (hp && hp->host->len)
2334                 address_str=hp->host;
2335         else if(send_sock->useinfo.name.len>0)
2336                 address_str=&(send_sock->useinfo.name);
2337         else
2338                 address_str=&(send_sock->address_str);
2339         if (hp && hp->port->len)
2340                 port_str=hp->port;
2341         else if(send_sock->useinfo.port_no>0)
2342                 port_str=&(send_sock->useinfo.port_no_str);
2343         else
2344                 port_str=&(send_sock->port_no_str);
2345         
2346         comp_len=comp_name_len=0;
2347 #ifdef USE_COMP
2348         comp_name=0;
2349         switch(send_info->comp){
2350                 case COMP_NONE:
2351                         break;
2352                 case COMP_SIGCOMP:
2353                         comp_len=COMP_PARAM_LEN;
2354                         comp_name_len=SIGCOMP_NAME_LEN;
2355                         comp_name=SIGCOMP_NAME;
2356                         break;
2357                 case COMP_SERGZ:
2358                         comp_len=COMP_PARAM_LEN;
2359                         comp_name_len=SERGZ_NAME_LEN;
2360                         comp_name=SERGZ_NAME;
2361                         break;
2362                 default:
2363                         LOG(L_CRIT, "BUG: via_builder: unknown comp %d\n",
2364                                         send_info->comp);
2365                         /* continue, we'll just ignore comp */
2366         }
2367 #endif /* USE_COMP */
2368                         
2369         via_prefix_len=MY_VIA_LEN+(send_info->proto==PROTO_SCTP);
2370         max_len=via_prefix_len +address_str->len /* space in MY_VIA */
2371                 +2 /* just in case it is a v6 address ... [ ] */
2372                 +1 /*':'*/+port_str->len
2373                 +(branch?(MY_BRANCH_LEN+branch->len):0)
2374                 +(extra_params?extra_params->len:0)
2375                 +comp_len+comp_name_len
2376                 +CRLF_LEN+1;
2377         line_buf=pkg_malloc( max_len );
2378         if (line_buf==0){
2379                 ser_error=E_OUT_OF_MEM;
2380                 LOG(L_ERR, "ERROR: via_builder: out of memory\n");
2381                 return 0;
2382         }
2383
2384         extra_len=0;
2385
2386         via_len=via_prefix_len+address_str->len; /*space included in MY_VIA*/
2387
2388         memcpy(line_buf, MY_VIA, MY_VIA_LEN);
2389         if (send_info->proto==PROTO_UDP){
2390                 /* do nothing */
2391         }else if (send_info->proto==PROTO_TCP){
2392                 memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
2393         }else if (send_info->proto==PROTO_TLS){
2394                 memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
2395         }else if (send_info->proto==PROTO_SCTP){
2396                 memcpy(line_buf+MY_VIA_LEN-4, "SCTP ", 5);
2397         }else{
2398                 LOG(L_CRIT, "BUG: via_builder: unknown proto %d\n", send_info->proto);
2399                 return 0;
2400         }
2401 #       ifdef USE_IPV6
2402         /* add [] only if ipv6 and outbound socket address is used;
2403          * if using pre-set no check is made */
2404         if ((send_sock->address.af==AF_INET6) &&
2405                 (address_str==&(send_sock->address_str))) {
2406                 line_buf[via_prefix_len]='[';
2407                 line_buf[via_prefix_len+1+address_str->len]=']';
2408                 extra_len=1;
2409                 via_len+=2; /* [ ]*/
2410         }
2411 #       endif
2412         memcpy(line_buf+via_prefix_len+extra_len, address_str->s,
2413                                 address_str->len);
2414         if ((send_sock->port_no!=SIP_PORT) ||
2415                                 (port_str!=&send_sock->port_no_str)){
2416                 line_buf[via_len]=':'; via_len++;
2417                 memcpy(line_buf+via_len, port_str->s, port_str->len);
2418                 via_len+=port_str->len;
2419         }
2420
2421         /* branch parameter */
2422         if (branch){
2423                 memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
2424                 via_len+=MY_BRANCH_LEN;
2425                 memcpy(line_buf+via_len, branch->s, branch->len );
2426                 via_len+=branch->len;
2427         }
2428         /* extra params  */
2429         if (extra_params){
2430                 memcpy(line_buf+via_len, extra_params->s, extra_params->len);
2431                 via_len+=extra_params->len;
2432         }
2433 #ifdef USE_COMP
2434         /* comp */
2435         if (comp_len){
2436                 memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
2437                 via_len+=COMP_PARAM_LEN;
2438                 memcpy(line_buf+via_len, comp_name, comp_name_len);
2439                 via_len+=comp_name_len;
2440         }
2441 #endif
2442
2443         memcpy(line_buf+via_len, CRLF, CRLF_LEN);
2444         via_len+=CRLF_LEN;
2445         line_buf[via_len]=0; /* null terminate the string*/
2446
2447         *len = via_len;
2448         return line_buf;
2449 }
2450
2451 /* creates a via header honoring the protocol of the incomming socket
2452  * msg is an optional parameter */
2453 char* create_via_hf( unsigned int *len,
2454         struct sip_msg *msg,
2455         struct dest_info* send_info /* where to send the reply */,
2456         str* branch)
2457 {
2458         char* via;
2459         str extra_params;
2460         struct hostport hp;
2461 #if defined USE_TCP || defined USE_SCTP
2462         char* id_buf;
2463         unsigned int id_len;
2464
2465
2466         id_buf=0;
2467         id_len=0;
2468 #endif
2469         extra_params.len=0;
2470         extra_params.s=0;
2471
2472
2473 #if defined USE_TCP || defined USE_SCTP
2474         /* add id if tcp */
2475         if (msg && (
2476 #ifdef USE_TCP
2477                 (msg->rcv.proto==PROTO_TCP)
2478 #ifdef USE_TLS
2479                         || (msg->rcv.proto==PROTO_TLS)
2480 #endif
2481 #ifdef USE_SCTP
2482                         ||
2483 #endif /* USE_SCTP */
2484 #endif /* USE_TCP */
2485 #ifdef USE_SCTP
2486                         (msg->rcv.proto==PROTO_SCTP)
2487 #endif /* USE_SCTP */
2488                         )){
2489                 if  ((id_buf=id_builder(msg, &id_len))==0){
2490                         LOG(L_ERR, "ERROR: create_via_hf:"
2491                                                         " id_builder failed\n");
2492                         return 0; /* we don't need to free anything,
2493                                          nothing alloc'ed yet*/
2494                 }
2495                 DBG("create_via_hf: id added: <%.*s>, rcv proto=%d\n",
2496                                 (int)id_len, id_buf, msg->rcv.proto);
2497                 extra_params.s=id_buf;
2498                 extra_params.len=id_len;
2499         }
2500 #endif /* USE_TCP || USE_SCTP */
2501
2502         /* test and add rport parameter to local via - rfc3581 */
2503         if(msg->msg_flags&FL_ADD_LOCAL_RPORT) {
2504                 /* params so far + ';rport' + '\0' */
2505                 via = (char*)pkg_malloc(extra_params.len+RPORT_LEN);
2506                 if(via==0) {
2507                         LM_ERR("building local rport via param failed\n");
2508                         if (extra_params.s) pkg_free(extra_params.s);
2509                         return 0;
2510                 }
2511                 if(extra_params.len!=0) {
2512                         memcpy(via, extra_params.s, extra_params.len);
2513                         pkg_free(extra_params.s);
2514                 }
2515                 memcpy(via + extra_params.len, RPORT, RPORT_LEN-1);
2516                 extra_params.s = via;
2517                 extra_params.len += RPORT_LEN-1;
2518                 extra_params.s[extra_params.len]='\0';
2519         }
2520
2521         set_hostport(&hp, msg);
2522         via = via_builder( len, send_info, branch,
2523                                                         extra_params.len?&extra_params:0, &hp);
2524
2525         /* we do not need extra_params any more, already in the new via header */
2526         if (extra_params.s) pkg_free(extra_params.s);
2527         return via;
2528 }
2529
2530 /* builds a char* buffer from message headers without body
2531  * first line is excluded in case of skip_first_line=1
2532  * error is set -1 if the memory allocation failes
2533  */
2534 char * build_only_headers( struct sip_msg* msg, int skip_first_line,
2535                                 unsigned int *returned_len,
2536                                 int *error,
2537                                 struct dest_info* send_info)
2538 {
2539         char            *buf, *new_buf;
2540         unsigned int    offset, s_offset, len, new_len;
2541
2542         *error = 0;
2543         buf = msg->buf;
2544         if (skip_first_line)
2545                 s_offset = msg->headers->name.s - buf;
2546         else
2547                 s_offset = 0;
2548
2549         /* original length without body, and without final \r\n */
2550         len = msg->unparsed - buf;
2551         /* new msg length */
2552         new_len =       len - /* original length  */
2553                         s_offset + /* skipped first line */
2554                         lumps_len(msg, msg->add_rm, send_info); /* lumps */
2555
2556         if (new_len == 0) {
2557                 *returned_len = 0;
2558                 return 0;
2559         }
2560
2561         new_buf = (char *)pkg_malloc(new_len+1);
2562         if (!new_buf) {
2563                 LOG(L_ERR, "ERROR: build_only_headers: Not enough memory\n");
2564                 *error = -1;
2565                 return 0;
2566         }
2567         new_buf[0] = 0;
2568         offset = 0;
2569
2570         /* copy message lumps */
2571         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2572         /* copy the rest of the message without body */
2573         if (len > s_offset) {
2574                 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2575                 offset += (len-s_offset);
2576         }
2577         new_buf[offset] = 0;
2578
2579         *returned_len = offset;
2580         return new_buf;
2581 }
2582
2583 /* builds a char* buffer from message body
2584  * error is set -1 if the memory allocation failes
2585  */
2586 char * build_body( struct sip_msg* msg,
2587                         unsigned int *returned_len,
2588                         int *error,
2589                         struct dest_info* send_info)
2590 {
2591         char            *buf, *new_buf, *body;
2592         unsigned int    offset, s_offset, len, new_len;
2593
2594         *error = 0;
2595         body = get_body(msg);
2596
2597         if (!body || (body[0] == 0)) {
2598                 *returned_len = 0;
2599                 return 0;
2600         }
2601         buf = msg->buf;
2602         s_offset = body - buf;
2603
2604         /* original length of msg with body */
2605         len = msg->len;
2606         /* new body length */
2607         new_len =       len - /* original length  */
2608                         s_offset + /* msg without body */
2609                         lumps_len(msg, msg->body_lumps, send_info); /* lumps */
2610
2611         new_buf = (char *)pkg_malloc(new_len+1);
2612         if (!new_buf) {
2613                 LOG(L_ERR, "ERROR: build_body: Not enough memory\n");
2614                 *error = -1;
2615                 return 0;
2616         }
2617         new_buf[0] = 0;
2618         offset = 0;
2619
2620         /* copy body lumps */
2621         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2622         /* copy the rest of the message without body */
2623         if (len > s_offset) {
2624                 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2625                 offset += (len-s_offset);
2626         }
2627         new_buf[offset] = 0;
2628
2629         *returned_len = offset;
2630         return new_buf;
2631 }
2632
2633 /* builds a char* buffer from SIP message including body
2634  * The function adjusts the Content-Length HF according
2635  * to body lumps in case of touch_clen=1.
2636  */
2637 char * build_all( struct sip_msg* msg, int touch_clen,
2638                         unsigned int *returned_len,
2639                         int *error,
2640                         struct dest_info* send_info)
2641 {
2642         char            *buf, *new_buf;
2643         unsigned int    offset, s_offset, len, new_len;
2644         unsigned int    body_delta;
2645
2646         *error = 0;
2647         /* Calculate message body difference */
2648         body_delta = lumps_len(msg, msg->body_lumps, send_info);
2649         if (touch_clen) {
2650                 /* adjust Content-Length */
2651                 if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
2652                         LOG(L_ERR, "ERROR: build_all: Error while adjusting"
2653                                         " Content-Length\n");
2654                         *error = -1;
2655                         return 0;
2656                 }
2657         }
2658
2659         buf = msg->buf;
2660         /* original msg length */
2661         len = msg->len;
2662         /* new msg length */
2663         new_len =       len + /* original length  */
2664                         lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
2665                         body_delta; /* body lumps */
2666
2667         if (new_len == 0) {
2668                 returned_len = 0;
2669                 return 0;
2670         }
2671
2672         new_buf = (char *)pkg_malloc(new_len+1);
2673         if (!new_buf) {
2674                 LOG(L_ERR, "ERROR: build_all: Not enough memory\n");
2675                 *error = -1;
2676                 return 0;
2677         }
2678         new_buf[0] = 0;
2679         offset = s_offset = 0;
2680
2681         /* copy message lumps */
2682         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2683         /* copy body lumps */
2684         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2685         /* copy the rest of the message */
2686         memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2687         offset += (len-s_offset);
2688         new_buf[offset] = 0;
2689
2690         *returned_len = offset;
2691         return new_buf; 
2692 }
2693
2694
2695
2696 /**
2697  * parse buf in msg and fill several fields
2698  */
2699 int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len,
2700                 unsigned int id)
2701 {
2702         if(msg==0 || buf==0)
2703                 return -1;
2704
2705         memset(msg, 0, sizeof(sip_msg_t));
2706         msg->id = id;
2707         msg->buf = buf;
2708         msg->len = len;
2709         if (parse_msg(buf, len, msg)!=0) {
2710                 LM_ERR("parsing failed");
2711                 return -1;
2712         }
2713         msg->set_global_address=default_global_address;
2714         msg->set_global_port=default_global_port;
2715         return 0;
2716 }
2717