socketpair.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2019 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "socketpair.h"
  24. #if !defined(HAVE_SOCKETPAIR) && !defined(CURL_DISABLE_SOCKETPAIR)
  25. #ifdef WIN32
  26. /*
  27. * This is a socketpair() implementation for Windows.
  28. */
  29. #include <string.h>
  30. #include <winsock2.h>
  31. #include <ws2tcpip.h>
  32. #include <windows.h>
  33. #include <io.h>
  34. #else
  35. #ifdef HAVE_NETDB_H
  36. #include <netdb.h>
  37. #endif
  38. #ifdef HAVE_NETINET_IN_H
  39. #include <netinet/in.h> /* IPPROTO_TCP */
  40. #endif
  41. #ifdef HAVE_ARPA_INET_H
  42. #include <arpa/inet.h>
  43. #endif
  44. #ifndef INADDR_LOOPBACK
  45. #define INADDR_LOOPBACK 0x7f000001
  46. #endif /* !INADDR_LOOPBACK */
  47. #endif /* !WIN32 */
  48. #include "nonblock.h" /* for curlx_nonblock */
  49. #include "timeval.h" /* needed before select.h */
  50. #include "select.h" /* for Curl_poll */
  51. /* The last 3 #include files should be in this order */
  52. #include "curl_printf.h"
  53. #include "curl_memory.h"
  54. #include "memdebug.h"
  55. int Curl_socketpair(int domain, int type, int protocol,
  56. curl_socket_t socks[2])
  57. {
  58. union {
  59. struct sockaddr_in inaddr;
  60. struct sockaddr addr;
  61. } a, a2;
  62. curl_socket_t listener;
  63. curl_socklen_t addrlen = sizeof(a.inaddr);
  64. int reuse = 1;
  65. struct pollfd pfd[1];
  66. (void)domain;
  67. (void)type;
  68. (void)protocol;
  69. listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  70. if(listener == CURL_SOCKET_BAD)
  71. return -1;
  72. memset(&a, 0, sizeof(a));
  73. a.inaddr.sin_family = AF_INET;
  74. a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  75. a.inaddr.sin_port = 0;
  76. socks[0] = socks[1] = CURL_SOCKET_BAD;
  77. if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
  78. (char *)&reuse, (curl_socklen_t)sizeof(reuse)) == -1)
  79. goto error;
  80. if(bind(listener, &a.addr, sizeof(a.inaddr)) == -1)
  81. goto error;
  82. if(getsockname(listener, &a.addr, &addrlen) == -1 ||
  83. addrlen < (int)sizeof(a.inaddr))
  84. goto error;
  85. if(listen(listener, 1) == -1)
  86. goto error;
  87. socks[0] = socket(AF_INET, SOCK_STREAM, 0);
  88. if(socks[0] == CURL_SOCKET_BAD)
  89. goto error;
  90. if(connect(socks[0], &a.addr, sizeof(a.inaddr)) == -1)
  91. goto error;
  92. /* use non-blocking accept to make sure we don't block forever */
  93. if(curlx_nonblock(listener, TRUE) < 0)
  94. goto error;
  95. pfd[0].fd = listener;
  96. pfd[0].events = POLLIN;
  97. pfd[0].revents = 0;
  98. (void)Curl_poll(pfd, 1, 10*1000); /* 10 seconds */
  99. socks[1] = accept(listener, NULL, NULL);
  100. if(socks[1] == CURL_SOCKET_BAD)
  101. goto error;
  102. /* verify that nothing else connected */
  103. addrlen = sizeof(a.inaddr);
  104. if(getsockname(socks[0], &a.addr, &addrlen) == -1 ||
  105. addrlen < (int)sizeof(a.inaddr))
  106. goto error;
  107. addrlen = sizeof(a2.inaddr);
  108. if(getpeername(socks[1], &a2.addr, &addrlen) == -1 ||
  109. addrlen < (int)sizeof(a2.inaddr))
  110. goto error;
  111. if(a.inaddr.sin_family != a2.inaddr.sin_family ||
  112. a.inaddr.sin_addr.s_addr != a2.inaddr.sin_addr.s_addr ||
  113. a.inaddr.sin_port != a2.inaddr.sin_port)
  114. goto error;
  115. sclose(listener);
  116. return 0;
  117. error:
  118. sclose(listener);
  119. sclose(socks[0]);
  120. sclose(socks[1]);
  121. return -1;
  122. }
  123. #endif /* ! HAVE_SOCKETPAIR */