diff options
author | Uros Majstorovic <majstor@majstor.org> | 2024-05-12 20:48:29 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2024-05-12 20:48:29 +0200 |
commit | 52f23e15fc485f6b297568fad64a8582e68e9da8 (patch) | |
tree | 94aa22b85bd78fb2a792e24efd6cf6f8418750db /ecp | |
parent | ca41e5b94a9bf6e8261cdf47081a9c5b002f8dbb (diff) |
fixed double conn close; add conditional compile ifdefs for pthread code
Diffstat (limited to 'ecp')
-rw-r--r-- | ecp/src/ecp/core.c | 52 | ||||
-rw-r--r-- | ecp/src/ecp/core.h | 7 |
2 files changed, 58 insertions, 1 deletions
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); |