]> git.meshlink.io Git - meshlink/blob - src/net_setup.c
Add support for opening a MeshLink instance without permanent storage.
[meshlink] / src / net_setup.c
1 /*
2     net_setup.c -- Setup.
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "conf.h"
23 #include "connection.h"
24 #include "ecdsa.h"
25 #include "graph.h"
26 #include "logger.h"
27 #include "meshlink_internal.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "packmsg.h"
31 #include "protocol.h"
32 #include "route.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 /// Helper function to start parsing a host config file
37 static bool node_get_config(meshlink_handle_t *mesh, node_t *n, config_t *config, packmsg_input_t *in) {
38         if(!config_read(mesh, n->name, config)) {
39                 return false;
40         }
41
42         in->ptr = config->buf;
43         in->len = config->len;
44
45         uint32_t version = packmsg_get_uint32(in);
46
47         if(version != MESHLINK_CONFIG_VERSION) {
48                 config_free(config);
49                 return false;
50         }
51
52         const char *name;
53         uint32_t len = packmsg_get_str_raw(in, &name);
54
55         if(len != strlen(n->name) || strncmp(name, n->name, len)) {
56                 config_free(config);
57                 return false;
58         }
59
60         return true;
61 }
62
63 /// Read just the devclass from a host config file. Used at startup when reading all host config files.
64 bool node_read_devclass(meshlink_handle_t *mesh, node_t *n) {
65         config_t config;
66         packmsg_input_t in;
67
68         if(!node_get_config(mesh, n, &config, &in)) {
69                 return false;
70         }
71
72         int32_t devclass = packmsg_get_int32(&in);
73         bool blacklisted = packmsg_get_bool(&in);
74         config_free(&config);
75
76         if(!packmsg_input_ok(&in) || devclass < 0 || devclass > _DEV_CLASS_MAX) {
77                 return false;
78         }
79
80         n->devclass = devclass;
81         n->status.blacklisted = blacklisted;
82         return true;
83 }
84
85 /// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
86 bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
87         if(ecdsa_active(n->ecdsa)) {
88                 return true;
89         }
90
91         config_t config;
92         packmsg_input_t in;
93
94         if(!node_get_config(mesh, n, &config, &in)) {
95                 return false;
96         }
97
98         packmsg_get_int32(&in); /* devclass */
99         packmsg_get_bool(&in); /* blacklisted */
100
101         const void *key;
102         uint32_t len = packmsg_get_bin_raw(&in, &key);
103
104         if(len != 32) {
105                 config_free(&config);
106                 return false;
107         }
108
109         n->ecdsa = ecdsa_set_public_key(key);
110         config_free(&config);
111         return true;
112 }
113
114 /// Fill in node details from a config blob.
115 bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *config) {
116         if(n->canonical_address) {
117                 return true;
118         }
119
120         packmsg_input_t in = {config->buf, config->len};
121         uint32_t version = packmsg_get_uint32(&in);
122
123         if(version != MESHLINK_CONFIG_VERSION) {
124                 return false;
125         }
126
127         char *name = packmsg_get_str_dup(&in);
128
129         if(!name) {
130                 return false;
131         }
132
133         if(n->name) {
134                 if(strcmp(n->name, name)) {
135                         free(name);
136                         return false;
137                 }
138
139                 free(name);
140         } else {
141                 n->name = name;
142         }
143
144         n->devclass = packmsg_get_int32(&in);
145         n->status.blacklisted = packmsg_get_bool(&in);
146         const void *key;
147         uint32_t len = packmsg_get_bin_raw(&in, &key);
148
149         if(len != 32) {
150                 return false;
151         }
152
153         if(!ecdsa_active(n->ecdsa)) {
154                 n->ecdsa = ecdsa_set_public_key(key);
155         }
156
157         n->canonical_address = packmsg_get_str_dup(&in);
158         uint32_t count = packmsg_get_array(&in);
159
160         if(count > 5) {
161                 count = 5;
162         }
163
164         for(uint32_t i = 0; i < count; i++) {
165                 n->recent[i] = packmsg_get_sockaddr(&in);
166         }
167
168         return packmsg_done(&in);
169 }
170
171 /// Read the full host config file. Used whenever we need to start an SPTPS session.
172 bool node_read_full(meshlink_handle_t *mesh, node_t *n) {
173         if(n->canonical_address) {
174                 return true;
175         }
176
177         config_t config;
178         packmsg_input_t in;
179
180         if(!node_get_config(mesh, n, &config, &in)) {
181                 return false;
182         }
183
184         packmsg_get_int32(&in); /* devclass */
185         packmsg_get_bool(&in); /* blacklisted */
186         const void *key;
187         uint32_t len = packmsg_get_bin_raw(&in, &key); /* public key */
188
189         if(len != 32) {
190                 return false;
191         }
192
193         if(!ecdsa_active(n->ecdsa)) {
194                 n->ecdsa = ecdsa_set_public_key(key);
195         }
196
197         n->canonical_address = packmsg_get_str_dup(&in);
198
199         uint32_t count = packmsg_get_array(&in);
200
201         if(count > 5) {
202                 count = 5;
203         }
204
205         for(uint32_t i = 0; i < count; i++) {
206                 n->recent[i] = packmsg_get_sockaddr(&in);
207         }
208
209         config_free(&config);
210
211         return packmsg_done(&in);
212 }
213
214 bool node_write_config(meshlink_handle_t *mesh, node_t *n) {
215         uint8_t buf[4096];
216         packmsg_output_t out = {buf, sizeof(buf)};
217
218         packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
219         packmsg_add_str(&out, n->name);
220         packmsg_add_int32(&out, n->devclass);
221         assert(n->devclass != 3);
222         packmsg_add_bool(&out, n->status.blacklisted);
223
224         if(ecdsa_active(n->ecdsa)) {
225                 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
226         } else {
227                 packmsg_add_bin(&out, "", 0);
228         }
229
230         packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
231
232         uint32_t count = 0;
233
234         for(uint32_t i = 0; i < 5; i++) {
235                 if(n->recent[i].sa.sa_family) {
236                         count++;
237                 } else {
238                         break;
239                 }
240         }
241
242         packmsg_add_array(&out, count);
243
244         for(uint32_t i = 0; i < count; i++) {
245                 packmsg_add_sockaddr(&out, &n->recent[i]);
246         }
247
248         if(!packmsg_output_ok(&out)) {
249                 return false;
250         }
251
252         config_t config = {buf, packmsg_output_size(&out, buf)};
253         return config_write(mesh, n->name, &config);
254 }
255
256 static void load_node(meshlink_handle_t *mesh, const char *name) {
257         if(!check_id(name)) {
258                 return;
259         }
260
261         node_t *n = lookup_node(mesh, name);
262
263         if(n) {
264                 return;
265         }
266
267         n = new_node();
268         n->name = xstrdup(name);
269         node_read_devclass(mesh, n);
270         node_add(mesh, n);
271 }
272
273 /*
274   Add listening sockets.
275 */
276 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
277         char *port = mesh->myport;
278
279         if(address) {
280                 char *space = strchr(address, ' ');
281
282                 if(space) {
283                         *space++ = 0;
284                         port = space;
285                 }
286
287                 if(!strcmp(address, "*")) {
288                         *address = 0;
289                 }
290         }
291
292         struct addrinfo *ai;
293
294         struct addrinfo hint = {
295                 .ai_family = addressfamily,
296                 .ai_socktype = SOCK_STREAM,
297                 .ai_protocol = IPPROTO_TCP,
298                 .ai_flags = AI_PASSIVE,
299         };
300
301         int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
302
303         free(address);
304
305         if(err || !ai) {
306                 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
307                 return false;
308         }
309
310         bool success = false;
311
312         for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
313                 // Ignore duplicate addresses
314                 bool found = false;
315
316                 for(int i = 0; i < mesh->listen_sockets; i++)
317                         if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
318                                 found = true;
319                                 break;
320                         }
321
322                 if(found) {
323                         continue;
324                 }
325
326                 if(mesh->listen_sockets >= MAXSOCKETS) {
327                         logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
328                         return false;
329                 }
330
331                 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
332
333                 if(tcp_fd < 0) {
334                         continue;
335                 }
336
337                 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
338
339                 if(udp_fd < 0) {
340                         close(tcp_fd);
341                         continue;
342                 }
343
344                 io_add(&mesh->loop, &mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ);
345                 io_add(&mesh->loop, &mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ);
346
347                 if(mesh->log_level >= MESHLINK_INFO) {
348                         char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
349                         logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
350                         free(hostname);
351                 }
352
353                 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
354                 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
355                 mesh->listen_sockets++;
356                 success = true;
357         }
358
359         freeaddrinfo(ai);
360         return success;
361 }
362
363 /*
364   Configure node_t mesh->self and set up the local sockets (listen only)
365 */
366 bool setup_myself(meshlink_handle_t *mesh) {
367         /* Set some defaults */
368
369         mesh->localdiscovery = true;
370         keylifetime = 3600; // TODO: check if this can be removed as well
371         mesh->maxtimeout = 900;
372         mesh->self->options |= OPTION_PMTU_DISCOVERY;
373
374         /* Done */
375
376         mesh->self->nexthop = mesh->self;
377         mesh->self->via = mesh->self;
378         mesh->self->status.reachable = true;
379         mesh->self->last_state_change = mesh->loop.now.tv_sec;
380
381         node_add(mesh, mesh->self);
382
383         graph(mesh);
384
385         config_scan_all(mesh, load_node);
386
387         /* Open sockets */
388
389         mesh->listen_sockets = 0;
390
391         if(!add_listen_address(mesh, NULL, NULL)) {
392                 if(strcmp(mesh->myport, "0")) {
393                         logger(mesh, MESHLINK_INFO, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
394                         free(mesh->myport);
395                         mesh->myport = strdup("0");
396
397                         if(!mesh->myport) {
398                                 return false;
399                         }
400
401                         if(!add_listen_address(mesh, NULL, NULL)) {
402                                 return false;
403                         }
404                 } else {
405                         return false;
406                 }
407         }
408
409         if(!mesh->listen_sockets) {
410                 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
411                 return false;
412         }
413
414         /* Done. */
415
416         mesh->last_config_check = mesh->loop.now.tv_sec;
417
418         return true;
419 }
420
421 /*
422   initialize network
423 */
424 bool setup_network(meshlink_handle_t *mesh) {
425         init_connections(mesh);
426         init_nodes(mesh);
427         init_edges(mesh);
428         init_requests(mesh);
429
430         mesh->pinginterval = 60;
431         mesh->pingtimeout = 5;
432         maxoutbufsize = 10 * MTU;
433
434         if(!setup_myself(mesh)) {
435                 return false;
436         }
437
438         return true;
439 }
440
441 /*
442   close all open network connections
443 */
444 void close_network_connections(meshlink_handle_t *mesh) {
445         if(mesh->connections) {
446                 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
447                         next = node->next;
448                         connection_t *c = node->data;
449                         c->outgoing = NULL;
450                         terminate_connection(mesh, c, false);
451                 }
452         }
453
454         for(int i = 0; i < mesh->listen_sockets; i++) {
455                 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
456                 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
457                 close(mesh->listen_socket[i].tcp.fd);
458                 close(mesh->listen_socket[i].udp.fd);
459         }
460
461         exit_requests(mesh);
462         exit_edges(mesh);
463         exit_nodes(mesh);
464         exit_connections(mesh);
465
466         if(mesh->myport) {
467                 free(mesh->myport);
468         }
469
470         return;
471 }