#if HAVE_SOCKETS
#include "php_network.h"
+#include "ext/standard/file.h"
#include "ext/standard/info.h"
#include "php_ini.h"
la.sin_port = htons((unsigned short) port);
sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
+ sock->blocking = 1;
if (IS_INVALID_SOCKET(sock)) {
PHP_SOCKET_ERROR(sock, "unable to create listening socket", errno);
*new_sock = out_sock;
salen = sizeof(*la);
+ out_sock->blocking = 1;
out_sock->bsd_socket = accept(in_sock->bsd_socket, la, &salen);
/* }}} */
/* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */
-static int php_read(int bsd_socket, void *buf, size_t maxlen, int flags)
+static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags)
{
int m = 0;
size_t n = 0;
int nonblock = 0;
char *t = (char *) buf;
- m = fcntl(bsd_socket, F_GETFL);
+#ifndef PHP_WIN32
+ m = fcntl(sock->bsd_socket, F_GETFL);
if (m < 0) {
return m;
}
-
nonblock = (m & O_NONBLOCK);
m = 0;
-
+#else
+ nonblock = !sock->blocking;
+#endif
set_errno(0);
*t = '\0';
}
if (n < maxlen) {
- m = recv(bsd_socket, (void *) t, 1, flags);
+ m = recv(sock->bsd_socket, (void *) t, 1, flags);
}
if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
}
php_sock->error = 0;
+ php_sock->blocking = 1;
ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
}
}
new_sock->error = 0;
+ new_sock->blocking = 1;
ZEND_REGISTER_RESOURCE(return_value, new_sock, le_socket);
}
{
zval *arg1;
php_socket *php_sock;
- int flags;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
return;
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
- flags = fcntl(php_sock->bsd_socket, F_GETFL);
-
- /* Safely append non blocking to other flags unless the get fails.
- * Note: This does not abort on failure becuse getfl will always fail
- * under the current win32 code. */
- if (flags > -1) flags |= O_NONBLOCK;
- else flags = O_NONBLOCK;
-
- if (fcntl(php_sock->bsd_socket, F_SETFL, flags) > -1) {
+ if (php_set_sock_blocking(php_sock->bsd_socket, 0 TSRMLS_CC) == SUCCESS) {
+ php_sock->blocking = 0;
RETURN_TRUE;
}
-
RETURN_FALSE;
}
/* }}} */
{
zval *arg1;
php_socket *php_sock;
- int flags;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
return;
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
- flags = fcntl(php_sock->bsd_socket, F_GETFL);
-
- /* Safely remove blocking from flags unless the get fails.
- * Note: This does not abort on failure becuse getfl will always fail
- * under the current win32 code. */
- if (flags > -1) flags &= ~O_NONBLOCK;
- else flags = 0;
-
- if (fcntl(php_sock->bsd_socket, F_SETFL, flags) > -1) {
+ if (php_set_sock_blocking(php_sock->bsd_socket, 1 TSRMLS_CC) == SUCCESS) {
+ php_sock->blocking = 1;
RETURN_TRUE;
}
-
RETURN_FALSE;
}
/* }}} */
PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno);
RETURN_FALSE;
}
-
RETURN_TRUE;
}
/* }}} */
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
if (type == PHP_NORMAL_READ) {
- retval = php_read(php_sock->bsd_socket, tmpbuf, length, 0);
+ retval = php_read(php_sock, tmpbuf, length, 0);
} else {
retval = recv(php_sock->bsd_socket, tmpbuf, length, 0);
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &arg1, &arg2, &arg3) == FAILURE) {
efree(php_sock);
return;
- }
+ }
if (arg1 != AF_UNIX
#if HAVE_IPV6
}
php_sock->error = 0;
+ php_sock->blocking = 1;
ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
}
ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
-
switch (php_sock->type) {
case AF_UNIX:
memset(&s_un, 0, sizeof(s_un));
#endif
array_init(return_value);
+
add_assoc_long(return_value, "sec", tv.tv_sec);
add_assoc_long(return_value, "usec", tv.tv_usec);
break;
PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
RETURN_FALSE;
}
+
RETURN_LONG(other_val);
break;
}
#ifdef PHP_WIN32
int timeout;
#else
- struct timeval tv;
+ struct timeval tv;
#endif
long level, optname;
void *opt_ptr;
HashTable *opt_ht;
zval **l_onoff, **l_linger;
zval **sec, **usec;
-
/* key name constants */
char *l_onoff_key = "l_onoff";
char *l_linger_key = "l_linger";
php_sock[1]->type = domain;
php_sock[0]->error = 0;
php_sock[1]->error = 0;
+ php_sock[0]->blocking = 1;
+ php_sock[1]->blocking = 1;
ZEND_REGISTER_RESOURCE(retval[0], php_sock[0], le_socket);
ZEND_REGISTER_RESOURCE(retval[1], php_sock[1], le_socket);