summaryrefslogtreecommitdiff
path: root/ecp/test/dir.c
blob: 9beea27438fb354a4921e3d582b66cc8bbe1b892 (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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <arpa/inet.h>

#include <ecp/core.h>
#include <ecp/dir/dir.h>
#include <ecp/dir/dir_client.h>

#include <util.h>

ECPContext ctx;
ECPSocket sock;
ECPConnection conn;
ECPConnHandler dir_handler;

#define LOG(fmt, rv)     { printf(fmt " RV:%d\n", rv); if (rv<0) exit(1); }

static void handle_err(ECPConnection *conn, unsigned char mtype, int err) {
    printf("ERROR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err);
}

static void print_list(ECPConnection *conn, void *list, int err) {
    ECPDirList *dir_list = list;
    unsigned char *host;
    uint16_t port;
    int i;

    LOG("print_list", err);

    printf("DIR LIST COUNT:%d\n", dir_list->count);
    for (i=0; i<dir_list->count; i++) {
        host = dir_list->items[i].node.addr.host;
        port = dir_list->items[i].node.addr.port;

        printf("ADDR: %d.%d.%d.%d:%d\n", host[0], host[1], host[2], host[3], ntohs(port));
    }
    ecp_dir_list_destroy(dir_list);
}

static void usage(char *arg) {
    fprintf(stderr, "Usage: %s <address> <dir.pub>\n", arg);
    exit(1);
}

int main(int argc, char *argv[]) {
    ecp_tr_addr_t addr;
    ecp_ecdh_public_t public;
    int rv;

    if (argc != 3) usage(argv[0]);

    rv = ecp_ctx_init(&ctx, NULL, NULL, NULL, handle_err, printf);
    LOG("ecp_ctx_init", rv);

    rv = ecp_dir_set_handler(&ctx, &dir_handler, print_list);
    LOG("ecp_dir_set_handler", rv);

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

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

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

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

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

    rv = ecp_dir_get(&conn, &sock, &public, &addr, 0);
    LOG("ecp_dir_get", rv);

    while(1) pause();
}