[sr-dev] git:master:0a626a25: textops: split the code for msg_apply_changes()

Daniel-Constantin Mierla miconda at gmail.com
Wed Nov 22 09:05:37 CET 2017


Module: kamailio
Branch: master
Commit: 0a626a257dceb0c7cee13bf4ed173b644a5e5fd7
URL: https://github.com/kamailio/kamailio/commit/0a626a257dceb0c7cee13bf4ed173b644a5e5fd7

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2017-11-22T09:05:14+01:00

textops: split the code for msg_apply_changes()

- exported msg_set_buffer(...) to kemi framework

---

Modified: src/modules/textopsx/textopsx.c

---

Diff:  https://github.com/kamailio/kamailio/commit/0a626a257dceb0c7cee13bf4ed173b644a5e5fd7.diff
Patch: https://github.com/kamailio/kamailio/commit/0a626a257dceb0c7cee13bf4ed173b644a5e5fd7.patch

---

diff --git a/src/modules/textopsx/textopsx.c b/src/modules/textopsx/textopsx.c
index d7a948c8ea..6d188e8622 100644
--- a/src/modules/textopsx/textopsx.c
+++ b/src/modules/textopsx/textopsx.c
@@ -143,39 +143,17 @@ static int mod_init(void)
 /**
  *
  */
-static int ki_msg_apply_changes(sip_msg_t *msg)
+static int ki_msg_update_buffer(sip_msg_t *msg, str *obuf)
 {
-	struct dest_info dst;
-	str obuf;
 	sip_msg_t tmp;
 
-	if(msg->first_line.type != SIP_REPLY && get_route_type() != REQUEST_ROUTE) {
-		LM_ERR("invalid usage - not in request route\n");
+	if(obuf==NULL || obuf->s==NULL || obuf->len<=0) {
+		LM_ERR("invalid buffer parameter\n");
 		return -1;
 	}
 
-	init_dest_info(&dst);
-	dst.proto = PROTO_UDP;
-	if(msg->first_line.type == SIP_REPLY) {
-		obuf.s = generate_res_buf_from_sip_res(
-				msg, (unsigned int *)&obuf.len, BUILD_NO_VIA1_UPDATE);
-	} else {
-		if(msg->msg_flags & FL_RR_ADDED) {
-			LM_ERR("cannot apply msg changes after adding record-route"
-				   " header - it breaks conditional 2nd header\n");
-			return -1;
-		}
-		obuf.s = build_req_buf_from_sip_req(msg, (unsigned int *)&obuf.len,
-				&dst,
-				BUILD_NO_PATH | BUILD_NO_LOCAL_VIA | BUILD_NO_VIA1_UPDATE);
-	}
-	if(obuf.s == NULL) {
-		LM_ERR("couldn't update msg buffer content\n");
-		return -1;
-	}
-	if(obuf.len >= BUF_SIZE) {
-		LM_ERR("new buffer overflow (%d)\n", obuf.len);
-		pkg_free(obuf.s);
+	if(obuf->len >= BUF_SIZE) {
+		LM_ERR("new buffer is too large (%d)\n", obuf->len);
 		return -1;
 	}
 	/* temporary copy */
@@ -210,13 +188,10 @@ static int ki_msg_apply_changes(sip_msg_t *msg)
 	msg->dst_uri = tmp.dst_uri;
 	msg->path_vec = tmp.path_vec;
 
-	memcpy(msg->buf, obuf.s, obuf.len);
-	msg->len = obuf.len;
+	memcpy(msg->buf, obuf->s, obuf->len);
+	msg->len = obuf->len;
 	msg->buf[msg->len] = '\0';
 
-	/* free new buffer - copied in the static buffer from old sip_msg_t */
-	pkg_free(obuf.s);
-
 	/* reparse the message */
 	LM_DBG("SIP message content updated - reparsing\n");
 	if(parse_msg(msg->buf, msg->len, msg) != 0) {
@@ -229,6 +204,59 @@ static int ki_msg_apply_changes(sip_msg_t *msg)
 	return 1;
 }
 
+/**
+ *
+ */
+static int ki_msg_set_buffer(sip_msg_t *msg, str *obuf)
+{
+	if(msg->first_line.type != SIP_REPLY && get_route_type() != REQUEST_ROUTE) {
+		LM_ERR("invalid usage - not in request route or a reply\n");
+		return -1;
+	}
+
+	return ki_msg_update_buffer(msg, obuf);
+}
+
+/**
+ *
+ */
+static int ki_msg_apply_changes(sip_msg_t *msg)
+{
+	int ret;
+	dest_info_t dst;
+	str obuf;
+
+	if(msg->first_line.type != SIP_REPLY && get_route_type() != REQUEST_ROUTE) {
+		LM_ERR("invalid usage - not in request route or a reply\n");
+		return -1;
+	}
+
+	init_dest_info(&dst);
+	dst.proto = PROTO_UDP;
+	if(msg->first_line.type == SIP_REPLY) {
+		obuf.s = generate_res_buf_from_sip_res(
+				msg, (unsigned int *)&obuf.len, BUILD_NO_VIA1_UPDATE);
+	} else {
+		if(msg->msg_flags & FL_RR_ADDED) {
+			LM_ERR("cannot apply msg changes after adding record-route"
+				   " header - it breaks conditional 2nd header\n");
+			return -1;
+		}
+		obuf.s = build_req_buf_from_sip_req(msg, (unsigned int *)&obuf.len,
+				&dst,
+				BUILD_NO_PATH | BUILD_NO_LOCAL_VIA | BUILD_NO_VIA1_UPDATE);
+	}
+	if(obuf.s == NULL) {
+		LM_ERR("couldn't update msg buffer content\n");
+		return -1;
+	}
+	ret = ki_msg_update_buffer(msg, &obuf);
+	/* free new buffer - copied in the static buffer from old sip_msg_t */
+	pkg_free(obuf.s);
+
+	return ret;
+}
+
 /**
  *
  */
@@ -2124,6 +2152,11 @@ static sr_kemi_t sr_kemi_textopsx_exports[] = {
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
 	},
+	{ str_init("textopsx"), str_init("msg_set_buffer"),
+		SR_KEMIP_INT, ki_msg_set_buffer,
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
 	{ str_init("textopsx"), str_init("change_reply_status"),
 		SR_KEMIP_INT, ki_change_reply_status,
 		{ SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_NONE,




More information about the sr-dev mailing list