-int read_rsa_private_key(void)
-{
- FILE *fp;
- char *fname, *key;
-cp
- if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key))
- {
- myself->connection->rsa_key = RSA_new();
- BN_hex2bn(&myself->connection->rsa_key->d, key);
- BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
- free(key);
- return 0;
- }
-
- if(!get_config_string(lookup_config(config_tree, "PrivateKeyFile"), &fname))
- asprintf(&fname, "%s/rsa_key.priv", confbase);
-
- if(is_safe_path(fname))
- {
- if((fp = fopen(fname, "r")) == NULL)
- {
- syslog(LOG_ERR, _("Error reading RSA private key file `%s': %s"),
- fname, strerror(errno));
- free(fname);
- return -1;
- }
- free(fname);
- myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
- fclose(fp);
- if(!myself->connection->rsa_key)
- {
- syslog(LOG_ERR, _("Reading RSA private key file `%s' failed: %s"),
- fname, strerror(errno));
- return -1;
- }
- return 0;
- }
-
- free(fname);
- return -1;
+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;
+ }
+
+ n = new_node();
+ n->name = xstrdup(name);
+
+ config_t config;
+ packmsg_input_t in;
+
+ if(!node_get_config(mesh, n, &config, &in)) {
+ free_node(n);
+ return false;
+ }
+
+ if(!node_read_from_config(mesh, n, &config)) {
+ logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
+ config_free(&config);
+ free_node(n);
+ return false;
+ }
+
+ config_free(&config);
+
+ node_add(mesh, n);
+
+ return true;
+}
+
+int setup_tcp_listen_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
+ int nfd = socket(aip->ai_family, SOCK_STREAM, IPPROTO_TCP);
+
+ if(nfd == -1) {
+ return -1;
+ }
+
+#ifdef FD_CLOEXEC
+ fcntl(nfd, F_SETFD, FD_CLOEXEC);
+#endif
+
+ int option = 1;
+ setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
+
+#if defined(IPV6_V6ONLY)
+
+ if(aip->ai_family == AF_INET6) {
+ setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
+ }
+
+#else
+#warning IPV6_V6ONLY not defined
+#endif
+
+ if(bind(nfd, aip->ai_addr, aip->ai_addrlen)) {
+ closesocket(nfd);
+ return -1;
+ }
+
+ if(listen(nfd, 3)) {
+ logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
+ closesocket(nfd);
+ return -1;
+ }
+
+ return nfd;
+}
+
+int setup_udp_listen_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
+ int nfd = socket(aip->ai_family, SOCK_DGRAM, IPPROTO_UDP);
+
+ if(nfd == -1) {
+ return -1;
+ }
+
+#ifdef FD_CLOEXEC
+ fcntl(nfd, F_SETFD, FD_CLOEXEC);
+#endif
+
+#ifdef O_NONBLOCK
+ int flags = fcntl(nfd, F_GETFL);
+
+ if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
+ closesocket(nfd);
+ logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "fcntl", strerror(errno));
+ return -1;
+ }
+
+#elif defined(WIN32)
+ unsigned long arg = 1;
+
+ if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
+ closesocket(nfd);
+ logger(mesh, MESHLINK_ERROR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
+ return -1;
+ }
+
+#endif
+
+ int option = 1;
+ setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
+ setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
+
+#if defined(IPV6_V6ONLY)
+
+ if(aip->ai_family == AF_INET6) {
+ setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
+ }
+
+#endif
+
+#if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
+#define IP_DONTFRAGMENT IP_DONTFRAG
+#endif
+
+#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
+ option = IP_PMTUDISC_DO;
+ setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
+#elif defined(IP_DONTFRAGMENT)
+ option = 1;
+ setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
+#endif
+
+ if(aip->ai_family == AF_INET6) {
+#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
+ option = IPV6_PMTUDISC_DO;
+ setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
+#elif defined(IPV6_DONTFRAG)
+ option = 1;
+ setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
+#endif
+ }
+
+ if(bind(nfd, aip->ai_addr, aip->ai_addrlen)) {
+ closesocket(nfd);
+ return -1;
+ }
+
+ return nfd;