-void handle_meta_connection_data(int fd, short events, void *data) {
- connection_t *c = data;
- int result;
- socklen_t len = sizeof result;
-
- if(c->status.connecting) {
- c->status.connecting = false;
-
- getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
-
- if(!result)
- finish_connecting(c);
- else {
- ifdebug(CONNECTIONS) logger(LOG_DEBUG,
- "Error while connecting to %s (%s): %s",
- c->name, c->hostname, sockstrerror(result));
- closesocket(c->socket);
- do_outgoing_connection(c);
- return;
+/// 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;
+ }