php_sockets.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Chris Vandomelen <chrisv@b0rked.dhs.org> |
  14. | Sterling Hughes <sterling@php.net> |
  15. | |
  16. | WinSock: Daniel Beulshausen <daniel@php4win.de> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #ifndef PHP_SOCKETS_H
  20. #define PHP_SOCKETS_H
  21. #if HAVE_CONFIG_H
  22. # include "config.h"
  23. #endif
  24. #if HAVE_SOCKETS
  25. #include <php.h>
  26. #ifdef PHP_WIN32
  27. # include "windows_common.h"
  28. #else
  29. # define IS_INVALID_SOCKET(a) (a->bsd_socket < 0)
  30. #endif
  31. #define PHP_SOCKETS_VERSION PHP_VERSION
  32. extern zend_module_entry sockets_module_entry;
  33. #define phpext_sockets_ptr &sockets_module_entry
  34. #ifdef PHP_WIN32
  35. #include <Winsock2.h>
  36. #else
  37. #if HAVE_SYS_SOCKET_H
  38. #include <sys/socket.h>
  39. #endif
  40. #endif
  41. #ifndef PHP_WIN32
  42. typedef int PHP_SOCKET;
  43. #else
  44. typedef SOCKET PHP_SOCKET;
  45. #endif
  46. #ifdef PHP_WIN32
  47. # ifdef PHP_SOCKETS_EXPORTS
  48. # define PHP_SOCKETS_API __declspec(dllexport)
  49. # else
  50. # define PHP_SOCKETS_API __declspec(dllimport)
  51. # endif
  52. #elif defined(__GNUC__) && __GNUC__ >= 4
  53. # define PHP_SOCKETS_API __attribute__ ((visibility("default")))
  54. #else
  55. # define PHP_SOCKETS_API
  56. #endif
  57. /* Socket class */
  58. typedef struct {
  59. PHP_SOCKET bsd_socket;
  60. int type;
  61. int error;
  62. int blocking;
  63. zval zstream;
  64. zend_object std;
  65. } php_socket;
  66. extern PHP_SOCKETS_API zend_class_entry *socket_ce;
  67. static inline php_socket *socket_from_obj(zend_object *obj) {
  68. return (php_socket *)((char *)(obj) - XtOffsetOf(php_socket, std));
  69. }
  70. #define Z_SOCKET_P(zv) socket_from_obj(Z_OBJ_P(zv))
  71. #define ENSURE_SOCKET_VALID(php_sock) do { \
  72. if (IS_INVALID_SOCKET(php_sock)) { \
  73. zend_argument_error(NULL, 1, "has already been closed"); \
  74. RETURN_THROWS(); \
  75. } \
  76. } while (0)
  77. #ifdef PHP_WIN32
  78. struct sockaddr_un {
  79. short sun_family;
  80. char sun_path[108];
  81. };
  82. #endif
  83. #define PHP_SOCKET_ERROR(socket, msg, errn) \
  84. do { \
  85. int _err = (errn); /* save value to avoid repeated calls to WSAGetLastError() on Windows */ \
  86. (socket)->error = _err; \
  87. SOCKETS_G(last_error) = _err; \
  88. if (_err != EAGAIN && _err != EWOULDBLOCK && _err != EINPROGRESS) { \
  89. php_error_docref(NULL, E_WARNING, "%s [%d]: %s", msg, _err, sockets_strerror(_err)); \
  90. } \
  91. } while (0)
  92. ZEND_BEGIN_MODULE_GLOBALS(sockets)
  93. int last_error;
  94. char *strerror_buf;
  95. #ifdef PHP_WIN32
  96. uint32_t wsa_child_count;
  97. HashTable wsa_info;
  98. #endif
  99. ZEND_END_MODULE_GLOBALS(sockets)
  100. PHP_SOCKETS_API ZEND_EXTERN_MODULE_GLOBALS(sockets)
  101. #define SOCKETS_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(sockets, v)
  102. enum sockopt_return {
  103. SOCKOPT_ERROR,
  104. SOCKOPT_CONTINUE,
  105. SOCKOPT_SUCCESS
  106. };
  107. PHP_SOCKETS_API char *sockets_strerror(int error);
  108. PHP_SOCKETS_API int socket_import_file_descriptor(PHP_SOCKET socket, php_socket *retsock);
  109. #else
  110. #define phpext_sockets_ptr NULL
  111. #endif
  112. #if defined(_AIX) && !defined(HAVE_SA_SS_FAMILY)
  113. # define ss_family __ss_family
  114. #endif
  115. #endif