summaryrefslogtreecommitdiff
path: root/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c
blob: 890085cc0ba72dd15b637115e54f861ef8706677 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* DHT boostrap
 *
 * A simple DHT boostrap server for tox - daemon edition.
 *
 *  Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *  This file is part of Tox.
 *
 *  Tox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Tox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <sys/types.h> /* pid_t */
#include <sys/stat.h> /* umask */
#include <unistd.h> /* POSIX things */
#include <errno.h>

#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
#include <arpa/inet.h> /* htons() */
#include <string.h> /* strcpy() */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "../../toxcore/DHT.h"
#include "../../toxcore/friend_requests.h"

#define DEFAULT_PORT 33445
#define DEFAULT_PID_FILE "bootstrap_server.pid"
#define DEFAULT_KEYS_FILE "bootstrap_server.keys"

/* Server info struct */
struct server_info_s {
    int valid;
    IP_Port conn;
    uint8_t bs_pk[32];
};

/* This is the struct configure_server() uses to return its data to */
struct server_conf_s {
    int err;
    int port;
    char pid_file[512];
    char keys_file[512];
    struct server_info_s info[32];
};

int b16_to_key(char b16_string[], uint8_t *bs_pubkey)
{

    int i;
    unsigned int num1 = 0, num2 = 0;

    for (i = 0; i < 32; ++i) {
        sscanf(&b16_string[i * 2], "%1X", &num1);
        sscanf(&b16_string[i * 2 + 1], "%1X", &num2);
        num1 = num1 << 4;
        bs_pubkey[i] = bs_pubkey[i] | num1;
        bs_pubkey[i] = bs_pubkey[i] | num2;
    }

    return 0;
}

/*
  resolve_addr():
    address should represent IPv4 or a hostname with a record

    returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
    returns 0 on failure

    TODO: Fix ipv6 support
*/

uint32_t resolve_addr(const char *address)
{
    struct addrinfo *server = NULL;
    struct addrinfo  hints;
    int              rc;
    uint32_t         addr;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family   = AF_INET;    // IPv4 only right now.
    hints.ai_socktype = SOCK_DGRAM; // Type of socket Tox uses.

    rc = getaddrinfo(address, "echo", &hints, &server);

    // Lookup failed.
    if (rc != 0) {
        return 0;
    }

    // IPv4 records only..
    if (server->ai_family != AF_INET) {
        freeaddrinfo(server);
        return 0;
    }


    addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr;

    freeaddrinfo(server);
    return addr;
}

/* This function connects to all specified servers
and connect to them.
returns 1 if the connection to the DHT is up
returns -1 if all attempts failed
*/
int connect_to_servers(DHT *dht, struct server_info_s *info)
{
    int i;
    int c;

    for (i = 0; i < 32; ++i) {
        if (info[i].valid) {
            /* Actual bootstrapping code goes here */
            //puts("Calling DHT_bootstrap");
            DHT_bootstrap(dht, info[i].conn, info[i].bs_pk);
        }
    }

    /* Check if we're connected to the DHT */
    for (c = 0; c != 100; ++c) {
        usleep(10000);

        if (DHT_isconnected(dht)) {
            //puts("Connected");
            return 1;
            break;
        }

        if (DHT_isconnected(dht) == 0 && c == 99) {
            //puts("Not connected");
            return -1;
            break;
        }

        do_DHT(dht);

        networking_poll(dht->c->lossless_udp->net);
    }

    /* This probably never happens */
    return 0;
}

void manage_keys(DHT *dht, char *keys_file)
{
    const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
    uint8_t keys[KEYS_SIZE];
    struct stat existence;
    FILE *keysf;

    /* Check if file exits, proceed to open and load keys */
    if (stat(keys_file, &existence) >= 0) {
        keysf = fopen(keys_file, "r");
        size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keysf);

        if (read_size != KEYS_SIZE) {
            printf("Error while reading the key file\nExiting.\n");
            exit(1);
        } else {
            printf("Keys loaded successfully\n");
        }

        load_keys(dht->c, keys);

    } else {
        /* Otherwise save new keys */
        /* Silly work-around to ignore any errors coming from new_keys() */
        new_keys(dht->c);
        save_keys(dht->c, keys);
        keysf = fopen(keys_file, "w");

        if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keysf) != KEYS_SIZE) {
            printf("Error while writing the key file.\nExiting.\n");
            exit(1);
        } else {
            printf("Keys saved successfully\n");
        }
    }

    fclose(keysf);
}

