From: Mladen Turk Date: Sat, 30 Sep 2006 11:04:46 +0000 (+0000) Subject: Add alternative is_socket_connected implementation. X-Git-Tag: 2.3.0~2105 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=907c904bc63cb63a53d9edc8c5649f061a5a9769;p=thirdparty%2Fapache%2Fhttpd.git Add alternative is_socket_connected implementation. It works on win32 and linux for sure, so that's why I put it inside the #ifdef. Of course something like that needs APR abstraction. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@451580 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/modules/proxy/proxy_util.c b/modules/proxy/proxy_util.c index fdf926f0801..66fe5bdff4d 100644 --- a/modules/proxy/proxy_util.c +++ b/modules/proxy/proxy_util.c @@ -2074,6 +2074,63 @@ ap_proxy_determine_connection(apr_pool_t *p, request_rec *r, return OK; } +#if defined(WIN32) || defined(LINUX) +#define USE_ALTERNATE_IS_CONNECTED 1 +#else +#define USE_ALTERNATE_IS_CONNECTED 0 +#endif + +#if USE_ALTERNATE_IS_CONNECTED +static int is_socket_connected(apr_socket_t *socket) + +{ + fd_set fd; + struct timeval tv; + int rc; + apr_os_sock_t sock; + + if (apr_os_sock_get(&sock, socket) != APR_SUCCESS) + return 0; + + FD_ZERO(&fd); + FD_SET(sock, &fd); + + /* Wait one microsecond */ + tv.tv_sec = 0; + tv.tv_usec = 1; + + do { + rc = select((int)sock + 1, &fd, NULL, NULL, &tv); +#if defined(WIN32) || (defined(NETWARE) && defined(__NOVELL_LIBC__)) + errno = WSAGetLastError() - WSABASEERR; +#endif + } while (rc == -1 && errno == EINTR); + + if (rc == 0) { + /* If we get a timeout, then we are still connected */ + return 1; + } + else if (rc == 1) { +#if defined(WIN32) || (defined(NETWARE) && defined(__NOVELL_LIBC__)) + u_long nr; + if (ioctlsocket(sock, FIONREAD, &nr) == 0) { + if (WSAGetLastError() == 0) + errno = 0; + else + errno = WSAGetLastError() - WSABASEERR; + return nr == 0 ? 0 : 1; + } + errno = WSAGetLastError() - WSABASEERR; +#else + int nr; + if (ioctl(sock, FIONREAD, (void*)&nr) == 0) { + return nr == 0 ? 0 : 1; + } +#endif + } + return 0; +} +#else static int is_socket_connected(apr_socket_t *sock) { @@ -2097,6 +2154,7 @@ static int is_socket_connected(apr_socket_t *sock) return 1; } } +#endif /* USE_ALTERNATE_IS_CONNECTED */ PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function, proxy_conn_rec *conn,