瀏覽代碼

domain: use core fixup helper functions for string parameters

- simplifies getting the values and it is more flexible allowing static
  strings as well
Daniel-Constantin Mierla 8 年之前
父節點
當前提交
9dd7e97a74
共有 2 個文件被更改,包括 64 次插入81 次删除
  1. 59 76
      src/modules/domain/domain.c
  2. 5 5
      src/modules/domain/domain_mod.c

+ 59 - 76
src/modules/domain/domain.c

@@ -29,7 +29,7 @@
 #include "../../core/ut.h"
 #include "../../core/dset.h"
 #include "../../core/route.h"
-#include "../../core/pvar.h"
+#include "../../core/mod_fix.h"
 #include "../../core/str.h"
 
 static db1_con_t* db_handle=0;
@@ -157,28 +157,17 @@ int is_uri_host_local(struct sip_msg* _msg, char* _s1, char* _s2)
  */
 int w_is_domain_local(struct sip_msg* _msg, char* _sp, char* _s2)
 {
-    pv_spec_t *sp;
-    pv_value_t pv_val;
     struct attr_list *attrs;
     str did;
+	str sdomain;
 
-    sp = (pv_spec_t *)_sp;
-
-    if (sp && (pv_get_spec_value(_msg, sp, &pv_val) == 0)) {
-	if (pv_val.flags & PV_VAL_STR) {
-	    if (pv_val.rs.len == 0 || pv_val.rs.s == NULL) {
-		LM_DBG("missing domain name\n");
+	if(fixup_get_svalue(_msg, (gparam_t*)_sp, &sdomain)<0) {
+		LM_ERR("cannot get domain paramter\n");
 		return -1;
-	    }
-	    return hash_table_lookup(&(pv_val.rs), &did, &attrs);
-	} else {
-	   LM_DBG("domain pseudo variable value is not string\n");
-	   return -1;
 	}
-    } else {
-	LM_DBG("cannot get domain pseudo variable value\n");
-	return -1;
-    }
+
+	return hash_table_lookup(&sdomain, &did, &attrs);
+
 }
 
 /*
@@ -186,86 +175,80 @@ int w_is_domain_local(struct sip_msg* _msg, char* _sp, char* _s2)
  */
 int w_lookup_domain(struct sip_msg* _msg, char* _sp, char* _prefix)
 {
-    pv_spec_t *sp;
-    pv_value_t pv_val;
+
     int_str name, val;
     struct attr_list *attrs;
     str *prefix, did;
     unsigned short flags;
+	str sdomain;
+	str sprefix;
 
-    sp = (pv_spec_t *)_sp;
-    prefix = (str *)_prefix;
+	if(fixup_get_svalue(_msg, (gparam_t*)_sp, &sdomain)<0) {
+		LM_ERR("cannot get domain paramter\n");
+		return -1;
+	}
+	if(_prefix) {
+		if(fixup_get_svalue(_msg, (gparam_t*)_prefix, &sprefix)<0) {
+			LM_ERR("cannot get prefix paramter\n");
+			return -1;
+		}
+	}
 
-    if (sp && (pv_get_spec_value(_msg, sp, &pv_val) == 0)) {
-	if (pv_val.flags & PV_VAL_STR) {
-	    if (pv_val.rs.len == 0 || pv_val.rs.s == NULL) {
-		LM_DBG("domain name pseudo variable is missing\n");
+	if (hash_table_lookup(&sdomain, &did, &attrs) != 1) {
 		return -1;
-	    }
-	    if (hash_table_lookup(&(pv_val.rs), &did, &attrs) == 1) {
-		while (attrs) {
-		    if (attrs->type == 2)
+	}
+
+	while (attrs) {
+		if (attrs->type == 2)
 			flags = AVP_NAME_STR | AVP_VAL_STR;
-		    else
+		else
 			flags = AVP_NAME_STR;
-		    if (_prefix) {
-			name.s.len = prefix->len + attrs->name.len;
+		if (_prefix) {
+			name.s.len = sprefix.len + attrs->name.len;
 			name.s.s = pkg_malloc(name.s.len);
 			if (name.s.s == NULL) {
-			    ERR("no pkg memory for avp name\n");
-			    return -1;
+				ERR("no pkg memory for avp name\n");
+				return -1;
 			}
-			memcpy(name.s.s, prefix->s, prefix->len);
-			memcpy(name.s.s + prefix->len, attrs->name.s,
-			       attrs->name.len);
-		    } else {
+			memcpy(name.s.s, sprefix.s, sprefix.len);
+			memcpy(name.s.s + sprefix.len, attrs->name.s, attrs->name.len);
+		} else {
 			name.s = attrs->name;
-		    }
-		    if (add_avp(flags, name, attrs->val) < 0) {
+		}
+		if (add_avp(flags, name, attrs->val) < 0) {
 			LM_ERR("unable to add a new AVP '%.*s'\n",
-			       name.s.len, name.s.s);
+					name.s.len, name.s.s);
 			if (_prefix) pkg_free(name.s.s);
 			return -1;
-		    }
-		    LM_DBG("added AVP '%.*s'\n", name.s.len, name.s.s);
-		    if (prefix) pkg_free(name.s.s);
-		    attrs = attrs->next;
 		}
-		flags = AVP_NAME_STR | AVP_VAL_STR;
-		if (_prefix) {
-		    name.s.len = prefix->len + 3;
-		    name.s.s = pkg_malloc(name.s.len);
-		    if (name.s.s == NULL) {
+		LM_DBG("added AVP '%.*s'\n", name.s.len, name.s.s);
+		if (prefix) pkg_free(name.s.s);
+		attrs = attrs->next;
+	}
+	flags = AVP_NAME_STR | AVP_VAL_STR;
+	if (_prefix) {
+		name.s.len = sprefix.len + 3;
+		name.s.s = pkg_malloc(name.s.len);
+		if (name.s.s == NULL) {
 			ERR("no pkg memory for avp name\n");
 			return -1;
-		    }
-		    memcpy(name.s.s, prefix->s, prefix->len);
-		    memcpy(name.s.s + prefix->len, "did", 3);
-		} else {
-		    name.s.s = "did";
-		    name.s.len = 3;
 		}
-		val.s = did;
-		if (add_avp(flags, name, val) < 0) {
-		    LM_ERR("unable to add a new AVP '%.*s'\n",
-			   name.s.len, name.s.s);
-		    if (_prefix) pkg_free(name.s.s);
-		    return -1;
-		}		
-		LM_DBG("added AVP '%.*s'\n", name.s.len, name.s.s);
+		memcpy(name.s.s, sprefix.s, sprefix.len);
+		memcpy(name.s.s + sprefix.len, "did", 3);
+	} else {
+		name.s.s = "did";
+		name.s.len = 3;
+	}
+	val.s = did;
+	if (add_avp(flags, name, val) < 0) {
+		LM_ERR("unable to add a new AVP '%.*s'\n",
+			name.s.len, name.s.s);
 		if (_prefix) pkg_free(name.s.s);
-		return 1;
-	    } else {
 		return -1;
-	    }
-	} else {
-	   LM_DBG("domain name pseudo variable value is not string\n");
-	   return -1;
 	}
-    } else {
-	LM_DBG("cannot get domain name pseudo variable value\n");
-	return -1;
-    }
+	LM_DBG("added AVP '%.*s'\n", name.s.len, name.s.s);
+	if (_prefix) pkg_free(name.s.s);
+	return 1;
 }
 
 /*
@@ -281,7 +264,7 @@ int domain_check_self(str* host, unsigned short port, unsigned short proto)
     struct attr_list *attrs;
     str did;
     if (hash_table_lookup(host, &did, &attrs) > 0)
-	return 1;
+		return 1;
     return 0;
 }
 

+ 5 - 5
src/modules/domain/domain_mod.c

@@ -93,14 +93,14 @@ static cmd_export_t cmds[] = {
 		REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
 	{"is_uri_host_local", (cmd_function)is_uri_host_local, 0, 0, 0,
 		REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE|LOCAL_ROUTE},
-	{"is_domain_local", (cmd_function)w_is_domain_local, 1, fixup_pvar_null,
-		fixup_free_pvar_null,
+	{"is_domain_local", (cmd_function)w_is_domain_local, 1, fixup_spve_null,
+		fixup_free_spve_null,
 		REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
 	{"lookup_domain", (cmd_function)w_lookup_domain_no_prefix, 1,
-		fixup_pvar_null, fixup_free_pvar_null,
+		fixup_spve_null, fixup_free_spve_null,
 		REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
-	{"lookup_domain", (cmd_function)w_lookup_domain, 2, fixup_pvar_str,
-		fixup_free_pvar_str,
+	{"lookup_domain", (cmd_function)w_lookup_domain, 2, fixup_spve_spve,
+		fixup_free_spve_spve,
 		REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
 	{"bind_domain", (cmd_function)bind_domain, 1, 0, 0, 0},
 	{0, 0, 0, 0, 0, 0}