Browse code

core: build_req_buf_from_sip_req() has a new parameter

- mode - a set of flags that define how the sip request is printed to buffer
- BUILD_NO_LOCAL_VIA (1<<0) - skip adding local via
- BUILD_NO_VIA1_UPDATE (1<<1) - skip updating top most via
- BUILD_IN_SHM (1<<2) - output buffer allocated in memory
- new function inc_msg_no() - increment and return msg_no
- new function build_sip_msg_from_buf(...) to parse and partially fill
sip_msg structure from a buffer

Daniel-Constantin Mierla authored on 08/07/2009 11:10:37
Showing 5 changed files
... ...
@@ -433,7 +433,7 @@ int forward_request(struct sip_msg* msg, str* dst, unsigned short port,
433 433
 #endif
434 434
 			if (buf) pkg_free(buf);
435 435
 			send_info->proto=proto;
436
-			buf = build_req_buf_from_sip_req(msg, &len, send_info);
436
+			buf = build_req_buf_from_sip_req(msg, &len, send_info, 0);
437 437
 			if (!buf){
438 438
 				LOG(L_ERR, "ERROR: forward_request: building failed\n");
439 439
 				ret=E_OUT_OF_MEM; /* most probable */
... ...
@@ -1522,8 +1522,8 @@ error:
1522 1522
   */
1523 1523
 char * build_req_buf_from_sip_req( struct sip_msg* msg,
1524 1524
 								unsigned int *returned_len,
1525
-								struct dest_info* send_info
1526
-								)
1525
+								struct dest_info* send_info,
1526
+								unsigned int mode)
1527 1527
 {
1528 1528
 	unsigned int len, new_len, received_len, rport_len, uri_len, via_len,
1529 1529
 				 body_delta;
... ...
@@ -1561,6 +1561,9 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
1561 1561
 		goto error00;
1562 1562
 	}
1563 1563
 
1564
+	if(unlikely(mode&BUILD_NO_LOCAL_VIA))
1565
+		goto after_local_via;
1566
+
1564 1567
 	/* create the via header */
1565 1568
 	branch.s=msg->add_to_branch_s;
1566 1569
 	branch.len=msg->add_to_branch_len;
... ...
@@ -1571,6 +1574,9 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
1571 1574
 					"memory allocation failure\n");
1572 1575
 		goto error00;
1573 1576
 	}
1577
+after_local_via:
1578
+	if(unlikely(mode&BUILD_NO_VIA1_UPDATE))
1579
+		goto after_update_via1;
1574 1580
 	/* check if received needs to be added */
