X-Git-Url: http://www.privoxy.org/gitweb/?a=blobdiff_plain;f=jbsockets.c;h=0517ee08526b02e8d90ff127d6c232cafdd8f4d7;hb=81e7a2aa8724b3bb8ee753d268cc2abf54e48ff8;hp=97d733563401d210cc0cf62bc253a032e90d6037;hpb=d416d6368199977e2e0a61b8c2d880f53251a671;p=privoxy.git diff --git a/jbsockets.c b/jbsockets.c index 97d73356..0517ee08 100644 --- a/jbsockets.c +++ b/jbsockets.c @@ -1,4 +1,4 @@ -const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.115 2012/10/12 11:17:48 fabiankeil Exp $"; +const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.123 2013/03/06 21:06:18 diem Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jbsockets.c,v $ @@ -83,7 +83,6 @@ const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.115 2012/10/12 11:17:48 fabia #endif -#ifdef FEATURE_CONNECTION_KEEP_ALIVE #ifdef HAVE_POLL #ifdef __GLIBC__ #include @@ -91,7 +90,6 @@ const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.115 2012/10/12 11:17:48 fabia #include #endif /* def __GLIBC__ */ #endif /* HAVE_POLL */ -#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */ #include "project.h" @@ -133,7 +131,7 @@ static jb_socket no_rfc2553_connect_to(const char *host, int portnum, struct cli * * Parameters : * 1 : host = hostname to connect to - * 2 : portnum = port to connent on (XXX: should be unsigned) + * 2 : portnum = port to connect to (XXX: should be unsigned) * 3 : csp = Current client state (buffers, headers, etc...) * * Returns : JB_INVALID_SOCKET => failure, else it is the socket @@ -280,6 +278,17 @@ static jb_socket rfc2553_connect_to(const char *host, int portnum, struct client continue; } +#ifndef _WIN32 + if (fd >= FD_SETSIZE) + { + log_error(LOG_LEVEL_ERROR, + "Server socket number too high to use select(): %d >= %d", + fd, FD_SETSIZE); + close_socket(fd); + return JB_INVALID_SOCKET; + } +#endif + #ifdef TCP_NODELAY { /* turn off TCP coalescence */ int mi = 1; @@ -462,6 +471,17 @@ static jb_socket no_rfc2553_connect_to(const char *host, int portnum, struct cli return(JB_INVALID_SOCKET); } +#ifndef _WIN32 + if (fd >= FD_SETSIZE) + { + log_error(LOG_LEVEL_ERROR, + "Server socket number too high to use select(): %d >= %d", + fd, FD_SETSIZE); + close_socket(fd); + return JB_INVALID_SOCKET; + } +#endif + #ifdef TCP_NODELAY { /* turn off TCP coalescence */ int mi = 1; @@ -734,7 +754,7 @@ void drain_and_close_socket(jb_socket fd) #endif if (0 != shutdown(fd, SHUT_WR)) { - log_error(LOG_LEVEL_ERROR, "Failed to shutdown socket %d %E", fd); + log_error(LOG_LEVEL_CONNECT, "Failed to shutdown socket %d: %E", fd); } #endif #define ARBITRARY_DRAIN_LIMIT 10000 @@ -742,10 +762,19 @@ void drain_and_close_socket(jb_socket fd) { char drainage[500]; + if (!data_is_available(fd, 0)) + { + /* + * If there is no data available right now, don't try + * to drain the socket as read_socket() could block. + */ + break; + } + bytes_drained = read_socket(fd, drainage, sizeof(drainage)); if (bytes_drained < 0) { - log_error(LOG_LEVEL_ERROR, "Failed to drain socket %d %E", fd); + log_error(LOG_LEVEL_CONNECT, "Failed to drain socket %d: %E", fd); } else if (bytes_drained > 0) { @@ -1005,7 +1034,7 @@ void get_host_information(jb_socket afd, char **ip_address, char **port, struct sockaddr_in server; struct hostent *host = NULL; #endif /* HAVE_RFC2553 */ -#if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA) +#if defined(_WIN32) || defined(__OS2__) || defined(AMIGA) /* according to accept_connection() this fixes a warning. */ int s_length, s_length_provided; #else @@ -1043,7 +1072,7 @@ void get_host_information(jb_socket afd, char **ip_address, char **port, * configured with --disable-ipv6-support. * The proper fix is to not use NI_MAXSERV in * that case. It works by accident on other platforms - * as in included unconditionally there. + * as is included unconditionally there. */ #ifndef NI_MAXSERV #define NI_MAXSERV 32 @@ -1175,7 +1204,7 @@ int accept_connection(struct client_state * csp, jb_socket fds[]) struct sockaddr_in client; #endif jb_socket afd; -#if defined(_WIN32) || defined(__OS2__) || defined(__APPLE_CC__) || defined(AMIGA) +#if defined(_WIN32) || defined(__OS2__) || defined(AMIGA) /* Wierdness - fix a warning. */ int c_length; #else @@ -1267,6 +1296,29 @@ int accept_connection(struct client_state * csp, jb_socket fds[]) } #endif +#ifdef SO_LINGER + { + struct linger linger_options; + linger_options.l_onoff = 1; + linger_options.l_linger = 5; + if (0 != setsockopt(fd, SOL_SOCKET, SO_LINGER, &linger_options, sizeof(linger_options))) + { + log_error(LOG_LEVEL_ERROR, "Setting SO_LINGER on socket %d failed.", afd); + } + } +#endif + +#ifndef _WIN32 + if (afd >= FD_SETSIZE) + { + log_error(LOG_LEVEL_ERROR, + "Client socket number too high to use select(): %d >= %d", + afd, FD_SETSIZE); + close_socket(afd); + return 0; + } +#endif + csp->cfd = afd; #ifdef HAVE_RFC2553 csp->ip_addr_str = malloc(NI_MAXHOST); @@ -1411,7 +1463,6 @@ unsigned long resolve_hostname_to_ip(const char *host) } -#ifdef FEATURE_CONNECTION_KEEP_ALIVE /********************************************************************* * * Function : socket_is_still_alive @@ -1465,7 +1516,6 @@ int socket_is_still_alive(jb_socket sfd) return (no_data_waiting || (1 == recv(sfd, buf, 1, MSG_PEEK))); } -#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */ /*