CVS update: php3/functions
| From: | sas | Date: | Wed, 26 May 1999 19:03:19 +0000 |
| Subject: | CVS update: php3/functions | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-6029@lists.php.net to get a copy of this message | ||
Date: Wednesday May 26, 1999 @ 15:03
Author: sas
Update of /repository/php3/functions
In directory php:/tmp/cvs-serv14754
Modified Files:
fsock.c
Log Message:
add connection timeout to p?fsockopen. supplied as 5th parameter to
the function call
Index: php3/functions/fsock.c
diff -u php3/functions/fsock.c:1.105 php3/functions/fsock.c:1.106
--- php3/functions/fsock.c:1.105 Wed May 26 09:47:11 1999
+++ php3/functions/fsock.c Wed May 26 15:03:18 1999
@@ -27,7 +27,7 @@
| Jim Winstead (jimw@php.net) |
+----------------------------------------------------------------------+
*/
-/* $Id: fsock.c,v 1.105 1999/05/26 13:47:11 sas Exp $ */
+/* $Id: fsock.c,v 1.106 1999/05/26 19:03:18 sas Exp $ */
#ifdef THREAD_SAFE
#include "tls.h"
#endif
@@ -40,6 +40,14 @@
#include <unistd.h>
#endif
+#if HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
#include <sys/types.h>
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
@@ -149,28 +157,96 @@
return 0;
}
/* }}} */
-/* {{{ _php3_fsockopen() */
+/* probably won't work on Win32, someone else might try it (read: fix it ;) */
+#if !defined(WIN32) && (defined(O_NONBLOCK) || defined(O_NDELAY))
+
+#ifndef O_NONBLOCK
+#define O_NONBLOCK O_NDELAY
+#endif
+
+static int connect_nonb(int sockfd, struct sockaddr *addr, int addrlen, struct timeval *timeout)
+{
+ int flags;
+ int n;
+ int error = 0;
+ int len;
+ int ret = 0;
+ fd_set rset;
+ fd_set wset;
+
+ flags = fcntl(sockfd, F_GETFL, 0);
+ fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
+
+ if((n = connect(sockfd, addr, addrlen)) < 0)
+ if(errno != EINPROGRESS)
+ return -1;
+
+ if(n == 0)
+ goto ok;
+
+ FD_ZERO(&rset);
+ FD_SET(sockfd, &rset);
+
+ wset = rset;
+
+ if((n = select(sockfd + 1, &rset, &wset, NULL, timeout)) == 0) {
+ error = ETIMEDOUT;
+ }
+
+ if(FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
+ len = sizeof(error);
+ /*
+ BSD-derived systems set errno correctly
+ Solaris returns -1 from getsockopt in case of error
+ */
+ if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
+ ret = -1;
+ } else {
+ /* whoops: sockfd has disappeared */
+ ret = -1;
+ }
+
+ok:
+ fcntl(sockfd, F_SETFL, flags);
+
+ if(error) {
+ errno = error;
+ ret = -1;
+ }
+ return ret;
+}
+#else
+
+#define connect_nonb(a,b,c,d) connect(a,b,c)
+
+#endif
+
+/* {{{ _php3_fsockopen() */
/*
This function takes an optional third argument which should be
passed by reference. The error code from the connect call is written
to this variable.
*/
static void _php3_fsockopen(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
- pval *args[4];
+ pval *args[5];
int *sock=emalloc(sizeof(int));
int *sockp;
int id, arg_count=ARG_COUNT(ht);
int socketd = -1;
+ struct timeval timeout = { 60, 0 };
unsigned short portno;
char *key = NULL;
TLS_VARS;
- if (arg_count > 4 || arg_count < 2 || getParametersArray(ht,arg_count,args)==FAILURE) {
+ if (arg_count > 5 || arg_count < 2 || getParametersArray(ht,arg_count,args)==FAILURE) {
FREE_SOCK;
WRONG_PARAM_COUNT;
}
switch(arg_count) {
+ case 5:
+ convert_to_long(args[4]);
+ timeout.tv_sec = args[4]->value.lval;
case 4:
if(!ParameterPassedByReference(ht,4)) {
php3_error(E_WARNING,"error string argument to fsockopen not passed by reference");
@@ -221,7 +297,7 @@
server.sin_port = htons(portno);
- if (connect(socketd, (struct sockaddr *)&server, sizeof(server)) == SOCK_CONN_ERR) {
+ if (connect_nonb(socketd, (struct sockaddr *)&server, sizeof(server), &timeout) ==
SOCK_CONN_ERR) {
FREE_SOCK;
if(arg_count>2) args[2]->value.lval = errno;
if(arg_count>3) {
@@ -244,7 +320,7 @@
unix_addr.sun_family = AF_UNIX;
strcpy(unix_addr.sun_path, args[0]->value.str.val);
- if (connect(socketd, (struct sockaddr *) &unix_addr, sizeof(unix_addr)) == SOCK_CONN_ERR) {
+ if (connect_nonb(socketd, (struct sockaddr *) &unix_addr, sizeof(unix_addr), &timeout) ==
SOCK_CONN_ERR) {
FREE_SOCK;
if(arg_count>2) args[2]->value.lval = errno;
if(arg_count>3) {
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net