Browse code

textopsx: use functions from core to apply changes

Daniel-Constantin Mierla authored on 02/09/2018 10:42:37
Showing 1 changed files
... ...
@@ -145,8 +145,6 @@ static int mod_init(void)
145 145
  */
146 146
 static int ki_msg_update_buffer(sip_msg_t *msg, str *obuf)
147 147
 {
148
-	sip_msg_t tmp;
149
-
150 148
 	if(obuf==NULL || obuf->s==NULL || obuf->len<=0) {
151 149
 		LM_ERR("invalid buffer parameter\n");
152 150
 		return -1;
... ...
@@ -156,52 +154,8 @@ static int ki_msg_update_buffer(sip_msg_t *msg, str *obuf)
156 154
 		LM_ERR("new buffer is too large (%d)\n", obuf->len);
157 155
 		return -1;
158 156
 	}
159
-	/* temporary copy */
160
-	memcpy(&tmp, msg, sizeof(sip_msg_t));
161
-
162
-	/* reset dst uri and path vector to avoid freeing - restored later */
163
-	if(msg->dst_uri.s != NULL) {
164
-		msg->dst_uri.s = NULL;
165
-		msg->dst_uri.len = 0;
166
-	}
167
-	if(msg->path_vec.s != NULL) {
168
-		msg->path_vec.s = NULL;
169
-		msg->path_vec.len = 0;
170
-	}
171
-
172
-	/* free old msg structure */
173
-	free_sip_msg(msg);
174
-	memset(msg, 0, sizeof(sip_msg_t));
175
-
176
-	/* restore msg fields */
177
-	msg->buf = tmp.buf;
178
-	msg->id = tmp.id;
179
-	msg->rcv = tmp.rcv;
180
-	msg->set_global_address = tmp.set_global_address;
181
-	msg->set_global_port = tmp.set_global_port;
182
-	msg->flags = tmp.flags;
183
-	msg->msg_flags = tmp.msg_flags;
184
-	msg->hash_index = tmp.hash_index;
185
-	msg->force_send_socket = tmp.force_send_socket;
186
-	msg->fwd_send_flags = tmp.fwd_send_flags;
187
-	msg->rpl_send_flags = tmp.rpl_send_flags;
188
-	msg->dst_uri = tmp.dst_uri;
189
-	msg->path_vec = tmp.path_vec;
190
-
191
-	memcpy(msg->buf, obuf->s, obuf->len);
192
-	msg->len = obuf->len;
193
-	msg->buf[msg->len] = '\0';
194
-
195
-	/* reparse the message */
196
-	LM_DBG("SIP message content updated - reparsing\n");
197
-	if(parse_msg(msg->buf, msg->len, msg) != 0) {
198
-		LM_ERR("parsing new sip message failed [[%.*s]]\n", msg->len, msg->buf);
199
-		/* exit config execution - sip_msg_t structure is no longer
200
-		 * valid/safe for config */
201
-		return 0;
202
-	}
203 157
 
204
-	return 1;
158
+	return sip_msg_update_buffer(msg, obuf);
205 159
 }
206 160
 
207 161
 /**
... ...
@@ -222,39 +176,12 @@ static int ki_msg_set_buffer(sip_msg_t *msg, str *obuf)
222 176
  */
223 177
 static int ki_msg_apply_changes(sip_msg_t *msg)
224 178
 {
225
-	int ret;
226
-	dest_info_t dst;
227
-	str obuf;
228
-
229 179
 	if(msg->first_line.type != SIP_REPLY && get_route_type() != REQUEST_ROUTE) {
230 180
 		LM_ERR("invalid usage - not in request route or a reply\n");
231 181
 		return -1;
232 182
 	}
233 183
 
234
-	init_dest_info(&dst);
235
-	dst.proto = PROTO_UDP;
236
-	if(msg->first_line.type == SIP_REPLY) {
237
-		obuf.s = generate_res_buf_from_sip_res(
238
-				msg, (unsigned int *)&obuf.len, BUILD_NO_VIA1_UPDATE);
239
-	} else {
240
-		if(msg->msg_flags & FL_RR_ADDED) {
241
-			LM_ERR("cannot apply msg changes after adding record-route"
242
-				   " header - it breaks conditional 2nd header\n");
243
-			return -1;
244
-		}
245
-		obuf.s = build_req_buf_from_sip_req(msg, (unsigned int *)&obuf.len,
246
-				&dst,
247
-				BUILD_NO_PATH | BUILD_NO_LOCAL_VIA | BUILD_NO_VIA1_UPDATE);
248
-	}
249
-	if(obuf.s == NULL) {
250
-		LM_ERR("couldn't update msg buffer content\n");
251
-		return -1;
252
-	}
253
-	ret = ki_msg_update_buffer(msg, &obuf);
254
-	/* free new buffer - copied in the static buffer from old sip_msg_t */
255
-	pkg_free(obuf.s);
256
-
257
-	return ret;
184
+	return sip_msg_apply_changes(msg);
258 185
 }
259 186
 
260 187
 /**