Skip to content

Commit

Permalink
aaa_diameter: Code + filename improvements; Improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Feb 27, 2024
1 parent b43964f commit ca0006b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion locking.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* Implements (in lock_ops.h & lock_alloc.h):
*
* simple locks:
* simple, thread-safe locks:
* - type: gen_lock_t
* - gen_lock_t* lock_alloc(); - allocates a lock in shared mem.
* - gen_lock_t* lock_init(gen_lock_t* lock); - inits the lock
Expand Down
31 changes: 18 additions & 13 deletions modules/aaa_diameter/aaa_diameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
#include "../../async.h"
#include "../../ut.h"

#include "aaa_impl.h"
#include "aaa_evi.h"
#include "peer.h"
#include "dm_impl.h"
#include "dm_evi.h"
#include "dm_peer.h"

static int mod_init(void);
static int child_init(int rank);
Expand Down Expand Up @@ -76,7 +76,7 @@ static const acmd_export_t acmds[]= {
};

static const proc_export_t procs[] = {
{ "diameter-peer", NULL, NULL, diameter_peer_loop, 1, 0 },
{ "diameter-peer", NULL, NULL, dm_peer_loop, 1, 0 },
{ 0, 0, 0, 0, 0, 0 },
};

Expand Down Expand Up @@ -323,6 +323,11 @@ static int dm_send_answer(struct sip_msg *msg, str *avp_json)
int appid, cmdcode, rc;
unsigned long fd_req;

if (route_type != EVENT_ROUTE) {
LM_ERR("can only run 'dm_send_answer()' inside an EVENT_ROUTE\n");
return -1;
}

