Browse code

- lump_rpl extened - type added LUMP_RPL_HDR for adding hdrs and LUMP_RPL_BODY for adding body (only one allowed!!) - function build_res_buf_with_body_from_sip_req() (11 params) replaced with old build_res_buf_from_sip_req() (6 params) - new version knows how to interpret typed lump_rpl.

Bogdan-Andrei Iancu authored on 11/09/2003 19:54:43
Showing 4 changed files
... ...
@@ -33,7 +33,7 @@
33 33
 #include "data_lump_rpl.h"
34 34
 
35 35
 
36
-struct lump_rpl* build_lump_rpl( char* text, int len )
36
+struct lump_rpl* build_lump_rpl( char* text, int len , int type)
37 37
 {
38 38
 	struct lump_rpl *lump = 0;
39 39
 
... ...
@@ -53,6 +53,7 @@ struct lump_rpl* build_lump_rpl( char* text, int len )
53 53
 
54 54
 	memcpy(lump->text.s,text,len);
55 55
 	lump->text.len = len;
56
+	lump->type = type;
56 57
 	lump->next = 0;
57 58
 
58 59
 	return lump;
... ...
@@ -64,7 +65,7 @@ error:
64 64
 
65 65
 
66 66
 
67
-void add_lump_rpl(struct sip_msg * msg, struct lump_rpl* lump)
67
+int add_lump_rpl(struct sip_msg * msg, struct lump_rpl* lump)
68 68
 {
69 69
 	struct lump_rpl *foo;
70 70
 
... ...
@@ -72,9 +73,18 @@ void add_lump_rpl(struct sip_msg * msg, struct lump_rpl* lump)
72 72
 	{
73 73
 		msg->reply_lump = lump;
74 74
 	}else{
75
-		for(foo=msg->reply_lump;foo->next;foo=foo->next);
75
+		if (lump->type!=LUMP_RPL_BODY)
76
+			for(foo=msg->reply_lump;foo->next;foo=foo->next);
77
+		else
78
+			for(foo=msg->reply_lump;foo->next;foo=foo->next)
79
+				if (lump->type==LUMP_RPL_BODY) {
80
+					LOG(L_ERR,"ERROR:add_lump_rpl: LUMP_RPL_BODY "
81
+						"already added!\n");
82
+					return -1;
83
+				}
76 84
 		foo->next = lump;
77 85
 	}
86
+	return 0;
78 87
 }
79 88
 
80 89
 
... ...
@@ -86,4 +96,25 @@ void free_lump_rpl(struct lump_rpl* lump)
86 86
 }
87 87
 
88 88
 
89
+void unlink_lump_rpl(struct sip_msg * msg, struct lump_rpl* lump)
90
+{
91
+	struct lump_rpl *foo,*prev;
92
+
93
+	/* look for the lump to be unlink */
94
+	foo = msg->reply_lump;
95
+	prev = 0;
96
+	while( foo && foo!=lump ) {
97
+		prev = foo;
98
+		foo = foo->next;
99
+	}
100
+
101
+	/* if the lump was found into the list -> unlink it */
102
+	if (foo) {
103
+		if (prev)
104
+			prev->next = foo->next;
105
+		else
106
+			msg->reply_lump = foo->next;
107
+	}
108
+}
109
+
89 110
 
... ...
@@ -33,16 +33,22 @@
33 33
 #include "parser/msg_parser.h"
34 34
 
35 35
 
36
+#define LUMP_RPL_HDR   1
37
+#define LUMP_RPL_BODY  2
38
+
36 39
 struct lump_rpl
37 40
 {
38 41
 	str text;
42
+	int type;
39 43
 	struct lump_rpl* next;
40 44
 };
41 45
 
42
-struct lump_rpl* build_lump_rpl( char* , int );
46
+struct lump_rpl* build_lump_rpl( char* , int , int );
43 47
 
44
-void add_lump_rpl(struct sip_msg * , struct lump_rpl* );
48
+int add_lump_rpl(struct sip_msg * , struct lump_rpl* );
45 49
 
46 50
 void free_lump_rpl(struct lump_rpl* );
47 51
 
