summaryrefslogtreecommitdiff
path: root/ecp/server/server.c
blob: d34fb1903446ed91c315e25a266bb05686ec37b6 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

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

#include "dir.h"
#include "vlink.h"
#include "ht.h"

#include "server.h"

static SRVConfig srv_config;

SRVConfig *srv_get_config(void) {
    return &srv_config;
}

static void usage(char *arg) {
    fprintf(stderr, "Usage: %s <capabilities> <priv> <addr> [ <pub> <addr> ]\n", arg);
    exit(1);
}

static void handle_err(ECPConnection *conn, unsigned char mtype, int err) {
    if (conn->type == ECP_CTYPE_VLINK) vlink_handle_err(conn, mtype, err);
    LOG(LOG_ERR, "handle error ctype:%d mtype:%d err:%d\n", conn->type, mtype, err);
}

static ECPConnection *conn_new(ECPSocket *sock, unsigned char type) {
    ECPConnection *conn = NULL;

    switch (type) {
        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: {
            conn = malloc(sizeof(ECPConnection));
            if (conn) ecp_vlink_init(conn, sock);
            break;
        }

        default: {
            conn = malloc(sizeof(ECPConnection));
            if (conn) ecp_conn_init(conn, sock, type);
            break;
        }
    }

    return conn;
}

static void conn_free(ECPConnection *conn) {
    free(conn);
}

int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler) {
    int rv;

    rv = ecp_ctx_init(ctx, handle_err, conn_new, conn_free, NULL);
    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;
}

int main(int argc, char *argv[]) {
    ecp_tr_addr_t addr;
    ECPContext ctx;
    ECPSocket sock;
    ECPConnHandler vconn_handler;
    ECPConnHandler vlink_handler;
    char *endptr;
    int fd;
    int rv;

    if ((argc < 4) || (argc > 6)) usage(argv[0]);

    srv_config.capabilities = (uint16_t)strtol(argv[1], &endptr, 16);
    if (endptr[0] != '\0') {
        fprintf(stderr, "Bad capabilities\n");
        exit(1);
    }

    if ((fd = open(argv[2], O_RDONLY)) < 0) {
        fprintf(stderr, "Unable to open %s\n", argv[2]);
        exit(1);
    }
    if (read(fd, &srv_config.key_perma.public, sizeof(ecp_ecdh_public_t)) != sizeof(ecp_ecdh_public_t)) {
        close(fd);
        fprintf(stderr, "Unable to read public key from %s\n", argv[2]);
        exit(1);
    }
    if (read(fd, &srv_config.key_perma.private, sizeof(ecp_ecdh_private_t)) != sizeof(ecp_ecdh_private_t)) {
        close(fd);
        fprintf(stderr, "Unable to read private key from %s\n", argv[2]);
        exit(1);
    }
    close(fd);
    srv_config.key_perma.valid = 1;

    rv = ecp_init(&ctx, &vconn_handler, &vlink_handler);
    if (rv) {
        fprintf(stderr, "ecp_init RV:%d\n", rv);
        exit(1);
    }
    rv = ecp_sock_create(&sock, &ctx, &srv_config.key_perma);
    if (rv) {
        fprintf(stderr, "ecp_sock_create RV:%d\n", rv);
        exit(1);
    }

    rv = ecp_addr_init(&addr, argv[3]);
    if (rv) {
        fprintf(stderr, "ecp_addr_init RV:%d\n", rv);
        exit(1);
    }

    rv = ecp_sock_open(&sock, &addr);
    if (rv) {
        fprintf(stderr, "ecp_sock_open RV:%d\n", rv);
        exit(1);
    }

    rv = dir_init(&ctx);
    if (rv) {
        fprintf(stderr, "dir_init RV:%d\n", rv);
        exit(1);
    }

    rv = vlink_init(&ctx);
    if (rv) {
        fprintf(stderr, "vlink_init RV:%d\n", rv);
        exit(1);
    }

    rv = dir_start_announce(&sock);
    if (rv) {
        fprintf(stderr, "dir_start_announce RV:%d\n", rv);
        exit(1);
    }

    rv = vlink_start_open(&sock);
    if (rv) {
        fprintf(stderr, "vlink_start_open RV:%d\n", rv);
        exit(1);
    }

    rv = vlink_start_keyx();
    if (rv) {
        fprintf(stderr, "vlink_start_keyx RV:%d\n", rv);
        exit(1);
    }

    if (argc == 6) {
        ECPNode node;
        ecp_ecdh_public_t node_pub;
        ecp_tr_addr_t node_addr;

        if ((fd = open(argv[4], O_RDONLY)) < 0) {
            fprintf(stderr, "Unable to open %s\n", argv[4]);
            exit(1);
        }
        if (read(fd, &node_pub, sizeof(ecp_ecdh_public_t))  != sizeof(ecp_ecdh_public_t)) {
            close(fd);
            fprintf(stderr, "Unable to read public key from %s\n", argv[4]);
            exit(1);
        }
        close(fd);

        int rv;

        ecp_node_init(&node, &node_pub, NULL);
        rv = ecp_node_set_addr(&node, argv[5]);
        if (rv) {
            fprintf(stderr, "ecp_node_set_addr RV:%d\n", rv);
            exit(1);
        }

        rv = dir_open_conn(&sock, &node);
        if (rv) {
            fprintf(stderr, "dir_open_conn RV:%d\n", rv);
            exit(1);
        }
    }

    ecp_receiver(&sock);
}