[sr-dev] git:master: domain: Add internal function is_domain_local

Jan Janak jan at iptel.org
Fri Jul 17 11:37:13 CEST 2009


Module: sip-router
Branch: master
Commit: d8fdebf9c589516185d5c6a4ff9453148f1b76f2
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=d8fdebf9c589516185d5c6a4ff9453148f1b76f2

Author: Jan Janak <jan at iptel.org>
Committer: Jan Janak <jan at iptel.org>
Date:   Fri Jul 17 11:10:51 2009 +0200

domain: Add internal function is_domain_local

Add internal function is_domain_local that can be used to determine if
the domain name given in parameter is on the list of local domains. The
comparison of the domain name is always case insensitive.

There already is is_local function that does something similar, so we
can reuse the code from that function. is_domain_local takes a pointer
to str as parameter and this function is then called by is_local which
takes all the usual parameters that function which can be called from
the script take.

Finally, is_domain_local is exported through the internal API of the
domain module.

Function is_domain_local is now located in file domain.c and because of
db_get_did was also moved to file domain.c

---

 modules_s/domain/domain.c     |  104 +++++++++++++++++++++++++++++++++++++++++
 modules_s/domain/domain.h     |   17 +++++++
 modules_s/domain/domain_api.c |    1 +
 modules_s/domain/domain_api.h |    3 +-
 modules_s/domain/domain_mod.c |   91 +-----------------------------------
 5 files changed, 126 insertions(+), 90 deletions(-)

diff --git a/modules_s/domain/domain.c b/modules_s/domain/domain.c
index cd347bf..cb5cb32 100644
--- a/modules_s/domain/domain.c
+++ b/modules_s/domain/domain.c
@@ -22,6 +22,7 @@
 
 #include <string.h>
 #include "domain_mod.h"
+#include "hash.h"
 #include "../../dprint.h"
 #include "../../mem/shm_mem.h"
 #include "../../lib/srdb2/db.h"
@@ -308,3 +309,106 @@ int load_domains(domain_t** dest)
 	free_domain_list(list);
 	return 1;
 }
+
+
+/* Retrieve did directly from database, without using memory cache. Use 0 as
+ * the value of first parameter if you only want to know whether the entry is
+ * in the database. The function returns 1 if there is such entry, 0 if not,
+ * and -1 on error.  The result is allocated using pkg_malloc and must be
+ * freed.
+ */
+int db_get_did(str* did, str* domain)
+{
+	db_res_t* res = NULL;
+	db_rec_t* rec;
+
+	if (!domain) {
+		ERR("BUG:Invalid parameter value\n");
+		goto err;
+	}
+
+	get_did_cmd->match[0].v.lstr = *domain;
+
+	if (db_exec(&res, get_did_cmd) < 0) {
+		ERR("Error in database query\n");
+		goto err;
+	}
+
+	rec = db_first(res);
+	if (rec) {
+		/* Test flags first, we are only interested in rows
+		 * that are not disabled
+		 */
+		if (rec->fld[1].flags & DB_NULL || (rec->fld[1].v.bitmap &
+											SRDB_DISABLED)) {
+			db_res_free(res);
+			return 0;
+		}
+
+		if (did) {
+			if (rec->fld[0].flags & DB_NULL) {
+				did->len = 0;
+				did->s = 0;
+				WARN("Domain '%.*s' has NULL did\n",
+					 domain->len, ZSW(domain->s));
+			} else {
+				did->s = pkg_malloc(rec->fld[0].v.lstr.len);
+				if (!did->s) {
+					ERR("No memory left\n");
+					goto err;
+				}
+				memcpy(did->s, rec->fld[0].v.lstr.s, rec->fld[0].v.lstr.len);
+				did->len = rec->fld[0].v.lstr.len;
+			}
+		}
+
+		db_res_free(res);
+		return 1;
+	} else {
+		db_res_free(res);
+		return 0;
+	}
+
+ err:
+	if (res) db_res_free(res);
+	return -1;
+}
+
+
+/* Check if the domain name given in the parameter is one
+ * of the locally configured domain names.
+ * Returns 1 if yes and -1 otherwise
+ */
+int is_domain_local(str* domain)
+{
+	str tmp;
+
+	/* Make a temporary copy, domain name comparisons are always
+	 * case insensitive
+	 */
+	tmp.s = pkg_malloc(domain->len);
+	if (!tmp.s) {
+		ERR("No memory left\n");
+		return -1;
+	}
+	memcpy(tmp.s, domain->s, domain->len);
+	tmp.len = domain->len;
+	strlower(&tmp);
+
+	if (!db_mode) {
+		switch(db_get_did(0, &tmp)) {
+		case 1:  goto found;
+		default: goto not_found;
+		}
+	} else {
+		if (hash_lookup(0, *active_hash, &tmp) == 1) goto found;
+		else goto not_found;
+	}
+
+ found:
+	pkg_free(tmp.s);
+	return 1;
+ not_found:
+	pkg_free(tmp.s);
+	return -1;
+}
diff --git a/modules_s/domain/domain.h b/modules_s/domain/domain.h
index d79a593..e0d0d37 100644
--- a/modules_s/domain/domain.h
+++ b/modules_s/domain/domain.h
@@ -72,4 +72,21 @@ void free_domain_list(domain_t* list);
 
 typedef int (*domain_get_did_t)(str* did, str* domain);
 
