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