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"
34 /// Helper function to start parsing a host config file
35 static bool node_get_config(meshlink_handle_t *mesh, node_t *n, config_t *config, packmsg_input_t *in) {
36 if(!config_read(mesh, "current", n->name, config, mesh->config_key)) {
40 in->ptr = config->buf;
41 in->len = config->len;
43 uint32_t version = packmsg_get_uint32(in);
45 if(version != MESHLINK_CONFIG_VERSION) {
46 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
52 uint32_t len = packmsg_get_str_raw(in, &name);
54 if(len != strlen(n->name) || !name || strncmp(name, n->name, len)) {
55 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
63 /// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
64 bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
65 if(ecdsa_active(n->ecdsa)) {
72 if(!node_get_config(mesh, n, &config, &in)) {
76 packmsg_skip_element(&in); /* submesh */
77 packmsg_get_int32(&in); /* devclass */
78 packmsg_get_bool(&in); /* blacklisted */
81 uint32_t len = packmsg_get_bin_raw(&in, &key);
88 n->ecdsa = ecdsa_set_public_key(key);
90 // While we are at it, read known address information
91 if(!n->canonical_address) {
92 n->canonical_address = packmsg_get_str_dup(&in);
94 if(!*n->canonical_address) {
95 free(n->canonical_address);
96 n->canonical_address = NULL;
99 packmsg_skip_element(&in);
102 // Append any known addresses in the config file to the list we currently have
103 uint32_t known_count = 0;
105 for(uint32_t i = 0; i < MAX_RECENT; i++) {
106 if(n->recent[i].sa.sa_family) {
111 uint32_t count = packmsg_get_array(&in);
113 for(uint32_t i = 0; i < count; i++) {
114 if(i < MAX_RECENT - known_count) {
115 n->recent[i + known_count] = packmsg_get_sockaddr(&in);
117 packmsg_skip_element(&in);
121 packmsg_skip_element(&in); // last_reachable
122 packmsg_skip_element(&in); // last_unreachable
124 config_free(&config);
128 /// Fill in node details from a config blob.
129 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 packmsg_skip_element(&in); // submesh_name
162 n->devclass = packmsg_get_int32(&in);
163 n->status.blacklisted = packmsg_get_bool(&in);
165 uint32_t len = packmsg_get_bin_raw(&in, &key);
172 if(!ecdsa_active(n->ecdsa)) {
173 n->ecdsa = ecdsa_set_public_key(key);
177 n->canonical_address = packmsg_get_str_dup(&in);
179 if(!*n->canonical_address) {
180 free(n->canonical_address);
181 n->canonical_address = NULL;
184 uint32_t count = packmsg_get_array(&in);
186 for(uint32_t i = 0; i < count; i++) {
188 n->recent[i] = packmsg_get_sockaddr(&in);
190 packmsg_skip_element(&in);
194 packmsg_skip_element(&in); // last_reachable
195 packmsg_skip_element(&in); // last_unreachable
197 return packmsg_done(&in);
200 bool node_write_config(meshlink_handle_t *mesh, node_t *n, bool new_key) {
201 if(!mesh->confbase) {
205 switch(mesh->storage_policy) {
206 case MESHLINK_STORAGE_KEYS_ONLY:
213 case MESHLINK_STORAGE_DISABLED:
221 packmsg_output_t out = {buf, sizeof(buf)};
223 packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
224 packmsg_add_str(&out, n->name);
225 packmsg_add_str(&out, CORE_MESH);
226 packmsg_add_int32(&out, n->devclass);
227 packmsg_add_bool(&out, n->status.blacklisted);
229 if(ecdsa_active(n->ecdsa)) {
230 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
232 packmsg_add_bin(&out, "", 0);
235 packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
239 for(uint32_t i = 0; i < MAX_RECENT; i++) {
240 if(n->recent[i].sa.sa_family) {
247 packmsg_add_array(&out, count);
249 for(uint32_t i = 0; i < count; i++) {
250 packmsg_add_sockaddr(&out, &n->recent[i]);
253 packmsg_add_int64(&out, 0); // last_reachable
254 packmsg_add_int64(&out, 0); // last_unreachable
256 if(!packmsg_output_ok(&out)) {
257 meshlink_errno = MESHLINK_EINTERNAL;
261 config_t config = {buf, packmsg_output_size(&out, buf)};
263 if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
264 call_error_cb(mesh, MESHLINK_ESTORAGE);
268 n->status.dirty = false;
272 static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
275 if(!check_id(name)) {
276 // Check if this is a temporary file, if so remove it
277 const char *suffix = strstr(name, ".tmp");
279 if(suffix && !suffix[4]) {
280 char filename[PATH_MAX];
281 snprintf(filename, sizeof(filename), "%s" SLASH "current" SLASH "hosts", mesh->confbase);
288 node_t *n = lookup_node(mesh, name);
295 n->name = xstrdup(name);
300 if(!node_get_config(mesh, n, &config, &in)) {
305 if(!node_read_from_config(mesh, n, &config)) {
306 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
307 config_free(&config);
312 config_free(&config);
320 Configure node_t mesh->self and set up the local sockets (listen only)
322 static bool setup_myself(meshlink_handle_t *mesh) {
323 mesh->self->nexthop = mesh->self;
325 node_add(mesh, mesh->self);
327 if(!config_scan_all(mesh, "current", "hosts", load_node, NULL)) {
328 logger(mesh, MESHLINK_WARNING, "Could not scan all host config files");
333 mesh->last_unreachable = mesh->loop.now.tv_sec;
341 bool setup_network(meshlink_handle_t *mesh) {
342 init_connections(mesh);
345 if(!setup_myself(mesh)) {
353 close all open network connections
355 void close_network_connections(meshlink_handle_t *mesh) {
356 if(mesh->connection) {
357 mesh->connection->outgoing = NULL;
358 terminate_connection(mesh, mesh->connection, false);
362 exit_connections(mesh);