summaryrefslogtreecommitdiff
path: root/ecp/server/server.c
blob: 890d468879bbf33e49749aa6c721b27b82e80153 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>

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

#include <util.h>

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

#include "server.h"

static SRVConfig srv_config;
static const char *srv_llevel_str[] = {
    "DEBUG",
    "INFO",
    "ERROR"
};

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

static void usage(char *arg) {
    fprintf(stderr, "Usage: %s <region> <capabilities> <private key> <addr> [ <dir acl> <vconn acl> ] [ <public key> <addr> ]\n", arg);
    exit(1);
}

static void fail(char *format, ...) {
    va_list args;

    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);
    exit(1);
}

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

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

    switch (ctype) {
        case CTYPE_DIR: {
            if (!(srv_config.capabilities & ECP_DIR_CAP_DIR)) return NULL;

            conn = malloc(sizeof(ECPConnection));
            if (conn) {
                ecp_conn_init(conn, sock, ctype);
                ecp_conn_set_flags(conn, ECP_CONN_FLAG_VBOX);
            }
            break;
        }

        case ECP_CTYPE_DIR: {
            if (!(srv_config.capabilities & ECP_DIR_CAP_DIR)) return NULL;

            conn = malloc(sizeof(ECPConnection));
            if (conn) ecp_conn_init(conn, sock, ctype);
            break;
        }

        case ECP_CTYPE_VCONN: {
            ECPVConnInb *_conn;

            if (!(srv_config.capabilities & ECP_DIR_CAP_VCONN)) return NULL;

            _conn = malloc(sizeof(ECPVConnInb));
            if (_conn) {
                ecp_vconn_init_inb(_conn, sock);
                conn = &_conn->b;
            }
            break;
        }

        case ECP_CTYPE_VLINK: {
            if (!(srv_config.capabilities & ECP_DIR_CAP_VCONN)) return NULL;

            conn = malloc(sizeof(ECPConnection));
            if (conn) ecp_vlink_init(conn, sock);
            break;
        }
    }

    if (conn) ecp_conn_set_flags(conn, ECP_CONN_FLAG_GC);
    return conn;
}

static int key_check(ECPSocket *sock, ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *public) {
    switch (ctype) {
        case CTYPE_DIR: {
            if (public == NULL) return 0;
            return acl_inlist(public);
        }

        case ECP_CTYPE_VLINK: {
            if (public == NULL) return 0;
            if (parent == NULL) return acl_inlist(public);
            return 1;
        }

        default:
            return 1;
    }
}

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

void log_print(int level, char *format, ...) {
    va_list args;
    time_t t;
    char buf[26];

    if (level >= (sizeof(srv_llevel_str) / sizeof(char *))) return;

    t = time(NULL);
    ctime_r(&t, buf);
    buf[24] = '\0';
    fprintf(stderr, "%s [%s]: ", buf, srv_llevel_str[level]);

    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);
}

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

    rv = ecp_ctx_init(ctx, handle_err, conn_new, conn_free, key_check);
    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;
    char *debug_init_str;
    int _argc, fd;
    int rv;

    memset(&srv_config, 0, sizeof(srv_config));

    if (argc < 3) usage(argv[0]);

    _argc = 1;
    srv_config.region = (uint8_t)strtol(argv[_argc], &endptr, 16);
    if (endptr[0] != '\0') fail("Bad region\n");
    if (srv_config.region >= MAX_REGION) fail("Bad region\n");
    _argc++;

    srv_config.capabilities = (uint8_t)strtol(argv[_argc], &endptr, 16);
    if (endptr[0] != '\0') fail("Bad capabilities\n");
    _argc++;

    if (srv_config.capabilities & ECP_DIR_CAP_DIR) {
        if (argc < 7) usage(argv[0]);
    } else {
        if (argc < 5) usage(argv[0]);
    }

    fd = open(argv[_argc], O_RDONLY);
    if (fd < 0) fail("Unable to open %s\n", argv[_argc]);

    rv = ecp_util_read_key(fd, &srv_config.key_perma.public, &srv_config.key_perma.private);
    close(fd);
    if (rv) fail("Unable to read key from %s\n", argv[_argc]);
    srv_config.key_perma.valid = 1;
    _argc++;

    rv = ecp_init(&ctx, &vconn_handler, &vlink_handler);
    if (rv) fail("ecp_init err:%d\n", rv);

    rv = ecp_sock_create(&sock, &ctx, &srv_config.key_perma);
    if (rv) fail("ecp_sock_create err:%d\n", rv);

    rv = ecp_vconn_sock_create(&sock);
    if (rv) fail("ecp_vconn_sock_create err:%d\n", rv);

    rv = ecp_addr_init(&addr, argv[_argc]);
    if (rv) fail("ecp_addr_init err:%d\n", rv);
    _argc++;

    rv = ecp_sock_open(&sock, &addr);
    if (rv) fail("ecp_sock_open err:%d\n", rv);
    srv_config.my_addr = addr;

    rv = acl_init();
    if (rv) fail("acl_init err:%d\n", rv);

    if (srv_config.capabilities & ECP_DIR_CAP_DIR) {
        srv_config.acl_fn_dir = strdup(argv[_argc]);
        _argc++;
        srv_config.acl_fn = strdup(argv[_argc]);
        _argc++;

        rv = acl_load();
        if (rv) fail("acl_load err:%d\n", rv);
    }

    rv = dir_init(&sock);
    if (rv) fail("dir_init err:%d\n", rv);

    rv = vlink_init(&sock);
    if (rv) fail("vlink_init err:%d\n", rv);

    rv = ecp_start_receiver(&sock);
    if (rv) fail("ecp_start_receiver err:%d\n", rv);

    if (argc == _argc + 2) {
        ECPNode node;
        ecp_ecdh_public_t node_pub;
        ecp_tr_addr_t node_addr;

        fd = open(argv[_argc], O_RDONLY);
        if (fd < 0) fail("Unable to open %s\n", argv[_argc]);

        rv = ecp_util_read_key(fd, &node_pub, NULL);
        close(fd);
        if (rv) fail("Unable to read public key from %s\n", argv[_argc]);
        _argc++;

        ecp_node_init(&node, &node_pub, NULL);

        rv = ecp_node_set_addr(&node, argv[_argc]);
        if (rv) fail("ecp_node_set_addr err:%d\n", rv);
        _argc++;

        rv = dir_init_ann(&sock, &node);
        if (rv) fail("dir_init_ann err:%d\n", rv);
    }

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

    debug_init_str = getenv("ECP_DBG_INIT");
    if (debug_init_str) {
        int init_ann;

        init_ann = (int)strtol(debug_init_str, &endptr, 10);
        if (endptr[0] == '\0') {
            LOG(LOG_DEBUG, "init switch start - number of announces:%d\n", init_ann);
            dir_init_switch(&sock, init_ann);
            LOG(LOG_DEBUG, "init switch done\n");
        }
    }

    rv = dir_start_announce(&sock);
    if (rv) fail("dir_start_announce err:%d\n", rv);

    rv = vlink_start_keyx(&sock);
    if (rv) fail("vlink_start_keyx err:%d\n", rv);

    while(1) pause();
}