Skip to content

Commit

Permalink
Merge pull request #832 from ionutrazvanionita/dr-wildcard
Browse files Browse the repository at this point in the history
Wildcard operator for partitions in some drouting functions
  • Loading branch information
bogdan-iancu committed Mar 18, 2016
2 parents 992b49b + 66399b7 commit d2b5c52
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
6 changes: 4 additions & 2 deletions modules/drouting/README
Expand Up @@ -872,7 +872,8 @@ modparam("drouting", "db_partitions_table", "partition_defs")
name and the groupId may be statical values or AVP
specifications. If no group is specified the function
will try to query the dr_group table for the given
partition to get this information.
partition to get this information. If * (wildcard)
operator is used all partitions shall be checked.
* flags - Controls the behavior of the function. Possible
flags are:
+ W - Instead of using the destination (from the rule
Expand Down Expand Up @@ -1203,7 +1204,8 @@ if (goes_to_gw("my_partition", "1", , "$var(gw_attrs)")) {
marks "," are properly placed.
* partition (mandatory if use_partitions parameter is 1,
otherwise it will be omitted altogether) - Partition
containing the destination/gw to be checked.
containing the destination/gw to be checked. If *(wildcard)
operator is used all partitions shall be checked.
* type (optional) - GW/destination type to be checked; when
omitting this parameter or specifying a negative value i.e.
"-1", matching will be done against all groups
Expand Down
8 changes: 5 additions & 3 deletions modules/drouting/doc/drouting_admin.xml
Expand Up @@ -818,7 +818,7 @@ modparam("drouting", "persistent_state", 0)
<para>
If enabled, the module will not allow do run multiple dr_reload
MI commands in parallel (with overlapping) Any new reload will
be rejected (and discarded) while an existing reload is in
be rejected (and discarded) while an existing reload is in
progress.
</para>
<para>
Expand Down Expand Up @@ -1117,6 +1117,7 @@ modparam("drouting", "db_partitions_table", "partition_defs")
parameter is 1 - both the partition name and the groupId may be statical
values or AVP specifications. If no group is specified the function will
try to query the dr_group table for the given partition to get this information.
If <emphasis>* (wildcard)</emphasis> operator is used all partitions shall be checked.
</para>
</listitem>
</itemizedlist>
Expand Down Expand Up @@ -1648,7 +1649,8 @@ if (goes_to_gw("my_partition", "1", , "$var(gw_attrs)")) {
<para>
<emphasis role='bold'>partition</emphasis> (mandatory if <varname>use_partitions
</varname> parameter is 1, otherwise it will be omitted altogether) - Partition
containing the destination/gw to be checked.
containing the destination/gw to be checked. If <emphasis>*(wildcard)</emphasis>
operator is used all partitions shall be checked.
</para>
</listitem>
<listitem>
Expand Down Expand Up @@ -2094,7 +2096,7 @@ Partition:: part_test Date=Tue Aug 12 12:24:13 2014
<section>
<title><varname>dr_number_routing</varname></title>
<para>
Gets the matched prefix along with the list of the gateways / carriers to which a number
Gets the matched prefix along with the list of the gateways / carriers to which a number
would be routed when using the do_routing function
</para>
<itemizedlist>
Expand Down
74 changes: 68 additions & 6 deletions modules/drouting/drouting.c
Expand Up @@ -186,7 +186,7 @@ typedef struct dr_partition {
gparam_p part_name;
} v;

enum dr_partition_type { DR_PTR_PART, DR_GPARAM_PART, DR_NO_PART } type;
enum dr_partition_type { DR_PTR_PART, DR_GPARAM_PART, DR_WILDCARD_PART, DR_NO_PART } type;
} dr_partition_t;

typedef struct dr_part_group {
Expand Down Expand Up @@ -2549,10 +2549,12 @@ static int do_routing(struct sip_msg* msg, dr_part_group_t * part_group,
str ruri;
str next_carrier_attrs = {NULL, 0};
str next_gw_attrs = {NULL, 0};
int ret;
int ret, fret;
char tmp;
char *ruri_buf;

gparam_p tmp_gparam = NULL;

ret = -1;
ruri_buf = NULL;
wl_list = NULL;
Expand All @@ -2564,13 +2566,42 @@ static int do_routing(struct sip_msg* msg, dr_part_group_t * part_group,
LM_ERR("Partition name is mandatory for do_routing\n");
return -1;
}

if(part_group->dr_part->type == DR_GPARAM_PART) {
if (to_partition(msg, part_group->dr_part, &current_partition)<0) {
if ((fret=to_partition(msg, part_group->dr_part, &current_partition))<0) {
return -1;
} else if (fret == 1) {
tmp_gparam = part_group->dr_part->v.part_name;
part_group->dr_part->type = DR_WILDCARD_PART;
}

} else if(part_group->dr_part->type == DR_PTR_PART) {
current_partition = part_group->dr_part->v.part;
}


if (part_group->dr_part->type == DR_WILDCARD_PART) {
for (current_partition = head_db_start;
current_partition; current_partition = current_partition->next) {
part_group->dr_part->v.part = current_partition;
part_group->dr_part->type = DR_PTR_PART;

ret=do_routing( msg, part_group, flags, whitelist);
if (ret > 0)
break;
}

/* restore to initial state */
if (tmp_gparam) {
part_group->dr_part->type = DR_GPARAM_PART;
} else {
memset(part_group->dr_part, 0, sizeof(dr_partition_t));
part_group->dr_part->type = DR_WILDCARD_PART;
}

/* ret must be less than 0 here if nothing found */
return ret;
}
} else {
if(part_group->dr_part->type == DR_PTR_PART) {
current_partition = part_group->dr_part->v.part;
Expand Down Expand Up @@ -3390,6 +3421,12 @@ int fxup_get_partition(void ** part_name, dr_partition_t ** x) {
if( ((gparam_p)(*part_name))->type==GPARAM_TYPE_STR ) { /* was
defined statically */
str_part_name = (( (gparam_p) (*part_name))->v.sval);
str_trim_spaces_lr(str_part_name);
if (str_part_name.len == 1 && str_part_name.s[0] == '*') {
(*x)->type = DR_WILDCARD_PART;
return 0;
}

if((part = get_partition(&str_part_name)) == NULL) {
LM_CRIT("Partition <%.*s> was not found.\n", str_part_name.len,
str_part_name.s);
Expand All @@ -3413,6 +3450,14 @@ static int to_partition(struct sip_msg* msg, dr_partition_t *part,
LM_ERR("Failed to parse avp/pve.\n");
return -1;
}

str_trim_spaces_lr(part_name);

/* check for wildcard operator */
if ( part_name.len == 1 && part_name.s[0] == '*') {
return 1;
}

if((*current_partition = get_partition(&part_name)) == NULL) {
LM_ERR("Partition <%.*s> was not found.\n", part_name.len, part_name.s);
return -1;
Expand Down Expand Up @@ -3923,20 +3968,37 @@ static int gw_matches_ip(pgw_t *pgwa, struct ip_addr *ip, unsigned short port)
static int _is_dr_gw(struct sip_msg* msg, char * part,
char * flags_pv, int type, struct ip_addr *ip,
unsigned int port) {

int ret=-1;

struct head_db * it;
if(use_partitions) {
if(part == NULL || ((dr_partition_t*)part)->type == DR_NO_PART) {
LM_ERR("Partition is mandatory!\n");
return -1;
} else if(((dr_partition_t*)part)->type == DR_PTR_PART) {
}

if(((dr_partition_t*)part)->type == DR_PTR_PART) {
return _is_dr_gw_w_part(msg, (char*)((dr_partition_t*)part)->v.part,
flags_pv, type, ip, port);
} else if(((dr_partition_t*)part)->type == DR_GPARAM_PART) {
if(to_partition(msg, (dr_partition_t*)part, &it) < 0) {
if((ret=to_partition(msg, (dr_partition_t*)part, &it) < 0)) {
return -1;
} else if (ret == 0) {
return _is_dr_gw_w_part(msg, (char*)it,flags_pv, type, ip, port);
}
return _is_dr_gw_w_part(msg, (char*)it,flags_pv, type, ip, port);
}

/* if we got here we have the wildcard operator */
for (it = head_db_start; it; it = it->next) {
ret = _is_dr_gw_w_part(msg, (char *)it, flags_pv, type, ip, port);
if (ret > 0)
return ret;
}

return ret;


} else {
if( head_db_start == NULL ) {
LM_ERR("Error loading config.");
Expand Down

0 comments on commit d2b5c52

Please sign in to comment.