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)
66 /* Via special params:
68 * - if the address in via is different from the src_ip or an existing
69 * received=something is found, received=src_ip is added (and any preexisting
70 * received is deleted). received is added as the first via parameter if no
71 * receive is previously present or over the old receive.
72 * - if the original via contains rport / rport=something or msg->msg_flags
73 * FL_FORCE_RPORT is set (e.g. script force_rport() cmd) rport=src_port
74 * is added (over previous rport / as first via param or after received
75 * if no received was present and received is added too)
77 * (see also sl_send_reply)
78 * - rport and received are added in mostly the same way as for requests, but
79 * in the reverse order (first rport and then received). See also
81 * - if reply_to_via is set (default off) the local reply will be sent to
82 * the address in via (received is ignored since it was not set by us). The
83 * destination port is either the message source port if via contains rport
84 * or the FL_FORCE_RPORT flag is set or the port from the via. If either
85 * port or rport are present a normal dns lookup (instead of a srv lookup)
86 * is performed on the address. If no port is present and a srv lookup is
87 * performed the port is taken from the srv lookup. If the srv lookup failed
88 * or it was not performed, the port is set to the default sip port (5060).
89 * - if reply_to_via is off (default) the local reply is sent to the message
90 * source ip address. The destination port is set to the source port if
91 * rport is present or FL_FORCE_RPORT flag is set, to the via port or to
92 * the default sip port (5060) if neither rport or via port are present.
94 * - if received is present the message is sent to the received address else
95 * if no port is present (neither a normal via port or rport) a dns srv
96 * lookup is performed on the host part and the reply is sent to the
97 * resulting ip. If a port is present or the host part is an ip address
98 * the dns lookup will be a "normal" one (A or AAAA).
99 * - if rport is present, it's value will be used as the destination port
100 * (and this will also disable srv lookups)
101 * - if no port is present the destination port will be taken from the srv
102 * lookup. If the srv lookup fails or is not performed (e.g. ip address
103 * in host) the destination port will be set to the default sip port (5060).
106 * - when locally replying to a message, rport and received will be appended to
107 * the via header parameters (for forwarded requests they are inserted at the
109 * - a locally generated reply might get two received via parameters if a
110 * received is already present in the original message (this should not
111 * happen though, but ...)
116 #include <sys/types.h>
117 #include <sys/socket.h>
123 #include "comp_defs.h"
124 #include "msg_translator.h"
130 #include "md5utils.h"
131 #include "data_lump.h"
132 #include "data_lump_rpl.h"
139 #define append_str(_dest,_src,_len) \
141 memcpy( (_dest) , (_src) , (_len) );\
145 #define append_str_trans(_dest,_src,_len,_msg) \
146 append_str( (_dest), (_src), (_len) );
148 extern char version[];
149 extern int version_len;
157 /* checks if ip is in host(name) and ?host(ip)=name?
158 * ip must be in network byte order!
159 * resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
160 * return 0 if equal */
161 static int check_via_address(struct ip_addr* ip, str *name,
162 unsigned short port, int resolver)
169 /* maybe we are lucky and name it's an ip */
172 DBG("check_via_address(%s, %.*s, %d)\n",
173 s, name->len, name->s, resolver);
177 /* check if name->s is an ipv6 address or an ipv6 address ref. */
178 if ((ip->af==AF_INET6) &&
179 ( ((len==name->len)&&(strncasecmp(name->s, s, name->len)==0))
181 ((len==(name->len-2))&&(name->s[0]=='[')&&
182 (name->s[name->len-1]==']')&&
183 (strncasecmp(name->s+1, s, len)==0))
190 if (strncmp(name->s, s, name->len)==0)
193 LOG(L_CRIT, "check_via_address: BUG: could not convert ip address\n");
197 if (port==0) port=SIP_PORT;
198 if (resolver&DO_DNS){
199 DBG("check_via_address: doing dns lookup\n");
200 /* try all names ips */
201 he=sip_resolvehost(name, &port, 0); /* don't use naptr */
202 if (he && ip->af==he->h_addrtype){
203 for(i=0;he && he->h_addr_list[i];i++){
204 if ( memcmp(&he->h_addr_list[i], ip->u.addr, ip->len)==0)
209 if (resolver&DO_REV_DNS){
210 DBG("check_via_address: doing rev. dns lookup\n");
211 /* try reverse dns */
212 he=rev_resolvehost(ip);
213 if (he && (strncmp(he->h_name, name->s, name->len)==0))
215 for (i=0; he && he->h_aliases[i];i++){
216 if (strncmp(he->h_aliases[i],name->s, name->len)==0)
224 /* check if IP address in Via != source IP address of signaling */
225 int received_test( struct sip_msg *msg )
229 rcvd=msg->via1->received
230 || check_via_address(&msg->rcv.src_ip, &msg->via1->host,
231 msg->via1->port, received_dns);
236 static char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
238 static char buf[MAX_WARNING_LEN];
244 #define str_print(string, string_len) \
247 if ((clen+l)>MAX_WARNING_LEN) \
248 goto error_overflow; \
249 memcpy(buf+clen, (string), l); \
253 #define str_lenpair_print(string, string_len, string2, string2_len) \
255 str_print(string, string_len); \
256 str_print(string2, string2_len);\
259 #define str_pair_print( string, string2, string2_len) \
260 str_lenpair_print((string), strlen((string)), (string2), (string2_len))
262 #define str_int_print(string, intval)\
264 t=int2str((intval), &print_len); \
265 str_pair_print(string, t, print_len);\
268 #define str_ipaddr_print(string, ipaddr_val)\
270 t=ip_addr2a((ipaddr_val)); \
271 print_len=strlen(t); \
272 str_pair_print(string, t, print_len);\
276 str_lenpair_print(WARNING, WARNING_LEN,
277 msg->rcv.bind_address->name.s,
278 msg->rcv.bind_address->name.len);
279 str_lenpair_print(":", 1, msg->rcv.bind_address->port_no_str.s,
280 msg->rcv.bind_address->port_no_str.len);
281 str_print(WARNING_PHRASE, WARNING_PHRASE_LEN);
287 foo=&(msg->first_line.u.request.uri);
289 str_int_print(" pid=", my_pid());
291 str_ipaddr_print(" req_src_ip=", &msg->rcv.src_ip);
292 str_int_print(" req_src_port=", msg->rcv.src_port);
293 str_pair_print(" in_uri=", msg->first_line.u.request.uri.s,
294 msg->first_line.u.request.uri.len);
295 str_pair_print(" out_uri=", foo->s, foo->len);
296 str_pair_print(" via_cnt",
297 (msg->parsed_flag & HDR_EOH_F)==HDR_EOH_F ? "=" : ">", 1);
298 str_int_print("=", via_cnt);
299 if (clen<MAX_WARNING_LEN){ buf[clen]='"'; clen++; }
300 else goto error_overflow;
306 LOG(L_NOTICE, "NOTICE: warning_builder: buffer size exceeded (probably too long URI)\n");
314 char* received_builder(struct sip_msg *msg, unsigned int *received_len)
318 struct ip_addr *source_ip;
322 source_ip=&msg->rcv.src_ip;
324 buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
326 ser_error=E_OUT_OF_MEM;
327 LOG(L_ERR, "ERROR: received_builder: out of memory\n");
330 memcpy(buf, RECEIVED, RECEIVED_LEN);
331 if ( (tmp=ip_addr2a(source_ip))==0)
334 len=RECEIVED_LEN+tmp_len;
336 memcpy(buf+RECEIVED_LEN, tmp, tmp_len);
337 buf[len]=0; /*null terminate it */
345 char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
353 tmp=int2str(msg->rcv.src_port, &tmp_len);
354 len=RPORT_LEN+tmp_len;
355 buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
357 ser_error=E_OUT_OF_MEM;
358 LOG(L_ERR, "ERROR: rport_builder: out of memory\n");
361 memcpy(buf, RPORT, RPORT_LEN);
362 memcpy(buf+RPORT_LEN, tmp, tmp_len);
363 buf[len]=0; /*null terminate it*/
371 char* id_builder(struct sip_msg* msg, unsigned int *id_len)
375 char revhex[sizeof(int)*2];
381 if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
382 LOG(L_CRIT, "BUG: id_builder: not enough space for id\n");
385 value_len=p-&revhex[0];
386 len=ID_PARAM_LEN+value_len;
387 buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
389 ser_error=E_OUT_OF_MEM;
390 LOG(L_ERR, "ERROR: id_builder: out of memory\n");
393 memcpy(buf, ID_PARAM, ID_PARAM_LEN);
394 memcpy(buf+ID_PARAM_LEN, revhex, value_len);
395 buf[len]=0; /* null terminate it */
402 char* clen_builder( struct sip_msg* msg, int *clen_len, int diff,
416 LOG(L_ERR, "ERROR: clen_builder: no message body found"
420 value=msg->len-(int)(body-msg->buf)+diff;
421 value_s=int2str(value, &value_len);
422 DBG("clen_builder: content-length: %d (%s)\n", value, value_s);
428 len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
430 buf=pkg_malloc(sizeof(char)*(len+1));
432 ser_error=E_OUT_OF_MEM;
433 LOG(L_ERR, "ERROR: clen_builder: out of memory\n");
437 memcpy(buf, value_s, value_len);
440 memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
441 memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
442 memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
444 buf[len]=0; /* null terminate it */
451 /* checks if a lump opt condition
452 * returns 1 if cond is true, 0 if false */
453 static inline int lump_check_opt( enum lump_conditions cond,
455 struct dest_info* snd_i
462 #define get_ip_port_proto \
463 if ((snd_i==0) || (snd_i->send_sock==0)){ \
464 LOG(L_CRIT, "ERROR: lump_check_opt: null send socket\n"); \
465 return 1; /* we presume they are different :-) */ \
467 if (msg->rcv.bind_address){ \
468 ip=&msg->rcv.bind_address->address; \
469 port=msg->rcv.bind_address->port_no; \
470 proto=msg->rcv.bind_address->proto; \
472 ip=&msg->rcv.dst_ip; \
473 port=msg->rcv.dst_port; \
474 proto=msg->rcv.proto; \
482 case COND_IF_DIFF_REALMS:
484 /* faster tests first */
485 if ((port==snd_i->send_sock->port_no) &&
486 (proto==snd_i->send_sock->proto) &&
488 (msg->rcv.comp==snd_i->comp) &&
490 (ip_addr_cmp(ip, &snd_i->send_sock->address)))
493 case COND_IF_DIFF_AF:
495 if (ip->af!=snd_i->send_sock->address.af) return 1;
497 case COND_IF_DIFF_PROTO:
499 if (proto!=snd_i->send_sock->proto) return 1;
501 case COND_IF_DIFF_PORT:
503 if (port!=snd_i->send_sock->port_no) return 1;
505 case COND_IF_DIFF_IP:
507 if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
510 return (rand()>=RAND_MAX/2);
512 LOG(L_CRIT, "BUG: lump:w_check_opt: unknown lump condition %d\n",
515 return 0; /* false */
520 /* computes the "unpacked" len of a lump list,
521 code moved from build_req_from_req */
522 static inline int lumps_len(struct sip_msg* msg, struct lump* lumps,
523 struct dest_info* send_info)
529 str* send_address_str;
531 struct socket_info* send_sock;
535 #define RCVCOMP_LUMP_LEN \
537 switch(msg->rcv.comp){ \
541 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
544 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
547 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
551 #define SENDCOMP_LUMP_LEN \
553 switch(send_info->comp){ \
557 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
560 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
563 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
567 #define RCVCOMP_LUMP_LEN
568 #define SENDCOMP_LUMP_LEN
571 #define SUBST_LUMP_LEN(subst_l) \
572 switch((subst_l)->u.subst){ \
574 if (msg->rcv.bind_address){ \
575 new_len+=msg->rcv.bind_address->address_str.len; \
576 if (msg->rcv.bind_address->address.af!=AF_INET) \
580 LOG(L_CRIT, "FIXME: null bind_address\n"); \
583 case SUBST_RCV_PORT: \
584 if (msg->rcv.bind_address){ \
585 new_len+=msg->rcv.bind_address->port_no_str.len; \
588 LOG(L_CRIT, "FIXME: null bind_address\n"); \
591 case SUBST_RCV_PROTO: \
592 if (msg->rcv.bind_address){ \
593 switch(msg->rcv.bind_address->proto){ \
604 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
605 msg->rcv.bind_address->proto); \
609 LOG(L_CRIT, "FIXME: null bind_address\n"); \
612 case SUBST_RCV_ALL: \
613 if (msg->rcv.bind_address){ \
614 new_len+=msg->rcv.bind_address->address_str.len; \
615 if (msg->rcv.bind_address->address.af!=AF_INET) \
617 if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
619 new_len+=1+msg->rcv.bind_address->port_no_str.len; \
621 /*add;transport=xxx*/ \
622 switch(msg->rcv.bind_address->proto){ \
625 break; /* udp is the default */ \
628 new_len+=TRANSPORT_PARAM_LEN+3; \
631 new_len+=TRANSPORT_PARAM_LEN+4; \
634 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
635 msg->rcv.bind_address->proto); \
640 LOG(L_CRIT, "FIXME: null bind_address\n"); \
645 new_len+=send_address_str->len; \
646 if (send_sock->address.af!=AF_INET && \
647 send_address_str==&(send_sock->address_str)) \
650 LOG(L_CRIT, "FIXME: lumps_len called with" \
651 " null send_sock\n"); \
654 case SUBST_SND_PORT: \
656 new_len+=send_port_str->len; \
658 LOG(L_CRIT, "FIXME: lumps_len called with" \
659 " null send_sock\n"); \
662 case SUBST_SND_PROTO: \
664 switch(send_sock->proto){ \
675 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
679 LOG(L_CRIT, "FIXME: lumps_len called with" \
680 " null send_sock\n"); \
683 case SUBST_SND_ALL: \
685 new_len+=send_address_str->len; \
686 if ((send_sock->address.af!=AF_INET) && \
687 (send_address_str==&(send_sock->address_str))) \
689 if ((send_sock->port_no!=SIP_PORT) || \
690 (send_port_str!=&(send_sock->port_no_str))){ \
692 new_len+=1+send_port_str->len; \
694 /*add;transport=xxx*/ \
695 switch(send_sock->proto){ \
698 break; /* udp is the default */ \
701 new_len+=TRANSPORT_PARAM_LEN+3; \
704 new_len+=TRANSPORT_PARAM_LEN+4; \
707 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
713 LOG(L_CRIT, "FIXME: lumps_len called with" \
714 " null send_sock\n"); \
717 case SUBST_NOP: /* do nothing */ \
720 LOG(L_CRIT, "BUG: unknown subst type %d\n", \
721 (subst_l)->u.subst); \
725 send_sock=send_info->send_sock;
731 /* init send_address_str & send_port_str */
732 if (msg->set_global_address.len)
733 send_address_str=&(msg->set_global_address);
735 send_address_str=&(send_sock->address_str);
736 if (msg->set_global_port.len)
737 send_port_str=&(msg->set_global_port);
739 send_port_str=&(send_sock->port_no_str);
742 for(t=lumps;t;t=t->next){
743 /* skip if this is an OPT lump and the condition is not satisfied */
744 if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t->u.cond, msg, send_info))
746 for(r=t->before;r;r=r->before){
755 /* skip if this is an OPT lump and the condition is
757 if (!lump_check_opt(r->u.cond, msg, send_info))
761 /* only ADD allowed for before/after */
762 LOG(L_CRIT, "BUG: lumps_len: invalid op "
763 "for data lump (%x)\n", r->op);
775 /* we don't do anything here, it's only a condition for
779 /* fix overlapping deleted zones */
780 if (t->u.offset < s_offset){
782 if (t->len>s_offset-t->u.offset)
783 t->len-=s_offset-t->u.offset;
785 t->u.offset=s_offset;
787 s_offset=t->u.offset+t->len;
791 /* fix offset if overlapping on a deleted zone */
792 if (t->u.offset < s_offset){
793 t->u.offset=s_offset;
795 s_offset=t->u.offset;
799 LOG(L_CRIT,"BUG:lumps_len: invalid"
800 " op for data lump (%x)\n", r->op);
802 for (r=t->after;r;r=r->after){
811 /* skip if this is an OPT lump and the condition is
813 if (!lump_check_opt(r->u.cond, msg, send_info))
817 /* only ADD allowed for before/after */
818 LOG(L_CRIT, "BUG:lumps_len: invalid"
819 " op for data lump (%x)\n", r->op);
823 ; /* to make gcc 3.* happy */
826 #undef RCVCOMP_LUMP_LEN
827 #undef SENDCOMP_LUMP_LEN
832 /* another helper functions, adds/Removes the lump,
833 code moved form build_req_from_req */
835 static inline void process_lumps( struct sip_msg* msg,
838 unsigned int* new_buf_offs,
839 unsigned int* orig_offs,
840 struct dest_info* send_info)
848 str* send_address_str;
850 struct socket_info* send_sock;
853 #define RCVCOMP_PARAM_ADD \
854 /* add ;comp=xxxx */ \
855 switch(msg->rcv.comp){ \
859 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
860 offset+=COMP_PARAM_LEN; \
861 memcpy(new_buf+offset, SIGCOMP_NAME, \
863 offset+=SIGCOMP_NAME_LEN; \
866 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
867 offset+=COMP_PARAM_LEN; \
868 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
869 offset+=SERGZ_NAME_LEN; \
872 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
876 #define SENDCOMP_PARAM_ADD \
877 /* add ;comp=xxxx */ \
878 switch(send_info->comp){ \
882 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
883 offset+=COMP_PARAM_LEN; \
884 memcpy(new_buf+offset, SIGCOMP_NAME, \
886 offset+=SIGCOMP_NAME_LEN; \
889 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
890 offset+=COMP_PARAM_LEN; \
891 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
892 offset+=SERGZ_NAME_LEN; \
895 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
899 #define RCVCOMP_PARAM_ADD
900 #define SENDCOMP_PARAM_ADD
901 #endif /* USE_COMP */
903 #define SUBST_LUMP(subst_l) \
904 switch((subst_l)->u.subst){ \
906 if (msg->rcv.bind_address){ \
907 if (msg->rcv.bind_address->address.af!=AF_INET){\
908 new_buf[offset]='['; offset++; \
910 memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
911 msg->rcv.bind_address->address_str.len); \
912 offset+=msg->rcv.bind_address->address_str.len; \
913 if (msg->rcv.bind_address->address.af!=AF_INET){\
914 new_buf[offset]=']'; offset++; \
918 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
921 case SUBST_RCV_PORT: \
922 if (msg->rcv.bind_address){ \
923 memcpy(new_buf+offset, msg->rcv.bind_address->port_no_str.s, \
924 msg->rcv.bind_address->port_no_str.len); \
925 offset+=msg->rcv.bind_address->port_no_str.len; \
928 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
931 case SUBST_RCV_ALL: \
932 if (msg->rcv.bind_address){ \
934 if (msg->rcv.bind_address->address.af!=AF_INET){\
935 new_buf[offset]='['; offset++; \
937 memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
938 msg->rcv.bind_address->address_str.len); \
939 offset+=msg->rcv.bind_address->address_str.len; \
940 if (msg->rcv.bind_address->address.af!=AF_INET){\
941 new_buf[offset]=']'; offset++; \
944 if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
945 new_buf[offset]=':'; offset++; \
946 memcpy(new_buf+offset, \
947 msg->rcv.bind_address->port_no_str.s, \
948 msg->rcv.bind_address->port_no_str.len); \
949 offset+=msg->rcv.bind_address->port_no_str.len; \
951 switch(msg->rcv.bind_address->proto){ \
954 break; /* nothing to do, udp is default*/ \
956 memcpy(new_buf+offset, TRANSPORT_PARAM, \
957 TRANSPORT_PARAM_LEN); \
958 offset+=TRANSPORT_PARAM_LEN; \
959 memcpy(new_buf+offset, "tcp", 3); \
963 memcpy(new_buf+offset, TRANSPORT_PARAM, \
964 TRANSPORT_PARAM_LEN); \
965 offset+=TRANSPORT_PARAM_LEN; \
966 memcpy(new_buf+offset, "tls", 3); \
970 memcpy(new_buf+offset, TRANSPORT_PARAM, \
971 TRANSPORT_PARAM_LEN); \
972 offset+=TRANSPORT_PARAM_LEN; \
973 memcpy(new_buf+offset, "sctp", 4); \
977 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
978 msg->rcv.bind_address->proto); \
983 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
988 if ((send_sock->address.af!=AF_INET) && \
989 (send_address_str==&(send_sock->address_str))){\
990 new_buf[offset]='['; offset++; \
992 memcpy(new_buf+offset, send_address_str->s, \
993 send_address_str->len); \
994 offset+=send_address_str->len; \
995 if ((send_sock->address.af!=AF_INET) && \
996 (send_address_str==&(send_sock->address_str))){\
997 new_buf[offset]=']'; offset++; \
1001 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1002 " null send_sock\n"); \
1005 case SUBST_SND_PORT: \
1007 memcpy(new_buf+offset, send_port_str->s, \
1008 send_port_str->len); \
1009 offset+=send_port_str->len; \
1012 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1013 " null send_sock\n"); \
1016 case SUBST_SND_ALL: \
1019 if ((send_sock->address.af!=AF_INET) && \
1020 (send_address_str==&(send_sock->address_str))){\
1021 new_buf[offset]='['; offset++; \
1023 memcpy(new_buf+offset, send_address_str->s, \
1024 send_address_str->len); \
1025 offset+=send_address_str->len; \
1026 if ((send_sock->address.af!=AF_INET) && \
1027 (send_address_str==&(send_sock->address_str))){\
1028 new_buf[offset]=']'; offset++; \
1031 if ((send_sock->port_no!=SIP_PORT) || \
1032 (send_port_str!=&(send_sock->port_no_str))){ \
1033 new_buf[offset]=':'; offset++; \
1034 memcpy(new_buf+offset, send_port_str->s, \
1035 send_port_str->len); \
1036 offset+=send_port_str->len; \
1038 switch(send_sock->proto){ \
1041 break; /* nothing to do, udp is default*/ \
1043 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1044 TRANSPORT_PARAM_LEN); \
1045 offset+=TRANSPORT_PARAM_LEN; \
1046 memcpy(new_buf+offset, "tcp", 3); \
1050 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1051 TRANSPORT_PARAM_LEN); \
1052 offset+=TRANSPORT_PARAM_LEN; \
1053 memcpy(new_buf+offset, "tls", 3); \
1057 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1058 TRANSPORT_PARAM_LEN); \
1059 offset+=TRANSPORT_PARAM_LEN; \
1060 memcpy(new_buf+offset, "sctp", 4); \
1064 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1065 send_sock->proto); \
1067 SENDCOMP_PARAM_ADD \
1070 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1073 case SUBST_RCV_PROTO: \
1074 if (msg->rcv.bind_address){ \
1075 switch(msg->rcv.bind_address->proto){ \
1078 memcpy(new_buf+offset, "udp", 3); \
1082 memcpy(new_buf+offset, "tcp", 3); \
1086 memcpy(new_buf+offset, "tls", 3); \
1090 memcpy(new_buf+offset, "sctp", 4); \
1094 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1095 msg->rcv.bind_address->proto); \
1099 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1103 case SUBST_SND_PROTO: \
1105 switch(send_sock->proto){ \
1108 memcpy(new_buf+offset, "udp", 3); \
1112 memcpy(new_buf+offset, "tcp", 3); \
1116 memcpy(new_buf+offset, "tls", 3); \
1120 memcpy(new_buf+offset, "sctp", 4); \
1124 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1125 send_sock->proto); \
1129 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1134 LOG(L_CRIT, "BUG: process_lumps: unknown subst type %d\n", \
1135 (subst_l)->u.subst); \
1139 send_sock=send_info->send_sock;
1143 /* init send_address_str & send_port_str */
1144 if (msg->set_global_address.len)
1145 send_address_str=&(msg->set_global_address);
1147 send_address_str=&(send_sock->address_str);
1148 if (msg->set_global_port.len)
1149 send_port_str=&(msg->set_global_port);
1151 send_port_str=&(send_sock->port_no_str);
1155 offset=*new_buf_offs;
1156 s_offset=*orig_offs;
1158 for (t=lumps;t;t=t->next){
1161 case LUMP_ADD_SUBST:
1163 /* skip if this is an OPT lump and the condition is
1165 if ((t->op==LUMP_ADD_OPT) &&
1166 (!lump_check_opt(t->u.cond, msg, send_info)))
1168 /* just add it here! */
1169 /* process before */
1170 for(r=t->before;r;r=r->before){
1173 /*just add it here*/
1174 memcpy(new_buf+offset, r->u.value, r->len);
1177 case LUMP_ADD_SUBST:
1181 /* skip if this is an OPT lump and the condition is
1183 if (!lump_check_opt(r->u.cond, msg, send_info))
1187 /* only ADD allowed for before/after */
1188 LOG(L_CRIT, "BUG:process_lumps: "
1189 "invalid op for data lump (%x)\n", r->op);
1193 /* copy "main" part */
1196 memcpy(new_buf+offset, t->u.value, t->len);
1199 case LUMP_ADD_SUBST:
1203 /* do nothing, it's only a condition */
1206 /* should not ever get here */
1207 LOG(L_CRIT, "BUG: process_lumps: unhandled data lump "
1211 for(r=t->after;r;r=r->after){
1214 /*just add it here*/
1215 memcpy(new_buf+offset, r->u.value, r->len);
1218 case LUMP_ADD_SUBST:
1222 /* skip if this is an OPT lump and the condition is
1224 if (!lump_check_opt(r->u.cond, msg, send_info))
1228 /* only ADD allowed for before/after */
1229 LOG(L_CRIT, "BUG:process_lumps: "
1230 "invalid op for data lump (%x)\n", r->op);
1237 /* copy till offset */
1238 if (s_offset>t->u.offset){
1239 DBG("Warning: (%d) overlapped lumps offsets,"
1240 " ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
1241 /* this should've been fixed above (when computing len) */
1245 size=t->u.offset-s_offset;
1247 memcpy(new_buf+offset, orig+s_offset,size);
1251 /* process before */
1252 for(r=t->before;r;r=r->before){
1255 /*just add it here*/
1256 memcpy(new_buf+offset, r->u.value, r->len);
1259 case LUMP_ADD_SUBST:
1263 /* skip if this is an OPT lump and the condition is
1265 if (!lump_check_opt(r->u.cond, msg, send_info))
1266 goto skip_nop_before;
1269 /* only ADD allowed for before/after */
1270 LOG(L_CRIT, "BUG:process_lumps: "
1271 "invalid op for data lump (%x)\n",r->op);
1275 /* process main (del only) */
1276 if (t->op==LUMP_DEL){
1277 /* skip len bytes from orig msg */
1281 for(r=t->after;r;r=r->after){
1284 /*just add it here*/
1285 memcpy(new_buf+offset, r->u.value, r->len);
1288 case LUMP_ADD_SUBST:
1292 /* skip if this is an OPT lump and the condition is
1294 if (!lump_check_opt(r->u.cond, msg, send_info))
1295 goto skip_nop_after;
1298 /* only ADD allowed for before/after */
1299 LOG(L_CRIT, "BUG:process_lumps: "
1300 "invalid op for data lump (%x)\n", r->op);
1306 LOG(L_CRIT, "BUG: process_lumps: "
1307 "unknown op (%x)\n", t->op);
1310 *new_buf_offs=offset;
1311 *orig_offs=s_offset;
1312 #undef RCVCOMP_PARAM_ADD
1313 #undef SENDCOMP_PARAM_ADD
1318 * Adjust/insert Content-Length if necessary
1320 static inline int adjust_clen(struct sip_msg* msg, int body_delta, int proto)
1322 struct lump* anchor;
1324 int clen_len, body_only;
1326 /* Calculate message length difference caused by lumps modifying message
1327 * body, from this point on the message body must not be modified. Zero
1328 * value indicates that the body hasn't been modified
1335 /* check to see if we need to add clen */
1337 if (proto == PROTO_TCP
1339 || proto == PROTO_TLS
1342 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
1343 LOG(L_ERR, "adjust_clen: error parsing content-length\n");
1346 if (msg->content_length==0){
1347 /* not present, we need to add it */
1348 /* msg->unparsed should point just before the final crlf
1349 * - whole message was parsed by the above parse_headers
1350 * which did not find content-length */
1351 anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1352 HDR_CONTENTLENGTH_T);
1354 LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1363 if ((anchor==0) && body_delta){
1364 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
1365 LOG(L_ERR, "adjust_clen: Error parsing Content-Length\n");
1369 /* The body has been changed, try to find
1370 * existing Content-Length
1372 /* no need for Content-Length if it's and UDP packet and
1373 * it hasn't Content-Length already */
1374 if ((msg->content_length==0)){
1375 /* content-length doesn't exist, append it */
1376 /* msg->unparsed should point just before the final crlf
1377 * - whole message was parsed by the above parse_headers
1378 * which did not find content-length */
1379 if (proto!=PROTO_UDP){
1380 anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1381 HDR_CONTENTLENGTH_T);
1383 LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1388 DBG("adjust_clen: UDP packet with no clen => not adding one \n");
1391 /* Content-Length has been found, remove it */
1392 anchor = del_lump( msg, msg->content_length->body.s - msg->buf,
1393 msg->content_length->body.len, HDR_CONTENTLENGTH_T);
1395 LOG(L_ERR, "adjust_clen: Can't remove original"
1396 " Content-Length\n");
1403 clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
1404 if (!clen_buf) goto error;
1405 if (insert_new_lump_after(anchor, clen_buf, clen_len,
1406 HDR_CONTENTLENGTH_T) == 0)
1412 if (clen_buf) pkg_free(clen_buf);
1418 char * build_req_buf_from_sip_req( struct sip_msg* msg,
1419 unsigned int *returned_len,
1420 struct dest_info* send_info)
1422 unsigned int len, new_len, received_len, rport_len, uri_len, via_len, body_delta;
1428 unsigned int offset, s_offset, size;
1429 struct lump* anchor;
1430 struct lump* via_insert_param;
1444 /* Calculate message body difference and adjust
1447 body_delta = lumps_len(msg, msg->body_lumps, send_info);
1448 if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
1449 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1450 " Content-Length\n");
1454 /* create a the via header */
1455 branch.s=msg->add_to_branch_s;
1456 branch.len=msg->add_to_branch_len;
1458 line_buf = create_via_hf( &via_len, msg, send_info, &branch);
1460 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: no via received!\n");
1463 /* check if received needs to be added */
1464 if ( received_test(msg) ) {
1465 if ((received_buf=received_builder(msg,&received_len))==0){
1466 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1467 " received_builder failed\n");
1468 goto error01; /* free also line_buf */
1472 /* check if rport needs to be updated:
1473 * - if FL_FORCE_RPORT is set add it (and del. any previous version)
1474 * - if via already contains an rport add it and overwrite the previous
1475 * rport value if present (if you don't want to overwrite the previous
1476 * version remove the comments) */
1477 if ((msg->msg_flags&FL_FORCE_RPORT)||
1478 (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1479 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1480 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1481 " rport_builder failed\n");
1482 goto error01; /* free everything */
1486 /* add via header to the list */
1487 /* try to add it before msg. 1st via */
1488 /* add first via, as an anchor for second via*/
1489 anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
1490 if (anchor==0) goto error01;
1491 if (insert_new_lump_before(anchor, line_buf, via_len, HDR_VIA_T)==0)
1493 /* find out where the offset of the first parameter that should be added
1494 * (after host:port), needed by add receive & maybe rport */
1495 if (msg->via1->params.s){
1496 size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
1499 size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
1500 if (msg->via1->port!=0){
1501 /*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
1502 size += msg->via1->port_str.len + 1; /* +1 for ':'*/
1505 /* no longer necessary, now hots.s contains [] */
1507 if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
1511 /* if received needs to be added, add anchor after host and add it, or
1512 * overwrite the previous one if already present */
1514 if (msg->via1->received){ /* received already present => overwrite it*/
1515 via_insert_param=del_lump(msg,
1516 msg->via1->received->start-buf-1, /*;*/
1517 msg->via1->received->size+1, /*;*/ HDR_VIA_T);
1518 }else if (via_insert_param==0){ /* receive not present, ok */
1519 via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1522 if (via_insert_param==0) goto error02; /* free received_buf */
1523 if (insert_new_lump_after(via_insert_param, received_buf, received_len,
1524 HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
1526 /* if rport needs to be updated, delete it if present and add it's value */
1528 if (msg->via1->rport){ /* rport already present */
1529 via_insert_param=del_lump(msg,
1530 msg->via1->rport->start-buf-1, /*';'*/
1531 msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
1532 }else if (via_insert_param==0){ /*force rport, no rport present */
1533 /* no rport, add it */
1534 via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1537 if (via_insert_param==0) goto error03; /* free rport_buf */
1538 if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
1540 goto error03; /* free rport_buf */
1544 /* compute new msg len and fix overlapping zones*/
1545 new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info);
1547 LOG(L_ERR, "DEBUG: new_len(%d)=len(%d)+lumps_len\n", new_len, len);
1550 if (msg->new_uri.s){
1551 uri_len=msg->new_uri.len;
1552 new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
1554 new_buf=(char*)pkg_malloc(new_len+1);
1556 ser_error=E_OUT_OF_MEM;
1557 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
1562 if (msg->new_uri.s){
1563 /* copy message up to uri */
1564 size=msg->first_line.u.request.uri.s-buf;
1565 memcpy(new_buf, buf, size);
1569 memcpy(new_buf+offset, msg->new_uri.s, uri_len);
1571 s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
1574 /* copy msg adding/removing lumps */
1575 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
1576 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info);
1577 /* copy the rest of the message */
1578 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
1582 if (new_buf[new_len-1]==0) {
1583 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: 0 in the end\n");
1588 *returned_len=new_len;
1592 if (line_buf) pkg_free(line_buf);
1594 if (received_buf) pkg_free(received_buf);
1596 if (rport_buf) pkg_free(rport_buf);
1604 char * build_res_buf_from_sip_res( struct sip_msg* msg,
1605 unsigned int *returned_len)
1607 unsigned int new_len, via_len, body_delta;
1609 unsigned offset, s_offset, via_offset;
1616 /* we must remove the first via */
1617 if (msg->via1->next) {
1618 via_len=msg->via1->bsize;
1619 via_offset=msg->h_via1->body.s-buf;
1621 via_len=msg->h_via1->len;
1622 via_offset=msg->h_via1->name.s-buf;
1625 /* Calculate message body difference and adjust
1628 body_delta = lumps_len(msg, msg->body_lumps, 0);
1629 if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
1631 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1632 " Content-Length\n");
1636 /* remove the first via*/
1637 if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
1638 LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
1643 new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
1644 know the send sock */
1646 DBG(" old size: %d, new size: %d\n", len, new_len);
1647 new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
1650 LOG(L_ERR, "ERROR: build_res_buf_from_sip_res: out of mem\n");
1653 new_buf[new_len]=0; /* debug: print the message */
1655 /*FIXME: no send sock*/
1656 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0);/*FIXME:*/
1657 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0);
1658 /* copy the rest of the message */
1659 memcpy(new_buf+offset,
1663 DBG("build_res_from_sip_res: copied size: orig:%d, new: %d, rest: %d"
1664 " msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
1666 *returned_len=new_len;
1674 char * build_res_buf_from_sip_req( unsigned int code, char *text ,str *new_tag,
1675 struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
1678 unsigned int len,foo;
1679 struct hdr_field *hdr;
1680 struct lump_rpl *lump;
1681 struct lump_rpl *body;
1684 unsigned int received_len;
1686 unsigned int rport_len;
1688 unsigned int warning_len;
1689 char* content_len_buf;
1690 unsigned int content_len_len;
1691 unsigned int text_len;
1698 received_buf=rport_buf=warning_buf=content_len_buf=0;
1699 received_len=rport_len=warning_len=content_len_len=0;
1701 text_len=strlen(text);
1703 to_tag.s=0; /* fixes gcc 4.0 warning */
1706 /* force parsing all headers -- we want to return all
1707 Via's in the reply and they may be scattered down to the
1708 end of header (non-block Vias are a really poor property
1710 if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
1711 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1712 "alas, parse_headers failed\n");
1716 /*computes the length of the new response buffer*/
1719 /* check if received needs to be added */
1720 if (received_test(msg)) {
1721 if ((received_buf=received_builder(msg,&received_len))==0) {
1722 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1723 "alas, received_builder failed\n");
1727 /* check if rport needs to be updated */
1728 if ( (msg->msg_flags&FL_FORCE_RPORT)||
1729 (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1730 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1731 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"
1732 " rport_builder failed\n");
1733 goto error01; /* free everything */
1735 if (msg->via1->rport)
1736 len -= msg->via1->rport->size+1; /* include ';' */
1740 len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
1741 text_len + CRLF_LEN/*new line*/;
1742 /*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
1743 for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
1744 switch (hdr->type) {
1746 if (new_tag && new_tag->len) {
1747 to_tag=get_to(msg)->tag_value;
1749 len+=new_tag->len-to_tag.len;
1751 len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
1756 /* we always add CRLF to via*/
1757 len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
1758 if (hdr==msg->h_via1) len += received_len+rport_len;
1760 case HDR_RECORDROUTE_T:
1761 /* RR only for 1xx and 2xx replies */
1762 if (code<180 || code>=300)
1767 /* we keep the original termination for these headers*/
1771 /* do nothing, we are interested only in the above headers */
1776 for(lump=msg->reply_lump;lump;lump=lump->next) {
1777 len += lump->text.len;
1778 if (lump->flags&LUMP_RPL_BODY)
1782 if (server_signature)
1783 len += SERVER_HDR_LEN + CRLF_LEN;
1786 warning_buf = warning_builder(msg,&warning_len);
1787 if (warning_buf) len += warning_len + CRLF_LEN;
1788 else LOG(L_WARN, "WARNING: warning skipped -- too big\n");
1790 /* content length hdr */
1792 content_len_buf = int2str(body->text.len, (int*)&content_len_len);
1793 len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
1795 len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
1797 /* end of message */
1798 len += CRLF_LEN; /*new line*/
1801 buf = (char*) pkg_malloc( len+1 );
1804 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: out of memory "
1805 " ; needs %d\n",len);
1809 /* filling the buffer*/
1812 memcpy( p , msg->first_line.u.request.version.s ,
1813 msg->first_line.u.request.version.len);
1814 p += msg->first_line.u.request.version.len;
1817 for ( i=2 , foo = code ; i>=0 ; i-- , foo=foo/10 )
1818 *(p+i) = '0' + foo - ( foo/10 )*10;
1821 memcpy( p , text , text_len );
1823 memcpy( p, CRLF, CRLF_LEN );
1826 for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
1830 if (hdr==msg->h_via1){
1832 if (msg->via1->rport){ /* delete the old one */
1833 /* copy until rport */
1834 append_str_trans( p, hdr->name.s ,
1835 msg->via1->rport->start-hdr->name.s-1,msg);
1836 /* copy new rport */
1837 append_str(p, rport_buf, rport_len);
1838 /* copy the rest of the via */
1839 append_str_trans(p, msg->via1->rport->start+
1840 msg->via1->rport->size,
1841 hdr->body.s+hdr->body.len-
1842 msg->via1->rport->start-
1843 msg->via1->rport->size, msg);
1844 }else{ /* just append the new one */
1845 /* normal whole via copy */
1846 append_str_trans( p, hdr->name.s ,
1847 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1848 append_str(p, rport_buf, rport_len);
1851 /* normal whole via copy */
1852 append_str_trans( p, hdr->name.s ,
1853 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1856 append_str( p, received_buf, received_len);
1858 /* normal whole via copy */
1859 append_str_trans( p, hdr->name.s,
1860 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1862 append_str( p, CRLF,CRLF_LEN);
1864 case HDR_RECORDROUTE_T:
1865 /* RR only for 1xx and 2xx replies */
1866 if (code<180 || code>=300) break;
1867 append_str(p, hdr->name.s, hdr->len);
1870 if (new_tag && new_tag->len){
1871 if (to_tag.s ) { /* replacement */
1873 append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
1874 /* to tag replacement */
1875 bmark->to_tag_val.s=p;
1876 bmark->to_tag_val.len=new_tag->len;
1877 append_str( p, new_tag->s,new_tag->len);
1878 /* the rest after to-tag */
1879 append_str( p, to_tag.s+to_tag.len,
1880 hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
1881 }else{ /* adding a new to-tag */
1882 after_body=hdr->body.s+hdr->body.len;
1883 append_str( p, hdr->name.s, after_body-hdr->name.s);
1884 append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
1885 bmark->to_tag_val.s=p;
1886 bmark->to_tag_val.len=new_tag->len;
1887 append_str( p, new_tag->s,new_tag->len);
1888 append_str( p, after_body,
1889 hdr->name.s+hdr->len-after_body);
1892 } /* no new to-tag -- proceed to 1:1 copying */
1893 totags=((struct to_body*)(hdr->parsed))->tag_value.s;
1895 bmark->to_tag_val.s=p+(totags-hdr->name.s);
1896 bmark->to_tag_val.len=
1897 ((struct to_body*)(hdr->parsed))->tag_value.len;
1900 bmark->to_tag_val.len = 0;
1901 bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
1907 append_str(p, hdr->name.s, hdr->len);
1910 /* do nothing, we are interested only in the above headers */
1915 for(lump=msg->reply_lump;lump;lump=lump->next)
1916 if (lump->flags&LUMP_RPL_HDR){
1917 memcpy(p,lump->text.s,lump->text.len);
1918 p += lump->text.len;
1921 if (server_signature) {
1922 memcpy( p, SERVER_HDR , SERVER_HDR_LEN );
1924 memcpy( p, CRLF, CRLF_LEN );
1927 /* content_length hdr */
1928 if (content_len_len) {
1929 append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
1930 append_str( p, content_len_buf, content_len_len );
1931 append_str( p, CRLF, CRLF_LEN );
1933 append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
1935 /* warning header */
1937 memcpy( p, warning_buf, warning_len);
1939 memcpy( p, CRLF, CRLF_LEN);
1943 memcpy( p, CRLF, CRLF_LEN );
1947 memcpy ( p, body->text.s, body->text.len );
1952 LOG(L_CRIT,"BUG:build_res_from_sip_req: diff len=%d p-buf=%d\n",
1956 *returned_len = len;
1957 /* in req2reply, received_buf is not introduced to lumps and
1958 needs to be deleted here
1960 if (received_buf) pkg_free(received_buf);
1961 if (rport_buf) pkg_free(rport_buf);
1965 if (received_buf) pkg_free(received_buf);
1966 if (rport_buf) pkg_free(rport_buf);
1974 /* return number of chars printed or 0 if space exceeded;
1975 assumes buffer size of at least MAX_BRANCH_PARAM_LEN
1977 int branch_builder( unsigned int hash_index,
1978 /* only either parameter useful */
1979 unsigned int label, char * char_v,
1981 char *branch_str, int *len )
1987 /* hash id provided ... start with it */
1988 size=MAX_BRANCH_PARAM_LEN;
1992 memcpy(begin, MCOOKIE, MCOOKIE_LEN );
1993 size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
1995 if (int2reverse_hex( &begin, &size, hash_index)==-1)
1999 *begin=BRANCH_SEPARATOR;
2003 /* string with request's characteristic value ... use it ... */
2005 if (memcpy(begin,char_v,MD5_LEN)) {
2006 begin+=MD5_LEN; size-=MD5_LEN;
2008 } else { /* ... use the "label" value otherwise */
2009 if (int2reverse_hex( &begin, &size, label )==-1)
2014 *begin=BRANCH_SEPARATOR;
2018 if (int2reverse_hex( &begin, &size, branch)==-1)
2021 *len=MAX_BRANCH_PARAM_LEN-size;
2028 /* uses only the send_info->send_socket, send_info->proto and
2029 * send_info->comp (so that a send_info used for sending can be passed
2030 * to this function w/o changes and the correct via will be built) */
2031 char* via_builder( unsigned int *len,
2032 struct dest_info* send_info /* where to send the reply */,
2033 str* branch, str* extra_params, struct hostport* hp)
2035 unsigned int via_len, extra_len;
2039 str* address_str; /* address displayed in via */
2040 str* port_str; /* port no displayed in via */
2041 struct socket_info* send_sock;
2042 int comp_len, comp_name_len;
2045 send_sock=send_info->send_sock;
2046 /* use pre-set address in via or the outbound socket one */
2047 if ( hp && hp->host->len)
2048 address_str=hp->host;
2050 address_str=&(send_sock->address_str);
2051 if (hp && hp->port->len)
2054 port_str=&(send_sock->port_no_str);
2056 comp_len=comp_name_len=0;
2059 switch(send_info->comp){
2063 comp_len=COMP_PARAM_LEN;
2064 comp_name_len=SIGCOMP_NAME_LEN;
2065 comp_name=SIGCOMP_NAME;
2068 comp_len=COMP_PARAM_LEN;
2069 comp_name_len=SERGZ_NAME_LEN;
2070 comp_name=SERGZ_NAME;
2073 LOG(L_CRIT, "BUG: via_builder: unknown comp %d\n",
2075 /* continue, we'll just ignore comp */
2077 #endif /* USE_COMP */
2079 via_prefix_len=MY_VIA_LEN+(send_info->proto==PROTO_SCTP);
2080 max_len=via_prefix_len +address_str->len /* space in MY_VIA */
2081 +2 /* just in case it is a v6 address ... [ ] */
2082 +1 /*':'*/+port_str->len
2083 +(branch?(MY_BRANCH_LEN+branch->len):0)
2084 +(extra_params?extra_params->len:0)
2085 +comp_len+comp_name_len
2087 line_buf=pkg_malloc( max_len );
2089 ser_error=E_OUT_OF_MEM;
2090 LOG(L_ERR, "ERROR: via_builder: out of memory\n");
2096 via_len=via_prefix_len+address_str->len; /*space included in MY_VIA*/
2098 memcpy(line_buf, MY_VIA, MY_VIA_LEN);
2099 if (send_info->proto==PROTO_UDP){
2101 }else if (send_info->proto==PROTO_TCP){
2102 memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
2103 }else if (send_info->proto==PROTO_TLS){
2104 memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
2105 }else if (send_info->proto==PROTO_SCTP){
2106 memcpy(line_buf+MY_VIA_LEN-4, "SCTP ", 5);
2108 LOG(L_CRIT, "BUG: via_builder: unknown proto %d\n", send_info->proto);
2112 /* add [] only if ipv6 and outbound socket address is used;
2113 * if using pre-set no check is made */
2114 if ((send_sock->address.af==AF_INET6) &&
2115 (address_str==&(send_sock->address_str))) {
2116 line_buf[via_prefix_len]='[';
2117 line_buf[via_prefix_len+1+address_str->len]=']';
2119 via_len+=2; /* [ ]*/
2122 memcpy(line_buf+via_prefix_len+extra_len, address_str->s,
2124 if ((send_sock->port_no!=SIP_PORT) ||
2125 (port_str!=&send_sock->port_no_str)){
2126 line_buf[via_len]=':'; via_len++;
2127 memcpy(line_buf+via_len, port_str->s, port_str->len);
2128 via_len+=port_str->len;
2131 /* branch parameter */
2133 memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
2134 via_len+=MY_BRANCH_LEN;
2135 memcpy(line_buf+via_len, branch->s, branch->len );
2136 via_len+=branch->len;
2140 memcpy(line_buf+via_len, extra_params->s, extra_params->len);
2141 via_len+=extra_params->len;
2146 memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
2147 via_len+=COMP_PARAM_LEN;
2148 memcpy(line_buf+via_len, comp_name, comp_name_len);
2149 via_len+=comp_name_len;
2153 memcpy(line_buf+via_len, CRLF, CRLF_LEN);
2155 line_buf[via_len]=0; /* null terminate the string*/
2161 /* creates a via header honoring the protocol of the incomming socket
2162 * msg is an optional parameter */
2163 char* create_via_hf( unsigned int *len,
2164 struct sip_msg *msg,
2165 struct dest_info* send_info /* where to send the reply */,
2173 unsigned int id_len;
2186 && ((msg->rcv.proto==PROTO_TCP)
2188 || (msg->rcv.proto==PROTO_TLS)
2191 if ((id_buf=id_builder(msg, &id_len))==0){
2192 LOG(L_ERR, "ERROR: create_via_hf:"
2193 " id_builder failed\n");
2194 return 0; /* we don't need to free anything,
2195 nothing alloc'ed yet*/
2197 DBG("create_via_hf: id added: <%.*s>, rcv proto=%d\n",
2198 (int)id_len, id_buf, msg->rcv.proto);
2199 extra_params.s=id_buf;
2200 extra_params.len=id_len;
2204 set_hostport(&hp, msg);
2205 via = via_builder( len, send_info, branch,
2206 extra_params.len?&extra_params:0, &hp);
2209 /* we do not need id_buf any more, the id is already in the new via header */
2210 if (id_buf) pkg_free(id_buf);
2215 /* builds a char* buffer from message headers without body
2216 * first line is excluded in case of skip_first_line=1
2217 * error is set -1 if the memory allocation failes
2219 char * build_only_headers( struct sip_msg* msg, int skip_first_line,
2220 unsigned int *returned_len,
2222 struct dest_info* send_info)
2224 char *buf, *new_buf;
2225 unsigned int offset, s_offset, len, new_len;
2229 if (skip_first_line)
2230 s_offset = msg->headers->name.s - buf;
2234 /* original length without body, and without final \r\n */
2235 len = msg->unparsed - buf;
2236 /* new msg length */
2237 new_len = len - /* original length */
2238 s_offset + /* skipped first line */
2239 lumps_len(msg, msg->add_rm, send_info); /* lumps */
2246 new_buf = (char *)pkg_malloc(new_len+1);
2248 LOG(L_ERR, "ERROR: build_only_headers: Not enough memory\n");
2255 /* copy message lumps */
2256 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2257 /* copy the rest of the message without body */
2258 if (len > s_offset) {
2259 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2260 offset += (len-s_offset);
2262 new_buf[offset] = 0;
2264 *returned_len = offset;
2268 /* builds a char* buffer from message body
2269 * error is set -1 if the memory allocation failes
2271 char * build_body( struct sip_msg* msg,
2272 unsigned int *returned_len,
2274 struct dest_info* send_info)
2276 char *buf, *new_buf, *body;
2277 unsigned int offset, s_offset, len, new_len;
2280 body = get_body(msg);
2282 if (!body || (body[0] == 0)) {
2287 s_offset = body - buf;
2289 /* original length of msg with body */
2291 /* new body length */
2292 new_len = len - /* original length */
2293 s_offset + /* msg without body */
2294 lumps_len(msg, msg->body_lumps, send_info); /* lumps */
2296 new_buf = (char *)pkg_malloc(new_len+1);
2298 LOG(L_ERR, "ERROR: build_body: Not enough memory\n");
2305 /* copy body lumps */
2306 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2307 /* copy the rest of the message without body */
2308 if (len > s_offset) {
2309 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2310 offset += (len-s_offset);
2312 new_buf[offset] = 0;
2314 *returned_len = offset;
2318 /* builds a char* buffer from SIP message including body
2319 * The function adjusts the Content-Length HF according
2320 * to body lumps in case of touch_clen=1.
2322 char * build_all( struct sip_msg* msg, int touch_clen,
2323 unsigned int *returned_len,
2325 struct dest_info* send_info)
2327 char *buf, *new_buf;
2328 unsigned int offset, s_offset, len, new_len;
2329 unsigned int body_delta;
2332 /* Calculate message body difference */
2333 body_delta = lumps_len(msg, msg->body_lumps, send_info);
2335 /* adjust Content-Length */
2336 if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
2337 LOG(L_ERR, "ERROR: build_all: Error while adjusting"
2338 " Content-Length\n");
2345 /* original msg length */
2347 /* new msg length */
2348 new_len = len + /* original length */
2349 lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
2350 body_delta; /* body lumps */
2357 new_buf = (char *)pkg_malloc(new_len+1);
2359 LOG(L_ERR, "ERROR: build_all: Not enough memory\n");
2364 offset = s_offset = 0;
2366 /* copy message lumps */
2367 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2368 /* copy body lumps */
2369 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2370 /* copy the rest of the message */
2371 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2372 offset += (len-s_offset);
2373 new_buf[offset] = 0;
2375 *returned_len = offset;