Forráskód Böngészése

modules/mtree: added new mode to mt_match()

- Added mode=2 to mt_match() call that instead of setting value of
  longest matching prefix to a pv, sets values of all matching prefixes to
  an avp so that value of longest matching prefix is in avp index 0.
Juha Heinanen 14 éve
szülő
commit
bd8b01b9e2

+ 51 - 16
modules/mtree/README

@@ -10,7 +10,19 @@ Daniel-Constantin Mierla
 
    <[email protected]>
 
+Juha Heinanen
+
+   tutpro.com
+
+Edited by
+
+Juha Heinanen
+
+   <[email protected]>
+
    Copyright © 2010 Daniel-Constantin Mierla (asipto.com)
+
+   Copyright © 2011 Juha Heinanen
      __________________________________________________________________
 
    Table of Contents
@@ -34,8 +46,9 @@ Daniel-Constantin Mierla
               3.7. fetch_rows (integer)
               3.8. char_list (string)
               3.9. pv_value (string)
-              3.10. mt_tree_type (integer)
-              3.11. mt_ignore_duplicates (integer)
+              3.10. pv_values (string)
+              3.11. mt_tree_type (integer)
+              3.12. mt_ignore_duplicates (integer)
 
         4. Exported Functions
 
@@ -58,9 +71,10 @@ Daniel-Constantin Mierla
    1.7. Set fetch_rows parameter
    1.8. Set char_list parameter
    1.9. Set pv_value parameter
-   1.10. Set mt_tree_type parameter
-   1.11. Set mt_ignore_duplicates parameter
-   1.12. mt_match usage
+   1.10. Set pv_values parameter
+   1.11. Set mt_tree_type parameter
+   1.12. Set mt_ignore_duplicates parameter
+   1.13. mt_match usage
 
 Chapter 1. Admin Guide
 
@@ -83,8 +97,9 @@ Chapter 1. Admin Guide
         3.7. fetch_rows (integer)
         3.8. char_list (string)
         3.9. pv_value (string)
-        3.10. mt_tree_type (integer)
-        3.11. mt_ignore_duplicates (integer)
+        3.10. pv_values (string)
+        3.11. mt_tree_type (integer)
+        3.12. mt_ignore_duplicates (integer)
 
    4. Exported Functions
 
@@ -128,8 +143,9 @@ Chapter 1. Admin Guide
    3.7. fetch_rows (integer)
    3.8. char_list (string)
    3.9. pv_value (string)
-   3.10. mt_tree_type (integer)
-   3.11. mt_ignore_duplicates (integer)
+   3.10. pv_values (string)
+   3.11. mt_tree_type (integer)
+   3.12. mt_ignore_duplicates (integer)
 
 3.1. db_url (string)
 
@@ -232,24 +248,37 @@ modparam("mtree", "char_list", "0123456789*+")
 modparam("mtree", "pv_value", "$var(mtval)")
 ...
 
-3.10. mt_tree_type (integer)
+3.10. pv_values (string)
+
+   The PV spec where to store the matched values when mtree consists of
+   string values and mode of mt_match() call has value 2. It can be any
+   AVP.
+
+   Default value is “$avp(s:tvalues)”.
+
+   Example 1.10. Set pv_values parameter
+...
+modparam("mtree", "pv_values", "$avp(mtvals)")
+...
+
+3.11. mt_tree_type (integer)
 
    Default payload type for trees data stored in 'db_table'.
 
    Default value is 0.
 
-   Example 1.10. Set mt_tree_type parameter
+   Example 1.11. Set mt_tree_type parameter
 ...
 modparam("mtree", "mt_tree_type", 1)
 ...
 
-3.11. mt_ignore_duplicates (integer)
+3.12. mt_ignore_duplicates (integer)
 
    Ignore duplicated prefixes when loading data.
 
    Default value is 0.
 
-   Example 1.11. Set mt_ignore_duplicates parameter
+   Example 1.12. Set mt_ignore_duplicates parameter
 ...
 modparam("mtree", "mt_ignore_duplicates", 1)
 ...
@@ -260,10 +289,16 @@ modparam("mtree", "mt_ignore_duplicates", 1)
 
 4.1.  mt_match(mtree, pv, mode)
 
-   Match 'pv' value against mtree. mode is reserved for further use, now
-   set any integer or PV.
+   Match 'pv' value against 'mtree'. If 'mtree' consists of string values
+   (mtree type = 0) and value of 'mode' is NOT 2, sets value of longest
+   matching prefix to pseudo variable specified by pv_value parameter. If
+   'mtree' consists of string values (mtree type = 0) and value of 'mode'
+   is 2, sets values of all matching prefixes to avp specified by
+   pv_values parameter so that value of longest matching prefix is in avp
+   index 0. 'mode' can be an integer constant or a pseudo variable with
+   integer value.
 
