Skip to content

Commit

Permalink
usrloc: simplify processing of the refresh events
Browse files Browse the repository at this point in the history
Do not abuse "struct socket_info" just to pass sock_str over.
Instead, add str to the refresh event data and pass that alone.
Not only it reduces amount of data to pass, but also makes
code much easier to read and understand.
  • Loading branch information
sobomax committed Dec 3, 2023
1 parent 42d83f4 commit 04cb08e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
55 changes: 32 additions & 23 deletions modules/usrloc/ul_evi.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,12 @@ void ul_raise_contact_event(event_id_t _e, const ucontact_t *_c)
}


static inline void _ul_raise_ct_refresh_event(
const ucontact_t *_c, const str *reason, const str *req_callid)
static inline void _ul_raise_ct_refresh_event(const struct ct_refresh_event_data *ev)
{
const ucontact_t *_c = ev->ct;
const str *reason = &ev->reason;
const str *req_callid = &ev->req_callid;

if (ei_c_refresh_id == EVI_ERROR) {
LM_ERR("event not yet registered ("UL_EV_CT_REFRESH")\n");
return;
Expand Down Expand Up @@ -537,8 +540,7 @@ static inline void _ul_raise_ct_refresh_event(
}

/* the socket */
if (evi_param_set_str(ul_ct_pn_event.socket,
(_c->sock ? &_c->sock->sock_str : _str(""))) < 0) {
if (evi_param_set_str(ul_ct_pn_event.socket, &ev->sock_str) < 0) {
LM_ERR("cannot set socket parameter\n");
return;
}
Expand Down Expand Up @@ -593,42 +595,52 @@ static inline void _ul_raise_ct_refresh_event(
}


#if UL_ASYNC_CT_REFRESH
struct refresh_event_rpc_data {
struct ct_refresh_event_data ev;
ucontact_t ct;
str domain;
str aor;
char _stor[0];
};

static void ul_rpc_raise_ct_refresh(int _, void *_ev)
{
struct ct_refresh_event_data *ev = (struct ct_refresh_event_data *)_ev;
struct refresh_event_rpc_data *rev = (struct refresh_event_rpc_data*)_ev;

_ul_raise_ct_refresh_event(ev->ct, &ev->reason, &ev->req_callid);
shm_free(ev);
_ul_raise_ct_refresh_event(&rev->ev);
shm_free(rev);
}
#endif


void ul_raise_ct_refresh_event(const ucontact_t *c, const str *reason,
const str *req_callid)
{
#if !UL_ASYNC_CT_REFRESH
_ul_raise_ct_refresh_event(c, reason, req_callid);
const struct ct_refresh_event_data ev = {
.ct = c,
.reason = *reason,
.req_callid = *req_callid,
.sock_str = (c->sock != NULL) ? c->sock->sock_str : (str){0},
};
_ul_raise_ct_refresh_event(&ev);
#else
struct {
struct ct_refresh_event_data ev;
ucontact_t ct;
str domain;
str aor;
struct socket_info sock[0];
} *buf;
struct refresh_event_rpc_data *buf;
char *p;

/* since we cannot send a (ucontact_t *), we must dup the data */
buf = shm_malloc(sizeof(*buf) + c->domain->len + c->aor->len + c->c.len +
c->received.len + c->path.len + c->user_agent.len +
(c->sock ? (sizeof *c->sock + c->sock->sock_str.len) : 0) +
(c->sock ? c->sock->sock_str.len : 0) +
c->callid.len + c->attr.len + c->shtag.len + reason->len +
(req_callid ? req_callid->len : 0));
if (buf == NULL) {
LM_ERR("oom\n");
return;
}

p = (c->sock) ? (char *)(&buf->sock[1]) : (char *)(&buf->sock[0]);
p = buf->_stor;

buf->ev.reason.s = p;
buf->ev.reason.len = reason->len;
Expand Down Expand Up @@ -675,13 +687,10 @@ void ul_raise_ct_refresh_event(const ucontact_t *c, const str *reason,
p += c->user_agent.len;

if (!c->sock) {
buf->ct.sock = NULL;
buf->ev.sock_str = (str){0};
} else {
struct socket_info *sock = &buf->sock[0];

sock->sock_str.s = p;
str_cpy(&sock->sock_str, &c->sock->sock_str);
buf->ct.sock = sock;
buf->ev.sock_str.s = p;
str_cpy(&buf->ev.sock_str, &c->sock->sock_str);
p += c->sock->sock_str.len;
}

Expand Down
3 changes: 2 additions & 1 deletion modules/usrloc/ul_evi.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@
#define UL_EV_PARAM_CT_RCLID "req_callid"

struct ct_refresh_event_data {
ucontact_t *ct;
const ucontact_t *ct;
str reason;
str req_callid;
str sock_str;
};

/* AoR event IDs */
Expand Down

0 comments on commit 04cb08e

Please sign in to comment.