summaryrefslogtreecommitdiff
path: root/ecp
diff options
context:
space:
mode:
Diffstat (limited to 'ecp')
-rw-r--r--ecp/README24
-rwxr-xr-xecp/build.sh4
-rw-r--r--ecp/src/ecp/common.mk2
-rw-r--r--ecp/src/ecp/core.c339
-rw-r--r--ecp/src/ecp/core.h22
-rw-r--r--ecp/src/ecp/timer.c20
-rw-r--r--ecp/src/ecp/vconn/vconn.c12
-rw-r--r--ecp/src/platform/fe310/features_tmpl.mk (renamed from ecp/src/platform/fe310/features.mk)0
-rw-r--r--ecp/src/platform/posix/features.mk6
-rw-r--r--ecp/src/platform/posix/features_tmpl.mk8
-rw-r--r--ecp/test/Makefile2
-rw-r--r--ecp/test/vc_common.c1
12 files changed, 220 insertions, 220 deletions
diff --git a/ecp/README b/ecp/README
new file mode 100644
index 0000000..ec6ae47
--- /dev/null
+++ b/ecp/README
@@ -0,0 +1,24 @@
+To build this library you will need GNU Make installed and any modern C compiler. Library is tested on Linux, *BSD and macOS operating systems.
+
+First you need to create features.mk makefile:
+ cp src/platform/posix/features_tmpl.mk src/platform/posix/features.mk
+
+Edit it according to your needs. Defaults are fine. Note that with_frag, with_rbuf and with_msgq are experimental features and should not be used in production.
+
+To (re)build library, utilities and tests simply do:
+ ./build.sh (MAKE=gmake ./build.sh on *BSD systems)
+
+In tests subdirectory you will find simple test examples. To test vc_server and vc_client (vconn feature) do:
+ util/mknode n1
+ util/mknode n2
+ util/mknode n3
+ util/mknode server
+ cd tests
+
+Then launch servers (each in its own terminal):
+ ./vcs 0.0.0.0:3001 ../n1.priv
+ ./vcs 0.0.0.0:3002 ../n2.priv 127.0.0.1:3001 ../n1.pub
+ ./vcs 0.0.0.0:3003 ../n3.priv 127.0.0.1:3002 ../n2.pub
+ ./vc_server ../server.priv 127.0.0.1:3001 ../n1.pub ../n2.pub ../n3.pub
+ ./vc_client ../server.pub 127.0.0.1:3001 ../n1.pub ../n2.pub ../n3.pub
+ \ No newline at end of file
diff --git a/ecp/build.sh b/ecp/build.sh
index d28c794..a050e85 100755
--- a/ecp/build.sh
+++ b/ecp/build.sh
@@ -13,6 +13,10 @@ fi
PLATFORM=posix
+if [ ! -f src/platform/posix/features.mk ]; then
+ cp src/platform/posix/features_tmpl.mk src/platform/posix/features.mk
+fi
+
cd src/ecp
if [ "$ARG" != "clean" ]; then
$MAKE platform=$PLATFORM clean
diff --git a/ecp/src/ecp/common.mk b/ecp/src/ecp/common.mk
index 9380130..ad8277a 100644
--- a/ecp/src/ecp/common.mk
+++ b/ecp/src/ecp/common.mk
@@ -7,7 +7,7 @@ platform_dir = $(abspath $(src_dir)/platform/$(platform))
include $(platform_dir)/platform.mk
include $(platform_dir)/features.mk
-CFLAGS += -I$(src_dir) -I$(platform_dir) -I$(ssl_dir)/include
+CFLAGS += -I$(src_dir) -I$(platform_dir) -I$(ssl_dir)/include -I$(ssl_dir)/crypto/compat
ifeq ($(with_pthread),yes)
CFLAGS += -DECP_WITH_PTHREAD=1
diff --git a/ecp/src/ecp/core.c b/ecp/src/ecp/core.c
index 9f9bab5..f6d8447 100644
--- a/ecp/src/ecp/core.c
+++ b/ecp/src/ecp/core.c
@@ -420,7 +420,7 @@ static void conn_table_expire(ECPSocket *sock, ecp_sts_t to, ecp_conn_expired_t
pthread_mutex_lock(&conn->mutex);
#endif
- expired = conn_expired(conn, now, to);
+ expired = conn_expired(conn, now, to) || !_ecp_conn_is_reg(conn);
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&conn->mutex);
@@ -450,7 +450,8 @@ static void conn_table_expire(ECPSocket *sock, ecp_sts_t to, ecp_conn_expired_t
#endif
for (i=0; i<remove_cnt; i++) {
- _ecp_conn_close(to_remove[i]);
+ ecp_conn_close(to_remove[i]);
+ ecp_conn_refcount_dec(to_remove[i]);
}
} while (remove_next);
@@ -470,7 +471,7 @@ static void conn_table_expire(ECPSocket *sock, ecp_sts_t to, ecp_conn_expired_t
pthread_mutex_lock(&conn->mutex);
#endif
- expired = conn_expired(conn, now, to);
+ expired = conn_expired(conn, now, to) || !_ecp_conn_is_reg(conn);
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&conn->mutex);
@@ -489,7 +490,8 @@ static void conn_table_expire(ECPSocket *sock, ecp_sts_t to, ecp_conn_expired_t
#endif
for (i=0; i<remove_cnt; i++) {
- _ecp_conn_close(to_remove[i]);
+ ecp_conn_close(to_remove[i]);
+ ecp_conn_refcount_dec(to_remove[i]);
}
} while (remove_cnt < ECP_MAX_EXP);
@@ -928,7 +930,7 @@ void ecp_conn_init(ECPConnection *conn, ECPSocket *sock, unsigned char ctype) {
conn->key_next = ECP_ECDH_IDX_INV;
conn->rkey_curr = ECP_ECDH_IDX_INV;
arc4random_buf(&conn->nonce_out, sizeof(conn->nonce_out));
- conn->access_ts = ecp_tm_get_s();
+ conn->access_ts = 0;
conn->keyx_ts = 0;
}
@@ -942,6 +944,8 @@ int ecp_conn_reset(ECPConnection *conn) {
memset(&conn->rkey, 0, sizeof(conn->rkey));
memset(&conn->shkey, 0, sizeof(conn->shkey));
arc4random_buf(&conn->nonce_out, sizeof(conn->nonce_out));
+ conn->access_ts = 0;
+ conn->keyx_ts = 0;
conn->nonce_in = 0;
conn->nonce_map = 0;
@@ -1102,24 +1106,30 @@ int ecp_conn_insert_gc(ECPConnection *conn) {
return rv;
}
-void ecp_conn_remove(ECPConnection *conn, unsigned short *refcount) {
+int ecp_conn_remove(ECPConnection *conn, int *refcount) {
ECPSocket *sock = conn->sock;
+ int rv = ECP_OK;
#ifdef ECP_WITH_PTHREAD
pthread_mutex_lock(&sock->conn_table.mutex);
pthread_mutex_lock(&conn->mutex);
#endif
- if (_ecp_conn_is_reg(conn)) {
+ if (refcount) {
+ if ((*refcount != -1) && (*refcount != conn->refcount)) rv = ECP_ERR_BUSY;
+ *refcount = conn->refcount;
+ }
+ if (!rv && _ecp_conn_is_reg(conn)) {
conn_table_remove(conn);
_ecp_conn_clr_reg(conn);
}
- if (refcount) *refcount = conn->refcount;
#ifdef ECP_WITH_PTHREAD
pthread_mutex_unlock(&conn->mutex);
pthread_mutex_unlock(&sock->conn_table.mutex);
#endif
+
+ return rv;
}
void ecp_conn_remove_addr(ECPConnection *conn) {
@@ -1140,7 +1150,7 @@ void ecp_conn_remove_addr(ECPConnection *conn) {
}
int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, ECPNode *node, int retry) {
- int rv;
+ int rv = ECP_OK;
ssize_t _rv;
rv = ecp_conn_create_outb(conn, parent, node);
@@ -1152,25 +1162,15 @@ int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, ECPNode *node, in
return rv;
}
- if (ecp_conn_is_gc(conn)) {
- rv = ecp_conn_insert_gc(conn);
- if (rv) {
- ecp_conn_refcount_dec(conn);
- _ecp_conn_close(conn);
- return rv;
- }
- }
-
_rv = ecp_send_init_req(conn, retry);
if (_rv < 0) {
- ecp_conn_refcount_dec(conn);
ecp_conn_close(conn);
- return _rv;
+ rv = _rv;
}
ecp_conn_refcount_dec(conn);
- return ECP_OK;
+ return rv;
}
int ecp_conn_open(ECPConnection *conn, ECPNode *node) {
@@ -1187,7 +1187,7 @@ int ecp_conn_try_open(ECPConnection *conn, ECPNode *node) {
return rv;
}
-static void conn_close(ECPConnection *conn) {
+static void _conn_close(ECPConnection *conn) {
if (_ecp_conn_is_open(conn)) {
ecp_close_handler_t handler;
@@ -1199,44 +1199,19 @@ static void conn_close(ECPConnection *conn) {
ecp_conn_destroy(conn);
}
-int _ecp_conn_close(ECPConnection *conn) {
- unsigned short refcount = 0;
+int ecp_conn_close(ECPConnection *conn) {
+ int refcount = -1;
ecp_timer_remove(conn);
ecp_conn_remove(conn, &refcount);
if (refcount) return ECP_ERR_BUSY;
+ _conn_close(conn);
- conn_close(conn);
return ECP_OK;
}
-int ecp_conn_close(ECPConnection *conn) {
- int rv;
-
- if (ecp_conn_is_gc(conn)) {
- ecp_conn_mark_closed(conn);
- rv = ECP_OK;
- } else {
- rv = _ecp_conn_close(conn);
- }
-
- return rv;
-}
-
-void ecp_conn_mark_closed(ECPConnection *conn) {
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&conn->mutex);
-#endif
-
- _ecp_conn_set_closed(conn);
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&conn->mutex);
-#endif
-}
-
int _ecp_conn_expired(ECPConnection *conn, ecp_sts_t now, ecp_sts_t to) {
- if (_ecp_conn_is_closed(conn) || (ECP_STS_LT(conn->access_ts, now) && (now - conn->access_ts > to))) return 1;
+ if (ECP_STS_LT(conn->access_ts, now) && (now - conn->access_ts > to)) return 1;
return 0;
}
@@ -1284,7 +1259,7 @@ void ecp_conn_refcount_dec(ECPConnection *conn) {
pthread_mutex_unlock(&conn->mutex);
#endif
- if (!is_reg && (refcount == 0)) conn_close(conn);
+ if (!is_reg && (refcount == 0)) _conn_close(conn);
}
void ecp_conn_set_flags(ECPConnection *conn, unsigned char flags) {
@@ -1555,12 +1530,12 @@ static ssize_t _send_ireq(ECPConnection *conn, ECPTimerItem *ti) {
}
static ssize_t _retry_ireq(ECPConnection *conn, ECPTimerItem *ti) {
- unsigned short refcount = 0;
+ int refcount = 1;
int rv;
- /* timer holds one reference to this connection */
- ecp_conn_remove(conn, &refcount);
- if (refcount > 1) return ECP_ERR_BUSY;
+ /* timer holds one reference to this connection, nobody else should */
+ rv = ecp_conn_remove(conn, &refcount);
+ if (rv) return rv;
rv = ecp_conn_reset(conn);
if (rv) return rv;
@@ -1792,19 +1767,9 @@ ssize_t ecp_handle_open_req(ECPSocket *sock, ECPConnection *parent, unsigned cha
ecp_conn_destroy(conn);
return _rv;
}
-
- if (ecp_conn_is_gc(conn)) {
- _rv = ecp_conn_insert_gc(conn);
- if (_rv) {
- ecp_conn_refcount_dec(conn);
- _ecp_conn_close(conn);
- return _rv;
- }
- }
-
*_conn = conn;
- /* handle_open will be called later from msg handler */
+ /* handle_open will be called from ecp_unpack */
return 2+(vbox ? ECP_SIZE_VBOX : 0);
}
@@ -1825,8 +1790,10 @@ ssize_t ecp_send_open_rep(ECPConnection *conn) {
return rv;
}
-ssize_t ecp_check_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size) {
- int is_open;
+ssize_t ecp_handle_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) {
+ ecp_open_handler_t handler;
+ int is_open, is_gc;
+ int _rv = ECP_OK;
ssize_t rv;
#ifdef ECP_WITH_PTHREAD
@@ -1854,22 +1821,35 @@ ssize_t ecp_check_open(ECPConnection *conn, unsigned char mtype, unsigned char *
rv = 0;
}
- return rv;
-}
+ is_gc = ecp_conn_is_gc(conn);
+ if (is_gc) {
+ ECPSocket *sock = conn->sock;
-int ecp_handle_open(ECPConnection *conn, ECP2Buffer *bufs) {
- ecp_open_handler_t handler;
- int rv = ECP_OK;
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_lock(&sock->conn_table.mutex_gc);
+ pthread_mutex_lock(&conn->mutex);
+#endif
- rv = ecp_ext_conn_open(conn);
- if (rv) return rv;
+ _rv = conn_table_insert_gc(conn);
+ if (!_rv) conn->refcount++;
+
+#ifdef ECP_WITH_PTHREAD
+ pthread_mutex_unlock(&conn->mutex);
+ pthread_mutex_unlock(&sock->conn_table.mutex_gc);
+#endif
+
+ if (_rv) return _rv;
+ }
+
+ _rv = ecp_ext_conn_open(conn);
+ if (_rv) return _rv;
handler = ecp_get_open_handler(conn);
if (handler) {
- rv = handler(conn, bufs);
- if (rv) {
+ _rv = handler(conn, bufs);
+ if (_rv) {
ecp_ext_conn_close(conn);
- return rv;
+ return _rv;
}
}
@@ -1890,7 +1870,7 @@ int ecp_handle_open(ECPConnection *conn, ECP2Buffer *bufs) {
ecp_conn_remove_addr(conn);
}
- return ECP_OK;
+ return rv;
}
static ssize_t _send_kxreq(ECPConnection *conn, ECPTimerItem *ti) {
@@ -1982,7 +1962,7 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype,
switch (mtype) {
case ECP_MTYPE_OPEN_REQ:
case ECP_MTYPE_OPEN_REP:
- return ecp_check_open(conn, mtype, msg, msg_size);
+ return ecp_handle_open(conn, mtype, msg, msg_size, bufs);
case ECP_MTYPE_KEYX_REQ:
case ECP_MTYPE_KEYX_REP:
@@ -2043,7 +2023,6 @@ ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payloa
while (pld_size) {
rv = ecp_pld_handle_one(conn, seq, payload, pld_size, bufs);
- if (rv == ECP_ERR_HANDLER) return _pld_size - pld_size;
if (rv < 0) return rv;
payload += rv;
@@ -2053,7 +2032,7 @@ ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payloa
return _pld_size;
}
-ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq, int *is_open_msg) {
+ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq) {
ECPConnection *conn = NULL;
unsigned char idx;
unsigned char s_idx;
@@ -2065,8 +2044,9 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
ecp_aead_key_t shkey;
unsigned char *public_buf = NULL;
unsigned char *nonce_buf = NULL;
- unsigned char is_open = 0;
unsigned char is_inb = 0;
+ unsigned char is_open = 0;
+ unsigned char is_open_msg = 0;
ecp_nonce_t nonce_pkt, nonce_conn, nonce_in;
ecp_ack_t nonce_map;
ssize_t rv;
@@ -2077,7 +2057,6 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
*_conn = NULL;
*_payload = NULL;
*_seq = 0;
- *is_open_msg = 0;
packet = bufs->packet->buffer;
idx = packet[ECP_SIZE_PROTO];
@@ -2103,6 +2082,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
is_open = _ecp_conn_is_open(conn);
if (!is_open && !is_inb && (idx == ECP_ECDH_IDX_INV)) {
+ /* init_rep message */
nonce_buf = packet+ECP_SIZE_PROTO+1;
packet += ECP_SIZE_PROTO+1+ECP_SIZE_NONCE;
pkt_size -= ECP_SIZE_PROTO+1+ECP_SIZE_NONCE;
@@ -2164,7 +2144,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
}
ecp_buf2nonce(&nonce_pkt, nonce_buf);
- if (conn && is_open) {
+ if (is_open) {
if (ECP_NONCE_LTE(nonce_pkt, nonce_conn)) {
ecp_nonce_t nonce_offset = nonce_conn - nonce_pkt;
@@ -2193,7 +2173,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
}
if (_rv) {
rv = _rv;
- goto unpack_err;
+ goto unpack_fin;
}
}
@@ -2237,58 +2217,15 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
ecp_conn_refcount_dec(_conn);
}
- if (rv < 0) goto unpack_err;
+ if (rv < 0) goto unpack_fin;
pld_size = rv;
if (pld_size < ECP_MIN_PLD) {
rv = ECP_ERR_SIZE;
- goto unpack_err;
+ goto unpack_fin;
}
- if (conn == NULL) {
- unsigned char mtype;
- unsigned char *msg;
- size_t hdr_size, msg_size;
-
- _rv = ecp_pld_get_type(payload, pld_size, &mtype);
- if (_rv) return _rv;
-
- msg = ecp_pld_get_msg(payload, pld_size);
- if (msg == NULL) return ECP_ERR;
- hdr_size = msg - payload;
- msg_size = pld_size - hdr_size;
-
- switch (mtype) {
- case ECP_MTYPE_INIT_REQ: {
- unsigned char _public_buf[ECP_SIZE_ECDH_PUB];
-
- memcpy(_public_buf, public_buf, sizeof(_public_buf));
- rv = ecp_handle_init_req(sock, parent, addr, c_idx, _public_buf, &shkey, bufs);
- if (rv < 0) return rv;
-
- rv += hdr_size;
- break;
- }
-
- case ECP_MTYPE_OPEN_REQ: {
- rv = ecp_handle_open_req(sock, parent, s_idx, c_idx, public_buf, msg, msg_size, &shkey, &conn);
- if (rv < 0) return rv;
-
- /* pass to payload handler */
- nonce_in = nonce_pkt;
- nonce_map = ECP_ACK_FULL;
- *is_open_msg = 1;
- rv = 0;
- break;
- }
-
- default:
- return ECP_ERR_MTYPE;
- }
-
- payload += rv;
- pld_size -= rv;
- } else if (!is_open) {
+ if (!(conn && is_open)) {
unsigned char mtype;
unsigned char *msg;
size_t hdr_size, msg_size;
@@ -2296,85 +2233,121 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr,
_rv = ecp_pld_get_type(payload, pld_size, &mtype);
if (_rv) {
rv = _rv;
- goto unpack_err;
+ goto unpack_fin;
}
msg = ecp_pld_get_msg(payload, pld_size);
if (msg == NULL) {
rv = ECP_ERR;
- goto unpack_err;
+ goto unpack_fin;
}
hdr_size = msg - payload;
msg_size = pld_size - hdr_size;
- switch (mtype) {
- case ECP_MTYPE_INIT_REP: {
- rv = ecp_handle_init_rep(conn, msg, msg_size, nonce_buf, bufs);
- if (rv < 0) goto unpack_err;
+ if (conn == NULL) {
+ switch (mtype) {
+ case ECP_MTYPE_INIT_REQ: {
+ unsigned char _public_buf[ECP_SIZE_ECDH_PUB];
- rv += hdr_size;
- break;
- }
+ memcpy(_public_buf, public_buf, sizeof(_public_buf));
+ rv = ecp_handle_init_req(sock, parent, addr, c_idx, _public_buf, &shkey, bufs);
+ if (rv < 0) goto unpack_fin;
- case ECP_MTYPE_OPEN_REP: {
- /* pass to payload handler */
- nonce_in = nonce_pkt;
- nonce_map = ECP_ACK_FULL;
- *is_open_msg = 1;
- rv = 0;
- break;
+ payload += (hdr_size + rv);
+ pld_size -= (hdr_size + rv);
+ break;
+ }
+
+ case ECP_MTYPE_OPEN_REQ: {
+ rv = ecp_handle_open_req(sock, parent, s_idx, c_idx, public_buf, msg, msg_size, &shkey, &conn);
+ if (rv < 0) goto unpack_fin;
+
+ /* pass to open handler */
+ nonce_in = nonce_pkt;
+ nonce_map = ECP_ACK_FULL;
+ is_open_msg = 1;
+ break;
+ }
+
+ default:
+ rv = ECP_ERR_MTYPE;
+ goto unpack_fin;
}
+ } else if (!is_open) {
+ switch (mtype) {
+ case ECP_MTYPE_INIT_REP: {
+ rv = ecp_handle_init_rep(conn, msg, msg_size, nonce_buf, bufs);
+ if (rv < 0) goto unpack_fin;
+
+ payload += (hdr_size + rv);
+ pld_size -= (hdr_size + rv);
+ break;
+ }
- default:
- rv = ECP_ERR_MTYPE;
- goto unpack_err;
- }
+ case ECP_MTYPE_OPEN_REP: {
+ /* pass to open handler */
+ nonce_in = nonce_pkt;
+ nonce_map = ECP_ACK_FULL;
+ is_open_msg = 1;
+ break;
+ }
- payload += rv;
- pld_size -= rv;
+ default:
+ rv = ECP_ERR_MTYPE;
+ goto unpack_fin;
+ }
+ }
}
- if (conn) {
- if (is_open || *is_open_msg) {
+ if (is_open || is_open_msg) {
+ ecp_seq_t seq = (ecp_seq_t)nonce_pkt;
+
#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&conn->mutex);
+ pthread_mutex_lock(&conn->mutex);
#endif
- conn->nonce_in = nonce_in;
- conn->nonce_map = nonce_map;
- conn->access_ts = ecp_tm_get_s();
- if (is_inb && addr) {
- conn->remote.addr = *addr;
- }
+ conn->nonce_in = nonce_in;
+ conn->nonce_map = nonce_map;
+ conn->access_ts = ecp_tm_get_s();
+ if (is_inb && addr) {
+ conn->remote.addr = *addr;
+ }
#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&conn->mutex);
+ pthread_mutex_unlock(&conn->mutex);
#endif
- *_conn = conn;
- *_payload = payload;
- *_seq = (ecp_seq_t)nonce_pkt;
- } else {
- ecp_conn_refcount_dec(conn);
+ if (is_open_msg) {
+ rv = ecp_pld_handle_one(conn, seq, payload, pld_size, bufs);
+ if (rv < 0) {
+ ecp_conn_close(conn);
+ goto unpack_fin;
+ }
+
+ payload += rv;
+ pld_size -= rv;
}
+
+ *_conn = conn;
+ *_payload = payload;
+ *_seq = seq;
}
- return _pkt_size - pld_size;
+ rv = _pkt_size - pld_size;
-unpack_err:
- if (conn) ecp_conn_refcount_dec(conn);
+unpack_fin:
+ if (conn && (*_conn == NULL)) ecp_conn_refcount_dec(conn);
return rv;
}
ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t pkt_size) {
ECPConnection *conn;
unsigned char *payload;
- int is_open_msg;
ecp_seq_t seq;
size_t pld_size;
ssize_t rv;
- rv = ecp_unpack(sock, parent, addr, bufs, pkt_size, &conn, &payload, &seq, &is_open_msg);
+ rv = ecp_unpack(sock, parent, addr, bufs, pkt_size, &conn, &payload, &seq);
if (rv < 0) return rv;
pld_size = pkt_size - rv;
@@ -2399,16 +2372,6 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *ad
pld_size -= rv;
}
- if (is_open_msg) {
- int _rv;
-
- _rv = ecp_handle_open(conn, bufs);
- if (_rv) {
- rv = _rv;
- goto pkt_handle_fin;
- }
- }
-
rv = pkt_size - pld_size;
pkt_handle_fin:
@@ -2817,11 +2780,11 @@ int ecp_receiver(ECPSocket *sock) {
rv = ecp_tr_recv(sock, &packet, &addr, next ? next : ECP_POLL_TIMEOUT);
if (rv > 0) {
_rv = recv_p(sock, &addr, &packet, rv);
+
#ifdef ECP_DEBUG
- if (_rv) {
- printf("RCV ERR:%d\n", _rv);
- }
+ if (_rv) printf("RECEIVE ERR:%d\n", _rv);
#endif
+
}
next = ecp_timer_exe(sock);
}
diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h
index 09e28f8..e0fcde7 100644
--- a/ecp/src/ecp/core.h
+++ b/ecp/src/ecp/core.h
@@ -75,15 +75,20 @@
#define ECP_SIZE_PLD(X,T) ((X)+ECP_SIZE_MTYPE+ECP_SIZE_MT_FLAG(T))
#define ECP_SIZE_PKT(X,T) (ECP_SIZE_PKT_HDR+ECP_SIZE_PLD(X,T)+ECP_SIZE_AEAD_TAG)
+/*
+#define ECP_CONN_PCOUNT(C) ((C)->pcount)
+*/
+#define ECP_CONN_PCOUNT(C) ECP_MAX_PARENT
+
#ifdef ECP_WITH_VCONN
-#define ECP_SIZE_PLD_BUF(X,T,C) (ECP_SIZE_PLD(X,T)+(C)->pcount*(ECP_SIZE_PKT_HDR+ECP_SIZE_MTYPE+ECP_SIZE_AEAD_TAG))
+#define ECP_SIZE_PLD_BUF(X,T,C) (ECP_SIZE_PLD(X,T)+ECP_CONN_PCOUNT(C)*(ECP_SIZE_PKT_HDR+ECP_SIZE_MTYPE+ECP_SIZE_AEAD_TAG))
#else
#define ECP_SIZE_PLD_BUF(X,T,C) (ECP_SIZE_PLD(X,T))
#endif
#define ECP_SIZE_PKT_BUF(X,T,C) (ECP_SIZE_PKT_HDR+ECP_SIZE_PLD_BUF(X,T,C)+ECP_SIZE_AEAD_TAG)
#ifdef ECP_WITH_VCONN
-#define ECP_SIZE_PLD_BUF_IREP(X,T,P) (ECP_SIZE_PLD(X,T)+((P) ? ((P)->pcount+1)*(ECP_SIZE_PKT_HDR+ECP_SIZE_MTYPE+ECP_SIZE_AEAD_TAG) : 0))
+#define ECP_SIZE_PLD_BUF_IREP(X,T,P) (ECP_SIZE_PLD(X,T)+((P) ? (ECP_CONN_PCOUNT(P)+1)*(ECP_SIZE_PKT_HDR+ECP_SIZE_MTYPE+ECP_SIZE_AEAD_TAG) : 0))
#else
#define ECP_SIZE_PLD_BUF_IREP(X,T,P) (ECP_SIZE_PLD(X,T))
#endif
@@ -133,7 +138,6 @@
/* mutable flags */
#define ECP_CONN_FLAG_REG 0x01
#define ECP_CONN_FLAG_OPEN 0x02
-#define ECP_CONN_FLAG_CLOSED 0x04
#define ECP_SEND_FLAG_REPLY 0x01
#define ECP_SEND_FLAG_MORE 0x02
@@ -146,17 +150,14 @@
#define ecp_conn_is_sys(conn) ((conn)->type & ECP_CTYPE_FLAG_SYS)
#define _ecp_conn_is_reg(conn) ((conn)->flags & ECP_CONN_FLAG_REG)
#define _ecp_conn_is_open(conn) ((conn)->flags & ECP_CONN_FLAG_OPEN)
-#define _ecp_conn_is_closed(conn) ((conn)->flags & ECP_CONN_FLAG_CLOSED)
#define ecp_conn_set_inb(conn) ((conn)->flags_im |= ECP_CONN_FLAG_INB)
#define ecp_conn_set_outb(conn) ((conn)->flags_im &= ~ECP_CONN_FLAG_INB)
#define _ecp_conn_set_reg(conn) ((conn)->flags |= ECP_CONN_FLAG_REG)
#define _ecp_conn_set_open(conn) ((conn)->flags |= ECP_CONN_FLAG_OPEN)
-#define _ecp_conn_set_closed(conn) ((conn)->flags |= ECP_CONN_FLAG_CLOSED)
#define _ecp_conn_clr_reg(conn) ((conn)->flags &= ~ECP_CONN_FLAG_REG)
#define _ecp_conn_clr_open(conn) ((conn)->flags &= ~ECP_CONN_FLAG_OPEN)
-#define _ecp_conn_clr_closed(conn) ((conn)->flags &= ~ECP_CONN_FLAG_CLOSED)
typedef uint32_t ecp_ack_t;
#define ECP_SIZE_ACKB (sizeof(ecp_ack_t)*8)
@@ -369,15 +370,13 @@ void ecp_conn_free(ECPConnection *conn);
int ecp_conn_insert(ECPConnection *conn);
int ecp_conn_insert_gc(ECPConnection *conn);
-void ecp_conn_remove(ECPConnection *conn, unsigned short *refcount);
+int ecp_conn_remove(ECPConnection *conn, int *refcount);
void ecp_conn_remove_addr(ECPConnection *conn);
int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, ECPNode *node, int retry);
int ecp_conn_open(ECPConnection *conn, ECPNode *node);
int ecp_conn_try_open(ECPConnection *conn, ECPNode *node);
-int _ecp_conn_close(ECPConnection *conn);
int ecp_conn_close(ECPConnection *conn);
-void ecp_conn_mark_closed(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);
void ecp_conn_refcount_inc(ECPConnection *conn);
@@ -411,8 +410,7 @@ ssize_t ecp_write_open_req(ECPConnection *conn, ECPBuffer *payload);
ssize_t ecp_send_open_req(ECPConnection *conn, unsigned char *cookie);
ssize_t ecp_handle_open_req(ECPSocket *sock, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, unsigned char *public_buf, unsigned char *msg, size_t msg_size, ecp_aead_key_t *shkey, ECPConnection **_conn);
ssize_t ecp_send_open_rep(ECPConnection *conn);
-ssize_t ecp_check_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size);
-int ecp_handle_open(ECPConnection *conn, ECP2Buffer *bufs);
+ssize_t ecp_handle_open(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs);
ssize_t ecp_send_keyx_req(ECPConnection *conn, int retry);
ssize_t ecp_send_keyx_rep(ECPConnection *conn);
@@ -422,7 +420,7 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype,
ssize_t ecp_pld_handle_one(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t pld_size, ECP2Buffer *bufs);
ssize_t ecp_pld_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *payload, size_t _pld_size, ECP2Buffer *bufs);
-ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq, int *is_open_msg);
+ssize_t ecp_unpack(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t _pkt_size, ECPConnection **_conn, unsigned char **_payload, ecp_seq_t *_seq);
ssize_t ecp_pkt_handle(ECPSocket *sock, ECPConnection *parent, ecp_tr_addr_t *addr, ECP2Buffer *bufs, size_t pkt_size);
ssize_t ecp_pkt_send(ECPSocket *sock, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ecp_tr_addr_t *addr);
diff --git a/ecp/src/ecp/timer.c b/ecp/src/ecp/timer.c
index 38df398..9e86a88 100644
--- a/ecp/src/ecp/timer.c
+++ b/ecp/src/ecp/timer.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include "core.h"
#include "tm.h"
@@ -49,8 +50,8 @@ int ecp_timer_push(ECPTimerItem *ti) {
pthread_mutex_unlock(&conn->mutex);
#endif
- if (timer->head == ECP_MAX_TIMER-1) rv = ECP_ERR_MAX_TIMER;
- if (!rv && !is_reg) rv = ECP_ERR_CLOSED;
+ if (!is_reg) rv = ECP_ERR_CLOSED;
+ if (!rv && (timer->head == ECP_MAX_TIMER-1)) rv = ECP_ERR_MAX_TIMER;
if (!rv) {
for (i=timer->head; i>=0; i--) {
@@ -73,6 +74,9 @@ int ecp_timer_push(ECPTimerItem *ti) {
pthread_mutex_unlock(&timer->mutex);
#endif
+ if (rv && is_reg) {
+ ecp_conn_refcount_dec(conn);
+ }
return rv;
}
@@ -166,8 +170,9 @@ ecp_sts_t ecp_timer_exe(ECPSocket *sock) {
ECPConnection *conn = to_exec[i].conn;
unsigned char mtype = to_exec[i].mtype;
ecp_timer_retry_t retry = to_exec[i].retry;
- int rv = ECP_OK;
+ int rv;
+ rv = ECP_OK;
if (to_exec[i].cnt > 0) {
to_exec[i].cnt--;
if (retry) {
@@ -178,8 +183,15 @@ ecp_sts_t ecp_timer_exe(ECPSocket *sock) {
}
if (rv && (rv != ECP_ERR_CLOSED)) ecp_err_handle(conn, mtype, rv);
} else {
- ecp_err_handle(conn, mtype, ECP_ERR_TIMEOUT);
+ rv = ECP_ERR_TIMEOUT;
+ ecp_err_handle(conn, mtype, rv);
+ if (mtype == ECP_MTYPE_OPEN_REP) ecp_conn_close(conn);
}
+
+#ifdef ECP_DEBUG
+ if (rv) printf("TIMER ERR:%d\n", rv);
+#endif
+
ecp_conn_refcount_dec(conn);
}
diff --git a/ecp/src/ecp/vconn/vconn.c b/ecp/src/ecp/vconn/vconn.c
index f52b2f5..f8dc5df 100644
--- a/ecp/src/ecp/vconn/vconn.c
+++ b/ecp/src/ecp/vconn/vconn.c
@@ -74,17 +74,7 @@ static ssize_t handle_next(ECPConnection *conn, unsigned char *msg, size_t msg_s
if (msg_size < ECP_SIZE_ECDH_PUB) return ECP_ERR_SIZE;
if (ecp_conn_is_outb(conn)) return ECP_ERR;
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_lock(&conn->mutex);
-#endif
-
- is_open = _ecp_conn_is_open(conn);
-
-#ifdef ECP_WITH_PTHREAD
- pthread_mutex_unlock(&conn->mutex);
-#endif
-
- if (is_open) return ECP_ERR_NEXT;
+ if (vconn->next) return ECP_ERR_NEXT;
#ifdef ECP_WITH_PTHREAD
pthread_mutex_lock(&sock->conn_table.mutex);
diff --git a/ecp/src/platform/fe310/features.mk b/ecp/src/platform/fe310/features_tmpl.mk
index 39f95cf..39f95cf 100644
--- a/ecp/src/platform/fe310/features.mk
+++ b/ecp/src/platform/fe310/features_tmpl.mk
diff --git a/ecp/src/platform/posix/features.mk b/ecp/src/platform/posix/features.mk
index ea5e44c..4efdc90 100644
--- a/ecp/src/platform/posix/features.mk
+++ b/ecp/src/platform/posix/features.mk
@@ -1,8 +1,8 @@
with_pthread = yes
with_htable = yes
with_vconn = yes
-with_frag = yes
-with_rbuf = yes
-with_msgq = yes
with_dir = yes
with_debug = yes
+#with_frag = yes
+#with_rbuf = yes
+#with_msgq = yes
diff --git a/ecp/src/platform/posix/features_tmpl.mk b/ecp/src/platform/posix/features_tmpl.mk
new file mode 100644
index 0000000..4efdc90
--- /dev/null
+++ b/ecp/src/platform/posix/features_tmpl.mk
@@ -0,0 +1,8 @@
+with_pthread = yes
+with_htable = yes
+with_vconn = yes
+with_dir = yes
+with_debug = yes
+#with_frag = yes
+#with_rbuf = yes
+#with_msgq = yes
diff --git a/ecp/test/Makefile b/ecp/test/Makefile
index b2fd6f8..56cc879 100644
--- a/ecp/test/Makefile
+++ b/ecp/test/Makefile
@@ -1,6 +1,6 @@
src_dir = ../src
include $(src_dir)/ecp/common.mk
-CFLAGS += -I../util -Wno-int-to-void-pointer-cast
+CFLAGS += -I../util
dep = ../build-posix/*.a ../util/libecputil.a
target = basic client server vc_inb vc_outb vcs vc_server vc_client
diff --git a/ecp/test/vc_common.c b/ecp/test/vc_common.c
index 0508ac1..efc7140 100644
--- a/ecp/test/vc_common.c
+++ b/ecp/test/vc_common.c
@@ -2,6 +2,7 @@
#include <string.h>
#include <unistd.h>
#include <stdio.h>
+#include <arpa/inet.h>
#include <ecp/core.h>
#include <ecp/dir/dir.h>