]> granicus.if.org Git - php/commitdiff
Switch streams socket abstraction to use a timeval structure instead of an
authorJason Greene <jason@php.net>
Mon, 22 Jul 2002 18:46:26 +0000 (18:46 +0000)
committerJason Greene <jason@php.net>
Mon, 22 Jul 2002 18:46:26 +0000 (18:46 +0000)
integer to allow subsecond timeouts.

This supports the previous behavior of fsockopen()
Fixes bug #16261

ext/ftp/ftp.c
ext/standard/fsock.c
ext/standard/ftp_fopen_wrapper.c
ext/standard/http_fopen_wrapper.c
main/network.c
main/php_network.h

index 97d24f349a398736f165ae59e3b74cac88971d1f..ff5e775644af719bf0be94b1df88ce7177ca1c37 100644 (file)
@@ -105,6 +105,7 @@ ftp_open(const char *host, short port, long timeout_sec)
 {
        ftpbuf_t                *ftp;
        int                     size;
+       struct timeval tv;
 
 
        /* alloc the ftp structure */
@@ -113,8 +114,11 @@ ftp_open(const char *host, short port, long timeout_sec)
                perror("calloc");
                return NULL;
        }
+       
+       tv.tv_sec = timeout_sec;
+       tv.tv_usec = 0;
 
-       ftp->fd = php_hostconnect(host, (unsigned short) (port ? port : 21), SOCK_STREAM, (int) timeout_sec);
+       ftp->fd = php_hostconnect(host, (unsigned short) (port ? port : 21), SOCK_STREAM, &tv);
        if (ftp->fd == -1) {
                goto bail;
        }
index 2b1f1ee20a631fe68445337de02aeb605eae9fcb..514d557296bb01c31bc31f6eeeb11c30f2b0744c 100644 (file)
@@ -194,7 +194,7 @@ static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
                }
                else
 #endif
-               stream = php_stream_sock_open_host(host, (unsigned short)port, socktype, (int)timeout, persistent);
+               stream = php_stream_sock_open_host(host, (unsigned short)port, socktype, &tv, persistent);
 
 #ifdef PHP_WIN32
                /* Preserve error */
index f37b71e01c63cbfda98be3709e1eb693c42d1bbb..9bbdbedee43097a99b90f47a8c275cc076839eed 100644 (file)
@@ -124,7 +124,7 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, char *path, ch
        if (resource->port == 0)
                resource->port = 21;
        
-       stream = php_stream_sock_open_host(resource->host, resource->port, SOCK_STREAM, 0, 0);
+       stream = php_stream_sock_open_host(resource->host, resource->port, SOCK_STREAM, NULL, 0);
        if (stream == NULL)
                goto errexit;
 
