summaryrefslogtreecommitdiff
path: root/ecp/test/vcs.c
blob: 48bab41bc61693a9f039cbe3443b60cc2b746b3f (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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>

#include <ecp/core.h>
#include <ecp/vconn/vconn.h>

#include <util.h>

#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 <my addr> <my.priv> [ <node addr> <node.pub> ]\n", arg);
    exit(1);
}

static int vlink_handle_open(ECPConnection *_conn, ECP2Buffer *bufs) {
    int rv;

    /* open return connection */
    if ((_conn->parent == NULL) && ecp_conn_is_inb(_conn) && (memcmp(&_conn->remote.key_perma.public, &conn.remote.key_perma.public, sizeof(conn.remote.key_perma.public)) != 0)) {
        ECPConnection *conn_r;

        conn_r = malloc(sizeof(ECPConnection));
        if (conn_r == NULL) return ECP_ERR_ALLOC;

        ecp_vlink_init(conn_r, _conn->sock);
        rv = ecp_conn_open(conn_r, &_conn->remote.key_perma.public, &_conn->remote.addr);
        printf("open return connection: ecp_conn_open RV:%d\n", rv);
        if (rv) return rv;
    }

    rv = ecp_vlink_handle_open(_conn, bufs);
    return rv;
}

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, &vconn_handler, &vlink_handler);
    LOG("ecp_init", rv);
    vlink_handler.handle_open = vlink_handle_open;

    rv = ecp_util_load_key(argv[2], &key_perma.public, &key_perma.private);
    LOG("ecp_util_load_key", rv);
    key_perma.valid = 1;

    rv = ecp_sock_create(&sock, &ctx, &key_perma);
    LOG("ecp_sock_create", rv);

    rv = ecp_vconn_sock_create(&sock);
    LOG("ecp_vconn_sock_create", rv);

    rv = ecp_addr_init(&addr, argv[1]);
    LOG("ecp_addr_init", rv);

    rv = ecp_sock_open(&sock, &addr);
    LOG("ecp_sock_open", rv);

    rv = ecp_start_receiver(&sock);
    LOG("ecp_start_receiver", rv);

    if (argc == 5) {
        ecp_ecdh_public_t public;

        rv = ecp_addr_init(&addr, argv[3]);
        LOG("ecp_addr_init", rv);

        rv = ecp_util_load_key(argv[4], &public, NULL);
        LOG("ecp_util_load_key", rv);

        ecp_vlink_init(&conn, &sock);
        rv = ecp_conn_open(&conn, &public, &addr);
        LOG("ecp_conn_open", rv);
    } else {
        memset(&conn, 0, sizeof(conn));
    }

    while (1) sleep(1);
}