+ in->ptr = config->buf;
+ in->len = config->len;
+
+ uint32_t version = packmsg_get_uint32(in);
+
+ if(version != MESHLINK_CONFIG_VERSION) {
+ logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
+ config_free(config);
+ return false;
+ }
+
+ const char *name;
+ uint32_t len = packmsg_get_str_raw(in, &name);
+
+ if(len != strlen(n->name) || !name || strncmp(name, n->name, len)) {
+ logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
+ config_free(config);
+ return false;
+ }
+
+ return true;
+}
+
+/// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
+bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
+ if(ecdsa_active(n->ecdsa)) {
+ return true;
+ }
+
+ config_t config;
+ packmsg_input_t in;
+
+ if(!node_get_config(mesh, n, &config, &in)) {
+ return false;
+ }
+
+ packmsg_skip_element(&in); /* submesh */
+ packmsg_get_int32(&in); /* devclass */
+ packmsg_get_bool(&in); /* blacklisted */
+
+ const void *key;
+ uint32_t len = packmsg_get_bin_raw(&in, &key);
+
+ if(len != 32) {
+ config_free(&config);
+ return false;
+ }
+
+ n->ecdsa = ecdsa_set_public_key(key);
+
+ // While we are at it, read known address information
+ if(!n->canonical_address) {
+ n->canonical_address = packmsg_get_str_dup(&in);
+ } else {
+ packmsg_skip_element(&in);
+ }
+
+ // Append any known addresses in the config file to the list we currently have
+ uint32_t known_count = 0;
+
+ for(uint32_t i = 0; i < MAX_RECENT; i++) {
+ if(n->recent[i].sa.sa_family) {
+ known_count++;
+ }
+ }
+
+ uint32_t count = packmsg_get_array(&in);
+
+ for(uint32_t i = 0; i < count; i++) {
+ if(i < MAX_RECENT - known_count) {
+ n->recent[i + known_count] = packmsg_get_sockaddr(&in);
+ } else {
+ packmsg_skip_element(&in);
+ }
+ }
+
+ time_t last_reachable = packmsg_get_int64(&in);
+ time_t last_unreachable = packmsg_get_int64(&in);
+
+ if(!n->last_reachable) {
+ n->last_reachable = last_reachable;
+ }
+
+ if(!n->last_unreachable) {
+ n->last_unreachable = last_unreachable;
+ }
+
+ config_free(&config);
+ return true;
+}
+
+/// Fill in node details from a config blob.
+bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *config) {
+ if(n->canonical_address) {
+ return true;
+ }
+
+ packmsg_input_t in = {config->buf, config->len};
+ uint32_t version = packmsg_get_uint32(&in);
+
+ if(version != MESHLINK_CONFIG_VERSION) {
+ return false;
+ }
+
+ char *name = packmsg_get_str_dup(&in);
+
+ if(!name) {
+ return false;
+ }
+
+ if(n->name) {
+ if(strcmp(n->name, name)) {
+ free(name);
+ return false;
+ }
+
+ free(name);
+ } else {
+ n->name = name;
+ }
+
+ char *submesh_name = packmsg_get_str_dup(&in);
+
+ if(!strcmp(submesh_name, CORE_MESH)) {
+ free(submesh_name);
+ n->submesh = NULL;
+ } else {
+ n->submesh = lookup_or_create_submesh(mesh, submesh_name);
+ free(submesh_name);
+
+ if(!n->submesh) {
+ return false;
+ }
+ }
+
+ n->devclass = packmsg_get_int32(&in);
+ n->status.blacklisted = packmsg_get_bool(&in);
+ const void *key;
+ uint32_t len = packmsg_get_bin_raw(&in, &key);
+
+ if(len) {
+ if(len != 32) {
+ return false;
+ }
+
+ if(!ecdsa_active(n->ecdsa)) {
+ n->ecdsa = ecdsa_set_public_key(key);
+ }
+ }
+
+ n->canonical_address = packmsg_get_str_dup(&in);
+ uint32_t count = packmsg_get_array(&in);
+
+ for(uint32_t i = 0; i < count; i++) {
+ if(i < MAX_RECENT) {
+ n->recent[i] = packmsg_get_sockaddr(&in);
+ } else {
+ packmsg_skip_element(&in);
+ }
+ }
+
+ n->last_reachable = packmsg_get_int64(&in);
+ n->last_unreachable = packmsg_get_int64(&in);
+
+ return packmsg_done(&in);
+}
+
+bool node_write_config(meshlink_handle_t *mesh, node_t *n) {
+ if(!mesh->confbase) {
+ return true;
+ }
+
+ uint8_t buf[4096];
+ packmsg_output_t out = {buf, sizeof(buf)};
+
+ packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
+ packmsg_add_str(&out, n->name);
+ packmsg_add_str(&out, n->submesh ? n->submesh->name : CORE_MESH);
+ packmsg_add_int32(&out, n->devclass);
+ packmsg_add_bool(&out, n->status.blacklisted);
+
+ if(ecdsa_active(n->ecdsa)) {
+ packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
+ } else {
+ packmsg_add_bin(&out, "", 0);
+ }
+
+ packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
+
+ uint32_t count = 0;
+
+ for(uint32_t i = 0; i < MAX_RECENT; i++) {
+ if(n->recent[i].sa.sa_family) {
+ count++;
+ } else {
+ break;
+ }
+ }
+
+ packmsg_add_array(&out, count);
+
+ for(uint32_t i = 0; i < count; i++) {
+ packmsg_add_sockaddr(&out, &n->recent[i]);
+ }
+
+ packmsg_add_int64(&out, n->last_reachable);
+ packmsg_add_int64(&out, n->last_unreachable);
+
+ if(!packmsg_output_ok(&out)) {
+ meshlink_errno = MESHLINK_EINTERNAL;
+ return false;
+ }
+
+ config_t config = {buf, packmsg_output_size(&out, buf)};
+
+ if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
+ call_error_cb(mesh, MESHLINK_ESTORAGE);
+ return false;
+ }
+
+ return true;
+}
+
+static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
+ (void)priv;
+
+ if(!check_id(name)) {
+ return true;
+ }
+
+ node_t *n = lookup_node(mesh, name);
+
+ if(n) {
+ return true;
+ }