Skip to content

Commit

Permalink
domain: add attributes support
Browse files Browse the repository at this point in the history
Provide a method to pass certain attributes to a domain.
  • Loading branch information
razvancrainea committed Aug 4, 2014
1 parent 5bc30f4 commit 57ec0f3
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 60 deletions.
12 changes: 11 additions & 1 deletion db/schema/domain.xml
Expand Up @@ -9,7 +9,7 @@

<table id="domain" xmlns:db="http://docbook.org/ns/docbook">
<name>domain</name>
<version>2</version>
<version>3</version>
<type db="mysql">&MYSQL_TABLE_TYPE;</type>
<description>
<db:para>This table is used by the domain module to determine if a host part of a URI is "local" or not. More information about the domain module can be found at: &OPENSIPS_MOD_DOC;domain.html
Expand All @@ -35,6 +35,16 @@
<natural/>
</column>

<column id="attrs">
<name>attrs</name>
<type>string</type>
<size>255</size>
<description>Domain Attributes</description>
<default><null/></default>
<null/>
</column>


<column>
<name>last_modified</name>
<type>datetime</type>
Expand Down
39 changes: 31 additions & 8 deletions modules/domain/README
Expand Up @@ -30,9 +30,10 @@ Juha Heinanen

1.4. Exported Functions

1.4.1. is_from_local()
1.4.2. is_uri_host_local()
1.4.3. is_domain_local(pseudo_variable)
1.4.1. is_from_local([attrs_pvar])
1.4.2. is_uri_host_local([attrs_pvar])
1.4.3. is_domain_local(pseudo_variable [,
attrs_pvar])

1.5. Exported MI Functions

Expand Down Expand Up @@ -128,10 +129,13 @@ modparam("domain", "domain_col", "domain_name")

1.4. Exported Functions

1.4.1. is_from_local()
1.4.1. is_from_local([attrs_pvar])

Checks based on domain table if host part of From header uri is
one of the local domains that the proxy is responsible for
one of the local domains that the proxy is responsible for. The
argument is optional and if present it should contain a
writable pseudo variable that will be populated with the
attributes from the database.

This function can be used from REQUEST_ROUTE.

Expand All @@ -141,15 +145,22 @@ if (is_from_local()) {
...
};
...
if (is_from_local("$var(attrs)")) {
xlog("Domain attributes are $var(attrs)\n");
...
};
...

1.4.2. is_uri_host_local()
1.4.2. is_uri_host_local([attrs_pvar])

If called from route or failure route block, checks based on
domain table if host part of Request-URI is one of the local
domains that the proxy is responsible for. If called from
branch route, the test is made on host part of URI of first
branch, which thus must have been appended to the transaction
before is_uri_host_local() is called.
before is_uri_host_local() is called. The argument is optional
and if present it should contain a writable pseudo variable
that will be populated with the attributes from the database.

This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
BRANCH_ROUTE.
Expand All @@ -160,8 +171,12 @@ if (is_uri_host_local()) {
...
};
...
if (is_uri_host_local("$var(attrs)")) {
xlog("Domain attributes are $var(attrs)\n");
...
};

1.4.3. is_domain_local(pseudo_variable)
1.4.3. is_domain_local(pseudo_variable [, attrs_pvar])

