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.
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;
44 static const int default_interval = 60;
47 Terminate a 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) {
55 if(c->status.active) {
56 logger(mesh, MESHLINK_INFO, "Closing connection with %s", c->name);
59 if(c->node && c->node->connection == c) {
60 if(c->status.active && mesh->meta_status_cb) {
61 mesh->meta_status_cb(mesh, (meshlink_node_t *)c->node, false);
64 c->node->connection = NULL;
65 c->node->status.reachable = false;
66 update_node_status(mesh, c->node);
69 c->status.active = false;
71 outgoing_t *outgoing = c->outgoing;
72 connection_del(mesh, c);
74 /* Check if this was our outgoing connection */
77 do_outgoing_connection(mesh, outgoing);
82 Check if the other end is active.
83 If we have sent packets, but didn't receive any,
84 then possibly the other end is dead. We send a
85 PING request over the meta connection. If the other
86 end does not reply in time, we consider them dead
87 and close the connection.
89 static void timeout_handler(event_loop_t *loop, void *data) {
92 meshlink_handle_t *mesh = loop->data;
93 logger(mesh, MESHLINK_DEBUG, "timeout_handler()");
95 for(connection_t *c = mesh->connection; c; c = NULL) {
96 int pingtimeout = c->node ? mesh->dev_class_traits[c->node->devclass].pingtimeout : default_timeout;
97 int pinginterval = c->node ? mesh->dev_class_traits[c->node->devclass].pinginterval : default_interval;
99 if(c->outgoing && !c->status.active && c->outgoing->timeout < 5) {
103 // Also make sure that if outstanding key requests for the UDP counterpart of a connection has timed out, we restart it.
104 if(c->status.active && c->last_key_renewal + 3600 < mesh->loop.now.tv_sec) {
105 devtool_sptps_renewal_probe((meshlink_node_t *)c->node);
107 if(!sptps_force_kex(&c->sptps)) {
108 logger(mesh, MESHLINK_ERROR, "SPTPS key renewal for connection with %s failed", c->name);
109 terminate_connection(mesh, c, true);
112 c->last_key_renewal = mesh->loop.now.tv_sec;
116 if(c->last_ping_time + pingtimeout < mesh->loop.now.tv_sec) {
117 if(c->status.active) {
118 if(c->status.pinged) {
119 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);
120 } else if(c->last_ping_time + pinginterval <= mesh->loop.now.tv_sec) {
127 if(c->status.connecting) {
128 logger(mesh, MESHLINK_WARNING, "Timeout while connecting to %s", c->name);
130 logger(mesh, MESHLINK_WARNING, "Timeout from %s during authentication", c->name);
134 terminate_connection(mesh, c, c->status.active);
138 timeout_set(&mesh->loop, data, &(struct timespec) {
139 1, prng(mesh, TIMER_FUDGE)
143 static void periodic_handler(event_loop_t *loop, void *data) {
144 meshlink_handle_t *mesh = loop->data;
146 /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
147 This usually only happens when another node has the same Name as this node.
148 If so, sleep for a short while to prevent a storm of contradicting messages.
151 if(mesh->contradicting_del_edge > 100 && mesh->contradicting_add_edge > 100) {
152 logger(mesh, MESHLINK_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", mesh->sleeptime);
153 struct timespec ts = {mesh->sleeptime, 0};
154 usleep(mesh->sleeptime * 1000000);
155 mesh->sleeptime *= 2;
157 if(mesh->sleeptime < 0) {
158 mesh->sleeptime = 3600;
161 mesh->sleeptime /= 2;
163 if(mesh->sleeptime < 10) {
164 mesh->sleeptime = 10;
168 mesh->contradicting_add_edge = 0;
169 mesh->contradicting_del_edge = 0;
171 int timeout = default_timeout;
173 /* Check if we need to make or break connections. */
175 if(mesh->peer && !mesh->connection && !mesh->outgoing) {
176 logger(mesh, MESHLINK_DEBUG, "Autoconnecting to %s", mesh->peer->name);
177 mesh->outgoing = xzalloc(sizeof(outgoing_t));
178 mesh->outgoing->node = mesh->peer;
179 setup_outgoing_connection(mesh, mesh->outgoing);
182 for(node_t *n = mesh->peer; n; n = NULL) {
183 if(n->status.dirty) {
184 if(!node_write_config(mesh, n, false)) {
185 logger(mesh, MESHLINK_DEBUG, "Could not update %s", n->name);
190 timeout_set(&mesh->loop, data, &(struct timespec) {
191 timeout, prng(mesh, TIMER_FUDGE)
195 void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) {
196 if(!receive_meta(mesh, c)) {
197 terminate_connection(mesh, c, c->status.active);
202 void retry(meshlink_handle_t *mesh) {
203 /* Reset the reconnection timers for all outgoing connections */
204 for(outgoing_t *outgoing = mesh->outgoing; outgoing; outgoing = NULL) {
205 outgoing->timeout = 0;
207 if(outgoing->ev.cb) {
208 timeout_set(&mesh->loop, &outgoing->ev, &(struct timespec) {
214 /* For active connections, check if their addresses are still valid.
215 * If yes, reset their ping timers, otherwise terminate them. */
216 for(connection_t *c = mesh->connection; c; c = NULL) {
217 if(!c->status.active) {
221 if(!c->status.pinged) {
222 c->last_ping_time = -3600;
226 socklen_t salen = sizeof(sa);
228 if(getsockname(c->socket, &sa.sa, &salen)) {
232 switch(sa.sa.sa_family) {
238 sa.in6.sin6_port = 0;
245 int sock = socket(sa.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
251 if(bind(sock, &sa.sa, salen) && errno == EADDRNOTAVAIL) {
252 logger(mesh, MESHLINK_DEBUG, "Local address for connection to %s no longer valid, terminating", c->name);
253 terminate_connection(mesh, c, c->status.active);
259 /* Kick the ping timeout handler */
260 if(mesh->pingtimer.cb) {
261 timeout_set(&mesh->loop, &mesh->pingtimer, &(struct timespec) {
268 this is where it all happens...
270 void main_loop(meshlink_handle_t *mesh) {
271 timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timespec) {
272 1, prng(mesh, TIMER_FUDGE)
274 timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timespec) {
279 mesh->datafromapp.signum = 0;
280 signal_add(&mesh->loop, &mesh->datafromapp, meshlink_send_from_queue, mesh, mesh->datafromapp.signum);
282 if(!event_loop_run(&mesh->loop, mesh)) {
283 logger(mesh, MESHLINK_ERROR, "Error while waiting for input: %s", strerror(errno));
284 call_error_cb(mesh, MESHLINK_ENETWORK);
287 signal_del(&mesh->loop, &mesh->datafromapp);
288 timeout_del(&mesh->loop, &mesh->periodictimer);
289 timeout_del(&mesh->loop, &mesh->pingtimer);