if (ZSTRP(avp_json)) {
LM_ERR("unable to build reply (NULL 'avps_json' input)\n");
return -1;
Expand All @@ -347,15 +352,6 @@ static int dm_send_answer(struct sip_msg *msg, str *avp_json)
evp.pvn.type = PV_NAME_INTSTR;
evp.pvn.u.isname.type = AVP_NAME_STR;

evp.pvn.u.isname.name.s = dmev_req_pname_sessid;
route_params_run(msg, &evp, &res);
if (ZSTR(res.rs) || !pvv_is_str(&res)) {
LM_ERR("failed to fetch unique session ID\n");
sessid = STR_NULL;
} else {
sessid = res.rs;
}

evp.pvn.u.isname.name.s = dmev_req_pname_appid;
route_params_run(msg, &evp, &res);
if (!pvv_is_int(&res)) {
Expand Down Expand Up @@ -398,6 +394,15 @@ static int dm_send_answer(struct sip_msg *msg, str *avp_json)

rc = _dm_send_message(NULL, dmsg, NULL, NULL);
if (rc < 0) {
evp.pvn.u.isname.name.s = dmev_req_pname_sessid;
route_params_run(msg, &evp, &res);
if (ZSTR(res.rs) || !pvv_is_str(&res)) {
LM_DBG("failed to fetch the unique session ID\n");
sessid = STR_NULL;
} else {
sessid = res.rs;
}

LM_ERR("failed to send Diameter reply (sess: %.*s, app: %d, cmd: %d)\n",
sessid.len, sessid.s, appid, cmdcode);
cJSON_Delete(avps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
*/

#include "aaa_evi.h"
#include "dm_evi.h"

#include "../../dprint.h"
#include "../../ut.h"
Expand Down Expand Up @@ -123,6 +123,11 @@ int dm_dispatch_event_req(struct msg *msg, const str *sessid, int app_id,
}


/**
* The purpose of this dispatched job is for the logic to be ran by a
* process other than the Diameter peer, since PROC_MODULE workers have NULL
* @sroutes, causing a crash when attempting to raise a script event
*/
void dm_raise_event_request(int sender, void *dm_req)
{
char buf[sizeof(long)*2 + 1], *p = buf;
Expand All @@ -134,7 +139,6 @@ void dm_raise_event_request(int sender, void *dm_req)
LM_DBG("received Diameter request via IPC, tid: %.*s\n",
job->sessid.len, job->sessid.s);

/* raise the event to script (dispatch) */
if (evi_param_set_str(dmev_req_param_sessid, &job->sessid) < 0) {
LM_ERR("failed to set 'sess_id'\n");
goto out;
Expand Down
File renamed without changes.
26 changes: 13 additions & 13 deletions modules/aaa_diameter/aaa_impl.c → modules/aaa_diameter/dm_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
#include "../../evi/evi_modules.h"
#include "../../ipc.h"

#include "aaa_impl.h"
#include "aaa_evi.h"
#include "peer.h"
#include "dm_impl.h"
#include "dm_evi.h"
#include "dm_peer.h"
#include "app_opensips/avps.h"

struct local_rules_definition {
Expand Down Expand Up @@ -505,14 +505,14 @@ static int dm_avps2json(void *root, cJSON *avps)
}


static int dm_receive_req(struct msg **_msg, struct avp * avp, struct session * sess, void * data, enum disp_action * act)
static int dm_receive_req(struct msg **_req, struct avp * avp, struct session * sess, void * data, enum disp_action * act)
{
cJSON *avps = NULL, *it;
struct msg *msg = *_msg;
struct msg *req = *_req;
struct msg_hdr *hdr = NULL;
str tid = STR_NULL, avp_arr = STR_NULL;

FD_CHECK(fd_msg_hdr(msg, &hdr));
FD_CHECK(fd_msg_hdr(req, &hdr));
LM_DBG("received Diameter request (appl: %u, cmd: %u)\n", hdr->msg_appl, hdr->msg_code);

cJSON_InitHooks(&shm_mem_hooks);
Expand All @@ -522,7 +522,7 @@ static int dm_receive_req(struct msg **_msg, struct avp * avp, struct session *
goto error;
}

if (dm_avps2json(msg, avps) != 0) {
if (dm_avps2json(req, avps) != 0) {
LM_ERR("failed to pack request AVPs as JSON string\n");
goto error;
}
Expand All @@ -548,22 +548,22 @@ static int dm_receive_req(struct msg **_msg, struct avp * avp, struct session *
init_str(&avp_arr, cJSON_PrintUnformatted(avps));

/* keep the request for a while in order to be able to generate the answer */
dm_update_unreplied_req(msg);
dm_update_unreplied_req(req);

if (dm_dispatch_event_req(msg, &tid, hdr->msg_appl, hdr->msg_code, &avp_arr))
if (dm_dispatch_event_req(req, &tid, hdr->msg_appl, hdr->msg_code, &avp_arr))
LM_ERR("failed to dispatch DM Request (tid: %.*s, %d/%d)\n", tid.len,
tid.s, hdr->msg_appl, hdr->msg_code);

goto out;

error:
FD_CHECK(fd_msg_free(msg));
FD_CHECK(fd_msg_free(req));
out:
cJSON_PurgeString(avp_arr.s);
cJSON_Delete(avps);
cJSON_InitHooks(NULL);

*_msg = NULL;
*_req = NULL;
*act = DISP_ACT_CONT;
return 0;
}
Expand Down Expand Up @@ -1899,8 +1899,8 @@ int _dm_send_message_async(aaa_conn *_, aaa_message *req, int *fd)
int _dm_get_message_response(struct dm_cond *cond, char **rpl_avps)
{

LM_DBG("reply received, Result-Code: %d (%s)\n", cond->rc,
cond->is_error ? "FAILURE" : "SUCCESS");
LM_DBG("reply received, Result-Code: %d, is_error: %d\n", cond->rc,
cond->is_error);
LM_DBG("AVPs: %s\n", cond->rpl_avps_json);

if (rpl_avps)
Expand Down
File renamed without changes.
11 changes: 6 additions & 5 deletions modules/aaa_diameter/peer.c → modules/aaa_diameter/dm_peer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#include "../../locking.h"
#include "../../lib/list.h"

#include "aaa_impl.h"
#include "peer.h"
#include "dm_impl.h"
#include "dm_peer.h"
#include "app_opensips/avps.h"

#define EVENT_RECORD 1
Expand All @@ -41,6 +41,7 @@ pthread_mutex_t *msg_send_lk;
extern str dm_realm;
extern str dm_peer_identity;


int dm_init_peer(void)
{
struct {
Expand Down Expand Up @@ -548,7 +549,7 @@ static int dm_custom_rpl(struct dm_message *dm)
}


static inline int diameter_send_msg(struct dm_message *msg)
static inline int dm_peer_send_msg(struct dm_message *msg)
{
aaa_message *am = msg->am;

Expand Down Expand Up @@ -611,7 +612,7 @@ static int dm_prepare_globals(void)
}


void diameter_peer_loop(int _)
void dm_peer_loop(int _)
{
struct dm_message *msg;

Expand Down Expand Up @@ -650,7 +651,7 @@ void diameter_peer_loop(int _)
msg = list_entry(msg_send_queue->next, struct dm_message, list);
list_del(&msg->list);

if (diameter_send_msg(msg) != 0)
if (dm_peer_send_msg(msg) != 0)
LM_ERR("failed to send message\n");
else
LM_DBG("successfully sent\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ extern pthread_cond_t *msg_send_cond;
extern pthread_mutex_t *msg_send_lk;

int dm_init_peer(void);
void diameter_peer_loop(int _);
void dm_peer_loop(int _);

#endif

0 comments on commit ca0006b

Please sign in to comment.