summaryrefslogtreecommitdiff
path: root/ecp/src/core.c
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2021-08-28 02:58:35 +0200
committerUros Majstorovic <majstor@majstor.org>2021-08-28 02:58:35 +0200
commitcdaa2560c0271585317450b57047a7f812d8366e (patch)
tree325f726cb1ab1fd81a20faa6b52d2679591946c4 /ecp/src/core.c
parent76ec318118106cb25d762de83857f6579c13e273 (diff)
code cleanup
Diffstat (limited to 'ecp/src/core.c')
-rw-r--r--ecp/src/core.c301
1 files changed, 141 insertions, 160 deletions
diff --git a/ecp/src/core.c b/ecp/src/core.c
index ecec401..d0a303a 100644
--- a/ecp/src/core.c
+++ b/ecp/src/core.c
@@ -33,7 +33,7 @@ int ecp_node_init(ECPNode *node, ecp_dh_public_t *public, void *addr) {
return ECP_OK;
}
-int ecp_ctx_create(ECPContext *ctx) {
+int ecp_ctx_init(ECPContext *ctx) {
int rv;
if (ctx == NULL) return ECP_ERR;
@@ -48,10 +48,6 @@ int ecp_ctx_create(ECPContext *ctx) {
return ECP_OK;
}
-int ecp_ctx_destroy(ECPContext *ctx) {
- return ECP_OK;
-}
-
static int ctable_create(ECPSockCTable *conn, ECPContext *ctx) {
int rv = ECP_OK;
@@ -85,116 +81,92 @@ static void ctable_destroy(ECPSockCTable *conn, ECPContext *ctx) {
}
static int ctable_insert(ECPConnection *conn) {
- int with_htable = 0;
ECPSocket *sock = conn->sock;
#ifdef ECP_WITH_HTABLE
- with_htable = 1;
-#endif
-
- if (with_htable) {
-#ifdef ECP_WITH_HTABLE
- int i, rv = ECP_OK;
- if (conn->out) {
- for (i=0; i<ECP_MAX_CONN_KEY; i++) {
- if (conn->key[i].valid) rv = ecp_ht_insert(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[i].public), conn);
- if (rv) {
- int j;
- for (j=0; j<i; j++) if (conn->key[j].valid) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[j].public));
- return rv;
- }
- }
- } else {
- ECPDHRKeyBucket *remote = &conn->remote;
+ int i, rv = ECP_OK;
- for (i=0; i<ECP_MAX_NODE_KEY; i++) {
- if (remote->key[i].idx != ECP_ECDH_IDX_INV) rv = ecp_ht_insert(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[i].public), conn);
- if (rv) {
- int j;
- for (j=0; j<i; j++) if (remote->key[j].idx != ECP_ECDH_IDX_INV) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[j].public));
- return rv;
- }
+ if (conn->out) {
+ for (i=0; i<ECP_MAX_CONN_KEY; i++) {
+ if (conn->key[i].valid) rv = ecp_ht_insert(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[i].public), conn);
+ if (rv) {
+ int j;
+ for (j=0; j<i; j++) if (conn->key[j].valid) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[j].public));
+ return rv;
}
}
-#endif
} else {
-#ifndef ECP_WITH_HTABLE
- if (sock->conn.size == ECP_MAX_SOCK_CONN) return ECP_ERR_MAX_SOCK_CONN;
- sock->conn.array[sock->conn.size] = conn;
- sock->conn.size++;
-#endif
+ ECPDHRKeyBucket *remote = &conn->remote;
+
+ for (i=0; i<ECP_MAX_NODE_KEY; i++) {
+ if (remote->key[i].idx != ECP_ECDH_IDX_INV) rv = ecp_ht_insert(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[i].public), conn);
+ if (rv) {
+ int j;
+ for (j=0; j<i; j++) if (remote->key[j].idx != ECP_ECDH_IDX_INV) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[j].public));
+ return rv;
+ }
+ }
}
+#else
+ if (sock->conn.size == ECP_MAX_SOCK_CONN) return ECP_ERR_MAX_SOCK_CONN;
+ sock->conn.array[sock->conn.size] = conn;
+ sock->conn.size++;
+#endif
return ECP_OK;
}
static void ctable_remove(ECPConnection *conn) {
- int i, with_htable = 0;
+ int i;
ECPSocket *sock = conn->sock;
#ifdef ECP_WITH_HTABLE
- with_htable = 1;
-#endif
-
- if (with_htable) {
-#ifdef ECP_WITH_HTABLE
- if (conn->out) {
- for (i=0; i<ECP_MAX_CONN_KEY; i++) if (conn->key[i].valid) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[i].public));
- } else {
- ECPDHRKeyBucket *remote = &conn->remote;
- for (i=0; i<ECP_MAX_NODE_KEY; i++) if (remote->key[i].idx != ECP_ECDH_IDX_INV) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[i].public));
- }
-#endif
+ if (conn->out) {
+ for (i=0; i<ECP_MAX_CONN_KEY; i++) if (conn->key[i].valid) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[i].public));
} else {
-#ifndef ECP_WITH_HTABLE
- for (i=0; i<sock->conn.size; i++) {
- if (conn == sock->conn.array[i]) {
- sock->conn.array[i] = sock->conn.array[sock->conn.size-1];
- sock->conn.array[sock->conn.size-1] = NULL;
- sock->conn.size--;
- return;
- }
+ ECPDHRKeyBucket *remote = &conn->remote;
+ for (i=0; i<ECP_MAX_NODE_KEY; i++) if (remote->key[i].idx != ECP_ECDH_IDX_INV) ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[i].public));
+ }
+#else
+ for (i=0; i<sock->conn.size; i++) {
+ if (conn == sock->conn.array[i]) {
+ sock->conn.array[i] = sock->conn.array[sock->conn.size-1];
+ sock->conn.array[sock->conn.size-1] = NULL;
+ sock->conn.size--;
+ return;
}
-#endif
}
+#endif
}
static ECPConnection *ctable_search(ECPSocket *sock, unsigned char c_idx, unsigned char *c_public, ECPNetAddr *addr) {
- int i, with_htable = 0;
- ECPConnection *conn = NULL;
-
#ifdef ECP_WITH_HTABLE
- with_htable = 1;
-#endif
+ return ecp_ht_search(sock->conn.htable, c_public);
+#else
+ ECPConnection *conn = NULL;
+ int i;
- if (with_htable) {
-#ifdef ECP_WITH_HTABLE
- return ecp_ht_search(sock->conn.htable, c_public);
-#endif
- } else {
-#ifndef ECP_WITH_HTABLE
- if (c_public) {
- for (i=0; i<sock->conn.size; i++) {
- conn = sock->conn.array[i];
- if (conn->out) {
- if ((c_idx < ECP_MAX_CONN_KEY) && conn->key[c_idx].valid && ecp_cr_dh_pub_eq(c_public, &conn->key[c_idx].public))
- return conn;
- } else {
- if ((c_idx < ECP_MAX_SOCK_KEY) && (conn->remote.key_idx_map[c_idx] != ECP_ECDH_IDX_INV) && ecp_cr_dh_pub_eq(c_public, &conn->remote.key[conn->remote.key_idx_map[c_idx]].public))
- return conn;
- }
- }
- } else if (addr) {
- /* in case server is not returning client's public key in packet */
- for (i=0; i<sock->conn.size; i++) {
- conn = sock->conn.array[i];
- if (conn->out && ecp_tr_addr_eq(&conn->node.addr, addr)) return conn;
+ if (c_public) {
+ for (i=0; i<sock->conn.size; i++) {
+ conn = sock->conn.array[i];
+ if (conn->out) {
+ if ((c_idx < ECP_MAX_CONN_KEY) && conn->key[c_idx].valid && ecp_cr_dh_pub_eq(c_public, &conn->key[c_idx].public))
+ return conn;
+ } else {
+ if ((c_idx < ECP_MAX_SOCK_KEY) && (conn->remote.key_idx_map[c_idx] != ECP_ECDH_IDX_INV) && ecp_cr_dh_pub_eq(c_public, &conn->remote.key[conn->remote.key_idx_map[c_idx]].public))
+ return conn;
}
}
-#endif
+ } else if (addr) {
+ /* in case server is not returning client's public key in packet */
+ for (i=0; i<sock->conn.size; i++) {
+ conn = sock->conn.array[i];
+ if (conn->out && ecp_tr_addr_eq(&conn->node.addr, addr)) return conn;
+ }
}
return NULL;
+#endif
}
int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key) {
@@ -1665,79 +1637,19 @@ ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *conten
return rv;
}
+#if defined(ECP_WITH_RBUF) && defined(ECP_WITH_MSGQ)
ssize_t ecp_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ecp_cts_t timeout) {
-#ifdef ECP_WITH_RBUF
-#ifdef ECP_WITH_MSGQ
+ ssize_t rv;
+
pthread_mutex_lock(&conn->rbuf.recv->msgq.mutex);
- ssize_t rv = ecp_conn_msgq_pop(conn, mtype, msg, msg_size, timeout);
+ rv = ecp_conn_msgq_pop(conn, mtype, msg, msg_size, timeout);
pthread_mutex_unlock(&conn->rbuf.recv->msgq.mutex);
+
return rv;
+}
#else
+ssize_t ecp_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ecp_cts_t timeout) {
return ECP_ERR_NOT_IMPLEMENTED;
-#endif
-#else
- return ECP_ERR_NOT_IMPLEMENTED;
-#endif
-}
-
-#ifdef ECP_DEBUG
-static char *_utoa(unsigned value, char *str, int base) {
- const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
- int i, j;
- unsigned remainder;
- char c;
-
- /* Check base is supported. */
- if ((base < 2) || (base > 36))
- {
- str[0] = '\0';
- return NULL;
- }
-
- /* Convert to string. Digits are in reverse order. */
- i = 0;
- do
- {
- remainder = value % base;
- str[i++] = digits[remainder];
- value = value / base;
- } while (value != 0);
- str[i] = '\0';
-
- /* Reverse string. */
- for (j = 0, i--; j < i; j++, i--)
- {
- c = str[j];
- str[j] = str[i];
- str[i] = c;
- }
-
- return str;
-}
-
-static char *_itoa(int value, char *str, int base) {
- unsigned uvalue;
- int i = 0;
-
- /* Check base is supported. */
- if ((base < 2) || (base > 36))
- {
- str[0] = '\0';
- return NULL;
- }
-
- /* Negative numbers are only supported for decimal.
- * Cast to unsigned to avoid overflow for maximum negative value. */
- if ((base == 10) && (value < 0))
- {
- str[i++] = '-';
- uvalue = (unsigned)-value;
- }
- else
- uvalue = (unsigned)value;
-
- _utoa(uvalue, &str[i], base);
- return str;
}
#endif
@@ -1758,6 +1670,11 @@ static int recv_p(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t s
return ECP_OK;
}
+#ifdef ECP_DEBUG
+static char *_utoa(unsigned value, char *str, int base);
+static char *_itoa(int value, char *str, int base);
+#endif
+
int ecp_receiver(ECPSocket *sock) {
ECPNetAddr addr;
ECPBuffer packet;
@@ -1790,27 +1707,91 @@ int ecp_receiver(ECPSocket *sock) {
#ifdef ECP_WITH_PTHREAD
static void *_ecp_receiver(void *arg) {
ecp_receiver((ECPSocket *)arg);
+ return NULL;
pthread_exit(NULL);
}
-#endif
int ecp_start_receiver(ECPSocket *sock) {
-#ifdef ECP_WITH_PTHREAD
int rv = pthread_create(&sock->rcvr_thd, NULL, _ecp_receiver, sock);
if (rv) return ECP_ERR;
return ECP_OK;
-#else
- return ECP_ERR_NOT_IMPLEMENTED;
-#endif
}
int ecp_stop_receiver(ECPSocket *sock) {
+ int rv;
+
sock->running = 0;
-#ifdef ECP_WITH_PTHREAD
- int rv = pthread_join(sock->rcvr_thd, NULL);
+ rv = pthread_join(sock->rcvr_thd, NULL);
if (rv) return ECP_ERR;
return ECP_OK;
+}
#else
+int ecp_start_receiver(ECPSocket *sock) {
return ECP_ERR_NOT_IMPLEMENTED;
+}
+
+int ecp_stop_receiver(ECPSocket *sock) {
+ return ECP_ERR_NOT_IMPLEMENTED;
+}
#endif
+
+#ifdef ECP_DEBUG
+static char *_utoa(unsigned value, char *str, int base) {
+ const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
+ int i, j;
+ unsigned remainder;
+ char c;
+
+ /* Check base is supported. */
+ if ((base < 2) || (base > 36))
+ {
+ str[0] = '\0';
+ return NULL;
+ }
+
+ /* Convert to string. Digits are in reverse order. */
+ i = 0;
+ do
+ {
+ remainder = value % base;
+ str[i++] = digits[remainder];
+ value = value / base;
+ } while (value != 0);
+ str[i] = '\0';
+
+ /* Reverse string. */
+ for (j = 0, i--; j < i; j++, i--)
+ {
+ c = str[j];
+ str[j] = str[i];
+ str[i] = c;
+ }
+
+ return str;
+}
+
+static char *_itoa(int value, char *str, int base) {
+ unsigned uvalue;
+ int i = 0;
+
+ /* Check base is supported. */
+ if ((base < 2) || (base > 36))
+ {
+ str[0] = '\0';
+ return NULL;
+ }
+
+ /* Negative numbers are only supported for decimal.
+ * Cast to unsigned to avoid overflow for maximum negative value. */
+ if ((base == 10) && (value < 0))
+ {
+ str[i++] = '-';
+ uvalue = (unsigned)-value;
+ }
+ else
+ uvalue = (unsigned)value;
+
+ _utoa(uvalue, &str[i], base);
+ return str;
}
+#endif