socket.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_SOCKET_H
  6. #define TOR_SOCKET_H
  7. #include "orconfig.h"
  8. #include "lib/cc/torint.h"
  9. #include "lib/net/nettypes.h"
  10. #include "lib/testsupport/testsupport.h"
  11. #include <errno.h>
  12. struct sockaddr;
  13. int tor_close_socket_simple(tor_socket_t s);
  14. MOCK_DECL(int, tor_close_socket, (tor_socket_t s));
  15. void tor_take_socket_ownership(tor_socket_t s);
  16. tor_socket_t tor_open_socket_with_extensions(
  17. int domain, int type, int protocol,
  18. int cloexec, int nonblock);
  19. MOCK_DECL(tor_socket_t,tor_open_socket,(int domain, int type, int protocol));
  20. tor_socket_t tor_open_socket_nonblocking(int domain, int type, int protocol);
  21. tor_socket_t tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr,
  22. socklen_t *len);
  23. tor_socket_t tor_accept_socket_nonblocking(tor_socket_t sockfd,
  24. struct sockaddr *addr,
  25. socklen_t *len);
  26. tor_socket_t tor_accept_socket_with_extensions(tor_socket_t sockfd,
  27. struct sockaddr *addr,
  28. socklen_t *len,
  29. int cloexec, int nonblock);
  30. MOCK_DECL(tor_socket_t, tor_connect_socket,(tor_socket_t socket,
  31. const struct sockaddr *address,
  32. socklen_t address_len));
  33. int get_n_open_sockets(void);
  34. MOCK_DECL(int,tor_getsockname,(tor_socket_t socket, struct sockaddr *address,
  35. socklen_t *address_len));
  36. struct tor_addr_t;
  37. int tor_addr_from_getsockname(struct tor_addr_t *addr_out, tor_socket_t sock);
  38. #define tor_socket_send(s, buf, len, flags) send(s, buf, len, flags)
  39. #define tor_socket_recv(s, buf, len, flags) recv(s, buf, len, flags)
  40. int set_socket_nonblocking(tor_socket_t socket);
  41. int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]);
  42. int network_init(void);
  43. int get_max_sockets(void);
  44. void set_max_sockets(int);
  45. /* For stupid historical reasons, windows sockets have an independent
  46. * set of errnos, and an independent way to get them. Also, you can't
  47. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  48. * errnos against expected values, and use tor_socket_errno to find
  49. * the actual errno after a socket operation fails.
  50. */
  51. #if defined(_WIN32)
  52. /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
  53. #define SOCK_ERRNO(e) WSA##e
  54. /** Return true if e is EAGAIN or the local equivalent. */
  55. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  56. /** Return true if e is EINPROGRESS or the local equivalent. */
  57. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  58. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  59. * a call to connect(). */
  60. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  61. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  62. /** Return true if e is EAGAIN or another error indicating that a call to
  63. * accept() has no pending connections to return. */
  64. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  65. /** Return true if e is EMFILE or another error indicating that a call to
  66. * accept() has failed because we're out of fds or something. */
  67. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  68. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  69. /** Return true if e is EADDRINUSE or the local equivalent. */
  70. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  71. /** Return true if e is EINTR or the local equivalent */
  72. #define ERRNO_IS_EINTR(e) ((e) == WSAEINTR || 0)
  73. int tor_socket_errno(tor_socket_t sock);
  74. const char *tor_socket_strerror(int e);
  75. #else /* !(defined(_WIN32)) */
  76. #define SOCK_ERRNO(e) e
  77. #if EAGAIN == EWOULDBLOCK
  78. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  79. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || 0)
  80. #else
  81. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
  82. #endif /* EAGAIN == EWOULDBLOCK */
  83. #define ERRNO_IS_EINTR(e) ((e) == EINTR || 0)
  84. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  85. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  86. #define ERRNO_IS_ACCEPT_EAGAIN(e) \
  87. (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
  88. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  89. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  90. #define ERRNO_IS_EADDRINUSE(e) (((e) == EADDRINUSE) || 0)
  91. #define tor_socket_errno(sock) (errno)
  92. #define tor_socket_strerror(e) strerror(e)
  93. #endif /* defined(_WIN32) */
  94. #ifdef SOCKET_PRIVATE
  95. #if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
  96. #define NEED_ERSATZ_SOCKETPAIR
  97. STATIC int tor_ersatz_socketpair(int family, int type, int protocol,
  98. tor_socket_t fd[2]);
  99. #endif
  100. #endif /* defined(COMPAT_PRIVATE) */
  101. #endif