msg_translator.c
552f030b
 /*
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
6a0f4382
  * This file is part of Kamailio, a free SIP server.
7dd0b342
  *
6a0f4382
  * Kamailio is free software; you can redistribute it and/or modify
7dd0b342
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
6a0f4382
  * Kamailio is distributed in the hope that it will be useful,
7dd0b342
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
552f030b
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
552f030b
  *
0bf716b8
  *
  *
9d045b03
  */
6a0f4382
 /** Via special params:
3e8c3475
  * requests:
  * - if the address in via is different from the src_ip or an existing
  *   received=something is found, received=src_ip is added (and any preexisting
  *   received is deleted). received is added as the first via parameter if no
53c7e0f1
  *   receive is previously present or over the old receive.
3e8c3475
  * - if the original via contains rport / rport=something or msg->msg_flags
  *   FL_FORCE_RPORT is set (e.g. script force_rport() cmd) rport=src_port
  *   is added (over previous rport / as first via param or after received
  *   if no received was present and received is added too)
  * local replies:
  *    (see also sl_send_reply)
552f030b
  *  - rport and received are added in mostly the same way as for requests, but
  *    in the reverse order (first rport and then received). See also
3e8c3475
  *    limitations.
  *  - if reply_to_via is set (default off) the local reply will be sent to
  *    the address in via (received is ignored since it was not set by us). The
  *    destination port is either the message source port if via contains rport
  *    or the FL_FORCE_RPORT flag is set or the port from the via. If either
  *    port or rport are present a normal dns lookup (instead of a srv lookup)
552f030b
  *    is performed on the address. If no port is present and a srv lookup is
3e8c3475
  *    performed the port is taken from the srv lookup. If the srv lookup failed
  *    or it was not performed, the port is set to the default sip port (5060).
a7e13a89
  *  - if reply_to_via is off (default) the local reply is sent to the message
552f030b
  *    source ip address. The destination port is set to the source port if
3e8c3475
  *    rport is present or FL_FORCE_RPORT flag is set, to the via port or to
  *    the default sip port (5060) if neither rport or via port are present.
  * "normal" replies:
  *  - if received is present the message is sent to the received address else
552f030b
  *    if no port is present (neither a normal via port or rport) a dns srv
  *    lookup is performed on the host part and the reply is sent to the
  *    resulting ip. If a port is present or the host part is an ip address
3e8c3475
  *    the dns lookup will be a "normal" one (A or AAAA).
  *  - if rport is present, it's value will be used as the destination port
  *   (and this will also disable srv lookups)
  *  - if no port is present the destination port will be taken from the srv
  *    lookup. If the srv lookup fails or is not performed (e.g. ip address
  *    in host) the destination port will be set to the default sip port (5060).
552f030b
  *
3e8c3475
  * Known limitations:
  * - when locally replying to a message, rport and received will be appended to
  *   the via header parameters (for forwarded requests they are inserted at the
  *   beginning).
  * - a locally generated reply might get two received via parameters if a
  *   received is already present in the original message (this should not
  *   happen though, but ...)
  *
  *--andrei
 */
7dd0b342
 
1d0661db
 /*!
  * \file
6a0f4382
  * \brief Kamailio core :: Message translations
1d0661db
  * \ingroup core
  * Module: \ref core
  */
 
381659ac
 #include <sys/types.h>
dd01b806
 #include <sys/socket.h>
e22bbdb8
 #include <netdb.h>
 #include <string.h>
5e39b907
 #include <stdio.h>
20374571
 #include <stdlib.h>
dd01b806
 
1540fad1
 #include "comp_defs.h"
26b939c6
 #include "msg_translator.h"
4e2fdd79
 #include "globals.h"
1400b772
 #include "error.h"
dda9dab1
 #include "mem/mem.h"
26b939c6
 #include "dprint.h"
 #include "config.h"
676eb608
 #include "md5utils.h"
4c37101e
 #include "data_lump.h"
0498ceb9
 #include "data_lump_rpl.h"
4e2fdd79
 #include "ip_addr.h"
 #include "resolve.h"
caf80ae6
 #include "ut.h"
f51155cf
 #include "pt.h"
bf8dfee3
 #include "cfg/cfg.h"
08739917
 #include "parser/parse_to.h"
8f007d20
 #include "parser/parse_param.h"
bf8dfee3
 #include "forward.h"
8f007d20
 #include "str_list.h"
db11faca
 #include "rand/kam_rand.h"
26b939c6
 
049f64c2
 #define append_str_trans(_dest,_src,_len,_msg) \
 	append_str( (_dest), (_src), (_len) );
340bddb4
 
5655ca6f
 extern char version[];
 extern int version_len;
26b939c6
 
 
049f64c2
 
bf8dfee3
 /** per process fixup function for global_req_flags.
   * It should be called from the configuration framework.
   */
33bfeb9d
 void fix_global_req_flags(str* gname, str* name)
bf8dfee3
 {
 	global_req_flags=0;
 	switch(cfg_get(core, core_cfg, udp_mtu_try_proto)){
 		case PROTO_NONE:
 		case PROTO_UDP:
 			/* do nothing */
 			break;
 		case PROTO_TCP:
 			global_req_flags|=FL_MTU_TCP_FB;
 			break;
 		case PROTO_TLS:
 			global_req_flags|=FL_MTU_TLS_FB;
 			break;
 		case PROTO_SCTP:
 			global_req_flags|=FL_MTU_SCTP_FB;
 			break;
 	}
 	if (cfg_get(core, core_cfg, force_rport))
 		global_req_flags|=FL_FORCE_RPORT;
 }
049f64c2
 
6bfaa042
 
 
049f64c2
 /* checks if ip is in host(name) and ?host(ip)=name?
  * ip must be in network byte order!
  *  resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
  * return 0 if equal */
552f030b
 static int check_via_address(struct ip_addr* ip, str *name,
049f64c2
 				unsigned short port, int resolver)
 {
 	struct hostent* he;
 	int i;
 	char* s;
116f7bad
 	int len;
aff1dcd3
 	char lproto;
049f64c2
 
 	/* maybe we are lucky and name it's an ip */
 	s=ip_addr2a(ip);
 	if (s){
80d01114
 		LM_DBG("(%s, %.*s, %d)\n", s, name->len, name->s, resolver);
049f64c2
 
85de7fe6
 		len=strlen(s);
 
116f7bad
 		/* check if name->s is an ipv6 address or an ipv6 address ref. */
 		if ((ip->af==AF_INET6) &&
 				(	((len==name->len)&&(strncasecmp(name->s, s, name->len)==0))
 					||
 					((len==(name->len-2))&&(name->s[0]=='[')&&
 						(name->s[name->len-1]==']')&&
 						(strncasecmp(name->s+1, s, len)==0))
 				)
 		   )
049f64c2
 			return 0;
 		else
 
552f030b
 			if (strncmp(name->s, s, name->len)==0)
049f64c2
 				return 0;
 	}else{
eeeb3693
 		LM_CRIT("could not convert ip address\n");
049f64c2
 		return -1;
 	}
 
 	if (port==0) port=SIP_PORT;
 	if (resolver&DO_DNS){
80d01114
 		LM_DBG("doing dns lookup\n");
049f64c2
 		/* try all names ips */
aff1dcd3
 		lproto = PROTO_NONE;
 		he=sip_resolvehost(name, &port, &lproto); /* don't use naptr */
049f64c2
 		if (he && ip->af==he->h_addrtype){
 			for(i=0;he && he->h_addr_list[i];i++){
 				if ( memcmp(&he->h_addr_list[i], ip->u.addr, ip->len)==0)
 					return 0;
 			}
 		}
 	}
 	if (resolver&DO_REV_DNS){
80d01114
 		LM_DBG("doing rev. dns lookup\n");
049f64c2
 		/* try reverse dns */
 		he=rev_resolvehost(ip);
 		if (he && (strncmp(he->h_name, name->s, name->len)==0))
 			return 0;
 		for (i=0; he && he->h_aliases[i];i++){
 			if (strncmp(he->h_aliases[i],name->s, name->len)==0)
 				return 0;
 		}
 	}
 	return -1;
 }
26b939c6
 
 
194743bf
 /* check if IP address in Via != source IP address of signaling,
  * or the sender requires adding rport or received values */
0b15425f
 int received_test( struct sip_msg *msg )
 {
 	int rcvd;
 
a1e96cbd
 	rcvd=msg->via1->received || msg->via1->rport
0b15425f
 			|| check_via_address(&msg->rcv.src_ip, &msg->via1->host,
 							msg->via1->port, received_dns);
 	return rcvd;
 }
 
194743bf
 /* check if IP address in Via != source IP address of signaling */
 int received_via_test( struct sip_msg *msg )
 {
 	int rcvd;
 
 	rcvd = (check_via_address(&msg->rcv.src_ip, &msg->via1->host,
 							msg->via1->port, received_dns)!=0);
 	return rcvd;
 }
0b15425f
 
0bf716b8
 static char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
5e39b907
 {
 	static char buf[MAX_WARNING_LEN];
 	str *foo;
40686bb1
 	int print_len, l;
 	int clen;
 	char* t;
 
a2339d9a
 #define str_print(string, string_len) \
40686bb1
 		do{ \
a2339d9a
 			l=(string_len); \
 			if ((clen+l)>MAX_WARNING_LEN) \
 				goto error_overflow; \
 			memcpy(buf+clen, (string), l); \
 			clen+=l; \
 		}while(0)
552f030b
 
a2339d9a
 #define str_lenpair_print(string, string_len, string2, string2_len) \
 		do{ \
 			str_print(string, string_len); \
 			str_print(string2, string2_len);\
 		}while(0)
552f030b
 
a2339d9a
 #define str_pair_print( string, string2, string2_len) \
 		str_lenpair_print((string), strlen((string)), (string2), (string2_len))
552f030b
 
40686bb1
 #define str_int_print(string, intval)\
 		do{\
 			t=int2str((intval), &print_len); \
 			str_pair_print(string, t, print_len);\
 		} while(0)
552f030b
 
65a59f7a
 #define str_su_print(sockaddr)\
 		do{\
 			t=su2a(&sockaddr, sizeof(sockaddr)); \
 			print_len=strlen(t); \
 			str_print(t, print_len); \
 		} while(0)
 
40686bb1
 #define str_ipaddr_print(string, ipaddr_val)\
 		do{\
 			t=ip_addr2a((ipaddr_val)); \
 			print_len=strlen(t); \
 			str_pair_print(string, t, print_len);\
 		} while(0)
552f030b
 
a2339d9a
 	clen=0;
65a59f7a
 	str_print(WARNING, WARNING_LEN);
 	str_su_print(msg->rcv.bind_address->su);
a2339d9a
 	str_print(WARNING_PHRASE, WARNING_PHRASE_LEN);
552f030b
 
5e39b907
 	/*adding out_uri*/
 	if (msg->new_uri.s)
 		foo=&(msg->new_uri);
 	else
 		foo=&(msg->first_line.u.request.uri);
a2339d9a
 	/* pid= */
 	str_int_print(" pid=", my_pid());
 	/* req_src_ip= */
 	str_ipaddr_print(" req_src_ip=", &msg->rcv.src_ip);
 	str_int_print(" req_src_port=", msg->rcv.src_port);
 	str_pair_print(" in_uri=", msg->first_line.u.request.uri.s,
 								msg->first_line.u.request.uri.len);
 	str_pair_print(" out_uri=", foo->s, foo->len);
5c28a534
 	str_pair_print(" via_cnt",
 					(msg->parsed_flag & HDR_EOH_F)==HDR_EOH_F ? "=" : ">", 1);
a2339d9a
 	str_int_print("=", via_cnt);
 	if (clen<MAX_WARNING_LEN){ buf[clen]='"'; clen++; }
 	else goto error_overflow;
552f030b
 
 
a2339d9a
 	*returned_len=clen;
 	return buf;
40686bb1
 error_overflow:
eeeb3693
 	LM_NOTICE("buffer size exceeded (probably too long URI)\n");
a2339d9a
 	*returned_len=0;
 	return 0;
5e39b907
 }
3b32ca4b
 
 
 
 
a9a8cb6e
 char* received_builder(struct sip_msg *msg, unsigned int *received_len)
a2688733
 {
 	char *buf;
 	int  len;
 	struct ip_addr *source_ip;
 	char *tmp;
 	int  tmp_len;
 
f2f969dd
 	source_ip=&msg->rcv.src_ip;
a2688733
 
 	buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
 	if (buf==0){
 		ser_error=E_OUT_OF_MEM;
eeeb3693
 		LM_ERR("out of memory\n");
a2688733
 		return 0;
 	}
 	memcpy(buf, RECEIVED, RECEIVED_LEN);
3c062c12
 	if ( (tmp=ip_addr2a(source_ip))==0) {
 		pkg_free(buf);
57e2cd15
 		return 0; /* error*/
3c062c12
 	}
a2688733
 	tmp_len=strlen(tmp);
bd93aa3e
 	len=RECEIVED_LEN+tmp_len;
552f030b
 
0cbace85
 	memcpy(buf+RECEIVED_LEN, tmp, tmp_len);
6419a43f
 	buf[len]=0; /*null terminate it */
a2688733
 
 	*received_len = len;
 	return buf;
 }
 
 
 
09f7cd2c
 char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
 {
 	char* buf;
 	char* tmp;
 	int tmp_len;
 	int len;
552f030b
 
09f7cd2c
 	tmp_len=0;
4c4e112f
 	tmp=int2str(msg->rcv.src_port, &tmp_len);
6419a43f
 	len=RPORT_LEN+tmp_len;
 	buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
09f7cd2c
 	if (buf==0){
 		ser_error=E_OUT_OF_MEM;
eeeb3693
 		LM_ERR("out of memory\n");
09f7cd2c
 		return 0;
 	}
 	memcpy(buf, RPORT, RPORT_LEN);
 	memcpy(buf+RPORT_LEN, tmp, tmp_len);
6419a43f
 	buf[len]=0; /*null terminate it*/
552f030b
 
09f7cd2c
 	*rport_len=len;
 	return buf;
 }
 
 
 
e1e6c914
 char* id_builder(struct sip_msg* msg, unsigned int *id_len)
 {
 	char* buf;
 	int len, value_len;
 	char revhex[sizeof(int)*2];
 	char* p;
 	int size;
552f030b
 
e1e6c914
 	size=sizeof(int)*2;
 	p=&revhex[0];
 	if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
eeeb3693
 		LM_CRIT("not enough space for id\n");
e1e6c914
 		return 0;
 	}
 	value_len=p-&revhex[0];
552f030b
 	len=ID_PARAM_LEN+value_len;
6419a43f
 	buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
e1e6c914
 	if (buf==0){
 		ser_error=E_OUT_OF_MEM;
eeeb3693
 		LM_ERR("out of memory\n");
e1e6c914
 		return 0;
 	}
 	memcpy(buf, ID_PARAM, ID_PARAM_LEN);
 	memcpy(buf+ID_PARAM_LEN, revhex, value_len);
6419a43f
 	buf[len]=0; /* null terminate it */
e1e6c914
 	*id_len=len;
 	return buf;
 }
 
 
 
1ed9c271
 char* clen_builder(	struct sip_msg* msg, int *clen_len, int diff,
05c1b57f
 					int body_only)
