sockets.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. | Authors: Chris Vandomelen <chrisv@b0rked.dhs.org> |
  14. | Sterling Hughes <sterling@php.net> |
  15. | |
  16. | WinSock: Daniel Beulshausen <daniel@php4win.de> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* Code originally from ext/sockets */
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22. #include "php.h"
  23. PHPAPI int socketpair_win32(int domain, int type, int protocol, SOCKET sock[2], int overlapped)
  24. {
  25. struct sockaddr_in address;
  26. SOCKET redirect;
  27. int size = sizeof(address);
  28. if (domain != AF_INET) {
  29. WSASetLastError(WSAENOPROTOOPT);
  30. return -1;
  31. }
  32. sock[1] = redirect = INVALID_SOCKET;
  33. sock[0] = socket(domain, type, protocol);
  34. if (INVALID_SOCKET == sock[0]) {
  35. goto error;
  36. }
  37. address.sin_addr.s_addr = INADDR_ANY;
  38. address.sin_family = AF_INET;
  39. address.sin_port = 0;
  40. if (bind(sock[0], (struct sockaddr *) &address, sizeof(address)) != 0) {
  41. goto error;
  42. }
  43. if (getsockname(sock[0], (struct sockaddr *) &address, &size) != 0) {
  44. goto error;
  45. }
  46. if (listen(sock[0], 2) != 0) {
  47. goto error;
  48. }
  49. if (overlapped) {
  50. sock[1] = socket(domain, type, protocol);
  51. } else {
  52. sock[1] = WSASocket(domain, type, protocol, NULL, 0, 0);
  53. }
  54. if (INVALID_SOCKET == sock[1]) {
  55. goto error;
  56. }
  57. address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  58. if (connect(sock[1], (struct sockaddr *) &address, sizeof(address)) != 0) {
  59. goto error;
  60. }
  61. redirect = accept(sock[0], (struct sockaddr *) &address, &size);
  62. if (INVALID_SOCKET == redirect) {
  63. goto error;
  64. }
  65. closesocket(sock[0]);
  66. sock[0] = redirect;
  67. return 0;
  68. error:
  69. closesocket(redirect);
  70. closesocket(sock[0]);
  71. closesocket(sock[1]);
  72. WSASetLastError(WSAECONNABORTED);
  73. return -1;
  74. }
  75. PHPAPI int socketpair(int domain, int type, int protocol, SOCKET sock[2])
  76. {
  77. return socketpair_win32(domain, type, protocol, sock, 1);
  78. }