Skip to content

Commit

Permalink
[qrouting] dr callbacks for creating rules, and registering gw to dr …
Browse files Browse the repository at this point in the history
…and qr_status mi cmd
  • Loading branch information
tallicamike authored and razvancrainea committed Feb 11, 2020
1 parent 72ec8f6 commit d69747c
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 98 deletions.
3 changes: 3 additions & 0 deletions modules/drouting/dr_api.h
Expand Up @@ -32,6 +32,7 @@
#include "../../lock_ops.h"
#include "../../rw_locking.h"
#include "../../sr_module.h"
#include "dr_cb.h"

typedef struct _dr_head_t {
ptree_t *pt;
Expand All @@ -46,13 +47,15 @@ typedef void (*free_head_f)(dr_head_p partition);
typedef int (*add_rule_f)(dr_head_p partition, unsigned int rid,
str *prefix, unsigned int gr_id, unsigned int priority,
tmrec_t *time_rec, void *attr);
typedef str * (*get_gw_name_f) (pgw_t *gw);

struct dr_binds {
create_head_f create_head;
free_head_f free_head;
match_number_f match_number;
add_rule_f add_rule;
register_drcb_f register_drcb;
get_gw_name_f get_gw_name;
};

typedef int (*load_dr_api_f)(struct dr_binds *drb);
Expand Down
9 changes: 9 additions & 0 deletions modules/drouting/dr_api_internal.c
Expand Up @@ -26,6 +26,7 @@

#include "dr_api_internal.h"
#include "dr_api.h"
#include "dr_cb.h"


#include "../../str.h"
Expand All @@ -39,6 +40,13 @@ static int add_rule_api(dr_head_p partition, unsigned int rid,
tmrec_t *time_rec, void *attr);


static str * get_gw_name(pgw_t * gw);


static str * get_gw_name(pgw_t *gw) {
return &gw->id;
}

/* Warning this function assumes the lock is already taken */
rt_info_t* find_rule_by_prefix_unsafe(ptree_t *pt, ptree_node_t *noprefix,
str prefix, unsigned int grp_id, unsigned int *matched_len)
Expand Down Expand Up @@ -68,6 +76,7 @@ int load_dr (struct dr_binds *drb)
drb->free_head = free_dr_head;
drb->add_rule = add_rule_api;
drb->register_drcb = register_dr_cb;
drb->get_gw_name = get_gw_name;
return 0;
}

Expand Down
79 changes: 72 additions & 7 deletions modules/drouting/routing.c
Expand Up @@ -43,6 +43,7 @@
#include "../../time_rec.h"
#include "prefix_tree.h"
#include "parse.h"
#include "dr_cb.h"

