+ logger(mesh, MESHLINK_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname);
+ }
+ terminate_connection(mesh, c, c->status.active);
+ }
+ }
+
+ timeout_set(&mesh->loop, data, &(struct timeval){mesh->pingtimeout, rand() % 100000});
+}
+
+/// Utility function to establish connections based on condition check
+/** The function iterates over all nodes, but skips those that do
+ * not pass the condition check.
+ *
+ * The condition check function is passed
+ * a pointer to a random number r between 0 and rand_modulo, a pointer to the
+ * current node index i, and the node pointer n. This function should return true
+ * if a connection attempt to the node should be made.
+ *
+ * @param mesh A pointer to the mesh structure
+ * @param rand_modulo Random index is selected between 0 and rand_modulo
+ * @cond_check A function pointer. This function should return true
+ * if a connection attempt to the node should be made
+ */
+static void cond_add_connection(meshlink_handle_t *mesh, int rand_modulo, bool (*cond_check)(int*, int*, node_t*)) {
+ int r = rand() % rand_modulo;
+ int i = 0;
+
+ for splay_each(node_t, n, mesh->nodes) {
+ /* skip nodes that do not pass condition check */
+ if(!(*cond_check)(&i, &r, n))
+ continue;
+
+ /* check if there is already a connection attempt to this node */
+ bool found = false;
+ for list_each(outgoing_t, outgoing, mesh->outgoings) {
+ if(!strcmp(outgoing->name, n->name)) {
+ found = true;
+ break;