2 net.c -- most of the network code
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.
22 #include "autoconnect.h"
24 #include "connection.h"
27 #include "meshlink_internal.h"
38 static inline int min(int a, int b) {
43 static const int default_timeout = 5;
46 Terminate a connection:
48 - Remove the edge representing this connection
50 - Check if we need to retry making an outgoing connection
52 void terminate_connection(meshlink_handle_t *mesh, connection_t *c, bool report) {
53 logger(mesh, MESHLINK_INFO, "Closing connection with %s", c->name);
55 c->status.active = false;
57 if(c->node && c->node->connection == c) {
58 c->node->connection = NULL;
63 send_del_edge(mesh, mesh->everyone, c->edge, 0);
66 edge_del(mesh, c->edge);
69 /* Run MST and SSSP algorithms */
73 /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */
75 if(report && c->node && !c->node->status.reachable) {
77 e = lookup_edge(c->node, mesh->self);
80 send_del_edge(mesh, mesh->everyone, e, 0);
86 outgoing_t *outgoing = c->outgoing;
87 connection_del(mesh, c);
89 /* Check if this was our outgoing connection */
92 do_outgoing_connection(mesh, outgoing);
96 /* Clean up dead proxy processes */
98 while(waitpid(-1, NULL, WNOHANG) > 0);
104 Check if the other end is active.
105 If we have sent packets, but didn't receive any,
106 then possibly the other end is dead. We send a
107 PING request over the meta connection. If the other
108 end does not reply in time, we consider them dead
109 and close the connection.
111 static void timeout_handler(event_loop_t *loop, void *data) {
112 meshlink_handle_t *mesh = loop->data;
113 logger(mesh, MESHLINK_DEBUG, "timeout_handler()");
115 for list_each(connection_t, c, mesh->connections) {
116 int pingtimeout = c->node ? mesh->dev_class_traits[c->node->devclass].pingtimeout : default_timeout;
118 // Also make sure that if outstanding key requests for the UDP counterpart of a connection has timed out, we restart it.
120 if(c->node->status.waitingforkey && c->node->last_req_key + pingtimeout <= mesh->loop.now.tv_sec) {
121 send_req_key(mesh, c->node);
125 if(c->last_ping_time + pingtimeout <= mesh->loop.now.tv_sec) {
126 if(c->status.active) {
127 if(c->status.pinged) {
128 logger(mesh, MESHLINK_INFO, "%s didn't respond to PING in %ld seconds", c->name, (long)mesh->loop.now.tv_sec - c->last_ping_time);
129 } else if(c->last_ping_time + mesh->dev_class_traits[c->node->devclass].pinginterval <= mesh->loop.now.tv_sec) {
136 if(c->status.connecting) {
137 logger(mesh, MESHLINK_WARNING, "Timeout while connecting to %s", c->name);
139 logger(mesh, MESHLINK_WARNING, "Timeout from %s during authentication", c->name);
143 terminate_connection(mesh, c, c->status.active);
147 timeout_set(&mesh->loop, data, &(struct timeval) {
148 default_timeout, rand() % 100000
152 static void periodic_handler(event_loop_t *loop, void *data) {
153 meshlink_handle_t *mesh = loop->data;
155 /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
156 This usually only happens when another node has the same Name as this node.
157 If so, sleep for a short while to prevent a storm of contradicting messages.
160 if(mesh->contradicting_del_edge > 100 && mesh->contradicting_add_edge > 100) {
161 logger(mesh, MESHLINK_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", mesh->sleeptime);
162 usleep(mesh->sleeptime * 1000000LL);
163 mesh->sleeptime *= 2;
165 if(mesh->sleeptime < 0) {
166 mesh->sleeptime = 3600;
169 mesh->sleeptime /= 2;
171 if(mesh->sleeptime < 10) {
172 mesh->sleeptime = 10;
176 mesh->contradicting_add_edge = 0;
177 mesh->contradicting_del_edge = 0;
179 int timeout = default_timeout;
181 /* Check if we need to make or break connections. */
183 if(mesh->nodes->count > 1) {
184 unsigned int cur_connects = do_autoconnect(mesh);
186 // reduce timeout if we don't have enough connections + outgoings
187 if (cur_connects + mesh->outgoings->count < 3) {
192 /* Write dirty config files out to disk */
194 for splay_each(node_t, n, mesh->nodes) {
195 if(n->status.dirty) {
196 node_write_config(mesh, n);
197 n->status.dirty = false;
201 timeout_set(&mesh->loop, data, &(struct timeval) {
202 timeout, rand() % 100000
206 void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) {
207 if(!receive_meta(mesh, c)) {
208 terminate_connection(mesh, c, c->status.active);
213 void retry(meshlink_handle_t *mesh) {
214 /* Reset the reconnection timers for all outgoing connections */
215 for list_each(outgoing_t, outgoing, mesh->outgoings) {
216 outgoing->timeout = 0;
219 timeout_set(&mesh->loop, &outgoing->ev, &(struct timeval) {
224 #ifdef HAVE_IFADDRS_H
225 struct ifaddrs *ifa = NULL;
229 /* For active connections, check if their addresses are still valid.
230 * If yes, reset their ping timers, otherwise terminate them. */
231 for list_each(connection_t, c, mesh->connections) {
232 if(!c->status.active) {
236 if(!c->status.pinged) {
237 c->last_ping_time = 0;
240 #ifdef HAVE_IFADDRS_H
247 socklen_t salen = sizeof(sa);
249 if(getsockname(c->socket, &sa.sa, &salen)) {
255 for(struct ifaddrs *ifap = ifa; ifap; ifap = ifap->ifa_next) {
256 if(ifap->ifa_addr && !sockaddrcmp_noport(&sa, (sockaddr_t *)ifap->ifa_addr)) {
264 logger(mesh, MESHLINK_DEBUG, "Local address for connection to %s no longer valid, terminating", c->name);
265 terminate_connection(mesh, c, c->status.active);
271 #ifdef HAVE_IFADDRS_H
279 /* Kick the ping timeout handler */
280 timeout_set(&mesh->loop, &mesh->pingtimer, &(struct timeval) {
286 this is where it all happens...
288 int main_loop(meshlink_handle_t *mesh) {
289 timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval) {
290 default_timeout, rand() % 100000
292 timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval) {
297 mesh->datafromapp.signum = 0;
298 signal_add(&(mesh->loop), &(mesh->datafromapp), (signal_cb_t)meshlink_send_from_queue, mesh, mesh->datafromapp.signum);
300 if(!event_loop_run(&(mesh->loop), &(mesh->mesh_mutex))) {
301 logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
303 timeout_del(&mesh->loop, &mesh->periodictimer);
304 timeout_del(&mesh->loop, &mesh->pingtimer);
309 timeout_del(&mesh->loop, &mesh->periodictimer);
310 timeout_del(&mesh->loop, &mesh->pingtimer);