From cdaa2560c0271585317450b57047a7f812d8366e Mon Sep 17 00:00:00 2001
From: Uros Majstorovic <majstor@majstor.org>
Date: Sat, 28 Aug 2021 02:58:35 +0200
Subject: code cleanup

---
 ecp/src/core.c        | 301 +++++++++++++++++++++++---------------------------
 ecp/src/core.h        |   3 +-
 ecp/src/vconn/vconn.c |  79 +++++++------
 ecp/src/vconn/vconn.h |   3 +-
 ecp/test/init.c       |  10 +-
 ecp/test/init_vconn.c |  13 ++-
 ecp/util/mknode.c     |  13 ++-
 7 files changed, 209 insertions(+), 213 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
diff --git a/ecp/src/core.h b/ecp/src/core.h
index 7ab116e..3c62450 100644
--- a/ecp/src/core.h
+++ b/ecp/src/core.h
@@ -294,8 +294,7 @@ int ecp_init(ECPContext *ctx);
 int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key);
 int ecp_node_init(ECPNode *node, ecp_dh_public_t *public, void *addr);
 
-int ecp_ctx_create(ECPContext *ctx);
-int ecp_ctx_destroy(ECPContext *ctx);
+int ecp_ctx_init(ECPContext *ctx);
 
 int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key);
 void ecp_sock_destroy(ECPSocket *sock);
diff --git a/ecp/src/vconn/vconn.c b/ecp/src/vconn/vconn.c
index 348b2f2..bd5e94c 100644
--- a/ecp/src/vconn/vconn.c
+++ b/ecp/src/vconn/vconn.c
@@ -8,15 +8,15 @@
 #include "vconn.h"
 
 #ifdef ECP_WITH_HTABLE
-#ifdef ECP_WITH_PTHREAD
 static void *key_perma_table;
 static void *key_next_table;
+#ifdef ECP_WITH_PTHREAD
 static pthread_mutex_t key_perma_mutex;
 static pthread_mutex_t key_next_mutex;
 #endif
 #endif
 
-    
+
 static unsigned char key_null[ECP_ECDH_SIZE_KEY] = { 0 };
 
 static ECPConnHandler handler_vc;
@@ -27,7 +27,7 @@ static ECPConnHandler handler_vl;
 static int vconn_create(ECPConnection *conn, unsigned char *payload, size_t size) {
     ECPVConnIn *conn_v = (ECPVConnIn *)conn;
     int rv = ECP_OK;
-    
+
     if (conn->out) return ECP_ERR;
     if (conn->type != ECP_CTYPE_VCONN) return ECP_ERR;
     if (size < 2*ECP_ECDH_SIZE_KEY) return ECP_ERR;
@@ -45,7 +45,7 @@ static int vconn_create(ECPConnection *conn, unsigned char *payload, size_t size
 #ifdef ECP_WITH_PTHREAD
     pthread_mutex_unlock(&key_next_mutex);
 #endif
-    
+
     return rv;
 }
 
@@ -76,7 +76,7 @@ static ssize_t _vconn_send_open(ECPConnection *conn, ECPTimerItem *ti) {
     ECPBuffer payload;
     unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn)];
     unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn)];
-    
+
     packet.buffer = pkt_buf;
     packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn);
     payload.buffer = pld_buf;
@@ -152,7 +152,7 @@ static ssize_t vconn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch
         rv = ECP_ERR;
 
 #endif  /* ECP_WITH_HTABLE */
-    
+
         if (rv) return rv;
 
         return 1+2*ECP_ECDH_SIZE_KEY;
@@ -170,7 +170,7 @@ static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c
     if (conn->out) return ECP_ERR;
     if (conn->type != ECP_CTYPE_VCONN) return ECP_ERR;
     if (b == NULL) return ECP_ERR;
-    
+
     if (size < 0) return size;
     if (size < ECP_MIN_PKT) return ECP_ERR_MIN_PKT;
 
@@ -192,7 +192,7 @@ static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c
 #endif
 
     if (conn_out == NULL) return ECP_ERR;
-    
+
     ECPBuffer payload;
     payload.buffer = msg - ECP_SIZE_PLD_HDR - 1;
     payload.size = b->payload->size - (payload.buffer - b->payload->buffer);
@@ -214,7 +214,7 @@ static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c
 
 static int vlink_insert(ECPConnection *conn) {
     int rv = ECP_OK;
-    
+
 #ifdef ECP_WITH_PTHREAD
     pthread_mutex_lock(&key_perma_mutex);
 #endif
@@ -263,7 +263,7 @@ static ssize_t _vlink_send_open(ECPConnection *conn, ECPTimerItem *ti) {
     unsigned char pld_buf[ECP_SIZE_PLD_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ, conn)];
     unsigned char *buf = ecp_pld_get_buf(pld_buf, ECP_MTYPE_OPEN_REQ);
     int rv = ECP_OK;
-    
+
     packet.buffer = pkt_buf;
     packet.size = ECP_SIZE_PKT_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ, conn);
     payload.buffer = pld_buf;