ab130758
 {
 	char* buf;
 	int len;
 	int value;
 	char* value_s;
 	int value_len;
 	char* body;
552f030b
 
 
ab130758
 	body=get_body(msg);
 	if (body==0){
 		ser_error=E_BAD_REQ;
1ed9c271
 		LM_ERR("no message body found (missing crlf?) [[%.*s]]\n",
 				msg->len, msg->buf);
ab130758
 		return 0;
 	}
4c37101e
 	value=msg->len-(int)(body-msg->buf)+diff;
ab130758
 	value_s=int2str(value, &value_len);
80d01114
 	LM_DBG("content-length: %d (%s)\n", value, value_s);
552f030b
 
05c1b57f
 	if (body_only) {
 		len=value_len;
 	}
 	else {
 		len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
 	}
ab130758
 	buf=pkg_malloc(sizeof(char)*(len+1));
 	if (buf==0){
 		ser_error=E_OUT_OF_MEM;
eeeb3693
 		LM_ERR("out of memory\n");
ab130758
 		return 0;
 	}
05c1b57f
 	if (body_only) {
 		memcpy(buf, value_s, value_len);
 	}
 	else {
 		memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
 		memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
 		memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
 	}
6419a43f
 	buf[len]=0; /* null terminate it */
ab130758
 	*clen_len=len;
 	return buf;
 }
 
 
 
552f030b
 /* checks if a lump opt condition
d1e2a411
  * returns 1 if cond is true, 0 if false */
b05f7d8f
 static inline int lump_check_opt(	struct lump *l,
d1e2a411
 									struct sip_msg* msg,
7a068ff3
 									struct dest_info* snd_i
d1e2a411
 									)
 {
 	struct ip_addr* ip;
 	unsigned short port;
 	int proto;
 
 #define get_ip_port_proto \
7a068ff3
 			if ((snd_i==0) || (snd_i->send_sock==0)){ \
eeeb3693
 				LM_CRIT("null send socket\n"); \
d1e2a411
 				return 1; /* we presume they are different :-) */ \
 			} \
 			if (msg->rcv.bind_address){ \
 				ip=&msg->rcv.bind_address->address; \
 				port=msg->rcv.bind_address->port_no; \
 				proto=msg->rcv.bind_address->proto; \
 			}else{ \
 				ip=&msg->rcv.dst_ip; \
 				port=msg->rcv.dst_port; \
 				proto=msg->rcv.proto; \
 			} \
552f030b
 
b05f7d8f
 	switch(l->u.cond){
d1e2a411
 		case COND_FALSE:
 			return 0;
 		case COND_TRUE:
b05f7d8f
 			LUMP_SET_COND_TRUE(l);
d1e2a411
 			return 1;
 		case COND_IF_DIFF_REALMS:
 			get_ip_port_proto;
 			/* faster tests first */
7a068ff3
 			if ((port==snd_i->send_sock->port_no) && 
 					(proto==snd_i->send_sock->proto) &&
 #ifdef USE_COMP
 					(msg->rcv.comp==snd_i->comp) &&
 #endif
 					(ip_addr_cmp(ip, &snd_i->send_sock->address)))
d1e2a411
 				return 0;
b05f7d8f
 			else {
 				LUMP_SET_COND_TRUE(l);
 				return 1;
 			}
d1e2a411
 		case COND_IF_DIFF_AF:
 			get_ip_port_proto;
b05f7d8f
 			if (ip->af!=snd_i->send_sock->address.af) {
 				LUMP_SET_COND_TRUE(l);
 				return 1;
 			} else return 0;
d1e2a411
 		case COND_IF_DIFF_PROTO:
 			get_ip_port_proto;
b05f7d8f
 			if (proto!=snd_i->send_sock->proto) {
 				LUMP_SET_COND_TRUE(l);
 				return 1;
 			} else return 0;
d1e2a411
 		case COND_IF_DIFF_PORT:
 			get_ip_port_proto;
b05f7d8f
 			if (port!=snd_i->send_sock->port_no) {
 				LUMP_SET_COND_TRUE(l);
 				return 1;
 			} else return 0;
d1e2a411
 		case COND_IF_DIFF_IP:
 			get_ip_port_proto;
7a068ff3
 			if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
b05f7d8f
 			else {
 				LUMP_SET_COND_TRUE(l);
 				return 1;
 			}
d1e2a411
 		case COND_IF_RAND:
db11faca
 			if(kam_rand()>=KAM_RAND_MAX/2) {
b05f7d8f
 				LUMP_SET_COND_TRUE(l);
 				return 1;
 			} else return 0;
d1e2a411
 		default:
eeeb3693
 			LM_CRIT("unknown lump condition %d\n", l->u.cond);
d1e2a411
 	}
 	return 0; /* false */
 }
 
 
 
b0e03da6
 /* computes the "unpacked" len of a lump list,
    code moved from build_req_from_req */
7a068ff3
 static inline int lumps_len(struct sip_msg* msg, struct lump* lumps, 
 								struct dest_info* send_info)
3b32ca4b
 {
b0e03da6
 	int s_offset;
 	int new_len;
 	struct lump* t;
 	struct lump* r;
6bfaa042
 	str* send_address_str;
 	str* send_port_str;
ff874650
 	str* recv_address_str = NULL;
 	str* recv_port_str = NULL;
 	int  recv_port_no = 0;
7a068ff3
 	struct socket_info* send_sock;
 	
 
 #ifdef USE_COMP
 	#define RCVCOMP_LUMP_LEN \
 						/* add;comp=xxx */ \
 					switch(msg->rcv.comp){ \
 						case COMP_NONE: \
 								break; \
 						case COMP_SIGCOMP: \
 								new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
 								break; \
 						case COMP_SERGZ: \
 								new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
 								break; \
 						default: \
eeeb3693
 						LM_CRIT("unknown comp %d\n", msg->rcv.comp); \
7a068ff3
 					}
26b939c6
 
7a068ff3
 	#define SENDCOMP_LUMP_LEN \
 					/* add;comp=xxx */ \
 					switch(send_info->comp){ \
 						case COMP_NONE: \
 								break; \
 						case COMP_SIGCOMP: \
 								new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
 								break; \
 						case COMP_SERGZ: \
 								new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
 								break; \
 						default: \
eeeb3693
 						LM_CRIT("unknown comp %d\n", send_info->comp); \
7a068ff3
 					}
 #else
 	#define RCVCOMP_LUMP_LEN
 	#define SENDCOMP_LUMP_LEN
 #endif /*USE_COMP */
 	
f15bede1
 #define SUBST_LUMP_LEN(subst_l) \
 		switch((subst_l)->u.subst){ \
 			case SUBST_RCV_IP: \
 				if (msg->rcv.bind_address){ \
574daa6d
 					new_len+=recv_address_str->len; \
f15bede1
 					if (msg->rcv.bind_address->address.af!=AF_INET) \
 						new_len+=2; \
 				}else{ \
 					/* FIXME */ \
eeeb3693
 					LM_CRIT("FIXME: null bind_address\n"); \
f15bede1
 				}; \
 				break; \
 			case SUBST_RCV_PORT: \
 				if (msg->rcv.bind_address){ \
574daa6d
 					new_len+=recv_port_str->len; \
f15bede1
 				}else{ \
 					/* FIXME */ \
eeeb3693
 					LM_CRIT("FIXME: null bind_address\n"); \
f15bede1
 				}; \
 				break; \
 			case SUBST_RCV_PROTO: \
 				if (msg->rcv.bind_address){ \
1ccd1f68
 					switch(msg->rcv.bind_address->proto){ \
4b50480e
 						case PROTO_NONE: \
 						case PROTO_UDP: \
1ccd1f68
 							new_len+=3; \
 							break; \
4b50480e
 						case PROTO_TCP: \
 						case PROTO_TLS: \
1ccd1f68
 							switch(msg->rcv.proto){ \
 								case PROTO_WS: \
 								case PROTO_WSS: \
 									new_len+=2; \
 									break; \
 								default: \
 									new_len+=3; \
 									break; \
 							} \
3d4a77d8
 							break; \
1ccd1f68
 						case PROTO_SCTP: \
 							new_len+=4; \
3d4a77d8
 							break; \
4b50480e
 						default: \
eeeb3693
 						LM_CRIT("unknown proto %d\n", msg->rcv.bind_address->proto); \
4b50480e
 					}\
c55adae8
 				}else{ \
 					/* FIXME */ \
eeeb3693
 					LM_CRIT("FIXME: null bind_address\n"); \
c55adae8
 				}; \
 				break; \
 			case SUBST_RCV_ALL: \
 				if (msg->rcv.bind_address){ \
574daa6d
 					new_len+=recv_address_str->len; \
c55adae8
 					if (msg->rcv.bind_address->address.af!=AF_INET) \
 						new_len+=2; \
574daa6d
 					if (recv_port_no!=SIP_PORT){ \
c55adae8
 						/* add :port_no */ \
574daa6d
 						new_len+=1+recv_port_str->len; \
c55adae8
 					}\
 						/*add;transport=xxx*/ \
1ccd1f68
 					switch(msg->rcv.bind_address->proto){ \
4b50480e
 						case PROTO_NONE: \
 						case PROTO_UDP: \
1ccd1f68
 							break; /* udp is the default */ \
4b50480e
 						case PROTO_TCP: \
 						case PROTO_TLS: \
1ccd1f68
 							switch(msg->rcv.proto){ \
 								case PROTO_WS: \
 								case PROTO_WSS: \
 									new_len+=TRANSPORT_PARAM_LEN+2; \
 									break; \
 								default: \
 									new_len+=TRANSPORT_PARAM_LEN+3; \
 									break; \
 							} \
 							break; \
4b50480e
 						case PROTO_SCTP: \
1ccd1f68
 							new_len+=TRANSPORT_PARAM_LEN+4; \
 							break; \
4b50480e
 						default: \
eeeb3693
 						LM_CRIT("unknown proto %d\n", \
4b50480e
 								msg->rcv.bind_address->proto); \
c55adae8
 					}\
7a068ff3
 					RCVCOMP_LUMP_LEN \
f15bede1
 				}else{ \
 					/* FIXME */ \
eeeb3693
 					LM_CRIT("FIXME: null bind_address\n"); \
f15bede1
 				}; \
 				break; \
 			case SUBST_SND_IP: \
 				if (send_sock){ \
6bfaa042
 					new_len+=send_address_str->len; \
 					if (send_sock->address.af!=AF_INET && \
 							send_address_str==&(send_sock->address_str)) \
f15bede1
 						new_len+=2; \
 				}else{ \
eeeb3693
 					LM_CRIT("FIXME: null send_sock\n"); \
f15bede1
 				}; \
 				break; \
 			case SUBST_SND_PORT: \
 				if (send_sock){ \
6bfaa042
 					new_len+=send_port_str->len; \
f15bede1
 				}else{ \
eeeb3693
 					LM_CRIT("FIXME: null send_sock\n"); \
f15bede1
 				}; \
 				break; \
 			case SUBST_SND_PROTO: \
 				if (send_sock){ \
1ccd1f68
 					switch(send_sock->proto){ \
4b50480e
 						case PROTO_NONE: \
 						case PROTO_UDP: \
1ccd1f68
 							new_len+=3; \
 							break; \
4b50480e
 						case PROTO_TCP: \
 						case PROTO_TLS: \
1ccd1f68
 							switch(send_info->proto){ \
 								case PROTO_WS: \
 								case PROTO_WSS: \
 									new_len+=2; \
 									break; \
 								default: \
 									new_len+=3; \
 									break; \
 							} \
 							break; \
4b50480e
 						case PROTO_SCTP: \
1ccd1f68
 							new_len+=4; \
 							break; \
4b50480e
 						default: \
eeeb3693
 						LM_CRIT("unknown proto %d\n", send_sock->proto); \
4b50480e
 					}\
f15bede1
 				}else{ \
eeeb3693
 					LM_CRIT("FIXME: null send_sock\n"); \
f15bede1
 				}; \
 				break; \
c55adae8
 			case SUBST_SND_ALL: \
 				if (send_sock){ \
6bfaa042
 					new_len+=send_address_str->len; \
 					if ((send_sock->address.af!=AF_INET) && \
 							(send_address_str==&(send_sock->address_str))) \
c55adae8
 						new_len+=2; \
6bfaa042
 					if ((send_sock->port_no!=SIP_PORT) || \
 							(send_port_str!=&(send_sock->port_no_str))){ \
c55adae8
 						/* add :port_no */ \
6bfaa042
 						new_len+=1+send_port_str->len; \
c55adae8
 					}\
4b50480e
 					/*add;transport=xxx*/ \
1ccd1f68
 					switch(send_sock->proto){ \
4b50480e
 						case PROTO_NONE: \
 						case PROTO_UDP: \
1ccd1f68
 							break; /* udp is the default */ \
4b50480e
 						case PROTO_TCP: \
 						case PROTO_TLS: \
1ccd1f68
 							switch(send_info->proto){ \
 								case PROTO_WS: \
 								case PROTO_WSS: \
 									new_len+=TRANSPORT_PARAM_LEN+2; \
 									break; \
 								default: \
 									new_len+=TRANSPORT_PARAM_LEN+3; \
 									break; \
 							} \
 							break; \
4b50480e
 						case PROTO_SCTP: \
1ccd1f68
 							new_len+=TRANSPORT_PARAM_LEN+4; \
 							break; \
4b50480e
 						default: \
eeeb3693
 						LM_CRIT("unknown proto %d\n", send_sock->proto); \
c55adae8
 					}\
7a068ff3
 					SENDCOMP_LUMP_LEN \
c55adae8
 				}else{ \
 					/* FIXME */ \
eeeb3693
 					LM_CRIT("FIXME: null send_sock\n"); \
c55adae8
 				}; \
 				break; \
f15bede1
 			case SUBST_NOP: /* do nothing */ \
 				break; \
 			default: \
eeeb3693
 				LM_CRIT("unknown subst type %d\n", (subst_l)->u.subst); \
f15bede1
 		}
7a068ff3
 	
 	if (send_info){
 		send_sock=send_info->send_sock;
 	}else{
 		send_sock=0;
 	};
26b939c6
 	s_offset=0;
b0e03da6
 	new_len=0;
6bfaa042
 	/* init send_address_str & send_port_str */
574daa6d
 	if(send_sock && send_sock->useinfo.name.len>0)
 		send_address_str=&(send_sock->useinfo.name);
 	else if (msg->set_global_address.len)
6bfaa042
 		send_address_str=&(msg->set_global_address);
 	else
 		send_address_str=&(send_sock->address_str);
574daa6d
 	if(send_sock && send_sock->useinfo.port_no>0)
 		send_port_str=&(send_sock->useinfo.port_no_str);
 	else if (msg->set_global_port.len)
acc6bdfb
 		send_port_str=&(msg->set_global_port);
6bfaa042
 	else
 		send_port_str=&(send_sock->port_no_str);
574daa6d
 	/* init recv_address_str, recv_port_str & recv_port_no */
 	if(msg->rcv.bind_address) {
 		if(msg->rcv.bind_address->useinfo.name.len>0)
 			recv_address_str=&(msg->rcv.bind_address->useinfo.name);
 		else
 			recv_address_str=&(msg->rcv.bind_address->address_str);
 		if(msg->rcv.bind_address->useinfo.port_no>0) {
 			recv_port_str=&(msg->rcv.bind_address->useinfo.port_no_str);
 			recv_port_no = msg->rcv.bind_address->useinfo.port_no;
 		} else {
 			recv_port_str=&(msg->rcv.bind_address->port_no_str);
 			recv_port_no = msg->rcv.bind_address->port_no;
 		}
 	}
552f030b
 
4c37101e
 	for(t=lumps;t;t=t->next){
d1e2a411
 		/* skip if this is an OPT lump and the condition is not satisfied */
b05f7d8f
 		if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t, msg, send_info))
