sockets.c 2.8 KB

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