#define is_valid_gw_char(_c) \
(isalpha(_c) || isdigit(_c) || (_c)=='_' || (_c)=='-' || (_c)=='.')
Expand Down Expand Up @@ -89,12 +90,14 @@ int parse_destination_list(rt_data_t* rd, char *dstlist, pgw_list_t** pgwl_ret,
unsigned short *len, int no_resize, osips_malloc_f mf)
{
#define PGWL_SIZE 32
pgw_list_t *pgwl=NULL, *p=NULL;
pgw_list_t *pgwl=NULL, *p = NULL;
unsigned int size, pgwl_size;
long int t;
char *tmp, *ep;
int i;
str id;


/* temporary list of gw while parsing */
pgwl_size = PGWL_SIZE;
pgwl = (pgw_list_t*)pkg_malloc(pgwl_size*sizeof(pgw_list_t));
Expand Down Expand Up @@ -185,6 +188,10 @@ int parse_destination_list(rt_data_t* rd, char *dstlist, pgw_list_t** pgwl_ret,
size++;
}





/* separator */
if ( (*tmp==SEP) || (*tmp==SEP1) ) {
tmp++;
Expand Down Expand Up @@ -216,6 +223,7 @@ int parse_destination_list(rt_data_t* rd, char *dstlist, pgw_list_t** pgwl_ret,
goto error;
}
memcpy( p, pgwl, size*sizeof(pgw_list_t));

pkg_free(pgwl);
*len = size;
*pgwl_ret = p;
Expand Down Expand Up @@ -320,6 +328,15 @@ build_rt_info(
{
rt_info_t* rt = NULL;

/* callback parameters for the QR module */
int i;
void * qr_rule;
struct dr_callback *dr_cb_it;
struct dr_cb_params *cb_params;
struct dr_reg_gw_params *reg_gw_params;
struct dr_reg_init_rule_params *init_rule_params;
pgw_list_t *p = NULL;

rt = (rt_info_t*)func_malloc(mf, sizeof(rt_info_t) +
(attrs?strlen(attrs):0) + (route_idx?strlen(route_idx)+1:0) );
if (rt==NULL) {
Expand Down Expand Up @@ -348,6 +365,54 @@ build_rt_info(
}
}

/* call the create rule callbacks */
init_rule_params = (struct dr_reg_init_rule_params *) shm_malloc(
sizeof(struct dr_reg_init_rule_params));
cb_params = (struct dr_cb_params *) shm_malloc(sizeof(struct dr_cb_params));
if(init_rule_params != NULL && cb_params != NULL) {
memset(init_rule_params, 0, sizeof(struct dr_reg_init_rule_params));
init_rule_params->n_dst = rt->pgwa_len;
init_rule_params->r_id = id; /* name of the rule */
cb_params->param = (void*)&init_rule_params;
if(dr_reg_cbs != NULL && (dr_reg_cbs->types & DRCB_REG_INIT_RULE)) {
dr_cb_it = dr_reg_cbs->first;
while(dr_cb_it) {
if(dr_cb_it->types & DRCB_REG_INIT_RULE) {
dr_cb_it->callback(dr_cb_it->types, cb_params);
break; /* execute just the first callback */
}
dr_cb_it = dr_cb_it->next;
}
} else {
LM_ERR("No callback list to match the given type\n");
}
qr_rule = (void*)((struct dr_reg_init_rule_params*)*cb_params->param)->rule;

p = rt->pgwl;


/* TODO: should check if qr loaded */
for(i = 0; i < rt->pgwa_len; i++) {
if(p[i].is_carrier) {

} else {
reg_gw_params = (struct dr_reg_gw_params *) shm_malloc(sizeof(struct
dr_reg_gw_params));
if(reg_gw_params == NULL) {
LM_ERR("no more shm memory\n");
/* TODO: should we crash all together? */
} else {
reg_gw_params->rule = qr_rule;
reg_gw_params->n_dst = i;
reg_gw_params->gw = p[i].dst.gw;

run_callbacks(dr_reg_cbs, DRCB_REG_GW, (void*)reg_gw_params);
}
}
}
}
run_callbacks(dr_reg_cbs, DRCB_REG_ADD_RULE, (void*)qr_rule);

return rt;

err_exit:
Expand Down Expand Up @@ -513,7 +578,7 @@ add_dst(
if( sip_prefix==0 ) {
if(l_ip+4>=GWABUF_MAX_SIZE) {
LM_ERR("GW address (%d) longer "
"than %d\n",l_ip+4,GWABUF_MAX_SIZE);
"than %d\n",l_ip+4,GWABUF_MAX_SIZE);
goto err_exit;
}
memcpy(gwabuf, "sip:", 4);
Expand All @@ -528,7 +593,7 @@ add_dst(
memset(&uri, 0, sizeof(struct sip_uri));
if(parse_uri(gwas.s, gwas.len, &uri)!=0) {
LM_ERR("invalid uri <%.*s>\n",
gwas.len, gwas.s);
gwas.len, gwas.s);
goto err_exit;
}
/* update the sip_prefix to skip to domain part */
Expand All @@ -540,7 +605,7 @@ add_dst(
l_pri + l_attrs);
if (NULL==pgw) {
LM_ERR("no more shm mem (%u)\n",
(unsigned int)(sizeof(pgw_t)+l_id+l_ip-sip_prefix+l_pri +l_attrs));
(unsigned int)(sizeof(pgw_t)+l_id+l_ip-sip_prefix+l_pri +l_attrs));
goto err_exit;
}
memset(pgw,0,sizeof(pgw_t));
Expand Down Expand Up @@ -604,7 +669,7 @@ add_dst(
if (proxy==NULL) {
if(dr_force_dns) {
LM_ERR("cannot resolve <%.*s>\n",
uri.host.len, uri.host.s);
uri.host.len, uri.host.s);
goto err_exit;
} else {
LM_DBG("cannot resolve <%.*s> - won't be used"
Expand All @@ -624,7 +689,7 @@ add_dst(
pgw->ports[pgw->ips_no] = proxy->port;
pgw->protos[pgw->ips_no] = proxy->proto;
LM_DBG("additional gw ip addr [%s]\n",
ip_addr2a( &pgw->ips[pgw->ips_no] ) );
ip_addr2a( &pgw->ips[pgw->ips_no] ) );
pgw->ips_no++;
}

Expand Down Expand Up @@ -692,7 +757,7 @@ void del_carriers_list(
destroy_pcr_rpm_w:destroy_pcr_shm_w));
}

void
void
free_rt_data(
rt_data_t* rt_data,
osips_free_f free_f
Expand Down
36 changes: 30 additions & 6 deletions modules/qrouting/qr_acc.c
@@ -1,6 +1,33 @@
/**
*
* qrouting module:qr_acc.c
*
* Copyright (C) 2004-2005 FhG Fokus
* Copyright (C) 2006-2010 Voice Sistem SRL
* Copyright (C) 2014 OpenSIPS Foundation
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History
* -------
* 2014-08-28 initial version (Mihai Tiganus)
*/
#include "qr_acc.h"

extern qr_rule_t * qr_rules_start;
int myn = 0;

/* free the parameter of the dialog callback */
Expand Down Expand Up @@ -53,10 +80,9 @@ int test_acc(struct sip_msg* msg) {

if(msg->first_line.type != SIP_REQUEST ||
msg->first_line.u.request.method_value != METHOD_INVITE) {
LM_INFO("it is not an invite!\n");
/*TODO: check if works only on invite (as it should) */
return -1;/* it is not an invite */
}
LM_INFO("it is an invite!\n");

trans_prop = (qr_trans_prop_t*)shm_malloc(sizeof(qr_trans_prop_t));
if(trans_prop == NULL) {
Expand All @@ -72,9 +98,7 @@ int test_acc(struct sip_msg* msg) {
}

/* save transaction properties */
trans_prop->gw = qr_rules_start->dest[0].dst.gw;
LM_INFO("Nume qr_rules_start:%*.s\n", qr_rules_start->name.len,
qr_rules_start->name.s);
trans_prop->gw = (*qr_rules_start)->dest[0].dst.gw;

/* get the time of INVITE */
if(clock_gettime(CLOCK_REALTIME, trans_prop->invite) < 0) {
Expand Down
31 changes: 31 additions & 0 deletions modules/qrouting/qr_acc.h
@@ -1,3 +1,31 @@
/**
*
* qrouting module:qr_acc.h
*
* Copyright (C) 2004-2005 FhG Fokus
* Copyright (C) 2006-2010 Voice Sistem SRL
* Copyright (C) 2014 OpenSIPS Foundation
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History
* -------
* 2014-08-28 initial version (Mihai Tiganus)
*/
#ifndef __QR_ACC_H__
#define __QR_ACC_H__

Expand All @@ -6,14 +34,17 @@
#include "../tm/tm_load.h"
#include "../dialog/dlg_load.h"
#include "qr_stats.h"
#include "../drouting/dr_api.h"


#define QR_TM_100RCVD (1<<0)

struct tm_binds tmb;
struct dlg_binds dlgcb;
struct dr_binds drb;
struct dlg * dlg_cell;


typedef struct qr_trans_prop {
qr_gw_t *gw;
gen_lock_t *prop_lock;
Expand Down

0 comments on commit d69747c

Please sign in to comment.