diff options
author | Uros Majstorovic <majstor@majstor.org> | 2017-05-23 03:49:54 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2017-05-23 03:49:54 +0200 |
commit | 7c70a430f9c708be2fcce8c9a0d8cecde7f75fc0 (patch) | |
tree | 5ea4cfa8a21be07690c28161153017b7270bc54b /code/core.h | |
parent | 25de5e761daab8b897a4f09ff8503e6f43c299f9 (diff) |
changes for proxy
Diffstat (limited to 'code/core.h')
-rw-r--r-- | code/core.h | 113 |
1 files changed, 77 insertions, 36 deletions
diff --git a/code/core.h b/code/core.h index 09c3a2e..929461f 100644 --- a/code/core.h +++ b/code/core.h @@ -4,10 +4,13 @@ #define ECP_ERR_ALLOC -3 #define ECP_ERR_MAX_SOCK_CONN -10 -#define ECP_ERR_MAX_PTYPE -11 -#define ECP_ERR_MAX_PLD -12 -#define ECP_ERR_MIN_MSG -13 -#define ECP_ERR_NET_ADDR -14 +#define ECP_ERR_MAX_CTYPE -11 +#define ECP_ERR_MAX_MTYPE -12 +#define ECP_ERR_MIN_PKT -13 +#define ECP_ERR_MAX_PLD -14 +#define ECP_ERR_MIN_MSG -15 +#define ECP_ERR_MAX_MSG -16 +#define ECP_ERR_NET_ADDR -17 #define ECP_ERR_CONN_NOT_FOUND -20 #define ECP_ERR_ECDH_KEY_DUP -21 @@ -31,27 +34,32 @@ #define ECP_MAX_SOCK_KEY 8 #define ECP_MAX_CONN_KEY 2 #define ECP_MAX_NODE_KEY 2 -#define ECP_MAX_PTYPE 32 -#define ECP_MAX_PTYPE_SYS 4 +#define ECP_MAX_CTYPE 8 +#define ECP_MAX_MTYPE 16 #define ECP_SIZE_PKT_HDR (ECP_SIZE_PROTO+1+ECP_ECDH_SIZE_KEY+ECP_AEAD_SIZE_NONCE) -#define ECP_SIZE_PLD_HDR (ECP_SIZE_SEQ+1) +#define ECP_SIZE_PLD_HDR (ECP_SIZE_SEQ) +#define ECP_SIZE_MSG_HDR (ECP_SIZE_PLD_HDR+1) -#define ECP_MIN_MSG 16 #define ECP_MAX_PKT 1412 -#define ECP_MIN_PKT (ECP_SIZE_PKT_HDR+ECP_SIZE_PLD_HDR+ECP_MIN_MSG+ECP_AEAD_SIZE_TAG) -#define ECP_MAX_PLD (ECP_MAX_PKT-ECP_SIZE_PKT_HDR-ECP_SIZE_PLD_HDR-ECP_AEAD_SIZE_TAG) -#define ECP_MIN_PLD ECP_SIZE_PLD_HDR+ECP_MIN_MSG +#define ECP_MAX_PLD (ECP_MAX_PKT-ECP_SIZE_PKT_HDR-ECP_AEAD_SIZE_TAG) +#define ECP_MAX_MSG (ECP_MAX_PLD-ECP_SIZE_MSG_HDR) + +#define ECP_MIN_MSG 8 +#define ECP_MIN_PLD (ECP_SIZE_MSG_HDR+ECP_MIN_MSG) +#define ECP_MIN_PKT (ECP_SIZE_PKT_HDR+ECP_MIN_PLD+ECP_AEAD_SIZE_TAG) #define ECP_POLL_TIMEOUT 500 #define ECP_ECDH_IDX_INV 0xFF #define ECP_ECDH_IDX_PERMA 0x0F -#define ECP_PTYPE_OPEN 0x00 -#define ECP_PTYPE_KGET 0x01 -#define ECP_PTYPE_KPUT 0x02 +#define ECP_MTYPE_OPEN 0x00 +#define ECP_MTYPE_KGET 0x01 +#define ECP_MTYPE_KPUT 0x02 +#define ECP_MTYPE_EXEC 0x03 -#define ECP_SIZE_PLD(X) (X+ECP_SIZE_PLD_HDR) +#define ECP_SIZE_PLD(X) ((X) < ECP_MIN_MSG ? ECP_MIN_MSG + ECP_SIZE_MSG_HDR : (X) + ECP_SIZE_MSG_HDR) +#define ECP_SIZE_PKT(X) ((X) < ECP_MIN_MSG ? ECP_MIN_MSG + ECP_SIZE_PKT_HDR+ECP_SIZE_MSG_HDR+ECP_AEAD_SIZE_TAG : (X) + ECP_SIZE_PKT_HDR+ECP_SIZE_MSG_HDR+ECP_AEAD_SIZE_TAG) #define ECP_CONN_FLAG_REG 0x01 #define ECP_CONN_FLAG_OPEN 0x02 @@ -66,13 +74,16 @@ typedef long ssize_t; #ifdef ECP_WITH_PTHREAD #include <pthread.h> -#include "msgq.h" #endif #include "posix/transport.h" #include "crypto/crypto.h" #include "timer.h" +#ifdef ECP_WITH_PTHREAD +#include "msgq.h" +#endif + #ifdef ECP_DEBUG #include <stdio.h> #define DPRINT(cnd, format, ...) { if (cnd) { fprintf (stderr, format, __VA_ARGS__); } } @@ -81,14 +92,19 @@ typedef long ssize_t; #endif struct ECPContext; +struct ECPSocket; struct ECPConnection; typedef int ecp_rng_t (void *, size_t); -typedef struct ECPConnection * ecp_conn_alloc_t (void); -typedef void ecp_conn_free_t (struct ECPConnection *); -typedef int ecp_conn_create_t (struct ECPConnection *, unsigned char *, size_t); -typedef void ecp_conn_destroy_t (struct ECPConnection *); -typedef ssize_t ecp_conn_handler_t (struct ECPConnection *, unsigned char, unsigned char *, ssize_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, unsigned char t, unsigned char *msg, ssize_t sz); + +typedef struct ECPConnection * ecp_conn_alloc_t (unsigned char t); +typedef void ecp_conn_free_t (struct ECPConnection *c); +typedef int ecp_conn_create_t (struct ECPConnection *c, unsigned char *msg, size_t sz); +typedef void ecp_conn_destroy_t (struct ECPConnection *c); +typedef ssize_t ecp_conn_open_t (struct ECPConnection *c); typedef struct ECPCryptoIface { int init; @@ -134,6 +150,12 @@ typedef struct ECPTimeIface { void (*sleep_ms) (unsigned int); } ECPTimeIface; +typedef struct ECPProxyIface { + int init; + ssize_t (*pack) (struct ECPConnection *conn, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size); + ssize_t (*pack_raw) (struct ECPConnection *proxy, ECPNetAddr *addr, 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, unsigned char *seq, unsigned char *payload, size_t payload_size); +} ECPProxyIface; + typedef struct ECPDHKey { ecp_dh_public_t public; ecp_dh_private_t private; @@ -162,7 +184,10 @@ typedef struct ECPNode { } ECPNode; typedef struct ECPConnHandler { - ecp_conn_handler_t *f[ECP_MAX_PTYPE]; + ecp_conn_handler_msg_t *msg[ECP_MAX_MTYPE]; + ecp_conn_create_t *conn_create; + ecp_conn_destroy_t *conn_destroy; + ecp_conn_open_t *conn_open; } ECPConnHandler; typedef struct ECPSockCTable { @@ -182,6 +207,8 @@ typedef struct ECPContext { ECPHTableIface ht; ECPTransportIface tr; ECPTimeIface tm; + ECPProxyIface pr; + ECPConnHandler *handler[ECP_MAX_CTYPE]; } ECPContext; typedef struct ECPSocket { @@ -194,7 +221,7 @@ typedef struct ECPSocket { unsigned char key_curr; ECPSockCTable conn; ECPTimer timer; - ecp_conn_handler_t *handler[ECP_MAX_PTYPE_SYS]; + ecp_conn_handler_new_t *conn_new; ecp_conn_create_t *conn_create; ecp_conn_destroy_t *conn_destroy; #ifdef ECP_WITH_PTHREAD @@ -204,6 +231,7 @@ typedef struct ECPSocket { } ECPSocket; typedef struct ECPConnection { + unsigned char type; unsigned char out; unsigned char flags; unsigned short refcount; @@ -220,11 +248,11 @@ typedef struct ECPConnection { unsigned char key_idx_map[ECP_MAX_SOCK_KEY]; ECPDHShared shared[ECP_MAX_NODE_KEY][ECP_MAX_NODE_KEY]; unsigned char nonce[ECP_AEAD_SIZE_NONCE]; - ECPConnHandler *handler; #ifdef ECP_WITH_PTHREAD ECPConnMsgQ msgq; pthread_mutex_t mutex; #endif + struct ECPConnection *proxy; void *conn_data; } ECPConnection; @@ -236,7 +264,6 @@ int ecp_time_init(ECPTimeIface *t); int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key); int ecp_node_init(ECPContext *ctx, ECPNode *node, void *addr, ecp_dh_public_t *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, unsigned char *seq, unsigned char *payload, size_t payload_size); int ecp_ctx_create(ECPContext *ctx); int ecp_ctx_destroy(ECPContext *ctx); @@ -248,31 +275,45 @@ 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_create(ECPConnection *conn, ECPSocket *sock); +int ecp_conn_create(ECPConnection *conn, ECPSocket *sock, unsigned char ctype); void ecp_conn_destroy(ECPConnection *conn); int ecp_conn_register(ECPConnection *conn); -int ecp_conn_unregister(ECPConnection *conn, unsigned int timeout); -int ecp_conn_open(ECPConnection *conn, ECPNode *node, ECPConnHandler *handler); +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, unsigned int timeout); -int ecp_conn_hander_init(ECPConnHandler *handler); -int ecp_conn_dhkey_get_curr(ECPConnection *conn, unsigned char *idx, unsigned char *public); + +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 *proxy, 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, unsigned char mtype, unsigned char *msg, ssize_t size); +ssize_t ecp_conn_handle_kget(ECPConnection *conn, unsigned char mtype, unsigned char *msg, ssize_t size); +ssize_t ecp_conn_handle_kput(ECPConnection *conn, unsigned char mtype, unsigned char *msg, ssize_t size); + 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, unsigned char *seq, unsigned char *payload, size_t payload_size); ssize_t ecp_conn_pack(ECPConnection *conn, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size); +ssize_t ecp_proxy_pack(ECPConnection *conn, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size); +ssize_t ecp_proxy_pack_raw(ECPSocket *sock, ECPConnection *proxy, ECPNetAddr *addr, 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, unsigned char *seq, unsigned char *payload, size_t payload_size); -ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size); +ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *proxy, 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_pld_handle(ECPConnection *conn, unsigned char *payload, size_t payload_size); +ssize_t ecp_msg_handle(ECPConnection *conn, unsigned char *msg, size_t msg_size); unsigned char *ecp_pld_get_buf(unsigned char *payload); -void ecp_pld_set_type(unsigned char *payload, unsigned char ptype); +void ecp_pld_set_type(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_wkey(ECPConnection *conn, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size); -ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPNetAddr *addr, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, unsigned char *seq, unsigned char *payload, size_t payload_size); +ssize_t ecp_pld_send_raw(ECPSocket *sock, ECPConnection *proxy, ECPNetAddr *addr, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, unsigned char *seq, unsigned char *payload, size_t payload_size); -ssize_t ecp_send(ECPConnection *conn, unsigned char ptype, unsigned char *payload, size_t payload_size); -ssize_t ecp_receive(ECPConnection *conn, unsigned char ptype, unsigned char *payload, size_t payload_size, unsigned int timeout); +ssize_t ecp_send(ECPConnection *conn, unsigned char *payload, size_t payload_size); +ssize_t ecp_receive(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, unsigned int timeout); int ecp_receiver(ECPSocket *sock); int ecp_start_receiver(ECPSocket *sock); |