[SR-Dev] git:janakj/kcore: Adding print_rr_body from kamailio/parser/parse_rr.[hc]

Jan Janak jan at iptel.org
Fri Mar 6 15:23:03 CET 2009


Module: sip-router
Branch: janakj/kcore
Commit: 895489f0d7e0ec36a20868e94fa31361fe9c5e45
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=895489f0d7e0ec36a20868e94fa31361fe9c5e45

Author: Jan Janak <jan at iptel.org>
Committer: Jan Janak <jan at iptel.org>
Date:   Fri Mar  6 02:13:42 2009 +0100

Adding print_rr_body from kamailio/parser/parse_rr.[hc]

---

 lib/kcore/parser_helpers.c |  110 ++++++++++++++++++++++++++++++++++++++++++++
 lib/kcore/parser_helpers.h |   10 ++++
 2 files changed, 120 insertions(+), 0 deletions(-)

diff --git a/lib/kcore/parser_helpers.c b/lib/kcore/parser_helpers.c
index 15c48c6..5d4ef16 100644
--- a/lib/kcore/parser_helpers.c
+++ b/lib/kcore/parser_helpers.c
@@ -2,7 +2,9 @@
 #include "errinfo.h"
 #include "../../parser/parse_to.h"
 #include "../../parser/parse_from.h"
+#include "../../parser/parse_rr.h"
 #include "../../dprint.h"
+#include "../../mem/mem.h"
 
 #include <string.h>
 
@@ -63,3 +65,111 @@ struct sip_uri *parse_from_uri(struct sip_msg *msg)
 	return &tb->parsed_uri;
 }
 
+
+/*!
+ * get first RR header and print comma separated bodies in oroute
+ * - order = 0 normal; order = 1 reverse
+ * - nb_recs - input=skip number of rr; output=number of printed rrs
+ */
+int print_rr_body(struct hdr_field *iroute, str *oroute, int order,
+												unsigned int * nb_recs)
+{
+	rr_t *p;
+	int n = 0, nr=0;
+	int i = 0;
+	int route_len;
+#define MAX_RR_HDRS	64
+	static str route[MAX_RR_HDRS];
+	char *cp, *start;
+
+	if(iroute==NULL)
+		return 0;
+
+	route_len= 0;
+	memset(route, 0, MAX_RR_HDRS*sizeof(str));
+
+	while (iroute!=NULL) 
+	{
+		if (parse_rr(iroute) < 0) 
+		{
+			LM_ERR("failed to parse RR\n");
+			goto error;
+		}
+
+		p =(rr_t*)iroute->parsed;
+		while (p)
+		{
+			route[n].s = p->nameaddr.name.s;
+			route[n].len = p->len;
+			LM_DBG("current rr is %.*s\n", route[n].len, route[n].s);
+
+			n++;
+			if(n==MAX_RR_HDRS)
+			{
+				LM_ERR("too many RR\n");
+				goto error;
+			}
+			p = p->next;
+		}
+		iroute = iroute->sibling;
+	}
+
+	for(i=0;i<n;i++){
+		if(!nb_recs || (nb_recs && 
+		 ( (!order&& (i>=*nb_recs)) || (order && (i<=(n-*nb_recs)) )) ) )
+		{
+			route_len+= route[i].len;
+			nr++;
+		}
+	
+	}
+
+	if(nb_recs)
+		LM_DBG("skipping %i route records\n", *nb_recs);
+	
+	route_len += --nr; /* for commas */
+
+	oroute->s=(char*)pkg_malloc(route_len);
+
+
+	if(oroute->s==0)
+	{
+		LM_ERR("no more pkg mem\n");
+		goto error;
+	}
+	cp = start = oroute->s;
+	if(order==0)
+	{
+		i= (nb_recs == NULL) ? 0:*nb_recs;
+
+		while (i<n)
+		{
+			memcpy(cp, route[i].s, route[i].len);
+			cp += route[i].len;
+			if (++i<n)
+				*(cp++) = ',';
+		}
+	} else {
+		
+		i = (nb_recs == NULL) ? n-1 : (n-*nb_recs-1);
+			
+		while (i>=0)
+		{
+			memcpy(cp, route[i].s, route[i].len);
+			cp += route[i].len;
+			if (i-->0)
+				*(cp++) = ',';
+		}
+	}
+	oroute->len=cp - start;
+
+	LM_DBG("out rr [%.*s]\n", oroute->len, oroute->s);
+	LM_DBG("we have %i records\n", n);
+	if(nb_recs != NULL)
+		*nb_recs = (unsigned int)n; 
+
+	return 0;
+
+error:
+	return -1;
+}
diff --git a/lib/kcore/parser_helpers.h b/lib/kcore/parser_helpers.h
index 58e8764..76f7e98 100644
--- a/lib/kcore/parser_helpers.h
+++ b/lib/kcore/parser_helpers.h
@@ -3,9 +3,19 @@
 
 #include "../../parser/msg_parser.h"
 #include "../../parser/parse_uri.h"
+#include "../../str.h"
 
 struct sip_uri* parse_to_uri(struct sip_msg* msg);
 
 struct sip_uri* parse_from_uri(struct sip_msg* msg);
 
+/*!
+ * get first RR header and print comma separated bodies in oroute
+ * - order = 0 normal; order = 1 reverse
+ * - nb_recs - input=skip number of rr; output=number of printed rrs
+ */
+int print_rr_body(struct hdr_field *iroute, str *oroute, int order,
+				  unsigned int * nb_recs);
+
+
 #endif /* _PARSER_HELPERS_H */




More information about the sr-dev mailing list