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"
37 bool node_read_ecdsa_public_key(meshlink_handle_t *mesh, node_t *n) {
38 if(ecdsa_active(n->ecdsa))
41 splay_tree_t *config_tree;
44 init_configuration(&config_tree);
45 if(!read_host_config(mesh, config_tree, n->name))
48 /* First, check for simple ECDSAPublicKey statement */
50 if(get_config_string(lookup_config(config_tree, "ECDSAPublicKey"), &p)) {
51 n->ecdsa = ecdsa_set_base64_public_key(p);
56 exit_configuration(&config_tree);
60 bool read_ecdsa_public_key(meshlink_handle_t *mesh, connection_t *c) {
61 if(ecdsa_active(c->ecdsa))
67 init_configuration(&c->config_tree);
68 if(!read_host_config(mesh, c->config_tree, c->name))
72 /* First, check for simple ECDSAPublicKey statement */
74 if(get_config_string(lookup_config(c->config_tree, "ECDSAPublicKey"), &p)) {
75 c->ecdsa = ecdsa_set_base64_public_key(p);
83 static bool read_ecdsa_private_key(meshlink_handle_t *mesh) {
85 char filename[PATH_MAX];
87 snprintf(filename,PATH_MAX, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
88 fp = fopen(filename, "r");
91 logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file: %s", strerror(errno));
95 mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
98 if(!mesh->self->connection->ecdsa)
99 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file failed: %s", strerror(errno));
101 return mesh->self->connection->ecdsa;
104 static bool read_invitation_key(meshlink_handle_t *mesh) {
106 char filename[PATH_MAX];
108 if(mesh->invitation_key) {
109 ecdsa_free(mesh->invitation_key);
110 mesh->invitation_key = NULL;
113 snprintf(filename,PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
115 fp = fopen(filename, "r");
118 mesh->invitation_key = ecdsa_read_pem_private_key(fp);
120 if(!mesh->invitation_key)
121 logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", filename, strerror(errno));
124 return mesh->invitation_key;
127 void load_all_nodes(meshlink_handle_t *mesh) {
130 char dname[PATH_MAX];
132 snprintf(dname,PATH_MAX, "%s" SLASH "hosts", mesh->confbase);
133 dir = opendir(dname);
135 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
139 while((ent = readdir(dir))) {
140 if(!check_id(ent->d_name))
143 node_t *n = lookup_node(mesh, ent->d_name);
148 n->name = xstrdup(ent->d_name);
156 char *get_name(meshlink_handle_t *mesh) {
159 get_config_string(lookup_config(mesh->config, "Name"), &name);
164 if(!check_id(name)) {
165 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for mesh->self!");
173 bool setup_myself_reloadable(meshlink_handle_t *mesh) {
174 mesh->localdiscovery = true;
175 keylifetime = 3600; // TODO: check if this can be removed as well
176 mesh->maxtimeout = 900;
178 mesh->self->options |= OPTION_PMTU_DISCOVERY;
180 read_invitation_key(mesh);
186 Add listening sockets.
188 static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bindto) {
189 char *port = mesh->myport;
192 char *space = strchr(address, ' ');
198 if(!strcmp(address, "*"))
202 struct addrinfo *ai, hint = {0};
203 hint.ai_family = addressfamily;
204 hint.ai_socktype = SOCK_STREAM;
205 hint.ai_protocol = IPPROTO_TCP;
206 hint.ai_flags = AI_PASSIVE;
208 int err = getaddrinfo(address && *address ? address : NULL, port, &hint, &ai);
212 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "getaddrinfo", err == EAI_SYSTEM ? strerror(err) : gai_strerror(err));
216 for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
217 // Ignore duplicate addresses
220 for(int i = 0; i < mesh->listen_sockets; i++)
221 if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
229 if(mesh->listen_sockets >= MAXSOCKETS) {
230 logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
234 int tcp_fd = setup_listen_socket((sockaddr_t *) aip->ai_addr);
239 int udp_fd = setup_vpn_in_socket(mesh, (sockaddr_t *) aip->ai_addr);
246 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);
247 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);
249 if(mesh->debug_level >= DEBUG_CONNECTIONS) {
250 char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
251 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
255 mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
256 memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
257 mesh->listen_sockets++;
265 Configure node_t mesh->self and set up the local sockets (listen only)
267 bool setup_myself(meshlink_handle_t *mesh) {
269 char *address = NULL;
271 if(!(name = get_name(mesh))) {
272 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
276 mesh->self = new_node();
277 mesh->self->connection = new_connection();
278 mesh->self->name = name;
279 mesh->self->connection->name = xstrdup(name);
280 read_host_config(mesh, mesh->config, name);
282 if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport))
283 mesh->myport = xstrdup("655");
285 mesh->self->connection->options = 0;
286 mesh->self->connection->protocol_major = PROT_MAJOR;
287 mesh->self->connection->protocol_minor = PROT_MINOR;
289 mesh->self->options |= PROT_MINOR << 24;
291 if(!read_ecdsa_private_key(mesh))
294 /* Ensure mesh->myport is numeric */
296 if(!atoi(mesh->myport)) {
297 struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
299 if(!ai || !ai->ai_addr)
302 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
303 sockaddr2str(&sa, NULL, &mesh->myport);
306 /* Check some options */
308 if(!setup_myself_reloadable(mesh))
313 // TODO: drop compression in the packet layer?
314 mesh->self->incompression = 0;
315 mesh->self->connection->outcompression = 0;
319 mesh->self->nexthop = mesh->self;
320 mesh->self->via = mesh->self;
321 mesh->self->status.reachable = true;
322 mesh->self->last_state_change = mesh->loop.now.tv_sec;
323 node_add(mesh, mesh->self);
328 load_all_nodes(mesh);
332 mesh->listen_sockets = 0;
334 if(!add_listen_address(mesh, address, NULL))
337 if(!mesh->listen_sockets) {
338 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
342 // TODO: require Port to be set? Or use "0" and use getsockname()?
345 mesh->myport = xstrdup("655");
347 xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport);
348 mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
352 mesh->last_config_check = mesh->loop.now.tv_sec;
360 bool setup_network(meshlink_handle_t *mesh) {
361 init_connections(mesh);
366 mesh->pinginterval = 60;
367 mesh->pingtimeout = 5;
368 maxoutbufsize = 10 * MTU;
370 if(!setup_myself(mesh))
377 close all open network connections
379 void close_network_connections(meshlink_handle_t *mesh) {
380 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
382 connection_t *c = node->data;
384 terminate_connection(mesh, c, false);
388 list_delete_list(mesh->outgoings);
390 if(mesh->self && mesh->self->connection) {
391 terminate_connection(mesh, mesh->self->connection, false);
392 free_connection(mesh->self->connection);
395 for(int i = 0; i < mesh->listen_sockets; i++) {
396 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
397 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
398 close(mesh->listen_socket[i].tcp.fd);
399 close(mesh->listen_socket[i].udp.fd);
405 exit_connections(mesh);
407 if(mesh->myport) free(mesh->myport);