socketpair.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <winsock2.h>
  2. int
  3. xmlrpc_win32_socketpair(int const domain,
  4. int const type,
  5. int const protocol,
  6. SOCKET socks[2]) {
  7. bool error;
  8. error = false; // initial value
  9. SOCKET listener;
  10. listener = socket(AF_INET, SOCK_STREAM, 0);
  11. if (listener == INVALID_SOCKET)
  12. error = true;
  13. else {
  14. struct sockaddr_in addr;
  15. memset(&addr, 0, sizeof(addr));
  16. addr.sin_family = AF_INET;
  17. addr.sin_addr.s_addr = htonl(0x7f000001);
  18. addr.sin_port = 0;
  19. int rc;
  20. rc = bind(listener, (const struct sockaddr*) &addr, sizeof(addr));
  21. if (rc == SOCKET_ERROR)
  22. error = true;
  23. else {
  24. int addrlen;
  25. int rc;
  26. addrlen = sizeof(addr); // initial value
  27. rc = getsockname(listener, (struct sockaddr*) &addr, &addrlen);
  28. if (rc == SOCKET_ERROR)
  29. error = true;
  30. else {
  31. int rc;
  32. rc = listen(listener, 1);
  33. if (rc == SOCKET_ERROR)
  34. error = true;
  35. else {
  36. socks[0] = socket(AF_INET, SOCK_STREAM, 0);
  37. if (socks[0] == INVALID_SOCKET)
  38. error = true;
  39. else {
  40. int rc;
  41. rc = connect(socks[0],
  42. (const struct sockaddr*) &addr,
  43. sizeof(addr));
  44. if (rc == SOCKET_ERROR)
  45. error = true;
  46. else {
  47. socks[1] = accept(listener, NULL, NULL);
  48. if (socks[1] == INVALID_SOCKET)
  49. error = true;
  50. }
  51. if (error)
  52. closesocket(socks[0]);
  53. }
  54. }
  55. }
  56. }
  57. closesocket(listener);
  58. }
  59. return error ? -1 : 0;
  60. }