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"
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)) {
41 in->ptr = config->buf;
42 in->len = config->len;
44 uint32_t version = packmsg_get_uint32(in);
46 if(version != MESHLINK_CONFIG_VERSION) {
47 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
53 uint32_t len = packmsg_get_str_raw(in, &name);
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);
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)) {
73 if(!node_get_config(mesh, n, &config, &in)) {
77 packmsg_skip_element(&in); /* submesh */
78 packmsg_get_int32(&in); /* devclass */
79 packmsg_get_bool(&in); /* blacklisted */
82 uint32_t len = packmsg_get_bin_raw(&in, &key);
89 n->ecdsa = ecdsa_set_public_key(key);
91 // While we are at it, read known address information
92 if(!n->canonical_address) {
93 n->canonical_address = packmsg_get_str_dup(&in);
95 if(!*n->canonical_address) {
96 free(n->canonical_address);
97 n->canonical_address = NULL;
100 packmsg_skip_element(&in);
103 // Append any known addresses in the config file to the list we currently have
104 uint32_t known_count = 0;
106 for(uint32_t i = 0; i < MAX_RECENT; i++) {
107 if(n->recent[i].sa.sa_family) {
112 uint32_t count = packmsg_get_array(&in);
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);
118 packmsg_skip_element(&in);
122 packmsg_skip_element(&in); // last_reachable
123 packmsg_skip_element(&in); // last_unreachable
125 config_free(&config);
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) {
133 if(n->canonical_address) {
137 packmsg_input_t in = {config->buf, config->len};
138 uint32_t version = packmsg_get_uint32(&in);
140 if(version != MESHLINK_CONFIG_VERSION) {
144 char *name = packmsg_get_str_dup(&in);
151 if(strcmp(n->name, name)) {
161 packmsg_skip_element(&in); // submesh_name
163 n->devclass = packmsg_get_int32(&in);
164 n->status.blacklisted = packmsg_get_bool(&in);
166 uint32_t len = packmsg_get_bin_raw(&in, &key);
173 if(!ecdsa_active(n->ecdsa)) {
174 n->ecdsa = ecdsa_set_public_key(key);
178 n->canonical_address = packmsg_get_str_dup(&in);
180 if(!*n->canonical_address) {
181 free(n->canonical_address);
182 n->canonical_address = NULL;
185 uint32_t count = packmsg_get_array(&in);
187 for(uint32_t i = 0; i < count; i++) {
189 n->recent[i] = packmsg_get_sockaddr(&in);
191 packmsg_skip_element(&in);
195 packmsg_skip_element(&in); // last_reachable
196 packmsg_skip_element(&in); // last_unreachable
198 return packmsg_done(&in);
201 bool node_write_config(meshlink_handle_t *mesh, node_t *n, bool new_key) {
202 if(!mesh->confbase) {
206 switch(mesh->storage_policy) {
207 case MESHLINK_STORAGE_KEYS_ONLY:
214 case MESHLINK_STORAGE_DISABLED:
222 packmsg_output_t out = {buf, sizeof(buf)};
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);
230 if(ecdsa_active(n->ecdsa)) {
231 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
233 packmsg_add_bin(&out, "", 0);
236 packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
240 for(uint32_t i = 0; i < MAX_RECENT; i++) {
241 if(n->recent[i].sa.sa_family) {
248 packmsg_add_array(&out, count);
250 for(uint32_t i = 0; i < count; i++) {
251 packmsg_add_sockaddr(&out, &n->recent[i]);
254 packmsg_add_int64(&out, 0); // last_reachable
255 packmsg_add_int64(&out, 0); // last_unreachable
257 if(!packmsg_output_ok(&out)) {
258 meshlink_errno = MESHLINK_EINTERNAL;
262 config_t config = {buf, packmsg_output_size(&out, buf)};
264 if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
265 call_error_cb(mesh, MESHLINK_ESTORAGE);
269 n->status.dirty = false;
273 static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
276 if(!check_id(name)) {
277 // Check if this is a temporary file, if so remove it
278 const char *suffix = strstr(name, ".tmp");
280 if(suffix && !suffix[4]) {
281 char filename[PATH_MAX];
282 snprintf(filename, sizeof(filename), "%s" SLASH "current" SLASH "hosts", mesh->confbase);
289 node_t *n = lookup_node(mesh, name);
296 n->name = xstrdup(name);
301 if(!node_get_config(mesh, n, &config, &in)) {
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);
313 config_free(&config);
321 Configure node_t mesh->self and set up the local sockets (listen only)
323 static bool setup_myself(meshlink_handle_t *mesh) {
324 mesh->self->nexthop = mesh->self;
326 node_add(mesh, mesh->self);
328 if(!config_scan_all(mesh, "current", "hosts", load_node, NULL)) {
329 logger(mesh, MESHLINK_WARNING, "Could not scan all host config files");
334 mesh->last_unreachable = mesh->loop.now.tv_sec;
342 bool setup_network(meshlink_handle_t *mesh) {
343 init_connections(mesh);
347 if(!setup_myself(mesh)) {
355 close all open network connections
357 void close_network_connections(meshlink_handle_t *mesh) {
358 if(mesh->connections) {
359 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
361 connection_t *c = node->data;
363 terminate_connection(mesh, c, false);
369 exit_connections(mesh);