pkg/kamailio/fedora/16: Updated rel in .spec to dev2
[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                                                         new_len+=3; \
655                                                         break; \
656                                                 case PROTO_TCP: \
657                                                 case PROTO_TLS: \
658                                                         switch(msg->rcv.proto){ \
659                                                                 case PROTO_WS: \
660                                                                 case PROTO_WSS: \
661                                                                         new_len+=2; \
662                                                                         break; \
663                                                                 default: \
664                                                                         new_len+=3; \
665                                                                         break; \
666                                                         } \
667                                                         break; \
668                                                 case PROTO_SCTP: \
669                                                         new_len+=4; \
670                                                         break; \
671                                                 default: \
672                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
673                                                                 msg->rcv.bind_address->proto); \
674                                         }\
675                                 }else{ \
676                                         /* FIXME */ \
677                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
678                                 }; \
679                                 break; \
680                         case SUBST_RCV_ALL: \
681                                 if (msg->rcv.bind_address){ \
682                                         new_len+=recv_address_str->len; \
683                                         if (msg->rcv.bind_address->address.af!=AF_INET) \
684                                                 new_len+=2; \
685                                         if (recv_port_no!=SIP_PORT){ \
686                                                 /* add :port_no */ \
687                                                 new_len+=1+recv_port_str->len; \
688                                         }\
689                                                 /*add;transport=xxx*/ \
690                                         switch(msg->rcv.bind_address->proto){ \
691                                                 case PROTO_NONE: \
692                                                 case PROTO_UDP: \
693                                                         break; /* udp is the default */ \
694                                                 case PROTO_TCP: \
695                                                 case PROTO_TLS: \
696                                                         switch(msg->rcv.proto){ \
697                                                                 case PROTO_WS: \
698                                                                 case PROTO_WSS: \
699                                                                         new_len+=TRANSPORT_PARAM_LEN+2; \
700                                                                         break; \
701                                                                 default: \
702                                                                         new_len+=TRANSPORT_PARAM_LEN+3; \
703                                                                         break; \
704                                                         } \
705                                                         break; \
706                                                 case PROTO_SCTP: \
707                                                         new_len+=TRANSPORT_PARAM_LEN+4; \
708                                                         break; \
709                                                 default: \
710                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
711                                                                 msg->rcv.bind_address->proto); \
712                                         }\
713                                         RCVCOMP_LUMP_LEN \
714                                 }else{ \
715                                         /* FIXME */ \
716                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
717                                 }; \
718                                 break; \
719                         case SUBST_SND_IP: \
720                                 if (send_sock){ \
721                                         new_len+=send_address_str->len; \
722                                         if (send_sock->address.af!=AF_INET && \
723                                                         send_address_str==&(send_sock->address_str)) \
724                                                 new_len+=2; \
725                                 }else{ \
726                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
727                                                         " null send_sock\n"); \
728                                 }; \
729                                 break; \
730                         case SUBST_SND_PORT: \
731                                 if (send_sock){ \
732                                         new_len+=send_port_str->len; \
733                                 }else{ \
734                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
735                                                         " null send_sock\n"); \
736                                 }; \
737                                 break; \
738                         case SUBST_SND_PROTO: \
739                                 if (send_sock){ \
740                                         switch(send_sock->proto){ \
741                                                 case PROTO_NONE: \
742                                                 case PROTO_UDP: \
743                                                         new_len+=3; \
744                                                         break; \
745                                                 case PROTO_TCP: \
746                                                 case PROTO_TLS: \
747                                                         switch(send_info->proto){ \
748                                                                 case PROTO_WS: \
749                                                                 case PROTO_WSS: \
750                                                                         new_len+=2; \
751                                                                         break; \
752                                                                 default: \
753                                                                         new_len+=3; \
754                                                                         break; \
755                                                         } \
756                                                         break; \
757                                                 case PROTO_SCTP: \
758                                                         new_len+=4; \
759                                                         break; \
760                                                 default: \
761                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
762                                                                 send_sock->proto); \
763                                         }\
764                                 }else{ \
765                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
766                                                         " null send_sock\n"); \
767                                 }; \
768                                 break; \
769                         case SUBST_SND_ALL: \
770                                 if (send_sock){ \
771                                         new_len+=send_address_str->len; \
772                                         if ((send_sock->address.af!=AF_INET) && \
773                                                         (send_address_str==&(send_sock->address_str))) \
774                                                 new_len+=2; \
775                                         if ((send_sock->port_no!=SIP_PORT) || \
776                                                         (send_port_str!=&(send_sock->port_no_str))){ \
777                                                 /* add :port_no */ \
778                                                 new_len+=1+send_port_str->len; \
779                                         }\
780                                         /*add;transport=xxx*/ \
781                                         switch(send_sock->proto){ \
782                                                 case PROTO_NONE: \
783                                                 case PROTO_UDP: \
784                                                         break; /* udp is the default */ \
785                                                 case PROTO_TCP: \
786                                                 case PROTO_TLS: \
787                                                         switch(send_info->proto){ \
788                                                                 case PROTO_WS: \
789                                                                 case PROTO_WSS: \
790                                                                         new_len+=TRANSPORT_PARAM_LEN+2; \
791                                                                         break; \
792                                                                 default: \
793                                                                         new_len+=TRANSPORT_PARAM_LEN+3; \
794                                                                         break; \
795                                                         } \
796                                                         break; \
797                                                 case PROTO_SCTP: \
798                                                         new_len+=TRANSPORT_PARAM_LEN+4; \
799                                                         break; \
800                                                 default: \
801                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
802                                                                 send_sock->proto); \
803                                         }\
804                                         SENDCOMP_LUMP_LEN \
805                                 }else{ \
806                                         /* FIXME */ \
807                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
808                                                         " null send_sock\n"); \
809                                 }; \
810                                 break; \
811                         case SUBST_NOP: /* do nothing */ \
812                                 break; \
813                         default: \
814                                 LOG(L_CRIT, "BUG: unknown subst type %d\n", \
815                                                 (subst_l)->u.subst); \
816                 }
817         
818         if (send_info){
819                 send_sock=send_info->send_sock;
820         }else{
821                 send_sock=0;
822         };
823         s_offset=0;
824         new_len=0;
825         /* init send_address_str & send_port_str */
826         if(send_sock && send_sock->useinfo.name.len>0)
827                 send_address_str=&(send_sock->useinfo.name);
828         else if (msg->set_global_address.len)
829                 send_address_str=&(msg->set_global_address);
830         else
831                 send_address_str=&(send_sock->address_str);
832         if(send_sock && send_sock->useinfo.port_no>0)
833                 send_port_str=&(send_sock->useinfo.port_no_str);
834         else if (msg->set_global_port.len)
835                 send_port_str=&(msg->set_global_port);
836         else
837                 send_port_str=&(send_sock->port_no_str);
838         /* init recv_address_str, recv_port_str & recv_port_no */
839         if(msg->rcv.bind_address) {
840                 if(msg->rcv.bind_address->useinfo.name.len>0)
841                         recv_address_str=&(msg->rcv.bind_address->useinfo.name);
842                 else
843                         recv_address_str=&(msg->rcv.bind_address->address_str);
844                 if(msg->rcv.bind_address->useinfo.port_no>0) {
845                         recv_port_str=&(msg->rcv.bind_address->useinfo.port_no_str);
846                         recv_port_no = msg->rcv.bind_address->useinfo.port_no;
847                 } else {
848                         recv_port_str=&(msg->rcv.bind_address->port_no_str);
849                         recv_port_no = msg->rcv.bind_address->port_no;
850                 }
851         }
852
853         for(t=lumps;t;t=t->next){
854                 /* skip if this is an OPT lump and the condition is not satisfied */
855                 if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t, msg, send_info))
856                         continue;
857                 for(r=t->before;r;r=r->before){
858                         switch(r->op){
859                                 case LUMP_ADD:
860                                         new_len+=r->len;
861                                         break;
862                                 case LUMP_ADD_SUBST:
863                                         SUBST_LUMP_LEN(r);
864                                         break;
865                                 case LUMP_ADD_OPT:
866                                         /* skip if this is an OPT lump and the condition is
867                                          * not satisfied */
868                                         if (!lump_check_opt(r, msg, send_info))
869                                                 goto skip_before;
870                                         break;
871                                 default:
872                                         /* only ADD allowed for before/after */
873                                                 LOG(L_CRIT, "BUG: lumps_len: invalid op "
874                                                         "for data lump (%x)\n", r->op);
875                         }
876                 }
877 skip_before:
878                 switch(t->op){
879                         case LUMP_ADD:
880                                 new_len+=t->len;
881                                 break;
882                         case LUMP_ADD_SUBST:
883                                 SUBST_LUMP_LEN(t);
884                                 break;
885                         case LUMP_ADD_OPT:
886                                 /* we don't do anything here, it's only a condition for
887                                  * before & after */
888                                 break;
889                         case LUMP_DEL:
890                                 /* fix overlapping deleted zones */
891                                 if (t->u.offset < s_offset){
892                                         /* change len */
893                                         if (t->len>s_offset-t->u.offset)
894                                                         t->len-=s_offset-t->u.offset;
895                                         else t->len=0;
896                                         t->u.offset=s_offset;
897                                 }
898                                 s_offset=t->u.offset+t->len;
899                                 new_len-=t->len;
900                                 break;
901                         case LUMP_NOP:
902                                 /* fix offset if overlapping on a deleted zone */
903                                 if (t->u.offset < s_offset){
904                                         t->u.offset=s_offset;
905                                 }else
906                                         s_offset=t->u.offset;
907                                 /* do nothing */
908                                 break;
909                         default:
910                                 LOG(L_CRIT,"BUG:lumps_len: invalid"
911                                                         " op for data lump (%x)\n", r->op);
912                 }
913                 for (r=t->after;r;r=r->after){
914                         switch(r->op){
915                                 case LUMP_ADD:
916                                         new_len+=r->len;
917                                         break;
918                                 case LUMP_ADD_SUBST:
919                                         SUBST_LUMP_LEN(r);
920                                         break;
921                                 case LUMP_ADD_OPT:
922                                         /* skip if this is an OPT lump and the condition is
923                                          * not satisfied */
924                                         if (!lump_check_opt(r, msg, send_info))
925                                                 goto skip_after;
926                                         break;
927                                 default:
928                                         /* only ADD allowed for before/after */
929                                         LOG(L_CRIT, "BUG:lumps_len: invalid"
930                                                                 " op for data lump (%x)\n", r->op);
931                         }
932                 }
933 skip_after:
934                 ; /* to make gcc 3.* happy */
935         }
936         return new_len;
937 #undef RCVCOMP_LUMP_LEN
938 #undef SENDCOMP_LUMP_LEN
939 }
940
941
942
943 /* another helper functions, adds/Removes the lump,
944         code moved form build_req_from_req  */
945
946 static inline void process_lumps(       struct sip_msg* msg,
947                                                                         struct lump* lumps,
948                                                                         char* new_buf,
949                                                                         unsigned int* new_buf_offs,
950                                                                         unsigned int* orig_offs,
951                                                                         struct dest_info* send_info)
952 {
953         struct lump *t;
954         struct lump *r;
955         char* orig;
956         int size;
957         int offset;
958         int s_offset;
959         str* send_address_str;
960         str* send_port_str;
961         str* recv_address_str = NULL;
962         str* recv_port_str = NULL;
963         int  recv_port_no = 0;
964         struct socket_info* send_sock;
965
966 #ifdef USE_COMP
967         #define RCVCOMP_PARAM_ADD \
968                                 /* add ;comp=xxxx */ \
969                                 switch(msg->rcv.comp){ \
970                                         case COMP_NONE: \
971                                                 break; \
972                                         case COMP_SIGCOMP: \
973                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
974                                                 offset+=COMP_PARAM_LEN; \
975                                                 memcpy(new_buf+offset, SIGCOMP_NAME, \
976                                                                 SIGCOMP_NAME_LEN); \
977                                                 offset+=SIGCOMP_NAME_LEN; \
978                                                 break; \
979                                         case COMP_SERGZ: \
980                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
981                                                 offset+=COMP_PARAM_LEN; \
982                                                 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
983                                                 offset+=SERGZ_NAME_LEN; \
984                                                 break;\
985                                         default:\
986                                                 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
987                                                                 msg->rcv.comp); \
988                                 }
989         
990         #define SENDCOMP_PARAM_ADD \
991                                 /* add ;comp=xxxx */ \
992                                 switch(send_info->comp){ \
993                                         case COMP_NONE: \
994                                                 break; \
995                                         case COMP_SIGCOMP: \
996                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
997                                                 offset+=COMP_PARAM_LEN; \
998                                                 memcpy(new_buf+offset, SIGCOMP_NAME, \
999                                                                 SIGCOMP_NAME_LEN); \
1000                                                 offset+=SIGCOMP_NAME_LEN; \
1001                                                 break; \
1002                                         case COMP_SERGZ: \
1003                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
1004                                                 offset+=COMP_PARAM_LEN; \
1005                                                 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
1006                                                 offset+=SERGZ_NAME_LEN; \
1007                                                 break;\
1008                                         default:\
1009                                                 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
1010                                                                 msg->rcv.comp); \
1011                                 } 
1012 #else
1013         #define RCVCOMP_PARAM_ADD
1014         #define SENDCOMP_PARAM_ADD
1015 #endif /* USE_COMP */
1016
1017 #define SUBST_LUMP(subst_l) \
1018         switch((subst_l)->u.subst){ \
1019                 case SUBST_RCV_IP: \
1020                         if (msg->rcv.bind_address){  \
1021                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
1022                                         new_buf[offset]='['; offset++; \
1023                                 }\
1024                                 memcpy(new_buf+offset, recv_address_str->s, \
1025                                                 recv_address_str->len); \
1026                                 offset+=recv_address_str->len; \
1027                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
1028                                         new_buf[offset]=']'; offset++; \
1029                                 }\
1030                         }else{  \
1031                                 /*FIXME*/ \
1032                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1033                         }; \
1034                         break; \
1035                 case SUBST_RCV_PORT: \
1036                         if (msg->rcv.bind_address){  \
1037                                 memcpy(new_buf+offset, recv_port_str->s, \
1038                                                 recv_port_str->len); \
1039                                 offset+=recv_port_str->len; \
1040                         }else{  \
1041                                 /*FIXME*/ \
1042                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1043                         }; \
1044                         break; \
1045                 case SUBST_RCV_ALL: \
1046                         if (msg->rcv.bind_address){  \
1047                                 /* address */ \
1048                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
1049                                         new_buf[offset]='['; offset++; \
1050                                 }\
1051                                 memcpy(new_buf+offset, recv_address_str->s, \
1052                                                 recv_address_str->len); \
1053                                 offset+=recv_address_str->len; \
1054                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
1055                                         new_buf[offset]=']'; offset++; \
1056                                 }\
1057                                 /* :port */ \
1058                                 if (recv_port_no!=SIP_PORT){ \
1059                                         new_buf[offset]=':'; offset++; \
1060                                         memcpy(new_buf+offset, \
1061                                                         recv_port_str->s, \
1062                                                         recv_port_str->len); \
1063                                         offset+=recv_port_str->len; \
1064                                 }\
1065                                 switch(msg->rcv.bind_address->proto){ \
1066                                         case PROTO_NONE: \
1067                                         case PROTO_UDP: \
1068                                                 break; /* nothing to do, udp is default*/ \
1069                                         case PROTO_TCP: \
1070                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1071                                                                 TRANSPORT_PARAM_LEN); \
1072                                                 offset+=TRANSPORT_PARAM_LEN; \
1073                                                 if (msg->rcv.proto == PROTO_WS) { \
1074                                                         memcpy(new_buf+offset, "ws", 2); \
1075                                                         offset+=2; \
1076                                                 } else { \
1077                                                         memcpy(new_buf+offset, "tcp", 3); \
1078                                                         offset+=3; \
1079                                                 } \
1080                                                 break; \
1081                                         case PROTO_TLS: \
1082                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1083                                                                 TRANSPORT_PARAM_LEN); \
1084                                                 offset+=TRANSPORT_PARAM_LEN; \
1085                                                 if (msg->rcv.proto == PROTO_WS || msg->rcv.proto == PROTO_WSS) { \
1086                                                         memcpy(new_buf+offset, "ws", 2); \
1087                                                         offset+=2; \
1088                                                 } else { \
1089                                                         memcpy(new_buf+offset, "tls", 3); \
1090                                                         offset+=3; \
1091                                                 } \
1092                                                 break; \
1093                                         case PROTO_SCTP: \
1094                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1095                                                                 TRANSPORT_PARAM_LEN); \
1096                                                 offset+=TRANSPORT_PARAM_LEN; \
1097                                                 memcpy(new_buf+offset, "sctp", 4); \
1098                                                 offset+=4; \
1099                                                 break; \
1100                                         default: \
1101                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1102                                                                 msg->rcv.bind_address->proto); \
1103                                 } \
1104                                 RCVCOMP_PARAM_ADD \
1105                         }else{  \
1106                                 /*FIXME*/ \
1107                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1108                         }; \
1109                         break; \
1110                 case SUBST_SND_IP: \
1111                         if (send_sock){  \
1112                                 if ((send_sock->address.af!=AF_INET) && \
1113                                                 (send_address_str==&(send_sock->address_str))){\
1114                                         new_buf[offset]='['; offset++; \
1115                                 }\
1116                                 memcpy(new_buf+offset, send_address_str->s, \
1117                                                                         send_address_str->len); \
1118                                 offset+=send_address_str->len; \
1119                                 if ((send_sock->address.af!=AF_INET) && \
1120                                                 (send_address_str==&(send_sock->address_str))){\
1121                                         new_buf[offset]=']'; offset++; \
1122                                 }\
1123                         }else{  \
1124                                 /*FIXME*/ \
1125                                 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1126                                                         " null send_sock\n"); \
1127                         }; \
1128                         break; \
1129                 case SUBST_SND_PORT: \
1130                         if (send_sock){  \
1131                                 memcpy(new_buf+offset, send_port_str->s, \
1132                                                                         send_port_str->len); \
1133                                 offset+=send_port_str->len; \
1134                         }else{  \
1135                                 /*FIXME*/ \
1136                                 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1137                                                 " null send_sock\n"); \
1138                         }; \
1139                         break; \
1140                 case SUBST_SND_ALL: \
1141                         if (send_sock){  \
1142                                 /* address */ \
1143                                 if ((send_sock->address.af!=AF_INET) && \
1144                                                 (send_address_str==&(send_sock->address_str))){\
1145                                         new_buf[offset]='['; offset++; \
1146                                 }\
1147                                 memcpy(new_buf+offset, send_address_str->s, \
1148                                                 send_address_str->len); \
1149                                 offset+=send_address_str->len; \
1150                                 if ((send_sock->address.af!=AF_INET) && \
1151                                                 (send_address_str==&(send_sock->address_str))){\
1152                                         new_buf[offset]=']'; offset++; \
1153                                 }\
1154                                 /* :port */ \
1155                                 if ((send_sock->port_no!=SIP_PORT) || \
1156                                         (send_port_str!=&(send_sock->port_no_str))){ \
1157                                         new_buf[offset]=':'; offset++; \
1158                                         memcpy(new_buf+offset, send_port_str->s, \
1159                                                         send_port_str->len); \
1160                                         offset+=send_port_str->len; \
1161                                 }\
1162                                 switch(send_sock->proto){ \
1163                                         case PROTO_NONE: \
1164                                         case PROTO_UDP: \
1165                                                 break; /* nothing to do, udp is default*/ \
1166                                         case PROTO_TCP: \
1167                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1168                                                                 TRANSPORT_PARAM_LEN); \
1169                                                 offset+=TRANSPORT_PARAM_LEN; \
1170                                                 if (send_info->proto == PROTO_WS) { \
1171                                                         memcpy(new_buf+offset, "ws", 2); \
1172                                                         offset+=2; \
1173                                                 } else { \
1174                                                         memcpy(new_buf+offset, "tcp", 3); \
1175                                                         offset+=3; \
1176                                                 } \
1177                                                 break; \
1178                                         case PROTO_TLS: \
1179                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1180                                                                 TRANSPORT_PARAM_LEN); \
1181                                                 offset+=TRANSPORT_PARAM_LEN; \
1182                                                 if (send_info->proto == PROTO_WS || send_info->proto == PROTO_WSS) { \
1183                                                         memcpy(new_buf+offset, "ws", 2); \
1184                                                         offset+=2; \
1185                                                 } else { \
1186                                                         memcpy(new_buf+offset, "tls", 3); \
1187                                                         offset+=3; \
1188                                                 } \
1189                                                 break; \
1190                                         case PROTO_SCTP: \
1191                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1192                                                                 TRANSPORT_PARAM_LEN); \
1193                                                 offset+=TRANSPORT_PARAM_LEN; \
1194                                                 memcpy(new_buf+offset, "sctp", 4); \
1195                                                 offset+=4; \
1196                                                 break; \
1197                                         default: \
1198                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1199                                                                 send_sock->proto); \
1200                                 } \
1201                                 SENDCOMP_PARAM_ADD \
1202                         }else{  \
1203                                 /*FIXME*/ \
1204                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1205                         }; \
1206                         break; \
1207                 case SUBST_RCV_PROTO: \
1208                         if (msg->rcv.bind_address){ \
1209                                 switch(msg->rcv.bind_address->proto){ \
1210                                         case PROTO_NONE: \
1211                                         case PROTO_UDP: \
1212                                                 memcpy(new_buf+offset, "udp", 3); \
1213                                                 offset+=3; \
1214                                                 break; \
1215                                         case PROTO_TCP: \
1216                                                 if (msg->rcv.proto == PROTO_WS) { \
1217                                                         memcpy(new_buf+offset, "ws", 2); \
1218                                                         offset+=2; \
1219                                                 } else { \
1220                                                         memcpy(new_buf+offset, "tcp", 3); \
1221                                                         offset+=3; \
1222                                                 } \
1223                                                 break; \
1224                                         case PROTO_TLS: \
1225                                                 if (msg->rcv.proto == PROTO_WS || msg->rcv.proto == PROTO_WSS) { \
1226                                                         memcpy(new_buf+offset, "ws", 2); \
1227                                                         offset+=2; \
1228                                                 } else { \
1229                                                         memcpy(new_buf+offset, "tls", 3); \
1230                                                         offset+=3; \
1231                                                 } \
1232                                                 break; \
1233                                         case PROTO_SCTP: \
1234                                                 memcpy(new_buf+offset, "sctp", 4); \
1235                                                 offset+=4; \
1236                                                 break; \
1237                                         default: \
1238                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1239                                                                 msg->rcv.bind_address->proto); \
1240                                 } \
1241                         }else{  \
1242                                 /*FIXME*/ \
1243                                 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1244                                                         " send_sock \n"); \
1245                         }; \
1246                         break; \
1247                 case  SUBST_SND_PROTO: \
1248                         if (send_sock){ \
1249                                 switch(send_sock->proto){ \
1250                                         case PROTO_NONE: \
1251                                         case PROTO_UDP: \
1252                                                 memcpy(new_buf+offset, "udp", 3); \
1253                                                 offset+=3; \
1254                                                 break; \
1255                                         case PROTO_TCP: \
1256                                                 if (send_info->proto == PROTO_WS) { \
1257                                                         memcpy(new_buf+offset, "ws", 2); \
1258                                                         offset+=2; \
1259                                                 } else { \
1260                                                         memcpy(new_buf+offset, "tcp", 3); \
1261                                                         offset+=3; \
1262                                                 } \
1263                                                 break; \
1264                                         case PROTO_TLS: \
1265                                                 if (send_info->proto == PROTO_WS || send_info->proto == PROTO_WSS) { \
1266                                                         memcpy(new_buf+offset, "ws", 2); \
1267                                                         offset+=2; \
1268                                                 } else { \
1269                                                         memcpy(new_buf+offset, "tls", 3); \
1270                                                         offset+=3; \
1271                                                 } \
1272                                                 break; \
1273                                         case PROTO_SCTP: \
1274                                                 memcpy(new_buf+offset, "sctp", 4); \
1275                                                 offset+=4; \
1276                                                 break; \
1277                                         default: \
1278                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1279                                                                 send_sock->proto); \
1280                                 } \
1281                         }else{  \
1282                                 /*FIXME*/ \
1283                                 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1284                                                         " send_sock \n"); \
1285                         }; \
1286                         break; \
1287                 default: \
1288                                 LOG(L_CRIT, "BUG: process_lumps: unknown subst type %d\n", \
1289                                                         (subst_l)->u.subst); \
1290         }
1291
1292         if (send_info){
1293                 send_sock=send_info->send_sock;
1294         }else{
1295                 send_sock=0;
1296         }
1297         /* init send_address_str & send_port_str */
1298         if (msg->set_global_address.len)
1299                 send_address_str=&(msg->set_global_address);
1300         else
1301                 send_address_str=&(send_sock->address_str);
1302         if (msg->set_global_port.len)
1303                 send_port_str=&(msg->set_global_port);
1304         else
1305                 send_port_str=&(send_sock->port_no_str);
1306         /* init send_address_str & send_port_str */
1307         if(send_sock && send_sock->useinfo.name.len>0)
1308                 send_address_str=&(send_sock->useinfo.name);
1309         else if (msg->set_global_address.len)
1310                 send_address_str=&(msg->set_global_address);
1311         else
1312                 send_address_str=&(send_sock->address_str);
1313         if(send_sock && send_sock->useinfo.port_no>0)
1314                 send_port_str=&(send_sock->useinfo.port_no_str);
1315         else if (msg->set_global_port.len)
1316                 send_port_str=&(msg->set_global_port);
1317         else
1318                 send_port_str=&(send_sock->port_no_str);
1319         /* init recv_address_str, recv_port_str & recv_port_no */
1320         if(msg->rcv.bind_address) {
1321                 if(msg->rcv.bind_address->useinfo.name.len>0)
1322                         recv_address_str=&(msg->rcv.bind_address->useinfo.name);
1323                 else
1324                         recv_address_str=&(msg->rcv.bind_address->address_str);
1325                 if(msg->rcv.bind_address->useinfo.port_no>0) {
1326                         recv_port_str=&(msg->rcv.bind_address->useinfo.port_no_str);
1327                         recv_port_no = msg->rcv.bind_address->useinfo.port_no;
1328                 } else {
1329                         recv_port_str=&(msg->rcv.bind_address->port_no_str);
1330                         recv_port_no = msg->rcv.bind_address->port_no;
1331                 }
1332         }
1333
1334         orig=msg->buf;
1335         offset=*new_buf_offs;
1336         s_offset=*orig_offs;
1337
1338         for (t=lumps;t;t=t->next){
1339                 switch(t->op){
1340                         case LUMP_ADD:
1341                         case LUMP_ADD_SUBST:
1342                         case LUMP_ADD_OPT:
1343                                 /* skip if this is an OPT lump and the condition is
1344                                  * not satisfied */
1345                                 if ((t->op==LUMP_ADD_OPT) &&
1346                                                 (!lump_check_opt(t, msg, send_info)))
1347                                         continue;
1348                                 /* just add it here! */
1349                                 /* process before  */
1350                                 for(r=t->before;r;r=r->before){
1351                                         switch (r->op){
1352                                                 case LUMP_ADD:
1353                                                         /*just add it here*/
1354                                                         memcpy(new_buf+offset, r->u.value, r->len);
1355                                                         offset+=r->len;
1356                                                         break;
1357                                                 case LUMP_ADD_SUBST:
1358                                                         SUBST_LUMP(r);
1359                                                         break;
1360                                                 case LUMP_ADD_OPT:
1361                                                         /* skip if this is an OPT lump and the condition is
1362                                                         * not satisfied */
1363                                                         if (!lump_check_opt(r, msg, send_info))
1364                                                                 goto skip_before;
1365                                                         break;
1366                                                 default:
1367                                                         /* only ADD allowed for before/after */
1368                                                         LOG(L_CRIT, "BUG:process_lumps: "
1369                                                                         "invalid op for data lump (%x)\n", r->op);
1370                                         }
1371                                 }
1372 skip_before:
1373                                 /* copy "main" part */
1374                                 switch(t->op){
1375                                         case LUMP_ADD:
1376                                                 memcpy(new_buf+offset, t->u.value, t->len);
1377                                                 offset+=t->len;
1378                                                 break;
1379                                         case LUMP_ADD_SUBST:
1380                                                 SUBST_LUMP(t);
1381                                                 break;
1382                                         case LUMP_ADD_OPT:
1383                                                 /* do nothing, it's only a condition */
1384                                                 break;
1385                                         default:
1386                                                 /* should not ever get here */
1387                                                 LOG(L_CRIT, "BUG: process_lumps: unhandled data lump "
1388                                                                 " op %d\n", t->op);
1389                                 }
1390                                 /* process after */
1391                                 for(r=t->after;r;r=r->after){
1392                                         switch (r->op){
1393                                                 case LUMP_ADD:
1394                                                         /*just add it here*/
1395                                                         memcpy(new_buf+offset, r->u.value, r->len);
1396                                                         offset+=r->len;
1397                                                         break;
1398                                                 case LUMP_ADD_SUBST:
1399                                                         SUBST_LUMP(r);
1400                                                         break;
1401                                                 case LUMP_ADD_OPT:
1402                                                         /* skip if this is an OPT lump and the condition is
1403                                                         * not satisfied */
1404                                                         if (!lump_check_opt(r, msg, send_info))
1405                                                                 goto skip_after;
1406                                                         break;
1407                                                 default:
1408                                                         /* only ADD allowed for before/after */
1409                                                         LOG(L_CRIT, "BUG:process_lumps: "
1410                                                                         "invalid op for data lump (%x)\n", r->op);
1411                                         }
1412                                 }
1413 skip_after:
1414                                 break;
1415                         case LUMP_NOP:
1416                         case LUMP_DEL:
1417                                 /* copy till offset */
1418                                 if (s_offset>t->u.offset){
1419                                         DBG("Warning: (%d) overlapped lumps offsets,"
1420                                                 " ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
1421                                         /* this should've been fixed above (when computing len) */
1422                                         /* just ignore it*/
1423                                         break;
1424                                 }
1425                                 size=t->u.offset-s_offset;
1426                                 if (size){
1427                                         memcpy(new_buf+offset, orig+s_offset,size);
1428                                         offset+=size;
1429                                         s_offset+=size;
1430                                 }
1431                                 /* process before  */
1432                                 for(r=t->before;r;r=r->before){
1433                                         switch (r->op){
1434                                                 case LUMP_ADD:
1435                                                         /*just add it here*/
1436                                                         memcpy(new_buf+offset, r->u.value, r->len);
1437                                                         offset+=r->len;
1438                                                         break;
1439                                                 case LUMP_ADD_SUBST:
1440                                                         SUBST_LUMP(r);
1441                                                         break;
1442                                                 case LUMP_ADD_OPT:
1443                                                         /* skip if this is an OPT lump and the condition is
1444                                                         * not satisfied */
1445                                                         if (!lump_check_opt(r, msg, send_info))
1446                                                                 goto skip_nop_before;
1447                                                         break;
1448                                                 default:
1449                                                         /* only ADD allowed for before/after */
1450                                                         LOG(L_CRIT, "BUG:process_lumps: "
1451                                                                         "invalid op for data lump (%x)\n",r->op);
1452                                         }
1453                                 }
1454 skip_nop_before:
1455                                 /* process main (del only) */
1456                                 if (t->op==LUMP_DEL){
1457                                         /* skip len bytes from orig msg */
1458                                         s_offset+=t->len;
1459                                 }
1460                                 /* process after */
1461                                 for(r=t->after;r;r=r->after){
1462                                         switch (r->op){
1463                                                 case LUMP_ADD:
1464                                                         /*just add it here*/
1465                                                         memcpy(new_buf+offset, r->u.value, r->len);
1466                                                         offset+=r->len;
1467                                                         break;
1468                                                 case LUMP_ADD_SUBST:
1469                                                         SUBST_LUMP(r);
1470                                                         break;
1471                                                 case LUMP_ADD_OPT:
1472                                                         /* skip if this is an OPT lump and the condition is
1473                                                         * not satisfied */
1474                                                         if (!lump_check_opt(r, msg, send_info))
1475                                                                 goto skip_nop_after;
1476                                                         break;
1477                                                 default:
1478                                                         /* only ADD allowed for before/after */
1479                                                         LOG(L_CRIT, "BUG:process_lumps: "
1480                                                                         "invalid op for data lump (%x)\n", r->op);
1481                                         }
1482                                 }
1483 skip_nop_after:
1484                                 break;
1485                         default:
1486                                         LOG(L_CRIT, "BUG: process_lumps: "
1487                                                         "unknown op (%x)\n", t->op);
1488                 }
1489         }
1490         *new_buf_offs=offset;
1491         *orig_offs=s_offset;
1492 #undef RCVCOMP_PARAM_ADD 
1493 #undef SENDCOMP_PARAM_ADD
1494 }
1495
1496
1497 /*
1498  * Adjust/insert Content-Length if necessary
1499  */
1500 static inline int adjust_clen(struct sip_msg* msg, int body_delta, int proto)
1501 {
1502         struct lump* anchor;
1503         char* clen_buf;
1504         int clen_len, body_only;
1505 #ifdef USE_TCP
1506         char* body;
1507         int comp_clen;
1508 #endif /* USE_TCP */
1509
1510         /* Calculate message length difference caused by lumps modifying message
1511          * body, from this point on the message body must not be modified. Zero
1512          * value indicates that the body hasn't been modified
1513         */
1514
1515         clen_buf = 0;
1516         anchor=0;
1517         body_only=1;
1518
1519         /* check to see if we need to add clen */
1520 #ifdef USE_TCP
1521         if (proto == PROTO_TCP
1522 #ifdef USE_TLS
1523             || proto == PROTO_TLS
1524 #endif
1525             ) {
1526                 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
1527                         LOG(L_ERR, "adjust_clen: error parsing content-length\n");
1528                         goto error;
1529                 }
1530                 if (unlikely(msg->content_length==0)){
1531                         /* not present, we need to add it */
1532                         /* msg->unparsed should point just before the final crlf
1533                          * - whole message was parsed by the above parse_headers
1534                          *   which did not find content-length */
1535                         anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1536                                                                                                 HDR_CONTENTLENGTH_T);
1537                         if (anchor==0){
1538                                 LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1539                                 goto error;
1540                         }
1541                         body_only=0;
1542                 }else{
1543                         /* compute current content length and compare it with the
1544                            one in the message */
1545                         body=get_body(msg);
1546                         if (unlikely(body==0)){
1547                                 ser_error=E_BAD_REQ;
1548                                 LOG(L_ERR, "adjust_clen: no message body found"
1549                                                 " (missing crlf?)");
1550                                 goto error;
1551                         }
1552                         comp_clen=msg->len-(int)(body-msg->buf)+body_delta;
1553                         if (comp_clen!=(int)(long)msg->content_length->parsed){
1554                                 /* note: we don't distinguish here between received with
1555                                    wrong content-length and content-length changed, we just
1556                                    fix it automatically in both cases (the reason being
1557                                    that an error message telling we have received a msg-
1558                                    with wrong content-length is of very little use) */
1559                                 anchor = del_lump(msg, msg->content_length->body.s-msg->buf,
1560                                                                         msg->content_length->body.len,
1561                                                                         HDR_CONTENTLENGTH_T);
1562                                 if (anchor==0) {
1563                                         LOG(L_ERR, "adjust_clen: Can't remove original"
1564                                                                 " Content-Length\n");
1565                                         goto error;
1566                                 }
1567                                 body_only=1;
1568                         }
1569                 }
1570         }else
1571 #endif /* USE_TCP */
1572         if (body_delta){
1573                 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
1574                         LOG(L_ERR, "adjust_clen: Error parsing Content-Length\n");
1575                         goto error;
1576                 }
1577
1578                 /* The body has been changed, try to find
1579                  * existing Content-Length
1580                  */
1581                 /* no need for Content-Length if it's and UDP packet and
1582                  * it hasn't Content-Length already */
1583                 if ((msg->content_length==0)){
1584                     /* content-length doesn't exist, append it */
1585                         /* msg->unparsed should point just before the final crlf
1586                          * - whole message was parsed by the above parse_headers
1587                          *   which did not find content-length */
1588                         if (proto!=PROTO_UDP){
1589                                 anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1590                                                                                                         HDR_CONTENTLENGTH_T);
1591                                 if (anchor==0){
1592                                         LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1593                                         goto error;
1594                                 }
1595                                 body_only=0;
1596                         } /* else
1597                                 DBG("adjust_clen: UDP packet with no clen => "
1598                                                 "not adding one \n"); */
1599                 }else{
1600                         /* Content-Length has been found, remove it */
1601                         anchor = del_lump(      msg, msg->content_length->body.s - msg->buf,
1602                                                                 msg->content_length->body.len,
1603                                                                 HDR_CONTENTLENGTH_T);
1604                         if (anchor==0) {
1605                                 LOG(L_ERR, "adjust_clen: Can't remove original"
1606                                                         " Content-Length\n");
1607                                 goto error;
1608                         }
1609                 }
1610         }
1611
1612         if (anchor){
1613                 clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
1614                 if (!clen_buf) goto error;
1615                 if (insert_new_lump_after(anchor, clen_buf, clen_len,
1616                                         HDR_CONTENTLENGTH_T) == 0)
1617                         goto error;
1618         }
1619         
1620         return 0;
1621 error:
1622         if (clen_buf) pkg_free(clen_buf);
1623         return -1;
1624 }
1625
1626
1627
1628 /** builds a request in memory from another sip request.
1629   *
1630   * Side-effects: - it adds lumps to the msg which are _not_ cleaned.
1631   * The added lumps are HDR_VIA_T (almost always added), HDR_CONTENLENGTH_T
1632   * and HDR_ROUTE_T (when a Route: header is added as a result of a non-null
1633   * msg->path_vec).
1634   *               - it might change send_info->proto and send_info->send_socket
1635   *                 if proto fallback is enabled (see below).
1636   *
1637   * Uses also global_req_flags ( OR'ed with msg->msg_flags, see send_info
1638   * below).
1639   *
1640   * @param msg  - sip message structure, complete with lumps
1641   * @param returned_len - result length (filled in)
1642   * @param send_info  - dest_info structure (value/result), contains where the
1643   *                     packet will be sent to (it's needed for building a 
1644   *                     correct via, fill RR lumps a.s.o.). If MTU based
1645   *                     protocol fall-back is enabled (see flags below),
1646   *                     send_info->proto might be updated with the new
1647   *                     protocol.
1648   *                     msg->msg_flags used:
1649   *                     - FL_TCP_MTU_FB, FL_TLS_MTU_FB and FL_SCTP_MTU_FB -
1650   *                       fallback to the corresp. proto if the built 
1651   *                       message > mtu and send_info->proto==PROTO_UDP. 
1652   *                       It will also update send_info->proto.
1653   *                     - FL_FORCE_RPORT: add rport to via
1654   * @param mode - flags for building the message, can be a combination of:
1655   *                 * BUILD_NO_LOCAL_VIA - don't add a local via
1656   *                 * BUILD_NO_VIA1_UPDATE - don't update first via (rport,
1657   *                    received a.s.o)
1658   *                 * BUILD_NO_PATH - don't add a Route: header with the 
1659   *                   msg->path_vec content.
1660   *                 * BUILD_IN_SHM - build the result in shm memory
1661   *
1662   * @return pointer to the new request (pkg_malloc'ed or shm_malloc'ed,
1663   * depending on the presence of the BUILD_IN_SHM flag, needs freeing when
1664   *   done) and sets returned_len or 0 on error.
1665   */
1666 char * build_req_buf_from_sip_req( struct sip_msg* msg,
1667                                                                 unsigned int *returned_len,
1668                                                                 struct dest_info* send_info,
1669                                                                 unsigned int mode)
1670 {
1671         unsigned int len, new_len, received_len, rport_len, uri_len, via_len,
1672                                  body_delta;
1673         char* line_buf;
1674         char* received_buf;
1675         char* rport_buf;
1676         char* new_buf;
1677         char* buf;
1678         str path_buf;
1679         unsigned int offset, s_offset, size;
1680         struct lump* via_anchor;
1681         struct lump* via_lump;
1682         struct lump* via_insert_param;
1683         struct lump* path_anchor;
1684         struct lump* path_lump;
1685         str branch;
1686         unsigned int flags;
1687         unsigned int udp_mtu;
1688         struct dest_info di;
1689
1690         via_insert_param=0;
1691         uri_len=0;
1692         buf=msg->buf;
1693         len=msg->len;
1694         received_len=0;
1695         rport_len=0;
1696         new_buf=0;
1697         received_buf=0;
1698         rport_buf=0;
1699         via_anchor=0;
1700         line_buf=0;
1701         via_len=0;
1702         path_buf.s=0;
1703         path_buf.len=0;
1704
1705         flags=msg->msg_flags|global_req_flags;
1706         /* Calculate message body difference and adjust Content-Length */
1707         body_delta = lumps_len(msg, msg->body_lumps, send_info);
1708         if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
1709                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1710                                 " Content-Length\n");
1711                 goto error00;
1712         }
1713
1714         if(unlikely(mode&BUILD_NO_LOCAL_VIA))
1715                 goto after_local_via;
1716
1717         /* create the via header */
1718         branch.s=msg->add_to_branch_s;
1719         branch.len=msg->add_to_branch_len;
1720
1721         via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
1722         if (unlikely(via_anchor==0)) goto error00;
1723         line_buf = create_via_hf( &via_len, msg, send_info, &branch);
1724         if (unlikely(!line_buf)){
1725                 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
1726                                         "memory allocation failure\n");
1727                 goto error00;
1728         }
1729 after_local_via:
1730         if(unlikely(mode&BUILD_NO_VIA1_UPDATE))
1731                 goto after_update_via1;
1732         /* check if received needs to be added */
1733         if ( received_test(msg) ) {
1734                 if ((received_buf=received_builder(msg,&received_len))==0){
1735                         LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1736                                                         " received_builder failed\n");
1737                         goto error01;  /* free also line_buf */
1738                 }
1739         }
1740
1741         /* check if rport needs to be updated:
1742          *  - if FL_FORCE_RPORT is set add it (and del. any previous version)
1743          *  - if via already contains an rport add it and overwrite the previous
1744          *  rport value if present (if you don't want to overwrite the previous
1745          *  version remove the comments) */
1746         if ((flags&FL_FORCE_RPORT)||
1747                         (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1748                 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1749                         LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1750                                                         " rport_builder failed\n");
1751                         goto error01; /* free everything */
1752                 }
1753         }
1754
1755         /* find out where the offset of the first parameter that should be added
1756          * (after host:port), needed by add receive & maybe rport */
1757         if (msg->via1->params.s){
1758                         size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
1759                                                                                                                   for ';' */
1760         }else{
1761                         size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
1762                         if (msg->via1->port!=0){
1763                                 /*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
1764                                 size += msg->via1->port_str.len + 1; /* +1 for ':'*/
1765                         }
1766 #if 0
1767                         /* no longer necessary, now hots.s contains [] */
1768                 #ifdef USE_IPV6
1769                         if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
1770                 #endif
1771 #endif
1772         }
1773         /* if received needs to be added, add anchor after host and add it, or
1774          * overwrite the previous one if already present */
1775         if (received_len){
1776                 if (msg->via1->received){ /* received already present => overwrite it*/
1777                         via_insert_param=del_lump(msg,
1778                                                                 msg->via1->received->start-buf-1, /*;*/
1779                                                                 msg->via1->received->size+1, /*;*/ HDR_VIA_T);
1780                 }else if (via_insert_param==0){ /* receive not present, ok */
1781                         via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1782                                                                                         HDR_VIA_T);
1783                 }
1784                 if (via_insert_param==0) goto error02; /* free received_buf */
1785                 if (insert_new_lump_after(via_insert_param, received_buf, received_len,
1786                                         HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
1787         }
1788         /* if rport needs to be updated, delete it if present and add it's value */
1789         if (rport_len){
1790                 if (msg->via1->rport){ /* rport already present */
1791                         via_insert_param=del_lump(msg,
1792                                                                 msg->via1->rport->start-buf-1, /*';'*/
1793                                                                 msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
1794                 }else if (via_insert_param==0){ /*force rport, no rport present */
1795                         /* no rport, add it */
1796                         via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1797                                                                                         HDR_VIA_T);
1798                 }
1799                 if (via_insert_param==0) goto error03; /* free rport_buf */
1800                 if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
1801                                                                         HDR_VIA_T) ==0 )
1802                         goto error03; /* free rport_buf */
1803
1804         }
1805
1806 after_update_via1:
1807         /* add route with path content */
1808         if(unlikely(!(mode&BUILD_NO_PATH) && msg->path_vec.s &&
1809                                         msg->path_vec.len)){
1810                 path_buf.len=ROUTE_PREFIX_LEN+msg->path_vec.len+CRLF_LEN;
1811                 path_buf.s=pkg_malloc(path_buf.len+1);
1812                 if (unlikely(path_buf.s==0)){
1813                         LOG(L_ERR, "out of memory\n");
1814                         ser_error=E_OUT_OF_MEM;
1815                         goto error00;
1816                 }
1817                 memcpy(path_buf.s, ROUTE_PREFIX, ROUTE_PREFIX_LEN);
1818                 memcpy(path_buf.s+ROUTE_PREFIX_LEN, msg->path_vec.s,
1819                                         msg->path_vec.len);
1820                 memcpy(path_buf.s+ROUTE_PREFIX_LEN+msg->path_vec.len, CRLF, CRLF_LEN);
1821                 path_buf.s[path_buf.len]=0;
1822                 /* insert Route header either before the other routes
1823                    (if present & parsed), after the local via or after in front of
1824                     the first via if we don't add a local via*/
1825                 if (msg->route){
1826                         path_anchor=anchor_lump(msg, msg->route->name.s-buf, 0, 
1827                                                                         HDR_ROUTE_T);
1828                 }else if (likely(via_anchor)){
1829                         path_anchor=via_anchor;
1830                 }else if (likely(msg->via1)){
1831                         path_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, 
1832                                                                         HDR_ROUTE_T);
1833                 }else{
1834                         /* if no via1 (theoretically possible for non-sip messages,
1835                            e.g. http xmlrpc) */
1836                         path_anchor=anchor_lump(msg, msg->headers->name.s-buf, 0, 
1837                                                                         HDR_ROUTE_T);
1838                 }
1839                 if (unlikely(path_anchor==0))
1840                         goto error05;
1841                 if (unlikely((path_lump=insert_new_lump_after(path_anchor, path_buf.s,
1842                                                                                                                 path_buf.len,
1843                                                                                                                 HDR_ROUTE_T))==0))
1844                         goto error05;
1845         }
1846         /* compute new msg len and fix overlapping zones*/
1847         new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
1848 #ifdef XL_DEBUG
1849         LOG(L_ERR, "DEBUG: new_len(%d)=len(%d)+lumps_len\n", new_len, len);
1850 #endif
1851         udp_mtu=cfg_get(core, core_cfg, udp_mtu);
1852         di.proto=PROTO_NONE;
1853         if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu && 
1854                                         (flags & FL_MTU_FB_MASK) && (new_len>udp_mtu)
1855                                         && (!(mode&BUILD_NO_LOCAL_VIA)))){
1856
1857                 di=*send_info; /* copy whole struct - will be used in the Via builder */
1858                 di.proto=PROTO_NONE; /* except the proto */
1859 #ifdef USE_TCP
1860                 if (!tcp_disable && (flags & FL_MTU_TCP_FB) &&
1861                                 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TCP))){
1862                         di.proto=PROTO_TCP;
1863                 }
1864         #ifdef USE_TLS
1865                 else if (!tls_disable && (flags & FL_MTU_TLS_FB) &&
1866                                 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TLS))){
1867                         di.proto=PROTO_TLS;
1868                 }
1869         #endif /* USE_TLS */
1870 #endif /* USE_TCP */
1871 #ifdef USE_SCTP
1872         #ifdef USE_TCP
1873                 else
1874         #endif /* USE_TCP */
1875                  if (!sctp_disable && (flags & FL_MTU_SCTP_FB) &&
1876                                 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_SCTP))){
1877                         di.proto=PROTO_SCTP;
1878                  }
1879 #endif /* USE_SCTP */
1880                 
1881                 if (di.proto!=PROTO_NONE){
1882                         new_len-=via_len;
1883                         if(likely(line_buf)) pkg_free(line_buf);
1884                         line_buf = create_via_hf( &via_len, msg, &di, &branch);
1885                         if (!line_buf){
1886                                 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
1887                                                         "memory allocation failure!\n");
1888                                 goto error00;
1889                         }
1890                         new_len+=via_len;
1891                 }
1892         }
1893         /* add via header to the list */
1894         /* try to add it before msg. 1st via */
1895         /* add first via, as an anchor for second via*/
1896         if(likely(line_buf)) {
1897                 if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
1898                                                                                         HDR_VIA_T))==0)
1899                         goto error04;
1900         }
1901         if (msg->new_uri.s){
1902                 uri_len=msg->new_uri.len;
1903                 new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
1904         }
1905         if(unlikely(mode&BUILD_IN_SHM))
1906                 new_buf=(char*)shm_malloc(new_len+1);
1907         else
1908                 new_buf=(char*)pkg_malloc(new_len+1);
1909         if (new_buf==0){
1910                 ser_error=E_OUT_OF_MEM;
1911                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
1912                 goto error00;
1913         }
1914
1915         offset=s_offset=0;
1916         if (msg->new_uri.s){
1917                 /* copy message up to uri */
1918                 size=msg->first_line.u.request.uri.s-buf;
1919                 memcpy(new_buf, buf, size);
1920                 offset+=size;
1921                 s_offset+=size;
1922                 /* add our uri */
1923                 memcpy(new_buf+offset, msg->new_uri.s, uri_len);
1924                 offset+=uri_len;
1925                 s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
1926         }
1927         new_buf[new_len]=0;
1928         /* copy msg adding/removing lumps */
1929         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
1930         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info);
1931         /* copy the rest of the message */
1932         memcpy(new_buf+offset, buf+s_offset, len-s_offset);
1933         new_buf[new_len]=0;
1934
1935         /* update the send_info if udp_mtu affected */
1936         if (di.proto!=PROTO_NONE) { 
1937                 send_info->proto=di.proto;
1938                 send_info->send_sock=di.send_sock;
1939         }
1940
1941 #ifdef DBG_MSG_QA
1942         if (new_buf[new_len-1]==0) {
1943                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: 0 in the end\n");
1944                 abort();
1945         }
1946 #endif
1947
1948         *returned_len=new_len;
1949         return new_buf;
1950
1951 error01:
1952 error02:
1953         if (received_buf) pkg_free(received_buf);
1954 error03:
1955         if (rport_buf) pkg_free(rport_buf);
1956 error04:
1957         if (line_buf) pkg_free(line_buf);
1958 error05:
1959         if (path_buf.s) pkg_free(path_buf.s);
1960 error00:
1961         *returned_len=0;
1962         return 0;
1963 }
1964
1965
1966
1967 char * build_res_buf_from_sip_res( struct sip_msg* msg,
1968                                 unsigned int *returned_len)
1969 {
1970         unsigned int new_len, via_len, body_delta;
1971         char* new_buf;
1972         unsigned offset, s_offset, via_offset;
1973         char* buf;
1974         unsigned int len;
1975
1976         buf=msg->buf;
1977         len=msg->len;
1978         new_buf=0;
1979         /* we must remove the first via */
1980         if (msg->via1->next) {
1981                 via_len=msg->via1->bsize;
1982                 via_offset=msg->h_via1->body.s-buf;
1983         } else {
1984                 via_len=msg->h_via1->len;
1985                 via_offset=msg->h_via1->name.s-buf;
1986         }
1987
1988              /* Calculate message body difference and adjust
1989               * Content-Length
1990               */
1991         body_delta = lumps_len(msg, msg->body_lumps, 0);
1992         if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
1993                         < 0) {
1994                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1995                                 " Content-Length\n");
1996                 goto error;
1997         }
1998
1999         /* remove the first via*/
2000         if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
2001                 LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
2002                                         "via\n");
2003                 goto error;
2004         }
2005
2006         new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
2007                                                                                                                 know the send sock */
2008
2009         DBG(" old size: %d, new size: %d\n", len, new_len);
2010         new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
2011                                                                                          (\0 to print it )*/
2012         if (new_buf==0){
2013                 LOG(L_ERR, "ERROR: build_res_buf_from_sip_res: out of mem\n");
2014                 goto error;
2015         }
2016         new_buf[new_len]=0; /* debug: print the message */
2017         offset=s_offset=0;
2018         /*FIXME: no send sock*/
2019         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0);/*FIXME:*/
2020         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0);
2021         /* copy the rest of the message */
2022         memcpy(new_buf+offset,
2023                 buf+s_offset,
2024                 len-s_offset);
2025          /* send it! */
2026         DBG("build_res_from_sip_res: copied size: orig:%d, new: %d, rest: %d"
2027                         " msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
2028
2029         *returned_len=new_len;
2030         return new_buf;
2031 error:
2032         *returned_len=0;
2033         return 0;
2034 }
2035
2036
2037 char * build_res_buf_from_sip_req( unsigned int code, str *text ,str *new_tag,
2038                 struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
2039 {
2040         char              *buf, *p;
2041         unsigned int      len,foo;
2042         struct hdr_field  *hdr;
2043         struct lump_rpl   *lump;
2044         struct lump_rpl   *body;
2045         int               i;
2046         char*             received_buf;
2047         unsigned int      received_len;
2048         char*             rport_buf;
2049         unsigned int      rport_len;
2050         char*             warning_buf;
2051         unsigned int      warning_len;
2052         char*             content_len_buf;
2053         unsigned int      content_len_len;
2054         char *after_body;
2055         str  to_tag;
2056         char *totags;
2057
2058         body = 0;
2059         buf=0;
2060         received_buf=rport_buf=warning_buf=content_len_buf=0;
2061         received_len=rport_len=warning_len=content_len_len=0;
2062
2063         to_tag.s=0;  /* fixes gcc 4.0 warning */
2064         to_tag.len=0;
2065
2066         /* force parsing all headers -- we want to return all
2067         Via's in the reply and they may be scattered down to the
2068         end of header (non-block Vias are a really poor property
2069         of SIP :( ) */
2070         if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
2071                 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
2072                         "alas, parse_headers failed\n");
2073                 goto error00;
2074         }
2075
2076         /*computes the length of the new response buffer*/
2077         len = 0;
2078
2079         /* check if received needs to be added */
2080         if (received_test(msg)) {
2081                 if ((received_buf=received_builder(msg,&received_len))==0) {
2082                         LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
2083                                 "alas, received_builder failed\n");
2084                         goto error00;
2085                 }
2086         }
2087         /* check if rport needs to be updated */
2088         if ( ((msg->msg_flags|global_req_flags)&FL_FORCE_RPORT)||
2089                 (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
2090                 if ((rport_buf=rport_builder(msg, &rport_len))==0){
2091                         LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"
2092                                                         " rport_builder failed\n");
2093                         goto error01; /* free everything */
2094                 }
2095                 if (msg->via1->rport)
2096                         len -= msg->via1->rport->size+1; /* include ';' */
2097         }
2098
2099         /* first line */
2100         len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
2101                 text->len + CRLF_LEN/*new line*/;
2102         /*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
2103         for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
2104                 switch (hdr->type) {
2105                         case HDR_TO_T:
2106                                 if (new_tag && new_tag->len) {
2107                                         to_tag=get_to(msg)->tag_value;
2108                                         if (to_tag.len )
2109                                                 len+=new_tag->len-to_tag.len;
2110                                         else
2111                                                 len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
2112                                 }
2113                                 len += hdr->len;
2114                                 break;
2115                         case HDR_VIA_T:
2116                                 /* we always add CRLF to via*/
2117                                 len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
2118                                 if (hdr==msg->h_via1) len += received_len+rport_len;
2119                                 break;
2120                         case HDR_RECORDROUTE_T:
2121                                 /* RR only for 1xx and 2xx replies */
2122                                 if (code<180 || code>=300)
2123                                         break;
2124                         case HDR_FROM_T:
2125                         case HDR_CALLID_T:
2126                         case HDR_CSEQ_T:
2127                                 /* we keep the original termination for these headers*/
2128                                 len += hdr->len;
2129                                 break;
2130                         default:
2131                                 /* do nothing, we are interested only in the above headers */
2132                                 ;
2133                 }
2134         }
2135         /* lumps length */
2136         for(lump=msg->reply_lump;lump;lump=lump->next) {
2137                 len += lump->text.len;
2138                 if (lump->flags&LUMP_RPL_BODY)
2139                         body = lump;
2140         }
2141         /* server header */
2142         if (server_signature)
2143                 len += server_hdr.len + CRLF_LEN;
2144         /* warning hdr */
2145         if (sip_warning) {
2146                 warning_buf = warning_builder(msg,&warning_len);
2147                 if (warning_buf) len += warning_len + CRLF_LEN;
2148                 else LOG(L_WARN, "WARNING: warning skipped -- too big\n");
2149         }
2150         /* content length hdr */
2151         if (body) {
2152                 content_len_buf = int2str(body->text.len, (int*)&content_len_len);
2153                 len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
2154         } else {
2155                 len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
2156         }
2157         /* end of message */
2158         len += CRLF_LEN; /*new line*/
2159
2160         /*allocating mem*/
2161         buf = (char*) pkg_malloc( len+1 );
2162         if (!buf)
2163         {
2164                 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: out of memory "
2165                         " ; needs %d\n",len);
2166                 goto error01;
2167         }
2168
2169         /* filling the buffer*/
2170         p=buf;
2171         /* first line */
2172         memcpy( p , msg->first_line.u.request.version.s , 
2173                 msg->first_line.u.request.version.len);
2174         p += msg->first_line.u.request.version.len;
2175         *(p++) = ' ' ;
2176         /*code*/
2177         for ( i=2 , foo = code  ;  i>=0  ;  i-- , foo=foo/10 )
2178                 *(p+i) = '0' + foo - ( foo/10 )*10;
2179         p += 3;
2180         *(p++) = ' ' ;
2181         memcpy( p , text->s , text->len );
2182         p += text->len;
2183         memcpy( p, CRLF, CRLF_LEN );
2184         p+=CRLF_LEN;
2185         /* headers*/
2186         for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
2187                 switch (hdr->type)
2188                 {
2189                         case HDR_VIA_T:
2190                                 if (hdr==msg->h_via1){
2191                                         if (rport_buf){
2192                                                 if (msg->via1->rport){ /* delete the old one */
2193                                                         /* copy until rport */
2194                                                         append_str_trans( p, hdr->name.s ,
2195                                                                 msg->via1->rport->start-hdr->name.s-1,msg);
2196                                                         /* copy new rport */
2197                                                         append_str(p, rport_buf, rport_len);
2198                                                         /* copy the rest of the via */
2199                                                         append_str_trans(p, msg->via1->rport->start+
2200                                                                                                 msg->via1->rport->size,
2201                                                                                                 hdr->body.s+hdr->body.len-
2202                                                                                                 msg->via1->rport->start-
2203                                                                                                 msg->via1->rport->size, msg);
2204                                                 }else{ /* just append the new one */
2205                                                         /* normal whole via copy */
2206                                                         append_str_trans( p, hdr->name.s ,
2207                                                                 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2208                                                         append_str(p, rport_buf, rport_len);
2209                                                 }
2210                                         }else{
2211                                                 /* normal whole via copy */
2212                                                 append_str_trans( p, hdr->name.s ,
2213                                                                 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2214                                         }
2215                                         if (received_buf)
2216                                                 append_str( p, received_buf, received_len);
2217                                 }else{
2218                                         /* normal whole via copy */
2219                                         append_str_trans( p, hdr->name.s,
2220                                                         (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2221                                 }
2222                                 append_str( p, CRLF,CRLF_LEN);
2223                                 break;
2224                         case HDR_RECORDROUTE_T:
2225                                 /* RR only for 1xx and 2xx replies */
2226                                 if (code<180 || code>=300) break;
2227                                 append_str(p, hdr->name.s, hdr->len);
2228                                 break;
2229                         case HDR_TO_T:
2230                                 if (new_tag && new_tag->len){
2231                                         if (to_tag.s ) { /* replacement */
2232                                                 /* before to-tag */
2233                                                 append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
2234                                                 /* to tag replacement */
2235                                                 bmark->to_tag_val.s=p;
2236                                                 bmark->to_tag_val.len=new_tag->len;
2237                                                 append_str( p, new_tag->s,new_tag->len);
2238                                                 /* the rest after to-tag */
2239                                                 append_str( p, to_tag.s+to_tag.len,
2240                                                         hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
2241                                         }else{ /* adding a new to-tag */
2242                                                 after_body=hdr->body.s+hdr->body.len;
2243                                                 append_str( p, hdr->name.s, after_body-hdr->name.s);
2244                                                 append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
2245                                                 bmark->to_tag_val.s=p;
2246                                                 bmark->to_tag_val.len=new_tag->len;
2247                                                 append_str( p, new_tag->s,new_tag->len);
2248                                                 append_str( p, after_body,
2249                                                                                 hdr->name.s+hdr->len-after_body);
2250                                         }
2251                                         break;
2252                                 } /* no new to-tag -- proceed to 1:1 copying  */
2253                                 totags=((struct to_body*)(hdr->parsed))->tag_value.s;
2254                                 if (totags) {
2255                                         bmark->to_tag_val.s=p+(totags-hdr->name.s);
2256                                         bmark->to_tag_val.len=
2257                                                         ((struct to_body*)(hdr->parsed))->tag_value.len;
2258                                 }
2259                                 else {
2260                                         bmark->to_tag_val.len = 0;
2261                                         bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
2262                                 }
2263                                 /* no break */
2264                         case HDR_FROM_T:
2265                         case HDR_CALLID_T:
2266                         case HDR_CSEQ_T:
2267                                         append_str(p, hdr->name.s, hdr->len);
2268                                         break;
2269                         default:
2270                                 /* do nothing, we are interested only in the above headers */
2271                                 ;
2272                 } /* end switch */
2273         } /* end for */
2274         /* lumps */
2275         for(lump=msg->reply_lump;lump;lump=lump->next)
2276                 if (lump->flags&LUMP_RPL_HDR){
2277                         memcpy(p,lump->text.s,lump->text.len);
2278                         p += lump->text.len;
2279                 }
2280         /* server header */
2281         if (server_signature) {
2282                 memcpy( p, server_hdr.s, server_hdr.len );
2283                 p+=server_hdr.len;
2284                 memcpy( p, CRLF, CRLF_LEN );
2285                 p+=CRLF_LEN;
2286         }
2287         /* content_length hdr */
2288         if (content_len_len) {
2289                 append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
2290                 append_str( p, content_len_buf, content_len_len );
2291                 append_str( p, CRLF, CRLF_LEN );
2292         } else {
2293                 append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
2294         }
2295         /* warning header */
2296         if (warning_buf) {
2297                 memcpy( p, warning_buf, warning_len);
2298                 p+=warning_len;
2299                 memcpy( p, CRLF, CRLF_LEN);
2300                 p+=CRLF_LEN;
2301         }
2302         /*end of message*/
2303         memcpy( p, CRLF, CRLF_LEN );
2304         p+=CRLF_LEN;
2305         /* body */
2306         if (body) {
2307                 memcpy ( p, body->text.s, body->text.len );
2308                 p+=body->text.len;
2309         }
2310
2311         if (len!=p-buf)
2312                 LOG(L_CRIT,"BUG:build_res_from_sip_req: diff len=%d p-buf=%d\n",
2313                                         len, (int)(p-buf));
2314
2315         *(p) = 0;
2316         *returned_len = len;
2317         /* in req2reply, received_buf is not introduced to lumps and
2318            needs to be deleted here
2319         */
2320         if (received_buf) pkg_free(received_buf);
2321         if (rport_buf) pkg_free(rport_buf);
2322         return buf;
2323
2324 error01:
2325         if (received_buf) pkg_free(received_buf);
2326         if (rport_buf) pkg_free(rport_buf);
2327 error00:
2328         *returned_len=0;
2329         return 0;
2330 }
2331
2332
2333
2334 /* return number of chars printed or 0 if space exceeded;
2335    assumes buffer size of at least MAX_BRANCH_PARAM_LEN
2336  */
2337 int branch_builder( unsigned int hash_index,
2338         /* only either parameter useful */
2339         unsigned int label, char * char_v,
2340         int branch,
2341         char *branch_str, int *len )
2342 {
2343
2344         char *begin;
2345         int size;
2346
2347         /* hash id provided ... start with it */
2348         size=MAX_BRANCH_PARAM_LEN;
2349         begin=branch_str;
2350         *len=0;
2351
2352         memcpy(begin, MCOOKIE, MCOOKIE_LEN );
2353         size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
2354
2355         if (int2reverse_hex( &begin, &size, hash_index)==-1)
2356                 return 0;
2357
2358         if (size) {
2359                 *begin=BRANCH_SEPARATOR;
2360                 begin++; size--;
2361         } else return 0;
2362
2363         /* string with request's characteristic value ... use it ... */
2364         if (char_v) {
2365                 if (memcpy(begin,char_v,MD5_LEN)) {
2366                         begin+=MD5_LEN; size-=MD5_LEN;
2367                 } else return 0;
2368         } else { /* ... use the "label" value otherwise */
2369                 if (int2reverse_hex( &begin, &size, label )==-1)
2370                         return 0;
2371         }
2372
2373         if (size) {
2374                 *begin=BRANCH_SEPARATOR;
2375                 begin++; size--;
2376         } else return 0;
2377
2378         if (int2reverse_hex( &begin, &size, branch)==-1)
2379                 return 0;
2380
2381         *len=MAX_BRANCH_PARAM_LEN-size;
2382         return size;
2383
2384 }
2385
2386
2387
2388 /* uses only the send_info->send_socket, send_info->proto and 
2389  * send_info->comp (so that a send_info used for sending can be passed
2390  * to this function w/o changes and the correct via will be built) */
2391 char* via_builder( unsigned int *len,
2392         struct dest_info* send_info /* where to send the reply */,
2393         str* branch, str* extra_params, struct hostport* hp)
2394 {
2395         unsigned int  via_len, extra_len;
2396         char               *line_buf;
2397         int max_len;
2398         int via_prefix_len;
2399         str* address_str; /* address displayed in via */
2400         str* port_str; /* port no displayed in via */
2401         struct socket_info* send_sock;
2402         int comp_len, comp_name_len;
2403 #ifdef USE_COMP
2404         char* comp_name;
2405 #endif /* USE_COMP */
2406         int port;
2407         struct ip_addr ip;
2408         union sockaddr_union *from = NULL;
2409         union sockaddr_union local_addr;
2410         struct tcp_connection *con = NULL;
2411
2412         send_sock=send_info->send_sock;
2413         /* use pre-set address in via, the outbound socket alias or address one */
2414         if (hp && hp->host->len)
2415                 address_str=hp->host;
2416         else if(send_sock->useinfo.name.len>0)
2417                 address_str=&(send_sock->useinfo.name);
2418         else
2419                 address_str=&(send_sock->address_str);
2420         if (hp && hp->port->len)
2421                 port_str=hp->port;
2422         else if(send_sock->useinfo.port_no>0)
2423                 port_str=&(send_sock->useinfo.port_no_str);
2424         else
2425                 port_str=&(send_sock->port_no_str);
2426         
2427         comp_len=comp_name_len=0;
2428 #ifdef USE_COMP
2429         comp_name=0;
2430         switch(send_info->comp){
2431                 case COMP_NONE:
2432                         break;
2433                 case COMP_SIGCOMP:
2434                         comp_len=COMP_PARAM_LEN;
2435                         comp_name_len=SIGCOMP_NAME_LEN;
2436                         comp_name=SIGCOMP_NAME;
2437                         break;
2438                 case COMP_SERGZ:
2439                         comp_len=COMP_PARAM_LEN;
2440                         comp_name_len=SERGZ_NAME_LEN;
2441                         comp_name=SERGZ_NAME;
2442                         break;
2443                 default:
2444                         LOG(L_CRIT, "BUG: via_builder: unknown comp %d\n",
2445                                         send_info->comp);
2446                         /* continue, we'll just ignore comp */
2447         }
2448 #endif /* USE_COMP */
2449                         
2450         via_prefix_len=MY_VIA_LEN+(send_info->proto==PROTO_SCTP);
2451         max_len=via_prefix_len +address_str->len /* space in MY_VIA */
2452                 +2 /* just in case it is a v6 address ... [ ] */
2453                 +1 /*':'*/+port_str->len
2454                 +(branch?(MY_BRANCH_LEN+branch->len):0)
2455                 +(extra_params?extra_params->len:0)
2456                 +comp_len+comp_name_len
2457                 +CRLF_LEN+1;
2458         line_buf=pkg_malloc( max_len );
2459         if (line_buf==0){
2460                 ser_error=E_OUT_OF_MEM;
2461                 LOG(L_ERR, "ERROR: via_builder: out of memory\n");
2462                 return 0;
2463         }
2464
2465         extra_len=0;
2466
2467         via_len=via_prefix_len+address_str->len; /*space included in MY_VIA*/
2468
2469         memcpy(line_buf, MY_VIA, MY_VIA_LEN);
2470         if (send_info->proto==PROTO_UDP){
2471                 /* do nothing */
2472         }else if (send_info->proto==PROTO_TCP){
2473                 memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
2474         }else if (send_info->proto==PROTO_TLS){
2475                 memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
2476         }else if (send_info->proto==PROTO_SCTP){
2477                 memcpy(line_buf+MY_VIA_LEN-4, "SCTP ", 5);
2478         }else if (send_info->proto==PROTO_WS){
2479                 if (unlikely(send_info->send_flags.f & SND_F_FORCE_SOCKET
2480                                 && send_info->send_sock)) {
2481                         local_addr = send_info->send_sock->su;
2482                         su_setport(&local_addr, 0); /* any local port will do */
2483                         from = &local_addr;
2484                 }
2485
2486                 port = su_getport(&send_info->to);
2487                 if (likely(port)) {
2488                         su2ip_addr(&ip, &send_info->to);
2489                         con = tcpconn_get(send_info->id, &ip, port, from, 0);
2490                 }
2491                 else if (likely(send_info->id))
2492                         con = tcpconn_get(send_info->id, 0, 0, 0, 0);
2493                 else {
2494                         LM_CRIT("BUG: via_builder called with null_id & to\n");
2495                         return 0;
2496                 }
2497
2498                 if (con == NULL) {
2499                         LM_WARN("TCP/TLS connection for WebSocket could not be found\n");
2500                         return 0;
2501                 }
2502
2503                 if (con->rcv.proto==PROTO_WS) {
2504                         memcpy(line_buf+MY_VIA_LEN-4, "WS ", 3);
2505                 } else if (con->rcv.proto==PROTO_WSS) {
2506                         memcpy(line_buf+MY_VIA_LEN-4, "WSS ", 4);
2507                 } else {
2508                         LOG(L_CRIT, "BUG: via_builder: unknown proto %d\n", con->rcv.proto);
2509                         return 0;
2510                 }
2511         }else if (send_info->proto==PROTO_WSS){
2512                 memcpy(line_buf+MY_VIA_LEN-4, "WSS ", 4);
2513         }else{
2514                 LOG(L_CRIT, "BUG: via_builder: unknown proto %d\n", send_info->proto);
2515                 return 0;
2516         }
2517 #       ifdef USE_IPV6
2518         /* add [] only if ipv6 and outbound socket address is used;
2519          * if using pre-set no check is made */
2520         if ((send_sock->address.af==AF_INET6) &&
2521                 (address_str==&(send_sock->address_str))) {
2522                 line_buf[via_prefix_len]='[';
2523                 line_buf[via_prefix_len+1+address_str->len]=']';
2524                 extra_len=1;
2525                 via_len+=2; /* [ ]*/
2526         }
2527 #       endif
2528         memcpy(line_buf+via_prefix_len+extra_len, address_str->s,
2529                                 address_str->len);
2530         if ((send_sock->port_no!=SIP_PORT) ||
2531                                 (port_str!=&send_sock->port_no_str)){
2532                 line_buf[via_len]=':'; via_len++;
2533                 memcpy(line_buf+via_len, port_str->s, port_str->len);
2534                 via_len+=port_str->len;
2535         }
2536
2537         /* branch parameter */
2538         if (branch){
2539                 memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
2540                 via_len+=MY_BRANCH_LEN;
2541                 memcpy(line_buf+via_len, branch->s, branch->len );
2542                 via_len+=branch->len;
2543         }
2544         /* extra params  */
2545         if (extra_params){
2546                 memcpy(line_buf+via_len, extra_params->s, extra_params->len);
2547                 via_len+=extra_params->len;
2548         }
2549 #ifdef USE_COMP
2550         /* comp */
2551         if (comp_len){
2552                 memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
2553                 via_len+=COMP_PARAM_LEN;
2554                 memcpy(line_buf+via_len, comp_name, comp_name_len);
2555                 via_len+=comp_name_len;
2556         }
2557 #endif
2558
2559         memcpy(line_buf+via_len, CRLF, CRLF_LEN);
2560         via_len+=CRLF_LEN;
2561         line_buf[via_len]=0; /* null terminate the string*/
2562
2563         *len = via_len;
2564         return line_buf;
2565 }
2566
2567 /* creates a via header honoring the protocol of the incomming socket
2568  * msg is an optional parameter */
2569 char* create_via_hf( unsigned int *len,
2570         struct sip_msg *msg,
2571         struct dest_info* send_info /* where to send the reply */,
2572         str* branch)
2573 {
2574         char* via;
2575         str extra_params;
2576         struct hostport hp;
2577 #if defined USE_TCP || defined USE_SCTP
2578         char* id_buf;
2579         unsigned int id_len;
2580
2581
2582         id_buf=0;
2583         id_len=0;
2584 #endif
2585         extra_params.len=0;
2586         extra_params.s=0;
2587
2588
2589 #if defined USE_TCP || defined USE_SCTP
2590         /* add id if tcp */
2591         if (msg && (
2592 #ifdef USE_TCP
2593                 (msg->rcv.proto==PROTO_TCP)
2594 #ifdef USE_TLS
2595                         || (msg->rcv.proto==PROTO_TLS)
2596 #endif
2597 #ifdef USE_SCTP
2598                         ||
2599 #endif /* USE_SCTP */
2600 #endif /* USE_TCP */
2601 #ifdef USE_SCTP
2602                         (msg->rcv.proto==PROTO_SCTP)
2603 #endif /* USE_SCTP */
2604                         )){
2605                 if  ((id_buf=id_builder(msg, &id_len))==0){
2606                         LOG(L_ERR, "ERROR: create_via_hf:"
2607                                                         " id_builder failed\n");
2608                         return 0; /* we don't need to free anything,
2609                                          nothing alloc'ed yet*/
2610                 }
2611                 DBG("create_via_hf: id added: <%.*s>, rcv proto=%d\n",
2612                                 (int)id_len, id_buf, msg->rcv.proto);
2613                 extra_params.s=id_buf;
2614                 extra_params.len=id_len;
2615         }
2616 #endif /* USE_TCP || USE_SCTP */
2617
2618         /* test and add rport parameter to local via - rfc3581 */
2619         if(msg->msg_flags&FL_ADD_LOCAL_RPORT) {
2620                 /* params so far + ';rport' + '\0' */
2621                 via = (char*)pkg_malloc(extra_params.len+RPORT_LEN);
2622                 if(via==0) {
2623                         LM_ERR("building local rport via param failed\n");
2624                         if (extra_params.s) pkg_free(extra_params.s);
2625                         return 0;
2626                 }
2627                 if(extra_params.len!=0) {
2628                         memcpy(via, extra_params.s, extra_params.len);
2629                         pkg_free(extra_params.s);
2630                 }
2631                 memcpy(via + extra_params.len, RPORT, RPORT_LEN-1);
2632                 extra_params.s = via;
2633                 extra_params.len += RPORT_LEN-1;
2634                 extra_params.s[extra_params.len]='\0';
2635         }
2636
2637         set_hostport(&hp, msg);
2638         via = via_builder( len, send_info, branch,
2639                                                         extra_params.len?&extra_params:0, &hp);
2640
2641         /* we do not need extra_params any more, already in the new via header */
2642         if (extra_params.s) pkg_free(extra_params.s);
2643         return via;
2644 }
2645
2646 /* builds a char* buffer from message headers without body
2647  * first line is excluded in case of skip_first_line=1
2648  * error is set -1 if the memory allocation failes
2649  */
2650 char * build_only_headers( struct sip_msg* msg, int skip_first_line,
2651                                 unsigned int *returned_len,
2652                                 int *error,
2653                                 struct dest_info* send_info)
2654 {
2655         char            *buf, *new_buf;
2656         unsigned int    offset, s_offset, len, new_len;
2657
2658         *error = 0;
<