core: fix another bunch of 'no real prototype' warnings, add doxygen docs
[sip-router] / msg_translator.c
1 /*
2  * $Id$
3  *
4  *
5  * Copyright (C) 2001-2003 FhG Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  *
28  *
29  * History:
30  * --------
31  * 2003-01-20  bug_fix: use of return value of snprintf aligned to C99 (jiri)
32  * 2003-01-23  added rport patches, contributed by
33  *              Maxim Sobolev <sobomax@FreeBSD.org> and heavily modified by me
34  *              (andrei)
35  * 2003-01-24  added i param to via of outgoing requests (used by tcp),
36  *              modified via_builder params (andrei)
37  * 2003-01-27  more rport fixes (make use of new via_param->start)  (andrei)
38  * 2003-01-27  next baby-step to removing ZT - PRESERVE_ZT (jiri)
39  * 2003-01-29  scratchpad removed (jiri)
40  * 2003-02-28  scratchpad compatibility abandoned (jiri)
41  * 2003-03-01  VOICE_MAIL defs removed (jiri)
42  * 2003-03-06  totags in outgoing replies bookmarked to enable
43  *             ACK/200 tag matching (andrei)
44  * 2003-03-18  killed the build_warning snprintf (andrei)
45  * 2003-03-31  added subst lump support (andrei)
46  * 2003-04-01  added opt (conditional) lump support (andrei)
47  * 2003-04-02  added more subst lumps: SUBST_{SND,RCV}_ALL
48  *              => ip:port;transport=proto (andrei)
49  * 2003-04-12  added FL_FORCE_RPORT support (andrei)
50  * 2003-04-13  updated warning builder -- fixed (andrei)
51  * 2003-07-10  check_via_address knows now how to compare with ipv6 address
52  *              references (e.g [::1]) (andrei)
53  *             build_req_fomr_sip_req no longer adds 1 for ipv6 via parameter
54  *              position calculations ([] are part of host.s now) (andrei)
55  * 2003-10-02  via+lump dst address/port can be set to preset values (andrei)
56  * 2003-10-08  receive_test function-alized (jiri)
57  * 2003-10-20  added body_lump list (sip_msg), adjust_clen (andrei & jan)
58  * 2003-11-11  type of rpl_lumps replaced by flags (bogdan)
59  * 2006-04-20  build_req_from_sip_req, via_builder and lump_* functions
60  *              use now struct dest_info; lumps & via comp param support
61  *              (rfc3486) (andrei)
62  * 2007-08-31  id_builder() and via_builder() are grouped into one function:
63  *             create_via_hf() -- tm module needs them as well (Miklos)
64  * 2008-12-17  build_req_from_sip_req() will now fallback to tcp, tls or sctp
65  *              if packet size > udp_mtu and fallback is enabled 
66  *             build_req_from_sip_req() uses now global_req_flags along
67  *               msg->msg_flags  (andrei)
68  *
69  */
70 /* Via special params:
71  * requests:
72  * - if the address in via is different from the src_ip or an existing
73  *   received=something is found, received=src_ip is added (and any preexisting
74  *   received is deleted). received is added as the first via parameter if no
75  *   receive is previously present or over the old receive.
76  * - if the original via contains rport / rport=something or msg->msg_flags
77  *   FL_FORCE_RPORT is set (e.g. script force_rport() cmd) rport=src_port
78  *   is added (over previous rport / as first via param or after received
79  *   if no received was present and received is added too)
80  * local replies:
81  *    (see also sl_send_reply)
82  *  - rport and received are added in mostly the same way as for requests, but
83  *    in the reverse order (first rport and then received). See also
84  *    limitations.
85  *  - if reply_to_via is set (default off) the local reply will be sent to
86  *    the address in via (received is ignored since it was not set by us). The
87  *    destination port is either the message source port if via contains rport
88  *    or the FL_FORCE_RPORT flag is set or the port from the via. If either
89  *    port or rport are present a normal dns lookup (instead of a srv lookup)
90  *    is performed on the address. If no port is present and a srv lookup is
91  *    performed the port is taken from the srv lookup. If the srv lookup failed
92  *    or it was not performed, the port is set to the default sip port (5060).
93  *  - if reply_to_via is off (default) the local reply is sent to the message
94  *    source ip address. The destination port is set to the source port if
95  *    rport is present or FL_FORCE_RPORT flag is set, to the via port or to
96  *    the default sip port (5060) if neither rport or via port are present.
97  * "normal" replies:
98  *  - if received is present the message is sent to the received address else
99  *    if no port is present (neither a normal via port or rport) a dns srv
100  *    lookup is performed on the host part and the reply is sent to the
101  *    resulting ip. If a port is present or the host part is an ip address
102  *    the dns lookup will be a "normal" one (A or AAAA).
103  *  - if rport is present, it's value will be used as the destination port
104  *   (and this will also disable srv lookups)
105  *  - if no port is present the destination port will be taken from the srv
106  *    lookup. If the srv lookup fails or is not performed (e.g. ip address
107  *    in host) the destination port will be set to the default sip port (5060).
108  *
109  * Known limitations:
110  * - when locally replying to a message, rport and received will be appended to
111  *   the via header parameters (for forwarded requests they are inserted at the
112  *   beginning).
113  * - a locally generated reply might get two received via parameters if a
114  *   received is already present in the original message (this should not
115  *   happen though, but ...)
116  *
117  *--andrei
118 */
119
120 /*!
121  * \file
122  * \brief SIP-router core :: 
123  * \ingroup core
124  * Module: \ref core
125  */
126
127 #include <sys/types.h>
128 #include <sys/socket.h>
129 #include <netdb.h>
130 #include <string.h>
131 #include <stdio.h>
132 #include <stdlib.h>
133
134 #include "comp_defs.h"
135 #include "msg_translator.h"
136 #include "globals.h"
137 #include "error.h"
138 #include "mem/mem.h"
139 #include "dprint.h"
140 #include "config.h"
141 #include "md5utils.h"
142 #include "data_lump.h"
143 #include "data_lump_rpl.h"
144 #include "ip_addr.h"
145 #include "resolve.h"
146 #include "ut.h"
147 #include "pt.h"
148 #include "cfg/cfg.h"
149 #include "parser/parse_to.h"
150 #include "forward.h"
151
152
153 #define append_str_trans(_dest,_src,_len,_msg) \
154         append_str( (_dest), (_src), (_len) );
155
156 extern char version[];
157 extern int version_len;
158
159
160
161 /** per process fixup function for global_req_flags.
162   * It should be called from the configuration framework.
163   */
164 void fix_global_req_flags(str* gname, str* name)
165 {
166         global_req_flags=0;
167         switch(cfg_get(core, core_cfg, udp_mtu_try_proto)){
168                 case PROTO_NONE:
169                 case PROTO_UDP:
170                         /* do nothing */
171                         break;
172                 case PROTO_TCP:
173                         global_req_flags|=FL_MTU_TCP_FB;
174                         break;
175                 case PROTO_TLS:
176                         global_req_flags|=FL_MTU_TLS_FB;
177                         break;
178                 case PROTO_SCTP:
179                         global_req_flags|=FL_MTU_SCTP_FB;
180                         break;
181         }
182         if (cfg_get(core, core_cfg, force_rport))
183                 global_req_flags|=FL_FORCE_RPORT;
184 }
185
186
187
188 /* checks if ip is in host(name) and ?host(ip)=name?
189  * ip must be in network byte order!
190  *  resolver = DO_DNS | DO_REV_DNS; if 0 no dns check is made
191  * return 0 if equal */
192 static int check_via_address(struct ip_addr* ip, str *name,
193                                 unsigned short port, int resolver)
194 {
195         struct hostent* he;
196         int i;
197         char* s;
198         int len;
199
200         /* maybe we are lucky and name it's an ip */
201         s=ip_addr2a(ip);
202         if (s){
203                 DBG("check_via_address(%s, %.*s, %d)\n",
204                         s, name->len, name->s, resolver);
205                 len=strlen(s);
206
207         #ifdef USE_IPV6
208                 /* check if name->s is an ipv6 address or an ipv6 address ref. */
209                 if ((ip->af==AF_INET6) &&
210                                 (       ((len==name->len)&&(strncasecmp(name->s, s, name->len)==0))
211                                         ||
212                                         ((len==(name->len-2))&&(name->s[0]=='[')&&
213                                                 (name->s[name->len-1]==']')&&
214                                                 (strncasecmp(name->s+1, s, len)==0))
215                                 )
216                    )
217                         return 0;
218                 else
219         #endif
220
221                         if (strncmp(name->s, s, name->len)==0)
222                                 return 0;
223         }else{
224                 LOG(L_CRIT, "check_via_address: BUG: could not convert ip address\n");
225                 return -1;
226         }
227
228         if (port==0) port=SIP_PORT;
229         if (resolver&DO_DNS){
230                 DBG("check_via_address: doing dns lookup\n");
231                 /* try all names ips */
232                 he=sip_resolvehost(name, &port, 0); /* don't use naptr */
233                 if (he && ip->af==he->h_addrtype){
234                         for(i=0;he && he->h_addr_list[i];i++){
235                                 if ( memcmp(&he->h_addr_list[i], ip->u.addr, ip->len)==0)
236                                         return 0;
237                         }
238                 }
239         }
240         if (resolver&DO_REV_DNS){
241                 DBG("check_via_address: doing rev. dns lookup\n");
242                 /* try reverse dns */
243                 he=rev_resolvehost(ip);
244                 if (he && (strncmp(he->h_name, name->s, name->len)==0))
245                         return 0;
246                 for (i=0; he && he->h_aliases[i];i++){
247                         if (strncmp(he->h_aliases[i],name->s, name->len)==0)
248                                 return 0;
249                 }
250         }
251         return -1;
252 }
253
254
255 /* check if IP address in Via != source IP address of signaling */
256 int received_test( struct sip_msg *msg )
257 {
258         int rcvd;
259
260         rcvd=msg->via1->received
261                         || check_via_address(&msg->rcv.src_ip, &msg->via1->host,
262                                                         msg->via1->port, received_dns);
263         return rcvd;
264 }
265
266
267 static char * warning_builder( struct sip_msg *msg, unsigned int *returned_len)
268 {
269         static char buf[MAX_WARNING_LEN];
270         str *foo;
271         int print_len, l;
272         int clen;
273         char* t;
274
275 #define str_print(string, string_len) \
276                 do{ \
277                         l=(string_len); \
278                         if ((clen+l)>MAX_WARNING_LEN) \
279                                 goto error_overflow; \
280                         memcpy(buf+clen, (string), l); \
281                         clen+=l; \
282                 }while(0)
283
284 #define str_lenpair_print(string, string_len, string2, string2_len) \
285                 do{ \
286                         str_print(string, string_len); \
287                         str_print(string2, string2_len);\
288                 }while(0)
289
290 #define str_pair_print( string, string2, string2_len) \
291                 str_lenpair_print((string), strlen((string)), (string2), (string2_len))
292
293 #define str_int_print(string, intval)\
294                 do{\
295                         t=int2str((intval), &print_len); \
296                         str_pair_print(string, t, print_len);\
297                 } while(0)
298
299 #define str_su_print(sockaddr)\
300                 do{\
301                         t=su2a(&sockaddr, sizeof(sockaddr)); \
302                         print_len=strlen(t); \
303                         str_print(t, print_len); \
304                 } while(0)
305
306 #define str_ipaddr_print(string, ipaddr_val)\
307                 do{\
308                         t=ip_addr2a((ipaddr_val)); \
309                         print_len=strlen(t); \
310                         str_pair_print(string, t, print_len);\
311                 } while(0)
312
313         clen=0;
314         str_print(WARNING, WARNING_LEN);
315         str_su_print(msg->rcv.bind_address->su);
316         str_print(WARNING_PHRASE, WARNING_PHRASE_LEN);
317
318         /*adding out_uri*/
319         if (msg->new_uri.s)
320                 foo=&(msg->new_uri);
321         else
322                 foo=&(msg->first_line.u.request.uri);
323         /* pid= */
324         str_int_print(" pid=", my_pid());
325         /* req_src_ip= */
326         str_ipaddr_print(" req_src_ip=", &msg->rcv.src_ip);
327         str_int_print(" req_src_port=", msg->rcv.src_port);
328         str_pair_print(" in_uri=", msg->first_line.u.request.uri.s,
329                                                                 msg->first_line.u.request.uri.len);
330         str_pair_print(" out_uri=", foo->s, foo->len);
331         str_pair_print(" via_cnt",
332                                         (msg->parsed_flag & HDR_EOH_F)==HDR_EOH_F ? "=" : ">", 1);
333         str_int_print("=", via_cnt);
334         if (clen<MAX_WARNING_LEN){ buf[clen]='"'; clen++; }
335         else goto error_overflow;
336
337
338         *returned_len=clen;
339         return buf;
340 error_overflow:
341         LOG(L_NOTICE, "NOTICE: warning_builder: buffer size exceeded (probably too long URI)\n");
342         *returned_len=0;
343         return 0;
344 }
345
346
347
348
349 char* received_builder(struct sip_msg *msg, unsigned int *received_len)
350 {
351         char *buf;
352         int  len;
353         struct ip_addr *source_ip;
354         char *tmp;
355         int  tmp_len;
356
357         source_ip=&msg->rcv.src_ip;
358
359         buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
360         if (buf==0){
361                 ser_error=E_OUT_OF_MEM;
362                 LOG(L_ERR, "ERROR: received_builder: out of memory\n");
363                 return 0;
364         }
365         memcpy(buf, RECEIVED, RECEIVED_LEN);
366         if ( (tmp=ip_addr2a(source_ip))==0)
367                 return 0; /* error*/
368         tmp_len=strlen(tmp);
369         len=RECEIVED_LEN+tmp_len;
370
371         memcpy(buf+RECEIVED_LEN, tmp, tmp_len);
372         buf[len]=0; /*null terminate it */
373
374         *received_len = len;
375         return buf;
376 }
377
378
379
380 char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
381 {
382         char* buf;
383         char* tmp;
384         int tmp_len;
385         int len;
386
387         tmp_len=0;
388         tmp=int2str(msg->rcv.src_port, &tmp_len);
389         len=RPORT_LEN+tmp_len;
390         buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
391         if (buf==0){
392                 ser_error=E_OUT_OF_MEM;
393                 LOG(L_ERR, "ERROR: rport_builder: out of memory\n");
394                 return 0;
395         }
396         memcpy(buf, RPORT, RPORT_LEN);
397         memcpy(buf+RPORT_LEN, tmp, tmp_len);
398         buf[len]=0; /*null terminate it*/
399
400         *rport_len=len;
401         return buf;
402 }
403
404
405
406 char* id_builder(struct sip_msg* msg, unsigned int *id_len)
407 {
408         char* buf;
409         int len, value_len;
410         char revhex[sizeof(int)*2];
411         char* p;
412         int size;
413
414         size=sizeof(int)*2;
415         p=&revhex[0];
416         if (int2reverse_hex(&p, &size, msg->rcv.proto_reserved1)==-1){
417                 LOG(L_CRIT, "BUG: id_builder: not enough space for id\n");
418                 return 0;
419         }
420         value_len=p-&revhex[0];
421         len=ID_PARAM_LEN+value_len;
422         buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
423         if (buf==0){
424                 ser_error=E_OUT_OF_MEM;
425                 LOG(L_ERR, "ERROR: id_builder: out of memory\n");
426                 return 0;
427         }
428         memcpy(buf, ID_PARAM, ID_PARAM_LEN);
429         memcpy(buf+ID_PARAM_LEN, revhex, value_len);
430         buf[len]=0; /* null terminate it */
431         *id_len=len;
432         return buf;
433 }
434
435
436
437 char* clen_builder(     struct sip_msg* msg, int *clen_len, int diff, 
438                                         int body_only)
439 {
440         char* buf;
441         int len;
442         int value;
443         char* value_s;
444         int value_len;
445         char* body;
446
447
448         body=get_body(msg);
449         if (body==0){
450                 ser_error=E_BAD_REQ;
451                 LOG(L_ERR, "ERROR: clen_builder: no message body found"
452                                         " (missing crlf?)");
453                 return 0;
454         }
455         value=msg->len-(int)(body-msg->buf)+diff;
456         value_s=int2str(value, &value_len);
457         DBG("clen_builder: content-length: %d (%s)\n", value, value_s);
458
459         if (body_only) {
460                 len=value_len;
461         }
462         else {
463                 len=CONTENT_LENGTH_LEN+value_len+CRLF_LEN;
464         }
465         buf=pkg_malloc(sizeof(char)*(len+1));
466         if (buf==0){
467                 ser_error=E_OUT_OF_MEM;
468                 LOG(L_ERR, "ERROR: clen_builder: out of memory\n");
469                 return 0;
470         }
471         if (body_only) {
472                 memcpy(buf, value_s, value_len);
473         }
474         else {
475                 memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
476                 memcpy(buf+CONTENT_LENGTH_LEN, value_s, value_len);
477                 memcpy(buf+CONTENT_LENGTH_LEN+value_len, CRLF, CRLF_LEN);
478         }
479         buf[len]=0; /* null terminate it */
480         *clen_len=len;
481         return buf;
482 }
483
484
485
486 /* checks if a lump opt condition
487  * returns 1 if cond is true, 0 if false */
488 static inline int lump_check_opt(       struct lump *l,
489                                                                         struct sip_msg* msg,
490                                                                         struct dest_info* snd_i
491                                                                         )
492 {
493         struct ip_addr* ip;
494         unsigned short port;
495         int proto;
496
497 #define get_ip_port_proto \
498                         if ((snd_i==0) || (snd_i->send_sock==0)){ \
499                                 LOG(L_CRIT, "ERROR: lump_check_opt: null send socket\n"); \
500                                 return 1; /* we presume they are different :-) */ \
501                         } \
502                         if (msg->rcv.bind_address){ \
503                                 ip=&msg->rcv.bind_address->address; \
504                                 port=msg->rcv.bind_address->port_no; \
505                                 proto=msg->rcv.bind_address->proto; \
506                         }else{ \
507                                 ip=&msg->rcv.dst_ip; \
508                                 port=msg->rcv.dst_port; \
509                                 proto=msg->rcv.proto; \
510                         } \
511
512         switch(l->u.cond){
513                 case COND_FALSE:
514                         return 0;
515                 case COND_TRUE:
516                         LUMP_SET_COND_TRUE(l);
517                         return 1;
518                 case COND_IF_DIFF_REALMS:
519                         get_ip_port_proto;
520                         /* faster tests first */
521                         if ((port==snd_i->send_sock->port_no) && 
522                                         (proto==snd_i->send_sock->proto) &&
523 #ifdef USE_COMP
524                                         (msg->rcv.comp==snd_i->comp) &&
525 #endif
526                                         (ip_addr_cmp(ip, &snd_i->send_sock->address)))
527                                 return 0;
528                         else {
529                                 LUMP_SET_COND_TRUE(l);
530                                 return 1;
531                         }
532                 case COND_IF_DIFF_AF:
533                         get_ip_port_proto;
534                         if (ip->af!=snd_i->send_sock->address.af) {
535                                 LUMP_SET_COND_TRUE(l);
536                                 return 1;
537                         } else return 0;
538                 case COND_IF_DIFF_PROTO:
539                         get_ip_port_proto;
540                         if (proto!=snd_i->send_sock->proto) {
541                                 LUMP_SET_COND_TRUE(l);
542                                 return 1;
543                         } else return 0;
544                 case COND_IF_DIFF_PORT:
545                         get_ip_port_proto;
546                         if (port!=snd_i->send_sock->port_no) {
547                                 LUMP_SET_COND_TRUE(l);
548                                 return 1;
549                         } else return 0;
550                 case COND_IF_DIFF_IP:
551                         get_ip_port_proto;
552                         if (ip_addr_cmp(ip, &snd_i->send_sock->address)) return 0;
553                         else {
554                                 LUMP_SET_COND_TRUE(l);
555                                 return 1;
556                         }
557                 case COND_IF_RAND:
558                         if(rand()>=RAND_MAX/2) {
559                                 LUMP_SET_COND_TRUE(l);
560                                 return 1;
561                         } else return 0;
562                 default:
563                         LOG(L_CRIT, "BUG: lump:w_check_opt: unknown lump condition %d\n",
564                                         l->u.cond);
565         }
566         return 0; /* false */
567 }
568
569
570
571 /* computes the "unpacked" len of a lump list,
572    code moved from build_req_from_req */
573 static inline int lumps_len(struct sip_msg* msg, struct lump* lumps, 
574                                                                 struct dest_info* send_info)
575 {
576         int s_offset;
577         int new_len;
578         struct lump* t;
579         struct lump* r;
580         str* send_address_str;
581         str* send_port_str;
582         struct socket_info* send_sock;
583         
584
585 #ifdef USE_COMP
586         #define RCVCOMP_LUMP_LEN \
587                                                 /* add;comp=xxx */ \
588                                         switch(msg->rcv.comp){ \
589                                                 case COMP_NONE: \
590                                                                 break; \
591                                                 case COMP_SIGCOMP: \
592                                                                 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
593                                                                 break; \
594                                                 case COMP_SERGZ: \
595                                                                 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
596                                                                 break; \
597                                                 default: \
598                                                 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
599                                                                 msg->rcv.comp); \
600                                         }
601
602         #define SENDCOMP_LUMP_LEN \
603                                         /* add;comp=xxx */ \
604                                         switch(send_info->comp){ \
605                                                 case COMP_NONE: \
606                                                                 break; \
607                                                 case COMP_SIGCOMP: \
608                                                                 new_len+=COMP_PARAM_LEN+SIGCOMP_NAME_LEN; \
609                                                                 break; \
610                                                 case COMP_SERGZ: \
611                                                                 new_len+=COMP_PARAM_LEN+SERGZ_NAME_LEN ; \
612                                                                 break; \
613                                                 default: \
614                                                 LOG(L_CRIT, "BUG: lumps_len: unknown comp %d\n", \
615                                                                 send_info->comp); \
616                                         }
617 #else
618         #define RCVCOMP_LUMP_LEN
619         #define SENDCOMP_LUMP_LEN
620 #endif /*USE_COMP */
621         
622 #define SUBST_LUMP_LEN(subst_l) \
623                 switch((subst_l)->u.subst){ \
624                         case SUBST_RCV_IP: \
625                                 if (msg->rcv.bind_address){ \
626                                         new_len+=msg->rcv.bind_address->address_str.len; \
627                                         if (msg->rcv.bind_address->address.af!=AF_INET) \
628                                                 new_len+=2; \
629                                 }else{ \
630                                         /* FIXME */ \
631                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
632                                 }; \
633                                 break; \
634                         case SUBST_RCV_PORT: \
635                                 if (msg->rcv.bind_address){ \
636                                         new_len+=msg->rcv.bind_address->port_no_str.len; \
637                                 }else{ \
638                                         /* FIXME */ \
639                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
640                                 }; \
641                                 break; \
642                         case SUBST_RCV_PROTO: \
643                                 if (msg->rcv.bind_address){ \
644                                         switch(msg->rcv.bind_address->proto){ \
645                                                 case PROTO_NONE: \
646                                                 case PROTO_UDP: \
647                                                 case PROTO_TCP: \
648                                                 case PROTO_TLS: \
649                                                                 new_len+=3; \
650                                                                 break; \
651                                                 case PROTO_SCTP: \
652                                                                 new_len+=4; \
653                                                                 break; \
654                                                 default: \
655                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
656                                                                 msg->rcv.bind_address->proto); \
657                                         }\
658                                 }else{ \
659                                         /* FIXME */ \
660                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
661                                 }; \
662                                 break; \
663                         case SUBST_RCV_ALL: \
664                                 if (msg->rcv.bind_address){ \
665                                         new_len+=msg->rcv.bind_address->address_str.len; \
666                                         if (msg->rcv.bind_address->address.af!=AF_INET) \
667                                                 new_len+=2; \
668                                         if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
669                                                 /* add :port_no */ \
670                                                 new_len+=1+msg->rcv.bind_address->port_no_str.len; \
671                                         }\
672                                                 /*add;transport=xxx*/ \
673                                         switch(msg->rcv.bind_address->proto){ \
674                                                 case PROTO_NONE: \
675                                                 case PROTO_UDP: \
676                                                                 break; /* udp is the default */ \
677                                                 case PROTO_TCP: \
678                                                 case PROTO_TLS: \
679                                                                 new_len+=TRANSPORT_PARAM_LEN+3; \
680                                                                 break; \
681                                                 case PROTO_SCTP: \
682                                                                 new_len+=TRANSPORT_PARAM_LEN+4; \
683                                                                 break; \
684                                                 default: \
685                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
686                                                                 msg->rcv.bind_address->proto); \
687                                         }\
688                                         RCVCOMP_LUMP_LEN \
689                                 }else{ \
690                                         /* FIXME */ \
691                                         LOG(L_CRIT, "FIXME: null bind_address\n"); \
692                                 }; \
693                                 break; \
694                         case SUBST_SND_IP: \
695                                 if (send_sock){ \
696                                         new_len+=send_address_str->len; \
697                                         if (send_sock->address.af!=AF_INET && \
698                                                         send_address_str==&(send_sock->address_str)) \
699                                                 new_len+=2; \
700                                 }else{ \
701                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
702                                                         " null send_sock\n"); \
703                                 }; \
704                                 break; \
705                         case SUBST_SND_PORT: \
706                                 if (send_sock){ \
707                                         new_len+=send_port_str->len; \
708                                 }else{ \
709                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
710                                                         " null send_sock\n"); \
711                                 }; \
712                                 break; \
713                         case SUBST_SND_PROTO: \
714                                 if (send_sock){ \
715                                         switch(send_sock->proto){ \
716                                                 case PROTO_NONE: \
717                                                 case PROTO_UDP: \
718                                                 case PROTO_TCP: \
719                                                 case PROTO_TLS: \
720                                                                 new_len+=3; \
721                                                                 break; \
722                                                 case PROTO_SCTP: \
723                                                                 new_len+=4; \
724                                                                 break; \
725                                                 default: \
726                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
727                                                                 send_sock->proto); \
728                                         }\
729                                 }else{ \
730                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
731                                                         " null send_sock\n"); \
732                                 }; \
733                                 break; \
734                         case SUBST_SND_ALL: \
735                                 if (send_sock){ \
736                                         new_len+=send_address_str->len; \
737                                         if ((send_sock->address.af!=AF_INET) && \
738                                                         (send_address_str==&(send_sock->address_str))) \
739                                                 new_len+=2; \
740                                         if ((send_sock->port_no!=SIP_PORT) || \
741                                                         (send_port_str!=&(send_sock->port_no_str))){ \
742                                                 /* add :port_no */ \
743                                                 new_len+=1+send_port_str->len; \
744                                         }\
745                                         /*add;transport=xxx*/ \
746                                         switch(send_sock->proto){ \
747                                                 case PROTO_NONE: \
748                                                 case PROTO_UDP: \
749                                                                 break; /* udp is the default */ \
750                                                 case PROTO_TCP: \
751                                                 case PROTO_TLS: \
752                                                                 new_len+=TRANSPORT_PARAM_LEN+3; \
753                                                                 break; \
754                                                 case PROTO_SCTP: \
755                                                                 new_len+=TRANSPORT_PARAM_LEN+4; \
756                                                                 break; \
757                                                 default: \
758                                                 LOG(L_CRIT, "BUG: lumps_len: unknown proto %d\n", \
759                                                                 send_sock->proto); \
760                                         }\
761                                         SENDCOMP_LUMP_LEN \
762                                 }else{ \
763                                         /* FIXME */ \
764                                         LOG(L_CRIT, "FIXME: lumps_len called with" \
765                                                         " null send_sock\n"); \
766                                 }; \
767                                 break; \
768                         case SUBST_NOP: /* do nothing */ \
769                                 break; \
770                         default: \
771                                 LOG(L_CRIT, "BUG: unknown subst type %d\n", \
772                                                 (subst_l)->u.subst); \
773                 }
774         
775         if (send_info){
776                 send_sock=send_info->send_sock;
777         }else{
778                 send_sock=0;
779         };
780         s_offset=0;
781         new_len=0;
782         /* init send_address_str & send_port_str */
783         if (msg->set_global_address.len)
784                 send_address_str=&(msg->set_global_address);
785         else
786                 send_address_str=&(send_sock->address_str);
787         if (msg->set_global_port.len)
788                 send_port_str=&(msg->set_global_port);
789         else
790                 send_port_str=&(send_sock->port_no_str);
791
792
793         for(t=lumps;t;t=t->next){
794                 /* skip if this is an OPT lump and the condition is not satisfied */
795                 if ((t->op==LUMP_ADD_OPT)&& !lump_check_opt(t, msg, send_info))
796                         continue;
797                 for(r=t->before;r;r=r->before){
798                         switch(r->op){
799                                 case LUMP_ADD:
800                                         new_len+=r->len;
801                                         break;
802                                 case LUMP_ADD_SUBST:
803                                         SUBST_LUMP_LEN(r);
804                                         break;
805                                 case LUMP_ADD_OPT:
806                                         /* skip if this is an OPT lump and the condition is
807                                          * not satisfied */
808                                         if (!lump_check_opt(r, msg, send_info))
809                                                 goto skip_before;
810                                         break;
811                                 default:
812                                         /* only ADD allowed for before/after */
813                                                 LOG(L_CRIT, "BUG: lumps_len: invalid op "
814                                                         "for data lump (%x)\n", r->op);
815                         }
816                 }
817 skip_before:
818                 switch(t->op){
819                         case LUMP_ADD:
820                                 new_len+=t->len;
821                                 break;
822                         case LUMP_ADD_SUBST:
823                                 SUBST_LUMP_LEN(t);
824                                 break;
825                         case LUMP_ADD_OPT:
826                                 /* we don't do anything here, it's only a condition for
827                                  * before & after */
828                                 break;
829                         case LUMP_DEL:
830                                 /* fix overlapping deleted zones */
831                                 if (t->u.offset < s_offset){
832                                         /* change len */
833                                         if (t->len>s_offset-t->u.offset)
834                                                         t->len-=s_offset-t->u.offset;
835                                         else t->len=0;
836                                         t->u.offset=s_offset;
837                                 }
838                                 s_offset=t->u.offset+t->len;
839                                 new_len-=t->len;
840                                 break;
841                         case LUMP_NOP:
842                                 /* fix offset if overlapping on a deleted zone */
843                                 if (t->u.offset < s_offset){
844                                         t->u.offset=s_offset;
845                                 }else
846                                         s_offset=t->u.offset;
847                                 /* do nothing */
848                                 break;
849                         default:
850                                 LOG(L_CRIT,"BUG:lumps_len: invalid"
851                                                         " op for data lump (%x)\n", r->op);
852                 }
853                 for (r=t->after;r;r=r->after){
854                         switch(r->op){
855                                 case LUMP_ADD:
856                                         new_len+=r->len;
857                                         break;
858                                 case LUMP_ADD_SUBST:
859                                         SUBST_LUMP_LEN(r);
860                                         break;
861                                 case LUMP_ADD_OPT:
862                                         /* skip if this is an OPT lump and the condition is
863                                          * not satisfied */
864                                         if (!lump_check_opt(r, msg, send_info))
865                                                 goto skip_after;
866                                         break;
867                                 default:
868                                         /* only ADD allowed for before/after */
869                                         LOG(L_CRIT, "BUG:lumps_len: invalid"
870                                                                 " op for data lump (%x)\n", r->op);
871                         }
872                 }
873 skip_after:
874                 ; /* to make gcc 3.* happy */
875         }
876         return new_len;
877 #undef RCVCOMP_LUMP_LEN
878 #undef SENDCOMP_LUMP_LEN
879 }
880
881
882
883 /* another helper functions, adds/Removes the lump,
884         code moved form build_req_from_req  */
885
886 static inline void process_lumps(       struct sip_msg* msg,
887                                                                         struct lump* lumps,
888                                                                         char* new_buf,
889                                                                         unsigned int* new_buf_offs,
890                                                                         unsigned int* orig_offs,
891                                                                         struct dest_info* send_info)
892 {
893         struct lump *t;
894         struct lump *r;
895         char* orig;
896         int size;
897         int offset;
898         int s_offset;
899         str* send_address_str;
900         str* send_port_str;
901         struct socket_info* send_sock;
902
903 #ifdef USE_COMP
904         #define RCVCOMP_PARAM_ADD \
905                                 /* add ;comp=xxxx */ \
906                                 switch(msg->rcv.comp){ \
907                                         case COMP_NONE: \
908                                                 break; \
909                                         case COMP_SIGCOMP: \
910                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
911                                                 offset+=COMP_PARAM_LEN; \
912                                                 memcpy(new_buf+offset, SIGCOMP_NAME, \
913                                                                 SIGCOMP_NAME_LEN); \
914                                                 offset+=SIGCOMP_NAME_LEN; \
915                                                 break; \
916                                         case COMP_SERGZ: \
917                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
918                                                 offset+=COMP_PARAM_LEN; \
919                                                 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
920                                                 offset+=SERGZ_NAME_LEN; \
921                                                 break;\
922                                         default:\
923                                                 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
924                                                                 msg->rcv.comp); \
925                                 }
926         
927         #define SENDCOMP_PARAM_ADD \
928                                 /* add ;comp=xxxx */ \
929                                 switch(send_info->comp){ \
930                                         case COMP_NONE: \
931                                                 break; \
932                                         case COMP_SIGCOMP: \
933                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
934                                                 offset+=COMP_PARAM_LEN; \
935                                                 memcpy(new_buf+offset, SIGCOMP_NAME, \
936                                                                 SIGCOMP_NAME_LEN); \
937                                                 offset+=SIGCOMP_NAME_LEN; \
938                                                 break; \
939                                         case COMP_SERGZ: \
940                                                 memcpy(new_buf+offset, COMP_PARAM, COMP_PARAM_LEN);\
941                                                 offset+=COMP_PARAM_LEN; \
942                                                 memcpy(new_buf+offset, SERGZ_NAME, SERGZ_NAME_LEN); \
943                                                 offset+=SERGZ_NAME_LEN; \
944                                                 break;\
945                                         default:\
946                                                 LOG(L_CRIT, "BUG: process_lumps: unknown comp %d\n", \
947                                                                 msg->rcv.comp); \
948                                 } 
949 #else
950         #define RCVCOMP_PARAM_ADD
951         #define SENDCOMP_PARAM_ADD
952 #endif /* USE_COMP */
953
954 #define SUBST_LUMP(subst_l) \
955         switch((subst_l)->u.subst){ \
956                 case SUBST_RCV_IP: \
957                         if (msg->rcv.bind_address){  \
958                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
959                                         new_buf[offset]='['; offset++; \
960                                 }\
961                                 memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
962                                                 msg->rcv.bind_address->address_str.len); \
963                                 offset+=msg->rcv.bind_address->address_str.len; \
964                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
965                                         new_buf[offset]=']'; offset++; \
966                                 }\
967                         }else{  \
968                                 /*FIXME*/ \
969                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
970                         }; \
971                         break; \
972                 case SUBST_RCV_PORT: \
973                         if (msg->rcv.bind_address){  \
974                                 memcpy(new_buf+offset, msg->rcv.bind_address->port_no_str.s, \
975                                                 msg->rcv.bind_address->port_no_str.len); \
976                                 offset+=msg->rcv.bind_address->port_no_str.len; \
977                         }else{  \
978                                 /*FIXME*/ \
979                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
980                         }; \
981                         break; \
982                 case SUBST_RCV_ALL: \
983                         if (msg->rcv.bind_address){  \
984                                 /* address */ \
985                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
986                                         new_buf[offset]='['; offset++; \
987                                 }\
988                                 memcpy(new_buf+offset, msg->rcv.bind_address->address_str.s, \
989                                                 msg->rcv.bind_address->address_str.len); \
990                                 offset+=msg->rcv.bind_address->address_str.len; \
991                                 if (msg->rcv.bind_address->address.af!=AF_INET){\
992                                         new_buf[offset]=']'; offset++; \
993                                 }\
994                                 /* :port */ \
995                                 if (msg->rcv.bind_address->port_no!=SIP_PORT){ \
996                                         new_buf[offset]=':'; offset++; \
997                                         memcpy(new_buf+offset, \
998                                                         msg->rcv.bind_address->port_no_str.s, \
999                                                         msg->rcv.bind_address->port_no_str.len); \
1000                                         offset+=msg->rcv.bind_address->port_no_str.len; \
1001                                 }\
1002                                 switch(msg->rcv.bind_address->proto){ \
1003                                         case PROTO_NONE: \
1004                                         case PROTO_UDP: \
1005                                                 break; /* nothing to do, udp is default*/ \
1006                                         case PROTO_TCP: \
1007                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1008                                                                 TRANSPORT_PARAM_LEN); \
1009                                                 offset+=TRANSPORT_PARAM_LEN; \
1010                                                 memcpy(new_buf+offset, "tcp", 3); \
1011                                                 offset+=3; \
1012                                                 break; \
1013                                         case PROTO_TLS: \
1014                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1015                                                                 TRANSPORT_PARAM_LEN); \
1016                                                 offset+=TRANSPORT_PARAM_LEN; \
1017                                                 memcpy(new_buf+offset, "tls", 3); \
1018                                                 offset+=3; \
1019                                                 break; \
1020                                         case PROTO_SCTP: \
1021                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1022                                                                 TRANSPORT_PARAM_LEN); \
1023                                                 offset+=TRANSPORT_PARAM_LEN; \
1024                                                 memcpy(new_buf+offset, "sctp", 4); \
1025                                                 offset+=4; \
1026                                                 break; \
1027                                         default: \
1028                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1029                                                                 msg->rcv.bind_address->proto); \
1030                                 } \
1031                                 RCVCOMP_PARAM_ADD \
1032                         }else{  \
1033                                 /*FIXME*/ \
1034                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1035                         }; \
1036                         break; \
1037                 case SUBST_SND_IP: \
1038                         if (send_sock){  \
1039                                 if ((send_sock->address.af!=AF_INET) && \
1040                                                 (send_address_str==&(send_sock->address_str))){\
1041                                         new_buf[offset]='['; offset++; \
1042                                 }\
1043                                 memcpy(new_buf+offset, send_address_str->s, \
1044                                                                         send_address_str->len); \
1045                                 offset+=send_address_str->len; \
1046                                 if ((send_sock->address.af!=AF_INET) && \
1047                                                 (send_address_str==&(send_sock->address_str))){\
1048                                         new_buf[offset]=']'; offset++; \
1049                                 }\
1050                         }else{  \
1051                                 /*FIXME*/ \
1052                                 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1053                                                         " null send_sock\n"); \
1054                         }; \
1055                         break; \
1056                 case SUBST_SND_PORT: \
1057                         if (send_sock){  \
1058                                 memcpy(new_buf+offset, send_port_str->s, \
1059                                                                         send_port_str->len); \
1060                                 offset+=send_port_str->len; \
1061                         }else{  \
1062                                 /*FIXME*/ \
1063                                 LOG(L_CRIT, "FIXME: process_lumps: called with" \
1064                                                 " null send_sock\n"); \
1065                         }; \
1066                         break; \
1067                 case SUBST_SND_ALL: \
1068                         if (send_sock){  \
1069                                 /* address */ \
1070                                 if ((send_sock->address.af!=AF_INET) && \
1071                                                 (send_address_str==&(send_sock->address_str))){\
1072                                         new_buf[offset]='['; offset++; \
1073                                 }\
1074                                 memcpy(new_buf+offset, send_address_str->s, \
1075                                                 send_address_str->len); \
1076                                 offset+=send_address_str->len; \
1077                                 if ((send_sock->address.af!=AF_INET) && \
1078                                                 (send_address_str==&(send_sock->address_str))){\
1079                                         new_buf[offset]=']'; offset++; \
1080                                 }\
1081                                 /* :port */ \
1082                                 if ((send_sock->port_no!=SIP_PORT) || \
1083                                         (send_port_str!=&(send_sock->port_no_str))){ \
1084                                         new_buf[offset]=':'; offset++; \
1085                                         memcpy(new_buf+offset, send_port_str->s, \
1086                                                         send_port_str->len); \
1087                                         offset+=send_port_str->len; \
1088                                 }\
1089                                 switch(send_sock->proto){ \
1090                                         case PROTO_NONE: \
1091                                         case PROTO_UDP: \
1092                                                 break; /* nothing to do, udp is default*/ \
1093                                         case PROTO_TCP: \
1094                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1095                                                                 TRANSPORT_PARAM_LEN); \
1096                                                 offset+=TRANSPORT_PARAM_LEN; \
1097                                                 memcpy(new_buf+offset, "tcp", 3); \
1098                                                 offset+=3; \
1099                                                 break; \
1100                                         case PROTO_TLS: \
1101                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1102                                                                 TRANSPORT_PARAM_LEN); \
1103                                                 offset+=TRANSPORT_PARAM_LEN; \
1104                                                 memcpy(new_buf+offset, "tls", 3); \
1105                                                 offset+=3; \
1106                                                 break; \
1107                                         case PROTO_SCTP: \
1108                                                 memcpy(new_buf+offset, TRANSPORT_PARAM, \
1109                                                                 TRANSPORT_PARAM_LEN); \
1110                                                 offset+=TRANSPORT_PARAM_LEN; \
1111                                                 memcpy(new_buf+offset, "sctp", 4); \
1112                                                 offset+=4; \
1113                                                 break; \
1114                                         default: \
1115                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1116                                                                 send_sock->proto); \
1117                                 } \
1118                                 SENDCOMP_PARAM_ADD \
1119                         }else{  \
1120                                 /*FIXME*/ \
1121                                 LOG(L_CRIT, "FIXME: process_lumps: null bind_address\n"); \
1122                         }; \
1123                         break; \
1124                 case SUBST_RCV_PROTO: \
1125                         if (msg->rcv.bind_address){ \
1126                                 switch(msg->rcv.bind_address->proto){ \
1127                                         case PROTO_NONE: \
1128                                         case PROTO_UDP: \
1129                                                 memcpy(new_buf+offset, "udp", 3); \
1130                                                 offset+=3; \
1131                                                 break; \
1132                                         case PROTO_TCP: \
1133                                                 memcpy(new_buf+offset, "tcp", 3); \
1134                                                 offset+=3; \
1135                                                 break; \
1136                                         case PROTO_TLS: \
1137                                                 memcpy(new_buf+offset, "tls", 3); \
1138                                                 offset+=3; \
1139                                                 break; \
1140                                         case PROTO_SCTP: \
1141                                                 memcpy(new_buf+offset, "sctp", 4); \
1142                                                 offset+=4; \
1143                                                 break; \
1144                                         default: \
1145                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1146                                                                 msg->rcv.bind_address->proto); \
1147                                 } \
1148                         }else{  \
1149                                 /*FIXME*/ \
1150                                 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1151                                                         " send_sock \n"); \
1152                         }; \
1153                         break; \
1154                 case  SUBST_SND_PROTO: \
1155                         if (send_sock){ \
1156                                 switch(send_sock->proto){ \
1157                                         case PROTO_NONE: \
1158                                         case PROTO_UDP: \
1159                                                 memcpy(new_buf+offset, "udp", 3); \
1160                                                 offset+=3; \
1161                                                 break; \
1162                                         case PROTO_TCP: \
1163                                                 memcpy(new_buf+offset, "tcp", 3); \
1164                                                 offset+=3; \
1165                                                 break; \
1166                                         case PROTO_TLS: \
1167                                                 memcpy(new_buf+offset, "tls", 3); \
1168                                                 offset+=3; \
1169                                                 break; \
1170                                         case PROTO_SCTP: \
1171                                                 memcpy(new_buf+offset, "sctp", 4); \
1172                                                 offset+=4; \
1173                                                 break; \
1174                                         default: \
1175                                                 LOG(L_CRIT, "BUG: process_lumps: unknown proto %d\n", \
1176                                                                 send_sock->proto); \
1177                                 } \
1178                         }else{  \
1179                                 /*FIXME*/ \
1180                                 LOG(L_CRIT, "FIXME: process_lumps: called with null" \
1181                                                         " send_sock \n"); \
1182                         }; \
1183                         break; \
1184                 default: \
1185                                 LOG(L_CRIT, "BUG: process_lumps: unknown subst type %d\n", \
1186                                                         (subst_l)->u.subst); \
1187         }
1188
1189         if (send_info){
1190                 send_sock=send_info->send_sock;
1191         }else{
1192                 send_sock=0;
1193         }
1194         /* init send_address_str & send_port_str */
1195         if (msg->set_global_address.len)
1196                 send_address_str=&(msg->set_global_address);
1197         else
1198                 send_address_str=&(send_sock->address_str);
1199         if (msg->set_global_port.len)
1200                 send_port_str=&(msg->set_global_port);
1201         else
1202                 send_port_str=&(send_sock->port_no_str);
1203
1204
1205         orig=msg->buf;
1206         offset=*new_buf_offs;
1207         s_offset=*orig_offs;
1208
1209         for (t=lumps;t;t=t->next){
1210                 switch(t->op){
1211                         case LUMP_ADD:
1212                         case LUMP_ADD_SUBST:
1213                         case LUMP_ADD_OPT:
1214                                 /* skip if this is an OPT lump and the condition is
1215                                  * not satisfied */
1216                                 if ((t->op==LUMP_ADD_OPT) &&
1217                                                 (!lump_check_opt(t, msg, send_info)))
1218                                         continue;
1219                                 /* just add it here! */
1220                                 /* process before  */
1221                                 for(r=t->before;r;r=r->before){
1222                                         switch (r->op){
1223                                                 case LUMP_ADD:
1224                                                         /*just add it here*/
1225                                                         memcpy(new_buf+offset, r->u.value, r->len);
1226                                                         offset+=r->len;
1227                                                         break;
1228                                                 case LUMP_ADD_SUBST:
1229                                                         SUBST_LUMP(r);
1230                                                         break;
1231                                                 case LUMP_ADD_OPT:
1232                                                         /* skip if this is an OPT lump and the condition is
1233                                                         * not satisfied */
1234                                                         if (!lump_check_opt(r, msg, send_info))
1235                                                                 goto skip_before;
1236                                                         break;
1237                                                 default:
1238                                                         /* only ADD allowed for before/after */
1239                                                         LOG(L_CRIT, "BUG:process_lumps: "
1240                                                                         "invalid op for data lump (%x)\n", r->op);
1241                                         }
1242                                 }
1243 skip_before:
1244                                 /* copy "main" part */
1245                                 switch(t->op){
1246                                         case LUMP_ADD:
1247                                                 memcpy(new_buf+offset, t->u.value, t->len);
1248                                                 offset+=t->len;
1249                                                 break;
1250                                         case LUMP_ADD_SUBST:
1251                                                 SUBST_LUMP(t);
1252                                                 break;
1253                                         case LUMP_ADD_OPT:
1254                                                 /* do nothing, it's only a condition */
1255                                                 break;
1256                                         default:
1257                                                 /* should not ever get here */
1258                                                 LOG(L_CRIT, "BUG: process_lumps: unhandled data lump "
1259                                                                 " op %d\n", t->op);
1260                                 }
1261                                 /* process after */
1262                                 for(r=t->after;r;r=r->after){
1263                                         switch (r->op){
1264                                                 case LUMP_ADD:
1265                                                         /*just add it here*/
1266                                                         memcpy(new_buf+offset, r->u.value, r->len);
1267                                                         offset+=r->len;
1268                                                         break;
1269                                                 case LUMP_ADD_SUBST:
1270                                                         SUBST_LUMP(r);
1271                                                         break;
1272                                                 case LUMP_ADD_OPT:
1273                                                         /* skip if this is an OPT lump and the condition is
1274                                                         * not satisfied */
1275                                                         if (!lump_check_opt(r, msg, send_info))
1276                                                                 goto skip_after;
1277                                                         break;
1278                                                 default:
1279                                                         /* only ADD allowed for before/after */
1280                                                         LOG(L_CRIT, "BUG:process_lumps: "
1281                                                                         "invalid op for data lump (%x)\n", r->op);
1282                                         }
1283                                 }
1284 skip_after:
1285                                 break;
1286                         case LUMP_NOP:
1287                         case LUMP_DEL:
1288                                 /* copy till offset */
1289                                 if (s_offset>t->u.offset){
1290                                         DBG("Warning: (%d) overlapped lumps offsets,"
1291                                                 " ignoring(%x, %x)\n", t->op, s_offset,t->u.offset);
1292                                         /* this should've been fixed above (when computing len) */
1293                                         /* just ignore it*/
1294                                         break;
1295                                 }
1296                                 size=t->u.offset-s_offset;
1297                                 if (size){
1298                                         memcpy(new_buf+offset, orig+s_offset,size);
1299                                         offset+=size;
1300                                         s_offset+=size;
1301                                 }
1302                                 /* process before  */
1303                                 for(r=t->before;r;r=r->before){
1304                                         switch (r->op){
1305                                                 case LUMP_ADD:
1306                                                         /*just add it here*/
1307                                                         memcpy(new_buf+offset, r->u.value, r->len);
1308                                                         offset+=r->len;
1309                                                         break;
1310                                                 case LUMP_ADD_SUBST:
1311                                                         SUBST_LUMP(r);
1312                                                         break;
1313                                                 case LUMP_ADD_OPT:
1314                                                         /* skip if this is an OPT lump and the condition is
1315                                                         * not satisfied */
1316                                                         if (!lump_check_opt(r, msg, send_info))
1317                                                                 goto skip_nop_before;
1318                                                         break;
1319                                                 default:
1320                                                         /* only ADD allowed for before/after */
1321                                                         LOG(L_CRIT, "BUG:process_lumps: "
1322                                                                         "invalid op for data lump (%x)\n",r->op);
1323                                         }
1324                                 }
1325 skip_nop_before:
1326                                 /* process main (del only) */
1327                                 if (t->op==LUMP_DEL){
1328                                         /* skip len bytes from orig msg */
1329                                         s_offset+=t->len;
1330                                 }
1331                                 /* process after */
1332                                 for(r=t->after;r;r=r->after){
1333                                         switch (r->op){
1334                                                 case LUMP_ADD:
1335                                                         /*just add it here*/
1336                                                         memcpy(new_buf+offset, r->u.value, r->len);
1337                                                         offset+=r->len;
1338                                                         break;
1339                                                 case LUMP_ADD_SUBST:
1340                                                         SUBST_LUMP(r);
1341                                                         break;
1342                                                 case LUMP_ADD_OPT:
1343                                                         /* skip if this is an OPT lump and the condition is
1344                                                         * not satisfied */
1345                                                         if (!lump_check_opt(r, msg, send_info))
1346                                                                 goto skip_nop_after;
1347                                                         break;
1348                                                 default:
1349                                                         /* only ADD allowed for before/after */
1350                                                         LOG(L_CRIT, "BUG:process_lumps: "
1351                                                                         "invalid op for data lump (%x)\n", r->op);
1352                                         }
1353                                 }
1354 skip_nop_after:
1355                                 break;
1356                         default:
1357                                         LOG(L_CRIT, "BUG: process_lumps: "
1358                                                         "unknown op (%x)\n", t->op);
1359                 }
1360         }
1361         *new_buf_offs=offset;
1362         *orig_offs=s_offset;
1363 #undef RCVCOMP_PARAM_ADD 
1364 #undef SENDCOMP_PARAM_ADD
1365 }
1366
1367
1368 /*
1369  * Adjust/insert Content-Length if necessary
1370  */
1371 static inline int adjust_clen(struct sip_msg* msg, int body_delta, int proto)
1372 {
1373         struct lump* anchor;
1374         char* clen_buf;
1375         int clen_len, body_only;
1376 #ifdef USE_TCP
1377         char* body;
1378         int comp_clen;
1379 #endif /* USE_TCP */
1380
1381         /* Calculate message length difference caused by lumps modifying message
1382          * body, from this point on the message body must not be modified. Zero
1383          * value indicates that the body hasn't been modified
1384         */
1385
1386         clen_buf = 0;
1387         anchor=0;
1388         body_only=1;
1389
1390         /* check to see if we need to add clen */
1391 #ifdef USE_TCP
1392         if (proto == PROTO_TCP
1393 #ifdef USE_TLS
1394             || proto == PROTO_TLS
1395 #endif
1396             ) {
1397                 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0)==-1){
1398                         LOG(L_ERR, "adjust_clen: error parsing content-length\n");
1399                         goto error;
1400                 }
1401                 if (unlikely(msg->content_length==0)){
1402                         /* not present, we need to add it */
1403                         /* msg->unparsed should point just before the final crlf
1404                          * - whole message was parsed by the above parse_headers
1405                          *   which did not find content-length */
1406                         anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1407                                                                                                 HDR_CONTENTLENGTH_T);
1408                         if (anchor==0){
1409                                 LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1410                                 goto error;
1411                         }
1412                         body_only=0;
1413                 }else{
1414                         /* compute current content length and compare it with the
1415                            one in the message */
1416                         body=get_body(msg);
1417                         if (unlikely(body==0)){
1418                                 ser_error=E_BAD_REQ;
1419                                 LOG(L_ERR, "adjust_clen: no message body found"
1420                                                 " (missing crlf?)");
1421                                 goto error;
1422                         }
1423                         comp_clen=msg->len-(int)(body-msg->buf)+body_delta;
1424                         if (comp_clen!=(int)(long)msg->content_length->parsed){
1425                                 /* note: we don't distinguish here between received with
1426                                    wrong content-length and content-length changed, we just
1427                                    fix it automatically in both cases (the reason being
1428                                    that an error message telling we have received a msg-
1429                                    with wrong content-length is of very little use) */
1430                                 anchor = del_lump(msg, msg->content_length->body.s-msg->buf,
1431                                                                         msg->content_length->body.len,
1432                                                                         HDR_CONTENTLENGTH_T);
1433                                 if (anchor==0) {
1434                                         LOG(L_ERR, "adjust_clen: Can't remove original"
1435                                                                 " Content-Length\n");
1436                                         goto error;
1437                                 }
1438                                 body_only=1;
1439                         }
1440                 }
1441         }else
1442 #endif /* USE_TCP */
1443         if (body_delta){
1444                 if (parse_headers(msg, HDR_CONTENTLENGTH_F, 0) == -1) {
1445                         LOG(L_ERR, "adjust_clen: Error parsing Content-Length\n");
1446                         goto error;
1447                 }
1448
1449                 /* The body has been changed, try to find
1450                  * existing Content-Length
1451                  */
1452                 /* no need for Content-Length if it's and UDP packet and
1453                  * it hasn't Content-Length already */
1454                 if ((msg->content_length==0)){
1455                     /* content-length doesn't exist, append it */
1456                         /* msg->unparsed should point just before the final crlf
1457                          * - whole message was parsed by the above parse_headers
1458                          *   which did not find content-length */
1459                         if (proto!=PROTO_UDP){
1460                                 anchor=anchor_lump(msg, msg->unparsed-msg->buf, 0,
1461                                                                                                         HDR_CONTENTLENGTH_T);
1462                                 if (anchor==0){
1463                                         LOG(L_ERR, "adjust_clen: cannot set clen anchor\n");
1464                                         goto error;
1465                                 }
1466                                 body_only=0;
1467                         } /* else
1468                                 DBG("adjust_clen: UDP packet with no clen => "
1469                                                 "not adding one \n"); */
1470                 }else{
1471                         /* Content-Length has been found, remove it */
1472                         anchor = del_lump(      msg, msg->content_length->body.s - msg->buf,
1473                                                                 msg->content_length->body.len,
1474                                                                 HDR_CONTENTLENGTH_T);
1475                         if (anchor==0) {
1476                                 LOG(L_ERR, "adjust_clen: Can't remove original"
1477                                                         " Content-Length\n");
1478                                 goto error;
1479                         }
1480                 }
1481         }
1482
1483         if (anchor){
1484                 clen_buf = clen_builder(msg, &clen_len, body_delta, body_only);
1485                 if (!clen_buf) goto error;
1486                 if (insert_new_lump_after(anchor, clen_buf, clen_len,
1487                                         HDR_CONTENTLENGTH_T) == 0)
1488                         goto error;
1489         }
1490         
1491         return 0;
1492 error:
1493         if (clen_buf) pkg_free(clen_buf);
1494         return -1;
1495 }
1496
1497
1498
1499 /** builds a request in memory from another sip request.
1500   *
1501   * Side-effects: - it adds lumps to the msg which are _not_ cleaned.
1502   * The added lumps are HDR_VIA_T (almost always added), HDR_CONTENLENGTH_T
1503   * and HDR_ROUTE_T (when a Route: header is added as a result of a non-null
1504   * msg->path_vec).
1505   *               - it might change send_info->proto and send_info->send_socket
1506   *                 if proto fallback is enabled (see below).
1507   *
1508   * Uses also global_req_flags ( OR'ed with msg->msg_flags, see send_info
1509   * below).
1510   *
1511   * @param msg  - sip message structure, complete with lumps
1512   * @param returned_len - result length (filled in)
1513   * @param send_info  - dest_info structure (value/result), contains where the
1514   *                     packet will be sent to (it's needed for building a 
1515   *                     correct via, fill RR lumps a.s.o.). If MTU based
1516   *                     protocol fall-back is enabled (see flags below),
1517   *                     send_info->proto might be updated with the new
1518   *                     protocol.
1519   *                     msg->msg_flags used:
1520   *                     - FL_TCP_MTU_FB, FL_TLS_MTU_FB and FL_SCTP_MTU_FB -
1521   *                       fallback to the corresp. proto if the built 
1522   *                       message > mtu and send_info->proto==PROTO_UDP. 
1523   *                       It will also update send_info->proto.
1524   *                     - FL_FORCE_RPORT: add rport to via
1525   * @param mode - flags for building the message, can be a combination of:
1526   *                 * BUILD_NO_LOCAL_VIA - don't add a local via
1527   *                 * BUILD_NO_VIA1_UPDATE - don't update first via (rport,
1528   *                    received a.s.o)
1529   *                 * BUILD_NO_PATH - don't add a Route: header with the 
1530   *                   msg->path_vec content.
1531   *                 * BUILD_IN_SHM - build the result in shm memory
1532   *
1533   * @return pointer to the new request (pkg_malloc'ed or shm_malloc'ed,
1534   * depending on the presence of the BUILD_IN_SHM flag, needs freeing when
1535   *   done) and sets returned_len or 0 on error.
1536   */
1537 char * build_req_buf_from_sip_req( struct sip_msg* msg,
1538                                                                 unsigned int *returned_len,
1539                                                                 struct dest_info* send_info,
1540                                                                 unsigned int mode)
1541 {
1542         unsigned int len, new_len, received_len, rport_len, uri_len, via_len,
1543                                  body_delta;
1544         char* line_buf;
1545         char* received_buf;
1546         char* rport_buf;
1547         char* new_buf;
1548         char* buf;
1549         str path_buf;
1550         unsigned int offset, s_offset, size;
1551         struct lump* via_anchor;
1552         struct lump* via_lump;
1553         struct lump* via_insert_param;
1554         struct lump* path_anchor;
1555         struct lump* path_lump;
1556         str branch;
1557         unsigned int flags;
1558         unsigned int udp_mtu;
1559         struct dest_info di;
1560
1561         via_insert_param=0;
1562         uri_len=0;
1563         buf=msg->buf;
1564         len=msg->len;
1565         received_len=0;
1566         rport_len=0;
1567         new_buf=0;
1568         received_buf=0;
1569         rport_buf=0;
1570         via_anchor=0;
1571         line_buf=0;
1572         via_len=0;
1573         path_buf.s=0;
1574         path_buf.len=0;
1575
1576         flags=msg->msg_flags|global_req_flags;
1577         /* Calculate message body difference and adjust Content-Length */
1578         body_delta = lumps_len(msg, msg->body_lumps, send_info);
1579         if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
1580                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1581                                 " Content-Length\n");
1582                 goto error00;
1583         }
1584
1585         if(unlikely(mode&BUILD_NO_LOCAL_VIA))
1586                 goto after_local_via;
1587
1588         /* create the via header */
1589         branch.s=msg->add_to_branch_s;
1590         branch.len=msg->add_to_branch_len;
1591
1592         via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
1593         if (unlikely(via_anchor==0)) goto error00;
1594         line_buf = create_via_hf( &via_len, msg, send_info, &branch);
1595         if (unlikely(!line_buf)){
1596                 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
1597                                         "memory allocation failure\n");
1598                 goto error00;
1599         }
1600 after_local_via:
1601         if(unlikely(mode&BUILD_NO_VIA1_UPDATE))
1602                 goto after_update_via1;
1603         /* check if received needs to be added */
1604         if ( received_test(msg) ) {
1605                 if ((received_buf=received_builder(msg,&received_len))==0){
1606                         LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1607                                                         " received_builder failed\n");
1608                         goto error01;  /* free also line_buf */
1609                 }
1610         }
1611
1612         /* check if rport needs to be updated:
1613          *  - if FL_FORCE_RPORT is set add it (and del. any previous version)
1614          *  - if via already contains an rport add it and overwrite the previous
1615          *  rport value if present (if you don't want to overwrite the previous
1616          *  version remove the comments) */
1617         if ((flags&FL_FORCE_RPORT)||
1618                         (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1619                 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1620                         LOG(L_ERR, "ERROR: build_req_buf_from_sip_req:"
1621                                                         " rport_builder failed\n");
1622                         goto error01; /* free everything */
1623                 }
1624         }
1625
1626         /* find out where the offset of the first parameter that should be added
1627          * (after host:port), needed by add receive & maybe rport */
1628         if (msg->via1->params.s){
1629                         size= msg->via1->params.s-msg->via1->hdr.s-1; /*compensate
1630                                                                                                                   for ';' */
1631         }else{
1632                         size= msg->via1->host.s-msg->via1->hdr.s+msg->via1->host.len;
1633                         if (msg->via1->port!=0){
1634                                 /*size+=strlen(msg->via1->hdr.s+size+1)+1;*/
1635                                 size += msg->via1->port_str.len + 1; /* +1 for ':'*/
1636                         }
1637 #if 0
1638                         /* no longer necessary, now hots.s contains [] */
1639                 #ifdef USE_IPV6
1640                         if(send_sock->address.af==AF_INET6) size+=1; /* +1 for ']'*/
1641                 #endif
1642 #endif
1643         }
1644         /* if received needs to be added, add anchor after host and add it, or
1645          * overwrite the previous one if already present */
1646         if (received_len){
1647                 if (msg->via1->received){ /* received already present => overwrite it*/
1648                         via_insert_param=del_lump(msg,
1649                                                                 msg->via1->received->start-buf-1, /*;*/
1650                                                                 msg->via1->received->size+1, /*;*/ HDR_VIA_T);
1651                 }else if (via_insert_param==0){ /* receive not present, ok */
1652                         via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1653                                                                                         HDR_VIA_T);
1654                 }
1655                 if (via_insert_param==0) goto error02; /* free received_buf */
1656                 if (insert_new_lump_after(via_insert_param, received_buf, received_len,
1657                                         HDR_VIA_T) ==0 ) goto error02; /* free received_buf */
1658         }
1659         /* if rport needs to be updated, delete it if present and add it's value */
1660         if (rport_len){
1661                 if (msg->via1->rport){ /* rport already present */
1662                         via_insert_param=del_lump(msg,
1663                                                                 msg->via1->rport->start-buf-1, /*';'*/
1664                                                                 msg->via1->rport->size+1 /* ; */, HDR_VIA_T);
1665                 }else if (via_insert_param==0){ /*force rport, no rport present */
1666                         /* no rport, add it */
1667                         via_insert_param=anchor_lump(msg, msg->via1->hdr.s-buf+size, 0,
1668                                                                                         HDR_VIA_T);
1669                 }
1670                 if (via_insert_param==0) goto error03; /* free rport_buf */
1671                 if (insert_new_lump_after(via_insert_param, rport_buf, rport_len,
1672                                                                         HDR_VIA_T) ==0 )
1673                         goto error03; /* free rport_buf */
1674
1675         }
1676
1677 after_update_via1:
1678         /* add route with path content */
1679         if(unlikely(!(mode&BUILD_NO_PATH) && msg->path_vec.s &&
1680                                         msg->path_vec.len)){
1681                 path_buf.len=ROUTE_PREFIX_LEN+msg->path_vec.len+CRLF_LEN;
1682                 path_buf.s=pkg_malloc(path_buf.len+1);
1683                 if (unlikely(path_buf.s==0)){
1684                         LOG(L_ERR, "out of memory\n");
1685                         ser_error=E_OUT_OF_MEM;
1686                         goto error00;
1687                 }
1688                 memcpy(path_buf.s, ROUTE_PREFIX, ROUTE_PREFIX_LEN);
1689                 memcpy(path_buf.s+ROUTE_PREFIX_LEN, msg->path_vec.s,
1690                                         msg->path_vec.len);
1691                 memcpy(path_buf.s+ROUTE_PREFIX_LEN+msg->path_vec.len, CRLF, CRLF_LEN);
1692                 path_buf.s[path_buf.len]=0;
1693                 /* insert Route header either before the other routes
1694                    (if present & parsed), after the local via or after in front of
1695                     the first via if we don't add a local via*/
1696                 if (msg->route){
1697                         path_anchor=anchor_lump(msg, msg->route->name.s-buf, 0, 
1698                                                                         HDR_ROUTE_T);
1699                 }else if (likely(via_anchor)){
1700                         path_anchor=via_anchor;
1701                 }else if (likely(msg->via1)){
1702                         path_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, 
1703                                                                         HDR_ROUTE_T);
1704                 }else{
1705                         /* if no via1 (theoretically possible for non-sip messages,
1706                            e.g. http xmlrpc) */
1707                         path_anchor=anchor_lump(msg, msg->headers->name.s-buf, 0, 
1708                                                                         HDR_ROUTE_T);
1709                 }
1710                 if (unlikely(path_anchor==0))
1711                         goto error05;
1712                 if (unlikely((path_lump=insert_new_lump_after(path_anchor, path_buf.s,
1713                                                                                                                 path_buf.len,
1714                                                                                                                 HDR_ROUTE_T))==0))
1715                         goto error05;
1716         }
1717         /* compute new msg len and fix overlapping zones*/
1718         new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
1719 #ifdef XL_DEBUG
1720         LOG(L_ERR, "DEBUG: new_len(%d)=len(%d)+lumps_len\n", new_len, len);
1721 #endif
1722         udp_mtu=cfg_get(core, core_cfg, udp_mtu);
1723         di.proto=PROTO_NONE;
1724         if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu && 
1725                                         (flags & FL_MTU_FB_MASK) && (new_len>udp_mtu)
1726                                         && (!(mode&BUILD_NO_LOCAL_VIA)))){
1727
1728                 di=*send_info; /* copy whole struct - will be used in the Via builder */
1729                 di.proto=PROTO_NONE; /* except the proto */
1730 #ifdef USE_TCP
1731                 if (!tcp_disable && (flags & FL_MTU_TCP_FB) &&
1732                                 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TCP))){
1733                         di.proto=PROTO_TCP;
1734                 }
1735         #ifdef USE_TLS
1736                 else if (!tls_disable && (flags & FL_MTU_TLS_FB) &&
1737                                 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_TLS))){
1738                         di.proto=PROTO_TLS;
1739                 }
1740         #endif /* USE_TLS */
1741 #endif /* USE_TCP */
1742 #ifdef USE_SCTP
1743         #ifdef USE_TCP
1744                 else
1745         #endif /* USE_TCP */
1746                  if (!sctp_disable && (flags & FL_MTU_SCTP_FB) &&
1747                                 (di.send_sock=get_send_socket(msg, &send_info->to, PROTO_SCTP))){
1748                         di.proto=PROTO_SCTP;
1749                  }
1750 #endif /* USE_SCTP */
1751                 
1752                 if (di.proto!=PROTO_NONE){
1753                         new_len-=via_len;
1754                         if(likely(line_buf)) pkg_free(line_buf);
1755                         line_buf = create_via_hf( &via_len, msg, &di, &branch);
1756                         if (!line_buf){
1757                                 LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
1758                                                         "memory allocation failure!\n");
1759                                 goto error00;
1760                         }
1761                         new_len+=via_len;
1762                 }
1763         }
1764         /* add via header to the list */
1765         /* try to add it before msg. 1st via */
1766         /* add first via, as an anchor for second via*/
1767         if(likely(line_buf)) {
1768                 if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
1769                                                                                         HDR_VIA_T))==0)
1770                         goto error04;
1771         }
1772         if (msg->new_uri.s){
1773                 uri_len=msg->new_uri.len;
1774                 new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
1775         }
1776         if(unlikely(mode&BUILD_IN_SHM))
1777                 new_buf=(char*)shm_malloc(new_len+1);
1778         else
1779                 new_buf=(char*)pkg_malloc(new_len+1);
1780         if (new_buf==0){
1781                 ser_error=E_OUT_OF_MEM;
1782                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
1783                 goto error00;
1784         }
1785
1786         offset=s_offset=0;
1787         if (msg->new_uri.s){
1788                 /* copy message up to uri */
1789                 size=msg->first_line.u.request.uri.s-buf;
1790                 memcpy(new_buf, buf, size);
1791                 offset+=size;
1792                 s_offset+=size;
1793                 /* add our uri */
1794                 memcpy(new_buf+offset, msg->new_uri.s, uri_len);
1795                 offset+=uri_len;
1796                 s_offset+=msg->first_line.u.request.uri.len; /* skip original uri */
1797         }
1798         new_buf[new_len]=0;
1799         /* copy msg adding/removing lumps */
1800         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
1801         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset,send_info);
1802         /* copy the rest of the message */
1803         memcpy(new_buf+offset, buf+s_offset, len-s_offset);
1804         new_buf[new_len]=0;
1805
1806         /* update the send_info if udp_mtu affected */
1807         if (di.proto!=PROTO_NONE) { 
1808                 send_info->proto=di.proto;
1809                 send_info->send_sock=di.send_sock;
1810         }
1811
1812 #ifdef DBG_MSG_QA
1813         if (new_buf[new_len-1]==0) {
1814                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: 0 in the end\n");
1815                 abort();
1816         }
1817 #endif
1818
1819         *returned_len=new_len;
1820         return new_buf;
1821
1822 error01:
1823 error02:
1824         if (received_buf) pkg_free(received_buf);
1825 error03:
1826         if (rport_buf) pkg_free(rport_buf);
1827 error04:
1828         if (line_buf) pkg_free(line_buf);
1829 error05:
1830         if (path_buf.s) pkg_free(path_buf.s);
1831 error00:
1832         *returned_len=0;
1833         return 0;
1834 }
1835
1836
1837
1838 char * build_res_buf_from_sip_res( struct sip_msg* msg,
1839                                 unsigned int *returned_len)
1840 {
1841         unsigned int new_len, via_len, body_delta;
1842         char* new_buf;
1843         unsigned offset, s_offset, via_offset;
1844         char* buf;
1845         unsigned int len;
1846
1847         buf=msg->buf;
1848         len=msg->len;
1849         new_buf=0;
1850         /* we must remove the first via */
1851         if (msg->via1->next) {
1852                 via_len=msg->via1->bsize;
1853                 via_offset=msg->h_via1->body.s-buf;
1854         } else {
1855                 via_len=msg->h_via1->len;
1856                 via_offset=msg->h_via1->name.s-buf;
1857         }
1858
1859              /* Calculate message body difference and adjust
1860               * Content-Length
1861               */
1862         body_delta = lumps_len(msg, msg->body_lumps, 0);
1863         if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
1864                         < 0) {
1865                 LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1866                                 " Content-Length\n");
1867                 goto error;
1868         }
1869
1870         /* remove the first via*/
1871         if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
1872                 LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
1873                                         "via\n");
1874                 goto error;
1875         }
1876
1877         new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
1878                                                                                                                 know the send sock */
1879
1880         DBG(" old size: %d, new size: %d\n", len, new_len);
1881         new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
1882                                                                                          (\0 to print it )*/
1883         if (new_buf==0){
1884                 LOG(L_ERR, "ERROR: build_res_buf_from_sip_res: out of mem\n");
1885                 goto error;
1886         }
1887         new_buf[new_len]=0; /* debug: print the message */
1888         offset=s_offset=0;
1889         /*FIXME: no send sock*/
1890         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, 0);/*FIXME:*/
1891         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, 0);
1892         /* copy the rest of the message */
1893         memcpy(new_buf+offset,
1894                 buf+s_offset,
1895                 len-s_offset);
1896          /* send it! */
1897         DBG("build_res_from_sip_res: copied size: orig:%d, new: %d, rest: %d"
1898                         " msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
1899
1900         *returned_len=new_len;
1901         return new_buf;
1902 error:
1903         *returned_len=0;
1904         return 0;
1905 }
1906
1907
1908 char * build_res_buf_from_sip_req( unsigned int code, str *text ,str *new_tag,
1909                 struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
1910 {
1911         char              *buf, *p;
1912         unsigned int      len,foo;
1913         struct hdr_field  *hdr;
1914         struct lump_rpl   *lump;
1915         struct lump_rpl   *body;
1916         int               i;
1917         char*             received_buf;
1918         unsigned int      received_len;
1919         char*             rport_buf;
1920         unsigned int      rport_len;
1921         char*             warning_buf;
1922         unsigned int      warning_len;
1923         char*             content_len_buf;
1924         unsigned int      content_len_len;
1925         char *after_body;
1926         str  to_tag;
1927         char *totags;
1928
1929         body = 0;
1930         buf=0;
1931         received_buf=rport_buf=warning_buf=content_len_buf=0;
1932         received_len=rport_len=warning_len=content_len_len=0;
1933
1934         to_tag.s=0;  /* fixes gcc 4.0 warning */
1935         to_tag.len=0;
1936
1937         /* force parsing all headers -- we want to return all
1938         Via's in the reply and they may be scattered down to the
1939         end of header (non-block Vias are a really poor property
1940         of SIP :( ) */
1941         if (parse_headers( msg, HDR_EOH_F, 0 )==-1) {
1942                 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1943                         "alas, parse_headers failed\n");
1944                 goto error00;
1945         }
1946
1947         /*computes the length of the new response buffer*/
1948         len = 0;
1949
1950         /* check if received needs to be added */
1951         if (received_test(msg)) {
1952                 if ((received_buf=received_builder(msg,&received_len))==0) {
1953                         LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1954                                 "alas, received_builder failed\n");
1955                         goto error00;
1956                 }
1957         }
1958         /* check if rport needs to be updated */
1959         if ( ((msg->msg_flags|global_req_flags)&FL_FORCE_RPORT)||
1960                 (msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1961                 if ((rport_buf=rport_builder(msg, &rport_len))==0){
1962                         LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"
1963                                                         " rport_builder failed\n");
1964                         goto error01; /* free everything */
1965                 }
1966                 if (msg->via1->rport)
1967                         len -= msg->via1->rport->size+1; /* include ';' */
1968         }
1969
1970         /* first line */
1971         len += msg->first_line.u.request.version.len + 1/*space*/ + 3/*code*/ + 1/*space*/ +
1972                 text->len + CRLF_LEN/*new line*/;
1973         /*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
1974         for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
1975                 switch (hdr->type) {
1976                         case HDR_TO_T:
1977                                 if (new_tag && new_tag->len) {
1978                                         to_tag=get_to(msg)->tag_value;
1979                                         if (to_tag.len )
1980                                                 len+=new_tag->len-to_tag.len;
1981                                         else
1982                                                 len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
1983                                 }
1984                                 len += hdr->len;
1985                                 break;
1986                         case HDR_VIA_T:
1987                                 /* we always add CRLF to via*/
1988                                 len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
1989                                 if (hdr==msg->h_via1) len += received_len+rport_len;
1990                                 break;
1991                         case HDR_RECORDROUTE_T:
1992                                 /* RR only for 1xx and 2xx replies */
1993                                 if (code<180 || code>=300)
1994                                         break;
1995                         case HDR_FROM_T:
1996                         case HDR_CALLID_T:
1997                         case HDR_CSEQ_T:
1998                                 /* we keep the original termination for these headers*/
1999                                 len += hdr->len;
2000                                 break;
2001                         default:
2002                                 /* do nothing, we are interested only in the above headers */
2003                                 ;
2004                 }
2005         }
2006         /* lumps length */
2007         for(lump=msg->reply_lump;lump;lump=lump->next) {
2008                 len += lump->text.len;
2009                 if (lump->flags&LUMP_RPL_BODY)
2010                         body = lump;
2011         }
2012         /* server header */
2013         if (server_signature)
2014                 len += server_hdr.len + CRLF_LEN;
2015         /* warning hdr */
2016         if (sip_warning) {
2017                 warning_buf = warning_builder(msg,&warning_len);
2018                 if (warning_buf) len += warning_len + CRLF_LEN;
2019                 else LOG(L_WARN, "WARNING: warning skipped -- too big\n");
2020         }
2021         /* content length hdr */
2022         if (body) {
2023                 content_len_buf = int2str(body->text.len, (int*)&content_len_len);
2024                 len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
2025         } else {
2026                 len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
2027         }
2028         /* end of message */
2029         len += CRLF_LEN; /*new line*/
2030
2031         /*allocating mem*/
2032         buf = (char*) pkg_malloc( len+1 );
2033         if (!buf)
2034         {
2035                 LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: out of memory "
2036                         " ; needs %d\n",len);
2037                 goto error01;
2038         }
2039
2040         /* filling the buffer*/
2041         p=buf;
2042         /* first line */
2043         memcpy( p , msg->first_line.u.request.version.s , 
2044                 msg->first_line.u.request.version.len);
2045         p += msg->first_line.u.request.version.len;
2046         *(p++) = ' ' ;
2047         /*code*/
2048         for ( i=2 , foo = code  ;  i>=0  ;  i-- , foo=foo/10 )
2049                 *(p+i) = '0' + foo - ( foo/10 )*10;
2050         p += 3;
2051         *(p++) = ' ' ;
2052         memcpy( p , text->s , text->len );
2053         p += text->len;
2054         memcpy( p, CRLF, CRLF_LEN );
2055         p+=CRLF_LEN;
2056         /* headers*/
2057         for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
2058                 switch (hdr->type)
2059                 {
2060                         case HDR_VIA_T:
2061                                 if (hdr==msg->h_via1){
2062                                         if (rport_buf){
2063                                                 if (msg->via1->rport){ /* delete the old one */
2064                                                         /* copy until rport */
2065                                                         append_str_trans( p, hdr->name.s ,
2066                                                                 msg->via1->rport->start-hdr->name.s-1,msg);
2067                                                         /* copy new rport */
2068                                                         append_str(p, rport_buf, rport_len);
2069                                                         /* copy the rest of the via */
2070                                                         append_str_trans(p, msg->via1->rport->start+
2071                                                                                                 msg->via1->rport->size,
2072                                                                                                 hdr->body.s+hdr->body.len-
2073                                                                                                 msg->via1->rport->start-
2074                                                                                                 msg->via1->rport->size, msg);
2075                                                 }else{ /* just append the new one */
2076                                                         /* normal whole via copy */
2077                                                         append_str_trans( p, hdr->name.s ,
2078                                                                 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2079                                                         append_str(p, rport_buf, rport_len);
2080                                                 }
2081                                         }else{
2082                                                 /* normal whole via copy */
2083                                                 append_str_trans( p, hdr->name.s ,
2084                                                                 (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2085                                         }
2086                                         if (received_buf)
2087                                                 append_str( p, received_buf, received_len);
2088                                 }else{
2089                                         /* normal whole via copy */
2090                                         append_str_trans( p, hdr->name.s,
2091                                                         (hdr->body.s+hdr->body.len)-hdr->name.s, msg);
2092                                 }
2093                                 append_str( p, CRLF,CRLF_LEN);
2094                                 break;
2095                         case HDR_RECORDROUTE_T:
2096                                 /* RR only for 1xx and 2xx replies */
2097                                 if (code<180 || code>=300) break;
2098                                 append_str(p, hdr->name.s, hdr->len);
2099                                 break;
2100                         case HDR_TO_T:
2101                                 if (new_tag && new_tag->len){
2102                                         if (to_tag.s ) { /* replacement */
2103                                                 /* before to-tag */
2104                                                 append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
2105                                                 /* to tag replacement */
2106                                                 bmark->to_tag_val.s=p;
2107                                                 bmark->to_tag_val.len=new_tag->len;
2108                                                 append_str( p, new_tag->s,new_tag->len);
2109                                                 /* the rest after to-tag */
2110                                                 append_str( p, to_tag.s+to_tag.len,
2111                                                         hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
2112                                         }else{ /* adding a new to-tag */
2113                                                 after_body=hdr->body.s+hdr->body.len;
2114                                                 append_str( p, hdr->name.s, after_body-hdr->name.s);
2115                                                 append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
2116                                                 bmark->to_tag_val.s=p;
2117                                                 bmark->to_tag_val.len=new_tag->len;
2118                                                 append_str( p, new_tag->s,new_tag->len);
2119                                                 append_str( p, after_body,
2120                                                                                 hdr->name.s+hdr->len-after_body);
2121                                         }
2122                                         break;
2123                                 } /* no new to-tag -- proceed to 1:1 copying  */
2124                                 totags=((struct to_body*)(hdr->parsed))->tag_value.s;
2125                                 if (totags) {
2126                                         bmark->to_tag_val.s=p+(totags-hdr->name.s);
2127                                         bmark->to_tag_val.len=
2128                                                         ((struct to_body*)(hdr->parsed))->tag_value.len;
2129                                 }
2130                                 else {
2131                                         bmark->to_tag_val.len = 0;
2132                                         bmark->to_tag_val.s = p+(hdr->body.s+hdr->body.len-hdr->name.s);
2133                                 }
2134                                 /* no break */
2135                         case HDR_FROM_T:
2136                         case HDR_CALLID_T:
2137                         case HDR_CSEQ_T:
2138                                         append_str(p, hdr->name.s, hdr->len);
2139                                         break;
2140                         default:
2141                                 /* do nothing, we are interested only in the above headers */
2142                                 ;
2143                 } /* end switch */
2144         } /* end for */
2145         /* lumps */
2146         for(lump=msg->reply_lump;lump;lump=lump->next)
2147                 if (lump->flags&LUMP_RPL_HDR){
2148                         memcpy(p,lump->text.s,lump->text.len);
2149                         p += lump->text.len;
2150                 }
2151         /* server header */
2152         if (server_signature) {
2153                 memcpy( p, server_hdr.s, server_hdr.len );
2154                 p+=server_hdr.len;
2155                 memcpy( p, CRLF, CRLF_LEN );
2156                 p+=CRLF_LEN;
2157         }
2158         /* content_length hdr */
2159         if (content_len_len) {
2160                 append_str( p, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
2161                 append_str( p, content_len_buf, content_len_len );
2162                 append_str( p, CRLF, CRLF_LEN );
2163         } else {
2164                 append_str( p, CONTENT_LENGTH"0"CRLF,CONTENT_LENGTH_LEN+1+CRLF_LEN);
2165         }
2166         /* warning header */
2167         if (warning_buf) {
2168                 memcpy( p, warning_buf, warning_len);
2169                 p+=warning_len;
2170                 memcpy( p, CRLF, CRLF_LEN);
2171                 p+=CRLF_LEN;
2172         }
2173         /*end of message*/
2174         memcpy( p, CRLF, CRLF_LEN );
2175         p+=CRLF_LEN;
2176         /* body */
2177         if (body) {
2178                 memcpy ( p, body->text.s, body->text.len );
2179                 p+=body->text.len;
2180         }
2181
2182         if (len!=p-buf)
2183                 LOG(L_CRIT,"BUG:build_res_from_sip_req: diff len=%d p-buf=%d\n",
2184                                         len, (int)(p-buf));
2185
2186         *(p) = 0;
2187         *returned_len = len;
2188         /* in req2reply, received_buf is not introduced to lumps and
2189            needs to be deleted here
2190         */
2191         if (received_buf) pkg_free(received_buf);
2192         if (rport_buf) pkg_free(rport_buf);
2193         return buf;
2194
2195 error01:
2196         if (received_buf) pkg_free(received_buf);
2197         if (rport_buf) pkg_free(rport_buf);
2198 error00:
2199         *returned_len=0;
2200         return 0;
2201 }
2202
2203
2204
2205 /* return number of chars printed or 0 if space exceeded;
2206    assumes buffer size of at least MAX_BRANCH_PARAM_LEN
2207  */
2208 int branch_builder( unsigned int hash_index,
2209         /* only either parameter useful */
2210         unsigned int label, char * char_v,
2211         int branch,
2212         char *branch_str, int *len )
2213 {
2214
2215         char *begin;
2216         int size;
2217
2218         /* hash id provided ... start with it */
2219         size=MAX_BRANCH_PARAM_LEN;
2220         begin=branch_str;
2221         *len=0;
2222
2223         memcpy(begin, MCOOKIE, MCOOKIE_LEN );
2224         size-=MCOOKIE_LEN;begin+=MCOOKIE_LEN;
2225
2226         if (int2reverse_hex( &begin, &size, hash_index)==-1)
2227                 return 0;
2228
2229         if (size) {
2230                 *begin=BRANCH_SEPARATOR;
2231                 begin++; size--;
2232         } else return 0;
2233
2234         /* string with request's characteristic value ... use it ... */
2235         if (char_v) {
2236                 if (memcpy(begin,char_v,MD5_LEN)) {
2237                         begin+=MD5_LEN; size-=MD5_LEN;
2238                 } else return 0;
2239         } else { /* ... use the "label" value otherwise */
2240                 if (int2reverse_hex( &begin, &size, label )==-1)
2241                         return 0;
2242         }
2243
2244         if (size) {
2245                 *begin=BRANCH_SEPARATOR;
2246                 begin++; size--;
2247         } else return 0;
2248
2249         if (int2reverse_hex( &begin, &size, branch)==-1)
2250                 return 0;
2251
2252         *len=MAX_BRANCH_PARAM_LEN-size;
2253         return size;
2254
2255 }
2256
2257
2258
2259 /* uses only the send_info->send_socket, send_info->proto and 
2260  * send_info->comp (so that a send_info used for sending can be passed
2261  * to this function w/o changes and the correct via will be built) */
2262 char* via_builder( unsigned int *len,
2263         struct dest_info* send_info /* where to send the reply */,
2264         str* branch, str* extra_params, struct hostport* hp)
2265 {
2266         unsigned int  via_len, extra_len;
2267         char               *line_buf;
2268         int max_len;
2269         int via_prefix_len;
2270         str* address_str; /* address displayed in via */
2271         str* port_str; /* port no displayed in via */
2272         struct socket_info* send_sock;
2273         int comp_len, comp_name_len;
2274 #ifdef USE_COMP
2275         char* comp_name;
2276 #endif /* USE_COMP */
2277
2278         send_sock=send_info->send_sock;
2279         /* use pre-set address in via or the outbound socket one */
2280         if ( hp && hp->host->len)
2281                 address_str=hp->host;
2282         else
2283                 address_str=&(send_sock->address_str);
2284         if (hp && hp->port->len)
2285                 port_str=hp->port;
2286         else
2287                 port_str=&(send_sock->port_no_str);
2288         
2289         comp_len=comp_name_len=0;
2290 #ifdef USE_COMP
2291         comp_name=0;
2292         switch(send_info->comp){
2293                 case COMP_NONE:
2294                         break;
2295                 case COMP_SIGCOMP:
2296                         comp_len=COMP_PARAM_LEN;
2297                         comp_name_len=SIGCOMP_NAME_LEN;
2298                         comp_name=SIGCOMP_NAME;
2299                         break;
2300                 case COMP_SERGZ:
2301                         comp_len=COMP_PARAM_LEN;
2302                         comp_name_len=SERGZ_NAME_LEN;
2303                         comp_name=SERGZ_NAME;
2304                         break;
2305                 default:
2306                         LOG(L_CRIT, "BUG: via_builder: unknown comp %d\n",
2307                                         send_info->comp);
2308                         /* continue, we'll just ignore comp */
2309         }
2310 #endif /* USE_COMP */
2311                         
2312         via_prefix_len=MY_VIA_LEN+(send_info->proto==PROTO_SCTP);
2313         max_len=via_prefix_len +address_str->len /* space in MY_VIA */
2314                 +2 /* just in case it is a v6 address ... [ ] */
2315                 +1 /*':'*/+port_str->len
2316                 +(branch?(MY_BRANCH_LEN+branch->len):0)
2317                 +(extra_params?extra_params->len:0)
2318                 +comp_len+comp_name_len
2319                 +CRLF_LEN+1;
2320         line_buf=pkg_malloc( max_len );
2321         if (line_buf==0){
2322                 ser_error=E_OUT_OF_MEM;
2323                 LOG(L_ERR, "ERROR: via_builder: out of memory\n");
2324                 return 0;
2325         }
2326
2327         extra_len=0;
2328
2329         via_len=via_prefix_len+address_str->len; /*space included in MY_VIA*/
2330
2331         memcpy(line_buf, MY_VIA, MY_VIA_LEN);
2332         if (send_info->proto==PROTO_UDP){
2333                 /* do nothing */
2334         }else if (send_info->proto==PROTO_TCP){
2335                 memcpy(line_buf+MY_VIA_LEN-4, "TCP ", 4);
2336         }else if (send_info->proto==PROTO_TLS){
2337                 memcpy(line_buf+MY_VIA_LEN-4, "TLS ", 4);
2338         }else if (send_info->proto==PROTO_SCTP){
2339                 memcpy(line_buf+MY_VIA_LEN-4, "SCTP ", 5);
2340         }else{
2341                 LOG(L_CRIT, "BUG: via_builder: unknown proto %d\n", send_info->proto);
2342                 return 0;
2343         }
2344 #       ifdef USE_IPV6
2345         /* add [] only if ipv6 and outbound socket address is used;
2346          * if using pre-set no check is made */
2347         if ((send_sock->address.af==AF_INET6) &&
2348                 (address_str==&(send_sock->address_str))) {
2349                 line_buf[via_prefix_len]='[';
2350                 line_buf[via_prefix_len+1+address_str->len]=']';
2351                 extra_len=1;
2352                 via_len+=2; /* [ ]*/
2353         }
2354 #       endif
2355         memcpy(line_buf+via_prefix_len+extra_len, address_str->s,
2356                                 address_str->len);
2357         if ((send_sock->port_no!=SIP_PORT) ||
2358                                 (port_str!=&send_sock->port_no_str)){
2359                 line_buf[via_len]=':'; via_len++;
2360                 memcpy(line_buf+via_len, port_str->s, port_str->len);
2361                 via_len+=port_str->len;
2362         }
2363
2364         /* branch parameter */
2365         if (branch){
2366                 memcpy(line_buf+via_len, MY_BRANCH, MY_BRANCH_LEN );
2367                 via_len+=MY_BRANCH_LEN;
2368                 memcpy(line_buf+via_len, branch->s, branch->len );
2369                 via_len+=branch->len;
2370         }
2371         /* extra params  */
2372         if (extra_params){
2373                 memcpy(line_buf+via_len, extra_params->s, extra_params->len);
2374                 via_len+=extra_params->len;
2375         }
2376 #ifdef USE_COMP
2377         /* comp */
2378         if (comp_len){
2379                 memcpy(line_buf+via_len, COMP_PARAM, COMP_PARAM_LEN);
2380                 via_len+=COMP_PARAM_LEN;
2381                 memcpy(line_buf+via_len, comp_name, comp_name_len);
2382                 via_len+=comp_name_len;
2383         }
2384 #endif
2385
2386         memcpy(line_buf+via_len, CRLF, CRLF_LEN);
2387         via_len+=CRLF_LEN;
2388         line_buf[via_len]=0; /* null terminate the string*/
2389
2390         *len = via_len;
2391         return line_buf;
2392 }
2393
2394 /* creates a via header honoring the protocol of the incomming socket
2395  * msg is an optional parameter */
2396 char* create_via_hf( unsigned int *len,
2397         struct sip_msg *msg,
2398         struct dest_info* send_info /* where to send the reply */,
2399         str* branch)
2400 {
2401         char* via;
2402         str extra_params;
2403         struct hostport hp;
2404 #if defined USE_TCP || defined USE_SCTP
2405         char* id_buf;
2406         unsigned int id_len;
2407
2408
2409         id_buf=0;
2410         id_len=0;
2411 #endif
2412         extra_params.len=0;
2413         extra_params.s=0;
2414
2415
2416 #if defined USE_TCP || defined USE_SCTP
2417         /* add id if tcp */
2418         if (msg && (
2419 #ifdef USE_TCP
2420                 (msg->rcv.proto==PROTO_TCP)
2421 #ifdef USE_TLS
2422                         || (msg->rcv.proto==PROTO_TLS)
2423 #endif
2424 #ifdef USE_SCTP
2425                         ||
2426 #endif /* USE_SCTP */
2427 #endif /* USE_TCP */
2428 #ifdef USE_SCTP
2429                         (msg->rcv.proto==PROTO_SCTP)
2430 #endif /* USE_SCTP */
2431                         )){
2432                 if  ((id_buf=id_builder(msg, &id_len))==0){
2433                         LOG(L_ERR, "ERROR: create_via_hf:"
2434                                                         " id_builder failed\n");
2435                         return 0; /* we don't need to free anything,
2436                                          nothing alloc'ed yet*/
2437                 }
2438                 DBG("create_via_hf: id added: <%.*s>, rcv proto=%d\n",
2439                                 (int)id_len, id_buf, msg->rcv.proto);
2440                 extra_params.s=id_buf;
2441                 extra_params.len=id_len;
2442         }
2443 #endif /* USE_TCP || USE_SCTP */
2444
2445         /* test and add rport parameter to local via - rfc3581 */
2446         if(msg->msg_flags&FL_ADD_LOCAL_RPORT) {
2447                 /* params so far + ';rport' + '\0' */
2448                 via = (char*)pkg_malloc(extra_params.len+RPORT_LEN);
2449                 if(via==0) {
2450                         LM_ERR("building local rport via param failed\n");
2451                         if (extra_params.s) pkg_free(extra_params.s);
2452                         return 0;
2453                 }
2454                 if(extra_params.len!=0) {
2455                         memcpy(via, extra_params.s, extra_params.len);
2456                         pkg_free(extra_params.s);
2457                 }
2458                 memcpy(via + extra_params.len, RPORT, RPORT_LEN-1);
2459                 extra_params.s = via;
2460                 extra_params.len += RPORT_LEN-1;
2461                 extra_params.s[extra_params.len]='\0';
2462         }
2463
2464         set_hostport(&hp, msg);
2465         via = via_builder( len, send_info, branch,
2466                                                         extra_params.len?&extra_params:0, &hp);
2467
2468         /* we do not need extra_params any more, already in the new via header */
2469         if (extra_params.s) pkg_free(extra_params.s);
2470         return via;
2471 }
2472
2473 /* builds a char* buffer from message headers without body
2474  * first line is excluded in case of skip_first_line=1
2475  * error is set -1 if the memory allocation failes
2476  */
2477 char * build_only_headers( struct sip_msg* msg, int skip_first_line,
2478                                 unsigned int *returned_len,
2479                                 int *error,
2480                                 struct dest_info* send_info)
2481 {
2482         char            *buf, *new_buf;
2483         unsigned int    offset, s_offset, len, new_len;
2484
2485         *error = 0;
2486         buf = msg->buf;
2487         if (skip_first_line)
2488                 s_offset = msg->headers->name.s - buf;
2489         else
2490                 s_offset = 0;
2491
2492         /* original length without body, and without final \r\n */
2493         len = msg->unparsed - buf;
2494         /* new msg length */
2495         new_len =       len - /* original length  */
2496                         s_offset + /* skipped first line */
2497                         lumps_len(msg, msg->add_rm, send_info); /* lumps */
2498
2499         if (new_len == 0) {
2500                 *returned_len = 0;
2501                 return 0;
2502         }
2503
2504         new_buf = (char *)pkg_malloc(new_len+1);
2505         if (!new_buf) {
2506                 LOG(L_ERR, "ERROR: build_only_headers: Not enough memory\n");
2507                 *error = -1;
2508                 return 0;
2509         }
2510         new_buf[0] = 0;
2511         offset = 0;
2512
2513         /* copy message lumps */
2514         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2515         /* copy the rest of the message without body */
2516         if (len > s_offset) {
2517                 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2518                 offset += (len-s_offset);
2519         }
2520         new_buf[offset] = 0;
2521
2522         *returned_len = offset;
2523         return new_buf;
2524 }
2525
2526 /* builds a char* buffer from message body
2527  * error is set -1 if the memory allocation failes
2528  */
2529 char * build_body( struct sip_msg* msg,
2530                         unsigned int *returned_len,
2531                         int *error,
2532                         struct dest_info* send_info)
2533 {
2534         char            *buf, *new_buf, *body;
2535         unsigned int    offset, s_offset, len, new_len;
2536
2537         *error = 0;
2538         body = get_body(msg);
2539
2540         if (!body || (body[0] == 0)) {
2541                 *returned_len = 0;
2542                 return 0;
2543         }
2544         buf = msg->buf;
2545         s_offset = body - buf;
2546
2547         /* original length of msg with body */
2548         len = msg->len;
2549         /* new body length */
2550         new_len =       len - /* original length  */
2551                         s_offset + /* msg without body */
2552                         lumps_len(msg, msg->body_lumps, send_info); /* lumps */
2553
2554         new_buf = (char *)pkg_malloc(new_len+1);
2555         if (!new_buf) {
2556                 LOG(L_ERR, "ERROR: build_body: Not enough memory\n");
2557                 *error = -1;
2558                 return 0;
2559         }
2560         new_buf[0] = 0;
2561         offset = 0;
2562
2563         /* copy body lumps */
2564         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2565         /* copy the rest of the message without body */
2566         if (len > s_offset) {
2567                 memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2568                 offset += (len-s_offset);
2569         }
2570         new_buf[offset] = 0;
2571
2572         *returned_len = offset;
2573         return new_buf;
2574 }
2575
2576 /* builds a char* buffer from SIP message including body
2577  * The function adjusts the Content-Length HF according
2578  * to body lumps in case of touch_clen=1.
2579  */
2580 char * build_all( struct sip_msg* msg, int touch_clen,
2581                         unsigned int *returned_len,
2582                         int *error,
2583                         struct dest_info* send_info)
2584 {
2585         char            *buf, *new_buf;
2586         unsigned int    offset, s_offset, len, new_len;
2587         unsigned int    body_delta;
2588
2589         *error = 0;
2590         /* Calculate message body difference */
2591         body_delta = lumps_len(msg, msg->body_lumps, send_info);
2592         if (touch_clen) {
2593                 /* adjust Content-Length */
2594                 if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
2595                         LOG(L_ERR, "ERROR: build_all: Error while adjusting"
2596                                         " Content-Length\n");
2597                         *error = -1;
2598                         return 0;
2599                 }
2600         }
2601
2602         buf = msg->buf;
2603         /* original msg length */
2604         len = msg->len;
2605         /* new msg length */
2606         new_len =       len + /* original length  */
2607                         lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
2608                         body_delta; /* body lumps */
2609
2610         if (new_len == 0) {
2611                 returned_len = 0;
2612                 return 0;
2613         }
2614
2615         new_buf = (char *)pkg_malloc(new_len+1);
2616         if (!new_buf) {
2617                 LOG(L_ERR, "ERROR: build_all: Not enough memory\n");
2618                 *error = -1;
2619                 return 0;
2620         }
2621         new_buf[0] = 0;
2622         offset = s_offset = 0;
2623
2624         /* copy message lumps */
2625         process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2626         /* copy body lumps */
2627         process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2628         /* copy the rest of the message */
2629         memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2630         offset += (len-s_offset);
2631         new_buf[offset] = 0;
2632
2633         *returned_len = offset;
2634         return new_buf; 
2635 }
2636
2637
2638
2639 /**
2640  * parse buf in msg and fill several fields
2641  */
2642 int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len,
2643                 unsigned int id)
2644 {
2645         if(msg==0 || buf==0)
2646                 return -1;
2647
2648         memset(msg, 0, sizeof(sip_msg_t));
2649         msg->id = id;
2650         msg->buf = buf;
2651         msg->len = len;
2652         if (parse_msg(buf, len, msg)!=0) {
2653                 LM_ERR("parsing failed");
2654                 return -1;
2655         }
2656         msg->set_global_address=default_global_address;
2657         msg->set_global_port=default_global_port;
2658         return 0;
2659 }
2660