2000-07-03 04:35:57 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 09:03:12 +00:00
|
|
|
| Copyright (c) The PHP Group |
|
2000-07-03 04:35:57 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 12:51:34 +00:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2000-07-03 04:35:57 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-10 20:04:29 +00:00
|
|
|
| available through the world-wide-web at the following url: |
|
2021-05-06 10:16:35 +00:00
|
|
|
| https://www.php.net/license/3_01.txt |
|
2000-07-03 04:35:57 +00:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2000-08-20 10:31:27 +00:00
|
|
|
| Authors: Chris Vandomelen <chrisv@b0rked.dhs.org> |
|
2001-05-17 17:02:37 +00:00
|
|
|
| Sterling Hughes <sterling@php.net> |
|
|
|
|
| |
|
|
|
|
| WinSock: Daniel Beulshausen <daniel@php4win.de> |
|
2000-07-03 04:35:57 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2000-07-03 06:45:07 +00:00
|
|
|
#ifndef PHP_SOCKETS_H
|
|
|
|
#define PHP_SOCKETS_H
|
2000-07-03 04:35:57 +00:00
|
|
|
|
2013-02-23 23:47:44 +00:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2000-07-03 04:35:57 +00:00
|
|
|
#if HAVE_SOCKETS
|
|
|
|
|
2013-01-31 13:01:31 +00:00
|
|
|
#include <php.h>
|
Fix bug and hopefully build on WinSDK 6.1
There build was failing on rmtools on the sockets extension for two reasons:
1. IPV6_TCLASS and IPV6_RECVTCLASS not being defined. These are probably
recent additions to SDK. Windows 7 doesn't event seem to have complete
support for IPV6_TCLASS, not accepting in WSASendMsg(). The parts that
needed this constant were not guarded by #ifdefs. They are now.
2. The constants EWOULDBLOCK and EINPROGRESS not being defined. These
were only defined in php_network.h, outside of the extension, and not
all source files included this header. Nevertheless, a macro defined in
php_sockets.h needed these constants. When this macro was used in files
that did not include php_network.h, the compilation would fail.
Surprisingly, the build did not fail when using the 7.1 Windows SDK
(more likely, the CRT headers used in VC10), as somehow errno.h was
being included through some other standard header. This would make the
constant EWOULDBLOCK defined; however, it would be defined to the wrong
value. In the winsock context, WSAEWOULDBLOCK should be used instead.
Because we have difficulty using Windows-only constants in the code, we
(re)define EWOULDBLOCK to WSAEWOULDBLOCK. This has the obvious
disavantage we may miss problems like this again in the future.
2013-02-03 00:22:44 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
# include "windows_common.h"
|
2020-07-27 19:23:27 +00:00
|
|
|
#else
|
|
|
|
# define IS_INVALID_SOCKET(a) (a->bsd_socket < 0)
|
Fix bug and hopefully build on WinSDK 6.1
There build was failing on rmtools on the sockets extension for two reasons:
1. IPV6_TCLASS and IPV6_RECVTCLASS not being defined. These are probably
recent additions to SDK. Windows 7 doesn't event seem to have complete
support for IPV6_TCLASS, not accepting in WSASendMsg(). The parts that
needed this constant were not guarded by #ifdefs. They are now.
2. The constants EWOULDBLOCK and EINPROGRESS not being defined. These
were only defined in php_network.h, outside of the extension, and not
all source files included this header. Nevertheless, a macro defined in
php_sockets.h needed these constants. When this macro was used in files
that did not include php_network.h, the compilation would fail.
Surprisingly, the build did not fail when using the 7.1 Windows SDK
(more likely, the CRT headers used in VC10), as somehow errno.h was
being included through some other standard header. This would make the
constant EWOULDBLOCK defined; however, it would be defined to the wrong
value. In the winsock context, WSAEWOULDBLOCK should be used instead.
Because we have difficulty using Windows-only constants in the code, we
(re)define EWOULDBLOCK to WSAEWOULDBLOCK. This has the obvious
disavantage we may miss problems like this again in the future.
2013-02-03 00:22:44 +00:00
|
|
|
#endif
|
2013-01-31 13:01:31 +00:00
|
|
|
|
2015-03-23 20:30:22 +00:00
|
|
|
#define PHP_SOCKETS_VERSION PHP_VERSION
|
|
|
|
|
2000-07-03 04:35:57 +00:00
|
|
|
extern zend_module_entry sockets_module_entry;
|
|
|
|
#define phpext_sockets_ptr &sockets_module_entry
|
|
|
|
|
|
|
|
#ifdef PHP_WIN32
|
2013-01-31 13:01:31 +00:00
|
|
|
#include <Winsock2.h>
|
2000-07-03 04:35:57 +00:00
|
|
|
#else
|
2001-11-06 14:35:26 +00:00
|
|
|
#if HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
2000-07-03 04:35:57 +00:00
|
|
|
#endif
|
|
|
|
|
2001-07-25 22:03:55 +00:00
|
|
|
#ifndef PHP_WIN32
|
2003-06-17 04:44:30 +00:00
|
|
|
typedef int PHP_SOCKET;
|
|
|
|
#else
|
|
|
|
typedef SOCKET PHP_SOCKET;
|
2001-05-17 17:02:37 +00:00
|
|
|
#endif
|
2001-07-25 22:03:55 +00:00
|
|
|
|
2020-09-08 20:51:46 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
# ifdef PHP_SOCKETS_EXPORTS
|
|
|
|
# define PHP_SOCKETS_API __declspec(dllexport)
|
|
|
|
# else
|
|
|
|
# define PHP_SOCKETS_API __declspec(dllimport)
|
|
|
|
# endif
|
|
|
|
#elif defined(__GNUC__) && __GNUC__ >= 4
|
|
|
|
# define PHP_SOCKETS_API __attribute__ ((visibility("default")))
|
|
|
|
#else
|
|
|
|
# define PHP_SOCKETS_API
|
|
|
|
#endif
|
|
|
|
|
2020-07-27 19:23:27 +00:00
|
|
|
/* Socket class */
|
|
|
|
|
2001-07-25 22:03:55 +00:00
|
|
|
typedef struct {
|
2011-03-22 00:44:23 +00:00
|
|
|
PHP_SOCKET bsd_socket;
|
|
|
|
int type;
|
|
|
|
int error;
|
|
|
|
int blocking;
|
2014-05-17 02:23:46 +00:00
|
|
|
zval zstream;
|
2020-07-27 19:23:27 +00:00
|
|
|
zend_object std;
|
2001-05-17 17:02:37 +00:00
|
|
|
} php_socket;
|
|
|
|
|
2020-09-08 20:51:46 +00:00
|
|
|
extern PHP_SOCKETS_API zend_class_entry *socket_ce;
|
2020-07-27 19:23:27 +00:00
|
|
|
|
|
|
|
static inline php_socket *socket_from_obj(zend_object *obj) {
|
|
|
|
return (php_socket *)((char *)(obj) - XtOffsetOf(php_socket, std));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define Z_SOCKET_P(zv) socket_from_obj(Z_OBJ_P(zv))
|
|
|
|
|
|
|
|
#define ENSURE_SOCKET_VALID(php_sock) do { \
|
|
|
|
if (IS_INVALID_SOCKET(php_sock)) { \
|
|
|
|
zend_argument_error(NULL, 1, "has already been closed"); \
|
|
|
|
RETURN_THROWS(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2009-01-23 15:49:49 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
struct sockaddr_un {
|
|
|
|
short sun_family;
|
|
|
|
char sun_path[108];
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-02-01 13:39:56 +00:00
|
|
|
#define PHP_SOCKET_ERROR(socket, msg, errn) \
|
|
|
|
do { \
|
|
|
|
int _err = (errn); /* save value to avoid repeated calls to WSAGetLastError() on Windows */ \
|
|
|
|
(socket)->error = _err; \
|
|
|
|
SOCKETS_G(last_error) = _err; \
|
2013-02-02 14:43:05 +00:00
|
|
|
if (_err != EAGAIN && _err != EWOULDBLOCK && _err != EINPROGRESS) { \
|
2014-12-13 22:06:14 +00:00
|
|
|
php_error_docref(NULL, E_WARNING, "%s [%d]: %s", msg, _err, sockets_strerror(_err)); \
|
2013-02-02 14:43:05 +00:00
|
|
|
} \
|
2013-02-01 13:39:56 +00:00
|
|
|
} while (0)
|
2001-11-01 22:40:43 +00:00
|
|
|
|
2002-04-29 14:52:29 +00:00
|
|
|
ZEND_BEGIN_MODULE_GLOBALS(sockets)
|
2002-09-30 02:09:42 +00:00
|
|
|
int last_error;
|
|
|
|
char *strerror_buf;
|
2018-04-23 15:03:09 +00:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
uint32_t wsa_child_count;
|
|
|
|
HashTable wsa_info;
|
|
|
|
#endif
|
2002-04-29 14:52:29 +00:00
|
|
|
ZEND_END_MODULE_GLOBALS(sockets)
|
|
|
|
|
2020-09-08 20:51:46 +00:00
|
|
|
PHP_SOCKETS_API ZEND_EXTERN_MODULE_GLOBALS(sockets)
|
2015-07-29 10:26:23 +00:00
|
|
|
#define SOCKETS_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(sockets, v)
|
2012-11-01 19:38:42 +00:00
|
|
|
|
2012-11-06 11:48:47 +00:00
|
|
|
enum sockopt_return {
|
|
|
|
SOCKOPT_ERROR,
|
|
|
|
SOCKOPT_CONTINUE,
|
|
|
|
SOCKOPT_SUCCESS
|
|
|
|
};
|
|
|
|
|
2020-09-08 20:51:46 +00:00
|
|
|
PHP_SOCKETS_API char *sockets_strerror(int error);
|
|
|
|
PHP_SOCKETS_API int socket_import_file_descriptor(PHP_SOCKET socket, php_socket *retsock);
|
2012-11-01 19:38:42 +00:00
|
|
|
|
2000-07-03 04:35:57 +00:00
|
|
|
#else
|
|
|
|
#define phpext_sockets_ptr NULL
|
|
|
|
#endif
|
|
|
|
|
2014-01-01 11:55:14 +00:00
|
|
|
#if defined(_AIX) && !defined(HAVE_SA_SS_FAMILY)
|
|
|
|
# define ss_family __ss_family
|
|
|
|
#endif
|
|
|
|
|
2001-05-17 17:02:37 +00:00
|
|
|
#endif
|