modules/carrierroute/cr_rule.c
60b5fe93
 /*
  * $Id$
  *
e27b0961
  * Copyright (C) 2007-2008 1&1 Internet AG
60b5fe93
  *
e89cea15
  * This file is part of SIP-router, a free SIP server.
60b5fe93
  *
e89cea15
  * SIP-router is free software; you can redistribute it and/or modify
60b5fe93
  * 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
  *
e89cea15
  * SIP-router is distributed in the hope that it will be useful,
60b5fe93
  * 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 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
60b5fe93
  */
 
 /**
5e92049e
  * \file cr_rule.c
875334e1
  * \brief Contains the functions to manage routing rule data.
  * \ingroup carrierroute
  * - Module; \ref carrierroute
60b5fe93
  */
 
cd42e6ed
 #include "../../ut.h"
5e92049e
 #include "cr_rule.h"
60b5fe93
 
614f67f5
 
60b5fe93
 /**
883aff8d
  * Adds a route rule to rf. prefix, rewrite_hostpart, rewrite_local_prefix,
be68f406
  * rewrite_local_suffix, and comment must not contain NULL pointers.
60b5fe93
  *
883aff8d
  * @param rf the current route_flags struct
be68f406
  * @param prefix the whole scan prefix
896b7ab8
  * @param max_targets the number of targets
60b5fe93
  * @param prob the weight of the rule
  * @param rewrite_hostpart the rewrite_host of the rule
597bb8dc
  * @param strip the strip value of the rule
60b5fe93
  * @param rewrite_local_prefix the rewrite prefix
  * @param rewrite_local_suffix the rewrite suffix
  * @param status the status of the rule
  * @param hash_index the hash index of the rule
614f67f5
  * @param backup indicates if the route is backed up by another. only
                  useful if status==0, if set, it is the hash value
                  of another rule
  * @param backed_up an NULL-termintated array of hash indices of the route
                     for which this route is backup
60b5fe93
  * @param comment a comment for the route rule
  *
  * @return 0 on success, -1 on failure
  *
  * @see add_route_to_tree()
  */
