phpdbg_io.c 7.9 KB

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