]> git.meshlink.io Git - meshlink-tiny/blob - src/net_setup.c
Remove listening sockets.
[meshlink-tiny] / 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 "logger.h"
26 #include "meshlink_internal.h"
27 #include "net.h"
28 #include "netutl.h"
29 #include "packmsg.h"
30 #include "protocol.h"
31 #include "route.h"
32 #include "utils.h"
33 #include "xalloc.h"
34 #include "submesh.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, "current", n->name, config, mesh->config_key)) {
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                 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
49                 config_free(config);
50                 return false;
51         }
52
53         const char *name;
54         uint32_t len = packmsg_get_str_raw(in, &name);
55
56         if(len != strlen(n->name) || !name || strncmp(name, n->name, len)) {
57                 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
58                 config_free(config);
59                 return false;
60         }
61
62         return true;
63 }
64
65 /// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
66 bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
67         if(ecdsa_active(n->ecdsa)) {
68                 return true;
69         }
70
71         config_t config;
72         packmsg_input_t in;
73
74         if(!node_get_config(mesh, n, &config, &in)) {
75                 return false;
76         }
77
78         packmsg_skip_element(&in); /* submesh */
79         packmsg_get_int32(&in); /* devclass */
80         packmsg_get_bool(&in); /* blacklisted */
81
82         const void *key;
83         uint32_t len = packmsg_get_bin_raw(&in, &key);
84
85         if(len != 32) {
86                 config_free(&config);
87                 return false;
88         }
89
90         n->ecdsa = ecdsa_set_public_key(key);
91
92         // While we are at it, read known address information
93         if(!n->canonical_address) {
94                 n->canonical_address = packmsg_get_str_dup(&in);
95
96                 if(!*n->canonical_address) {
97                         free(n->canonical_address);
98                         n->canonical_address = NULL;
99                 }
100         } else {
101                 packmsg_skip_element(&in);
102         }
103
104         // Append any known addresses in the config file to the list we currently have
105         uint32_t known_count = 0;
106
107         for(uint32_t i = 0; i < MAX_RECENT; i++) {
108                 if(n->recent[i].sa.sa_family) {
109                         known_count++;
110                 }
111         }
112
113         uint32_t count = packmsg_get_array(&in);
114
115         for(uint32_t i = 0; i < count; i++) {
116                 if(i < MAX_RECENT - known_count) {
117                         n->recent[i + known_count] = packmsg_get_sockaddr(&in);
118                 } else {
119                         packmsg_skip_element(&in);
120                 }
121         }
122
123         packmsg_skip_element(&in); // last_reachable
124         packmsg_skip_element(&in); // last_unreachable
125
126         config_free(&config);
127         return true;
128 }
129
130 /// Fill in node details from a config blob.
131 bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *config) {
132         if(n->canonical_address) {
133                 return true;
134         }
135
136         packmsg_input_t in = {config->buf, config->len};
137         uint32_t version = packmsg_get_uint32(&in);
138
139         if(version != MESHLINK_CONFIG_VERSION) {
140                 return false;
141         }
142
143         char *name = packmsg_get_str_dup(&in);
144
145         if(!name) {
146                 return false;
147         }
148
149         if(n->name) {
150                 if(strcmp(n->name, name)) {
151                         free(name);
152                         return false;
153                 }
154
155                 free(name);
156         } else {
157                 n->name = name;
158         }
159
160         char *submesh_name = packmsg_get_str_dup(&in);
161
162         if(!strcmp(submesh_name, CORE_MESH)) {
163                 free(submesh_name);
164                 n->submesh = NULL;
165         } else {
166                 n->submesh = lookup_or_create_submesh(mesh, submesh_name);
167                 free(submesh_name);
168
169                 if(!n->submesh) {
170                         return false;
171                 }
172         }
173
174         n->devclass = packmsg_get_int32(&in);
175         n->status.blacklisted = packmsg_get_bool(&in);
176         const void *key;
177         uint32_t len = packmsg_get_bin_raw(&in, &key);
178
179         if(len) {
180                 if(len != 32) {
181                         return false;
182                 }
183
184                 if(!ecdsa_active(n->ecdsa)) {
185                         n->ecdsa = ecdsa_set_public_key(key);
186                 }
187         }
188
189         n->canonical_address = packmsg_get_str_dup(&in);
190
191         if(!*n->canonical_address) {
192                 free(n->canonical_address);
193                 n->canonical_address = NULL;
194         }
195
196         uint32_t count = packmsg_get_array(&in);
197
198         for(uint32_t i = 0; i < count; i++) {
199                 if(i < MAX_RECENT) {
200                         n->recent[i] = packmsg_get_sockaddr(&in);
201                 } else {
202                         packmsg_skip_element(&in);
203                 }
204         }
205
206         packmsg_skip_element(&in); // last_reachable
207         packmsg_skip_element(&in); // last_unreachable
208
209         return packmsg_done(&in);
210 }
211
212 bool node_write_config(meshlink_handle_t *mesh, node_t *n, bool new_key) {
213         if(!mesh->confbase) {
214                 return true;
215         }
216
217         switch(mesh->storage_policy) {
218         case MESHLINK_STORAGE_KEYS_ONLY:
219                 if(!new_key) {
220                         return true;
221                 }
222
223                 break;
224
225         case MESHLINK_STORAGE_DISABLED:
226                 return true;
227
228         default:
229                 break;
230         }
231
232         uint8_t buf[4096];
233         packmsg_output_t out = {buf, sizeof(buf)};
234
235         packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
236         packmsg_add_str(&out, n->name);
237         packmsg_add_str(&out, n->submesh ? n->submesh->name : CORE_MESH);
238         packmsg_add_int32(&out, n->devclass);
239         packmsg_add_bool(&out, n->status.blacklisted);
240
241         if(ecdsa_active(n->ecdsa)) {
242                 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
243         } else {
244                 packmsg_add_bin(&out, "", 0);
245         }
246
247         packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
248
249         uint32_t count = 0;
250
251         for(uint32_t i = 0; i < MAX_RECENT; i++) {
252                 if(n->recent[i].sa.sa_family) {
253                         count++;
254                 } else {
255                         break;
256                 }
257         }
258
259         packmsg_add_array(&out, count);
260
261         for(uint32_t i = 0; i < count; i++) {
262                 packmsg_add_sockaddr(&out, &n->recent[i]);
263         }
264
265         packmsg_add_int64(&out, 0); // last_reachable
266         packmsg_add_int64(&out, 0); // last_unreachable
267
268         if(!packmsg_output_ok(&out)) {
269                 meshlink_errno = MESHLINK_EINTERNAL;
270                 return false;
271         }
272
273         config_t config = {buf, packmsg_output_size(&out, buf)};
274
275         if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
276                 call_error_cb(mesh, MESHLINK_ESTORAGE);
277                 return false;
278         }
279
280         n->status.dirty = false;
281         return true;
282 }
283
284 static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
285         (void)priv;
286
287         if(!check_id(name)) {
288                 // Check if this is a temporary file, if so remove it
289                 const char *suffix = strstr(name, ".tmp");
290
291                 if(suffix && !suffix[4]) {
292                         char filename[PATH_MAX];
293                         snprintf(filename, sizeof(filename), "%s" SLASH "current" SLASH "hosts", mesh->confbase);
294                         unlink(filename);
295                 }
296
297                 return true;
298         }
299
300         node_t *n = lookup_node(mesh, name);
301
302         if(n) {
303                 return true;
304         }
305
306         n = new_node();
307         n->name = xstrdup(name);
308
309         config_t config;
310         packmsg_input_t in;
311
312         if(!node_get_config(mesh, n, &config, &in)) {
313                 free_node(n);
314                 return false;
315         }
316
317         if(!node_read_from_config(mesh, n, &config)) {
318                 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
319                 config_free(&config);
320                 free_node(n);
321                 return false;
322         }
323
324         config_free(&config);
325
326         node_add(mesh, n);
327
328         return true;
329 }
330
331 /*
332   Configure node_t mesh->self and set up the local sockets (listen only)
333 */
334 static bool setup_myself(meshlink_handle_t *mesh) {
335         mesh->self->nexthop = mesh->self;
336
337         node_add(mesh, mesh->self);
338
339         if(!config_scan_all(mesh, "current", "hosts", load_node, NULL)) {
340                 logger(mesh, MESHLINK_WARNING, "Could not scan all host config files");
341         }
342
343         /* Done. */
344
345         mesh->last_unreachable = mesh->loop.now.tv_sec;
346
347         return true;
348 }
349
350 /*
351   initialize network
352 */
353 bool setup_network(meshlink_handle_t *mesh) {
354         init_connections(mesh);
355         init_submeshes(mesh);
356         init_nodes(mesh);
357         init_requests(mesh);
358
359         if(!setup_myself(mesh)) {
360                 return false;
361         }
362
363         return true;
364 }
365
366 /*
367   close all open network connections
368 */
369 void close_network_connections(meshlink_handle_t *mesh) {
370         if(mesh->connections) {
371                 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
372                         next = node->next;
373                         connection_t *c = node->data;
374                         c->outgoing = NULL;
375                         terminate_connection(mesh, c, false);
376                 }
377         }
378
379         exit_requests(mesh);
380         exit_nodes(mesh);
381         exit_submeshes(mesh);
382         exit_connections(mesh);
383
384         free(mesh->myport);
385         mesh->myport = NULL;
386
387         mesh->self = NULL;
388
389         return;
390 }