-   Example 1.12. mt_match usage
+   Example 1.13. mt_match usage
 ...
 mt_match("mytree", "$rU", "0");
 ...

+ 14 - 0
modules/mtree/doc/mtree.xml

@@ -23,11 +23,25 @@
 		<surname>Mierla</surname>
 		    <email>[email protected]</email>
 	    </editor>
+	    <author>
+		<firstname>Juha</firstname>
+		<surname>Heinanen</surname>
+		<affiliation><orgname>tutpro.com</orgname></affiliation>
+	    </author>
+	    <editor>
+		<firstname>Juha</firstname>
+		<surname>Heinanen</surname>
+		    <email>[email protected]</email>
+	    </editor>
 	</authorgroup>
 	<copyright>
 	    <year>2010</year>
 	    <holder>Daniel-Constantin Mierla (asipto.com)</holder>
 	</copyright>
+	<copyright>
+	    <year>2011</year>
+	    <holder>Juha Heinanen</holder>
+	</copyright>
     </bookinfo>
     <toc></toc>
     

+ 22 - 3
modules/mtree/doc/mtree_admin.xml

@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding='ISO-8859-1'?>
+<?xml version="1.0" encoding="iso-8859-1"?>
 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
 "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
 
@@ -235,6 +235,26 @@ modparam("mtree", "pv_value", "$var(mtval)")
 	    </example>
 	</section>
 
+	<section>
+	    <title><varname>pv_values</varname> (string)</title>
+	    <para>
+		The PV spec where to store the matched values when mtree consists of string values and mode of mt_match() call has value 2. It can be any AVP.
+		</para>
+	    <para>
+		<emphasis>
+		    Default value is <quote>$avp(s:tvalues)</quote>.
+		</emphasis>
+	    </para>
+	    <example>
+		<title>Set <varname>pv_values</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("mtree", "pv_values", "$avp(mtvals)")
+...
+</programlisting>
+	    </example>
+	</section>
+
 	<section>
 	    <title><varname>mt_tree_type</varname> (integer)</title>
 	    <para>
@@ -284,8 +304,7 @@ modparam("mtree", "mt_ignore_duplicates", 1)
 		<function moreinfo="none">mt_match(mtree, pv, mode)</function>
 	    </title>
 	    <para>
-		Match 'pv' value against mtree. mode is reserved for further
-		use, now set any integer or PV.
+		Match 'pv' value against 'mtree'. If 'mtree' consists of string values (mtree type = 0) and value of 'mode' is NOT 2, sets value of longest matching prefix to pseudo variable specified by pv_value parameter. If 'mtree' consists of string values (mtree type = 0) and value of 'mode' is 2, sets values of all matching prefixes to avp specified by pv_values parameter so that value of longest matching prefix is in avp index 0.  'mode' can be an integer constant or a pseudo variable with integer value.
 	    </para>
 		<example>
 		<title><function>mt_match</function> usage</title>

+ 51 - 2
modules/mtree/mtree.c

@@ -40,6 +40,7 @@
 //extern str mt_char_list = {"1234567890*",11};
 extern str mt_char_list;
 extern pv_spec_t pv_value;
+extern pv_spec_t pv_values;
 extern pv_spec_t pv_dstid;
 extern pv_spec_t pv_weight;
 extern int _mt_tree_type;
@@ -311,6 +312,51 @@ str* mt_get_tvalue(m_tree_t *pt, str *tomatch, int *plen)
 	return tvalue;
 }
 