@@ -290,11 +290,11 @@ static void vlink_close(ECPConnection *conn) {
 static ssize_t vlink_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) {
     ssize_t rv;
     int is_open;
-    
+
     if (conn->type != ECP_CTYPE_VLINK) return ECP_ERR;
 
     if (size < 0) return size;
-    
+
 #ifdef ECP_WITH_PTHREAD
     pthread_mutex_lock(&conn->mutex);
 #endif
@@ -324,10 +324,10 @@ static ssize_t vlink_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch
 
         // XXX should verify perma_key
         return rv+ECP_ECDH_SIZE_KEY;
-        
+
 #else   /* ECP_WITH_HTABLE */
 
-        return ECP_ERR; 
+        return ECP_ERR;
 
 #endif  /* ECP_WITH_HTABLE */
     }
@@ -466,10 +466,10 @@ static ssize_t ecp_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_
     if (conn->parent) {
         ECPBuffer payload_;
         unsigned char pld_buf[ECP_MAX_PLD];
-        
+
         payload_.buffer = pld_buf;
         payload_.size = ECP_MAX_PLD;
-        
+
         unsigned char mtype = ecp_pld_get_type(payload->buffer);
         ssize_t rv, hdr_size = vconn_set_msg(conn->parent, &payload_, mtype);
         if (hdr_size < 0) return hdr_size;
@@ -489,7 +489,7 @@ static ssize_t ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *p
     if (parent) {
         ECPBuffer payload_;
         unsigned char pld_buf[ECP_MAX_PLD];
-        
+
         payload_.buffer = pld_buf;
         payload_.size = ECP_MAX_PLD;
 
@@ -507,15 +507,12 @@ static ssize_t ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *p
 }
 */
 
-int ecp_ctx_create_vconn(ECPContext *ctx) {
+int ecp_vconn_ctx_init(ECPContext *ctx) {
     int rv;
-    
-    rv = ecp_ctx_create(ctx);
-    if (rv) return rv;
 
     rv = ecp_conn_handler_init(&handler_vc);
     if (rv) return rv;
-    
+
 #ifdef ECP_WITH_HTABLE
     handler_vc.conn_create = vconn_create;
     handler_vc.conn_destroy = vconn_destroy;
@@ -545,23 +542,39 @@ int ecp_ctx_create_vconn(ECPContext *ctx) {
     ctx->handler[ECP_CTYPE_VLINK] = &handler_vl;
 
 #ifdef ECP_WITH_HTABLE
-#ifdef ECP_WITH_PTHREAD
-    rv = pthread_mutex_init(&key_perma_mutex, NULL);
-    if (!rv) pthread_mutex_init(&key_next_mutex, NULL);
-    if (rv) return ECP_ERR;
-#endif
     key_perma_table = ecp_ht_create(ctx);
+    if (key_perma_table == NULL) {
+        return ECP_ERR;
+    }
     key_next_table = ecp_ht_create(ctx);
-    if ((key_perma_table == NULL) || (key_next_table == NULL)) return ECP_ERR;
+    if (key_next_table == NULL) {
+        ecp_ht_destroy(key_perma_table);
+        return ECP_ERR;
+    }
+#ifdef ECP_WITH_PTHREAD
+    rv = pthread_mutex_init(&key_perma_mutex, NULL);
+    if (rv) {
+        ecp_ht_destroy(key_next_table);
+        ecp_ht_destroy(key_perma_table);
+        return ECP_ERR;
+    }
+    rv = pthread_mutex_init(&key_next_mutex, NULL);
+    if (rv) {
+        pthread_mutex_destroy(&key_perma_mutex);
+        ecp_ht_destroy(key_next_table);
+        ecp_ht_destroy(key_perma_table);
+        return ECP_ERR;
+    }
+#endif  /* ECP_WITH_PTHREAD */
 #endif  /* ECP_WITH_HTABLE */
-    
+
     return ECP_OK;
 }
 
 int ecp_vconn_init(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size) {
     ECPSocket *sock = conn->sock;
     int i, rv;
-    
+
     rv = ecp_conn_init(conn, conn_node);
     if (rv) return rv;
 
@@ -587,7 +600,7 @@ int ecp_vconn_init(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn
             vconn[i].next = (ECPConnection *)&vconn[i+1];
         }
     }
-    
+
     return ECP_OK;
 }
 
@@ -596,7 +609,7 @@ static ssize_t _vconn_send_kget(ECPConnection *conn, ECPTimerItem *ti) {
     ECPBuffer payload;
     unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn)];
     unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn)];
