socketpair.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. #include "lib/cc/torint.h"
  5. #include "lib/net/socketpair.h"
  6. #include "lib/net/inaddr_st.h"
  7. #include "lib/arch/bytes.h"
  8. #include <errno.h>
  9. #include <string.h>
  10. #ifdef HAVE_UNISTD_H
  11. #include <unistd.h>
  12. #endif
  13. #ifdef HAVE_NETINET_IN_H
  14. #include <netinet/in.h>
  15. #endif
  16. #ifdef _WIN32
  17. #include <winsock2.h>
  18. #include <windows.h>
  19. #define socket_errno() (WSAGetLastError())
  20. #define SOCKET_EPROTONOSUPPORT WSAEPROTONOSUPPORT
  21. #else
  22. #define closesocket(x) close(x)
  23. #define socket_errno() (errno)
  24. #define SOCKET_EPROTONOSUPPORT EPROTONOSUPPORT
  25. #endif
  26. #ifdef NEED_ERSATZ_SOCKETPAIR
  27. // Avoid warning about call to memcmp.
  28. #define raw_memcmp memcmp
  29. /**
  30. * Return a new socket that is bound and listening on the loopback interface
  31. * of family <b>family</b> for a socket of type <b>type</b>. On failure return
  32. * TOR_INVALID_SOCKET.
  33. */
  34. static tor_socket_t
  35. get_local_listener(int family, int type)
  36. {
  37. struct sockaddr_in sin;
  38. struct sockaddr_in6 sin6;
  39. struct sockaddr *sa;
  40. int len;
  41. memset(&sin, 0, sizeof(sin));
  42. memset(&sin6, 0, sizeof(sin6));
  43. tor_socket_t sock = TOR_INVALID_SOCKET;
  44. sock = socket(family, type, 0);
  45. if (!SOCKET_OK(sock)) {
  46. return TOR_INVALID_SOCKET;
  47. }
  48. if (family == AF_INET) {
  49. sa = (struct sockaddr *) &sin;
  50. sin.sin_family = AF_INET;
  51. sin.sin_addr.s_addr = tor_htonl(0x7f000001);
  52. len = sizeof(sin);
  53. } else {
  54. sa = (struct sockaddr *) &sin6;
  55. sin6.sin6_family = AF_INET6;
  56. sin6.sin6_addr.s6_addr[15] = 1;
  57. len = sizeof(sin6);
  58. }
  59. if (bind(sock, sa, len) == -1)
  60. goto err;
  61. if (listen(sock, 1) == -1)
  62. goto err;
  63. return sock;
  64. err:
  65. closesocket(sock);
  66. return TOR_INVALID_SOCKET;
  67. }
  68. /**
  69. * Return true iff sa1 and sa2 are equivalent AF_INET or AF_INET6 addresses.
  70. */
  71. static int
  72. sockaddr_eq(struct sockaddr *sa1, struct sockaddr *sa2)
  73. {
  74. if (sa1->sa_family != sa2->sa_family)
  75. return 0;
  76. if (sa1->sa_family == AF_INET6) {
  77. struct sockaddr_in6 *sin6_1 = (struct sockaddr_in6 *) sa1;
  78. struct sockaddr_in6 *sin6_2 = (struct sockaddr_in6 *) sa2;
  79. return sin6_1->sin6_port == sin6_2->sin6_port &&
  80. 0==raw_memcmp(sin6_1->sin6_addr.s6_addr, sin6_2->sin6_addr.s6_addr, 16);
  81. } else if (sa1->sa_family == AF_INET) {
  82. struct sockaddr_in *sin_1 = (struct sockaddr_in *) sa1;
  83. struct sockaddr_in *sin_2 = (struct sockaddr_in *) sa2;
  84. return sin_1->sin_port == sin_2->sin_port &&
  85. sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr;
  86. } else {
  87. return 0;
  88. }
  89. }
  90. /**
  91. * Helper used to implement socketpair on systems that lack it, by
  92. * making a direct connection to localhost.
  93. *
  94. * See tor_socketpair() for details.
  95. *
  96. * The direct connection defaults to IPv4, but falls back to IPv6 if
  97. * IPv4 is not supported.
  98. **/
  99. int
  100. tor_ersatz_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
  101. {
  102. /* This socketpair does not work when localhost is down. So
  103. * it's really not the same thing at all. But it's close enough
  104. * for now, and really, when localhost is down sometimes, we
  105. * have other problems too.
  106. */
  107. tor_socket_t listener = TOR_INVALID_SOCKET;
  108. tor_socket_t connector = TOR_INVALID_SOCKET;
  109. tor_socket_t acceptor = TOR_INVALID_SOCKET;
  110. struct sockaddr_storage accepted_addr_ss;
  111. struct sockaddr_storage connect_addr_ss;
  112. struct sockaddr *connect_addr = (struct sockaddr *) &connect_addr_ss;
  113. struct sockaddr *accepted_addr = (struct sockaddr *) &accepted_addr_ss;
  114. socklen_t size;
  115. int saved_errno = -1;
  116. int ersatz_domain = AF_INET;
  117. socklen_t addrlen = sizeof(struct sockaddr_in);
  118. memset(&accepted_addr_ss, 0, sizeof(accepted_addr_ss));
  119. memset(&connect_addr_ss, 0, sizeof(connect_addr_ss));
  120. if (protocol
  121. #ifdef AF_UNIX
  122. || family != AF_UNIX
  123. #endif
  124. ) {
  125. #ifdef _WIN32
  126. return -WSAEAFNOSUPPORT;
  127. #else
  128. return -EAFNOSUPPORT;
  129. #endif
  130. }
  131. if (!fd) {
  132. return -EINVAL;
  133. }
  134. listener = get_local_listener(ersatz_domain, type);
  135. if (!SOCKET_OK(listener)) {
  136. int first_errno = socket_errno();
  137. if (first_errno == SOCKET_EPROTONOSUPPORT) {
  138. /* Assume we're on an IPv6-only system */
  139. ersatz_domain = AF_INET6;
  140. addrlen = sizeof(struct sockaddr_in6);
  141. listener = get_local_listener(ersatz_domain, type);
  142. }
  143. if (!SOCKET_OK(listener)) {
  144. /* Keep the previous behaviour, which was to return the IPv4 error.
  145. * (This may be less informative on IPv6-only systems.)
  146. * XX/teor - is there a better way to decide which errno to return?
  147. * (I doubt we care much either way, once there is an error.)
  148. */
  149. return -first_errno;
  150. }
  151. }
  152. connector = socket(ersatz_domain, type, 0);
  153. if (!SOCKET_OK(connector))
  154. goto tidy_up_and_fail;
  155. /* We want to find out the port number to connect to. */
  156. size = sizeof(connect_addr_ss);
  157. if (getsockname(listener, connect_addr, &size) == -1)
  158. goto tidy_up_and_fail;
  159. if (size != addrlen)
  160. goto abort_tidy_up_and_fail;
  161. if (connect(connector, connect_addr, size) == -1)
  162. goto tidy_up_and_fail;
  163. size = sizeof(accepted_addr_ss);
  164. acceptor = accept(listener, accepted_addr, &size);
  165. if (!SOCKET_OK(acceptor))
  166. goto tidy_up_and_fail;
  167. if (size != addrlen)
  168. goto abort_tidy_up_and_fail;
  169. /* Now check we are talking to ourself by matching port and host on the
  170. two sockets. */
  171. if (getsockname(connector, connect_addr, &size) == -1)
  172. goto tidy_up_and_fail;
  173. /* Set *_tor_addr and *_port to the address and port that was used */
  174. if (!sockaddr_eq(accepted_addr, connect_addr))
  175. goto abort_tidy_up_and_fail;
  176. closesocket(listener);
  177. fd[0] = connector;
  178. fd[1] = acceptor;
  179. return 0;
  180. abort_tidy_up_and_fail:
  181. #ifdef _WIN32
  182. saved_errno = WSAECONNABORTED;
  183. #else
  184. saved_errno = ECONNABORTED; /* I hope this is portable and appropriate. */
  185. #endif
  186. tidy_up_and_fail:
  187. if (saved_errno < 0)
  188. saved_errno = errno;
  189. if (SOCKET_OK(listener))
  190. closesocket(listener);
  191. if (SOCKET_OK(connector))
  192. closesocket(connector);
  193. if (SOCKET_OK(acceptor))
  194. closesocket(acceptor);
  195. return -saved_errno;
  196. }
  197. #endif /* defined(NEED_ERSATZ_SOCKETPAIR) */