883aff8d
 int add_route_rule(struct route_flags *rf, const str * prefix,
be68f406
 		int max_targets, double prob, const str * rewrite_hostpart, int strip,
 		const str * rewrite_local_prefix, const str * rewrite_local_suffix,
e27b0961
 		int status, int hash_index, int backup, int * backed_up, 
be68f406
 		const str * comment) {
18d476d8
 	struct route_rule * shm_rr, * prev = NULL, * tmp = NULL;
614f67f5
 	struct route_rule_p_list * t_rl;
 	int * t_bu;
60b5fe93
 
896b7ab8
 	if (max_targets) {
883aff8d
 		rf->max_targets = max_targets;
60b5fe93
 	} else {
883aff8d
 		rf->max_targets++;
60b5fe93
 	}
 
 	if ((shm_rr = shm_malloc(sizeof(struct route_rule))) == NULL) {
a083345f
 		SHM_MEM_ERROR;
60b5fe93
 		return -1;
 	}
 	memset(shm_rr, 0, sizeof(struct route_rule));
 
be68f406
 	if (shm_str_dup(&shm_rr->host, rewrite_hostpart) != 0) {
 		goto mem_error;
60b5fe93
 	}
 
be68f406
 	if (shm_str_dup(&shm_rr->prefix, prefix) != 0) {
 		goto mem_error;
  	}
60b5fe93
 
 	shm_rr->strip = strip;
 
be68f406
 	if (shm_str_dup(&shm_rr->local_prefix, rewrite_local_prefix) != 0) {
 		goto mem_error;
60b5fe93
 	}
 
be68f406
 	if (shm_str_dup(&shm_rr->local_suffix, rewrite_local_suffix) != 0) {
 		goto mem_error;
60b5fe93
 	}
 
6a738a91
 	if (comment && shm_str_dup(&shm_rr->comment, comment) != 0) {
be68f406
 		goto mem_error;
60b5fe93
 	}
 
 	shm_rr->status = status;
 	shm_rr->hash_index = hash_index;
614f67f5
 	shm_rr->orig_prob = prob;
 	if (shm_rr->status || backup != -1) {
 		shm_rr->prob = prob;
 	}	else {
 	    shm_rr->prob = 0;
 	}
 	if (backup >= 0) {
 		if ((shm_rr->backup = shm_malloc(sizeof(struct route_rule_p_list))) == NULL) {
7de68068
 			goto mem_error;
614f67f5
 		}
fca5b253
 		memset(shm_rr->backup, 0, sizeof(struct route_rule_p_list));
614f67f5
 		shm_rr->backup->hash_index = backup;
60b5fe93
 	}
614f67f5
 	shm_rr->backed_up = NULL;
 	t_bu = backed_up;
 	if(!backed_up){
4e80bd72
 		LM_INFO("no backed up rules\n");
614f67f5
 	}
 	while (t_bu && *t_bu != -1) {
 		if ((t_rl = shm_malloc(sizeof(struct route_rule_p_list))) == NULL) {
7de68068
 			goto mem_error;
614f67f5
 		}
 		memset(t_rl, 0, sizeof(struct route_rule_p_list));
 		t_rl->hash_index = *t_bu;
 		t_rl->next = shm_rr->backed_up;
 		shm_rr->backed_up = t_rl;
 		t_bu++;
 	}
 
18d476d8
 	/* rules with a probability of zero are always at the beginning of the list */
883aff8d
 	tmp = rf->rule_list;
18d476d8
 	while(tmp && tmp->prob == 0){
 		prev = tmp;
 		tmp = tmp->next;
 	}
ce40d1b8
 	/* rules with prob > 0 are sorted by hash_index */
 	while(tmp && (tmp->hash_index < shm_rr->hash_index)){
 		prev = tmp;
 		tmp = tmp->next;
 	}
18d476d8
 	if(prev){
 		shm_rr->next = prev->next;
 		prev->next = shm_rr;
 	} else {
883aff8d
 		shm_rr->next = rf->rule_list;
 		rf->rule_list = shm_rr;
18d476d8
 	}
60b5fe93
 
 	return 0;
7de68068
 
 mem_error:
a083345f
 	SHM_MEM_ERROR;
7de68068
 	destroy_route_rule(shm_rr);
 	return -1;
60b5fe93
 }
 
 
