modules/ims_qos: added patch for flow-description bug when request originates from...
[sip-router] / src / core / sip_msg_clone.c
1 /* 
2  * Copyright (C) 2009 iptelorg GmbH
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /** Kamailio core :: sip message shared memory cloner.
18  * @file
19  * @ingroup core
20  * Module: @ref core
21  */
22
23 #include "sip_msg_clone.h"
24
25
26 #include "dprint.h"
27 #include "mem/mem.h"
28 #include "data_lump.h"
29 #include "data_lump_rpl.h"
30 #include "ut.h"
31 #include "parser/digest/digest.h"
32 #include "parser/parse_to.h"
33 #include "atomic_ops.h"
34
35 /* rounds to the first 4 byte multiple on 32 bit archs
36  * and to the first 8 byte multiple on 64 bit archs */
37 #define ROUND4(s) \
38         (((s)+(sizeof(char*)-1))&(~(sizeof(char*)-1)))
39
40 #define lump_len( _lump) \
41         (ROUND4(sizeof(struct lump)) +\
42         ROUND4(((_lump)->op==LUMP_ADD)?(_lump)->len:0))
43 #define lump_clone( _new,_old,_ptr) \
44         {\
45                 (_new) = (struct lump*)(_ptr);\
46                 memcpy( (_new), (_old), sizeof(struct lump) );\
47                 (_new)->flags|=LUMPFLAG_SHMEM; \
48                 (_ptr)+=ROUND4(sizeof(struct lump));\
49                 if ( (_old)->op==LUMP_ADD) {\
50                         (_new)->u.value = (char*)(_ptr);\
51                         memcpy( (_new)->u.value , (_old)->u.value , (_old)->len);\
52                         (_ptr)+=ROUND4((_old)->len);}\
53         }
54
55 /* length of the data lump structures */
56 #define LUMP_LIST_LEN(len, list) \
57 do { \
58         struct lump* tmp, *chain; \
59         chain = (list); \
60         while (chain) \
61         { \
62                 (len) += lump_len(chain); \
63                 tmp = chain->before; \
64                 while ( tmp ) \
65                 { \
66                         (len) += lump_len( tmp ); \
67                         tmp = tmp->before; \
68                 } \
69                 tmp = chain->after; \
70                 while ( tmp ) \
71                 { \
72                         (len) += lump_len( tmp ); \
73                         tmp = tmp->after; \
74                 } \
75                 chain = chain->next; \
76         } \
77 } while(0);
78
79 /* length of the reply lump structure */
80 #define RPL_LUMP_LIST_LEN(len, list) \
81 do { \
82         struct lump_rpl* rpl_lump; \
83         for(rpl_lump=(list);rpl_lump;rpl_lump=rpl_lump->next) \
84                 (len)+=ROUND4(sizeof(struct lump_rpl))+ROUND4(rpl_lump->text.len); \
85 } while(0);
86
87 /* clones data lumps */
88 #define CLONE_LUMP_LIST(anchor, list, _ptr) \
89 do { \
90         struct lump* lump_tmp, *l; \
91         struct lump** lump_anchor2, **a; \
92         a = (anchor); \
93         l = (list); \
94         while (l) \
95         { \
96                 lump_clone( (*a) , l , (_ptr) ); \
97                 /*before list*/ \
98                 lump_tmp = l->before; \
99                 lump_anchor2 = &((*a)->before); \
100                 while ( lump_tmp ) \
101                 { \
102                         lump_clone( (*lump_anchor2) , lump_tmp , (_ptr) ); \
103                         lump_anchor2 = &((*lump_anchor2)->before); \
104                         lump_tmp = lump_tmp->before; \
105                 } \
106                 /*after list*/ \
107                 lump_tmp = l->after; \
108                 lump_anchor2 = &((*a)->after); \
109                 while ( lump_tmp ) \
110                 { \
111                         lump_clone( (*lump_anchor2) , lump_tmp , (_ptr) ); \
112                         lump_anchor2 = &((*lump_anchor2)->after); \
113                         lump_tmp = lump_tmp->after; \
114                 } \
115                 a = &((*a)->next); \
116                 l = l->next; \
117         } \
118 } while(0)
119
120 /* clones reply lumps */
121 #define CLONE_RPL_LUMP_LIST(anchor, list, _ptr) \
122 do { \
123         struct lump_rpl* rpl_lump; \
124         struct lump_rpl** rpl_lump_anchor; \
125         rpl_lump_anchor = (anchor); \
126         for(rpl_lump=(list);rpl_lump;rpl_lump=rpl_lump->next) \
127         { \
128                 *(rpl_lump_anchor)=(struct lump_rpl*)(_ptr); \
129                 (_ptr)+=ROUND4(sizeof( struct lump_rpl )); \
130                 (*rpl_lump_anchor)->flags = LUMP_RPL_SHMEM | \
131                         (rpl_lump->flags&(~(LUMP_RPL_NODUP|LUMP_RPL_NOFREE))); \
132                 (*rpl_lump_anchor)->text.len = rpl_lump->text.len; \
133                 (*rpl_lump_anchor)->text.s=(_ptr); \
134                 (_ptr)+=ROUND4(rpl_lump->text.len); \
135                 memcpy((*rpl_lump_anchor)->text.s,rpl_lump->text.s,rpl_lump->text.len); \
136                 (*rpl_lump_anchor)->next=0; \
137                 rpl_lump_anchor = &((*rpl_lump_anchor)->next); \
138         } \
139 } while (0)
140
141
142
143 static inline struct via_body* via_body_cloner( char* new_buf,
144                                         char *org_buf, struct via_body *param_org_via, char **p)
145 {
146         struct via_body *new_via;
147         struct via_body *first_via, *last_via;
148         struct via_body *org_via;
149
150         first_via = last_via = 0;
151         org_via = param_org_via;
152
153         do
154         {
155                 /* clones the via_body structure */
156                 new_via = (struct via_body*)(*p);
157                 memcpy( new_via , org_via , sizeof( struct via_body) );
158                 (*p) += ROUND4(sizeof( struct via_body ));
159
160                 /* hdr (str type) */
161                 new_via->hdr.s=translate_pointer(new_buf,org_buf,org_via->hdr.s);
162                 /* name (str type) */
163                 new_via->name.s=translate_pointer(new_buf,org_buf,org_via->name.s);
164                 /* version (str type) */
165                 new_via->version.s=
166                         translate_pointer(new_buf,org_buf,org_via->version.s);
167                 /* transport (str type) */
168                 new_via->transport.s=
169                         translate_pointer(new_buf,org_buf,org_via->transport.s);
170                 /* host (str type) */
171                 new_via->host.s=translate_pointer(new_buf,org_buf,org_via->host.s);
172                 /* port_str (str type) */
173                 new_via->port_str.s=
174                         translate_pointer(new_buf,org_buf,org_via->port_str.s);
175                 /* params (str type) */
176                 new_via->params.s=translate_pointer(new_buf,org_buf,org_via->params.s);
177                 /* transaction id */
178                 new_via->tid.s=
179                         translate_pointer(new_buf, org_buf, org_via->tid.s);
180                 /* comment (str type) */
181                 new_via->comment.s=
182                         translate_pointer(new_buf,org_buf,org_via->comment.s);
183
184                 if ( org_via->param_lst )
185                 {
186                         struct via_param *vp, *new_vp, *last_new_vp;
187                         for( vp=org_via->param_lst, last_new_vp=0 ; vp ; vp=vp->next )
188                         {
189                                 new_vp = (struct via_param*)(*p);
190                                 memcpy( new_vp , vp , sizeof(struct via_param));
191                                 (*p) += ROUND4(sizeof(struct via_param));
192                                 new_vp->name.s=translate_pointer(new_buf,org_buf,vp->name.s);
193                                 new_vp->value.s=translate_pointer(new_buf,org_buf,vp->value.s);
194                                 new_vp->start=translate_pointer(new_buf,org_buf,vp->start);
195
196                                 /* "translate" the shortcuts */
197                                 switch(new_vp->type){
198                                         case PARAM_BRANCH:
199                                                         new_via->branch = new_vp;
200                                                         break;
201                                         case PARAM_RECEIVED:
202                                                         new_via->received = new_vp;
203                                                         break;
204                                         case PARAM_RPORT:
205                                                         new_via->rport = new_vp;
206                                                         break;
207                                         case PARAM_I:
208                                                         new_via->i = new_vp;
209                                                         break;
210                                         case PARAM_ALIAS:
211                                                         new_via->alias = new_vp;
212                                                         break;
213
214 #ifdef USE_COMP
215                                         case PARAM_COMP:
216                                                         new_via->comp = new_vp;
217                                                         break;
218 #endif
219                                 }
220
221                                 if (last_new_vp)
222                                         last_new_vp->next = new_vp;
223                                 else
224                                         new_via->param_lst = new_vp;
225
226                                 last_new_vp = new_vp;
227                                 last_new_vp->next = NULL;
228                         }
229                         new_via->last_param = new_vp;
230                 }/*end if via has params */
231
232                 if (last_via)
233                         last_via->next = new_via;
234                 else
235                         first_via = new_via;
236                 last_via = new_via;
237                 org_via = org_via->next;
238         }while(org_via);
239
240         return first_via;
241 }
242
243
244 static void uri_trans(char *new_buf, char *org_buf, struct sip_uri *uri)
245 {
246         uri->user.s=translate_pointer(new_buf,org_buf,uri->user.s);
247         uri->passwd.s=translate_pointer(new_buf,org_buf,uri->passwd.s);
248         uri->host.s=translate_pointer(new_buf,org_buf,uri->host.s);
249         uri->port.s=translate_pointer(new_buf,org_buf,uri->port.s);
250         uri->params.s=translate_pointer(new_buf,org_buf,uri->params.s);
251         uri->headers.s=translate_pointer(new_buf,org_buf,uri->headers.s);
252         /* parameters */
253         uri->transport.s=translate_pointer(new_buf,org_buf,uri->transport.s);
254         uri->ttl.s=translate_pointer(new_buf,org_buf,uri->ttl.s);
255         uri->user_param.s=translate_pointer(new_buf,org_buf,uri->user_param.s);
256         uri->maddr.s=translate_pointer(new_buf,org_buf,uri->maddr.s);
257         uri->method.s=translate_pointer(new_buf,org_buf,uri->method.s);
258         uri->lr.s=translate_pointer(new_buf,org_buf,uri->lr.s);
259         uri->r2.s=translate_pointer(new_buf,org_buf,uri->r2.s);
260         /* values */
261         uri->transport_val.s
262                 =translate_pointer(new_buf,org_buf,uri->transport_val.s);
263         uri->ttl_val.s=translate_pointer(new_buf,org_buf,uri->ttl_val.s);
264         uri->user_param_val.s
265                 =translate_pointer(new_buf,org_buf,uri->user_param_val.s);
266         uri->maddr_val.s=translate_pointer(new_buf,org_buf,uri->maddr_val.s);
267         uri->method_val.s=translate_pointer(new_buf,org_buf,uri->method_val.s);
268         uri->lr_val.s=translate_pointer(new_buf,org_buf,uri->lr_val.s);
269         uri->r2_val.s=translate_pointer(new_buf,org_buf,uri->r2_val.s);
270 }
271
272
273 static inline struct auth_body* auth_body_cloner(char* new_buf, char *org_buf, struct auth_body *auth, char **p)
274 {
275         struct auth_body* new_auth;
276
277         new_auth = (struct auth_body*)(*p);
278         memcpy(new_auth , auth , sizeof(struct auth_body));
279         (*p) += ROUND4(sizeof(struct auth_body));
280
281         /* authorized field must be cloned elsewhere */
282         new_auth->digest.username.whole.s =
283                 translate_pointer(new_buf, org_buf, auth->digest.username.whole.s);
284         new_auth->digest.username.user.s =
285                 translate_pointer(new_buf, org_buf, auth->digest.username.user.s);
286         new_auth->digest.username.domain.s =
287                 translate_pointer(new_buf, org_buf, auth->digest.username.domain.s);
288         new_auth->digest.realm.s =
289                 translate_pointer(new_buf, org_buf, auth->digest.realm.s);
290         new_auth->digest.nonce.s =
291                 translate_pointer(new_buf, org_buf, auth->digest.nonce.s);
292         new_auth->digest.uri.s =
293                 translate_pointer(new_buf, org_buf, auth->digest.uri.s);
294         new_auth->digest.response.s =
295                 translate_pointer(new_buf, org_buf, auth->digest.response.s);
296         new_auth->digest.alg.alg_str.s =
297                 translate_pointer(new_buf, org_buf, auth->digest.alg.alg_str.s);
298         new_auth->digest.cnonce.s =
299                 translate_pointer(new_buf, org_buf, auth->digest.cnonce.s);
300         new_auth->digest.opaque.s =
301                 translate_pointer(new_buf, org_buf, auth->digest.opaque.s);
302         new_auth->digest.qop.qop_str.s =
303                 translate_pointer(new_buf, org_buf, auth->digest.qop.qop_str.s);
304         new_auth->digest.nc.s =
305                 translate_pointer(new_buf, org_buf, auth->digest.nc.s);
306         return new_auth;
307 }
308
309
310 static inline int clone_authorized_hooks(struct sip_msg* new,
311                                          struct sip_msg* old)
312 {
313         struct hdr_field* ptr, *new_ptr, *hook1, *hook2;
314         char stop = 0;
315
316         get_authorized_cred(old->authorization, &hook1);
317         if (!hook1) stop = 1;
318
319         get_authorized_cred(old->proxy_auth, &hook2);
320         if (!hook2) stop |= 2;
321
322         ptr = old->headers;
323         new_ptr = new->headers;
324
325         while(ptr) {
326                 if (ptr == hook1) {
327                         if (!new->authorization || !new->authorization->parsed) {
328                                 LM_CRIT("Error in message cloner (authorization)\n");
329                                 return -1;
330                         }
331                         ((struct auth_body*)new->authorization->parsed)->authorized =
332                                 new_ptr;
333                         stop |= 1;
334                 }
335
336                 if (ptr == hook2) {
337                         if (!new->proxy_auth || !new->proxy_auth->parsed) {
338                                 LM_CRIT("Error in message cloner (proxy_auth)\n");
339                                 return -1;
340                         }
341                         ((struct auth_body*)new->proxy_auth->parsed)->authorized =
342                                 new_ptr;
343                         stop |= 2;
344                 }
345
346                 if (stop == 3) break;
347
348                 ptr = ptr->next;
349                 new_ptr = new_ptr->next;
350         }
351         return 0;
352 }
353
354
355 #define AUTH_BODY_SIZE sizeof(struct auth_body)
356
357 #define HOOK_SET(hook) (new_msg->hook != org_msg->hook)
358
359
360
361 /** Creates a shm clone for a sip_msg.
362  * org_msg is cloned along with most of its headers and lumps into one
363  * shm memory block (so that a shm_free() on the result will free everything)
364  * @return shm malloced sip_msg on success, 0 on error
365  * Warning: Cloner does not clone all hdr_field headers (From, To, etc.).
366  */
367 struct sip_msg*  sip_msg_shm_clone( struct sip_msg *org_msg, int *sip_msg_len,
368                                                                         int clone_lumps)
369 {
370         unsigned int      len;
371         struct hdr_field  *hdr,*new_hdr,*last_hdr;
372         struct via_body   *via;
373         struct via_param  *prm;
374         struct to_param   *to_prm,*new_to_prm;
375         struct sip_msg    *new_msg;
376         char              *p;
377
378         /*computing the length of entire sip_msg structure*/
379         len = ROUND4(sizeof( struct sip_msg ));
380         /*we will keep only the original msg +ZT */
381         len += ROUND4(org_msg->len + 1);
382         /*the new uri (if any)*/
383         if (org_msg->new_uri.s && org_msg->new_uri.len)
384                 len+= ROUND4(org_msg->new_uri.len);
385         /*the dst uri (if any)*/
386         if (org_msg->dst_uri.s && org_msg->dst_uri.len)
387                 len+= ROUND4(org_msg->dst_uri.len);
388         if (org_msg->path_vec.s && org_msg->path_vec.len)
389                         len+= ROUND4(org_msg->path_vec.len);
390         /*all the headers*/
391         for( hdr=org_msg->headers ; hdr ; hdr=hdr->next )
392         {
393                 /*size of header struct*/
394                 len += ROUND4(sizeof( struct hdr_field));
395                 switch (hdr->type) {
396                              /* Safely ignore auxiliary header types */
397                 case HDR_ERROR_T:
398                 case HDR_OTHER_T:
399                 case HDR_VIA2_T:
400                 case HDR_EOH_T:
401                         break;
402
403                 case HDR_VIA_T:
404                         for (via=(struct via_body*)hdr->parsed;via;via=via->next) {
405                                 len+=ROUND4(sizeof(struct via_body));
406                                      /*via param*/
407                                 for(prm=via->param_lst;prm;prm=prm->next)
408                                         len+=ROUND4(sizeof(struct via_param ));
409                         }
410                         break;
411
412                 case HDR_TO_T:
413                 case HDR_FROM_T:
414                              /* From header might be unparsed */
415                         if (hdr->parsed) {
416                                 len+=ROUND4(sizeof(struct to_body));
417                                      /*to param*/
418                                 to_prm = ((struct to_body*)(hdr->parsed))->param_lst;
419                                 for(;to_prm;to_prm=to_prm->next)
420                                         len+=ROUND4(sizeof(struct to_param ));
421                         }
422                         break;
423
424                 case HDR_CSEQ_T:
425                         len+=ROUND4(sizeof(struct cseq_body));
426                         break;
427
428
429                 case HDR_AUTHORIZATION_T:
430                 case HDR_PROXYAUTH_T:
431                         if (hdr->parsed) {
432                                 len += ROUND4(AUTH_BODY_SIZE);
433                         }
434                         break;
435
436                 case HDR_CALLID_T:
437                 case HDR_CONTACT_T:
438                 case HDR_MAXFORWARDS_T:
439                 case HDR_ROUTE_T:
440                 case HDR_RECORDROUTE_T:
441                 case HDR_CONTENTTYPE_T:
442                 case HDR_CONTENTLENGTH_T:
443                 case HDR_RETRY_AFTER_T:
444                 case HDR_EXPIRES_T:
445                 case HDR_MIN_EXPIRES_T:
446                 case HDR_SUPPORTED_T:
447                 case HDR_REQUIRE_T:
448                 case HDR_PROXYREQUIRE_T:
449                 case HDR_UNSUPPORTED_T:
450                 case HDR_ALLOW_T:
451                 case HDR_EVENT_T:
452                 case HDR_ACCEPT_T:
453                 case HDR_ACCEPTLANGUAGE_T:
454                 case HDR_ORGANIZATION_T:
455                 case HDR_PRIORITY_T:
456                 case HDR_SUBJECT_T:
457                 case HDR_USERAGENT_T:
458                 case HDR_SERVER_T:
459                 case HDR_CONTENTDISPOSITION_T:
460                 case HDR_DIVERSION_T:
461                 case HDR_RPID_T:
462                 case HDR_REFER_TO_T:
463                 case HDR_SIPIFMATCH_T:
464                 case HDR_SESSIONEXPIRES_T:
465                 case HDR_MIN_SE_T:
466                 case HDR_SUBSCRIPTION_STATE_T:
467                 case HDR_ACCEPTCONTACT_T:
468                 case HDR_ALLOWEVENTS_T:
469                 case HDR_CONTENTENCODING_T:
470                 case HDR_REFERREDBY_T:
471                 case HDR_REJECTCONTACT_T:
472                 case HDR_REQUESTDISPOSITION_T:
473                 case HDR_WWW_AUTHENTICATE_T:
474                 case HDR_PROXY_AUTHENTICATE_T:
475                 case HDR_DATE_T:
476                 case HDR_IDENTITY_T:
477                 case HDR_IDENTITY_INFO_T:
478                 case HDR_PPI_T:
479                 case HDR_PAI_T:
480                 case HDR_PATH_T:
481                 case HDR_PRIVACY_T:
482                 case HDR_REASON_T:
483                         /* we ignore them for now even if they have something parsed*/
484                         break;
485                 }/*switch*/
486         }/*for all headers*/
487         
488         if (clone_lumps) {
489                 /* calculate the length of the data and reply lump structures */
490                 LUMP_LIST_LEN(len, org_msg->add_rm);
491                 LUMP_LIST_LEN(len, org_msg->body_lumps);
492                 RPL_LUMP_LIST_LEN(len, org_msg->reply_lump);
493         }
494         
495         p=(char *)shm_malloc(len);
496         if (!p)
497         {
498                 LM_ERR("cannot allocate memory\n" );
499                 return 0;
500         }
501         if (sip_msg_len)
502                 *sip_msg_len = len;
503
504         /* filling up the new structure */
505         new_msg = (struct sip_msg*)p;
506         /* sip msg structure */
507         memcpy( new_msg , org_msg , sizeof(struct sip_msg) );
508
509         new_msg->msg_flags |= FL_SHM_CLONE;
510         p += ROUND4(sizeof(struct sip_msg));
511         new_msg->body = 0;
512         new_msg->add_rm = 0;
513         new_msg->body_lumps = 0;
514         new_msg->reply_lump = 0;
515         /* zero *uri.s, in case len is 0 but org_msg->*uris!=0 (just to be safe)*/
516         new_msg->new_uri.s = 0;
517         new_msg->dst_uri.s = 0;
518         new_msg->path_vec.s = 0;
519         /* new_uri */
520         if (org_msg->new_uri.s && org_msg->new_uri.len)
521         {
522                 new_msg->new_uri.s = p;
523                 memcpy( p , org_msg->new_uri.s , org_msg->new_uri.len);
524                 p += ROUND4(org_msg->new_uri.len);
525         }
526         /* dst_uri */
527         if (org_msg->dst_uri.s && org_msg->dst_uri.len)
528         {
529                 new_msg->dst_uri.s = p;
530                 memcpy( p , org_msg->dst_uri.s , org_msg->dst_uri.len);
531                 p += ROUND4(org_msg->dst_uri.len);
532         }
533         /* path vector */
534         if (org_msg->path_vec.s && org_msg->path_vec.len) {
535                 new_msg->path_vec.s = p;
536                 memcpy(p, org_msg->path_vec.s, org_msg->path_vec.len);
537                 p += ROUND4(org_msg->path_vec.len);
538         }
539
540         /* instance is not cloned (it's reset instead) */
541         new_msg->instance.s=0;
542         new_msg->instance.len=0;
543         /* ruid is not cloned (it's reset instead) */
544         new_msg->ruid.s=0;
545         new_msg->ruid.len=0;
546         /* location ua is not cloned (it's reset instead) */
547         new_msg->location_ua.s=0;
548         new_msg->location_ua.len=0;
549         /* reg_id is not cloned (it's reset instead) */
550         new_msg->reg_id=0;
551         /* local data struct is not cloned (it's reset instead) */
552         memset(&new_msg->ldv, 0, sizeof(msg_ldata_t));
553         /* message buffers(org and scratch pad) */
554         memcpy( p , org_msg->buf, org_msg->len);
555         /* ZT to be safer */
556         *(p+org_msg->len)=0;
557         new_msg->buf = p;
558         p += ROUND4(new_msg->len+1);
559         /* unparsed and eoh pointer */
560         new_msg->unparsed = translate_pointer(new_msg->buf ,org_msg->buf,
561                 org_msg->unparsed );
562         new_msg->eoh = translate_pointer(new_msg->buf,org_msg->buf,org_msg->eoh);
563         /* first line, updating the pointers*/
564         if ( org_msg->first_line.type==SIP_REQUEST )
565         {
566                 new_msg->first_line.u.request.method.s =
567                         translate_pointer( new_msg->buf , org_msg->buf ,
568                         org_msg->first_line.u.request.method.s );
569                 new_msg->first_line.u.request.uri.s =
570                         translate_pointer( new_msg->buf , org_msg->buf ,
571                         org_msg->first_line.u.request.uri.s );
572                 new_msg->first_line.u.request.version.s =
573                         translate_pointer( new_msg->buf , org_msg->buf ,
574                         org_msg->first_line.u.request.version.s );
575                 uri_trans(new_msg->buf, org_msg->buf, &new_msg->parsed_orig_ruri);
576                 if (org_msg->new_uri.s && org_msg->new_uri.len)
577                         uri_trans(new_msg->new_uri.s, org_msg->new_uri.s,
578                                                                                         &new_msg->parsed_uri);
579                 else
580                         uri_trans(new_msg->buf, org_msg->buf, &new_msg->parsed_uri);
581         }
582         else if ( org_msg->first_line.type==SIP_REPLY )
583         {
584                 new_msg->first_line.u.reply.version.s =
585                         translate_pointer( new_msg->buf , org_msg->buf ,
586                         org_msg->first_line.u.reply.version.s );
587                 new_msg->first_line.u.reply.status.s =
588                         translate_pointer( new_msg->buf , org_msg->buf ,
589                         org_msg->first_line.u.reply.status.s );
590                 new_msg->first_line.u.reply.reason.s =
591                         translate_pointer( new_msg->buf , org_msg->buf ,
592                         org_msg->first_line.u.reply.reason.s );
593         }
594
595        /*headers list*/
596        new_msg->via1=0;
597        new_msg->via2=0;
598
599         for( hdr=org_msg->headers,last_hdr=0 ; hdr ; hdr=hdr->next )
600         {
601                 new_hdr = (struct hdr_field*)p;
602                 memcpy(new_hdr, hdr, sizeof(struct hdr_field) );
603                 p += ROUND4(sizeof( struct hdr_field));
604                 new_hdr->name.s = translate_pointer(new_msg->buf, org_msg->buf,
605                         hdr->name.s);
606                 new_hdr->body.s = translate_pointer(new_msg->buf, org_msg->buf,
607                         hdr->body.s);
608                 /* by default, we assume we don't understand this header in TM
609                    and better set it to zero; if we do, we will set a specific
610                    value in the following switch statement
611                 */
612                 new_hdr->parsed=0;
613
614                 switch (hdr->type)
615                 {
616                              /* Ignore auxiliary header types */
617                 case HDR_ERROR_T:
618                 case HDR_OTHER_T:
619                 case HDR_VIA2_T:
620                 case HDR_EOH_T:
621                 case HDR_ACCEPTCONTACT_T:
622                 case HDR_ALLOWEVENTS_T:
623                 case HDR_CONTENTENCODING_T:
624                 case HDR_REFERREDBY_T:
625                 case HDR_REJECTCONTACT_T:
626                 case HDR_REQUESTDISPOSITION_T:
627                 case HDR_WWW_AUTHENTICATE_T:
628                 case HDR_PROXY_AUTHENTICATE_T:
629                 case HDR_DATE_T:
630                 case HDR_IDENTITY_T:
631                 case HDR_IDENTITY_INFO_T:
632                 case HDR_RETRY_AFTER_T:
633                 case HDR_REASON_T:
634                         break;
635
636                 case HDR_VIA_T:
637                         if ( !new_msg->via1 ) {
638                                 new_msg->h_via1 = new_hdr;
639                                 new_msg->via1 = via_body_cloner(new_msg->buf,
640                                                                 org_msg->buf, (struct via_body*)hdr->parsed, &p);
641                                 new_hdr->parsed  = (void*)new_msg->via1;
642                                 if ( new_msg->via1->next ) {
643                                         new_msg->via2 = new_msg->via1->next;
644                                 }
645                         } else if ( !new_msg->via2 && new_msg->via1 ) {
646                                 new_msg->h_via2 = new_hdr;
647                                 if ( new_msg->via1->next ) {
648                                         new_hdr->parsed = (void*)new_msg->via1->next;
649                                 } else {
650                                         new_msg->via2 = via_body_cloner( new_msg->buf,
651                                                                          org_msg->buf, (struct via_body*)hdr->parsed, &p);
652                                         new_hdr->parsed  = (void*)new_msg->via2;
653                                 }
654                         } else if ( new_msg->via2 && new_msg->via1 ) {
655                                 new_hdr->parsed = via_body_cloner( new_msg->buf , org_msg->buf ,
656                                                                    (struct via_body*)hdr->parsed , &p);
657                         }
658                         break;
659                 case HDR_CSEQ_T:
660                         new_hdr->parsed = p;
661                         p +=ROUND4(sizeof(struct cseq_body));
662                         memcpy(new_hdr->parsed, hdr->parsed, sizeof(struct cseq_body));
663                         ((struct cseq_body*)new_hdr->parsed)->number.s =
664                                 translate_pointer(new_msg->buf ,org_msg->buf,
665                                                   ((struct cseq_body*)hdr->parsed)->number.s );
666                         ((struct cseq_body*)new_hdr->parsed)->method.s =
667                                 translate_pointer(new_msg->buf ,org_msg->buf,
668                                                   ((struct cseq_body*)hdr->parsed)->method.s );
669                         if (!HOOK_SET(cseq)) new_msg->cseq = new_hdr;
670                         break;
671                 case HDR_TO_T:
672                 case HDR_FROM_T:
673                         if (hdr->type == HDR_TO_T) {
674                                 if (!HOOK_SET(to)) new_msg->to = new_hdr;
675                         } else {
676                                 if (!HOOK_SET(from)) new_msg->from = new_hdr;
677                         }
678                              /* From header might be unparsed */
679                         if (!hdr->parsed) break;
680                         new_hdr->parsed = p;
681                         p +=ROUND4(sizeof(struct to_body));
682                         memcpy(new_hdr->parsed, hdr->parsed, sizeof(struct to_body));
683                         ((struct to_body*)new_hdr->parsed)->body.s =
684                                 translate_pointer( new_msg->buf , org_msg->buf ,
685                                                    ((struct to_body*)hdr->parsed)->body.s );
686                         ((struct to_body*)new_hdr->parsed)->display.s =
687                                 translate_pointer( new_msg->buf, org_msg->buf,
688                                                    ((struct to_body*)hdr->parsed)->display.s);
689                         ((struct to_body*)new_hdr->parsed)->uri.s =
690                                 translate_pointer( new_msg->buf , org_msg->buf ,
691                                                    ((struct to_body*)hdr->parsed)->uri.s );
692                         if ( ((struct to_body*)hdr->parsed)->tag_value.s )
693                                 ((struct to_body*)new_hdr->parsed)->tag_value.s =
694                                         translate_pointer( new_msg->buf , org_msg->buf ,
695                                                            ((struct to_body*)hdr->parsed)->tag_value.s );
696                         if ( (((struct to_body*)new_hdr->parsed)->parsed_uri.user.s)
697                                 || (((struct to_body*)new_hdr->parsed)->parsed_uri.host.s) )
698                                         uri_trans(new_msg->buf, org_msg->buf,
699                                                         &((struct to_body*)new_hdr->parsed)->parsed_uri);
700                              /*to params*/
701                         to_prm = ((struct to_body*)(hdr->parsed))->param_lst;
702                         for(;to_prm;to_prm=to_prm->next) {
703                                      /*alloc*/
704                                 new_to_prm = (struct to_param*)p;
705                                 p +=ROUND4(sizeof(struct to_param ));
706                                      /*coping*/
707                                 memcpy( new_to_prm, to_prm, sizeof(struct to_param ));
708                                 ((struct to_body*)new_hdr->parsed)->param_lst = 0;
709                                 new_to_prm->name.s = translate_pointer( new_msg->buf,
710                                                                         org_msg->buf , to_prm->name.s );
711                                 new_to_prm->value.s = translate_pointer( new_msg->buf,
712                                                                          org_msg->buf , to_prm->value.s );
713                                      /*linking*/
714                                 if ( !((struct to_body*)new_hdr->parsed)->param_lst )
715                                         ((struct to_body*)new_hdr->parsed)->param_lst
716                                                 = new_to_prm;
717                                 else
718                                         ((struct to_body*)new_hdr->parsed)->last_param->next
719                                                 = new_to_prm;
720                                 ((struct to_body*)new_hdr->parsed)->last_param
721                                         = new_to_prm;
722                         }
723                         break;
724                 case HDR_CALLID_T:
725                         if (!HOOK_SET(callid)) {
726                                 new_msg->callid = new_hdr;
727                         }
728                         break;
729                 case HDR_CONTACT_T:
730                         if (!HOOK_SET(contact)) {
731                                 new_msg->contact = new_hdr;
732                         }
733                         break;
734                 case HDR_MAXFORWARDS_T:
735                         if (!HOOK_SET(maxforwards)) {
736                                 new_msg->maxforwards = new_hdr;
737                                 new_msg->maxforwards->parsed = hdr->parsed;
738                         }
739                         break;
740                 case HDR_ROUTE_T:
741                         if (!HOOK_SET(route)) {
742                                 new_msg->route = new_hdr;
743                         }
744                         break;
745                 case HDR_RECORDROUTE_T:
746                         if (!HOOK_SET(record_route)) {
747                                 new_msg->record_route = new_hdr;
748                         }
749                         break;
750                 case HDR_CONTENTTYPE_T:
751                         if (!HOOK_SET(content_type)) {
752                                 new_msg->content_type = new_hdr;
753                                 new_msg->content_type->parsed = hdr->parsed;
754                         }
755                         break;
756                 case HDR_CONTENTLENGTH_T:
757                         if (!HOOK_SET(content_length)) {
758                                 new_msg->content_length = new_hdr;
759                                 new_msg->content_length->parsed = hdr->parsed;
760                         }
761                         break;
762                 case HDR_AUTHORIZATION_T:
763                         if (!HOOK_SET(authorization)) {
764                                 new_msg->authorization = new_hdr;
765                         }
766                         if (hdr->parsed) {
767                                 new_hdr->parsed = auth_body_cloner(new_msg->buf ,
768                                                                    org_msg->buf , (struct auth_body*)hdr->parsed , &p);
769                         }
770                         break;
771                 case HDR_EXPIRES_T:
772                         if (!HOOK_SET(expires)) {
773                                 new_msg->expires = new_hdr;
774                         }
775                         break;
776                 case HDR_MIN_EXPIRES_T:
777                         if (!HOOK_SET(min_expires)) {
778                                 new_msg->min_expires = new_hdr;
779                         }
780                         break;
781                 case HDR_PROXYAUTH_T:
782                         if (!HOOK_SET(proxy_auth)) {
783                                 new_msg->proxy_auth = new_hdr;
784                         }
785                         if (hdr->parsed) {
786                                 new_hdr->parsed = auth_body_cloner(new_msg->buf ,
787                                                                    org_msg->buf , (struct auth_body*)hdr->parsed , &p);
788                         }
789                         break;
790                 case HDR_SUPPORTED_T:
791                         if (!HOOK_SET(supported)) {
792                                 new_msg->supported = new_hdr;
793                         }
794                         break;
795                 case HDR_REQUIRE_T:
796                         if (!HOOK_SET(require)) {
797                                 new_msg->require = new_hdr;
798                         }
799                         break;
800                 case HDR_PROXYREQUIRE_T:
801                         if (!HOOK_SET(proxy_require)) {
802                                 new_msg->proxy_require = new_hdr;
803                         }
804                         break;
805                 case HDR_UNSUPPORTED_T:
806                         if (!HOOK_SET(unsupported)) {
807                                 new_msg->unsupported = new_hdr;
808                         }
809                         break;
810                 case HDR_ALLOW_T:
811                         if (!HOOK_SET(allow)) {
812                                 new_msg->allow = new_hdr;
813                         }
814                         break;
815                 case HDR_EVENT_T:
816                         if (!HOOK_SET(event)) {
817                                 new_msg->event = new_hdr;
818                         }
819                         break;
820                 case HDR_ACCEPT_T:
821                         if (!HOOK_SET(accept)) {
822                                 new_msg->accept = new_hdr;
823                         }
824                         break;
825                 case HDR_ACCEPTLANGUAGE_T:
826                         if (!HOOK_SET(accept_language)) {
827                                 new_msg->accept_language = new_hdr;
828                         }
829                         break;
830                 case HDR_ORGANIZATION_T:
831                         if (!HOOK_SET(organization)) {
832                                 new_msg->organization = new_hdr;
833                         }
834                         break;
835                 case HDR_PRIORITY_T:
836                         if (!HOOK_SET(priority)) {
837                                 new_msg->priority = new_hdr;
838                         }
839                         break;
840                 case HDR_SUBJECT_T:
841                         if (!HOOK_SET(subject)) {
842                                 new_msg->subject = new_hdr;
843                         }
844                         break;
845                 case HDR_USERAGENT_T:
846                         if (!HOOK_SET(user_agent)) {
847                                 new_msg->user_agent = new_hdr;
848                         }
849                         break;
850                 case HDR_SERVER_T:
851                         if (!HOOK_SET(server)) {
852                                 new_msg->server = new_hdr;
853                         }
854                         break;
855                 case HDR_CONTENTDISPOSITION_T:
856                         if (!HOOK_SET(content_disposition)) {
857                                 new_msg->content_disposition = new_hdr;
858                         }
859                         break;
860                 case HDR_DIVERSION_T:
861                         if (!HOOK_SET(diversion)) {
862                                 new_msg->diversion = new_hdr;
863                         }
864                         break;
865                 case HDR_RPID_T:
866                         if (!HOOK_SET(rpid)) {
867                                 new_msg->rpid = new_hdr;
868                         }
869                         break;
870                 case HDR_REFER_TO_T:
871                         if (!HOOK_SET(refer_to)) {
872                                 new_msg->refer_to = new_hdr;
873                         }
874                         break;
875                 case HDR_SESSIONEXPIRES_T:
876                         if (!HOOK_SET(session_expires)) {
877                                 new_msg->session_expires = new_hdr;
878                         }
879                         break;
880                 case HDR_MIN_SE_T:
881                         if (!HOOK_SET(min_se)) {
882                                 new_msg->min_se = new_hdr;
883                         }
884                         break;
885                 case HDR_SUBSCRIPTION_STATE_T:
886                         if (!HOOK_SET(subscription_state)) {
887                                 new_msg->subscription_state = new_hdr;
888                         }
889                         break;
890                 case HDR_SIPIFMATCH_T:
891                         if (!HOOK_SET(sipifmatch)) {
892                                 new_msg->sipifmatch = new_hdr;
893                         }
894                         break;
895                 case HDR_PPI_T:
896                         if (!HOOK_SET(ppi)) {
897                                 new_msg->ppi = new_hdr;
898                         }
899                         break;
900                 case HDR_PAI_T:
901                         if (!HOOK_SET(pai)) {
902                                 new_msg->pai = new_hdr;
903                         }
904                         break;
905                 case HDR_PATH_T:
906                         if (!HOOK_SET(path)) {
907                                 new_msg->path = new_hdr;
908                         }
909                         break;
910                 case HDR_PRIVACY_T:
911                         if (!HOOK_SET(privacy)) {
912                                 new_msg->privacy = new_hdr;
913                         }
914                         break;
915                 }/*switch*/
916
917                 if ( last_hdr )
918                 {
919                         last_hdr->next = new_hdr;
920                         last_hdr=last_hdr->next;
921                 }
922                 else
923                 {
924                         last_hdr=new_hdr;
925                         new_msg->headers =new_hdr;
926                 }
927                 last_hdr->next = 0;
928                 new_msg->last_header = last_hdr;
929         }
930         if (clone_lumps) {
931                 /*cloning data and reply lump structures*/
932                 CLONE_LUMP_LIST(&(new_msg->add_rm), org_msg->add_rm, p);
933                 CLONE_LUMP_LIST(&(new_msg->body_lumps), org_msg->body_lumps, p);
934                 CLONE_RPL_LUMP_LIST(&(new_msg->reply_lump), org_msg->reply_lump, p);
935         }
936         
937         if (clone_authorized_hooks(new_msg, org_msg) < 0) {
938                 shm_free(new_msg);
939                 return 0;
940         }
941
942         return new_msg;
943 }
944
945
946
947 /** clones the data and reply lumps from pkg_msg to shm_msg.
948  * A new memory block is allocated for the lumps (the lumps will point
949  * into it).
950  * Note: the new memory block is linked to add_rm if
951  * at least one data lump is set, else it is linked to body_lumps
952  * if at least one body lump is set, otherwise it is linked to
953  * shm_msg->reply_lump.
954  * @param pkg_msg - sip msg whoes lumps will be cloned
955  * @param add_rm - result parameter, filled with the list of cloned
956  *                 add_rm lumps (corresp. to msg->add_rm)
957  * @param body_lumps - result parameter, filled with the list of cloned
958  *                 body lumps (corresp. to msg->body_lumps)
959  * @param reply_lump - result parameter, filled with the list of cloned
960  *                 reply lumps (corresp. to msg->reply_lump)
961  * @return 0 or 1 on success: 0 - lumps cloned), 1 - nothing to do and 
962  *         -1 on error
963  */
964 int msg_lump_cloner(struct sip_msg *pkg_msg,
965                                         struct lump** add_rm,
966                                         struct lump** body_lumps,
967                                         struct lump_rpl** reply_lump)
968 {
969         unsigned int    len;
970         char            *p;
971
972         *add_rm = *body_lumps = 0;
973         *reply_lump = 0;
974
975         /* calculate the length of the lumps */
976         len = 0;
977         LUMP_LIST_LEN(len, pkg_msg->add_rm);
978         LUMP_LIST_LEN(len, pkg_msg->body_lumps);
979         RPL_LUMP_LIST_LEN(len, pkg_msg->reply_lump);
980
981         if (!len)
982                 return 1; /* nothing to do */
983
984         p=(char *)shm_malloc(len);
985         if (!p)
986         {
987                 LM_ERR("cannot allocate memory\n" );
988                 return -1;
989         }
990
991         /* clone the lumps */
992         CLONE_LUMP_LIST(add_rm, pkg_msg->add_rm, p);
993         CLONE_LUMP_LIST(body_lumps, pkg_msg->body_lumps, p);
994         CLONE_RPL_LUMP_LIST(reply_lump, pkg_msg->reply_lump, p);
995
996         return 0;
997 }
998
999
1000
1001 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */