1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2020 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Ruslan Osmanov <osmanov@php.net>                             |
16    +----------------------------------------------------------------------+
17 */
18 #ifndef PHP_EVENT_UTIL_H
19 #define PHP_EVENT_UTIL_H
20 
21 #include "common.h"
22 
23 #ifdef PHP_WIN32
24 # ifdef EINPROGRESS
25 #  undef EINPROGRESS
26 # endif
27 # define EINPROGRESS WSAEWOULDBLOCK
28 #endif
29 
30 php_socket_t php_event_zval_to_fd(zval **ppfd TSRMLS_DC);
31 int _php_event_getsockname(evutil_socket_t fd, zval **ppzaddress, zval **ppzport TSRMLS_DC);
32 
33 #define php_event_is_pending(e) \
34 	event_pending((e), EV_READ | EV_WRITE | EV_SIGNAL | EV_TIMEOUT, NULL)
35 
36 #ifdef PHP_EVENT_NS
37 # define PHP_EVENT_INIT_CLASS(tmp_ce, name, ce_functions) \
38 	INIT_NS_CLASS_ENTRY(tmp_ce, PHP_EVENT_NS, name, ce_functions)
39 #else
40 # define PHP_EVENT_INIT_CLASS(tmp_ce, name, ce_functions) \
41 	INIT_CLASS_ENTRY(tmp_ce, name, ce_functions)
42 #endif
43 
44 #define PHP_EVENT_REGISTER_CLASS(name, create_func, ce, ce_functions) \
45 {                                                                     \
46 	zend_class_entry tmp_ce;                                          \
47 	PHP_EVENT_INIT_CLASS(tmp_ce, name, ce_functions);                 \
48 	ce = zend_register_internal_class(&tmp_ce TSRMLS_CC);             \
49 	ce->create_object = create_func;                                  \
50 }
51 
52 #define PHP_EVENT_INIT_CLASS_OBJECT(pz, pce) \
53 	do {                                     \
54 		Z_TYPE_P((pz)) = IS_OBJECT;          \
55 		object_init_ex((pz), (pce));         \
56 		Z_SET_REFCOUNT_P((pz), 1);           \
57 		Z_SET_ISREF_P((pz));                 \
58 	} while (0)
59 
60 #define PHP_EVENT_FETCH_EVENT(e, ze) \
61 	e = (php_event_t *) zend_object_store_get_object(ze TSRMLS_CC)
62 
63 #define PHP_EVENT_FETCH_BASE(base, zbase) \
64 	base = (php_event_base_t *) zend_object_store_get_object(zbase TSRMLS_CC)
65 
66 #define PHP_EVENT_FETCH_CONFIG(cfg, zcfg) \
67 	cfg = (php_event_config_t *) zend_object_store_get_object(zcfg TSRMLS_CC)
68 
69 #define PHP_EVENT_FETCH_BEVENT(b, zb) \
70 	b = (php_event_bevent_t *) zend_object_store_get_object(zb TSRMLS_CC)
71 
72 #define PHP_EVENT_FETCH_BUFFER(b, zb) \
73 	b = (php_event_buffer_t *) zend_object_store_get_object(zb TSRMLS_CC)
74 
75 #define PHP_EVENT_FETCH_DNS_BASE(b, zb) \
76 	b = (php_event_dns_base_t *) zend_object_store_get_object(zb TSRMLS_CC)
77 
78 #define PHP_EVENT_FETCH_LISTENER(b, zb) \
79 	b = (php_event_listener_t *) zend_object_store_get_object(zb TSRMLS_CC)
80 
81 #define PHP_EVENT_FETCH_HTTP_CONN(b, zb) \
82 	b = (php_event_http_conn_t *) zend_object_store_get_object(zb TSRMLS_CC)
83 
84 #define PHP_EVENT_FETCH_HTTP(b, zb) \
85 	b = (php_event_http_t *) zend_object_store_get_object(zb TSRMLS_CC)
86 
87 #define PHP_EVENT_FETCH_HTTP_REQ(b, zb) \
88 	b = (php_event_http_req_t *) zend_object_store_get_object(zb TSRMLS_CC)
89 
90 #define PHP_EVENT_FETCH_BUFFER_POS(p, zp) \
91 	p = (php_event_buffer_pos_t *) zend_object_store_get_object(zp TSRMLS_CC)
92 
93 #define PHP_EVENT_FETCH_SSL_CONTEXT(p, zp) \
94 	p = (php_event_ssl_context_t *) zend_object_store_get_object(zp TSRMLS_CC)
95 
96 #define PHP_EVENT_TIMEVAL_SET(tv, t)                 \
97 	do {                                             \
98 		tv.tv_sec  = (long) t;                       \
99 		tv.tv_usec = (long) ((t - tv.tv_sec) * 1e6); \
100 	} while (0)
101 
102 #define PHP_EVENT_TIMEVAL_TO_DOUBLE(tv) (tv.tv_sec + tv.tv_usec * 1e-6)
103 
104 #define PHP_EVENT_SOCKETS_REQUIRED_NORET                                       \
105 	php_error_docref(NULL TSRMLS_CC, E_ERROR, "`sockets' extension required. " \
106 			"If you have `sockets' installed, rebuild `event' extension")
107 
108 #define PHP_EVENT_SOCKETS_REQUIRED_RET    \
109 	do {                                  \
110 		PHP_EVENT_SOCKETS_REQUIRED_NORET; \
111 		RETURN_FALSE;                     \
112 	} while (0)
113 
114 #define PHP_EVENT_REQUIRE_BASE_BY_REF(zbase)                  \
115 	do {                                                      \
116 		if (!Z_ISREF_P((zbase)) || Z_REFCOUNT_P(zbase) < 2) { \
117 			php_error_docref(NULL TSRMLS_CC, E_ERROR,         \
118 					"EventBase must be passed by reference"); \
119 		}                                                     \
120 	} while (0)
121 
122 #if defined(PHP_WIN32)
123 #if defined(ZTS)
124 #  define PHP_EVENT_TSRMLS_FETCH_FROM_CTX(ctx) tsrm_ls = (void ***)ctx
125 #  define PHP_EVENT_TSRM_DECL void ***tsrm_ls;
126 # else
127 #  define PHP_EVENT_TSRMLS_FETCH_FROM_CTX(ctx)
128 #  define PHP_EVENT_TSRM_DECL
129 # endif
130 #else
131 # define PHP_EVENT_TSRMLS_FETCH_FROM_CTX(ctx) TSRMLS_FETCH_FROM_CTX(ctx)
132 # define PHP_EVENT_TSRM_DECL
133 #endif
134 
135 #endif /* PHP_EVENT_UTIL_H */
136 
137 /*
138  * Local variables:
139  * tab-width: 4
140  * c-basic-offset: 4
141  * vim600: fdm=marker
142  * vim: noet sts=4 sw=4 ts=4
143  */
144