From 84735c23109b77e0691da208d31c374aa943b257 Mon Sep 17 00:00:00 2001
From: Uros Majstorovic <majstor@majstor.org>
Date: Sat, 19 Oct 2019 19:37:03 +0200
Subject: fixed transport interface; risc-v transport updated to support eos
 sock interface

---
 code/ecp/Makefile.fe310    |   2 +-
 code/ecp/core.c            | 213 +++++++++++++++++++++++----------------------
 code/ecp/core.h            |   2 +-
 code/ecp/fe310/Makefile    |   4 +-
 code/ecp/fe310/time.c      |  22 ++++-
 code/ecp/fe310/transport.c |  81 ++++++++++++-----
 code/ecp/fe310/transport.h |   9 +-
 code/ecp/posix/Makefile    |   2 +-
 code/ecp/posix/time.c      |   6 +-
 code/ecp/posix/transport.c |  30 ++++---
 code/ecp/tm.h              |   1 +
 code/ecp/tr.h              |   9 +-
 code/test/basic.c          |  24 ++---
 code/test/frag.c           |  30 +++----
 code/test/stress.c         |  36 ++++----
 15 files changed, 268 insertions(+), 203 deletions(-)

(limited to 'code')

diff --git a/code/ecp/Makefile.fe310 b/code/ecp/Makefile.fe310
index aae7793..7a13137 100644
--- a/code/ecp/Makefile.fe310
+++ b/code/ecp/Makefile.fe310
@@ -9,5 +9,5 @@ FE310_HOME = /opt/my/freedom-e-sdk
 CC = $(FE310_HOME)/work/build/riscv-gnu-toolchain/riscv64-unknown-elf/prefix/bin/riscv64-unknown-elf-gcc
 AR = $(FE310_HOME)/work/build/riscv-gnu-toolchain/riscv64-unknown-elf/prefix/bin/riscv64-unknown-elf-ar
 
-CFLAGS_PL = -DECP_WITH_VCONN=1 -DECP_DEBUG=1 -O3 -fno-builtin-printf -march=rv32imac -mabi=ilp32 -mcmodel=medany -I$(FE310_HOME)/bsp/include -I$(FE310_HOME)/bsp/drivers -I$(FE310_HOME)/bsp/env -I$(FE310_HOME)/bsp/env/freedom-e300-hifive1
+CFLAGS_PL = -DECP_WITH_VCONN=1 -DECP_DEBUG=1 -O3 -fno-builtin-printf -march=rv32imac -mabi=ilp32 -mcmodel=medany -I$(FE310_HOME)/bsp/include -I$(FE310_HOME)/bsp/drivers -I$(FE310_HOME)/bsp/env -I$(FE310_HOME)/bsp/env/freedom-e300-hifive1 -I../fe310 -I../../fe310
 # LDFLAGS_PL=-lm -pthread
\ No newline at end of file
diff --git a/code/ecp/core.c b/code/ecp/core.c
index ae348ac..b9efec4 100644
--- a/code/ecp/core.c
+++ b/code/ecp/core.c
@@ -10,7 +10,7 @@
 
 int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key) {
     int rv;
-    
+
     if (ctx->rng == NULL) return ECP_ERR_RNG;
 
     rv = ecp_cr_dh_mkpair(&key->public, &key->private, ctx->rng);
@@ -20,9 +20,9 @@ int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key) {
     return ECP_OK;
 }
 