d1e2a411
 			continue;
26b939c6
 		for(r=t->before;r;r=r->before){
 			switch(r->op){
 				case LUMP_ADD:
 					new_len+=r->len;
 					break;
f15bede1
 				case LUMP_ADD_SUBST:
 					SUBST_LUMP_LEN(r);
 					break;
d1e2a411
 				case LUMP_ADD_OPT:
552f030b
 					/* skip if this is an OPT lump and the condition is
d1e2a411
 					 * not satisfied */
b05f7d8f
 					if (!lump_check_opt(r, msg, send_info))
d1e2a411
 						goto skip_before;
 					break;
26b939c6
 				default:
 					/* only ADD allowed for before/after */
eeeb3693
 						LM_CRIT("invalid op for data lump (%x)\n", r->op);
26b939c6
 			}
 		}
d1e2a411
 skip_before:
26b939c6
 		switch(t->op){
 			case LUMP_ADD:
 				new_len+=t->len;
 				break;
f15bede1
 			case LUMP_ADD_SUBST:
 				SUBST_LUMP_LEN(t);
 				break;
d1e2a411
 			case LUMP_ADD_OPT:
 				/* we don't do anything here, it's only a condition for
 				 * before & after */
 				break;
26b939c6
 			case LUMP_DEL:
 				/* fix overlapping deleted zones */
 				if (t->u.offset < s_offset){
 					/* change len */
 					if (t->len>s_offset-t->u.offset)
 							t->len-=s_offset-t->u.offset;
 					else t->len=0;
 					t->u.offset=s_offset;
 				}
 				s_offset=t->u.offset+t->len;
 				new_len-=t->len;
 				break;
 			case LUMP_NOP:
 				/* fix offset if overlapping on a deleted zone */
 				if (t->u.offset < s_offset){
 					t->u.offset=s_offset;
 				}else
 					s_offset=t->u.offset;
 				/* do nothing */
 				break;
e22bbdb8
 			default:
eeeb3693
 				LM_CRIT("invalid op for data lump (%x)\n", r->op);
26b939c6
 		}
 		for (r=t->after;r;r=r->after){
 			switch(r->op){
 				case LUMP_ADD:
 					new_len+=r->len;
 					break;
f15bede1
 				case LUMP_ADD_SUBST:
 					SUBST_LUMP_LEN(r);
 					break;
d1e2a411
 				case LUMP_ADD_OPT:
552f030b
 					/* skip if this is an OPT lump and the condition is
d1e2a411
 					 * not satisfied */
b05f7d8f
 					if (!lump_check_opt(r, msg, send_info))
d1e2a411
 						goto skip_after;
 					break;
26b939c6
 				default:
 					/* only ADD allowed for before/after */
eeeb3693
 					LM_CRIT("invalid op for data lump (%x)\n", r->op);
26b939c6
 			}
 		}
d1e2a411
 skip_after:
e09e5e44
 		; /* to make gcc 3.* happy */
26b939c6
 	}
b0e03da6
 	return new_len;
7a068ff3
 #undef RCVCOMP_LUMP_LEN
 #undef SENDCOMP_LUMP_LEN
b0e03da6
 }
26b939c6
 
 
b0e03da6
 
 /* another helper functions, adds/Removes the lump,
 	code moved form build_req_from_req  */
 
363027e7
 void process_lumps( struct sip_msg* msg,
                     struct lump* lumps,
                     char* new_buf,
                     unsigned int* new_buf_offs,
                     unsigned int* orig_offs,
                     struct dest_info* send_info,
                     int flag)
