Skip to content

Commit

Permalink
[WIP] cgrates: parse values back for Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Mar 15, 2018
1 parent 3419f93 commit b0aeb0a
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 57 deletions.
38 changes: 19 additions & 19 deletions modules/cgrates/cgrates_acc.c
Expand Up @@ -340,49 +340,49 @@ static json_object *cgr_get_start_acc_msg(struct sip_msg *msg,
goto error;
} else {
callid = cgr_get_sess_callid(msg, s, &msg->callid->body);
if (!callid || cgr_msg_push_str(cmsg, "OriginID", callid) < 0) {
if (!callid || cgr_obj_push_str(cmsg->params, "OriginID", callid) < 0) {
LM_ERR("cannot push OriginID!\n");
goto error;
}
}
}

if (ctx && !cgr_get_const_kv(&s->event_kvs, "DialogID") &&
cgr_msg_push_int(cmsg, "DialogID", dlg->h_id) < 0) {
cgr_obj_push_int(cmsg->params, "DialogID", dlg->h_id) < 0) {
LM_ERR("cannot push DialogID!\n");
goto error;
}

if (ctx && !cgr_get_const_kv(&s->event_kvs, "DialogEntry") &&
cgr_msg_push_int(cmsg, "DialogEntry", dlg->h_entry) < 0) {
cgr_obj_push_int(cmsg->params, "DialogEntry", dlg->h_entry) < 0) {
LM_ERR("cannot push DialogEntry!\n");
goto error;
}

