2
0
Эх сурвалжийг харах

parser: relocated helper functions

Daniel-Constantin Mierla 13 жил өмнө
parent
commit
f7f5ca4fdf

+ 0 - 211
lib/kcore/parser_helpers.c

@@ -1,211 +0,0 @@
-#include "parser_helpers.h"
-#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>
-
-struct sip_uri *parse_to_uri(struct sip_msg *msg)
-{
-	struct to_body *tb = NULL;
-	
-	if(msg==NULL)
-		return NULL;
-
-	if(parse_to_header(msg)<0)
-	{
-		LM_ERR("cannot parse TO header\n");
-		return NULL;
-	}
-
-	if(msg->to==NULL || get_to(msg)==NULL)
-		return NULL;
-
-	tb = get_to(msg);
-	
-	if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
-		return &tb->parsed_uri;
-
-	if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
-	{
-		LM_ERR("failed to parse To uri\n");
-		memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
-		set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing To uri");
-		set_err_reply(400, "bad To uri");
-		return NULL;
-	}
-
-	return &tb->parsed_uri;
-}
-
-
-struct sip_uri *parse_from_uri(struct sip_msg *msg)
-{
-	struct to_body *tb = NULL;
-        
-	if(msg==NULL)
-		return NULL;
-
-	if(parse_from_header(msg)<0)
-	{
-		LM_ERR("cannot parse FROM header\n");
-		return NULL;
-	}
-	
-	if(msg->from==NULL || get_from(msg)==NULL)
-		return NULL;
-
-	tb = get_from(msg);
-	
-	if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
-		return &tb->parsed_uri;
-	
-	if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
-	{
-		LM_ERR("failed to parse From uri\n");
-		memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
-		set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing From uri");
-		set_err_reply(400, "bad From uri");
-		return NULL;
-	}
-	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 = next_sibling_hdr(iroute);
-	}
-
-	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;
-}
-
-
-/*!
- * Path must be available. Function returns the first uri 
- * from Path without any duplication.
- */
-int get_path_dst_uri(str *_p, str *_dst)
-{
-	rr_t *route = 0;
-
-	LM_DBG("path for branch: '%.*s'\n", _p->len, _p->s);
-	if(parse_rr_body(_p->s, _p->len, &route) < 0) {	
-		LM_ERR("failed to parse Path body\n");
-		return -1;
-	}
-
-	if(!route) {
-		LM_ERR("failed to parse Path body no head found\n");
-		return -1;
-	}
-	*_dst = route->nameaddr.uri;
-
-	free_rr(&route);
-	
-	return 0;
-}

+ 0 - 22
lib/kcore/parser_helpers.h

@@ -1,22 +0,0 @@
-#ifndef _PARSER_HELPERS_H
-#define _PARSER_HELPERS_H
-
-#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);
-
-int get_path_dst_uri(str *_p, str *_dst);
-
-#endif /* _PARSER_HELPERS_H */

+ 0 - 1
modules/enum/enum.c

@@ -61,7 +61,6 @@
 #include "../../qvalue.h"
 #include "enum_mod.h"
 #include "../../lib/kcore/regexp.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../pvar.h"
 
 /*

+ 0 - 1
modules/rtpproxy/rtpproxy.c

@@ -211,7 +211,6 @@
 #include "../../timer_proc.h"
 #include "../../lib/kmi/mi.h"
 #include "../../lib/kcore/km_ut.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../pvar.h"
 #include "../../lvalue.h"
 #include "../../msg_translator.h"

+ 2 - 1
modules_k/auth_db/authorize.c

@@ -42,10 +42,11 @@
 #include "../../parser/digest/digest.h"
 #include "../../parser/hf.h"
 #include "../../parser/parser_f.h"
+#include "../../parser/parse_from.h"
+#include "../../parser/parse_to.h"
 #include "../../usr_avp.h"
 #include "../../mod_fix.h"
 #include "../../mem/mem.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "api.h"
 #include "authdb_mod.h"
 

+ 0 - 1
modules_k/auth_radius/authorize.c

@@ -38,7 +38,6 @@
 #include "../../parser/parse_uri.h"
 #include "../../parser/parse_from.h"
 #include "../../parser/parse_to.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../dprint.h"
 #include "../../ut.h"
 #include "../../pvar.h"

+ 0 - 1
modules_k/dialog/dlg_handlers.c

@@ -59,7 +59,6 @@
 #include "../../action.h"
 #include "../../script_cb.h"
 #include "../../lib/kcore/faked_msg.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../parser/parse_from.h"
 #include "../../parser/parse_cseq.h"
 #include "../../parser/contact/parse_contact.h"

+ 0 - 1
modules_k/domain/domain.c

@@ -34,7 +34,6 @@
 #include "../../lib/srdb1/db.h"
 #include "../../parser/parse_uri.h"
 #include "../../parser/parse_from.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../ut.h"
 #include "../../dset.h"
 #include "../../route.h"

+ 0 - 1
modules_k/group/group.c

@@ -42,7 +42,6 @@
 #include "../../parser/hf.h"            /* Header Field types */
 #include "../../parser/parse_from.h"    /* From parser */
 #include "../../parser/parse_uri.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "group.h"
 #include "group_mod.h"                   /* Module parameters */
 

