summaryrefslogtreecommitdiff
path: root/src/ipcpd/broadcast/main.c
blob: de1feeb20968c1a46427bc8a93d74e8fc18d1c7b (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
/*
 * Ouroboros - Copyright (C) 2016 - 2023
 *
 * Broadcast IPC Process
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#if defined(__linux__) || defined(__CYGWIN__)
#define _DEFAULT_SOURCE
#else
#define _POSIX_C_SOURCE 200809L
#endif

#include "config.h"

#define OUROBOROS_PREFIX "broadcast-ipcp"
#define THIS_TYPE IPCP_BROADCAST

#include <ouroboros/errno.h>
#include <ouroboros/dev.h>
#include <ouroboros/ipcp-dev.h>
#include <ouroboros/logs.h>
#include <ouroboros/notifier.h>
#include <ouroboros/random.h>
#include <ouroboros/rib.h>
#include <ouroboros/time_utils.h>

#include "common/connmgr.h"
#include "common/enroll.h"
#include "dt.h"
#include "ipcp.h"

#include <stdbool.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>

struct ipcp ipcpi;

static int initialize_components(const struct ipcp_config * conf)
{
        strcpy(ipcpi.layer_name, conf->layer_info.layer_name);
        ipcpi.dir_hash_algo = conf->layer_info.dir_hash_algo;

        assert(ipcp_dir_hash_len() != 0);

        if (dt_init() < 0) {
                log_err("Failed to initialize forwarding component.");
                return -1;
        }

        ipcp_set_state(IPCP_INIT);

        return 0;
}

static void finalize_components(void)
{
        dt_fini();
}

static int start_components(void)
{
        if (enroll_start() < 0) {
                log_err("Failed to start enrollment.");
                goto fail_enroll_start;
        }

        if (connmgr_start() < 0) {
                log_err("Failed to start AP connection manager.");
                goto fail_connmgr_start;
        }

        return 0;

 fail_connmgr_start:
        enroll_stop();
 fail_enroll_start:
        ipcp_set_state(IPCP_INIT);
        return -1;
}

static void stop_components(void)
{
        connmgr_stop();

        enroll_stop();
}

static int broadcast_ipcp_enroll(const char *        dst,
                                 struct layer_info * info)
{
        struct conn conn;
        uint8_t     id[ENROLL_ID_LEN];

        if (random_buffer(id, ENROLL_ID_LEN) < 0) {
                log_err("Failed to generate enrollment ID.");
                goto fail_id;
        }

        log_info_id(id, "Requesting enrollment.");

        if (connmgr_alloc(COMPID_ENROLL, dst, NULL, &conn) < 0) {
                log_err_id(id, "Failed to get connection.");
                goto fail_id;
        }

        /* Get boot state from peer. */
        if (enroll_boot(&conn, id) < 0) {
                log_err_id(id, "Failed to get boot information.");
                goto fail_enroll_boot;
        }

        if (initialize_components(enroll_get_conf()) < 0) {
                log_err_id(id, "Failed to initialize components.");
                goto fail_enroll_boot;
        }

        if (start_components() < 0) {
                log_err_id(id, "Failed to start components.");
                goto fail_start_comp;
        }

        if (enroll_ack(&conn, id, 0) < 0)
                log_err_id(id, "Failed to confirm enrollment.");

        if (connmgr_dealloc(COMPID_ENROLL, &conn) < 0)
                log_warn_id(id, "Failed to dealloc enrollment flow.");

        log_info_id(id, "Enrolled with %s.", dst);

        info->dir_hash_algo = ipcpi.dir_hash_algo;
        strcpy(info->layer_name, ipcpi.layer_name);

        return 0;

 fail_start_comp:
        finalize_components();
 fail_enroll_boot:
        connmgr_dealloc(COMPID_ENROLL, &conn);
 fail_id:
        return -1;
}

static int broadcast_ipcp_bootstrap(const struct ipcp_config * conf)
{
        assert(conf);
        assert(conf->type == THIS_TYPE);
        ((struct ipcp_config *) conf)->layer_info.dir_hash_algo = HASH_SHA3_256;

        enroll_bootstrap(conf);

        if (initialize_components(conf)) {
                log_err("Failed to init IPCP components.");
                goto fail_init;
        }

        if (start_components()) {
                log_err("Failed to init IPCP components.");
                goto fail_start;
        }

        return 0;

 fail_start:
        finalize_components();
 fail_init:
        return -1;
}

static int name_check(const uint8_t * dst)
{
        uint8_t * buf;
        size_t    len;
        int       ret;

        len = hash_len(ipcpi.dir_hash_algo);
        buf =  malloc(len);
        if (buf == NULL)
                return -ENOMEM;

        str_hash(ipcpi.dir_hash_algo, buf, ipcpi.layer_name);

        ret = memcmp(buf, dst, len);

        free(buf);

        return ret;
}

static int broadcast_ipcp_join(int             fd,
                               const uint8_t * dst,
                               qosspec_t       qs)
{
        struct conn conn;
        time_t      mpl = IPCP_BROADCAST_MPL;

        (void) qs;

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

        conn.flow_info.fd = fd;

        if (name_check(dst) != 0) {
                log_err("Failed to check name.");
                return -1;
        }

        notifier_event(NOTIFY_DT_CONN_ADD, &conn);

        ipcp_flow_alloc_reply(fd, 0, mpl, NULL, 0);

        return 0;
}

int broadcast_ipcp_dealloc(int fd)
{
        struct conn conn;

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

        conn.flow_info.fd = fd;

        notifier_event(NOTIFY_DT_CONN_DEL, &conn);

        flow_dealloc(fd);

        return 0;
}

static struct ipcp_ops broadcast_ops = {
        .ipcp_bootstrap       = broadcast_ipcp_bootstrap,
        .ipcp_enroll          = broadcast_ipcp_enroll,
        .ipcp_connect         = connmgr_ipcp_connect,
        .ipcp_disconnect      = connmgr_ipcp_disconnect,
        .ipcp_reg             = NULL,
        .ipcp_unreg           = NULL,
        .ipcp_query           = NULL,
        .ipcp_flow_alloc      = NULL,
        .ipcp_flow_join       = broadcast_ipcp_join,
        .ipcp_flow_alloc_resp = NULL,
        .ipcp_flow_dealloc    = broadcast_ipcp_dealloc
};

int main(int    argc,
         char * argv[])
{
        if (ipcp_init(argc, argv, &broadcast_ops, THIS_TYPE) < 0) {
                log_err("Failed to initialize IPCP.");
                goto fail_init;
        }

        if (notifier_init()) {
                log_err("Failed to initialize notifier component.");
                goto fail_notifier_init;
        }

        if (connmgr_init()) {
                log_err("Failed to initialize connection manager.");
                goto fail_connmgr_init;
        }

        if (enroll_init()) {
                log_err("Failed to initialize enrollment component.");
                goto fail_enroll_init;
        }

        if (ipcp_start() < 0) {
                log_err("Failed to boot IPCP.");
                goto fail_start;
        }

        ipcp_sigwait();

        if (ipcp_get_state() == IPCP_SHUTDOWN) {
                stop_components();
                finalize_components();
        }

        ipcp_stop();

        enroll_fini();

        connmgr_fini();

        notifier_fini();

        ipcp_fini();

        exit(EXIT_SUCCESS);

 fail_start:
        enroll_fini();
 fail_enroll_init:
        connmgr_fini();
 fail_connmgr_init:
        notifier_fini();
 fail_notifier_init:
        ipcp_fini();
 fail_init:
        exit(EXIT_FAILURE);
}