This function checks if the domain contained in the
pseudo_variable is local.
Expand All @@ -174,6 +189,10 @@ if (is_uri_host_local()) {
* is_domain_local("$rd") is same as is_uri_host_local()
* is_domain_local("$fd") is same as is_from_local()

The second argument is optional and if present it should
contain a writable pseudo variable that will be populated with
the attributes from the database.

This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
BRANCH_ROUTE.

Expand All @@ -194,6 +213,10 @@ if (is_domain_local("$avp(850)")) {
if (is_domain_local("$avp(some_avp)")) {
...
};
if (is_domain_local("$avp(some_avp)", "$avp(attrs)")) {
xlog("Domain attributes are $avp(attrs)\n");
...
};
...

1.5. Exported MI Functions
Expand Down
32 changes: 28 additions & 4 deletions modules/domain/doc/domain_admin.xml
Expand Up @@ -113,10 +113,13 @@ modparam("domain", "domain_col", "domain_name")
<section>
<title>Exported Functions</title>
<section>
<title><function moreinfo="none">is_from_local()</function></title>
<title><function moreinfo="none">is_from_local([attrs_pvar])</function></title>
<para>
Checks based on domain table if host part of From header uri is
one of the local domains that the proxy is responsible for
one of the local domains that the proxy is responsible for.
The argument is optional and if present it should contain a writable
pseudo variable that will be populated with the attributes from the
database.
</para>
<para>
This function can be used from REQUEST_ROUTE.
Expand All @@ -128,12 +131,17 @@ modparam("domain", "domain_col", "domain_name")
if (is_from_local()) {
...
};
...
if (is_from_local("$var(attrs)")) {
xlog("Domain attributes are $var(attrs)\n");
...
};
...
</programlisting>
</example>
</section>
<section>
<title><function moreinfo="none">is_uri_host_local()</function></title>
<title><function moreinfo="none">is_uri_host_local([attrs_pvar])</function></title>
<para>
If called from route or failure route block, checks
based on domain table if host part of Request-URI is one
Expand All @@ -142,6 +150,9 @@ if (is_from_local()) {
part of URI of first branch, which thus must have been
appended to the transaction before is_uri_host_local()
is called.
The argument is optional and if present it should contain a writable
pseudo variable that will be populated with the attributes from the
database.
</para>
<para>
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
Expand All @@ -155,11 +166,15 @@ if (is_uri_host_local()) {
...
};
...
if (is_uri_host_local("$var(attrs)")) {
xlog("Domain attributes are $var(attrs)\n");
...
};
</programlisting>
</example>
</section>
<section>
<title><function moreinfo="none">is_domain_local(pseudo_variable)</function></title>
<title><function moreinfo="none">is_domain_local(pseudo_variable [, attrs_pvar])</function></title>
<para>
This function checks if the domain contained in the
pseudo_variable is local.
Expand All @@ -180,6 +195,11 @@ if (is_uri_host_local()) {
</para></listitem>
</itemizedlist>
<para>
The second argument is optional and if present it should contain a writable
pseudo variable that will be populated with the attributes from the
database.
</para>
<para>
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
BRANCH_ROUTE.
</para>
Expand All @@ -202,6 +222,10 @@ if (is_domain_local("$avp(850)")) {
if (is_domain_local("$avp(some_avp)")) {
...
};
if (is_domain_local("$avp(some_avp)", "$avp(attrs)")) {
xlog("Domain attributes are $avp(attrs)\n");
...
};
...
</programlisting>
</example>
Expand Down
85 changes: 62 additions & 23 deletions modules/domain/domain.c
Expand Up @@ -40,6 +40,8 @@
#include "../../pvar.h"
#include "../../str.h"

#define DOMAIN_TABLE_VERSION 3

static db_con_t* db_handle=0;
static db_func_t domain_dbf;

Expand Down Expand Up @@ -97,16 +99,21 @@ int domain_db_ver(str* name, int version)
/*
* Check if domain is local
*/
int is_domain_local(str* _host)
int is_domain_local(struct sip_msg *msg, str* _host, char *pvar)
{
pv_spec_t *pv = (pv_spec_t *)pvar;
pv_value_t val;
db_val_t *values;

if (db_mode == 0) {
db_key_t keys[1];
db_val_t vals[1];
db_key_t cols[1];
db_key_t cols[2];
db_res_t* res = NULL;

keys[0] = &domain_col;
cols[0] = &domain_col;
cols[1] = &domain_attrs_col;

if (domain_dbf.use_table(db_handle, &domain_table) < 0) {
LM_ERR("Error while trying to use domain table\n");
Expand All @@ -119,7 +126,7 @@ int is_domain_local(str* _host)
VAL_STR(vals).s = _host->s;
VAL_STR(vals).len = _host->len;

if (domain_dbf.query(db_handle, keys, 0, vals, cols, 1, 1, 0, &res) < 0
if (domain_dbf.query(db_handle, keys, 0, vals, cols, 1, 2, 0, &res) < 0
) {
LM_ERR("Error while querying database\n");
return -3;
Expand All @@ -133,11 +140,27 @@ int is_domain_local(str* _host)
} else {
LM_DBG("Realm '%.*s' is local\n",
_host->len, ZSW(_host->s));
if (pvar) {
/* XXX: what shall we do if there are duplicate entries? */
/* we only check the first row - razvanc */
values = ROW_VALUES(RES_ROWS(res));
if (!VAL_NULL(values +1)) {
if (VAL_TYPE(values + 1) == DB_STR) {
val.rs = VAL_STR(values + 1);
} else {
val.rs.s = (char *)VAL_STRING(values + 1);
val.rs.len = strlen(val.rs.s);
}
val.flags = PV_VAL_STR;
if (pv_set_value(msg, pv, 0, &val) != 0)
LM_ERR("Cannot set attributes value\n");
}
}
domain_dbf.free_result(db_handle, res);
return 1;
}
} else {
return hash_table_lookup (_host);
return hash_table_lookup (msg, _host, pv);
}

}
Expand All @@ -154,7 +177,7 @@ int is_from_local(struct sip_msg* _msg, char* _s1, char* _s2)
return -2;
}

return is_domain_local(&(puri->host));
return is_domain_local(_msg, &(puri->host), _s1);

}

Expand All @@ -167,7 +190,7 @@ int is_uri_host_local(struct sip_msg* _msg, char* _s1, char* _s2)
LM_ERR("Error while parsing R-URI\n");
return -1;
}
return is_domain_local(&(_msg->parsed_uri.host));
return is_domain_local(_msg, &(_msg->parsed_uri.host), _s1);
}


Expand All @@ -187,7 +210,7 @@ int w_is_domain_local(struct sip_msg* _msg, char* _sp, char* _s2)
LM_DBG("Missing domain name\n");
return -1;
}
return is_domain_local(&(pv_val.rs));
return is_domain_local(_msg, &(pv_val.rs), _s2);
} else {
LM_DBG("Pseudo variable value is not string\n");
return -1;
Expand All @@ -205,26 +228,25 @@ int w_is_domain_local(struct sip_msg* _msg, char* _sp, char* _s2)
*/
int reload_domain_table ( void )
{
db_val_t vals[1];
db_key_t cols[1];
db_key_t cols[2];
db_res_t* res = NULL;
db_row_t* row;
db_val_t* val;

struct domain_list **new_hash_table;
int i;

str domain, attrs;

cols[0] = &domain_col;
cols[1] = &domain_attrs_col;

if (domain_dbf.use_table(db_handle, &domain_table) < 0) {
LM_ERR("Error while trying to use domain table\n");
return -3;
}

VAL_TYPE(vals) = DB_STR;
VAL_NULL(vals) = 0;

if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 1, 0, &res) < 0) {
if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 2, 0, &res) < 0) {
LM_ERR("Error while querying database\n");
return -3;
}
Expand All @@ -244,17 +266,34 @@ int reload_domain_table ( void )