+ 0 - 1
modules_k/nathelper/nathelper.c

@@ -213,7 +213,6 @@
 #include "../../lib/kmi/attr.h"
 #include "../../lib/kmi/mi.h"
 #include "../../lib/kcore/km_ut.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../pvar.h"
 #include "../../lvalue.h"
 #include "../../msg_translator.h"

+ 0 - 1
modules_k/pdt/pdt.c

@@ -41,7 +41,6 @@
 #include <stdlib.h>
 
 #include "../../lib/srdb1/db_op.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../sr_module.h"
 #include "../../lib/srdb1/db.h"
 #include "../../mem/shm_mem.h"

+ 0 - 1
modules_k/presence/subscribe.c

@@ -39,7 +39,6 @@
 #include "../../parser/parse_event.h"
 #include "../../parser/contact/parse_contact.h"
 #include "../../lib/kcore/hash_func.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "presence.h"
 #include "subscribe.h"
 #include "utils_func.h"

+ 0 - 1
modules_k/pua/send_subscribe.c

@@ -40,7 +40,6 @@
 #include "../../parser/parse_expires.h"
 #include "../presence/hash.h"
 #include "../../lib/kcore/hash_func.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "hash.h"
 #include "pua.h"
 #include "send_subscribe.h"

+ 0 - 1
modules_k/pv/pv_core.c

@@ -42,7 +42,6 @@
 #include "../../parser/parse_diversion.h"
 #include "../../lib/kcore/parse_ppi.h"
 #include "../../lib/kcore/parse_pai.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../parser/digest/digest.h"
 
 #include "pv_core.h"

+ 0 - 1
modules_k/registrar/lookup.c

@@ -40,7 +40,6 @@
 #include "../../action.h"
 #include "../../parser/parse_rr.h"
 #include "../usrloc/usrloc.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "common.h"
 #include "regtime.h"
 #include "reg_mod.h"

+ 0 - 1
modules_k/rls/subscribe.c

@@ -36,7 +36,6 @@
 #include "../../lib/kcore/cmpapi.h"
 #include "../../lib/kcore/hash_func.h"
 #include "../../lib/kcore/parse_supported.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../parser/msg_parser.h"
 #include "../../parser/parse_event.h"
 #include "../../parser/parse_expires.h"

+ 0 - 1
modules_k/siputils/checks.c

@@ -48,7 +48,6 @@
 #include "../../parser/parse_param.h"
 #include "../../ut.h"                   /* Handy utilities */
 #include "../../lib/srdb1/db.h"                /* Database API */
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../dset.h"
 #include "../../pvar.h"
 #include "../../lvalue.h"

+ 0 - 1
modules_k/speeddial/sdlookup.c

@@ -35,7 +35,6 @@
 #include "../../parser/parse_uri.h"
 #include "../../parser/parse_from.h"
 #include "../../lib/srdb1/db.h"
-#include "../../lib/kcore/parser_helpers.h"
 
 #include "speeddial.h"
 #include "sdlookup.h"

+ 0 - 1
modules_k/uri_db/checks.c

@@ -35,7 +35,6 @@
 #include "../../parser/digest/digest.h" /* get_authorized_cred */
 #include "../../parser/parse_from.h"
 #include "../../parser/parse_uri.h"
