3 Copyright (C) 2014 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))
39 splay_tree_t *config_tree;
42 init_configuration(&config_tree);
43 if(!read_host_config(mesh, config_tree, n->name))
46 /* First, check for simple ECDSAPublicKey statement */
48 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
49 n->ecdsa = ecdsa_set_base64_public_key(p);
54 exit_configuration(&config_tree);
58 bool read_ecdsa_public_key(meshlink_handle_t *mesh, connection_t *c) {
59 if(ecdsa_active(c->ecdsa))
65 init_configuration(&c->config_tree);
66 if(!read_host_config(mesh, c->config_tree, c->name))
70 /* First, check for simple ECDSAPublicKey statement */
72 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
73 c->ecdsa = ecdsa_set_base64_public_key(p);
81 bool read_ecdsa_private_key(meshlink_handle_t *mesh) {
83 char filename[PATH_MAX];
85 snprintf(filename,PATH_MAX, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
86 fp = fopen(filename, "rb");
89 logger(mesh, MESHLINK_ERROR, "Error reading ECDSA private key file: %s", strerror(errno));
93 mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
96 if(!mesh->self->connection->ecdsa)
97 logger(mesh, MESHLINK_ERROR, "Reading ECDSA private key file failed: %s", strerror(errno));
99 return mesh->self->connection->ecdsa;
102 static bool read_invitation_key(meshlink_handle_t *mesh) {
104 char filename[PATH_MAX];
106 if(mesh->invitation_key) {
107 ecdsa_free(mesh->invitation_key);
108 mesh->invitation_key = NULL;
111 snprintf(filename,PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
113 fp = fopen(filename, "rb");
116 mesh->invitation_key = ecdsa_read_pem_private_key(fp);
118 if(!mesh->invitation_key)
119 logger(mesh, MESHLINK_ERROR, "Reading ECDSA private key file `%s' failed: %s", filename, strerror(errno));
122 return mesh->invitation_key;
125 bool node_read_devclass(meshlink_handle_t *mesh, node_t *n) {
127 splay_tree_t *config_tree;
130 init_configuration(&config_tree);
131 if(!read_host_config(mesh, config_tree, n->name))
134 if(get_config_string(lookup_config(config_tree, "DeviceClass"), &p)) {
135 n->devclass = atoi(p);
139 if(n->devclass < 0 || n->devclass > _DEV_CLASS_MAX)
140 n->devclass = _DEV_CLASS_MAX;
143 exit_configuration(&config_tree);
144 return n->devclass != 0;
147 bool node_write_devclass(meshlink_handle_t *mesh, node_t *n) {
149 if(n->devclass < 0 || n->devclass > _DEV_CLASS_MAX)
154 splay_tree_t *config_tree;
155 init_configuration(&config_tree);
157 // ignore read errors; in case the file does not exist we will create it
158 read_host_config(mesh, config_tree, n->name);
160 config_t* cnf = lookup_config(config_tree, "DeviceClass");
164 cnf->variable = xstrdup("DeviceClass");
165 config_add(config_tree, cnf);
168 set_config_int(cnf, n->devclass);
170 if(!write_host_config(mesh, config_tree, n->name))
176 exit_configuration(&config_tree);
180 void load_all_nodes(meshlink_handle_t *mesh) {
183 char dname[PATH_MAX];
185 snprintf(dname,PATH_MAX, "%s" SLASH "hosts", mesh->confbase);
186 dir = opendir(dname);
188 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
192 while((ent = readdir(dir))) {
193 if(!check_id(ent->d_name))
196 node_t *n = lookup_node(mesh, ent->d_name);
201 n->name = xstrdup(ent->d_name);
202 node_read_devclass(mesh, n);
210 char *get_name(meshlink_handle_t *mesh) {
213 get_config_string(lookup_config(mesh->config, "Name"), &name);
218 if(!check_id(name)) {
219 logger(mesh, MESHLINK_ERROR, "Invalid name for mesh->self!");
227 bool setup_myself_reloadable(meshlink_handle_t *mesh) {
228 mesh->localdiscovery = true;
229 keylifetime = 3600; // TODO: check if this can be removed as well
230 mesh->maxtimeout = 900;
231 mesh->self->options |= OPTION_PMTU_DISCOVERY;
233 read_invitation_key(mesh);
239 Add listening sockets.
241 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
242 char *port = mesh->myport;
245 char *space = strchr(address, ' ');
251 if(!strcmp(address, "*"))
255 struct addrinfo *ai, hint = {0};
256 hint.ai_family = addressfamily;
257 hint.ai_socktype = SOCK_STREAM;
258 hint.ai_protocol = IPPROTO_TCP;
259 hint.ai_flags = AI_PASSIVE;
261 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
265 logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
269 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
270 // Ignore duplicate addresses
273 for(int i = 0; i < mesh->listen_sockets; i++)
274 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
282 if(mesh->listen_sockets >= MAXSOCKETS) {
283 logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
287 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
292 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
299 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);
300 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);
302 if(mesh->log_level >= MESHLINK_INFO) {
303 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
304 logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
308 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
309 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
310 mesh->listen_sockets++;
318 Configure node_t mesh->self and set up the local sockets (listen only)
320 bool setup_myself(meshlink_handle_t *mesh) {
322 char *address = NULL;
324 if(!(name = get_name(mesh))) {
325 logger(mesh, MESHLINK_ERROR, "Name for MeshLink instance required!");
329 mesh->self = new_node();
330 mesh->self->connection = new_connection();
331 mesh->self->name = name;
332 mesh->self->devclass = mesh->devclass;
333 mesh->self->connection->name = xstrdup(name);
334 read_host_config(mesh, mesh->config, name);
336 if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport)) {
337 logger(mesh, MESHLINK_ERROR, "Port for MeshLink instance required!");
341 mesh->self->connection->options = 0;
342 mesh->self->connection->protocol_major = PROT_MAJOR;
343 mesh->self->connection->protocol_minor = PROT_MINOR;
345 mesh->self->options |= PROT_MINOR << 24;
347 if(!read_ecdsa_private_key(mesh))
350 /* Ensure mesh->myport is numeric */
352 if(!atoi(mesh->myport)) {
353 struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
355 if(!ai || !ai->ai_addr)
358 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
359 sockaddr2str(&sa, NULL, &mesh->myport);
362 /* Check some options */
364 if(!setup_myself_reloadable(mesh))
369 // TODO: drop compression in the packet layer?
370 mesh->self->incompression = 0;
371 mesh->self->connection->outcompression = 0;
375 mesh->self->nexthop = mesh->self;
376 mesh->self->via = mesh->self;
377 mesh->self->status.reachable = true;
378 mesh->self->last_state_change = mesh->loop.now.tv_sec;
380 node_write_devclass(mesh, mesh->self);
381 node_add(mesh, mesh->self);
385 load_all_nodes(mesh);
389 mesh->listen_sockets = 0;
391 if(!add_listen_address(mesh, address, NULL)) {
392 if(!strcmp(mesh->myport, "0")) {
393 logger(mesh, MESHLINK_WARNING, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
395 mesh->myport = strdup("0");
398 if(!add_listen_address(mesh, address, NULL))
404 if(!mesh->listen_sockets) {
405 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
409 xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport);
410 mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
414 mesh->last_config_check = mesh->loop.now.tv_sec;
422 bool setup_network(meshlink_handle_t *mesh) {
423 init_connections(mesh);
428 mesh->pinginterval = 60;
429 mesh->pingtimeout = 5;
430 maxoutbufsize = 10 * MTU;
432 if(!setup_myself(mesh))
439 close all open network connections
441 void close_network_connections(meshlink_handle_t *mesh) {
442 if(mesh->connections) {
443 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
445 connection_t *c = node->data;
447 terminate_connection(mesh, c, false);
452 list_delete_list(mesh->outgoings);
454 if(mesh->self && mesh->self->connection) {
455 terminate_connection(mesh, mesh->self->connection, false);
456 free_connection(mesh->self->connection);
459 for(int i = 0; i < mesh->listen_sockets; i++) {
460 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
461 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
462 close(mesh->listen_socket[i].tcp.fd);
463 close(mesh->listen_socket[i].udp.fd);
469 exit_connections(mesh);
471 if(mesh->myport) free(mesh->myport);