summaryrefslogtreecommitdiff
path: root/ecp/test/vc_common.c
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2022-08-09 21:54:45 +0200
committerUros Majstorovic <majstor@majstor.org>2022-08-09 21:54:45 +0200
commit810dde21ee65653c15606917b19566cfbaaf165e (patch)
tree4cd84b109e06660a9c59f2487822905e5672681e /ecp/test/vc_common.c
parentaee853a208d6abec53ec81dc4ef110b63e13342f (diff)
ecp server added
Diffstat (limited to 'ecp/test/vc_common.c')
-rw-r--r--ecp/test/vc_common.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/ecp/test/vc_common.c b/ecp/test/vc_common.c
new file mode 100644
index 0000000..0508ac1
--- /dev/null
+++ b/ecp/test/vc_common.c
@@ -0,0 +1,127 @@
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include <ecp/core.h>
+#include <ecp/dir/dir.h>
+#include <ecp/vconn/vconn.h>
+
+#include <openssl/crypto.h>
+#include <openssl/sha.h>
+
+#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; i<count; i++) {
+ unsigned char *host;
+ uint16_t port;
+ size_t rv;
+
+ rv = ecp_dir_item_parse(&dir_item, _msg);
+ _msg += rv;
+
+ host = dir_item.node.addr.host;
+ port = dir_item.node.addr.port;
+ printf("ITEM: %d.%d.%d.%d:%d\n", host[0], host[1], host[2], host[3], ntohs(port));
+
+ memcpy(&hash_pub[1], &dir_item.node.key_perma.public, sizeof(ecp_ecdh_public_t));
+ SHA1_Init(&ctx);
+ SHA1_Update(&ctx, hash_pub, sizeof(hash_pub));
+ SHA1_Final(hash, &ctx);
+ if (memcmp(hrw_hash, hash, sizeof(hash)) < 0) {
+ hrw_i = i;
+ memcpy(hrw_hash, hash, sizeof(hash));
+ memcpy(hrw_pub, &dir_item.node.key_perma, sizeof(ecp_ecdh_public_t));
+ }
+ }
+
+ printf("SELECTED: %d\n", hrw_i);
+ msg_remove_item(msg, hrw_i, count);
+ count--;
+
+ vconn_size = MIN(count, vconn_size);
+ for (i=0; i<vconn_size; i++) {
+ uint16_t s;
+
+ s = arc4random() % count;
+ _msg = msg + s * ECP_SIZE_DIR_ITEM;
+ ecp_dir_item_parse(&dir_item, _msg);
+ memcpy(&vconn_pub[i], &dir_item.node.key_perma, sizeof(ecp_ecdh_public_t));
+ if (i==0) *addr = dir_item.node.addr;
+ msg_remove_item(msg, s, count);
+ count--;
+ }
+ *_vconn_size = vconn_size;
+}
+
+int vc_open_inb(ECPConnection *vlink, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *local_pub) {
+ ecp_ecdh_public_t vconn_pub[VCONN_NODES - 1];
+ 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, local_pub, &hrw_pub, vconn_pub, &vconn_size, &addr);
+
+ vconn = malloc(sizeof(ECPVConnOutb) * vconn_size);
+ if (vconn == NULL) return ECP_ERR_ALLOC;
+
+ ecp_vconn_init(vconn, vconn_pub, vconn_size, vlink->sock);
+ 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;
+}