From 498fb8b691aaa62ec12b7326361140321c1a8220 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Sat, 4 Sep 2021 02:51:10 +0200 Subject: ecp fixed frag problems --- ecp/src/core.c | 107 ++++++++++---------------------------------------- ecp/src/core.h | 2 +- ecp/src/vconn/vconn.c | 4 +- ecp/src/vconn/vconn.h | 2 +- ecp/test/vc_client.c | 14 +++---- 5 files changed, 32 insertions(+), 97 deletions(-) (limited to 'ecp') diff --git a/ecp/src/core.c b/ecp/src/core.c index 23ed87b..b4b6055 100644 --- a/ecp/src/core.c +++ b/ecp/src/core.c @@ -53,9 +53,9 @@ int ecp_frag_iter_init(ECPFragIter *iter, unsigned char *buffer, size_t buf_size } void ecp_frag_iter_reset(ECPFragIter *iter) { - iter->content_size = 0; iter->seq = 0; iter->frag_cnt = 0; + iter->msg_size = 0; } int ecp_dhkey_gen(ECPContext *ctx, ECPDHKey *key) { @@ -399,6 +399,7 @@ static int conn_shsec_get(ECPConnection *conn, unsigned char s_idx, unsigned cha public_p = &conn->node.public; priv = conn_dhkey_get(conn, c_idx); } else { +#if 0 ECPDHRKey *pub = NULL; if (c_idx >= ECP_MAX_SOCK_KEY) return ECP_ERR_ECDH_IDX; @@ -407,6 +408,7 @@ static int conn_shsec_get(ECPConnection *conn, unsigned char s_idx, unsigned cha pub = &conn->remote.key[conn->remote.key_idx_map[c_idx]]; public_p = pub->idx != ECP_ECDH_IDX_INV ? &pub->public : NULL; priv = &conn->sock->key_perma; +#endif } if (public_p == NULL) return ECP_ERR_ECDH_IDX; if ((priv == NULL) || !priv->valid) return ECP_ERR_ECDH_IDX; @@ -1651,7 +1653,7 @@ int ecp_msg_defrag(ECPFragIter *iter, ecp_seq_t seq, unsigned char mtype, unsign unsigned char *content; unsigned char frag_cnt, frag_tot; uint16_t frag_size; - size_t content_size; + size_t msg_size; size_t buf_offset; int rv; @@ -1661,34 +1663,36 @@ int ecp_msg_defrag(ECPFragIter *iter, ecp_seq_t seq, unsigned char mtype, unsign content = ecp_msg_get_content(msg_in, msg_in_size); if (content == NULL) return ECP_ERR_MIN_MSG; - content_size = msg_in_size - (content - msg_in); - if (content_size == 0) return ECP_ERR_MIN_MSG; + msg_size = msg_in_size - (content - msg_in); + if (msg_size == 0) return ECP_ERR_MIN_MSG; - if (iter->content_size && (iter->seq + frag_cnt != seq)) ecp_frag_iter_reset(iter); + if (iter->msg_size && (iter->seq + frag_cnt != seq)) ecp_frag_iter_reset(iter); - if (iter->content_size == 0) { + if (iter->msg_size == 0) { iter->seq = seq - frag_cnt; iter->frag_cnt = 0; } mtype &= (~ECP_MTYPE_FLAG_FRAG); + buf_offset = 1 + ECP_SIZE_MT_FLAG(mtype) + frag_size * frag_cnt; + if (buf_offset + msg_size > iter->buf_size) return ECP_ERR_SIZE; + memcpy(iter->buffer + buf_offset, content, msg_size); + if (frag_cnt == 0) { if (1 + ECP_SIZE_MT_FLAG(mtype) > iter->buf_size) return ECP_ERR_SIZE; + iter->buffer[0] = mtype; if (ECP_SIZE_MT_FLAG(mtype)) { - memcpy(iter->buffer + 1, msg_in, ECP_SIZE_MT_FLAG(mtype)); + memcpy(iter->buffer + 1, msg_in + 1, ECP_SIZE_MT_FLAG(mtype)); } + msg_size += 1 + ECP_SIZE_MT_FLAG(mtype); } - buf_offset = 1 + ECP_SIZE_MT_FLAG(mtype) + frag_size * frag_cnt; - if (buf_offset + content_size > iter->buf_size) return ECP_ERR_SIZE; - - memcpy(iter->buffer + buf_offset, content, content_size); iter->frag_cnt++; - iter->content_size += content_size; + iter->msg_size += msg_size; if (iter->frag_cnt == frag_tot) { *msg_out = iter->buffer; - *msg_out_size = iter->content_size; + *msg_out_size = iter->msg_size; return ECP_OK; } else { return ECP_ITER_NEXT; @@ -1810,8 +1814,8 @@ ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *conten unsigned char *content_buf; ssize_t rv = 0; int pkt_cnt = 0; - int vc_cnt = conn->pcount + 1; - size_t pld_max = ECP_MAX_PKT - (ECP_SIZE_PKT_HDR + ECP_AEAD_SIZE_TAG + ECP_SIZE_PLD_HDR + 1) * vc_cnt; + int vc_cnt = conn->pcount; + size_t pld_max = ECP_MAX_PKT - (ECP_SIZE_PKT_HDR + ECP_AEAD_SIZE_TAG + ECP_SIZE_PLD_HDR + 1) * vc_cnt - (ECP_SIZE_PKT_HDR + ECP_AEAD_SIZE_TAG); packet.buffer = pkt_buf; packet.size = ECP_MAX_PKT; @@ -1829,7 +1833,7 @@ ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *conten if (_rv) return _rv; mtype |= ECP_MTYPE_FLAG_FRAG; - frag_size = pld_max - (ECP_SIZE_PLD_HDR +1 +ECP_SIZE_MT_FLAG(mtype)); + frag_size = pld_max - ECP_SIZE_PLD(0, mtype); pkt_cnt = content_size / frag_size; frag_size_final = content_size - frag_size * pkt_cnt; if (frag_size_final) pkt_cnt++; @@ -1907,11 +1911,6 @@ static int recv_p(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t s return ECP_OK; } -#ifdef ECP_DEBUG -static char *_utoa(unsigned value, char *str, int base); -static char *_itoa(int value, char *str, int base); -#endif - int ecp_receiver(ECPSocket *sock) { ECPNetAddr addr; ECPBuffer packet; @@ -1930,10 +1929,7 @@ int ecp_receiver(ECPSocket *sock) { _rv = recv_p(sock, &addr, &packet, rv); #ifdef ECP_DEBUG if (_rv) { - char b[16]; - puts("ERR:"); - puts(_itoa(_rv, b, 10)); - puts("\n"); + printf("RECEIVER ERR:%d\n", _rv); } #endif } @@ -1974,64 +1970,3 @@ int ecp_stop_receiver(ECPSocket *sock) { return ECP_ERR_NOT_IMPLEMENTED; } #endif - -#ifdef ECP_DEBUG -static char *_utoa(unsigned value, char *str, int base) { - const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - int i, j; - unsigned remainder; - char c; - - /* Check base is supported. */ - if ((base < 2) || (base > 36)) - { - str[0] = '\0'; - return NULL; - } - - /* Convert to string. Digits are in reverse order. */ - i = 0; - do - { - remainder = value % base; - str[i++] = digits[remainder]; - value = value / base; - } while (value != 0); - str[i] = '\0'; - - /* Reverse string. */ - for (j = 0, i--; j < i; j++, i--) - { - c = str[j]; - str[j] = str[i]; - str[i] = c; - } - - return str; -} - -static char *_itoa(int value, char *str, int base) { - unsigned uvalue; - int i = 0; - - /* Check base is supported. */ - if ((base < 2) || (base > 36)) - { - str[0] = '\0'; - return NULL; - } - - /* Negative numbers are only supported for decimal. - * Cast to unsigned to avoid overflow for maximum negative value. */ - if ((base == 10) && (value < 0)) - { - str[i++] = '-'; - uvalue = (unsigned)-value; - } - else - uvalue = (unsigned)value; - - _utoa(uvalue, &str[i], base); - return str; -} -#endif diff --git a/ecp/src/core.h b/ecp/src/core.h index 24ea4ee..bf5228b 100644 --- a/ecp/src/core.h +++ b/ecp/src/core.h @@ -220,7 +220,7 @@ typedef struct ECPFragIter { unsigned char frag_cnt; unsigned char *buffer; size_t buf_size; - size_t content_size; + size_t msg_size; } ECPFragIter; typedef struct ECPPktMeta { diff --git a/ecp/src/vconn/vconn.c b/ecp/src/vconn/vconn.c index 56a2105..c414454 100644 --- a/ecp/src/vconn/vconn.c +++ b/ecp/src/vconn/vconn.c @@ -605,7 +605,7 @@ int ecp_vconn_ctx_init(ECPContext *ctx) { return ECP_OK; } -int ecp_vconn_init(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size) { +int ecp_vconn_set_remote(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size) { ECPSocket *sock = conn->sock; int i, rv; @@ -657,7 +657,7 @@ int ecp_vconn_open(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn int rv; ssize_t _rv; - rv = ecp_vconn_init(conn, conn_node, vconn, vconn_node, size); + rv = ecp_vconn_set_remote(conn, conn_node, vconn, vconn_node, size); if (rv) return rv; _rv = ecp_timer_send((ECPConnection *)&vconn[0], _vconn_send_kget, ECP_MTYPE_KGET_REP, 3, 500); diff --git a/ecp/src/vconn/vconn.h b/ecp/src/vconn/vconn.h index 9e9078c..12eae77 100644 --- a/ecp/src/vconn/vconn.h +++ b/ecp/src/vconn/vconn.h @@ -17,5 +17,5 @@ typedef struct ECPVConnIn { } ECPVConnIn; int ecp_vconn_ctx_init(ECPContext *ctx); -int ecp_vconn_init(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size); +int ecp_vconn_set_remote(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size); int ecp_vconn_open(ECPConnection *conn, ECPNode *conn_node, ECPVConnection vconn[], ECPNode vconn_node[], int size); diff --git a/ecp/test/vc_client.c b/ecp/test/vc_client.c index e654e76..d669697 100644 --- a/ecp/test/vc_client.c +++ b/ecp/test/vc_client.c @@ -22,13 +22,13 @@ ECPNode vconn_node[20]; 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, b); if (s < 0) { printf("OPEN ERR:%ld\n", s); return s; } - + printf("OPEN!\n"); char *msg = "PERA JE CAR!"; @@ -52,23 +52,23 @@ static void usage(char *arg) { int main(int argc, char *argv[]) { int rv, i; - + if ((argc < 3) || (argc > 22)) usage(argv[0]); - + rv = ecp_init(&ctx); printf("ecp_init RV:%d\n", rv); - + rv = ecp_conn_handler_init(&handler); handler.msg[ECP_MTYPE_OPEN] = handle_open; handler.msg[MTYPE_MSG] = handle_msg; ctx.handler[CTYPE_TEST] = &handler; - + rv = ecp_sock_create(&sock, &ctx, NULL); printf("ecp_sock_create RV:%d\n", rv); rv = ecp_sock_open(&sock, NULL); printf("ecp_sock_open RV:%d\n", rv); - + rv = ecp_start_receiver(&sock); printf("ecp_start_receiver RV:%d\n", rv); -- cgit v1.2.3