b0e03da6
 {
 	struct lump *t;
 	struct lump *r;
f15bede1
 	char* orig;
b0e03da6
 	int size;
 	int offset;
 	int s_offset;
6bfaa042
 	str* send_address_str;
 	str* send_port_str;
ff874650
 	str* recv_address_str = NULL;
 	str* recv_port_str = NULL;
 	int  recv_port_no = 0;
7a068ff3
 	struct socket_info* send_sock;
 
 #ifdef USE_COMP
 	#define RCVCOMP_PARAM_ADD \
 				/* add ;comp=xxxx */ \
 				switch(msg->rcv.comp){ \
 					case COMP_NONE: \
 						break; \
 					case COMP_SIGCOMP: \
 						memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
 						offset+=COMP_PARAM_LEN; \
 						memcpy(new_buf+offset, SIGCOMP_NAME, \
 								SIGCOMP_NAME_LEN); \
 						offset+=SIGCOMP_NAME_LEN; \
 						break; \
 					case COMP_SERGZ: \
 						memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
 						offset+=COMP_PARAM_LEN; \
 						memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
 						offset+=SERGZ_NAME_LEN; \
 						break;\
 					default:\
eeeb3693
 						LM_CRIT("unknown comp %d\n", msg->rcv.comp); \
7a068ff3
 				}
 	
 	#define SENDCOMP_PARAM_ADD \
 				/* add ;comp=xxxx */ \
 				switch(send_info->comp){ \
 					case COMP_NONE: \
 						break; \
 					case COMP_SIGCOMP: \
 						memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
 						offset+=COMP_PARAM_LEN; \
 						memcpy(new_buf+offset, SIGCOMP_NAME, \
 								SIGCOMP_NAME_LEN); \
 						offset+=SIGCOMP_NAME_LEN; \
 						break; \
 					case COMP_SERGZ: \
 						memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
 						offset+=COMP_PARAM_LEN; \
 						memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
 						offset+=SERGZ_NAME_LEN; \
 						break;\
 					default:\
eeeb3693
 						LM_CRIT("unknown comp %d\n", msg->rcv.comp); \
7a068ff3
 				} 
 #else
 	#define RCVCOMP_PARAM_ADD
 	#define SENDCOMP_PARAM_ADD
 #endif /* USE_COMP */
f15bede1
 
 #define SUBST_LUMP(subst_l) \
 	switch((subst_l)->u.subst){ \
 		case SUBST_RCV_IP: \
 			if (msg->rcv.bind_address){  \
5646666d
 				if (msg->rcv.bind_address->address.af!=AF_INET){\
 					new_buf[offset]='['; offset++; \
 				}\
574daa6d
 				memcpy(new_buf+offset, recv_address_str->s, \
 						recv_address_str->len); \
 				offset+=recv_address_str->len; \
5646666d
 				if (msg->rcv.bind_address->address.af!=AF_INET){\
 					new_buf[offset]=']'; offset++; \
 				}\
f15bede1
 			}else{  \
 				/*FIXME*/ \
eeeb3693
 				LM_CRIT("FIXME: null bind_address\n"); \
f15bede1
 			}; \
 			break; \
 		case SUBST_RCV_PORT: \
 			if (msg->rcv.bind_address){  \
574daa6d
 				memcpy(new_buf+offset, recv_port_str->s, \
 						recv_port_str->len); \
 				offset+=recv_port_str->len; \
f15bede1
 			}else{  \
 				/*FIXME*/ \
eeeb3693
 				LM_CRIT("FIXME: null bind_address\n"); \
f15bede1
 			}; \
 			break; \
c55adae8
 		case SUBST_RCV_ALL: \
 			if (msg->rcv.bind_address){  \
 				/* address */ \
 				if (msg->rcv.bind_address->address.af!=AF_INET){\
 					new_buf[offset]='['; offset++; \
 				}\
574daa6d
 				memcpy(new_buf+offset, recv_address_str->s, \
 						recv_address_str->len); \
 				offset+=recv_address_str->len; \
c55adae8
 				if (msg->rcv.bind_address->address.af!=AF_INET){\
 					new_buf[offset]=']'; offset++; \
 				}\
 				/* :port */ \
574daa6d
 				if (recv_port_no!=SIP_PORT){ \
c55adae8
 					new_buf[offset]=':'; offset++; \
 					memcpy(new_buf+offset, \
574daa6d
 							recv_port_str->s, \
 							recv_port_str->len); \
 					offset+=recv_port_str->len; \
c55adae8
 				}\
1ccd1f68
 				switch(msg->rcv.bind_address->proto){ \
c55adae8
 					case PROTO_NONE: \
 					case PROTO_UDP: \
 						break; /* nothing to do, udp is default*/ \
 					case PROTO_TCP: \
 						memcpy(new_buf+offset, TRANSPORT_PARAM, \
 								TRANSPORT_PARAM_LEN); \
 						offset+=TRANSPORT_PARAM_LEN; \
1ccd1f68
 						if (msg->rcv.proto == PROTO_WS) { \
 							memcpy(new_buf+offset, "ws", 2); \
 							offset+=2; \
 						} else { \
 							memcpy(new_buf+offset, "tcp", 3); \
 							offset+=3; \
 						} \
c55adae8
 						break; \
 					case PROTO_TLS: \
 						memcpy(new_buf+offset, TRANSPORT_PARAM, \
 								TRANSPORT_PARAM_LEN); \
 						offset+=TRANSPORT_PARAM_LEN; \
1ccd1f68
 						if (msg->rcv.proto == PROTO_WS || msg->rcv.proto == PROTO_WSS) { \
 							memcpy(new_buf+offset, "ws", 2); \
 							offset+=2; \
 						} else { \
 							memcpy(new_buf+offset, "tls", 3); \
 							offset+=3; \
 						} \
c55adae8
 						break; \
4b50480e
 					case PROTO_SCTP: \
 						memcpy(new_buf+offset, TRANSPORT_PARAM, \
 								TRANSPORT_PARAM_LEN); \
 						offset+=TRANSPORT_PARAM_LEN; \
 						memcpy(new_buf+offset, "sctp", 4); \
 						offset+=4; \
 						break; \
c55adae8
 					default: \
eeeb3693
 						LM_CRIT("unknown proto %d\n", msg->rcv.bind_address->proto); \
c55adae8
 				} \
7a068ff3
 				RCVCOMP_PARAM_ADD \
c55adae8
 			}else{  \
 				/*FIXME*/ \
eeeb3693
 				LM_CRIT("FIXME: null bind_address\n"); \
c55adae8
 			}; \
 			break; \
f15bede1
 		case SUBST_SND_IP: \
 			if (send_sock){  \
6bfaa042
 				if ((send_sock->address.af!=AF_INET) && \
 						(send_address_str==&(send_sock->address_str))){\
5646666d
 					new_buf[offset]='['; offset++; \
 				}\
6bfaa042
 				memcpy(new_buf+offset, send_address_str->s, \
 									send_address_str->len); \
 				offset+=send_address_str->len; \
 				if ((send_sock->address.af!=AF_INET) && \
 						(send_address_str==&(send_sock->address_str))){\
5646666d
 					new_buf[offset]=']'; offset++; \
 				}\
f15bede1
 			}else{  \
 				/*FIXME*/ \
eeeb3693
 				LM_CRIT("FIXME: null send_sock\n"); \
f15bede1
 			}; \
 			break; \
 		case SUBST_SND_PORT: \
 			if (send_sock){  \
6bfaa042
 				memcpy(new_buf+offset, send_port_str->s, \
 									send_port_str->len); \
 				offset+=send_port_str->len; \
f15bede1
 			}else{  \
 				/*FIXME*/ \
eeeb3693
 				LM_CRIT("FIXME: null send_sock\n"); \
f15bede1
 			}; \
 			break; \
c55adae8
 		case SUBST_SND_ALL: \
 			if (send_sock){  \
 				/* address */ \
6bfaa042
 				if ((send_sock->address.af!=AF_INET) && \
 						(send_address_str==&(send_sock->address_str))){\
c55adae8
 					new_buf[offset]='['; offset++; \
 				}\
6bfaa042
 				memcpy(new_buf+offset, send_address_str->s, \
 						send_address_str->len); \
 				offset+=send_address_str->len; \
 				if ((send_sock->address.af!=AF_INET) && \
 						(send_address_str==&(send_sock->address_str))){\
c55adae8
 					new_buf[offset]=']'; offset++; \
 				}\
 				/* :port */ \
6bfaa042
 				if ((send_sock->port_no!=SIP_PORT) || \
 					(send_port_str!=&(send_sock->port_no_str))){ \
c55adae8
 					new_buf[offset]=':'; offset++; \
6bfaa042
 					memcpy(new_buf+offset, send_port_str->s, \
 							send_port_str->len); \
 					offset+=send_port_str->len; \
c55adae8
 				}\
1ccd1f68
 				switch(send_sock->proto){ \
c55adae8
 					case PROTO_NONE: \
 					case PROTO_UDP: \
 						break; /* nothing to do, udp is default*/ \
 					case PROTO_TCP: \
 						memcpy(new_buf+offset, TRANSPORT_PARAM, \
 								TRANSPORT_PARAM_LEN); \
 						offset+=TRANSPORT_PARAM_LEN; \
1ccd1f68
 						if (send_info->proto == PROTO_WS) { \
 							memcpy(new_buf+offset, "ws", 2); \
 							offset+=2; \
 						} else { \
 							memcpy(new_buf+offset, "tcp", 3); \
 							offset+=3; \
 						} \
c55adae8
 						break; \
 					case PROTO_TLS: \
 						memcpy(new_buf+offset, TRANSPORT_PARAM, \
 								TRANSPORT_PARAM_LEN); \
 						offset+=TRANSPORT_PARAM_LEN; \
1ccd1f68
 						if (send_info->proto == PROTO_WS || send_info->proto == PROTO_WSS) { \
 							memcpy(new_buf+offset, "ws", 2); \
 							offset+=2; \
 						} else { \
 							memcpy(new_buf+offset, "tls", 3); \
 							offset+=3; \
 						} \
c55adae8
 						break; \
4b50480e
 					case PROTO_SCTP: \
 						memcpy(new_buf+offset, TRANSPORT_PARAM, \
 								TRANSPORT_PARAM_LEN); \
 						offset+=TRANSPORT_PARAM_LEN; \
 						memcpy(new_buf+offset, "sctp", 4); \
 						offset+=4; \
 						break; \
c55adae8
 					default: \
eeeb3693
 						LM_CRIT("unknown proto %d\n", send_sock->proto); \
c55adae8
 				} \
7a068ff3
 				SENDCOMP_PARAM_ADD \
c55adae8
 			}else{  \
 				/*FIXME*/ \
eeeb3693
 				LM_CRIT("FIXME: null bind_address\n"); \
c55adae8
 			}; \
 			break; \
f15bede1
 		case SUBST_RCV_PROTO: \
 			if (msg->rcv.bind_address){ \
1ccd1f68
 				switch(msg->rcv.bind_address->proto){ \
f15bede1
 					case PROTO_NONE: \
 					case PROTO_UDP: \
 						memcpy(new_buf+offset, "udp", 3); \
 						offset+=3; \
 						break; \
 					case PROTO_TCP: \
1ccd1f68
 						if (msg->rcv.proto == PROTO_WS) { \
 							memcpy(new_buf+offset, "ws", 2); \
 							offset+=2; \
 						} else { \
 							memcpy(new_buf+offset, "tcp", 3); \
 							offset+=3; \
 						} \
f15bede1
 						break; \
 					case PROTO_TLS: \
1ccd1f68
 						if (msg->rcv.proto == PROTO_WS || msg->rcv.proto == PROTO_WSS) { \
 							memcpy(new_buf+offset, "ws", 2); \
 							offset+=2; \
 						} else { \
 							memcpy(new_buf+offset, "tls", 3); \
 							offset+=3; \
 						} \
f15bede1
 						break; \
4b50480e
 					case PROTO_SCTP: \
 						memcpy(new_buf+offset, "sctp", 4); \
 						offset+=4; \
 						break; \
f15bede1
 					default: \
eeeb3693
 						LM_CRIT("unknown proto %d\n", msg->rcv.bind_address->proto); \
f15bede1
 				} \
 			}else{  \
 				/*FIXME*/ \
eeeb3693
 				LM_CRIT("FIXME: null send_sock \n"); \
f15bede1
 			}; \
 			break; \
 		case  SUBST_SND_PROTO: \
 			if (send_sock){ \
1ccd1f68
 				switch(send_sock->proto){ \
f15bede1
 					case PROTO_NONE: \
 					case PROTO_UDP: \
 						memcpy(new_buf+offset, "udp", 3); \
 						offset+=3; \
 						break; \
 					case PROTO_TCP: \
1ccd1f68
 						if (send_info->proto == PROTO_WS) { \
 							memcpy(new_buf+offset, "ws", 2); \
 							offset+=2; \
 						} else { \
 							memcpy(new_buf+offset, "tcp", 3); \
 							offset+=3; \
 						} \
f15bede1
 						break; \
 					case PROTO_TLS: \
1ccd1f68
 						if (send_info->proto == PROTO_WS || send_info->proto == PROTO_WSS) { \
 							memcpy(new_buf+offset, "ws", 2); \
 							offset+=2; \
 						} else { \
 							memcpy(new_buf+offset, "tls", 3); \
 							offset+=3; \
 						} \
f15bede1
 						break; \
4b50480e
 					case PROTO_SCTP: \
 						memcpy(new_buf+offset, "sctp", 4); \
 						offset+=4; \
 						break; \
f15bede1
 					default: \
eeeb3693
 						LM_CRIT("unknown proto %d\n", send_sock->proto); \
f15bede1
 				} \
 			}else{  \
 				/*FIXME*/ \
eeeb3693
 				LM_CRIT("FIXME: null send_sock \n"); \
f15bede1
 			}; \
 			break; \
 		default: \
eeeb3693
 				LM_CRIT("unknown subst type %d\n", (subst_l)->u.subst); \
7a068ff3
 	}
552f030b
 
7a068ff3
 	if (send_info){
 		send_sock=send_info->send_sock;
 	}else{
 		send_sock=0;
 	}
6bfaa042
 	/* init send_address_str & send_port_str */
 	if (msg->set_global_address.len)
 		send_address_str=&(msg->set_global_address);
 	else
 		send_address_str=&(send_sock->address_str);
 	if (msg->set_global_port.len)
acc6bdfb
 		send_port_str=&(msg->set_global_port);
6bfaa042
 	else
 		send_port_str=&(send_sock->port_no_str);
574daa6d
 	/* init send_address_str & send_port_str */
 	if(send_sock && send_sock->useinfo.name.len>0)
 		send_address_str=&(send_sock->useinfo.name);
 	else if (msg->set_global_address.len)
 		send_address_str=&(msg->set_global_address);
 	else
 		send_address_str=&(send_sock->address_str);
 	if(send_sock && send_sock->useinfo.port_no>0)
 		send_port_str=&(send_sock->useinfo.port_no_str);
 	else if (msg->set_global_port.len)
 		send_port_str=&(msg->set_global_port);
 	else
 		send_port_str=&(send_sock->port_no_str);
 	/* init recv_address_str, recv_port_str & recv_port_no */
 	if(msg->rcv.bind_address) {
 		if(msg->rcv.bind_address->useinfo.name.len>0)
 			recv_address_str=&(msg->rcv.bind_address->useinfo.name);
 		else
 			recv_address_str=&(msg->rcv.bind_address->address_str);
 		if(msg->rcv.bind_address->useinfo.port_no>0) {
 			recv_port_str=&(msg->rcv.bind_address->useinfo.port_no_str);
 			recv_port_no = msg->rcv.bind_address->useinfo.port_no;
 		} else {
 			recv_port_str=&(msg->rcv.bind_address->port_no_str);
 			recv_port_no = msg->rcv.bind_address->port_no;
 		}
 	}
552f030b
 
f15bede1
 	orig=msg->buf;
b0e03da6
 	offset=*new_buf_offs;
 	s_offset=*orig_offs;
552f030b
 
4c37101e
 	for (t=lumps;t;t=t->next){
26b939c6
 		switch(t->op){
 			case LUMP_ADD:
f15bede1
 			case LUMP_ADD_SUBST:
d1e2a411
 			case LUMP_ADD_OPT:
552f030b
 				/* skip if this is an OPT lump and the condition is
d1e2a411
 				 * not satisfied */
 				if ((t->op==LUMP_ADD_OPT) &&
b05f7d8f
 						(!lump_check_opt(t, msg, send_info)))
d1e2a411
 					continue;
26b939c6
 				/* just add it here! */
 				/* process before  */
 				for(r=t->before;r;r=r->before){
 					switch (r->op){
 						case LUMP_ADD:
 							/*just add it here*/
 							memcpy(new_buf+offset, r->u.value, r->len);
 							offset+=r->len;
 							break;
f15bede1
 						case LUMP_ADD_SUBST:
 							SUBST_LUMP(r);
 							break;
d1e2a411
 						case LUMP_ADD_OPT:
552f030b
 							/* skip if this is an OPT lump and the condition is
d1e2a411
 					 		* not satisfied */
b05f7d8f
 							if (!lump_check_opt(r, msg, send_info))
d1e2a411
 								goto skip_before;
 							break;
26b939c6
 						default:
 							/* only ADD allowed for before/after */
eeeb3693
 							LM_CRIT("invalid op for data lump (%x)\n", r->op);
26b939c6
 					}
 				}
d1e2a411
 skip_before:
26b939c6
 				/* copy "main" part */
d1e2a411
 				switch(t->op){
 					case LUMP_ADD:
 						memcpy(new_buf+offset, t->u.value, t->len);
 						offset+=t->len;
 						break;
 					case LUMP_ADD_SUBST:
 						SUBST_LUMP(t);
 						break;
 					case LUMP_ADD_OPT:
 						/* do nothing, it's only a condition */
 						break;
552f030b
 					default:
d1e2a411
 						/* should not ever get here */
eeeb3693
 						LM_CRIT("unhandled data lump op %d\n", t->op);
f15bede1
 				}
26b939c6
 				/* process after */
 				for(r=t->after;r;r=r->after){
 					switch (r->op){
 						case LUMP_ADD:
 							/*just add it here*/
 							memcpy(new_buf+offset, r->u.value, r->len);
 							offset+=r->len;
 							break;
f15bede1
 						case LUMP_ADD_SUBST:
 							SUBST_LUMP(r);
 							break;
d1e2a411
 						case LUMP_ADD_OPT:
552f030b
 							/* skip if this is an OPT lump and the condition is
d1e2a411
 					 		* not satisfied */
b05f7d8f
 							if (!lump_check_opt(r, msg, send_info))
d1e2a411
 								goto skip_after;
 							break;
26b939c6
 						default:
 							/* only ADD allowed for before/after */
eeeb3693
 							LM_CRIT("invalid op for data lump (%x)\n", r->op);
26b939c6
 					}
 				}
d1e2a411
 skip_after:
26b939c6
 				break;
 			case LUMP_NOP:
 			case LUMP_DEL:
 				/* copy till offset */
 				if (s_offset>t->u.offset){
80d01114
 					LM_DBG("WARNING: (%d) overlapped lumps offsets,"
26b939c6
 						" ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
 					/* this should've been fixed above (when computing len) */
 					/* just ignore it*/
 					break;
 				}
 				size=t->u.offset-s_offset;
363027e7
                 if (size > 0 && flag == FLAG_MSG_ALL){
26b939c6
 					memcpy(new_buf+offset, orig+s_offset,size);
 					offset+=size;
 					s_offset+=size;
363027e7
                 } else if (flag == FLAG_MSG_LUMPS_ONLY) {
                     /* do not copy the whole message, jump to the lumps offs */
                     s_offset+=size;
                 }
 
                 /* the LUMP_DELs are printed with "- " before them */
                 if (t->op==LUMP_DEL && flag == FLAG_MSG_LUMPS_ONLY) {
                     new_buf[offset++] = '-';
                     new_buf[offset++] = ' ';
                 }
 
26b939c6
 				/* process before  */
 				for(r=t->before;r;r=r->before){
 					switch (r->op){
 						case LUMP_ADD:
 							/*just add it here*/
 							memcpy(new_buf+offset, r->u.value, r->len);
 							offset+=r->len;
 							break;
f15bede1
 						case LUMP_ADD_SUBST:
 							SUBST_LUMP(r);
 							break;
d1e2a411
 						case LUMP_ADD_OPT:
552f030b
 							/* skip if this is an OPT lump and the condition is
d1e2a411
 					 		* not satisfied */
b05f7d8f
 							if (!lump_check_opt(r, msg, send_info))
d1e2a411
 								goto skip_nop_before;
 							break;
26b939c6
 						default:
 							/* only ADD allowed for before/after */
eeeb3693
 							LM_CRIT("invalid op for data lump (%x)\n",r->op);
26b939c6
 					}
 				}
d1e2a411
 skip_nop_before:
363027e7
                 /* process main (del only) */
                 if (t->op==LUMP_DEL && flag == FLAG_MSG_ALL){
                     /* skip len bytes from orig msg */
                     s_offset+=t->len;
                 } else if (t->op==LUMP_DEL && flag == FLAG_MSG_LUMPS_ONLY) {
                     /* copy lump value and indent as necessarely */
                     memcpy(new_buf+offset, orig + t->u.offset, t->len);
                     offset+=t->len;
                     if (new_buf[offset-1] != '\n') {
                         new_buf[offset] = '\n';
                         offset+=1;
                     }
                     /* skip len bytes from orig msg */
                     s_offset+=t->len;
                  }
 
26b939c6
 				/* process after */
 				for(r=t->after;r;r=r->after){
 					switch (r->op){
 						case LUMP_ADD:
 							/*just add it here*/
 							memcpy(new_buf+offset, r->u.value, r->len);
 							offset+=r->len;
 							break;
f15bede1
 						case LUMP_ADD_SUBST:
 							SUBST_LUMP(r);
 							break;
d1e2a411
 						case LUMP_ADD_OPT:
552f030b
 							/* skip if this is an OPT lump and the condition is
d1e2a411
 					 		* not satisfied */
b05f7d8f
 							if (!lump_check_opt(r, msg, send_info))
d1e2a411
 								goto skip_nop_after;
 							break;
26b939c6
 						default:
 							/* only ADD allowed for before/after */
eeeb3693
 							LM_CRIT("invalid op for data lump (%x)\n", r->op);
26b939c6
 					}
 				}
d1e2a411
 skip_nop_after:
26b939c6
 				break;
 			default:
eeeb3693
 					LM_CRIT("unknown op (%x)\n", t->op);
26b939c6
 		}
 	}
b0e03da6
 	*new_buf_offs=offset;
 	*orig_offs=s_offset;
363027e7
 
     /* add '\0' to char* lump list to print it smoothly */
     if (flag == FLAG_MSG_LUMPS_ONLY) {
         new_buf[offset] = '\0';
     }
7a068ff3
 #undef RCVCOMP_PARAM_ADD 
 #undef SENDCOMP_PARAM_ADD
b0e03da6
 }
 
 
4c37101e
 /*
53c7e0f1
  * Adjust/insert Content-Length if necessary
4c37101e
  */
 static inline int adjust_clen(struct sip_msg* msg, int body_delta, int proto)
 {
 	struct lump* anchor;
 	char* clen_buf;
05c1b57f
 	int clen_len, body_only;
77932dbd
 #ifdef USE_TCP
 	char* body;
 	int comp_clen;
 #endif /* USE_TCP */
4c37101e
 
 	/* Calculate message length difference caused by lumps modifying message
 	 * body, from this point on the message body must not be modified. Zero
 	 * value indicates that the body hasn't been modified
 	*/
 
 	clen_buf = 0;
 	anchor=0;
05c1b57f
 	body_only=1;
552f030b
 
4c37101e
 	/* check to see if we need to add clen */
 #ifdef USE_TCP
 	if (proto == PROTO_TCP
 #ifdef USE_TLS
 	    || proto == PROTO_TLS
 #endif
 	    ) {
5c28a534
 		if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
eeeb3693
 			LM_ERR("error parsing content-length\n");
4c37101e
 			goto error;
 		}
77932dbd
 		if (unlikely(msg->content_length==0)){
4c37101e
 			/* not present, we need to add it */
 			/* msg->unparsed should point just before the final crlf
 			 * - whole message was parsed by the above parse_headers
 			 *   which did not find content-length */
 			anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
5c28a534
 												HDR_CONTENTLENGTH_T);
4c37101e
 			if (anchor==0){
eeeb3693
 				LM_ERR("cannot set clen anchor\n");
4c37101e
 				goto error;
 			}
05c1b57f
 			body_only=0;
77932dbd
 		}else{
 			/* compute current content length and compare it with the
 			   one in the message */
 			body=get_body(msg);
 			if (unlikely(body==0)){
 				ser_error=E_BAD_REQ;
eeeb3693
 				LM_ERR("no message body found (missing crlf?)");
77932dbd
 				goto error;
 			}
 			comp_clen=msg->len-(int)(body-msg->buf)+body_delta;
 			if (comp_clen!=(int)(long)msg->content_length->parsed){
 				/* note: we don't distinguish here between received with
 				   wrong content-length and content-length changed, we just
 				   fix it automatically in both cases (the reason being
 				   that an error message telling we have received a msg-
 				   with wrong content-length is of very little use) */
 				anchor = del_lump(msg, msg->content_length->body.s-msg->buf,
 									msg->content_length->body.len,
 									HDR_CONTENTLENGTH_T);
 				if (anchor==0) {
eeeb3693
 					LM_ERR("Can't remove original Content-Length\n");
77932dbd
 					goto error;
 				}
 				body_only=1;
 			}
4c37101e
 		}
77932dbd
 	}else
 #endif /* USE_TCP */
 	if (body_delta){
5c28a534
 		if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
eeeb3693
 			LM_ERR("Error parsing Content-Length\n");
4c37101e
 			goto error;
 		}
552f030b
 
4c37101e
 		/* The body has been changed, try to find
 		 * existing Content-Length
 		 */
 		/* no need for Content-Length if it's and UDP packet and
53c7e0f1
 		 * it hasn't Content-Length already */
