Sfoglia il codice sorgente

tm: avoid temporary clone of reason value for t_reply()

- use str field instead of char* for internal _reply() parameter
- split the w_t_reply() to config wrapper and helper functions
Daniel-Constantin Mierla 7 anni fa
parent
commit
162d7feb00
3 ha cambiato i file con 58 aggiunte e 32 eliminazioni
  1. 26 9
      src/modules/tm/t_reply.c
  2. 3 0
      src/modules/tm/t_reply.h
  3. 29 23
      src/modules/tm/tm.c

+ 26 - 9
src/modules/tm/t_reply.c

@@ -626,13 +626,12 @@ error:
  * returns 1 if everything was OK or -1 for error
  */
 static int _reply( struct cell *trans, struct sip_msg* p_msg,
-	unsigned int code, char * text, int lock )
+	unsigned int code, str *reason, int lock )
 {
 	unsigned int len;
 	char * buf, *dset;
 	struct bookmark bm;
 	int dset_len;
-	str reason;
 
 	if (code>=200) set_kr(REQ_RPLD);
 	/* compute the buffer in private memory prior to entering lock;
@@ -646,18 +645,16 @@ static int _reply( struct cell *trans, struct sip_msg* p_msg,
 		}
 	}
 
-	reason.s = text;
-	reason.len = strlen(text);
 	if (code>=180 && p_msg->to
 				&& (get_to(p_msg)->tag_value.s==0
 					|| get_to(p_msg)->tag_value.len==0)) {
 		calc_crc_suffix( p_msg, tm_tag_suffix );
-		buf = build_res_buf_from_sip_req(code, &reason, &tm_tag, p_msg,
+		buf = build_res_buf_from_sip_req(code, reason, &tm_tag, p_msg,
 				&len, &bm);
 		return _reply_light( trans, buf, len, code,
 			tm_tag.s, TOTAG_VALUE_LEN, lock, &bm);
 	} else {
-		buf = build_res_buf_from_sip_req(code, &reason, 0 /*no to-tag*/,
+		buf = build_res_buf_from_sip_req(code, reason, 0 /*no to-tag*/,
 			p_msg, &len, &bm);
 		return _reply_light(trans,buf,len,code,
 			0, 0, /* no to-tag */lock, &bm);
@@ -1600,16 +1597,36 @@ error:
 int t_reply( struct cell *t, struct sip_msg* p_msg, unsigned int code,
 	char * text )
 {
-	return _reply( t, p_msg, code, text, 1 /* lock replies */ );
+	str reason;
+
+	reason.s = text;
+	reason.len = strlen(text);
+
+	return _reply( t, p_msg, code, &reason, 1 /* lock replies */ );
+}
+
+int t_reply_str( struct cell *t, struct sip_msg* p_msg, unsigned int code,
+	str* reason)
+{
+	return _reply( t, p_msg, code, reason, 1 /* lock replies */ );
 }
 
 int t_reply_unsafe( struct cell *t, struct sip_msg* p_msg, unsigned int code,
 	char * text )
 {
-	return _reply( t, p_msg, code, text, 0 /* don't lock replies */ );
-}
+	str reason;
 
+	reason.s = text;
+	reason.len = strlen(text);
 
+	return _reply( t, p_msg, code, &reason, 0 /* don't lock replies */ );
+}
+
+int t_reply_str_unsafe( struct cell *t, struct sip_msg* p_msg, unsigned int code,
+	str* reason)
+{
+	return _reply( t, p_msg, code, reason, 0 /* don't lock replies */ );
+}
 
 void set_final_timer( struct cell *t )
 {

+ 3 - 0
src/modules/tm/t_reply.h

@@ -183,7 +183,10 @@ int t_reply( struct cell *t, struct sip_msg * , unsigned int , char * );
    REPLY_LOCK -- useful to be called within reply
    processing
 */
+int t_reply_str( struct cell *t, struct sip_msg * , unsigned int , str * );
+
 int t_reply_unsafe( struct cell *t, struct sip_msg * , unsigned int , char * );
+int t_reply_str_unsafe( struct cell *t, struct sip_msg * , unsigned int , str * );
 
 
 enum rps relay_reply( struct cell *t, struct sip_msg *p_msg, int branch,

+ 29 - 23
src/modules/tm/tm.c

@@ -1311,13 +1311,10 @@ static int w_t_forward_nonack_to( struct sip_msg  *p_msg ,
 	return r;
 }
 
-
-static int w_t_reply(struct sip_msg* msg, char* p1, char* p2)
+static int t_reply_helper(sip_msg_t* msg, int code, str* reason)
 {
-	struct cell *t;
-	int code, ret = -1;
-	str reason;
-	char* r;
+	tm_cell_t *t = NULL;
+	int ret = -1;
 
 	if (msg->REQ_METHOD==METHOD_ACK) {
 		LM_DBG("ACKs are not replied\n");
@@ -1330,18 +1327,6 @@ static int w_t_reply(struct sip_msg* msg, char* p1, char* p2)
 				" for which no T-state has been established\n");
 		return -1;
 	}
-
-	if (get_int_fparam(&code, msg, (fparam_t*)p1) < 0) {
-		code = cfg_get(tm, tm_cfg, default_code);
-	}
-
-	if (get_str_fparam(&reason, msg, (fparam_t*)p2) < 0) {
-		r = cfg_get(tm, tm_cfg, default_reason);
-	} else {
-		r = as_asciiz(&reason);
-		if (r == NULL) r = cfg_get(tm, tm_cfg, default_reason);
-	}
-
 	/* if called from reply_route, make sure that unsafe version
 	 * is called; we are already in a mutex and another mutex in
 	 * the safe version would lead to a deadlock
@@ -1350,15 +1335,15 @@ static int w_t_reply(struct sip_msg* msg, char* p1, char* p2)
 	t->flags |= T_ADMIN_REPLY;
 	if (is_route_type(FAILURE_ROUTE)) {
 		LM_DBG("t_reply_unsafe called from w_t_reply\n");
-		ret = t_reply_unsafe(t, msg, code, r);
+		ret = t_reply_str_unsafe(t, msg, code, reason);
 	} else if (is_route_type(REQUEST_ROUTE)) {
-		ret = t_reply( t, msg, code, r);
+		ret = t_reply_str( t, msg, code, reason);
 	} else if (is_route_type(ONREPLY_ROUTE)) {
 		if (likely(t->uas.request)){
 			if (is_route_type(CORE_ONREPLY_ROUTE))
-				ret=t_reply(t, t->uas.request, code, r);
+				ret=t_reply_str(t, t->uas.request, code, reason);
 			else
-				ret=t_reply_unsafe(t, t->uas.request, code, r);
+				ret=t_reply_str_unsafe(t, t->uas.request, code, reason);
 		}else
 			ret=-1;
 		/* t_check() above has the side effect of setting T and
@@ -1375,10 +1360,31 @@ static int w_t_reply(struct sip_msg* msg, char* p1, char* p2)
 		ret = -1;
 	}
 
-	if (r && (r != cfg_get(tm, tm_cfg, default_reason))) pkg_free(r);
 	return ret;
 }
 
+static int w_t_reply(struct sip_msg* msg, char* p1, char* p2)
+{
+	int code, ret = -1;
+	str reason;
+
+	if (msg->REQ_METHOD==METHOD_ACK) {
+		LM_DBG("ACKs are not replied\n");
+		return -1;
+	}
+
+	if (get_int_fparam(&code, msg, (fparam_t*)p1) < 0) {
+		code = cfg_get(tm, tm_cfg, default_code);
+	}
+
+	if (get_str_fparam(&reason, msg, (fparam_t*)p2) < 0) {
+		reason.s = cfg_get(tm, tm_cfg, default_reason);
+		reason.len = strlen(reason.s);
+	}
+
+	return t_reply_helper(msg, code, &reason);
+}
+
 
 static int t_release(sip_msg_t* msg)
 {