int pv_get_acc_current_leg(struct sip_msg *msg, pv_param_t *param, pv_value_t *val) { acc_ctx_t* ctx=try_fetch_ctx(); if (ctx == NULL) { /* if we don't have a context then create it */ if (init_acc_ctx(&ctx) < 0) { LM_ERR("failed to create accounting context!\n"); return -1; } ACC_PUT_CTX(ctx); } if (ctx->leg_values == NULL) { LM_ERR("no legs defined!\n"); return -1; } accX_lock(&ctx->lock); val->ri = ctx->legs_no - 1; val->rs.s = int2str(ctx->legs_no - 1, &val->rs.len); accX_unlock(&ctx->lock); val->flags = PV_VAL_INT | PV_VAL_STR | PV_TYPE_INT; return 0; }
/* * setter function for $acc_extra */ int pv_set_acc_extra(struct sip_msg *msg, pv_param_t *param, int op, pv_value_t *val) { int tag_idx; acc_ctx_t* ctx=try_fetch_ctx(); if (param == NULL || val == NULL) { LM_ERR("bad params!\n"); return -1; } if (ctx == NULL) { /* if we don't have a context then create it */ if (init_acc_ctx(&ctx) < 0) { LM_ERR("failed to create accounting context!\n"); return -1; } ACC_PUT_CTX(ctx); } tag_idx = param->pvn.u.isname.name.n; /* sanity checks for the tag; it should be valid since * we found it in the parse name function */ if (tag_idx < 0 || tag_idx >= extra_tgs_len) { LM_BUG("invalid tag value! probably a memory corruption issue!\n"); return -1; } /* go through all extras and fetch first value you find * all the extras with the same tag will have the same * value */ accX_lock(&ctx->lock); if (set_value_shm(val, &ctx->extra_values[tag_idx]) < 0) { LM_ERR("failed to set extra <%.*s> value!\n", extra_tags[tag_idx].len, extra_tags[tag_idx].s); accX_unlock(&ctx->lock); return -1; } accX_unlock(&ctx->lock); return 0; }
/* * getter function for $acc_extra */ int pv_get_acc_extra(struct sip_msg *msg, pv_param_t *param, pv_value_t *val) { int tag_idx; acc_ctx_t* ctx=try_fetch_ctx(); if (param == NULL || val == NULL) { LM_ERR("bad input params!\n"); return -1; } if (ctx == NULL) { /* if we don't have a context then create it */ if (init_acc_ctx(&ctx) < 0) { LM_ERR("failed to create accounting context!\n"); return -1; } ACC_PUT_CTX(ctx); } tag_idx = param->pvn.u.isname.name.n; /* sanity checks for the tag; it should be valid since * we found it in the parse name function */ if (tag_idx < 0 || tag_idx >= extra_tgs_len) { LM_BUG("invalid tag value! probably a memory corruption issue!\n"); return -1; } accX_lock(&ctx->lock); if (ctx->extra_values[tag_idx].value.s == NULL) { val->flags = PV_VAL_NULL; } else { val->rs = ctx->extra_values[tag_idx].value; val->flags = PV_VAL_STR; } accX_unlock(&ctx->lock); return 0; }
int w_new_leg(struct sip_msg* msg) { acc_ctx_t* ctx = try_fetch_ctx(); if (ctx == NULL) { if (init_acc_ctx(&ctx) < 0) { LM_ERR("failed to create accounting context!\n"); return -1; } } accX_lock(&ctx->lock); if (push_leg(ctx) < 0) { LM_ERR("failed to create new leg!\n"); accX_unlock(&ctx->lock); return -1; } accX_unlock(&ctx->lock); return 1; }
int pv_set_acc_leg(struct sip_msg *msg, pv_param_t *param, int flag, pv_value_t *val) { int tag_idx, leg_idx; acc_ctx_t* ctx = try_fetch_ctx(); pv_value_t idx_value; if (ctx == NULL) { /* if we don't have a context then create it */ if (init_acc_ctx(&ctx) < 0) { LM_ERR("failed to create accounting context!\n"); return -1; } ACC_PUT_CTX(ctx); } if (ctx->leg_values == NULL) { LM_ERR("no legs defined!\n"); return -1; } tag_idx = param->pvn.u.isname.name.n; if (param->pvi.type == PV_IDX_PVAR) { if (pv_get_spec_value(msg, param->pvi.u.dval, &idx_value) < 0) { LM_ERR("failed to fetch index value!\n"); return -1; } if (idx_value.flags&PV_VAL_INT) { leg_idx = idx_value.ri; } else if (idx_value.flags&PV_VAL_STR) { if (str2sint(&idx_value.rs, &leg_idx) < 0) { goto invalid_leg; } } else { goto invalid_leg; } } else if(param->pvi.type == PV_IDX_INT) { leg_idx = param->pvi.u.ival; } else { /* if not provided consider the value of the last leg */ leg_idx = ctx->legs_no - 1; } if (leg_idx >= (int)ctx->legs_no) { LM_ERR("there aren't that many legs!\n"); return -1; } if (leg_idx < 0) { if ((int)ctx->legs_no + leg_idx < 0) { LM_ERR("invalid leg index %d!\n", leg_idx); return -1; } /* -1 will be the last element and so on */ leg_idx += ctx->legs_no; } accX_lock(&ctx->lock); if (set_value_shm(val, &ctx->leg_values[leg_idx][tag_idx]) < 0) { LM_ERR("failed to set leg <%.*s> value for leg number %d!\n", extra_tags[tag_idx].len, leg_tags[tag_idx].s, leg_idx); accX_unlock(&ctx->lock); return -1; } accX_unlock(&ctx->lock); return 0; invalid_leg: LM_ERR("cannot fetch leg index value!\n"); return -1; }