summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2024-04-25 16:25:47 +0200
committerUros Majstorovic <majstor@majstor.org>2024-04-25 16:25:47 +0200
commit49ae18d9895336fb33d8745d3c92eb15eb23c1ac (patch)
tree56a731278159fb04f07223ac70791cc9db0c109b
parent085a8ed013a7390eb01450293ccf1f154fbfecd5 (diff)
added conn lock/unlock
-rw-r--r--ecp/src/ecp/core.c61
-rw-r--r--ecp/src/ecp/core.h16
2 files changed, 47 insertions, 30 deletions
diff --git a/ecp/src/ecp/core.c b/ecp/src/ecp/core.c
index 77017df..bb3309c 100644
--- a/ecp/src/ecp/core.c
+++ b/ecp/src/ecp/core.c
@@ -653,7 +653,7 @@ void ecp_sock_get_nonce(ECPSocket *sock, ecp_nonce_t *nonce) {
}
static int _conn_expired_inb(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to) {
- if (ecp_conn_is_inb(conn) && _ecp_conn_expired(conn, now, to)) return 1;
+ if (ecp_conn_is_inb(conn) && _ecp_conn_is_zombie(conn, now, to)) return 1;
return 0;
}
@@ -957,6 +957,24 @@ int ecp_conn_reset(ECPConnection *conn) {
return ECP_OK;
}
+void ecp_conn_set_flags(ECPConnection *conn, unsigned char flags) {
+ flags &= ECP_CONN_FLAG_MASK;
+ conn->flags_im |= flags;
+}
+
+void ecp_conn_clr_flags(ECPConnection *conn, unsigned char flags) {
+ flags &= ECP_CONN_FLAG_MASK;
+ conn->flags_im &= ~flags;
+}
+
+void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key) {
+ conn->remote.key_perma = *key;
+}
+
+void ecp_conn_set_remote_addr(ECPConnection *conn, ecp_tr_addr_t *addr) {
+ conn->remote.addr = *addr;
+}
+
int ecp_conn_create(ECPConnection *conn, ECPConnection *parent) {
int rv;
#ifdef ECP_WITH_VCONN
@@ -1028,6 +1046,7 @@ int ecp_conn_create_inb(ECPConnection *conn, ECPConnection *parent, unsigned cha
}
int ecp_conn_create_outb(ECPConnection *conn, ECPConnection *parent, ECPNode *node) {
+ ECPDHKey key;
int rv;
ecp_conn_set_outb(conn);
@@ -1035,9 +1054,12 @@ int ecp_conn_create_outb(ECPConnection *conn, ECPConnection *parent, ECPNode *no
conn->refcount = 1;
if (node) conn->remote = *node;
- rv = ecp_conn_init_outb(conn);
+ rv = ecp_dhkey_gen(&key);
if (rv) return rv;
+ conn->key_curr = 0;
+ conn->key[conn->key_curr] = key;
+
rv = ecp_conn_create(conn, parent);
if (rv) return rv;
@@ -1182,7 +1204,6 @@ void ecp_conn_remove_addr(ECPConnection *conn) {
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&sock->conn_table.mutex);
#endif
-
}
void ecp_conn_remove_gc(ECPConnection *conn) {
@@ -1299,7 +1320,7 @@ void ecp_conn_close(ECPConnection *conn) {
}
}
-int _ecp_conn_expired(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to) {
+int _ecp_conn_is_zombie(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to) {
if (ECP_STS_LT(conn->access_ts, now) && (now - conn->access_ts > to)) return 1;
return 0;
}
@@ -1311,7 +1332,7 @@ int ecp_conn_is_zombie(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to) {
pthread_mutex_lock(&conn->mutex);
#endif
- z = _ecp_conn_expired(conn, now, to);
+ z = _ecp_conn_is_zombie(conn, now, to);
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&conn->mutex);
@@ -1320,6 +1341,18 @@ int ecp_conn_is_zombie(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to) {
return z;
}
+void ecp_conn_lock(ECPConnection *conn) {
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&conn->mutex);
+#endif
+}
+
+void ecp_conn_unlock(ECPConnection *conn) {
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_unlock(&conn->mutex);
+#endif
+}
+
int ecp_conn_refcount_inc(ECPConnection *conn) {
int is_reg;
@@ -1357,24 +1390,6 @@ void ecp_conn_refcount_dec(ECPConnection *conn) {
}
}
-void ecp_conn_set_flags(ECPConnection *conn, unsigned char flags) {
- flags &= ECP_CONN_FLAG_MASK;
- conn->flags_im |= flags;
-}
-
-void ecp_conn_clr_flags(ECPConnection *conn, unsigned char flags) {
- flags &= ECP_CONN_FLAG_MASK;
- conn->flags_im &= ~flags;
-}
-
-void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key) {
- conn->remote.key_perma = *key;
-}
-
-void ecp_conn_set_remote_addr(ECPConnection *conn, ecp_tr_addr_t *addr) {
- conn->remote.addr = *addr;
-}
-
int ecp_conn_dhkey_new(ECPConnection *conn) {
ECPSocket *sock = conn->sock;
ECPDHKey new_key;
diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h
index a4ae5c9..30c00b3 100644
--- a/ecp/src/ecp/core.h
+++ b/ecp/src/ecp/core.h
@@ -405,6 +405,10 @@ ECPConnection *ecp_conn_new_inb(ECPSocket *sock, unsigned char ctype);
void ecp_conn_init(ECPConnection *conn, ECPSocket *sock, unsigned char ctype);
int ecp_conn_init_outb(ECPConnection *conn);
int ecp_conn_reset(ECPConnection *conn);
+void ecp_conn_set_flags(ECPConnection *conn, unsigned char flags);
+void ecp_conn_clr_flags(ECPConnection *conn, unsigned char flags);
+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);
int ecp_conn_create_inb(ECPConnection *conn, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, ecp_ecdh_public_t *public, ECPDHPub *rkey_perma, ecp_aead_key_t *shkey);
int ecp_conn_create_outb(ECPConnection *conn, ECPConnection *parent, ECPNode *node);
@@ -423,19 +427,17 @@ int _ecp_conn_reopen(ECPConnection *conn, int retry);
int ecp_conn_open(ECPConnection *conn, ECPNode *node);
int ecp_conn_try_open(ECPConnection *conn, ECPNode *node);
int ecp_conn_reopen(ECPConnection *conn);
-
void _ecp_conn_close(ECPConnection *conn);
void ecp_conn_close(ECPConnection *conn);
-int _ecp_conn_expired(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to);
+
+int _ecp_conn_is_zombie(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to);
int ecp_conn_is_zombie(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to);
+
+void ecp_conn_lock(ECPConnection *conn);
+void ecp_conn_unlock(ECPConnection *conn);
int ecp_conn_refcount_inc(ECPConnection *conn);
void ecp_conn_refcount_dec(ECPConnection *conn);
-void ecp_conn_set_flags(ECPConnection *conn, unsigned char flags);
-void ecp_conn_clr_flags(ECPConnection *conn, unsigned char flags);
-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_dhkey_new(ECPConnection *conn);
int ecp_conn_dhkey_get(ECPConnection *conn, unsigned char idx, ECPDHKey *key);
int ecp_conn_dhkey_get_remote(ECPConnection *conn, unsigned char idx, ECPDHPub *key);