]> git.meshlink.io Git - meshlink/blob - src/net_setup.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[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 #include "submesh.h"
36
37 /// Helper function to start parsing a host config file
38 static bool node_get_config(meshlink_handle_t *mesh, node_t *n, config_t *config, packmsg_input_t *in) {
39         if(!config_read(mesh, "current", n->name, config, mesh->config_key)) {
40                 return false;
41         }
42
43         in->ptr = config->buf;
44         in->len = config->len;
45
46         uint32_t version = packmsg_get_uint32(in);
47
48         if(version != MESHLINK_CONFIG_VERSION) {
49                 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
50                 config_free(config);
51                 return false;
52         }
53
54         const char *name;
55         uint32_t len = packmsg_get_str_raw(in, &name);
56
57         if(len != strlen(n->name) || !name || strncmp(name, n->name, len)) {
58                 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
59                 config_free(config);
60                 return false;
61         }
62
63         return true;
64 }
65
66 /// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
67 bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
68         if(ecdsa_active(n->ecdsa)) {
69                 return true;
70         }
71
72         config_t config;
73         packmsg_input_t in;
74
75         if(!node_get_config(mesh, n, &config, &in)) {
76                 return false;
77         }
78
79         packmsg_skip_element(&in); /* submesh */
80         packmsg_get_int32(&in); /* devclass */
81         packmsg_get_bool(&in); /* blacklisted */
82
83         const void *key;
84         uint32_t len = packmsg_get_bin_raw(&in, &key);
85
86         if(len != 32) {
87                 config_free(&config);
88                 return false;
89         }
90
91         n->ecdsa = ecdsa_set_public_key(key);
92
93         // While we are at it, read known address information
94         if(!n->canonical_address) {
95                 n->canonical_address = packmsg_get_str_dup(&in);
96         } else {
97                 packmsg_skip_element(&in);
98         }
99
100         // Append any known addresses in the config file to the list we currently have
101         uint32_t known_count = 0;
102
103         for(uint32_t i = 0; i < 5; i++) {
104                 if(n->recent[i].sa.sa_family) {
105                         known_count++;
106                 }
107         }
108
109         uint32_t count = packmsg_get_array(&in);
110
111         if(count > 5 - known_count) {
112                 count = 5 - known_count;
113         }
114
115         for(uint32_t i = 0; i < count; i++) {
116                 n->recent[i + known_count] = packmsg_get_sockaddr(&in);
117         }
118
119         config_free(&config);
120         return true;
121 }
122
123 /// Fill in node details from a config blob.
124 bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *config) {
125         if(n->canonical_address) {
126                 return true;
127         }
128
129         packmsg_input_t in = {config->buf, config->len};
130         uint32_t version = packmsg_get_uint32(&in);
131
132         if(version != MESHLINK_CONFIG_VERSION) {
133                 return false;
134         }
135
136         char *name = packmsg_get_str_dup(&in);
137
138         if(!name) {
139                 return false;
140         }
141
142         if(n->name) {
143                 if(strcmp(n->name, name)) {
144                         free(name);
145                         return false;
146                 }
147
148                 free(name);
149         } else {
150                 n->name = name;
151         }
152
153         char *submesh_name = packmsg_get_str_dup(&in);
154
155         if(!strcmp(submesh_name, CORE_MESH)) {
156                 free(submesh_name);
157                 n->submesh = NULL;
158         } else {
159                 n->submesh = lookup_or_create_submesh(mesh, submesh_name);
160                 free(submesh_name);
161
162                 if(!n->submesh) {
163                         return false;
164                 }
165         }
166
167         n->devclass = packmsg_get_int32(&in);
168         n->status.blacklisted = packmsg_get_bool(&in);
169         const void *key;
170         uint32_t len = packmsg_get_bin_raw(&in, &key);
171
172         if(len) {
173                 if(len != 32) {
174                         return false;
175                 }
176
177                 if(!ecdsa_active(n->ecdsa)) {
178                         n->ecdsa = ecdsa_set_public_key(key);
179                 }
180         }
181
182         n->canonical_address = packmsg_get_str_dup(&in);
183         uint32_t count = packmsg_get_array(&in);
184
185         if(count > 5) {
186                 count = 5;
187         }
188
189         for(uint32_t i = 0; i < count; i++) {
190                 n->recent[i] = packmsg_get_sockaddr(&in);
191         }
192
193         return packmsg_done(&in);
194 }
195
196 bool node_write_config(meshlink_handle_t *mesh, node_t *n) {
197         if(!mesh->confbase) {
198                 return true;
199         }
200
201         uint8_t buf[4096];
202         packmsg_output_t out = {buf, sizeof(buf)};
203
204         packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
205         packmsg_add_str(&out, n->name);
206         packmsg_add_str(&out, n->submesh ? n->submesh->name : CORE_MESH);
207         packmsg_add_int32(&out, n->devclass);
208         packmsg_add_bool(&out, n->status.blacklisted);
209
210         if(ecdsa_active(n->ecdsa)) {
211                 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
212         } else {
213                 packmsg_add_bin(&out, "", 0);
214         }
215
216         packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
217
218         uint32_t count = 0;
219
220         for(uint32_t i = 0; i < 5; i++) {
221                 if(n->recent[i].sa.sa_family) {
222                         count++;
223                 } else {
224                         break;
225                 }
226         }
227
228         packmsg_add_array(&out, count);
229
230         for(uint32_t i = 0; i < count; i++) {
231                 packmsg_add_sockaddr(&out, &n->recent[i]);
232         }
233
234         if(!packmsg_output_ok(&out)) {
235                 meshlink_errno = MESHLINK_EINTERNAL;
236                 return false;
237         }
238
239         config_t config = {buf, packmsg_output_size(&out, buf)};
240
241         if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
242                 call_error_cb(mesh, MESHLINK_ESTORAGE);
243                 return false;
244         }
245
246         return true;
247 }
248
249 static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
250         (void)priv;
251
252         if(!check_id(name)) {
253                 return true;
254         }
255
256         node_t *n = lookup_node(mesh, name);
257
258         if(n) {
259                 return true;
260         }
261
262         n = new_node();
263         n->name = xstrdup(name);
264
265         config_t config;
266         packmsg_input_t in;
267
268         if(!node_get_config(mesh, n, &config, &in)) {
269                 free_node(n);
270                 return false;
271         }
272
273         if(!node_read_from_config(mesh, n, &config)) {
274                 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
275                 config_free(&config);
276                 free_node(n);
277                 return false;
278         }
279
280         config_free(&config);
281
282         node_add(mesh, n);
283
284         return true;
285 }
286
287 /*
288   Add listening sockets.
289 */
290 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
291         char *port = mesh->myport;
292
293         if(address) {
294                 char *space = strchr(address, ' ');
295
296                 if(space) {
297                         *space++ = 0;
298                         port = space;
299                 }
300
301                 if(!strcmp(address, "*")) {
302                         *address = 0;
303                 }
304         }
305
306         struct addrinfo *ai;
307
308         struct addrinfo hint = {
309                 .ai_family = AF_UNSPEC,
310                 .ai_socktype = SOCK_STREAM,
311                 .ai_protocol = IPPROTO_TCP,
312                 .ai_flags = AI_PASSIVE,
313         };
314
315         int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
316
317         free(address);
318
319         if(err || !ai) {
320                 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
321                 return false;
322         }
323
324         bool success = false;
325
326         for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
327                 // Ignore duplicate addresses
328                 bool found = false;
329
330                 for(int i = 0; i < mesh->listen_sockets; i++)
331                         if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
332                                 found = true;
333                                 break;
334                         }
335
336                 if(found) {
337                         continue;
338                 }
339
340                 if(mesh->listen_sockets >= MAXSOCKETS) {
341                         logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
342                         return false;
343                 }
344
345                 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
346
347                 if(tcp_fd < 0) {
348                         continue;
349                 }
350
351                 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
352
353                 if(udp_fd < 0) {
354                         close(tcp_fd);
355                         continue;
356                 }
357
358                 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);
359                 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);
360
361                 if(mesh->log_level >= MESHLINK_INFO) {
362                         char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
363                         logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
364                         free(hostname);
365                 }
366
367                 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
368                 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
369                 memcpy(&mesh->listen_socket[mesh->listen_sockets].broadcast_sa, aip->ai_addr, aip->ai_addrlen);
370
371                 if(aip->ai_family == AF_INET6) {
372                         mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0x0] = 0xff;
373                         mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0x1] = 0x02;
374                         mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0xf] = 0x01;
375                 } else {
376                         mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in.sin_addr.s_addr = 0xffffffff;
377                 }
378
379                 mesh->listen_sockets++;
380                 success = true;
381         }
382
383         freeaddrinfo(ai);
384         return success;
385 }
386
387 /*
388   Configure node_t mesh->self and set up the local sockets (listen only)
389 */
390 bool setup_myself(meshlink_handle_t *mesh) {
391         /* Set some defaults */
392
393         mesh->maxtimeout = 900;
394
395         /* Done */
396
397         mesh->self->nexthop = mesh->self;
398         mesh->self->status.reachable = true;
399         mesh->self->last_state_change = mesh->loop.now.tv_sec;
400
401         node_add(mesh, mesh->self);
402
403         graph(mesh);
404
405         if(!config_scan_all(mesh, "current", "hosts", load_node, NULL)) {
406                 logger(mesh, MESHLINK_WARNING, "Could not scan all host config files");
407         }
408
409         /* Open sockets */
410
411         mesh->listen_sockets = 0;
412
413         if(!add_listen_address(mesh, NULL, NULL)) {
414                 if(strcmp(mesh->myport, "0")) {
415                         logger(mesh, MESHLINK_INFO, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
416                         free(mesh->myport);
417                         mesh->myport = strdup("0");
418
419                         if(!mesh->myport) {
420                                 return false;
421                         }
422
423                         if(!add_listen_address(mesh, NULL, NULL)) {
424                                 return false;
425                         }
426                 } else {
427                         return false;
428                 }
429         }
430
431         if(!mesh->listen_sockets) {
432                 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
433                 return false;
434         }
435
436         /* Done. */
437
438         mesh->last_config_check = mesh->loop.now.tv_sec;
439
440         return true;
441 }
442
443 /*
444   initialize network
445 */
446 bool setup_network(meshlink_handle_t *mesh) {
447         init_connections(mesh);
448         init_submeshes(mesh);
449         init_nodes(mesh);
450         init_edges(mesh);
451         init_requests(mesh);
452
453         if(!setup_myself(mesh)) {
454                 return false;
455         }
456
457         return true;
458 }
459
460 /*
461   close all open network connections
462 */
463 void close_network_connections(meshlink_handle_t *mesh) {
464         if(mesh->connections) {
465                 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
466                         next = node->next;
467                         connection_t *c = node->data;
468                         c->outgoing = NULL;
469                         terminate_connection(mesh, c, false);
470                 }
471         }
472
473         for(int i = 0; i < mesh->listen_sockets; i++) {
474                 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
475                 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
476                 close(mesh->listen_socket[i].tcp.fd);
477                 close(mesh->listen_socket[i].udp.fd);
478         }
479
480         exit_requests(mesh);
481         exit_edges(mesh);
482         exit_nodes(mesh);
483         exit_submeshes(mesh);
484         exit_connections(mesh);
485
486         free(mesh->myport);
487         mesh->myport = NULL;
488
489         mesh->self = NULL;
490
491         return;
492 }