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"
35 bool node_read_ecdsa_public_key(meshlink_handle_t *mesh, node_t *n) {
36 if(ecdsa_active(n->ecdsa)) {
40 splay_tree_t *config_tree;
43 init_configuration(&config_tree);
45 if(!read_host_config(mesh, config_tree, n->name)) {
49 /* First, check for simple ECDSAPublicKey statement */
51 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
52 n->ecdsa = ecdsa_set_base64_public_key(p);
57 exit_configuration(&config_tree);
61 bool read_ecdsa_public_key(meshlink_handle_t *mesh, connection_t *c) {
62 if(ecdsa_active(c->ecdsa)) {
69 init_configuration(&c->config_tree);
71 if(!read_host_config(mesh, c->config_tree, c->name)) {
76 /* First, check for simple ECDSAPublicKey statement */
78 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
79 c->ecdsa = ecdsa_set_base64_public_key(p);
87 bool read_ecdsa_private_key(meshlink_handle_t *mesh) {
89 char filename[PATH_MAX];
91 snprintf(filename, PATH_MAX, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
92 fp = fopen(filename, "rb");
95 logger(mesh, MESHLINK_ERROR, "Error reading ECDSA private key file: %s", strerror(errno));
99 mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
102 if(!mesh->self->connection->ecdsa) {
103 logger(mesh, MESHLINK_ERROR, "Reading ECDSA private key file failed: %s", strerror(errno));
106 return mesh->self->connection->ecdsa;
109 static bool read_invitation_key(meshlink_handle_t *mesh) {
111 char filename[PATH_MAX];
113 if(mesh->invitation_key) {
114 ecdsa_free(mesh->invitation_key);
115 mesh->invitation_key = NULL;
118 snprintf(filename, PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
120 fp = fopen(filename, "rb");
123 mesh->invitation_key = ecdsa_read_pem_private_key(fp);
126 if(!mesh->invitation_key) {
127 logger(mesh, MESHLINK_ERROR, "Reading ECDSA private key file `%s' failed: %s", filename, strerror(errno));
131 return mesh->invitation_key;
134 bool node_read_devclass(meshlink_handle_t *mesh, node_t *n) {
136 splay_tree_t *config_tree;
139 init_configuration(&config_tree);
141 if(!read_host_config(mesh, config_tree, n->name)) {
145 if(get_config_string(lookup_config(config_tree, "DeviceClass"), &p)) {
146 n->devclass = atoi(p);
150 if((int)n->devclass < 0 || n->devclass > _DEV_CLASS_MAX) {
151 n->devclass = _DEV_CLASS_MAX;
155 exit_configuration(&config_tree);
156 return n->devclass != 0;
159 bool node_write_devclass(meshlink_handle_t *mesh, node_t *n) {
161 if((int)n->devclass < 0 || n->devclass > _DEV_CLASS_MAX) {
167 splay_tree_t *config_tree;
168 init_configuration(&config_tree);
170 // ignore read errors; in case the file does not exist we will create it
171 read_host_config(mesh, config_tree, n->name);
173 config_t *cnf = lookup_config(config_tree, "DeviceClass");
177 cnf->variable = xstrdup("DeviceClass");
178 config_add(config_tree, cnf);
181 set_config_int(cnf, n->devclass);
183 if(!write_host_config(mesh, config_tree, n->name)) {
190 exit_configuration(&config_tree);
194 void load_all_nodes(meshlink_handle_t *mesh) {
197 char dname[PATH_MAX];
199 snprintf(dname, PATH_MAX, "%s" SLASH "hosts", mesh->confbase);
200 dir = opendir(dname);
203 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
207 while((ent = readdir(dir))) {
208 if(!check_id(ent->d_name)) {
212 node_t *n = lookup_node(mesh, ent->d_name);
219 n->name = xstrdup(ent->d_name);
220 node_read_devclass(mesh, n);
228 char *get_name(meshlink_handle_t *mesh) {
231 get_config_string(lookup_config(mesh->config, "Name"), &name);
237 if(!check_id(name)) {
238 logger(mesh, MESHLINK_ERROR, "Invalid name for mesh->self!");
246 bool setup_myself_reloadable(meshlink_handle_t *mesh) {
247 mesh->localdiscovery = true;
248 keylifetime = 3600; // TODO: check if this can be removed as well
249 mesh->maxtimeout = 900;
250 mesh->self->options |= OPTION_PMTU_DISCOVERY;
252 read_invitation_key(mesh);
258 Add listening sockets.
260 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
261 char *port = mesh->myport;
264 char *space = strchr(address, ' ');
271 if(!strcmp(address, "*")) {
278 struct addrinfo hint = {
279 .ai_family = addressfamily,
280 .ai_socktype = SOCK_STREAM,
281 .ai_protocol = IPPROTO_TCP,
282 .ai_flags = AI_PASSIVE,
285 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
290 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
294 bool success = false;
296 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
297 // Ignore duplicate addresses
300 for(int i = 0; i < mesh->listen_sockets; i++)
301 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
310 if(mesh->listen_sockets >= MAXSOCKETS) {
311 logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
315 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
321 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
328 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);
329 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);
331 if(mesh->log_level >= MESHLINK_INFO) {
332 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
333 logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
337 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
338 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
339 mesh->listen_sockets++;
348 Configure node_t mesh->self and set up the local sockets (listen only)
350 bool setup_myself(meshlink_handle_t *mesh) {
352 char *address = NULL;
354 if(!(name = get_name(mesh))) {
355 logger(mesh, MESHLINK_ERROR, "Name for MeshLink instance required!");
359 mesh->self = new_node();
360 mesh->self->connection = new_connection();
361 mesh->self->name = name;
362 mesh->self->devclass = mesh->devclass;
363 mesh->self->connection->name = xstrdup(name);
364 read_host_config(mesh, mesh->config, name);
366 if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport)) {
367 int port = check_port(mesh);
373 xasprintf(&mesh->myport, "%d", port);
376 mesh->self->connection->options = 0;
377 mesh->self->connection->protocol_major = PROT_MAJOR;
378 mesh->self->connection->protocol_minor = PROT_MINOR;
380 mesh->self->options |= PROT_MINOR << 24;
382 if(!read_ecdsa_private_key(mesh)) {
386 /* Ensure mesh->myport is numeric */
388 if(!atoi(mesh->myport)) {
389 struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
392 if(!ai || !ai->ai_addr) {
397 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
398 sockaddr2str(&sa, NULL, &mesh->myport);
401 /* Check some options */
403 if(!setup_myself_reloadable(mesh)) {
409 // TODO: drop compression in the packet layer?
410 mesh->self->incompression = 0;
411 mesh->self->connection->outcompression = 0;
415 mesh->self->nexthop = mesh->self;
416 mesh->self->via = mesh->self;
417 mesh->self->status.reachable = true;
418 mesh->self->last_state_change = mesh->loop.now.tv_sec;
420 node_write_devclass(mesh, mesh->self);
421 node_add(mesh, mesh->self);
425 load_all_nodes(mesh);
429 mesh->listen_sockets = 0;
431 if(!add_listen_address(mesh, address, NULL)) {
432 if(strcmp(mesh->myport, "0")) {
433 logger(mesh, MESHLINK_INFO, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
435 mesh->myport = strdup("0");
441 if(!add_listen_address(mesh, address, NULL)) {
449 if(!mesh->listen_sockets) {
450 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
456 mesh->last_config_check = mesh->loop.now.tv_sec;
464 bool setup_network(meshlink_handle_t *mesh) {
465 init_connections(mesh);
470 mesh->pinginterval = 60;
471 mesh->pingtimeout = 5;
472 maxoutbufsize = 10 * MTU;
474 if(!setup_myself(mesh)) {
482 close all open network connections
484 void close_network_connections(meshlink_handle_t *mesh) {
485 if(mesh->connections) {
486 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
488 connection_t *c = node->data;
490 terminate_connection(mesh, c, false);
494 if(mesh->outgoings) {
495 list_delete_list(mesh->outgoings);
498 if(mesh->self && mesh->self->connection) {
499 terminate_connection(mesh, mesh->self->connection, false);
500 free_connection(mesh->self->connection);
503 for(int i = 0; i < mesh->listen_sockets; i++) {
504 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
505 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
506 close(mesh->listen_socket[i].tcp.fd);
507 close(mesh->listen_socket[i].udp.fd);
513 exit_connections(mesh);