-#include "../../lib/kcore/parser_helpers.h"
 #include "../../ut.h"                   /* Handy utilities */
 #include "../../lib/srdb1/db.h"                /* Database API */
 #include "uridb_mod.h"

+ 29 - 0
parser/parse_from.c

@@ -37,6 +37,7 @@
 #include <string.h>
 #include "../dprint.h"
 #include "msg_parser.h"
+#include "parse_uri.h"
 #include "../ut.h"
 #include "../mem/mem.h"
 
@@ -88,4 +89,32 @@ error:
 	return -1;
 }
 
+sip_uri_t *parse_from_uri(sip_msg_t *msg)
+{
+	to_body_t *tb = NULL;
+        
+	if(msg==NULL)
+		return NULL;
+
+	if(parse_from_header(msg)<0)
+	{
+		LM_ERR("cannot parse FROM header\n");
+		return NULL;
+	}
+	
+	if(msg->from==NULL || get_from(msg)==NULL)
+		return NULL;
 
+	tb = get_from(msg);
+	
+	if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
+		return &tb->parsed_uri;
+	
+	if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
+	{
+		LM_ERR("failed to parse From uri\n");
+		memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
+		return NULL;
+	}
+	return &tb->parsed_uri;
+}

+ 2 - 0
parser/parse_from.h

@@ -47,4 +47,6 @@
  */
 int parse_from_header( struct sip_msg *msg);
 
+sip_uri_t *parse_from_uri(sip_msg_t *msg);
+
 #endif

+ 135 - 0
parser/parse_rr.c

@@ -343,3 +343,138 @@ int shm_duplicate_rr(rr_t** _new, rr_t* _r)
 {
 	return do_duplicate_rr(_new, _r, 1);
 }
+
+/*!
+ * 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 = next_sibling_hdr(iroute);
+	}
+
+	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;
+}
+
+
+/*!
+ * Path must be available. Function returns the first uri 
+ * from Path without any duplication.
+ */
+int get_path_dst_uri(str *_p, str *_dst)
+{
+	rr_t *route = 0;
+
+	LM_DBG("path for branch: '%.*s'\n", _p->len, _p->s);
+	if(parse_rr_body(_p->s, _p->len, &route) < 0) {	
+		LM_ERR("failed to parse Path body\n");
+		return -1;
+	}
+
+	if(!route) {
+		LM_ERR("failed to parse Path body no head found\n");
+		return -1;
+	}
+	*_dst = route->nameaddr.uri;
+
+	free_rr(&route);
+	
+	return 0;
+}

+ 10 - 0
parser/parse_rr.h

@@ -177,4 +177,14 @@ static inline int is_2rr(str* _params)
 	else return 0;
 }
 
+/*!
+ * 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);
+
+int get_path_dst_uri(str *_p, str *_dst);
+
 #endif /* PARSE_RR_H */

+ 32 - 0
parser/parse_to.c

@@ -35,6 +35,7 @@
 #include <string.h>
 #include "../dprint.h"
 #include "msg_parser.h"
+#include "parse_uri.h"
 #include "../ut.h"
 #include "../mem/mem.h"
 
@@ -854,3 +855,34 @@ int parse_to_header(struct sip_msg *msg)
 	else
 		return -1;
 }
+
+sip_uri_t *parse_to_uri(sip_msg_t *msg)
+{
+	to_body_t *tb = NULL;
+	
+	if(msg==NULL)
+		return NULL;
+
+	if(parse_to_header(msg)<0)
+	{
+		LM_ERR("cannot parse TO header\n");
+		return NULL;
+	}
+
+	if(msg->to==NULL || get_to(msg)==NULL)
+		return NULL;
+
+	tb = get_to(msg);
+	
+	if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
+		return &tb->parsed_uri;
+
+	if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
+	{
+		LM_ERR("failed to parse To uri\n");
+		memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
+		return NULL;
+	}
+
+	return &tb->parsed_uri;
+}

+ 2 - 0
parser/parse_to.h

@@ -73,4 +73,6 @@ void free_to(struct to_body* tb);
 
 int parse_to_header(struct sip_msg *msg);
 
+sip_uri_t *parse_to_uri(struct sip_msg* msg);
+
 #endif