-int ecp_node_init(ECPContext *ctx, ECPNode *node, ecp_dh_public_t *public, void *addr) {
+int ecp_node_init(ECPNode *node, ecp_dh_public_t *public, void *addr) {
     int rv = ECP_OK;
-    
+
     memset(node, 0, sizeof(ECPNode));
 
     if (public) memcpy(&node->public, public, sizeof(node->public));
@@ -35,11 +35,16 @@ int ecp_node_init(ECPContext *ctx, ECPNode *node, ecp_dh_public_t *public, void
 
 int ecp_ctx_create(ECPContext *ctx) {
     int rv;
-    
+
     if (ctx == NULL) return ECP_ERR;
 
     memset(ctx, 0, sizeof(ECPContext));
 
+    rv = ecp_tr_init(ctx);
+    if (rv) return rv;
+    rv = ecp_tm_init(ctx);
+    if (rv) return rv;
+
     return ECP_OK;
 }
 
@@ -49,7 +54,7 @@ int ecp_ctx_destroy(ECPContext *ctx) {
 
 static int ctable_create(ECPSockCTable *conn, ECPContext *ctx) {
     int rv = ECP_OK;
-    
+
     memset(conn, 0, sizeof(ECPSockCTable));
 
 #ifdef ECP_WITH_HTABLE
@@ -66,7 +71,7 @@ static int ctable_create(ECPSockCTable *conn, ECPContext *ctx) {
         return ECP_ERR;
     }
 #endif
-    
+
     return ECP_OK;
 }
 
@@ -86,7 +91,7 @@ static int ctable_insert(ECPConnection *conn) {
 #ifdef ECP_WITH_HTABLE
     with_htable = 1;
 #endif
-    
+
     if (with_htable) {
 #ifdef ECP_WITH_HTABLE
         int i, rv = ECP_OK;
@@ -126,7 +131,7 @@ static int ctable_insert(ECPConnection *conn) {
 static void ctable_remove(ECPConnection *conn) {
     int i, with_htable = 0;
     ECPSocket *sock = conn->sock;
-    
+
 #ifdef ECP_WITH_HTABLE
     with_htable = 1;
 #endif
@@ -157,7 +162,7 @@ static void ctable_remove(ECPConnection *conn) {
 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
@@ -193,8 +198,8 @@ static ECPConnection *ctable_search(ECPSocket *sock, unsigned char c_idx, unsign
 }
 
 int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key) {
-    int rv = ECP_OK; 
-    
+    int rv = ECP_OK;
+
     if (sock == NULL) return ECP_ERR;
     if (ctx == NULL) return ECP_ERR;
 
@@ -207,15 +212,15 @@ int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key) {
 
     rv = ecp_dhkey_generate(sock->ctx, &sock->key[sock->key_curr]);
     if (!rv) rv = ctable_create(&sock->conn, sock->ctx);
-    
+
     if (rv) return rv;
-    
+
     rv = ecp_timer_create(&sock->timer);
     if (rv) {
         ctable_destroy(&sock->conn, sock->ctx);
         return rv;
     }
-    
+
 #ifdef ECP_WITH_PTHREAD
     rv = pthread_mutex_init(&sock->mutex, NULL);
     if (rv) {
@@ -237,11 +242,11 @@ void ecp_sock_destroy(ECPSocket *sock) {
 }
 
 int ecp_sock_open(ECPSocket *sock, void *myaddr) {
-    return ecp_tr_open(&sock->sock, myaddr);
+    return ecp_tr_open(sock, myaddr);
 }
 
 void ecp_sock_close(ECPSocket *sock) {
-    ecp_tr_close(&sock->sock);
+    ecp_tr_close(sock);
 }
 
 int ecp_sock_dhkey_get_curr(ECPSocket *sock, unsigned char *idx, unsigned char *public) {
@@ -259,7 +264,7 @@ int ecp_sock_dhkey_get_curr(ECPSocket *sock, unsigned char *idx, unsigned char *
 #endif
 
     if (_idx == ECP_ECDH_IDX_INV) return ECP_ERR_ECDH_IDX;
-    
+
     if (idx) *idx = _idx;
     return ECP_OK;
 }
@@ -301,7 +306,7 @@ static int conn_dhkey_new_pair(ECPConnection *conn, ECPDHKey *key) {
         ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[conn->key_curr].public));
     }
 #endif
-    
+
     conn->key[conn->key_curr] = *key;
     conn->key_idx_map[conn->key_curr] = ECP_ECDH_IDX_INV;
 
@@ -311,19 +316,19 @@ static int conn_dhkey_new_pair(ECPConnection *conn, ECPDHKey *key) {
         if (rv) return rv;
     }
 #endif
-    
+
     return ECP_OK;
 }
 
 static void conn_dhkey_del_pair(ECPConnection *conn, unsigned char idx) {
     ECPSocket *sock = conn->sock;
-    
+
 #ifdef ECP_WITH_HTABLE
     if (conn->out && ecp_conn_is_reg(conn) && conn->key[idx].valid) {
         ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&conn->key[idx].public));
     }
 #endif
-    
+
     memset(&conn->key[idx], 0, sizeof(conn->key[idx]));
     conn->key_idx_map[idx] = ECP_ECDH_IDX_INV;
 }
@@ -332,7 +337,7 @@ static int conn_dhkey_new_pub_local(ECPConnection *conn, unsigned char idx) {
     // Remote obtained our key
     unsigned char new = conn->key_idx_curr == ECP_ECDH_IDX_INV ? 0 : (conn->key_idx_curr+1) % ECP_MAX_NODE_KEY;
     int i;
-    
+
     if (idx >= ECP_MAX_SOCK_KEY) return ECP_ERR_ECDH_IDX;
 
     if (conn->key_idx[new] != ECP_ECDH_IDX_INV) conn->key_idx_map[conn->key_idx[new]] = ECP_ECDH_IDX_INV;
@@ -362,7 +367,7 @@ static int conn_dhkey_new_pub_remote(ECPConnection *conn, unsigned char idx, uns
         ecp_ht_remove(sock->conn.htable, ecp_cr_dh_pub_get_buf(&remote->key[new].public));
     }
 #endif
-        
+
     if (remote->key[new].idx != ECP_ECDH_IDX_INV) remote->key_idx_map[remote->key[new].idx] = ECP_ECDH_IDX_INV;
     remote->key_idx_map[idx] = new;
     ecp_cr_dh_pub_from_buf(&remote->key[new].public, public);
@@ -375,7 +380,7 @@ static int conn_dhkey_new_pub_remote(ECPConnection *conn, unsigned char idx, uns
         if (rv) return rv;
     }
 #endif
-    
+
     for (i=0; i<ECP_MAX_NODE_KEY; i++) {
         conn->shared[i][new].valid = 0;
     }
@@ -386,13 +391,13 @@ static int conn_shsec_get(ECPConnection *conn, unsigned char s_idx, unsigned cha
     if (s_idx == ECP_ECDH_IDX_PERMA) {
         ECPDHKey *priv = NULL;
         ecp_dh_public_t *public_p = NULL;
-        
+
         if (conn->out) {
             public_p = &conn->node.public;
             priv = conn_dhkey_get(conn, c_idx);
         } else {
             ECPDHRKey *pub = NULL;
-            
+
             if (c_idx >= ECP_MAX_SOCK_KEY) return ECP_ERR_ECDH_IDX;
             if (conn->remote.key_idx_map[c_idx] == ECP_ECDH_IDX_INV) return ECP_ERR_ECDH_IDX_REMOTE;
 
@@ -428,7 +433,7 @@ static int conn_shsec_get(ECPConnection *conn, unsigned char s_idx, unsigned cha
 
         memcpy(shsec, &shared->secret, sizeof(shared->secret));
     }
-    
+
     return ECP_OK;
 }
 
@@ -436,7 +441,7 @@ static int conn_shsec_set(ECPConnection *conn, unsigned char s_idx, unsigned cha
     unsigned char l_idx = conn->out ? c_idx : s_idx;
     unsigned char r_idx = conn->out ? s_idx : c_idx;
     ECPDHShared *shared = NULL;
-    
+
     if ((l_idx >= ECP_MAX_SOCK_KEY) || (r_idx >= ECP_MAX_SOCK_KEY)) return ECP_ERR_ECDH_IDX;
 
     if (conn->key_idx_map[l_idx] == ECP_ECDH_IDX_INV) return ECP_ERR_ECDH_IDX_LOCAL;
@@ -445,20 +450,20 @@ static int conn_shsec_set(ECPConnection *conn, unsigned char s_idx, unsigned cha
     shared = &conn->shared[conn->key_idx_map[l_idx]][conn->remote.key_idx_map[r_idx]];
     memcpy(&shared->secret, shsec, sizeof(shared->secret));
     shared->valid = 1;
-    
+
     return ECP_OK;
 }
 
 int ecp_conn_create(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) {
     int i, rv = ECP_OK;
-    
+
     if (conn == NULL) return ECP_ERR;
     if (sock == NULL) return ECP_ERR;
 
     memset(conn, 0, sizeof(ECPConnection));
 
     if (ctype >= ECP_MAX_CTYPE) return ECP_ERR_MAX_CTYPE;
-        
+
     conn->type = ctype;
     conn->key_curr = ECP_ECDH_IDX_INV;
     conn->key_idx_curr = ECP_ECDH_IDX_INV;
@@ -512,7 +517,7 @@ void ecp_conn_unregister(ECPConnection *conn) {
     if (ecp_conn_is_reg(conn)) {
         ctable_remove(conn);
         conn->flags &= ~ECP_CONN_FLAG_REG;
-    } 
+    }
 #ifdef ECP_WITH_PTHREAD
     pthread_mutex_unlock(&conn->mutex);
     pthread_mutex_unlock(&sock->conn.mutex);
@@ -545,7 +550,7 @@ int ecp_conn_init(ECPConnection *conn, ECPNode *node) {
     rv = ecp_dhkey_generate(ctx, &key);
     if (!rv) rv = ctx->rng(conn->nonce, ECP_AEAD_SIZE_NONCE);
     if (!rv) rv = ctx->rng(&conn->seq_out, sizeof(conn->seq_out));
-    
+
     if (!rv) rv = conn_dhkey_new_pair(conn, &key);
     if (!rv) rv = conn_dhkey_new_pub_local(conn, conn->key_curr);
     if (!rv) rv = ecp_conn_register(conn);
@@ -555,7 +560,7 @@ int ecp_conn_init(ECPConnection *conn, ECPNode *node) {
 int ecp_conn_open(ECPConnection *conn, ECPNode *node) {
     int rv = ecp_conn_init(conn, node);
     if (rv) return rv;
-    
+
     ssize_t _rv = ecp_timer_send(conn, _conn_send_kget, ECP_MTYPE_KGET_REP, 3, 500);
     if (_rv < 0) {
         ecp_conn_unregister(conn);
@@ -568,7 +573,7 @@ int ecp_conn_open(ECPConnection *conn, ECPNode *node) {
 int ecp_conn_close(ECPConnection *conn, ecp_cts_t timeout) {
     ECPSocket *sock = conn->sock;
     int refcount = 0;
-    
+
     ecp_conn_unregister(conn);
     ecp_timer_remove(conn);
 
@@ -576,7 +581,7 @@ int ecp_conn_close(ECPConnection *conn, ecp_cts_t timeout) {
     pthread_mutex_lock(&conn->mutex);
     refcount = conn->refcount;
     pthread_mutex_unlock(&conn->mutex);
-    
+
     if (timeout && refcount) {
         ecp_tm_sleep_ms(timeout);
         pthread_mutex_lock(&conn->mutex);
@@ -605,7 +610,7 @@ int ecp_conn_close(ECPConnection *conn, ecp_cts_t timeout) {
         ecp_conn_destroy(conn);
         if (sock->ctx->conn_free) sock->ctx->conn_free(conn);
     }
-    
+
     return ECP_OK;
 }
 
@@ -666,7 +671,7 @@ static ssize_t _conn_send_open(ECPConnection *conn, ECPTimerItem *ti) {
     unsigned char pkt_buf[ECP_SIZE_PKT_BUF(1, ECP_MTYPE_OPEN_REQ, conn)];
     unsigned char pld_buf[ECP_SIZE_PLD_BUF(1, ECP_MTYPE_OPEN_REQ, conn)];
     unsigned char *buf = ecp_pld_get_buf(pld_buf, ECP_MTYPE_OPEN_REQ);
-    
+
     packet.buffer = pkt_buf;
     packet.size = ECP_SIZE_PKT_BUF(1, ECP_MTYPE_OPEN_REQ, conn);
     payload.buffer = pld_buf;
@@ -674,7 +679,7 @@ static ssize_t _conn_send_open(ECPConnection *conn, ECPTimerItem *ti) {
 
     ecp_pld_set_type(pld_buf, ECP_MTYPE_OPEN_REQ);
     buf[0] = conn->type;
-    
+
     return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(1, ECP_MTYPE_OPEN_REQ), 0, ti);
 }
 
@@ -688,9 +693,9 @@ int ecp_conn_handle_new(ECPSocket *sock, ECPConnection *parent, unsigned char *p
     unsigned char ctype = 0;
     ecp_conn_create_t *handle_create = NULL;
     ecp_conn_destroy_t *handle_destroy = NULL;
-    
+
     if (payload_size < 1) return ECP_ERR;
-    
+
     ctype = payload[0];
 
     conn = sock->ctx->conn_alloc ? sock->ctx->conn_alloc(ctype) : NULL;
@@ -702,13 +707,13 @@ int ecp_conn_handle_new(ECPSocket *sock, ECPConnection *parent, unsigned char *p
         if (sock->ctx->conn_free) sock->ctx->conn_free(conn);
         return rv;
     }
-    
+
     conn->refcount = 1;
     conn->parent = parent;
     conn->pcount = parent ? parent->pcount+1 : 0;
     handle_create = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->conn_create : NULL;
     handle_destroy = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->conn_destroy : NULL;
-    
+
     if (handle_create) rv = handle_create(conn, payload+1, payload_size-1);
     if (rv) {
         ecp_conn_destroy(conn);
@@ -779,7 +784,7 @@ ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char m
         ctype = msg[0];
         ecp_pld_set_type(pld_buf, ECP_MTYPE_OPEN_REP);
         ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_OPEN_REP), 0);
-        
+
         return 1;
     }
     return ECP_ERR;
@@ -788,9 +793,9 @@ ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char m
 ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) {
     if (mtype & ECP_MTYPE_FLAG_REP) {
         ECPContext *ctx = conn->sock->ctx;
-        
+
         if (!conn->out) return ECP_ERR;
-        
+
 #ifdef ECP_WITH_PTHREAD
         pthread_mutex_lock(&conn->mutex);
 #endif
@@ -816,7 +821,7 @@ ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char m
             }
         }
         if (rv) return rv;
-        
+
         return ECP_ECDH_SIZE_KEY+1;
     } else {
         if (conn->out) return ECP_ERR;
@@ -837,7 +842,7 @@ ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char m
         int rv = ecp_conn_dhkey_get_curr(conn, buf, buf+1);
         if (rv) return rv;
         ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP), 0);
-        
+
         return 0;
     }
     return ECP_ERR;
@@ -879,7 +884,7 @@ ssize_t ecp_conn_handle_exec(ECPConnection *conn, ecp_seq_t seq, unsigned char m
     if (size < 0) return size;
     if (b == NULL) return ECP_ERR;
     if (b->packet->buffer == NULL) return ECP_ERR;
-    
+
     memcpy(b->packet->buffer, msg, size);
     return ecp_pkt_handle(conn->sock, NULL, conn, b, size);
 }
@@ -889,7 +894,7 @@ ssize_t ecp_conn_handle_exec(ECPConnection *conn, ecp_seq_t seq, unsigned char m
 ssize_t ecp_conn_handle_exec(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) {
     if (size < 0) return size;
     if (b == NULL) return ECP_ERR;
-    
+
     ECP2Buffer b2;
     ECPBuffer packet;
     ECPBuffer payload;
@@ -902,7 +907,7 @@ ssize_t ecp_conn_handle_exec(ECPConnection *conn, ecp_seq_t seq, unsigned char m
     packet.size = b->payload->size - (msg - b->payload->buffer);
     payload.buffer = pld_buf;
     payload.size = ECP_MAX_PLD;
-    
+
     return ecp_pkt_handle(conn->sock, NULL, conn, &b2, size);
 }
 */
@@ -932,7 +937,7 @@ int ecp_conn_dhkey_new(ECPConnection *conn) {
     ECPDHKey new_key;
     int rv;
     ssize_t _rv;
-    
+
     rv = ecp_dhkey_generate(sock->ctx, &new_key);
     if (rv) return rv;
 
@@ -947,17 +952,17 @@ int ecp_conn_dhkey_new(ECPConnection *conn) {
 #endif
 
     if (rv) return rv;
-    
+
     _rv = ecp_timer_send(conn, _conn_send_kput, ECP_MTYPE_KPUT_REP, 3, 500);
     if (_rv < 0) return _rv;
-        
+
     return ECP_OK;
 }
 
 int ecp_conn_dhkey_new_pub(ECPConnection *conn, unsigned char idx, unsigned char *public) {
     ECPSocket *sock = conn->sock;
     int rv;
-    
+
     if (conn == NULL) return ECP_ERR;
     if (public == NULL) return ECP_ERR;
 
@@ -989,7 +994,7 @@ int ecp_conn_dhkey_get_curr(ECPConnection *conn, unsigned char *idx, unsigned ch
 #ifdef ECP_WITH_PTHREAD
     pthread_mutex_unlock(&conn->mutex);
 #endif
-    
+
     if (_idx == ECP_ECDH_IDX_INV) return ecp_sock_dhkey_get_curr(sock, idx, public);
 
     if (idx) *idx = _idx;
@@ -1000,7 +1005,7 @@ ssize_t ecp_pkt_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, un
     ssize_t rv;
 
     if (pkt_size < ECP_SIZE_PKT_HDR) return ECP_ERR;
-    
+
     // ECP_SIZE_PROTO
     packet[0] = 0;
     packet[1] = 0;
@@ -1024,10 +1029,10 @@ ssize_t ecp_pkt_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, un
 
 ssize_t ecp_pkt_pack_raw(ECPSocket *sock, ECPConnection *parent, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECPNetAddr *addr) {
     ECPContext *ctx = sock->ctx;
-    
+
     return ecp_pkt_pack(ctx, packet, pkt_size, s_idx, c_idx, public, shsec, nonce, seq, payload, pld_size);
 }
-    
+
 ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) {
     ecp_aead_key_t shsec;
     ecp_dh_public_t public;
@@ -1076,7 +1081,7 @@ ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_siz
 #ifdef ECP_WITH_RBUF
             if (conn->rbuf.send) rv = ecp_rbuf_pkt_prep(conn->rbuf.send, si, ecp_pld_get_type(payload));
 #endif
-                
+
             if (!rv && !si->seq_w) conn->seq_out = _seq;
         } else {
             _seq = conn->seq_out + 1;
@@ -1088,12 +1093,12 @@ ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_siz
 #ifdef ECP_WITH_PTHREAD
     pthread_mutex_unlock(&conn->mutex);
 #endif
-    
+
     if (rv) return rv;
-    
+
     ssize_t _rv = ecp_pkt_pack(conn->sock->ctx, packet, pkt_size, s_idx, c_idx, &public, &shsec, nonce, _seq, payload, pld_size);
     if (_rv < 0) return _rv;
-    
+
 #ifdef ECP_WITH_PTHREAD
     pthread_mutex_lock(&conn->mutex);
 #endif
@@ -1140,7 +1145,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP
     ecp_seq_t seq_c, seq_p, seq_n;
     ecp_ack_t seq_map;
     ssize_t dec_size;
-    
+
     *_conn = NULL;
     *_seq = 0;
 
@@ -1239,15 +1244,15 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP
         } else {
             return ECP_ERR_CONN_NOT_FOUND;
         }
-    } 
-    
+    }
+
     if (conn == NULL) return dec_size;
-    
+
     if (is_open) {
 #ifdef ECP_WITH_RBUF
         if (conn->rbuf.recv || (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_RBACK) || (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_RBFLUSH)) seq_check = 0;
 #endif
-        
+
         if (seq_check) {
             if (ECP_SEQ_LTE(seq_p, seq_c)) {
                 ecp_seq_t seq_offset = seq_c - seq_p;
@@ -1361,7 +1366,7 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent,
 }
 
 ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t pkt_size, unsigned char flags) {
-    ssize_t rv = ecp_tr_send(&sock->sock, packet, pkt_size, addr, flags);
+    ssize_t rv = ecp_tr_send(sock, packet, pkt_size, addr, flags);
     if (rv < 0) return rv;
     if (rv < ECP_MIN_PKT) return ECP_ERR_SEND;
 
@@ -1392,7 +1397,7 @@ unsigned char ecp_msg_get_type(unsigned char *msg) {
 
 unsigned char *ecp_msg_get_content(unsigned char *msg, size_t msg_size) {
     size_t offset = 1 + ECP_SIZE_MT_FLAG(msg[0]);
-    
+
     if (msg_size < offset) return NULL;
     return msg + offset;
 }
@@ -1400,7 +1405,7 @@ unsigned char *ecp_msg_get_content(unsigned char *msg, size_t msg_size) {
 int ecp_msg_get_frag(unsigned char *msg, size_t msg_size, unsigned char *frag_cnt, unsigned char *frag_tot) {
     if (!(msg[0] & ECP_MTYPE_FLAG_FRAG)) return ECP_ERR;
     if (msg_size < 3) return ECP_ERR;
-    
+
     *frag_cnt = msg[1];
     *frag_tot = msg[2];
 
@@ -1453,11 +1458,11 @@ int ecp_msg_defrag(ECPFragIter *iter, ecp_seq_t seq, unsigned char *msg_in, size
             if (iter->frag_cnt != frag_cnt) return ECP_ERR_ITER;
         }
         if ((1 + ECP_SIZE_MT_FLAG(mtype) + iter->frag_size * frag_tot) > iter->buf_size) return ECP_ERR_SIZE;
-        
+
         buf_offset = 1 + ECP_SIZE_MT_FLAG(mtype) + iter->frag_size * frag_cnt;
         memcpy(iter->buffer + buf_offset, content, content_size);
         iter->frag_cnt++;
-        
+
         if (iter->frag_cnt == frag_tot) {
             *msg_out = iter->buffer;
             *msg_out_size = buf_offset + content_size;
@@ -1479,7 +1484,7 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, s
     unsigned char mtype = 0;
     unsigned char *content = NULL;
     size_t rem_size = msg_size;
-    
+
     while (rem_size) {
         mtype = ecp_msg_get_type(msg);
         if ((mtype & ECP_MTYPE_MASK) >= ECP_MAX_MTYPE) return ECP_ERR_MAX_MTYPE;
@@ -1497,7 +1502,7 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, s
         content = ecp_msg_get_content(msg, rem_size);
         if (content == NULL) return ECP_ERR_MIN_MSG;
         rem_size -= content - msg;
-        
+
         handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->msg[mtype & ECP_MTYPE_MASK] : NULL;
         if (handler) {
             rv = handler(conn, seq, mtype, content, rem_size, bufs);
@@ -1558,7 +1563,7 @@ static ssize_t pld_send(ECPConnection *conn, ECPBuffer *packet, unsigned char s_
 #ifdef ECP_WITH_RBUF
     if (conn->rbuf.send) return ecp_rbuf_pkt_send(conn->rbuf.send, conn->sock, &addr, packet, rv, flags, ti, si);
 #endif
-    
+
     if (ti) {
         int _rv = ecp_timer_push(ti);
         if (_rv) return _rv;
@@ -1596,7 +1601,7 @@ ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *add
 
     rv = ecp_pack_raw(sock, parent, packet, s_idx, c_idx, public, shsec, nonce, seq, payload, pld_size, &_addr);
     if (rv < 0) return rv;
-    
+
     return ecp_pkt_send(sock, parent ? &_addr : addr, packet, rv, flags);
 }
 
@@ -1614,7 +1619,7 @@ ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *conten
     packet.size = ECP_MAX_PKT;
     payload.buffer = pld_buf;
     payload.size = ECP_MAX_PLD;
-    
+
     if (content_size + ECP_SIZE_MT_FLAG(mtype) > size_max) {
         int i;
         int _rv = ECP_OK;
@@ -1624,24 +1629,24 @@ ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *conten
 
         _rv = ecp_seq_item_init(&seq_item);
         if (_rv) return _rv;
-        
+
         mtype |= ECP_MTYPE_FLAG_FRAG;
         frag_size = size_max - ECP_SIZE_MT_FLAG(mtype);
         pkt_cnt = content_size / frag_size;
         frag_size_final = content_size - frag_size * pkt_cnt;
         if (frag_size_final) pkt_cnt++;
-        
+
 #ifdef ECP_WITH_PTHREAD
         pthread_mutex_lock(&conn->mutex);
 #endif
-        
+
         seq_start = conn->seq_out + 1;
         conn->seq_out += pkt_cnt;
-        
+
 #ifdef ECP_WITH_PTHREAD
         pthread_mutex_unlock(&conn->mutex);
 #endif
-        
+
         seq_item.seq_w = 1;
         for (i=0; i<pkt_cnt; i++) {
             if ((i == pkt_cnt - 1) && frag_size_final) frag_size = frag_size_final;
@@ -1685,56 +1690,56 @@ static char *_utoa(unsigned value, char *str, int base) {
   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 
+  do
     {
       remainder = value % base;
       str[i++] = digits[remainder];
       value = value / base;
-    } while (value != 0);  
-  str[i] = '\0'; 
-  
+    } 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; 
-    }       
-  
+      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.  */ 
+   * 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;
 }
@@ -1744,7 +1749,7 @@ static int recv_p(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t s
     ECP2Buffer bufs;
     ECPBuffer payload;
     unsigned char pld_buf[ECP_MAX_PLD];
-    
+
     bufs.packet = packet;
     bufs.payload = &payload;
 
@@ -1753,7 +1758,7 @@ static int recv_p(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t s
 
     ssize_t rv = ecp_pkt_handle(sock, addr, NULL, &bufs, size);
     if (rv < 0) return rv;
-    
+
     return ECP_OK;
 }
 
@@ -1769,7 +1774,7 @@ int ecp_receiver(ECPSocket *sock) {
         packet.buffer = pkt_buf;
         packet.size = ECP_MAX_PKT;
 
-        rv = ecp_tr_recv(&sock->sock, &packet, &addr, next ? next : sock->poll_timeout);
+        rv = ecp_tr_recv(sock, &packet, &addr, next ? next : sock->poll_timeout);
         if (rv > 0) {
             int _rv = recv_p(sock, &addr, &packet, rv);
 #ifdef ECP_DEBUG
diff --git a/code/ecp/core.h b/code/ecp/core.h
index dda96c5..419b563 100644
--- a/code/ecp/core.h
+++ b/code/ecp/core.h
@@ -292,7 +292,7 @@ typedef struct ECPConnection {
 int ecp_init(ECPContext *ctx);
 
 int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key);
-int ecp_node_init(ECPContext *ctx, ECPNode *node, ecp_dh_public_t *public, void *addr);
+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);
diff --git a/code/ecp/fe310/Makefile b/code/ecp/fe310/Makefile
index 1ccf322..66663f0 100644
--- a/code/ecp/fe310/Makefile
+++ b/code/ecp/fe310/Makefile
@@ -1,5 +1,5 @@
 include ../Makefile.platform
-CFLAGS=$(CFLAGS_PL) $(PIC) -I.. -I../../fe310
+CFLAGS=$(CFLAGS_PL) $(PIC) -I..
 
 obj_tr = transport.o
 obj_tm = time.o
@@ -9,7 +9,7 @@ obj_tm = time.o
 	$(CC) $(CFLAGS) -c $<
 
 all: libecptr.a libecptm.a
-	
+
 libecptr.a: $(obj_tr)
 	$(AR) rcs libecptr.a $(obj_tr)
 
diff --git a/code/ecp/fe310/time.c b/code/ecp/fe310/time.c
index 455c762..1346fea 100644
--- a/code/ecp/fe310/time.c
+++ b/code/ecp/fe310/time.c
@@ -1,21 +1,39 @@
 #include <core.h>
 #include <tr.h>
 
+#include <eos/eos.h>
+#include <eos/event.h>
 #include <eos/timer.h>
 
 #include "encoding.h"
 #include "platform.h"
 
+extern ECPSocket *_ecp_tr_sock;
+
+static void timer_handler(unsigned char type) {
+    ecp_cts_t next = ecp_timer_exe(_ecp_tr_sock);
+    if (next) {
+        volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
+        uint64_t tick = *mtime + next * (uint64_t)RTC_FREQ / 1000;
+        eos_timer_set(tick, EOS_TIMER_ETYPE_ECP, 0);
+    }
+}
+
+int ecp_tm_init(ECPContext *ctx) {
+    eos_timer_set_handler(EOS_TIMER_ETYPE_ECP, timer_handler, EOS_EVT_FLAG_NET_BUF_ACQ);
+    return ECP_OK;
+}
+
 ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec) {
     volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
-    
+
     uint64_t now_ms = *mtime * 1000 / RTC_FREQ;
     return now_ms + msec;
 }
 
 void ecp_tm_sleep_ms(ecp_cts_t msec) {
     volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
-    
+
     uint64_t now_ms = *mtime * 1000 / RTC_FREQ;
     while (*mtime * 1000 / RTC_FREQ < now_ms + msec);
 }
diff --git a/code/ecp/fe310/transport.c b/code/ecp/fe310/transport.c
index 61d3355..3493966 100644
--- a/code/ecp/fe310/transport.c
+++ b/code/ecp/fe310/transport.c
@@ -1,3 +1,5 @@
+#include <stdlib.h>
+
 #include <core.h>
 #include <tr.h>
 
@@ -6,6 +8,42 @@
 
 static unsigned char _flags = 0;
 
+ECPSocket *_ecp_tr_sock = NULL;
+
+static void packet_handler(unsigned char *buffer, uint16_t len) {
+    ECPNetAddr addr;
+
+    ECP2Buffer bufs;
+    ECPBuffer packet;
+    ECPBuffer payload;
+    unsigned char pld_buf[ECP_MAX_PLD];
+
+    bufs.packet = &packet;
+    bufs.payload = &payload;
+
+    packet.buffer = buffer+EOS_SOCK_SIZE_UDP_HDR;
+    packet.size = ECP_MAX_PKT;
+    payload.buffer = pld_buf;
+    payload.size = ECP_MAX_PLD;
+
+    eos_sock_getfrom(buffer, &addr);
+    ssize_t rv = ecp_pkt_handle(_ecp_tr_sock, &addr, NULL, &bufs, len-EOS_SOCK_SIZE_UDP_HDR);
+#ifdef ECP_DEBUG
+    if (rv < 0) {
+        char b[16];
+        puts("ERR:");
+        puts(itoa(rv, b, 10));
+        puts("\n");
+    }
+#endif
+    if (bufs.packet->buffer) eos_net_free(buffer, 0);
+    eos_net_release();
+}
+
+int ecp_tr_init(ECPContext *ctx) {
+    return ECP_OK;
+}
+
 int ecp_tr_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) {
     if (addr1->port != addr2->port) return 0;
     if (memcmp(addr1->host, addr2->host, sizeof(addr1->host)) != 0) return 0;
@@ -16,50 +54,49 @@ int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s) {
     return ECP_ERR;
 }
 
-int ecp_tr_open(int *sock, void *addr_s) {
-    *sock = 0;
+int ecp_tr_open(ECPSocket *sock, void *addr_s) {
+    sock->sock = eos_sock_open_udp();
+    if (sock->sock < 0) {
+        sock->sock = 0;
+        return ECP_ERR_SEND;
+    }
+    eos_sock_set_handler(sock->sock, packet_handler, 0);
+    _ecp_tr_sock = sock;
     return ECP_OK;
 }
 
-void ecp_tr_close(int *sock) {
+void ecp_tr_close(ECPSocket *sock) {
+    eos_sock_close(sock->sock);
+    _ecp_tr_sock = NULL;
 }
 
-ssize_t ecp_tr_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) {
+ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) {
     unsigned char *buf = NULL;
-    size_t addr_len = sizeof(addr->host) + sizeof(addr->port);
-    uint16_t buf_size = msg_size + addr_len;
-    unsigned char type = EOS_NET_MTYPE_SOCK;
     int rv;
 
     flags |= _flags;
-    if (flags & ECP_SEND_FLAG_MORE) {
-        type |= EOS_NET_MTYPE_FLAG_ONEW;
-    }
-    if (flags & ECP_SEND_FLAG_REPLY) {
-        if (packet && packet->buffer) {
-            buf = packet->buffer-addr_len;
+    if (packet && packet->buffer) {
+        if (flags & ECP_SEND_FLAG_REPLY) {
+            buf = packet->buffer-EOS_SOCK_SIZE_UDP_HDR;
             packet->buffer = NULL;
+        } else {
+            buf = eos_net_alloc();
+            memcpy(buf+EOS_SOCK_SIZE_UDP_HDR, packet->buffer, msg_size);
         }
-    } else {
-        buf = eos_net_alloc();
-        memcpy(buf+addr_len, packet->buffer, msg_size);
     }
     if (buf == NULL) return ECP_ERR;
-    memcpy(buf, addr->host, sizeof(addr->host));
-    memcpy(buf+sizeof(addr->host), &addr->port, sizeof(addr->port));
-    rv = eos_net_send(type, buf, buf_size);
+    rv = eos_sock_sendto(sock->sock, buf, msg_size+EOS_SOCK_SIZE_UDP_HDR, flags & ECP_SEND_FLAG_MORE, addr);
     if (rv) return ECP_ERR_SEND;
     return msg_size;
 }
 
-ssize_t ecp_tr_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) {
+ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) {
     return ECP_ERR;
 }
 
 void ecp_tr_buf_free(ECP2Buffer *b, unsigned char flags) {
-    size_t addr_len = ECP_IPv4_ADDR_SIZE + sizeof(uint16_t);
     if (b && b->packet && b->packet->buffer) {
-        eos_net_free(b->packet->buffer-addr_len, flags & ECP_SEND_FLAG_MORE);
+        eos_net_free(b->packet->buffer-EOS_SOCK_SIZE_UDP_HDR, flags & ECP_SEND_FLAG_MORE);
         b->packet->buffer = NULL;
     }
 }
diff --git a/code/ecp/fe310/transport.h b/code/ecp/fe310/transport.h
index c35e13e..0fcf43b 100644
--- a/code/ecp/fe310/transport.h
+++ b/code/ecp/fe310/transport.h
@@ -1,10 +1,5 @@
-#include <stdint.h>
-
-#define ECP_IPv4_ADDR_SIZE          4
+#include <eos/sock.h>
 
+typedef EOSNetAddr ECPNetAddr;
 typedef int ECPNetSock;
-typedef struct ECPNetAddr {
-    unsigned char host[ECP_IPv4_ADDR_SIZE];
-    uint16_t port;
-} ECPNetAddr;
 
diff --git a/code/ecp/posix/Makefile b/code/ecp/posix/Makefile
index 595b0b5..66663f0 100644
--- a/code/ecp/posix/Makefile
+++ b/code/ecp/posix/Makefile
@@ -9,7 +9,7 @@ obj_tm = time.o
 	$(CC) $(CFLAGS) -c $<
 
 all: libecptr.a libecptm.a
-	
+
 libecptr.a: $(obj_tr)
 	$(AR) rcs libecptr.a $(obj_tr)
 
diff --git a/code/ecp/posix/time.c b/code/ecp/posix/time.c
index 45d827e..111e598 100644
--- a/code/ecp/posix/time.c
+++ b/code/ecp/posix/time.c
@@ -4,10 +4,14 @@
 #include <core.h>
 #include <tm.h>
 
+int ecp_tm_init(ECPContext *ctx) {
+    return ECP_OK;
+}
+
 ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec) {
     struct timeval tv;
     ecp_cts_t ms_now;
-    
+
     gettimeofday(&tv, NULL);
     ms_now = tv.tv_sec * 1000 + tv.tv_usec / 1000;
     return ms_now + msec;
diff --git a/code/ecp/posix/transport.c b/code/ecp/posix/transport.c
index d6426d5..3c9b85b 100644
--- a/code/ecp/posix/transport.c
+++ b/code/ecp/posix/transport.c
@@ -9,6 +9,10 @@
 
 #define ADDR_S_MAX  32
 
+int ecp_tr_init(ECPContext *ctx) {
+    return ECP_OK;
+}
+
 int ecp_tr_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) {
     if (addr1->port != addr2->port) return 0;
     if (memcmp(addr1->host, addr2->host, sizeof(addr1->host)) != 0) return 0;
@@ -21,7 +25,7 @@ int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s) {
     char *colon = NULL;
     char *endptr = NULL;
 	uint16_t hport;
-    
+
     memset(addr_c, 0, sizeof(addr_c));
     strncpy(addr_c, addr_s, ADDR_S_MAX-1);
     colon = strchr(addr_c, ':');
@@ -38,7 +42,7 @@ int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s) {
     return 0;
 }
 
-int ecp_tr_open(int *sock, void *addr_s) {
+int ecp_tr_open(ECPSocket *sock, void *addr_s) {
     struct sockaddr_in _myaddr;
 
     memset((char *)&_myaddr, 0, sizeof(_myaddr));
@@ -55,42 +59,42 @@ int ecp_tr_open(int *sock, void *addr_s) {
         _myaddr.sin_port = htons(0);
     }
 
-    *sock = socket(PF_INET, SOCK_DGRAM, 0);
-    if (*sock < 0) return *sock;
+    sock->sock = socket(PF_INET, SOCK_DGRAM, 0);
+    if (sock->sock < 0) return sock->sock;
 
-    int rv = bind(*sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr));
+    int rv = bind(sock->sock, (struct sockaddr *)&_myaddr, sizeof(_myaddr));
     if (rv < 0) {
-        close(*sock);
+        close(sock->sock);
         return rv;
     }
     return ECP_OK;
 }
 
-void ecp_tr_close(int *sock) {
-    close(*sock);
+void ecp_tr_close(ECPSocket *sock) {
+    close(sock->sock);
 }
 
-ssize_t ecp_tr_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) {
+ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) {
     struct sockaddr_in servaddr;
 
     memset((void *)&servaddr, 0, sizeof(servaddr));
     servaddr.sin_family = AF_INET;
     servaddr.sin_port = addr->port;
     memcpy((void *)&servaddr.sin_addr, addr->host, sizeof(addr->host));
-    return sendto(*sock, packet->buffer, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
+    return sendto(sock->sock, packet->buffer, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
 }
 
-ssize_t ecp_tr_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) {
+ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) {
     struct sockaddr_in servaddr;
     socklen_t addrlen = sizeof(servaddr);
     struct pollfd fds[] = {
-        {*sock, POLLIN, 0}
+        {sock->sock, POLLIN, 0}
     };
 
     int rv = poll(fds, 1, timeout);
     memset((void *)&servaddr, 0, sizeof(servaddr));
     if (rv == 1) {
-        ssize_t recvlen = recvfrom(*sock, packet->buffer, packet->size, 0, (struct sockaddr *)&servaddr, &addrlen);
+        ssize_t recvlen = recvfrom(sock->sock, packet->buffer, packet->size, 0, (struct sockaddr *)&servaddr, &addrlen);
         if (recvlen < 0) return recvlen;
         if (recvlen < ECP_MIN_PKT) return ECP_ERR_RECV;
 
diff --git a/code/ecp/tm.h b/code/ecp/tm.h
index 77b175d..00c6fad 100644
--- a/code/ecp/tm.h
+++ b/code/ecp/tm.h
@@ -1,3 +1,4 @@
+int ecp_tm_init(ECPContext *ctx);
 ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec);
 void ecp_tm_sleep_ms(ecp_cts_t msec);
 void ecp_tm_timer_set(ecp_cts_t next);
diff --git a/code/ecp/tr.h b/code/ecp/tr.h
index c189925..e39dec0 100644
--- a/code/ecp/tr.h
+++ b/code/ecp/tr.h
@@ -1,9 +1,10 @@
+int ecp_tr_init(ECPContext *ctx);
 int ecp_tr_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2);
 int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s);
-int ecp_tr_open(int *sock, void *addr_s);
-void ecp_tr_close(int *sock);
-ssize_t ecp_tr_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags);
-ssize_t ecp_tr_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout);
+int ecp_tr_open(ECPSocket *sock, void *addr_s);
+void ecp_tr_close(ECPSocket *sock);
+ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags);
+ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout);
 void ecp_tr_buf_free(ECP2Buffer *b, unsigned char flags);
 void ecp_tr_buf_flag_set(ECP2Buffer *b, unsigned char flags);
 void ecp_tr_buf_flag_clear(ECP2Buffer *b, unsigned char flags);
diff --git a/code/test/basic.c b/code/test/basic.c
index 85c4f31..8268599 100644
--- a/code/test/basic.c
+++ b/code/test/basic.c
@@ -22,13 +22,13 @@ ECPConnection conn;
 
 ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     uint32_t seq = 0;
-    
+
     ecp_conn_handle_open(conn, sq, t, p, s, b);
     if (s < 0) {
         printf("OPEN ERR:%ld\n", s);
         return s;
     }
-    
+
     char *msg = "PERA JE CAR!";
     unsigned char buf[1000];
 
@@ -57,23 +57,23 @@ ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigne
 
 int main(int argc, char *argv[]) {
     int rv;
-    
+
     rv = ecp_init(&ctx_s);
     printf("ecp_init RV:%d\n", rv);
-    
+
     rv = ecp_conn_handler_init(&handler_s);
     handler_s.msg[MTYPE_MSG] = handle_msg_s;
     ctx_s.handler[CTYPE_TEST] = &handler_s;
-    
+
     rv = ecp_dhkey_generate(&ctx_s, &key_perma_s);
     printf("ecp_dhkey_generate RV:%d\n", rv);
-    
+
     rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s);
     printf("ecp_sock_create RV:%d\n", rv);
 
     rv = ecp_sock_open(&sock_s, "0.0.0.0:3000");
     printf("ecp_sock_open RV:%d\n", rv);
-    
+
     rv = ecp_start_receiver(&sock_s);
     printf("ecp_start_receiver RV:%d\n", rv);
 
@@ -84,20 +84,20 @@ int main(int argc, char *argv[]) {
     handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c;
     handler_c.msg[MTYPE_MSG] = handle_msg_c;
     ctx_c.handler[CTYPE_TEST] = &handler_c;
-    
+
     rv = ecp_dhkey_generate(&ctx_c, &key_perma_c);
     printf("ecp_dhkey_generate RV:%d\n", rv);
-    
+
     rv = ecp_sock_create(&sock_c, &ctx_c, &key_perma_c);
     printf("ecp_sock_create RV:%d\n", rv);
 
     rv = ecp_sock_open(&sock_c, NULL);
     printf("ecp_sock_open RV:%d\n", rv);
-    
+
     rv = ecp_start_receiver(&sock_c);
     printf("ecp_start_receiver RV:%d\n", rv);
 
-    rv = ecp_node_init(&ctx_c, &node, &key_perma_s.public, "127.0.0.1:3000");
+    rv = ecp_node_init(&node, &key_perma_s.public, "127.0.0.1:3000");
     printf("ecp_node_init RV:%d\n", rv);
 
     rv = ecp_conn_create(&conn, &sock_c, CTYPE_TEST);
@@ -105,6 +105,6 @@ int main(int argc, char *argv[]) {
 
     rv = ecp_conn_open(&conn, &node);
     printf("ecp_conn_open RV:%d\n", rv);
-    
+
     while (1) sleep(1);
 }
\ No newline at end of file
diff --git a/code/test/frag.c b/code/test/frag.c
index 6e7f6be..2bba856 100644
--- a/code/test/frag.c
+++ b/code/test/frag.c
@@ -27,13 +27,13 @@ unsigned char frag_buffer[8192];
 
 ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     uint32_t seq = 0;
-    
+
     ecp_conn_handle_open(conn, sq, t, p, s, b);
     if (s < 0) {
         printf("OPEN ERR:%ld\n", s);
         return s;
     }
-    
+
     unsigned char content[1000];
     char *msg = "PERA JE CAR!";
 
@@ -44,7 +44,7 @@ ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsign
 
 ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("MSG C:%s size:%ld\n", p, s);
-    
+
     ECPRBuffer *rbuf = &conn->rbuf.recv->rbuf;
     printf("RBUF: %d %d %d %d\n", rbuf->seq_start, rbuf->seq_max, rbuf->msg_start, rbuf->msg_size);
     return s;
@@ -63,23 +63,23 @@ ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigne
 
 int main(int argc, char *argv[]) {
     int rv;
-    
+
     rv = ecp_init(&ctx_s);
     printf("ecp_init RV:%d\n", rv);
-    
+
     rv = ecp_conn_handler_init(&handler_s);
     handler_s.msg[MTYPE_MSG] = handle_msg_s;
     ctx_s.handler[CTYPE_TEST] = &handler_s;
-    
+
     rv = ecp_dhkey_generate(&ctx_s, &key_perma_s);
     printf("ecp_dhkey_generate RV:%d\n", rv);
-    
+
     rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s);
     printf("ecp_sock_create RV:%d\n", rv);
 
     rv = ecp_sock_open(&sock_s, "0.0.0.0:3000");
     printf("ecp_sock_open RV:%d\n", rv);
-    
+
     rv = ecp_start_receiver(&sock_s);
     printf("ecp_start_receiver RV:%d\n", rv);
 
@@ -90,20 +90,20 @@ int main(int argc, char *argv[]) {
     handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c;
     handler_c.msg[MTYPE_MSG] = handle_msg_c;
     ctx_c.handler[CTYPE_TEST] = &handler_c;
-    
+
     rv = ecp_dhkey_generate(&ctx_c, &key_perma_c);
     printf("ecp_dhkey_generate RV:%d\n", rv);
-    
+
     rv = ecp_sock_create(&sock_c, &ctx_c, &key_perma_c);
     printf("ecp_sock_create RV:%d\n", rv);
 
     rv = ecp_sock_open(&sock_c, NULL);
     printf("ecp_sock_open RV:%d\n", rv);
-    
+
     rv = ecp_start_receiver(&sock_c);
     printf("ecp_start_receiver RV:%d\n", rv);
 
-    rv = ecp_node_init(&ctx_c, &node, &key_perma_s.public, "127.0.0.1:3000");
+    rv = ecp_node_init(&node, &key_perma_s.public, "127.0.0.1:3000");
     printf("ecp_node_init RV:%d\n", rv);
 
     rv = ecp_conn_create(&conn, &sock_c, CTYPE_TEST);
@@ -111,12 +111,12 @@ int main(int argc, char *argv[]) {
 
     rv = ecp_rbuf_create(&conn, NULL, NULL, 0, &rbuf_recv, rbuf_r_msg, 128);
     printf("ecp_rbuf_create RV:%d\n", rv);
-    
+
     ecp_frag_iter_init(&frag_iter, frag_buffer, 8192);
     rbuf_recv.frag_iter = &frag_iter;
-    
+
     rv = ecp_conn_open(&conn, &node);
     printf("ecp_conn_open RV:%d\n", rv);
-    
+
     while (1) sleep(1);
 }
\ No newline at end of file
diff --git a/code/test/stress.c b/code/test/stress.c
index 6bc5a64..73c511e 100644
--- a/code/test/stress.c
+++ b/code/test/stress.c
@@ -42,7 +42,7 @@ int c_start = 0;
 
 int num_s = NUM_S, num_c = NUM_C;
 int msg_rate = MSG_RATE;
-    
+
 static void display(void) {
     int i, s = 0, r = 0;
 
@@ -81,12 +81,12 @@ void *sender(ECPConnection *c) {
     ECPBuffer payload;
     unsigned char pkt_buf[ECP_MAX_PKT];
     unsigned char pld_buf[ECP_MAX_PLD];
-    
+
     packet.buffer = pkt_buf;
     packet.size = ECP_MAX_PKT;
     payload.buffer = pld_buf;
     payload.size = ECP_MAX_PLD;
-    
+
     printf("OPEN:%d\n", idx);
     while(1) {
         uint32_t rnd;
@@ -108,7 +108,7 @@ void *sender(ECPConnection *c) {
 ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     int idx = (int)(conn->conn_data);
     int rv = 0;
-    
+
     ecp_conn_handle_open(conn, sq, t, p, s, b);
     rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)conn);
     if (rv) {
@@ -126,7 +126,7 @@ ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigne
     ECPBuffer payload;
     unsigned char pkt_buf[ECP_MAX_PKT];
     unsigned char pld_buf[ECP_MAX_PLD];
-    
+
     packet.buffer = pkt_buf;
     packet.size = ECP_MAX_PKT;
     payload.buffer = pld_buf;
@@ -137,7 +137,7 @@ ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigne
         t_rcvd[idx]++;
         pthread_mutex_unlock(&t_mtx[idx]);
     }
-    
+
     // ecp_pld_set_type(payload, MTYPE_MSG);
     // ssize_t _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0));
     return s;
@@ -148,7 +148,7 @@ ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigne
     ECPBuffer payload;
     unsigned char pkt_buf[ECP_MAX_PKT];
     unsigned char pld_buf[ECP_MAX_PLD];
-    
+
     packet.buffer = pkt_buf;
     packet.size = ECP_MAX_PKT;
     payload.buffer = pld_buf;
@@ -164,7 +164,7 @@ int main(int argc, char *argv[]) {
     char addr[256];
     int rv;
     int i;
-    
+
     ECPConnHandler handler_c;
 
     ECPContext *ctx_c;
@@ -173,7 +173,7 @@ int main(int argc, char *argv[]) {
 
     ECPNode *node;
     ECPConnection *conn;
-    
+
     sock_s = malloc(num_s * sizeof(ECPSocket));
     ctx_c = malloc(num_c * sizeof(ECPContext));
     sock_c = malloc(num_c * sizeof(ECPSocket));
@@ -188,7 +188,7 @@ int main(int argc, char *argv[]) {
     t_rcvd = malloc(num_c * sizeof(int));
     memset(t_rcvd, 0, num_c * sizeof(int));
     memset(t_sent, 0, num_c * sizeof(int));
-    
+
     struct sigaction actINFO;
     memset(&actINFO, 0, sizeof(actINFO));
     actINFO.sa_handler = &catchINFO;
@@ -222,13 +222,13 @@ int main(int argc, char *argv[]) {
 
     handler_c.msg[ECP_MTYPE_OPEN] = handle_open_c;
     handler_c.msg[MTYPE_MSG] = handle_msg_c;
-    
+
     for (i=0; i<num_c; i++) {
         pthread_mutex_init(&t_mtx[i], NULL);
-        
+
         if (!rv) rv = ecp_init(&ctx_c[i]);
         ctx_c[i].handler[CTYPE_TEST] = &handler_c;
-        
+
         if (!rv) rv = ecp_dhkey_generate(&ctx_c[i], &key_perma_c[i]);
         if (!rv) rv = ecp_sock_create(&sock_c[i], &ctx_c[i], &key_perma_c[i]);
         if (!rv) rv = ecp_sock_open(&sock_c[i], NULL);
@@ -237,20 +237,20 @@ int main(int argc, char *argv[]) {
 
         strcpy(addr, "127.0.0.1:");
         sprintf(addr+strlen(addr), "%d", 3000 + (i % num_s));
-        if (!rv) rv = ecp_node_init(&ctx_c[i], &node[i], &key_perma_s.public, addr);
-        
+        if (!rv) rv = ecp_node_init(&node[i], &key_perma_s.public, addr);
+
         if (!rv) rv = ecp_conn_create(&conn[i], &sock_c[i], CTYPE_TEST);
         conn[i].conn_data = (void *)i;
-        
+
         if (!rv) rv = ecp_conn_open(&conn[i], &node[i]);
-        
+
         if (rv) {
             char msg[256];
             sprintf(msg, "CLIENT %d CREATE:%d\n", i, rv);
             perror(msg);
             exit(1);
         }
-        
+
     }
     while (1) sleep(1);
 }
\ No newline at end of file
-- 
cgit v1.2.3