From 58e7504a3c532832a7753f530efa3b96adfc0807 Mon Sep 17 00:00:00 2001 From: Anthony Hayward Date: Tue, 6 Sep 2022 08:16:24 +0100 Subject: [PATCH] Add hash_remove and use in transp to fix #261 --- include/re_hash.h | 1 + src/hash/hash.c | 17 +++++++++++++++++ src/sip/transp.c | 6 +----- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/include/re_hash.h b/include/re_hash.h index 8858dbcb..8ba2a4f3 100644 --- a/include/re_hash.h +++ b/include/re_hash.h @@ -12,6 +12,7 @@ struct pl; int hash_alloc(struct hash **hp, uint32_t bsize); void hash_append(struct hash *h, uint32_t key, struct le *le, void *data); void hash_unlink(struct le *le); +void hash_remove(struct le *le); struct le *hash_lookup(const struct hash *h, uint32_t key, list_apply_h *ah, void *arg); struct le *hash_apply(const struct hash *h, list_apply_h *ah, void *arg); diff --git a/src/hash/hash.c b/src/hash/hash.c index 45aa11e3..32d1cc14 100644 --- a/src/hash/hash.c +++ b/src/hash/hash.c @@ -95,6 +95,23 @@ void hash_unlink(struct le *le) } +/** + * If the element is present in the hashmap table, unlink it and deref the object, + * otherwise do nothing. This is like hash_flush but for a single entry. + * + * @param le List element + */ +void hash_remove(struct le *le) +{ + if (le->list != NULL) + { + void *data = le->data; + list_unlink(le); + mem_deref(data); + } +} + + /** * Apply a handler function to all elements in the hashmap with a matching key * diff --git a/src/sip/transp.c b/src/sip/transp.c index aed33bb0..625b650f 100644 --- a/src/sip/transp.c +++ b/src/sip/transp.c @@ -175,7 +175,6 @@ static void conn_close(struct sip_conn *conn, int err) conn->tc = mem_deref(conn->tc); tmr_cancel(&conn->tmr_ka); tmr_cancel(&conn->tmr); - hash_unlink(&conn->he); le = list_head(&conn->ql); @@ -195,6 +194,7 @@ static void conn_close(struct sip_conn *conn, int err) } sip_keepalive_signal(&conn->kal, err); + hash_remove(&conn->he); } @@ -203,7 +203,6 @@ static void conn_tmr_handler(void *arg) struct sip_conn *conn = arg; conn_close(conn, ETIMEDOUT); - mem_deref(conn); } @@ -221,7 +220,6 @@ static void conn_keepalive_handler(void *arg) err = tcp_send(conn->tc, &mb); if (err) { conn_close(conn, err); - mem_deref(conn); return; } @@ -448,7 +446,6 @@ static void tcp_recv_handler(struct mbuf *mb, void *arg) out: if (err) { conn_close(conn, err); - mem_deref(conn); } } @@ -492,7 +489,6 @@ static void tcp_close_handler(int err, void *arg) struct sip_conn *conn = arg; conn_close(conn, err ? err : ECONNRESET); - mem_deref(conn); }