[master] c69345a Make VTCP_Connect() able to support async connections where the calling code is responsible for poll(2)'ing the fd for connection completion.

Poul-Henning Kamp phk at FreeBSD.org
Wed Mar 11 23:53:01 CET 2015


commit c69345aa9c31c6bf4b02a8250c3c3beb299f54b0
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed Mar 11 22:50:07 2015 +0000

    Make VTCP_Connect() able to support async connections where
    the calling code is responsible for poll(2)'ing the fd for
    connection completion.

diff --git a/include/vtcp.h b/include/vtcp.h
index b49f3ae..3165364 100644
--- a/include/vtcp.h
+++ b/include/vtcp.h
@@ -51,6 +51,7 @@ int VTCP_check_hup(int sock);
 #ifdef SOL_SOCKET
 void VTCP_name(const struct suckaddr *addr, char *abuf, unsigned alen,
     char *pbuf, unsigned plen);
+int VTCP_connected(int s);
 int VTCP_connect(const struct suckaddr *name, int msec);
 void VTCP_close(int *s);
 void VTCP_set_read_timeout(int s, double seconds);
diff --git a/lib/libvarnish/vtcp.c b/lib/libvarnish/vtcp.c
index bdee1c7..be7f6e0 100644
--- a/lib/libvarnish/vtcp.c
+++ b/lib/libvarnish/vtcp.c
@@ -212,10 +212,30 @@ VTCP_nonblocking(int sock)
  */
 
 int
-VTCP_connect(const struct suckaddr *name, int msec)
+VTCP_connected(int s)
 {
-	int s, i, k;
+	int k;
 	socklen_t l;
+
+	/* Find out if we got a connection */
+	l = sizeof k;
+	AZ(getsockopt(s, SOL_SOCKET, SO_ERROR, &k, &l));
+
+	/* An error means no connection established */
+	errno = k;
+	if (k) {
+		AZ(close(s));
+		return (-1);
+	}
+
+	(void)VTCP_blocking(s);
+	return (s);
+}
+
+int
+VTCP_connect(const struct suckaddr *name, int msec)
+{
+	int s, i;
 	struct pollfd fds[1];
 	const struct sockaddr *sa;
 	socklen_t sl;
@@ -233,7 +253,7 @@ VTCP_connect(const struct suckaddr *name, int msec)
 		return (s);
 
 	/* Set the socket non-blocking */
-	if (msec > 0)
+	if (msec != 0)
 		(void)VTCP_nonblocking(s);
 
 	i = connect(s, sa, sl);
@@ -244,6 +264,14 @@ VTCP_connect(const struct suckaddr *name, int msec)
 		return (-1);
 	}
 
+	if (msec < 0) {
+		/*
+		 * Caller is responsible for waiting and
+		 * calling VTCP_connected
+		 */
+		return (s);
+	}
+
 	assert(msec > 0);
 	/* Exercise our patience, polling for write */
 	fds[0].fd = s;
@@ -258,19 +286,7 @@ VTCP_connect(const struct suckaddr *name, int msec)
 		return (-1);
 	}
 
-	/* Find out if we got a connection */
-	l = sizeof k;
-	AZ(getsockopt(s, SOL_SOCKET, SO_ERROR, &k, &l));
-
-	/* An error means no connection established */
-	errno = k;
-	if (k) {
-		AZ(close(s));
-		return (-1);
-	}
-
-	(void)VTCP_blocking(s);
-	return (s);
+	return (VTCP_connected(s));
 }
 
 /*--------------------------------------------------------------------



More information about the varnish-commit mailing list