5e92049e
 /**
  * Destroys route rule rr by freeing all its memory.
  *
  * @param rr route rule to be destroyed
  */
 void destroy_route_rule(struct route_rule * rr) {
 	struct route_rule_p_list * t_rl;
 	if (rr->host.s) {
 		shm_free(rr->host.s);
 	}
 	if (rr->local_prefix.s) {
 		shm_free(rr->local_prefix.s);
 	}
 	if (rr->local_suffix.s) {
 		shm_free(rr->local_suffix.s);
 	}
 	if (rr->comment.s) {
 		shm_free(rr->comment.s);
 	}
 	if (rr->prefix.s) {
 		shm_free(rr->prefix.s);
 	}
 	if(rr->backup){
 		shm_free(rr->backup);
 	}
 	while(rr->backed_up){
 		t_rl = rr->backed_up->next;
 		shm_free(rr->backed_up);
 		rr->backed_up = t_rl;
 	}
 	shm_free(rr);
 	return;
 }
 
 
 /**
  * Try to find a matching route_flags struct in rt and return it, add it if not found.
  *
  * @param rf_head pointer to the head of the route flags list, might be changed during insert.
  * @param flags user defined flags
  * @param mask mask for user defined flags
  *
  * @return pointer to the route_flags struct on success, NULL on failure.
  *
  */
 struct route_flags * add_route_flags(struct route_flags **rf_head, const flag_t flags, const flag_t mask)
 {
 	struct route_flags *shm_rf;
 	struct route_flags *prev_rf, *tmp_rf;
 	prev_rf = tmp_rf = NULL;
 
 	if (rf_head) {
 		/* search for matching route_flags struct */
 		for (tmp_rf=*rf_head; tmp_rf!=NULL; tmp_rf=tmp_rf->next) {
 			if ((tmp_rf->flags == flags) && (tmp_rf->mask == mask)) return tmp_rf;
 		}
 		
 		/* not found, insert one */
 		for (tmp_rf=*rf_head; tmp_rf!=NULL; tmp_rf=tmp_rf->next) {
 			if (tmp_rf->mask < mask) break;
 			prev_rf=tmp_rf;
 		}
 	}
 
 	if ((shm_rf = shm_malloc(sizeof(struct route_flags))) == NULL) {
a083345f
 		SHM_MEM_ERROR;
5e92049e
 		return NULL;
 	}
 	memset(shm_rf, 0, sizeof(struct route_flags));
 
 	shm_rf->flags=flags;
 	shm_rf->mask=mask;
 	shm_rf->next=tmp_rf;
 	
 	if (prev_rf) {
 		prev_rf->next = shm_rf;
 	}
 	else {
 		if (rf_head) *rf_head=shm_rf;
 	}
 
 	return shm_rf;
 }
 
 
 /**
  * Destroys route_flags in shared memory by freing all its memory.
  *
  * @param rf route_flags struct to be destroyed
  */
 void destroy_route_flags(struct route_flags *rf) {
 	struct route_rule *rs, *rs_tmp;
 
 	if (rf->rules) {
 		shm_free(rf->rules);
 	}
 	rs = rf->rule_list;
 	while (rs != NULL) {
 		rs_tmp = rs->next;
 		destroy_route_rule(rs);
 		rs = rs_tmp;
 	}
 	shm_free(rf);
 }
 
 
e27b0961
 /**
7de68068
  * Compares the priority of two failure route rules.
e27b0961
  *
5e92049e
  * @param frr1 first failure rule
  * @param frr2 second failure rule
e27b0961
  *
5e92049e
  * @return 0 if frr1 and frr2 have the same priority, -1 if frr1 has higher priority than frr2, 1 if frr1 has lower priority than frr2.
e27b0961
  *
  * @see add_failure_route_to_tree()
  */
5e92049e
 static int failure_rule_prio_cmp(struct failure_route_rule *frr1, struct failure_route_rule *frr2) {
e27b0961
 	int n1, n2, i;
 	
 	/* host has highest priority */
5e92049e
 	if ((frr1->host.len == 0) && (frr2->host.len > 0)) {
 		/* host1 is wildcard -> frr1 has lower priority */
e27b0961
 		return 1;
 	}
5e92049e
 	else if ((frr1->host.len > 0) && (frr2->host.len == 0)) {
 		/* host2 is wildcard -> frr1 has higher priority */
e27b0961
 		return -1;
 	}
 	else {
 		/* reply_code has second highest priority */
 		n1=0;
 		n2=0;
5e92049e
 		for (i=0; i < frr1->reply_code.len; i++) {
 			if (frr1->reply_code.s[i]=='.') n1++;
e27b0961
 		}
5e92049e
 		for (i=0; i < frr2->reply_code.len; i++) {
 			if (frr2->reply_code.s[i]=='.') n2++;
e27b0961
 		}
7de68068
 		if (n1 < n2) {
5e92049e
 			/* reply_code1 has fewer wildcards -> frr1 has higher priority */
e27b0961
 			return -1;
 		}
7de68068
 		else if (n1 > n2) {
5e92049e
 			/* reply_code1 has more wildcards -> frr1 has lower priority */
e27b0961
 			return 1;
 		}
 		else {
 			/* flags have lowest priority */
5e92049e
 			if (frr1->mask > frr2->mask) {
e27b0961
 				return -1;
 			}
5e92049e
 			else if (frr1->mask < frr2->mask) {
e27b0961
 				return 1;
 			}
 		}
 	}
 	
 	return 0;
 }
 
 
 /**
5e92049e
  * Adds a failure route rule to rule list. prefix, host, reply_code, and comment
be68f406
  * must not contain NULL pointers.
e27b0961
  *
5e92049e
  * @param frr_head pointer to the head of the failure route rule list, might be changed during insert
597bb8dc
  * @param prefix the whole scan prefix
e27b0961
  * @param host the hostname last tried
  * @param reply_code the reply code 
  * @param flags user defined flags
  * @param mask mask for user defined flags
  * @param next_domain continue routing with this domain
  * @param comment a comment for the route rule
  *
5e92049e
  * @return pointer to the failure_route_rul struct on success, NULL on failure.
e27b0961
  *
  * @see add_failure_route_to_tree()
  */
