diff options
Diffstat (limited to 'proxy')
-rw-r--r-- | proxy/proxy_common.c | 47 | ||||
-rw-r--r-- | proxy/proxy_common.h | 12 | ||||
-rw-r--r-- | proxy/proxy_http_rewriter.c | 2 |
3 files changed, 60 insertions, 1 deletions
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 <errno.h> #include "android/utils/misc.h" #include "android/utils/system.h" +#include "iolooper.h" #include <stdlib.h> 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; |