Skip to content

Commit

Permalink
Fix dialplan module when using db_text backend.
Browse files Browse the repository at this point in the history
The db_text makes no difference between NULL or "empty string" values in DB -> both are internally translated as NULL . The dialplan module, in a very abusive way, forces "not null" (in DB schema and in the code for data validation) even for columns that are optional (like subst_exp, repl_exp, timerec and attrs). Besides being bogus (if a column is not to be used, you have to set it to empty string rather than let it NULL), it makes impossible the usage of db_text with dialplan.
This fix allows (DB and code) the mentioned DB columns to be also NULL.
The change is backward compatible, it should not break any existing usage of the dialplan module.

(cherry picked from commit 8f54897)
  • Loading branch information
bogdan-iancu committed Jun 27, 2016
1 parent 055730a commit 618ff92
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
4 changes: 4 additions & 0 deletions db/schema/dialplan.xml
Expand Up @@ -66,20 +66,23 @@
<name>subst_exp</name>
<type>string</type>
<size>64</size>
<null/>
<description>Substitution expression.</description>
</column>

<column id="repl_exp">
<name>repl_exp</name>
<type>string</type>
<size>32</size>
<null/>
<description>Replacement expression (sed like).</description>
</column>

<column id="timerec">
<name>timerec</name>
<type>string</type>
<size>255</size>
<null/>
<description>Time recurrence used to match this rule.</description>
</column>

Expand All @@ -95,6 +98,7 @@
<name>attrs</name>
<type>string</type>
<size>32</size>
<null/>
<description>General attributes string to be returned in case of rule matching.</description>
</column>