for (i = 0; i < RES_ROW_N(res); i++) {
val = ROW_VALUES(row + i);
if ((ROW_N(row) == 1) && (VAL_TYPE(val) == DB_STRING)) {

LM_DBG("Value: %s inserted into domain hash table\n",VAL_STRING(val));

if (hash_table_install(new_hash_table,(char*)VAL_STRING(val))==-1){
LM_ERR("Hash table problem\n");
domain_dbf.free_result(db_handle, res);
return -3;
}
if (VAL_TYPE(val) == DB_STRING) {
domain.s = (char *)VAL_STRING(val);
domain.len = strlen(domain.s);
} else if (VAL_TYPE(val) == DB_STR) {
domain = VAL_STR(val);
} else {
LM_ERR("Database problem\n");
LM_ERR("Database problem on domain column\n");
domain_dbf.free_result(db_handle, res);
return -3;
}
if (VAL_NULL(val + 1)) {
/* add a marker to determine whether the attributes exist or not */
attrs.len = 0;
attrs.s = NULL;
} else if (VAL_TYPE(val + 1) == DB_STRING) {
attrs.s = (char *)VAL_STRING(val + 1);
attrs.len = strlen(attrs.s);
} else if (VAL_TYPE(val + 1) == DB_STR) {
attrs = VAL_STR(val + 1);
} else {
LM_ERR("Database problem on attrs column\n");
domain_dbf.free_result(db_handle, res);
return -3;
}
LM_DBG("Value: %s inserted into domain hash table\n",VAL_STRING(val));

if (hash_table_install(new_hash_table, &domain, &attrs)==-1){
LM_ERR("Hash table problem\n");
domain_dbf.free_result(db_handle, res);
return -3;
}
Expand Down

0 comments on commit 57ec0f3

Please sign in to comment.