/* Account */
if (cgr_msg_push_str(cmsg, "Account", &si->acc) < 0) {
if (cgr_obj_push_str(cmsg->params, "Account", &si->acc) < 0) {
LM_ERR("cannot push Account info!\n");
goto error;
}

/* SetupTime */
stime.s = int2str(si->start_time, &stime.len);
if (cgr_msg_push_str(cmsg, "SetupTime", &stime) < 0) {
if (cgr_obj_push_str(cmsg->params, "SetupTime", &stime) < 0) {
LM_ERR("cannot push SetupTime info!\n");
goto error;
}

/* AnswerTime */
if (ctx) {
stime.s = int2str(ctx->answer_time, &stime.len);
if (cgr_msg_push_str(cmsg, "AnswerTime", &stime) < 0) {
if (cgr_obj_push_str(cmsg->params, "AnswerTime", &stime) < 0) {
LM_ERR("cannot push AnswerTime info!\n");
goto error;
}
}

/* Destination */
if (cgr_msg_push_str(cmsg, "Destination", &si->dst) < 0) {
if (cgr_obj_push_str(cmsg->params, "Destination", &si->dst) < 0) {
LM_ERR("cannot push Destination info!\n");
goto error;
}
Expand Down Expand Up @@ -428,30 +428,30 @@ static json_object *cgr_get_stop_acc_msg(struct sip_msg *msg,
/* if origin was not added from script, add it now */
if (ctx && !cgr_get_const_kv(&s->event_kvs, "OriginID")) {
callid = cgr_get_sess_callid(msg, s, &dlg->callid);
if (cgr_msg_push_str(cmsg, "OriginID", callid) < 0) {
if (cgr_obj_push_str(cmsg->params, "OriginID", callid) < 0) {
LM_ERR("cannot push OriginID!\n");
goto error;
}
}

/* Account */
if (cgr_msg_push_str(cmsg, "Account", &si->acc) < 0) {
if (cgr_obj_push_str(cmsg->params, "Account", &si->acc) < 0) {
LM_ERR("cannot push Account info!\n");
goto error;
}

/* SetupTime */
if (ctx->answer_time != si->start_time) {
tmp.s = int2str(si->start_time, &tmp.len);
if (cgr_msg_push_str(cmsg, "SetupTime", &tmp) < 0) {
if (cgr_obj_push_str(cmsg->params, "SetupTime", &tmp) < 0) {
LM_ERR("cannot push SetupTime info!\n");
goto error;
}
}

/* AnswerTime */
tmp.s = int2str(ctx->answer_time, &tmp.len);
if (cgr_msg_push_str(cmsg, "AnswerTime", &tmp) < 0) {
if (cgr_obj_push_str(cmsg->params, "AnswerTime", &tmp) < 0) {
LM_ERR("cannot push AnswerTime info!\n");
goto error;
}
Expand All @@ -461,7 +461,7 @@ static json_object *cgr_get_stop_acc_msg(struct sip_msg *msg,
tmp.s[tmp.len] = 's';
tmp.len++;
tmp.s[tmp.len] = 0;
if (cgr_msg_push_str(cmsg, "Usage", &tmp) < 0) {
if (cgr_obj_push_str(cmsg->params, "Usage", &tmp) < 0) {
LM_ERR("cannot add Usage node\n");
goto error;
}
Expand Down Expand Up @@ -489,17 +489,17 @@ static json_object *cgr_get_cdr_acc_msg(struct sip_msg *msg,
return NULL;
}

if (cgr_msg_push_str(cmsg, "OriginID", callid) < 0) {
if (cgr_obj_push_str(cmsg->params, "OriginID", callid) < 0) {
LM_ERR("cannot add OriginID node\n");
goto error;
}

if (cgr_msg_push_str(cmsg, "Account", &si->acc) < 0) {
if (cgr_obj_push_str(cmsg->params, "Account", &si->acc) < 0) {
LM_ERR("cannot add Account node\n");
goto error;
}

if (cgr_msg_push_str(cmsg, "Destination", &si->dst) < 0) {
if (cgr_obj_push_str(cmsg->params, "Destination", &si->dst) < 0) {
LM_ERR("cannot add Destination node\n");
goto error;
}
Expand All @@ -509,28 +509,28 @@ static json_object *cgr_get_cdr_acc_msg(struct sip_msg *msg,
tmp.s[tmp.len] = 's';
tmp.len++;
tmp.s[tmp.len] = 0;
if (cgr_msg_push_str(cmsg, "Usage", &tmp) < 0) {
if (cgr_obj_push_str(cmsg->params, "Usage", &tmp) < 0) {
LM_ERR("cannot add Usage node\n");
goto error;
}

if (ctx->answer_time) {
tmp.s = int2str(ctx->answer_time, &tmp.len);
if (cgr_msg_push_str(cmsg, "AnswerTime", &tmp) < 0) {
if (cgr_obj_push_str(cmsg->params, "AnswerTime", &tmp) < 0) {
LM_ERR("cannot add AnswerTime node\n");
goto error;
}
}

if (si->start_time && si->start_time != ctx->answer_time) {
tmp.s = int2str(si->start_time, &tmp.len);
if (cgr_msg_push_str(cmsg, "SetupTime", &tmp) < 0) {
if (cgr_obj_push_str(cmsg->params, "SetupTime", &tmp) < 0) {
LM_ERR("cannot add SetupTime node\n");
goto error;
}
}

if (dlg && cgr_msg_push_str(cmsg, "DisconnectCause", &dlg->terminate_reason) < 0) {
if (dlg && cgr_obj_push_str(cmsg->params, "DisconnectCause", &dlg->terminate_reason) < 0) {
LM_ERR("cannot add DisconnectCause node\n");
goto error;
}
Expand Down
60 changes: 42 additions & 18 deletions modules/cgrates/cgrates_auth.c
Expand Up @@ -25,6 +25,7 @@
static int cgr_proc_auth_reply(struct cgr_conn *c, json_object *jobj,
void *p, char *error)
{
struct cgr_kv *k;
int_str val;
if (error) {
val.s.s = error;
Expand All @@ -36,29 +37,45 @@ static int cgr_proc_auth_reply(struct cgr_conn *c, json_object *jobj,
return -2;
}

if (json_object_get_type(jobj) != json_type_int) {
LM_ERR("CGRateS returned a non-int type in Auth reply: %d %s\n",
json_object_get_type(jobj), json_object_to_json_string(jobj));
return -5;
if (cgre_compat_mode) {
if (json_object_get_type(jobj) != json_type_int) {
LM_ERR("CGRateS returned a non-int type in Auth reply: %d %s\n",
json_object_get_type(jobj), json_object_to_json_string(jobj));
return -5;
}
val.n = json_object_get_int(jobj);
/* -1: always allowed (postpaid)
* 0: not allowed to call
* *: allowed
*/
if (cgrates_set_reply(CGR_KVF_TYPE_INT, &val) < 0) {
LM_ERR("cannot set the reply value!\n");
return -1;
}
return ((val.n == 0) ? -2: 1);
} else if (cgrates_set_reply_with_values(jobj) < 0) {
LM_ERR("cannot set the reply values!\n");
return -1;
}
val.n = json_object_get_int(jobj);
/* -1: always allowed (postpaid)
* 0: not allowed to call
* *: allowed
*/
if (cgrates_set_reply(CGR_KVF_TYPE_INT, &val) < 0) {
LM_ERR("cannot set the reply value!\n");
val.s.s = "MaxUsage";
val.s.len = strlen(val.s.s);
k = cgr_get_local(val.s);
if (!k || !(k->flags & CGR_KVF_TYPE_INT)) {
LM_ERR("MaxUsage not found in command reply!\n");
return -1;
}
return ((val.n == 0) ? -2: 1);

return ((k->value.n == 0) ? -2: 1);
}

static json_object *cgr_get_auth_msg(struct sip_msg *msg, str *acc, str *dst, str *tag)
{
struct cgr_session *s;
struct cgr_msg *cmsg = NULL;
static str cmd = str_init("SMGenericV1.GetMaxUsage");
static str cmd_ng = str_init("SessionSv1.AuthorizeEvent");
static str cmd_compat = str_init("SMGenericV1.GetMaxUsage");
str stime;
str *cmd = (cgre_compat_mode ? &cmd_compat: &cmd_ng);

if (msg->callid==NULL && ((parse_headers(msg, HDR_CALLID_F, 0)==-1) ||
(msg->callid==NULL)) ) {
Expand All @@ -68,34 +85,41 @@ static json_object *cgr_get_auth_msg(struct sip_msg *msg, str *acc, str *dst, st
s = cgr_get_sess(cgr_try_get_ctx(), tag);
stime.s = int2str(time(NULL), &stime.len);

cmsg = cgr_get_generic_msg(&cmd, s);
cmsg = cgr_get_generic_msg(cmd, s);
if (!cmsg) {
LM_ERR("cannot create generic cgrates message!\n");
return NULL;
}

if (!cgre_compat_mode &&
((s && !cgr_get_const_kv(&s->req_kvs, "GetMaxUsage")) || !s) &&
cgr_obj_push_bool(cmsg->opts, "GetMaxUsage", 1) < 0) {
LM_ERR("cannot push GetMaxUsage to request opts!\n");
goto error;
}

/* OriginID */
/* if origin was not added from script, add it now */
if (((s && !cgr_get_const_kv(&s->event_kvs, "OriginID")) || !s) &&
cgr_msg_push_str(cmsg, "OriginID", &msg->callid->body) < 0) {
cgr_obj_push_str(cmsg->params, "OriginID", &msg->callid->body) < 0) {
LM_ERR("cannot push OriginID!\n");
goto error;
}

/* Account */
if (cgr_msg_push_str(cmsg, "Account", acc) < 0) {
if (cgr_obj_push_str(cmsg->params, "Account", acc) < 0) {
LM_ERR("cannot push Account info!\n");
goto error;
}

/* SetupTime */
if (cgr_msg_push_str(cmsg, "SetupTime", &stime) < 0) {
if (cgr_obj_push_str(cmsg->params, "SetupTime", &stime) < 0) {
LM_ERR("cannot push SetupTime info!\n");
goto error;
}

/* Destination */
if (cgr_msg_push_str(cmsg, "Destination", dst) < 0) {
if (cgr_obj_push_str(cmsg->params, "Destination", dst) < 0) {
LM_ERR("cannot push Destination info!\n");
goto error;
}
Expand Down

0 comments on commit b0aeb0a

Please sign in to comment.