1575 1581
 	if ( received_test(msg) ) {
1576 1582
 		if ((received_buf=received_builder(msg,&received_len))==0){
... ...
@@ -1645,6 +1651,7 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
1645 1651
 
1646 1652
 	}
1647 1653
 
1654
+after_update_via1:
1648 1655
 	/* compute new msg len and fix overlapping zones*/
1649 1656
 	new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
1650 1657
 #ifdef XL_DEBUG
... ...
@@ -1653,7 +1660,8 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
1653 1660
 	udp_mtu=cfg_get(core, core_cfg, udp_mtu);
1654 1661
 	di.proto=PROTO_NONE;
1655 1662
 	if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu && 
1656
-					(flags & FL_MTU_FB_MASK) && (new_len>udp_mtu))){
1663
+					(flags & FL_MTU_FB_MASK) && (new_len>udp_mtu)
1664
+					&& (!(mode&BUILD_NO_LOCAL_VIA)))){
1657 1665
 
1658 1666
 		di=*send_info; /* copy whole struct - will be used in the Via builder */
1659 1667
 		di.proto=PROTO_NONE; /* except the proto */
... ...
@@ -1681,7 +1689,7 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
1681 1689
 		
1682 1690
 		if (di.proto!=PROTO_NONE){
1683 1691
 			new_len-=via_len;
1684
-			pkg_free(line_buf);
1692
+			if(likely(line_buf)) pkg_free(line_buf);
1685 1693
 			line_buf = create_via_hf( &via_len, msg, &di, &branch);
1686 1694
 			if (!line_buf){
1687 1695
 				LOG(L_ERR,"ERROR: build_req_buf_from_sip_req: "
... ...
@@ -1694,17 +1702,21 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
1694 1702
 	/* add via header to the list */
1695 1703
 	/* try to add it before msg. 1st via */
1696 1704
 	/* add first via, as an anchor for second via*/
1697
-	via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
1698
-	if (via_anchor==0) goto error04;
1699
-	if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
1705
+	if(likely(line_buf)) {
1706
+		via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
1707
+		if (via_anchor==0) goto error04;
1708
+		if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
1700 1709
 											HDR_VIA_T))==0)
1701
-		goto error04;
1702
-
1710
+			goto error04;
1711
+	}
1703 1712
 	if (msg->new_uri.s){
1704 1713
 		uri_len=msg->new_uri.len;
1705 1714
 		new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
1706 1715
 	}
1707
-	new_buf=(char*)pkg_malloc(new_len+1);
1716
+	if(unlikely(mode&BUILD_IN_SHM))
1717
+		new_buf=(char*)shm_malloc(new_len+1);
1718
+	else
1719
+		new_buf=(char*)pkg_malloc(new_len+1);
1708 1720
 	if (new_buf==0){
1709 1721
 		ser_error=E_OUT_OF_MEM;
1710 1722
 		LOG(L_ERR, "ERROR: build_req_buf_from_sip_req: out of memory\n");
... ...
@@ -2543,3 +2555,28 @@ char * build_all( struct sip_msg* msg, int touch_clen,
2543 2555
 	*returned_len = offset;
2544 2556
 	return new_buf;	
2545 2557
 }
2558
+
2559
+
2560
+
2561
+/**
2562
+ * parse buf in msg and fill several fields
2563
+ */
2564
+int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len,
2565
+		unsigned int id)
2566
+{
2567
+	if(msg==0 || buf==0)
2568
+		return -1;
2569
+
2570
+	memset(msg, 0, sizeof(sip_msg_t));
2571
+	msg->id = id;
2572
+	msg->buf = buf;
2573
+	msg->len = len;
2574
+	if (parse_msg(buf, len, msg)!=0) {
2575
+		LM_ERR("parsing failed");
2576
+		return -1;
2577
+	}
2578
+	msg->set_global_address=default_global_address;
2579
+	msg->set_global_port=default_global_port;
2580
+	return 0;
2581
+}
2582
+
... ...
@@ -48,6 +48,10 @@
48 48
 
49 49
 /*#define MAX_CONTENT_LEN_BUF INT2STR_MAX_LEN *//* see ut.h/int2str() */
50 50
 
51
+#define BUILD_NO_LOCAL_VIA		(1<<0)
52
+#define BUILD_NO_VIA1_UPDATE	(1<<1)
53
+#define BUILD_IN_SHM			(1<<2)
54
+
51 55
 #include "parser/msg_parser.h"
52 56
 #include "ip_addr.h"
53 57
 
... ...
@@ -76,7 +80,8 @@ struct hostport {
76 80
 	}while(0)
77 81
 
78 82
 char * build_req_buf_from_sip_req (	struct sip_msg* msg, 
79
-				unsigned int *returned_len, struct dest_info* send_info);
83
+				unsigned int *returned_len, struct dest_info* send_info,
84
+				unsigned int mode);
80 85
 
81 86
 char * build_res_buf_from_sip_res(	struct sip_msg* msg,
82 87
 				unsigned int *returned_len);
... ...
@@ -152,4 +157,6 @@ char * build_all( struct sip_msg* msg, int adjust_clen,
152 157
 /** cfg framework fixup */
153 158
 void fix_global_req_flags(str* gname, str* name);
154 159
 
160
+int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len,
161
+		unsigned int id);
155 162
 #endif
... ...
@@ -76,6 +76,13 @@ str default_global_port={0,0};
76 76
 str default_via_address={0,0};
77 77
 str default_via_port={0,0};
78 78
 
79
+/**
80
+ * increment msg_no and return the new value
81
+ */
82
+unsigned int inc_msg_no(void)
83
+{
84
+	return ++msg_no;
85
+}
79 86
 
80 87
 
81 88
 /* WARNING: buf must be 0 terminated (buf[len]=0) or some things might 
... ...
@@ -33,6 +33,6 @@
33 33
 #include "ip_addr.h"
34 34
 
35 35
 int receive_msg(char* buf, unsigned int len, struct receive_info *ri);
36
-
36
+unsigned int inc_msg_no(void);
37 37
 
38 38
 #endif