[sr-dev] git:master: corex: new parameter alias_subdomains

Daniel-Constantin Mierla miconda at gmail.com
Tue Sep 11 22:24:04 CEST 2012


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

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Tue Sep 11 21:52:01 2012 +0200

corex: new parameter alias_subdomains

- adds domain and all its subdomains to myself condition, registering a
  callback for check self event

---

 modules/corex/corex_lib.c |  123 +++++++++++++++++++++++++++++++++++++++++++++
 modules/corex/corex_lib.h |    4 ++
 modules/corex/corex_mod.c |   22 ++++++++
 3 files changed, 149 insertions(+), 0 deletions(-)

diff --git a/modules/corex/corex_lib.c b/modules/corex/corex_lib.c
index d8ad3c5..1f69847 100644
--- a/modules/corex/corex_lib.c
+++ b/modules/corex/corex_lib.c
@@ -26,6 +26,7 @@
 
 #include "../../dprint.h"
 #include "../../dset.h"
+#include "../../forward.h"
 
 #include "corex_lib.h"
 
@@ -88,3 +89,125 @@ int corex_append_branch(sip_msg_t *msg, gparam_t *pu, gparam_t *pq)
 
 	return ret;
 }
+
+typedef struct corex_alias {
+	str alias;
+	unsigned short port;
+	unsigned short proto;
+	int flags;
+	struct corex_alias* next;
+} corex_alias_t;
+
+static corex_alias_t *_corex_alias_list = NULL;
+
+int corex_add_alias_subdomains(char* aliasval)
+{
+	char *p = NULL;
+	corex_alias_t ta;
+	corex_alias_t *na;
+
+	memset(&ta, 0, sizeof(corex_alias_t));
+
+	p = strchr(aliasval, ':');
+	if(p==NULL) {
+		/* only hostname */
+		ta.alias.s = aliasval;
+		ta.alias.len = strlen(aliasval);
+		goto done;
+	}
+	if((p-aliasval)==3 || (p-aliasval)==4) {
+		/* check if it is protocol */
+		if((p-aliasval)==3 && strncasecmp(aliasval, "udp", 3)==0) {
+			ta.proto = PROTO_UDP;
+		} else if((p-aliasval)==3 && strncasecmp(aliasval, "tcp", 3)==0) {
+			ta.proto = PROTO_TCP;
+		} else if((p-aliasval)==3 && strncasecmp(aliasval, "tls", 3)==0) {
+			ta.proto = PROTO_TLS;
+		} else if((p-aliasval)==4 && strncasecmp(aliasval, "sctp", 4)==0) {
+			ta.proto = PROTO_SCTP;
+		} else {
+			/* use hostname */
+			ta.alias.s = aliasval;
+			ta.alias.len = p - aliasval;
+		}
+	}
+	if(ta.alias.len==0) {
+		p++;
+		if(p>=aliasval+strlen(aliasval))
+			goto error;
+		ta.alias.s = p;
+		p = strchr(ta.alias.s, ':');
+		if(p==NULL) {
+			ta.alias.len = strlen(ta.alias.s);
+			goto done;
+		}
+	}
+	/* port */
+	p++;
+	if(p>=aliasval+strlen(aliasval))
+		goto error;
+	ta.port = str2s(p, strlen(p), NULL);
+
+done:
+	if(ta.alias.len==0)
+		goto error;
+
+	na = (corex_alias_t*)pkg_malloc(sizeof(corex_alias_t));
+	if(na==NULL) {
+		LM_ERR("no memory for adding alias subdomains: %s\n", aliasval);
+		return -1;
+	}
+	memcpy(na, &ta, sizeof(corex_alias_t));
+	na->next = _corex_alias_list;
+	_corex_alias_list = na;
+
+	return 0;
+
+error:
+	LM_ERR("error adding alias subdomains: %s\n", aliasval);
+	return -1;
+}
+
+
+int corex_check_self(str* host, unsigned short port, unsigned short proto)
+{
+	corex_alias_t *ta;
+
+	for(ta=_corex_alias_list; ta; ta=ta->next) {
+		if(host->len<ta->alias.len)
+			continue;
+		if(ta->port!=0 && port!=0 && ta->port!=port)
+			continue;
+		if(ta->proto!=0 && proto!=0 && ta->proto!=proto)
+			continue;
+		if(host->len==ta->alias.len
+				&& strncasecmp(host->s, ta->alias.s, host->len)==0) {
+			/* match domain */
+			LM_DBG("check self domain match: %d:%.*s:%d\n", (int)ta->port,
+					ta->alias.len, ta->alias.s, (int)ta->proto);
+			return 1;
+		}
+		if(strncasecmp(ta->alias.s, host->s + host->len - ta->alias.len,
+					ta->alias.len)==0) {
+			if(host->s[host->len - ta->alias.len - 1]=='.') {
+				/* match sub-domain */
+				LM_DBG("check self sub-domain match: %d:%.*s:%d\n", (int)ta->port,
+					ta->alias.len, ta->alias.s, (int)ta->proto);
+				return 1;
+			}
+		}
+	}
+
+	return 0; /* no match */
+}
+
+int corex_register_check_self(void)
+{
+	if(_corex_alias_list==NULL)
+		return 0;
+	if (register_check_self_func(corex_check_self) <0 ) {
+	    LM_ERR("failed to register check self function\n");
+	    return -1;
+	}
+	return 0;
+}
diff --git a/modules/corex/corex_lib.h b/modules/corex/corex_lib.h
index c997fce..49fa712 100644
--- a/modules/corex/corex_lib.h
+++ b/modules/corex/corex_lib.h
@@ -26,4 +26,8 @@
 
 int corex_append_branch(sip_msg_t *msg, gparam_t *pu, gparam_t *pq);
 
+int corex_add_alias_subdomains(char* aliasval);
+
+int corex_register_check_self(void);
+
 #endif
diff --git a/modules/corex/corex_mod.c b/modules/corex/corex_mod.c
index 4fda1a2..5b4690c 100644
--- a/modules/corex/corex_mod.c
+++ b/modules/corex/corex_mod.c
@@ -35,6 +35,8 @@ MODULE_VERSION
 
 static int w_append_branch(sip_msg_t *msg, char *su, char *sq);
 
+int corex_alias_subdomains_param(modparam_t type, void *val);
+
 static int  mod_init(void);
 static int  child_init(int);
 static void mod_destroy(void);
@@ -52,6 +54,8 @@ static cmd_export_t cmds[]={
 };
 
 static param_export_t params[]={
+	{"alias_subdomains",  STR_PARAM|USE_FUNC_PARAM,
+								(void*)corex_alias_subdomains_param},
 	{0, 0, 0}
 };
 
@@ -83,6 +87,12 @@ static int mod_init(void)
 		return -1;
 	}
 
+	if(corex_register_check_self()<0)
+	{
+		LM_ERR("failed to register check self callback\n");
+		return -1;
+	}
+
 	return 0;
 }
 
@@ -113,3 +123,15 @@ static int w_append_branch(sip_msg_t *msg, char *su, char *sq)
 	return 1;
 }
 
+
+int corex_alias_subdomains_param(modparam_t type, void *val)
+{
+	if(val==NULL)
+		goto error;
+
+	return corex_add_alias_subdomains((char*)val);
+error:
+	return -1;
+
+}
+




More information about the sr-dev mailing list