From 810dde21ee65653c15606917b19566cfbaaf165e Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Tue, 9 Aug 2022 21:54:45 +0200 Subject: ecp server added --- ecp/test/Makefile | 14 ++- ecp/test/basic.c | 23 +++-- ecp/test/client.c | 15 +-- ecp/test/init.c | 19 ++-- ecp/test/init.h | 1 + ecp/test/init_vconn.c | 55 ++++++----- ecp/test/init_vconn.h | 1 + ecp/test/server.c | 12 ++- ecp/test/stress.c | 256 ------------------------------------------------- ecp/test/vc_client.c | 37 +++---- ecp/test/vc_client_t.c | 118 ----------------------- ecp/test/vc_common.c | 127 ++++++++++++++++++++++++ ecp/test/vc_common.h | 2 + ecp/test/vc_inb.c | 124 ++++++++++++++++++++++++ ecp/test/vc_outb.c | 128 +++++++++++++++++++++++++ ecp/test/vc_server.c | 44 +++++---- ecp/test/vcs.c | 25 +++-- 17 files changed, 522 insertions(+), 479 deletions(-) create mode 100644 ecp/test/init.h create mode 100644 ecp/test/init_vconn.h delete mode 100644 ecp/test/stress.c delete mode 100644 ecp/test/vc_client_t.c create mode 100644 ecp/test/vc_common.c create mode 100644 ecp/test/vc_common.h create mode 100644 ecp/test/vc_inb.c create mode 100644 ecp/test/vc_outb.c (limited to 'ecp/test') diff --git a/ecp/test/Makefile b/ecp/test/Makefile index 50a4202..b2fd6f8 100644 --- a/ecp/test/Makefile +++ b/ecp/test/Makefile @@ -1,13 +1,15 @@ src_dir = ../src include $(src_dir)/ecp/common.mk CFLAGS += -I../util -Wno-int-to-void-pointer-cast +dep = ../build-posix/*.a ../util/libecputil.a + +target = basic client server vc_inb vc_outb vcs vc_server vc_client -dep=../build-posix/*.a ../util/libecputil.a %.o: %.c $(CC) $(CFLAGS) -c $< -all: basic client server vcs vc_server vc_client +all: $(target) basic: basic.o init.o $(dep) $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) @@ -18,6 +20,12 @@ client: client.o init.o $(dep) server: server.o init.o $(dep) $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) +vc_inb: vc_inb.o vc_common.o init_vconn.o $(dep) + $(CC) -o $@ $< vc_common.o init_vconn.o $(dep) $(LDFLAGS) + +vc_outb: vc_outb.o vc_common.o init_vconn.o $(dep) + $(CC) -o $@ $< vc_common.o init_vconn.o $(dep) $(LDFLAGS) + vcs: vcs.o init_vconn.o $(dep) $(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) @@ -29,4 +37,4 @@ vc_client: vc_client.o init_vconn.o $(dep) clean: rm -f *.o - rm -f basic dir client server echo frag stress vcs vc_server vc_client vc_client_t voip + rm -f $(target) diff --git a/ecp/test/basic.c b/ecp/test/basic.c index b266a07..637a829 100644 --- a/ecp/test/basic.c +++ b/ecp/test/basic.c @@ -2,7 +2,9 @@ #include #include -#include +#include + +#include "init.h" ECPContext ctx_s; ECPSocket sock_s; @@ -43,6 +45,7 @@ static ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t seq, unsigned char mt } int main(int argc, char *argv[]) { + ecp_tr_addr_t addr; ECPDHKey key_perma_c; ECPDHKey key_perma_s; ECPNode node; @@ -53,7 +56,7 @@ int main(int argc, char *argv[]) { printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler_s, handle_msg_s, NULL, NULL, NULL); - ecp_ctx_set_handler(&ctx_s, &handler_s, CTYPE_TEST); + ecp_ctx_set_handler(&ctx_s, CTYPE_TEST, &handler_s); rv = ecp_dhkey_gen(&key_perma_s); printf("ecp_dhkey_gen RV:%d\n", rv); @@ -61,7 +64,10 @@ int main(int argc, char *argv[]) { rv = ecp_sock_create(&sock_s, &ctx_s, &key_perma_s); printf("ecp_sock_create RV:%d\n", rv); - rv = ecp_sock_open(&sock_s, "0.0.0.0:3000"); + rv = ecp_addr_init(&addr, "0.0.0.0:3000"); + printf("ecp_addr_init RV:%d\n", rv); + + rv = ecp_sock_open(&sock_s, &addr); printf("ecp_sock_open RV:%d\n", rv); rv = ecp_start_receiver(&sock_s); @@ -72,7 +78,7 @@ int main(int argc, char *argv[]) { printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler_c, handle_msg_c, handle_open_c, NULL, NULL); - ecp_ctx_set_handler(&ctx_c, &handler_c, CTYPE_TEST); + ecp_ctx_set_handler(&ctx_c, CTYPE_TEST, &handler_c); rv = ecp_dhkey_gen(&key_perma_c); printf("ecp_dhkey_gen RV:%d\n", rv); @@ -86,12 +92,11 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock_c); printf("ecp_start_receiver RV:%d\n", rv); - rv = ecp_node_init(&node, &key_perma_s.public, "127.0.0.1:3000"); - printf("ecp_node_init RV:%d\n", rv); - - rv = ecp_conn_create(&conn, &sock_c, CTYPE_TEST); - printf("ecp_conn_create RV:%d\n", rv); + ecp_node_init(&node, &key_perma_s.public, NULL); + rv = ecp_node_set_addr(&node, "127.0.0.1:3000"); + printf("ecp_node_set_addr RV:%d\n", rv); + ecp_conn_init(&conn, &sock_c, CTYPE_TEST); rv = ecp_conn_open(&conn, &node); printf("ecp_conn_open RV:%d\n", rv); diff --git a/ecp/test/client.c b/ecp/test/client.c index d526f76..52a8587 100644 --- a/ecp/test/client.c +++ b/ecp/test/client.c @@ -3,10 +3,12 @@ #include #include -#include +#include #include +#include "init.h" + ECPContext ctx; ECPSocket sock; ECPConnHandler handler; @@ -48,7 +50,7 @@ int main(int argc, char *argv[]) { printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); - ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_dhkey_gen(&key_perma); printf("ecp_dhkey_gen RV:%d\n", rv); @@ -62,15 +64,14 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); printf("ecp_start_receiver RV:%d\n", rv); - rv = ecp_conn_create(&conn, &sock, CTYPE_TEST); - printf("ecp_conn_create RV:%d\n", rv); - rv = ecp_util_load_pub(&node_pub, argv[2]); printf("ecp_util_load_pub RV:%d\n", rv); - rv = ecp_node_init(&node, &node_pub, argv[1]); - printf("ecp_node_init RV:%d\n", rv); + ecp_node_init(&node, &node_pub, NULL); + rv = ecp_node_set_addr(&node, argv[1]); + printf("ecp_node_set_addr RV:%d\n", rv); + ecp_conn_init(&conn, &sock, CTYPE_TEST); rv = ecp_conn_open(&conn, &node); printf("ecp_conn_open RV:%d\n", rv); diff --git a/ecp/test/init.c b/ecp/test/init.c index f4b033a..b75b2ee 100644 --- a/ecp/test/init.c +++ b/ecp/test/init.c @@ -1,34 +1,27 @@ #include #include -#include +#include -void handle_err(ECPConnection *conn, unsigned char mtype, int err) { +static void handle_err(ECPConnection *conn, unsigned char mtype, int err) { printf("ERR: CTYPE:%d MTYPE:%x ERR:%d\n", conn->type, mtype, err); } -static ECPConnection *conn_alloc(ECPSocket *sock, unsigned char type) { +static ECPConnection *conn_new(ECPSocket *sock, unsigned char type) { ECPConnection *conn; - int rv; conn = malloc(sizeof(ECPConnection)); - if (conn == NULL) return NULL; - - rv = ecp_conn_create_inb(conn, sock, type); - if (rv) { - free(conn); - return NULL; - } + if (conn) ecp_conn_init(conn, sock, type); return conn; } static void conn_free(ECPConnection *conn) { - free(conn); + if (ecp_conn_is_gc(conn)) free(conn); } int ecp_init(ECPContext *ctx) { int rv; - rv = ecp_ctx_init(ctx, handle_err, NULL, conn_alloc, conn_free); + rv = ecp_ctx_init(ctx, handle_err, conn_new, conn_free); return rv; } diff --git a/ecp/test/init.h b/ecp/test/init.h new file mode 100644 index 0000000..a1a21d1 --- /dev/null +++ b/ecp/test/init.h @@ -0,0 +1 @@ +int ecp_init(ECPContext *ctx); \ No newline at end of file diff --git a/ecp/test/init_vconn.c b/ecp/test/init_vconn.c index 88b6fa0..b690bec 100644 --- a/ecp/test/init_vconn.c +++ b/ecp/test/init_vconn.c @@ -2,50 +2,59 @@ #include #include -#include -#include +#include +#include -void handle_err(ECPConnection *conn, unsigned char mtype, int err) { +static void handle_err(ECPConnection *conn, unsigned char mtype, int err) { printf("ERR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err); } -static ECPConnection *conn_alloc(ECPSocket *sock, unsigned char type) { - ECPConnection *conn; - int rv; +static ECPConnection *conn_new(ECPSocket *sock, unsigned char type) { + ECPConnection *conn = NULL; switch (type) { - case ECP_CTYPE_VCONN: - conn = malloc(sizeof(ECPVConn)); - if (conn) rv = ecp_vconn_create_inb((ECPVConn *)conn, sock); + case ECP_CTYPE_VCONN: { + ECPVConnInb *_conn; + + _conn = malloc(sizeof(ECPVConnInb)); + if (_conn) { + ecp_vconn_init_inb(_conn, sock); + conn = &_conn->b; + } break; + } - case ECP_CTYPE_VLINK: + case ECP_CTYPE_VLINK: { conn = malloc(sizeof(ECPConnection)); - if (conn) rv = ecp_vlink_create_inb(conn, sock); + if (conn) ecp_vlink_init(conn, sock); break; + } - default: + default: { conn = malloc(sizeof(ECPConnection)); - if (conn) rv = ecp_conn_create_inb(conn, sock, type); + if (conn) ecp_conn_init(conn, sock, type); break; - } - - if (conn == NULL) return NULL; - if (rv) { - free(conn); - return NULL; + } } return conn; } static void conn_free(ECPConnection *conn) { - free(conn); + if (ecp_conn_is_gc(conn)) free(conn); } -int ecp_init(ECPContext *ctx) { +int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler) { int rv; - rv = ecp_ctx_init(ctx, handle_err, NULL, conn_alloc, conn_free); - return rv; + rv = ecp_ctx_init(ctx, handle_err, conn_new, conn_free); + if (rv) return rv; + + rv = ecp_vconn_handler_init(ctx, vconn_handler); + if (rv) return rv; + + rv = ecp_vlink_handler_init(ctx, vlink_handler); + if (rv) return rv; + + return ECP_OK; } \ No newline at end of file diff --git a/ecp/test/init_vconn.h b/ecp/test/init_vconn.h new file mode 100644 index 0000000..64e3269 --- /dev/null +++ b/ecp/test/init_vconn.h @@ -0,0 +1 @@ +int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler); \ No newline at end of file diff --git a/ecp/test/server.c b/ecp/test/server.c index 228dbea..19b2f4f 100644 --- a/ecp/test/server.c +++ b/ecp/test/server.c @@ -3,10 +3,12 @@ #include #include -#include +#include #include +#include "init.h" + ECPContext ctx; ECPSocket sock; ECPDHKey key_perma; @@ -31,6 +33,7 @@ static void usage(char *arg) { } int main(int argc, char *argv[]) { + ecp_tr_addr_t addr; ECPDHKey key_perma; int rv; @@ -41,7 +44,7 @@ int main(int argc, char *argv[]) { printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler, handle_msg, NULL, NULL, NULL); - ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[2]); printf("ecp_util_load_key RV:%d\n", rv); @@ -50,7 +53,10 @@ int main(int argc, char *argv[]) { rv = ecp_sock_create(&sock, &ctx, &key_perma); printf("ecp_sock_create RV:%d\n", rv); - rv = ecp_sock_open(&sock, argv[1]); + rv = ecp_addr_init(&addr, argv[1]); + printf("ecp_addr_init RV:%d\n", rv); + + rv = ecp_sock_open(&sock, &addr); printf("ecp_sock_open RV:%d\n", rv); rv = ecp_start_receiver(&sock); diff --git a/ecp/test/stress.c b/ecp/test/stress.c deleted file mode 100644 index 6eed851..0000000 --- a/ecp/test/stress.c +++ /dev/null @@ -1,256 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "core.h" - -#ifdef __linux__ -#define SIGINFO SIGTSTP -#endif - -#define NUM_S 32 -#define NUM_C 256 -#define MSG_RATE 65 - -#define CTYPE_TEST 0 -#define MTYPE_MSG 8 - -ECPContext ctx_s; -ECPDHKey key_perma_s; -ECPConnHandler handler_s; -ECPSocket *sock_s; - -ECPConnHandler handler_c; -ECPContext *ctx_c; -ECPSocket *sock_c; -ECPDHKey *key_perma_c; - -ECPNode *node; -ECPConnection *conn; - -pthread_t *s_thd; -pthread_t *r_thd; -pthread_mutex_t *t_mtx; -int *t_sent, *t_rcvd; - -uint64_t t_start = 0, t_end = 0; -int c_start = 0; - -int num_s = NUM_S, num_c = NUM_C; -int msg_rate = MSG_RATE; - -static void display(void) { - int i, s = 0, r = 0; - - for (i=0; iconn_data); - 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) { - uint32_t rnd; - c->sock->ctx->rng(&rnd, sizeof(uint32_t)); - usleep(rnd % (2000000/msg_rate)); - - ecp_pld_set_type(payload.buffer, payload.size, MTYPE_MSG); - ssize_t _rv = 0; - // XXX refactor - // _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]++; - pthread_mutex_unlock(&t_mtx[idx]); - } - } -} - -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, b); - rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)conn); - if (rv) { - char msg[256]; - sprintf(msg, "THD %d CREATE\n", idx); - perror(msg); - exit(1); - } - return 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); - 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]); - t_rcvd[idx]++; - pthread_mutex_unlock(&t_mtx[idx]); - } - - // ecp_pld_set_type(payload.buffer, payload.size, MTYPE_MSG); - // 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, 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(payload.buffer, payload.size, MTYPE_MSG); - // XXX refactor - // ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(1000, 0)); - return s; -} - -int main(int argc, char *argv[]) { - char addr[256]; - int rv; - int i; - - ECPConnHandler handler_c; - - ECPContext *ctx_c; - ECPSocket *sock_c; - ECPDHKey *key_perma_c; - - ECPNode *node; - ECPConnection *conn; - - sock_s = malloc(num_s * sizeof(ECPSocket)); - ctx_c = malloc(num_c * sizeof(ECPContext)); - sock_c = malloc(num_c * sizeof(ECPSocket)); - key_perma_c = malloc(num_c * sizeof(ECPDHKey)); - node = malloc(num_c * sizeof(ECPNode)); - conn = malloc(num_c * sizeof(ECPConnection)); - - s_thd = malloc(num_c * sizeof(pthread_t)); - r_thd = malloc(num_c * sizeof(pthread_t)); - t_mtx = malloc(num_c * sizeof(pthread_mutex_t)); - t_sent = malloc(num_c * sizeof(int)); - t_rcvd = malloc(num_c * sizeof(int)); - memset(t_rcvd, 0, num_c * sizeof(int)); - memset(t_sent, 0, num_c * sizeof(int)); - - struct sigaction actINFO; - memset(&actINFO, 0, sizeof(actINFO)); - actINFO.sa_handler = &catchINFO; - sigaction(SIGINFO, &actINFO, NULL); - - rv = ecp_init(&ctx_s); - if (!rv) rv = ecp_conn_handler_init(&handler_s); - handler_s.msg[MTYPE_MSG] = handle_msg_s; - ctx_s.handler[CTYPE_TEST] = &handler_s; - - if (!rv) rv = ecp_dhkey_gen(&ctx_s, &key_perma_s); - - for (i=0; i #include -#include -#include +#include +#include #include +#include "init_vconn.h" + ECPContext ctx; ECPSocket sock; ECPConnHandler handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; ECPConnection conn; -ECPConnection vconn[3]; +ECPVConnOutb vconn[3]; #define CTYPE_TEST 0 #define MTYPE_MSG 0 @@ -47,11 +51,11 @@ int main(int argc, char *argv[]) { if ((argc < 4) || (argc > 6)) usage(argv[0]); - rv = ecp_init(&ctx); + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); - ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_dhkey_gen(&key_perma); printf("ecp_dhkey_gen RV:%d\n", rv); @@ -65,24 +69,21 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); printf("ecp_start_receiver RV:%d\n", rv); + rv = ecp_util_load_pub(&node_pub, argv[1]); + printf("ecp_util_load_pub RV:%d\n", rv); + + ecp_node_init(&node, &node_pub, NULL); + rv = ecp_node_set_addr(&node, argv[2]); + printf("ecp_node_set_addr RV:%d\n", rv); + for (i=3; i -#include -#include -#include -#include - -#include "core.h" -#include "vconn/vconn.h" -#include "util.h" - -ECPContext ctx; -ECPSocket sock; -ECPConnHandler handler; - -ECPNode node; -ECPConnection conn; - -ECPVConnOut vconn[20]; -ECPNode vconn_node[20]; - -#define CTYPE_TEST 0 -#define MTYPE_MSG 8 - - -int counter = 0; -uint64_t t_start = 0; -uint64_t t_end = 0; -ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { - ssize_t rv = ecp_conn_handle_open(conn, sq, t, p, s, b); - if (rv < 0) { - printf("OPEN ERR:%ld\n", s); - return rv; - } - - printf("OPEN\n"); - char *msg = "PERA JE CAR!"; - unsigned char buf[1000]; - - strcpy((char *)buf, msg); - // ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); - - struct timeval tv; - gettimeofday(&tv, NULL); - t_start = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; - - return rv; -} - -ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) { - ecp_send(conn, t, p, s); - write(2, p+1, s-1); - fflush(stderr); - // printf("MSG C:%s size:%ld\n", p, s); - return s; - - counter++; - char *msg = "PERA JE CAR!"; - unsigned char buf[1000]; - - strcpy((char *)buf, msg); - ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000); - - if (counter % 100 == 0) { - struct timeval tv; - uint64_t t_time; - - gettimeofday(&tv, NULL); - t_end = tv.tv_sec*(uint64_t)1000000+tv.tv_usec; - t_time = t_end - t_start; - printf("T:%f\n", (float)t_time/1000000); - t_start = t_end; - } - return s; -} - -static void usage(char *arg) { - fprintf(stderr, "Usage: %s ... \n", arg); - exit(1); -} - -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_init(&sock, &ctx, NULL); - printf("ecp_sock_init 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); - - rv = ecp_util_node_load(&ctx, &node, argv[1]); - printf("ecp_util_node_load RV:%d\n", rv); - - for (i=0; i +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include "vc_common.h" + +#define VCONN_NODES 3 + +#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) +#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) + +static void msg_remove_item(unsigned char *msg, uint16_t i, uint16_t c) { + if (c - i - 1) memmove(msg + i * ECP_SIZE_DIR_ITEM, msg + (i + 1) * ECP_SIZE_DIR_ITEM, (c - i - 1) * ECP_SIZE_DIR_ITEM); +} + +static void hrw_select(unsigned char *msg, uint16_t count, ecp_ecdh_public_t *pub, ecp_ecdh_public_t *hrw_pub, ecp_ecdh_public_t vconn_pub[], size_t *_vconn_size, ecp_tr_addr_t *addr) { + unsigned char *_msg = msg; + unsigned char hash[SHA_DIGEST_LENGTH]; + unsigned char hrw_hash[SHA_DIGEST_LENGTH]; + ecp_ecdh_public_t hash_pub[2]; + size_t vconn_size = *_vconn_size; + ECPDirItem dir_item; + SHA_CTX ctx; + int i, hrw_i; + + if (count == 0) return; + + memset(hrw_hash, 0, sizeof(hrw_hash)); + memcpy(&hash_pub[0], pub, sizeof(ecp_ecdh_public_t)); + + for (i=0; isock); + ecp_node_init(&node, &hrw_pub, &addr); + rv = ecp_vconn_open(vconn, vlink, &node); + printf("ecp_vconn_open RV:%d\n", rv); + + return ECP_OK; +} + +int vc_open_outb(ECPConnection *conn, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *remote_pub) { + ecp_ecdh_public_t vconn_pub[VCONN_NODES]; + ecp_ecdh_public_t hrw_pub; + ecp_tr_addr_t addr; + ECPNode node; + ECPVConnOutb *vconn; + size_t vconn_size; + int rv; + + vconn_size = VCONN_NODES - 1; + hrw_select(msg, count, remote_pub, &hrw_pub, vconn_pub, &vconn_size, &addr); + memcpy(&vconn_pub[vconn_size], &hrw_pub, sizeof(ecp_ecdh_public_t)); + vconn_size++; + + vconn = malloc(sizeof(ECPVConnOutb) * vconn_size); + if (vconn == NULL) return ECP_ERR_ALLOC; + + ecp_vconn_init(vconn, vconn_pub, vconn_size, conn->sock); + ecp_node_init(&node, remote_pub, &addr); + rv = ecp_vconn_open(vconn, conn, &node); + printf("ecp_vconn_open RV:%d\n", rv); + + return ECP_OK; +} diff --git a/ecp/test/vc_common.h b/ecp/test/vc_common.h new file mode 100644 index 0000000..a684566 --- /dev/null +++ b/ecp/test/vc_common.h @@ -0,0 +1,2 @@ +int vc_open_inb(ECPConnection *vlink, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *local_pub); +int vc_open_outb(ECPConnection *conn, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *remote_pub); diff --git a/ecp/test/vc_inb.c b/ecp/test/vc_inb.c new file mode 100644 index 0000000..13b493c --- /dev/null +++ b/ecp/test/vc_inb.c @@ -0,0 +1,124 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "init_vconn.h" +#include "vc_common.h" + +ECPContext ctx; +ECPSocket sock; +ECPConnHandler handler; +ECPConnHandler dir_handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 0 + +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { + printf("OPEN\n"); + + return ECP_OK; +} + +static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { + char *_msg = "VAISTINU JE CAR!"; + ssize_t rv; + + printf("MSG:%s size:%ld\n", msg, msg_size); + rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); + + return msg_size; +} + +static ssize_t handle_dir_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { + size_t rsize; + uint16_t count; + ECPConnection *vlink; + int rv; + + if (msg_size < sizeof(uint16_t)) return ECP_ERR_SIZE; + + count = \ + ((uint16_t)msg[0] << 8) | \ + ((uint16_t)msg[1]); + + printf("DIR MSG:%d\n", count); + + rsize = sizeof(uint16_t) + count * ECP_SIZE_DIR_ITEM; + if (msg_size < rsize) return ECP_ERR_SIZE; + + msg += sizeof(uint16_t); + + vlink = malloc(sizeof(ECPConnection)); + if (vlink == NULL) return ECP_ERR_ALLOC; + + ecp_vlink_init(vlink, conn->sock); + rv = vc_open_inb(vlink, msg, count, &conn->sock->key_perma.public); + if (rv) { + free(vlink); + return rv; + } + + return rsize; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s \n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + ECPConnection *conn_dir; + ECPNode node_dir; + ecp_ecdh_public_t dir_pub; + ECPDHKey key_perma; + int rv; + + if (argc != 4) usage(argv[0]); + + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); + printf("ecp_init RV:%d\n", rv); + + ecp_conn_handler_init(&dir_handler, handle_dir_msg, ecp_dir_handle_open, NULL, NULL); + ecp_ctx_set_handler(&ctx, ECP_CTYPE_DIR, &dir_handler); + + ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); + + rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[1]); + printf("ecp_util_load_key RV:%d\n", rv); + key_perma.valid = 1; + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + 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); + + rv = ecp_util_load_pub(&dir_pub, argv[2]); + printf("ecp_util_load_pub RV:%d\n", rv); + + ecp_node_init(&node_dir, &dir_pub, NULL); + rv = ecp_node_set_addr(&node_dir, argv[3]); + printf("ecp_node_set_addr RV:%d\n", rv); + + conn_dir = malloc(sizeof(ECPConnection)); + if (conn_dir == NULL) printf("out of memory\n"); + + ecp_dir_conn_init(conn_dir, &sock); + rv = ecp_conn_open(conn_dir, &node_dir); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +} diff --git a/ecp/test/vc_outb.c b/ecp/test/vc_outb.c new file mode 100644 index 0000000..6018602 --- /dev/null +++ b/ecp/test/vc_outb.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "init_vconn.h" +#include "vc_common.h" + +ECPContext ctx; +ECPSocket sock; +ECPConnHandler handler; +ECPConnHandler dir_handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; +ecp_ecdh_public_t remote_pub; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 0 + +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { + char *_msg = "PERA JE CAR!"; + ssize_t rv; + + printf("OPEN\n"); + rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); + + return ECP_OK; +} + +static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { + printf("MSG:%s size:%ld\n", msg, msg_size); + + return msg_size; +} + +static ssize_t handle_dir_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { + size_t rsize; + uint16_t count; + ECPConnection *_conn; + int rv; + + if (msg_size < sizeof(uint16_t)) return ECP_ERR_SIZE; + + count = \ + ((uint16_t)msg[0] << 8) | \ + ((uint16_t)msg[1]); + + printf("DIR MSG:%d\n", count); + + rsize = sizeof(uint16_t) + count * ECP_SIZE_DIR_ITEM; + if (msg_size < rsize) return ECP_ERR_SIZE; + + msg += sizeof(uint16_t); + + _conn = malloc(sizeof(ECPConnection)); + if (_conn == NULL) return ECP_ERR_ALLOC; + + ecp_conn_init(_conn, conn->sock, CTYPE_TEST); + rv = vc_open_outb(_conn, msg, count, &remote_pub); + if (rv) { + printf("vc_open_outb RV:%d\n", rv); + free(_conn); + return rv; + } + + return rsize; +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s \n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + ECPConnection *conn_dir; + ECPNode node_dir; + ecp_ecdh_public_t dir_pub; + ECPDHKey key_perma; + int rv; + + if (argc != 4) usage(argv[0]); + + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); + printf("ecp_init RV:%d\n", rv); + + ecp_conn_handler_init(&dir_handler, handle_dir_msg, ecp_dir_handle_open, NULL, NULL); + ecp_ctx_set_handler(&ctx, ECP_CTYPE_DIR, &dir_handler); + + ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); + + rv = ecp_dhkey_gen(&key_perma); + printf("ecp_dhkey_gen RV:%d\n", rv); + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + 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); + + rv = ecp_util_load_pub(&remote_pub, argv[1]); + printf("ecp_util_load_pub RV:%d\n", rv); + + rv = ecp_util_load_pub(&dir_pub, argv[2]); + printf("ecp_util_load_pub RV:%d\n", rv); + + ecp_node_init(&node_dir, &dir_pub, NULL); + rv = ecp_node_set_addr(&node_dir, argv[3]); + printf("ecp_node_set_addr RV:%d\n", rv); + + conn_dir = malloc(sizeof(ECPConnection)); + if (conn_dir == NULL) printf("out of memory\n"); + + ecp_dir_conn_init(conn_dir, &sock); + rv = ecp_conn_open(conn_dir, &node_dir); + printf("ecp_conn_open RV:%d\n", rv); + + while (1) sleep(1); +} diff --git a/ecp/test/vc_server.c b/ecp/test/vc_server.c index de7f275..548b52d 100644 --- a/ecp/test/vc_server.c +++ b/ecp/test/vc_server.c @@ -3,16 +3,20 @@ #include #include -#include -#include +#include +#include #include +#include "init_vconn.h" + ECPContext ctx; ECPSocket sock; ECPConnHandler handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; ECPConnection conn; -ECPConnection vconn[3]; +ECPVConnOutb vconn[3]; #define CTYPE_TEST 0 #define MTYPE_MSG 0 @@ -46,11 +50,11 @@ int main(int argc, char *argv[]) { if ((argc < 4) || (argc > 7)) usage(argv[0]); - rv = ecp_init(&ctx); + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); ecp_conn_handler_init(&handler, handle_msg, handle_open, NULL, NULL); - ecp_ctx_set_handler(&ctx, &handler, CTYPE_TEST); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[1]); printf("ecp_util_load_key RV:%d\n", rv); @@ -65,29 +69,29 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); printf("ecp_start_receiver RV:%d\n", rv); - rv = ecp_vlink_create(&conn, &sock); - printf("ecp_vlink_create RV:%d\n", rv); + rv = ecp_util_load_pub(&node_pub, argv[argc-1]); + printf("ecp_util_load_pub RV:%d\n", rv); + ecp_node_init(&node, &node_pub, NULL); + rv = ecp_node_set_addr(&node, argv[2]); + printf("ecp_node_set_addr RV:%d\n", rv); + + ecp_vlink_init(&conn, &sock); if (argc > 4) { - ecp_ecdh_public_t vconn_key[3]; + ecp_ecdh_public_t vconn_pub[3]; for (i=3; i #include -#include -#include +#include +#include #include +#include "init_vconn.h" + ECPContext ctx; ECPSocket sock; ECPConnection conn; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; static void usage(char *arg) { fprintf(stderr, "Usage: %s [ ]\n", arg); @@ -18,12 +22,13 @@ static void usage(char *arg) { } int main(int argc, char *argv[]) { + ecp_tr_addr_t addr; ECPDHKey key_perma; int rv; if ((argc < 3) || (argc > 5)) usage(argv[0]); - rv = ecp_init(&ctx); + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); printf("ecp_init RV:%d\n", rv); rv = ecp_util_load_key(&key_perma.public, &key_perma.private, argv[2]); @@ -33,7 +38,10 @@ int main(int argc, char *argv[]) { rv = ecp_sock_create(&sock, &ctx, &key_perma); printf("ecp_sock_create RV:%d\n", rv); - rv = ecp_sock_open(&sock, argv[1]); + rv = ecp_addr_init(&addr, argv[1]); + printf("ecp_addr_init RV:%d\n", rv); + + rv = ecp_sock_open(&sock, &addr); printf("ecp_sock_open RV:%d\n", rv); rv = ecp_start_receiver(&sock); @@ -43,15 +51,14 @@ int main(int argc, char *argv[]) { ECPNode node; ecp_ecdh_public_t node_pub; - rv = ecp_vlink_create(&conn, &sock); - printf("ecp_vlink_create RV:%d\n", rv); - rv = ecp_util_load_pub(&node_pub, argv[4]); printf("ecp_util_load_pub RV:%d\n", rv); - rv = ecp_node_init(&node, &node_pub, argv[3]); - printf("ecp_node_init RV:%d\n", rv); + ecp_node_init(&node, &node_pub, NULL); + rv = ecp_node_set_addr(&node, argv[3]); + printf("ecp_node_set_addr RV:%d\n", rv); + ecp_vlink_init(&conn, &sock); rv = ecp_conn_open(&conn, &node); printf("ecp_conn_open RV:%d\n", rv); } -- cgit v1.2.3