Skip to content

Commit

Permalink
drouting api implementation done
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-datcu committed Sep 4, 2014
1 parent 8fb0996 commit a400113
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
10 changes: 7 additions & 3 deletions modules/drouting/dr_api.h
Expand Up @@ -44,11 +44,15 @@ typedef void* (*match_number_f) (dr_head_p partition, int gr_id,

typedef dr_head_p (*create_head_f) (void);
typedef void (*free_head_f)(dr_head_p partition);
typedef int (*add_rule_f)(dr_head_p partition,
str *prefix, unsigned int gr_id, unsigned int priority,
tmrec_t *time_rec, void *attr);

struct dr_binds {
create_head_f create_head;
free_head_f free_head;
match_number_f match_number;
create_head_f create_head;
free_head_f free_head;
match_number_f match_number;
add_rule_f add_rule;
};

typedef int (*load_dr_api_f)(struct dr_binds *drb);
Expand Down
58 changes: 57 additions & 1 deletion modules/drouting/dr_api_internal.c
Expand Up @@ -35,6 +35,9 @@ static void* match_number (dr_head_t *partition, int grp_id,
const str *number);
static dr_head_p create_dr_head(void);
static void free_dr_head(dr_head_p partition);
static int add_rule_api(dr_head_p partition,
str *prefix, unsigned int gr_id, unsigned int priority,
tmrec_t *time_rec, void *attr);


/* Warning this function assumes the lock is already taken */
Expand Down Expand Up @@ -64,6 +67,7 @@ int load_dr (struct dr_binds *drb)
drb->match_number = match_number;
drb->create_head = create_dr_head;
drb->free_head = free_dr_head;
drb->add_rule = add_rule_api;
return 0;
}

Expand All @@ -87,12 +91,19 @@ static dr_head_p create_dr_head(void)
{
dr_head_p new = shm_malloc(sizeof(dr_head_p));
if( new == NULL ) {
LM_ERR(" no more shm memory(add_head_db)\n");
LM_ERR(" no more shm memory\n");
return NULL;
}
memset( new, 0, sizeof(dr_head_t));

/* data pointer in shm */
new->pt = shm_malloc(sizeof (ptree_t));
if (new->pt == NULL) {
LM_ERR ("no more shm memory");
shm_free(new);
return NULL;
}
memset(new->pt, 0, sizeof(ptree_t));

/* create & init lock */
if ((new->ref_lock = lock_init_rw()) == NULL) {
Expand All @@ -108,7 +119,52 @@ static dr_head_p create_dr_head(void)

static void free_dr_head(dr_head_p partition)
{
int j;
lock_start_read(partition->ref_lock);
del_tree(partition->pt);
if(NULL!=partition->noprefix.rg) {
for(j=0;j<partition->noprefix.rg_pos;j++) {
if(partition->noprefix.rg[j].rtlw !=NULL) {
del_rt_list(partition->noprefix.rg[j].rtlw);
partition->noprefix.rg[j].rtlw = 0;
}
}
shm_free(partition->noprefix.rg);
partition->noprefix.rg = 0;
}
lock_stop_read(partition->ref_lock);
lock_destroy_rw(partition->ref_lock);
shm_free(partition);
}

static int add_rule_api(dr_head_p partition,
str *prefix, unsigned int gr_id, unsigned int priority,
tmrec_t *time_rec, void *attr)
{
static unsigned int autoinc = 0;
rt_info_t * rule = shm_malloc(sizeof(rt_info_t));
if (rule == NULL){
LM_ERR("no more shm mem(1)\n");
return -1;
}

memset(rule, 0, sizeof(rt_info_t));
rule->id = autoinc++;
rule->priority = priority;
rule->time_rec = time_rec;
rule->attrs.s = (char*) attr;

if (prefix->len) {
if ( add_prefix(partition->pt, prefix, rule, gr_id)!=0 ) {
LM_ERR("failed to add prefix route\n");
return -1;
}
} else {
if ( add_rt_info( &partition->noprefix, rule, gr_id)!=0 ) {
LM_ERR("failed to add prefixless route\n");
return -1;
}
}
return 0;
}

0 comments on commit a400113

Please sign in to comment.