Пример #1
0
/**
 * Get the id that belongs to a string name from gparam_t structure.
 *
 * Get the id that belongs to a string name from gparam_t structure, use the
 * search_id function for the lookup.
 * @param _msg SIP message
 * @param gp id as integer, pseudo-variable or AVP name of carrier
 * @param map lookup function
 * @param size size of the list
 * @return id on success, -1 otherwise
 */
static inline int cr_gp2id(struct sip_msg *_msg, gparam_t *gp, struct name_map_t *map, int size) {
	int id;
	struct usr_avp *avp;
	int_str avp_val;
	str tmp;

	switch (gp->type) {
	case GPARAM_TYPE_INT:
		return gp->v.i;
		break;
	case GPARAM_TYPE_PVE:
		/* does this PV hold an AVP? */
		if (gp->v.pve->spec->type==PVT_AVP) {
			avp = search_first_avp(gp->v.pve->spec->pvp.pvn.u.isname.type,
						gp->v.pve->spec->pvp.pvn.u.isname.name, &avp_val, 0);
			if (!avp) {
				if(gp->v.pve->spec->pvp.pvn.u.isname.type & AVP_NAME_STR)
					LM_ERR("cannot find AVP '%.*s'\n", gp->v.pve->spec->pvp.pvn.u.isname.name.s.len,
						gp->v.pve->spec->pvp.pvn.u.isname.name.s.s);
				else if(gp->v.pve->spec->pvp.pvn.u.isname.type & AVP_NAME_RE)
					LM_ERR("cannot find AVP regex\n");
				else 	LM_ERR("cannot find AVP '%d'\n", gp->v.pve->spec->pvp.pvn.u.isname.name.n);
				return -1;
			}
			if ((avp->flags&AVP_VAL_STR)==0) {
				return avp_val.n;
			} else {
				id = map_name2id(map, size, &avp_val.s);
				if (id < 0) {
					if(gp->v.pve->spec->pvp.pvn.u.isname.type & AVP_NAME_STR)
						LM_ERR("cannot map carrier with id %.*s from  AVP '%.*s'\n", avp_val.s.len, avp_val.s.s, gp->v.pve->spec->pvp.pvn.u.isname.name.s.len,
							gp->v.pve->spec->pvp.pvn.u.isname.name.s.s);
					else if(gp->v.pve->spec->pvp.pvn.u.isname.type & AVP_NAME_RE)
						LM_ERR("cannot map carrier with id %.*s from  AVP regex\n", avp_val.s.len, avp_val.s.s);
					else 	LM_ERR("cannot map carrier with id %.*s from  AVP '%d'\n", avp_val.s.len, avp_val.s.s, gp->v.pve->spec->pvp.pvn.u.isname.name.n);
					return -1;
				}
				return id;
			}
		} else {
			/* retrieve name from parameter */
			if (fixup_get_svalue(_msg, gp, &tmp)<0) {
				LM_ERR("cannot print the name from PV\n");
				return -1;
			}
			id = map_name2id(map, size, &tmp);
			if (id < 0) {
				LM_ERR("could not find id '%.*s' from PV\n", tmp.len, tmp.s);
				return -1;
			}
			return id;
		}
	default:
		LM_ERR("invalid parameter type\n");
		return -1;
	}
}
Пример #2
0
/**
 * The fixup funcions will use the initial mapping.
 * If the mapping changes afterwards (eg. due to cr_reload_routes),
 * the names used in the routing script will not be mapped
 * to the correct IDs!
 * @param name domain name
 * @return domain id 
 */
static int domain_name_2_id(const str *name) {
	int id;
	struct route_data_t * rd;

	do {
		rd = get_data();
	} while (rd == NULL);
	
	id = map_name2id(rd->domain_map, rd->domain_num, name);
	
	release_data(rd);

	return id;
}
Пример #3
0
/**
 * loads the config data into shared memory (but doesn't really
 * share it), updates the routing data and writes it to the config
 * file. Afterwards, the global routing data is reloaded.
 *
 * @param opts pointer to the option structure which contains
 * data to be modified or to be added
 *
 * @return 0 on success, -1 on failure
 */
