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
64 /* Via special params:
66 * - if the address in via is different from the src_ip or an existing
67 * received=something is found, received=src_ip is added (and any preexisting
68 * received is deleted). received is added as the first via parameter if no
69 * receive is previously present or over the old receive.
70 * - if the original via contains rport / rport=something or msg->msg_flags
71 * FL_FORCE_RPORT is set (e.g. script force_rport() cmd) rport=src_port
72 * is added (over previous rport / as first via param or after received
73 * if no received was present and received is added too)
75 * (see also sl_send_reply)
76 * - rport and received are added in mostly the same way as for requests, but
77 * in the reverse order (first rport and then received). See also
79 * - if reply_to_via is set (default off) the local reply will be sent to
80 * the address in via (received is ignored since it was not set by us). The
81 * destination port is either the message source port if via contains rport
82 * or the FL_FORCE_RPORT flag is set or the port from the via. If either
83 * port or rport are present a normal dns lookup (instead of a srv lookup)
84 * is performed on the address. If no port is present and a srv lookup is
85 * performed the port is taken from the srv lookup. If the srv lookup failed
86 * or it was not performed, the port is set to the default sip port (5060).
87 * - if reply_to_via is off (default) the local reply is sent to the message
88 * source ip address. The destination port is set to the source port if
89 * rport is present or FL_FORCE_RPORT flag is set, to the via port or to
90 * the default sip port (5060) if neither rport or via port are present.
92 * - if received is present the message is sent to the received address else
93 * if no port is present (neither a normal via port or rport) a dns srv
94 * lookup is performed on the host part and the reply is sent to the
95 * resulting ip. If a port is present or the host part is an ip address
96 * the dns lookup will be a "normal" one (A or AAAA).
97 * - if rport is present, it's value will be used as the destination port
98 * (and this will also disable srv lookups)
99 * - if no port is present the destination port will be taken from the srv
100 * lookup. If the srv lookup fails or is not performed (e.g. ip address
101 * in host) the destination port will be set to the default sip port (5060).
104 * - when locally replying to a message, rport and received will be appended to
105 * the via header parameters (for forwarded requests they are inserted at the
107 * - a locally generated reply might get two received via parameters if a
108 * received is already present in the original message (this should not
109 * happen though, but ...)
114 #include <sys/types.h>
115 #include <sys/socket.h>
121 #include "comp_defs.h"
122 #include "msg_translator.h"
128 #include "md5utils.h"
129 #include "data_lump.h"
130 #include "data_lump_rpl.h"
137 #define append_str(_dest,_src,_len) \
139 memcpy( (_dest) , (_src) , (_len) );\
143 #define append_str_trans(_dest,_src,_len,_msg) \
144 append_str( (_dest), (_src), (_len) );
146 extern char version[];
147 extern int version_len;
155 /* checks if ip is in host(name) and ?host(ip)=name?
156 * ip must be in network byte order!
157 * resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
158 * return 0 if equal */
159 static int check_via_address(struct ip_addr* ip, str *name,
160 unsigned short port, int resolver)
167 /* maybe we are lucky and name it's an ip */
170 DBG("check_via_address(%s, %.*s, %d)\n",
171 s, name->len, name->s, resolver);
175 /* check if name->s is an ipv6 address or an ipv6 address ref. */
176 if ((ip->af==AF_INET6) &&
177 ( ((len==name->len)&&(strncasecmp(name->s, s, name->len)==0))
179 ((len==(name->len-2))&&(name->s[0]=='[')&&
180 (name->s[name->len-1]==']')&&
181 (strncasecmp(name->s+1, s, len)==0))
188 if (strncmp(name->s, s, name->len)==0)
191 LOG(L_CRIT, "check_via_address: BUG: could not convert ip address\n");
195 if (port==0) port=SIP_PORT;
196 if (resolver&DO_DNS){
197 DBG("check_via_address: doing dns lookup\n");
198 /* try all names ips */
199 he=sip_resolvehost(name, &port, 0); /* don't use naptr */
200 if (he && ip->af==he->h_addrtype){
201 for(i=0;he && he->h_addr_list[i];i++){
202 if ( memcmp(&he->h_addr_list[i], ip->u.addr, ip->len)==0)
207 if (resolver&DO_REV_DNS){
208 DBG("check_via_address: doing rev. dns lookup\n");
209 /* try reverse dns */
210 he=rev_resolvehost(ip);
211 if (he && (strncmp(he->h_name, name->s, name->len)==0))
213 for (i=0; he && he->h_aliases[i];i++){
214 if (strncmp(he->h_aliases[i],name->s, name->len)==0)
222 /* check if IP address in Via != source IP address of signaling */
223 int received_test( struct sip_msg *msg )
227 rcvd=msg->via1->received
228 || check_via_address(&msg->rcv.src_ip, &msg->via1->host,
229 msg->via1->port, received_dns);
234 static char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
236 static char buf[MAX_WARNING_LEN];
242 #define str_print(string, string_len) \
245 if ((clen+l)>MAX_WARNING_LEN) \
246 goto error_overflow; \
247 memcpy(buf+clen, (string), l); \
251 #define str_lenpair_print(string, string_len, string2, string2_len) \
253 str_print(string, string_len); \
254 str_print(string2, string2_len);\
257 #define str_pair_print( string, string2, string2_len) \
258 str_lenpair_print((string), strlen((string)), (string2), (string2_len))
260 #define str_int_print(string, intval)\
262 t=int2str((intval), &print_len); \
263 str_pair_print(string, t, print_len);\
266 #define str_ipaddr_print(string, ipaddr_val)\
268 t=ip_addr2a((ipaddr_val)); \
269 print_len=strlen(t); \
270 str_pair_print(string, t, print_len);\
274 str_lenpair_print(WARNING, WARNING_LEN,
275 msg->rcv.bind_address->name.s,
276 msg->rcv.bind_address->name.len);
277 str_lenpair_print(":", 1, msg->rcv.bind_address->port_no_str.s,
278 msg->rcv.bind_address->port_no_str.len);
279 str_print(WARNING_PHRASE, WARNING_PHRASE_LEN);
285 foo=&(msg->first_line.u.request.uri);
287 str_int_print(" pid=", my_pid());
289 str_ipaddr_print(" req_src_ip=", &msg->rcv.src_ip);
290 str_int_print(" req_src_port=", msg->rcv.src_port);
291 str_pair_print(" in_uri=", msg->first_line.u.request.uri.s,
292 msg->first_line.u.request.uri.len);
293 str_pair_print(" out_uri=", foo->s, foo->len);
294 str_pair_print(" via_cnt",
295 (msg->parsed_flag & HDR_EOH_F)==HDR_EOH_F ? "=" : ">", 1);
296 str_int_print("=", via_cnt);
297 if (clen<MAX_WARNING_LEN){ buf[clen]='"'; clen++; }
298 else goto error_overflow;
304 LOG(L_NOTICE, "NOTICE: warning_builder: buffer size exceeded (probably too long URI)\n");
312 char* received_builder(struct sip_msg *msg, unsigned int *received_len)
316 struct ip_addr *source_ip;
320 source_ip=&msg->rcv.src_ip;
322 buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
324 ser_error=E_OUT_OF_MEM;
325 LOG(L_ERR, "ERROR: received_builder: out of memory\n");
328 memcpy(buf, RECEIVED, RECEIVED_LEN);
329 if ( (tmp=ip_addr2a(source_ip))==0)
332 len=RECEIVED_LEN+tmp_len;
334 memcpy(buf+RECEIVED_LEN, tmp, tmp_len);
335 buf[len]=0; /*null terminate it */
343 char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
351 tmp=int2str(msg->rcv.src_port, &tmp_len);
352 len=RPORT_LEN+tmp_len;
353 buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
355 ser_error=E_OUT_OF_MEM;
356 LOG(L_ERR, "ERROR: rport_builder: out of memory\n");
359 memcpy(buf, RPORT, RPORT_LEN);
360 memcpy(buf+RPORT_LEN, tmp, tmp_len);
361 buf[len]=0; /*null terminate it*/
369 char* id_builder(struct sip_msg* msg, unsigned int *id_len)
373 char revhex[sizeof(int)*2];
379 if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
380 LOG(L_CRIT, "BUG: id_builder: not enough space for id\n");
383 value_len=p-&revhex[0];
384 len=ID_PARAM_LEN+value_len;
385 buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
387 ser_error=E_OUT_OF_MEM;
388 LOG(L_ERR, "ERROR: rport_builder: out of memory\n");
391 memcpy(buf, ID_PARAM, ID_PARAM_LEN);
392 memcpy(buf+ID_PARAM_LEN, revhex, value_len);
393 buf[len]=0; /* null terminate it */
400 char* clen_builder( struct sip_msg* msg, int *clen_len, int diff,
414 LOG(L_ERR, "ERROR: clen_builder: no message body found"
418 value=msg->len-(int)(body-msg->buf)+diff;
419 value_s=int2str(value, &value_len);
420 DBG("clen_builder: content-length: %d (%s)\n", value, value_s);
426 len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
428 buf=pkg_malloc(sizeof(char)*(len+1));
430 ser_error=E_OUT_OF_MEM;
431 LOG(L_ERR, "ERROR: clen_builder: out of memory\n");
435 memcpy(buf, value_s, value_len);
438 memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
439 memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
440 memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
442 buf[len]=0; /* null terminate it */
449 /* checks if a lump opt condition
450 * returns 1 if cond is true, 0 if false */
451 static inline int lump_check_opt( enum lump_conditions cond,
453 struct dest_info* snd_i
460 #define get_ip_port_proto \
461 if ((snd_i==0) || (snd_i->send_sock==0)){ \
462 LOG(L_CRIT, "ERROR: lump_check_opt: null send socket\n"); \
463 return 1; /* we presume they are different :-) */ \
465 if (msg->rcv.bind_address){ \
466 ip=&msg->rcv.bind_address->address; \
467 port=msg->rcv.bind_address->port_no; \
468 proto=msg->rcv.bind_address->proto; \
470 ip=&msg->rcv.dst_ip; \
471 port=msg->rcv.dst_port; \
472 proto=msg->rcv.proto; \
480 case COND_IF_DIFF_REALMS:
482 /* faster tests first */
483 if ((port==snd_i->send_sock->port_no) &&
484 (proto==snd_i->send_sock->proto) &&
486 (msg->rcv.comp==snd_i->comp) &&
488 (ip_addr_cmp(ip, &snd_i->send_sock->address)))
491 case COND_IF_DIFF_AF:
493 if (ip->af!=snd_i->send_sock->address.af) return 1;
495 case COND_IF_DIFF_PROTO:
497 if (proto!=snd_i->send_sock->proto) return 1;
499 case COND_IF_DIFF_PORT:
501 if (port!=snd_i->send_sock->port_no) return 1;
503 case COND_IF_DIFF_IP:
505 if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
508 return (rand()>=RAND_MAX/2);
510 LOG(L_CRIT, "BUG: lump:w_check_opt: unknown lump condition %d\n",
513 return 0; /* false */
518 /* computes the "unpacked" len of a lump list,
519 code moved from build_req_from_req */
520 static inline int lumps_len(struct sip_msg* msg, struct lump* lumps,
521 struct dest_info* send_info)
527 str* send_address_str;
529 struct socket_info* send_sock;
533 #define RCVCOMP_LUMP_LEN \
535 switch(msg->rcv.comp){ \
539 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
542 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
545 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
549 #define SENDCOMP_LUMP_LEN \
551 switch(send_info->comp){ \
555 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
558 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
561 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
565 #define RCVCOMP_LUMP_LEN
566 #define SENDCOMP_LUMP_LEN
569 #define SUBST_LUMP_LEN(subst_l) \
570 switch((subst_l)->u.subst){ \
572 if (msg->rcv.bind_address){ \
573 new_len+=msg->rcv.bind_address->address_str.len; \
574 if (msg->rcv.bind_address->address.af!=AF_INET) \
578 LOG(L_CRIT, "FIXME: null bind_address\n"); \
581 case SUBST_RCV_PORT: \
582 if (msg->rcv.bind_address){ \
583 new_len+=msg->rcv.bind_address->port_no_str.len; \
586 LOG(L_CRIT, "FIXME: null bind_address\n"); \
589 case SUBST_RCV_PROTO: \
590 if (msg->rcv.bind_address){ \
591 switch(msg->rcv.bind_address->proto){ \
602 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
603 msg->rcv.bind_address->proto); \
607 LOG(L_CRIT, "FIXME: null bind_address\n"); \
610 case SUBST_RCV_ALL: \
611 if (msg->rcv.bind_address){ \
612 new_len+=msg->rcv.bind_address->address_str.len; \
613 if (msg->rcv.bind_address->address.af!=AF_INET) \
615 if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
617 new_len+=1+msg->rcv.bind_address->port_no_str.len; \
619 /*add;transport=xxx*/ \
620 switch(msg->rcv.bind_address->proto){ \
623 break; /* udp is the default */ \
626 new_len+=TRANSPORT_PARAM_LEN+3; \
629 new_len+=TRANSPORT_PARAM_LEN+4; \
632 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
633 msg->rcv.bind_address->proto); \
638 LOG(L_CRIT, "FIXME: null bind_address\n"); \
643 new_len+=send_address_str->len; \
644 if (send_sock->address.af!=AF_INET && \
645 send_address_str==&(send_sock->address_str)) \
648 LOG(L_CRIT, "FIXME: lumps_len called with" \
649 " null send_sock\n"); \
652 case SUBST_SND_PORT: \
654 new_len+=send_port_str->len; \
656 LOG(L_CRIT, "FIXME: lumps_len called with" \
657 " null send_sock\n"); \
660 case SUBST_SND_PROTO: \
662 switch(send_sock->proto){ \
673 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
677 LOG(L_CRIT, "FIXME: lumps_len called with" \
678 " null send_sock\n"); \
681 case SUBST_SND_ALL: \
683 new_len+=send_address_str->len; \
684 if ((send_sock->address.af!=AF_INET) && \
685 (send_address_str==&(send_sock->address_str))) \
687 if ((send_sock->port_no!=SIP_PORT) || \
688 (send_port_str!=&(send_sock->port_no_str))){ \
690 new_len+=1+send_port_str->len; \
692 /*add;transport=xxx*/ \
693 switch(send_sock->proto){ \
696 break; /* udp is the default */ \
699 new_len+=TRANSPORT_PARAM_LEN+3; \
702 new_len+=TRANSPORT_PARAM_LEN+4; \
705 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
711 LOG(L_CRIT, "FIXME: lumps_len called with" \
712 " null send_sock\n"); \
715 case SUBST_NOP: /* do nothing */ \
718 LOG(L_CRIT, "BUG: unknown subst type %d\n", \
719 (subst_l)->u.subst); \
723 send_sock=send_info->send_sock;
729 /* init send_address_str & send_port_str */
730 if (msg->set_global_address.len)
731 send_address_str=&(msg->set_global_address);
733 send_address_str=&(send_sock->address_str);
734 if (msg->set_global_port.len)
735 send_port_str=&(msg->set_global_port);
737 send_port_str=&(send_sock->port_no_str);
740 for(t=lumps;t;t=t->next){
741 /* skip if this is an OPT lump and the condition is not satisfied */
742 if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t->u.cond, msg, send_info))
744 for(r=t->before;r;r=r->before){
753 /* skip if this is an OPT lump and the condition is
755 if (!lump_check_opt(r->u.cond, msg, send_info))
759 /* only ADD allowed for before/after */
760 LOG(L_CRIT, "BUG: lumps_len: invalid op "
761 "for data lump (%x)\n", r->op);
773 /* we don't do anything here, it's only a condition for
777 /* fix overlapping deleted zones */
778 if (t->u.offset < s_offset){
780 if (t->len>s_offset-t->u.offset)
781 t->len-=s_offset-t->u.offset;
783 t->u.offset=s_offset;
785 s_offset=t->u.offset+t->len;
789 /* fix offset if overlapping on a deleted zone */
790 if (t->u.offset < s_offset){
791 t->u.offset=s_offset;
793 s_offset=t->u.offset;
797 LOG(L_CRIT,"BUG:lumps_len: invalid"
798 " op for data lump (%x)\n", r->op);
800 for (r=t->after;r;r=r->after){
809 /* skip if this is an OPT lump and the condition is
811 if (!lump_check_opt(r->u.cond, msg, send_info))
815 /* only ADD allowed for before/after */
816 LOG(L_CRIT, "BUG:lumps_len: invalid"
817 " op for data lump (%x)\n", r->op);
821 ; /* to make gcc 3.* happy */
824 #undef RCVCOMP_LUMP_LEN
825 #undef SENDCOMP_LUMP_LEN
830 /* another helper functions, adds/Removes the lump,
831 code moved form build_req_from_req */
833 static inline void process_lumps( struct sip_msg* msg,
836 unsigned int* new_buf_offs,
837 unsigned int* orig_offs,
838 struct dest_info* send_info)
846 str* send_address_str;
848 struct socket_info* send_sock;
851 #define RCVCOMP_PARAM_ADD \
852 /* add ;comp=xxxx */ \
853 switch(msg->rcv.comp){ \
857 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
858 offset+=COMP_PARAM_LEN; \
859 memcpy(new_buf+offset, SIGCOMP_NAME, \
861 offset+=SIGCOMP_NAME_LEN; \
864 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
865 offset+=COMP_PARAM_LEN; \
866 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
867 offset+=SERGZ_NAME_LEN; \
870 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
874 #define SENDCOMP_PARAM_ADD \
875 /* add ;comp=xxxx */ \
876 switch(send_info->comp){ \
880 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
881 offset+=COMP_PARAM_LEN; \
882 memcpy(new_buf+offset, SIGCOMP_NAME, \
884 offset+=SIGCOMP_NAME_LEN; \
887 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
888 offset+=COMP_PARAM_LEN; \
889 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
890 offset+=SERGZ_NAME_LEN; \
893 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
897 #define RCVCOMP_PARAM_ADD
898 #define SENDCOMP_PARAM_ADD
899 #endif /* USE_COMP */
901 #define SUBST_LUMP(subst_l) \
902 switch((subst_l)->u.subst){ \
904 if (msg->rcv.bind_address){ \
905 if (msg->rcv.bind_address->address.af!=AF_INET){\
906 new_buf[offset]='['; offset++; \
908 memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
909 msg->rcv.bind_address->address_str.len); \
910 offset+=msg->rcv.bind_address->address_str.len; \
911 if (msg->rcv.bind_address->address.af!=AF_INET){\
912 new_buf[offset]=']'; offset++; \
916 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
919 case SUBST_RCV_PORT: \
920 if (msg->rcv.bind_address){ \
921 memcpy(new_buf+offset, msg->rcv.bind_address->port_no_str.s, \
922 msg->rcv.bind_address->port_no_str.len); \
923 offset+=msg->rcv.bind_address->port_no_str.len; \
926 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
929 case SUBST_RCV_ALL: \
930 if (msg->rcv.bind_address){ \
932 if (msg->rcv.bind_address->address.af!=AF_INET){\
933 new_buf[offset]='['; offset++; \
935 memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
936 msg->rcv.bind_address->address_str.len); \
937 offset+=msg->rcv.bind_address->address_str.len; \
938 if (msg->rcv.bind_address->address.af!=AF_INET){\
939 new_buf[offset]=']'; offset++; \
942 if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
943 new_buf[offset]=':'; offset++; \
944 memcpy(new_buf+offset, \
945 msg->rcv.bind_address->port_no_str.s, \
946 msg->rcv.bind_address->port_no_str.len); \
947 offset+=msg->rcv.bind_address->port_no_str.len; \
949 switch(msg->rcv.bind_address->proto){ \
952 break; /* nothing to do, udp is default*/ \
954 memcpy(new_buf+offset, TRANSPORT_PARAM, \
955 TRANSPORT_PARAM_LEN); \
956 offset+=TRANSPORT_PARAM_LEN; \
957 memcpy(new_buf+offset, "tcp", 3); \
961 memcpy(new_buf+offset, TRANSPORT_PARAM, \
962 TRANSPORT_PARAM_LEN); \
963 offset+=TRANSPORT_PARAM_LEN; \
964 memcpy(new_buf+offset, "tls", 3); \
968 memcpy(new_buf+offset, TRANSPORT_PARAM, \
969 TRANSPORT_PARAM_LEN); \
970 offset+=TRANSPORT_PARAM_LEN; \
971 memcpy(new_buf+offset, "sctp", 4); \
975 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
976 msg->rcv.bind_address->proto); \
981 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
986 if ((send_sock->address.af!=AF_INET) && \
987 (send_address_str==&(send_sock->address_str))){\
988 new_buf[offset]='['; offset++; \
990 memcpy(new_buf+offset, send_address_str->s, \
991 send_address_str->len); \
992 offset+=send_address_str->len; \
993 if ((send_sock->address.af!=AF_INET) && \
994 (send_address_str==&(send_sock->address_str))){\
995 new_buf[offset]=']'; offset++; \
999 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1000 " null send_sock\n"); \
1003 case SUBST_SND_PORT: \
1005 memcpy(new_buf+offset, send_port_str->s, \
1006 send_port_str->len); \
1007 offset+=send_port_str->len; \
1010 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1011 " null send_sock\n"); \
1014 case SUBST_SND_ALL: \
1017 if ((send_sock->address.af!=AF_INET) && \
1018 (send_address_str==&(send_sock->address_str))){\
1019 new_buf[offset]='['; offset++; \
1021 memcpy(new_buf+offset, send_address_str->s, \
1022 send_address_str->len); \
1023 offset+=send_address_str->len; \
1024 if ((send_sock->address.af!=AF_INET) && \
1025 (send_address_str==&(send_sock->address_str))){\
1026 new_buf[offset]=']'; offset++; \
1029 if ((send_sock->port_no!=SIP_PORT) || \
1030 (send_port_str!=&(send_sock->port_no_str))){ \
1031 new_buf[offset]=':'; offset++; \
1032 memcpy(new_buf+offset, send_port_str->s, \
1033 send_port_str->len); \
1034 offset+=send_port_str->len; \
1036 switch(send_sock->proto){ \
1039 break; /* nothing to do, udp is default*/ \
1041 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1042 TRANSPORT_PARAM_LEN); \
1043 offset+=TRANSPORT_PARAM_LEN; \
1044 memcpy(new_buf+offset, "tcp", 3); \
1048 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1049 TRANSPORT_PARAM_LEN); \
1050 offset+=TRANSPORT_PARAM_LEN; \
1051 memcpy(new_buf+offset, "tls", 3); \
1055 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1056 TRANSPORT_PARAM_LEN); \
1057 offset+=TRANSPORT_PARAM_LEN; \
1058 memcpy(new_buf+offset, "sctp", 4); \
1062 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1063 send_sock->proto); \
1065 SENDCOMP_PARAM_ADD \
1068 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1071 case SUBST_RCV_PROTO: \
1072 if (msg->rcv.bind_address){ \
1073 switch(msg->rcv.bind_address->proto){ \
1076 memcpy(new_buf+offset, "udp", 3); \
1080 memcpy(new_buf+offset, "tcp", 3); \
1084 memcpy(new_buf+offset, "tls", 3); \
1088 memcpy(new_buf+offset, "sctp", 4); \
1092 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1093 msg->rcv.bind_address->proto); \
1097 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1101 case SUBST_SND_PROTO: \
1103 switch(send_sock->proto){ \
1106 memcpy(new_buf+offset, "udp", 3); \
1110 memcpy(new_buf+offset, "tcp", 3); \
1114 memcpy(new_buf+offset, "tls", 3); \
1118 memcpy(new_buf+offset, "sctp", 4); \
1122 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1123 send_sock->proto); \
1127 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1132 LOG(L_CRIT, "BUG: process_lumps: unknown subst type %d\n", \
1133 (subst_l)->u.subst); \
1137 send_sock=send_info->send_sock;
1141 /* init send_address_str & send_port_str */
1142 if (msg->set_global_address.len)
1143 send_address_str=&(msg->set_global_address);
1145 send_address_str=&(send_sock->address_str);
1146 if (msg->set_global_port.len)
1147 send_port_str=&(msg->set_global_port);
1149 send_port_str=&(send_sock->port_no_str);
1153 offset=*new_buf_offs;
1154 s_offset=*orig_offs;
1156 for (t=lumps;t;t=t->next){
1159 case LUMP_ADD_SUBST:
1161 /* skip if this is an OPT lump and the condition is
1163 if ((t->op==LUMP_ADD_OPT) &&
1164 (!lump_check_opt(t->u.cond, msg, send_info)))
1166 /* just add it here! */
1167 /* process before */
1168 for(r=t->before;r;r=r->before){
1171 /*just add it here*/
1172 memcpy(new_buf+offset, r->u.value, r->len);
1175 case LUMP_ADD_SUBST:
1179 /* skip if this is an OPT lump and the condition is
1181 if (!lump_check_opt(r->u.cond, msg, send_info))
1185 /* only ADD allowed for before/after */
1186 LOG(L_CRIT, "BUG:process_lumps: "
1187 "invalid op for data lump (%x)\n", r->op);
1191 /* copy "main" part */
1194 memcpy(new_buf+offset, t->u.value, t->len);
1197 case LUMP_ADD_SUBST:
1201 /* do nothing, it's only a condition */
1204 /* should not ever get here */
1205 LOG(L_CRIT, "BUG: process_lumps: unhandled data lump "
1209 for(r=t->after;r;r=r->after){
1212 /*just add it here*/
1213 memcpy(new_buf+offset, r->u.value, r->len);
1216 case LUMP_ADD_SUBST:
1220 /* skip if this is an OPT lump and the condition is
1222 if (!lump_check_opt(r->u.cond, msg, send_info))
1226 /* only ADD allowed for before/after */
1227 LOG(L_CRIT, "BUG:process_lumps: "
1228 "invalid op for data lump (%x)\n", r->op);
1235 /* copy till offset */
1236 if (s_offset>t->u.offset){
1237 DBG("Warning: (%d) overlapped lumps offsets,"
1238 " ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
1239 /* this should've been fixed above (when computing len) */
1243 size=t->u.offset-s_offset;
1245 memcpy(new_buf+offset, orig+s_offset,size);
1249 /* process before */
1250 for(r=t->before;r;r=r->before){
1253 /*just add it here*/
1254 memcpy(new_buf+offset, r->u.value, r->len);
1257 case LUMP_ADD_SUBST:
1261 /* skip if this is an OPT lump and the condition is
1263 if (!lump_check_opt(r->u.cond, msg, send_info))
1264 goto skip_nop_before;
1267 /* only ADD allowed for before/after */
1268 LOG(L_CRIT, "BUG:process_lumps: "
1269 "invalid op for data lump (%x)\n",r->op);
1273 /* process main (del only) */
1274 if (t->op==LUMP_DEL){
1275 /* skip len bytes from orig msg */
1279 for(r=t->after;r;r=r->after){
1282 /*just add it here*/
1283 memcpy(new_buf+offset, r->u.value, r->len);
1286 case LUMP_ADD_SUBST:
1290 /* skip if this is an OPT lump and the condition is
1292 if (!lump_check_opt(r->u.cond, msg, send_info))
1293 goto skip_nop_after;
1296 /* only ADD allowed for before/after */
1297 LOG(L_CRIT, "BUG:process_lumps: "
1298 "invalid op for data lump (%x)\n", r->op);
1304 LOG(L_CRIT, "BUG: process_lumps: "
1305 "unknown op (%x)\n", t->op);
1308 *new_buf_offs=offset;
1309 *orig_offs=s_offset;
1310 #undef RCVCOMP_PARAM_ADD
1311 #undef SENDCOMP_PARAM_ADD
1316 * Adjust/insert Content-Length if necessary
1318 static inline int adjust_clen(struct sip_msg* msg, int body_delta, int proto)
1320 struct lump* anchor;
1322 int clen_len, body_only;
1324 /* Calculate message length difference caused by lumps modifying message
1325 * body, from this point on the message body must not be modified. Zero
1326 * value indicates that the body hasn't been modified
1333 /* check to see if we need to add clen */
1335 if (proto == PROTO_TCP
1337 || proto == PROTO_TLS
1340 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
1341 LOG(L_ERR, "adjust_clen: error parsing content-length\n");
1344 if (msg->content_length==0){
1345 /* not present, we need to add it */
1346 /* msg->unparsed should point just before the final crlf
1347 * - whole message was parsed by the above parse_headers
1348 * which did not find content-length */
1349 anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1350 HDR_CONTENTLENGTH_T);
1352 LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1361 if ((anchor==0) && body_delta){
1362 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
1363 LOG(L_ERR, "adjust_clen: Error parsing Content-Length\n");
1367 /* The body has been changed, try to find
1368 * existing Content-Length
1370 /* no need for Content-Length if it's and UDP packet and
1371 * it hasn't Content-Length already */
1372 if ((msg->content_length==0)){
1373 /* content-length doesn't exist, append it */
1374 /* msg->unparsed should point just before the final crlf
1375 * - whole message was parsed by the above parse_headers
1376 * which did not find content-length */
1377 if (proto!=PROTO_UDP){
1378 anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1379 HDR_CONTENTLENGTH_T);
1381 LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1386 DBG("adjust_clen: UDP packet with no clen => not adding one \n");
1389 /* Content-Length has been found, remove it */
1390 anchor = del_lump( msg, msg->content_length->body.s - msg->buf,
1391 msg->content_length->body.len, HDR_CONTENTLENGTH_T);
1393 LOG(L_ERR, "adjust_clen: Can't remove original"
1394 " Content-Length\n");
1401 clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
1402 if (!clen_buf) goto error;
1403 if (insert_new_lump_after(anchor, clen_buf, clen_len,
1404 HDR_CONTENTLENGTH_T) == 0)
1410 if (clen_buf) pkg_free(clen_buf);
1416 char * build_req_buf_from_sip_req( struct sip_msg* msg,
1417 unsigned int *returned_len,
1418 struct dest_info* send_info)
1420 unsigned int len, new_len, received_len, rport_len, uri_len, via_len, body_delta;
1426 unsigned int offset, s_offset, size;
1427 struct lump* anchor;
1428 struct lump* via_insert_param;
1435 unsigned int id_len;
1456 if (msg->rcv.proto==PROTO_TCP
1458 || msg->rcv.proto==PROTO_TLS
1461 if ((id_buf=id_builder(msg, &id_len))==0){
1462 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1463 " id_builder failed\n");
1464 goto error00; /* we don't need to free anything,
1465 nothing alloc'ed yet*/
1467 DBG("build_req_from_req: id added: <%.*s>, rcv proto=%d\n",
1468 (int)id_len, id_buf, msg->rcv.proto);
1469 extra_params.s=id_buf;
1470 extra_params.len=id_len;
1473 /* Calculate message body difference and adjust
1476 body_delta = lumps_len(msg, msg->body_lumps, send_info);
1477 if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
1478 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1479 " Content-Length\n");
1483 branch.s=msg->add_to_branch_s;
1484 branch.len=msg->add_to_branch_len;
1485 set_hostport(&hp, msg);
1486 line_buf = via_builder( &via_len, send_info, &branch,
1487 extra_params.len?&extra_params:0, &hp);
1489 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: no via received!\n");
1492 /* check if received needs to be added */
1493 if ( received_test(msg) ) {
1494 if ((received_buf=received_builder(msg,&received_len))==0){
1495 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1496 " received_builder failed\n");
1497 goto error01; /* free also line_buf */
1501 /* check if rport needs to be updated:
1502 * - if FL_FORCE_RPORT is set add it (and del. any previous version)
1503 * - if via already contains an rport add it and overwrite the previous
1504 * rport value if present (if you don't want to overwrite the previous
1505 * version remove the comments) */
1506 if ((msg->msg_flags&FL_FORCE_RPORT)||
1507 (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1508 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1509 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1510 " rport_builder failed\n");
1511 goto error01; /* free everything */
1515 /* add via header to the list */
1516 /* try to add it before msg. 1st via */
1517 /* add first via, as an anchor for second via*/
1518 anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
1519 if (anchor==0) goto error01;
1520 if (insert_new_lump_before(anchor, line_buf, via_len, HDR_VIA_T)==0)
1522 /* find out where the offset of the first parameter that should be added
1523 * (after host:port), needed by add receive & maybe rport */
1524 if (msg->via1->params.s){
1525 size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
1528 size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
1529 if (msg->via1->port!=0){
1530 /*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
1531 size += msg->via1->port_str.len + 1; /* +1 for ':'*/
1534 /* no longer necessary, now hots.s contains [] */
1536 if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
1540 /* if received needs to be added, add anchor after host and add it, or
1541 * overwrite the previous one if already present */
1543 if (msg->via1->received){ /* received already present => overwrite it*/
1544 via_insert_param=del_lump(msg,
1545 msg->via1->received->start-buf-1, /*;*/
1546 msg->via1->received->size+1, /*;*/ HDR_VIA_T);
1547 }else if (via_insert_param==0){ /* receive not present, ok */
1548 via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1551 if (via_insert_param==0) goto error02; /* free received_buf */
1552 if (insert_new_lump_after(via_insert_param, received_buf, received_len,
1553 HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
1555 /* if rport needs to be updated, delete it if present and add it's value */
1557 if (msg->via1->rport){ /* rport already present */
1558 via_insert_param=del_lump(msg,
1559 msg->via1->rport->start-buf-1, /*';'*/
1560 msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
1561 }else if (via_insert_param==0){ /*force rport, no rport present */
1562 /* no rport, add it */
1563 via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1566 if (via_insert_param==0) goto error03; /* free rport_buf */
1567 if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
1569 goto error03; /* free rport_buf */
1573 /* compute new msg len and fix overlapping zones*/
1574 new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info);
1576 LOG(L_ERR, "DEBUG: new_len(%d)=len(%d)+lumps_len\n", new_len, len);
1579 if (msg->new_uri.s){
1580 uri_len=msg->new_uri.len;
1581 new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
1583 new_buf=(char*)pkg_malloc(new_len+1);
1585 ser_error=E_OUT_OF_MEM;
1586 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
1591 if (msg->new_uri.s){
1592 /* copy message up to uri */
1593 size=msg->first_line.u.request.uri.s-buf;
1594 memcpy(new_buf, buf, size);
1598 memcpy(new_buf+offset, msg->new_uri.s, uri_len);
1600 s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
1603 /* copy msg adding/removing lumps */
1604 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
1605 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info);
1606 /* copy the rest of the message */
1607 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
1611 if (new_buf[new_len-1]==0) {
1612 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: 0 in the end\n");
1617 *returned_len=new_len;
1620 if (id_buf) pkg_free(id_buf); /* it's not in a lump => we don't need it
1626 if (line_buf) pkg_free(line_buf);
1628 if (received_buf) pkg_free(received_buf);
1630 if (rport_buf) pkg_free(rport_buf);
1633 if (id_buf) pkg_free(id_buf);
1641 char * build_res_buf_from_sip_res( struct sip_msg* msg,
1642 unsigned int *returned_len)
1644 unsigned int new_len, via_len, body_delta;
1646 unsigned offset, s_offset, via_offset;
1653 /* we must remove the first via */
1654 if (msg->via1->next) {
1655 via_len=msg->via1->bsize;
1656 via_offset=msg->h_via1->body.s-buf;
1658 via_len=msg->h_via1->len;
1659 via_offset=msg->h_via1->name.s-buf;
1662 /* Calculate message body difference and adjust
1665 body_delta = lumps_len(msg, msg->body_lumps, 0);
1666 if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
1668 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1669 " Content-Length\n");
1673 /* remove the first via*/
1674 if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
1675 LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
1680 new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
1681 know the send sock */
1683 DBG(" old size: %d, new size: %d\n", len, new_len);
1684 new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
1687 LOG(L_ERR, "ERROR: build_res_buf_from_sip_res: out of mem\n");
1690 new_buf[new_len]=0; /* debug: print the message */
1692 /*FIXME: no send sock*/
1693 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0);/*FIXME:*/
1694 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0);
1695 /* copy the rest of the message */
1696 memcpy(new_buf+offset,
1700 DBG("build_res_from_sip_res: copied size: orig:%d, new: %d, rest: %d"
1701 " msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
1703 *returned_len=new_len;
1711 char * build_res_buf_from_sip_req( unsigned int code, char *text ,str *new_tag,
1712 struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
1715 unsigned int len,foo;
1716 struct hdr_field *hdr;
1717 struct lump_rpl *lump;
1718 struct lump_rpl *body;
1721 unsigned int received_len;
1723 unsigned int rport_len;
1725 unsigned int warning_len;
1726 char* content_len_buf;
1727 unsigned int content_len_len;
1728 unsigned int text_len;
1735 received_buf=rport_buf=warning_buf=content_len_buf=0;
1736 received_len=rport_len=warning_len=content_len_len=0;
1738 text_len=strlen(text);
1740 to_tag.s=0; /* fixes gcc 4.0 warning */
1743 /* force parsing all headers -- we want to return all
1744 Via's in the reply and they may be scattered down to the
1745 end of header (non-block Vias are a really poor property
1747 if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
1748 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1749 "alas, parse_headers failed\n");
1753 /*computes the length of the new response buffer*/
1756 /* check if received needs to be added */
1757 if (received_test(msg)) {
1758 if ((received_buf=received_builder(msg,&received_len))==0) {
1759 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1760 "alas, received_builder failed\n");
1764 /* check if rport needs to be updated */
1765 if ( (msg->msg_flags&FL_FORCE_RPORT)||
1766 (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1767 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1768 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"
1769 " rport_builder failed\n");
1770 goto error01; /* free everything */
1772 if (msg->via1->rport)
1773 len -= msg->via1->rport->size+1; /* include ';' */
1777 len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
1778 text_len + CRLF_LEN/*new line*/;
1779 /*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
1780 for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
1781 switch (hdr->type) {
1783 if (new_tag && new_tag->len) {
1784 to_tag=get_to(msg)->tag_value;
1786 len+=new_tag->len-to_tag.len;
1788 len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
1793 /* we always add CRLF to via*/
1794 len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
1795 if (hdr==msg->h_via1) len += received_len+rport_len;
1797 case HDR_RECORDROUTE_T:
1798 /* RR only for 1xx and 2xx replies */
1799 if (code<180 || code>=300)
1804 /* we keep the original termination for these headers*/
1808 /* do nothing, we are interested only in the above headers */
1813 for(lump=msg->reply_lump;lump;lump=lump->next) {
1814 len += lump->text.len;
1815 if (lump->flags&LUMP_RPL_BODY)
1819 if (server_signature)
1820 len += SERVER_HDR_LEN + CRLF_LEN;
1823 warning_buf = warning_builder(msg,&warning_len);
1824 if (warning_buf) len += warning_len + CRLF_LEN;
1825 else LOG(L_WARN, "WARNING: warning skipped -- too big\n");
1827 /* content length hdr */
1829 content_len_buf = int2str(body->text.len, (int*)&content_len_len);
1830 len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
1832 len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
1834 /* end of message */
1835 len += CRLF_LEN; /*new line*/
1838 buf = (char*) pkg_malloc( len+1 );
1841 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: out of memory "
1842 " ; needs %d\n",len);
1846 /* filling the buffer*/
1849 memcpy( p , msg->first_line.u.request.version.s ,
1850 msg->first_line.u.request.version.len);
1851 p += msg->first_line.u.request.version.len;
1854 for ( i=2 , foo = code ; i>=0 ; i-- , foo=foo/10 )
1855 *(p+i) = '0' + foo - ( foo/10 )*10;
1858 memcpy( p , text , text_len );
1860 memcpy( p, CRLF, CRLF_LEN );
1863 for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
1867 if (hdr==msg->h_via1){
1869 if (msg->via1->rport){ /* delete the old one */
1870 /* copy until rport */
1871 append_str_trans( p, hdr->name.s ,
1872 msg->via1->rport->start-hdr->name.s-1,msg);
1873 /* copy new rport */
1874 append_str(p, rport_buf, rport_len);
1875 /* copy the rest of the via */
1876 append_str_trans(p, msg->via1->rport->start+
1877 msg->via1->rport->size,
1878 hdr->body.s+hdr->body.len-
1879 msg->via1->rport->start-
1880 msg->via1->rport->size, msg);
1881 }else{ /* just append the new one */
1882 /* normal whole via copy */
1883 append_str_trans( p, hdr->name.s ,
1884 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1885 append_str(p, rport_buf, rport_len);
1888 /* normal whole via copy */
1889 append_str_trans( p, hdr->name.s ,
1890 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1893 append_str( p, received_buf, received_len);
1895 /* normal whole via copy */
1896 append_str_trans( p, hdr->name.s,
1897 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1899 append_str( p, CRLF,CRLF_LEN);
1901 case HDR_RECORDROUTE_T:
1902 /* RR only for 1xx and 2xx replies */
1903 if (code<180 || code>=300) break;
1904 append_str(p, hdr->name.s, hdr->len);
1907 if (new_tag && new_tag->len){
1908 if (to_tag.s ) { /* replacement */
1910 append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
1911 /* to tag replacement */
1912 bmark->to_tag_val.s=p;
1913 bmark->to_tag_val.len=new_tag->len;
1914 append_str( p, new_tag->s,new_tag->len);
1915 /* the rest after to-tag */
1916 append_str( p, to_tag.s+to_tag.len,
1917 hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
1918 }else{ /* adding a new to-tag */
1919 after_body=hdr->body.s+hdr->body.len;
1920 append_str( p, hdr->name.s, after_body-hdr->name.s);
1921 append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
1922 bmark->to_tag_val.s=p;
1923 bmark->to_tag_val.len=new_tag->len;
1924 append_str( p, new_tag->s,new_tag->len);
1925 append_str( p, after_body,
1926 hdr->name.s+hdr->len-after_body);
1929 } /* no new to-tag -- proceed to 1:1 copying */
1930 totags=((struct to_body*)(hdr->parsed))->tag_value.s;
1932 bmark->to_tag_val.s=p+(totags-hdr->name.s);
1933 bmark->to_tag_val.len=
1934 ((struct to_body*)(hdr->parsed))->tag_value.len;
1937 bmark->to_tag_val.len = 0;
1938 bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
1944 append_str(p, hdr->name.s, hdr->len);
1947 /* do nothing, we are interested only in the above headers */
1952 for(lump=msg->reply_lump;lump;lump=lump->next)
1953 if (lump->flags&LUMP_RPL_HDR){
1954 memcpy(p,lump->text.s,lump->text.len);
1955 p += lump->text.len;
1958 if (server_signature) {
1959 memcpy( p, SERVER_HDR , SERVER_HDR_LEN );
1961 memcpy( p, CRLF, CRLF_LEN );
1964 /* content_length hdr */
1965 if (content_len_len) {
1966 append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
1967 append_str( p, content_len_buf, content_len_len );
1968 append_str( p, CRLF, CRLF_LEN );
1970 append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
1972 /* warning header */
1974 memcpy( p, warning_buf, warning_len);
1976 memcpy( p, CRLF, CRLF_LEN);
1980 memcpy( p, CRLF, CRLF_LEN );
1984 memcpy ( p, body->text.s, body->text.len );
1989 LOG(L_CRIT,"BUG:build_res_from_sip_req: diff len=%d p-buf=%d\n",
1993 *returned_len = len;
1994 /* in req2reply, received_buf is not introduced to lumps and
1995 needs to be deleted here
1997 if (received_buf) pkg_free(received_buf);
1998 if (rport_buf) pkg_free(rport_buf);
2002 if (received_buf) pkg_free(received_buf);
2003 if (rport_buf) pkg_free(rport_buf);
2011 /* return number of chars printed or 0 if space exceeded;
2012 assumes buffer size of at least MAX_BRANCH_PARAM_LEN
2014 int branch_builder( unsigned int hash_index,
2015 /* only either parameter useful */
2016 unsigned int label, char * char_v,
2018 char *branch_str, int *len )
2024 /* hash id provided ... start with it */
2025 size=MAX_BRANCH_PARAM_LEN;
2029 memcpy(begin, MCOOKIE, MCOOKIE_LEN );
2030 size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
2032 if (int2reverse_hex( &begin, &size, hash_index)==-1)
2036 *begin=BRANCH_SEPARATOR;
2040 /* string with request's characteristic value ... use it ... */
2042 if (memcpy(begin,char_v,MD5_LEN)) {
2043 begin+=MD5_LEN; size-=MD5_LEN;
2045 } else { /* ... use the "label" value otherwise */
2046 if (int2reverse_hex( &begin, &size, label )==-1)
2051 *begin=BRANCH_SEPARATOR;
2055 if (int2reverse_hex( &begin, &size, branch)==-1)
2058 *len=MAX_BRANCH_PARAM_LEN-size;
2065 /* uses only the send_info->send_socket, send_info->proto and
2066 * send_info->comp (so that a send_info used for sending can be passed
2067 * to this function w/o changes and the correct via will be built) */
2068 char* via_builder( unsigned int *len,
2069 struct dest_info* send_info /* where to send the reply */,
2070 str* branch, str* extra_params, struct hostport* hp)
2072 unsigned int via_len, extra_len;
2075 str* address_str; /* address displayed in via */
2076 str* port_str; /* port no displayed in via */
2077 struct socket_info* send_sock;
2078 int comp_len, comp_name_len;
2081 send_sock=send_info->send_sock;
2082 /* use pre-set address in via or the outbound socket one */
2083 if ( hp && hp->host->len)
2084 address_str=hp->host;
2086 address_str=&(send_sock->address_str);
2087 if (hp && hp->port->len)
2090 port_str=&(send_sock->port_no_str);
2092 comp_len=comp_name_len=0;
2095 switch(send_info->comp){
2099 comp_len=COMP_PARAM_LEN;
2100 comp_name_len=SIGCOMP_NAME_LEN;
2101 comp_name=SIGCOMP_NAME;
2104 comp_len=COMP_PARAM_LEN;
2105 comp_name_len=SERGZ_NAME_LEN;
2106 comp_name=SERGZ_NAME;
2109 LOG(L_CRIT, "BUG: via_builder: unknown comp %d\n",
2111 /* continue, we'll just ignore comp */
2113 #endif /* USE_COMP */
2115 max_len=MY_VIA_LEN+address_str->len /* space in MY_VIA */
2116 +2 /* just in case it is a v6 address ... [ ] */
2117 +1 /*':'*/+port_str->len
2118 +(branch?(MY_BRANCH_LEN+branch->len):0)
2119 +(extra_params?extra_params->len:0)
2120 +comp_len+comp_name_len
2122 line_buf=pkg_malloc( max_len );
2124 ser_error=E_OUT_OF_MEM;
2125 LOG(L_ERR, "ERROR: via_builder: out of memory\n");
2131 via_len=MY_VIA_LEN+address_str->len; /*space included in MY_VIA*/
2133 memcpy(line_buf, MY_VIA, MY_VIA_LEN);
2134 if (send_info->proto==PROTO_UDP){
2136 }else if (send_info->proto==PROTO_TCP){
2137 memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
2138 }else if (send_info->proto==PROTO_TLS){
2139 memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
2141 LOG(L_CRIT, "BUG: via_builder: unknown proto %d\n", send_info->proto);
2145 /* add [] only if ipv6 and outbound socket address is used;
2146 * if using pre-set no check is made */
2147 if ((send_sock->address.af==AF_INET6) &&
2148 (address_str==&(send_sock->address_str))) {
2149 line_buf[MY_VIA_LEN]='[';
2150 line_buf[MY_VIA_LEN+1+address_str->len]=']';
2152 via_len+=2; /* [ ]*/
2155 memcpy(line_buf+MY_VIA_LEN+extra_len, address_str->s, address_str->len);
2156 if ((send_sock->port_no!=SIP_PORT) || (port_str!=&send_sock->port_no_str)){
2157 line_buf[via_len]=':'; via_len++;
2158 memcpy(line_buf+via_len, port_str->s, port_str->len);
2159 via_len+=port_str->len;
2162 /* branch parameter */
2164 memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
2165 via_len+=MY_BRANCH_LEN;
2166 memcpy(line_buf+via_len, branch->s, branch->len );
2167 via_len+=branch->len;
2171 memcpy(line_buf+via_len, extra_params->s, extra_params->len);
2172 via_len+=extra_params->len;
2177 memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
2178 via_len+=COMP_PARAM_LEN;
2179 memcpy(line_buf+via_len, comp_name, comp_name_len);
2180 via_len+=comp_name_len;
2184 memcpy(line_buf+via_len, CRLF, CRLF_LEN);
2186 line_buf[via_len]=0; /* null terminate the string*/
2192 /* builds a char* buffer from message headers without body
2193 * first line is excluded in case of skip_first_line=1
2194 * error is set -1 if the memory allocation failes
2196 char * build_only_headers( struct sip_msg* msg, int skip_first_line,
2197 unsigned int *returned_len,
2199 struct dest_info* send_info)
2201 char *buf, *new_buf;
2202 unsigned int offset, s_offset, len, new_len;
2206 if (skip_first_line)
2207 s_offset = msg->headers->name.s - buf;
2211 /* original length without body, and without final \r\n */
2212 len = msg->unparsed - buf;
2213 /* new msg length */
2214 new_len = len - /* original length */
2215 s_offset + /* skipped first line */
2216 lumps_len(msg, msg->add_rm, send_info); /* lumps */
2223 new_buf = (char *)pkg_malloc(new_len+1);
2225 LOG(L_ERR, "ERROR: build_only_headers: Not enough memory\n");
2232 /* copy message lumps */
2233 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2234 /* copy the rest of the message without body */
2235 if (len > s_offset) {
2236 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2237 offset += (len-s_offset);
2239 new_buf[offset] = 0;
2241 *returned_len = offset;
2245 /* builds a char* buffer from message body
2246 * error is set -1 if the memory allocation failes
2248 char * build_body( struct sip_msg* msg,
2249 unsigned int *returned_len,
2251 struct dest_info* send_info)
2253 char *buf, *new_buf, *body;
2254 unsigned int offset, s_offset, len, new_len;
2257 body = get_body(msg);
2259 if (!body || (body[0] == 0)) {
2264 s_offset = body - buf;
2266 /* original length of msg with body */
2268 /* new body length */
2269 new_len = len - /* original length */
2270 s_offset + /* msg without body */
2271 lumps_len(msg, msg->body_lumps, send_info); /* lumps */
2273 new_buf = (char *)pkg_malloc(new_len+1);
2275 LOG(L_ERR, "ERROR: build_body: Not enough memory\n");
2282 /* copy body lumps */
2283 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2284 /* copy the rest of the message without body */
2285 if (len > s_offset) {
2286 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2287 offset += (len-s_offset);
2289 new_buf[offset] = 0;
2291 *returned_len = offset;
2295 /* builds a char* buffer from SIP message including body
2296 * The function adjusts the Content-Length HF according
2297 * to body lumps in case of touch_clen=1.
2299 char * build_all( struct sip_msg* msg, int touch_clen,
2300 unsigned int *returned_len,
2302 struct dest_info* send_info)
2304 char *buf, *new_buf;
2305 unsigned int offset, s_offset, len, new_len;
2306 unsigned int body_delta;
2309 /* Calculate message body difference */
2310 body_delta = lumps_len(msg, msg->body_lumps, send_info);
2312 /* adjust Content-Length */
2313 if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
2314 LOG(L_ERR, "ERROR: build_all: Error while adjusting"
2315 " Content-Length\n");
2322 /* original msg length */
2324 /* new msg length */
2325 new_len = len + /* original length */
2326 lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
2327 body_delta; /* body lumps */
2334 new_buf = (char *)pkg_malloc(new_len+1);
2336 LOG(L_ERR, "ERROR: build_all: Not enough memory\n");
2341 offset = s_offset = 0;
2343 /* copy message lumps */
2344 process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2345 /* copy body lumps */
2346 process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2347 /* copy the rest of the message */
2348 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2349 offset += (len-s_offset);
2350 new_buf[offset] = 0;
2352 *returned_len = offset;