瀏覽代碼

presence: use separate structure to match presentity to replace or update

Daniel-Constantin Mierla 5 年之前
父節點
當前提交
eb5c1a41ce

+ 17 - 11
src/modules/presence/hash.c

@@ -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);
@@ -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;
@@ -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);
@@ -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;

+ 2 - 2
src/modules/presence/hash.h

@@ -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);

+ 25 - 14
src/modules/presence/presentity.c

@@ -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) {
@@ -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);
@@ -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;
 			}
@@ -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;
@@ -1497,9 +1505,10 @@ after_dialog_check:
 		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;
@@ -1560,6 +1569,9 @@ after_dialog_check:
 		}
 
 		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,
@@ -1569,8 +1581,7 @@ after_dialog_check:
 			}
 
 			/* 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) {
@@ -1634,13 +1645,13 @@ after_etag_generation:
 		 * 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);
@@ -1655,14 +1666,14 @@ after_etag_generation:
 			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;
 		}
 
@@ -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 */

+ 1 - 1
src/modules/presence/presentity.h

@@ -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);
 

+ 22 - 12
src/modules/presence/publish.c

@@ -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);
@@ -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) {
@@ -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);
@@ -528,14 +529,18 @@ 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;
 
@@ -543,8 +548,9 @@ 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;
@@ -556,14 +562,18 @@ 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;
 }