Module: sip-router
Branch: 4.0
Commit: 32a4710d4ab7e3b03121154cbc609c6ac41d17b2
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=32a4710…
Author: Victor Seva <linuxmaniac(a)torreviejawireless.org>
Committer: Victor Seva <linuxmaniac(a)torreviejawireless.org>
Date: Fri Nov 29 14:23:47 2013 +0100
usrloc: db_update contact by instance + reg-id
- backport from master f67171c
---
modules/usrloc/ucontact.c | 194 ++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 192 insertions(+), 2 deletions(-)
diff --git a/modules/usrloc/ucontact.c b/modules/usrloc/ucontact.c
index 996e8d5..fcd3005 100644
--- a/modules/usrloc/ucontact.c
+++ b/modules/usrloc/ucontact.c
@@ -1014,14 +1014,204 @@ int db_update_ucontact_ruid(ucontact_t* _c)
}
/*!
+ * \brief Update contact in the database by instance reg_id
+ * \param _c updated contact
+ * \return 0 on success, -1 on failure
+ */
+int db_update_ucontact_instance(ucontact_t* _c)
+{
+ str auser;
+ str adomain;
+ db_key_t keys1[2];
+ db_val_t vals1[2];
+ int n1;
+
+ db_key_t keys2[13];
+ db_val_t vals2[13];
+ int n2;
+
+
+ if (_c->flags & FL_MEM) {
+ return 0;
+ }
+
+ if(_c->instance.len<=0) {
+ LM_ERR("updating record in database failed - empty instance\n");
+ return -1;
+ }
+
+ n1 = 0;
+ keys1[n1] = &instance_col;
+ vals1[n1].type = DB1_STR;
+ vals1[n1].nul = 0;
+ vals1[n1].val.str_val = _c->instance;
+ n1++;
+
+ keys1[n1] = ®_id_col;
+ vals1[n1].type = DB1_INT;
+ vals1[n1].nul = 0;
+ vals1[n1].val.int_val = (int)_c->reg_id;
+ n1++;
+
+ n2 = 0;
+ keys2[n2] = &expires_col;
+ vals2[n2].type = DB1_DATETIME;
+ vals2[n2].nul = 0;
+ vals2[n2].val.time_val = _c->expires;
+ n2++;
+
+ keys2[n2] = &q_col;
+ vals2[n2].type = DB1_DOUBLE;
+ vals2[n2].nul = 0;
+ vals2[n2].val.double_val = q2double(_c->q);
+ n2++;
+
+ keys2[n2] = &cseq_col;
+ vals2[n2].type = DB1_INT;
+ vals2[n2].nul = 0;
+ vals2[n2].val.int_val = _c->cseq;
+ n2++;
+
+ keys2[n2] = &flags_col;
+ vals2[n2].type = DB1_INT;
+ vals2[n2].nul = 0;
+ vals2[n2].val.bitmap_val = _c->flags;
+ n2++;
+
+ keys2[n2] = &cflags_col;
+ vals2[n2].type = DB1_INT;
+ vals2[n2].nul = 0;
+ vals2[n2].val.bitmap_val = _c->cflags;
+ n2++;
+
+ keys2[n2] = &user_agent_col;
+ vals2[n2].type = DB1_STR;
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val = _c->user_agent;
+ n2++;
+
+ keys2[n2] = &received_col;
+ vals2[n2].type = DB1_STR;
+ if (_c->received.s == 0) {
+ vals2[n2].nul = 1;
+ } else {
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val = _c->received;
+ }
+ n2++;
+
+ keys2[n2] = &path_col;
+ vals2[n2].type = DB1_STR;
+ if (_c->path.s == 0) {
+ vals2[n2].nul = 1;
+ } else {
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val = _c->path;
+ }
+ n2++;
+
+ keys2[n2] = &sock_col;
+ vals2[n2].type = DB1_STR;
+ if (_c->sock) {
+ vals2[n2].val.str_val = _c->sock->sock_str;
+ vals2[n2].nul = 0;
+ } else {
+ vals2[n2].nul = 1;
+ }
+ n2++;
+
+ keys2[n2] = &methods_col;
+ vals2[n2].type = DB1_BITMAP;
+ if (_c->methods == 0xFFFFFFFF) {
+ vals2[n2].nul = 1;
+ } else {
+ vals2[n2].val.bitmap_val = _c->methods;
+ vals2[n2].nul = 0;
+ }
+ n2++;
+
+ keys2[n2] = &last_mod_col;
+ vals2[n2].type = DB1_DATETIME;
+ vals2[n2].nul = 0;
+ vals2[n2].val.time_val = _c->last_modified;
+ n2++;
+
+ keys2[n2] = &callid_col;
+ vals2[n2].type = DB1_STR;
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val = _c->callid;
+ n2++;
+
+ keys2[n2] = &contact_col;
+ vals2[n2].type = DB1_STR;
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val.s = _c->c.s;
+ vals2[n2].val.str_val.len = _c->c.len;
+ LM_DBG("contact:%.*s\n", vals2[n2].val.str_val.len, vals2[n2].val.str_val.s);
+ n2++;
+
+ if (ul_dbf.use_table(ul_dbh, _c->domain) < 0) {
+ LM_ERR("sql use_table failed\n");
+ return -1;
+ }
+
+ if (ul_dbf.update(ul_dbh, keys1, 0, vals1, keys2, vals2, n1, n2) < 0) {
+ LM_ERR("updating database failed\n");
+ return -1;
+ }
+
+ if (ul_db_check_update==1 && ul_dbf.affected_rows) {
+ /* supposed to be an UPDATE, but if affected rows is 0, then try
+ * to do an INSERT */
+ if(ul_dbf.affected_rows(ul_dbh)==0) {
+ LM_DBG("affected rows by UPDATE was 0, doing an INSERT\n");
+ if(db_insert_ucontact(_c)<0)
+ return -1;
+ }
+ }
+
+ /* delete old db attrs and add the current list */
+ auser = *_c->aor;
+ if (use_domain) {
+ adomain.s = memchr(_c->aor->s, '@', _c->aor->len);
+ if (adomain.s==0) {
+ auser.len = 0;
+ adomain = *_c->aor;
+ } else {
+ auser.len = adomain.s - _c->aor->s;
+ adomain.s++;
+ adomain.len = _c->aor->s + _c->aor->len - adomain.s;
+ }
+
+ uldb_delete_attrs(_c->domain, &auser,
+ &adomain, &_c->ruid);
+ uldb_insert_attrs(_c->domain, &auser,
+ &adomain, &_c->ruid, _c->xavp);
+ } else {
+ uldb_delete_attrs(_c->domain, &auser,
+ NULL, &_c->ruid);
+ uldb_insert_attrs(_c->domain, &auser,
+ NULL, &_c->ruid, _c->xavp);
+ }
+
+ return 0;
+}
+
+/*!
* \brief Update contact in the database
* \param _c updated contact
* \return 0 on success, -1 on failure
*/
int db_update_ucontact(ucontact_t* _c)
{
- if(ul_db_ops_ruid==0)
- return db_update_ucontact_addr(_c);
+ if(ul_db_ops_ruid==0) {
+ if (_c->instance.len<=0) {
+ return db_update_ucontact_addr(_c);
+ }
+ else {
+ return db_update_ucontact_instance(_c);
+ }
+ }
else
return db_update_ucontact_ruid(_c);
}