PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/sockets/sockets.c

http://github.com/infusion/PHP
C | 2041 lines | 1531 code | 355 blank | 155 comment | 258 complexity | 55153c2d14115e84a8daeaa38fad994e MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 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: Chris Vandomelen <chrisv@b0rked.dhs.org> |
  16. | Sterling Hughes <sterling@php.net> |
  17. | Jason Greene <jason@php.net> |
  18. | WinSock: Daniel Beulshausen <daniel@php4win.de> |
  19. +----------------------------------------------------------------------+
  20. */
  21. /* $Id: sockets.c 306939 2011-01-01 02:19:59Z felipe $ */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "php.h"
  26. #if HAVE_SOCKETS
  27. #include "php_network.h"
  28. #include "ext/standard/file.h"
  29. #include "ext/standard/info.h"
  30. #include "php_ini.h"
  31. #ifdef PHP_WIN32
  32. # include "win32/inet.h"
  33. # include <winsock2.h>
  34. # include <windows.h>
  35. # include <Ws2tcpip.h>
  36. # include "php_sockets.h"
  37. # include "win32/sockets.h"
  38. # define IS_INVALID_SOCKET(a) (a->bsd_socket == INVALID_SOCKET)
  39. # ifdef EPROTONOSUPPORT
  40. # undef EPROTONOSUPPORT
  41. # endif
  42. # ifdef ECONNRESET
  43. # undef ECONNRESET
  44. # endif
  45. # define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  46. # define ECONNRESET WSAECONNRESET
  47. # ifdef errno
  48. # undef errno
  49. # endif
  50. # define errno WSAGetLastError()
  51. # define h_errno WSAGetLastError()
  52. # define set_errno(a) WSASetLastError(a)
  53. # define close(a) closesocket(a)
  54. #else
  55. # include <sys/types.h>
  56. # include <sys/socket.h>
  57. # include <netdb.h>
  58. # include <netinet/in.h>
  59. # include <netinet/tcp.h>
  60. # include <sys/un.h>
  61. # include <arpa/inet.h>
  62. # include <sys/time.h>
  63. # include <unistd.h>
  64. # include <errno.h>
  65. # include <fcntl.h>
  66. # include <signal.h>
  67. # include <sys/uio.h>
  68. # define IS_INVALID_SOCKET(a) (a->bsd_socket < 0)
  69. # define set_errno(a) (errno = a)
  70. # include "php_sockets.h"
  71. #endif
  72. ZEND_DECLARE_MODULE_GLOBALS(sockets)
  73. static PHP_GINIT_FUNCTION(sockets);
  74. #ifndef MSG_WAITALL
  75. #ifdef LINUX
  76. #define MSG_WAITALL 0x00000100
  77. #else
  78. #define MSG_WAITALL 0x00000000
  79. #endif
  80. #endif
  81. #ifndef MSG_EOF
  82. #ifdef MSG_FIN
  83. #define MSG_EOF MSG_FIN
  84. #endif
  85. #endif
  86. #ifndef SUN_LEN
  87. #define SUN_LEN(su) (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
  88. #endif
  89. #ifndef PF_INET
  90. #define PF_INET AF_INET
  91. #endif
  92. static char *php_strerror(int error TSRMLS_DC);
  93. #define PHP_NORMAL_READ 0x0001
  94. #define PHP_BINARY_READ 0x0002
  95. #define PHP_SOCKET_ERROR(socket,msg,errn) socket->error = errn; \
  96. SOCKETS_G(last_error) = errn; \
  97. php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s [%d]: %s", msg, errn, php_strerror(errn TSRMLS_CC))
  98. static int le_socket;
  99. #define le_socket_name php_sockets_le_socket_name
  100. /* {{{ arginfo */
  101. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_select, 0, 0, 4)
  102. ZEND_ARG_INFO(1, read_fds)
  103. ZEND_ARG_INFO(1, write_fds)
  104. ZEND_ARG_INFO(1, except_fds)
  105. ZEND_ARG_INFO(0, tv_sec)
  106. ZEND_ARG_INFO(0, tv_usec)
  107. ZEND_END_ARG_INFO()
  108. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_listen, 0, 0, 1)
  109. ZEND_ARG_INFO(0, port)
  110. ZEND_ARG_INFO(0, backlog)
  111. ZEND_END_ARG_INFO()
  112. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_accept, 0, 0, 1)
  113. ZEND_ARG_INFO(0, socket)
  114. ZEND_END_ARG_INFO()
  115. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_nonblock, 0, 0, 1)
  116. ZEND_ARG_INFO(0, socket)
  117. ZEND_END_ARG_INFO()
  118. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_block, 0, 0, 1)
  119. ZEND_ARG_INFO(0, socket)
  120. ZEND_END_ARG_INFO()
  121. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_listen, 0, 0, 1)
  122. ZEND_ARG_INFO(0, socket)
  123. ZEND_ARG_INFO(0, backlog)
  124. ZEND_END_ARG_INFO()
  125. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_close, 0, 0, 1)
  126. ZEND_ARG_INFO(0, socket)
  127. ZEND_END_ARG_INFO()
  128. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_write, 0, 0, 2)
  129. ZEND_ARG_INFO(0, socket)
  130. ZEND_ARG_INFO(0, buf)
  131. ZEND_ARG_INFO(0, length)
  132. ZEND_END_ARG_INFO()
  133. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_read, 0, 0, 2)
  134. ZEND_ARG_INFO(0, socket)
  135. ZEND_ARG_INFO(0, length)
  136. ZEND_ARG_INFO(0, type)
  137. ZEND_END_ARG_INFO()
  138. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_getsockname, 0, 0, 2)
  139. ZEND_ARG_INFO(0, socket)
  140. ZEND_ARG_INFO(1, addr)
  141. ZEND_ARG_INFO(1, port)
  142. ZEND_END_ARG_INFO()
  143. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_getpeername, 0, 0, 2)
  144. ZEND_ARG_INFO(0, socket)
  145. ZEND_ARG_INFO(1, addr)
  146. ZEND_ARG_INFO(1, port)
  147. ZEND_END_ARG_INFO()
  148. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create, 0, 0, 3)
  149. ZEND_ARG_INFO(0, domain)
  150. ZEND_ARG_INFO(0, type)
  151. ZEND_ARG_INFO(0, protocol)
  152. ZEND_END_ARG_INFO()
  153. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_connect, 0, 0, 2)
  154. ZEND_ARG_INFO(0, socket)
  155. ZEND_ARG_INFO(0, addr)
  156. ZEND_ARG_INFO(0, port)
  157. ZEND_END_ARG_INFO()
  158. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_strerror, 0, 0, 1)
  159. ZEND_ARG_INFO(0, errno)
  160. ZEND_END_ARG_INFO()
  161. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_bind, 0, 0, 2)
  162. ZEND_ARG_INFO(0, socket)
  163. ZEND_ARG_INFO(0, addr)
  164. ZEND_ARG_INFO(0, port)
  165. ZEND_END_ARG_INFO()
  166. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recv, 0, 0, 4)
  167. ZEND_ARG_INFO(0, socket)
  168. ZEND_ARG_INFO(1, buf)
  169. ZEND_ARG_INFO(0, len)
  170. ZEND_ARG_INFO(0, flags)
  171. ZEND_END_ARG_INFO()
  172. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_send, 0, 0, 4)
  173. ZEND_ARG_INFO(0, socket)
  174. ZEND_ARG_INFO(0, buf)
  175. ZEND_ARG_INFO(0, len)
  176. ZEND_ARG_INFO(0, flags)
  177. ZEND_END_ARG_INFO()
  178. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_recvfrom, 0, 0, 5)
  179. ZEND_ARG_INFO(0, socket)
  180. ZEND_ARG_INFO(1, buf)
  181. ZEND_ARG_INFO(0, len)
  182. ZEND_ARG_INFO(0, flags)
  183. ZEND_ARG_INFO(1, name)
  184. ZEND_ARG_INFO(1, port)
  185. ZEND_END_ARG_INFO()
  186. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_sendto, 0, 0, 5)
  187. ZEND_ARG_INFO(0, socket)
  188. ZEND_ARG_INFO(0, buf)
  189. ZEND_ARG_INFO(0, len)
  190. ZEND_ARG_INFO(0, flags)
  191. ZEND_ARG_INFO(0, addr)
  192. ZEND_ARG_INFO(0, port)
  193. ZEND_END_ARG_INFO()
  194. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_get_option, 0, 0, 3)
  195. ZEND_ARG_INFO(0, socket)
  196. ZEND_ARG_INFO(0, level)
  197. ZEND_ARG_INFO(0, optname)
  198. ZEND_END_ARG_INFO()
  199. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_set_option, 0, 0, 4)
  200. ZEND_ARG_INFO(0, socket)
  201. ZEND_ARG_INFO(0, level)
  202. ZEND_ARG_INFO(0, optname)
  203. ZEND_ARG_INFO(0, optval)
  204. ZEND_END_ARG_INFO()
  205. #ifdef HAVE_SOCKETPAIR
  206. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_create_pair, 0, 0, 4)
  207. ZEND_ARG_INFO(0, domain)
  208. ZEND_ARG_INFO(0, type)
  209. ZEND_ARG_INFO(0, protocol)
  210. ZEND_ARG_INFO(1, fd)
  211. ZEND_END_ARG_INFO()
  212. #endif
  213. #ifdef HAVE_SHUTDOWN
  214. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_shutdown, 0, 0, 1)
  215. ZEND_ARG_INFO(0, socket)
  216. ZEND_ARG_INFO(0, how)
  217. ZEND_END_ARG_INFO()
  218. #endif
  219. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_last_error, 0, 0, 0)
  220. ZEND_ARG_INFO(0, socket)
  221. ZEND_END_ARG_INFO()
  222. ZEND_BEGIN_ARG_INFO_EX(arginfo_socket_clear_error, 0, 0, 0)
  223. ZEND_ARG_INFO(0, socket)
  224. ZEND_END_ARG_INFO()
  225. /* }}} */
  226. /* {{{ sockets_functions[]
  227. */
  228. const zend_function_entry sockets_functions[] = {
  229. PHP_FE(socket_select, arginfo_socket_select)
  230. PHP_FE(socket_create, arginfo_socket_create)
  231. PHP_FE(socket_create_listen, arginfo_socket_create_listen)
  232. #ifdef HAVE_SOCKETPAIR
  233. PHP_FE(socket_create_pair, arginfo_socket_create_pair)
  234. #endif
  235. PHP_FE(socket_accept, arginfo_socket_accept)
  236. PHP_FE(socket_set_nonblock, arginfo_socket_set_nonblock)
  237. PHP_FE(socket_set_block, arginfo_socket_set_block)
  238. PHP_FE(socket_listen, arginfo_socket_listen)
  239. PHP_FE(socket_close, arginfo_socket_close)
  240. PHP_FE(socket_write, arginfo_socket_write)
  241. PHP_FE(socket_read, arginfo_socket_read)
  242. PHP_FE(socket_getsockname, arginfo_socket_getsockname)
  243. PHP_FE(socket_getpeername, arginfo_socket_getpeername)
  244. PHP_FE(socket_connect, arginfo_socket_connect)
  245. PHP_FE(socket_strerror, arginfo_socket_strerror)
  246. PHP_FE(socket_bind, arginfo_socket_bind)
  247. PHP_FE(socket_recv, arginfo_socket_recv)
  248. PHP_FE(socket_send, arginfo_socket_send)
  249. PHP_FE(socket_recvfrom, arginfo_socket_recvfrom)
  250. PHP_FE(socket_sendto, arginfo_socket_sendto)
  251. PHP_FE(socket_get_option, arginfo_socket_get_option)
  252. PHP_FE(socket_set_option, arginfo_socket_set_option)
  253. #ifdef HAVE_SHUTDOWN
  254. PHP_FE(socket_shutdown, arginfo_socket_shutdown)
  255. #endif
  256. PHP_FE(socket_last_error, arginfo_socket_last_error)
  257. PHP_FE(socket_clear_error, arginfo_socket_clear_error)
  258. /* for downwards compatability */
  259. PHP_FALIAS(socket_getopt, socket_get_option, arginfo_socket_get_option)
  260. PHP_FALIAS(socket_setopt, socket_set_option, arginfo_socket_set_option)
  261. {NULL, NULL, NULL}
  262. };
  263. /* }}} */
  264. zend_module_entry sockets_module_entry = {
  265. STANDARD_MODULE_HEADER,
  266. "sockets",
  267. sockets_functions,
  268. PHP_MINIT(sockets),
  269. NULL,
  270. NULL,
  271. PHP_RSHUTDOWN(sockets),
  272. PHP_MINFO(sockets),
  273. NO_VERSION_YET,
  274. PHP_MODULE_GLOBALS(sockets),
  275. PHP_GINIT(sockets),
  276. NULL,
  277. NULL,
  278. STANDARD_MODULE_PROPERTIES_EX
  279. };
  280. #ifdef COMPILE_DL_SOCKETS
  281. ZEND_GET_MODULE(sockets)
  282. #endif
  283. /* inet_ntop should be used instead of inet_ntoa */
  284. int inet_ntoa_lock = 0;
  285. PHP_SOCKETS_API int php_sockets_le_socket(void) /* {{{ */
  286. {
  287. return le_socket;
  288. }
  289. /* }}} */
  290. static void php_destroy_socket(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
  291. {
  292. php_socket *php_sock = (php_socket *) rsrc->ptr;
  293. close(php_sock->bsd_socket);
  294. efree(php_sock);
  295. }
  296. /* }}} */
  297. static int php_open_listen_sock(php_socket **php_sock, int port, int backlog TSRMLS_DC) /* {{{ */
  298. {
  299. struct sockaddr_in la;
  300. struct hostent *hp;
  301. php_socket *sock = (php_socket*)emalloc(sizeof(php_socket));
  302. *php_sock = sock;
  303. #ifndef PHP_WIN32
  304. if ((hp = gethostbyname("0.0.0.0")) == NULL) {
  305. #else
  306. if ((hp = gethostbyname("localhost")) == NULL) {
  307. #endif
  308. efree(sock);
  309. return 0;
  310. }
  311. memcpy((char *) &la.sin_addr, hp->h_addr, hp->h_length);
  312. la.sin_family = hp->h_addrtype;
  313. la.sin_port = htons((unsigned short) port);
  314. sock->bsd_socket = socket(PF_INET, SOCK_STREAM, 0);
  315. sock->blocking = 1;
  316. if (IS_INVALID_SOCKET(sock)) {
  317. PHP_SOCKET_ERROR(sock, "unable to create listening socket", errno);
  318. efree(sock);
  319. return 0;
  320. }
  321. sock->type = PF_INET;
  322. if (bind(sock->bsd_socket, (struct sockaddr *)&la, sizeof(la)) != 0) {
  323. PHP_SOCKET_ERROR(sock, "unable to bind to given address", errno);
  324. close(sock->bsd_socket);
  325. efree(sock);
  326. return 0;
  327. }
  328. if (listen(sock->bsd_socket, backlog) != 0) {
  329. PHP_SOCKET_ERROR(sock, "unable to listen on socket", errno);
  330. close(sock->bsd_socket);
  331. efree(sock);
  332. return 0;
  333. }
  334. return 1;
  335. }
  336. /* }}} */
  337. static int php_accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la TSRMLS_DC) /* {{{ */
  338. {
  339. socklen_t salen;
  340. php_socket *out_sock = (php_socket*)emalloc(sizeof(php_socket));
  341. *new_sock = out_sock;
  342. salen = sizeof(*la);
  343. out_sock->blocking = 1;
  344. out_sock->bsd_socket = accept(in_sock->bsd_socket, la, &salen);
  345. if (IS_INVALID_SOCKET(out_sock)) {
  346. PHP_SOCKET_ERROR(out_sock, "unable to accept incoming connection", errno);
  347. efree(out_sock);
  348. return 0;
  349. }
  350. return 1;
  351. }
  352. /* }}} */
  353. /* {{{ php_read -- wrapper around read() so that it only reads to a \r or \n. */
  354. static int php_read(php_socket *sock, void *buf, size_t maxlen, int flags)
  355. {
  356. int m = 0;
  357. size_t n = 0;
  358. int no_read = 0;
  359. int nonblock = 0;
  360. char *t = (char *) buf;
  361. #ifndef PHP_WIN32
  362. m = fcntl(sock->bsd_socket, F_GETFL);
  363. if (m < 0) {
  364. return m;
  365. }
  366. nonblock = (m & O_NONBLOCK);
  367. m = 0;
  368. #else
  369. nonblock = !sock->blocking;
  370. #endif
  371. set_errno(0);
  372. *t = '\0';
  373. while (*t != '\n' && *t != '\r' && n < maxlen) {
  374. if (m > 0) {
  375. t++;
  376. n++;
  377. } else if (m == 0) {
  378. no_read++;
  379. if (nonblock && no_read >= 2) {
  380. return n;
  381. /* The first pass, m always is 0, so no_read becomes 1
  382. * in the first pass. no_read becomes 2 in the second pass,
  383. * and if this is nonblocking, we should return.. */
  384. }
  385. if (no_read > 200) {
  386. set_errno(ECONNRESET);
  387. return -1;
  388. }
  389. }
  390. if (n < maxlen) {
  391. m = recv(sock->bsd_socket, (void *) t, 1, flags);
  392. }
  393. if (errno != 0 && errno != ESPIPE && errno != EAGAIN) {
  394. return -1;
  395. }
  396. set_errno(0);
  397. }
  398. if (n < maxlen) {
  399. n++;
  400. /* The only reasons it makes it to here is
  401. * if '\n' or '\r' are encountered. So, increase
  402. * the return by 1 to make up for the lack of the
  403. * '\n' or '\r' in the count (since read() takes
  404. * place at the end of the loop..) */
  405. }
  406. return n;
  407. }
  408. /* }}} */
  409. static char *php_strerror(int error TSRMLS_DC) /* {{{ */
  410. {
  411. const char *buf;
  412. #ifndef PHP_WIN32
  413. if (error < -10000) {
  414. error = -error - 10000;
  415. #ifdef HAVE_HSTRERROR
  416. buf = hstrerror(error);
  417. #else
  418. {
  419. if (SOCKETS_G(strerror_buf)) {
  420. efree(SOCKETS_G(strerror_buf));
  421. }
  422. spprintf(&(SOCKETS_G(strerror_buf)), 0, "Host lookup error %d", error);
  423. buf = SOCKETS_G(strerror_buf);
  424. }
  425. #endif
  426. } else {
  427. buf = strerror(error);
  428. }
  429. #else
  430. {
  431. LPTSTR tmp = NULL;
  432. buf = NULL;
  433. if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  434. NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &tmp, 0, NULL)
  435. ) {
  436. if (SOCKETS_G(strerror_buf)) {
  437. efree(SOCKETS_G(strerror_buf));
  438. }
  439. SOCKETS_G(strerror_buf) = estrdup(tmp);
  440. LocalFree(tmp);
  441. buf = SOCKETS_G(strerror_buf);
  442. }
  443. }
  444. #endif
  445. return (buf ? (char *) buf : "");
  446. }
  447. /* }}} */
  448. #if HAVE_IPV6
  449. /* Sets addr by hostname, or by ip in string form (AF_INET6) */
  450. static int php_set_inet6_addr(struct sockaddr_in6 *sin6, char *string, php_socket *php_sock TSRMLS_DC) /* {{{ */
  451. {
  452. struct in6_addr tmp;
  453. #if HAVE_GETADDRINFO
  454. struct addrinfo hints;
  455. struct addrinfo *addrinfo = NULL;
  456. #endif
  457. if (inet_pton(AF_INET6, string, &tmp)) {
  458. memcpy(&(sin6->sin6_addr.s6_addr), &(tmp.s6_addr), sizeof(struct in6_addr));
  459. } else {
  460. #if HAVE_GETADDRINFO
  461. memset(&hints, 0, sizeof(struct addrinfo));
  462. hints.ai_family = PF_INET6;
  463. getaddrinfo(string, NULL, &hints, &addrinfo);
  464. if (!addrinfo) {
  465. #ifdef PHP_WIN32
  466. PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
  467. #else
  468. PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
  469. #endif
  470. return 0;
  471. }
  472. if (addrinfo->ai_family != PF_INET6 || addrinfo->ai_addrlen != sizeof(struct sockaddr_in6)) {
  473. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: Non AF_INET6 domain returned on AF_INET6 socket");
  474. freeaddrinfo(addrinfo);
  475. return 0;
  476. }
  477. memcpy(&(sin6->sin6_addr.s6_addr), ((struct sockaddr_in6*)(addrinfo->ai_addr))->sin6_addr.s6_addr, sizeof(struct in6_addr));
  478. freeaddrinfo(addrinfo);
  479. #else
  480. /* No IPv6 specific hostname resolution is available on this system? */
  481. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: getaddrinfo() not available on this system");
  482. return 0;
  483. #endif
  484. }
  485. return 1;
  486. }
  487. /* }}} */
  488. #endif
  489. /* Sets addr by hostname, or by ip in string form (AF_INET) */
  490. static int php_set_inet_addr(struct sockaddr_in *sin, char *string, php_socket *php_sock TSRMLS_DC) /* {{{ */
  491. {
  492. struct in_addr tmp;
  493. struct hostent *host_entry;
  494. if (inet_aton(string, &tmp)) {
  495. sin->sin_addr.s_addr = tmp.s_addr;
  496. } else {
  497. if (! (host_entry = gethostbyname(string))) {
  498. /* Note: < -10000 indicates a host lookup error */
  499. #ifdef PHP_WIN32
  500. PHP_SOCKET_ERROR(php_sock, "Host lookup failed", WSAGetLastError());
  501. #else
  502. PHP_SOCKET_ERROR(php_sock, "Host lookup failed", (-10000 - h_errno));
  503. #endif
  504. return 0;
  505. }
  506. if (host_entry->h_addrtype != AF_INET) {
  507. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host lookup failed: Non AF_INET domain returned on AF_INET socket");
  508. return 0;
  509. }
  510. memcpy(&(sin->sin_addr.s_addr), host_entry->h_addr_list[0], host_entry->h_length);
  511. }
  512. return 1;
  513. }
  514. /* }}} */
  515. /* {{{ PHP_GINIT_FUNCTION */
  516. static PHP_GINIT_FUNCTION(sockets)
  517. {
  518. sockets_globals->last_error = 0;
  519. sockets_globals->strerror_buf = NULL;
  520. }
  521. /* }}} */
  522. /* {{{ PHP_MINIT_FUNCTION
  523. */
  524. PHP_MINIT_FUNCTION(sockets)
  525. {
  526. struct protoent *pe;
  527. le_socket = zend_register_list_destructors_ex(php_destroy_socket, NULL, le_socket_name, module_number);
  528. REGISTER_LONG_CONSTANT("AF_UNIX", AF_UNIX, CONST_CS | CONST_PERSISTENT);
  529. REGISTER_LONG_CONSTANT("AF_INET", AF_INET, CONST_CS | CONST_PERSISTENT);
  530. #if HAVE_IPV6
  531. REGISTER_LONG_CONSTANT("AF_INET6", AF_INET6, CONST_CS | CONST_PERSISTENT);
  532. #endif
  533. REGISTER_LONG_CONSTANT("SOCK_STREAM", SOCK_STREAM, CONST_CS | CONST_PERSISTENT);
  534. REGISTER_LONG_CONSTANT("SOCK_DGRAM", SOCK_DGRAM, CONST_CS | CONST_PERSISTENT);
  535. REGISTER_LONG_CONSTANT("SOCK_RAW", SOCK_RAW, CONST_CS | CONST_PERSISTENT);
  536. REGISTER_LONG_CONSTANT("SOCK_SEQPACKET",SOCK_SEQPACKET, CONST_CS | CONST_PERSISTENT);
  537. REGISTER_LONG_CONSTANT("SOCK_RDM", SOCK_RDM, CONST_CS | CONST_PERSISTENT);
  538. REGISTER_LONG_CONSTANT("MSG_OOB", MSG_OOB, CONST_CS | CONST_PERSISTENT);
  539. REGISTER_LONG_CONSTANT("MSG_WAITALL", MSG_WAITALL, CONST_CS | CONST_PERSISTENT);
  540. #ifdef MSG_DONTWAIT
  541. REGISTER_LONG_CONSTANT("MSG_DONTWAIT", MSG_DONTWAIT, CONST_CS | CONST_PERSISTENT);
  542. #endif
  543. REGISTER_LONG_CONSTANT("MSG_PEEK", MSG_PEEK, CONST_CS | CONST_PERSISTENT);
  544. REGISTER_LONG_CONSTANT("MSG_DONTROUTE", MSG_DONTROUTE, CONST_CS | CONST_PERSISTENT);
  545. #ifdef MSG_EOR
  546. REGISTER_LONG_CONSTANT("MSG_EOR", MSG_EOR, CONST_CS | CONST_PERSISTENT);
  547. #endif
  548. #ifdef MSG_EOF
  549. REGISTER_LONG_CONSTANT("MSG_EOF", MSG_EOF, CONST_CS | CONST_PERSISTENT);
  550. #endif
  551. REGISTER_LONG_CONSTANT("SO_DEBUG", SO_DEBUG, CONST_CS | CONST_PERSISTENT);
  552. REGISTER_LONG_CONSTANT("SO_REUSEADDR", SO_REUSEADDR, CONST_CS | CONST_PERSISTENT);
  553. REGISTER_LONG_CONSTANT("SO_KEEPALIVE", SO_KEEPALIVE, CONST_CS | CONST_PERSISTENT);
  554. REGISTER_LONG_CONSTANT("SO_DONTROUTE", SO_DONTROUTE, CONST_CS | CONST_PERSISTENT);
  555. REGISTER_LONG_CONSTANT("SO_LINGER", SO_LINGER, CONST_CS | CONST_PERSISTENT);
  556. REGISTER_LONG_CONSTANT("SO_BROADCAST", SO_BROADCAST, CONST_CS | CONST_PERSISTENT);
  557. REGISTER_LONG_CONSTANT("SO_OOBINLINE", SO_OOBINLINE, CONST_CS | CONST_PERSISTENT);
  558. REGISTER_LONG_CONSTANT("SO_SNDBUF", SO_SNDBUF, CONST_CS | CONST_PERSISTENT);
  559. REGISTER_LONG_CONSTANT("SO_RCVBUF", SO_RCVBUF, CONST_CS | CONST_PERSISTENT);
  560. REGISTER_LONG_CONSTANT("SO_SNDLOWAT", SO_SNDLOWAT, CONST_CS | CONST_PERSISTENT);
  561. REGISTER_LONG_CONSTANT("SO_RCVLOWAT", SO_RCVLOWAT, CONST_CS | CONST_PERSISTENT);
  562. REGISTER_LONG_CONSTANT("SO_SNDTIMEO", SO_SNDTIMEO, CONST_CS | CONST_PERSISTENT);
  563. REGISTER_LONG_CONSTANT("SO_RCVTIMEO", SO_RCVTIMEO, CONST_CS | CONST_PERSISTENT);
  564. REGISTER_LONG_CONSTANT("SO_TYPE", SO_TYPE, CONST_CS | CONST_PERSISTENT);
  565. REGISTER_LONG_CONSTANT("SO_ERROR", SO_ERROR, CONST_CS | CONST_PERSISTENT);
  566. REGISTER_LONG_CONSTANT("SOL_SOCKET", SOL_SOCKET, CONST_CS | CONST_PERSISTENT);
  567. REGISTER_LONG_CONSTANT("SOMAXCONN", SOMAXCONN, CONST_CS | CONST_PERSISTENT);
  568. #ifdef TCP_NODELAY
  569. REGISTER_LONG_CONSTANT("TCP_NODELAY", TCP_NODELAY, CONST_CS | CONST_PERSISTENT);
  570. #endif
  571. REGISTER_LONG_CONSTANT("PHP_NORMAL_READ", PHP_NORMAL_READ, CONST_CS | CONST_PERSISTENT);
  572. REGISTER_LONG_CONSTANT("PHP_BINARY_READ", PHP_BINARY_READ, CONST_CS | CONST_PERSISTENT);
  573. #ifndef WIN32
  574. # include "unix_socket_constants.h"
  575. #else
  576. # include "win32_socket_constants.h"
  577. #endif
  578. if ((pe = getprotobyname("tcp"))) {
  579. REGISTER_LONG_CONSTANT("SOL_TCP", pe->p_proto, CONST_CS | CONST_PERSISTENT);
  580. }
  581. if ((pe = getprotobyname("udp"))) {
  582. REGISTER_LONG_CONSTANT("SOL_UDP", pe->p_proto, CONST_CS | CONST_PERSISTENT);
  583. }
  584. return SUCCESS;
  585. }
  586. /* }}} */
  587. /* {{{ PHP_MINFO_FUNCTION
  588. */
  589. PHP_MINFO_FUNCTION(sockets)
  590. {
  591. php_info_print_table_start();
  592. php_info_print_table_row(2, "Sockets Support", "enabled");
  593. php_info_print_table_end();
  594. }
  595. /* }}} */
  596. /* {{{ PHP_RSHUTDOWN_FUNCTION */
  597. PHP_RSHUTDOWN_FUNCTION(sockets)
  598. {
  599. if (SOCKETS_G(strerror_buf)) {
  600. efree(SOCKETS_G(strerror_buf));
  601. SOCKETS_G(strerror_buf) = NULL;
  602. }
  603. return SUCCESS;
  604. }
  605. /* }}} */
  606. static int php_sock_array_to_fd_set(zval *sock_array, fd_set *fds, PHP_SOCKET *max_fd TSRMLS_DC) /* {{{ */
  607. {
  608. zval **element;
  609. php_socket *php_sock;
  610. int num = 0;
  611. if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
  612. for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(sock_array));
  613. zend_hash_get_current_data(Z_ARRVAL_P(sock_array), (void **) &element) == SUCCESS;
  614. zend_hash_move_forward(Z_ARRVAL_P(sock_array))) {
  615. php_sock = (php_socket*) zend_fetch_resource(element TSRMLS_CC, -1, le_socket_name, NULL, 1, le_socket);
  616. if (!php_sock) continue; /* If element is not a resource, skip it */
  617. PHP_SAFE_FD_SET(php_sock->bsd_socket, fds);
  618. if (php_sock->bsd_socket > *max_fd) {
  619. *max_fd = php_sock->bsd_socket;
  620. }
  621. num++;
  622. }
  623. return num ? 1 : 0;
  624. }
  625. /* }}} */
  626. static int php_sock_array_from_fd_set(zval *sock_array, fd_set *fds TSRMLS_DC) /* {{{ */
  627. {
  628. zval **element;
  629. zval **dest_element;
  630. php_socket *php_sock;
  631. HashTable *new_hash;
  632. char *key;
  633. int num = 0;
  634. ulong num_key;
  635. uint key_len;
  636. if (Z_TYPE_P(sock_array) != IS_ARRAY) return 0;
  637. ALLOC_HASHTABLE(new_hash);
  638. zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(sock_array)), NULL, ZVAL_PTR_DTOR, 0);
  639. for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(sock_array));
  640. zend_hash_get_current_data(Z_ARRVAL_P(sock_array), (void **) &element) == SUCCESS;
  641. zend_hash_move_forward(Z_ARRVAL_P(sock_array))) {
  642. php_sock = (php_socket*) zend_fetch_resource(element TSRMLS_CC, -1, le_socket_name, NULL, 1, le_socket);
  643. if (!php_sock) continue; /* If element is not a resource, skip it */
  644. if (PHP_SAFE_FD_ISSET(php_sock->bsd_socket, fds)) {
  645. /* Add fd to new array */
  646. switch (zend_hash_get_current_key_ex(Z_ARRVAL_P(sock_array), &key, &key_len, &num_key, 0, NULL)) {
  647. case HASH_KEY_IS_STRING:
  648. zend_hash_add(new_hash, key, key_len, (void *)element, sizeof(zval *), (void **)&dest_element);
  649. break;
  650. case HASH_KEY_IS_LONG:
  651. zend_hash_index_update(new_hash, num_key, (void *)element, sizeof(zval *), (void **)&dest_element);
  652. break;
  653. }
  654. if (dest_element) zval_add_ref(dest_element);
  655. }
  656. num++;
  657. }
  658. /* Destroy old array, add new one */
  659. zend_hash_destroy(Z_ARRVAL_P(sock_array));
  660. efree(Z_ARRVAL_P(sock_array));
  661. zend_hash_internal_pointer_reset(new_hash);
  662. Z_ARRVAL_P(sock_array) = new_hash;
  663. return num ? 1 : 0;
  664. }
  665. /* }}} */
  666. /* {{{ proto int socket_select(array &read_fds, array &write_fds, array &except_fds, int tv_sec[, int tv_usec]) U
  667. Runs the select() system call on the sets mentioned with a timeout specified by tv_sec and tv_usec */
  668. PHP_FUNCTION(socket_select)
  669. {
  670. zval *r_array, *w_array, *e_array, *sec;
  671. struct timeval tv;
  672. struct timeval *tv_p = NULL;
  673. fd_set rfds, wfds, efds;
  674. PHP_SOCKET max_fd = 0;
  675. int retval, sets = 0;
  676. long usec = 0;
  677. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!a!z!|l", &r_array, &w_array, &e_array, &sec, &usec) == FAILURE) {
  678. return;
  679. }
  680. FD_ZERO(&rfds);
  681. FD_ZERO(&wfds);
  682. FD_ZERO(&efds);
  683. if (r_array != NULL) sets += php_sock_array_to_fd_set(r_array, &rfds, &max_fd TSRMLS_CC);
  684. if (w_array != NULL) sets += php_sock_array_to_fd_set(w_array, &wfds, &max_fd TSRMLS_CC);
  685. if (e_array != NULL) sets += php_sock_array_to_fd_set(e_array, &efds, &max_fd TSRMLS_CC);
  686. if (!sets) {
  687. php_error_docref(NULL TSRMLS_CC, E_WARNING, "no resource arrays were passed to select");
  688. RETURN_FALSE;
  689. }
  690. PHP_SAFE_MAX_FD(max_fd, 0); /* someone needs to make this look more like stream_socket_select */
  691. /* If seconds is not set to null, build the timeval, else we wait indefinitely */
  692. if (sec != NULL) {
  693. zval tmp;
  694. if (Z_TYPE_P(sec) != IS_LONG) {
  695. tmp = *sec;
  696. zval_copy_ctor(&tmp);
  697. convert_to_long(&tmp);
  698. sec = &tmp;
  699. }
  700. /* Solaris + BSD do not like microsecond values which are >= 1 sec */
  701. if (usec > 999999) {
  702. tv.tv_sec = Z_LVAL_P(sec) + (usec / 1000000);
  703. tv.tv_usec = usec % 1000000;
  704. } else {
  705. tv.tv_sec = Z_LVAL_P(sec);
  706. tv.tv_usec = usec;
  707. }
  708. tv_p = &tv;
  709. if (sec == &tmp) {
  710. zval_dtor(&tmp);
  711. }
  712. }
  713. retval = select(max_fd+1, &rfds, &wfds, &efds, tv_p);
  714. if (retval == -1) {
  715. SOCKETS_G(last_error) = errno;
  716. php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to select [%d]: %s", errno, php_strerror(errno TSRMLS_CC));
  717. RETURN_FALSE;
  718. }
  719. if (r_array != NULL) php_sock_array_from_fd_set(r_array, &rfds TSRMLS_CC);
  720. if (w_array != NULL) php_sock_array_from_fd_set(w_array, &wfds TSRMLS_CC);
  721. if (e_array != NULL) php_sock_array_from_fd_set(e_array, &efds TSRMLS_CC);
  722. RETURN_LONG(retval);
  723. }
  724. /* }}} */
  725. /* {{{ proto resource socket_create_listen(int port[, int backlog]) U
  726. Opens a socket on port to accept connections */
  727. PHP_FUNCTION(socket_create_listen)
  728. {
  729. php_socket *php_sock;
  730. long port, backlog = 128;
  731. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &port, &backlog) == FAILURE) {
  732. return;
  733. }
  734. if (!php_open_listen_sock(&php_sock, port, backlog TSRMLS_CC)) {
  735. RETURN_FALSE;
  736. }
  737. php_sock->error = 0;
  738. php_sock->blocking = 1;
  739. ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
  740. }
  741. /* }}} */
  742. /* {{{ proto resource socket_accept(resource socket) U
  743. Accepts a connection on the listening socket fd */
  744. PHP_FUNCTION(socket_accept)
  745. {
  746. zval *arg1;
  747. php_socket *php_sock, *new_sock;
  748. struct sockaddr_in sa;
  749. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
  750. return;
  751. }
  752. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  753. if (!php_accept_connect(php_sock, &new_sock, (struct sockaddr *) &sa TSRMLS_CC)) {
  754. RETURN_FALSE;
  755. }
  756. new_sock->error = 0;
  757. new_sock->blocking = 1;
  758. ZEND_REGISTER_RESOURCE(return_value, new_sock, le_socket);
  759. }
  760. /* }}} */
  761. /* {{{ proto bool socket_set_nonblock(resource socket) U
  762. Sets nonblocking mode on a socket resource */
  763. PHP_FUNCTION(socket_set_nonblock)
  764. {
  765. zval *arg1;
  766. php_socket *php_sock;
  767. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
  768. return;
  769. }
  770. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  771. if (php_set_sock_blocking(php_sock->bsd_socket, 0 TSRMLS_CC) == SUCCESS) {
  772. php_sock->blocking = 0;
  773. RETURN_TRUE;
  774. }
  775. RETURN_FALSE;
  776. }
  777. /* }}} */
  778. /* {{{ proto bool socket_set_block(resource socket) U
  779. Sets blocking mode on a socket resource */
  780. PHP_FUNCTION(socket_set_block)
  781. {
  782. zval *arg1;
  783. php_socket *php_sock;
  784. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
  785. return;
  786. }
  787. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  788. if (php_set_sock_blocking(php_sock->bsd_socket, 1 TSRMLS_CC) == SUCCESS) {
  789. php_sock->blocking = 1;
  790. RETURN_TRUE;
  791. }
  792. RETURN_FALSE;
  793. }
  794. /* }}} */
  795. /* {{{ proto bool socket_listen(resource socket[, int backlog]) U
  796. Sets the maximum number of connections allowed to be waited for on the socket specified by fd */
  797. PHP_FUNCTION(socket_listen)
  798. {
  799. zval *arg1;
  800. php_socket *php_sock;
  801. long backlog = 0;
  802. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &arg1, &backlog) == FAILURE) {
  803. return;
  804. }
  805. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  806. if (listen(php_sock->bsd_socket, backlog) != 0) {
  807. PHP_SOCKET_ERROR(php_sock, "unable to listen on socket", errno);
  808. RETURN_FALSE;
  809. }
  810. RETURN_TRUE;
  811. }
  812. /* }}} */
  813. /* {{{ proto void socket_close(resource socket) U
  814. Closes a file descriptor */
  815. PHP_FUNCTION(socket_close)
  816. {
  817. zval *arg1;
  818. php_socket *php_sock;
  819. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
  820. return;
  821. }
  822. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  823. zend_list_delete(Z_RESVAL_P(arg1));
  824. }
  825. /* }}} */
  826. /* {{{ proto int socket_write(resource socket, string buf[, int length])
  827. Writes the buffer to the socket resource, length is optional */
  828. PHP_FUNCTION(socket_write)
  829. {
  830. zval *arg1;
  831. php_socket *php_sock;
  832. int retval, str_len;
  833. long length = 0;
  834. char *str;
  835. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &str, &str_len, &length) == FAILURE) {
  836. return;
  837. }
  838. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  839. if (ZEND_NUM_ARGS() < 3) {
  840. length = str_len;
  841. }
  842. #ifndef PHP_WIN32
  843. retval = write(php_sock->bsd_socket, str, MIN(length, str_len));
  844. #else
  845. retval = send(php_sock->bsd_socket, str, min(length, str_len), 0);
  846. #endif
  847. if (retval < 0) {
  848. PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
  849. RETURN_FALSE;
  850. }
  851. RETURN_LONG(retval);
  852. }
  853. /* }}} */
  854. /* {{{ proto string socket_read(resource socket, int length [, int type]) U
  855. Reads a maximum of length bytes from socket */
  856. PHP_FUNCTION(socket_read)
  857. {
  858. zval *arg1;
  859. php_socket *php_sock;
  860. char *tmpbuf;
  861. int retval;
  862. long length, type = PHP_BINARY_READ;
  863. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|l", &arg1, &length, &type) == FAILURE) {
  864. return;
  865. }
  866. /* overflow check */
  867. if ((length + 1) < 2) {
  868. RETURN_FALSE;
  869. }
  870. tmpbuf = emalloc(length + 1);
  871. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  872. if (type == PHP_NORMAL_READ) {
  873. retval = php_read(php_sock, tmpbuf, length, 0);
  874. } else {
  875. retval = recv(php_sock->bsd_socket, tmpbuf, length, 0);
  876. }
  877. if (retval == -1) {
  878. /* if the socket is in non-blocking mode and there's no data to read,
  879. don't output any error, as this is a normal situation, and not an error */
  880. if (errno == EAGAIN
  881. #ifdef EWOULDBLOCK
  882. || errno == EWOULDBLOCK
  883. #endif
  884. ) {
  885. php_sock->error = errno;
  886. SOCKETS_G(last_error) = errno;
  887. } else {
  888. PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
  889. }
  890. efree(tmpbuf);
  891. RETURN_FALSE;
  892. } else if (!retval) {
  893. efree(tmpbuf);
  894. RETURN_EMPTY_STRING();
  895. }
  896. tmpbuf = erealloc(tmpbuf, retval + 1);
  897. tmpbuf[retval] = '\0' ;
  898. RETURN_STRINGL(tmpbuf, retval, 0);
  899. }
  900. /* }}} */
  901. /* {{{ proto bool socket_getsockname(resource socket, string &addr[, int &port])
  902. Queries the remote side of the given socket which may either result in host/port or in a UNIX filesystem path, dependent on its type. */
  903. PHP_FUNCTION(socket_getsockname)
  904. {
  905. zval *arg1, *addr, *port = NULL;
  906. php_sockaddr_storage sa_storage;
  907. php_socket *php_sock;
  908. struct sockaddr *sa;
  909. struct sockaddr_in *sin;
  910. #if HAVE_IPV6
  911. struct sockaddr_in6 *sin6;
  912. char addr6[INET6_ADDRSTRLEN+1];
  913. #endif
  914. struct sockaddr_un *s_un;
  915. char *addr_string;
  916. socklen_t salen = sizeof(php_sockaddr_storage);
  917. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|z", &arg1, &addr, &port) == FAILURE) {
  918. return;
  919. }
  920. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  921. sa = (struct sockaddr *) &sa_storage;
  922. if (getsockname(php_sock->bsd_socket, sa, &salen) != 0) {
  923. PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket name", errno);
  924. RETURN_FALSE;
  925. }
  926. switch (sa->sa_family) {
  927. #if HAVE_IPV6
  928. case AF_INET6:
  929. sin6 = (struct sockaddr_in6 *) sa;
  930. inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
  931. zval_dtor(addr);
  932. ZVAL_STRING(addr, addr6, 1);
  933. if (port != NULL) {
  934. zval_dtor(port);
  935. ZVAL_LONG(port, htons(sin6->sin6_port));
  936. }
  937. RETURN_TRUE;
  938. break;
  939. #endif
  940. case AF_INET:
  941. sin = (struct sockaddr_in *) sa;
  942. while (inet_ntoa_lock == 1);
  943. inet_ntoa_lock = 1;
  944. addr_string = inet_ntoa(sin->sin_addr);
  945. inet_ntoa_lock = 0;
  946. zval_dtor(addr);
  947. ZVAL_STRING(addr, addr_string, 1);
  948. if (port != NULL) {
  949. zval_dtor(port);
  950. ZVAL_LONG(port, htons(sin->sin_port));
  951. }
  952. RETURN_TRUE;
  953. break;
  954. case AF_UNIX:
  955. s_un = (struct sockaddr_un *) sa;
  956. zval_dtor(addr);
  957. ZVAL_STRING(addr, s_un->sun_path, 1);
  958. RETURN_TRUE;
  959. break;
  960. default:
  961. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa->sa_family);
  962. RETURN_FALSE;
  963. }
  964. }
  965. /* }}} */
  966. /* {{{ proto bool socket_getpeername(resource socket, string &addr[, int &port])
  967. Queries the remote side of the given socket which may either result in host/port or in a UNIX filesystem path, dependent on its type. */
  968. PHP_FUNCTION(socket_getpeername)
  969. {
  970. zval *arg1, *arg2, *arg3 = NULL;
  971. php_sockaddr_storage sa_storage;
  972. php_socket *php_sock;
  973. struct sockaddr *sa;
  974. struct sockaddr_in *sin;
  975. #if HAVE_IPV6
  976. struct sockaddr_in6 *sin6;
  977. char addr6[INET6_ADDRSTRLEN+1];
  978. #endif
  979. struct sockaddr_un *s_un;
  980. char *addr_string;
  981. socklen_t salen = sizeof(php_sockaddr_storage);
  982. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|z", &arg1, &arg2, &arg3) == FAILURE) {
  983. return;
  984. }
  985. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  986. sa = (struct sockaddr *) &sa_storage;
  987. if (getpeername(php_sock->bsd_socket, sa, &salen) < 0) {
  988. PHP_SOCKET_ERROR(php_sock, "unable to retrieve peer name", errno);
  989. RETURN_FALSE;
  990. }
  991. switch (sa->sa_family) {
  992. #if HAVE_IPV6
  993. case AF_INET6:
  994. sin6 = (struct sockaddr_in6 *) sa;
  995. inet_ntop(AF_INET6, &sin6->sin6_addr, addr6, INET6_ADDRSTRLEN);
  996. zval_dtor(arg2);
  997. ZVAL_STRING(arg2, addr6, 1);
  998. if (arg3 != NULL) {
  999. zval_dtor(arg3);
  1000. ZVAL_LONG(arg3, htons(sin6->sin6_port));
  1001. }
  1002. RETURN_TRUE;
  1003. break;
  1004. #endif
  1005. case AF_INET:
  1006. sin = (struct sockaddr_in *) sa;
  1007. while (inet_ntoa_lock == 1);
  1008. inet_ntoa_lock = 1;
  1009. addr_string = inet_ntoa(sin->sin_addr);
  1010. inet_ntoa_lock = 0;
  1011. zval_dtor(arg2);
  1012. ZVAL_STRING(arg2, addr_string, 1);
  1013. if (arg3 != NULL) {
  1014. zval_dtor(arg3);
  1015. ZVAL_LONG(arg3, htons(sin->sin_port));
  1016. }
  1017. RETURN_TRUE;
  1018. break;
  1019. case AF_UNIX:
  1020. s_un = (struct sockaddr_un *) sa;
  1021. zval_dtor(arg2);
  1022. ZVAL_STRING(arg2, s_un->sun_path, 1);
  1023. RETURN_TRUE;
  1024. break;
  1025. default:
  1026. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported address family %d", sa->sa_family);
  1027. RETURN_FALSE;
  1028. }
  1029. }
  1030. /* }}} */
  1031. /* {{{ proto resource socket_create(int domain, int type, int protocol) U
  1032. Creates an endpoint for communication in the domain specified by domain, of type specified by type */
  1033. PHP_FUNCTION(socket_create)
  1034. {
  1035. long arg1, arg2, arg3;
  1036. php_socket *php_sock = (php_socket*)emalloc(sizeof(php_socket));
  1037. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &arg1, &arg2, &arg3) == FAILURE) {
  1038. efree(php_sock);
  1039. return;
  1040. }
  1041. if (arg1 != AF_UNIX
  1042. #if HAVE_IPV6
  1043. && arg1 != AF_INET6
  1044. #endif
  1045. && arg1 != AF_INET) {
  1046. php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket domain [%ld] specified for argument 1, assuming AF_INET", arg1);
  1047. arg1 = AF_INET;
  1048. }
  1049. if (arg2 > 10) {
  1050. php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid socket type [%ld] specified for argument 2, assuming SOCK_STREAM", arg2);
  1051. arg2 = SOCK_STREAM;
  1052. }
  1053. php_sock->bsd_socket = socket(arg1, arg2, arg3);
  1054. php_sock->type = arg1;
  1055. if (IS_INVALID_SOCKET(php_sock)) {
  1056. SOCKETS_G(last_error) = errno;
  1057. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create socket [%d]: %s", errno, php_strerror(errno TSRMLS_CC));
  1058. efree(php_sock);
  1059. RETURN_FALSE;
  1060. }
  1061. php_sock->error = 0;
  1062. php_sock->blocking = 1;
  1063. ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
  1064. }
  1065. /* }}} */
  1066. /* {{{ proto bool socket_connect(resource socket, string addr [, int port])
  1067. Opens a connection to addr:port on the socket specified by socket */
  1068. PHP_FUNCTION(socket_connect)
  1069. {
  1070. zval *arg1;
  1071. php_socket *php_sock;
  1072. struct sockaddr_in sin;
  1073. #if HAVE_IPV6
  1074. struct sockaddr_in6 sin6;
  1075. #endif
  1076. struct sockaddr_un s_un;
  1077. char *addr;
  1078. int retval, addr_len;
  1079. long port = 0;
  1080. int argc = ZEND_NUM_ARGS();
  1081. if (zend_parse_parameters(argc TSRMLS_CC, "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
  1082. return;
  1083. }
  1084. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  1085. switch(php_sock->type) {
  1086. #if HAVE_IPV6
  1087. case AF_INET6:
  1088. if (argc != 3) {
  1089. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET6 requires 3 arguments");
  1090. RETURN_FALSE;
  1091. }
  1092. memset(&sin6, 0, sizeof(struct sockaddr_in6));
  1093. sin6.sin6_family = AF_INET6;
  1094. sin6.sin6_port = htons((unsigned short int)port);
  1095. if (! php_set_inet6_addr(&sin6, addr, php_sock TSRMLS_CC)) {
  1096. RETURN_FALSE;
  1097. }
  1098. retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6));
  1099. break;
  1100. #endif
  1101. case AF_INET:
  1102. if (argc != 3) {
  1103. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Socket of type AF_INET requires 3 arguments");
  1104. RETURN_FALSE;
  1105. }
  1106. memset(&sin, 0, sizeof(struct sockaddr_in));
  1107. sin.sin_family = AF_INET;
  1108. sin.sin_port = htons((unsigned short int)port);
  1109. if (! php_set_inet_addr(&sin, addr, php_sock TSRMLS_CC)) {
  1110. RETURN_FALSE;
  1111. }
  1112. retval = connect(php_sock->bsd_socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
  1113. break;
  1114. case AF_UNIX:
  1115. memset(&s_un, 0, sizeof(struct sockaddr_un));
  1116. s_un.sun_family = AF_UNIX;
  1117. memcpy(&s_un.sun_path, addr, addr_len);
  1118. retval = connect(php_sock->bsd_socket, (struct sockaddr *) &s_un, (socklen_t) XtOffsetOf(struct sockaddr_un, sun_path) + addr_len);
  1119. break;
  1120. default:
  1121. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
  1122. RETURN_FALSE;
  1123. }
  1124. if (retval != 0) {
  1125. PHP_SOCKET_ERROR(php_sock, "unable to connect", errno);
  1126. RETURN_FALSE;
  1127. }
  1128. RETURN_TRUE;
  1129. }
  1130. /* }}} */
  1131. /* {{{ proto string socket_strerror(int errno)
  1132. Returns a string describing an error */
  1133. PHP_FUNCTION(socket_strerror)
  1134. {
  1135. long arg1;
  1136. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &arg1) == FAILURE) {
  1137. return;
  1138. }
  1139. RETURN_STRING(php_strerror(arg1 TSRMLS_CC), 1);
  1140. }
  1141. /* }}} */
  1142. /* {{{ proto bool socket_bind(resource socket, string addr [, int port])
  1143. Binds an open socket to a listening port, port is only specified in AF_INET family. */
  1144. PHP_FUNCTION(socket_bind)
  1145. {
  1146. zval *arg1;
  1147. php_sockaddr_storage sa_storage;
  1148. struct sockaddr *sock_type = (struct sockaddr*) &sa_storage;
  1149. php_socket *php_sock;
  1150. char *addr;
  1151. int addr_len;
  1152. long port = 0;
  1153. long retval = 0;
  1154. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|l", &arg1, &addr, &addr_len, &port) == FAILURE) {
  1155. return;
  1156. }
  1157. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  1158. switch(php_sock->type) {
  1159. case AF_UNIX:
  1160. {
  1161. struct sockaddr_un *sa = (struct sockaddr_un *) sock_type;
  1162. memset(sa, 0, sizeof(sa_storage));
  1163. sa->sun_family = AF_UNIX;
  1164. snprintf(sa->sun_path, 108, "%s", addr);
  1165. retval = bind(php_sock->bsd_socket, (struct sockaddr *) sa, SUN_LEN(sa));
  1166. break;
  1167. }
  1168. case AF_INET:
  1169. {
  1170. struct sockaddr_in *sa = (struct sockaddr_in *) sock_type;
  1171. memset(sa, 0, sizeof(sa_storage)); /* Apparently, Mac OSX needs this */
  1172. sa->sin_family = AF_INET;
  1173. sa->sin_port = htons((unsigned short) port);
  1174. if (! php_set_inet_addr(sa, addr, php_sock TSRMLS_CC)) {
  1175. RETURN_FALSE;
  1176. }
  1177. retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
  1178. break;
  1179. }
  1180. #if HAVE_IPV6
  1181. case AF_INET6:
  1182. {
  1183. struct sockaddr_in6 *sa = (struct sockaddr_in6 *) sock_type;
  1184. memset(sa, 0, sizeof(sa_storage)); /* Apparently, Mac OSX needs this */
  1185. sa->sin6_family = AF_INET6;
  1186. sa->sin6_port = htons((unsigned short) port);
  1187. if (! php_set_inet6_addr(sa, addr, php_sock TSRMLS_CC)) {
  1188. RETURN_FALSE;
  1189. }
  1190. retval = bind(php_sock->bsd_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in6));
  1191. break;
  1192. }
  1193. #endif
  1194. default:
  1195. php_error_docref(NULL TSRMLS_CC, E_WARNING, "unsupported socket type '%d', must be AF_UNIX, AF_INET, or AF_INET6", php_sock->type);
  1196. RETURN_FALSE;
  1197. }
  1198. if (retval != 0) {
  1199. PHP_SOCKET_ERROR(php_sock, "unable to bind address", errno);
  1200. RETURN_FALSE;
  1201. }
  1202. RETURN_TRUE;
  1203. }
  1204. /* }}} */
  1205. /* {{{ proto int socket_recv(resource socket, string &buf, int len, int flags)
  1206. Receives data from a connected socket */
  1207. PHP_FUNCTION(socket_recv)
  1208. {
  1209. zval *php_sock_res, *buf;
  1210. char *recv_buf;
  1211. php_socket *php_sock;
  1212. int retval;
  1213. long len, flags;
  1214. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzll", &php_sock_res, &buf, &len, &flags) == FAILURE) {
  1215. return;
  1216. }
  1217. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &php_sock_res, -1, le_socket_name, le_socket);
  1218. /* overflow check */
  1219. if ((len + 1) < 2) {
  1220. RETURN_FALSE;
  1221. }
  1222. recv_buf = emalloc(len + 1);
  1223. memset(recv_buf, 0, len + 1);
  1224. if ((retval = recv(php_sock->bsd_socket, recv_buf, len, flags)) < 1) {
  1225. efree(recv_buf);
  1226. zval_dtor(buf);
  1227. Z_TYPE_P(buf) = IS_NULL;
  1228. } else {
  1229. recv_buf[retval] = '\0';
  1230. /* Rebuild buffer zval */
  1231. zval_dtor(buf);
  1232. Z_STRVAL_P(buf) = recv_buf;
  1233. Z_STRLEN_P(buf) = retval;
  1234. Z_TYPE_P(buf) = IS_STRING;
  1235. }
  1236. if (retval == -1) {
  1237. PHP_SOCKET_ERROR(php_sock, "unable to read from socket", errno);
  1238. RETURN_FALSE;
  1239. }
  1240. RETURN_LONG(retval);
  1241. }
  1242. /* }}} */
  1243. /* {{{ proto int socket_send(resource socket, string buf, int len, int flags)
  1244. Sends data to a connected socket */
  1245. PHP_FUNCTION(socket_send)
  1246. {
  1247. zval *arg1;
  1248. php_socket *php_sock;
  1249. int buf_len, retval;
  1250. long len, flags;
  1251. char *buf;
  1252. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsll", &arg1, &buf, &buf_len, &len, &flags) == FAILURE) {
  1253. return;
  1254. }
  1255. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  1256. retval = send(php_sock->bsd_socket, buf, (buf_len < len ? buf_len : len), flags);
  1257. if (retval == -1) {
  1258. PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
  1259. RETURN_FALSE;
  1260. }
  1261. RETURN_LONG(retval);
  1262. }
  1263. /* }}} */
  1264. /* {{{ proto int socket_recvfrom(resource socket, string &buf, int len, int flags, string &name [, int &port])
  1265. Receives data from a socket, connected or not */
  1266. PHP_FUNCTION(socket_recvfrom)
  1267. {
  1268. zval *arg1, *arg2, *arg5, *arg6 = NULL;
  1269. php_socket *php_sock;
  1270. struct sockaddr_un s_un;
  1271. struct sockaddr_in sin;
  1272. #if HAVE_IPV6
  1273. struct sockaddr_in6 sin6;
  1274. char addr6[INET6_ADDRSTRLEN];
  1275. #endif
  1276. socklen_t slen;
  1277. int retval;
  1278. long arg3, arg4;
  1279. char *recv_buf, *address;
  1280. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzllz|z", &arg1, &arg2, &arg3, &arg4, &arg5, &arg6) == FAILURE) {
  1281. return;
  1282. }
  1283. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  1284. /* overflow check */
  1285. if ((arg3 + 2) < 3) {
  1286. RETURN_FALSE;
  1287. }
  1288. recv_buf = emalloc(arg3 + 2);
  1289. memset(recv_buf, 0, arg3 + 2);
  1290. switch (php_sock->type) {
  1291. case AF_UNIX:
  1292. slen = sizeof(s_un);
  1293. s_un.sun_family = AF_UNIX;
  1294. retval = recvfrom(php_sock->bsd_socket, recv_buf, arg3, arg4, (struct sockaddr *)&s_un, (socklen_t *)&slen);
  1295. if (retval < 0) {
  1296. efree(recv_buf);
  1297. PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
  1298. RETURN_FALSE;
  1299. }
  1300. zval_dtor(arg2);
  1301. zval_dtor(arg5);
  1302. ZVAL_STRINGL(arg2, recv_buf, retval, 0);
  1303. ZVAL_STRING(arg5, s_un.sun_path, 1);
  1304. break;
  1305. case AF_INET:
  1306. slen = sizeof(sin);
  1307. memset(&sin, 0, slen);
  1308. sin.sin_family = AF_INET;
  1309. if (arg6 == NULL) {
  1310. efree(recv_buf);
  1311. WRONG_PARAM_COUNT;
  1312. }
  1313. retval = recvfrom(php_sock->bsd_socket, recv_buf, arg3, arg4, (struct sockaddr *)&sin, (socklen_t *)&slen);
  1314. if (retval < 0) {
  1315. efree(recv_buf);
  1316. PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
  1317. RETURN_FALSE;
  1318. }
  1319. zval_dtor(arg2);
  1320. zval_dtor(arg5);
  1321. zval_dtor(arg6);
  1322. address = inet_ntoa(sin.sin_addr);
  1323. ZVAL_STRINGL(arg2, recv_buf, retval, 0);
  1324. ZVAL_STRING(arg5, address ? address : "0.0.0.0", 1);
  1325. ZVAL_LONG(arg6, ntohs(sin.sin_port));
  1326. break;
  1327. #if HAVE_IPV6
  1328. case AF_INET6:
  1329. slen = sizeof(sin6);
  1330. memset(&sin6, 0, slen);
  1331. sin6.sin6_family = AF_INET6;
  1332. if (arg6 == NULL) {
  1333. efree(recv_buf);
  1334. WRONG_PARAM_COUNT;
  1335. }
  1336. retval = recvfrom(php_sock->bsd_socket, recv_buf, arg3, arg4, (struct sockaddr *)&sin6, (socklen_t *)&slen);
  1337. if (retval < 0) {
  1338. efree(recv_buf);
  1339. PHP_SOCKET_ERROR(php_sock, "unable to recvfrom", errno);
  1340. RETURN_FALSE;
  1341. }
  1342. zval_dtor(arg2);
  1343. zval_dtor(arg5);
  1344. zval_dtor(arg6);
  1345. memset(addr6, 0, INET6_ADDRSTRLEN);
  1346. inet_ntop(AF_INET6, &sin6.sin6_addr, addr6, INET6_ADDRSTRLEN);
  1347. ZVAL_STRINGL(arg2, recv_buf, retval, 0);
  1348. ZVAL_STRING(arg5, addr6[0] ? addr6 : "::", 1);
  1349. ZVAL_LONG(arg6, ntohs(sin6.sin6_port));
  1350. break;
  1351. #endif
  1352. default:
  1353. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
  1354. RETURN_FALSE;
  1355. }
  1356. RETURN_LONG(retval);
  1357. }
  1358. /* }}} */
  1359. /* {{{ proto int socket_sendto(resource socket, string buf, int len, int flags, string addr [, int port])
  1360. Sends a message to a socket, whether it is connected or not */
  1361. PHP_FUNCTION(socket_sendto)
  1362. {
  1363. zval *arg1;
  1364. php_socket *php_sock;
  1365. struct sockaddr_un s_un;
  1366. struct sockaddr_in sin;
  1367. #if HAVE_IPV6
  1368. struct sockaddr_in6 sin6;
  1369. #endif
  1370. int retval, buf_len, addr_len;
  1371. long len, flags, port = 0;
  1372. char *buf, *addr;
  1373. int argc = ZEND_NUM_ARGS();
  1374. if (zend_parse_parameters(argc TSRMLS_CC, "rslls|l", &arg1, &buf, &buf_len, &len, &flags, &addr, &addr_len, &port) == FAILURE) {
  1375. return;
  1376. }
  1377. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  1378. switch (php_sock->type) {
  1379. case AF_UNIX:
  1380. memset(&s_un, 0, sizeof(s_un));
  1381. s_un.sun_family = AF_UNIX;
  1382. snprintf(s_un.sun_path, 108, "%s", addr);
  1383. retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &s_un, SUN_LEN(&s_un));
  1384. break;
  1385. case AF_INET:
  1386. if (argc != 6) {
  1387. WRONG_PARAM_COUNT;
  1388. }
  1389. memset(&sin, 0, sizeof(sin));
  1390. sin.sin_family = AF_INET;
  1391. sin.sin_port = htons((unsigned short) port);
  1392. if (! php_set_inet_addr(&sin, addr, php_sock TSRMLS_CC)) {
  1393. RETURN_FALSE;
  1394. }
  1395. retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &sin, sizeof(sin));
  1396. break;
  1397. #if HAVE_IPV6
  1398. case AF_INET6:
  1399. if (argc != 6) {
  1400. WRONG_PARAM_COUNT;
  1401. }
  1402. memset(&sin6, 0, sizeof(sin6));
  1403. sin6.sin6_family = AF_INET6;
  1404. sin6.sin6_port = htons((unsigned short) port);
  1405. if (! php_set_inet6_addr(&sin6, addr, php_sock TSRMLS_CC)) {
  1406. RETURN_FALSE;
  1407. }
  1408. retval = sendto(php_sock->bsd_socket, buf, (len > buf_len) ? buf_len : len, flags, (struct sockaddr *) &sin6, sizeof(sin6));
  1409. break;
  1410. #endif
  1411. default:
  1412. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported socket type %d", php_sock->type);
  1413. RETURN_FALSE;
  1414. }
  1415. if (retval == -1) {
  1416. PHP_SOCKET_ERROR(php_sock, "unable to write to socket", errno);
  1417. RETURN_FALSE;
  1418. }
  1419. RETURN_LONG(retval);
  1420. }
  1421. /* }}} */
  1422. /* {{{ proto mixed socket_get_option(resource socket, int level, int optname) U
  1423. Gets socket options for the socket */
  1424. PHP_FUNCTION(socket_get_option)
  1425. {
  1426. zval *arg1;
  1427. struct linger linger_val;
  1428. struct timeval tv;
  1429. #ifdef PHP_WIN32
  1430. int timeout = 0;
  1431. #endif
  1432. socklen_t optlen;
  1433. php_socket *php_sock;
  1434. int other_val;
  1435. long level, optname;
  1436. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rll", &arg1, &level, &optname) == FAILURE) {
  1437. return;
  1438. }
  1439. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  1440. switch(optname) {
  1441. case SO_LINGER:
  1442. optlen = sizeof(linger_val);
  1443. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&linger_val, &optlen) != 0) {
  1444. PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
  1445. RETURN_FALSE;
  1446. }
  1447. array_init(return_value);
  1448. add_assoc_long(return_value, "l_onoff", linger_val.l_onoff);
  1449. add_assoc_long(return_value, "l_linger", linger_val.l_linger);
  1450. break;
  1451. case SO_RCVTIMEO:
  1452. case SO_SNDTIMEO:
  1453. #ifndef PHP_WIN32
  1454. optlen = sizeof(tv);
  1455. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&tv, &optlen) != 0) {
  1456. PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
  1457. RETURN_FALSE;
  1458. }
  1459. #else
  1460. optlen = sizeof(int);
  1461. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&timeout, &optlen) != 0) {
  1462. PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
  1463. RETURN_FALSE;
  1464. }
  1465. tv.tv_sec = timeout ? timeout / 1000 : 0;
  1466. tv.tv_usec = timeout ? (timeout * 1000) % 1000000 : 0;
  1467. #endif
  1468. array_init(return_value);
  1469. add_assoc_long(return_value, "sec", tv.tv_sec);
  1470. add_assoc_long(return_value, "usec", tv.tv_usec);
  1471. break;
  1472. default:
  1473. optlen = sizeof(other_val);
  1474. if (getsockopt(php_sock->bsd_socket, level, optname, (char*)&other_val, &optlen) != 0) {
  1475. PHP_SOCKET_ERROR(php_sock, "unable to retrieve socket option", errno);
  1476. RETURN_FALSE;
  1477. }
  1478. RETURN_LONG(other_val);
  1479. break;
  1480. }
  1481. }
  1482. /* }}} */
  1483. /* {{{ proto bool socket_set_option(resource socket, int level, int optname, int|array optval)
  1484. Sets socket options for the socket */
  1485. PHP_FUNCTION(socket_set_option)
  1486. {
  1487. zval *arg1, **arg4;
  1488. struct linger lv;
  1489. php_socket *php_sock;
  1490. int ov, optlen, retval;
  1491. #ifdef PHP_WIN32
  1492. int timeout;
  1493. #else
  1494. struct timeval tv;
  1495. #endif
  1496. long level, optname;
  1497. void *opt_ptr;
  1498. HashTable *opt_ht;
  1499. zval **l_onoff, **l_linger;
  1500. zval **sec, **usec;
  1501. /* key name constants */
  1502. char *l_onoff_key = "l_onoff";
  1503. char *l_linger_key = "l_linger";
  1504. char *sec_key = "sec";
  1505. char *usec_key = "usec";
  1506. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllZ", &arg1, &level, &optname, &arg4) == FAILURE) {
  1507. return;
  1508. }
  1509. ZEND_FETCH_RESOURCE(php_sock, php_socket *, &arg1, -1, le_socket_name, le_socket);
  1510. set_errno(0);
  1511. switch (optname) {
  1512. case SO_LINGER:
  1513. convert_to_array_ex(arg4);
  1514. opt_ht = HASH_OF(*arg4);
  1515. if (zend_hash_find(opt_ht, l_onoff_key, strlen(l_onoff_key) + 1, (void **)&l_onoff) == FAILURE) {
  1516. php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", l_onoff_key);
  1517. RETURN_FALSE;
  1518. }
  1519. if (zend_hash_find(opt_ht, l_linger_key, strlen(l_linger_key) + 1, (void **)&l_linger) == FAILURE) {
  1520. php_error_docref(NULL TSRMLS_CC, E_WARNING, "no key \"%s\" passed in optval", l_linger_key);
  1521. RETURN_FALSE;
  1522. }
  1523. convert_to_long_ex(l_onoff);
  1524. convert_to_long_ex(l_linger);
  1525. lv.l_onoff = (unsigned short)Z_LVAL_PP(l_onoff);
  1526. lv.l_linger = (unsigned short)Z_LVAL_PP(l_linger);
  1527. optlen = sizeof(lv);
  1528. opt_ptr = &lv;
  1529. break;
  1530. case SO_RCVTIMEO:
  1531. case SO_SNDTIMEO:
  1532. convert_to_array_ex(arg4);
  1533. opt_ht = HASH_OF(*arg4);
  1534. if (zend_hash_find(opt_ht, sec_key, strlen(sec_key) + 1

Large files files are truncated, but you can click here to view the full file