[sr-dev] git:master:c943fdf2: htable: export sht_is_null() function to kemi

Daniel-Constantin Mierla miconda at gmail.com
Tue Jan 28 15:08:56 CET 2020


Module: kamailio
Branch: master
Commit: c943fdf2f3a690927d489d9f57cfc53bce00b49f
URL: https://github.com/kamailio/kamailio/commit/c943fdf2f3a690927d489d9f57cfc53bce00b49f

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2020-01-28T15:07:16+01:00

htable: export sht_is_null() function to kemi

  - return >0 if htable or item not found
  - return <0 if item found or htable defined with default value

---

Modified: src/modules/htable/ht_api.c
Modified: src/modules/htable/ht_api.h
Modified: src/modules/htable/htable.c

---

Diff:  https://github.com/kamailio/kamailio/commit/c943fdf2f3a690927d489d9f57cfc53bce00b49f.diff
Patch: https://github.com/kamailio/kamailio/commit/c943fdf2f3a690927d489d9f57cfc53bce00b49f.patch

---

diff --git a/src/modules/htable/ht_api.c b/src/modules/htable/ht_api.c
index e8c27c426e..4812e8aacd 100644
--- a/src/modules/htable/ht_api.c
+++ b/src/modules/htable/ht_api.c
@@ -851,6 +851,46 @@ ht_cell_t* ht_cell_pkg_copy(ht_t *ht, str *name, ht_cell_t *old)
 	return NULL;
 }
 
+int ht_cell_exists(ht_t *ht, str *name)
+{
+	unsigned int idx;
+	unsigned int hid;
+	ht_cell_t *it;
+
+	if(ht==NULL || ht->entries==NULL)
+		return 0;
+
+	hid = ht_compute_hash(name);
+
+	idx = ht_get_entry(hid, ht->htsize);
+
+	/* head test and return */
+	if(ht->entries[idx].first==NULL)
+		return 0;
+
+	ht_slot_lock(ht, idx);
+	it = ht->entries[idx].first;
+	while(it!=NULL && it->cellid < hid)
+		it = it->next;
+	while(it!=NULL && it->cellid == hid) {
+		if(name->len==it->name.len
+				&& strncmp(name->s, it->name.s, name->len)==0) {
+			/* found */
+			if(ht->htexpire>0 && it->expire!=0 && it->expire<time(NULL)) {
+				/* entry has expired */
+				ht_slot_unlock(ht, idx);
+				return 0;
+			}
+			ht_slot_unlock(ht, idx);
+			return 1;
+		}
+		it = it->next;
+	}
+	ht_slot_unlock(ht, idx);
+	return 0;
+}
+
+
 int ht_dbg(void)
 {
 	int i;
diff --git a/src/modules/htable/ht_api.h b/src/modules/htable/ht_api.h
index d1e9d2383e..65f821a266 100644
--- a/src/modules/htable/ht_api.h
+++ b/src/modules/htable/ht_api.h
@@ -92,6 +92,7 @@ int ht_destroy(void);
 int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode);
 int ht_del_cell(ht_t *ht, str *name);
 ht_cell_t* ht_cell_value_add(ht_t *ht, str *name, int val, ht_cell_t *old);
+int ht_cell_exists(ht_t *ht, str *name);
 
 int ht_dbg(void);
 ht_cell_t* ht_cell_pkg_copy(ht_t *ht, str *name, ht_cell_t *old);
diff --git a/src/modules/htable/htable.c b/src/modules/htable/htable.c
index 443502078d..afb22fe241 100644
--- a/src/modules/htable/htable.c
+++ b/src/modules/htable/htable.c
@@ -998,6 +998,31 @@ static sr_kemi_xval_t* ki_ht_getw(sip_msg_t *msg, str *htname, str *itname)
 }
 
 
+/**
+ *
+ */
+static int ki_ht_is_null(sip_msg_t *msg, str *htname, str *itname)
+{
+	ht_t *ht = NULL;
+
+	/* find the hash htable */
+	ht = ht_get_table(htname);
+	if (ht == NULL) {
+		return 2;
+	}
+
+	if(ht->flags==PV_VAL_INT) {
+		/* htable defined with default value */
+		return -2;
+	}
+
+	if(ht_cell_exists(ht, itname)>0) {
+		return -1;
+	}
+
+	return 1;
+}
+
 /**
  *
  */
@@ -1816,6 +1841,11 @@ static sr_kemi_t sr_kemi_htable_exports[] = {
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
 			SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE }
 	},
+	{ str_init("htable"), str_init("sht_is_null"),
+		SR_KEMIP_INT, ki_ht_is_null,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
 
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
 };




More information about the sr-dev mailing list