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