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) {
54 uint32_t len = packmsg_get_str_raw(in, &name);
56 if(len != strlen(n->name) || strncmp(name, n->name, len)) {
64 /// Read device class, blacklist status and submesh from a host config file. Used at startup when reading all host config files.
65 bool node_read_partial(meshlink_handle_t *mesh, node_t *n) {
69 if(!node_get_config(mesh, n, &config, &in)) {
73 char *submesh_name = packmsg_get_str_dup(&in);
75 if(!strcmp(submesh_name, CORE_MESH)) {
79 n->submesh = lookup_or_create_submesh(mesh, submesh_name);
88 int32_t devclass = packmsg_get_int32(&in);
89 bool blacklisted = packmsg_get_bool(&in);
92 if(!packmsg_input_ok(&in) || devclass < 0 || devclass > _DEV_CLASS_MAX) {
96 n->devclass = devclass;
97 n->status.blacklisted = blacklisted;
101 /// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
102 bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
103 if(ecdsa_active(n->ecdsa)) {
110 if(!node_get_config(mesh, n, &config, &in)) {
114 packmsg_skip_element(&in); /* submesh */
115 packmsg_get_int32(&in); /* devclass */
116 packmsg_get_bool(&in); /* blacklisted */
119 uint32_t len = packmsg_get_bin_raw(&in, &key);
122 config_free(&config);
126 n->ecdsa = ecdsa_set_public_key(key);
128 // While we are at it, read known address information
129 if(!n->canonical_address) {
130 n->canonical_address = packmsg_get_str_dup(&in);
132 packmsg_skip_element(&in);
135 // Append any known addresses in the config file to the list we currently have
136 uint32_t known_count = 0;
138 for(uint32_t i = 0; i < 5; i++) {
139 if(n->recent[i].sa.sa_family) {
144 uint32_t count = packmsg_get_array(&in);
146 if(count > 5 - known_count) {
147 count = 5 - known_count;
150 for(uint32_t i = 0; i < count; i++) {
151 n->recent[i + known_count] = packmsg_get_sockaddr(&in);
155 config_free(&config);
159 /// Fill in node details from a config blob.
160 bool node_read_from_config(meshlink_handle_t *mesh, node_t *n, const config_t *config) {
161 if(n->canonical_address) {
165 packmsg_input_t in = {config->buf, config->len};
166 uint32_t version = packmsg_get_uint32(&in);
168 if(version != MESHLINK_CONFIG_VERSION) {
172 char *name = packmsg_get_str_dup(&in);
179 if(strcmp(n->name, name)) {
189 char *submesh_name = packmsg_get_str_dup(&in);
191 if(!strcmp(submesh_name, CORE_MESH)) {
195 n->submesh = lookup_or_create_submesh(mesh, submesh_name);
203 n->devclass = packmsg_get_int32(&in);
204 n->status.blacklisted = packmsg_get_bool(&in);
206 uint32_t len = packmsg_get_bin_raw(&in, &key);
212 if(!ecdsa_active(n->ecdsa)) {
213 n->ecdsa = ecdsa_set_public_key(key);
216 n->canonical_address = packmsg_get_str_dup(&in);
217 uint32_t count = packmsg_get_array(&in);
223 for(uint32_t i = 0; i < count; i++) {
224 n->recent[i] = packmsg_get_sockaddr(&in);
227 return packmsg_done(&in);
230 bool node_write_config(meshlink_handle_t *mesh, node_t *n) {
231 if(!mesh->confbase) {
236 packmsg_output_t out = {buf, sizeof(buf)};
238 packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
239 packmsg_add_str(&out, n->name);
240 packmsg_add_str(&out, n->submesh ? n->submesh->name : CORE_MESH);
241 packmsg_add_int32(&out, n->devclass);
242 packmsg_add_bool(&out, n->status.blacklisted);
244 if(ecdsa_active(n->ecdsa)) {
245 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
247 packmsg_add_bin(&out, "", 0);
250 packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
254 for(uint32_t i = 0; i < 5; i++) {
255 if(n->recent[i].sa.sa_family) {
262 packmsg_add_array(&out, count);
264 for(uint32_t i = 0; i < count; i++) {
265 packmsg_add_sockaddr(&out, &n->recent[i]);
268 if(!packmsg_output_ok(&out)) {
272 config_t config = {buf, packmsg_output_size(&out, buf)};
273 return config_write(mesh, "current", n->name, &config, mesh->config_key);
276 static bool load_node(meshlink_handle_t *mesh, const char *name, void *priv) {
279 if(!check_id(name)) {
283 node_t *n = lookup_node(mesh, name);
290 n->name = xstrdup(name);
292 if(!node_read_partial(mesh, n)) {
303 Add listening sockets.
305 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
306 char *port = mesh->myport;
309 char *space = strchr(address, ' ');
316 if(!strcmp(address, "*")) {
323 struct addrinfo hint = {
324 .ai_family = addressfamily,
325 .ai_socktype = SOCK_STREAM,
326 .ai_protocol = IPPROTO_TCP,
327 .ai_flags = AI_PASSIVE,
330 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
335 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
339 bool success = false;
341 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
342 // Ignore duplicate addresses
345 for(int i = 0; i < mesh->listen_sockets; i++)
346 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
355 if(mesh->listen_sockets >= MAXSOCKETS) {
356 logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
360 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
366 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
373 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);
374 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);
376 if(mesh->log_level >= MESHLINK_INFO) {
377 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
378 logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
382 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
383 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
384 mesh->listen_sockets++;
393 Configure node_t mesh->self and set up the local sockets (listen only)
395 bool setup_myself(meshlink_handle_t *mesh) {
396 /* Set some defaults */
398 mesh->localdiscovery = true;
399 keylifetime = 3600; // TODO: check if this can be removed as well
400 mesh->maxtimeout = 900;
401 mesh->self->options |= OPTION_PMTU_DISCOVERY;
405 mesh->self->nexthop = mesh->self;
406 mesh->self->via = mesh->self;
407 mesh->self->status.reachable = true;
408 mesh->self->last_state_change = mesh->loop.now.tv_sec;
410 node_add(mesh, mesh->self);
414 config_scan_all(mesh, "current", "hosts", load_node, NULL);
418 mesh->listen_sockets = 0;
420 if(!add_listen_address(mesh, NULL, NULL)) {
421 if(strcmp(mesh->myport, "0")) {
422 logger(mesh, MESHLINK_INFO, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
424 mesh->myport = strdup("0");
430 if(!add_listen_address(mesh, NULL, NULL)) {
438 if(!mesh->listen_sockets) {
439 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
445 mesh->last_config_check = mesh->loop.now.tv_sec;
453 bool setup_network(meshlink_handle_t *mesh) {
454 init_connections(mesh);
455 init_submeshes(mesh);
460 mesh->pinginterval = 60;
461 mesh->pingtimeout = 5;
462 maxoutbufsize = 10 * MTU;
464 if(!setup_myself(mesh)) {
472 close all open network connections
474 void close_network_connections(meshlink_handle_t *mesh) {
475 if(mesh->connections) {
476 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
478 connection_t *c = node->data;
480 terminate_connection(mesh, c, false);
484 for(int i = 0; i < mesh->listen_sockets; i++) {
485 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
486 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
487 close(mesh->listen_socket[i].tcp.fd);
488 close(mesh->listen_socket[i].udp.fd);
494 exit_submeshes(mesh);
495 exit_connections(mesh);