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"
36 bool node_read_ecdsa_public_key(meshlink_handle_t *mesh, node_t *n) {
37 if(ecdsa_active(n->ecdsa)) {
41 splay_tree_t *config_tree;
44 init_configuration(&config_tree);
46 if(!read_host_config(mesh, config_tree, n->name)) {
50 /* First, check for simple ECDSAPublicKey statement */
52 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
53 n->ecdsa = ecdsa_set_base64_public_key(p);
58 exit_configuration(&config_tree);
62 bool read_ecdsa_public_key(meshlink_handle_t *mesh, connection_t *c) {
63 if(ecdsa_active(c->ecdsa)) {
70 init_configuration(&c->config_tree);
72 if(!read_host_config(mesh, c->config_tree, c->name)) {
77 /* First, check for simple ECDSAPublicKey statement */
79 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
80 c->ecdsa = ecdsa_set_base64_public_key(p);
88 bool read_ecdsa_private_key(meshlink_handle_t *mesh) {
90 char filename[PATH_MAX];
92 snprintf(filename, PATH_MAX, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
93 fp = fopen(filename, "rb");
96 logger(mesh, MESHLINK_ERROR, "Error reading ECDSA private key file: %s", strerror(errno));
100 mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
103 if(!mesh->self->connection->ecdsa) {
104 logger(mesh, MESHLINK_ERROR, "Reading ECDSA private key file failed: %s", strerror(errno));
107 return mesh->self->connection->ecdsa;
110 static bool read_invitation_key(meshlink_handle_t *mesh) {
112 char filename[PATH_MAX];
114 if(mesh->invitation_key) {
115 ecdsa_free(mesh->invitation_key);
116 mesh->invitation_key = NULL;
119 snprintf(filename, PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
121 fp = fopen(filename, "rb");
124 mesh->invitation_key = ecdsa_read_pem_private_key(fp);
127 if(!mesh->invitation_key) {
128 logger(mesh, MESHLINK_ERROR, "Reading ECDSA private key file `%s' failed: %s", filename, strerror(errno));
132 return mesh->invitation_key;
135 bool node_read_devclass(meshlink_handle_t *mesh, node_t *n) {
137 splay_tree_t *config_tree;
140 init_configuration(&config_tree);
142 if(!read_host_config(mesh, config_tree, n->name)) {
146 if(get_config_string(lookup_config(config_tree, "DeviceClass"), &p)) {
147 n->devclass = atoi(p);
151 if((int)n->devclass < 0 || n->devclass > _DEV_CLASS_MAX) {
152 n->devclass = _DEV_CLASS_MAX;
156 exit_configuration(&config_tree);
157 return n->devclass != 0;
160 bool node_read_submesh(meshlink_handle_t *mesh, node_t *n) {
162 splay_tree_t *config_tree;
165 init_configuration(&config_tree);
167 if(!read_host_config(mesh, config_tree, n->name)) {
171 if(get_config_string(lookup_config(config_tree, "SubMesh"), &p)) {
174 for list_each(submesh_t, s, mesh->submeshes) {
175 if(!strcmp(p, s->name)) {
182 n->submesh = (submesh_t *)meshlink_submesh_open(mesh, p);
189 exit_configuration(&config_tree);
190 return n->submesh != NULL;
193 bool node_write_devclass(meshlink_handle_t *mesh, node_t *n) {
195 if((int)n->devclass < 0 || n->devclass > _DEV_CLASS_MAX) {
201 splay_tree_t *config_tree;
202 init_configuration(&config_tree);
204 // ignore read errors; in case the file does not exist we will create it
205 read_host_config(mesh, config_tree, n->name);
207 config_t *cnf = lookup_config(config_tree, "DeviceClass");
211 cnf->variable = xstrdup("DeviceClass");
212 config_add(config_tree, cnf);
215 set_config_int(cnf, n->devclass);
217 if(!write_host_config(mesh, config_tree, n->name)) {
224 exit_configuration(&config_tree);
228 bool node_write_submesh(meshlink_handle_t *mesh, node_t *n) {
236 splay_tree_t *config_tree;
237 init_configuration(&config_tree);
239 // ignore read errors; in case the file does not exist we will create it
240 read_host_config(mesh, config_tree, n->name);
242 config_t *cnf = lookup_config(config_tree, "SubMesh");
246 cnf->variable = xstrdup("SubMesh");
247 config_add(config_tree, cnf);
250 set_config_string(cnf, n->submesh->name);
252 if(!write_host_config(mesh, config_tree, n->name)) {
259 exit_configuration(&config_tree);
263 void load_all_nodes(meshlink_handle_t *mesh) {
266 char dname[PATH_MAX];
268 snprintf(dname, PATH_MAX, "%s" SLASH "hosts", mesh->confbase);
269 dir = opendir(dname);
272 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
276 while((ent = readdir(dir))) {
277 if(!check_id(ent->d_name)) {
281 node_t *n = lookup_node(mesh, ent->d_name);
284 if(n == mesh->self && !n->submesh) {
285 node_read_submesh(mesh, n);
292 n->name = xstrdup(ent->d_name);
293 node_read_devclass(mesh, n);
294 node_read_submesh(mesh, n);
302 char *get_name(meshlink_handle_t *mesh) {
305 get_config_string(lookup_config(mesh->config, "Name"), &name);
311 if(!check_id(name)) {
312 logger(mesh, MESHLINK_ERROR, "Invalid name for mesh->self!");
320 bool setup_myself_reloadable(meshlink_handle_t *mesh) {
321 mesh->localdiscovery = true;
322 keylifetime = 3600; // TODO: check if this can be removed as well
323 mesh->maxtimeout = 900;
324 mesh->self->options |= OPTION_PMTU_DISCOVERY;
326 read_invitation_key(mesh);
332 Add listening sockets.
334 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
335 char *port = mesh->myport;
338 char *space = strchr(address, ' ');
345 if(!strcmp(address, "*")) {
352 struct addrinfo hint = {
353 .ai_family = addressfamily,
354 .ai_socktype = SOCK_STREAM,
355 .ai_protocol = IPPROTO_TCP,
356 .ai_flags = AI_PASSIVE,
359 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
364 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
368 bool success = false;
370 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
371 // Ignore duplicate addresses
374 for(int i = 0; i < mesh->listen_sockets; i++)
375 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
384 if(mesh->listen_sockets >= MAXSOCKETS) {
385 logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
389 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
395 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
402 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);
403 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);
405 if(mesh->log_level >= MESHLINK_INFO) {
406 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
407 logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
411 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
412 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
413 mesh->listen_sockets++;
422 Configure node_t mesh->self and set up the local sockets (listen only)
424 bool setup_myself(meshlink_handle_t *mesh) {
426 char *address = NULL;
428 if(!(name = get_name(mesh))) {
429 logger(mesh, MESHLINK_ERROR, "Name for MeshLink instance required!");
433 mesh->self = new_node();
434 mesh->self->connection = new_connection();
435 mesh->self->name = name;
436 mesh->self->devclass = mesh->devclass;
437 mesh->self->connection->name = xstrdup(name);
438 read_host_config(mesh, mesh->config, name);
440 if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport)) {
441 int port = check_port(mesh);
447 xasprintf(&mesh->myport, "%d", port);
450 mesh->self->connection->options = 0;
451 mesh->self->connection->protocol_major = PROT_MAJOR;
452 mesh->self->connection->protocol_minor = PROT_MINOR;
454 mesh->self->options |= PROT_MINOR << 24;
456 if(!read_ecdsa_private_key(mesh)) {
460 /* Ensure mesh->myport is numeric */
462 if(!atoi(mesh->myport)) {
463 struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
466 if(!ai || !ai->ai_addr) {
471 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
472 sockaddr2str(&sa, NULL, &mesh->myport);
475 /* Check some options */
477 if(!setup_myself_reloadable(mesh)) {
483 // TODO: drop compression in the packet layer?
484 mesh->self->incompression = 0;
485 mesh->self->connection->outcompression = 0;
489 mesh->self->nexthop = mesh->self;
490 mesh->self->via = mesh->self;
491 mesh->self->status.reachable = true;
492 mesh->self->last_state_change = mesh->loop.now.tv_sec;
494 node_write_devclass(mesh, mesh->self);
495 node_add(mesh, mesh->self);
499 load_all_nodes(mesh);
503 mesh->listen_sockets = 0;
505 if(!add_listen_address(mesh, address, NULL)) {
506 if(strcmp(mesh->myport, "0")) {
507 logger(mesh, MESHLINK_INFO, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
509 mesh->myport = strdup("0");
515 if(!add_listen_address(mesh, address, NULL)) {
523 if(!mesh->listen_sockets) {
524 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
530 mesh->last_config_check = mesh->loop.now.tv_sec;
538 bool setup_network(meshlink_handle_t *mesh) {
539 init_connections(mesh);
540 init_submeshes(mesh);
545 mesh->pinginterval = 60;
546 mesh->pingtimeout = 5;
547 maxoutbufsize = 10 * MTU;
549 if(!setup_myself(mesh)) {
557 close all open network connections
559 void close_network_connections(meshlink_handle_t *mesh) {
560 if(mesh->connections) {
561 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
563 connection_t *c = node->data;
565 terminate_connection(mesh, c, false);
569 if(mesh->outgoings) {
570 list_delete_list(mesh->outgoings);
573 if(mesh->self && mesh->self->connection) {
574 terminate_connection(mesh, mesh->self->connection, false);
575 free_connection(mesh->self->connection);
578 for(int i = 0; i < mesh->listen_sockets; i++) {
579 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
580 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
581 close(mesh->listen_socket[i].tcp.fd);
582 close(mesh->listen_socket[i].udp.fd);
588 exit_connections(mesh);