Skip to content

Commit

Permalink
aaa_diameter: make Transaction-Id optional if Session-Id is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed May 8, 2023
1 parent 008b861 commit ba06123
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 28 deletions.
30 changes: 21 additions & 9 deletions modules/aaa_diameter/aaa_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,19 +374,31 @@ static int dm_custom_cmd_reply(struct msg **_msg, struct avp * avp, struct sessi
goto out;
}

rc = fd_msg_search_avp(msg, dm_dict.Transaction_Id, &a);
rc = fd_msg_search_avp(msg, dm_dict.Session_Id, &a);
if (rc != 0) {
LM_WARN("Missing Transaction-Id AVP in Diameter Answer %d/%d\n",
LM_DBG("Missing Session-Id AVP in Diameter Answer %d/%d, looking for Transaction-Id\n",
hdr->msg_appl, hdr->msg_code);
goto out;
}
rc = fd_msg_search_avp(msg, dm_dict.Transaction_Id, &a);
if (rc != 0) {
LM_WARN("Missing Transaction-Id AVP in Diameter Answer %d/%d\n",
hdr->msg_appl, hdr->msg_code);
goto out;
}

FD_CHECK_GT(fd_msg_avp_hdr(a, &h));
tid.s = (char *)h->avp_value->os.data;
tid.len = (int)h->avp_value->os.len;

FD_CHECK_GT(fd_msg_avp_hdr(a, &h));
tid.s = (char *)h->avp_value->os.data;
tid.len = (int)h->avp_value->os.len;
LM_DBG("%d/%d reply %d, Transaction-Id: %.*s\n", hdr->msg_appl,
hdr->msg_code, rc, tid.len, tid.s);
} else {
FD_CHECK_GT(fd_msg_avp_hdr(a, &h));
tid.s = (char *)h->avp_value->os.data;
tid.len = (int)h->avp_value->os.len;

LM_DBG("%d/%d reply %d, Transaction-Id: %.*s\n", hdr->msg_appl,
hdr->msg_code, rc, tid.len, tid.s);
LM_DBG("%d/%d reply %d, Session-Id: %.*s\n", hdr->msg_appl,
hdr->msg_code, rc, tid.len, tid.s);
}

prpl_cond = (struct dm_cond **)hash_find_key(pending_replies, tid);
if (!prpl_cond) {
Expand Down
1 change: 1 addition & 0 deletions modules/aaa_diameter/aaa_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct _dm_dict {
struct dict_object *SIP_Method;

struct dict_object *Transaction_Id;
struct dict_object *Session_Id;
struct dict_object *Event_Timestamp;
struct dict_object *Route_Record;
};
Expand Down
2 changes: 2 additions & 0 deletions modules/aaa_diameter/app_opensips/app_opensips.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static struct {
struct dict_object * Acct_Session_Id;
struct dict_object * Event_Timestamp;
struct dict_object * Transaction_Id;
struct dict_object * Session_Id;

struct dict_object * Auth_Application_Id;
struct dict_object * Auth_Session_State;
Expand Down Expand Up @@ -726,6 +727,7 @@ static int os_entry(char *confstring)
CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Acct-Session-Id", &dm_dict.Acct_Session_Id, ENOENT) );
CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Event-Timestamp", &dm_dict.Event_Timestamp, ENOENT) );
CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Transaction-Id", &dm_dict.Transaction_Id, ENOENT) );
CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &dm_dict.Session_Id, ENOENT) );

CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Application-Id", &dm_dict.Auth_Application_Id, ENOENT) );
CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Session-State", &dm_dict.Auth_Session_State, ENOENT) );
Expand Down
38 changes: 24 additions & 14 deletions modules/aaa_diameter/app_opensips/avps.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,33 +962,43 @@ int parse_command_def(char *line, FILE *fp, int cmd_type)
}

{
/* all custom requests and replies MUST include Transaction-Id */
/* all custom requests and replies MUST include Transaction-Id
* but only if they they don't require a Session-Id already */
struct dict_rule_data data = {NULL, RULE_REQUIRED, 0, -1, 1};

FD_CHECK(fd_dict_search(fd_g_config->cnf_dict,
DICT_AVP, AVP_BY_NAME, "Transaction-Id", &data.rule_avp, 0));

DICT_AVP, AVP_BY_NAME, "Session-Id", &data.rule_avp, 0));
if (!data.rule_avp) {
printf("ERROR: failed to locate Transaction-Id AVP\n");
return -1;
}
FD_CHECK(fd_dict_search(fd_g_config->cnf_dict,
DICT_AVP, AVP_BY_NAME, "Transaction-Id", &data.rule_avp, 0));

