summaryrefslogtreecommitdiff
path: root/ecp
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2024-05-07 20:04:36 +0200
committerUros Majstorovic <majstor@majstor.org>2024-05-07 20:04:36 +0200
commit98763d1f48434dfb8c5f4556ec3573920204b7d9 (patch)
treea04184a05152dfa3767c615643864aa28ef1a6b5 /ecp
parent34eca987e2b3f8c08cf386a644beb7465324f638 (diff)
added search gc
Diffstat (limited to 'ecp')
-rw-r--r--ecp/src/ecp/core.c76
-rw-r--r--ecp/src/ecp/core.h1
2 files changed, 68 insertions, 9 deletions
diff --git a/ecp/src/ecp/core.c b/ecp/src/ecp/core.c
index 03fa815..9fd857b 100644
--- a/ecp/src/ecp/core.c
+++ b/ecp/src/ecp/core.c
@@ -643,6 +643,57 @@ void ecp_sock_expire(ECPSocket *sock, ecp_conn_expired_t conn_expired) {
conn_table_expire(sock, conn_expired);
}
+ECPConnection *ecp_sock_search_gc(ECPSocket *sock, ecp_ecdh_public_t *public) {
+ ECPConnection *conn = NULL;
+ int i, rv;
+
+#ifdef ECP_WITH_HTABLE
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&sock->conn_table.mutex_gc);
+#endif
+
+ conn = ecp_ht_search(sock->conn_table.keys_gc, public);
+ if (conn) {
+ rv = ecp_conn_refcount_inc(conn);
+ if (rv) conn = NULL;
+ }
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&sock->conn_table.mutex_gc);
+#endif
+#else /* ECP_WITH_HTABLE */
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&sock->conn_table.mutex);
+#endif
+
+ for (i=0; i<sock->conn_table.size; i++) {
+ ECPConnection *_conn = sock->conn_table.arr[i];
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&_conn->mutex);
+#endif
+
+ if (_ecp_conn_in_gct(_conn) && (memcmp(&_conn->remote.key_perma.public, public) == 0)) {
+ /* conn has to be registered to be in conn_table */
+ _conn->refcount++;
+ conn = _conn;
+ }
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_unlock(&_conn->mutex);
+#endif
+
+ if (conn) break;
+ }
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&sock->conn_table.mutex);
+#endif
+#endif /* ECP_WITH_HTABLE */
+
+ return conn;
+}
+
void ecp_atag_gen(ECPSocket *sock, unsigned char *public_buf, unsigned char *atag, ecp_nonce_t *nonce) {
unsigned char msg[ECP_SIZE_ECDH_PUB+ECP_SIZE_NONCE];
ecp_hmac_key_t key;
@@ -1154,16 +1205,17 @@ int ecp_conn_insert_gc(ECPConnection *conn) {
ECPSocket *sock = conn->sock;
int rv = ECP_OK;
+#ifdef ECP_WITH_HTABLE
#ifdef ECP_WITH_PTHREAD
pthread_mutex_lock(&sock->conn_table.mutex_gc);
#endif
+#endif
rv = ecp_conn_refcount_inc(conn);
if (!rv) {
#ifdef ECP_WITH_HTABLE
rv = ecp_ht_insert(sock->conn_table.keys_gc, &conn->remote.key_perma.public, conn);
- if (rv) ecp_conn_refcount_dec(conn);
#else /* ECP_WITH_HTABLE */
@@ -1171,18 +1223,23 @@ int ecp_conn_insert_gc(ECPConnection *conn) {
pthread_mutex_lock(&conn->mutex);
#endif
- _ecp_conn_push_gct(conn);
+ if (!_ecp_conn_is_reg(conn)) rv = ECP_CONN_CLOSED;
+ if (!rv) _ecp_conn_push_gct(conn);
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&conn->mutex);
#endif
#endif /* ECP_WITH_HTABLE */
+
+ if (rv) ecp_conn_refcount_dec(conn);
}
+#ifdef ECP_WITH_HTABLE
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&sock->conn_table.mutex_gc);
#endif
+#endif
return rv;
}
@@ -1199,8 +1256,8 @@ int _ecp_conn_remove(ECPConnection *conn) {
if (!_ecp_conn_is_reg(conn)) rv = ECP_ERR_CLOSED;
if (!rv && _ecp_conn_is_open(conn)) rv = ECP_ERR_BUSY;
if (!rv) {
- conn_table_remove(conn);
_ecp_conn_clr_reg(conn);
+ conn_table_remove(conn);
}
#ifdef ECP_WITH_PTHREAD
@@ -1220,8 +1277,8 @@ void ecp_conn_remove(ECPConnection *conn) {
#endif
if (_ecp_conn_is_reg(conn)) {
- conn_table_remove(conn);
_ecp_conn_clr_reg(conn);
+ conn_table_remove(conn);
}
#ifdef ECP_WITH_PTHREAD
@@ -1250,12 +1307,17 @@ void ecp_conn_remove_gc(ECPConnection *conn) {
ECPSocket *sock = conn->sock;
ECPConnection *_conn = NULL;
+#ifdef ECP_WITH_HTABLE
#ifdef ECP_WITH_PTHREAD
pthread_mutex_lock(&sock->conn_table.mutex_gc);
#endif
-#ifdef ECP_WITH_HTABLE
_conn = ecp_ht_remove_kv(sock->conn_table.keys_gc, &conn->remote.key_perma.public, conn);
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_unlock(&sock->conn_table.mutex_gc);
+#endif
+
#else /* ECP_WITH_HTABLE */
#ifdef ECP_WITH_PTHREAD
@@ -1273,10 +1335,6 @@ void ecp_conn_remove_gc(ECPConnection *conn) {
#endif /* ECP_WITH_HTABLE */
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&sock->conn_table.mutex_gc);
-#endif
-
if (_conn) {
ecp_conn_refcount_dec(conn);
}
diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h
index 8b76895..36c5e42 100644
--- a/ecp/src/ecp/core.h
+++ b/ecp/src/ecp/core.h
@@ -399,6 +399,7 @@ int ecp_sock_dhkey_new(ECPSocket *sock);
int ecp_sock_dhkey_get(ECPSocket *sock, unsigned char idx, ECPDHKey *key);
int ecp_sock_dhkey_get_pub(ECPSocket *sock, unsigned char *idx, ecp_ecdh_public_t *public);
void ecp_sock_expire(ECPSocket *sock, ecp_conn_expired_t conn_expired);
+ECPConnection *ecp_sock_search_gc(ECPSocket *sock, ecp_ecdh_public_t *public);
void ecp_atag_gen(ECPSocket *sock, unsigned char *public_buf, unsigned char *atag, ecp_nonce_t *nonce);
int ecp_cookie_verify(ECPSocket *sock, unsigned char *cookie, unsigned char *public_buf);