예제 #1
0
/*!
 * \brief Evaluate kazoo transformations
 * \param msg SIP message
 * \param tp transformation
 * \param subtype transformation type
 * \param val pseudo-variable
 * \return 0 on success, -1 on error
 */
int kz_tr_eval(struct sip_msg *msg, tr_param_t *tp, int subtype, pv_value_t *val)
{

	if(val==NULL || (val->flags&PV_VAL_NULL))
		return -1;

	char* tofree = NULL;
	int oldflags = 0;

	kz_tr_set_crt_buffer();

	switch(subtype)
	{
		case TR_KAZOO_ENCODE:
			if(!(val->flags&PV_VAL_STR))
				return -1;

			oldflags = val->flags;
			tofree = val->rs.s;

			if( kz_amqp_encode_ex(&val->rs, val ) != 1) {
				LM_ERR("error encoding value\n");
				return -1;
			}

			// it seems that val memory is not freed
			// event with flag set to PV_VAL_PKG

			strncpy(_kz_tr_buffer, val->rs.s, val->rs.len);
			if(val->flags & PV_VAL_PKG)
				pkg_free(val->rs.s);
			else if(val->flags & PV_VAL_SHM)
				shm_free(val->rs.s);
			_kz_tr_buffer[val->rs.len] = '\0';
			val->flags = PV_VAL_STR;
			val->ri = 0;
			val->rs.s = _kz_tr_buffer;

			if(oldflags & PV_VAL_PKG) {
				pkg_free(tofree);
			} else if(oldflags & PV_VAL_SHM) {
				shm_free(tofree);
			}


			break;
		case TR_KAZOO_JSON:
			if(tp==NULL)
			{
				LM_ERR("kazoo json transform invalid parameter\n");
				return -1;
			}

			oldflags = val->flags;
			tofree = val->rs.s;

			if(kz_json_get_field_ex(&val->rs, &tp->v.s, val ) != 1) {
				LM_ERR("error getting json\n");
				return -1;
			}
			// it seems that val memory is not freed
			// event with flag set to PV_VAL_PKG

			strncpy(_kz_tr_buffer, val->rs.s, val->rs.len);
			if(val->flags & PV_VAL_PKG)
				pkg_free(val->rs.s);
			else if(val->flags & PV_VAL_SHM)
				shm_free(val->rs.s);
			_kz_tr_buffer[val->rs.len] = '\0';
			val->flags = PV_VAL_STR;
			val->ri = 0;
			val->rs.s = _kz_tr_buffer;

			if(oldflags & PV_VAL_PKG) {
				pkg_free(tofree);
			} else if(oldflags & PV_VAL_SHM) {
				shm_free(tofree);
			}

			break;

		default:
			LM_ERR("unknown kazoo transformation subtype %d\n", subtype);
			return -1;
	}
	return 0;
}
예제 #2
0
/*!
 * \brief Evaluate kazoo transformations
 * \param msg SIP message
 * \param tp transformation
 * \param subtype transformation type
 * \param val pseudo-variable
 * \return 0 on success, -1 on error
 */
int kz_tr_eval(struct sip_msg *msg, tr_param_t *tp, int subtype, pv_value_t *val)
{

	str sv;
	pv_value_t* pv;
	pv_value_t v;
    str v2 = {0,0};
	void* v1 = NULL;

	if(val==NULL || (val->flags&PV_VAL_NULL))
		return -1;


	kz_tr_set_crt_buffer();

	switch(subtype)
	{
		case TR_KAZOO_ENCODE:
			if(!(val->flags&PV_VAL_STR))
				return -1;

			pv = kz_alloc_pv_value();
			if(pv == NULL)
			{
				LM_ERR("kazoo encode transform : no more private memory\n");
				return -1;
			}

			if( kz_amqp_encode_ex(&val->rs, pv ) != 1) {
				LM_ERR("error encoding value\n");
				kz_destroy_pv_value(pv);
				return -1;
			}

			strncpy(_kz_tr_buffer, pv->rs.s, pv->rs.len);
			_kz_tr_buffer[pv->rs.len] = '\0';

			val->flags = PV_VAL_STR;
			val->ri = 0;
			val->rs.s = _kz_tr_buffer;
			val->rs.len = pv->rs.len;

			kz_destroy_pv_value(pv);
			kz_free_pv_value(val);

			break;
		case TR_KAZOO_JSON:
			if(!(val->flags&PV_VAL_STR))
				return -1;

			if(tp==NULL)
			{
				LM_ERR("kazoo json transform invalid parameter\n");
				return -1;
			}

			pv = kz_alloc_pv_value();
			if(pv == NULL)
			{
				LM_ERR("kazoo encode transform : no more private memory\n");
				return -1;
			}


			if(tp->type == TR_PARAM_STRING)
			{
				v1 = tp->v.s.s;
				if(fixup_spve_null(&v1, 1) != 0) {
					LM_ERR("cannot get spve_value from TR_PARAM_STRING : %.*s\n", tp->v.s.len, tp->v.s.s);
					return -1;
				}
				if (fixup_get_svalue(msg, (gparam_p)v1, &v2) != 0) {
					LM_ERR("cannot get value from TR_PARAM_STRING\n");
					fixup_free_spve_null(&v1, 1);
					return -1;
				}
				fixup_free_spve_null(&v1, 1);
				sv = v2;
			} else {
				if(pv_get_spec_value(msg, (pv_spec_p)tp->v.data, &v)!=0
						|| (!(v.flags&PV_VAL_STR)) || v.rs.len<=0)
				{
					LM_ERR("value cannot get spec value in json transform\n");
					kz_destroy_pv_value(pv);
					return -1;
				}
				sv = v.rs;
			}


			if(kz_json_get_field_ex(&val->rs, &sv, pv ) != 1) {
				LM_ERR("error getting json\n");
				kz_destroy_pv_value(pv);
				return -1;
			}
            
			strncpy(_kz_tr_buffer, pv->rs.s, pv->rs.len);
			_kz_tr_buffer[pv->rs.len] = '\0';

			val->flags = PV_VAL_STR;
			val->ri = 0;
			val->rs.s = _kz_tr_buffer;
			val->rs.len = pv->rs.len;

			kz_destroy_pv_value(pv);
			kz_free_pv_value(val);

			break;

		default:
			LM_ERR("unknown kazoo transformation subtype %d\n", subtype);
			return -1;
	}
	return 0;
}