5e92049e
 struct failure_route_rule *add_failure_route_rule(struct failure_route_rule **frr_head,
 		const str * prefix, const str * host, const str * reply_code,
 		flag_t flags, flag_t mask, const int next_domain, const str * comment) {
 	struct failure_route_rule *shm_frr, *frr, *prev;
 	frr = prev = NULL;
e27b0961
 	
5e92049e
 	if ((shm_frr = shm_malloc(sizeof(struct failure_route_rule))) == NULL) {
a083345f
 		SHM_MEM_ERROR;
5e92049e
 		return NULL;
e27b0961
 	}
5e92049e
 	memset(shm_frr, 0, sizeof(struct failure_route_rule));
e27b0961
 	
5e92049e
 	if (shm_str_dup(&shm_frr->host, host) != 0) {
be68f406
 		goto mem_error;
e27b0961
 	}
 	
5e92049e
 	if (shm_str_dup(&shm_frr->reply_code, reply_code) != 0) {
be68f406
 		goto mem_error;
e27b0961
 	}
 	
5e92049e
 	shm_frr->flags = flags;
 	shm_frr->mask = mask;
 	shm_frr->next_domain = next_domain;
e27b0961
 	
5e92049e
 	if (shm_str_dup(&shm_frr->comment, comment) != 0) {
be68f406
 		goto mem_error;
e27b0961
 	}
 	
 	/* before inserting into list, check priorities! */
5e92049e
 	if (frr_head) {
 		frr=*frr_head;
 		prev=NULL;
 		while ((frr != NULL) && (failure_rule_prio_cmp(shm_frr, frr) > 0)) {
 			prev=frr;
 			frr=frr->next;
 		}
e27b0961
 	}
5e92049e
 
 	shm_frr->next = frr;
 
e27b0961
 	if(prev){
5e92049e
 		prev->next = shm_frr;
 	}
 	else {
 		if (frr_head) *frr_head=shm_frr;
e27b0961
 	}
 
5e92049e
 	return shm_frr;
 	
e27b0961
 mem_error:
8efd99bc
 	SHM_MEM_ERROR;
5e92049e
 	destroy_failure_route_rule(shm_frr);
 	return NULL;
60b5fe93
 }
 
 
 /**
5e92049e
  * Destroys failure route rule frr by freeing all its memory.
60b5fe93
  *
a103c1a1
  * @param frr route rule to be destroyed
60b5fe93
  */
5e92049e
 void destroy_failure_route_rule(struct failure_route_rule * frr) {
 	if (frr->host.s) {
 		shm_free(frr->host.s);
60b5fe93
 	}
5e92049e
 	if (frr->comment.s) {
 		shm_free(frr->comment.s);
60b5fe93
 	}
5e92049e
 	if (frr->prefix.s) {
 		shm_free(frr->prefix.s);
614f67f5
 	}
5e92049e
 	if (frr->reply_code.s) {
 		shm_free(frr->reply_code.s);
614f67f5
 	}
5e92049e
 	shm_free(frr);
 	return;
614f67f5
 }
 
 
