try_outgoing_connections(mesh);
- main_loop();
+ main_loop(mesh);
return NULL;
}
void meshlink_close(meshlink_handle_t *mesh) {
// Close and free all resources used.
- close_network_connections();
+ close_network_connections(mesh);
logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
#include "protocol.h"
#include "xalloc.h"
-/* Purge edges of unreachable nodes. Use carefully. */
-
-// TODO: remove
-void purge(void) {
- logger(DEBUG_PROTOCOL, LOG_DEBUG, "Purging unreachable nodes");
-
- /* Remove all edges owned by unreachable nodes. */
-
- for splay_each(node_t, n, mesh->nodes) {
- if(!n->status.reachable) {
- logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname);
-
- for splay_each(edge_t, e, n->edge_tree) {
- send_del_edge(mesh->everyone, e);
- edge_del(e);
- }
- }
- }
-
- /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */
-
- for splay_each(node_t, n, mesh->nodes) {
- if(!n->status.reachable) {
- for splay_each(edge_t, e, mesh->edges)
- if(e->to == n)
- return;
- }
- }
-}
-
/*
Terminate a connection:
- Mark it as inactive
- Kill it with fire
- Check if we need to retry making an outgoing connection
*/
-void terminate_connection(connection_t *c, bool report) {
+void terminate_connection(meshlink_handle_t *mesh, connection_t *c, bool report) {
logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname);
c->status.active = false;
and close the connection.
*/
static void timeout_handler(event_loop_t *loop, void *data) {
+ meshlink_handle_t *mesh = loop->data;
+
for list_each(connection_t, c, mesh->connections) {
if(c->last_ping_time + mesh->pingtimeout <= mesh->loop.now.tv_sec) {
if(c->status.active) {
else
logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
}
- terminate_connection(c, c->status.active);
+ terminate_connection(mesh, c, c->status.active);
}
}
}
static void periodic_handler(event_loop_t *loop, void *data) {
+ meshlink_handle_t *mesh = loop->data;
+
/* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
This usually only happens when another node has the same Name as this node.
If so, sleep for a short while to prevent a storm of contradicting messages.
logger(DEBUG_CONNECTIONS, LOG_INFO, "Autodisconnecting from %s", c->name);
list_delete(mesh->outgoings, c->outgoing);
c->outgoing = NULL;
- terminate_connection(c, c->status.active);
+ terminate_connection(mesh, c, c->status.active);
break;
}
}
timeout_set(&mesh->loop, data, &(struct timeval){5, rand() % 100000});
}
-void handle_meta_connection_data(connection_t *c) {
+void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) {
if (!receive_meta(c)) {
- terminate_connection(c, c->status.active);
+ terminate_connection(mesh, c, c->status.active);
return;
}
}
-void retry(void) {
+void retry(meshlink_handle_t *mesh) {
/* Reset the reconnection timers for all outgoing connections */
for list_each(outgoing_t, outgoing, mesh->outgoings) {
outgoing->timeout = 0;
/*
this is where it all happens...
*/
-int main_loop(void) {
+int main_loop(meshlink_handle_t *mesh) {
timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval){mesh->pingtimeout, rand() % 100000});
timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){mesh->pingtimeout, rand() % 100000});
extern bool setup_myself_reloadable(struct meshlink_handle *mesh);
extern bool setup_network(struct meshlink_handle *mesh);
extern void setup_outgoing_connection(struct meshlink_handle *mesh, struct outgoing_t *);
-extern void try_outgoing_connections(struct meshlink_handle * mesh);
-extern void close_network_connections(void);
-extern int main_loop(void);
-extern void terminate_connection(struct connection_t *, bool);
+extern void try_outgoing_connections(struct meshlink_handle *mesh);
+extern void close_network_connections(struct meshlink_handle *mesh);
+extern int main_loop(struct meshlink_handle *mesh);
+extern void terminate_connection(struct meshlink_handle *mesh, struct connection_t *, bool);
extern bool node_read_ecdsa_public_key(struct meshlink_handle *mesh, struct node_t *);
extern bool read_ecdsa_public_key(struct meshlink_handle *mesh, struct connection_t *);
extern void send_mtu_probe(struct node_t *);
-extern void handle_meta_connection_data(struct connection_t *);
-extern void regenerate_key(void);
-extern void purge(void);
-extern void retry(void);
+extern void handle_meta_connection_data(struct meshlink_handle *mesh, struct connection_t *);
+extern void retry(struct meshlink_handle *mesh);
#ifndef HAVE_MINGW
#define closesocket(s) close(s)
/*
close all open network connections
*/
-void close_network_connections(void) {
+void close_network_connections(meshlink_handle_t *mesh) {
for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
next = node->next;
connection_t *c = node->data;
c->outgoing = NULL;
- terminate_connection(c, false);
+ terminate_connection(mesh, c, false);
}
if(mesh->outgoings)
list_delete_list(mesh->outgoings);
if(mesh->self && mesh->self->connection) {
- terminate_connection(mesh->self->connection, false);
+ terminate_connection(mesh, mesh->self->connection, false);
free_connection(mesh->self->connection);
}
logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not send %d bytes of data to %s (%s): %s", c->outbuf.len - c->outbuf.offset, c->name, c->hostname, strerror(errno));
}
- terminate_connection(c, c->status.active);
+ terminate_connection(mesh, c, c->status.active);
return;
}
finish_connecting(mesh, c);
else {
logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): %s", c->name, c->hostname, sockstrerror(result));
- terminate_connection(c, false);
+ terminate_connection(mesh, c, false);
return;
}
}
if(flags & IO_WRITE)
handle_meta_write(mesh, c);
else
- handle_meta_connection_data(c);
+ handle_meta_connection_data(mesh, c);
}
bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
if(c->outgoing && c->outgoing->timeout == -1) {
c->outgoing = NULL;
logger(DEBUG_CONNECTIONS, LOG_INFO, "No more outgoing connection to %s", c->name);
- terminate_connection(c, c->status.active);
+ terminate_connection(mesh, c, c->status.active);
}
}
n->connection->outgoing = NULL;
}
- terminate_connection(n->connection, false);
+ terminate_connection(mesh, n->connection, false);
/* Run graph algorithm to keep things in sync */
graph();
}