Skip to content

Commit

Permalink
usrloc: Include the matching algorithm in replication packets
Browse files Browse the repository at this point in the history
Although this commit also bumps the usrloc BIN packet version from
2 -> 3, there is added logic for backwards-compatibility with
version 2 packets, so the migration in a clustered environment should
be silky smooth!

Many thanks to Giovanni Maruzzelli (@gmaruzz) for catching this issue
and providing a detailed bug report!

Fixes #2351
  • Loading branch information
liviuchircu committed Jun 17, 2021
1 parent 8cc4b32 commit 60f1a53
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 86 deletions.
39 changes: 21 additions & 18 deletions modules/mid_registrar/save.c
Expand Up @@ -1009,7 +1009,7 @@ int append_contacts(ucontact_t *contacts, struct sip_msg *msg)
return 0;
}

int trim_contacts(urecord_t *r, int trims)
int trim_contacts(urecord_t *r, int trims, const struct ct_match *match)
{
ucontact_t *uc;

Expand All @@ -1020,7 +1020,7 @@ int trim_contacts(urecord_t *r, int trims)
LM_DBG("overflow on inserting new contact -> removing <%.*s>\n",
uc->c.len, uc->c.s);

if (ul.delete_ucontact(r, uc, 0) != 0) {
if (ul.delete_ucontact(r, uc, match, 0) != 0) {
LM_ERR("failed to remove contact, aor: %.*s\n",
r->aor.len, r->aor.s);
return -1;
Expand Down Expand Up @@ -1358,13 +1358,14 @@ static inline int save_restore_rpl_contacts(struct sip_msg *req,
goto error;
}

if (trim_contacts(r, vct - mri->max_contacts + 1) != 0)
if (trim_contacts(r, vct - mri->max_contacts + 1, &mri->cmatch))
goto error;
}

LM_DBG("INSERTING contact with expires %lu\n", ci->expires);

if (ul.insert_ucontact(r, &ctmap->req_ct_uri, ci, &c, 0) < 0) {
if (ul.insert_ucontact(r, &ctmap->req_ct_uri, ci,
&mri->cmatch, 0, &c) < 0) {
rerrno = R_UL_INS_C;
LM_ERR("failed to insert contact\n");
goto error;
Expand All @@ -1389,7 +1390,7 @@ static inline int save_restore_rpl_contacts(struct sip_msg *req,
}

was_valid = VALID_CONTACT(c, get_act_time());
if (ul.delete_ucontact(r, c, 0) < 0) {
if (ul.delete_ucontact(r, c, &mri->cmatch, 0) < 0) {
rerrno = R_UL_UPD_C;
LM_ERR("failed to delete contact\n");
goto error;
Expand All @@ -1412,7 +1413,7 @@ static inline int save_restore_rpl_contacts(struct sip_msg *req,
goto error;
}

if (trim_contacts(r, vct - mri->max_contacts) != 0)
if (trim_contacts(r, vct - mri->max_contacts, &mri->cmatch))
goto error;
}

Expand All @@ -1423,7 +1424,7 @@ static inline int save_restore_rpl_contacts(struct sip_msg *req,
goto error;
}

if (ul.update_ucontact(r, c, ci, 0) < 0) {
if (ul.update_ucontact(r, c, ci, &mri->cmatch, 0) < 0) {
rerrno = R_UL_UPD_C;
LM_ERR("failed to update contact\n");
goto error;
Expand Down Expand Up @@ -1619,13 +1620,14 @@ static inline int save_restore_req_contacts(struct sip_msg *req,
goto out_clear_err;
}

if (trim_contacts(r, vct - mri->max_contacts + 1) != 0)
if (trim_contacts(r, vct - mri->max_contacts + 1, &mri->cmatch))
goto out_clear_err;
}

LM_DBG("INSERTING contact with expires %lu\n", ci->expires);

if (ul.insert_ucontact( r, &ctmap->req_ct_uri, ci, &c, 0) < 0) {
if (ul.insert_ucontact( r, &ctmap->req_ct_uri, ci, &mri->cmatch,
0, &c) < 0) {
rerrno = R_UL_INS_C;
LM_ERR("failed to insert contact\n");
goto out_clear_err;
Expand All @@ -1643,7 +1645,7 @@ static inline int save_restore_req_contacts(struct sip_msg *req,
/* delete expired or stale contact (not present on main reg) */
if (ctmap->expires == 0 || !_c) {
was_valid = VALID_CONTACT(c, get_act_time());
if (ul.delete_ucontact(r, c, 0) < 0) {
if (ul.delete_ucontact(r, c, &mri->cmatch, 0) < 0) {
rerrno = R_UL_UPD_C;
LM_ERR("failed to update contact\n");
goto out_clear_err;
Expand All @@ -1664,7 +1666,7 @@ static inline int save_restore_req_contacts(struct sip_msg *req,
goto out_clear_err;
}

if (trim_contacts(r, vct - mri->max_contacts) != 0)
if (trim_contacts(r, vct - mri->max_contacts, &mri->cmatch))
goto out_clear_err;
}

Expand All @@ -1674,7 +1676,7 @@ static inline int save_restore_req_contacts(struct sip_msg *req,
goto out_clear_err;
}

if (ul.update_ucontact( r, c, ci, 0) < 0) {
if (ul.update_ucontact( r, c, ci, &mri->cmatch, 0) < 0) {
rerrno = R_UL_UPD_C;
LM_ERR("failed to update contact\n");
goto out_clear_err;
Expand Down Expand Up @@ -2226,7 +2228,7 @@ static int process_contacts_by_ct(struct sip_msg *msg, urecord_t *urec,
if (!ul.put_ucontact_key(c, &ul_key_last_cseq, &value))
LM_ERR("failed to update CSeq - oom?\n");

if (ul.update_ucontact(urec, c, ci, 0) < 0) {
if (ul.update_ucontact(urec, c, ci, &_sctx->cmatch, 0) < 0) {
rerrno = R_UL_UPD_C;
LM_ERR("failed to update contact\n");
return -1;
Expand Down Expand Up @@ -2377,7 +2379,7 @@ static int process_contacts_by_aor(struct sip_msg *req, urecord_t *urec,
return 1;
}

if (ul.delete_ucontact(urec, c, 0) < 0) {
if (ul.delete_ucontact(urec, c, &_sctx->cmatch, 0) < 0) {
rerrno = R_UL_UPD_C;
return -1;
}
Expand All @@ -2397,7 +2399,7 @@ static int process_contacts_by_aor(struct sip_msg *req, urecord_t *urec,
return -1;
}

if (trim_contacts(urec, vct - _sctx->max_contacts) != 0)
if (trim_contacts(urec, vct - _sctx->max_contacts, &_sctx->cmatch))
return -1;
}

Expand All @@ -2417,7 +2419,7 @@ static int process_contacts_by_aor(struct sip_msg *req, urecord_t *urec,
return -1;
}

if (ul.update_ucontact(urec, c, ci, 0) < 0) {
if (ul.update_ucontact(urec, c, ci, &_sctx->cmatch, 0) < 0) {
rerrno = R_UL_UPD_C;
LM_ERR("failed to update contact\n");
return -1;
Expand All @@ -2435,7 +2437,7 @@ static int process_contacts_by_aor(struct sip_msg *req, urecord_t *urec,
return -1;
}

if (trim_contacts(urec, vct - _sctx->max_contacts + 1) != 0)
if (trim_contacts(urec, vct - _sctx->max_contacts + 1, &_sctx->cmatch))
return -1;
}

Expand All @@ -2450,7 +2452,8 @@ static int process_contacts_by_aor(struct sip_msg *req, urecord_t *urec,
}
ci->expires_out = e_out;

if (ul.insert_ucontact(urec, &ct->uri, ci, &c, 0) < 0) {
if (ul.insert_ucontact(urec, &ct->uri, ci, &_sctx->cmatch,
0, &c) < 0) {
rerrno = R_UL_INS_C;
LM_ERR("failed to insert contact\n");
return -1;
Expand Down
20 changes: 11 additions & 9 deletions modules/registrar/save.c
Expand Up @@ -244,7 +244,7 @@ static inline int insert_contacts(struct sip_msg* _m, contact_t* _c,
rerrno = R_INTERNAL;
goto error;
}
if (ul.delete_ucontact( r, r->contacts, 0)!=0) {
if (ul.delete_ucontact( r, r->contacts, &_sctx->cmatch, 0)!=0) {
LM_ERR("failed to remove contact\n");
rerrno = R_INTERNAL;
goto error;
Expand Down Expand Up @@ -279,13 +279,14 @@ static inline int insert_contacts(struct sip_msg* _m, contact_t* _c,
if ( r->contacts==0 ||
ul.get_ucontact(r, &_c->uri, ci->callid, ci->cseq+1, &_sctx->cmatch,
&c)!=0 ){
if (ul.insert_ucontact( r, &_c->uri, ci, &c, 0) < 0) {
if (ul.insert_ucontact( r, &_c->uri, ci, &_sctx->cmatch,
0, &c) < 0) {
rerrno = R_UL_INS_C;
LM_ERR("failed to insert contact\n");
goto error;
}
} else {
if (ul.update_ucontact( r, c, ci, 0) < 0) {
if (ul.update_ucontact( r, c, ci, &_sctx->cmatch, 0) < 0) {
rerrno = R_UL_UPD_C;
LM_ERR("failed to update contact\n");
goto error;
Expand Down Expand Up @@ -429,7 +430,7 @@ static inline int update_contacts(struct sip_msg* _m, urecord_t* _r,
}
LM_DBG("overflow on inserting new contact -> removing "
"<%.*s>\n", c_last->c.len, c_last->c.s);
if (ul.delete_ucontact( _r, c_last, 0)!=0) {
if (ul.delete_ucontact( _r, c_last, &_sctx->cmatch, 0)!=0) {
LM_ERR("failed to remove contact\n");
goto error;
}
Expand All @@ -449,7 +450,8 @@ static inline int update_contacts(struct sip_msg* _m, urecord_t* _r,
goto error;
}

if (ul.insert_ucontact( _r, &_c->uri, ci, &c, 0) < 0) {
if (ul.insert_ucontact( _r, &_c->uri, ci, &_sctx->cmatch,
0, &c) < 0) {
rerrno = R_UL_INS_C;
LM_ERR("failed to insert contact\n");
goto error;
Expand All @@ -466,7 +468,7 @@ static inline int update_contacts(struct sip_msg* _m, urecord_t* _r,
c->flags &= ~FL_MEM;
}

if (ul.delete_ucontact(_r, c, 0) < 0) {
if (ul.delete_ucontact(_r, c, &_sctx->cmatch, 0) < 0) {
rerrno = R_UL_DEL_C;
LM_ERR("failed to delete contact\n");
goto error;
Expand Down Expand Up @@ -496,7 +498,7 @@ static inline int update_contacts(struct sip_msg* _m, urecord_t* _r,
}
LM_DBG("overflow on update -> removing contact "
"<%.*s>\n", c_last->c.len, c_last->c.s);
if (ul.delete_ucontact( _r, c_last, 0)!=0) {
if (ul.delete_ucontact( _r, c_last, &_sctx->cmatch, 0)!=0) {
LM_ERR("failed to remove contact\n");
goto error;
}
Expand All @@ -516,7 +518,7 @@ static inline int update_contacts(struct sip_msg* _m, urecord_t* _r,
goto error;
}

if (ul.update_ucontact(_r, c, ci, 0) < 0) {
if (ul.update_ucontact(_r, c, ci, &_sctx->cmatch, 0) < 0) {
rerrno = R_UL_UPD_C;
LM_ERR("failed to update contact\n");
goto error;
Expand Down Expand Up @@ -1010,7 +1012,7 @@ int _remove(struct sip_msg *msg, void *udomain, str *aor_uri, str *match_ct,
continue;
}

ul.delete_ucontact(record, contact, 0);
ul.delete_ucontact(record, contact, NULL, 0);
}

ul.release_urecord(record, 0);
Expand Down
8 changes: 4 additions & 4 deletions modules/registrar/test/test.c
Expand Up @@ -77,7 +77,7 @@ static void test_lookup(void)

fill_ucontact_info(&ci);
ci.methods = METHOD_UNDEF;
ok(ul.insert_ucontact(r, &ct2, &ci, &c, 0) == 0, "insert Contact");
ok(ul.insert_ucontact(r, &ct2, &ci, NULL, 1, &c) == 0, "insert Contact");
ul.unlock_udomain(d, &aor);

ok(reg_lookup(&msg, d, _str(""), NULL) == LOOKUP_OK, "lookup-2");
Expand All @@ -93,17 +93,17 @@ static void test_lookup(void)
set_ruri(&msg, &aor_ruri);
ok(reg_lookup(&msg, d, _str("m"), NULL) == LOOKUP_OK, "lookup-5");

ok(ul.delete_ucontact(r, c, 0) == 0, "delete ucontact");
ok(ul.delete_ucontact(r, c, NULL, 0) == 0, "delete ucontact");

fill_ucontact_info(&ci);
ci.flags |= FL_PN_ON; /* this is needed until we rewrite to call save() */
ok(ul.insert_ucontact(r, &ct1, &ci, &c, 0) == 0, "insert ct1 (PN)");
ok(ul.insert_ucontact(r, &ct1, &ci, NULL, 1, &c) == 0, "insert ct1 (PN)");

set_ruri(&msg, &aor_ruri);
ok(reg_lookup(&msg, d, _str(""), NULL) == LOOKUP_PN_SENT, "lookup-6");

fill_ucontact_info(&ci);
ok(ul.insert_ucontact(r, &ct2, &ci, &c, 0) == 0, "insert ct2 (normal)");
ok(ul.insert_ucontact(r, &ct2, &ci, NULL, 1, &c) == 0, "insert ct2 (normal)");

set_ruri(&msg, &aor_ruri);
ok(reg_lookup(&msg, d, _str(""), NULL) == LOOKUP_OK, "lookup-7");
Expand Down
2 changes: 1 addition & 1 deletion modules/usrloc/dlist.c
Expand Up @@ -1205,7 +1205,7 @@ int delete_ucontact_from_coords(udomain_t *d, ucontact_coords ct_coords,
}

if (!skip_replication && location_cluster)
replicate_ucontact_delete(r, c);
replicate_ucontact_delete(r, c, NULL);

if (exists_ulcb_type(UL_CONTACT_DELETE)) {
run_ul_callbacks( UL_CONTACT_DELETE, c);
Expand Down
4 changes: 2 additions & 2 deletions modules/usrloc/ucontact.c
Expand Up @@ -966,7 +966,7 @@ static inline void update_contact_pos(struct urecord* _r, ucontact_t* _c)
* Update ucontact with new values
*/
int update_ucontact(struct urecord* _r, ucontact_t* _c, ucontact_info_t* _ci,
char skip_replication)
const struct ct_match *match, char skip_replication)
{
int ret, persist_kv_store = 1;

Expand All @@ -986,7 +986,7 @@ int update_ucontact(struct urecord* _r, ucontact_t* _c, ucontact_info_t* _ci,
else
persist_kv_store = 0;

replicate_ucontact_update(_r, _c);
replicate_ucontact_update(_r, _c, match);
}

/* run callbacks for UPDATE event */
Expand Down
2 changes: 1 addition & 1 deletion modules/usrloc/ucontact.h
Expand Up @@ -300,7 +300,7 @@ struct urecord;
* Update ucontact with new values
*/
int update_ucontact(struct urecord* _r, ucontact_t* _c, ucontact_info_t* _ci,
char skip_replication);
const struct ct_match *match, char skip_replication);

/*! \brief
* Fetch a key from the contact-level storage
Expand Down
2 changes: 1 addition & 1 deletion modules/usrloc/udomain.c
Expand Up @@ -1525,7 +1525,7 @@ int delete_urecord(udomain_t* _d, str* _aor, struct urecord* _r,
while(c) {
t = c;
c = c->next;
if (delete_ucontact(_r, t, skip_replication) < 0) {
if (delete_ucontact(_r, t, NULL, skip_replication) < 0) {
LM_ERR("deleting contact failed\n");
return -1;
}
Expand Down

0 comments on commit 60f1a53

Please sign in to comment.