From 52f23e15fc485f6b297568fad64a8582e68e9da8 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Sun, 12 May 2024 20:48:29 +0200 Subject: fixed double conn close; add conditional compile ifdefs for pthread code --- ecp/src/ecp/core.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ ecp/src/ecp/core.h | 7 ++++++- 2 files changed, 58 insertions(+), 1 deletion(-) (limited to 'ecp') diff --git a/ecp/src/ecp/core.c b/ecp/src/ecp/core.c index d666d32..66ed814 100644 --- a/ecp/src/ecp/core.c +++ b/ecp/src/ecp/core.c @@ -1053,9 +1053,15 @@ int _ecp_conn_test_uflags(ECPConnection *conn, unsigned char flags) { int ecp_conn_set_uflags(ECPConnection *conn, unsigned char flags) { int rv; +#ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); +#endif + rv = _ecp_conn_set_uflags(conn, flags); + +#ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); +#endif return rv; } @@ -1063,9 +1069,15 @@ int ecp_conn_set_uflags(ECPConnection *conn, unsigned char flags) { int ecp_conn_clr_uflags(ECPConnection *conn, unsigned char flags) { int rv; +#ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); +#endif + rv = _ecp_conn_clr_uflags(conn, flags); + +#ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); +#endif return rv; } @@ -1073,9 +1085,15 @@ int ecp_conn_clr_uflags(ECPConnection *conn, unsigned char flags) { int ecp_conn_test_uflags(ECPConnection *conn, unsigned char flags) { int rv; +#ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); +#endif + rv = _ecp_conn_test_uflags(conn, flags); + +#ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); +#endif return rv; } @@ -1083,9 +1101,15 @@ int ecp_conn_test_uflags(ECPConnection *conn, unsigned char flags) { int ecp_conn_is_reg(ECPConnection *conn) { int rv; +#ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); +#endif + rv = _ecp_conn_is_reg(conn); + +#ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); +#endif return rv; } @@ -1093,13 +1117,36 @@ int ecp_conn_is_reg(ECPConnection *conn) { int ecp_conn_is_open(ECPConnection *conn) { int rv; +#ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); +#endif + rv = _ecp_conn_is_open(conn); + +#ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); +#endif return rv; } +int ecp_conn_set_closed(ECPConnection *conn) { + int is_cls; + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&conn->mutex); +#endif + + is_cls = _ecp_conn_is_closed(conn); + if (!is_cls) _ecp_conn_set_closed(conn); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&conn->mutex); +#endif + + return is_cls ? ECP_ERR_CLOSED : ECP_OK; +} + void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key) { conn->remote.key_perma = *key; } @@ -1433,6 +1480,11 @@ void _ecp_conn_close(ECPConnection *conn) { } void ecp_conn_close(ECPConnection *conn) { + int rv; + + rv = ecp_conn_set_closed(conn); + if (rv) return; + ecp_timer_remove(conn); ecp_conn_remove(conn); if (ecp_conn_is_gc(conn)) { diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h index 3631942..45e24dc 100644 --- a/ecp/src/ecp/core.h +++ b/ecp/src/ecp/core.h @@ -165,7 +165,8 @@ /* mutable flags */ #define ECP_CONN_FLAG_REG 0x01 #define ECP_CONN_FLAG_OPEN 0x02 -#define ECP_CONN_FLAG_GCT 0x04 +#define ECP_CONN_FLAG_CLOSED 0x04 +#define ECP_CONN_FLAG_GCT 0x08 #define ECP_CONN_FLAG_UMASK 0xF0 @@ -180,16 +181,19 @@ #define ecp_conn_is_sys(conn) ((conn)->type & ECP_CTYPE_FLAG_SYS) #define _ecp_conn_is_reg(conn) ((conn)->flags & ECP_CONN_FLAG_REG) #define _ecp_conn_is_open(conn) ((conn)->flags & ECP_CONN_FLAG_OPEN) +#define _ecp_conn_is_closed(conn) ((conn)->flags & ECP_CONN_FLAG_CLOSED) #define _ecp_conn_in_gct(conn) ((conn)->flags & ECP_CONN_FLAG_GCT) #define ecp_conn_set_inb(conn) ((conn)->flags_im |= ECP_CONN_FLAG_INB) #define ecp_conn_set_outb(conn) ((conn)->flags_im &= ~ECP_CONN_FLAG_INB) #define _ecp_conn_set_reg(conn) ((conn)->flags |= ECP_CONN_FLAG_REG) #define _ecp_conn_set_open(conn) ((conn)->flags |= ECP_CONN_FLAG_OPEN) +#define _ecp_conn_set_closed(conn) ((conn)->flags |= ECP_CONN_FLAG_CLOSED) #define _ecp_conn_push_gct(conn) ((conn)->flags |= ECP_CONN_FLAG_GCT) #define _ecp_conn_clr_reg(conn) ((conn)->flags &= ~ECP_CONN_FLAG_REG) #define _ecp_conn_clr_open(conn) ((conn)->flags &= ~ECP_CONN_FLAG_OPEN) +#define _ecp_conn_clr_closed(conn) ((conn)->flags &= ~ECP_CONN_FLAG_CLOSED) #define _ecp_conn_pull_gct(conn) ((conn)->flags &= ~ECP_CONN_FLAG_GCT) typedef uint32_t ecp_ack_t; @@ -423,6 +427,7 @@ int ecp_conn_clr_uflags(ECPConnection *conn, unsigned char flags); int ecp_conn_test_uflags(ECPConnection *conn, unsigned char flags); int ecp_conn_is_reg(ECPConnection *conn); int ecp_conn_is_open(ECPConnection *conn); +int ecp_conn_set_closed(ECPConnection *conn); void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key); void ecp_conn_set_remote_addr(ECPConnection *conn, ecp_tr_addr_t *addr); int ecp_conn_create(ECPConnection *conn, ECPConnection *parent); -- cgit v1.2.3