xref: /PHP-8.0/sapi/phpdbg/phpdbg_io.c (revision 5d6e923d)
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    | http://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: Anatol Belski <ab@php.net>                                  |
14    +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "phpdbg_io.h"
22 
23 #ifdef PHP_WIN32
24 #undef UNICODE
25 #include "win32/inet.h"
26 #include <winsock2.h>
27 #include <windows.h>
28 #include <Ws2tcpip.h>
29 #include "win32/sockets.h"
30 
31 #else
32 
33 #if HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #if HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
40 #endif
41 #include <netdb.h>
42 #include <fcntl.h>
43 #include <poll.h>
44 #endif
45 
ZEND_EXTERN_MODULE_GLOBALS(phpdbg)46 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
47 
48 /* is easy to generalize ... but not needed for now */
49 PHPDBG_API int phpdbg_consume_stdin_line(char *buf) {
50 	int bytes = PHPDBG_G(input_buflen), len = 0;
51 
52 	if (PHPDBG_G(input_buflen)) {
53 		memcpy(buf, PHPDBG_G(input_buffer), bytes);
54 	}
55 
56 	PHPDBG_G(last_was_newline) = 1;
57 
58 	do {
59 		int i;
60 		if (bytes <= 0) {
61 			continue;
62 		}
63 
64 		for (i = len; i < len + bytes; i++) {
65 			if (buf[i] == '\x03') {
66 				if (i != len + bytes - 1) {
67 					memmove(buf + i, buf + i + 1, len + bytes - i - 1);
68 				}
69 				len--;
70 				i--;
71 				continue;
72 			}
73 			if (buf[i] == '\n') {
74 				PHPDBG_G(input_buflen) = len + bytes - 1 - i;
75 				if (PHPDBG_G(input_buflen)) {
76 					memcpy(PHPDBG_G(input_buffer), buf + i + 1, PHPDBG_G(input_buflen));
77 				}
78 				if (i != PHPDBG_MAX_CMD - 1) {
79 					buf[i + 1] = 0;
80 				}
81 				return i;
82 			}
83 		}
84 
85 		len += bytes;
86 	} while ((bytes = phpdbg_mixed_read(PHPDBG_G(io)[PHPDBG_STDIN].fd, buf + len, PHPDBG_MAX_CMD - len, -1)) > 0);
87 
88 	if (bytes <= 0) {
89 		PHPDBG_G(flags) |= PHPDBG_IS_QUITTING | PHPDBG_IS_DISCONNECTED;
90 		zend_bailout();
91 	}
92 
93 	return bytes;
94 }
95 
phpdbg_consume_bytes(int sock,char * ptr,int len,int tmo)96 PHPDBG_API int phpdbg_consume_bytes(int sock, char *ptr, int len, int tmo) {
97 	int got_now, i = len, j;
98 	char *p = ptr;
99 #ifndef PHP_WIN32
100 	struct pollfd pfd;
101 
102 	if (tmo < 0) goto recv_once;
103 	pfd.fd = sock;
104 	pfd.events = POLLIN;
105 
106 	j = poll(&pfd, 1, tmo);
107 
108 	if (j == 0) {
109 #else
110 	struct fd_set readfds;
111 	struct timeval ttmo;
112 
113 	if (tmo < 0) goto recv_once;
114 	FD_ZERO(&readfds);
115 	FD_SET(sock, &readfds);
116 
117 	ttmo.tv_sec = 0;
118 	ttmo.tv_usec = tmo*1000;
119 
120 	j = select(0, &readfds, NULL, NULL, &ttmo);
121 
122 	if (j <= 0) {
123 #endif
124 		return -1;
125 	}
126 
127 recv_once:
128 	while(i > 0) {
129 		if (tmo < 0) {
130 			/* There's something to read. Read what's available and proceed
131 			disregarding whether len could be exhausted or not.*/
132 			int can_read = recv(sock, p, i, MSG_PEEK);
133 #ifndef _WIN32
134 			if (can_read == -1 && errno == EINTR) {
135 				continue;
136 			}
137 #endif
138 			i = can_read;
139 		}
140 
141 #ifdef _WIN32
142 		got_now = recv(sock, p, i, 0);
143 #else
144 		do {
145 			got_now = recv(sock, p, i, 0);
146 		} while (got_now == -1 && errno == EINTR);
147 #endif
148 
149 		if (got_now == -1) {
150 			zend_quiet_write(PHPDBG_G(io)[PHPDBG_STDERR].fd, ZEND_STRL("Read operation timed out!\n"));
151 			return -1;
152 		}
153 		i -= got_now;
154 		p += got_now;
155 	}
156 
157 	return p - ptr;
158 }
159 
160 PHPDBG_API int phpdbg_send_bytes(int sock, const char *ptr, int len) {
161 	int sent, i = len;
162 	const char *p = ptr;
163 /* XXX poll/select needed here? */
164 	while(i > 0) {
165 		sent = send(sock, p, i, 0);
166 		if (sent == -1) {
167 			return -1;
168 		}
169 		i -= sent;
170 		p += sent;
171 	}
172 
173 	return len;
174 }
175 
176 
177 PHPDBG_API int phpdbg_mixed_read(int sock, char *ptr, int len, int tmo) {
178 	int ret;
179 
180 	if (PHPDBG_G(flags) & PHPDBG_IS_REMOTE) {
181 		return phpdbg_consume_bytes(sock, ptr, len, tmo);
182 	}
183 
184 	do {
185 		ret = read(sock, ptr, len);
186 	} while (ret == -1 && errno == EINTR);
187 
188 	return ret;
189 }
190 
191 static int phpdbg_output_pager(int sock, const char *ptr, int len) {
192 	int count = 0, bytes = 0;
193 	const char *p = ptr, *endp = ptr + len;
194 
195 	while ((p = memchr(p, '\n', endp - p))) {
196 		count++;
197 		p++;
198 
199 		if (count % PHPDBG_G(lines) == 0) {
200 			bytes += write(sock, ptr + bytes, (p - ptr) - bytes);
201 
202 			if (memchr(p, '\n', endp - p)) {
203 				char buf[PHPDBG_MAX_CMD];
204 				zend_quiet_write(sock, ZEND_STRL("\r---Type <return> to continue or q <return> to quit---"));
205 				phpdbg_consume_stdin_line(buf);
206 				if (*buf == 'q') {
207 					break;
208 				}
209 				zend_quiet_write(sock, "\r", 1);
210 			} else break;
211 		}
212 	}
213 	if (bytes && count % PHPDBG_G(lines) != 0) {
214 		bytes += write(sock, ptr + bytes, len - bytes);
215 	} else if (!bytes) {
216 		bytes += write(sock, ptr, len);
217 	}
218 	return bytes;
219 }
220 
221 PHPDBG_API int phpdbg_mixed_write(int sock, const char *ptr, int len) {
222 	if (PHPDBG_G(flags) & PHPDBG_IS_REMOTE) {
223 		return phpdbg_send_bytes(sock, ptr, len);
224 	}
225 
226 	if ((PHPDBG_G(flags) & PHPDBG_HAS_PAGINATION)
227 	 && !(PHPDBG_G(flags) & PHPDBG_WRITE_XML)
228 	 && PHPDBG_G(io)[PHPDBG_STDOUT].fd == sock
229 	 && PHPDBG_G(lines) > 0) {
230 		return phpdbg_output_pager(sock, ptr, len);
231 	}
232 
233 	return write(sock, ptr, len);
234 }
235 
236 PHPDBG_API int phpdbg_open_socket(const char *interface, unsigned short port) {
237 	struct addrinfo res;
238 	int fd = phpdbg_create_listenable_socket(interface, port, &res);
239 
240 	if (fd == -1) {
241 		return -1;
242 	}
243 
244 	if (bind(fd, res.ai_addr, res.ai_addrlen) == -1) {
245 		phpdbg_close_socket(fd);
246 		return -4;
247 	}
248 
249 	listen(fd, 5);
250 
251 	return fd;
252 }
253 
254 
255 PHPDBG_API int phpdbg_create_listenable_socket(const char *addr, unsigned short port, struct addrinfo *addr_res) {
256 	int sock = -1, rc;
257 	int reuse = 1;
258 	struct in6_addr serveraddr;
259 	struct addrinfo hints, *res = NULL;
260 	char port_buf[8];
261 	int8_t any_addr = *addr == '*';
262 
263 	do {
264 		memset(&hints, 0, sizeof hints);
265 		if (any_addr) {
266 			hints.ai_flags = AI_PASSIVE;
267 		} else {
268 			hints.ai_flags = AI_NUMERICSERV;
269 		}
270 		hints.ai_family = AF_UNSPEC;
271 		hints.ai_socktype = SOCK_STREAM;
272 
273 		rc = inet_pton(AF_INET, addr, &serveraddr);
274 		if (1 == rc) {
275 			hints.ai_family = AF_INET;
276 			if (!any_addr) {
277 				hints.ai_flags |= AI_NUMERICHOST;
278 			}
279 		} else {
280 			rc = inet_pton(AF_INET6, addr, &serveraddr);
281 			if (1 == rc) {
282 				hints.ai_family = AF_INET6;
283 				if (!any_addr) {
284 					hints.ai_flags |= AI_NUMERICHOST;
285 				}
286 			} else {
287 				/* XXX get host by name ??? */
288 			}
289 		}
290 
291 		snprintf(port_buf, sizeof(port_buf), "%u", port);
292 		if (!any_addr) {
293 			rc = getaddrinfo(addr, port_buf, &hints, &res);
294 		} else {
295 			rc = getaddrinfo(NULL, port_buf, &hints, &res);
296 		}
297 
298 		if (0 != rc) {
299 #ifndef PHP_WIN32
300 			if (rc == EAI_SYSTEM) {
301 				char buf[128];
302 
303 				snprintf(buf, sizeof(buf), "Could not translate address '%s'", addr);
304 
305 				zend_quiet_write(PHPDBG_G(io)[PHPDBG_STDERR].fd, buf, strlen(buf));
306 
307 				return sock;
308 			} else {
309 #endif
310 				char buf[256];
311 
312 				snprintf(buf, sizeof(buf), "Host '%s' not found. %s", addr, estrdup(gai_strerror(rc)));
313 
314 				zend_quiet_write(PHPDBG_G(io)[PHPDBG_STDERR].fd, buf, strlen(buf));
315 
316 				return sock;
317 #ifndef PHP_WIN32
318 			}
319 #endif
320 			return sock;
321 		}
322 
323 		if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
324 			const char *msg = "Unable to create socket";
325 
326 			zend_quiet_write(PHPDBG_G(io)[PHPDBG_STDERR].fd, msg, strlen(msg));
327 
328 			return sock;
329 		}
330 
331 		if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*) &reuse, sizeof(reuse)) == -1) {
332 			phpdbg_close_socket(sock);
333 			return sock;
334 		}
335 
336 
337 	} while (0);
338 
339 	*addr_res = *res;
340 
341 	return sock;
342 }
343 
344 PHPDBG_API void phpdbg_close_socket(int sock) {
345 	if (sock >= 0) {
346 #ifdef _WIN32
347 		closesocket(sock);
348 #else
349 		shutdown(sock, SHUT_RDWR);
350 		close(sock);
351 #endif
352 	}
353 }
354