f0fb6f1e
 		if (msg->content_length==0){
4c37101e
 		    /* content-length doesn't exist, append it */
 			/* msg->unparsed should point just before the final crlf
 			 * - whole message was parsed by the above parse_headers
 			 *   which did not find content-length */
 			if (proto!=PROTO_UDP){
 				anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
5c28a534
 													HDR_CONTENTLENGTH_T);
4c37101e
 				if (anchor==0){
eeeb3693
 					LM_ERR("cannot set clen anchor\n");
4c37101e
 					goto error;
 				}
05c1b57f
 				body_only=0;
77932dbd
 			} /* else
80d01114
 				LM_DBG("UDP packet with no clen => not adding one \n"); */
4c37101e
 		}else{
 			/* Content-Length has been found, remove it */
05c1b57f
 			anchor = del_lump(	msg, msg->content_length->body.s - msg->buf,
77932dbd
 								msg->content_length->body.len,
 								HDR_CONTENTLENGTH_T);
4c37101e
 			if (anchor==0) {
eeeb3693
 				LM_ERR("Can't remove original Content-Length\n");
4c37101e
 				goto error;
 			}
 		}
 	}
552f030b
 
4c37101e
 	if (anchor){
05c1b57f
 		clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
4c37101e
 		if (!clen_buf) goto error;
 		if (insert_new_lump_after(anchor, clen_buf, clen_len,
5c28a534
 					HDR_CONTENTLENGTH_T) == 0)
4c37101e
 			goto error;
 	}
77932dbd
 	
4c37101e
 	return 0;
 error:
 	if (clen_buf) pkg_free(clen_buf);
 	return -1;
 }
 
8f007d20
 static inline int find_line_start(char *text, unsigned int text_len,
 				  char **buf, unsigned int *buf_len)
 {
 	char *ch, *start;
 	unsigned int len;
 
 	start = *buf;
 	len = *buf_len;
 
 	while (text_len <= len) {
 		if (strncmp(text, start, text_len) == 0) {
 			*buf = start;
 			*buf_len = len;
 			return 1;
 		}
 		if ((ch = memchr(start, 13, len - 1))) {
 			if (*(ch + 1) != 10) {
 				LM_ERR("No LF after CR\n");
 				return 0;
 			}
 			len = len - (ch - start + 2);
 			start = ch + 2;
 		} else {
 			LM_ERR("No CRLF found\n");
 			return 0;
 		}
 	}
 	return 0;
 }
 
 static inline int get_line(str s)
 {
 	char *ch;
 
 	if ((ch = memchr(s.s, 13, s.len))) {
 		if (*(ch + 1) != 10) {
 			LM_ERR("No LF after CR\n");
 			return 0;
 		}
 		return ch - s.s + 2;
 	} else {
 		LM_ERR("No CRLF found\n");
 		return s.len;
 	}
 	return 0;
 }
 
 int replace_body(struct sip_msg *msg, str txt)
 {
 	struct lump *anchor;
 	char *buf;
 	str body = {0,0};
 
 	body.s = get_body(msg);
 	if(body.s==0)
 	{
 		LM_ERR("malformed sip message\n");
 		return 0;
 	}
 	body.len = msg->len -(int)(body.s-msg->buf);
 	LM_DBG("old size body[%d] actual[%d]\n", body.len, txt.len);
 	if(body.s+body.len>msg->buf+msg->len)
 	{
 		LM_ERR("invalid content length: %d\n", body.len);
 		return 0;
 	}
 	del_nonshm_lump( &(msg->body_lumps) );
 	msg->body_lumps = NULL;
 
 	if(del_lump(msg, body.s-msg->buf, body.len, 0) == 0)
 	{
 		LM_ERR("cannot delete existing body");
 		return 0;
 	}
 
 	anchor = anchor_lump(msg, body.s - msg->buf, 0, 0);
 	if(anchor==0)
 	{
 		LM_ERR("failed to get anchor\n");
 		return 0;
 	}
 
 	buf=pkg_malloc(sizeof(char)*txt.len);
 	if(buf==0)
 	{
 		PKG_MEM_ERROR;
 		return 0;
 	}
 	memcpy(buf, txt.s, txt.len);
 	if(insert_new_lump_after(anchor, buf, txt.len, 0)==0)
 	{
 		LM_ERR("failed to insert body lump\n");
 		pkg_free(buf);
 		return 0;
 	}
 	return 1;
 }
 
 /**
  * returns the boundary defined by the Content-Type
  * header
  */
 int get_boundary(struct sip_msg* msg, str* boundary)
 {
 	str params;
 	param_t *p, *list;
 	param_hooks_t hooks;
 
 	params.s = memchr(msg->content_type->body.s, ';',
 		msg->content_type->body.len);
 	if (params.s == NULL)
 	{
2b427a33
 		LM_INFO("Content-Type hdr has no params <%.*s>\n",
 				msg->content_type->body.len, msg->content_type->body.s);
8f007d20
 		return -1;
 	}
 	params.len = msg->content_type->body.len -
 		(params.s - msg->content_type->body.s);
 	if (parse_params(&params, CLASS_ANY, &hooks, &list) < 0)
 	{
 		LM_ERR("while parsing Content-Type params\n");
 		return -1;
 	}
 	boundary->s = NULL;
 	boundary->len = 0;
 	for (p = list; p; p = p->next) {
 		if ((p->name.len == 8)
 			&& (strncasecmp(p->name.s, "boundary", 8) == 0))
 		{
 			boundary->s = pkg_malloc(p->body.len + 2);
 			if (boundary->s == NULL)
 			{
 				free_params(list);
 				LM_ERR("no memory for boundary string\n");
 				return -1;
 			}
 			*(boundary->s) = '-';
 			*(boundary->s + 1) = '-';
 			memcpy(boundary->s + 2, p->body.s, p->body.len);
 			boundary->len = 2 + p->body.len;
 			LM_DBG("boundary is <%.*s>\n", boundary->len, boundary->s);
 			break;
 		}
 	}
 	free_params(list);
 	return 0;
 }
4c37101e
 
8f007d20
 int check_boundaries(struct sip_msg *msg, struct dest_info *send_info)
 {
 	str b = {0,0};
 	str fb = {0,0};
 	str ob = {0,0};
 	str bsuffix = {"\r\n", 2};
 	str fsuffix = {"--\r\n", 4};
 	str body = {0,0};
 	str buf = {0,0};
 	str tmp = {0,0};
 	struct str_list* lb = NULL;
 	struct str_list* lb_t = NULL;
 	int lb_found = 0;
 	int t, ret, lb_size;
 	char *pb;
 
 	if(!(msg->msg_flags&FL_BODY_MULTIPART)) return 0;
 	else
 	{
 		buf.s = build_body(msg, (unsigned int *)&buf.len, &ret, send_info);
 		if(ret) {
 			LM_ERR("Can't get body\n");
 			return -1;
 		}
 		tmp.s = buf.s;
 		t = tmp.len = buf.len;
2b427a33
 		if(get_boundary(msg, &ob)!=0) {
 			if(tmp.s) pkg_free(tmp.s);
 			return -1;
 		}
8f007d20
 		if(str_append(&ob, &bsuffix, &b)!=0) {
 			LM_ERR("Can't append suffix to boundary\n");
 			goto error;
 		}
 		if(str_append(&ob, &fsuffix,&fb)!=0) {
 			LM_ERR("Can't append suffix to final boundary\n");
 			goto error;
 		}
 		ret = b.len-2;
 		while(t>0)
 		{
 			if(find_line_start(b.s, ret, &tmp.s,
 				(unsigned int *)&tmp.len))
 			{
 				/*LM_DBG("found t[%d] tmp.len[%d]:[%.*s]\n",
 					t, tmp.len, tmp.len, tmp.s);*/
 				if(!lb)
 				{
 					lb = pkg_malloc(sizeof(struct str_list));
 					if (!lb) {
 						PKG_MEM_ERROR;
 						goto error;
 					}
 					lb->s.s = tmp.s;
 					lb->s.len = tmp.len;
 					lb->next = 0;
 					lb_t = lb;
 				}
 				else
 				{
 					lb_t = append_str_list(tmp.s, tmp.len, &lb_t, &lb_size);
 				}
 				lb_found = lb_found + 1;
 				tmp.s = tmp.s + ret;
 				t =  t - ret;
 				tmp.len = tmp.len - ret;
 			}
 			else { t=0; }
 		}
 		if(lb_found<2)
 		{
 			LM_ERR("found[%d] wrong number of boundaries\n", lb_found);
 			goto error;
 		}
 		/* adding 2 chars in advance */
 		body.len = buf.len + 2;
 		body.s = pkg_malloc(sizeof(char)*body.len);
 		if (!body.s) {
 			PKG_MEM_ERROR;
 			goto error;
 		}
 		pb = body.s; body.len = 0;
 		lb_t = lb;
 		while(lb_t)
 		{
 			tmp.s = lb_t->s.s; tmp.len = lb_t->s.len;
 			tmp.len = get_line(lb_t->s);
 			if(tmp.len!=b.len || strncmp(b.s, tmp.s, b.len)!=0)
 			{
 				LM_DBG("malformed bondary in the middle\n");
 				memcpy(pb, b.s, b.len); body.len = body.len + b.len;
 				pb = pb + b.len;
 				t = lb_t->s.s - (lb_t->s.s + tmp.len);
 				memcpy(pb, lb_t->s.s+tmp.len, t); pb = pb + t;
 				/*LM_DBG("new chunk[%d][%.*s]\n", t, t, pb-t);*/
 			}
 			else {
 				t = lb_t->next->s.s - lb_t->s.s;
 				memcpy(pb, lb_t->s.s, t);
 				/*LM_DBG("copy[%d][%.*s]\n", t, t, pb);*/
 				pb = pb + t;
 			}
 			body.len = body.len + t;
 			/*LM_DBG("body[%d][%.*s]\n", body.len, body.len, body.s);*/
 			lb_t = lb_t->next;
 			if(!lb_t->next) lb_t = NULL;
 		}
 		/* last boundary */
 		tmp.s = lb->s.s; tmp.len = lb->s.len;
 		tmp.len = get_line(lb->s);
 		if(tmp.len!=fb.len || strncmp(fb.s, tmp.s, fb.len)!=0)
 		{
 			LM_DBG("last bondary without -- at the end\n");
 			memcpy(pb, fb.s, fb.len);
 			/*LM_DBG("new chunk[%d][%.*s]\n", fb.len, fb.len, pb);*/
 			pb = pb + fb.len;
 			body.len = body.len + fb.len;
 		}
 		else {
 			memcpy(pb, lb->s.s, lb->s.len); pb = pb + lb->s.len;
 			body.len = body.len + lb->s.len;
 			/*LM_DBG("copy[%d][%.*s]\n", lb->s.len, lb->s.len, pb - lb->s.len);*/
 		}
 		/*LM_DBG("body[%d][%.*s] expected[%ld]\n",
 			body.len, body.len, body.s, pb-body.s); */
 		if(!replace_body(msg, body))
 		{
 			LM_ERR("Can't replace body\n");
 			goto error;
 		}
 		msg->msg_flags &= ~FL_BODY_MULTIPART;
 		ret = 1;
 		goto clean;
 	}
 
 error:
 	ret = -1;
 clean:
 	if(ob.s) pkg_free(ob.s);
 	if(b.s) pkg_free(b.s);
 	if(fb.s) pkg_free(fb.s);
 	if(body.s) pkg_free(body.s);
 	if(buf.s) pkg_free(buf.s);
 	while(lb)
 	{
 		lb_t = lb->next;
 		pkg_free(lb);
 		lb = lb_t;
 	}
 	return ret;
 }
b0e03da6
 
bf8dfee3
 /** builds a request in memory from another sip request.
   *
   * Side-effects: - it adds lumps to the msg which are _not_ cleaned.
d64d6bb9
   * The added lumps are HDR_VIA_T (almost always added), HDR_CONTENLENGTH_T
   * and HDR_ROUTE_T (when a Route: header is added as a result of a non-null
   * msg->path_vec).
bf8dfee3
   *               - it might change send_info->proto and send_info->send_socket
   *                 if proto fallback is enabled (see below).
   *
   * Uses also global_req_flags ( OR'ed with msg->msg_flags, see send_info
   * below).
   *
   * @param msg  - sip message structure, complete with lumps
   * @param returned_len - result length (filled in)
   * @param send_info  - dest_info structure (value/result), contains where the
   *                     packet will be sent to (it's needed for building a 
   *                     correct via, fill RR lumps a.s.o.). If MTU based
   *                     protocol fall-back is enabled (see flags below),
   *                     send_info->proto might be updated with the new
   *                     protocol.
   *                     msg->msg_flags used:
   *                     - FL_TCP_MTU_FB, FL_TLS_MTU_FB and FL_SCTP_MTU_FB -
   *                       fallback to the corresp. proto if the built 
   *                       message > mtu and send_info->proto==PROTO_UDP. 
   *                       It will also update send_info->proto.
   *                     - FL_FORCE_RPORT: add rport to via
d64d6bb9
   * @param mode - flags for building the message, can be a combination of:
   *                 * BUILD_NO_LOCAL_VIA - don't add a local via
   *                 * BUILD_NO_VIA1_UPDATE - don't update first via (rport,
   *                    received a.s.o)
   *                 * BUILD_NO_PATH - don't add a Route: header with the 
   *                   msg->path_vec content.
   *                 * BUILD_IN_SHM - build the result in shm memory
bf8dfee3
   *
d64d6bb9
   * @return pointer to the new request (pkg_malloc'ed or shm_malloc'ed,
   * depending on the presence of the BUILD_IN_SHM flag, needs freeing when
bf8dfee3
   *   done) and sets returned_len or 0 on error.
   */
b0e03da6
 char * build_req_buf_from_sip_req( struct sip_msg* msg,
 								unsigned int *returned_len,
61f86410
 								struct dest_info* send_info,
 								unsigned int mode)
eaffa61d
 {
bf8dfee3
 	unsigned int len, new_len, received_len, rport_len, uri_len, via_len,
 				 body_delta;
b0e03da6
 	char* line_buf;
 	char* received_buf;
09f7cd2c
 	char* rport_buf;
b0e03da6
 	char* new_buf;
 	char* buf;
d64d6bb9
 	str path_buf;
b0e03da6
 	unsigned int offset, s_offset, size;
bf8dfee3
 	struct lump* via_anchor;
 	struct lump* via_lump;
3e8c3475
 	struct lump* via_insert_param;
d64d6bb9
 	struct lump* path_anchor;
 	struct lump* path_lump;
e1e6c914
 	str branch;
bf8dfee3
 	unsigned int flags;
 	unsigned int udp_mtu;
19e6b61f
 	struct dest_info di;
4c37101e
 
3e8c3475
 	via_insert_param=0;
b0e03da6
 	uri_len=0;
 	buf=msg->buf;
 	len=msg->len;
 	received_len=0;
09f7cd2c
 	rport_len=0;
b0e03da6
 	new_buf=0;
 	received_buf=0;
09f7cd2c
 	rport_buf=0;
3c7744da
 	via_anchor=0;
e1e6c914
 	line_buf=0;
6be3dfe5
 	via_len=0;
d64d6bb9
 	path_buf.s=0;
 	path_buf.len=0;
552f030b
 
bf8dfee3
 	flags=msg->msg_flags|global_req_flags;
8f007d20
 	if(check_boundaries(msg, send_info)<0){
 		LM_WARN("check_boundaries error\n");
 	}
bf8dfee3
 	/* Calculate message body difference and adjust Content-Length */
7a068ff3
 	body_delta = lumps_len(msg, msg->body_lumps, send_info);
 	if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
eeeb3693
 		LM_ERR("Error while adjusting Content-Length\n");
4c37101e
 		goto error00;
ab130758
 	}
