Esempio n. 1
0
/**
 * returns the routing tree for the given domain, if domain's tree
 * doesnt exist, it will be created. If the trees are completely
 * filled and a not existing domain shall be added, an error is
 * returned
 *
 * @param domain the domain name of desired routing tree
 * @param rd route data to be searched
 *
 * @return a pointer to the root node of the desired routing tree,
 * NULL on failure
 */
struct route_tree_item * get_route_tree(const char * domain, struct carrier_tree * rd) {
	int i, id;
	struct route_tree * rt = NULL;
	if (!rd) {
		LM_ERR("NULL-pointer in parameter\n");
		return NULL;
	}
	for (i=0; i<rd->tree_num; i++) {
		if (rd->trees[i] && rd->trees[i]->name.s) {
			if (strcmp(rd->trees[i]->name.s, domain) == 0) {
				LM_INFO("found domain %.*s\n", rd->trees[i]->name.len, rd->trees[i]->name.s);
				return rd->trees[i]->tree;
			}
		}
	}
	LM_INFO("domain %s not found, add it\n", domain);
	if ((id = add_domain(domain)) < 0) {
		LM_ERR("could not add domain\n");
		return NULL;
	}
	if ((rt = create_route_tree(domain, id)) == NULL) {
		return NULL;
	}
	if ((rt->tree = create_route_tree_item()) == NULL) {
		return NULL;
	}
	if (add_route_tree(rd, rt) < 0) {
		LM_ERR("couldn't add route tree\n");
		destroy_route_tree(rt);
		return NULL;
	}
	LM_INFO("created route tree: %.*s, %i\n", rt->name.len, rt->name.s, rt->id);
	return rt->tree;
}
Esempio n. 2
0
/**
 * Adds the given route information to the route tree identified by
 * route_tree. scan_prefix identifies the number for which the information
 * is and the rewrite_* parameters define what to do in case of a match.
 * prob gives the probability with which this rule applies if there are
 * more than one for a given prefix.
 *
 * Note that this is a recursive function. It strips off digits from the
 * beginning of scan_prefix and calls itself.
 *
 * @param route_tree the current route tree node
 * @param scan_prefix the prefix at the current position
 * @param flags user defined flags
 * @param mask mask for user defined flags
 * @param full_prefix the whole scan prefix
 * @param max_targets the number of targets
 * @param prob the weight of the rule
 * @param rewrite_hostpart the rewrite_host of the rule
 * @param strip the number of digits to be stripped off userpart before prepending prefix
 * @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
 * @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 -1-termintated array of hash indices of the route
                    for which this route is backup
 * @param comment a comment for the route rule
 *
 * @return 0 on success, -1 on failure
 *
 * @see add_route()
 */
int add_route_to_tree(struct route_tree_item * route_tree, const str * scan_prefix,
		flag_t flags, flag_t mask, const str * full_prefix, int max_targets, double prob,
		const str * rewrite_hostpart, int strip, const str * rewrite_local_prefix,
		const str * rewrite_local_suffix, int status, int hash_index,
		int backup, int * backed_up, const str * comment) {
	str next_prefix;
	struct route_flags *rf;

	if (scan_prefix->len == 0) {
		rf = add_route_flags(route_tree, flags, mask);
		if (rf==NULL) {
			LM_ERR("cannot add route_flags struct to route_tree\n");
			return -1;
		}
		return add_route_rule(rf, full_prefix, max_targets, prob, rewrite_hostpart, strip,
		                      rewrite_local_prefix, rewrite_local_suffix, status, hash_index,
		                      backup, backed_up, comment);
	} else {
		if (route_tree->nodes[*scan_prefix->s - '0'] == NULL) {
			route_tree->nodes[*scan_prefix->s - '0']
			= create_route_tree_item();
			if (route_tree->nodes[*scan_prefix->s - '0'] == NULL) {
				return -1;
			}
		}
		next_prefix.s = scan_prefix->s + 1;
		next_prefix.len = scan_prefix->len - 1;
		return add_route_to_tree(route_tree->nodes[*scan_prefix->s - '0'],
		                         &next_prefix, flags, mask, full_prefix, max_targets, prob,
		                         rewrite_hostpart, strip, rewrite_local_prefix,
		                         rewrite_local_suffix, status, hash_index,
		                         backup, backed_up, comment);
	}
}
Esempio n. 3
0
/**
 * Adds the given route information to the route tree identified by
 * route_tree. scan_prefix identifies the number for which the information
 * is and the rewrite_* parameters define what to do in case of a match.
 * prob gives the probability with which this rule applies if there are
 * more than one for a given prefix.
 *
 * Note that this is a recursive function. It strips off digits from the
 * beginning of scan_prefix and calls itself.
 *
 * @param rt the current route tree node
 * @param scan_prefix the prefix at the current position
 * @param full_prefix the whole scan prefix
 * @param max_targets the number of targets
 * @param prob the weight of the rule
 * @param rewrite_hostpart the rewrite_host of the rule
 * @param strip the number of digits to be stripped off userpart before prepending prefix
 * @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
 * @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 -1-termintated array of hash indices of the route 
                    for which this route is backup
 * @param comment a comment for the route rule
 *
 * @return 0 on success, -1 on failure
 *
 * @see add_route()
 */
int add_route_to_tree(struct route_tree_item * route_tree, const char * scan_prefix,
                      const char * full_prefix, int max_targets, double prob,
                      const char * rewrite_hostpart, int strip, const char * rewrite_local_prefix,
                      const char * rewrite_local_suffix, int status, int hash_index, 
                      int backup, int * backed_up, const char * comment) {
	if (!scan_prefix || *scan_prefix == '\0') {
		return add_route_rule(route_tree, full_prefix, max_targets, prob, rewrite_hostpart, strip,
		                      rewrite_local_prefix, rewrite_local_suffix, status, hash_index,
		                      backup, backed_up, comment);
	} else {
		if (route_tree->nodes[*scan_prefix - '0'] == NULL) {
			route_tree->nodes[*scan_prefix - '0']
			= create_route_tree_item();
			if (route_tree->nodes[*scan_prefix - '0'] == NULL) {
				return -1;
			}
		}
		return add_route_to_tree(route_tree->nodes[*scan_prefix - '0'],
		                         scan_prefix + 1, full_prefix, max_targets, prob,
		                         rewrite_hostpart, strip, rewrite_local_prefix,
		                         rewrite_local_suffix, status, hash_index,
		                         backup, backed_up, comment);
	}
}