Пример #1
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);
	}
}
Пример #2
0
/**
 * Adds the given route information to the routing domain identified by
 * domain. 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.
 *
 * @param rd the route data to which the route shall be added
 * @param carrier_id the carrier id of the route to be added
 * @param domain_id the routing domain id of the new route
 * @param scan_prefix the number prefix
 * @param flags user defined flags
 * @param mask mask for user defined flags
 * @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 error in which case it LOGs a message.
 */
int add_route(struct route_data_t * rd, int carrier_id,
		int domain_id, const str * scan_prefix, flag_t flags, flag_t mask, 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) {
	struct carrier_data_t * carrier_data = NULL;
	struct domain_data_t * domain_data = NULL;
	LM_INFO("adding prefix %.*s, prob %f\n", scan_prefix->len, scan_prefix->s, prob);

	if ((carrier_data = get_carrier_data(rd, carrier_id)) == NULL) {
		LM_ERR("could not retrieve carrier data for carrier id %d\n", carrier_id);
		return -1;
	}

	if ((domain_data = get_domain_data_or_add(rd, carrier_data, domain_id)) == NULL) {
		LM_ERR("could not retrieve domain data\n");
		return -1;
	}

	LM_INFO("found carrier and domain, now adding route\n");
	return add_route_to_tree(domain_data->tree, scan_prefix, flags, mask, scan_prefix, max_targets, prob, rewrite_hostpart,
	                         strip, rewrite_local_prefix, rewrite_local_suffix, status,
	                         hash_index, backup, backed_up, comment);
}
Пример #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);
	}
}
Пример #4
0
/**
 * Loads the routing data from the config file given in global
 * variable config_data and stores it in routing tree rd.
 * The function mixes code parsing calls with rd structure
 * completion.
 *
 * @param rd Pointer to the route data tree where the routing data
 * shall be loaded into
 *
 * @return 0 means ok, -1 means an error occurred
 *
 */
