3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
23 #include "connection.h"
26 #include "meshlink_internal.h"
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)) {
42 in->ptr = config->buf;
43 in->len = config->len;
45 uint32_t version = packmsg_get_uint32(in);
47 if(version != MESHLINK_CONFIG_VERSION) {
48 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
54 uint32_t len = packmsg_get_str_raw(in, &name);
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);
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)) {
74 if(!node_get_config(mesh, n, &config, &in)) {
78 packmsg_skip_element(&in); /* submesh */
79 packmsg_get_int32(&in); /* devclass */
80 packmsg_get_bool(&in); /* blacklisted */
83 uint32_t len = packmsg_get_bin_raw(&in, &key);
90 n->ecdsa = ecdsa_set_public_key(key);
92 // While we are at it, read known address information
93 if(!n->canonical_address) {
94 n->canonical_address = packmsg_get_str_dup(&in);
96 if(!*n->canonical_address) {
97 free(n->canonical_address);
98 n->canonical_address = NULL;
101 packmsg_skip_element(&in);
104 // Append any known addresses in the config file to the list we currently have
105 uint32_t known_count = 0;
107 for(uint32_t i = 0; i < MAX_RECENT; i++) {
108 if(n->recent[i].sa.sa_family) {
113 uint32_t count = packmsg_get_array(&in);
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);
119 packmsg_skip_element(&in);
123 packmsg_skip_element(&in); // last_reachable
124 packmsg_skip_element(&in); // last_unreachable
126 config_free(&config);
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) {
136 packmsg_input_t in = {config->buf, config->len};
137 uint32_t version = packmsg_get_uint32(&in);
139 if(version != MESHLINK_CONFIG_VERSION) {
143 char *name = packmsg_get_str_dup(&in);
150 if(strcmp(n->name, name)) {
160 char *submesh_name = packmsg_get_str_dup(&in);
162 if(!strcmp(submesh_name, CORE_MESH)) {
166 n->submesh = lookup_or_create_submesh(mesh, submesh_name);
174 n->devclass = packmsg_get_int32(&in);
175 n->status.blacklisted = packmsg_get_bool(&in);
177 uint32_t len = packmsg_get_bin_raw(&in, &key);
184 if(!ecdsa_active(n->ecdsa)) {
185 n->ecdsa = ecdsa_set_public_key(key);
189 n->canonical_address = packmsg_get_str_dup(&in);
191 if(!*n->canonical_address) {
192 free(n->canonical_address);
193 n->canonical_address = NULL;
196 uint32_t count = packmsg_get_array(&in);
198 for(uint32_t i = 0; i < count; i++) {
200 n->recent[i] = packmsg_get_sockaddr(&in);
202 packmsg_skip_element(&in);
206 packmsg_skip_element(&in); // last_reachable
207 packmsg_skip_element(&in); // last_unreachable
209 return packmsg_done(&in);
212 bool node_write_config(meshlink_handle_t *mesh, node_t *n, bool new_key) {
213 if(!mesh->confbase) {
217 switch(mesh->storage_policy) {
218 case MESHLINK_STORAGE_KEYS_ONLY:
225 case MESHLINK_STORAGE_DISABLED:
233 packmsg_output_t out = {buf, sizeof(buf)};
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);
241 if(ecdsa_active(n->ecdsa)) {
242 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
244 packmsg_add_bin(&out, "", 0);
247 packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
251 for(uint32_t i = 0; i < MAX_RECENT; i++) {
252 if(n->recent[i].sa.sa_family) {
259 packmsg_add_array(&out, count);
261 for(uint32_t i = 0; i < count; i++) {
262 packmsg_add_sockaddr(&out, &n->recent[i]);
265 packmsg_add_int64(&out, 0); // last_reachable
266 packmsg_add_int64(&out, 0); // last_unreachable
268 if(!packmsg_output_ok(&out)) {
269 meshlink_errno = MESHLINK_EINTERNAL;
273 config_t config = {buf, packmsg_output_size(&out, buf)};
275 if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
276 call_error_cb(mesh, MESHLINK_ESTORAGE);
280 n->status.dirty = false;
284 static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
287 if(!check_id(name)) {
288 // Check if this is a temporary file, if so remove it
289 const char *suffix = strstr(name, ".tmp");
291 if(suffix && !suffix[4]) {
292 char filename[PATH_MAX];
293 snprintf(filename, sizeof(filename), "%s" SLASH "current" SLASH "hosts", mesh->confbase);
300 node_t *n = lookup_node(mesh, name);
307 n->name = xstrdup(name);
312 if(!node_get_config(mesh, n, &config, &in)) {
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);
324 config_free(&config);
332 Configure node_t mesh->self and set up the local sockets (listen only)
334 static bool setup_myself(meshlink_handle_t *mesh) {
335 mesh->self->nexthop = mesh->self;
337 node_add(mesh, mesh->self);
339 if(!config_scan_all(mesh, "current", "hosts", load_node, NULL)) {
340 logger(mesh, MESHLINK_WARNING, "Could not scan all host config files");
345 mesh->last_unreachable = mesh->loop.now.tv_sec;
353 bool setup_network(meshlink_handle_t *mesh) {
354 init_connections(mesh);
355 init_submeshes(mesh);
359 if(!setup_myself(mesh)) {
367 close all open network connections
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) {
373 connection_t *c = node->data;
375 terminate_connection(mesh, c, false);
381 exit_submeshes(mesh);
382 exit_connections(mesh);