diff options
author | Uros Majstorovic <majstor@majstor.org> | 2017-08-24 14:34:05 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2017-08-24 14:34:05 +0200 |
commit | e800e8df9fbe633d09a534ae07eb361833572f1a (patch) | |
tree | 57a827276059da457c65c23c936843d42647664c /code/core | |
parent | a3f0e09379759e4cfb79e1d5c7d7ac752728fe5f (diff) |
seq item implemented for sender
Diffstat (limited to 'code/core')
-rw-r--r-- | code/core/core.c | 79 | ||||
-rw-r--r-- | code/core/core.h | 20 | ||||
-rw-r--r-- | code/core/rbuf.c | 17 | ||||
-rw-r--r-- | code/core/rbuf.h | 12 | ||||
-rw-r--r-- | code/core/rbuf_send.c | 18 | ||||
-rw-r--r-- | code/core/timer.h | 2 |
6 files changed, 77 insertions, 71 deletions
diff --git a/code/core/core.c b/code/core/core.c index cee72c4..ca097c4 100644 --- a/code/core/core.c +++ b/code/core/core.c @@ -985,7 +985,7 @@ ssize_t ecp_pack_raw(ECPSocket *sock, ECPConnection *parent, unsigned char *pack return ecp_pack(ctx, packet, pkt_size, s_idx, c_idx, public, shsec, nonce, seq, payload, payload_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, ECPNetAddr *addr, void *_rbuf_info) { +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) { ecp_aead_key_t shsec; ecp_dh_public_t public; ecp_seq_t _seq; @@ -1022,27 +1022,26 @@ ssize_t ecp_conn_pack(ECPConnection *conn, unsigned char *packet, size_t pkt_siz } } if (!rv) { -#ifdef ECP_WITH_RBUF - ECPRBInfo *rbuf_info = _rbuf_info; - - if (rbuf_info) { - if (rbuf_info->seq_force) { - _seq = rbuf_info->seq; + if (si) { + if (si->seq_w) { + _seq = si->seq; } else { - unsigned char mtype = ecp_pld_get_type(payload); _seq = conn->seq_out + 1; + si->seq = _seq; + } - rv = ecp_rbuf_pkt_prep(conn->rbuf.send, _seq, mtype, rbuf_info); - if (!rv) conn->seq_out = _seq; +#ifdef ECP_WITH_RBUF + if (conn->rbuf.send && !si->rb_pass) { + si->rb_mtype = ecp_pld_get_type(payload); + rv = ecp_rbuf_pkt_prep(conn->rbuf.send, si); } +#endif + + if (!rv && !si->seq_w) conn->seq_out = _seq; } else { _seq = conn->seq_out + 1; conn->seq_out = _seq; } -#else - _seq = conn->seq_out + 1; - conn->seq_out = _seq; -#endif if (!rv && addr) *addr = conn->node.addr; } @@ -1315,6 +1314,12 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, s return msg_size; } +int ecp_seq_item_init(ECPSeqItem *seq_item) { + memset(seq_item, 0, sizeof(ECPSeqItem)); + + return ECP_OK; +} + unsigned char *ecp_pld_get_buf(unsigned char *payload) { return payload+ECP_SIZE_MSG_HDR; } @@ -1327,6 +1332,26 @@ unsigned char ecp_pld_get_type(unsigned char *payload) { return payload[ECP_SIZE_PLD_HDR]; } +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]; + 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); + if (rv < 0) return rv; + +#ifdef ECP_WITH_RBUF + if (conn->rbuf.send && !si->rb_pass) return ecp_rbuf_pkt_send(conn->rbuf.send, conn->sock, &addr, ti, si, packet, rv); +#endif + + if (ti) { + int _rv = ecp_timer_push(ti); + if (_rv) return _rv; + } + return ecp_pkt_send(sock, &addr, packet, 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); } @@ -1336,35 +1361,19 @@ ssize_t ecp_pld_send_wtimer(ECPConnection *conn, ECPTimerItem *ti, unsigned char } 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) { - unsigned char packet[ECP_MAX_PKT]; - ECPSocket *sock = conn->sock; - ECPContext *ctx = sock->ctx; - ECPNetAddr addr; - int _rv = ECP_OK; - void *_rbuf_info = NULL; + ECPSeqItem *_seq_item = NULL; #ifdef ECP_WITH_RBUF - ECPRBInfo rbuf_info; + ECPSeqItem seq_item; if (conn->rbuf.send) { - _rv = ecp_rbuf_info_init(&rbuf_info); + int _rv = ecp_seq_item_init(&seq_item); if (_rv) return _rv; - _rbuf_info = &rbuf_info; + _seq_item = &seq_item; } #endif - ssize_t rv = ctx->pack(conn, packet, ECP_MAX_PKT, s_idx, c_idx, payload, payload_size, &addr, _rbuf_info); - 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, packet, rv, _rbuf_info); -#endif - - if (ti) { - _rv = ecp_timer_push(ti); - if (_rv) return _rv; - } - return ecp_pkt_send(sock, &addr, packet, rv); + return pld_send(conn, ti, s_idx, c_idx, payload, payload_size, _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) { diff --git a/code/core/core.h b/code/core/core.h index 989927a..27d1e0d 100644 --- a/code/core/core.h +++ b/code/core/core.h @@ -84,6 +84,10 @@ #include <stddef.h> #include <stdint.h> +struct ECPConnection; +struct ECPSocket; +struct ECPSeqItem; + typedef long ssize_t; typedef uint32_t ecp_ack_t; @@ -203,6 +207,17 @@ typedef struct ECPNode { ecp_dh_public_t public; } ECPNode; +typedef struct ECPSeqItem { + ecp_seq_t seq; + unsigned char seq_w; +#ifdef ECP_WITH_RBUF + unsigned char rb_pass; + unsigned char rb_mtype; + unsigned int rb_idx; +#endif +} ECPSeqItem; + + typedef struct ECPConnHandler { ecp_conn_handler_msg_t *msg[ECP_MAX_MTYPE]; ecp_conn_create_t *conn_create; @@ -232,7 +247,7 @@ 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, ECPNetAddr *addr, void *_rbuf_info); + 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); ECPConnHandler *handler[ECP_MAX_CTYPE]; } ECPContext; @@ -326,13 +341,14 @@ int ecp_conn_dhkey_get_curr(ECPConnection *conn, unsigned char *idx, unsigned ch ssize_t ecp_pack(ECPContext *ctx, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t 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, ECPNetAddr *addr, void *_rbuf_info); +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_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); ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size); +int ecp_seq_item_init(ECPSeqItem *seq_item); unsigned char *ecp_pld_get_buf(unsigned char *payload); void ecp_pld_set_type(unsigned char *payload, unsigned char mtype); unsigned char ecp_pld_get_type(unsigned char *payload); diff --git a/code/core/rbuf.c b/code/core/rbuf.c index 5cbc18e..93d6fc6 100644 --- a/code/core/rbuf.c +++ b/code/core/rbuf.c @@ -44,27 +44,22 @@ ssize_t ecp_rbuf_msg_store(ECPRBuffer *rbuf, ecp_seq_t seq, int idx, unsigned ch return msg_size; } -int ecp_rbuf_info_init(ECPRBInfo *rbuf_info) { - memset(rbuf_info, 0, sizeof(ECPRBInfo)); - - return ECP_OK; -} - 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]; ECPSocket *sock = conn->sock; ECPContext *ctx = sock->ctx; ECPNetAddr addr; - ECPRBInfo rbuf_info; + ECPSeqItem seq_item; ssize_t rv; - int _rv = ecp_rbuf_info_init(&rbuf_info); + int _rv = ecp_seq_item_init(&seq_item); if (_rv) return _rv; - rbuf_info.seq = seq; - rbuf_info.seq_force = 1; + seq_item.seq = seq; + 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, &addr, &rbuf_info); + rv = ctx->pack(conn, packet, ECP_MAX_PKT, ECP_ECDH_IDX_INV, ECP_ECDH_IDX_INV, payload, payload_size, &seq_item, &addr); if (rv < 0) return rv; rv = ecp_pkt_send(sock, &addr, packet, rv); diff --git a/code/core/rbuf.h b/code/core/rbuf.h index a965755..d10370a 100644 --- a/code/core/rbuf.h +++ b/code/core/rbuf.h @@ -86,19 +86,11 @@ typedef struct ECPConnRBuffer { ECPRBSend *send; } ECPConnRBuffer; -typedef struct ECPRBInfo { - ecp_seq_t seq; - unsigned int idx; - unsigned char mtype; - unsigned char seq_force; -} ECPRBInfo; - int ecp_rbuf_init(ECPRBuffer *rbuf, ECPRBMessage *msg, unsigned int msg_size); 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); -int ecp_rbuf_info_init(ECPRBInfo *rbuf_info); ssize_t ecp_rbuf_pld_send(struct ECPConnection *conn, unsigned char *payload, size_t payload_size, ecp_seq_t seq); int ecp_rbuf_conn_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); @@ -115,8 +107,8 @@ 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, ecp_seq_t seq, unsigned char mtype, ECPRBInfo *rbuf_info); -ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, struct ECPSocket *sock, ECPNetAddr *addr, ECPTimerItem *ti, unsigned char *packet, size_t pkt_size, ECPRBInfo *rbuf_info); +int ecp_rbuf_pkt_prep(ECPRBSend *buf, struct ECPSeqItem *si); +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); 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); diff --git a/code/core/rbuf_send.c b/code/core/rbuf_send.c index f70f162..e0c7f29 100644 --- a/code/core/rbuf_send.c +++ b/code/core/rbuf_send.c @@ -269,35 +269,31 @@ int ecp_rbuf_flush(ECPConnection *conn) { return ECP_OK; } -int ecp_rbuf_pkt_prep(ECPRBSend *buf, ecp_seq_t seq, unsigned char mtype, ECPRBInfo *rbuf_info) { - if (buf == NULL) return ECP_ERR; - +int ecp_rbuf_pkt_prep(ECPRBSend *buf, ECPSeqItem *si) { #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&buf->mutex); #endif - int idx = ecp_rbuf_msg_idx(&buf->rbuf, seq); + int idx = ecp_rbuf_msg_idx(&buf->rbuf, si->seq); #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&buf->mutex); #endif if (idx < 0) return idx; - rbuf_info->seq = seq; - rbuf_info->idx = idx; - rbuf_info->mtype = mtype; + si->rb_idx = idx; buf->rbuf.msg[idx].size = 0; buf->rbuf.msg[idx].flags = 0; return ECP_OK; } -ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, ECPSocket *sock, ECPNetAddr *addr, ECPTimerItem *ti, unsigned char *packet, size_t pkt_size, ECPRBInfo *rbuf_info) { +ssize_t ecp_rbuf_pkt_send(ECPRBSend *buf, ECPSocket *sock, ECPNetAddr *addr, ECPTimerItem *ti, ECPSeqItem *si, unsigned char *packet, size_t pkt_size) { unsigned char flags = 0; int do_send = 1; ssize_t rv = 0; - ecp_seq_t seq = rbuf_info->seq; - unsigned int idx = rbuf_info->idx; - unsigned char mtype = rbuf_info->mtype & ECP_MTYPE_MASK; + ecp_seq_t seq = si->seq; + unsigned int idx = si->rb_idx; + unsigned char mtype = si->rb_mtype & ECP_MTYPE_MASK; if (mtype < ECP_MAX_MTYPE_SYS) flags |= ECP_RBUF_FLAG_SYS; diff --git a/code/core/timer.h b/code/core/timer.h index 0d43a99..ba25b6c 100644 --- a/code/core/timer.h +++ b/code/core/timer.h @@ -8,8 +8,6 @@ #include <pthread.h> #endif -struct ECPConnection; -struct ECPSocket; struct ECPTimerItem; typedef ssize_t ecp_timer_retry_t (struct ECPConnection *, struct ECPTimerItem *); |