Clones the sip_msg_body structure attached to a sip msg into shm or pkg * memory. * Parameters: * * src_msg - the original sip_msg containing the sip_msg_body to be cloned * It can be in shm or pkg and it is mandatory. * * dst_msg - the destination sip_msg - this is need only when cloning into * shm, as we need to translate to this new sip msg all the *
| 483 | * * shared - 1 if to SHM or 0 if to PKG |
| 484 | */ |
| 485 | int clone_sip_msg_body(struct sip_msg *src_msg, struct sip_msg *dst_msg, |
| 486 | struct sip_msg_body **p_dst, int shared) |
| 487 | { |
| 488 | struct sip_msg_body *dst, *src; |
| 489 | struct body_part *p, *np; |
| 490 | osips_malloc_f my_malloc; |
| 491 | int extra_len; |
| 492 | |
| 493 | if (src_msg==NULL || src_msg->body==NULL) { |
| 494 | *p_dst = NULL; |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | my_malloc = shared ? shm_malloc_func : pkg_malloc_func; |
| 499 | src = src_msg->body; |
| 500 | |
| 501 | /* clone the SIP MSG BODY */ |
| 502 | extra_len = (src->flags&SIP_BODY_FLAG_NEW) ? |
| 503 | src->first.mime_s.len+src->first.body.len : 0 ; |
| 504 | if ( (dst=func_malloc(my_malloc, sizeof(struct sip_msg_body)+extra_len))==NULL ) { |
| 505 | LM_ERR("failed to allocate new sip_msg_body clone (shared=%d)\n", |
| 506 | shared); |
| 507 | goto err; |
| 508 | } |
| 509 | memcpy( dst, src, sizeof(struct sip_msg_body)+extra_len); |
| 510 | if (shared) |
| 511 | dst->flags |= SIP_BODY_FLAG_SHM; |
| 512 | else |
| 513 | dst->flags &= ~SIP_BODY_FLAG_SHM; |
| 514 | /* update the links inside it */ |
| 515 | if (dst_msg) { \ |
| 516 | dst->body.s = translate_pointer(dst_msg->buf ,src_msg->buf, |
| 517 | src->body.s ); |
| 518 | dst->boundary.s = translate_pointer(dst_msg->buf ,src_msg->buf, |
| 519 | src->boundary.s ); |
| 520 | } |
| 521 | /* clone the body parts */ |
| 522 | for( p=&src->first,np=NULL ; p ; p=p->next) { |
| 523 | if (np==NULL) { \ |
| 524 | /* first body part */ |
| 525 | np = &dst->first; |
| 526 | extra_len = 0; |
| 527 | } else { |
| 528 | extra_len = (p->flags&SIP_BODY_PART_FLAG_NEW) ? |
| 529 | p->mime_s.len+p->body.len+p->headers.len : 0 ; |
| 530 | if((np->next=func_malloc(my_malloc, sizeof(struct body_part)+extra_len))==NULL){ |
| 531 | LM_ERR("failed to allocate new body_part clone (shared=%d)\n", |
| 532 | shared); |
| 533 | goto err; |
| 534 | } \ |
| 535 | np = np->next; |
| 536 | } \ |
| 537 | memcpy( np, p, sizeof(struct body_part)+extra_len); |
| 538 | /* update the links inside it */ |
| 539 | if (p->flags&SIP_BODY_PART_FLAG_NEW) { |
| 540 | /* links are pointing inside the body_part structure */ |
| 541 | if (p==&src->first) { |
| 542 | np->body.s = translate_pointer((char*)dst ,(char*)src, |
no test coverage detected