Module: kamailio
Branch: master
Commit: 1a67b637d928a2d94b4ef3b6d6cc03c196492eee
URL: 
https://github.com/kamailio/kamailio/commit/1a67b637d928a2d94b4ef3b6d6cc03c196492eee

Author: Daniel-Constantin Mierla <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2026-01-19T07:57:23+01:00

core: helper function to copy sip message to local memory

---

Modified: src/core/sip_msg_clone.c
Modified: src/core/sip_msg_clone.h

---

Diff:  
https://github.com/kamailio/kamailio/commit/1a67b637d928a2d94b4ef3b6d6cc03c196492eee.diff
Patch: 
https://github.com/kamailio/kamailio/commit/1a67b637d928a2d94b4ef3b6d6cc03c196492eee.patch

---

diff --git a/src/core/sip_msg_clone.c b/src/core/sip_msg_clone.c
index 4b8b0e7a052..d3092e08ce8 100644
--- a/src/core/sip_msg_clone.c
+++ b/src/core/sip_msg_clone.c
@@ -996,5 +996,98 @@ int msg_lump_cloner(struct sip_msg *pkg_msg, struct lump 
**add_rm,
        return 0;
 }
 
+/**
+ *
+ */
+int sip_msg_copy(sip_msg_t *imsg, sip_msg_t *omsg, unsigned int flags)
+{
+       if(imsg == NULL || omsg == NULL) {
+               LM_ERR("invalid parameters\n");
+               return -1;
+       }
+
+       if(omsg->buf == NULL || imsg->buf_size > omsg->buf_size) {
+               LM_ERR("invalid output buffer\n");
+               return -1;
+       }
 
+       memcpy(omsg->buf, imsg->buf, imsg->len);
+       omsg->len = imsg->len;
+       omsg->buf[omsg->len] = '\0';
+
+       /* parse the output message */
+       LM_DBG("SIP message content copied - reparsing\n");
+       if(parse_msg(omsg->buf, omsg->len, omsg) != 0) {
+               LM_ERR("parsing out sip message failed [[%.*s]]\n", omsg->len,
+                               omsg->buf);
+               goto error;
+       }
+       if(parse_headers(omsg, HDR_FROM_F | HDR_TO_F | HDR_CALLID_F | 
HDR_CSEQ_F, 0)
+                       < 0) {
+               LM_ERR("parsing main headers of new sip message failed 
[[%.*s]]\n",
+                               omsg->len, omsg->buf);
+               goto error;
+       }
+
+       omsg->id = imsg->id;
+       omsg->pid = imsg->pid;
+       omsg->tval = imsg->tval;
+
+       omsg->fwd_send_flags = imsg->fwd_send_flags;
+       omsg->rpl_send_flags = imsg->rpl_send_flags;
+       omsg->rcv = imsg->rcv;
+
+       omsg->set_global_address = imsg->set_global_address;
+       omsg->set_global_port = imsg->set_global_port;
+       omsg->flags = imsg->flags;
+       omsg->msg_flags = imsg->msg_flags;
+       memcpy(omsg->xflags, imsg->xflags, KSR_XFLAGS_SIZE * sizeof(flag_t));
+       omsg->hash_index = imsg->hash_index;
+       omsg->force_send_socket = imsg->force_send_socket;
+
+       omsg->reg_id = imsg->reg_id;
+       omsg->otcpid = imsg->otcpid;
+       omsg->hash_index = imsg->hash_index;
+
+       /* duplicate */
+       if(imsg->new_uri.s != NULL) {
+               if(pkg_str_dup(&omsg->new_uri, &imsg->new_uri) < 0) {
+                       goto error;
+               }
+       }
+       if(imsg->dst_uri.s != NULL) {
+               if(pkg_str_dup(&omsg->dst_uri, &imsg->dst_uri) < 0) {
+                       goto error;
+               }
+       }
+       if(imsg->path_vec.s != NULL) {
+               if(pkg_str_dup(&omsg->path_vec, &imsg->path_vec) < 0) {
+                       goto error;
+               }
+       }
+       if(imsg->instance.s != NULL) {
+               if(pkg_str_dup(&omsg->instance, &imsg->instance) < 0) {
+                       goto error;
+               }
+       }
+       if(imsg->ruid.s != NULL) {
+               if(pkg_str_dup(&omsg->ruid, &imsg->ruid) < 0) {
+                       goto error;
+               }
+       }
+       if(imsg->location_ua.s != NULL) {
+               if(pkg_str_dup(&omsg->location_ua, &imsg->location_ua) < 0) {
+                       goto error;
+               }
+       }
+
+       memcpy(omsg->add_to_branch_s, imsg->add_to_branch_s, 
MAX_BRANCH_PARAM_LEN);
+       omsg->add_to_branch_len = omsg->add_to_branch_len;
+
+       return 0;
+
+error:
+       free_sip_msg(omsg);
+       return -1;
+}
 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */
diff --git a/src/core/sip_msg_clone.h b/src/core/sip_msg_clone.h
index 4b768df7438..8789a791628 100644
--- a/src/core/sip_msg_clone.h
+++ b/src/core/sip_msg_clone.h
@@ -27,6 +27,7 @@ struct sip_msg *sip_msg_shm_clone(
 int msg_lump_cloner(struct sip_msg *pkg_msg, struct lump **add_rm,
                struct lump **body_lumps, struct lump_rpl **reply_lump);
 
+int sip_msg_copy(sip_msg_t *imsg, sip_msg_t *omsg, unsigned int flags);
 
 #endif /*__sip_msg_clone_h*/
 

_______________________________________________
Kamailio - Development Mailing List -- [email protected]
To unsubscribe send an email to [email protected]
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to