Skip to content

Commit

Permalink
[evi] do not raise event from TCP MAIN
Browse files Browse the repository at this point in the history
The TCP MAIN process does not have script capabilities, so we should NOT raise event here (as they may translate into scripting due to the event_route module). So, if we have an event to be raised from TCP MAIN (as E_CORE_TCP_DISCONNECT) we better use the newly added evi_dispatch_event() function which does IPC dispatching (into a different process) and raise for the event.
Closes #3212

(cherry picked from commit fd06576)
  • Loading branch information
bogdan-iancu committed Oct 9, 2023
1 parent df1f73e commit 5508b95
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 29 deletions.
69 changes: 43 additions & 26 deletions evi/event_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -897,20 +897,57 @@ evi_params_p mi_raise_event_array_params(mi_item_t *array, int no)
return NULL;
}

struct mi_raise_event_dispatch {
struct rpc_raise_event_dispatch {
event_id_t id;
evi_params_p params;
};

void mi_raise_event_rpc(int sender, void *param)

void rpc_raise_event(int sender, void *param)
{
struct mi_raise_event_dispatch *p = (struct mi_raise_event_dispatch *)param;
struct rpc_raise_event_dispatch *p = (struct rpc_raise_event_dispatch *)param;
if (evi_raise_event(p->id, p->params))
LM_ERR("cannot raise event RPC\n");
evi_free_shm_params(p->params);
shm_free(p);
}


int evi_dispatch_event( event_id_t id, evi_params_p params)
{
evi_params_p sparams = NULL;
struct rpc_raise_event_dispatch *djob;

if (params) {
sparams = evi_dup_shm_params(params);
evi_free_params(params);
if (!sparams) {
LM_ERR("could not shm duplicate evi params!\n");
goto error;
}
}

djob = shm_malloc(sizeof (*djob));
if (!djob) {
LM_ERR("could not allocate new job!\n");
goto error;
}
djob->id = id;
djob->params = sparams;

if (ipc_dispatch_rpc(rpc_raise_event, djob) < 0) {
LM_ERR("could not dispatch raise event job!\n");
goto error;
}

return 0;
error:
if (sparams)
evi_free_shm_params(sparams);
return -1;
}


mi_response_t *w_mi_raise_event(const mi_params_t *params,
struct mi_handler *async_hdl)
{
Expand All @@ -919,8 +956,7 @@ mi_response_t *w_mi_raise_event(const mi_params_t *params,
str tparams;
event_id_t id;
mi_item_t *values;
evi_params_p eparams = NULL, sparams;
struct mi_raise_event_dispatch *djob;
evi_params_p eparams = NULL;

if (get_mi_string_param(params, "event", &event_s.s, &event_s.len) < 0)
return init_mi_param_error();
Expand Down Expand Up @@ -955,33 +991,14 @@ mi_response_t *w_mi_raise_event(const mi_params_t *params,
break;
}

if (eparams) {
sparams = evi_dup_shm_params(eparams);
evi_free_params(eparams);
eparams = NULL;
if (!sparams) {
LM_ERR("could not duplicate evi params!\n");
goto error;
}
eparams = sparams;
}

djob = shm_malloc(sizeof (*djob));
if (!djob) {
LM_ERR("could not allocate new job!\n");
goto error;
}
djob->id = id;
djob->params = eparams;

if (ipc_dispatch_rpc(mi_raise_event_rpc, djob) < 0) {
if (evi_dispatch_event( id, eparams)<0) {
LM_ERR("could not dispatch raise event job!\n");
goto error;
}

return init_mi_result_ok();
error:
if (eparams)
evi_free_shm_params(eparams);
evi_free_params(eparams);
return init_mi_error(500, MI_SSTR("Cannot Raise Event"));
}
13 changes: 13 additions & 0 deletions evi/evi_modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ event_id_t evi_publish_event(str event_name);
*/
int evi_raise_event(event_id_t id, evi_params_t* params);


/*
* Used to dispatch and raise an event into an different process
* Parameters:
* + event id
* + parameters
*
* Returns:
* - 0 on success or negative on error
*/
int evi_dispatch_event(event_id_t id, evi_params_t* params);


/*
* Used to raise an event that uses the message attached
* Parameters:
Expand Down
12 changes: 9 additions & 3 deletions net/net_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ static str e_tcp_dst_ip = str_init("dst_ip");
static str e_tcp_dst_port = str_init("dst_port");
static str e_tcp_c_proto = str_init("proto");

void tcp_disconnect_event_raise(struct tcp_connection* c)
static void tcp_disconnect_event_raise(struct tcp_connection* c)
{
evi_params_p list = 0;
str src_ip,dst_ip, proto;
Expand Down Expand Up @@ -681,8 +681,14 @@ void tcp_disconnect_event_raise(struct tcp_connection* c)
goto end;
}

if (evi_raise_event(EVI_TCP_DISCONNECT, list)) {
LM_ERR("unable to send tcp disconnect event\n");
if (is_tcp_main) {
if (evi_dispatch_event(EVI_TCP_DISCONNECT, list)) {
LM_ERR("unable to dispatch tcp disconnect event\n");
}
} else {
if (evi_raise_event(EVI_TCP_DISCONNECT, list)) {
LM_ERR("unable to send tcp disconnect event\n");
}
}
list = 0;

Expand Down

0 comments on commit 5508b95

Please sign in to comment.