5 * Copyright (C) 2001-2003 FhG Fokus
7 * This file is part of ser, a free SIP server.
9 * ser is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version
14 * For a license to use the ser software under conditions
15 * other than those described here, or to purchase support for this
16 * software, please contact iptel.org by e-mail at the following addresses:
19 * ser is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * 2003-01-20 bug_fix: use of return value of snprintf aligned to C99 (jiri)
32 * 2003-01-23 added rport patches, contributed by
33 * Maxim Sobolev <sobomax@FreeBSD.org> and heavily modified by me
35 * 2003-01-24 added i param to via of outgoing requests (used by tcp),
36 * modified via_builder params (andrei)
37 * 2003-01-27 more rport fixes (make use of new via_param->start) (andrei)
38 * 2003-01-27 next baby-step to removing ZT - PRESERVE_ZT (jiri)
39 * 2003-01-29 scratchpad removed (jiri)
40 * 2003-02-28 scratchpad compatibility abandoned (jiri)
41 * 2003-03-01 VOICE_MAIL defs removed (jiri)
42 * 2003-03-06 totags in outgoing replies bookmarked to enable
43 * ACK/200 tag matching (andrei)
44 * 2003-03-18 killed the build_warning snprintf (andrei)
45 * 2003-03-31 added subst lump support (andrei)
46 * 2003-04-01 added opt (conditional) lump support (andrei)
47 * 2003-04-02 added more subst lumps: SUBST_{SND,RCV}_ALL
48 * => ip:port;transport=proto (andrei)
49 * 2003-04-12 added FL_FORCE_RPORT support (andrei)
50 * 2003-04-13 updated warning builder -- fixed (andrei)
51 * 2003-07-10 check_via_address knows now how to compare with ipv6 address
52 * references (e.g [::1]) (andrei)
53 * build_req_fomr_sip_req no longer adds 1 for ipv6 via parameter
54 * position calculations ([] are part of host.s now) (andrei)
55 * 2003-10-02 via+lump dst address/port can be set to preset values (andrei)
56 * 2003-10-08 receive_test function-alized (jiri)
57 * 2003-10-20 added body_lump list (sip_msg), adjust_clen (andrei & jan)
58 * 2003-11-11 type of rpl_lumps replaced by flags (bogdan)
59 * 2006-04-20 build_req_from_sip_req, via_builder and lump_* functions
60 * use now struct dest_info; lumps & via comp param support
62 * 2007-08-31 id_builder() and via_builder() are grouped into one function:
63 * create_via_hf() -- tm module needs them as well (Miklos)
64 * 2008-12-17 build_req_from_sip_req() will now fallback to tcp, tls or sctp
65 * if packet size > udp_mtu and fallback is enabled
66 * build_req_from_sip_req() uses now global_req_flags along
67 * msg->msg_flags (andrei)
70 /* Via special params:
72 * - if the address in via is different from the src_ip or an existing
73 * received=something is found, received=src_ip is added (and any preexisting
74 * received is deleted). received is added as the first via parameter if no
75 * receive is previously present or over the old receive.
76 * - if the original via contains rport / rport=something or msg->msg_flags
77 * FL_FORCE_RPORT is set (e.g. script force_rport() cmd) rport=src_port
78 * is added (over previous rport / as first via param or after received
79 * if no received was present and received is added too)
81 * (see also sl_send_reply)
82 * - rport and received are added in mostly the same way as for requests, but
83 * in the reverse order (first rport and then received). See also
85 * - if reply_to_via is set (default off) the local reply will be sent to
86 * the address in via (received is ignored since it was not set by us). The
87 * destination port is either the message source port if via contains rport
88 * or the FL_FORCE_RPORT flag is set or the port from the via. If either
89 * port or rport are present a normal dns lookup (instead of a srv lookup)
90 * is performed on the address. If no port is present and a srv lookup is
91 * performed the port is taken from the srv lookup. If the srv lookup failed
92 * or it was not performed, the port is set to the default sip port (5060).
93 * - if reply_to_via is off (default) the local reply is sent to the message
94 * source ip address. The destination port is set to the source port if
95 * rport is present or FL_FORCE_RPORT flag is set, to the via port or to
96 * the default sip port (5060) if neither rport or via port are present.
98 * - if received is present the message is sent to the received address else
99 * if no port is present (neither a normal via port or rport) a dns srv
100 * lookup is performed on the host part and the reply is sent to the
101 * resulting ip. If a port is present or the host part is an ip address
102 * the dns lookup will be a "normal" one (A or AAAA).
103 * - if rport is present, it's value will be used as the destination port
104 * (and this will also disable srv lookups)
105 * - if no port is present the destination port will be taken from the srv
106 * lookup. If the srv lookup fails or is not performed (e.g. ip address
107 * in host) the destination port will be set to the default sip port (5060).
110 * - when locally replying to a message, rport and received will be appended to
111 * the via header parameters (for forwarded requests they are inserted at the
113 * - a locally generated reply might get two received via parameters if a
114 * received is already present in the original message (this should not
115 * happen though, but ...)
122 * \brief SIP-router core ::
127 #include <sys/types.h>
128 #include <sys/socket.h>
134 #include "comp_defs.h"
135 #include "msg_translator.h"
141 #include "md5utils.h"
142 #include "data_lump.h"
143 #include "data_lump_rpl.h"
149 #include "parser/parse_to.h"
153 #define append_str_trans(_dest,_src,_len,_msg) \
154 append_str( (_dest), (_src), (_len) );
156 extern char version[];
157 extern int version_len;
161 /** per process fixup function for global_req_flags.
162 * It should be called from the configuration framework.
164 void fix_global_req_flags(str* gname, str* name)
167 switch(cfg_get(core, core_cfg, udp_mtu_try_proto)){
173 global_req_flags|=FL_MTU_TCP_FB;
176 global_req_flags|=FL_MTU_TLS_FB;
179 global_req_flags|=FL_MTU_SCTP_FB;
182 if (cfg_get(core, core_cfg, force_rport))
183 global_req_flags|=FL_FORCE_RPORT;
188 /* checks if ip is in host(name) and ?host(ip)=name?
189 * ip must be in network byte order!
190 * resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
191 * return 0 if equal */
192 static int check_via_address(struct ip_addr* ip, str *name,
193 unsigned short port, int resolver)
200 /* maybe we are lucky and name it's an ip */
203 DBG("check_via_address(%s, %.*s, %d)\n",
204 s, name->len, name->s, resolver);
208 /* check if name->s is an ipv6 address or an ipv6 address ref. */
209 if ((ip->af==AF_INET6) &&
210 ( ((len==name->len)&&(strncasecmp(name->s, s, name->len)==0))
212 ((len==(name->len-2))&&(name->s[0]=='[')&&
213 (name->s[name->len-1]==']')&&
214 (strncasecmp(name->s+1, s, len)==0))
221 if (strncmp(name->s, s, name->len)==0)
224 LOG(L_CRIT, "check_via_address: BUG: could not convert ip address\n");
228 if (port==0) port=SIP_PORT;
229 if (resolver&DO_DNS){
230 DBG("check_via_address: doing dns lookup\n");
231 /* try all names ips */
232 he=sip_resolvehost(name, &port, 0); /* don't use naptr */
233 if (he && ip->af==he->h_addrtype){
234 for(i=0;he && he->h_addr_list[i];i++){
235 if ( memcmp(&he->h_addr_list[i], ip->u.addr, ip->len)==0)
240 if (resolver&DO_REV_DNS){
241 DBG("check_via_address: doing rev. dns lookup\n");
242 /* try reverse dns */
243 he=rev_resolvehost(ip);
244 if (he && (strncmp(he->h_name, name->s, name->len)==0))
246 for (i=0; he && he->h_aliases[i];i++){
247 if (strncmp(he->h_aliases[i],name->s, name->len)==0)
255 /* check if IP address in Via != source IP address of signaling */
256 int received_test( struct sip_msg *msg )
260 rcvd=msg->via1->received
261 || check_via_address(&msg->rcv.src_ip, &msg->via1->host,
262 msg->via1->port, received_dns);
267 static char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
269 static char buf[MAX_WARNING_LEN];
275 #define str_print(string, string_len) \
278 if ((clen+l)>MAX_WARNING_LEN) \
279 goto error_overflow; \
280 memcpy(buf+clen, (string), l); \
284 #define str_lenpair_print(string, string_len, string2, string2_len) \
286 str_print(string, string_len); \
287 str_print(string2, string2_len);\
290 #define str_pair_print( string, string2, string2_len) \
291 str_lenpair_print((string), strlen((string)), (string2), (string2_len))
293 #define str_int_print(string, intval)\
295 t=int2str((intval), &print_len); \
296 str_pair_print(string, t, print_len);\
299 #define str_su_print(sockaddr)\
301 t=su2a(&sockaddr, sizeof(sockaddr)); \
302 print_len=strlen(t); \
303 str_print(t, print_len); \
306 #define str_ipaddr_print(string, ipaddr_val)\
308 t=ip_addr2a((ipaddr_val)); \
309 print_len=strlen(t); \
310 str_pair_print(string, t, print_len);\
314 str_print(WARNING, WARNING_LEN);
315 str_su_print(msg->rcv.bind_address->su);
316 str_print(WARNING_PHRASE, WARNING_PHRASE_LEN);
322 foo=&(msg->first_line.u.request.uri);
324 str_int_print(" pid=", my_pid());
326 str_ipaddr_print(" req_src_ip=", &msg->rcv.src_ip);
327 str_int_print(" req_src_port=", msg->rcv.src_port);
328 str_pair_print(" in_uri=", msg->first_line.u.request.uri.s,
329 msg->first_line.u.request.uri.len);
330 str_pair_print(" out_uri=", foo->s, foo->len);
331 str_pair_print(" via_cnt",
332 (msg->parsed_flag & HDR_EOH_F)==HDR_EOH_F ? "=" : ">", 1);
333 str_int_print("=", via_cnt);
334 if (clen<MAX_WARNING_LEN){ buf[clen]='"'; clen++; }
335 else goto error_overflow;
341 LOG(L_NOTICE, "NOTICE: warning_builder: buffer size exceeded (probably too long URI)\n");
349 char* received_builder(struct sip_msg *msg, unsigned int *received_len)
353 struct ip_addr *source_ip;
357 source_ip=&msg->rcv.src_ip;
359 buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
361 ser_error=E_OUT_OF_MEM;
362 LOG(L_ERR, "ERROR: received_builder: out of memory\n");
365 memcpy(buf, RECEIVED, RECEIVED_LEN);
366 if ( (tmp=ip_addr2a(source_ip))==0)
369 len=RECEIVED_LEN+tmp_len;
371 memcpy(buf+RECEIVED_LEN, tmp, tmp_len);
372 buf[len]=0; /*null terminate it */
380 char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
388 tmp=int2str(msg->rcv.src_port, &tmp_len);
389 len=RPORT_LEN+tmp_len;
390 buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
392 ser_error=E_OUT_OF_MEM;
393 LOG(L_ERR, "ERROR: rport_builder: out of memory\n");
396 memcpy(buf, RPORT, RPORT_LEN);
397 memcpy(buf+RPORT_LEN, tmp, tmp_len);
398 buf[len]=0; /*null terminate it*/
406 char* id_builder(struct sip_msg* msg, unsigned int *id_len)
410 char revhex[sizeof(int)*2];
416 if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
417 LOG(L_CRIT, "BUG: id_builder: not enough space for id\n");
420 value_len=p-&revhex[0];
421 len=ID_PARAM_LEN+value_len;
422 buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
424 ser_error=E_OUT_OF_MEM;
425 LOG(L_ERR, "ERROR: id_builder: out of memory\n");
428 memcpy(buf, ID_PARAM, ID_PARAM_LEN);
429 memcpy(buf+ID_PARAM_LEN, revhex, value_len);
430 buf[len]=0; /* null terminate it */
437 char* clen_builder( struct sip_msg* msg, int *clen_len, int diff,
451 LOG(L_ERR, "ERROR: clen_builder: no message body found"
455 value=msg->len-(int)(body-msg->buf)+diff;
456 value_s=int2str(value, &value_len);
457 DBG("clen_builder: content-length: %d (%s)\n", value, value_s);
463 len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
465 buf=pkg_malloc(sizeof(char)*(len+1));
467 ser_error=E_OUT_OF_MEM;
468 LOG(L_ERR, "ERROR: clen_builder: out of memory\n");
472 memcpy(buf, value_s, value_len);
475 memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
476 memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
477 memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
479 buf[len]=0; /* null terminate it */
486 /* checks if a lump opt condition
487 * returns 1 if cond is true, 0 if false */
488 static inline int lump_check_opt( struct lump *l,
490 struct dest_info* snd_i
497 #define get_ip_port_proto \
498 if ((snd_i==0) || (snd_i->send_sock==0)){ \
499 LOG(L_CRIT, "ERROR: lump_check_opt: null send socket\n"); \
500 return 1; /* we presume they are different :-) */ \
502 if (msg->rcv.bind_address){ \
503 ip=&msg->rcv.bind_address->address; \
504 port=msg->rcv.bind_address->port_no; \
505 proto=msg->rcv.bind_address->proto; \
507 ip=&msg->rcv.dst_ip; \
508 port=msg->rcv.dst_port; \
509 proto=msg->rcv.proto; \
516 LUMP_SET_COND_TRUE(l);
518 case COND_IF_DIFF_REALMS:
520 /* faster tests first */
521 if ((port==snd_i->send_sock->port_no) &&
522 (proto==snd_i->send_sock->proto) &&
524 (msg->rcv.comp==snd_i->comp) &&
526 (ip_addr_cmp(ip, &snd_i->send_sock->address)))
529 LUMP_SET_COND_TRUE(l);
532 case COND_IF_DIFF_AF:
534 if (ip->af!=snd_i->send_sock->address.af) {
535 LUMP_SET_COND_TRUE(l);
538 case COND_IF_DIFF_PROTO:
540 if (proto!=snd_i->send_sock->proto) {
541 LUMP_SET_COND_TRUE(l);
544 case COND_IF_DIFF_PORT:
546 if (port!=snd_i->send_sock->port_no) {
547 LUMP_SET_COND_TRUE(l);
550 case COND_IF_DIFF_IP:
552 if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
554 LUMP_SET_COND_TRUE(l);
558 if(rand()>=RAND_MAX/2) {
559 LUMP_SET_COND_TRUE(l);
563 LOG(L_CRIT, "BUG: lump:w_check_opt: unknown lump condition %d\n",
566 return 0; /* false */
571 /* computes the "unpacked" len of a lump list,
572 code moved from build_req_from_req */
573 static inline int lumps_len(struct sip_msg* msg, struct lump* lumps,
574 struct dest_info* send_info)
580 str* send_address_str;
582 struct socket_info* send_sock;
586 #define RCVCOMP_LUMP_LEN \
588 switch(msg->rcv.comp){ \
592 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
595 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
598 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
602 #define SENDCOMP_LUMP_LEN \
604 switch(send_info->comp){ \
608 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
611 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
614 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
618 #define RCVCOMP_LUMP_LEN
619 #define SENDCOMP_LUMP_LEN
622 #define SUBST_LUMP_LEN(subst_l) \
623 switch((subst_l)->u.subst){ \
625 if (msg->rcv.bind_address){ \
626 new_len+=msg->rcv.bind_address->address_str.len; \
627 if (msg->rcv.bind_address->address.af!=AF_INET) \
631 LOG(L_CRIT, "FIXME: null bind_address\n"); \
634 case SUBST_RCV_PORT: \
635 if (msg->rcv.bind_address){ \
636 new_len+=msg->rcv.bind_address->port_no_str.len; \
639 LOG(L_CRIT, "FIXME: null bind_address\n"); \
642 case SUBST_RCV_PROTO: \
643 if (msg->rcv.bind_address){ \
644 switch(msg->rcv.bind_address->proto){ \
655 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
656 msg->rcv.bind_address->proto); \
660 LOG(L_CRIT, "FIXME: null bind_address\n"); \
663 case SUBST_RCV_ALL: \
664 if (msg->rcv.bind_address){ \
665 new_len+=msg->rcv.bind_address->address_str.len; \
666 if (msg->rcv.bind_address->address.af!=AF_INET) \
668 if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
670 new_len+=1+msg->rcv.bind_address->port_no_str.len; \
672 /*add;transport=xxx*/ \
673 switch(msg->rcv.bind_address->proto){ \
676 break; /* udp is the default */ \
679 new_len+=TRANSPORT_PARAM_LEN+3; \
682 new_len+=TRANSPORT_PARAM_LEN+4; \
685 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
686 msg->rcv.bind_address->proto); \
691 LOG(L_CRIT, "FIXME: null bind_address\n"); \
696 new_len+=send_address_str->len; \
697 if (send_sock->address.af!=AF_INET && \
698 send_address_str==&(send_sock->address_str)) \
701 LOG(L_CRIT, "FIXME: lumps_len called with" \
702 " null send_sock\n"); \
705 case SUBST_SND_PORT: \
707 new_len+=send_port_str->len; \
709 LOG(L_CRIT, "FIXME: lumps_len called with" \
710 " null send_sock\n"); \
713 case SUBST_SND_PROTO: \
715 switch(send_sock->proto){ \
726 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
730 LOG(L_CRIT, "FIXME: lumps_len called with" \
731 " null send_sock\n"); \
734 case SUBST_SND_ALL: \
736 new_len+=send_address_str->len; \
737 if ((send_sock->address.af!=AF_INET) && \
738 (send_address_str==&(send_sock->address_str))) \
740 if ((send_sock->port_no!=SIP_PORT) || \
741 (send_port_str!=&(send_sock->port_no_str))){ \
743 new_len+=1+send_port_str->len; \
745 /*add;transport=xxx*/ \
746 switch(send_sock->proto){ \
749 break; /* udp is the default */ \
752 new_len+=TRANSPORT_PARAM_LEN+3; \
755 new_len+=TRANSPORT_PARAM_LEN+4; \
758 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
764 LOG(L_CRIT, "FIXME: lumps_len called with" \
765 " null send_sock\n"); \
768 case SUBST_NOP: /* do nothing */ \
771 LOG(L_CRIT, "BUG: unknown subst type %d\n", \
772 (subst_l)->u.subst); \
776 send_sock=send_info->send_sock;
782 /* init send_address_str & send_port_str */
783 if (msg->set_global_address.len)
784 send_address_str=&(msg->set_global_address);
786 send_address_str=&(send_sock->address_str);
787 if (msg->set_global_port.len)
788 send_port_str=&(msg->set_global_port);
790 send_port_str=&(send_sock->port_no_str);
793 for(t=lumps;t;t=t->next){
794 /* skip if this is an OPT lump and the condition is not satisfied */
795 if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t, msg, send_info))
797 for(r=t->before;r;r=r->before){
806 /* skip if this is an OPT lump and the condition is
808 if (!lump_check_opt(r, msg, send_info))
812 /* only ADD allowed for before/after */
813 LOG(L_CRIT, "BUG: lumps_len: invalid op "
814 "for data lump (%x)\n", r->op);
826 /* we don't do anything here, it's only a condition for
830 /* fix overlapping deleted zones */
831 if (t->u.offset < s_offset){
833 if (t->len>s_offset-t->u.offset)
834 t->len-=s_offset-t->u.offset;
836 t->u.offset=s_offset;
838 s_offset=t->u.offset+t->len;
842 /* fix offset if overlapping on a deleted zone */
843 if (t->u.offset < s_offset){
844 t->u.offset=s_offset;
846 s_offset=t->u.offset;
850 LOG(L_CRIT,"BUG:lumps_len: invalid"
851 " op for data lump (%x)\n", r->op);
853 for (r=t->after;r;r=r->after){
862 /* skip if this is an OPT lump and the condition is
864 if (!lump_check_opt(r, msg, send_info))
868 /* only ADD allowed for before/after */
869 LOG(L_CRIT, "BUG:lumps_len: invalid"
870 " op for data lump (%x)\n", r->op);
874 ; /* to make gcc 3.* happy */
877 #undef RCVCOMP_LUMP_LEN
878 #undef SENDCOMP_LUMP_LEN
883 /* another helper functions, adds/Removes the lump,
884 code moved form build_req_from_req */
886 static inline void process_lumps( struct sip_msg* msg,
889 unsigned int* new_buf_offs,
890 unsigned int* orig_offs,
891 struct dest_info* send_info)
899 str* send_address_str;
901 struct socket_info* send_sock;
904 #define RCVCOMP_PARAM_ADD \
905 /* add ;comp=xxxx */ \
906 switch(msg->rcv.comp){ \
910 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
911 offset+=COMP_PARAM_LEN; \
912 memcpy(new_buf+offset, SIGCOMP_NAME, \
914 offset+=SIGCOMP_NAME_LEN; \
917 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
918 offset+=COMP_PARAM_LEN; \
919 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
920 offset+=SERGZ_NAME_LEN; \
923 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
927 #define SENDCOMP_PARAM_ADD \
928 /* add ;comp=xxxx */ \
929 switch(send_info->comp){ \
933 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
934 offset+=COMP_PARAM_LEN; \
935 memcpy(new_buf+offset, SIGCOMP_NAME, \
937 offset+=SIGCOMP_NAME_LEN; \
940 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
941 offset+=COMP_PARAM_LEN; \
942 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
943 offset+=SERGZ_NAME_LEN; \
946 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
950 #define RCVCOMP_PARAM_ADD
951 #define SENDCOMP_PARAM_ADD
952 #endif /* USE_COMP */
954 #define SUBST_LUMP(subst_l) \
955 switch((subst_l)->u.subst){ \
957 if (msg->rcv.bind_address){ \
958 if (msg->rcv.bind_address->address.af!=AF_INET){\
959 new_buf[offset]='['; offset++; \
961 memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
962 msg->rcv.bind_address->address_str.len); \
963 offset+=msg->rcv.bind_address->address_str.len; \
964 if (msg->rcv.bind_address->address.af!=AF_INET){\
965 new_buf[offset]=']'; offset++; \
969 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
972 case SUBST_RCV_PORT: \
973 if (msg->rcv.bind_address){ \
974 memcpy(new_buf+offset, msg->rcv.bind_address->port_no_str.s, \
975 msg->rcv.bind_address->port_no_str.len); \
976 offset+=msg->rcv.bind_address->port_no_str.len; \
979 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
982 case SUBST_RCV_ALL: \
983 if (msg->rcv.bind_address){ \
985 if (msg->rcv.bind_address->address.af!=AF_INET){\
986 new_buf[offset]='['; offset++; \
988 memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
989 msg->rcv.bind_address->address_str.len); \
990 offset+=msg->rcv.bind_address->address_str.len; \
991 if (msg->rcv.bind_address->address.af!=AF_INET){\
992 new_buf[offset]=']'; offset++; \
995 if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
996 new_buf[offset]=':'; offset++; \
997 memcpy(new_buf+offset, \
998 msg->rcv.bind_address->port_no_str.s, \
999 msg->rcv.bind_address->port_no_str.len); \
1000 offset+=msg->rcv.bind_address->port_no_str.len; \
1002 switch(msg->rcv.bind_address->proto){ \
1005 break; /* nothing to do, udp is default*/ \
1007 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1008 TRANSPORT_PARAM_LEN); \
1009 offset+=TRANSPORT_PARAM_LEN; \
1010 memcpy(new_buf+offset, "tcp", 3); \
1014 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1015 TRANSPORT_PARAM_LEN); \
1016 offset+=TRANSPORT_PARAM_LEN; \
1017 memcpy(new_buf+offset, "tls", 3); \
1021 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1022 TRANSPORT_PARAM_LEN); \
1023 offset+=TRANSPORT_PARAM_LEN; \
1024 memcpy(new_buf+offset, "sctp", 4); \
1028 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1029 msg->rcv.bind_address->proto); \
1034 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1037 case SUBST_SND_IP: \
1039 if ((send_sock->address.af!=AF_INET) && \
1040 (send_address_str==&(send_sock->address_str))){\
1041 new_buf[offset]='['; offset++; \
1043 memcpy(new_buf+offset, send_address_str->s, \
1044 send_address_str->len); \
1045 offset+=send_address_str->len; \
1046 if ((send_sock->address.af!=AF_INET) && \
1047 (send_address_str==&(send_sock->address_str))){\
1048 new_buf[offset]=']'; offset++; \
1052 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1053 " null send_sock\n"); \
1056 case SUBST_SND_PORT: \
1058 memcpy(new_buf+offset, send_port_str->s, \
1059 send_port_str->len); \
1060 offset+=send_port_str->len; \
1063 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1064 " null send_sock\n"); \
1067 case SUBST_SND_ALL: \
1070 if ((send_sock->address.af!=AF_INET) && \
1071 (send_address_str==&(send_sock->address_str))){\
1072 new_buf[offset]='['; offset++; \
1074 memcpy(new_buf+offset, send_address_str->s, \
1075 send_address_str->len); \
1076 offset+=send_address_str->len; \
1077 if ((send_sock->address.af!=AF_INET) && \
1078 (send_address_str==&(send_sock->address_str))){\
1079 new_buf[offset]=']'; offset++; \
1082 if ((send_sock->port_no!=SIP_PORT) || \
1083 (send_port_str!=&(send_sock->port_no_str))){ \
1084 new_buf[offset]=':'; offset++; \
1085 memcpy(new_buf+offset, send_port_str->s, \
1086 send_port_str->len); \
1087 offset+=send_port_str->len; \
1089 switch(send_sock->proto){ \
1092 break; /* nothing to do, udp is default*/ \
1094 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1095 TRANSPORT_PARAM_LEN); \
1096 offset+=TRANSPORT_PARAM_LEN; \
1097 memcpy(new_buf+offset, "tcp", 3); \
1101 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1102 TRANSPORT_PARAM_LEN); \
1103 offset+=TRANSPORT_PARAM_LEN; \
1104 memcpy(new_buf+offset, "tls", 3); \
1108 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1109 TRANSPORT_PARAM_LEN); \
1110 offset+=TRANSPORT_PARAM_LEN; \
1111 memcpy(new_buf+offset, "sctp", 4); \
1115 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1116 send_sock->proto); \
1118 SENDCOMP_PARAM_ADD \
1121 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1124 case SUBST_RCV_PROTO: \
1125 if (msg->rcv.bind_address){ \
1126 switch(msg->rcv.bind_address->proto){ \
1129 memcpy(new_buf+offset, "udp", 3); \
1133 memcpy(new_buf+offset, "tcp", 3); \
1137 memcpy(new_buf+offset, "tls", 3); \
1141 memcpy(new_buf+offset, "sctp", 4); \
1145 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1146 msg->rcv.bind_address->proto); \
1150 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1154 case SUBST_SND_PROTO: \
1156 switch(send_sock->proto){ \
1159 memcpy(new_buf+offset, "udp", 3); \
1163 memcpy(new_buf+offset, "tcp", 3); \
1167 memcpy(new_buf+offset, "tls", 3); \
1171 memcpy(new_buf+offset, "sctp", 4); \
1175 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1176 send_sock->proto); \
1180 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1185 LOG(L_CRIT, "BUG: process_lumps: unknown subst type %d\n", \
1186 (subst_l)->u.subst); \
1190 send_sock=send_info->send_sock;
1194 /* init send_address_str & send_port_str */
1195 if (msg->set_global_address.len)
1196 send_address_str=&(msg->set_global_address);
1198 send_address_str=&(send_sock->address_str);
1199 if (msg->set_global_port.len)
1200 send_port_str=&(msg->set_global_port);
1202 send_port_str=&(send_sock->port_no_str);
1206 offset=*new_buf_offs;
1207 s_offset=*orig_offs;
1209 for (t=lumps;t;t=t->next){
1212 case LUMP_ADD_SUBST:
1214 /* skip if this is an OPT lump and the condition is
1216 if ((t->op==LUMP_ADD_OPT) &&
1217 (!lump_check_opt(t, msg, send_info)))
1219 /* just add it here! */
1220 /* process before */
1221 for(r=t->before;r;r=r->before){
1224 /*just add it here*/
1225 memcpy(new_buf+offset, r->u.value, r->len);
1228 case LUMP_ADD_SUBST:
1232 /* skip if this is an OPT lump and the condition is
1234 if (!lump_check_opt(r, msg, send_info))
1238 /* only ADD allowed for before/after */
1239 LOG(L_CRIT, "BUG:process_lumps: "
1240 "invalid op for data lump (%x)\n", r->op);
1244 /* copy "main" part */
1247 memcpy(new_buf+offset, t->u.value, t->len);
1250 case LUMP_ADD_SUBST:
1254 /* do nothing, it's only a condition */
1257 /* should not ever get here */
1258 LOG(L_CRIT, "BUG: process_lumps: unhandled data lump "
1262 for(r=t->after;r;r=r->after){
1265 /*just add it here*/
1266 memcpy(new_buf+offset, r->u.value, r->len);
1269 case LUMP_ADD_SUBST:
1273 /* skip if this is an OPT lump and the condition is
1275 if (!lump_check_opt(r, msg, send_info))
1279 /* only ADD allowed for before/after */
1280 LOG(L_CRIT, "BUG:process_lumps: "
1281 "invalid op for data lump (%x)\n", r->op);
1288 /* copy till offset */
1289 if (s_offset>t->u.offset){
1290 DBG("Warning: (%d) overlapped lumps offsets,"
1291 " ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
1292 /* this should've been fixed above (when computing len) */
1296 size=t->u.offset-s_offset;
1298 memcpy(new_buf+offset, orig+s_offset,size);
1302 /* process before */
1303 for(r=t->before;r;r=r->before){
1306 /*just add it here*/
1307 memcpy(new_buf+offset, r->u.value, r->len);
1310 case LUMP_ADD_SUBST:
1314 /* skip if this is an OPT lump and the condition is
1316 if (!lump_check_opt(r, msg, send_info))
1317 goto skip_nop_before;
1320 /* only ADD allowed for before/after */
1321 LOG(L_CRIT, "BUG:process_lumps: "
1322 "invalid op for data lump (%x)\n",r->op);
1326 /* process main (del only) */
1327 if (t->op==LUMP_DEL){
1328 /* skip len bytes from orig msg */
1332 for(r=t->after;r;r=r->after){
1335 /*just add it here*/
1336 memcpy(new_buf+offset, r->u.value, r->len);
1339 case LUMP_ADD_SUBST:
1343 /* skip if this is an OPT lump and the condition is
1345 if (!lump_check_opt(r, msg, send_info))
1346 goto skip_nop_after;
1349 /* only ADD allowed for before/after */
1350 LOG(L_CRIT, "BUG:process_lumps: "
1351 "invalid op for data lump (%x)\n", r->op);
1357 LOG(L_CRIT, "BUG: process_lumps: "
1358 "unknown op (%x)\n", t->op);
1361 *new_buf_offs=offset;
1362 *orig_offs=s_offset;
1363 #undef RCVCOMP_PARAM_ADD
1364 #undef SENDCOMP_PARAM_ADD
1369 * Adjust/insert Content-Length if necessary
1371 static inline int adjust_clen(struct sip_msg* msg, int body_delta, int proto)
1373 struct lump* anchor;
1375 int clen_len, body_only;
1379 #endif /* USE_TCP */
1381 /* Calculate message length difference caused by lumps modifying message
1382 * body, from this point on the message body must not be modified. Zero
1383 * value indicates that the body hasn't been modified
1390 /* check to see if we need to add clen */
1392 if (proto == PROTO_TCP
1394 || proto == PROTO_TLS
1397 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
1398 LOG(L_ERR, "adjust_clen: error parsing content-length\n");
1401 if (unlikely(msg->content_length==0)){
1402 /* not present, we need to add it */
1403 /* msg->unparsed should point just before the final crlf
1404 * - whole message was parsed by the above parse_headers
1405 * which did not find content-length */
1406 anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1407 HDR_CONTENTLENGTH_T);
1409 LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1414 /* compute current content length and compare it with the
1415 one in the message */
1417 if (unlikely(body==0)){
1418 ser_error=E_BAD_REQ;
1419 LOG(L_ERR, "adjust_clen: no message body found"
1420 " (missing crlf?)");
1423 comp_clen=msg->len-(int)(body-msg->buf)+body_delta;
1424 if (comp_clen!=(int)(long)msg->content_length->parsed){
1425 /* note: we don't distinguish here between received with
1426 wrong content-length and content-length changed, we just
1427 fix it automatically in both cases (the reason being
1428 that an error message telling we have received a msg-
1429 with wrong content-length is of very little use) */
1430 anchor = del_lump(msg, msg->content_length->body.s-msg->buf,
1431 msg->content_length->body.len,
1432 HDR_CONTENTLENGTH_T);
1434 LOG(L_ERR, "adjust_clen: Can't remove original"
1435 " Content-Length\n");
1442 #endif /* USE_TCP */
1444 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
1445 LOG(L_ERR, "adjust_clen: Error parsing Content-Length\n");
1449 /* The body has been changed, try to find
1450 * existing Content-Length
1452 /* no need for Content-Length if it's and UDP packet and
1453 * it hasn't Content-Length already */
1454 if ((msg->content_length==0)){
1455 /* content-length doesn't exist, append it */
1456 /* msg->unparsed should point just before the final crlf
1457 * - whole message was parsed by the above parse_headers
1458 * which did not find content-length */
1459 if (proto!=PROTO_UDP){
1460 anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1461 HDR_CONTENTLENGTH_T);
1463 LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1468 DBG("adjust_clen: UDP packet with no clen => "
1469 "not adding one \n"); */
1471 /* Content-Length has been found, remove it */
1472 anchor = del_lump( msg, msg->content_length->body.s - msg->buf,
1473 msg->content_length->body.len,
1474 HDR_CONTENTLENGTH_T);
1476 LOG(L_ERR, "adjust_clen: Can't remove original"
1477 " Content-Length\n");
1484 clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
1485 if (!clen_buf) goto error;
1486 if (insert_new_lump_after(anchor, clen_buf, clen_len,
1487 HDR_CONTENTLENGTH_T) == 0)
1493 if (clen_buf) pkg_free(clen_buf);
1499 /** builds a request in memory from another sip request.
1501 * Side-effects: - it adds lumps to the msg which are _not_ cleaned.
1502 * The added lumps are HDR_VIA_T (almost always added), HDR_CONTENLENGTH_T
1503 * and HDR_ROUTE_T (when a Route: header is added as a result of a non-null
1505 * - it might change send_info->proto and send_info->send_socket
1506 * if proto fallback is enabled (see below).
1508 * Uses also global_req_flags ( OR'ed with msg->msg_flags, see send_info
1511 * @param msg - sip message structure, complete with lumps
1512 * @param returned_len - result length (filled in)
1513 * @param send_info - dest_info structure (value/result), contains where the
1514 * packet will be sent to (it's needed for building a
1515 * correct via, fill RR lumps a.s.o.). If MTU based
1516 * protocol fall-back is enabled (see flags below),
1517 * send_info->proto might be updated with the new
1519 * msg->msg_flags used:
1520 * - FL_TCP_MTU_FB, FL_TLS_MTU_FB and FL_SCTP_MTU_FB -
1521 * fallback to the corresp. proto if the built
1522 * message > mtu and send_info->proto==PROTO_UDP.
1523 * It will also update send_info->proto.
1524 * - FL_FORCE_RPORT: add rport to via
1525 * @param mode - flags for building the message, can be a combination of:
1526 * * BUILD_NO_LOCAL_VIA - don't add a local via
1527 * * BUILD_NO_VIA1_UPDATE - don't update first via (rport,
1529 * * BUILD_NO_PATH - don't add a Route: header with the
1530 * msg->path_vec content.
1531 * * BUILD_IN_SHM - build the result in shm memory
1533 * @return pointer to the new request (pkg_malloc'ed or shm_malloc'ed,
1534 * depending on the presence of the BUILD_IN_SHM flag, needs freeing when
1535 * done) and sets returned_len or 0 on error.
1537 char * build_req_buf_from_sip_req( struct sip_msg* msg,
1538 unsigned int *returned_len,
1539 struct dest_info* send_info,
1542 unsigned int len, new_len, received_len, rport_len, uri_len, via_len,
1550 unsigned int offset, s_offset, size;
1551 struct lump* via_anchor;
1552 struct lump* via_lump;
1553 struct lump* via_insert_param;
1554 struct lump* path_anchor;
1555 struct lump* path_lump;
1558 unsigned int udp_mtu;
1559 struct dest_info di;
1576 flags=msg->msg_flags|global_req_flags;
1577 /* Calculate message body difference and adjust Content-Length */
1578 body_delta = lumps_len(msg, msg->body_lumps, send_info);
1579 if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
1580 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1581 " Content-Length\n");
1585 if(unlikely(mode&BUILD_NO_LOCAL_VIA))
1586 goto after_local_via;
1588 /* create the via header */
1589 branch.s=msg->add_to_branch_s;
1590 branch.len=msg->add_to_branch_len;
1592 via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
1593 if (unlikely(via_anchor==0)) goto error00;
1594 line_buf = create_via_hf( &via_len, msg, send_info, &branch);
1595 if (unlikely(!line_buf)){
1596 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
1597 "memory allocation failure\n");
1601 if(unlikely(mode&BUILD_NO_VIA1_UPDATE))
1602 goto after_update_via1;
1603 /* check if received needs to be added */
1604 if ( received_test(msg) ) {
1605 if ((received_buf=received_builder(msg,&received_len))==0){
1606 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1607 " received_builder failed\n");
1608 goto error01; /* free also line_buf */
1612 /* check if rport needs to be updated:
1613 * - if FL_FORCE_RPORT is set add it (and del. any previous version)
1614 * - if via already contains an rport add it and overwrite the previous
1615 * rport value if present (if you don't want to overwrite the previous
1616 * version remove the comments) */
1617 if ((flags&FL_FORCE_RPORT)||
1618 (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1619 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1620 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1621 " rport_builder failed\n");
1622 goto error01; /* free everything */
1626 /* find out where the offset of the first parameter that should be added
1627 * (after host:port), needed by add receive & maybe rport */
1628 if (msg->via1->params.s){
1629 size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
1632 size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
1633 if (msg->via1->port!=0){
1634 /*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
1635 size += msg->via1->port_str.len + 1; /* +1 for ':'*/
1638 /* no longer necessary, now hots.s contains [] */
1640 if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
1644 /* if received needs to be added, add anchor after host and add it, or
1645 * overwrite the previous one if already present */
1647 if (msg->via1->received){ /* received already present => overwrite it*/
1648 via_insert_param=del_lump(msg,
1649 msg->via1->received->start-buf-1, /*;*/
1650 msg->via1->received->size+1, /*;*/ HDR_VIA_T);
1651 }else if (via_insert_param==0){ /* receive not present, ok */
1652 via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1655 if (via_insert_param==0) goto error02; /* free received_buf */
1656 if (insert_new_lump_after(via_insert_param, received_buf, received_len,
1657 HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
1659 /* if rport needs to be updated, delete it if present and add it's value */
1661 if (msg->via1->rport){ /* rport already present */
1662 via_insert_param=del_lump(msg,
1663 msg->via1->rport->start-buf-1, /*';'*/
1664 msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
1665 }else if (via_insert_param==0){ /*force rport, no rport present */
1666 /* no rport, add it */
1667 via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1670 if (via_insert_param==0) goto error03; /* free rport_buf */
1671 if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
1673 goto error03; /* free rport_buf */
1678 /* add route with path content */
1679 if(unlikely(!(mode&BUILD_NO_PATH) && msg->path_vec.s &&
1680 msg->path_vec.len)){
1681 path_buf.len=ROUTE_PREFIX_LEN+msg->path_vec.len+CRLF_LEN;
1682 path_buf.s=pkg_malloc(path_buf.len+1);
1683 if (unlikely(path_buf.s==0)){
1684 LOG(L_ERR, "out of memory\n");
1685 ser_error=E_OUT_OF_MEM;
1688 memcpy(path_buf.s, ROUTE_PREFIX, ROUTE_PREFIX_LEN);
1689 memcpy(path_buf.s+ROUTE_PREFIX_LEN, msg->path_vec.s,
1691 memcpy(path_buf.s+ROUTE_PREFIX_LEN+msg->path_vec.len, CRLF, CRLF_LEN);
1692 path_buf.s[path_buf.len]=0;
1693 /* insert Route header either before the other routes
1694 (if present & parsed), after the local via or after in front of
1695 the first via if we don't add a local via*/
1697 path_anchor=anchor_lump(msg, msg->route->name.s-buf, 0,
1699 }else if (likely(via_anchor)){
1700 path_anchor=via_anchor;
1701 }else if (likely(msg->via1)){
1702 path_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0,
1705 /* if no via1 (theoretically possible for non-sip messages,
1706 e.g. http xmlrpc) */
1707 path_anchor=anchor_lump(msg, msg->headers->name.s-buf, 0,
1710 if (unlikely(path_anchor==0))
1712 if (unlikely((path_lump=insert_new_lump_after(path_anchor, path_buf.s,
1717 /* compute new msg len and fix overlapping zones*/
1718 new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
1720 LOG(L_ERR, "DEBUG: new_len(%d)=len(%d)+lumps_len\n", new_len, len);
1722 udp_mtu=cfg_get(core, core_cfg, udp_mtu);
1723 di.proto=PROTO_NONE;
1724 if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu &&
1725 (flags & FL_MTU_FB_MASK) && (new_len>udp_mtu)
1726 && (!(mode&BUILD_NO_LOCAL_VIA)))){
1728 di=*send_info; /* copy whole struct - will be used in the Via builder */
1729 di.proto=PROTO_NONE; /* except the proto */
1731 if (!tcp_disable && (flags & FL_MTU_TCP_FB) &&
1732 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TCP))){
1736 else if (!tls_disable && (flags & FL_MTU_TLS_FB) &&
1737 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TLS))){
1740 #endif /* USE_TLS */
1741 #endif /* USE_TCP */
1745 #endif /* USE_TCP */
1746 if (!sctp_disable && (flags & FL_MTU_SCTP_FB) &&
1747 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_SCTP))){
1748 di.proto=PROTO_SCTP;
1750 #endif /* USE_SCTP */
1752 if (di.proto!=PROTO_NONE){
1754 if(likely(line_buf)) pkg_free(line_buf);
1755 line_buf = create_via_hf( &via_len, msg, &di, &branch);
1757 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
1758 "memory allocation failure!\n");
1764 /* add via header to the list */
1765 /* try to add it before msg. 1st via */
1766 /* add first via, as an anchor for second via*/
1767 if(likely(line_buf)) {
1768 if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
1772 if (msg->new_uri.s){
1773 uri_len=msg->new_uri.len;
1774 new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
1776 if(unlikely(mode&BUILD_IN_SHM))
1777 new_buf=(char*)shm_malloc(new_len+1);
1779 new_buf=(char*)pkg_malloc(new_len+1);
1781 ser_error=E_OUT_OF_MEM;
1782 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
1787 if (msg->new_uri.s){
1788 /* copy message up to uri */
1789 size=msg->first_line.u.request.uri.s-buf;
1790 memcpy(new_buf, buf, size);
1794 memcpy(new_buf+offset, msg->new_uri.s, uri_len);
1796 s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
1799 /* copy msg adding/removing lumps */
1800 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
1801 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info);
1802 /* copy the rest of the message */
1803 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
1806 /* update the send_info if udp_mtu affected */
1807 if (di.proto!=PROTO_NONE) {
1808 send_info->proto=di.proto;
1809 send_info->send_sock=di.send_sock;
1813 if (new_buf[new_len-1]==0) {
1814 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: 0 in the end\n");
1819 *returned_len=new_len;
1824 if (received_buf) pkg_free(received_buf);
1826 if (rport_buf) pkg_free(rport_buf);
1828 if (line_buf) pkg_free(line_buf);
1830 if (path_buf.s) pkg_free(path_buf.s);
1838 char * build_res_buf_from_sip_res( struct sip_msg* msg,
1839 unsigned int *returned_len)
1841 unsigned int new_len, via_len, body_delta;
1843 unsigned offset, s_offset, via_offset;
1850 /* we must remove the first via */
1851 if (msg->via1->next) {
1852 via_len=msg->via1->bsize;
1853 via_offset=msg->h_via1->body.s-buf;
1855 via_len=msg->h_via1->len;
1856 via_offset=msg->h_via1->name.s-buf;
1859 /* Calculate message body difference and adjust
1862 body_delta = lumps_len(msg, msg->body_lumps, 0);
1863 if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
1865 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1866 " Content-Length\n");
1870 /* remove the first via*/
1871 if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
1872 LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
1877 new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
1878 know the send sock */
1880 DBG(" old size: %d, new size: %d\n", len, new_len);
1881 new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
1884 LOG(L_ERR, "ERROR: build_res_buf_from_sip_res: out of mem\n");
1887 new_buf[new_len]=0; /* debug: print the message */
1889 /*FIXME: no send sock*/
1890 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0);/*FIXME:*/
1891 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0);
1892 /* copy the rest of the message */
1893 memcpy(new_buf+offset,
1897 DBG("build_res_from_sip_res: copied size: orig:%d, new: %d, rest: %d"
1898 " msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
1900 *returned_len=new_len;
1908 char * build_res_buf_from_sip_req( unsigned int code, str *text ,str *new_tag,
1909 struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
1912 unsigned int len,foo;
1913 struct hdr_field *hdr;
1914 struct lump_rpl *lump;
1915 struct lump_rpl *body;
1918 unsigned int received_len;
1920 unsigned int rport_len;
1922 unsigned int warning_len;
1923 char* content_len_buf;
1924 unsigned int content_len_len;
1931 received_buf=rport_buf=warning_buf=content_len_buf=0;
1932 received_len=rport_len=warning_len=content_len_len=0;
1934 to_tag.s=0; /* fixes gcc 4.0 warning */
1937 /* force parsing all headers -- we want to return all
1938 Via's in the reply and they may be scattered down to the
1939 end of header (non-block Vias are a really poor property
1941 if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
1942 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1943 "alas, parse_headers failed\n");
1947 /*computes the length of the new response buffer*/
1950 /* check if received needs to be added */
1951 if (received_test(msg)) {
1952 if ((received_buf=received_builder(msg,&received_len))==0) {
1953 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1954 "alas, received_builder failed\n");
1958 /* check if rport needs to be updated */
1959 if ( ((msg->msg_flags|global_req_flags)&FL_FORCE_RPORT)||
1960 (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1961 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1962 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"
1963 " rport_builder failed\n");
1964 goto error01; /* free everything */
1966 if (msg->via1->rport)
1967 len -= msg->via1->rport->size+1; /* include ';' */
1971 len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
1972 text->len + CRLF_LEN/*new line*/;
1973 /*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
1974 for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
1975 switch (hdr->type) {
1977 if (new_tag && new_tag->len) {
1978 to_tag=get_to(msg)->tag_value;
1980 len+=new_tag->len-to_tag.len;
1982 len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
1987 /* we always add CRLF to via*/
1988 len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
1989 if (hdr==msg->h_via1) len += received_len+rport_len;
1991 case HDR_RECORDROUTE_T:
1992 /* RR only for 1xx and 2xx replies */
1993 if (code<180 || code>=300)
1998 /* we keep the original termination for these headers*/
2002 /* do nothing, we are interested only in the above headers */
2007 for(lump=msg->reply_lump;lump;lump=lump->next) {
2008 len += lump->text.len;
2009 if (lump->flags&LUMP_RPL_BODY)
2013 if (server_signature)
2014 len += server_hdr.len + CRLF_LEN;
2017 warning_buf = warning_builder(msg,&warning_len);
2018 if (warning_buf) len += warning_len + CRLF_LEN;
2019 else LOG(L_WARN, "WARNING: warning skipped -- too big\n");
2021 /* content length hdr */
2023 content_len_buf = int2str(body->text.len, (int*)&content_len_len);
2024 len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
2026 len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
2028 /* end of message */
2029 len += CRLF_LEN; /*new line*/
2032 buf = (char*) pkg_malloc( len+1 );
2035 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: out of memory "
2036 " ; needs %d\n",len);
2040 /* filling the buffer*/
2043 memcpy( p , msg->first_line.u.request.version.s ,
2044 msg->first_line.u.request.version.len);
2045 p += msg->first_line.u.request.version.len;
2048 for ( i=2 , foo = code ; i>=0 ; i-- , foo=foo/10 )
2049 *(p+i) = '0' + foo - ( foo/10 )*10;
2052 memcpy( p , text->s , text->len );
2054 memcpy( p, CRLF, CRLF_LEN );
2057 for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
2061 if (hdr==msg->h_via1){
2063 if (msg->via1->rport){ /* delete the old one */
2064 /* copy until rport */
2065 append_str_trans( p, hdr->name.s ,
2066 msg->via1->rport->start-hdr->name.s-1,msg);
2067 /* copy new rport */
2068 append_str(p, rport_buf, rport_len);
2069 /* copy the rest of the via */
2070 append_str_trans(p, msg->via1->rport->start+
2071 msg->via1->rport->size,
2072 hdr->body.s+hdr->body.len-
2073 msg->via1->rport->start-
2074 msg->via1->rport->size, msg);
2075 }else{ /* just append the new one */
2076 /* normal whole via copy */
2077 append_str_trans( p, hdr->name.s ,
2078 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2079 append_str(p, rport_buf, rport_len);
2082 /* normal whole via copy */
2083 append_str_trans( p, hdr->name.s ,
2084 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2087 append_str( p, received_buf, received_len);
2089 /* normal whole via copy */
2090 append_str_trans( p, hdr->name.s,
2091 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2093 append_str( p, CRLF,CRLF_LEN);
2095 case HDR_RECORDROUTE_T:
2096 /* RR only for 1xx and 2xx replies */
2097 if (code<180 || code>=300) break;
2098 append_str(p, hdr->name.s, hdr->len);
2101 if (new_tag && new_tag->len){
2102 if (to_tag.s ) { /* replacement */
2104 append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
2105 /* to tag replacement */
2106 bmark->to_tag_val.s=p;
2107 bmark->to_tag_val.len=new_tag->len;
2108 append_str( p, new_tag->s,new_tag->len);
2109 /* the rest after to-tag */
2110 append_str( p, to_tag.s+to_tag.len,
2111 hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
2112 }else{ /* adding a new to-tag */
2113 after_body=hdr->body.s+hdr->body.len;
2114 append_str( p, hdr->name.s, after_body-hdr->name.s);
2115 append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
2116 bmark->to_tag_val.s=p;
2117 bmark->to_tag_val.len=new_tag->len;
2118 append_str( p, new_tag->s,new_tag->len);
2119 append_str( p, after_body,
2120 hdr->name.s+hdr->len-after_body);
2123 } /* no new to-tag -- proceed to 1:1 copying */
2124 totags=((struct to_body*)(hdr->parsed))->tag_value.s;
2126 bmark->to_tag_val.s=p+(totags-hdr->name.s);
2127 bmark->to_tag_val.len=
2128 ((struct to_body*)(hdr->parsed))->tag_value.len;
2131 bmark->to_tag_val.len = 0;
2132 bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
2138 append_str(p, hdr->name.s, hdr->len);
2141 /* do nothing, we are interested only in the above headers */
2146 for(lump=msg->reply_lump;lump;lump=lump->next)
2147 if (lump->flags&LUMP_RPL_HDR){
2148 memcpy(p,lump->text.s,lump->text.len);
2149 p += lump->text.len;
2152 if (server_signature) {
2153 memcpy( p, server_hdr.s, server_hdr.len );
2155 memcpy( p, CRLF, CRLF_LEN );
2158 /* content_length hdr */
2159 if (content_len_len) {
2160 append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
2161 append_str( p, content_len_buf, content_len_len );
2162 append_str( p, CRLF, CRLF_LEN );
2164 append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
2166 /* warning header */
2168 memcpy( p, warning_buf, warning_len);
2170 memcpy( p, CRLF, CRLF_LEN);
2174 memcpy( p, CRLF, CRLF_LEN );
2178 memcpy ( p, body->text.s, body->text.len );
2183 LOG(L_CRIT,"BUG:build_res_from_sip_req: diff len=%d p-buf=%d\n",
2187 *returned_len = len;
2188 /* in req2reply, received_buf is not introduced to lumps and
2189 needs to be deleted here
2191 if (received_buf) pkg_free(received_buf);
2192 if (rport_buf) pkg_free(rport_buf);
2196 if (received_buf) pkg_free(received_buf);
2197 if (rport_buf) pkg_free(rport_buf);
2205 /* return number of chars printed or 0 if space exceeded;
2206 assumes buffer size of at least MAX_BRANCH_PARAM_LEN
2208 int branch_builder( unsigned int hash_index,
2209 /* only either parameter useful */
2210 unsigned int label, char * char_v,
2212 char *branch_str, int *len )
2218 /* hash id provided ... start with it */
2219 size=MAX_BRANCH_PARAM_LEN;
2223 memcpy(begin, MCOOKIE, MCOOKIE_LEN );
2224 size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
2226 if (int2reverse_hex( &begin, &size, hash_index)==-1)
2230 *begin=BRANCH_SEPARATOR;
2234 /* string with request's characteristic value ... use it ... */
2236 if (memcpy(begin,char_v,MD5_LEN)) {
2237 begin+=MD5_LEN; size-=MD5_LEN;
2239 } else { /* ... use the "label" value otherwise */
2240 if (int2reverse_hex( &begin, &size, label )==-1)
2245 *begin=BRANCH_SEPARATOR;
2249 if (int2reverse_hex( &begin, &size, branch)==-1)
2252 *len=MAX_BRANCH_PARAM_LEN-size;
2259 /* uses only the send_info->send_socket, send_info->proto and
2260 * send_info->comp (so that a send_info used for sending can be passed
2261 * to this function w/o changes and the correct via will be built) */
2262 char* via_builder( unsigned int *len,
2263 struct dest_info* send_info /* where to send the reply */,
2264 str* branch, str* extra_params, struct hostport* hp)
2266 unsigned int via_len, extra_len;
2270 str* address_str; /* address displayed in via */
2271 str* port_str; /* port no displayed in via */
2272 struct socket_info* send_sock;
2273 int comp_len, comp_name_len;
2276 send_sock=send_info->send_sock;
2277 /* use pre-set address in via or the outbound socket one */
2278 if ( hp && hp->host->len)
2279 address_str=hp->host;
2281 address_str=&(send_sock->address_str);
2282 if (hp && hp->port->len)
2285 port_str=&(send_sock->port_no_str);
2287 comp_len=comp_name_len=0;
2290 switch(send_info->comp){
2294 comp_len=COMP_PARAM_LEN;
2295 comp_name_len=SIGCOMP_NAME_LEN;
2296 comp_name=SIGCOMP_NAME;
2299 comp_len=COMP_PARAM_LEN;
2300 comp_name_len=SERGZ_NAME_LEN;
2301 comp_name=SERGZ_NAME;
2304 LOG(L_CRIT, "BUG: via_builder: unknown comp %d\n",
2306 /* continue, we'll just ignore comp */
2308 #endif /* USE_COMP */
2310 via_prefix_len=MY_VIA_LEN+(send_info->proto==PROTO_SCTP);
2311 max_len=via_prefix_len +address_str->len /* space in MY_VIA */
2312 +2 /* just in case it is a v6 address ... [ ] */
2313 +1 /*':'*/+port_str->len
2314 +(branch?(MY_BRANCH_LEN+branch->len):0)
2315 +(extra_params?extra_params->len:0)
2316 +comp_len+comp_name_len
2318 line_buf=pkg_malloc( max_len );
2320 ser_error=E_OUT_OF_MEM;
2321 LOG(L_ERR, "ERROR: via_builder: out of memory\n");
2327 via_len=via_prefix_len+address_str->len; /*space included in MY_VIA*/
2329 memcpy(line_buf, MY_VIA, MY_VIA_LEN);
2330 if (send_info->proto==PROTO_UDP){
2332 }else if (send_info->proto==PROTO_TCP){
2333 memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
2334 }else if (send_info->proto==PROTO_TLS){
2335 memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
2336 }else if (send_info->proto==PROTO_SCTP){
2337 memcpy(line_buf+MY_VIA_LEN-4, "SCTP ", 5);
2339 LOG(L_CRIT, "BUG: via_builder: unknown proto %d\n", send_info->proto);
2343 /* add [] only if ipv6 and outbound socket address is used;
2344 * if using pre-set no check is made */
2345 if ((send_sock->address.af==AF_INET6) &&
2346 (address_str==&(send_sock->address_str))) {
2347 line_buf[via_prefix_len]='[';
2348 line_buf[via_prefix_len+1+address_str->len]=']';
2350 via_len+=2; /* [ ]*/
2353 memcpy(line_buf+via_prefix_len+extra_len, address_str->s,
2355 if ((send_sock->port_no!=SIP_PORT) ||
2356 (port_str!=&send_sock->port_no_str)){
2357 line_buf[via_len]=':'; via_len++;
2358 memcpy(line_buf+via_len, port_str->s, port_str->len);
2359 via_len+=port_str->len;
2362 /* branch parameter */
2364 memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
2365 via_len+=MY_BRANCH_LEN;
2366 memcpy(line_buf+via_len, branch->s, branch->len );
2367 via_len+=branch->len;
2371 memcpy(line_buf+via_len, extra_params->s, extra_params->len);
2372 via_len+=extra_params->len;
2377 memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
2378 via_len+=COMP_PARAM_LEN;
2379 memcpy(line_buf+via_len, comp_name, comp_name_len);
2380 via_len+=comp_name_len;
2384 memcpy(line_buf+via_len, CRLF, CRLF_LEN);
2386 line_buf[via_len]=0; /* null terminate the string*/
2392 /* creates a via header honoring the protocol of the incomming socket
2393 * msg is an optional parameter */
2394 char* create_via_hf( unsigned int *len,
2395 struct sip_msg *msg,
2396 struct dest_info* send_info /* where to send the reply */,
2402 #if defined USE_TCP || defined USE_SCTP
2404 unsigned int id_len;
2414 #if defined USE_TCP || defined USE_SCTP
2418 (msg->rcv.proto==PROTO_TCP)
2420 || (msg->rcv.proto==PROTO_TLS)
2424 #endif /* USE_SCTP */
2425 #endif /* USE_TCP */
2427 (msg->rcv.proto==PROTO_SCTP)
2428 #endif /* USE_SCTP */
2430 if ((id_buf=id_builder(msg, &id_len))==0){
2431 LOG(L_ERR, "ERROR: create_via_hf:"
2432 " id_builder failed\n");
2433 return 0; /* we don't need to free anything,
2434 nothing alloc'ed yet*/
2436 DBG("create_via_hf: id added: <%.*s>, rcv proto=%d\n",
2437 (int)id_len, id_buf, msg->rcv.proto);
2438 extra_params.s=id_buf;
2439 extra_params.len=id_len;
2441 #endif /* USE_TCP || USE_SCTP */
2443 /* test and add rport parameter to local via - rfc3581 */
2444 if(msg->msg_flags&FL_ADD_LOCAL_RPORT) {
2445 /* params so far + ';rport' + '\0' */
2446 via = (char*)pkg_malloc(extra_params.len+RPORT_LEN);
2448 LM_ERR("building local rport via param failed\n");
2449 if (extra_params.s) pkg_free(extra_params.s);
2452 if(extra_params.len!=0) {
2453 memcpy(via, extra_params.s, extra_params.len);
2454 pkg_free(extra_params.s);
2456 memcpy(via + extra_params.len, RPORT, RPORT_LEN-1);
2457 extra_params.s = via;
2458 extra_params.len += RPORT_LEN-1;
2459 extra_params.s[extra_params.len]='\0';
2462 set_hostport(&hp, msg);
2463 via = via_builder( len, send_info, branch,
2464 extra_params.len?&extra_params:0, &hp);
2466 /* we do not need extra_params any more, already in the new via header */
2467 if (extra_params.s) pkg_free(extra_params.s);
2471 /* builds a char* buffer from message headers without body
2472 * first line is excluded in case of skip_first_line=1
2473 * error is set -1 if the memory allocation failes
2475 char * build_only_headers( struct sip_msg* msg, int skip_first_line,
2476 unsigned int *returned_len,
2478 struct dest_info* send_info)
2480 char *buf, *new_buf;
2481 unsigned int offset, s_offset, len, new_len;
2485 if (skip_first_line)
2486 s_offset = msg->headers->name.s - buf;
2490 /* original length without body, and without final \r\n */
2491 len = msg->unparsed - buf;
2492 /* new msg length */
2493 new_len = len - /* original length */
2494 s_offset + /* skipped first line */
2495 lumps_len(msg, msg->add_rm, send_info); /* lumps */
2502 new_buf = (char *)pkg_malloc(new_len+1);
2504 LOG(L_ERR, "ERROR: build_only_headers: Not enough memory\n");
2511 /* copy message lumps */
2512 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2513 /* copy the rest of the message without body */
2514 if (len > s_offset) {
2515 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2516 offset += (len-s_offset);
2518 new_buf[offset] = 0;
2520 *returned_len = offset;
2524 /* builds a char* buffer from message body
2525 * error is set -1 if the memory allocation failes
2527 char * build_body( struct sip_msg* msg,
2528 unsigned int *returned_len,
2530 struct dest_info* send_info)
2532 char *buf, *new_buf, *body;
2533 unsigned int offset, s_offset, len, new_len;
2536 body = get_body(msg);
2538 if (!body || (body[0] == 0)) {
2543 s_offset = body - buf;
2545 /* original length of msg with body */
2547 /* new body length */
2548 new_len = len - /* original length */
2549 s_offset + /* msg without body */
2550 lumps_len(msg, msg->body_lumps, send_info); /* lumps */
2552 new_buf = (char *)pkg_malloc(new_len+1);
2554 LOG(L_ERR, "ERROR: build_body: Not enough memory\n");
2561 /* copy body lumps */
2562 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2563 /* copy the rest of the message without body */
2564 if (len > s_offset) {
2565 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2566 offset += (len-s_offset);
2568 new_buf[offset] = 0;
2570 *returned_len = offset;
2574 /* builds a char* buffer from SIP message including body
2575 * The function adjusts the Content-Length HF according
2576 * to body lumps in case of touch_clen=1.
2578 char * build_all( struct sip_msg* msg, int touch_clen,
2579 unsigned int *returned_len,
2581 struct dest_info* send_info)
2583 char *buf, *new_buf;
2584 unsigned int offset, s_offset, len, new_len;
2585 unsigned int body_delta;
2588 /* Calculate message body difference */
2589 body_delta = lumps_len(msg, msg->body_lumps, send_info);
2591 /* adjust Content-Length */
2592 if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
2593 LOG(L_ERR, "ERROR: build_all: Error while adjusting"
2594 " Content-Length\n");
2601 /* original msg length */
2603 /* new msg length */
2604 new_len = len + /* original length */
2605 lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
2606 body_delta; /* body lumps */
2613 new_buf = (char *)pkg_malloc(new_len+1);
2615 LOG(L_ERR, "ERROR: build_all: Not enough memory\n");
2620 offset = s_offset = 0;
2622 /* copy message lumps */
2623 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2624 /* copy body lumps */
2625 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2626 /* copy the rest of the message */
2627 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2628 offset += (len-s_offset);
2629 new_buf[offset] = 0;
2631 *returned_len = offset;
2638 * parse buf in msg and fill several fields
2640 int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len,
2643 if(msg==0 || buf==0)
2646 memset(msg, 0, sizeof(sip_msg_t));
2650 if (parse_msg(buf, len, msg)!=0) {
2651 LM_ERR("parsing failed");
2654 msg->set_global_address=default_global_address;
2655 msg->set_global_port=default_global_port;