FD_CHECK_dict_new(DICT_RULE, &data, cmd, NULL);
if (!data.rule_avp) {
printf("ERROR: failed to locate Transaction-Id AVP\n");
return -1;
}

FD_CHECK_dict_new(DICT_RULE, &data, cmd, NULL);
}
}

/* all replies MUST include a Result-Code */
/* all replies MUST include a Result-Code
* but only if they they don't require an Experimental-Result already */
if (cmd_type == CMD_ANSWER) {
struct dict_rule_data data = {NULL, RULE_REQUIRED, 0, -1, 1};

FD_CHECK(fd_dict_search(fd_g_config->cnf_dict,
DICT_AVP, AVP_BY_NAME, "Result-Code", &data.rule_avp, 0));

DICT_AVP, AVP_BY_NAME, "Experimental-Result", &data.rule_avp, 0));
if (!data.rule_avp) {
printf("ERROR: failed to locate Result-Code AVP\n");
return -1;
}
FD_CHECK(fd_dict_search(fd_g_config->cnf_dict,
DICT_AVP, AVP_BY_NAME, "Result-Code", &data.rule_avp, 0));

FD_CHECK_dict_new(DICT_RULE, &data, cmd, NULL);
if (!data.rule_avp) {
printf("ERROR: failed to locate Result-Code AVP\n");
return -1;
}

FD_CHECK_dict_new(DICT_RULE, &data, cmd, NULL);
}
}

return 0;
Expand Down
19 changes: 14 additions & 5 deletions modules/aaa_diameter/peer.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,13 @@ static int dm_pack_avps(void *root, struct list_head *subavps)

static int dm_custom_req(struct dm_message *msg)
{
str tid_str;
struct msg *dmsg;
struct avp *avp;
struct avp_hdr *h;
union avp_value val;
struct dict_object *req; /* a custom Diameter request */
int rc;

FD_CHECK(fd_dict_search(fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_CODE_R,
&msg->cmd_code, &req, ENOENT));
Expand All @@ -470,12 +473,13 @@ static int dm_custom_req(struct dm_message *msg)
LM_ERR("failed to pack AVPs\n");
return -1;
}

/* Transaction-Id */
{
/* check if we already have a Session-Id in the message - if so, use it! */
rc = fd_msg_search_avp(dmsg, dm_dict.Session_Id, &avp);
if (rc != 0) {
/* Transaction-Id */
struct timeval now;
char tid[16 + 1];
str tid_str;
LM_DBG("No Session-Id in Answer, forcing Transaction-Id\n");

FD_CHECK(fd_msg_avp_new(dm_dict.Transaction_Id, 0, &avp));

Expand All @@ -489,8 +493,11 @@ static int dm_custom_req(struct dm_message *msg)
FD_CHECK(fd_msg_avp_add(dmsg, MSG_BRW_LAST_CHILD, avp));

tid_str = (str){(char *)val.os.data, val.os.len};
FD_CHECK(dm_add_pending_reply(&tid_str, msg->reply_cond));
} else {
FD_CHECK(fd_msg_avp_hdr(avp, &h));
tid_str = (str){(char *)h->avp_value->os.data, h->avp_value->os.len};
}
FD_CHECK(dm_add_pending_reply(&tid_str, msg->reply_cond));

FD_CHECK(fd_msg_send(&dmsg, NULL, NULL));
return 0;
Expand Down Expand Up @@ -549,6 +556,8 @@ static int dm_prepare_globals(void)

FD_CHECK(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME,
"Transaction-Id", &dm_dict.Transaction_Id, ENOENT));
FD_CHECK(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME,
"Session-Id", &dm_dict.Session_Id, ENOENT));
FD_CHECK(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME,
"Route-Record", &dm_dict.Route_Record, ENOENT));

Expand Down

0 comments on commit ba06123

Please sign in to comment.