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



commit f0b8370563d3ab644b973b0270985a8b8928e17a
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 3bc1253..333cf31 100644
--- a/camel/camel-tcp-stream-raw.c
+++ b/camel/camel-tcp-stream-raw.c
@@ -386,6 +386,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);
 
@@ -394,16 +395,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;
@@ -421,14 +427,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 93bad39..70d98d9 100644
--- a/camel/camel-tcp-stream-ssl.c
+++ b/camel/camel-tcp-stream-ssl.c
@@ -1101,6 +1101,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);
 
@@ -1109,16 +1110,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;
@@ -1136,8 +1142,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 */
 
@@ -1151,8 +1159,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]