- if(connect_to && !connect_to->connection)
- {
- logger(mesh, MESHLINK_INFO, "Autoconnecting to %s", connect_to->name);
- outgoing_t *outgoing = xzalloc(sizeof(outgoing_t));
- outgoing->mesh = mesh;
- outgoing->name = xstrdup(connect_to->name);
- list_insert_tail(mesh->outgoings, outgoing);
- setup_outgoing_connection(mesh, outgoing);
+ if(connect_to && !connect_to->connection) {
+ connect_to->last_connect_try = time(NULL);
+ logger(mesh, MESHLINK_DEBUG, "Autoconnect trying to connect to %s", connect_to->name);
+
+ /* check if there is already a connection attempt to this node */
+ bool skip = false;
+
+ for list_each(outgoing_t, outgoing, mesh->outgoings) {
+ if(outgoing->node == connect_to) {
+ logger(mesh, MESHLINK_DEBUG, "* skip autoconnect since it is an outgoing connection already");
+ skip = true;
+ break;
+ }
+ }
+
+ if(!connect_to->status.reachable && !node_read_public_key(mesh, connect_to)) {
+ logger(mesh, MESHLINK_DEBUG, "* skip autoconnect since we don't know this node's public key");
+ skip = true;
+ }
+
+ if(!skip) {
+ logger(mesh, MESHLINK_DEBUG, "Autoconnecting to %s", connect_to->name);
+ outgoing_t *outgoing = xzalloc(sizeof(outgoing_t));
+ outgoing->node = connect_to;
+ list_insert_tail(mesh->outgoings, outgoing);
+ setup_outgoing_connection(mesh, outgoing);
+ }