From 01c3e3af2394f863323b846fa304ff7e0a30e9df Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Tue, 16 Jan 2018 23:43:07 +0100 Subject: eos support --- code/ecp/Makefile | 2 +- code/ecp/Makefile.fe310 | 12 +++++ code/ecp/Makefile.posix | 7 +++ code/ecp/config_fe310.h | 1 + code/ecp/config_posix.h | 5 ++ code/ecp/core.c | 92 ++++++++++++++++++++++--------------- code/ecp/core.h | 25 ++++++---- code/ecp/crypto/Makefile | 2 +- code/ecp/crypto/chacha/Makefile | 2 +- code/ecp/crypto/compat/Makefile | 2 +- code/ecp/crypto/curve25519/Makefile | 2 +- code/ecp/crypto/poly1305/Makefile | 2 +- code/ecp/crypto/sha/Makefile | 2 +- code/ecp/crypto/test/Makefile | 2 +- code/ecp/fe310/Makefile | 4 +- code/ecp/fe310/time.c | 9 +++- code/ecp/fe310/transport.c | 46 +++++++++++++++---- code/ecp/htable/Makefile | 2 +- code/ecp/posix/Makefile | 2 +- code/ecp/posix/transport.c | 8 ++-- code/ecp/rbuf.c | 4 +- code/ecp/rbuf.h | 8 ++-- code/ecp/rbuf_recv.c | 55 +++++++++++----------- code/ecp/rbuf_send.c | 34 +++++++++----- code/ecp/timer.c | 1 + code/ecp/vconn/Makefile | 2 +- code/ecp/vconn/vconn.c | 16 +++++-- 27 files changed, 225 insertions(+), 124 deletions(-) create mode 100644 code/ecp/Makefile.fe310 create mode 100644 code/ecp/Makefile.posix create mode 100644 code/ecp/config_fe310.h create mode 100644 code/ecp/config_posix.h (limited to 'code/ecp') diff --git a/code/ecp/Makefile b/code/ecp/Makefile index 88d8c84..1b16f39 100644 --- a/code/ecp/Makefile +++ b/code/ecp/Makefile @@ -1,4 +1,4 @@ -include ../Makefile.platform +include Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -I. obj = core.o timer.o $(obj_rbuf) diff --git a/code/ecp/Makefile.fe310 b/code/ecp/Makefile.fe310 new file mode 100644 index 0000000..130566b --- /dev/null +++ b/code/ecp/Makefile.fe310 @@ -0,0 +1,12 @@ +htable= +vconn=vconn +platform=fe310 +rbuf_obj= + +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=-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 +# LDFLAGS_PL=-lm -pthread \ No newline at end of file diff --git a/code/ecp/Makefile.posix b/code/ecp/Makefile.posix new file mode 100644 index 0000000..2680875 --- /dev/null +++ b/code/ecp/Makefile.posix @@ -0,0 +1,7 @@ +htable=htable +vconn=vconn +platform=posix +obj_rbuf=rbuf.o rbuf_send.o rbuf_recv.o msgq.o + +CFLAGS_PL=-O3 +LDFLAGS_PL=-lm -pthread \ No newline at end of file diff --git a/code/ecp/config_fe310.h b/code/ecp/config_fe310.h new file mode 100644 index 0000000..fc95c26 --- /dev/null +++ b/code/ecp/config_fe310.h @@ -0,0 +1 @@ +#define ECP_DEBUG 1 \ No newline at end of file diff --git a/code/ecp/config_posix.h b/code/ecp/config_posix.h new file mode 100644 index 0000000..ad67d8a --- /dev/null +++ b/code/ecp/config_posix.h @@ -0,0 +1,5 @@ +#define ECP_WITH_PTHREAD 1 +#define ECP_WITH_HTABLE 1 +#define ECP_WITH_RBUF 1 +#define ECP_WITH_MSGQ 1 +#define ECP_DEBUG 1 \ No newline at end of file diff --git a/code/ecp/core.c b/code/ecp/core.c index f81bbc6..3f77c6f 100644 --- a/code/ecp/core.c +++ b/code/ecp/core.c @@ -544,7 +544,7 @@ static ssize_t _conn_send_kget(ECPConnection *conn, ECPTimerItem *ti) { payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn); ecp_pld_set_type(pld_buf, ECP_MTYPE_KGET_REQ); - return ecp_pld_send_ll(conn, &packet, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_KGET_REQ), ti); + return ecp_pld_send_ll(conn, &packet, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_KGET_REQ), 0, ti); } int ecp_conn_init(ECPConnection *conn, ECPNode *node) { @@ -687,7 +687,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), ti); + return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(1, ECP_MTYPE_OPEN_REQ), 0, ti); } ssize_t ecp_conn_send_open(ECPConnection *conn) { @@ -781,6 +781,7 @@ ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char m unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_OPEN_REP, conn)]; unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_OPEN_REP, conn)]; unsigned char ctype = 0; + packet.buffer = pkt_buf; packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_OPEN_REP, conn); payload.buffer = pld_buf; @@ -788,7 +789,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)); + ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_OPEN_REP), 0); return 1; } @@ -820,8 +821,10 @@ ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char m int rv = ecp_conn_dhkey_new_pub(conn, msg[0], msg+1); if (!rv && !is_open) { ecp_conn_open_t *conn_open = ctx->handler[conn->type] ? ctx->handler[conn->type]->conn_open : NULL; - ssize_t _rv = conn_open(conn); - if (_rv < 0) rv = _rv; + if (conn_open) { + ssize_t _rv = conn_open(conn); + if (_rv < 0) rv = _rv; + } } if (rv) return rv; @@ -844,7 +847,7 @@ ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char m ecp_pld_set_type(pld_buf, ECP_MTYPE_KGET_REP); 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)); + ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP), 0); return 0; } @@ -875,7 +878,7 @@ ssize_t ecp_conn_handle_kput(ECPConnection *conn, ecp_seq_t seq, unsigned char m if (rv) return rv; ecp_pld_set_type(pld_buf, ECP_MTYPE_KPUT_REP); - ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_KPUT_REP)); + ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_KPUT_REP), 0); return ECP_ECDH_SIZE_KEY+1; } @@ -886,6 +889,7 @@ ssize_t ecp_conn_handle_kput(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; + if (b->packet->buffer == NULL) return ECP_ERR; memcpy(b->packet->buffer, msg, size); return ecp_pkt_handle(conn->sock, NULL, conn, b, size); @@ -931,7 +935,7 @@ static ssize_t _conn_send_kput(ECPConnection *conn, ECPTimerItem *ti) { rv = ecp_conn_dhkey_get_curr(conn, buf, buf+1); if (rv) return rv; - return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KPUT_REQ), ti); + return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KPUT_REQ), 0, ti); } int ecp_conn_dhkey_new(ECPConnection *conn) { @@ -1006,7 +1010,6 @@ int ecp_conn_dhkey_get_curr(ECPConnection *conn, unsigned char *idx, unsigned ch ssize_t ecp_pack(ECPContext *ctx, 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) { ssize_t rv; - if (payload == NULL) return ECP_ERR; if (pkt_size < ECP_SIZE_PKT_HDR) return ECP_ERR; // ECP_SIZE_PROTO @@ -1114,21 +1117,30 @@ ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_siz } ssize_t _ecp_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) { + if ((packet == NULL) || (packet->buffer == NULL)) return ECP_ERR; + if ((payload == NULL) || (payload->buffer == NULL)) return ECP_ERR; + return ecp_conn_pack(conn, packet->buffer, packet->size, s_idx, c_idx, payload->buffer, pld_size, si, addr); } ssize_t _ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, 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, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr) { + if ((packet == NULL) || (packet->buffer == NULL)) return ECP_ERR; + if ((payload == NULL) || (payload->buffer == NULL)) return ECP_ERR; + return ecp_pack_raw(sock, parent, packet->buffer, packet->size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size, addr); } -ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, unsigned char *payload, size_t pld_size, unsigned char *packet, size_t pkt_size, ECPConnection **_conn, ecp_seq_t *_seq) { +ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size, ECPConnection **_conn, ecp_seq_t *_seq) { unsigned char s_idx; unsigned char c_idx; unsigned char l_idx = ECP_ECDH_IDX_INV; + unsigned char *payload = bufs->payload->buffer; + unsigned char *packet = bufs->packet->buffer; ecp_aead_key_t shsec; ecp_dh_public_t public; ecp_dh_private_t private; + ECPContext *ctx = sock->ctx; ECPConnection *conn = NULL; ECPDHKey *key = NULL; int rv = ECP_OK; @@ -1198,7 +1210,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, uns memset(&private, 0, sizeof(private)); } - dec_size = sock->ctx->cr.aead_dec(payload, pld_size, packet+ECP_SIZE_PKT_HDR, pkt_size-ECP_SIZE_PKT_HDR, &shsec, packet+ECP_SIZE_PROTO+1+ECP_ECDH_SIZE_KEY); + dec_size = sock->ctx->cr.aead_dec(payload, bufs->payload->size, packet+ECP_SIZE_PKT_HDR, pkt_size-ECP_SIZE_PKT_HDR, &shsec, packet+ECP_SIZE_PROTO+1+ECP_ECDH_SIZE_KEY); if (dec_size < ECP_SIZE_PLD_HDR+1) rv = ECP_ERR_DECRYPT; if (rv) goto ecp_unpack_err; @@ -1208,6 +1220,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, uns (payload[2] << 8) | \ (payload[3]); + if (ctx->tr.buf_free && ((payload[ECP_SIZE_PLD_HDR] & ECP_MTYPE_MASK) < ECP_MAX_MTYPE_SYS)) ctx->tr.buf_free(bufs, ECP_SEND_FLAG_MORE); if (conn == NULL) { if (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_OPEN_REQ) { is_new = 1; @@ -1229,7 +1242,7 @@ ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, uns rv = ecp_sock_dhkey_get_curr(sock, buf, buf+1); if (!rv) { - ssize_t _rv = ecp_pld_send_raw(sock, parent, addr, &_packet, s_idx, c_idx, &public, &shsec, packet+ECP_SIZE_PKT_HDR, seq_p, &_payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP)); + ssize_t _rv = ecp_pld_send_raw(sock, parent, addr, &_packet, s_idx, c_idx, &public, &shsec, packet+ECP_SIZE_PKT_HDR, seq_p, &_payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP), 0); if (_rv < 0) rv = _rv; } if (rv) return rv; @@ -1327,13 +1340,13 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECPConnection *conn; ecp_seq_t seq; ssize_t rv = 0; - ssize_t pld_size = ecp_unpack(sock, addr, parent, bufs->payload->buffer, bufs->payload->size, bufs->packet->buffer, pkt_size, &conn, &seq); + ssize_t pld_size = ecp_unpack(sock, addr, parent, bufs, pkt_size, &conn, &seq); if (pld_size < 0) return pld_size; if (conn) { rv = 0; #ifdef ECP_WITH_RBUF - if (conn->rbuf.recv) rv = ecp_rbuf_recv_store(conn, seq, bufs->payload->buffer+ECP_SIZE_PLD_HDR, pld_size-ECP_SIZE_PLD_HDR); + if (conn->rbuf.recv) rv = ecp_rbuf_recv_store(conn, seq, bufs->payload->buffer+ECP_SIZE_PLD_HDR, pld_size-ECP_SIZE_PLD_HDR, bufs); #endif if (rv == 0) rv = ecp_msg_handle(conn, seq, bufs->payload->buffer+ECP_SIZE_PLD_HDR, pld_size-ECP_SIZE_PLD_HDR, bufs); @@ -1352,11 +1365,15 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, return ECP_SIZE_PKT_HDR+ECP_AEAD_SIZE_TAG+ECP_SIZE_PLD_HDR+rv; } -ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size) { - ssize_t rv = sock->ctx->tr.send(&sock->sock, packet, pkt_size, addr); +ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t pkt_size, unsigned char flags) { + ssize_t rv = sock->ctx->tr.send(&sock->sock, packet, pkt_size, addr, flags); if (rv < 0) return rv; if (rv < ECP_MIN_PKT) return ECP_ERR_SEND; + if (flags & ECP_SEND_FLAG_REPLY) { + packet->buffer = NULL; + packet->size = 0; + } return rv; } @@ -1536,7 +1553,7 @@ unsigned char *ecp_pld_get_buf(unsigned char *payload, unsigned char mtype) { return payload + ECP_SIZE_PLD_HDR + 1 + ECP_SIZE_MT_FLAG(mtype); } -static ssize_t pld_send(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPTimerItem *ti, ECPSeqItem *si) { +static ssize_t pld_send(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti, ECPSeqItem *si) { ECPSocket *sock = conn->sock; ECPContext *ctx = sock->ctx; ECPNetAddr addr; @@ -1545,25 +1562,25 @@ static ssize_t pld_send(ECPConnection *conn, ECPBuffer *packet, unsigned char s_ if (rv < 0) return rv; #ifdef ECP_WITH_RBUF - if (conn->rbuf.send) return ecp_rbuf_pkt_send(conn->rbuf.send, conn->sock, &addr, packet->buffer, rv, ti, si); + 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; } - return ecp_pkt_send(sock, &addr, packet->buffer, rv); + return ecp_pkt_send(sock, &addr, packet, rv, flags); } -ssize_t ecp_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size) { - return ecp_pld_send_ll(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, pld_size, NULL); +ssize_t ecp_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags) { + return ecp_pld_send_ll(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, pld_size, flags, NULL); } -ssize_t ecp_pld_send_wtimer(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, ECPTimerItem *ti) { - return ecp_pld_send_ll(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, pld_size, ti); +ssize_t ecp_pld_send_wtimer(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti) { + return ecp_pld_send_ll(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, pld_size, flags, ti); } -ssize_t ecp_pld_send_ll(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPTimerItem *ti) { +ssize_t ecp_pld_send_ll(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti) { ECPSeqItem *_seq_item = NULL; #ifdef ECP_WITH_RBUF @@ -1576,10 +1593,10 @@ ssize_t ecp_pld_send_ll(ECPConnection *conn, ECPBuffer *packet, unsigned char s_ } #endif - return pld_send(conn, packet, s_idx, c_idx, payload, pld_size, ti, _seq_item); + return pld_send(conn, packet, s_idx, c_idx, payload, pld_size, flags, ti, _seq_item); } -ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *addr, ECPBuffer *packet, 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, ECPBuffer *payload, size_t pld_size) { +ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *addr, ECPBuffer *packet, 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, ECPBuffer *payload, size_t pld_size, unsigned char flags) { ECPContext *ctx = sock->ctx; ECPNetAddr _addr; ssize_t rv; @@ -1587,7 +1604,7 @@ ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *add rv = ctx->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->buffer, rv); + return ecp_pkt_send(sock, parent ? &_addr : addr, packet, rv, flags); } ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *content, size_t content_size) { @@ -1641,7 +1658,7 @@ ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *conten content += frag_size; seq_item.seq = seq_start + i; - ssize_t _rv = pld_send(conn, &packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, &payload, ECP_SIZE_PLD(frag_size, mtype), NULL, &seq_item); + ssize_t _rv = pld_send(conn, &packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, &payload, ECP_SIZE_PLD(frag_size, mtype), 0, NULL, &seq_item); if (_rv < 0) return _rv; rv += _rv; @@ -1649,7 +1666,7 @@ ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *conten } else { ecp_pld_set_type(pld_buf, mtype); memcpy(ecp_pld_get_buf(pld_buf, mtype), content, content_size); - rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(content_size, mtype)); + rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(content_size, mtype), 0); } return rv; } @@ -1669,17 +1686,14 @@ ssize_t ecp_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg #endif } -static int recv_p(ECPSocket *sock, ECPNetAddr *addr, unsigned char *pkt_buf, size_t size) { +static int recv_p(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t size) { ECP2Buffer bufs; - ECPBuffer packet; ECPBuffer payload; unsigned char pld_buf[ECP_MAX_PLD]; - bufs.packet = &packet; + bufs.packet = packet; bufs.payload = &payload; - packet.buffer = pkt_buf; - packet.size = ECP_MAX_PKT; payload.buffer = pld_buf; payload.size = ECP_MAX_PLD; @@ -1691,16 +1705,20 @@ static int recv_p(ECPSocket *sock, ECPNetAddr *addr, unsigned char *pkt_buf, siz int ecp_receiver(ECPSocket *sock) { ECPNetAddr addr; + ECPBuffer packet; unsigned char pkt_buf[ECP_MAX_PKT]; ecp_cts_t next = 0; - + ssize_t rv = 0; if (sock->ctx->tr.recv == NULL) return ECP_ERR; sock->running = 1; while(sock->running) { - ssize_t rv = sock->ctx->tr.recv(&sock->sock, pkt_buf, ECP_MAX_PKT, &addr, next ? next : sock->poll_timeout); + packet.buffer = pkt_buf; + packet.size = ECP_MAX_PKT; + + rv = sock->ctx->tr.recv(&sock->sock, &packet, &addr, next ? next : sock->poll_timeout); if (rv > 0) { - int _rv = recv_p(sock, &addr, pkt_buf, rv); + int _rv = recv_p(sock, &addr, &packet, rv); DPRINT(_rv, "ERR:recv_p - RV:%d\n", _rv); } next = ecp_timer_exe(sock); diff --git a/code/ecp/core.h b/code/ecp/core.h index ce81d9d..0248814 100644 --- a/code/ecp/core.h +++ b/code/ecp/core.h @@ -78,10 +78,13 @@ #define ECP_CONN_FLAG_REG 0x01 #define ECP_CONN_FLAG_OPEN 0x02 +#define ECP_SEND_FLAG_REPLY 0x01 +#define ECP_SEND_FLAG_MORE 0x02 + #define ecp_conn_is_reg(conn) ((conn->flags) & ECP_CONN_FLAG_REG) #define ecp_conn_is_open(conn) ((conn->flags) & ECP_CONN_FLAG_OPEN) -#include "../config.h" +#include "config.h" #include #include @@ -184,16 +187,20 @@ typedef struct ECPTransportIface { int init; int (*open) (ECPNetSock *, void *addr); void (*close) (ECPNetSock *); - ssize_t (*send) (ECPNetSock *, void *, size_t, ECPNetAddr *); - ssize_t (*recv) (ECPNetSock *, void *, size_t, ECPNetAddr *, int); + ssize_t (*send) (ECPNetSock *, struct ECPBuffer *packet, size_t, ECPNetAddr *, unsigned char); + ssize_t (*recv) (ECPNetSock *, struct ECPBuffer *packet, ECPNetAddr *, int); int (*addr_eq) (ECPNetAddr *, ECPNetAddr *); int (*addr_set) (ECPNetAddr *, void *addr); + void (*buf_free) (struct ECP2Buffer *, unsigned char); + void (*buf_flag_set) (struct ECP2Buffer *, unsigned char); + void (*buf_flag_clear) (struct ECP2Buffer *, unsigned char); } ECPTransportIface; typedef struct ECPTimeIface { int init; ecp_cts_t (*abstime_ms) (ecp_cts_t); void (*sleep_ms) (ecp_cts_t); + void (*timer_set) (ecp_cts_t); } ECPTimeIface; #ifdef ECP_WITH_HTABLE @@ -392,10 +399,10 @@ ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_siz ssize_t _ecp_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr); ssize_t _ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, 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, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr); -ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, unsigned char *payload, size_t pld_size, unsigned char *packet, size_t pkt_size, ECPConnection **_conn, ecp_seq_t *_seq); +ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size, ECPConnection **_conn, ecp_seq_t *_seq); ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size); -ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size); +ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t pkt_size, unsigned char flags); int ecp_seq_item_init(ECPSeqItem *seq_item); int ecp_frag_iter_init(ECPFragIter *iter, unsigned char *buffer, size_t buf_size); @@ -413,10 +420,10 @@ int ecp_pld_set_pts(unsigned char *payload, unsigned char mtype, ecp_pts_t pts); unsigned char *ecp_pld_get_buf(unsigned char *payload, unsigned char mtype); unsigned char ecp_pld_get_type(unsigned char *payload); -ssize_t ecp_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size); -ssize_t ecp_pld_send_wtimer(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, ECPTimerItem *ti); -ssize_t ecp_pld_send_ll(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPTimerItem *ti); -ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *addr, ECPBuffer *packet, 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, ECPBuffer *payload, size_t pld_size); +ssize_t ecp_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags); +ssize_t ecp_pld_send_wtimer(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti); +ssize_t ecp_pld_send_ll(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti); +ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *addr, ECPBuffer *packet, 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, ECPBuffer *payload, size_t pld_size, unsigned char flags); ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *content, size_t content_size); ssize_t ecp_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ecp_cts_t timeout); diff --git a/code/ecp/crypto/Makefile b/code/ecp/crypto/Makefile index a7145c7..797cb16 100644 --- a/code/ecp/crypto/Makefile +++ b/code/ecp/crypto/Makefile @@ -1,4 +1,4 @@ -include ../../Makefile.platform +include ../Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -Iinclude -I.. -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= obj = e_chacha20poly1305.o crypto.o diff --git a/code/ecp/crypto/chacha/Makefile b/code/ecp/crypto/chacha/Makefile index 8a977ae..e64aac3 100644 --- a/code/ecp/crypto/chacha/Makefile +++ b/code/ecp/crypto/chacha/Makefile @@ -1,4 +1,4 @@ -include ../../../Makefile.platform +include ../../Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= obj = chacha.o diff --git a/code/ecp/crypto/compat/Makefile b/code/ecp/crypto/compat/Makefile index 1afe391..9e96d9e 100644 --- a/code/ecp/crypto/compat/Makefile +++ b/code/ecp/crypto/compat/Makefile @@ -1,4 +1,4 @@ -include ../../../Makefile.platform +include ../../Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= getentropy = getentropy_osx diff --git a/code/ecp/crypto/curve25519/Makefile b/code/ecp/crypto/curve25519/Makefile index c3f94f0..5799e0e 100644 --- a/code/ecp/crypto/curve25519/Makefile +++ b/code/ecp/crypto/curve25519/Makefile @@ -1,4 +1,4 @@ -include ../../../Makefile.platform +include ../../Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= -DED25519 obj = curve25519.o curve25519-generic.o diff --git a/code/ecp/crypto/poly1305/Makefile b/code/ecp/crypto/poly1305/Makefile index 6bc384f..5751602 100644 --- a/code/ecp/crypto/poly1305/Makefile +++ b/code/ecp/crypto/poly1305/Makefile @@ -1,4 +1,4 @@ -include ../../../Makefile.platform +include ../../Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= obj = poly1305.o diff --git a/code/ecp/crypto/sha/Makefile b/code/ecp/crypto/sha/Makefile index 1eb727f..4dc3f39 100644 --- a/code/ecp/crypto/sha/Makefile +++ b/code/ecp/crypto/sha/Makefile @@ -1,4 +1,4 @@ -include ../../../Makefile.platform +include ../../Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -I../include -D__BEGIN_HIDDEN_DECLS= -D__END_HIDDEN_DECLS= obj = sha1dgst.o sha1_one.o sha256.o sha512.o diff --git a/code/ecp/crypto/test/Makefile b/code/ecp/crypto/test/Makefile index 2a5fa3e..8ea463d 100644 --- a/code/ecp/crypto/test/Makefile +++ b/code/ecp/crypto/test/Makefile @@ -1,4 +1,4 @@ -include ../../../Makefile.platform +include ../../Makefile.platform CFLAGS=$(CFLAGS_PL) -I.. -I../include LDFLAGS=$(LDFLAGS_PL) diff --git a/code/ecp/fe310/Makefile b/code/ecp/fe310/Makefile index 6dd42e2..1ccf322 100644 --- a/code/ecp/fe310/Makefile +++ b/code/ecp/fe310/Makefile @@ -1,5 +1,5 @@ -include ../../Makefile.platform -CFLAGS=$(CFLAGS_PL) $(PIC) -I.. +include ../Makefile.platform +CFLAGS=$(CFLAGS_PL) $(PIC) -I.. -I../../fe310 obj_tr = transport.o obj_tm = time.o diff --git a/code/ecp/fe310/time.c b/code/ecp/fe310/time.c index 2ff98cb..4b8b2a7 100644 --- a/code/ecp/fe310/time.c +++ b/code/ecp/fe310/time.c @@ -1,5 +1,7 @@ #include +#include + #include "encoding.h" #include "platform.h" @@ -10,9 +12,14 @@ static ecp_cts_t t_abstime_ms(ecp_cts_t msec) { return now_ms + msec; } +static void t_timer_set(ecp_cts_t next) { + uint32_t tick = next * (uint64_t)RTC_FREQ / 1000; + eos_timer_set(tick, 1); +} + int ecp_time_init(ECPTimeIface *t) { t->init = 1; t->abstime_ms = t_abstime_ms; - t->sleep_ms = NULL; + t->timer_set = t_timer_set; return 0; } diff --git a/code/ecp/fe310/transport.c b/code/ecp/fe310/transport.c index b833ee3..9ff04ca 100644 --- a/code/ecp/fe310/transport.c +++ b/code/ecp/fe310/transport.c @@ -1,10 +1,10 @@ #include #include -#include +#include +#include -#include "eos.h" -#include "net.h" +#include static int t_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) { if (addr1->port != addr2->port) return 0; @@ -20,27 +20,55 @@ static int t_open(int *sock, void *addr_s) { static void t_close(int *sock) { } -static ssize_t t_send(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) { - unsigned char *buf = msg; +static ssize_t t_send(int *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 cmd = EOS_NET_CMD_PKT; int rv; - buf -= addr_len; + if (flags & ECP_SEND_FLAG_MORE) { + cmd |= EOS_NET_CMD_FLAG_ONEW; + } + if (flags & ECP_SEND_FLAG_REPLY) { + if (packet && packet->buffer) buf = packet->buffer-addr_len; + } else { + buf = eos_net_alloc(); + } + 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(EOS_NET_CMD_PKT, buf, buf_size); + rv = eos_net_send(cmd, buf, buf_size); if (rv) return ECP_ERR_SEND; return msg_size; } +static void t_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); +} + +static void t_buf_flag_set(ECP2Buffer *b, unsigned char flags) { + size_t addr_len = ECP_IPv4_ADDR_SIZE + sizeof(uint16_t); + if (flags & ECP_SEND_FLAG_MORE) { + if (b && b->packet && b->packet->buffer) eos_net_reserve(b->packet->buffer-addr_len); + } +} + +static void t_buf_flag_clear(ECP2Buffer *b, unsigned char flags) { + if (flags & ECP_SEND_FLAG_MORE) { + eos_net_release(1); + } +} + int ecp_transport_init(ECPTransportIface *t) { t->init = 1; t->open = t_open; t->close = t_close; t->send = t_send; - t->recv = NULL; t->addr_eq = t_addr_eq; - t->addr_set = NULL; + t->buf_free = t_buf_free; + t->buf_flag_set = t_buf_flag_set; + t->buf_flag_clear = t_buf_flag_clear; return ECP_OK; } diff --git a/code/ecp/htable/Makefile b/code/ecp/htable/Makefile index eb73403..6f5b888 100644 --- a/code/ecp/htable/Makefile +++ b/code/ecp/htable/Makefile @@ -1,4 +1,4 @@ -include ../../Makefile.platform +include ../Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -I.. -std=gnu89 obj = htable.o hashtable.o hashtable_itr.o diff --git a/code/ecp/posix/Makefile b/code/ecp/posix/Makefile index 6dd42e2..595b0b5 100644 --- a/code/ecp/posix/Makefile +++ b/code/ecp/posix/Makefile @@ -1,4 +1,4 @@ -include ../../Makefile.platform +include ../Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -I.. obj_tr = transport.o diff --git a/code/ecp/posix/transport.c b/code/ecp/posix/transport.c index df8e0f5..bdd166a 100644 --- a/code/ecp/posix/transport.c +++ b/code/ecp/posix/transport.c @@ -71,17 +71,17 @@ static void t_close(int *sock) { close(*sock); } -static ssize_t t_send(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr) { +static ssize_t t_send(int *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, msg, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); + return sendto(*sock, packet->buffer, msg_size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); } -static ssize_t t_recv(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr, int timeout) { +static ssize_t t_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) { struct sockaddr_in servaddr; socklen_t addrlen = sizeof(servaddr); struct pollfd fds[] = { @@ -91,7 +91,7 @@ static ssize_t t_recv(int *sock, void *msg, size_t msg_size, ECPNetAddr *addr, i int rv = poll(fds, 1, timeout); memset((void *)&servaddr, 0, sizeof(servaddr)); if (rv == 1) { - ssize_t recvlen = recvfrom(*sock, msg, msg_size, 0, (struct sockaddr *)&servaddr, &addrlen); + ssize_t recvlen = recvfrom(*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/rbuf.c b/code/ecp/rbuf.c index b5f66a9..d24ac23 100644 --- a/code/ecp/rbuf.c +++ b/code/ecp/rbuf.c @@ -44,7 +44,7 @@ ssize_t ecp_rbuf_msg_store(ECPRBuffer *rbuf, ecp_seq_t seq, int idx, unsigned ch return msg_size; } -ssize_t ecp_rbuf_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, ecp_seq_t seq) { +ssize_t ecp_rbuf_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags, ecp_seq_t seq) { ECPSocket *sock = conn->sock; ECPContext *ctx = sock->ctx; ECPNetAddr addr; @@ -61,7 +61,7 @@ ssize_t ecp_rbuf_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *pay rv = ctx->pack(conn, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, pld_size, &seq_item, &addr); if (rv < 0) return rv; - rv = ecp_pkt_send(sock, &addr, packet->buffer, rv); + rv = ecp_pkt_send(sock, &addr, packet, rv, flags); if (rv < 0) return rv; return rv; diff --git a/code/ecp/rbuf.h b/code/ecp/rbuf.h index 145234f..c4ea08e 100644 --- a/code/ecp/rbuf.h +++ b/code/ecp/rbuf.h @@ -51,8 +51,8 @@ typedef struct ECPRBTimer { typedef struct ECPRBRecv { unsigned char flags; - unsigned char flush; unsigned char timer_pts; + unsigned char ack_do; unsigned short hole_max; unsigned short ack_rate; ecp_pts_t deliver_delay; @@ -94,7 +94,7 @@ int ecp_rbuf_start(ECPRBuffer *rbuf, ecp_seq_t seq); int ecp_rbuf_msg_idx(ECPRBuffer *rbuf, ecp_seq_t seq); ssize_t ecp_rbuf_msg_store(ECPRBuffer *rbuf, ecp_seq_t seq, int idx, unsigned char *msg, size_t msg_size, unsigned char test_flags, unsigned char set_flags); -ssize_t ecp_rbuf_pld_send(struct ECPConnection *conn, struct ECPBuffer *packet, struct ECPBuffer *payload, size_t pld_size, ecp_seq_t seq); +ssize_t ecp_rbuf_pld_send(struct ECPConnection *conn, struct ECPBuffer *packet, struct ECPBuffer *payload, size_t pld_size, unsigned char flags, ecp_seq_t seq); int ecp_rbuf_create(struct ECPConnection *conn, ECPRBSend *buf_s, ECPRBMessage *msg_s, unsigned int msg_s_size, ECPRBRecv *buf_r, ECPRBMessage *msg_r, unsigned int msg_r_size); void ecp_rbuf_destroy(struct ECPConnection *conn); @@ -104,13 +104,13 @@ void ecp_rbuf_recv_destroy(struct ECPConnection *conn); int ecp_rbuf_recv_set_hole(struct ECPConnection *conn, unsigned short hole_max); int ecp_rbuf_recv_set_delay(struct ECPConnection *conn, ecp_pts_t delay); int ecp_rbuf_recv_start(struct ECPConnection *conn, ecp_seq_t seq); -ssize_t ecp_rbuf_recv_store(struct ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size); +ssize_t ecp_rbuf_recv_store(struct ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size, struct ECP2Buffer *b); int ecp_rbuf_send_create(struct ECPConnection *conn, ECPRBSend *buf, ECPRBMessage *msg, unsigned int msg_size); void ecp_rbuf_send_destroy(struct ECPConnection *conn); int ecp_rbuf_send_start(struct ECPConnection *conn); int ecp_rbuf_pkt_prep(ECPRBSend *buf, struct ECPSeqItem *si, unsigned char mtype); -ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, struct ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size, ECPTimerItem *ti, struct ECPSeqItem *si); +ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, struct ECPSocket *sock, ECPNetAddr *addr, struct ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, struct ECPSeqItem *si); ssize_t ecp_rbuf_handle_ack(struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, struct ECP2Buffer *b); ssize_t ecp_rbuf_handle_flush(struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, struct ECP2Buffer *b); diff --git a/code/ecp/rbuf_recv.c b/code/ecp/rbuf_recv.c index 44b5113..c262f1d 100644 --- a/code/ecp/rbuf_recv.c +++ b/code/ecp/rbuf_recv.c @@ -5,7 +5,7 @@ #define ACK_RATE 8 #define ACK_MASK_FIRST ((ecp_ack_t)1 << (ECP_SIZE_ACKB - 1)) -static ssize_t msg_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size) { +static ssize_t msg_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { ECPRBRecv *buf = conn->rbuf.recv; unsigned char flags = ECP_RBUF_FLAG_IN_RBUF; unsigned char mtype = ecp_msg_get_type(msg) & ECP_MTYPE_MASK; @@ -29,12 +29,12 @@ static ssize_t msg_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, if (rv < 0) return rv; if (ECP_SEQ_LT(buf->rbuf.seq_max, seq)) buf->rbuf.seq_max = seq; - if (flags & ECP_RBUF_FLAG_SYS) ecp_msg_handle(conn, seq, msg, msg_size, NULL); + if (flags & ECP_RBUF_FLAG_SYS) ecp_msg_handle(conn, seq, msg, msg_size, b); return rv; } -static void msg_flush(ECPConnection *conn) { +static void msg_flush(ECPConnection *conn, ECP2Buffer *b) { ECPRBRecv *buf = conn->rbuf.recv; #ifdef ECP_WITH_MSGQ @@ -101,7 +101,7 @@ static void msg_flush(ECPConnection *conn) { buf->rbuf.msg[idx].flags |= ECP_RBUF_FLAG_IN_MSGQ; #endif } else { - ecp_msg_handle(conn, seq, buf->rbuf.msg[idx].msg, buf->rbuf.msg[idx].size, NULL); + ecp_msg_handle(conn, seq, buf->rbuf.msg[idx].msg, buf->rbuf.msg[idx].size, b); } } buf->rbuf.msg[idx].flags &= ~ECP_RBUF_FLAG_IN_RBUF; @@ -148,10 +148,11 @@ static int ack_send(ECPConnection *conn) { buf_[6] = (buf->ack_map & 0x0000FF00) >> 8; buf_[7] = (buf->ack_map & 0x000000FF); - rv = ecp_rbuf_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK), 0); + rv = ecp_rbuf_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK), 0, 0); if (rv < 0) return rv; buf->ack_pkt = 0; + buf->ack_do = 0; return ECP_OK; } @@ -170,7 +171,7 @@ static int ack_shift(ECPRBRecv *buf) { if (in_rbuf && (buf->ack_map == ECP_ACK_FULL)) continue; buf->ack_map = buf->ack_map << 1; - if (in_rbuf & ECP_RBUF_FLAG_IN_RBUF) { + if (in_rbuf) { buf->ack_map |= 1; } else if (!do_ack && ECP_SEQ_LTE(buf->seq_ack, buf->rbuf.seq_max - 2 * buf->hole_max)) { do_ack = 1; @@ -197,10 +198,12 @@ static int ack_shift(ECPRBRecv *buf) { ssize_t ecp_rbuf_handle_flush(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { if (size < 0) return size; + ECPContext *ctx = conn->sock->ctx; ECPRBRecv *buf = conn->rbuf.recv; if (buf == NULL) return ECP_ERR; - buf->flush = 1; + if (ctx->tr.buf_free) ctx->tr.buf_free(b, ECP_SEND_FLAG_MORE); + ack_send(conn); return 0; } @@ -209,7 +212,7 @@ ssize_t ecp_rbuf_handle_flush_pts(ECPConnection *conn, ecp_seq_t seq, unsigned c if (buf == NULL) return ECP_ERR; buf->timer_pts = 0; - msg_flush(conn); + msg_flush(conn, b); return 0; } @@ -282,17 +285,20 @@ int ecp_rbuf_recv_start(ECPConnection *conn, ecp_seq_t seq) { return ECP_OK; } -ssize_t ecp_rbuf_recv_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size) { +ssize_t ecp_rbuf_recv_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { ECPRBRecv *buf = conn->rbuf.recv; ecp_seq_t ack_pkt = 0; ssize_t rv; - int do_ack = 0; unsigned char mtype; mtype = ecp_msg_get_type(msg) & ECP_MTYPE_MASK; - if ((mtype == ECP_MTYPE_RBACK) || (mtype == ECP_MTYPE_RBFLUSH)) return ecp_msg_handle(conn, seq, msg, msg_size, NULL); + if ((mtype == ECP_MTYPE_RBACK) || (mtype == ECP_MTYPE_RBFLUSH)) return ecp_msg_handle(conn, seq, msg, msg_size, b); - if (ECP_SEQ_LT(buf->rbuf.seq_max, seq)) ack_pkt = seq - buf->rbuf.seq_max; + if (ECP_SEQ_LT(buf->rbuf.seq_max, seq)) { + ack_pkt = seq - buf->rbuf.seq_max; + buf->ack_pkt += ack_pkt; + if (buf->ack_pkt > buf->ack_rate) buf->ack_do = 1; + } if (ECP_SEQ_LTE(seq, buf->seq_ack)) { ecp_seq_t seq_offset = buf->seq_ack - seq; if (seq_offset < ECP_SIZE_ACKB) { @@ -301,9 +307,9 @@ ssize_t ecp_rbuf_recv_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *m if (ack_mask & buf->ack_map) return ECP_ERR_RBUF_DUP; buf->ack_map |= ack_mask; - do_ack = ack_shift(buf); + buf->ack_do = buf->ack_do || ack_shift(buf); - rv = msg_store(conn, seq, msg, msg_size); + rv = msg_store(conn, seq, msg, msg_size, b); if (rv < 0) return rv; } else { return ECP_ERR_RBUF_DUP; @@ -311,10 +317,10 @@ ssize_t ecp_rbuf_recv_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *m } else { if ((buf->ack_map == ECP_ACK_FULL) && (seq == (ecp_seq_t)(buf->seq_ack + 1))) { if ((buf->flags & ECP_RBUF_FLAG_MSGQ) || buf->deliver_delay) { - rv = msg_store(conn, seq, msg, msg_size); + rv = msg_store(conn, seq, msg, msg_size, b); if (rv < 0) return rv; } else { - ecp_msg_handle(conn, seq, msg, msg_size, NULL); + ecp_msg_handle(conn, seq, msg, msg_size, b); rv = msg_size; buf->rbuf.seq_max++; buf->rbuf.seq_start++; @@ -322,23 +328,14 @@ ssize_t ecp_rbuf_recv_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *m } buf->seq_ack++; } else { - rv = msg_store(conn, seq, msg, msg_size); + rv = msg_store(conn, seq, msg, msg_size, b); if (rv < 0) return rv; - do_ack = ack_shift(buf); + buf->ack_do = buf->ack_do || ack_shift(buf); } } - msg_flush(conn); - if (buf->flush) { - buf->flush = 0; - do_ack = 1; - } - if (ack_pkt && !do_ack) { - buf->ack_pkt += ack_pkt; - // should send acks more aggresively when reliable and ack_map is not full (rate ~ PPS * RTT) - if (buf->ack_pkt > buf->ack_rate) do_ack = 1; - } - if (do_ack) { + msg_flush(conn, b); + if (!(mtype < ECP_MAX_MTYPE_SYS) && buf->ack_do) { int _rv = ack_send(conn); if (_rv) return _rv; } diff --git a/code/ecp/rbuf_send.c b/code/ecp/rbuf_send.c index f913e7e..61b29af 100644 --- a/code/ecp/rbuf_send.c +++ b/code/ecp/rbuf_send.c @@ -25,7 +25,7 @@ static ssize_t flush_send(ECPConnection *conn, ECPTimerItem *ti) { rv = ecp_timer_push(&_ti); if (rv) return rv; } - return ecp_rbuf_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_RBFLUSH), 0); + return ecp_rbuf_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_RBFLUSH), 0, 0); } static void cc_flush(ECPConnection *conn) { @@ -65,8 +65,12 @@ static void cc_flush(ECPConnection *conn) { #endif for (i=0; imsg[_idx].idx_t != -1) ecp_timer_push(&ti[rbuf->msg[_idx].idx_t]); - ecp_pkt_send(conn->sock, &conn->node.addr, rbuf->msg[_idx].msg, rbuf->msg[_idx].size); + packet.buffer = rbuf->msg[_idx].msg; + packet.size = rbuf->msg[_idx].size; + ecp_pkt_send(conn->sock, &conn->node.addr, &packet, rbuf->msg[_idx].size, 0); _idx = ECP_RBUF_IDX_MASK(_idx + 1, rbuf->msg_size); } @@ -82,6 +86,7 @@ static void cc_flush(ECPConnection *conn) { ssize_t ecp_rbuf_handle_ack(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { ECPRBSend *buf; + ECPContext *ctx = conn->sock->ctx; ssize_t rsize = sizeof(ecp_seq_t)+sizeof(ecp_ack_t); ecp_seq_t seq_ack = 0; ecp_ack_t ack_map = 0; @@ -105,6 +110,8 @@ ssize_t ecp_rbuf_handle_ack(ECPConnection *conn, ecp_seq_t seq, unsigned char mt (msg[6] << 8) | \ (msg[7]); + if (ctx->tr.buf_flag_set) ctx->tr.buf_flag_set(b, ECP_SEND_FLAG_MORE); + #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&buf->mutex); #endif @@ -146,9 +153,12 @@ ssize_t ecp_rbuf_handle_ack(ECPConnection *conn, ecp_seq_t seq, unsigned char mt payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_NOP, conn); ecp_pld_set_type(pld_buf, ECP_MTYPE_NOP); - ecp_rbuf_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_NOP), seq_ack + i); + ecp_rbuf_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_NOP), 0, seq_ack + i); } else { - ecp_pkt_send(conn->sock, &conn->node.addr, rbuf->msg[_idx].msg, rbuf->msg[_idx].size); + ECPBuffer packet; + packet.buffer = rbuf->msg[_idx].msg; + packet.size = rbuf->msg[_idx].size; + ecp_pkt_send(conn->sock, &conn->node.addr, &packet, rbuf->msg[_idx].size, 0); } if (!nack_first) { nack_first = 1; @@ -188,12 +198,14 @@ ssize_t ecp_rbuf_handle_ack(ECPConnection *conn, ecp_seq_t seq, unsigned char mt pthread_mutex_unlock(&buf->mutex); #endif - if (rv) return rv; - - if (do_flush) { + if (!rv && do_flush) { ssize_t _rv = flush_send(conn, NULL); - if (_rv < 0) return _rv; + if (_rv < 0) rv = _rv; } + + if (ctx->tr.buf_flag_clear) ctx->tr.buf_flag_clear(b, ECP_SEND_FLAG_MORE); + + if (rv) return rv; return rsize; } @@ -308,7 +320,7 @@ int ecp_rbuf_pkt_prep(ECPRBSend *buf, ECPSeqItem *si, unsigned char mtype) { return ECP_OK; } -ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size, ECPTimerItem *ti, ECPSeqItem *si) { +ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t pkt_size, unsigned char flags, ECPTimerItem *ti, ECPSeqItem *si) { int do_send = 1; ssize_t rv = 0; @@ -320,7 +332,7 @@ ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, ECPSocket *sock, ECPNetAddr *addr, uns if (mtype < ECP_MAX_MTYPE_SYS) flags |= ECP_RBUF_FLAG_SYS; - rv = ecp_rbuf_msg_store(&buf->rbuf, seq, idx, packet, pkt_size, 0, flags); + rv = ecp_rbuf_msg_store(&buf->rbuf, seq, idx, packet->buffer, pkt_size, 0, flags); if (rv < 0) return rv; if (buf->flags & ECP_RBUF_FLAG_CCONTROL) { @@ -369,7 +381,7 @@ ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, ECPSocket *sock, ECPNetAddr *addr, uns int _rv = ecp_timer_push(ti); if (_rv) return _rv; } - rv = ecp_pkt_send(sock, addr, packet, pkt_size); + rv = ecp_pkt_send(sock, addr, packet, pkt_size, flags); } return rv; } diff --git a/code/ecp/timer.c b/code/ecp/timer.c index 691f75c..16a6ad0 100644 --- a/code/ecp/timer.c +++ b/code/ecp/timer.c @@ -58,6 +58,7 @@ int ecp_timer_push(ECPTimerItem *ti) { if (!rv && !is_reg) rv = ECP_ERR_CLOSED; if (!rv) { + if (ctx->tm.timer_set) ctx->tm.timer_set(ti->timeout); for (i=timer->head; i>=0; i--) { if (ECP_CTS_LTE(ti->abstime, timer->item[i].abstime)) { if (i != timer->head) memmove(timer->item+i+2, timer->item+i+1, sizeof(ECPTimerItem) * (timer->head-i)); diff --git a/code/ecp/vconn/Makefile b/code/ecp/vconn/Makefile index 613601c..d9d2e6f 100644 --- a/code/ecp/vconn/Makefile +++ b/code/ecp/vconn/Makefile @@ -1,4 +1,4 @@ -include ../../Makefile.platform +include ../Makefile.platform CFLAGS=$(CFLAGS_PL) $(PIC) -I.. obj = vconn.o diff --git a/code/ecp/vconn/vconn.c b/code/ecp/vconn/vconn.c index dadff0f..4023aa9 100644 --- a/code/ecp/vconn/vconn.c +++ b/code/ecp/vconn/vconn.c @@ -81,7 +81,7 @@ static ssize_t _vconn_send_open(ECPConnection *conn, ECPTimerItem *ti) { payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn); ecp_pld_set_type(pld_buf, ECP_MTYPE_KGET_REQ); - return ecp_pld_send_ll(conn, &packet, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_KGET_REQ), ti); + return ecp_pld_send_ll(conn, &packet, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_KGET_REQ), 0, ti); } static ssize_t vconn_open(ECPConnection *conn) { @@ -198,7 +198,7 @@ static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c payload.size = b->payload->size - (payload.buffer - b->payload->buffer); ecp_pld_set_type(payload.buffer, ECP_MTYPE_EXEC); - rv = ecp_pld_send(conn_out, b->packet, &payload, ECP_SIZE_PLD_HDR+1+size); + rv = ecp_pld_send(conn_out, b->packet, &payload, ECP_SIZE_PLD_HDR+1+size, ECP_SEND_FLAG_REPLY); #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn_out->mutex); @@ -286,7 +286,7 @@ static ssize_t _vlink_send_open(ECPConnection *conn, ECPTimerItem *ti) { buf[0] = conn->type; memcpy(buf+1, ctx->cr.dh_pub_get_buf(&sock->key_perma.public), ECP_ECDH_SIZE_KEY); - return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ), ti); + return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ), 0, ti); } static ssize_t vlink_open(ECPConnection *conn) { @@ -372,7 +372,7 @@ static ssize_t vlink_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c payload.size = b->payload->size - (payload.buffer - b->payload->buffer); ecp_pld_set_type(payload.buffer, ECP_MTYPE_EXEC); - rv = ecp_pld_send(conn, b->packet, &payload, ECP_SIZE_PLD_HDR+1+size); + rv = ecp_pld_send(conn, b->packet, &payload, ECP_SIZE_PLD_HDR+1+size, ECP_SEND_FLAG_REPLY); #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); @@ -422,6 +422,9 @@ static ssize_t vconn_set_msg(ECPConnection *conn, ECPBuffer *payload, unsigned c static ssize_t vconn_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr) { ECPContext *ctx = conn->sock->ctx; + if ((packet == NULL) || (packet->buffer == NULL)) return ECP_ERR; + if ((payload == NULL) || (payload->buffer == NULL)) return ECP_ERR; + if (conn->parent) { unsigned char mtype = ecp_pld_get_type(payload->buffer); ssize_t rv, hdr_size = vconn_set_msg(conn->parent, packet, mtype); @@ -442,6 +445,9 @@ static ssize_t vconn_pack(ECPConnection *conn, ECPBuffer *packet, unsigned char static ssize_t vconn_pack_raw(ECPSocket *sock, ECPConnection *parent, ECPBuffer *packet, 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, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr) { ECPContext *ctx = sock->ctx; + if ((packet == NULL) || (packet->buffer == NULL)) return ECP_ERR; + if ((payload == NULL) || (payload->buffer == NULL)) return ECP_ERR; + if (parent) { unsigned char mtype = ecp_pld_get_type(payload->buffer); ssize_t rv, hdr_size = vconn_set_msg(parent, packet, mtype); @@ -599,7 +605,7 @@ static ssize_t _vconn_send_kget(ECPConnection *conn, ECPTimerItem *ti) { payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn); ecp_pld_set_type(pld_buf, ECP_MTYPE_KGET_REQ); - return ecp_pld_send_ll(conn, &packet, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_KGET_REQ), ti); + return ecp_pld_send_ll(conn, &packet, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_KGET_REQ), 0, ti); } int ecp_vconn_open(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size) { -- cgit v1.2.3