index 3ff9d0edc9bec1f45f2468c638d9e915c4aa1a01..3a2aa8e57e9f774730d3fa5fcdde66d3a471922b 100644 (file)
@@ -98,7 +98,7 @@ php_stream *php_stream_url_wrap_http(php_stream_wrapper *wrapper, char *path, ch
        else if (resource->port == 0)
                resource->port = 80;
 
-       stream = php_stream_sock_open_host(resource->host, resource->port, SOCK_STREAM, 0, 0);
+       stream = php_stream_sock_open_host(resource->host, resource->port, SOCK_STREAM, NULL, 0);
        if (stream == NULL)     
                goto out;
 
index 046c5562c7bef019b487bfc798d517eb526bd36b..c697907b31c768ea980fefc81313612988d94a51 100644 (file)
@@ -366,11 +366,12 @@ PHPAPI int php_connect_nonb_win32(SOCKET sockfd,
  * port, returns the created socket on success, else returns -1.
  * timeout gives timeout in seconds, 0 means blocking mode.
  */
-int php_hostconnect(const char *host, unsigned short port, int socktype, int timeout)
+int php_hostconnect(const char *host, unsigned short port, int socktype, struct timeval *timeout)
 {      
        int n, repeatto, s;
        struct sockaddr **sal, **psal;
-       struct timeval timeoutval;
+       struct timeval individual_timeout;
+       int set_timeout = 0;
 #ifdef PHP_WIN32
        int err;
 #endif
@@ -380,14 +381,24 @@ int php_hostconnect(const char *host, unsigned short port, int socktype, int tim
        if (n == 0)
                return -1;
        
-       /* is this a good idea? 5s? */
-       repeatto = timeout / n > 5;
-       if (repeatto) {
-               timeout /= n;
-       }
-       timeoutval.tv_sec = timeout;
-       timeoutval.tv_usec = 0;
+       if (timeout != NULL) {
+               /* is this a good idea? 5s? */
+               repeatto = timeout->tv_sec / n > 5;
+               if (repeatto) {
+                       individual_timeout.tv_sec = timeout->tv_sec / n;
+               } else {
+                       individual_timeout.tv_sec = timeout->tv_sec;
+               }
 
+               individual_timeout.tv_usec = timeout->tv_usec;
+       } else {
+               individual_timeout.tv_sec = 0;
+               individual_timeout.tv_usec = 0;
+       }
+       
+       /* Boolean indicating whether to pass a timeout */
+       set_timeout = individual_timeout.tv_sec + individual_timeout.tv_usec;
+       
        psal = sal;
        while (*sal != NULL) {
                s = socket((*sal)->sa_family, socktype, 0);
@@ -402,7 +413,7 @@ int php_hostconnect(const char *host, unsigned short port, int socktype, int tim
                                                sa->sin6_family = (*sal)->sa_family;
                                                sa->sin6_port = htons(port);
                                                if (php_connect_nonb(s, (struct sockaddr *) sa,
-                                                                       sizeof(*sa), timeout ? &timeoutval : NULL) != SOCK_CONN_ERR)
+                                                                       sizeof(*sa), (set_timeout) ? &individual_timeout : NULL) != SOCK_CONN_ERR)
                                                        goto ok;
                                        } 
                                        break;
@@ -415,7 +426,7 @@ int php_hostconnect(const char *host, unsigned short port, int socktype, int tim
                                                sa->sin_family = (*sal)->sa_family;
                                                sa->sin_port = htons(port);
                                                if (php_connect_nonb(s, (struct sockaddr *) sa,
-                                                                       sizeof(*sa), timeout ? &timeoutval : NULL) != SOCK_CONN_ERR)
+                                                                       sizeof(*sa), (set_timeout) ? &individual_timeout : NULL) != SOCK_CONN_ERR)
                                                        goto ok;
 
                                        } 
@@ -428,10 +439,6 @@ int php_hostconnect(const char *host, unsigned short port, int socktype, int tim
                        close (s);
                }
                sal++;
-               if (repeatto) {
-                       timeoutval.tv_sec = timeout;
-                       timeoutval.tv_usec = 0;
-               }
        }
        php_network_freeaddresses(psal);
        php_error(E_WARNING, "php_hostconnect: connect failed");
@@ -520,7 +527,7 @@ PHPAPI php_stream *_php_stream_sock_open_from_socket(int socket, int persistent
 }
 
 PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
-               int socktype, int timeout, int persistent STREAMS_DC TSRMLS_DC)
+               int socktype, struct timeval *timeout, int persistent STREAMS_DC TSRMLS_DC)
 {
        int socket;
 
index 404dc619de648a84e46ac4a8f4dfe309469802ec..70e7ad5a9c35597120650e4ff052a5f2dce2bf68 100644 (file)
@@ -91,7 +91,7 @@ typedef struct {
 #endif
 
 
-int php_hostconnect(const char *host, unsigned short port, int socktype, int timeout);
+int php_hostconnect(const char *host, unsigned short port, int socktype, struct timeval *timeout);
 PHPAPI int php_connect_nonb(int sockfd, const struct sockaddr *addr, socklen_t addrlen, struct timeval *timeout);
 
 #ifdef PHP_WIN32
@@ -128,7 +128,7 @@ extern php_stream_ops php_stream_socket_ops;
 PHPAPI php_stream *_php_stream_sock_open_from_socket(int socket, int persistent STREAMS_DC TSRMLS_DC );
 /* open a connection to a host using php_hostconnect and return a stream */
 PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
-               int socktype, int timeout, int persistent STREAMS_DC TSRMLS_DC);
+               int socktype, struct timeval *timeout, int persistent STREAMS_DC TSRMLS_DC);
 PHPAPI php_stream *_php_stream_sock_open_unix(const char *path, int pathlen, int persistent,
                struct timeval *timeout STREAMS_DC TSRMLS_DC);