+int mt_add_tvalues(struct sip_msg *msg, m_tree_t *pt, str *tomatch)
+{
+    int l, len;
+    mt_node_t *itn;
+    int_str val, values_avp_name;
+    unsigned short values_name_type;
+
+    if (pt == NULL || tomatch == NULL || tomatch->s == NULL) {
+	LM_ERR("bad parameters\n");
+	return -1;
+    }
+    
+    if (pv_get_avp_name(msg, &pv_values.pvp, &values_avp_name,
+			&values_name_type) < 0) {
+	LM_ERR("cannot get values avp name\n");
+	return -1;
+    }
+	
+    l = len = 0;
+    itn = pt->head;
+
+    while (itn != NULL && l < tomatch->len && l < MT_MAX_DEPTH) {
+	/* check validity */
+	if(_mt_char_table[(unsigned int)tomatch->s[l]]==255) {
+	    LM_ERR("invalid char at %d in [%.*s]\n",
+		   l, tomatch->len, tomatch->s);
+	    return -1;
+	}
+
+	if (itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalue.s != NULL) {
+	    val.s = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalue;
+	    LM_DBG("adding avp <%.*s> with value <%.*s>\n",
+		   values_avp_name.s.len, values_avp_name.s.s,
+		   val.s.len, val.s.s);
+	    add_avp(values_name_type|AVP_VAL_STR, values_avp_name, val);
+	    len = l+1;
+	}
+		
+	itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child;
+	l++;	
+    }
+
+    return 0;
+}
+
 int mt_match_prefix(struct sip_msg *msg, m_tree_t *it,
 		str *tomatch, int mode)
 {
@@ -343,6 +389,9 @@ int mt_match_prefix(struct sip_msg *msg, m_tree_t *it,
 	n = 0;
 	if(it->type==MT_TREE_SVAL)
 	{
+	        if (mode == 2) 
+		    return mt_add_tvalues(msg, it, tomatch);
+
 		tvalue = mt_get_tvalue(it, tomatch, &len);
 		if(tvalue==NULL)
 		{
@@ -684,8 +733,8 @@ int mt_table_spec(char* val)
 	/* add new tname*/
 	if(it==NULL || str_strcmp(&it->tname, &tmp.tname)>0)
 	{
-		LM_DBG("adding new tname [%s]\n", tmp.tname.s);
-		
+                LM_DBG("adding new tname [%s]\n", tmp.tname.s);
+
 		ndl = mt_init_tree(&tmp.tname, &tmp.dbtable, tmp.type);
 		if(ndl==NULL)
 		{

+ 16 - 1
modules/mtree/mtree_mod.c

@@ -82,10 +82,12 @@ static str tvalue_column  = str_init("tvalue");
 str mt_char_list = {"0123456789", 10};
 
 static str value_param = {"$avp(s:tvalue)", 0};
+static str values_param = {"$avp(s:tvalues)", 0};
 static str dstid_param = {"$avp(s:tdstid)", 0};
 static str weight_param = {"$avp(s:tweight)", 0};
 static str count_param = {"$avp(s:tcount)", 0};
 pv_spec_t pv_value;
+pv_spec_t pv_values;
 pv_spec_t pv_dstid;
 pv_spec_t pv_weight;
 pv_spec_t pv_count;
@@ -133,6 +135,7 @@ static param_export_t params[]={
 	{"char_list",      STR_PARAM, &mt_char_list.s},
 	{"fetch_rows",     INT_PARAM, &mt_fetch_rows},
 	{"pv_value",       STR_PARAM, &value_param.s},
+	{"pv_values",      STR_PARAM, &values_param.s},
 	{"pv_dstid",       STR_PARAM, &dstid_param.s},
 	{"pv_weight",      STR_PARAM, &weight_param.s},
 	{"pv_count",       STR_PARAM, &count_param.s},
@@ -186,6 +189,7 @@ static int mod_init(void)
 	tvalue_column.len = strlen(tvalue_column.s);
 
 	value_param.len = strlen(value_param.s);
+	values_param.len = strlen(values_param.s);
 	dstid_param.len = strlen(dstid_param.s);
 	weight_param.len = strlen(weight_param.s);
 	count_param.len = strlen(count_param.s);
@@ -197,6 +201,12 @@ static int mod_init(void)
 		return -1;
 	}
 
+	if (pv_parse_spec(&values_param, &pv_values) <0
+	    || pv_values.type != PVT_AVP) {
+	    LM_ERR("cannot parse values avp\n");
+	    return -1;
+	}
+
 	if(pv_parse_spec(&dstid_param, &pv_dstid)<0
 			|| pv_dstid.type!=PVT_AVP)
 	{
@@ -483,7 +493,7 @@ static int mt_load_db(str *tname)
 		LM_ERR("no db connection\n");
 		return -1;
 	}
-	LM_ERR("attempting to load [%.*s]\n", tname->len, tname->s);
+
 	old_tree = mt_get_tree(tname);
 	if(old_tree==NULL)
 	{
@@ -923,6 +933,11 @@ struct mi_root* mt_mi_summary(struct mi_root* cmd_tree, void* param)
 				pt->tname.s, pt->tname.len);
 		if(attr == NULL)
 			goto error;
+		val.s = int2str(pt->type, &val.len);
+		attr = add_mi_attr(node, MI_DUP_VALUE, "TTYPE", 5,
+				   val.s, val.len);
+		if(attr == NULL)
+			goto error;
 		val.s = int2str(pt->memsize, &val.len);
 		attr = add_mi_attr(node, MI_DUP_VALUE, "MEMSIZE", 7,
 				val.s, val.len);