Browse Source

- avp helper parsing api changes:
- added avp_parse_spec() - parses an avp spec. which can be an avp name or an avp global alias
- avp_parse_name & lookup_avp_galias have the same prototype now

Andrei Pelinescu-Onciul 21 năm trước cách đây
mục cha
commit
d34839a3ff
2 tập tin đã thay đổi với 44 bổ sung14 xóa
  1. 40 5
      usr_avp.c
  2. 4 9
      usr_avp.h

+ 40 - 5
usr_avp.c

@@ -47,7 +47,7 @@
 #include "usr_avp.h"
 #include "usr_avp.h"
 
 
 
 
-
+/* usr_avp data bodies */
 struct str_int_data {
 struct str_int_data {
 	str  name;
 	str  name;
 	int  val;
 	int  val;
@@ -58,6 +58,12 @@ struct str_str_data {
 	str  val;
 	str  val;
 };
 };
 
 
+/* avp aliases structs*/
+struct avp_spec {
+	int type;
+	int_str name;
+};
+
 struct avp_galias {
 struct avp_galias {
 	str alias;
 	str alias;
 	struct avp_spec  avp;
 	struct avp_spec  avp;
@@ -458,15 +464,19 @@ error:
 }
 }
 
 
 
 
-struct avp_spec *lookup_avp_galias(char *alias, int len)
+int lookup_avp_galias(str *alias, int *type, int_str *avp_name)
 {
 {
 	struct avp_galias *ga;
 	struct avp_galias *ga;
 
 
 	for( ga=galiases ; ga ; ga=ga->next )
 	for( ga=galiases ; ga ; ga=ga->next )
-		if (len==ga->alias.len && (strncasecmp( alias, ga->alias.s, len)==0) )
-			return &ga->avp;
+		if (alias->len==ga->alias.len &&
+		(strncasecmp( alias->s, ga->alias.s, alias->len)==0) ) {
+			*type = ga->avp.type;
+			*avp_name = ga->avp.name;
+			return 0;
+		}
 
 
-	return 0;
+	return -1;
 }
 }
 
 
 
 
@@ -477,6 +487,9 @@ int parse_avp_name( str *name, int *type, int_str *avp_name)
 	unsigned int id;
 	unsigned int id;
 	char c;
 	char c;
 
 
+	if (name==0 || name->s==0 || name->len==0)
+		goto error;
+
 	if (name->len>=2 && name->s[1]==':') {
 	if (name->len>=2 && name->s[1]==':') {
 		c = name->s[0];
 		c = name->s[0];
 		name->s += 2;
 		name->s += 2;
@@ -514,6 +527,28 @@ error:
 }
 }
 
 
 
 
+int parse_avp_spec( str *name, int *type, int_str *avp_name)
+{
+	str alias;
+
+	if (name==0 || name->s==0 || name->len==0)
+		return -1;
+
+	if (name->s[0]==GALIAS_CHAR_MARKER) {
+		/* it's an avp alias */
+		if (name->len==1) {
+			LOG(L_ERR,"ERROR:parse_avp_spec: empty alias\n");
+			return -1;
+		}
+		alias.s = name->s+1;
+		alias.len = name->len-1;
+		return lookup_avp_galias( &alias, type, avp_name);
+	} else {
+		return parse_avp_name( name, type, avp_name);
+	}
+}
+
+
 int add_avp_galias_str(char *alias_definition)
 int add_avp_galias_str(char *alias_definition)
 {
 {
 	int_str avp_name;
 	int_str avp_name;

+ 4 - 9
usr_avp.h

@@ -50,25 +50,19 @@ struct usr_avp {
 };
 };
 
 
 
 
-struct avp_spec {
-	int type;
-	int_str name;
-};
-
-
 #define AVP_NAME_STR     (1<<0)
 #define AVP_NAME_STR     (1<<0)
 #define AVP_VAL_STR      (1<<1)
 #define AVP_VAL_STR      (1<<1)
 
 
+#define GALIAS_CHAR_MARKER  '$'
+
 /* add functions */
 /* add functions */
 int add_avp( unsigned short flags, int_str name, int_str val);
 int add_avp( unsigned short flags, int_str name, int_str val);
 
 
-
 /* search functions */
 /* search functions */
 struct usr_avp *search_first_avp( unsigned short name_type, int_str name,
 struct usr_avp *search_first_avp( unsigned short name_type, int_str name,
 															int_str *val );
 															int_str *val );
 struct usr_avp *search_next_avp( struct usr_avp *avp, int_str *val  );
 struct usr_avp *search_next_avp( struct usr_avp *avp, int_str *val  );
 
 
-
 /* free functions */
 /* free functions */
 void reset_avps( );
 void reset_avps( );
 void destroy_avp( struct usr_avp *avp);
 void destroy_avp( struct usr_avp *avp);
@@ -83,9 +77,10 @@ struct usr_avp** get_avp_list( );
 
 
 /* global alias functions (manipulation and parsing)*/
 /* global alias functions (manipulation and parsing)*/
 int add_avp_galias_str(char *alias_definition);
 int add_avp_galias_str(char *alias_definition);
-struct avp_spec *lookup_avp_galias(char *alias, int len);
+int lookup_avp_galias(str *alias, int *type, int_str *avp_name);
 int add_avp_galias(str *alias, int type, int_str avp_name);
 int add_avp_galias(str *alias, int type, int_str avp_name);
 int parse_avp_name( str *name, int *type, int_str *avp_name);
 int parse_avp_name( str *name, int *type, int_str *avp_name);
+int parse_avp_spec( str *name, int *type, int_str *avp_name);
 
 
 #endif
 #endif