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 /// Helper function to start parsing a host config file
37 static bool node_get_config(meshlink_handle_t *mesh, node_t *n, config_t *config, packmsg_input_t *in) {
38 if(!config_read(mesh, n->name, config)) {
42 in->ptr = config->buf;
43 in->len = config->len;
45 uint32_t version = packmsg_get_uint32(in);
47 if(version != MESHLINK_CONFIG_VERSION) {
53 uint32_t len = packmsg_get_str_raw(in, &name);
55 if(len != strlen(n->name) || strncmp(name, n->name, len)) {
63 /// Read just the devclass from a host config file. Used at startup when reading all host config files.
64 bool node_read_devclass(meshlink_handle_t *mesh, node_t *n) {
68 if(!node_get_config(mesh, n, &config, &in)) {
72 int32_t devclass = packmsg_get_int32(&in);
73 bool blacklisted = packmsg_get_bool(&in);
76 if(!packmsg_input_ok(&in) || devclass < 0 || devclass > _DEV_CLASS_MAX) {
80 n->devclass = devclass;
81 n->status.blacklisted = blacklisted;
85 /// Read the public key from a host config file. Used whenever we need to start an SPTPS session.
86 bool node_read_public_key(meshlink_handle_t *mesh, node_t *n) {
87 if(ecdsa_active(n->ecdsa)) {
94 if(!node_get_config(mesh, n, &config, &in)) {
98 packmsg_get_int32(&in); /* devclass */
99 packmsg_get_bool(&in); /* blacklisted */
102 uint32_t len = packmsg_get_bin_raw(&in, &key);
105 config_free(&config);
109 n->ecdsa = ecdsa_set_public_key(key);
110 config_free(&config);
114 /// Read the full host config file. Used whenever we need to start an SPTPS session.
115 bool node_read_full(meshlink_handle_t *mesh, node_t *n) {
116 if(n->canonical_address) {
123 if(!node_get_config(mesh, n, &config, &in)) {
127 packmsg_get_int32(&in); /* devclass */
128 packmsg_get_bool(&in); /* blacklisted */
130 uint32_t len = packmsg_get_bin_raw(&in, &key); /* public key */
136 if(!ecdsa_active(n->ecdsa)) {
137 n->ecdsa = ecdsa_set_public_key(key);
140 n->canonical_address = packmsg_get_str_dup(&in);
142 uint32_t count = packmsg_get_array(&in);
148 for(uint32_t i = 0; i < count; i++) {
149 n->recent[i] = packmsg_get_sockaddr(&in);
152 config_free(&config);
154 return packmsg_done(&in);
157 bool node_write_config(meshlink_handle_t *mesh, node_t *n) {
159 packmsg_output_t out = {buf, sizeof(buf)};
161 packmsg_add_uint32(&out, MESHLINK_CONFIG_VERSION);
162 packmsg_add_str(&out, n->name);
163 packmsg_add_int32(&out, n->devclass);
164 assert(n->devclass != 3);
165 packmsg_add_bool(&out, n->status.blacklisted);
167 if(ecdsa_active(n->ecdsa)) {
168 packmsg_add_bin(&out, ecdsa_get_public_key(n->ecdsa), 32);
170 packmsg_add_bin(&out, "", 0);
173 packmsg_add_str(&out, n->canonical_address ? n->canonical_address : "");
177 for(uint32_t i = 0; i < 5; i++) {
178 if(n->recent[i].sa.sa_family) {
185 packmsg_add_array(&out, count);
187 for(uint32_t i = 0; i < count; i++) {
188 packmsg_add_sockaddr(&out, &n->recent[i]);
191 if(!packmsg_output_ok(&out)) {
195 config_t config = {buf, packmsg_output_size(&out, buf)};
196 return config_write(mesh, n->name, &config);
199 void load_all_nodes(meshlink_handle_t *mesh) {
202 char dname[PATH_MAX];
204 snprintf(dname, PATH_MAX, "%s" SLASH "hosts", mesh->confbase);
205 dir = opendir(dname);
208 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
212 while((ent = readdir(dir))) {
213 if(!check_id(ent->d_name)) {
217 node_t *n = lookup_node(mesh, ent->d_name);
224 n->name = xstrdup(ent->d_name);
225 node_read_devclass(mesh, n);
233 Add listening sockets.
235 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
236 char *port = mesh->myport;
239 char *space = strchr(address, ' ');
246 if(!strcmp(address, "*")) {
253 struct addrinfo hint = {
254 .ai_family = addressfamily,
255 .ai_socktype = SOCK_STREAM,
256 .ai_protocol = IPPROTO_TCP,
257 .ai_flags = AI_PASSIVE,
260 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 bool success = false;
271 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
272 // Ignore duplicate addresses
275 for(int i = 0; i < mesh->listen_sockets; i++)
276 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
285 if(mesh->listen_sockets >= MAXSOCKETS) {
286 logger(mesh, MESHLINK_ERROR, "Too many listening sockets");
290 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
296 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
303 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);
304 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);
306 if(mesh->log_level >= MESHLINK_INFO) {
307 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
308 logger(mesh, MESHLINK_INFO, "Listening on %s", hostname);
312 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
313 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
314 mesh->listen_sockets++;
323 Configure node_t mesh->self and set up the local sockets (listen only)
325 bool setup_myself(meshlink_handle_t *mesh) {
326 /* Set some defaults */
328 mesh->localdiscovery = true;
329 keylifetime = 3600; // TODO: check if this can be removed as well
330 mesh->maxtimeout = 900;
331 mesh->self->options |= OPTION_PMTU_DISCOVERY;
335 mesh->self->nexthop = mesh->self;
336 mesh->self->via = mesh->self;
337 mesh->self->status.reachable = true;
338 mesh->self->last_state_change = mesh->loop.now.tv_sec;
340 node_add(mesh, mesh->self);
344 load_all_nodes(mesh);
348 mesh->listen_sockets = 0;
350 if(!add_listen_address(mesh, NULL, NULL)) {
351 if(strcmp(mesh->myport, "0")) {
352 logger(mesh, MESHLINK_INFO, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
354 mesh->myport = strdup("0");
360 if(!add_listen_address(mesh, NULL, NULL)) {
368 if(!mesh->listen_sockets) {
369 logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
375 mesh->last_config_check = mesh->loop.now.tv_sec;
383 bool setup_network(meshlink_handle_t *mesh) {
384 init_connections(mesh);
389 mesh->pinginterval = 60;
390 mesh->pingtimeout = 5;
391 maxoutbufsize = 10 * MTU;
393 if(!setup_myself(mesh)) {
401 close all open network connections
403 void close_network_connections(meshlink_handle_t *mesh) {
404 if(mesh->connections) {
405 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
407 connection_t *c = node->data;
409 terminate_connection(mesh, c, false);
413 for(int i = 0; i < mesh->listen_sockets; i++) {
414 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
415 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
416 close(mesh->listen_socket[i].tcp.fd);
417 close(mesh->listen_socket[i].udp.fd);
423 exit_connections(mesh);