52
+void unlink_lump_rpl(struct sip_msg *, struct lump_rpl* );
53
+
48 54
 #endif
... ...
@@ -1505,57 +1505,28 @@ error:
1505 1505
 
1506 1506
 
1507 1507
 
1508
-
1509
-
1510
-char * build_res_buf_from_sip_req( unsigned int code, char *text,
1511
-					char *new_tag, unsigned int new_tag_len,
1512
-					struct sip_msg* msg, unsigned int *returned_len,
1513
-					struct bookmark *bmark)
1514
-{
1515
-    return build_res_buf_with_body_from_sip_req(code,text,new_tag,new_tag_len,
1516
-						0,0, /* no body */
1517
-						0,0, /* no content type */
1518
-						msg,returned_len, bmark);
1519
-}
1520
-
1521
-char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,
1522
-					     char *new_tag, unsigned int new_tag_len ,
1523
-					     char *body, unsigned int body_len,
1524
-					     char *content_type, unsigned int content_type_len,
1525
-					     struct sip_msg* msg, unsigned int *returned_len,
1526
-						 struct bookmark *bmark)
1508
+char * build_res_buf_from_sip_req( unsigned int code, char *text ,str *new_tag,
1509
+		struct sip_msg* msg, unsigned int *returned_len, struct bookmark *bmark)
1527 1510
 {
1528 1511
 	char              *buf, *p;
1529 1512
 	unsigned int      len,foo;
1530 1513
 	struct hdr_field  *hdr;
1531 1514
 	struct lump_rpl   *lump;
1515
+	struct lump_rpl   *body;
1532 1516
 	int               i;
1533 1517
 	char              backup;
1534
-	char              *received_buf;
1535
-	char              *rport_buf;
1536
-	unsigned int      received_len;
1537
-	unsigned int      rport_len;
1538
-	unsigned int      delete_len;
1539
-	char              *warning;
1540
-	unsigned int      warning_len;
1541
-	unsigned int	  text_len;
1542
-	int  content_len_len;
1543
-	char *content_len;
1544
-	char content_len_buf[MAX_CONTENT_LEN_BUF];
1518
+	str               received = {0,0};
1519
+	str               rport = {0,0};
1520
+	str               warning = {0,0};
1521
+	str               content_len = {0,0};
1522
+	unsigned int      text_len;
1545 1523
 	char *after_body;
1546 1524
 	str  to_tag;
1547 1525
 	char *totags;
1548 1526
 	int rcvd;
1549 1527
 
1550
-	received_buf=0;
1551
-	received_len=0;
1552
-	rport_buf=0;
1553
-	rport_len=0;
1554
-	delete_len=0;
1528
+	body = 0;
1555 1529
 	buf=0;
1556
-	/* make -Wall happy */
1557
-	warning=0;
1558
-	content_len=0;
1559 1530
 
1560 1531
 	text_len=strlen(text);
1561 1532
 
... ...
@@ -1569,6 +1540,9 @@ char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,
1569 1569
 		goto error00;
1570 1570
 	}
1571 1571
 
1572
+	/*computes the lenght of the new response buffer*/
1573
+	len = 0;
1574
+
1572 1575
 	/* check if received needs to be added */
1573 1576
 	backup = msg->via1->host.s[msg->via1->host.len];
1574 1577
 	msg->via1->host.s[msg->via1->host.len] = 0;
... ...
@@ -1577,7 +1551,7 @@ char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,
1577 1577
 						msg->via1->port, received_dns);
1578 1578
 	msg->via1->host.s[msg->via1->host.len] = backup;
