Skip to content

Commit

Permalink
presence: use separate structure to match presentity to replace or up…
Browse files Browse the repository at this point in the history
…date
  • Loading branch information
miconda committed Apr 22, 2020
1 parent 8077f38 commit eb5c1a4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 40 deletions.
28 changes: 17 additions & 11 deletions src/modules/presence/hash.c
Expand Up @@ -1010,22 +1010,25 @@ int ps_ptable_insert(ps_presentity_t *pt)
/**
*
*/
int ps_ptable_replace(ps_presentity_t *pt)
int ps_ptable_replace(ps_presentity_t *ptm, ps_presentity_t *pt)
{
ps_presentity_t ptc;
ps_presentity_t ptv;
ps_presentity_t *ptn = NULL;
uint32_t idx = 0;

/* copy struct to fill in missing fields */
memcpy(&ptc, pt, sizeof(ps_presentity_t));
memcpy(&ptc, ptm, sizeof(ps_presentity_t));
memcpy(&ptv, pt, sizeof(ps_presentity_t));

ptc.hashid = core_case_hash(&pt->user, &pt->domain, 0);
ptv.hashid = core_case_hash(&pt->user, &pt->domain, 0);

if(ptc.ruid.s == NULL) {
if(ptv.ruid.s == NULL) {
if(sruid_next(&pres_sruid) < 0) {
return -1;
}
ptc.ruid = pres_sruid.uid;
ptv.ruid = pres_sruid.uid;
}

idx = core_hash_idx(ptc.hashid, _ps_ptable->ssize);
Expand All @@ -1051,7 +1054,7 @@ int ps_ptable_replace(ps_presentity_t *pt)
ps_presentity_free(ptn, 0);
}

ptn = ps_presentity_new(&ptc, 0);
ptn = ps_presentity_new(&ptv, 0);
if(ptn==NULL) {
lock_release(&_ps_ptable->slots[idx].lock);
return -1;
Expand All @@ -1072,22 +1075,25 @@ int ps_ptable_replace(ps_presentity_t *pt)
/**
*
*/
int ps_ptable_update(ps_presentity_t *pt)
int ps_ptable_update(ps_presentity_t *ptm, ps_presentity_t *pt)
{
ps_presentity_t ptc;
ps_presentity_t ptv;
ps_presentity_t *ptn = NULL;
uint32_t idx = 0;

/* copy struct to fill in missing fields */
memcpy(&ptc, pt, sizeof(ps_presentity_t));
memcpy(&ptc, ptm, sizeof(ps_presentity_t));
memcpy(&ptv, pt, sizeof(ps_presentity_t));

ptc.hashid = core_case_hash(&pt->user, &pt->domain, 0);
ptc.hashid = core_case_hash(&ptm->user, &ptm->domain, 0);
ptv.hashid = core_case_hash(&pt->user, &pt->domain, 0);

if(ptc.ruid.s == NULL) {
if(ptv.ruid.s == NULL) {
if(sruid_next(&pres_sruid) < 0) {
return -1;
}
ptc.ruid = pres_sruid.uid;
ptv.ruid = pres_sruid.uid;
}

idx = core_hash_idx(ptc.hashid, _ps_ptable->ssize);
Expand Down Expand Up @@ -1115,7 +1121,7 @@ int ps_ptable_update(ps_presentity_t *pt)
}
ps_presentity_free(ptn, 0);

ptn = ps_presentity_new(&ptc, 0);
ptn = ps_presentity_new(&ptv, 0);
if(ptn==NULL) {
lock_release(&_ps_ptable->slots[idx].lock);
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/presence/hash.h
Expand Up @@ -176,8 +176,8 @@ int ps_presentity_match(ps_presentity_t *pta, ps_presentity_t *ptb, int mmode);
int ps_ptable_init(int ssize);
void ps_ptable_destroy(void);
int ps_ptable_insert(ps_presentity_t *pt);
int ps_ptable_replace(ps_presentity_t *pt);
int ps_ptable_update(ps_presentity_t *pt);
int ps_ptable_replace(ps_presentity_t *ptm, ps_presentity_t *pt);
int ps_ptable_update(ps_presentity_t *ptm, ps_presentity_t *pt);
int ps_ptable_remove(ps_presentity_t *pt);
ps_presentity_t *ps_ptable_get_list(str *user, str *domain);
ps_presentity_t *ps_ptable_get_item(str *user, str *domain, str *event, str *etag);
Expand Down
39 changes: 25 additions & 14 deletions src/modules/presence/presentity.c
Expand Up @@ -1316,12 +1316,14 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
str crt_ruid = STR_NULL;
str p_ruid = STR_NULL;
ps_presentity_t ptc;
ps_presentity_t ptm;
ps_presentity_t *ptx = NULL;

if(sent_reply) {
*sent_reply = 0;
}
memset(&ptc, 0, sizeof(ps_presentity_t));
memset(&ptm, 0, sizeof(ps_presentity_t));

/* here pres_notifier_processes == 0 -- used for db-only */
if(presentity->event->req_auth) {
Expand All @@ -1344,6 +1346,11 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
ptc.event = presentity->event->name;
ptc.etag = presentity->etag;

ptm.user = presentity->user;
ptm.domain = presentity->domain;
ptm.event = presentity->event->name;
ptm.etag = presentity->etag;

if(new_t) {
LM_DBG("new presentity with etag %.*s\n", presentity->etag.len,
presentity->etag.s);
Expand Down Expand Up @@ -1402,7 +1409,7 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
ptc.expires = presentity->expires + (int)time(NULL);
}
/* update/replace in memory */
if(ps_ptable_replace(&ptc) <0) {
if(ps_ptable_replace(&ptm, &ptc) <0) {
LM_ERR("replacing record in database\n");
goto error;
}
Expand All @@ -1423,9 +1430,10 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
p_ruid = *ruid;
}
if(EVENT_DIALOG_SLA(presentity->event->evp)) {
ptx = ps_ptable_get_item(&ptc.user, &ptc.domain, &ptc.event,
&ptc.etag);
ptx = ps_ptable_get_item(&ptm.user, &ptm.domain, &ptm.event,
&ptm.etag);
if(ptx == NULL) {
LM_DBG("presentity record not found\n");
goto send_412;
}
cache_record_exists = 1;
Expand Down Expand Up @@ -1497,9 +1505,10 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
if(presentity->expires <= 0) {

if(!cache_record_exists) {
ptx = ps_ptable_get_item(&ptc.user, &ptc.domain, &ptc.event,
&ptc.etag);
ptx = ps_ptable_get_item(&ptm.user, &ptm.domain, &ptm.event,
&ptm.etag);
if(ptx == NULL) {
LM_DBG("presentity record not found\n");
goto send_412;
}
cache_record_exists = 1;
Expand Down Expand Up @@ -1560,6 +1569,9 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
}

if(presentity->event->etag_not_new == 0 || etag_override) {
unsigned int publ_nr;
str str_publ_nr = {0, 0};

if(etag_override) {
/* use the supplied etag */
LM_DBG("updating with supplied etag %.*s\n", etag_override->len,
Expand All @@ -1569,8 +1581,7 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
}

/* generate another etag */
unsigned int publ_nr;
str str_publ_nr = {0, 0};
LM_DBG("generating a new etag (%d)\n", presentity->event->etag_not_new);

dot = presentity->etag.s + presentity->etag.len;
while(*dot != '.' && str_publ_nr.len < presentity->etag.len) {
Expand Down Expand Up @@ -1634,13 +1645,13 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
* or dmq replication is enabled and we don't already know the ruid, do query */
if((!cache_record_exists)
|| (pres_enable_dmq > 0 && !p_ruid.s)) {
ptx = ps_ptable_get_item(&ptc.user, &ptc.domain, &ptc.event,
&ptc.etag);
ptx = ps_ptable_get_item(&ptm.user, &ptm.domain, &ptm.event,
&ptm.etag);
if(ptx == NULL) {
LM_DBG("presentity record not found\n");
goto send_412;
}
cache_record_exists = 1;
affected_rows = 1;
if(!p_ruid.s && ptx->ruid.s) {
crt_ruid.len = ptx->ruid.len;
crt_ruid.s = (char *)pkg_malloc(sizeof(char) * crt_ruid.len);
Expand All @@ -1655,14 +1666,14 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
ps_presentity_free(ptx, 1);
ptx = NULL;
}
affected_rows = ps_ptable_update(&ptc);
affected_rows = ps_ptable_update(&ptm, &ptc);
if(affected_rows < 0) {
LM_ERR("updating published info in database\n");
goto error;
}
affected_rows = 1;
/* if either affected_rows (if exists) or select query show that there is no line in database*/
if((!affected_rows && !cache_record_exists) || (!cache_record_exists)) {
if(affected_rows==0) {
LM_DBG("no presentity record found to be updated\n");
goto send_412;
}

Expand Down Expand Up @@ -1780,7 +1791,7 @@ int update_presentity(sip_msg_t *msg, presentity_t *presentity, str *body,
/**
*
*/
int pres_htable_restore(void)
int pres_htable_db_restore(void)
{
/* query all records from presentity table and insert records
* in presentity table */
Expand Down
2 changes: 1 addition & 1 deletion src/modules/presence/presentity.h
Expand Up @@ -66,7 +66,7 @@ void free_presentity(presentity_t *p);

char *generate_ETag(int publ_count);

int pres_htable_restore(void);
int pres_htable_db_restore(void);

char *extract_sphere(str *body);

Expand Down
34 changes: 22 additions & 12 deletions src/modules/presence/publish.c
Expand Up @@ -381,8 +381,9 @@ int ki_handle_publish_uri(struct sip_msg *msg, str *sender_uri)
reply_str = pu_400a_rpl;
goto error;
}
} else
} else {
goto unsupported_event;
}

/* search event in the list */
event = search_event((event_t *)msg->event->parsed);
Expand Down Expand Up @@ -472,7 +473,7 @@ int ki_handle_publish_uri(struct sip_msg *msg, str *sender_uri)
reply_str = pu_400a_rpl;
goto error;
}
body.len = get_content_length(msg);
body.len = msg->buf + msg->len - body.s;

if(pres_sphere_enable && event->evp->type == EVENT_PRESENCE
&& get_content_type(msg) == SUBTYPE_PIDFXML) {
Expand Down Expand Up @@ -512,7 +513,7 @@ int ki_handle_publish_uri(struct sip_msg *msg, str *sender_uri)
}

/* now we have all the necessary values */
/* fill in the filds of the structure */
/* fill in the fields of the structure */

presentity = new_presentity(
&pres_domain, &pres_user, lexpire, event, &etag, sender);
Expand All @@ -528,23 +529,28 @@ int ki_handle_publish_uri(struct sip_msg *msg, str *sender_uri)
goto error;
}

if(presentity)
if(presentity) {
pkg_free(presentity);
if(etag.s)
}
if(etag.s) {
pkg_free(etag.s);
if(sender)
}
if(sender) {
pkg_free(sender);
if(sphere)
}
if(sphere) {
pkg_free(sphere);
}

return 1;

unsupported_event:

LM_WARN("Missing or unsupported event header field value\n");

if(msg->event && msg->event->body.s && msg->event->body.len > 0)
if(msg->event && msg->event->body.s && msg->event->body.len > 0) {
LM_ERR(" event=[%.*s]\n", msg->event->body.len, msg->event->body.s);
}

reply_code = BAD_EVENT_CODE;
reply_str = pu_489_rpl;
Expand All @@ -556,14 +562,18 @@ int ki_handle_publish_uri(struct sip_msg *msg, str *sender_uri)
}
}

if(presentity)
if(presentity) {
pkg_free(presentity);
if(etag.s)
}
if(etag.s) {
pkg_free(etag.s);
if(sender)
}
if(sender) {
pkg_free(sender);
if(sphere)
}
if(sphere) {
pkg_free(sphere);
}

return -1;
}
Expand Down

0 comments on commit eb5c1a4

Please sign in to comment.