/* This reads the configuration file, and returns a struct server_conf_s with:
 *an error number:
    *-1 = file wasn't read, for whatever reason
    *-2 = no bootstrap servers found
 *the port
 *the location of the keys file
 *the location of the PID file
 *the list of bootstrap servers
*/
struct server_conf_s configure_server(char *cfg_file)
{
    config_t cfg;
    config_setting_t *server_list;

    /* This one will be strcpy'd into the pid_file array in server_conf */
    const char *pid_file_tmp;
    const char *keys_file_tmp;

    /* Remote bootstrap server variables */
    int bs_port;
    const char *bs_ip;
    const char *bs_pk;

    /* The big struct */
    static struct server_conf_s server_conf;

    /* Set both to their default values. If there's an error
    with opening/reading the config file, we return right away */
    server_conf.port = DEFAULT_PORT;
    strcpy(server_conf.pid_file, DEFAULT_PID_FILE);
    strcpy(server_conf.keys_file, DEFAULT_KEYS_FILE);

    config_init(&cfg);

    /* Read the file. If there is an error, report it and exit. */
    if (! config_read_file(&cfg, cfg_file)) {
        fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
                config_error_line(&cfg), config_error_text(&cfg));
        config_destroy(&cfg);
        server_conf.err = -1;
        return server_conf;
    }

    /* Get the port to listen on */
    if (config_lookup_int(&cfg, "port", &server_conf.port)) {
        //printf("Port: %d\n", port);
    } else {
        fprintf(stderr, "No 'port' setting in configuration file.\n");
    }

    /* Get PID file location */
    if (config_lookup_string(&cfg, "pid_file", &pid_file_tmp)) {
        //printf("PID file: %s\n", pid_file_tmp);
        strcpy(server_conf.pid_file, pid_file_tmp);
    } else {
        fprintf(stderr, "No 'pid_file' setting in configuration file.\n");
    }

    /* Get keys file location */
    if (config_lookup_string(&cfg, "keys_file", &keys_file_tmp)) {
        //printf("Keys file: %s\n", keys_file_tmp);
        strcpy(server_conf.keys_file, keys_file_tmp);
    } else {
        fprintf(stderr, "No 'keys_file' setting in configuration file.\n");
    }

    /* Get all the servers in the list */
    server_list = config_lookup(&cfg, "bootstrap_servers");

    if (server_list != NULL) {
        int count = config_setting_length(server_list);
        int i;

        char tmp_ip[30]; /* IP */
        char tmp_pk[64]; /* bs_pk */

        for (i = 0; i < count; ++i) {
            config_setting_t *server = config_setting_get_elem(server_list, i);
            /* Get a pointer on the key array */
            uint8_t *bs_pk_p = server_conf.info[i].bs_pk;

            /* Only output the record if all of the expected fields are present. */
            if (!(config_setting_lookup_string(server, "ip", &bs_ip)
                    && config_setting_lookup_int(server, "port", &bs_port)
                    && config_setting_lookup_string(server, "bs_pk", &bs_pk)))
                continue;

            /* Converting all that stuff into usable formats and storing
            it away in the server_info struct */
            server_conf.info[i].valid = 1;

            if (resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) {
                server_conf.info[i].valid = 0;
                printf("bootstrap_server %d: Invalid IP.\n", i);
            }

            if (strlen(bs_pk) != 64) {
                server_conf.info[i].valid = 0;
                printf("bootstrap_server %d: Invalid public key.\n", i);
            }

            if (!bs_port) {
                server_conf.info[i].valid = 0;
                printf("bootstrap_server %d: Invalid port.\n", i);
            }

#ifdef TOX_ENABLE_IPV6
            server_conf.info[i].conn.ip.family = AF_INET;
            server_conf.info[i].conn.ip.ip4.uint32 = resolve_addr(strcpy(tmp_ip, bs_ip));
#else
            server_conf.info[i].conn.ip.uint32 = resolve_addr(strcpy(tmp_ip, bs_ip));
#endif
            server_conf.info[i].conn.port = htons(bs_port);
            b16_to_key(strcpy(tmp_pk, bs_pk), bs_pk_p);
        }

        /* Check if at least one server entry is valid */
        for (i = 0; i < 32; ++i) {
            if (server_conf.info[i].valid)
                break;
            else
                server_conf.err = -2;
        }

    } else {
        server_conf.err = -2;
    }

    config_destroy(&cfg);
    return server_conf;
}

