summaryrefslogtreecommitdiff
path: root/ecp/test/vc_common.c
blob: 0508ac1e04a229754a44df2fb4be4a8a9ebf5545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;
}