[evolution-data-server/camel-socks-proxy] Propagate errno from the proxy connection functions



commit 15875670819a3d2dddc0836ba6fdd965867cbdb5
Author: Federico Mena Quintero <federico novell com>
Date:   Tue Jun 1 14:48:13 2010 -0500

    Propagate errno from the proxy connection functions
    
    One missing case, however, is at the step where we camel_getaddrinfo() for the
    proxy hostname.  That function returns a CamelException, but we don't have a
    good way to translate that to an errno.  So, we return a EHOSTUNREACH in
    that case.
    
    Signed-off-by: Federico Mena Quintero <federico novell com>

 camel/camel-tcp-stream-raw.c |   18 ++++++++++++++----
 camel/camel-tcp-stream-ssl.c |   18 ++++++++++++++----
 2 files changed, 28 insertions(+), 8 deletions(-)
---
diff --git a/camel/camel-tcp-stream-raw.c b/camel/camel-tcp-stream-raw.c
index 21ae9c0..89a29e5 100644
--- a/camel/camel-tcp-stream-raw.c
+++ b/camel/camel-tcp-stream-raw.c
@@ -414,6 +414,7 @@ connect_to_socks4_proxy (const gchar *proxy_host, gint proxy_port, struct addrin
 	gchar request[9];
 	struct sockaddr_in *sin;
 	gchar reply[8];
+	gint save_errno;
 
 	g_assert (proxy_host != NULL);
 
@@ -422,16 +423,21 @@ connect_to_socks4_proxy (const gchar *proxy_host, gint proxy_port, struct addrin
 	memset (&hints, 0, sizeof (hints));
 	hints.ai_socktype = SOCK_STREAM;
 
-	ai = camel_getaddrinfo (proxy_host, serv, &hints, NULL);
-	if (!ai)
+	ai = camel_getaddrinfo (proxy_host, serv, &hints, NULL); /* NULL-CamelException */
+	if (!ai) {
+		errno = EHOSTUNREACH; /* FIXME: this is not an accurate error; we should translate the CamelException to an errno */
 		return -1;
+	}
 
 	fd = socket_connect (ai);
+	save_errno = errno;
 
 	camel_freeaddrinfo (ai);
 
-	if (fd == -1)
+	if (fd == -1) {
+		errno = save_errno;
 		goto error;
+	}
 
 	g_assert (connect_addr->ai_addr->sa_family == AF_INET); /* FIXME: what to do about IPv6?  Are we just screwed with SOCKS4? */
 	sin = (struct sockaddr_in *) connect_addr->ai_addr;
@@ -449,14 +455,18 @@ connect_to_socks4_proxy (const gchar *proxy_host, gint proxy_port, struct addrin
 		goto error;
 
 	if (!(reply[0] == 0		/* first byte of reply is 0 */
-	      && reply[1] == 90))	/* 90 means "request granted" */
+	      && reply[1] == 90)) {	/* 90 means "request granted" */
+		errno = ECONNREFUSED;
 		goto error;
+	}
 
 	goto out;
 
 error:
 	if (fd != -1) {
+		save_errno = errno;
 		SOCKET_CLOSE (fd);
+		errno = save_errno;
 		fd = -1;
 	}
 
diff --git a/camel/camel-tcp-stream-ssl.c b/camel/camel-tcp-stream-ssl.c
index b22201e..d013e96 100644
--- a/camel/camel-tcp-stream-ssl.c
+++ b/camel/camel-tcp-stream-ssl.c
@@ -1242,6 +1242,7 @@ connect_to_socks4_proxy (CamelTcpStreamSSL *ssl, const gchar *proxy_host, gint p
 	gchar request[9];
 	struct sockaddr_in *sin;
 	gchar reply[8];
+	gint save_errno;
 
 	g_assert (proxy_host != NULL);
 
@@ -1250,16 +1251,21 @@ connect_to_socks4_proxy (CamelTcpStreamSSL *ssl, const gchar *proxy_host, gint p
 	memset (&hints, 0, sizeof (hints));
 	hints.ai_socktype = SOCK_STREAM;
 	
-	ai = camel_getaddrinfo (proxy_host, serv, &hints, NULL);
-	if (!ai)
+	ai = camel_getaddrinfo (proxy_host, serv, &hints, NULL);  /* NULL-CamelException */
+	if (!ai) {
+		errno = EHOSTUNREACH; /* FIXME: this is not an accurate error; we should translate the CamelException to an errno */
 		return NULL;
+	}
 
 	fd = socket_connect (CAMEL_TCP_STREAM (ssl), ai, FALSE);
+	save_errno = errno;
 
 	camel_freeaddrinfo (ai);
 
-	if (!fd)
+	if (!fd) {
+		errno = save_errno;
 		goto error;
+	}
 
 	g_assert (connect_addr->ai_addr->sa_family == AF_INET); /* FIXME: what to do about IPv6?  Are we just screwed with SOCKS4? */
 	sin = (struct sockaddr_in *) connect_addr->ai_addr;
@@ -1277,8 +1283,10 @@ connect_to_socks4_proxy (CamelTcpStreamSSL *ssl, const gchar *proxy_host, gint p
 		goto error;
 
 	if (!(reply[0] == 0		/* first byte of reply is 0 */
-	      && reply[1] == 90))	/* 90 means "request granted" */
+	      && reply[1] == 90)) {	/* 90 means "request granted" */
+		errno = ECONNREFUSED;
 		goto error;
+	}
 
 	/* We are now proxied we are ready to send "normal" data through the socket */
 
@@ -1292,8 +1300,10 @@ connect_to_socks4_proxy (CamelTcpStreamSSL *ssl, const gchar *proxy_host, gint p
 
 error:
 	if (fd) {
+		save_errno = errno;
 		PR_Shutdown (fd, PR_SHUTDOWN_BOTH);
 		PR_Close (fd);
+		errno = save_errno;
 		fd = NULL;
 	}
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]