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(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(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(connection_t *c) {
61 if(ecdsa_active(c->ecdsa))
67 init_configuration(&c->config_tree);
68 if(!read_host_config(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(void) {
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(void) {
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(void) {
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(ent->d_name);
148 n->name = xstrdup(ent->d_name);
156 char *get_name(void) {
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(void) {
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();
186 Add listening sockets.
188 static bool add_listen_address(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((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(void) {
268 char *name, *hostname, *cipher, *digest, *type;
269 char *address = NULL;
270 bool port_specified = false;
272 if(!(name = get_name())) {
273 logger(DEBUG_ALWAYS, LOG_ERR, "Name for tinc daemon required!");
277 mesh->self = new_node();
278 mesh->self->connection = new_connection();
279 mesh->self->name = name;
280 mesh->self->connection->name = xstrdup(name);
281 read_host_config(mesh->config, name);
283 if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport))
284 mesh->myport = xstrdup("655");
286 port_specified = true;
288 mesh->self->connection->options = 0;
289 mesh->self->connection->protocol_major = PROT_MAJOR;
290 mesh->self->connection->protocol_minor = PROT_MINOR;
292 mesh->self->options |= PROT_MINOR << 24;
294 if(!read_ecdsa_private_key())
297 /* Ensure mesh->myport is numeric */
299 if(!atoi(mesh->myport)) {
300 struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
302 if(!ai || !ai->ai_addr)
305 memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
306 sockaddr2str(&sa, NULL, &mesh->myport);
309 /* Check some options */
311 if(!setup_myself_reloadable())
316 // TODO: drop compression in the packet layer?
317 mesh->self->incompression = 0;
318 mesh->self->connection->outcompression = 0;
322 mesh->self->nexthop = mesh->self;
323 mesh->self->via = mesh->self;
324 mesh->self->status.reachable = true;
325 mesh->self->last_state_change = now.tv_sec;
326 node_add(mesh->self);
335 mesh->listen_sockets = 0;
338 if(!add_listen_address(address, NULL))
341 if(!mesh->listen_sockets) {
342 logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
346 // TODO: require Port to be set? Or use "0" and use getsockname()?
349 mesh->myport = xstrdup("655");
351 xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport);
352 mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
356 mesh->last_config_check = now.tv_sec;
364 bool setup_network(void) {
370 mesh->pinginterval = 60;
371 mesh->pingtimeout = 5;
372 maxoutbufsize = 10 * MTU;
381 close all open network connections
383 void close_network_connections(void) {
384 for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
386 connection_t *c = node->data;
388 terminate_connection(c, false);
392 list_delete_list(mesh->outgoings);
394 if(mesh->self && mesh->self->connection) {
395 terminate_connection(mesh->self->connection, false);
396 free_connection(mesh->self->connection);
399 for(int i = 0; i < mesh->listen_sockets; i++) {
400 io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
401 io_del(&mesh->loop, &mesh->listen_socket[i].udp);
402 close(mesh->listen_socket[i].tcp.fd);
403 close(mesh->listen_socket[i].udp.fd);
411 if(mesh->myport) free(mesh->myport);