+
+/* Retrieve did directly from database, without using memory cache. Use 0 as
+ * the value of first parameter if you only want to know whether the entry is
+ * in the database. The function returns 1 if there is such entry, 0 if not,
+ * and -1 on error.  The result is allocated using pkg_malloc and must be
+ * freed.
+ */
+int db_get_did(str* did, str* domain);
+
+/* Check if the domain name given in the parameter is one
+ * of the locally configured domain names.
+ * Returns 1 if yes and -1 otherwise
+ */
+typedef int (*is_domain_local_f)(str* domain);
+int is_domain_local(str* domain);
+
+
 #endif /* _DOMAIN_H */
diff --git a/modules_s/domain/domain_api.c b/modules_s/domain/domain_api.c
index c88e5c8..0996c67 100644
--- a/modules_s/domain/domain_api.c
+++ b/modules_s/domain/domain_api.c
@@ -31,5 +31,6 @@ int bind_domain(domain_api_t* api)
 		ERR("Invalid parameter value\n");
 		return -1;
 	}
+	api->is_domain_local = is_domain_local;
 	return 0;
 }
diff --git a/modules_s/domain/domain_api.h b/modules_s/domain/domain_api.h
index 16ddd1f..2882332 100644
--- a/modules_s/domain/domain_api.h
+++ b/modules_s/domain/domain_api.h
@@ -25,9 +25,10 @@
 
 #include "../../sr_module.h"
 #include "../../dprint.h"
+#include "domain.h"
 
 typedef struct domain_api {
-
+	is_domain_local_f is_domain_local;
 } domain_api_t;
 
 typedef int (*bind_domain_f)(domain_api_t* api);
diff --git a/modules_s/domain/domain_mod.c b/modules_s/domain/domain_mod.c
index c7e0515..e5d5cb0 100644
--- a/modules_s/domain/domain_mod.c
+++ b/modules_s/domain/domain_mod.c
@@ -381,107 +381,20 @@ static void destroy(void)
 }
 
 
-/* Retrieve did directly from database, without using memory cache. Use 0 as
- * the value of first parameter if you only want to know whether the entry is
- * in the database. The function returns 1 if there is such entry, 0 if not,
- * and -1 on error.  The result is allocated using pkg_malloc and must be
- * freed.
- */
-static int db_get_did(str* did, str* domain)
-{
-	db_res_t* res = NULL;
-	db_rec_t* rec;
-
-	if (!domain) {
-		ERR("BUG:Invalid parameter value\n");
-		goto err;
-	}
-
-	get_did_cmd->match[0].v.lstr = *domain;
-
-	if (db_exec(&res, get_did_cmd) < 0) {
-		ERR("Error in database query\n");
-		goto err;
-	}
-
-	rec = db_first(res);
-	if (rec) {
-		/* Test flags first, we are only interested in rows
-		 * that are not disabled
-		 */
-		if (rec->fld[1].flags & DB_NULL || (rec->fld[1].v.bitmap &
-											SRDB_DISABLED)) {
-			db_res_free(res);
-			return 0;
-		}
-
-		if (did) {
-			if (rec->fld[0].flags & DB_NULL) {
-				did->len = 0;
-				did->s = 0;
-				WARN("Domain '%.*s' has NULL did\n",
-					 domain->len, ZSW(domain->s));
-			} else {
-				did->s = pkg_malloc(rec->fld[0].v.lstr.len);
-				if (!did->s) {
-					ERR("No memory left\n");
-					goto err;
-				}
-				memcpy(did->s, rec->fld[0].v.lstr.s, rec->fld[0].v.lstr.len);
-				did->len = rec->fld[0].v.lstr.len;
-			}
-		}
-
-		db_res_free(res);
-		return 1;
-	} else {
-		db_res_free(res);
-		return 0;
-	}
-
- err:
-	if (res) db_res_free(res);
-	return -1;
-}
-
 
 /*
  * Check if domain is local
  */
 static int is_local(struct sip_msg* msg, char* fp, char* s2)
 {
-	str domain, tmp;
+	str domain;
 
 	if (get_str_fparam(&domain, msg, (fparam_t*)fp) != 0) {
 		ERR("Unable to get domain to check\n");
 		return -1;
 	}
 
-	tmp.s = pkg_malloc(domain.len);
-	if (!tmp.s) {
-		ERR("No memory left\n");
-		return -1;
-	}
-	memcpy(tmp.s, domain.s, domain.len);
-	tmp.len = domain.len;
-	strlower(&tmp);
-
-	if (!db_mode) {
-		switch(db_get_did(0, &tmp)) {
-		case 1:  goto found;
-		default: goto not_found;
-		}
-	} else {
-		if (hash_lookup(0, *active_hash, &tmp) == 1) goto found;
-		else goto not_found;
-	}
-
- found:
-	pkg_free(tmp.s);
-	return 1;
- not_found:
-	pkg_free(tmp.s);
-	return -1;
+	return is_domain_local(&domain);
 }
 
 




More information about the sr-dev mailing list