4c37101e
 
61f86410
 	if(unlikely(mode&BUILD_NO_LOCAL_VIA))
 		goto after_local_via;
 
bf8dfee3
 	/* create the via header */
e1e6c914
 	branch.s=msg->add_to_branch_s;
 	branch.len=msg->add_to_branch_len;
d9dec029
 
3c7744da
 	via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
 	if (unlikely(via_anchor==0)) goto error00;
d9dec029
 	line_buf = create_via_hf( &via_len, msg, send_info, &branch);
3c7744da
 	if (unlikely(!line_buf)){
897dfc4c
 		LM_ERR("could not create Via header\n");
caf80ae6
 		goto error00;
b0e03da6
 	}
61f86410
 after_local_via:
 	if(unlikely(mode&BUILD_NO_VIA1_UPDATE))
 		goto after_update_via1;
b0e03da6
 	/* check if received needs to be added */
552f030b
 	if ( received_test(msg) ) {
1cec1f4e
 		if ((received_buf=received_builder(msg,&received_len))==0){
eeeb3693
 			LM_ERR("received_builder failed\n");
caf80ae6
 			goto error01;  /* free also line_buf */
1cec1f4e
 		}
b0e03da6
 	}
552f030b
 
3e8c3475
 	/* check if rport needs to be updated:
 	 *  - if FL_FORCE_RPORT is set add it (and del. any previous version)
 	 *  - if via already contains an rport add it and overwrite the previous
 	 *  rport value if present (if you don't want to overwrite the previous
 	 *  version remove the comments) */
bf8dfee3
 	if ((flags&FL_FORCE_RPORT)||
3e8c3475
 			(msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1cec1f4e
 		if ((rport_buf=rport_builder(msg, &rport_len))==0){
eeeb3693
 			LM_ERR("rport_builder failed\n");
09f7cd2c
 			goto error01; /* free everything */
1cec1f4e
 		}
09f7cd2c
 	}
b0e03da6
 
3e8c3475
 	/* find out where the offset of the first parameter that should be added
 	 * (after host:port), needed by add receive & maybe rport */
 	if (msg->via1->params.s){
 			size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
 														  for ';' */
 	}else{
 			size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
 			if (msg->via1->port!=0){
 				/*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
 				size += msg->via1->port_str.len + 1; /* +1 for ':'*/
 			}
116f7bad
 #if 0
 			/* no longer necessary, now hots.s contains [] */
3e8c3475
 			if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
116f7bad
 #endif
3e8c3475
 	}
552f030b
 	/* if received needs to be added, add anchor after host and add it, or
3e8c3475
 	 * overwrite the previous one if already present */
b0e03da6
 	if (received_len){
3e8c3475
 		if (msg->via1->received){ /* received already present => overwrite it*/
4c37101e
 			via_insert_param=del_lump(msg,
3e8c3475
 								msg->via1->received->start-buf-1, /*;*/
5c28a534
 								msg->via1->received->size+1, /*;*/ HDR_VIA_T);
3e8c3475
 		}else if (via_insert_param==0){ /* receive not present, ok */
5c28a534
 			via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
 											HDR_VIA_T);
b0e03da6
 		}
3e8c3475
 		if (via_insert_param==0) goto error02; /* free received_buf */
 		if (insert_new_lump_after(via_insert_param, received_buf, received_len,
5c28a534
 					HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
09f7cd2c
 	}
3e8c3475
 	/* if rport needs to be updated, delete it if present and add it's value */
09f7cd2c
 	if (rport_len){
3e8c3475
 		if (msg->via1->rport){ /* rport already present */
4c37101e
 			via_insert_param=del_lump(msg,
3e8c3475
 								msg->via1->rport->start-buf-1, /*';'*/
5c28a534
 								msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
3e8c3475
 		}else if (via_insert_param==0){ /*force rport, no rport present */
 			/* no rport, add it */
5c28a534
 			via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
 											HDR_VIA_T);
3e8c3475
 		}
 		if (via_insert_param==0) goto error03; /* free rport_buf */
 		if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
5c28a534
 									HDR_VIA_T) ==0 )
3e8c3475
 			goto error03; /* free rport_buf */
552f030b
 
b0e03da6
 	}
 
61f86410
 after_update_via1:
d64d6bb9
 	/* add route with path content */
 	if(unlikely(!(mode&BUILD_NO_PATH) && msg->path_vec.s &&
 					msg->path_vec.len)){
 		path_buf.len=ROUTE_PREFIX_LEN+msg->path_vec.len+CRLF_LEN;
 		path_buf.s=pkg_malloc(path_buf.len+1);
 		if (unlikely(path_buf.s==0)){
eeeb3693
 			LM_ERR("out of memory\n");
d64d6bb9
 			ser_error=E_OUT_OF_MEM;
 			goto error00;
 		}
 		memcpy(path_buf.s, ROUTE_PREFIX, ROUTE_PREFIX_LEN);
 		memcpy(path_buf.s+ROUTE_PREFIX_LEN, msg->path_vec.s,
 					msg->path_vec.len);
 		memcpy(path_buf.s+ROUTE_PREFIX_LEN+msg->path_vec.len, CRLF, CRLF_LEN);
 		path_buf.s[path_buf.len]=0;
 		/* insert Route header either before the other routes
3c7744da
 		   (if present & parsed), after the local via or after in front of
 		    the first via if we don't add a local via*/
d64d6bb9
 		if (msg->route){
 			path_anchor=anchor_lump(msg, msg->route->name.s-buf, 0, 
 									HDR_ROUTE_T);
3c7744da
 		}else if (likely(via_anchor)){
 			path_anchor=via_anchor;
d64d6bb9
 		}else if (likely(msg->via1)){
 			path_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, 
 									HDR_ROUTE_T);
 		}else{
 			/* if no via1 (theoretically possible for non-sip messages,
 			   e.g. http xmlrpc) */
 			path_anchor=anchor_lump(msg, msg->headers->name.s-buf, 0, 
 									HDR_ROUTE_T);
 		}
 		if (unlikely(path_anchor==0))
 			goto error05;
 		if (unlikely((path_lump=insert_new_lump_after(path_anchor, path_buf.s,
 														path_buf.len,
 														HDR_ROUTE_T))==0))
 			goto error05;
 	}
b0e03da6
 	/* compute new msg len and fix overlapping zones*/
bf8dfee3
 	new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
548d11fb
 #ifdef XL_DEBUG
eeeb3693
 	LM_ERR("new_len(%d)=len(%d)+lumps_len\n", new_len, len);
548d11fb
 #endif
bf8dfee3
 	udp_mtu=cfg_get(core, core_cfg, udp_mtu);
19e6b61f
 	di.proto=PROTO_NONE;
bf8dfee3
 	if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu && 
61f86410
 					(flags & FL_MTU_FB_MASK) && (new_len>udp_mtu)
 					&& (!(mode&BUILD_NO_LOCAL_VIA)))){
19e6b61f
 
 		di=*send_info; /* copy whole struct - will be used in the Via builder */
 		di.proto=PROTO_NONE; /* except the proto */
bf8dfee3
 #ifdef USE_TCP
 		if (!tcp_disable && (flags & FL_MTU_TCP_FB) &&
19e6b61f
 				(di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TCP))){
 			di.proto=PROTO_TCP;
bf8dfee3
 		}
 	#ifdef USE_TLS
 		else if (!tls_disable && (flags & FL_MTU_TLS_FB) &&
19e6b61f
 				(di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TLS))){
 			di.proto=PROTO_TLS;
bf8dfee3
 		}
 	#endif /* USE_TLS */
 #endif /* USE_TCP */
 #ifdef USE_SCTP
 	#ifdef USE_TCP
 		else
 	#endif /* USE_TCP */
 		 if (!sctp_disable && (flags & FL_MTU_SCTP_FB) &&
19e6b61f
 				(di.send_sock=get_send_socket(msg, &send_info->to, PROTO_SCTP))){
 			di.proto=PROTO_SCTP;
bf8dfee3
 		 }
 #endif /* USE_SCTP */
 		
19e6b61f
 		if (di.proto!=PROTO_NONE){
bf8dfee3
 			new_len-=via_len;
61f86410
 			if(likely(line_buf)) pkg_free(line_buf);
19e6b61f
 			line_buf = create_via_hf( &via_len, msg, &di, &branch);
bf8dfee3
 			if (!line_buf){
eeeb3693
 				LM_ERR("memory allocation failure!\n");
bf8dfee3
 				goto error00;
 			}
 			new_len+=via_len;
 		}
 	}
 	/* add via header to the list */
 	/* try to add it before msg. 1st via */
 	/* add first via, as an anchor for second via*/
61f86410
 	if(likely(line_buf)) {
 		if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
bf8dfee3
 											HDR_VIA_T))==0)
61f86410
 			goto error04;
 	}
b0e03da6
 	if (msg->new_uri.s){
 		uri_len=msg->new_uri.len;
 		new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
 	}
61f86410
 	if(unlikely(mode&BUILD_IN_SHM))
 		new_buf=(char*)shm_malloc(new_len+1);
 	else
 		new_buf=(char*)pkg_malloc(new_len+1);
b0e03da6
 	if (new_buf==0){
 		ser_error=E_OUT_OF_MEM;
eeeb3693
 		LM_ERR("out of memory\n");
caf80ae6
 		goto error00;
b0e03da6
 	}
 
 	offset=s_offset=0;
 	if (msg->new_uri.s){
 		/* copy message up to uri */
 		size=msg->first_line.u.request.uri.s-buf;
049f64c2
 		memcpy(new_buf, buf, size);
b0e03da6
 		offset+=size;
 		s_offset+=size;
 		/* add our uri */
 		memcpy(new_buf+offset, msg->new_uri.s, uri_len);
 		offset+=uri_len;
 		s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
 	}
 	new_buf[new_len]=0;
049f64c2
 	/* copy msg adding/removing lumps */
363027e7
 	process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info, FLAG_MSG_ALL);
 	process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info, FLAG_MSG_ALL);
049f64c2
 	/* copy the rest of the message */
 	memcpy(new_buf+offset, buf+s_offset, len-s_offset);
26b939c6
 	new_buf[new_len]=0;
 
19e6b61f
 	/* update the send_info if udp_mtu affected */
 	if (di.proto!=PROTO_NONE) { 
 		send_info->proto=di.proto;
 		send_info->send_sock=di.send_sock;
 	}
 
6cd1dc34
 #ifdef DBG_MSG_QA
 	if (new_buf[new_len-1]==0) {
eeeb3693
 		LM_ERR("0 in the end\n");
6cd1dc34
 		abort();
 	}
 #endif
 
26b939c6
 	*returned_len=new_len;
 	return new_buf;
 
caf80ae6
 error01:
 error02:
4938d15f
 	if (received_buf) pkg_free(received_buf);
09f7cd2c
 error03:
 	if (rport_buf) pkg_free(rport_buf);
bf8dfee3
 error04:
 	if (line_buf) pkg_free(line_buf);
d64d6bb9
 error05:
 	if (path_buf.s) pkg_free(path_buf.s);
caf80ae6
 error00:
26b939c6
 	*returned_len=0;
 	return 0;
 }
 
8248a1b2
 char * generate_res_buf_from_sip_res( struct sip_msg* msg,
 				unsigned int *returned_len, unsigned int mode)
8cc98445
 {
4c37101e
 	unsigned int new_len, via_len, body_delta;
8cc98445
 	char* new_buf;
b0e03da6
 	unsigned offset, s_offset, via_offset;
8cc98445
 	char* buf;
 	unsigned int len;
4c37101e
 
8cc98445
 	buf=msg->buf;
 	len=msg->len;
 	new_buf=0;
8248a1b2
 
 	if(unlikely(mode&BUILD_NO_VIA1_UPDATE)) {
 		via_len = 0;
 		via_offset = 0;
1540fad1
 	} else {
8248a1b2
 		/* we must remove the first via */
 		if (msg->via1->next) {
 			via_len=msg->via1->bsize;
 			via_offset=msg->h_via1->body.s-buf;
 		} else {
 			via_len=msg->h_via1->len;
 			via_offset=msg->h_via1->name.s-buf;
 		}
1540fad1
 	}
ab130758
 
4c37101e
 	     /* Calculate message body difference and adjust
 	      * Content-Length
 	      */
 	body_delta = lumps_len(msg, msg->body_lumps, 0);
 	if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
 			< 0) {
eeeb3693
 		LM_ERR("error while adjusting Content-Length\n");
4c37101e
 		goto error;
eaffa61d
 	}
4c37101e
 
8248a1b2
 	if(likely(!(mode&BUILD_NO_VIA1_UPDATE))) {
 		/* remove the first via*/
 		if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
eeeb3693
 			LM_ERR("error trying to remove first via\n");
8248a1b2
 			goto error;
 		}
b0e03da6
 	}
4c37101e
 
 	new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
 														know the send sock */
552f030b
 
80d01114
 	LM_DBG("old size: %d, new size: %d\n", len, new_len);
552f030b
 	new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
e3fc93f4
 											 (\0 to print it )*/
8cc98445
 	if (new_buf==0){
eeeb3693
 		LM_ERR("out of mem\n");
8cc98445
 		goto error;
 	}
 	new_buf[new_len]=0; /* debug: print the message */
b0e03da6
 	offset=s_offset=0;
4c37101e
 	/*FIXME: no send sock*/
363027e7
 	process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0, FLAG_MSG_ALL);/*FIXME:*/
 	process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0, FLAG_MSG_ALL);
b0e03da6
 	/* copy the rest of the message */
049f64c2
 	memcpy(new_buf+offset,
552f030b
 		buf+s_offset,
049f64c2
 		len-s_offset);
8cc98445
 	 /* send it! */
80d01114
 	LM_DBG("copied size: orig:%d, new: %d, rest: %d msg=\n%s\n",
 			s_offset, offset, len-s_offset, new_buf);
8cc98445
 
 	*returned_len=new_len;
 	return new_buf;
 error:
 	*returned_len=0;
 	return 0;
 }
 
8248a1b2
 char * build_res_buf_from_sip_res( struct sip_msg* msg,
 				unsigned int *returned_len)
 {
 	return generate_res_buf_from_sip_res(msg, returned_len, 0);
 }
8cc98445
 
