diff options
Diffstat (limited to 'ecp/src/core.h')
-rw-r--r-- | ecp/src/core.h | 98 |
1 files changed, 58 insertions, 40 deletions
diff --git a/ecp/src/core.h b/ecp/src/core.h index 3c62450..bb96f26 100644 --- a/ecp/src/core.h +++ b/ecp/src/core.h @@ -7,6 +7,8 @@ #include <pthread.h> #endif +#define ECP_MEM_TINY 1 + #define ECP_OK 0 #define ECP_ITER_NEXT 1 @@ -52,6 +54,7 @@ #define ECP_MAX_CTYPE 8 #define ECP_MAX_MTYPE 16 #define ECP_MAX_MTYPE_SYS 4 +#define ECP_MAX_MTYPE_SOCK ECP_MAX_MTYPE_SYS #define ECP_MAX_SEQ_FWD 1024 #define ECP_SIZE_PKT_HDR (ECP_SIZE_PROTO+1+ECP_ECDH_SIZE_KEY+ECP_AEAD_SIZE_NONCE) @@ -75,7 +78,7 @@ #define ECP_MTYPE_OPEN 0x00 #define ECP_MTYPE_KGET 0x01 #define ECP_MTYPE_KPUT 0x02 -#define ECP_MTYPE_NOP 0x03 +#define ECP_MTYPE_DIR 0x03 #define ECP_MTYPE_OPEN_REQ (ECP_MTYPE_OPEN) #define ECP_MTYPE_OPEN_REP (ECP_MTYPE_OPEN | ECP_MTYPE_FLAG_REP) @@ -115,7 +118,7 @@ typedef uint32_t ecp_seq_t; #define ECP_SEQ_LTE(a,b) ((ecp_seq_t)((ecp_seq_t)(b) - (ecp_seq_t)(a)) < ECP_SEQ_HALF) -#define ECP_SIZE_MT_FRAG(F) ((F) & ECP_MTYPE_FLAG_FRAG ? 2 : 0) +#define ECP_SIZE_MT_FRAG(F) ((F) & ECP_MTYPE_FLAG_FRAG ? 2 + sizeof(uint16_t) : 0) #define ECP_SIZE_MT_PTS(F) ((F) & ECP_MTYPE_FLAG_PTS ? sizeof(ecp_pts_t) : 0) #define ECP_SIZE_MT_FLAG(F) (ECP_SIZE_MT_FRAG(F)+ECP_SIZE_MT_PTS(F)) #define ECP_SIZE_PLD(X,F) ((X) + ECP_SIZE_PLD_HDR+1+ECP_SIZE_MT_FLAG(F)) @@ -126,8 +129,8 @@ typedef uint32_t ecp_seq_t; #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) +#define ECP_SIZE_PLD_BUF_TR(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_BUF_TR(X,T,P) (ECP_SIZE_PLD_BUF_TR(X,T,P)+ECP_SIZE_PKT_HDR+ECP_AEAD_SIZE_TAG) #ifdef ECP_DEBUG #include <stdio.h> @@ -139,7 +142,7 @@ struct ECPContext; struct ECPSocket; struct ECPConnection; struct ECPSeqItem; -struct ECPFragIter; +struct ECPPktMeta; #include "platform/transport.h" #include "crypto/crypto.h" @@ -151,7 +154,7 @@ struct ECPFragIter; typedef int (*ecp_rng_t) (void *, size_t); -typedef int (*ecp_conn_handler_new_t) (struct ECPSocket *s, struct ECPConnection *p, unsigned char *msg, size_t sz, struct ECPConnection **c); +typedef int (*ecp_sock_handler_msg_t) (struct ECPSocket *s, ECPNetAddr *a, struct ECPConnection *p, unsigned char *msg, size_t sz, struct ECPPktMeta *m, struct ECP2Buffer *b, 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); @@ -210,12 +213,21 @@ typedef struct ECPSeqItem { typedef struct ECPFragIter { ecp_seq_t seq; - size_t frag_size; unsigned char frag_cnt; unsigned char *buffer; size_t buf_size; + size_t content_size; } ECPFragIter; +typedef struct ECPPktMeta { + ecp_aead_key_t shsec; + ecp_dh_public_t public; + ecp_seq_t seq; + unsigned char nonce[ECP_AEAD_SIZE_NONCE]; + unsigned char s_idx; + unsigned char c_idx; +} ECPPktMeta; + typedef struct ECPConnHandler { ecp_conn_handler_msg_t msg[ECP_MAX_MTYPE]; ecp_conn_create_t conn_create; @@ -253,7 +265,7 @@ typedef struct ECPSocket { unsigned char key_curr; ECPSockCTable conn; ECPTimer timer; - ecp_conn_handler_new_t conn_new; + ecp_sock_handler_msg_t handler[ECP_MAX_MTYPE_SOCK]; #ifdef ECP_WITH_PTHREAD pthread_t rcvr_thd; pthread_mutex_t mutex; @@ -290,11 +302,12 @@ typedef struct ECPConnection { } ECPConnection; int ecp_init(ECPContext *ctx); - -int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key); -int ecp_node_init(ECPNode *node, ecp_dh_public_t *public, void *addr); - int ecp_ctx_init(ECPContext *ctx); +int ecp_node_init(ECPNode *node, ecp_dh_public_t *public, void *addr); +int ecp_seq_item_init(ECPSeqItem *seq_item); +int ecp_frag_iter_init(ECPFragIter *iter, unsigned char *buffer, size_t buf_size); +void ecp_frag_iter_reset(ECPFragIter *iter); +int ecp_dhkey_gen(ECPContext *ctx, ECPDHKey *key); int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key); void ecp_sock_destroy(ECPSocket *sock); @@ -303,59 +316,64 @@ void ecp_sock_close(ECPSocket *sock); int ecp_sock_dhkey_get_curr(ECPSocket *sock, unsigned char *idx, unsigned char *public); int ecp_sock_dhkey_new(ECPSocket *sock); +int ecp_conn_init(ECPConnection *conn, ECPNode *node); int ecp_conn_create(ECPConnection *conn, ECPSocket *sock, unsigned char ctype); void ecp_conn_destroy(ECPConnection *conn); int ecp_conn_register(ECPConnection *conn); void ecp_conn_unregister(ECPConnection *conn); -int ecp_conn_init(ECPConnection *conn, ECPNode *node); int ecp_conn_open(ECPConnection *conn, ECPNode *node); int ecp_conn_close(ECPConnection *conn, ecp_cts_t timeout); - +int ecp_conn_reset(ECPConnection *conn); int ecp_conn_handler_init(ECPConnHandler *handler); + +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_conn_send_open(ECPConnection *conn); -int ecp_conn_handle_new(ECPSocket *sock, ECPConnection *parent, unsigned char *payload, size_t payload_size, ECPConnection **_conn); +ssize_t ecp_conn_send_kget(ECPConnection *conn); +ssize_t ecp_conn_send_kput(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); +ssize_t ecp_conn_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, size_t msg_size, ECP2Buffer *bufs); +int ecp_sock_handle_open(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, unsigned char *msg, size_t msg_size, ECPPktMeta *pkt_meta, ECP2Buffer *bufs, ECPConnection **_conn); +int ecp_sock_handle_kget(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, unsigned char *msg, size_t msg_size, ECPPktMeta *pkt_meta, ECP2Buffer *bufs, ECPConnection **_conn); -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, ECPPktMeta *pkt_meta, unsigned char *payload, size_t pld_size); +ssize_t ecp_pack(ECPContext *ctx, ECPConnection *parent, ECPBuffer *packet, ECPPktMeta *pkt_meta, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr) ; -ssize_t ecp_pkt_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_pkt_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_pack_conn(ECPConnection *conn, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ECPSeqItem *si, unsigned char *payload, size_t pld_size, ECPNetAddr *addr); +ssize_t ecp_pack_conn(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPSeqItem *si, ECPBuffer *payload, size_t pld_size, ECPNetAddr *addr); ssize_t ecp_unpack(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size, ECPConnection **_conn, ecp_seq_t *_seq); ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECP2Buffer *bufs, size_t pkt_size); - ssize_t ecp_pkt_send(ECPSocket *sock, ECPNetAddr *addr, ECPBuffer *packet, size_t pkt_size, unsigned char flags); -int ecp_seq_item_init(ECPSeqItem *seq_item); -int ecp_frag_iter_init(ECPFragIter *iter, unsigned char *buffer, size_t buf_size); - -unsigned char ecp_msg_get_type(unsigned char *msg); -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_type(unsigned char *msg, size_t msg_size, unsigned char *mtype); +int ecp_msg_set_type(unsigned char *msg, size_t msg_size, unsigned char mtype); +int ecp_msg_get_frag(unsigned char *msg, size_t msg_size, unsigned char *frag_cnt, unsigned char *frag_tot, uint16_t *frag_size); +int ecp_msg_set_frag(unsigned char *msg, size_t msg_size, unsigned char frag_cnt, unsigned char frag_tot, uint16_t frag_size); 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, ECP2Buffer *bufs); +int ecp_msg_set_pts(unsigned char *msg, size_t msg_size, ecp_pts_t pts); +unsigned char *ecp_msg_get_content(unsigned char *msg, size_t msg_size); +int ecp_msg_defrag(ECPFragIter *iter, ecp_seq_t seq, unsigned char mtype, unsigned char *msg_in, size_t msg_in_size, unsigned char **msg_out, size_t *msg_out_size); -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); -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); +int ecp_pld_get_type(unsigned char *payload, size_t pld_size, unsigned char *mtype); +int ecp_pld_set_type(unsigned char *payload, size_t pld_size, unsigned char mtype); +int ecp_pld_get_frag(unsigned char *payload, size_t pld_size, unsigned char *frag_cnt, unsigned char *frag_tot, uint16_t *frag_size); +int ecp_pld_set_frag(unsigned char *payload, size_t pld_size, unsigned char frag_cnt, unsigned char frag_tot, uint16_t frag_size); +int ecp_pld_get_pts(unsigned char *payload, size_t pld_size, ecp_pts_t *pts); +int ecp_pld_set_pts(unsigned char *payload, size_t pld_size, ecp_pts_t pts); +unsigned char *ecp_pld_get_buf(unsigned char *payload, size_t pld_size); +ssize_t _ecp_pld_send(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPSeqItem *si, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti); ssize_t ecp_pld_send(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags); ssize_t ecp_pld_send_wtimer(ECPConnection *conn, ECPBuffer *packet, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti); -ssize_t ecp_pld_send_ll(ECPConnection *conn, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ECPBuffer *payload, size_t pld_size, unsigned char flags, ECPTimerItem *ti); -ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *parent, ECPNetAddr *addr, ECPBuffer *packet, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, ECPBuffer *payload, size_t pld_size, unsigned char flags); +ssize_t ecp_pld_send_tr(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECPBuffer *packet, ECPPktMeta *pkt_meta, ECPBuffer *payload, size_t pld_size, unsigned char flags); ssize_t ecp_send(ECPConnection *conn, unsigned char mtype, unsigned char *content, size_t content_size); ssize_t ecp_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, ecp_cts_t timeout); |