From bc0a24ec810cb911610ae7aafa245e47d1268cd2 Mon Sep 17 00:00:00 2001
From: Guus Sliepen <guus@tinc-vpn.org>
Date: Sat, 19 May 2007 13:34:32 +0000
Subject: [PATCH] Fix retrying outgoing connections.

---
 src/connection.h |  2 +-
 src/net.c        | 17 ++++++++---------
 src/net_socket.c |  4 +++-
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/connection.h b/src/connection.h
index 6537e519..ec6c0635 100644
--- a/src/connection.h
+++ b/src/connection.h
@@ -41,7 +41,7 @@ typedef union connection_status_t {
 		int connecting:1;			/* 1 if we are waiting for a non-blocking connect() to finish */
 		int termreq:1;				/* the termination of this connection was requested */
 		int remove_unused:1;				/* Set to 1 if you want this connection removed */
-		int timeout:1;				/* 1 if gotten timeout */
+		int timeout_unused:1;				/* 1 if gotten timeout */
 		int encryptout:1;			/* 1 if we can encrypt outgoing traffic */
 		int decryptin:1;			/* 1 if we have to decrypt incoming traffic */
 		int mst:1;				/* 1 if this connection is part of a minimum spanning tree */
diff --git a/src/net.c b/src/net.c
index d78770e1..2c7cc55a 100644
--- a/src/net.c
+++ b/src/net.c
@@ -154,12 +154,10 @@ void terminate_connection(connection_t *c, bool report) {
 
 	/* Check if this was our outgoing connection */
 
-	if(c->outgoing) {
+	if(c->outgoing)
 		retry_outgoing(c->outgoing);
-		c->outgoing = NULL;
-	} else {
-		connection_del(c);
-	}
+
+	connection_del(c);
 }
 
 /*
@@ -186,20 +184,20 @@ static void timeout_handler(int fd, short events, void *event) {
 				if(c->status.pinged) {
 					ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
 							   c->name, c->hostname, now - c->last_ping_time);
-					c->status.timeout = true;
 					terminate_connection(c, true);
 					continue;
 				} else if(c->last_ping_time + pinginterval < now) {
 					send_ping(c);
 				}
 			} else {
-				ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"),
-						   c->name, c->hostname);
 				if(c->status.connecting) {
+					ifdebug(CONNECTIONS)
+						logger(LOG_WARNING, _("Timeout while connecting to %s (%s)"), c->name, c->hostname);
 					c->status.connecting = false;
 					closesocket(c->socket);
 					do_outgoing_connection(c);
 				} else {
+					ifdebug(CONNECTIONS) logger(LOG_WARNING, _("Timeout from %s (%s) during authentication"), c->name, c->hostname);
 					terminate_connection(c, false);
 					continue;
 				}
@@ -216,6 +214,8 @@ void handle_meta_connection_data(int fd, short events, void *data) {
 	socklen_t len = sizeof(result);
 
 	if(c->status.connecting) {
+		c->status.connecting = false;
+
 		getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
 
 		if(!result)
@@ -224,7 +224,6 @@ void handle_meta_connection_data(int fd, short events, void *data) {
 			ifdebug(CONNECTIONS) logger(LOG_DEBUG,
 					   _("Error while connecting to %s (%s): %s"),
 					   c->name, c->hostname, strerror(result));
-			c->status.connecting = false;
 			closesocket(c->socket);
 			do_outgoing_connection(c);
 			return;
diff --git a/src/net_socket.c b/src/net_socket.c
index 3fea33e1..7ef81931 100644
--- a/src/net_socket.c
+++ b/src/net_socket.c
@@ -231,7 +231,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) {
 }
 
 static void retry_outgoing_handler(int fd, short events, void *data) {
-	do_outgoing_connection(data);
+	setup_outgoing_connection(data);
 }
 
 void retry_outgoing(outgoing_t *outgoing) {
@@ -275,6 +275,8 @@ begin:
 			ifdebug(CONNECTIONS) logger(LOG_ERR, _("Could not set up a meta connection to %s"),
 					   c->name);
 			retry_outgoing(c->outgoing);
+			c->outgoing = NULL;
+			connection_del(c);
 			return;
 		}
 
-- 
2.39.5