Expand Down
2 changes: 1 addition & 1 deletion modules/dialplan/dialplan.c
Expand Up @@ -761,7 +761,7 @@ static int dp_translate_f(struct sip_msg *msg, char *str1, char *str2,
/* we are done reading -> unref the data */
lock_stop_read( connection->ref_lock );

if (attr_spec) {
if (attr_spec && attrs.s && attrs.len) {
pval.flags = PV_VAL_STR;
pval.rs = attrs;

Expand Down
47 changes: 28 additions & 19 deletions modules/dialplan/dp_db.c
Expand Up @@ -48,14 +48,20 @@ str attrs_column = str_init(ATTRS_COL);
str timerec_column = str_init(TIMEREC_COL);


#define GET_STR_VALUE(_res, _values, _index)\
#define GET_STR_VALUE(_res, _values, _index, _null)\
do{\
if ( VAL_NULL((_values)+ (_index)) ) { \
LM_ERR(" values %d is NULL - not allowed\n",_index);\
goto err;\
} \
(_res).s = VAL_STR((_values)+ (_index)).s;\
(_res).len = strlen(VAL_STR((_values)+ (_index)).s);\
if ( VAL_NULL((_values)+ (_index))) { \
if ( !_null) { \
LM_ERR(" values %d is NULL - not allowed\n",_index);\
goto err;\
} else { \
(_res).s = NULL; \
(_res).len = 0; \
} \
} else { \
(_res).s = VAL_STR((_values)+ (_index)).s;\
(_res).len = strlen(VAL_STR((_values)+ (_index)).s);\
}\
}while(0);

void destroy_rule(dpl_node_t * rule);
Expand Down Expand Up @@ -458,7 +464,7 @@ dpl_node_t * build_rule(db_val_t * values)
repl_comp = 0;
new_rule = 0;

GET_STR_VALUE(match_exp, values, 3);
GET_STR_VALUE(match_exp, values, 3, 0);
if(matchop == REGEX_OP){

LM_DBG("Compiling %.*s expression with flag: %d\n",
Expand All @@ -474,8 +480,8 @@ dpl_node_t * build_rule(db_val_t * values)
}

LM_DBG("building subst rule\n");
GET_STR_VALUE(subst_exp, values, 5);
if(subst_exp.s && subst_exp.len){
GET_STR_VALUE(subst_exp, values, 5, 1);
if(!VAL_NULL(values+5) && subst_exp.s && subst_exp.len){
/* subst regexp */
subst_comp = wrap_pcre_compile(subst_exp.s, VAL_INT(values+4));
if(subst_comp == NULL){
Expand All @@ -485,8 +491,8 @@ dpl_node_t * build_rule(db_val_t * values)
}

/* replace exp */
GET_STR_VALUE(repl_exp, values, 6);
if(repl_exp.len && repl_exp.s){
GET_STR_VALUE(repl_exp, values, 6, 1);
if(!VAL_NULL(values+6) && repl_exp.len && repl_exp.s){
repl_comp = repl_exp_parse(repl_exp);
if(!repl_comp){
LM_ERR("failed to compile replacing expression %.*s\n",
Expand Down Expand Up @@ -532,16 +538,19 @@ dpl_node_t * build_rule(db_val_t * values)
new_rule->pr = VAL_INT(values+1);
new_rule->match_flags = VAL_INT(values+4);
new_rule->matchop = matchop;
GET_STR_VALUE(attrs, values, 7);
if(str_to_shm(attrs, &new_rule->attrs)!=0)
goto err;

LM_DBG("attrs are %.*s\n",
new_rule->attrs.len, new_rule->attrs.s);
/* attributes */
GET_STR_VALUE(attrs, values, 7, 1);
if( !VAL_NULL(values+7) && attrs.len && attrs.s) {
if(str_to_shm(attrs, &new_rule->attrs)!=0)
goto err;
LM_DBG("attrs are %.*s\n",
new_rule->attrs.len, new_rule->attrs.s);
}

/* Retrieve and Parse Timerec Matching Pattern */
GET_STR_VALUE(timerec, values, 8);
if(timerec.len && timerec.s) {
GET_STR_VALUE(timerec, values, 8, 1);
if( !VAL_NULL(values+8) && timerec.len && timerec.s) {
parsed_timerec = parse_time_def(timerec.s);
if(!parsed_timerec) {
LM_ERR("failed to parse timerec pattern %.*s\n",
Expand Down
2 changes: 1 addition & 1 deletion scripts/dbtext/opensips/dialplan
@@ -1 +1 @@
id(int,auto) dpid(int) pr(int) match_op(int) match_exp(string) match_flags(int) subst_exp(string) repl_exp(string) timerec(string) disabled(int) attrs(string)
id(int,auto) dpid(int) pr(int) match_op(int) match_exp(string) match_flags(int) subst_exp(string,null) repl_exp(string,null) timerec(string,null) disabled(int) attrs(string,null)
8 changes: 4 additions & 4 deletions scripts/mysql/dialplan-create.sql
Expand Up @@ -6,10 +6,10 @@ CREATE TABLE dialplan (
match_op INT(11) NOT NULL,
match_exp CHAR(64) NOT NULL,
match_flags INT(11) NOT NULL,
subst_exp CHAR(64) NOT NULL,
repl_exp CHAR(32) NOT NULL,
timerec CHAR(255) NOT NULL,
subst_exp CHAR(64),
repl_exp CHAR(32),
timerec CHAR(255),
disabled INT(11) DEFAULT 0 NOT NULL,
attrs CHAR(32) NOT NULL
attrs CHAR(32)
) ENGINE=InnoDB;

8 changes: 4 additions & 4 deletions scripts/postgres/dialplan-create.sql
Expand Up @@ -6,11 +6,11 @@ CREATE TABLE dialplan (
match_op INTEGER NOT NULL,
match_exp VARCHAR(64) NOT NULL,
match_flags INTEGER NOT NULL,
subst_exp VARCHAR(64) NOT NULL,
repl_exp VARCHAR(32) NOT NULL,
timerec VARCHAR(255) NOT NULL,
subst_exp VARCHAR(64),
repl_exp VARCHAR(32),
timerec VARCHAR(255),
disabled INTEGER DEFAULT 0 NOT NULL,
attrs VARCHAR(32) NOT NULL
attrs VARCHAR(32)
);

ALTER SEQUENCE dialplan_id_seq MAXVALUE 2147483647 CYCLE;

0 comments on commit 618ff92

Please sign in to comment.