-    
+
     packet.buffer = pkt_buf;
     packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn);
     payload.buffer = pld_buf;
@@ -612,6 +625,6 @@ int ecp_vconn_open(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn
 
     ssize_t _rv = ecp_timer_send((ECPConnection *)&vconn[0], _vconn_send_kget, ECP_MTYPE_KGET_REP, 3, 500);
     if (_rv < 0) return _rv;
-    
+
     return ECP_OK;
 }
\ No newline at end of file
diff --git a/ecp/src/vconn/vconn.h b/ecp/src/vconn/vconn.h
index dbf51f1..9e9078c 100644
--- a/ecp/src/vconn/vconn.h
+++ b/ecp/src/vconn/vconn.h
@@ -16,7 +16,6 @@ typedef struct ECPVConnIn {
     unsigned char key_out[ECP_ECDH_SIZE_KEY];
 } ECPVConnIn;
 
-int ecp_ctx_create_vconn(ECPContext *ctx);
-
+int ecp_vconn_ctx_init(ECPContext *ctx);
 int ecp_vconn_init(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size);
 int ecp_vconn_open(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size);
diff --git a/ecp/test/init.c b/ecp/test/init.c
index 7e39f87..4dff693 100644
--- a/ecp/test/init.c
+++ b/ecp/test/init.c
@@ -6,7 +6,7 @@
 
 static int v_rng(void *buf, size_t bufsize) {
     int fd;
-    
+
     if((fd = open("/dev/urandom", O_RDONLY)) < 0) return -1;
     size_t nb = read(fd, buf, bufsize);
     close(fd);
@@ -24,13 +24,13 @@ static void conn_free(ECPConnection *conn) {
 
 int ecp_init(ECPContext *ctx) {
     int rv;
-    
-    rv = ecp_ctx_create(ctx);
+
+    rv = ecp_ctx_init(ctx);
     if (rv) return rv;
-    
+
     ctx->rng = v_rng;
     ctx->conn_alloc = conn_alloc;
     ctx->conn_free = conn_free;
-    
+
     return ECP_OK;
 }
\ No newline at end of file
diff --git a/ecp/test/init_vconn.c b/ecp/test/init_vconn.c
index 7e3dd04..b79aa3d 100644
--- a/ecp/test/init_vconn.c
+++ b/ecp/test/init_vconn.c
@@ -7,7 +7,7 @@
 
 static int v_rng(void *buf, size_t bufsize) {
     int fd;
-    
+
     if((fd = open("/dev/urandom", O_RDONLY)) < 0) return -1;
     size_t nb = read(fd, buf, bufsize);
     close(fd);
@@ -30,13 +30,16 @@ static void conn_free(ECPConnection *conn) {
 
 int ecp_init(ECPContext *ctx) {
     int rv;
-    
-    rv = ecp_ctx_create_vconn(ctx);
+
+    rv = ecp_ctx_init(ctx);
+    if (rv) return rv;
+
+    rv = ecp_vconn_ctx_init(ctx);
     if (rv) return rv;
-    
+
     ctx->rng = v_rng;
     ctx->conn_alloc = conn_alloc;
     ctx->conn_free = conn_free;
-    
+
     return ECP_OK;
 }
\ No newline at end of file
diff --git a/ecp/util/mknode.c b/ecp/util/mknode.c
index a601a45..882c58d 100644
--- a/ecp/util/mknode.c
+++ b/ecp/util/mknode.c
@@ -14,7 +14,7 @@ static char fn_node[FN_LEN];
 
 static int v_rng(void *buf, size_t bufsize) {
     int fd;
-    
+
     if((fd = open("/dev/urandom", O_RDONLY)) < 0) return -1;
     size_t nb = read(fd, buf, bufsize);
     close(fd);
@@ -32,7 +32,7 @@ int main(int argc, char *argv[]) {
     ECPContext ctx;
     ECPDHKey key;
     ECPNode node;
-    
+
     if ((argc < 2) || (argc > 3)) usage(argv[0]);
 
     if (strlen(argv[1]) > FN_LEN - 6) usage(argv[0]);
@@ -40,11 +40,11 @@ int main(int argc, char *argv[]) {
     strcpy(fn_key, argv[1]);
     strcat(fn_key, ".priv");
     strcat(fn_node, ".pub");
-    
-    rv = ecp_ctx_create(&ctx);
+
+    rv = ecp_ctx_init(&ctx);
     if (rv) goto err;
     ctx.rng = v_rng;
-    
+
     rv = ecp_dhkey_generate(&ctx, &key);
     if (rv) goto err;
 
@@ -58,7 +58,8 @@ int main(int argc, char *argv[]) {
     if (rv) goto err;
 
     return 0;
-    err:
+
+err:
     printf("ERR:%d\n", rv);
     return 1;
 }
\ No newline at end of file
-- 
cgit v1.2.3