Skip to content

Commit

Permalink
Merge pull request #2393 from sippy/tmerge_20210203
Browse files Browse the repository at this point in the history
Refactor structure(s) packing code to do not put C structures at random unaligned offsets
  • Loading branch information
sobomax committed Feb 8, 2021
2 parents 6aba946 + fe28d3e commit 024b93b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 83 deletions.
27 changes: 15 additions & 12 deletions modules/event_route/route_send.c
Expand Up @@ -36,13 +36,16 @@
int route_build_buffer(str *event_name, evi_reply_sock *sock,
evi_params_t *params, route_send_t **msg)
{
route_send_t *buf;
struct {
route_send_t rt;
evi_param_t eps[0];
} *buf;
evi_param_p param, buf_param;
int len, params_len=0;
unsigned int param_no = 0;
char *s;

len = sizeof(route_send_t) + event_name->len;
len = sizeof(*buf) + event_name->len;
if (params) {
for (param = params->first; param; param = param->next) {
if (param->flags & EVI_INT_VAL) {
Expand All @@ -58,23 +61,23 @@ int route_build_buffer(str *event_name, evi_reply_sock *sock,
}
}

len += sizeof(evi_params_t) + param_no*sizeof(evi_param_t) + params_len;
len += param_no*sizeof(evi_param_t) + params_len;
buf = shm_malloc(len);
if (!buf) {
LM_ERR("oom\n");
return -1;
}
memset(buf, 0, len);

/* First,is event */
buf->event.s = (char*)(buf + 1);
buf->event.len = event_name->len;
memcpy(buf->event.s, event_name->s, event_name->len);
/* Stick the event name at the end */
buf->rt.event.s = (char*)(buf) + len - event_name->len;
buf->rt.event.len = event_name->len;
memcpy(buf->rt.event.s, event_name->s, event_name->len);

if (params) {
buf_param = (evi_param_p)(buf->event.s + buf->event.len);
buf->params.first = buf_param;
s = (char*)(buf_param + param_no);
buf_param = &buf->eps[0];
buf->rt.params.first = buf_param;
s = (char*)(&buf->eps[param_no]);
for (param = params->first; param; param = param->next) {
if (param->flags & EVI_INT_VAL) {
buf_param->flags = EVI_INT_VAL;
Expand Down Expand Up @@ -104,10 +107,10 @@ int route_build_buffer(str *event_name, evi_reply_sock *sock,
}
buf_param--;
buf_param->next = NULL;
buf->params.last = buf_param;
buf->rt.params.last = buf_param;
}

*msg = buf;
*msg = &buf->rt;
return 0;
}

Expand Down
29 changes: 14 additions & 15 deletions modules/mi_fifo/fifo_fnc.c
Expand Up @@ -622,27 +622,26 @@ static void fifo_close_async(mi_response_t *resp, struct mi_handler *hdl, int do

static inline struct mi_handler* build_async_handler(char *name, int len, mi_item_t *id)
{
struct mi_handler *hdl;
struct mi_async_param *p;
char *file;

hdl = (struct mi_handler*)shm_malloc(sizeof(struct mi_handler) +
sizeof(struct mi_async_param) + len + 1);
if (hdl==0) {
struct {
struct mi_handler hdl;
struct mi_async_param p;
char file[0];
} *buf;

buf = shm_malloc(sizeof(*buf) + len + 1);
if (buf == NULL) {
LM_ERR("no more shared memory\n");
return 0;
}
p = (struct mi_async_param*)((char *)hdl + sizeof(struct mi_handler));
file = (char *)(p + 1);
p->file = file;
p->id = shm_clone_mi_item(id);
buf->p.file = buf->file;
buf->p.id = shm_clone_mi_item(id);

memcpy(file, name, len+1 );
memcpy(buf->file, name, len+1 );

hdl->handler_f = fifo_close_async;
hdl->param = (void*)p;
buf->hdl.handler_f = fifo_close_async;
buf->hdl.param = &buf->p;

return hdl;
return &buf->hdl;
}


Expand Down
113 changes: 57 additions & 56 deletions modules/usrloc/ul_evi.c
Expand Up @@ -608,104 +608,105 @@ void ul_raise_ct_refresh_event(const ucontact_t *c, const str *reason,
#if !UL_ASYNC_CT_REFRESH
_ul_raise_ct_refresh_event(c, reason, req_callid);
#else
struct ct_refresh_event_data *ev;
ucontact_t *ct;
struct {
struct ct_refresh_event_data ev;
ucontact_t ct;
str domain;
str aor;
struct socket_info sock[0];
} *buf;
char *p;

/* since we cannot send a (ucontact_t *), we must dup the data */
ev = shm_malloc(sizeof *ev + sizeof *ct + sizeof *c->domain +
c->domain->len + sizeof *c->aor + c->aor->len + c->c.len +
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->callid.len + c->attr.len + c->shtag.len + reason->len +
(req_callid ? req_callid->len : 0));
if (!ev) {
if (buf == NULL) {
LM_ERR("oom\n");
return;
}

p = (char *)(ev + 1);
p = (c->sock) ? (char *)(&buf->sock[1]) + c->sock->sock_str.len :
(char *)(&buf->sock[0]);

ev->reason.s = p;
ev->reason.len = reason->len;
buf->ev.reason.s = p;
buf->ev.reason.len = reason->len;
memcpy(p, reason->s, reason->len);
p += reason->len;

if (!req_callid) {
memset(&ev->req_callid, 0, sizeof ev->req_callid);
memset(&buf->ev.req_callid, 0, sizeof buf->ev.req_callid);
} else {
ev->req_callid.s = p;
ev->req_callid.len = req_callid->len;
buf->ev.req_callid.s = p;
buf->ev.req_callid.len = req_callid->len;
memcpy(p, req_callid->s, req_callid->len);
p += req_callid->len;
}

ct = ev->ct = (ucontact_t *)p;
p = (char *)(ct + 1);
buf->ev.ct = &buf->ct;

ct->domain = (str *)p;
p += sizeof *ct->domain;
buf->ct.domain = &buf->domain;

ct->domain->s = p;
str_cpy(ct->domain, c->domain);
p += ct->domain->len;
buf->domain.s = p;
str_cpy(&buf->domain, c->domain);
p += c->domain->len;

ct->aor = (str *)p;
p += sizeof *ct->aor;
buf->ct.aor = &buf->aor;

ct->aor->s = p;
str_cpy(ct->aor, c->aor);
p += ct->aor->len;
buf->aor.s = p;
str_cpy(&buf->aor, c->aor);
p += c->aor->len;

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

ct->received.s = p;
str_cpy(&ct->received, &c->received);
p += ct->received.len;
buf->ct.received.s = p;
str_cpy(&buf->ct.received, &c->received);
p += c->received.len;

ct->path.s = p;
str_cpy(&ct->path, &c->path);
p += ct->path.len;
buf->ct.path.s = p;
str_cpy(&buf->ct.path, &c->path);
p += c->path.len;

ct->user_agent.s = p;
str_cpy(&ct->user_agent, &c->user_agent);
p += ct->user_agent.len;
buf->ct.user_agent.s = p;
str_cpy(&buf->ct.user_agent, &c->user_agent);
p += c->user_agent.len;

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

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

ct->callid.s = p;
str_cpy(&ct->callid, &c->callid);
p += ct->callid.len;
buf->ct.callid.s = p;
str_cpy(&buf->ct.callid, &c->callid);
p += c->callid.len;

ct->attr.s = p;
str_cpy(&ct->attr, &c->attr);
p += ct->attr.len;
buf->ct.attr.s = p;
str_cpy(&buf->ct.attr, &c->attr);
p += c->attr.len;

if (!c->shtag.s) {
memset(&ct->shtag, 0, sizeof ct->shtag);
memset(&buf->ct.shtag, 0, sizeof buf->ct.shtag);
} else {
ct->shtag.s = p;
str_cpy(&ct->shtag, &c->shtag);
buf->ct.shtag.s = p;
str_cpy(&buf->ct.shtag, &c->shtag);
}

ct->q = c->q;
ct->cflags = c->cflags;
ct->expires = c->expires;
ct->cseq = c->cseq;
ct->sipping_latency = c->sipping_latency;
buf->ct.q = c->q;
buf->ct.cflags = c->cflags;
buf->ct.expires = c->expires;
buf->ct.cseq = c->cseq;
buf->ct.sipping_latency = c->sipping_latency;

if (ipc_dispatch_rpc(ul_rpc_raise_ct_refresh, (void *)ev) != 0) {
if (ipc_dispatch_rpc(ul_rpc_raise_ct_refresh, (void *)&buf->ev) != 0) {
LM_ERR("failed to send RPC for "UL_EV_CT_REFRESH"\n");
return;
}
Expand Down

0 comments on commit 024b93b

Please sign in to comment.