static int update_route_data(fifo_opt_t * opts) {
	struct route_data_t * rd;
	int i,j;
	int domain_id;
	str tmp_domain;
	str tmp_prefix;
	str tmp_host;
	str tmp_rewrite_prefix;
	str tmp_rewrite_suffix;
	str tmp_comment = str_init("");

	if ((rd = shm_malloc(sizeof(struct route_data_t))) == NULL) {
		SHM_MEM_ERROR;
		return -1;
	}
	memset(rd, 0, sizeof(struct route_data_t));
	if (load_config(rd) < 0) {
		LM_ERR("could not load config");
		FIFO_ERR(E_LOADCONF);
		return -1;
	}

	if (rule_fixup(rd) < 0) {
		LM_ERR("could not fixup rules");
		FIFO_ERR(E_RULEFIXUP);
		return -1;
	}
	updated = 0;

	if (opts->cmd == OPT_ADD) {
		tmp_domain=opts->domain;
		tmp_prefix=opts->prefix;
		tmp_host=opts->host;
		tmp_rewrite_prefix=opts->rewrite_prefix;
		tmp_rewrite_suffix=opts->rewrite_suffix;
		if (tmp_domain.s==NULL) {
			tmp_domain.s="";
			tmp_domain.len=0;
		}
		if (tmp_prefix.s==NULL) {
			tmp_prefix.s="";
			tmp_prefix.len=0;
		}
		if (tmp_host.s==NULL) {
			tmp_host.s="";
			tmp_host.len=0;
		}
		if (tmp_rewrite_prefix.s==NULL) {
			tmp_rewrite_prefix.s="";
			tmp_rewrite_prefix.len=0;
		}
		if (tmp_rewrite_suffix.s==NULL) {
			tmp_rewrite_suffix.s="";
			tmp_rewrite_suffix.len=0;
		}

		domain_id = map_name2id(rd->domain_map, rd->domain_num, &tmp_domain);
		if (domain_id < 0) {
			LM_ERR("cannot find id for domain '%.*s'", tmp_domain.len, tmp_domain.s);
			goto errout;
		}

		if (add_route(rd, 1, domain_id, &tmp_prefix, 0, 0, 0, opts->prob,
		              &tmp_host, opts->strip, &tmp_rewrite_prefix, &tmp_rewrite_suffix,
		              opts->status, opts->hash_index, -1, NULL, &tmp_comment) < 0) {
			goto errout;
		}
		updated = 1;
		if (rule_fixup(rd) < 0) {
			LM_ERR("could not fixup rules after route appending");
			FIFO_ERR(E_RULEFIXUP);
			goto errout;
		}
	} else {
		for (i=0; i<rd->carrier_num; i++) {
			if(rd->carriers[i]){
				for (j=0; j<rd->carriers[i]->domain_num; j++) {
					if (rd->carriers[i]->domains[j] && rd->carriers[i]->domains[j]->tree) {
						if (update_route_data_recursor(rd->carriers[i]->domains[j]->tree, rd->carriers[i]->domains[j]->name, opts) < 0) {
							goto errout;
						}
					}
				}
			}
		}
	}

	if(!updated){
		LM_ERR("no match for update found");
		FIFO_ERR(E_NOUPDATE);
		goto errout;
	}

	if (save_config(rd) < 0) {
		LM_ERR("could not save config");
		FIFO_ERR(E_SAVECONF);
		goto errout;
	}

	if (reload_route_data() == -1) {
		LM_ERR("could not reload route data");
		FIFO_ERR(E_LOADCONF);
		goto errout;
	}

	clear_route_data(rd);
	return 0;
errout:
	clear_route_data(rd);
	return -1;
}