20f4c97f
 char * build_res_buf_from_sip_req( unsigned int code, str *text ,str *new_tag,
7e8d3f17
 		struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
5843cd1b
 {
5e39b907
 	char              *buf, *p;
 	unsigned int      len,foo;
b644f031
 	struct hdr_field  *hdr;
5e39b907
 	struct lump_rpl   *lump;
7e8d3f17
 	struct lump_rpl   *body;
5e39b907
 	int               i;
b23a7a00
 	char*             received_buf;
 	unsigned int      received_len;
 	char*             rport_buf;
 	unsigned int      rport_len;
 	char*             warning_buf;
 	unsigned int      warning_len;
 	char*             content_len_buf;
 	unsigned int      content_len_len;
1540fad1
 	char *after_body;
74ed389d
 	str  to_tag;
fde02f64
 	char *totags;
dfb34a65
 	int httpreq;
 	char *pvia;
68a3fc65
 
7e8d3f17
 	body = 0;
a2688733
 	buf=0;
b23a7a00
 	received_buf=rport_buf=warning_buf=content_len_buf=0;
 	received_len=rport_len=warning_len=content_len_len=0;
552f030b
 
e0931fb0
 	to_tag.s=0;  /* fixes gcc 4.0 warning */
 	to_tag.len=0;
552f030b
 
8df23b70
 	/* force parsing all headers -- we want to return all
 	Via's in the reply and they may be scattered down to the
 	end of header (non-block Vias are a really poor property
 	of SIP :( ) */
5c28a534
 	if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
eeeb3693
 		LM_ERR("alas, parse_headers failed\n");
caf80ae6
 		goto error00;
 	}
b644f031
 
53c7e0f1
 	/*computes the length of the new response buffer*/
7e8d3f17
 	len = 0;
 
dfb34a65
 	httpreq = IS_HTTP(msg);
 
a2688733
 	/* check if received needs to be added */
0b15425f
 	if (received_test(msg)) {
b23a7a00
 		if ((received_buf=received_builder(msg,&received_len))==0) {
eeeb3693
 			LM_ERR("alas, received_builder failed\n");
caf80ae6
 			goto error00;
 		}
 	}
1cec1f4e
 	/* check if rport needs to be updated */
bf8dfee3
 	if ( ((msg->msg_flags|global_req_flags)&FL_FORCE_RPORT)||
3e8c3475
 		(msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
b23a7a00
 		if ((rport_buf=rport_builder(msg, &rport_len))==0){
eeeb3693
 			LM_ERR("rport_builder failed\n");
1cec1f4e
 			goto error01; /* free everything */
 		}
552f030b
 		if (msg->via1->rport)
7e8d3f17
 			len -= msg->via1->rport->size+1; /* include ';' */
1cec1f4e
 	}
a2688733
 
b644f031
 	/* first line */
cc9fbc76
 	len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
20f4c97f
 		text->len + CRLF_LEN/*new line*/;
8df23b70
 	/*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
814bd485
 	for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
7e8d3f17
 		switch (hdr->type) {
5c28a534
 			case HDR_TO_T:
7e8d3f17
 				if (new_tag && new_tag->len) {
 					to_tag=get_to(msg)->tag_value;
14183ffa
 					if ( to_tag.len || to_tag.s )
7e8d3f17
 						len+=new_tag->len-to_tag.len;
 					else
 						len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
 				}
db1a35b2
 				len += hdr->len;
7e8d3f17
 				break;
5c28a534
 			case HDR_VIA_T:
6419a43f
 				/* we always add CRLF to via*/
 				len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
b23a7a00
 				if (hdr==msg->h_via1) len += received_len+rport_len;
7e8d3f17
 				break;
5c28a534
 			case HDR_RECORDROUTE_T:
814bd485
 				/* RR only for 1xx and 2xx replies */
7e8d3f17
 				if (code<180 || code>=300)
 					break;
5c28a534
 			case HDR_FROM_T:
 			case HDR_CALLID_T:
 			case HDR_CSEQ_T:
7e8d3f17
 				/* we keep the original termination for these headers*/
 				len += hdr->len;
5c28a534
 				break;
 			default:
 				/* do nothing, we are interested only in the above headers */
 				;
340bddb4
 		}
814bd485
 	}
7e8d3f17
 	/* lumps length */
 	for(lump=msg->reply_lump;lump;lump=lump->next) {
0498ceb9
 		len += lump->text.len;
6ec61efb
 		if (lump->flags&LUMP_RPL_BODY)
7e8d3f17
 			body = lump;
a5b16152
 	}
7e8d3f17
 	/* server header */
866ddc10
 	if (server_signature && server_hdr.len)
c745469f
 		len += server_hdr.len + CRLF_LEN;
7e8d3f17
 	/* warning hdr */
caf80ae6
 	if (sip_warning) {
b23a7a00
 		warning_buf = warning_builder(msg,&warning_len);
 		if (warning_buf) len += warning_len + CRLF_LEN;
eeeb3693
 		else LM_WARN("warning skipped -- too big\n");
caf80ae6
 	}
7e8d3f17
 	/* content length hdr */
 	if (body) {
b23a7a00
 		content_len_buf = int2str(body->text.len, (int*)&content_len_len);
 		len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
cfbc8ec5
 	} else {
 		len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
7e8d3f17
 	}
b644f031
 	/* end of message */
68a3fc65
 	len += CRLF_LEN; /*new line*/
96dd9e9f
 
b644f031
 	/*allocating mem*/
caf80ae6
 	buf = (char*) pkg_malloc( len+1 );
b644f031
 	if (!buf)
 	{
eeeb3693
 		LM_ERR("out of memory; needs %d\n",len);
caf80ae6
 		goto error01;
b644f031
 	}
 
 	/* filling the buffer*/
8df23b70
 	p=buf;
b644f031
 	/* first line */
cc9fbc76
 	memcpy( p , msg->first_line.u.request.version.s , 
 		msg->first_line.u.request.version.len);
 	p += msg->first_line.u.request.version.len;
8b863d8c
 	*(p++) = ' ' ;
 	/*code*/
b644f031
 	for ( i=2 , foo = code  ;  i>=0  ;  i-- , foo=foo/10 )
8df23b70
 		*(p+i) = '0' + foo - ( foo/10 )*10;
b644f031
 	p += 3;
 	*(p++) = ' ' ;
20f4c97f
 	memcpy( p , text->s , text->len );
 	p += text->len;
68a3fc65
 	memcpy( p, CRLF, CRLF_LEN );
 	p+=CRLF_LEN;
b644f031
 	/* headers*/
db1a35b2
 	for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
340bddb4
 		switch (hdr->type)