int load_config(struct route_data_t * rd) {
	FILE * file;

	int ret_domain, ret_prefix, ret_target, ret_prefix_opts, ret_target_opts;
	int domain_id, allocated_domain_num = DEFAULT_DOMAIN_NUM;
	str domain_name, prefix_name, rewrite_host;
	char domain_buf[CR_MAX_LINE_SIZE], prefix_buf[CR_MAX_LINE_SIZE],  rewrite_buf[CR_MAX_LINE_SIZE];

	str rewrite_prefix, rewrite_suffix, comment;
	struct domain_data_t *domain_data = NULL;
	struct carrier_data_t * tmp_carrier_data;
	int hash_index, max_targets = 0, strip;
	double prob;
	int * backed_up = NULL;
	int backed_up_size = 0, backup = 0, status;
	void* p_realloc;
	int i=0, l, k;

	domain_name.s = domain_buf; domain_name.len = CR_MAX_LINE_SIZE;
	prefix_name.s = prefix_buf; prefix_name.len = CR_MAX_LINE_SIZE;
	rewrite_host.s = rewrite_buf; rewrite_host.len = CR_MAX_LINE_SIZE;

	/* open configuration file */
	if ((file = fopen(config_file, "rb"))==NULL) {
		LM_ERR("Cannot open source file.\n");
		return -1;
	}

	rd->carrier_num = 1;
	rd->first_empty_carrier = 0;
	rd->domain_num = 0;

	if ((rd->carriers = shm_malloc(sizeof(struct carrier_data_t *))) == NULL) {
		SHM_MEM_ERROR;
		goto errclose;
	}
	memset(rd->carriers, 0, sizeof(struct carrier_data_t *));

	/* Create carrier map */
	if ((rd->carrier_map = shm_malloc(sizeof(struct name_map_t))) == NULL) {
		SHM_MEM_ERROR;
		goto errclose;
	}

	memset(rd->carrier_map, 0, sizeof(struct name_map_t));
	rd->carrier_map[0].id = 1;
	rd->carrier_map[0].name.len = default_tree.len;
	rd->carrier_map[0].name.s = shm_malloc(rd->carrier_map[0].name.len);

	if (rd->carrier_map[0].name.s == NULL) {
		SHM_MEM_ERROR;
		goto errclose;
	}
	memcpy(rd->carrier_map[0].name.s, default_tree.s, rd->carrier_map[0].name.len);

	/* Create domain map */
	if ((rd->domain_map = shm_malloc(sizeof(struct name_map_t) * allocated_domain_num)) == NULL) {
		SHM_MEM_ERROR;
		goto errclose;
	}
	memset(rd->domain_map, 0, sizeof(struct name_map_t) * allocated_domain_num);

	/* Create and insert carrier data structure */
	tmp_carrier_data = create_carrier_data(1, &rd->carrier_map[0].name, allocated_domain_num);
	if (tmp_carrier_data == NULL) {
		LM_ERR("can't create new carrier\n");
		goto errclose;
	}
	tmp_carrier_data->domain_num = 0;
	tmp_carrier_data->id = 1;
	tmp_carrier_data->name = &(rd->carrier_map[0].name);

	if (add_carrier_data(rd, tmp_carrier_data) < 0) {
		LM_ERR("couldn't add carrier data\n");
		destroy_carrier_data(tmp_carrier_data);
		goto errclose;
	}

	init_prefix_opts();
	init_target_opts();

	/* add all routes by parsing the route conf file */
	/* while there are domain structures, get name and parse the structure*/
	while ((ret_domain = parse_struct_header(file, "domain", &domain_name))
			== SUCCESSFUL_PARSING) {

		domain_id = ++rd->domain_num;
		tmp_carrier_data->domain_num++;

		/* (re)allocate memory for a maximum of MAX_DOMAIN_NUM domains
		 rd is not fully allocated from the start as this would require the preparsing
		 of the entire route file */
		if ( rd->domain_num > allocated_domain_num){

			if (MAX_DOMAIN_NUM <= allocated_domain_num){
				LM_ERR("Maximum number of domains reached");
				break;
			}

			LM_INFO("crt_alloc_size=%d must be increased \n", allocated_domain_num);
			allocated_domain_num *= 2;

			if ( ( p_realloc = shm_realloc(rd->domain_map,
					sizeof(struct name_map_t) * allocated_domain_num) ) == NULL)
			{
				SHM_MEM_ERROR;
				goto errclose;
			}

			rd->domain_map = (struct name_map_t *)p_realloc;

			if (( p_realloc = shm_realloc( rd->carriers[0]->domains,
					sizeof(struct domain_data_t *) * allocated_domain_num)) == NULL) {
				SHM_MEM_ERROR;
				goto errclose;
			}
			rd->carriers[0]->domains = (struct domain_data_t **)p_realloc;

			for (i=0; i<rd->domain_num-1; i++){
				rd->carriers[0]->domains[i]->name = &(rd->domain_map[i].name);
			}
		}// end of mem (re)allocation for domains

		/*insert domain in domain map*/
		rd->domain_map[domain_id-1].id = domain_id;
		rd->domain_map[domain_id-1].name.len = domain_name.len;
		rd->domain_map[domain_id-1].name.s = shm_malloc(rd->domain_map[domain_id-1].name.len);
		if (rd->domain_map[domain_id-1].name.s == NULL) {
			SHM_MEM_ERROR;
			goto errclose;
		}
		memcpy(rd->domain_map[domain_id-1].name.s, domain_name.s, rd->domain_map[domain_id-1].name.len);

		/* create new domain data */
		if ((domain_data = create_domain_data(domain_id,&(rd->domain_map[domain_id-1].name))) == NULL) {
			LM_ERR("could not create new domain data\n");
			goto errclose;
		}

		if (add_domain_data(tmp_carrier_data, domain_data, domain_id-1) < 0) {
			LM_ERR("could not add domain data\n");
			destroy_domain_data(domain_data);
			goto errclose;
		}
		LM_DBG("added domain %d '%.*s' to carrier %d '%.*s'\n",
				domain_id, domain_name.len, domain_name.s,
				tmp_carrier_data->id, tmp_carrier_data->name->len, tmp_carrier_data->name->s);

		/* while there are prefix structures, get name and parse the structure */
		while ((ret_prefix = parse_struct_header(file, "prefix", &prefix_name))
				== SUCCESSFUL_PARSING) {

			reset_prefix_opts();
			if (str_strcasecmp(&prefix_name, &CR_EMPTY_PREFIX) == 0) {
				prefix_name.s[0] = '\0';
				prefix_name.len = 0;
			}

			/* look for max_targets = value which is described in prefix_options */
			if ((ret_prefix_opts = parse_options(file, prefix_options,
					PO_MAX_IDS, "target")) != SUCCESSFUL_PARSING) {
				LM_ERR("Error in parsing \n");
				goto errclose;
			}

			max_targets = prefix_options[PO_MAX_TARGETS].value.int_data;
			/* look for max_targets target structures */
			for ( k = 0; k < max_targets; k++) {
				/* parse the target header, get name and continue*/
				ret_target = parse_struct_header(file, "target", &rewrite_host);
				if (ret_target != SUCCESSFUL_PARSING) {
					LM_ERR("Error in parsing \n");
					goto errclose;
				}

				reset_target_opts();
				/* look for the target options: prob, hash index, status, etc*/
				ret_target_opts = parse_options(file, target_options, TO_MAX_IDS, "}");
				if ( SUCCESSFUL_PARSING == ret_target_opts ){
					/* parsing target structure closing bracket*/
					parse_struct_stop(file);
				}else{
					LM_ERR("Error in parsing in target options \n");
					goto errclose;
				}
				/* intermediary variables for more lisibility */
				if (str_strcasecmp(&rewrite_host, &CR_EMPTY_PREFIX) == 0) {
					rewrite_host.s[0] = '\0';
					rewrite_host.len = 0;
				}
				LM_DBG("loading target %.*s\n", rewrite_host.len, rewrite_host.s);
				prob = target_options[TO_ID_PROB].value.float_data;
				strip = target_options[TO_ID_STRIP].value.int_data;
				rewrite_prefix.s = target_options[TO_ID_REWR_PREFIX].value.string_data.s;
				rewrite_prefix.len = target_options[TO_ID_REWR_PREFIX].value.string_data.len;
				rewrite_suffix.s = target_options[TO_ID_REWR_SUFFIX].value.string_data.s;
				rewrite_suffix.len = target_options[TO_ID_REWR_SUFFIX].value.string_data.len;
				hash_index = target_options[TO_ID_HASH_INDEX].value.int_data;
				comment.s = target_options[TO_ID_COMMENT].value.string_data.s;
				comment.len = target_options[TO_ID_COMMENT].value.string_data.len;
				status = target_options[TO_ID_STATUS].value.int_data;

				if ( (backed_up_size = target_options[TO_ID_BACKED_UP].no_elems) > 0){
					if ((backed_up = pkg_malloc(sizeof(int) * (backed_up_size + 1))) == NULL) {
						PKG_MEM_ERROR;
						goto errclose;
					}
					for (l = 0; l < backed_up_size; l++) {
						backed_up[l] = target_options[TO_ID_BACKED_UP].value.int_list[l];
					}
					backed_up[backed_up_size] = -1;
				}
				backup = target_options[TO_ID_BACKUP].value.int_data;

				LM_DBG("\n Adding route to tree <'%.*s'>: prefix_name:%s\n,"
						" max_targets =%d\n, prob=%f\n, rewr_host=%s\n,"
						" strip=%i\n, rwr_prefix=%s\n, rwr_suff=%s\n,"
						" status=%i\n, hash_index=%i\n, comment=%s \n",
						domain_data->name->len, domain_data->name->s, prefix_name.s,
						max_targets, prob, rewrite_host.s, strip, rewrite_prefix.s,
						rewrite_suffix.s, status, hash_index, comment.s);

				if (add_route_to_tree(domain_data->tree, &prefix_name, 0, 0,
						&prefix_name, max_targets, prob, &rewrite_host,
						strip, &rewrite_prefix, &rewrite_suffix, status,
						hash_index, backup, backed_up, &comment) < 0) {
					LM_INFO("Error while adding route\n");
					if (backed_up) {
						pkg_free(backed_up);
					}
					goto errclose;
				}

				if (backed_up) {
					pkg_free(backed_up);
				}
				backed_up = NULL;
			}

			if (k != prefix_options[0].value.int_data ) {
				LM_ERR("Error in parsing: max_targets =%i, actual targets =%i \n",
						prefix_options[0].value.int_data, i);
				goto errclose;
			}
			/* parsing prefix structure closing bracket */
			if (parse_struct_stop(file) != SUCCESSFUL_PARSING) {
				LM_ERR("Error in parsing targets, expecting } \n");
				goto errclose;

			}
		} // END OF PREFIX part

		/* parsing domain structure closing bracket */
		if (parse_struct_stop(file) != SUCCESSFUL_PARSING) {
			LM_ERR("Error in parsing targets, expecting } \n");
			goto errclose;
		}
	}

	if (EOF_REACHED != ret_domain){
		LM_ERR("Error appeared while parsing domain header \n");
		goto errclose;
	}

	LM_INFO("File parsed successfully \n");
	fclose(file);
	return 0;
errclose:
	fclose(file);
	return -1;
}