diff options
author | Uros Majstorovic <majstor@majstor.org> | 2017-12-06 07:17:24 +0100 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2017-12-06 07:17:24 +0100 |
commit | 848f44266dd4b9213af632d55747d66f03881422 (patch) | |
tree | 2f1a348e6e5c77760bbbec8d2ec54e89cedc7e66 /code | |
parent | 028af540a2d856bccc84d3f5e3e8d3517105e411 (diff) |
refactoring for memory limited systems
Diffstat (limited to 'code')
-rw-r--r-- | code/core/core.c | 448 | ||||
-rw-r--r-- | code/core/core.h | 68 | ||||
-rw-r--r-- | code/core/rbuf.c | 7 | ||||
-rw-r--r-- | code/core/rbuf.h | 12 | ||||
-rw-r--r-- | code/core/rbuf_recv.c | 29 | ||||
-rw-r--r-- | code/core/rbuf_send.c | 38 | ||||
-rw-r--r-- | code/core/timer.c | 11 | ||||
-rw-r--r-- | code/core/timer.h | 2 | ||||
-rw-r--r-- | code/test/basic.c | 21 | ||||
-rw-r--r-- | code/test/client.c | 13 | ||||
-rw-r--r-- | code/test/echo.c | 7 | ||||
-rw-r--r-- | code/test/frag.c | 8 | ||||
-rw-r--r-- | code/test/server.c | 8 | ||||
-rw-r--r-- | code/test/stress.c | 49 | ||||
-rw-r--r-- | code/test/vc_client.c | 13 | ||||
-rw-r--r-- | code/test/vc_server.c | 12 | ||||
-rw-r--r-- | code/test/vid/Makefile | 4 | ||||
-rw-r--r-- | code/test/vid/client.c | 6 | ||||
-rw-r--r-- | code/test/voip.c | 6 | ||||
-rw-r--r-- | code/vconn/vconn.c | 207 |
20 files changed, 610 insertions, 359 deletions
diff --git a/code/core/core.c b/code/core/core.c index fafda4f..b273fcd 100644 --- a/code/core/core.c +++ b/code/core/core.c @@ -33,8 +33,8 @@ int ecp_ctx_create(ECPContext *ctx) { memset(ctx, 0, sizeof(ECPContext)); - ctx->pack = ecp_conn_pack; - ctx->pack_raw = ecp_pack_raw; + ctx->pack = _ecp_pack; + ctx->pack_raw = _ecp_pack_raw; rv = ecp_crypto_init(&ctx->cr); if (rv) return rv; @@ -528,10 +528,17 @@ void ecp_conn_unregister(ECPConnection *conn) { } static ssize_t _conn_send_kget(ECPConnection *conn, ECPTimerItem *ti) { - unsigned char payload[ECP_SIZE_PLD(0, 0)]; + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn)]; + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn); - ecp_pld_set_type(payload, ECP_MTYPE_KGET_REQ); - return ecp_pld_send_ll(conn, ti, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, payload, sizeof(payload)); + 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); } int ecp_conn_init(ECPConnection *conn, ECPNode *node) { @@ -660,20 +667,28 @@ int ecp_conn_handler_init(ECPConnHandler *handler) { } static ssize_t _conn_send_open(ECPConnection *conn, ECPTimerItem *ti) { - unsigned char payload[ECP_SIZE_PLD(1, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); + ECPBuffer packet; + ECPBuffer payload; + 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); - ecp_pld_set_type(payload, ECP_MTYPE_OPEN_REQ); + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(1, ECP_MTYPE_OPEN_REQ, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(1, ECP_MTYPE_OPEN_REQ, conn); + + ecp_pld_set_type(pld_buf, ECP_MTYPE_OPEN_REQ); buf[0] = conn->type; - return ecp_pld_send_wtimer(conn, ti, payload, sizeof(payload)); + return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(1, ECP_MTYPE_OPEN_REQ), ti); } ssize_t ecp_conn_send_open(ECPConnection *conn) { - return ecp_timer_send(conn, _conn_send_open, ECP_MTYPE_OPEN_REP, 3, 500); + return ecp_timer_send(conn, _conn_send_open, ECP_MTYPE_OPEN_REP, 1, 500); } -int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, unsigned char *c_public, ecp_aead_key_t *shsec, unsigned char *payload, size_t payload_size) { +int ecp_conn_handle_new(ECPSocket *sock, ECPConnection *parent, unsigned char *payload, size_t payload_size, ECPConnection **_conn) { ECPConnection *conn = NULL; int rv = ECP_OK; unsigned char ctype = 0; @@ -695,17 +710,10 @@ int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *p 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; - rv = conn_dhkey_new_pub_local(conn, s_idx); - if (!rv) rv = conn_dhkey_new_pub_remote(conn, c_idx, c_public); - if (!rv) rv = conn_shsec_set(conn, s_idx, c_idx, shsec); - if (rv) { - ecp_conn_destroy(conn); - if (sock->ctx->conn_free) sock->ctx->conn_free(conn); - return rv; - } if (handle_create) rv = handle_create(conn, payload+1, payload_size-1); if (rv) { ecp_conn_destroy(conn); @@ -735,7 +743,7 @@ int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *p return rv; } -ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); #endif @@ -758,24 +766,30 @@ ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char m if (size < 0) return size; return 0; } else { - unsigned char payload[ECP_SIZE_PLD(0, 0)]; - unsigned char ctype = 0; - if (conn->out) return ECP_ERR; if (size < 0) return size; if (size < 1) return ECP_ERR; - ctype = msg[0]; - - ecp_pld_set_type(payload, ECP_MTYPE_OPEN_REP); - ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); + ECPBuffer packet; + ECPBuffer payload; + 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; + payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_OPEN_REP, conn); + 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)); + return 1; } return ECP_ERR; } -ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +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; @@ -791,7 +805,7 @@ ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char m if ((size < 0) && !is_open) { ecp_conn_handler_msg_t *handler = ctx->handler[conn->type] ? ctx->handler[conn->type]->msg[ECP_MTYPE_OPEN] : NULL; - return handler ? handler(conn, seq, mtype, msg, size) : size; + return handler ? handler(conn, seq, mtype, msg, size, b) : size; } if (size < 0) return size; @@ -807,61 +821,111 @@ ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char m return ECP_ECDH_SIZE_KEY+1; } else { - unsigned char payload[ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); - ecp_pld_set_type(payload, ECP_MTYPE_KGET_REP); - if (conn->out) return ECP_ERR; if (size < 0) return size; + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP, conn)]; + unsigned char *buf = ecp_pld_get_buf(pld_buf, 0); + + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP, conn); + + 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, payload, sizeof(payload)); - return 0; } return ECP_ERR; } -ssize_t ecp_conn_handle_kput(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +ssize_t ecp_conn_handle_kput(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { if (size < 0) return size; if (mtype & ECP_MTYPE_FLAG_REP) { if (!conn->out) return ECP_ERR; return 0; } else { - unsigned char payload[ECP_SIZE_PLD(0, 0)]; - if (conn->out) return ECP_ERR; if (size < ECP_ECDH_SIZE_KEY+1) return ECP_ERR; + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KPUT_REP, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KPUT_REP, conn)]; + + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KPUT_REP, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KPUT_REP, conn); + int rv = ecp_conn_dhkey_new_pub(conn, msg[0], msg+1); if (rv) return rv; - ecp_pld_set_type(payload, ECP_MTYPE_KPUT_REP); - ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); + 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)); return ECP_ECDH_SIZE_KEY+1; } return ECP_ERR; } -ssize_t ecp_conn_handle_exec(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +/* Memory limited version: */ +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; - return ecp_pkt_handle(conn->sock, NULL, conn, msg, size); + if (b == NULL) return ECP_ERR; + + memcpy(b->packet->buffer, msg, size); + return ecp_pkt_handle(conn->sock, NULL, conn, b, size); } +/* Non memory limited version: */ +/* +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; + unsigned char pld_buf[ECP_MAX_PLD]; + + b2.packet = &packet; + b2.payload = &payload; + + packet.buffer = msg; + 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); +} +*/ + static ssize_t _conn_send_kput(ECPConnection *conn, ECPTimerItem *ti) { - unsigned char payload[ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KPUT_REQ, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KPUT_REQ, conn)]; + unsigned char *buf = ecp_pld_get_buf(pld_buf, ECP_MTYPE_KPUT_REQ); int rv = ECP_OK; - - ecp_pld_set_type(payload, ECP_MTYPE_KPUT_REQ); + + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KPUT_REQ, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KPUT_REQ, conn); + + ecp_pld_set_type(pld_buf, ECP_MTYPE_KPUT_REQ); rv = ecp_conn_dhkey_get_curr(conn, buf, buf+1); if (rv) return rv; - return ecp_pld_send_wtimer(conn, ti, payload, sizeof(payload)); + return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KPUT_REQ), ti); } int ecp_conn_dhkey_new(ECPConnection *conn) { @@ -933,7 +997,7 @@ int ecp_conn_dhkey_get_curr(ECPConnection *conn, unsigned char *idx, unsigned ch return ECP_OK; } -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 payload_size) { +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; @@ -952,7 +1016,7 @@ ssize_t ecp_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, unsign payload[1] = (seq & 0x00FF0000) >> 16; payload[2] = (seq & 0x0000FF00) >> 8; payload[3] = (seq & 0x000000FF); - rv = ctx->cr.aead_enc(packet+ECP_SIZE_PKT_HDR, pkt_size-ECP_SIZE_PKT_HDR, payload, payload_size, shsec, nonce); + rv = ctx->cr.aead_enc(packet+ECP_SIZE_PKT_HDR, pkt_size-ECP_SIZE_PKT_HDR, payload, pld_size, shsec, nonce); if (rv < 0) return ECP_ERR_ENCRYPT; memcpy(nonce, packet+ECP_SIZE_PKT_HDR, ECP_AEAD_SIZE_NONCE); @@ -960,13 +1024,13 @@ ssize_t ecp_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, unsign return rv+ECP_SIZE_PKT_HDR; } -ssize_t ecp_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 payload_size, ECPNetAddr *addr) { +ssize_t ecp_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_pack(ctx, packet, pkt_size, s_idx, c_idx, public, shsec, nonce, seq, payload, payload_size); + return ecp_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 payload_size, ECPSeqItem *si, ECPNetAddr *addr) { +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; ecp_seq_t _seq; @@ -1012,7 +1076,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, payload); + 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; @@ -1029,7 +1093,7 @@ ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_siz if (rv) return rv; - ssize_t _rv = ecp_pack(conn->sock->ctx, packet, pkt_size, s_idx, c_idx, &public, &shsec, nonce, _seq, payload, payload_size); + ssize_t _rv = ecp_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 @@ -1043,24 +1107,35 @@ ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_siz return _rv; } -ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, unsigned char *packet, size_t pkt_size) { +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) { + 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) { + 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) { unsigned char s_idx; unsigned char c_idx; unsigned char l_idx = ECP_ECDH_IDX_INV; - unsigned char nonce[ECP_AEAD_SIZE_NONCE]; ecp_aead_key_t shsec; ecp_dh_public_t public; ecp_dh_private_t private; - unsigned char payload[ECP_MAX_PLD]; ECPConnection *conn = NULL; ECPDHKey *key = NULL; int rv = ECP_OK; - int is_open = 0; - int seq_check = 1; + unsigned char is_open = 0; + unsigned char is_new = 0; + unsigned char seq_check = 1; ecp_seq_t seq_c, seq_p, seq_n; ecp_ack_t seq_map; - ssize_t pld_size, cnt_size, proc_size; + ssize_t dec_size; + *_conn = NULL; + *_seq = 0; + s_idx = (packet[ECP_SIZE_PROTO] & 0xF0) >> 4; c_idx = (packet[ECP_SIZE_PROTO] & 0x0F); @@ -1117,9 +1192,9 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, memset(&private, 0, sizeof(private)); } - pld_size = sock->ctx->cr.aead_dec(payload, ECP_MAX_PLD, packet+ECP_SIZE_PKT_HDR, pkt_size-ECP_SIZE_PKT_HDR, &shsec, packet+ECP_SIZE_PROTO+1+ECP_ECDH_SIZE_KEY); - if (pld_size < ECP_SIZE_PLD_HDR+1) rv = ECP_ERR_DECRYPT; - if (rv) goto pkt_handle_err; + 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); + if (dec_size < ECP_SIZE_PLD_HDR+1) rv = ECP_ERR_DECRYPT; + if (rv) goto ecp_unpack_err; seq_p = \ (payload[0] << 24) | \ @@ -1127,124 +1202,108 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, (payload[2] << 8) | \ (payload[3]); - memcpy(nonce, packet+ECP_SIZE_PKT_HDR, sizeof(nonce)); - if (conn == NULL) { if (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_OPEN_REQ) { - rv = sock->conn_new(sock, &conn, parent, s_idx, c_idx, packet+ECP_SIZE_PROTO+1, &shsec, payload+ECP_SIZE_PLD_HDR+1, pld_size-ECP_SIZE_PLD_HDR-1); + is_new = 1; + rv = sock->conn_new(sock, parent, payload+ECP_SIZE_PLD_HDR+1, dec_size-ECP_SIZE_PLD_HDR-1, &conn); if (rv) return rv; - - seq_n = seq_p; - seq_map = 1; } else if (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_KGET_REQ) { - unsigned char payload_[ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload_, 0); - ecp_pld_set_type(payload_, ECP_MTYPE_KGET_REP); - + ECPBuffer _packet; + ECPBuffer _payload; + unsigned char pkt_buf[ECP_SIZE_PKT_RAW_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP, parent)]; + unsigned char pld_buf[ECP_SIZE_PLD_RAW_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP, parent)]; + + unsigned char *buf = ecp_pld_get_buf(pld_buf, ECP_MTYPE_KGET_REP); + ecp_pld_set_type(pld_buf, ECP_MTYPE_KGET_REP); + + _packet.buffer = pkt_buf; + _packet.size = ECP_SIZE_PKT_RAW_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP, parent); + _payload.buffer = pld_buf; + _payload.size = ECP_SIZE_PLD_RAW_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_KGET_REP, parent); + rv = ecp_sock_dhkey_get_curr(sock, buf, buf+1); if (!rv) { - ssize_t _rv = ecp_pld_send_raw(sock, parent, addr, s_idx, c_idx, &public, &shsec, nonce, seq_p, payload_, sizeof(payload_)); + 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)); if (_rv < 0) rv = _rv; } - return ECP_MIN_PKT; + if (rv) return rv; } else { return ECP_ERR_CONN_NOT_FOUND; } - } else { - if (is_open) { + } + + 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; + 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; + + if (seq_check) { + if (ECP_SEQ_LTE(seq_p, seq_c)) { + ecp_seq_t seq_offset = seq_c - seq_p; + if (seq_offset < ECP_SIZE_ACKB) { + ecp_ack_t ack_mask = ((ecp_ack_t)1 << seq_offset); + if (ack_mask & seq_map) rv = ECP_ERR_SEQ; + if (!rv) seq_n = seq_c; + } else { + rv = ECP_ERR_SEQ; + } + } else { + ecp_seq_t seq_offset = seq_p - seq_c; + if (seq_offset < ECP_MAX_SEQ_FWD) { if (seq_offset < ECP_SIZE_ACKB) { - ecp_ack_t ack_mask = ((ecp_ack_t)1 << seq_offset); - if (ack_mask & seq_map) rv = ECP_ERR_SEQ; - if (!rv) seq_n = seq_c; + seq_map = seq_map << seq_offset; } else { - rv = ECP_ERR_SEQ; + seq_map = 0; } + seq_map |= 1; + seq_n = seq_p; } else { - ecp_seq_t seq_offset = seq_p - seq_c; - if (seq_offset < ECP_MAX_SEQ_FWD) { - if (seq_offset < ECP_SIZE_ACKB) { - seq_map = seq_map << seq_offset; - } else { - seq_map = 0; - } - seq_map |= 1; - seq_n = seq_p; - } else { - rv = ECP_ERR_SEQ; - } + rv = ECP_ERR_SEQ; } - - if (rv) goto pkt_handle_err; } - } else { - seq_n = seq_p; - seq_map = 1; - } - if (key) { -#ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&conn->mutex); -#endif - rv = conn_dhkey_new_pub_local(conn, l_idx); - if (!rv) rv = conn_shsec_set(conn, s_idx, c_idx, &shsec); -#ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&conn->mutex); -#endif - if (rv) goto pkt_handle_err; + if (rv) goto ecp_unpack_err; } - } - if (conn == NULL) return ECP_ERR; - - proc_size = 0; - cnt_size = pld_size-ECP_SIZE_PLD_HDR; - -#ifdef ECP_WITH_PTHREAD - pthread_mutex_lock(&conn->mutex); -#endif - memcpy(conn->nonce, nonce, sizeof(conn->nonce)); - if (addr) conn->node.addr = *addr; -#ifdef ECP_WITH_PTHREAD - pthread_mutex_unlock(&conn->mutex); -#endif + } else { + seq_n = seq_p; + seq_map = 1; #ifdef ECP_WITH_RBUF - if (conn->rbuf.recv) { - if (!is_open) { + if (conn->rbuf.recv) { rv = ecp_rbuf_recv_start(conn, seq_p); - if (rv) goto pkt_handle_err; + if (rv) goto ecp_unpack_err; } - proc_size = ecp_rbuf_recv_store(conn, seq_p, payload+pld_size-cnt_size, cnt_size); - } #endif - if (proc_size == 0) proc_size = ecp_msg_handle(conn, seq_p, payload+pld_size-cnt_size, cnt_size); - - if (proc_size < 0) rv = proc_size; - if (!rv) cnt_size -= proc_size; + } #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); #endif - if (!rv && seq_check) { + memcpy(conn->nonce, packet+ECP_SIZE_PKT_HDR, sizeof(conn->nonce)); + if (addr) conn->node.addr = *addr; + if (seq_check) { conn->seq_in = seq_n; conn->seq_in_map = seq_map; } - conn->refcount--; + if (key) { + if (is_new) rv = conn_dhkey_new_pub_remote(conn, c_idx, packet+ECP_SIZE_PROTO+1); + if (!rv) rv = conn_dhkey_new_pub_local(conn, l_idx); + if (!rv) rv = conn_shsec_set(conn, s_idx, c_idx, &shsec); + } #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); #endif - if (rv) return rv; - return ECP_SIZE_PKT_HDR+ECP_AEAD_SIZE_TAG+pld_size-cnt_size; - -pkt_handle_err: + if (rv) goto ecp_unpack_err; + + *_conn = conn; + *_seq = seq_p; + return dec_size; + +ecp_unpack_err: if (conn == NULL) return rv; #ifdef ECP_WITH_PTHREAD @@ -1258,6 +1317,35 @@ pkt_handle_err: return rv; } +ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size) { + 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); + + 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); +#endif + if (rv == 0) rv = ecp_msg_handle(conn, seq, bufs->payload->buffer+ECP_SIZE_PLD_HDR, pld_size-ECP_SIZE_PLD_HDR, bufs); + +#ifdef ECP_WITH_PTHREAD + pthread_mutex_lock(&conn->mutex); +#endif + conn->refcount--; +#ifdef ECP_WITH_PTHREAD + pthread_mutex_unlock(&conn->mutex); +#endif + } else { + rv = pld_size-ECP_SIZE_PLD_HDR; + } + + if (rv < 0) return rv; + 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); if (rv < 0) return rv; @@ -1375,7 +1463,7 @@ int ecp_msg_defrag(ECPFragIter *iter, ecp_seq_t seq, unsigned char *msg_in, size } } -ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size) { +ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs) { ecp_conn_handler_msg_t *handler = NULL; ssize_t rv = 0; unsigned char mtype = 0; @@ -1402,7 +1490,7 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, s 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); + rv = handler(conn, seq, mtype, content, rem_size, bufs); if (rv < 0) return rv; if (rv > rem_size) return ECP_ERR; @@ -1450,35 +1538,34 @@ 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, ECPTimerItem *ti, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size, ECPSeqItem *si) { - unsigned char packet[ECP_MAX_PKT]; +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) { ECPSocket *sock = conn->sock; ECPContext *ctx = sock->ctx; ECPNetAddr addr; - ssize_t rv = ctx->pack(conn, packet, ECP_MAX_PKT, s_idx, c_idx, payload, payload_size, si, &addr); + ssize_t rv = ctx->pack(conn, packet, s_idx, c_idx, payload, pld_size, si, &addr); if (rv < 0) return rv; #ifdef ECP_WITH_RBUF - if (conn->rbuf.send) return ecp_rbuf_pkt_send(conn->rbuf.send, conn->sock, &addr, ti, si, packet, rv); + if (conn->rbuf.send) return ecp_rbuf_pkt_send(conn->rbuf.send, conn->sock, &addr, packet->buffer, rv, ti, si); #endif if (ti) { int _rv = ecp_timer_push(ti); if (_rv) return _rv; } - return ecp_pkt_send(sock, &addr, packet, rv); + return ecp_pkt_send(sock, &addr, packet->buffer, rv); } -ssize_t ecp_pld_send(ECPConnection *conn, unsigned char *payload, size_t payload_size) { - return ecp_pld_send_ll(conn, NULL, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, payload_size); +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_wtimer(ECPConnection *conn, ECPTimerItem *ti, unsigned char *payload, size_t payload_size) { - return ecp_pld_send_ll(conn, ti, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, payload_size); +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_ll(ECPConnection *conn, ECPTimerItem *ti, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size) { +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) { ECPSeqItem *_seq_item = NULL; #ifdef ECP_WITH_RBUF @@ -1491,27 +1578,34 @@ ssize_t ecp_pld_send_ll(ECPConnection *conn, ECPTimerItem *ti, unsigned char s_i } #endif - return pld_send(conn, ti, s_idx, c_idx, payload, payload_size, _seq_item); + return pld_send(conn, packet, s_idx, c_idx, payload, pld_size, ti, _seq_item); } -ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *addr, 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 payload_size) { - unsigned char packet[ECP_MAX_PKT]; +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) { ECPContext *ctx = sock->ctx; ECPNetAddr _addr; ssize_t rv; - rv = ctx->pack_raw(sock, parent, packet, ECP_MAX_PKT, s_idx, c_idx, public, shsec, nonce, seq, payload, payload_size, &_addr); + 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, rv); + return ecp_pkt_send(sock, parent ? &_addr : addr, packet->buffer, rv); } ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *content, size_t content_size) { + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_MAX_PKT]; + unsigned char pld_buf[ECP_MAX_PLD]; ssize_t rv = 0; - unsigned char payload[ECP_MAX_PLD]; int pkt_cnt = 0; int vc_cnt = conn->pcount + 1; size_t size_max = ECP_MAX_PKT - (ECP_SIZE_PKT_HDR + ECP_AEAD_SIZE_TAG + ECP_SIZE_SEQ + 1) * vc_cnt; + + packet.buffer = pkt_buf; + 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; @@ -1543,21 +1637,21 @@ ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *conten 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; - ecp_pld_set_type(payload, mtype); - ecp_pld_set_frag(payload, mtype, i, pkt_cnt); - memcpy(ecp_pld_get_buf(payload, mtype), content, frag_size); + ecp_pld_set_type(pld_buf, mtype); + ecp_pld_set_frag(pld_buf, mtype, i, pkt_cnt); + memcpy(ecp_pld_get_buf(pld_buf, mtype), content, frag_size); content += frag_size; seq_item.seq = seq_start + i; - ssize_t _rv = pld_send(conn, NULL, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, ECP_SIZE_PLD(frag_size, mtype), &seq_item); + 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); if (_rv < 0) return _rv; rv += _rv; } } else { - ecp_pld_set_type(payload, mtype); - memcpy(ecp_pld_get_buf(payload, mtype), content, content_size); - rv = ecp_pld_send(conn, payload, ECP_SIZE_PLD(content_size, mtype)); + 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)); } return rv; } @@ -1579,12 +1673,24 @@ ssize_t ecp_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg static int recv_p(ECPSocket *sock) { ECPNetAddr addr; - unsigned char packet[ECP_MAX_PKT]; + ECP2Buffer bufs; + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_MAX_PKT]; + unsigned char pld_buf[ECP_MAX_PLD]; - ssize_t rv = ecp_pkt_recv(sock, &addr, packet, ECP_MAX_PKT); + bufs.packet = &packet; + bufs.payload = &payload; + + packet.buffer = pkt_buf; + packet.size = ECP_MAX_PKT; + payload.buffer = pld_buf; + payload.size = ECP_MAX_PLD; + + ssize_t rv = ecp_pkt_recv(sock, &addr, pkt_buf, ECP_MAX_PKT); if (rv < 0) return rv; - rv = ecp_pkt_handle(sock, &addr, NULL, packet, rv); + rv = ecp_pkt_handle(sock, &addr, NULL, &bufs, rv); if (rv < 0) return rv; return ECP_OK; diff --git a/code/core/core.h b/code/core/core.h index aa883ce..88a8263 100644 --- a/code/core/core.h +++ b/code/core/core.h @@ -86,8 +86,11 @@ #include <stddef.h> #include <stdint.h> -struct ECPConnection; +struct ECPBuffer; +struct ECP2Buffer; +struct ECPContext; struct ECPSocket; +struct ECPConnection; struct ECPSeqItem; struct ECPFragIter; @@ -119,6 +122,14 @@ typedef uint32_t ecp_seq_t; #define ECP_SIZE_PLD(X,F) ((X) + ECP_SIZE_PLD_HDR+1+ECP_SIZE_MT_FLAG(F)) #define ECP_SIZE_PKT(X,F) (ECP_SIZE_PKT_HDR+ECP_SIZE_PLD(X,F)+ECP_AEAD_SIZE_TAG) +#define ECP_SIZE_MSG_BUF(T,P) ((P) && (P)->out && (((T) == ECP_MTYPE_OPEN_REQ) || ((T) == ECP_MTYPE_KGET_REQ)) ? ECP_SIZE_PLD_HDR+3+2*ECP_ECDH_SIZE_KEY : ECP_SIZE_PLD_HDR+1) + +#define ECP_SIZE_PLD_BUF(X,T,C) (ECP_SIZE_PLD(X,T)+(C)->pcount*(ECP_SIZE_PKT_HDR+ECP_SIZE_MSG_BUF(T,(C)->parent)+ECP_AEAD_SIZE_TAG)) +#define ECP_SIZE_PKT_BUF(X,T,C) (ECP_SIZE_PLD_BUF(X,T,C)+ECP_SIZE_PKT_HDR+ECP_AEAD_SIZE_TAG) + +#define ECP_SIZE_PLD_RAW_BUF(X,T,P) (ECP_SIZE_PLD(X,T)+((P) ? ((P)->pcount+1)*(ECP_SIZE_PKT_HDR+ECP_SIZE_MSG_BUF(T,P)+ECP_AEAD_SIZE_TAG) : 0)) +#define ECP_SIZE_PKT_RAW_BUF(X,T,P) (ECP_SIZE_PLD_RAW_BUF(X,T,P)+ECP_SIZE_PKT_HDR+ECP_AEAD_SIZE_TAG) + #ifdef ECP_WITH_PTHREAD #include <pthread.h> #endif @@ -138,14 +149,10 @@ typedef uint32_t ecp_seq_t; #define DPRINT(cnd, format, ...) {} #endif -struct ECPContext; -struct ECPSocket; -struct ECPConnection; - typedef int ecp_rng_t (void *, size_t); -typedef int ecp_conn_handler_new_t (struct ECPSocket *s, struct ECPConnection **c, struct ECPConnection *p, unsigned char s_idx, unsigned char c_idx, unsigned char *pub, ecp_aead_key_t *sh, unsigned char *msg, size_t sz); -typedef ssize_t ecp_conn_handler_msg_t (struct ECPConnection *c, ecp_seq_t s, unsigned char t, unsigned char *msg, ssize_t sz); +typedef int ecp_conn_handler_new_t (struct ECPSocket *s, struct ECPConnection *p, unsigned char *msg, size_t sz, struct ECPConnection **c); +typedef ssize_t ecp_conn_handler_msg_t (struct ECPConnection *c, ecp_seq_t s, unsigned char t, unsigned char *msg, ssize_t sz, struct ECP2Buffer *b); typedef struct ECPConnection * ecp_conn_alloc_t (unsigned char t); typedef void ecp_conn_free_t (struct ECPConnection *c); @@ -200,6 +207,16 @@ typedef struct ECPHTableIface { } ECPHTableIface; #endif +typedef struct ECPBuffer { + unsigned char *buffer; + size_t size; +} ECPBuffer; + +typedef struct ECP2Buffer { + ECPBuffer *packet; + ECPBuffer *payload; +} ECP2Buffer; + typedef struct ECPDHKey { ecp_dh_public_t public; ecp_dh_private_t private; @@ -274,8 +291,8 @@ typedef struct ECPContext { #ifdef ECP_WITH_HTABLE ECPHTableIface ht; #endif - ssize_t (*pack) (struct ECPConnection *conn, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size, ECPSeqItem *si, ECPNetAddr *addr); - ssize_t (*pack_raw) (struct ECPSocket *sock, struct 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 payload_size, ECPNetAddr *addr); + ssize_t (*pack) (struct ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, ECPSeqItem *si, ECPNetAddr *addr); + ssize_t (*pack_raw) (struct ECPSocket *sock, struct 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); ECPConnHandler *handler[ECP_MAX_CTYPE]; } ECPContext; @@ -357,21 +374,26 @@ int ecp_conn_close(ECPConnection *conn, ecp_cts_t timeout); int ecp_conn_handler_init(ECPConnHandler *handler); ssize_t ecp_conn_send_open(ECPConnection *conn); -int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, unsigned char *c_public, ecp_aead_key_t *shsec, unsigned char *payload, size_t payload_size); -ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size); -ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size); -ssize_t ecp_conn_handle_kput(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size); -ssize_t ecp_conn_handle_exec(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size); +int ecp_conn_handle_new(ECPSocket *sock, ECPConnection *parent, unsigned char *payload, size_t payload_size, ECPConnection **_conn); +ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b); +ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b); +ssize_t ecp_conn_handle_kput(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b); +ssize_t ecp_conn_handle_exec(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b); int ecp_conn_dhkey_new(ECPConnection *conn); int ecp_conn_dhkey_new_pub(ECPConnection *conn, unsigned char idx, unsigned char *public); int ecp_conn_dhkey_get_curr(ECPConnection *conn, unsigned char *idx, unsigned char *public); -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 payload_size); -ssize_t ecp_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 payload_size, ECPNetAddr *addr); -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 payload_size, ECPSeqItem *si, ECPNetAddr *addr); +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 ecp_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); +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); + +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_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size); -ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, unsigned char *packet, size_t pkt_size); ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size); ssize_t ecp_pkt_recv(ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size); @@ -383,7 +405,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); int ecp_msg_get_pts(unsigned char *msg, size_t msg_size, ecp_pts_t *pts); int ecp_msg_defrag(ECPFragIter *iter, ecp_seq_t seq, unsigned char *msg_in, size_t msg_in_size, unsigned char **msg_out, size_t *msg_out_size); -ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size); +ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs); void ecp_pld_set_type(unsigned char *payload, unsigned char mtype); int ecp_pld_set_frag(unsigned char *payload, unsigned char mtype, unsigned char frag_cnt, unsigned char frag_tot); @@ -391,10 +413,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, unsigned char *payload, size_t payload_size); -ssize_t ecp_pld_send_wtimer(ECPConnection *conn, ECPTimerItem *ti, unsigned char *payload, size_t payload_size); -ssize_t ecp_pld_send_ll(ECPConnection *conn, ECPTimerItem *ti, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size); -ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *addr, 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 payload_size); +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_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/core/rbuf.c b/code/core/rbuf.c index 67c9f8d..b5f66a9 100644 --- a/code/core/rbuf.c +++ b/code/core/rbuf.c @@ -44,8 +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, unsigned char *payload, size_t payload_size, ecp_seq_t seq) { - unsigned char packet[ECP_MAX_PKT]; +ssize_t ecp_rbuf_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, ecp_seq_t seq) { ECPSocket *sock = conn->sock; ECPContext *ctx = sock->ctx; ECPNetAddr addr; @@ -59,10 +58,10 @@ ssize_t ecp_rbuf_pld_send(ECPConnection *conn, unsigned char *payload, size_t pa seq_item.seq_w = 1; seq_item.rb_pass = 1; - rv = ctx->pack(conn, packet, ECP_MAX_PKT, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, payload_size, &seq_item, &addr); + 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, rv); + rv = ecp_pkt_send(sock, &addr, packet->buffer, rv); if (rv < 0) return rv; return rv; diff --git a/code/core/rbuf.h b/code/core/rbuf.h index 3941125..145234f 100644 --- a/code/core/rbuf.h +++ b/code/core/rbuf.h @@ -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, unsigned char *payload, size_t payload_size, ecp_seq_t seq); +ssize_t ecp_rbuf_pld_send(struct ECPConnection *conn, struct ECPBuffer *packet, struct ECPBuffer *payload, size_t pld_size, 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); @@ -109,9 +109,9 @@ ssize_t ecp_rbuf_recv_store(struct ECPConnection *conn, ecp_seq_t seq, unsigned 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 *payload); -ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, struct ECPSocket *sock, ECPNetAddr *addr, ECPTimerItem *ti, struct ECPSeqItem *si, unsigned char *packet, size_t pkt_size); +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_handle_ack(struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size); -ssize_t ecp_rbuf_handle_flush(struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size); -ssize_t ecp_rbuf_handle_flush_pts(struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size); +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); +ssize_t ecp_rbuf_handle_flush_pts(struct ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, struct ECP2Buffer *b); diff --git a/code/core/rbuf_recv.c b/code/core/rbuf_recv.c index b3b48dc..44b5113 100644 --- a/code/core/rbuf_recv.c +++ b/code/core/rbuf_recv.c @@ -29,7 +29,7 @@ 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); + if (flags & ECP_RBUF_FLAG_SYS) ecp_msg_handle(conn, seq, msg, msg_size, NULL); return rv; } @@ -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); + ecp_msg_handle(conn, seq, buf->rbuf.msg[idx].msg, buf->rbuf.msg[idx].size, NULL); } } buf->rbuf.msg[idx].flags &= ~ECP_RBUF_FLAG_IN_RBUF; @@ -126,11 +126,19 @@ static void msg_flush(ECPConnection *conn) { static int ack_send(ECPConnection *conn) { ECPRBRecv *buf = conn->rbuf.recv; - unsigned char payload[ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), 0)]; - unsigned char *buf_ = ecp_pld_get_buf(payload, 0); + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK, conn)]; + unsigned char *buf_ = ecp_pld_get_buf(pld_buf, ECP_MTYPE_RBACK); ssize_t rv; + + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK, conn); - ecp_pld_set_type(payload, ECP_MTYPE_RBACK); + ecp_pld_set_type(pld_buf, ECP_MTYPE_RBACK); buf_[0] = (buf->seq_ack & 0xFF000000) >> 24; buf_[1] = (buf->seq_ack & 0x00FF0000) >> 16; buf_[2] = (buf->seq_ack & 0x0000FF00) >> 8; @@ -140,7 +148,7 @@ 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, payload, sizeof(payload), 0); + rv = ecp_rbuf_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(sizeof(ecp_seq_t) + sizeof(ecp_ack_t), ECP_MTYPE_RBACK), 0); if (rv < 0) return rv; buf->ack_pkt = 0; @@ -186,7 +194,7 @@ static int ack_shift(ECPRBRecv *buf) { return do_ack; } -ssize_t ecp_rbuf_handle_flush(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +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; ECPRBRecv *buf = conn->rbuf.recv; @@ -196,9 +204,8 @@ ssize_t ecp_rbuf_handle_flush(ECPConnection *conn, ecp_seq_t seq, unsigned char return 0; } -ssize_t ecp_rbuf_handle_flush_pts(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +ssize_t ecp_rbuf_handle_flush_pts(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { ECPRBRecv *buf = conn->rbuf.recv; - if (buf == NULL) return ECP_ERR; buf->timer_pts = 0; @@ -283,7 +290,7 @@ ssize_t ecp_rbuf_recv_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *m 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); + if ((mtype == ECP_MTYPE_RBACK) || (mtype == ECP_MTYPE_RBFLUSH)) return ecp_msg_handle(conn, seq, msg, msg_size, NULL); if (ECP_SEQ_LT(buf->rbuf.seq_max, seq)) ack_pkt = seq - buf->rbuf.seq_max; if (ECP_SEQ_LTE(seq, buf->seq_ack)) { @@ -307,7 +314,7 @@ ssize_t ecp_rbuf_recv_store(ECPConnection *conn, ecp_seq_t seq, unsigned char *m rv = msg_store(conn, seq, msg, msg_size); if (rv < 0) return rv; } else { - ecp_msg_handle(conn, seq, msg, msg_size); + ecp_msg_handle(conn, seq, msg, msg_size, NULL); rv = msg_size; buf->rbuf.seq_max++; buf->rbuf.seq_start++; diff --git a/code/core/rbuf_send.c b/code/core/rbuf_send.c index c78fecf..f913e7e 100644 --- a/code/core/rbuf_send.c +++ b/code/core/rbuf_send.c @@ -5,9 +5,17 @@ #define NACK_RATE_UNIT 10000 static ssize_t flush_send(ECPConnection *conn, ECPTimerItem *ti) { - unsigned char payload[ECP_SIZE_PLD(0, 0)]; + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_RBFLUSH, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_RBFLUSH, conn)]; + + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_RBFLUSH, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_RBFLUSH, conn); - ecp_pld_set_type(payload, ECP_MTYPE_RBFLUSH); + ecp_pld_set_type(pld_buf, ECP_MTYPE_RBFLUSH); if (ti == NULL) { ECPTimerItem _ti; int rv = ecp_timer_item_init(&_ti, conn, ECP_MTYPE_RBACK, 3, 500); @@ -17,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, payload, sizeof(payload), 0); + return ecp_rbuf_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_RBFLUSH), 0); } static void cc_flush(ECPConnection *conn) { @@ -72,7 +80,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) { +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; ssize_t rsize = sizeof(ecp_seq_t)+sizeof(ecp_ack_t); ecp_seq_t seq_ack = 0; @@ -127,9 +135,18 @@ ssize_t ecp_rbuf_handle_ack(ECPConnection *conn, ecp_seq_t seq, unsigned char mt if (buf->flags & ECP_RBUF_FLAG_RELIABLE) { unsigned int _idx = ECP_RBUF_IDX_MASK(idx + 1 - ECP_SIZE_ACKB + i, rbuf->msg_size); if ((rbuf->msg[_idx].size == 0) || (rbuf->msg[_idx].flags & ECP_RBUF_FLAG_SYS)) { - unsigned char payload[ECP_SIZE_PLD(0, 0)]; - ecp_pld_set_type(payload, ECP_MTYPE_NOP); - ecp_rbuf_pld_send(conn, payload, sizeof(payload), seq_ack + i); + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_NOP, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_NOP, conn)]; + + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_NOP, conn); + payload.buffer = pld_buf; + 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); } else { ecp_pkt_send(conn->sock, &conn->node.addr, rbuf->msg[_idx].msg, rbuf->msg[_idx].size); } @@ -237,7 +254,6 @@ int ecp_rbuf_send_start(ECPConnection *conn) { int ecp_rbuf_flush(ECPConnection *conn) { ECPRBSend *buf = conn->rbuf.send; - unsigned char payload[ECP_SIZE_PLD(0, 0)]; ecp_seq_t seq; if (buf == NULL) return ECP_ERR; @@ -271,7 +287,7 @@ int ecp_rbuf_flush(ECPConnection *conn) { return ECP_OK; } -int ecp_rbuf_pkt_prep(ECPRBSend *buf, ECPSeqItem *si, unsigned char *payload) { +int ecp_rbuf_pkt_prep(ECPRBSend *buf, ECPSeqItem *si, unsigned char mtype) { if (si->rb_pass) return ECP_OK; #ifdef ECP_WITH_PTHREAD @@ -284,7 +300,7 @@ int ecp_rbuf_pkt_prep(ECPRBSend *buf, ECPSeqItem *si, unsigned char *payload) { if (idx < 0) return idx; - si->rb_mtype = ecp_pld_get_type(payload); + si->rb_mtype = mtype; si->rb_idx = idx; buf->rbuf.msg[idx].size = 0; buf->rbuf.msg[idx].flags = 0; @@ -292,7 +308,7 @@ int ecp_rbuf_pkt_prep(ECPRBSend *buf, ECPSeqItem *si, unsigned char *payload) { return ECP_OK; } -ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, ECPSocket *sock, ECPNetAddr *addr, ECPTimerItem *ti, ECPSeqItem *si, unsigned char *packet, size_t pkt_size) { +ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size, ECPTimerItem *ti, ECPSeqItem *si) { int do_send = 1; ssize_t rv = 0; diff --git a/code/core/timer.c b/code/core/timer.c index 9d2569e..691f75c 100644 --- a/code/core/timer.c +++ b/code/core/timer.c @@ -32,8 +32,6 @@ int ecp_timer_item_init(ECPTimerItem *ti, ECPConnection *conn, unsigned char mty ti->timeout = timeout; ti->abstime = 0; ti->retry = NULL; - ti->pld = NULL; - ti->pld_size = 0; return ECP_OK; } @@ -186,8 +184,6 @@ ecp_cts_t ecp_timer_exe(ECPSocket *sock) { int rv = ECP_OK; ECPConnection *conn = to_exec[i].conn; unsigned char mtype = to_exec[i].mtype; - unsigned char *pld = to_exec[i].pld; - unsigned char pld_size = to_exec[i].pld_size; ecp_timer_retry_t *retry = to_exec[i].retry; ecp_conn_handler_msg_t *handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->msg[mtype & ECP_MTYPE_MASK] : NULL; @@ -197,13 +193,10 @@ ecp_cts_t ecp_timer_exe(ECPSocket *sock) { if (retry) { _rv = retry(conn, to_exec+i); if (_rv < 0) rv = _rv; - } else { - _rv = ecp_pld_send_wtimer(conn, to_exec+i, pld, pld_size); - if (_rv < 0) rv = _rv; } - if (rv && (rv != ECP_ERR_CLOSED) && handler) handler(conn, 0, mtype, NULL, rv); + if (rv && (rv != ECP_ERR_CLOSED) && handler) handler(conn, 0, mtype, NULL, rv, NULL); } else if (handler) { - handler(conn, 0, mtype, NULL, ECP_ERR_TIMEOUT); + handler(conn, 0, mtype, NULL, ECP_ERR_TIMEOUT, NULL); } #ifdef ECP_WITH_PTHREAD diff --git a/code/core/timer.h b/code/core/timer.h index 5919a42..8863226 100644 --- a/code/core/timer.h +++ b/code/core/timer.h @@ -19,8 +19,6 @@ typedef struct ECPTimerItem { ecp_cts_t abstime; ecp_cts_t timeout; ecp_timer_retry_t *retry; - unsigned char *pld; - size_t pld_size; } ECPTimerItem; typedef struct ECPTimer { diff --git a/code/test/basic.c b/code/test/basic.c index 5627fd3..85c4f31 100644 --- a/code/test/basic.c +++ b/code/test/basic.c @@ -20,40 +20,37 @@ ECPConnection conn; #define CTYPE_TEST 0 #define MTYPE_MSG 8 -ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +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); + ecp_conn_handle_open(conn, sq, t, p, s, b); if (s < 0) { printf("OPEN ERR:%ld\n", s); return s; } - unsigned char payload[ECP_SIZE_PLD(1000, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); char *msg = "PERA JE CAR!"; + unsigned char buf[1000]; - ecp_pld_set_type(payload, MTYPE_MSG); strcpy((char *)buf, msg); - ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); + return s; } -ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +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); return s; } -ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { printf("MSG S:%s size:%ld\n", p, s); - unsigned char payload[ECP_SIZE_PLD(1000, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); char *msg = "VAISTINU JE CAR!"; + unsigned char buf[1000]; - ecp_pld_set_type(payload, MTYPE_MSG); strcpy((char *)buf, msg); - ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); return s; } diff --git a/code/test/client.c b/code/test/client.c index edde70b..03b459f 100644 --- a/code/test/client.c +++ b/code/test/client.c @@ -16,26 +16,25 @@ ECPConnection conn; #define CTYPE_TEST 0 #define MTYPE_MSG 8 -ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +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); + ecp_conn_handle_open(conn, sq, t, p, s, b); if (s < 0) { printf("OPEN ERR:%ld\n", s); return s; } - unsigned char payload[ECP_SIZE_PLD(1000, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); char *msg = "PERA JE CAR!"; + unsigned char buf[1000]; - ecp_pld_set_type(payload, MTYPE_MSG); strcpy((char *)buf, msg); - ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); + return s; } -ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +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); return s; } diff --git a/code/test/echo.c b/code/test/echo.c index aa27529..ff9c01c 100644 --- a/code/test/echo.c +++ b/code/test/echo.c @@ -14,8 +14,11 @@ ECPConnHandler handler_s; #define CTYPE_TEST 0 #define MTYPE_MSG 8 -ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { - ssize_t rv = ecp_pld_send(conn, p-ECP_SIZE_PLD_HDR-1, ECP_SIZE_PLD_HDR+1+s); +ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + unsigned char buf[ECP_MAX_PLD]; + + memcpy(buf, p, s); + ssize_t _rv = ecp_send(conn, t, buf, s); return s; } diff --git a/code/test/frag.c b/code/test/frag.c index cbc8cdf..6e7f6be 100644 --- a/code/test/frag.c +++ b/code/test/frag.c @@ -25,10 +25,10 @@ unsigned char frag_buffer[8192]; #define CTYPE_TEST 0 #define MTYPE_MSG 8 -ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +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); + ecp_conn_handle_open(conn, sq, t, p, s, b); if (s < 0) { printf("OPEN ERR:%ld\n", s); return s; @@ -42,7 +42,7 @@ ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsign return s; } -ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +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; @@ -50,7 +50,7 @@ ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigne return s; } -ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { printf("MSG S:%s size:%ld\n", p, s); unsigned char content[5000]; diff --git a/code/test/server.c b/code/test/server.c index 1b2e2b1..e2001eb 100644 --- a/code/test/server.c +++ b/code/test/server.c @@ -14,16 +14,14 @@ ECPConnHandler handler_s; #define CTYPE_TEST 0 #define MTYPE_MSG 8 -ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { printf("MSG S:%s size:%ld\n", p, s); - unsigned char payload[ECP_SIZE_PLD(1000, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); char *msg = "VAISTINU JE CAR!"; + unsigned char buf[1000]; - ecp_pld_set_type(payload, MTYPE_MSG); strcpy((char *)buf, msg); - ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); return s; } diff --git a/code/test/stress.c b/code/test/stress.c index 0e46f84..d6bc74a 100644 --- a/code/test/stress.c +++ b/code/test/stress.c @@ -77,7 +77,15 @@ static void catchINFO(int sig) { void *sender(ECPConnection *c) { int idx = (int)(c->conn_data); - unsigned char payload[ECP_SIZE_PLD(1000, 0)]; + ECPBuffer packet; + 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) { @@ -85,8 +93,8 @@ void *sender(ECPConnection *c) { c->sock->ctx->rng(&rnd, sizeof(uint32_t)); usleep(rnd % (2000000/msg_rate)); - ecp_pld_set_type(payload, MTYPE_MSG); - ssize_t _rv = ecp_pld_send(c, payload, sizeof(payload)); + ecp_pld_set_type(pld_buf, MTYPE_MSG); + ssize_t _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0)); if (c_start && (_rv > 0)) { pthread_mutex_lock(&t_mtx[idx]); t_sent[idx]++; @@ -95,11 +103,11 @@ void *sender(ECPConnection *c) { } } -ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +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); + ecp_conn_handle_open(conn, sq, t, p, s, b); rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)conn); if (rv) { char msg[256]; @@ -110,9 +118,17 @@ ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsign return s; } -ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { int idx = (int)(conn->conn_data); - unsigned char payload[ECP_SIZE_PLD(1000, 0)]; + ECPBuffer packet; + 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; if (c_start) { pthread_mutex_lock(&t_mtx[idx]); @@ -121,14 +137,23 @@ ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigne } // ecp_pld_set_type(payload, MTYPE_MSG); - // ssize_t _rv = ecp_pld_send(c, payload, sizeof(payload)); + // ssize_t _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0)); return s; } -ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { - unsigned char payload[ECP_SIZE_PLD(1000, 0)]; - ecp_pld_set_type(payload, MTYPE_MSG); - ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); +ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { + ECPBuffer packet; + 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; + + ecp_pld_set_type(pld_buf, MTYPE_MSG); + ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(1000, 0)); return s; } diff --git a/code/test/vc_client.c b/code/test/vc_client.c index eb23d05..9ea3d1b 100644 --- a/code/test/vc_client.c +++ b/code/test/vc_client.c @@ -20,10 +20,10 @@ ECPNode vconn_node[20]; #define CTYPE_TEST 0 #define MTYPE_MSG 8 -ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_open(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); + ecp_conn_handle_open(conn, sq, t, p, s, b); if (s < 0) { printf("OPEN ERR:%ld\n", s); return s; @@ -31,17 +31,16 @@ ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned printf("OPEN!\n"); - unsigned char payload[ECP_SIZE_PLD(1000, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); char *msg = "PERA JE CAR!"; + unsigned char buf[1156]; - ecp_pld_set_type(payload, MTYPE_MSG); strcpy((char *)buf, msg); - ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1156); + return s; } -ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { printf("MSG S:%s size:%ld\n", p, s); return s; } diff --git a/code/test/vc_server.c b/code/test/vc_server.c index 455f1a0..8525ad4 100644 --- a/code/test/vc_server.c +++ b/code/test/vc_server.c @@ -18,21 +18,19 @@ ECPConnection conn; #define CTYPE_TEST 0 #define MTYPE_MSG 8 -ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { printf("OPEN RECEIVED\n"); - return ecp_conn_handle_open(conn, sq, t, p, s); + return ecp_conn_handle_open(conn, sq, t, p, s, b); } -ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { printf("MSG S:%s size:%ld\n", p, s); - unsigned char payload[ECP_SIZE_PLD(1000, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); char *msg = "VAISTINU JE CAR!"; + unsigned char buf[1000]; - ecp_pld_set_type(payload, MTYPE_MSG); strcpy((char *)buf, msg); - ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); + ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); return s; } diff --git a/code/test/vid/Makefile b/code/test/vid/Makefile index 9ef247e..04d5b5c 100644 --- a/code/test/vid/Makefile +++ b/code/test/vid/Makefile @@ -1,5 +1,5 @@ -LIBVPX_HOME=/home/majstor/libvpx -# LIBVPX_HOME=/Users/majstor/src/libvpx +# LIBVPX_HOME=/home/majstor/libvpx +LIBVPX_HOME=/Users/majstor/src/libvpx CFLAGS=-D_V4L2_KERNEL_ -I/usr/src/linux-headers-$(uname -r) -I$(LIBVPX_HOME) -I/opt/local/include/SDL2 -I../../core -I../../vconn -I../../util LDFLAGS=-L$(LIBVPX_HOME) -L/opt/local/lib dep=../../core/libecpcore.a ../../core/crypto/libecpcr.a ../../core/htable/libecpht.a ../../core/posix/libecptr.a ../../core/posix/libecptm.a ../../vconn/libecpvconn.a ../../util/libecputil.a ../init.o diff --git a/code/test/vid/client.c b/code/test/vid/client.c index 2c32c51..98e95b2 100644 --- a/code/test/vid/client.c +++ b/code/test/vid/client.c @@ -37,7 +37,11 @@ ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned vpx_codec_iter_t iter = NULL; vpx_image_t *img = NULL; - if (vpx_codec_decode(&codec, f, (unsigned int)sz, NULL, 0)) die_codec(&codec, "Failed to decode frame."); + if (vpx_codec_decode(&codec, f, (unsigned int)sz, NULL, 0)) { + fprintf(stderr, "\n%lu\n", sz); + fprintf(stderr, "ERROR!\n"); + // die_codec(&codec, "Failed to decode frame."); + } while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) { if (!vpx_img_write(img, sdl_canvas.yuvBuffer, sdl_canvas.yPlaneSz + 2 * sdl_canvas.uvPlaneSz)) die_codec(NULL, "Failed to write image."); diff --git a/code/test/voip.c b/code/test/voip.c index 4c3d30b..ad5032b 100644 --- a/code/test/voip.c +++ b/code/test/voip.c @@ -125,10 +125,10 @@ int a_init(void) { return ECP_OK; } -ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +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, t, p, s); + ecp_conn_handle_open(conn, sq, t, p, s, b); if (s < 0) { printf("OPEN ERR:%ld\n", s); return s; @@ -139,7 +139,7 @@ ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsign return s; } -ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) { +ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { a_write(opus_dec, p, s, handle_plb, alsa_out_buf, alsa_frames); return s; } diff --git a/code/vconn/vconn.c b/code/vconn/vconn.c index 740f400..dadff0f 100644 --- a/code/vconn/vconn.c +++ b/code/vconn/vconn.c @@ -70,10 +70,18 @@ static void vconn_destroy(ECPConnection *conn) { } static ssize_t _vconn_send_open(ECPConnection *conn, ECPTimerItem *ti) { - unsigned char payload[ECP_SIZE_PLD(0, 0)]; + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn)]; + + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn); - ecp_pld_set_type(payload, ECP_MTYPE_KGET_REQ); - return ecp_pld_send_ll(conn, ti, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, payload, sizeof(payload)); + 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); } static ssize_t vconn_open(ECPConnection *conn) { @@ -90,7 +98,7 @@ static ssize_t vconn_open(ECPConnection *conn) { return _rv; } -static ssize_t vconn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +static ssize_t vconn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { if (conn->type != ECP_CTYPE_VCONN) return ECP_ERR; if (mtype & ECP_MTYPE_FLAG_REP) { @@ -102,10 +110,10 @@ static ssize_t vconn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch conn = conn_v->next; } handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->msg[ECP_MTYPE_OPEN] : NULL; - return handler ? handler(conn, seq, mtype, msg, size) : size; + return handler ? handler(conn, seq, mtype, msg, size, b) : size; } - return ecp_conn_handle_open(conn, seq, mtype, msg, size); + return ecp_conn_handle_open(conn, seq, mtype, msg, size, b); } else { ECPContext *ctx = conn->sock->ctx; ECPVConnIn *conn_v = (ECPVConnIn *)conn; @@ -151,16 +159,16 @@ static ssize_t vconn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch return ECP_ERR; } -static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { ECPContext *ctx = conn->sock->ctx; ECPConnection *conn_out = NULL; ECPVConnIn *conn_v = (ECPVConnIn *)conn; - unsigned char *payload = NULL; ssize_t rv; if (conn->out) return ECP_ERR; if (conn->type != ECP_CTYPE_VCONN) return ECP_ERR; - + if (b == NULL) return ECP_ERR; + if (size < 0) return size; if (size < ECP_MIN_PKT) return ECP_ERR_MIN_PKT; @@ -185,9 +193,12 @@ static ssize_t vconn_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c if (conn_out == NULL) return ECP_ERR; - payload = msg - ECP_SIZE_PLD_HDR - 1; - ecp_pld_set_type(payload, ECP_MTYPE_EXEC); - rv = ecp_pld_send(conn_out, payload, size + msg - payload); + ECPBuffer payload; + payload.buffer = msg - ECP_SIZE_PLD_HDR - 1; + 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); #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn_out->mutex); @@ -257,16 +268,25 @@ static void vlink_destroy(ECPConnection *conn) { static ssize_t _vlink_send_open(ECPConnection *conn, ECPTimerItem *ti) { ECPSocket *sock = conn->sock; ECPContext *ctx = sock->ctx; - unsigned char payload[ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, 0)]; - unsigned char *buf = ecp_pld_get_buf(payload, 0); + + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ, conn)]; + unsigned char *buf = ecp_pld_get_buf(pld_buf, ECP_MTYPE_OPEN_REQ); int rv = ECP_OK; + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ, conn); + // XXX server should verify perma_key - ecp_pld_set_type(payload, ECP_MTYPE_OPEN_REQ); + ecp_pld_set_type(pld_buf, ECP_MTYPE_OPEN_REQ); 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, ti, payload, sizeof(payload)); + return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1, ECP_MTYPE_OPEN_REQ), ti); } static ssize_t vlink_open(ECPConnection *conn) { @@ -277,7 +297,7 @@ static void vlink_close(ECPConnection *conn) { vlink_remove(conn); } -static ssize_t vlink_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +static ssize_t vlink_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { ssize_t rv; int is_open; @@ -293,7 +313,7 @@ static ssize_t vlink_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch pthread_mutex_unlock(&conn->mutex); #endif - rv = ecp_conn_handle_open(conn, seq, mtype, msg, size); + rv = ecp_conn_handle_open(conn, seq, mtype, msg, size, b); if (rv < 0) return rv; if (mtype & ECP_MTYPE_FLAG_REP) { @@ -316,12 +336,12 @@ static ssize_t vlink_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned ch return ECP_ERR; } -static ssize_t vlink_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { +static ssize_t vlink_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { ECPContext *ctx = conn->sock->ctx; - unsigned char *payload = NULL; ssize_t rv; if (conn->type != ECP_CTYPE_VLINK) return ECP_ERR; + if (b == NULL) return ECP_ERR; if (size < 0) return size; if (size < ECP_MIN_PKT) return ECP_ERR_MIN_PKT; @@ -347,9 +367,12 @@ static ssize_t vlink_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c if (conn == NULL) return ECP_ERR; - payload = msg - ECP_SIZE_PLD_HDR - 1; - ecp_pld_set_type(payload, ECP_MTYPE_EXEC); - rv = ecp_pld_send(conn, payload, size + msg - payload); + ECPBuffer payload; + payload.buffer = msg - ECP_SIZE_PLD_HDR - 1; + 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); #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); @@ -363,71 +386,127 @@ static ssize_t vlink_handle_relay(ECPConnection *conn, ecp_seq_t seq, unsigned c return size; } -static ssize_t vconn_set_msg(ECPConnection *conn, unsigned char *pld_out, size_t pld_out_size, unsigned char *pld_in, size_t pld_in_size) { - if ((conn->type == ECP_CTYPE_VCONN) && conn->out) { - unsigned char mtype = ecp_pld_get_type(pld_in); - if ((mtype == ECP_MTYPE_OPEN_REQ) || (mtype == ECP_MTYPE_KGET_REQ)) { - ECPVConnection *conn_v = (ECPVConnection *)conn; - ECPContext *ctx = conn->sock->ctx; - ECPConnection *conn_next = conn_v->next; - unsigned char *buf = NULL; - int rv; +static ssize_t vconn_set_msg(ECPConnection *conn, ECPBuffer *payload, unsigned char mtype) { + if (conn->out && (conn->type == ECP_CTYPE_VCONN) && ((mtype == ECP_MTYPE_OPEN_REQ) || (mtype == ECP_MTYPE_KGET_REQ))) { + ECPVConnection *conn_v = (ECPVConnection *)conn; + ECPContext *ctx = conn->sock->ctx; + ECPConnection *conn_next = conn_v->next; + unsigned char *buf = NULL; + int rv; - if (pld_out_size < ECP_SIZE_PLD_HDR + 3 + 2 * ECP_ECDH_SIZE_KEY) return ECP_ERR; - if (conn_next == NULL) return ECP_ERR; + if (payload->size < ECP_SIZE_PLD_HDR + 3 + 2 * ECP_ECDH_SIZE_KEY) return ECP_ERR; + if (conn_next == NULL) return ECP_ERR; - ecp_pld_set_type(pld_out, ECP_MTYPE_OPEN_REQ); - buf = ecp_pld_get_buf(pld_out, 0); + ecp_pld_set_type(payload->buffer, ECP_MTYPE_OPEN_REQ); + buf = ecp_pld_get_buf(payload->buffer, 0); - buf[0] = ECP_CTYPE_VCONN; - rv = ecp_conn_dhkey_get_curr(conn_next, NULL, buf+1); - if (rv) return rv; + buf[0] = ECP_CTYPE_VCONN; + rv = ecp_conn_dhkey_get_curr(conn_next, NULL, buf+1); + if (rv) return rv; - memcpy(buf+1+ECP_ECDH_SIZE_KEY, ctx->cr.dh_pub_get_buf(&conn_next->node.public), ECP_ECDH_SIZE_KEY); - buf[1+2*ECP_ECDH_SIZE_KEY] = ECP_MTYPE_RELAY; + memcpy(buf+1+ECP_ECDH_SIZE_KEY, ctx->cr.dh_pub_get_buf(&conn_next->node.public), ECP_ECDH_SIZE_KEY); + buf[1+2*ECP_ECDH_SIZE_KEY] = ECP_MTYPE_RELAY; - return ECP_SIZE_PLD_HDR + 3 + 2 * ECP_ECDH_SIZE_KEY; - } + return ECP_SIZE_PLD_HDR + 3 + 2 * ECP_ECDH_SIZE_KEY; + } else { + if (payload->size < ECP_SIZE_PLD_HDR + 1) return ECP_ERR; + + ecp_pld_set_type(payload->buffer, ECP_MTYPE_RELAY); + + return ECP_SIZE_PLD_HDR + 1; } - ecp_pld_set_type(pld_out, ECP_MTYPE_RELAY); - return ECP_SIZE_PLD_HDR + 1; } +/* Memory limited version: */ +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 (conn->parent) { + unsigned char mtype = ecp_pld_get_type(payload->buffer); + ssize_t rv, hdr_size = vconn_set_msg(conn->parent, packet, mtype); + if (hdr_size < 0) return hdr_size; + + rv = ecp_conn_pack(conn, packet->buffer+hdr_size, packet->size-hdr_size, s_idx, c_idx, payload->buffer, pld_size, si, NULL); + if (rv < 0) return rv; + + if (payload->size < rv+hdr_size) return ECP_ERR; + memcpy(payload->buffer, packet->buffer, rv+hdr_size); + + return vconn_pack(conn->parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, rv+hdr_size, NULL, addr); + } else { + return ecp_conn_pack(conn, packet->buffer, packet->size, s_idx, c_idx, payload->buffer, pld_size, si, addr); + } +} + +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; -static ssize_t vconn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size, ECPSeqItem *si, ECPNetAddr *addr) { + if (parent) { + unsigned char mtype = ecp_pld_get_type(payload->buffer); + ssize_t rv, hdr_size = vconn_set_msg(parent, packet, mtype); + if (hdr_size < 0) return hdr_size; + + rv = ecp_pack(ctx, packet->buffer+hdr_size, packet->size-hdr_size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); + if (rv < 0) return rv; + + if (payload->size < rv+hdr_size) return ECP_ERR; + memcpy(payload->buffer, packet->buffer, rv+hdr_size); + + return vconn_pack(parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, rv+hdr_size, NULL, addr); + } else { + return ecp_pack(ctx, packet->buffer, packet->size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); + } +} + +/* Non memory limited version: */ +/* +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 (conn->parent) { - unsigned char payload_[ECP_MAX_PLD]; - ssize_t rv, hdr_size = vconn_set_msg(conn->parent, payload_, sizeof(payload_), payload, payload_size); + ECPBuffer payload_; + unsigned char pld_buf[ECP_MAX_PLD]; + + payload_.buffer = pld_buf; + payload_.size = ECP_MAX_PLD; + + unsigned char mtype = ecp_pld_get_type(payload->buffer); + ssize_t rv, hdr_size = vconn_set_msg(conn->parent, &payload_, mtype); if (hdr_size < 0) return hdr_size; - rv = ecp_conn_pack(conn, payload_+hdr_size, ECP_MAX_PLD-hdr_size, s_idx, c_idx, payload, payload_size, si, NULL); + rv = ecp_conn_pack(conn, payload_.buffer+hdr_size, payload_.size-hdr_size, s_idx, c_idx, payload->buffer, pld_size, si, NULL); if (rv < 0) return rv; - return vconn_pack(conn->parent, packet, pkt_size, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload_, rv+hdr_size, NULL, addr); + return vconn_pack(conn->parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, &payload_, rv+hdr_size, NULL, addr); } else { - return ecp_conn_pack(conn, packet, pkt_size, s_idx, c_idx, payload, payload_size, si, addr); + return ecp_conn_pack(conn, packet->buffer, packet->size, s_idx, c_idx, payload->buffer, pld_size, si, addr); } } -static ssize_t vconn_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 payload_size, ECPNetAddr *addr) { +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 (parent) { - unsigned char payload_[ECP_MAX_PLD]; - ssize_t rv, hdr_size = vconn_set_msg(parent, payload_, sizeof(payload_), payload, payload_size); + ECPBuffer payload_; + unsigned char pld_buf[ECP_MAX_PLD]; + + payload_.buffer = pld_buf; + payload_.size = ECP_MAX_PLD; + + unsigned char mtype = ecp_pld_get_type(payload->buffer); + ssize_t rv, hdr_size = vconn_set_msg(parent, &payload_, mtype); if (hdr_size < 0) return hdr_size; - rv = ecp_pack(ctx, payload_+hdr_size, ECP_MAX_PLD-hdr_size, s_idx, c_idx, public, shsec, nonce, seq, payload, payload_size); + rv = ecp_pack(ctx, payload_.buffer+hdr_size, payload_.size-hdr_size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); if (rv < 0) return rv; - return vconn_pack(parent, packet, pkt_size, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload_, rv+hdr_size, NULL, addr); + return vconn_pack(parent, packet, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, &payload_, rv+hdr_size, NULL, addr); } else { - return ecp_pack(ctx, packet, pkt_size, s_idx, c_idx, public, shsec, nonce, seq, payload, payload_size); + return ecp_pack(ctx, packet->buffer, packet->size, s_idx, c_idx, public, shsec, nonce, seq, payload->buffer, pld_size); } } +*/ int ecp_ctx_vconn_init(ECPContext *ctx) { int rv; @@ -509,10 +588,18 @@ int ecp_vconn_init(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn } static ssize_t _vconn_send_kget(ECPConnection *conn, ECPTimerItem *ti) { - unsigned char payload[ECP_SIZE_PLD(0, 0)]; + ECPBuffer packet; + ECPBuffer payload; + unsigned char pkt_buf[ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn)]; + unsigned char pld_buf[ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn)]; + + packet.buffer = pkt_buf; + packet.size = ECP_SIZE_PKT_BUF(0, ECP_MTYPE_KGET_REQ, conn); + payload.buffer = pld_buf; + payload.size = ECP_SIZE_PLD_BUF(0, ECP_MTYPE_KGET_REQ, conn); - ecp_pld_set_type(payload, ECP_MTYPE_KGET_REQ); - return ecp_pld_send_ll(conn, ti, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, payload, sizeof(payload)); + 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); } int ecp_vconn_open(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size) { |