883aff8d
 struct route_rule * find_rule_by_hash(struct route_flags * rf, int hash){
614f67f5
 	struct route_rule * rr;
883aff8d
 	rr = rf->rule_list;
614f67f5
 	while(rr){
 		if(rr->hash_index == hash){
 			return rr;
 		}
 		rr = rr->next;
 	}
 	return NULL;
 }
 
 
883aff8d
 struct route_rule * find_rule_by_host(struct route_flags * rf, str * host){
614f67f5
 	struct route_rule * rr;
883aff8d
 	rr = rf->rule_list;
614f67f5
 	while(rr){
cd42e6ed
 		if(str_strcmp(&(rr->host), host) == 0){
614f67f5
 			return rr;
 		}
 		rr = rr->next;
 	}
 	return NULL;
 }
 
5e92049e
 
 int add_backup_rule(struct route_rule * rule, struct route_rule * backup){
614f67f5
 	struct route_rule_p_list * tmp = NULL;
 	if(!backup->status){
 		LM_ERR("desired backup route is inactive\n");
 		return -1;
 	}
 	if((tmp = shm_malloc(sizeof(struct route_rule_p_list))) == NULL) {
a083345f
 		SHM_MEM_ERROR;
614f67f5
 		return -1;
 	}
fca5b253
 	memset(tmp, 0, sizeof(struct route_rule_p_list));
614f67f5
 	tmp->hash_index = rule->hash_index;
 	tmp->rr = rule;
 	tmp->next = backup->backed_up;
 	backup->backed_up =  tmp;
 
 	tmp = NULL;
 	if((tmp = shm_malloc(sizeof(struct route_rule_p_list))) == NULL) {
a083345f
 		SHM_MEM_ERROR;
614f67f5
 		return -1;
 	}
fca5b253
 	memset(tmp, 0, sizeof(struct route_rule_p_list));
614f67f5
 	tmp->hash_index = backup->hash_index;
 	tmp->rr = backup;
 	rule->backup = tmp;
 
 	if(rule->backed_up){
 		tmp = rule->backed_up;
7de68068
 		while(tmp->next) {
614f67f5
 			tmp = tmp->next;
 		}
 		tmp->next = backup->backed_up;
 		backup->backed_up = rule->backed_up;
 		rule->backed_up = NULL;
 	}
 	tmp = rule->backup->rr->backed_up;
7de68068
 	while(tmp) {
614f67f5
 		tmp->rr->backup->hash_index = rule->backup->hash_index;
 		tmp->rr->backup->rr = rule->backup->rr;
 		tmp = tmp->next;
 	}
 	return 0;
 }
 
 
 int remove_backed_up(struct route_rule * rule){
 	struct route_rule_p_list * rl, * prev = NULL;
7de68068
 	if(rule->backup) {
 		if(rule->backup->rr) {
614f67f5
 			rl = rule->backup->rr->backed_up;
7de68068
 			while(rl) {
 				if(rl->hash_index == rule->hash_index) {
 					if(prev) {
614f67f5
 						prev->next = rl->next;
 					} else {
 						rule->backup->rr->backed_up = rl->next;
 					}
 					shm_free(rl);
 					shm_free(rule->backup);
 					rule->backup = NULL;
 					return 0;
 				}
 				prev = rl;
 				rl = rl->next;
 			}
 		}
 		return -1;
 	}
 	return 0;
 }
 
 
883aff8d
 struct route_rule * find_auto_backup(struct route_flags * rf, struct route_rule * rule){
614f67f5
 	struct route_rule * rr;
883aff8d
 	rr = rf->rule_list;
614f67f5
 	while(rr){
 		if(!rr->backed_up && (rr->hash_index != rule->hash_index) && rr->status){
 			return rr;
 		}
 		rr = rr->next;
 	}
 	return NULL;
 }