1579 1579
 	if (rcvd) {
1580
-		if ((received_buf=received_builder(msg,&received_len))==0) {
1580
+		if ((received.s=received_builder(msg,&received.len))==0) {
1581 1581
 			LOG(L_ERR, "ERROR: build_res_buf_from_sip_req: "
1582 1582
 				"alas, received_builder failed\n");
1583 1583
 			goto error00;
... ...
@@ -1586,78 +1560,70 @@ char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,
1586 1586
 	/* check if rport needs to be updated */
1587 1587
 	if ( (msg->msg_flags&FL_FORCE_RPORT)||
1588 1588
 		(msg->via1->rport /*&& msg->via1->rport->value.s==0*/)){
1589
-		if ((rport_buf=rport_builder(msg, &rport_len))==0){
1589
+		if ((rport.s=rport_builder(msg, &rport.len))==0){
1590 1590
 			LOG(L_ERR, "ERROR: build_res_buf_from_sip_req:"
1591 1591
 							" rport_builder failed\n");
1592 1592
 			goto error01; /* free everything */
1593 1593
 		}
1594 1594
 		if (msg->via1->rport) 
1595
-			delete_len=msg->via1->rport->size+1; /* include ';' */
1595
+			len -= msg->via1->rport->size+1; /* include ';' */
1596 1596
 	}
1597 1597
 
1598
-	/*computes the lenght of the new response buffer*/
1599
-	len = 0;
1600 1598
 	/* first line */
1601 1599
 	len += SIP_VERSION_LEN + 1/*space*/ + 3/*code*/ + 1/*space*/ +
1602 1600
 		text_len + CRLF_LEN/*new line*/;
1603 1601
 	/*headers that will be copied (TO, FROM, CSEQ,CALLID,VIA)*/
1604 1602
 	for ( hdr=msg->headers ; hdr ; hdr=hdr->next ) {
1605
-		if (hdr->type==HDR_TO) {
1606
-			if (new_tag)
1607
-			{
1608
-				to_tag=get_to(msg)->tag_value;
1609
-				if (to_tag.s )
1610
-					len+=new_tag_len-to_tag.len;
1611
-				else
1612
-					len+=new_tag_len+TOTAG_TOKEN_LEN/*";tag="*/;
1613
-			}
1614
-			else {
1615
-				len+=hdr->len;
1616
-				continue;
1617
-			}
1618
-		} else if (hdr->type==HDR_VIA) {
1603
+		switch (hdr->type) {
1604
+			case HDR_TO:
1605
+				if (new_tag && new_tag->len) {
1606
+					to_tag=get_to(msg)->tag_value;
1607
+					if (to_tag.len )
1608
+						len+=new_tag->len-to_tag.len;
1609
+					else
1610
+						len+=new_tag->len+TOTAG_TOKEN_LEN/*";tag="*/;
1611
+				} else {
1612
+					len += hdr->len;
1613
+				}
1614
+				break;
1615
+			case HDR_VIA:
1619 1616
 				/* we always add CRLF to via*/
1620 1617
 				len+=(hdr->body.s+hdr->body.len)-hdr->name.s+CRLF_LEN;
1621
-				if (hdr==msg->h_via1) len += received_len+rport_len;
1622
-				continue;
1623
-		} else if (hdr->type==HDR_RECORDROUTE) {
1618
+				if (hdr==msg->h_via1) len += received.len+rport.len;
1619
+				break;
1620
+			case HDR_RECORDROUTE:
1624 1621
 				/* RR only for 1xx and 2xx replies */
1625
-				if (code<180 || code>=300) continue;
1626
-		} else if (!(hdr->type==HDR_FROM 
1627
-					|| hdr->type==HDR_CALLID
1628
-					|| hdr->type==HDR_CSEQ)) {
1629
-			continue;
1622
+				if (code<180 || code>=300)
1623
+					break;
1624
+			case HDR_FROM:
1625
+			case HDR_CALLID:
1626
+			case HDR_CSEQ:
1627
+				/* we keep the original termination for these headers*/
1628
+				len += hdr->len;
1630 1629
 		}
1631
-		len += hdr->len; /* we keep the original termination for these 
1632
-							headers*/
1633 1630
 	}
1634
-	len-=delete_len;
1635
-	/*lumps length*/
1636
-	for(lump=msg->reply_lump;lump;lump=lump->next)
1631
+	/* lumps length */
1632
+	for(lump=msg->reply_lump;lump;lump=lump->next) {
1637 1633
 		len += lump->text.len;
1638
-	if (server_signature) {
1639
-		/*server header*/
1640
-		len += SERVER_HDR_LEN + CRLF_LEN;
1641
-	}
1642
-
1643
-	if (body_len) {
1644
-		content_len=int2str(body_len, &content_len_len);
1645
-		memcpy(content_len_buf,content_len,content_len_len+1);
1646
-		content_len = content_len_buf;
1647
-		len += CONTENT_LENGTH_LEN + content_len_len + CRLF_LEN;
1648
-		len += body_len;
1649
-	} else {
1650
-		len +=CONTENT_LENGTH_LEN+1 + CRLF_LEN;
1651
-	}
1652
-	if(content_type_len) {
1653
-	    len += content_type_len + CRLF_LEN;
1634
+		if (lump->type==LUMP_RPL_BODY && lump->text.s && lump->text.len)
1635
+			body = lump;
1654 1636
 	}
1655
-
1637
+	/* server header */
1638
+	if (server_signature)
1639
+		len += SERVER_HDR_LEN + CRLF_LEN;
1640
+	/* warning hdr */
1656 1641
 	if (sip_warning) {
1657
-		warning = warning_builder(msg,&warning_len);
1658
-		if (warning) len += warning_len + CRLF_LEN;
1642
+		warning.s = warning_builder(msg,&warning.len);
1643
+		if (warning.s) len += warning.len + CRLF_LEN;
1659 1644
 		else LOG(L_WARN, "WARNING: warning skipped -- too big\n");
1660 1645
 	}
1646
+	/* content length hdr */
1647
+	len += CONTENT_LENGTH_LEN + 1/*0*/ + CRLF_LEN;
1648
+	/* body */
1649
+	if (body) {
1650
+		content_len.s = int2str(body->text.len, &content_len.len);
1651
+		len += content_len.len - 1 + body->text.len;
1652
+	}
1661 1653
 	/* end of message */
1662 1654
 	len += CRLF_LEN; /*new line*/
1663 1655
 
... ...
@@ -1691,13 +1657,13 @@ char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,
1691 1691
 		{
1692 1692
 			case HDR_VIA:
1693 1693
 				if (hdr==msg->h_via1){
1694
-					if (rport_buf){
1694
+					if (rport.s){
1695 1695
 						if (msg->via1->rport){ /* delete the old one */
1696 1696
 							/* copy until rport */
1697 1697
 							append_str_trans( p, hdr->name.s ,
1698 1698
 								msg->via1->rport->start-hdr->name.s-1,msg);
1699 1699
 							/* copy new rport */
1700
-							append_str(p, rport_buf, rport_len);
1700
+							append_str(p, rport.s, rport.len);
1701 1701
 							/* copy the rest of the via */
1702 1702
 							append_str_trans(p, msg->via1->rport->start+
1703 1703
 												msg->via1->rport->size, 
... ...
@@ -1708,15 +1674,15 @@ char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,
1708 1708
 							/* normal whole via copy */
1709 1709
 							append_str_trans( p, hdr->name.s , 
1710 1710
 								(hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1711
-							append_str(p, rport_buf, rport_len);
1711
+							append_str(p, rport.s, rport.len);
1712 1712
 						}
1713 1713
 					}else{
1714 1714
 						/* normal whole via copy */
1715 1715
 						append_str_trans( p, hdr->name.s , 
1716 1716
 								(hdr->body.s+hdr->body.len)-hdr->name.s, msg);
1717 1717
 					}
1718
-					if (received_buf)
1719
-						append_str( p, received_buf, received_len);
1718
+					if (received.s)
1719
+						append_str( p, received.s, received.len);
1720 1720
 				}else{
1721 1721
 					/* normal whole via copy */
1722 1722
 					append_str_trans( p, hdr->name.s,
... ...
@@ -1730,14 +1696,14 @@ char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,
1730 1730
 				append_str(p, hdr->name.s, hdr->len);
1731 1731
 				break;
1732 1732
 			case HDR_TO:
1733
-				if (new_tag){
1733
+				if (new_tag && new_tag->len){
1734 1734
 					if (to_tag.s ) { /* replacement */
1735 1735
 						/* before to-tag */
1736 1736
 						append_str( p, hdr->name.s, to_tag.s-hdr->name.s);
1737 1737
 						/* to tag replacement */
1738 1738
 						bmark->to_tag_val.s=p;
1739
-						bmark->to_tag_val.len=new_tag_len;
1740
-						append_str( p, new_tag,new_tag_len);
1739
+						bmark->to_tag_val.len=new_tag->len;
1740
+						append_str( p, new_tag->s,new_tag->len);
1741 1741
 						/* the rest after to-tag */
1742 1742
 						append_str( p, to_tag.s+to_tag.len,
1743 1743
 							hdr->name.s+hdr->len-(to_tag.s+to_tag.len));
... ...
@@ -1746,8 +1712,8 @@ char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,
1746 1746
 						append_str( p, hdr->name.s, after_body-hdr->name.s);
1747 1747
 						append_str(p, TOTAG_TOKEN, TOTAG_TOKEN_LEN);
1748 1748
 						bmark->to_tag_val.s=p;
1749
-						bmark->to_tag_val.len=new_tag_len;
1750
-						append_str( p, new_tag,new_tag_len);
1749
+						bmark->to_tag_val.len=new_tag->len;
1750
+						append_str( p, new_tag->s,new_tag->len);
1751 1751
 						append_str( p, after_body, 
1752 1752
 										hdr->name.s+hdr->len-after_body);
1753 1753
 					}
... ...
@@ -1764,72 +1730,68 @@ char * build_res_buf_with_body_from_sip_req( unsigned int code, char *text ,
1764 1764
 			case HDR_CSEQ:
1765 1765
 					append_str(p, hdr->name.s, hdr->len);
1766 1766
 		} /* for switch */
1767
-	/*lumps*/
1767
+	/* lumps */
1768 1768
 	for(lump=msg->reply_lump;lump;lump=lump->next)
1769
-	{
1770
-		memcpy(p,lump->text.s,lump->text.len);
1771
-		p += lump->text.len;
1772
-	}
1769
+		if (lump->type==LUMP_RPL_HDR){
1770
+			memcpy(p,lump->text.s,lump->text.len);
1771
+			p += lump->text.len;
1772
+		}
1773
+	/* server header */
1773 1774
 	if (server_signature) {
1774
-		/*server header*/
1775 1775
 		memcpy( p, SERVER_HDR , SERVER_HDR_LEN );
1776 1776
 		p+=SERVER_HDR_LEN;
1777 1777
 		memcpy( p, CRLF, CRLF_LEN );
1778 1778
 		p+=CRLF_LEN;
1779 1779
 	}
1780
-	
1781
-	if (body_len) {
1782
-		memcpy(p, CONTENT_LENGTH, CONTENT_LENGTH_LEN );
1783
-		p+=CONTENT_LENGTH_LEN;
1784
-		memcpy( p, content_len, content_len_len );
1785
-		p+=content_len_len;
1786
-		memcpy( p, CRLF, CRLF_LEN );
1787
-		p+=CRLF_LEN;
1780
+	/* content_length hdr */
1781
+	memcpy(p, CONTENT_LENGTH, CONTENT_LENGTH_LEN );
1782
+	p+=CONTENT_LENGTH_LEN;
1783
+	if (content_len.len) {
1784
+		memcpy( p, content_len.s, content_len.len );
1785
+		p+=content_len.len;
1788 1786
 	} else {
1789
-		/* content length header*/
1790
-		memcpy( p, CONTENT_LENGTH "0" CRLF, CONTENT_LENGTH_LEN+1+CRLF_LEN );
1791
-		p+=CONTENT_LENGTH_LEN+1+CRLF_LEN;
1787
+		*(p++) = '0';
1792 1788
 	}
1793
-	if(content_type_len){
1794
-	    memcpy( p, content_type, content_type_len );
1795
-	    p+=content_type_len;
1796
-	    memcpy( p, CRLF, CRLF_LEN );
1797
-	    p+=CRLF_LEN;
1798
-	}
1799
-	if (sip_warning && warning) {
1800
-		memcpy( p, warning, warning_len);
1801
-		p+=warning_len;
1789
+	memcpy( p, CRLF, CRLF_LEN );
1790
+	p+=CRLF_LEN;
1791
+	/* warning header */
1792
+	if (warning.s) {
1793
+		memcpy( p, warning.s, warning.len);
1794
+		p+=warning.len;
1802 1795
 		memcpy( p, CRLF, CRLF_LEN);
1803 1796
 		p+=CRLF_LEN;
1804 1797
 	}
1805 1798
 	/*end of message*/
1806 1799
 	memcpy( p, CRLF, CRLF_LEN );
1807 1800
 	p+=CRLF_LEN;
1808
-	if(body_len){
1809
-	    memcpy ( p, body, body_len );
1810
-	    p+=body_len;
1801
+	/* body */
1802
+	if (body) {
1803
+		memcpy ( p, body->text.s, body->text.len );
1804
+		p+=body->text.len;
1811 1805
 	}
1806
+
1812 1807
 	*(p) = 0;
1813 1808
 	*returned_len = len;
1814 1809
 	/* in req2reply, received_buf is not introduced to lumps and
1815 1810
 	   needs to be deleted here
1816 1811
 	*/
1817
-	if (received_buf) pkg_free(received_buf);
1818
-	if (rport_buf) pkg_free(rport_buf);
1812
+	if (received.s) pkg_free(received.s);
1813
+	if (rport.s) pkg_free(rport.s);
1819 1814
 	return buf;
1820 1815
 
1821 1816
 error01:
1822
-	if (received_buf) pkg_free(received_buf);
1823
-	if (rport_buf) pkg_free(rport_buf);
1817
+	if (received.s) pkg_free(received.s);
1818
+	if (rport.s) pkg_free(rport.s);
1824 1819
 error00:
1825 1820
 	*returned_len=0;
1826 1821
 	return 0;
1827 1822
 }
1828 1823
 
1824
+
1825
+
1829 1826
 /* return number of chars printed or 0 if space exceeded;
1830 1827
    assumes buffer sace of at least MAX_BRANCH_PARAM_LEN
1831 1828
  */
1832
-
1833 1829
 int branch_builder( unsigned int hash_index,
1834 1830
 	/* only either parameter useful */
1835 1831
 	unsigned int label, char * char_v,
... ...
@@ -45,7 +45,7 @@
45 45
 #define WARNING_PHRASE " \"Noisy feedback tells: "
46 46
 #define WARNING_PHRASE_LEN (sizeof(WARNING_PHRASE)-1)
47 47
 
48
-#define MAX_CONTENT_LEN_BUF INT2STR_MAX_LEN /* see ut.h/int2str() */
48
+//#define MAX_CONTENT_LEN_BUF INT2STR_MAX_LEN /* see ut.h/int2str() */
49 49
 
50 50
 #include "parser/msg_parser.h"
51 51
 #include "ip_addr.h"
... ...
@@ -62,13 +62,14 @@ char * build_req_buf_from_sip_req (	struct sip_msg* msg,
62 62
 char * build_res_buf_from_sip_res(	struct sip_msg* msg,
63 63
 				unsigned int *returned_len);
64 64
 
65
-char * build_res_buf_from_sip_req(	unsigned int code ,
65
+
66
+char * build_res_buf_from_sip_req( unsigned int code ,
66 67
 				char *text ,
67
-				char *new_tag ,
68
-				unsigned int new_tag_len ,
68
+				str *new_tag ,
69 69
 				struct sip_msg* msg,
70 70
 				unsigned int *returned_len,
71 71
 				struct bookmark *bmark);
72
+/*
72 73
 char * build_res_buf_with_body_from_sip_req(	unsigned int code ,
73 74
 				char *text ,
74 75
 				char *new_tag ,
... ...
@@ -80,7 +81,7 @@ char * build_res_buf_with_body_from_sip_req(	unsigned int code ,
80 80
 				struct sip_msg* msg,
81 81
 				unsigned int *returned_len,
82 82
 				struct bookmark *bmark);
83
-
83
+*/
84 84
 char* via_builder( unsigned int *len,
85 85
 	struct socket_info* send_sock,
86 86
 	str *branch, str* extra_params, int proto );