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"
27 #include "meshlink_internal.h"
37 /// Helper function to start parsing a host config file
38 static bool node_get_config(meshlink_handle_t *mesh, node_t *n, config_t *config, packmsg_input_t *in) {
39 if(!config_read(mesh, "current", n->name, config, mesh->config_key)) {
43 in->ptr = config->buf;
44 in->len = config->len;
46 uint32_t version = packmsg_get_uint32(in);
48 if(version != MESHLINK_CONFIG_VERSION) {
49 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
55 uint32_t len = packmsg_get_str_raw(in, &name);
57 if(len != strlen(n->name) || !name || strncmp(name, n->name, len)) {
58 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
66 /// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
67 bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
68 if(ecdsa_active(n->ecdsa)) {
75 if(!node_get_config(mesh, n, &config, &in)) {
79 packmsg_skip_element(&in); /* submesh */
80 packmsg_get_int32(&in); /* devclass */
81 packmsg_get_bool(&in); /* blacklisted */
84 uint32_t len = packmsg_get_bin_raw(&in, &key);
91 n->ecdsa = ecdsa_set_public_key(key);
93 // While we are at it, read known address information
94 if(!n->canonical_address) {
95 n->canonical_address = packmsg_get_str_dup(&in);
97 packmsg_skip_element(&in);
100 // Append any known addresses in the config file to the list we currently have
101 uint32_t known_count = 0;
103 for(uint32_t i = 0; i < MAX_RECENT; i++) {
104 if(n->recent[i].sa.sa_family) {
109 uint32_t count = packmsg_get_array(&in);
111 for(uint32_t i = 0; i < count; i++) {
112 if(i < MAX_RECENT - known_count) {
113 n->recent[i + known_count] = packmsg_get_sockaddr(&in);
115 packmsg_skip_element(&in);
119 time_t last_reachable = packmsg_get_int64(&in);
120 time_t last_unreachable = packmsg_get_int64(&in);
122 if(!n->last_reachable) {
123 n->last_reachable = last_reachable;
126 if(!n->last_unreachable) {
127 n->last_unreachable = last_unreachable;
130 config_free(&config);
134 /// Fill in node details from a config blob.
135 bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *config) {
136 if(n->canonical_address) {
140 packmsg_input_t in = {config->buf, config->len};
141 uint32_t version = packmsg_get_uint32(&in);
143 if(version != MESHLINK_CONFIG_VERSION) {
147 char *name = packmsg_get_str_dup(&in);
154 if(strcmp(n->name, name)) {
164 char *submesh_name = packmsg_get_str_dup(&in);
166 if(!strcmp(submesh_name, CORE_MESH)) {
170 n->submesh = lookup_or_create_submesh(mesh, submesh_name);
178 n->devclass = packmsg_get_int32(&in);
179 n->status.blacklisted = packmsg_get_bool(&in);
181 uint32_t len = packmsg_get_bin_raw(&in, &key);
188 if(!ecdsa_active(n->ecdsa)) {
189 n->ecdsa = ecdsa_set_public_key(key);
193 n->canonical_address = packmsg_get_str_dup(&in);
194 uint32_t count = packmsg_get_array(&in);
196 for(uint32_t i = 0; i < count; i++) {
198 n->recent[i] = packmsg_get_sockaddr(&in);
200 packmsg_skip_element(&in);
204 n->last_reachable = packmsg_get_int64(&in);
205 n->last_unreachable = packmsg_get_int64(&in);
207 return packmsg_done(&in);
210 bool node_write_config(meshlink_handle_t *mesh, node_t *n) {
211 if(!mesh->confbase) {
216 packmsg_output_t out = {buf, sizeof(buf)};
218 packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
219 packmsg_add_str(&out, n->name);
220 packmsg_add_str(&out, n->submesh ? n->submesh->name : CORE_MESH);
221 packmsg_add_int32(&out, n->devclass);
222 packmsg_add_bool(&out, n->status.blacklisted);
224 if(ecdsa_active(n->ecdsa)) {
225 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
227 packmsg_add_bin(&out, "", 0);
230 packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
234 for(uint32_t i = 0; i < MAX_RECENT; i++) {
235 if(n->recent[i].sa.sa_family) {
242 packmsg_add_array(&out, count);
244 for(uint32_t i = 0; i < count; i++) {
245 packmsg_add_sockaddr(&out, &n->recent[i]);
248 packmsg_add_int64(&out, n->last_reachable);
249 packmsg_add_int64(&out, n->last_unreachable);
251 if(!packmsg_output_ok(&out)) {
252 meshlink_errno = MESHLINK_EINTERNAL;
256 config_t config = {buf, packmsg_output_size(&out, buf)};
258 if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
259 call_error_cb(mesh, MESHLINK_ESTORAGE);
266 static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
269 if(!check_id(name)) {
273 node_t *n = lookup_node(mesh, name);
280 n->name = xstrdup(name);
285 if(!node_get_config(mesh, n, &config, &in)) {
290 if(!node_read_from_config(mesh, n, &config)) {
291 logger(mesh, MESHLINK_ERROR, "Invalid config file for node %s", n->name);
292 config_free(&config);
297 config_free(&config);
305 Add listening sockets.
307 static bool add_listen_sockets(meshlink_handle_t *mesh) {
310 struct addrinfo hint = {
311 .ai_family = AF_UNSPEC,
312 .ai_socktype = SOCK_STREAM,
313 .ai_protocol = IPPROTO_TCP,
314 .ai_flags = AI_PASSIVE,
317 int err = getaddrinfo(NULL, mesh->myport, &hint, &ai);
320 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
324 bool success = false;
326 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
327 // Ignore duplicate addresses
330 for(int i = 0; i < mesh->listen_sockets; i++) {
331 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
341 if(mesh->listen_sockets >= MAXSOCKETS) {
342 logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
346 /* Try to bind to TCP */
348 int tcp_fd = socket(aip->ai_family, SOCK_STREAM, IPPROTO_TCP);
354 if(bind(tcp_fd, aip->ai_addr, aip->ai_addrlen)) {
357 if(errno == EADDRINUSE) {
358 /* If this port is in use for any address family, avoid it. */
366 if(!setup_listen_socket(mesh, tcp_fd, aip->ai_family)) {
372 /* If TCP worked, then we require that UDP works as well. */
374 int udp_fd = socket(aip->ai_family, SOCK_DGRAM, IPPROTO_UDP);
382 if(bind(udp_fd, aip->ai_addr, aip->ai_addrlen) || !setup_vpn_in_socket(mesh, udp_fd, aip->ai_family)) {
389 io_add(&mesh->loop, &mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ);
390 io_add(&mesh->loop, &mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ);
392 if(mesh->log_level >= MESHLINK_INFO) {
393 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
394 logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
398 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
399 memcpy(&mesh->listen_socket[mesh->listen_sockets].broadcast_sa, aip->ai_addr, aip->ai_addrlen);
401 if(aip->ai_family == AF_INET6) {
402 mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0x0] = 0xff;
403 mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0x1] = 0x02;
404 mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0xf] = 0x01;
406 mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in.sin_addr.s_addr = 0xffffffff;
409 mesh->listen_sockets++;
416 for(int i = 0; i < mesh->listen_sockets; i++) {
417 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
418 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
419 close(mesh->listen_socket[i].tcp.fd);
420 close(mesh->listen_socket[i].udp.fd);
423 mesh->listen_sockets = 0;
430 Configure node_t mesh->self and set up the local sockets (listen only)
432 bool setup_myself(meshlink_handle_t *mesh) {
433 /* Set some defaults */
435 mesh->maxtimeout = 900;
439 mesh->self->nexthop = mesh->self;
441 node_add(mesh, mesh->self);
443 if(!config_scan_all(mesh, "current", "hosts", load_node, NULL)) {
444 logger(mesh, MESHLINK_WARNING, "Could not scan all host config files");
449 mesh->listen_sockets = 0;
451 if(!add_listen_sockets(mesh)) {
452 if(strcmp(mesh->myport, "0")) {
453 logger(mesh, MESHLINK_WARNING, "Could not bind to port %s, trying to find an alternative port", mesh->myport);
455 if(!check_port(mesh)) {
456 logger(mesh, MESHLINK_WARNING, "Could not bind to any port, trying to bind to port 0");
458 mesh->myport = xstrdup("0");
461 if(!add_listen_sockets(mesh)) {
469 if(!mesh->listen_sockets) {
470 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
476 mesh->last_config_check = mesh->loop.now.tv_sec;
477 mesh->last_unreachable = mesh->loop.now.tv_sec;
485 bool setup_network(meshlink_handle_t *mesh) {
486 init_connections(mesh);
487 init_submeshes(mesh);
492 if(!setup_myself(mesh)) {
500 close all open network connections
502 void close_network_connections(meshlink_handle_t *mesh) {
503 if(mesh->connections) {
504 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
506 connection_t *c = node->data;
508 terminate_connection(mesh, c, false);
512 for(int i = 0; i < mesh->listen_sockets; i++) {
513 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
514 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
515 close(mesh->listen_socket[i].tcp.fd);
516 close(mesh->listen_socket[i].udp.fd);
522 exit_submeshes(mesh);
523 exit_connections(mesh);