Browse code

core: helper function to generate sip reply content without removing top via

Daniel-Constantin Mierla authored on 04/07/2013 20:27:32
Showing 2 changed files
... ...
@@ -1956,8 +1956,8 @@ error00:
1956 1956
 
1957 1957
 
1958 1958
 
1959
-char * build_res_buf_from_sip_res( struct sip_msg* msg,
1960
-				unsigned int *returned_len)
1959
+char * generate_res_buf_from_sip_res( struct sip_msg* msg,
1960
+				unsigned int *returned_len, unsigned int mode)
1961 1961
 {
1962 1962
 	unsigned int new_len, via_len, body_delta;
1963 1963
 	char* new_buf;
... ...
@@ -1968,13 +1968,19 @@ char * build_res_buf_from_sip_res( struct sip_msg* msg,
1968 1968
 	buf=msg->buf;
1969 1969
 	len=msg->len;
1970 1970
 	new_buf=0;
1971
-	/* we must remove the first via */
1972
-	if (msg->via1->next) {
1973
-		via_len=msg->via1->bsize;
1974
-		via_offset=msg->h_via1->body.s-buf;
1971
+
1972
+	if(unlikely(mode&BUILD_NO_VIA1_UPDATE)) {
1973
+		via_len = 0;
1974
+		via_offset = 0;
1975 1975
 	} else {
1976
-		via_len=msg->h_via1->len;
1977
-		via_offset=msg->h_via1->name.s-buf;
1976
+		/* we must remove the first via */
1977
+		if (msg->via1->next) {
1978
+			via_len=msg->via1->bsize;
1979
+			via_offset=msg->h_via1->body.s-buf;
1980
+		} else {
1981
+			via_len=msg->h_via1->len;
1982
+			via_offset=msg->h_via1->name.s-buf;
1983
+		}
1978 1984
 	}
1979 1985
 
1980 1986
 	     /* Calculate message body difference and adjust
... ...
@@ -1983,16 +1989,16 @@ char * build_res_buf_from_sip_res( struct sip_msg* msg,
1983 1983
 	body_delta = lumps_len(msg, msg->body_lumps, 0);
1984 1984
 	if (adjust_clen(msg, body_delta, (msg->via2? msg->via2->proto:PROTO_UDP))
1985 1985
 			< 0) {
1986
-		LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: Error while adjusting"
1987
-				" Content-Length\n");
1986
+		LOG(L_ERR, "error while adjusting Content-Length\n");
1988 1987
 		goto error;
1989 1988
 	}
1990 1989
 
1991
-	/* remove the first via*/
1992
-	if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
1993
-		LOG(L_ERR, "build_res_buf_from_sip_res: error trying to remove first"
1994
-					"via\n");
1995
-		goto error;
1990
+	if(likely(!(mode&BUILD_NO_VIA1_UPDATE))) {
1991
+		/* remove the first via*/
1992
+		if (del_lump( msg, via_offset, via_len, HDR_VIA_T)==0){
1993
+			LOG(L_ERR, "error trying to remove first via\n");
1994
+			goto error;
1995
+		}
1996 1996
 	}
1997 1997
 
1998 1998
 	new_len=len+body_delta+lumps_len(msg, msg->add_rm, 0); /*FIXME: we don't
... ...
@@ -2002,7 +2008,7 @@ char * build_res_buf_from_sip_res( struct sip_msg* msg,
2002 2002
 	new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
2003 2003
 											 (\0 to print it )*/
2004 2004
 	if (new_buf==0){
2005
-		LOG(L_ERR, "ERROR: build_res_buf_from_sip_res: out of mem\n");
2005
+		LOG(L_ERR, "out of mem\n");
2006 2006
 		goto error;
2007 2007
 	}
2008 2008
 	new_buf[new_len]=0; /* debug: print the message */
... ...
@@ -2015,7 +2021,7 @@ char * build_res_buf_from_sip_res( struct sip_msg* msg,
2015 2015
 		buf+s_offset,
2016 2016
 		len-s_offset);
2017 2017
 	 /* send it! */
2018
-	DBG("build_res_from_sip_res: copied size: orig:%d, new: %d, rest: %d"
2018
+	DBG("copied size: orig:%d, new: %d, rest: %d"
2019 2019
 			" msg=\n%s\n", s_offset, offset, len-s_offset, new_buf);
2020 2020
 
2021 2021
 	*returned_len=new_len;
... ...
@@ -2025,6 +2031,11 @@ error:
2025 2025
 	return 0;
2026 2026
 }
2027 2027
 
2028
+char * build_res_buf_from_sip_res( struct sip_msg* msg,
2029
+				unsigned int *returned_len)
2030
+{
2031
+	return generate_res_buf_from_sip_res(msg, returned_len, 0);
2032
+}
2028 2033
 
2029 2034
 char * build_res_buf_from_sip_req( unsigned int code, str *text ,str *new_tag,
2030 2035
 		struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
... ...
@@ -87,6 +87,8 @@ char * build_req_buf_from_sip_req(struct sip_msg* msg,
87 87
 char * build_res_buf_from_sip_res(struct sip_msg* msg,
88 88
 				unsigned int *returned_len);
89 89
 
90
+char * generate_res_buf_from_sip_res(struct sip_msg* msg,
91
+				unsigned int *returned_len, unsigned int mode);
90 92
 
91 93
 char * build_res_buf_from_sip_req(unsigned int code,
92 94
 				str *text,