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 1571
 					"memory allocation failure\n");
1572 1572
 		goto error00;
1573 1573
 	}
1574
+after_local_via:
1575
+	if(unlikely(mode&BUILD_NO_VIA1_UPDATE))
1576
+		goto after_update_via1;
1574 1577
 	/* check if received needs to be added */
1575 1578
 	if ( received_test(msg) ) {
1576 1579
 		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 1645
 
1646 1646
 	}
1647 1647
 
1648
+after_update_via1:
1648 1649
 	/* compute new msg len and fix overlapping zones*/
1649 1650
 	new_len=len+body_delta+lumps_len(msg, msg->add_rm, send_info)+via_len;
1650 1651
 #ifdef XL_DEBUG
... ...
@@ -1653,7 +1660,8 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
1653 1653
 	udp_mtu=cfg_get(core, core_cfg, udp_mtu);
1654 1654
 	di.proto=PROTO_NONE;
1655 1655
 	if (unlikely((send_info->proto==PROTO_UDP) && udp_mtu && 
1656
-					(flags & FL_MTU_FB_MASK) && (new_len>udp_mtu))){
1656
+					(flags & FL_MTU_FB_MASK) && (new_len>udp_mtu)
1657
+					&& (!(mode&BUILD_NO_LOCAL_VIA)))){
1657 1658
 
1658 1659
 		di=*send_info; /* copy whole struct - will be used in the Via builder */
1659 1660
 		di.proto=PROTO_NONE; /* except the proto */
... ...
@@ -1681,7 +1689,7 @@ char * build_req_buf_from_sip_req( struct sip_msg* msg,
1681 1681
 		
1682 1682
 		if (di.proto!=PROTO_NONE){
1683 1683
 			new_len-=via_len;
1684
-			pkg_free(line_buf);
1684
+			if(likely(line_buf)) pkg_free(line_buf);
1685 1685
 			line_buf = create_via_hf( &via_len, msg, &di, &branch);
1686 1686
 			if (!line_buf){
1687 1687
 				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 1694
 	/* add via header to the list */
1695 1695
 	/* try to add it before msg. 1st via */
1696 1696
 	/* 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,
1697
+	if(likely(line_buf)) {
1698
+		via_anchor=anchor_lump(msg, msg->via1->hdr.s-buf, 0, HDR_VIA_T);
1699
+		if (via_anchor==0) goto error04;
1700
+		if ((via_lump=insert_new_lump_before(via_anchor, line_buf, via_len,
1700 1701
 											HDR_VIA_T))==0)
1701
-		goto error04;
1702
-
1702
+			goto error04;
1703
+	}
1703 1704
 	if (msg->new_uri.s){
1704 1705
 		uri_len=msg->new_uri.len;
1705 1706
 		new_len=new_len-msg->first_line.u.request.uri.len+uri_len;
1706 1707
 	}
1707
-	new_buf=(char*)pkg_malloc(new_len+1);
1708
+	if(unlikely(mode&BUILD_IN_SHM))
1709
+		new_buf=(char*)shm_malloc(new_len+1);
1710
+	else
1711
+		new_buf=(char*)pkg_malloc(new_len+1);
1708 1712
 	if (new_buf==0){
1709 1713
 		ser_error=E_OUT_OF_MEM;
1710 1714
 		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 2543
 	*returned_len = offset;
2544 2544
 	return new_buf;	
2545 2545
 }
2546
+
2547
+
2548
+
2549
+/**
2550
+ * parse buf in msg and fill several fields
2551
+ */
2552
+int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len,
2553
+		unsigned int id)
2554
+{
2555
+	if(msg==0 || buf==0)
2556
+		return -1;
2557
+
2558
+	memset(msg, 0, sizeof(sip_msg_t));
2559
+	msg->id = id;
2560
+	msg->buf = buf;
2561
+	msg->len = len;
2562
+	if (parse_msg(buf, len, msg)!=0) {
2563
+		LM_ERR("parsing failed");
2564
+		return -1;
2565
+	}
2566
+	msg->set_global_address=default_global_address;
2567
+	msg->set_global_port=default_global_port;
2568
+	return 0;
2569
+}
2570
+
... ...
@@ -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 76
 	}while(0)
77 77
 
78 78
 char * build_req_buf_from_sip_req (	struct sip_msg* msg, 
79
-				unsigned int *returned_len, struct dest_info* send_info);
79
+				unsigned int *returned_len, struct dest_info* send_info,
80
+				unsigned int mode);
80 81
 
81 82
 char * build_res_buf_from_sip_res(	struct sip_msg* msg,
82 83
 				unsigned int *returned_len);
... ...
@@ -152,4 +157,6 @@ char * build_all( struct sip_msg* msg, int adjust_clen,
152 152
 /** cfg framework fixup */
153 153
 void fix_global_req_flags(str* gname, str* name);
154 154
 
155
+int build_sip_msg_from_buf(struct sip_msg *msg, char *buf, int len,
156
+		unsigned int id);
155 157
 #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