int main(int argc, char *argv[])
{

    pid_t pid, sid; /* Process- and Session-ID */
    struct server_conf_s server_conf;

    FILE *pidf; /* The PID file */

    if (argc < 2) {
        printf("Please specify a configuration file.\n");
        exit(EXIT_FAILURE);
    }

    server_conf = configure_server(argv[1]);

    /* Initialize networking
    bind to ip 0.0.0.0:PORT */
    IP ip;
    ip_init(&ip, 0);
    DHT *dht = new_DHT(new_net_crypto(new_networking(ip, server_conf.port)));
    /* Read the config file */
    printf("PID file: %s\n", server_conf.pid_file);
    printf("Key file: %s\n", server_conf.keys_file);

    if (server_conf.err == -1)
        printf("Config file not read.\n");

    if (server_conf.err == -2)
        printf("No valid servers in list.\n");

    /* Open PID file for writing - if an error happens,
    it will be caught down the line */
    pidf = fopen(server_conf.pid_file, "w");

    /* Manage the keys */
    /* for now, just ignore any errors after this call. */
    int tmperr = errno;
    manage_keys(dht, server_conf.keys_file);
    errno = tmperr;

    /* Public key */
    int i;
    printf("\nPublic Key: ");

    for (i = 0; i < 32; ++i) {
        uint8_t ln, hn;
        ln = 0x0F & dht->c->self_public_key[i];
        hn = 0xF0 & dht->c->self_public_key[i];
        hn = hn >> 4;
        printf("%X%X", hn, ln);
    }

    printf("\n");

    /* Bootstrap the DHT
    This one throws odd errors, too. Ignore. I assume they come
    from somewhere in the core. */
    tmperr = errno;
    connect_to_servers(dht, server_conf.info);
    errno = tmperr;

    if (!DHT_isconnected(dht)) {
        puts("Could not establish DHT connection. Check server settings.\n");
        exit(EXIT_FAILURE);
    } else {
        printf("Connected to DHT successfully.\n");
    }

    /* If there's been an error, exit before forking off */
    if (errno != 0) {
        perror("Error");
        printf("Error(s) occured during start-up. Exiting.\n");
        exit(EXIT_FAILURE);
    }

    /* Things that make the daemon work come past here.
    There should be nothing here but the daemon code and
    the main loop. */

    /* Fork off from the parent process */
    pid = fork();

    if (pid < 0) {
        printf("Forking failed.\n");
        exit(EXIT_FAILURE);
    }

    /* If we got a good PID, then
    we can exit the parent process. */
    if (pid > 0) {
        printf("Forked successfully: %d.\n", pid);

        /* Write the PID file */
        fprintf(pidf, "%d\n", pid);
        fclose(pidf);

        /* Exit parent */
        exit(EXIT_SUCCESS);
    }

    /* Change the file mode mask */
    umask(0);

    /* Create a new SID for the child process */
    sid = setsid();

    if (sid < 0) {
        printf("SID creation failure.\n");
        exit(EXIT_FAILURE);
    }

    /* Change the current working directory */
    if ((chdir("/")) < 0) {
        exit(EXIT_FAILURE);
    }

    /* Go quiet */
    close(STDOUT_FILENO);
    close(STDIN_FILENO);
    close(STDERR_FILENO);

    while (1) {
        do_DHT(dht);

        networking_poll(dht->c->lossless_udp->net);
        usleep(10000);
    }

    //shutdown_networking();
    exit(EXIT_SUCCESS);
}