From 9b98dbde344781e93e2bdcfa599428cda2fda41d Mon Sep 17 00:00:00 2001 From: David 'Digit' Turner Date: Fri, 30 Jul 2010 15:35:00 -0700 Subject: Better detection of incorrect proxy values when starting the emulator. Change-Id: I344f06fc16e051359669b8fe9b8dcec0ceca377d --- proxy/proxy_common.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ proxy/proxy_common.h | 12 ++++++++++++ proxy/proxy_http_rewriter.c | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) (limited to 'proxy') diff --git a/proxy/proxy_common.c b/proxy/proxy_common.c index 7794a62..3a04eb8 100644 --- a/proxy/proxy_common.c +++ b/proxy/proxy_common.c @@ -17,6 +17,7 @@ #include #include "android/utils/misc.h" #include "android/utils/system.h" +#include "iolooper.h" #include int proxy_log = 0; @@ -530,3 +531,49 @@ Exit: } +int +proxy_check_connection( const char* proxyname, + int proxyname_len, + int proxyport, + int timeout_ms ) +{ + SockAddress addr; + int sock; + IoLooper* looper; + int ret; + + if (proxy_resolve_server(&addr, proxyname, proxyname_len, proxyport) < 0) { + return -1; + } + + sock = socket_create(addr.family, SOCKET_STREAM); + if (sock < 0) { + PROXY_LOG("%s: Could not create socket !?: %s", __FUNCTION__, errno_str); + return -1; + } + + socket_set_nonblock(sock); + + /* An immediate connection is very unlikely, but deal with it, just in case */ + if (socket_connect(sock, &addr) == 0) { + PROXY_LOG("%s: Immediate connection to %.*s:%d: %s !", + __FUNCTION__, proxyname_len, proxyname, proxyport); + socket_close(sock); + return 0; + } + + /* Ok, create an IoLooper object to wait for the connection */ + looper = iolooper_new(); + iolooper_add_write(looper, sock); + + ret = iolooper_wait(looper, timeout_ms); + + iolooper_free(looper); + socket_close(sock); + + if (ret == 0) { + errno = ETIMEDOUT; + ret = -1; + } + return ret; +} diff --git a/proxy/proxy_common.h b/proxy/proxy_common.h index 78eddd8..5c5789f 100644 --- a/proxy/proxy_common.h +++ b/proxy/proxy_common.h @@ -85,4 +85,16 @@ extern void proxy_manager_poll( fd_set* read_fds, fd_set* write_fds, fd_set* err_fds ); +/* this function checks that one can connect to a given proxy. It will simply try to connect() + * to it, for a specified timeout, in milliseconds, then close the connection. + * + * returns 0 in case of success, and -1 in case of error. errno will be set to ETIMEDOUT in + * case of timeout, or ECONNREFUSED if the connection is refused. + */ + +extern int proxy_check_connection( const char* proxyname, + int proxyname_len, + int proxyport, + int timeout_ms ); + #endif /* END */ diff --git a/proxy/proxy_http_rewriter.c b/proxy/proxy_http_rewriter.c index 7645ecc..af3f5e7 100644 --- a/proxy/proxy_http_rewriter.c +++ b/proxy/proxy_http_rewriter.c @@ -1121,7 +1121,7 @@ http_rewriter_connect( HttpService* service, RewriteConnection* conn; int s; - s = socket_create_inet( SOCKET_STREAM ); + s = socket_create(address->family, SOCKET_STREAM ); if (s < 0) return NULL; -- cgit v1.1