b644f031
 		{
5c28a534
 			case HDR_VIA_T:
dfb34a65
 				/* if is HTTP, backup start of Via header in response */
 				if(unlikely(httpreq)) pvia = p;
1cec1f4e
 				if (hdr==msg->h_via1){
b23a7a00
 					if (rport_buf){
3e8c3475
 						if (msg->via1->rport){ /* delete the old one */
 							/* copy until rport */
 							append_str_trans( p, hdr->name.s ,
 								msg->via1->rport->start-hdr->name.s-1,msg);
 							/* copy new rport */
b23a7a00
 							append_str(p, rport_buf, rport_len);
3e8c3475
 							/* copy the rest of the via */
 							append_str_trans(p, msg->via1->rport->start+
552f030b
 												msg->via1->rport->size,
3e8c3475
 												hdr->body.s+hdr->body.len-
 												msg->via1->rport->start-
 												msg->via1->rport->size, msg);
 						}else{ /* just append the new one */
 							/* normal whole via copy */
552f030b
 							append_str_trans( p, hdr->name.s ,
3e8c3475
 								(hdr->body.s+hdr->body.len)-hdr->name.s, msg);
b23a7a00
 							append_str(p, rport_buf, rport_len);
3e8c3475
 						}
1cec1f4e
 					}else{
 						/* normal whole via copy */
552f030b
 						append_str_trans( p, hdr->name.s ,
6419a43f
 								(hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1cec1f4e
 					}
b23a7a00
 					if (received_buf)
 						append_str( p, received_buf, received_len);
1cec1f4e
 				}else{
 					/* normal whole via copy */
6419a43f
 					append_str_trans( p, hdr->name.s,
 							(hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1cec1f4e
 				}
1540fad1
 				append_str( p, CRLF,CRLF_LEN);
dfb34a65
 				/* if is HTTP, replace Via with Sia
 				 * - HTTP Via format is different than SIP Via
 				 */
 				if(unlikely(httpreq)) *pvia = 'S';
a2688733
 				break;
5c28a534
 			case HDR_RECORDROUTE_T:
814bd485
 				/* RR only for 1xx and 2xx replies */
 				if (code<180 || code>=300) break;
6419a43f
 				append_str(p, hdr->name.s, hdr->len);
 				break;
5c28a534
 			case HDR_TO_T:
7e8d3f17
 				if (new_tag && new_tag->len){
1540fad1
 					if (to_tag.s ) { /* replacement */
 						/* before to-tag */
 						append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
 						/* to tag replacement */
fde02f64
 						bmark->to_tag_val.s=p;
7e8d3f17
 						bmark->to_tag_val.len=new_tag->len;
 						append_str( p, new_tag->s,new_tag->len);
1540fad1
 						/* the rest after to-tag */
 						append_str( p, to_tag.s+to_tag.len,
 							hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
 					}else{ /* adding a new to-tag */
 						after_body=hdr->body.s+hdr->body.len;
 						append_str( p, hdr->name.s, after_body-hdr->name.s);
 						append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
fde02f64
 						bmark->to_tag_val.s=p;
7e8d3f17
 						bmark->to_tag_val.len=new_tag->len;
 						append_str( p, new_tag->s,new_tag->len);
552f030b
 						append_str( p, after_body,
1540fad1
 										hdr->name.s+hdr->len-after_body);
 					}
 					break;
 				} /* no new to-tag -- proceed to 1:1 copying  */
fde02f64
 				totags=((struct to_body*)(hdr->parsed))->tag_value.s;
 				if (totags) {
 					bmark->to_tag_val.s=p+(totags-hdr->name.s);
 					bmark->to_tag_val.len=
 							((struct to_body*)(hdr->parsed))->tag_value.len;
552f030b
 				}
 				else {
 					bmark->to_tag_val.len = 0;
 					bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
 				}
 				/* no break */
5c28a534
 			case HDR_FROM_T:
 			case HDR_CALLID_T:
 			case HDR_CSEQ_T:
1540fad1
 					append_str(p, hdr->name.s, hdr->len);
5c28a534
 					break;
 			default:
 				/* do nothing, we are interested only in the above headers */
 				;
db1a35b2
 		} /* end switch */
 	} /* end for */
7e8d3f17
 	/* lumps */
0498ceb9
 	for(lump=msg->reply_lump;lump;lump=lump->next)
6ec61efb
 		if (lump->flags&LUMP_RPL_HDR){
7e8d3f17
 			memcpy(p,lump->text.s,lump->text.len);
 			p += lump->text.len;
 		}
 	/* server header */
866ddc10
 	if (server_signature && server_hdr.len>0) {
c745469f
 		memcpy( p, server_hdr.s, server_hdr.len );
 		p+=server_hdr.len;
caf80ae6
 		memcpy( p, CRLF, CRLF_LEN );
 		p+=CRLF_LEN;
a5b16152
 	}
7e8d3f17
 	/* content_length hdr */
b23a7a00
 	if (content_len_len) {
cfbc8ec5
 		append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
b23a7a00
 		append_str( p, content_len_buf, content_len_len );
cfbc8ec5
 		append_str( p, CRLF, CRLF_LEN );
a5b16152
 	} else {
cfbc8ec5
 		append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
96dd9e9f
 	}
7e8d3f17
 	/* warning header */
b23a7a00
 	if (warning_buf) {
 		memcpy( p, warning_buf, warning_len);
 		p+=warning_len;
caf80ae6
 		memcpy( p, CRLF, CRLF_LEN);
 		p+=CRLF_LEN;
 	}
af64f4fb
 	/*end of message*/
68a3fc65
 	memcpy( p, CRLF, CRLF_LEN );
8b299ebc
 	p+=CRLF_LEN;
7e8d3f17
 	/* body */
 	if (body) {
 		memcpy ( p, body->text.s, body->text.len );
 		p+=body->text.len;
96dd9e9f
 	}
7e8d3f17
 
db1a35b2
 	if (len!=p-buf)
eeeb3693
 		LM_CRIT("diff len=%d p-buf=%d\n", len, (int)(p-buf));
db1a35b2
 
0938e8b5
 	*(p) = 0;
d9c55ba4
 	*returned_len = len;
caf80ae6
 	/* in req2reply, received_buf is not introduced to lumps and
 	   needs to be deleted here
 	*/
b23a7a00
 	if (received_buf) pkg_free(received_buf);
 	if (rport_buf) pkg_free(rport_buf);
b644f031
 	return buf;
caf80ae6
 
 error01:
b23a7a00
 	if (received_buf) pkg_free(received_buf);
 	if (rport_buf) pkg_free(rport_buf);
caf80ae6
 error00:
b644f031
 	*returned_len=0;
 	return 0;
5843cd1b
 }
caf80ae6
 
7e8d3f17
 
 
caf80ae6
 /* return number of chars printed or 0 if space exceeded;
53c7e0f1
    assumes buffer size of at least MAX_BRANCH_PARAM_LEN
caf80ae6
  */
 int branch_builder( unsigned int hash_index,
 	/* only either parameter useful */
 	unsigned int label, char * char_v,
 	int branch,
 	char *branch_str, int *len )
 {
 
 	char *begin;
 	int size;
 
 	/* hash id provided ... start with it */
 	size=MAX_BRANCH_PARAM_LEN;
 	begin=branch_str;
 	*len=0;
7958eeae
 
 	memcpy(begin, MCOOKIE, MCOOKIE_LEN );
 	size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
 
caf80ae6
 	if (int2reverse_hex( &begin, &size, hash_index)==-1)
 		return 0;
 
 	if (size) {
 		*begin=BRANCH_SEPARATOR;
 		begin++; size--;
 	} else return 0;
 
e5a9bc72
 	/* string with request's characteristic value ... use it ... */
 	if (char_v) {
caf80ae6
 		if (memcpy(begin,char_v,MD5_LEN)) {
 			begin+=MD5_LEN; size-=MD5_LEN;
 		} else return 0;
e5a9bc72
 	} else { /* ... use the "label" value otherwise */
 		if (int2reverse_hex( &begin, &size, label )==-1)
 			return 0;
caf80ae6
 	}
 
 	if (size) {
 		*begin=BRANCH_SEPARATOR;
 		begin++; size--;
 	} else return 0;
 
 	if (int2reverse_hex( &begin, &size, branch)==-1)
 		return 0;
 
 	*len=MAX_BRANCH_PARAM_LEN-size;
 	return size;
552f030b
 
caf80ae6
 }
 
 
7a068ff3
 
 /* uses only the send_info->send_socket, send_info->proto and 
  * send_info->comp (so that a send_info used for sending can be passed
  * to this function w/o changes and the correct via will be built) */
552f030b
 char* via_builder( unsigned int *len,
7a068ff3
 	struct dest_info* send_info /* where to send the reply */,
 	str* branch, str* extra_params, struct hostport* hp)
caf80ae6
 {
 	unsigned int  via_len, extra_len;
 	char               *line_buf;
 	int max_len;
ed990c31
 	int via_prefix_len;
6bfaa042
 	str* address_str; /* address displayed in via */
 	str* port_str; /* port no displayed in via */
7a068ff3
 	struct socket_info* send_sock;
 	int comp_len, comp_name_len;
e4a75063
 #ifdef USE_COMP
7a068ff3
 	char* comp_name;
e4a75063
 #endif /* USE_COMP */
1ccd1f68
 	int port;
 	struct ip_addr ip;
 	union sockaddr_union *from = NULL;
 	union sockaddr_union local_addr;
 	struct tcp_connection *con = NULL;
552f030b
 
7a068ff3
 	send_sock=send_info->send_sock;
574daa6d
 	/* use pre-set address in via, the outbound socket alias or address one */
 	if (hp && hp->host->len)
6bfaa042
 		address_str=hp->host;
574daa6d
 	else if(send_sock->useinfo.name.len>0)
 		address_str=&(send_sock->useinfo.name);
6bfaa042
 	else
 		address_str=&(send_sock->address_str);
 	if (hp && hp->port->len)
 		port_str=hp->port;
574daa6d
 	else if(send_sock->useinfo.port_no>0)
 		port_str=&(send_sock->useinfo.port_no_str);
6bfaa042
 	else
 		port_str=&(send_sock->port_no_str);
7a068ff3
 	
 	comp_len=comp_name_len=0;
 #ifdef USE_COMP
e4a75063
 	comp_name=0;
7a068ff3
 	switch(send_info->comp){
 		case COMP_NONE:
 			break;
 		case COMP_SIGCOMP:
 			comp_len=COMP_PARAM_LEN;
 			comp_name_len=SIGCOMP_NAME_LEN;
 			comp_name=SIGCOMP_NAME;
 			break;
 		case COMP_SERGZ:
 			comp_len=COMP_PARAM_LEN;
 			comp_name_len=SERGZ_NAME_LEN;
 			comp_name=SERGZ_NAME;
 			break;
 		default:
eeeb3693
 			LM_CRIT("unknown comp %d\n", send_info->comp);
7a068ff3
 			/* continue, we'll just ignore comp */
 	}
 #endif /* USE_COMP */
 			
ed990c31
 	via_prefix_len=MY_VIA_LEN+(send_info->proto==PROTO_SCTP);
 	max_len=via_prefix_len +address_str->len /* space in MY_VIA */
e1e6c914
 		+2 /* just in case it is a v6 address ... [ ] */
6bfaa042
 		+1 /*':'*/+port_str->len
c94efc41
 		+(branch?(MY_BRANCH_LEN+branch->len):0)
 		+(extra_params?extra_params->len:0)
7a068ff3
 		+comp_len+comp_name_len
e1e6c914
 		+CRLF_LEN+1;
caf80ae6
 	line_buf=pkg_malloc( max_len );
 	if (line_buf==0){
 		ser_error=E_OUT_OF_MEM;
eeeb3693
 		LM_ERR("out of memory\n");
caf80ae6
 		return 0;
 	}
 
 	extra_len=0;
 
ed990c31
 	via_len=via_prefix_len+address_str->len; /*space included in MY_VIA*/
caf80ae6
 
552f030b
 	memcpy(line_buf, MY_VIA, MY_VIA_LEN);
7a068ff3
 	if (send_info->proto==PROTO_UDP){
53c7e0f1
 		/* do nothing */
7a068ff3
 	}else if (send_info->proto==PROTO_TCP){
f2f969dd
 		memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
7a068ff3
 	}else if (send_info->proto==PROTO_TLS){
8e807134
 		memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
ed990c31
 	}else if (send_info->proto==PROTO_SCTP){
 		memcpy(line_buf+MY_VIA_LEN-4, "SCTP ", 5);
8393efff
 	}else if (send_info->proto==PROTO_WS){
1ccd1f68
                 if (unlikely(send_info->send_flags.f & SND_F_FORCE_SOCKET
                                 && send_info->send_sock)) {
                         local_addr = send_info->send_sock->su;
                         su_setport(&local_addr, 0); /* any local port will do */
                         from = &local_addr;
                 }
 
                 port = su_getport(&send_info->to);
                 if (likely(port)) {
                         su2ip_addr(&ip, &send_info->to);
                         con = tcpconn_get(send_info->id, &ip, port, from, 0);
                 }
                 else if (likely(send_info->id))
                         con = tcpconn_get(send_info->id, 0, 0, 0, 0);
                 else {
eeeb3693
                         LM_CRIT("null_id & to\n"); pkg_free(line_buf);
1ccd1f68
                         return 0;
                 }
 
                 if (con == NULL) {
53135e42
                         LM_WARN("TCP/TLS connection (id: %d) for WebSocket could not be found\n",
3c062c12
 								send_info->id);
 						pkg_free(line_buf);
1ccd1f68
                         return 0;
                 }
 
 		if (con->rcv.proto==PROTO_WS) {
 			memcpy(line_buf+MY_VIA_LEN-4, "WS ", 3);
 		} else if (con->rcv.proto==PROTO_WSS) {
 			memcpy(line_buf+MY_VIA_LEN-4, "WSS ", 4);
 		} else {
6cfd13cb
 			tcpconn_put(con);
eeeb3693
 			LM_CRIT("unknown proto %d\n", con->rcv.proto);
3c062c12
 			pkg_free(line_buf);
1ccd1f68
 			return 0;
 		}
6cfd13cb
 		tcpconn_put(con);
ba0ff312
 	}else if (send_info->proto==PROTO_WSS){
 		memcpy(line_buf+MY_VIA_LEN-4, "WSS ", 4);
c55adae8
 	}else{
eeeb3693
 		LM_CRIT("unknown proto %d\n", send_info->proto);
3c062c12
 		pkg_free(line_buf);
f2f969dd
 		return 0;
 	}
6bfaa042
 	/* add [] only if ipv6 and outbound socket address is used;
 	 * if using pre-set no check is made */
 	if ((send_sock->address.af==AF_INET6) &&
 		(address_str==&(send_sock->address_str))) {
ed990c31
 		line_buf[via_prefix_len]='[';
 		line_buf[via_prefix_len+1+address_str->len]=']';
caf80ae6
 		extra_len=1;
 		via_len+=2; /* [ ]*/
 	}
ed990c31
 	memcpy(line_buf+via_prefix_len+extra_len, address_str->s,
 				address_str->len);
 	if ((send_sock->port_no!=SIP_PORT) ||
 				(port_str!=&send_sock->port_no_str)){
19710544
 		line_buf[via_len]=':'; via_len++;
6bfaa042
 		memcpy(line_buf+via_len, port_str->s, port_str->len);
 		via_len+=port_str->len;
caf80ae6
 	}
 
 	/* branch parameter */
e1e6c914
 	if (branch){
 		memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
 		via_len+=MY_BRANCH_LEN;
 		memcpy(line_buf+via_len, branch->s, branch->len );
 		via_len+=branch->len;
 	}
 	/* extra params  */
 	if (extra_params){
 		memcpy(line_buf+via_len, extra_params->s, extra_params->len);
 		via_len+=extra_params->len;
 	}
7a068ff3
 #ifdef USE_COMP
 	/* comp */
 	if (comp_len){
 		memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
 		via_len+=COMP_PARAM_LEN;
 		memcpy(line_buf+via_len, comp_name, comp_name_len);
 		via_len+=comp_name_len;
 	}
 #endif
552f030b
 
caf80ae6
 	memcpy(line_buf+via_len, CRLF, CRLF_LEN);
 	via_len+=CRLF_LEN;
 	line_buf[via_len]=0; /* null terminate the string*/
 
 	*len = via_len;
 	return line_buf;
 }
ca570f95
 
d9dec029
 /* creates a via header honoring the protocol of the incomming socket
  * msg is an optional parameter */
 char* create_via_hf( unsigned int *len,
 	struct sip_msg *msg,
 	struct dest_info* send_info /* where to send the reply */,
 	str* branch)
 {
 	char* via;
 	str extra_params;
 	struct hostport hp;
31fd952b
 #if defined USE_TCP || defined USE_SCTP
d9dec029
 	char* id_buf;
 	unsigned int id_len;
 
 
 	id_buf=0;
 	id_len=0;
 #endif
 	extra_params.len=0;
 	extra_params.s=0;
 
 
31fd952b
 #if defined USE_TCP || defined USE_SCTP
d9dec029
 	/* add id if tcp */
31fd952b
 	if (msg && (
 #ifdef USE_TCP
 		(msg->rcv.proto==PROTO_TCP)
d9dec029
 #ifdef USE_TLS
 			|| (msg->rcv.proto==PROTO_TLS)
 #endif
31fd952b
 #ifdef USE_SCTP
 			||
 #endif /* USE_SCTP */
 #endif /* USE_TCP */
 #ifdef USE_SCTP
 			(msg->rcv.proto==PROTO_SCTP)
 #endif /* USE_SCTP */
d9dec029
 			)){
 		if  ((id_buf=id_builder(msg, &id_len))==0){
eeeb3693
 			LM_ERR("id_builder failed\n");
d9dec029
 			return 0; /* we don't need to free anything,
 			                 nothing alloc'ed yet*/
 		}
80d01114
 		LM_DBG("id added: <%.*s>, rcv proto=%d\n",
d9dec029
 				(int)id_len, id_buf, msg->rcv.proto);
 		extra_params.s=id_buf;
 		extra_params.len=id_len;
 	}
31fd952b
 #endif /* USE_TCP || USE_SCTP */
d9dec029
 
c37c22b4
 	/* test and add rport parameter to local via - rfc3581 */
3c062c12
 	if(msg && msg->msg_flags&FL_ADD_LOCAL_RPORT) {
c37c22b4
 		/* params so far + ';rport' + '\0' */
 		via = (char*)pkg_malloc(extra_params.len+RPORT_LEN);
 		if(via==0) {
 			LM_ERR("building local rport via param failed\n");
 			if (extra_params.s) pkg_free(extra_params.s);
 			return 0;
 		}
 		if(extra_params.len!=0) {
 			memcpy(via, extra_params.s, extra_params.len);
 			pkg_free(extra_params.s);
 		}
 		memcpy(via + extra_params.len, RPORT, RPORT_LEN-1);
 		extra_params.s = via;
 		extra_params.len += RPORT_LEN-1;
 		extra_params.s[extra_params.len]='\0';
 	}
 
d9dec029
 	set_hostport(&hp, msg);
 	via = via_builder( len, send_info, branch,
 							extra_params.len?&extra_params:0, &hp);
 
c37c22b4
 	/* we do not need extra_params any more, already in the new via header */
 	if (extra_params.s) pkg_free(extra_params.s);
d9dec029
 	return via;
 }
 
ca570f95
 /* builds a char* buffer from message headers without body
  * first line is excluded in case of skip_first_line=1
  * error is set -1 if the memory allocation failes
  */
 char * build_only_headers( struct sip_msg* msg, int skip_first_line,
 				unsigned int *returned_len,
 				int *error,
 				struct dest_info* send_info)
 {
 	char		*buf, *new_buf;
 	unsigned int	offset, s_offset, len, new_len;
 
 	*error = 0;
 	buf = msg->buf;
 	if (skip_first_line)
 		s_offset = msg->headers->name.s - buf;
 	else
 		s_offset = 0;
 
 	/* original length without body, and without final \r\n */
 	len = msg->unparsed - buf;
 	/* new msg length */
 	new_len =	len - /* original length  */
 			s_offset + /* skipped first line */
 			lumps_len(msg, msg->add_rm, send_info); /* lumps */
 
 	if (new_len == 0) {
 		*returned_len = 0;
 		return 0;
 	}
 
 	new_buf = (char *)pkg_malloc(new_len+1);
 	if (!new_buf) {
eeeb3693
 		LM_ERR("Not enough memory\n");
ca570f95
 		*error = -1;
 		return 0;
 	}
 	new_buf[0] = 0;
 	offset = 0;
 
 	/* copy message lumps */
363027e7
 	process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info, FLAG_MSG_ALL);
ca570f95
 	/* copy the rest of the message without body */
 	if (len > s_offset) {
 		memcpy(new_buf+offset, buf+s_offset, len-s_offset);
 		offset += (len-s_offset);
 	}
 	new_buf[offset] = 0;
 
 	*returned_len = offset;
 	return new_buf;
 }
 
 /* builds a char* buffer from message body
  * error is set -1 if the memory allocation failes
  */
 char * build_body( struct sip_msg* msg,
 			unsigned int *returned_len,
 			int *error,
 			struct dest_info* send_info)
 {
 	char		*buf, *new_buf, *body;
 	unsigned int	offset, s_offset, len, new_len;
 
 	*error = 0;
 	body = get_body(msg);
 
 	if (!body || (body[0] == 0)) {
 		*returned_len = 0;
 		return 0;
 	}
 	buf = msg->buf;
 	s_offset = body - buf;
 
 	/* original length of msg with body */
 	len = msg->len;
 	/* new body length */
 	new_len =	len - /* original length  */
 			s_offset + /* msg without body */
 			lumps_len(msg, msg->body_lumps, send_info); /* lumps */
 
 	new_buf = (char *)pkg_malloc(new_len+1);
 	if (!new_buf) {
eeeb3693
 		LM_ERR("Not enough memory\n");
ca570f95
 		*error = -1;
 		return 0;
 	}
 	new_buf[0] = 0;
 	offset = 0;
 
 	/* copy body lumps */
363027e7
 	process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info, FLAG_MSG_ALL);
ca570f95
 	/* copy the rest of the message without body */
 	if (len > s_offset) {
 		memcpy(new_buf+offset, buf+s_offset, len-s_offset);
 		offset += (len-s_offset);
 	}
 	new_buf[offset] = 0;
 
 	*returned_len = offset;
 	return new_buf;
 }
 
 /* builds a char* buffer from SIP message including body
  * The function adjusts the Content-Length HF according
  * to body lumps in case of touch_clen=1.
  */
 char * build_all( struct sip_msg* msg, int touch_clen,
 			unsigned int *returned_len,
 			int *error,
 			struct dest_info* send_info)
 {
 	char		*buf, *new_buf;
 	unsigned int	offset, s_offset, len, new_len;
 	unsigned int	body_delta;
 
 	*error = 0;
 	/* Calculate message body difference */
 	body_delta = lumps_len(msg, msg->body_lumps, send_info);
 	if (touch_clen) {
 		/* adjust Content-Length */
 		if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
eeeb3693
 			LM_ERR("Error while adjusting Content-Length\n");
ca570f95
 			*error = -1;
 			return 0;
 		}
 	}
 
 	buf = msg->buf;
 	/* original msg length */
 	len = msg->len;
 	/* new msg length */
 	new_len =	len + /* original length  */
 			lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
 			body_delta; /* body lumps */
 
 	if (new_len == 0) {
 		returned_len = 0;
 		return 0;
 	}
 
 	new_buf = (char *)pkg_malloc(new_len+1);
 	if (!new_buf) {
eeeb3693
 		LM_ERR("Not enough memory\n");
ca570f95
 		*error = -1;
 		return 0;
 	}
 	new_buf[0] = 0;
 	offset = s_offset = 0;
 
 	/* copy message lumps */
363027e7
 	process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info, FLAG_MSG_ALL);
ca570f95
 	/* copy body lumps */
363027e7
 	process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info, FLAG_MSG_ALL);
ca570f95
 	/* copy the rest of the message */
 	memcpy(new_buf+offset, buf+s_offset, len-s_offset);
 	offset += (len-s_offset);
 	new_buf[offset] = 0;
 
 	*returned_len = offset;
 	return new_buf;	
 }
61f86410
 
 
 
 /**
  * parse buf in msg and fill several fields
  */
 int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len,
 		unsigned int id)
 {
 	if(msg==0 || buf==0)
 		return -1;
 
 	memset(msg, 0, sizeof(sip_msg_t));
 	msg->id = id;
 	msg->buf = buf;
 	msg->len = len;
 	if (parse_msg(buf, len, msg)!=0) {
 		LM_ERR("parsing failed");
 		return -1;
 	}
 	msg->set_global_address=default_global_address;
 	msg->set_global_port=default_global_port;
 	return 0;
 }