php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
php_network.h
Go to the documentation of this file.
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 | Author: Stig Venaas <venaas@uninett.no> |
14 +----------------------------------------------------------------------+
15 */
16
17#ifndef _PHP_NETWORK_H
18#define _PHP_NETWORK_H
19
20#include <php.h>
21
22#ifndef PHP_WIN32
23# undef closesocket
24# define closesocket close
25# include <netinet/tcp.h>
26#endif
27
28#ifndef HAVE_SHUTDOWN
29#undef shutdown
30#define shutdown(s,n) /* nothing */
31#endif
32
33#ifdef PHP_WIN32
34# ifdef EWOULDBLOCK
35# undef EWOULDBLOCK
36# endif
37# ifdef EINPROGRESS
38# undef EINPROGRESS
39# endif
40# define EWOULDBLOCK WSAEWOULDBLOCK
41# define EINPROGRESS WSAEWOULDBLOCK
42# define fsync _commit
43# define ftruncate(a, b) chsize(a, b)
44#endif /* defined(PHP_WIN32) */
45
46#ifndef EWOULDBLOCK
47# define EWOULDBLOCK EAGAIN
48#endif
49
50/* This is a workaround for GCC bug 69602: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69602 */
51#if EAGAIN != EWOULDBLOCK
52# define PHP_IS_TRANSIENT_ERROR(err) (err == EAGAIN || err == EWOULDBLOCK)
53#else
54# define PHP_IS_TRANSIENT_ERROR(err) (err == EAGAIN)
55#endif
56
57#ifdef PHP_WIN32
58#define php_socket_errno() WSAGetLastError()
59#else
60#define php_socket_errno() errno
61#endif
62
63/* like strerror, but caller must efree the returned string,
64 * unless buf is not NULL.
65 * Also works sensibly for win32 */
67PHPAPI char *php_socket_strerror(long err, char *buf, size_t bufsize);
70
71#ifdef HAVE_NETINET_IN_H
72# include <netinet/in.h>
73#endif
74
75#ifdef HAVE_SYS_SOCKET_H
76#include <sys/socket.h>
77#endif
78
79#ifdef HAVE_GETHOSTBYNAME_R
80#include <netdb.h>
81#endif
82
83/* These are here, rather than with the win32 counterparts above,
84 * since <sys/socket.h> defines them. */
85#ifndef SHUT_RD
86# define SHUT_RD 0
87# define SHUT_WR 1
88# define SHUT_RDWR 2
89#endif
90
91#ifdef HAVE_SYS_TIME_H
92#include <sys/time.h>
93#endif
94
95#include <stddef.h>
96
97#ifdef PHP_WIN32
98typedef SOCKET php_socket_t;
99#else
100typedef int php_socket_t;
101#endif
102
103#ifdef PHP_WIN32
104# define SOCK_ERR INVALID_SOCKET
105# define SOCK_CONN_ERR SOCKET_ERROR
106# define SOCK_RECV_ERR SOCKET_ERROR
107#else
108# define SOCK_ERR -1
109# define SOCK_CONN_ERR -1
110# define SOCK_RECV_ERR -1
111#endif
112
113#define STREAM_SOCKOP_NONE (1 << 0)
114#define STREAM_SOCKOP_SO_REUSEPORT (1 << 1)
115#define STREAM_SOCKOP_SO_BROADCAST (1 << 2)
116#define STREAM_SOCKOP_IPV6_V6ONLY (1 << 3)
117#define STREAM_SOCKOP_IPV6_V6ONLY_ENABLED (1 << 4)
118#define STREAM_SOCKOP_TCP_NODELAY (1 << 5)
119
120
121/* uncomment this to debug poll(2) emulation on systems that have poll(2) */
122/* #define PHP_USE_POLL_2_EMULATION 1 */
123
124#if defined(HAVE_POLL)
125# if defined(HAVE_POLL_H)
126# include <poll.h>
127# elif defined(HAVE_SYS_POLL_H)
128# include <sys/poll.h>
129# endif
130typedef struct pollfd php_pollfd;
131#else
137
138PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout);
139
140#ifndef POLLIN
141# define POLLIN 0x0001 /* There is data to read */
142# define POLLPRI 0x0002 /* There is urgent data to read */
143# define POLLOUT 0x0004 /* Writing now will not block */
144# define POLLERR 0x0008 /* Error condition */
145# define POLLHUP 0x0010 /* Hung up */
146# define POLLNVAL 0x0020 /* Invalid request: fd not open */
147#endif
148
149# ifndef PHP_USE_POLL_2_EMULATION
150# define PHP_USE_POLL_2_EMULATION 1
151# endif
152#endif
153
154#define PHP_POLLREADABLE (POLLIN|POLLERR|POLLHUP)
155
156#ifndef PHP_USE_POLL_2_EMULATION
157# define php_poll2(ufds, nfds, timeout) poll(ufds, nfds, timeout)
158#endif
159
160/* timeval-to-timeout (for poll(2)) */
161static inline int php_tvtoto(struct timeval *timeouttv)
162{
163 if (timeouttv && timeouttv->tv_sec >= 0 && timeouttv->tv_sec <= ((INT_MAX - 1000) / 1000)) {
164 return (timeouttv->tv_sec * 1000) + (timeouttv->tv_usec / 1000);
165 }
166 return -1;
167}
168
169/* hybrid select(2)/poll(2) for a single descriptor.
170 * timeouttv follows same rules as select(2), but is reduced to millisecond accuracy.
171 * Returns 0 on timeout, -1 on error, or the event mask (ala poll(2)).
172 */
173static inline int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv)
174{
176 int n;
177
178 p.fd = fd;
179 p.events = events;
180 p.revents = 0;
181
182 n = php_poll2(&p, 1, php_tvtoto(timeouttv));
183
184 if (n > 0) {
185 return p.revents;
186 }
187
188 return n;
189}
190
191static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout)
192{
194 int n;
195
196 p.fd = fd;
197 p.events = events;
198 p.revents = 0;
199
200 n = php_poll2(&p, 1, timeout);
201
202 if (n > 0) {
203 return p.revents;
204 }
205
206 return n;
207}
208
209/* emit warning and suggestion for unsafe select(2) usage */
210PHPAPI void _php_emit_fd_setsize_warning(int max_fd);
211
212static inline bool _php_check_fd_setsize(php_socket_t *max_fd, int setsize)
213{
214#ifdef PHP_WIN32
215 (void)(max_fd); // Unused
216 if (setsize + 1 >= FD_SETSIZE) {
218 return false;
219 }
220#else
221 (void)(setsize); // Unused
222 if (*max_fd >= FD_SETSIZE) {
224 *max_fd = FD_SETSIZE - 1;
225 return false;
226 }
227#endif
228 return true;
229}
230
231#ifdef PHP_WIN32
232/* it is safe to FD_SET too many fd's under win32; the macro will simply ignore
233 * descriptors that go beyond the default FD_SETSIZE */
234# define PHP_SAFE_FD_SET(fd, set) FD_SET(fd, set)
235# define PHP_SAFE_FD_CLR(fd, set) FD_CLR(fd, set)
236# define PHP_SAFE_FD_ISSET(fd, set) FD_ISSET(fd, set)
237# define PHP_SAFE_MAX_FD(m, n) _php_check_fd_setsize(&m, n)
238#else
239# define PHP_SAFE_FD_SET(fd, set) do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
240# define PHP_SAFE_FD_CLR(fd, set) do { if (fd < FD_SETSIZE) FD_CLR(fd, set); } while(0)
241# define PHP_SAFE_FD_ISSET(fd, set) ((fd < FD_SETSIZE) && FD_ISSET(fd, set))
242# define PHP_SAFE_MAX_FD(m, n) _php_check_fd_setsize(&m, n)
243#endif
244
245
246#define PHP_SOCK_CHUNK_SIZE 8192
247
248#ifdef HAVE_STRUCT_SOCKADDR_STORAGE
249typedef struct sockaddr_storage php_sockaddr_storage;
250#else
251typedef struct {
252#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
253 unsigned char ss_len;
254 unsigned char ss_family;
255#else
256 unsigned short ss_family;
257#endif
258 char info[126];
260#endif
261
263PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string);
264PHPAPI void php_network_freeaddresses(struct sockaddr **sal);
265
266PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
267 int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
268 int *error_code, const char *bindto, unsigned short bindport, long sockopts
269 );
270
272 const struct sockaddr *addr,
273 socklen_t addrlen,
274 int asynchronous,
275 struct timeval *timeout,
276 zend_string **error_string,
277 int *error_code);
278
279#define php_connect_nonb(sock, addr, addrlen, timeout) \
280 php_network_connect_socket((sock), (addr), (addrlen), 0, (timeout), NULL, NULL)
281
282PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port,
283 int socktype, long sockopts, zend_string **error_string, int *error_code
284 );
285
287 zend_string **textaddr,
288 struct sockaddr **addr,
289 socklen_t *addrlen,
290 struct timeval *timeout,
291 zend_string **error_string,
292 int *error_code,
293 int tcp_nodelay
294 );
295
297 zend_string **textaddr,
298 struct sockaddr **addr,
299 socklen_t *addrlen
300 );
301
303 zend_string **textaddr,
304 struct sockaddr **addr,
305 socklen_t *addrlen
306 );
307
308PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port);
311
322#define PHP_STREAM_IS_SOCKET (&php_stream_socket_ops)
323
326/* open a connection to a host using php_hostconnect and return a stream */
327PHPAPI php_stream *_php_stream_sock_open_host(const char *host, unsigned short port,
328 int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC);
330 /* input address */
331 struct sockaddr *sa, socklen_t sl,
332 /* output readable address */
333 zend_string **textaddr,
334 /* output address */
335 struct sockaddr **addr,
336 socklen_t *addrlen
337 );
338
340 size_t addrlen, struct sockaddr *sa, socklen_t *sl);
341
342PHPAPI struct hostent* php_network_gethostbyname(const char *name);
343
346
347#define php_stream_sock_open_from_socket(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_CC)
348#define php_stream_sock_open_host(host, port, socktype, timeout, persistent) _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_CC)
349
350/* {{{ memory debug */
351#define php_stream_sock_open_from_socket_rel(socket, persistent) _php_stream_sock_open_from_socket((socket), (persistent) STREAMS_REL_CC)
352#define php_stream_sock_open_host_rel(host, port, socktype, timeout, persistent) _php_stream_sock_open_host((host), (port), (socktype), (timeout), (persistent) STREAMS_REL_CC)
353#define php_stream_sock_open_unix_rel(path, pathlen, persistent, timeval) _php_stream_sock_open_unix((path), (pathlen), (persistent), (timeval) STREAMS_REL_CC)
354
355/* }}} */
356
357#ifndef MAXFQDNLEN
358#define MAXFQDNLEN 255
359#endif
360
361#endif /* _PHP_NETWORK_H */
unsigned int socklen_t
Definition fastcgi.c:87
zend_long n
Definition ffi.c:4979
char * err
Definition ffi.c:3029
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
PHPAPI zend_result php_set_sock_blocking(php_socket_t socketd, bool block)
Definition network.c:1145
PHPAPI int php_network_connect_socket(php_socket_t sockfd, const struct sockaddr *addr, socklen_t addrlen, int asynchronous, struct timeval *timeout, zend_string **error_string, int *error_code)
Definition network.c:334
PHPAPI php_stream * _php_stream_sock_open_host(const char *host, unsigned short port, int socktype, struct timeval *timeout, const char *persistent_id STREAMS_DC)
Definition network.c:1128
PHPAPI char * php_socket_strerror(long err, char *buf, size_t bufsize)
Definition network.c:1045
PHPAPI void php_network_freeaddresses(struct sockaddr **sal)
Definition network.c:133
PHPAPI void php_network_populate_name_from_sockaddr(struct sockaddr *sa, socklen_t sl, zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen)
Definition network.c:643
PHPAPI struct hostent * php_network_gethostbyname(const char *name)
Definition network.c:1350
PHPAPI zend_result php_network_parse_network_address_with_port(const char *addr, size_t addrlen, struct sockaddr *sa, socklen_t *sl)
Definition network.c:552
PHPAPI php_stream * _php_stream_sock_open_from_socket(php_socket_t socket, const char *persistent_id STREAMS_DC)
Definition network.c:1104
PHPAPI zend_string * php_socket_error_str(long err)
Definition network.c:1079
php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port, int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string, int *error_code, const char *bindto, unsigned short bindport, long sockopts)
Definition network.c:816
PHPAPI int php_network_getaddresses(const char *host, int socktype, struct sockaddr ***sal, zend_string **error_string)
Definition network.c:148
#define INT_MAX
Definition php.h:237
#define PHPAPI
Definition php.h:71
php_json_error_code error_code
Definition php_json.h:92
struct _php_netstream_data_t php_netstream_data_t
PHPAPI void _php_emit_fd_setsize_warning(int max_fd)
Definition network.c:1178
const php_stream_ops php_stream_generic_socket_ops
Definition xp_socket.c:532
PHPAPI socklen_t php_sockaddr_size(php_sockaddr_storage *addr)
Definition network.c:1020
PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout)
Definition network.c:1206
PHPAPI php_socket_t php_network_bind_socket_to_local_addr(const char *host, unsigned port, int socktype, long sockopts, zend_string **error_string, int *error_code)
Definition network.c:454
struct _php_pollfd php_pollfd
PHPAPI int php_network_get_sock_name(php_socket_t sock, zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen)
Definition network.c:726
PHPAPI const php_stream_ops php_stream_socket_ops
Definition xp_socket.c:543
PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock, zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen, struct timeval *timeout, zend_string **error_string, int *error_code, int tcp_nodelay)
Definition network.c:757
int php_socket_t
PHPAPI void php_any_addr(int family, php_sockaddr_storage *addr, unsigned short port)
Definition network.c:993
PHPAPI int php_network_get_peer_name(php_socket_t sock, zend_string **textaddr, struct sockaddr **addr, socklen_t *addrlen)
Definition network.c:706
struct _php_stream php_stream
Definition php_streams.h:96
#define STREAMS_DC
Definition php_streams.h:53
struct _php_stream_ops php_stream_ops
int fd
Definition phpdbg.h:282
p
Definition session.c:1105
struct timeval timeout
php_socket_t fd
unsigned short ss_family
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
struct _zend_string zend_string
#define END_EXTERN_C()
#define BEGIN_EXTERN_C()
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
zend_string * name