Skip to content

Commit

Permalink
dialplan: Allow re-pointing the default partition
Browse files Browse the repository at this point in the history
This is useful in situations where script writers do not intend to cache
the default 'dialplan' table, rather only the named partitions.

Example syntax:
    db_default_url = "mysql://opensips:opensipsrw@127.0.0.1/opensips"
    ...
    modparam("dialplan", "partition", "
	pstn:
	    table_name = dialplan_pstn")
    modparam("dialplan", "partition", "default: pstn")

(cherry picked from commit efee343)
(cherry picked from commit 4abd261)
  • Loading branch information
liviuchircu committed Nov 4, 2020
1 parent 02df9b8 commit 03bf0ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
16 changes: 11 additions & 5 deletions modules/dialplan/dialplan.c
Expand Up @@ -259,6 +259,14 @@ static int dp_create_head(const str *in)
if (!params)
goto bad_input;

/* support for the "default: my_part" syntax */
if (!props->next && !params->next && !ZSTR(params->s)) {
dp_df_part = params->s;
LM_DBG("changing the default partition to '%.*s'\n",
dp_df_part.len, dp_df_part.s);
return 0;
}

if (str_match(&params->s, _str(PARAM_URL))) {
have_db_url = 1;
dp_head_insert(DP_TYPE_URL, &params->next->s, &partition);
Expand Down Expand Up @@ -349,7 +357,7 @@ static int mod_init(void)
timerec_column.len = strlen(timerec_column.s);
disabled_column.len = strlen(disabled_column.s);

if (!dp_df_head) {
if (!dp_df_head && str_match(&dp_df_part, _str(DEFAULT_PARTITION))) {
if (default_dp_db_url.s)
dp_head_insert(DP_TYPE_URL, &default_dp_db_url, &dp_df_part);

Expand Down Expand Up @@ -482,13 +490,12 @@ static int dp_update(struct sip_msg * msg, pv_spec_t * src, pv_spec_t * dest,

static int fix_partition(void** param)
{
str def_str = str_init(DEFAULT_PARTITION);
str *s=(str*)*param;

/* handle the special case when the fix is triggered for
missing parameter */
if (s==NULL)
s = &def_str;
s = &dp_df_part;

*param = (void*)dp_get_connection( s );
if (*param==NULL) {
Expand Down Expand Up @@ -842,10 +849,9 @@ static mi_response_t *mi_translate(const mi_params_t *params,
static mi_response_t *mi_translate2(const mi_params_t *params,
struct mi_handler *async_hdl)
{
str def_str = str_init(DEFAULT_PARTITION);
dp_connection_list_t *part;

part = dp_get_connection(&def_str);
part = dp_get_connection(&dp_df_part);
if (part==NULL){
LM_ERR("translating without partition, but no default defined\n");
return init_mi_error(404, MI_SSTR("'default' partition not found"));
Expand Down
7 changes: 4 additions & 3 deletions modules/dialplan/doc/dialplan_admin.xml
Expand Up @@ -346,15 +346,16 @@ modparam("dialplan", "partition", "
</example>

<example>
<title> The 'default' partition inherits its URL from <varname>'db_default_url'</varname></title>
<title> Define the 'pstn' partition and make it the 'default' partition, so we avoid loading the 'dialplan' table</title>
<programlisting format="linespecific">
...
db_default_url = "mysql://opensips:opensipsrw@localhost/opensips"

loadmodule "dialplan.so"
modparam("dialplan", "partition", "
default:
table_name = dialplan")
pstn:
table_name = dialplan_pstn")
modparam("dialplan", "partition", "default: pstn")
...
</programlisting>
</example>
Expand Down
11 changes: 11 additions & 0 deletions modules/dialplan/dp_db.c
Expand Up @@ -169,6 +169,7 @@ void dp_disconnect_db(dp_connection_list_p dp_conn)
int init_data(void)
{
dp_head_p start, tmp;
int found_df_part = 0;

start = dp_hlist;
if (!start) {
Expand All @@ -179,6 +180,10 @@ int init_data(void)
while (start) {
LM_DBG("Adding partition with name [%.*s]\n",
start->partition.len, start->partition.s);

if (str_match(&start->partition, &dp_df_part))
found_df_part = 1;

if (!dp_add_connection(start)) {
LM_ERR("failed to initialize partition '%.*s'\n",
start->partition.len, start->partition.s);
Expand All @@ -190,6 +195,12 @@ int init_data(void)
pkg_free(tmp);
}

if (!found_df_part) {
LM_ERR("partition '%.*s' is not defined\n",
dp_df_part.len, dp_df_part.s);
return -1;
}

dp_hlist = NULL;
return 0;
}
Expand Down

0 comments on commit 03bf0ae

Please sign in to comment.