socket.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. ssize_t write_all_to_socket(tor_socket_t fd, const char *buf, size_t count);
  46. ssize_t read_all_from_socket(tor_socket_t fd, char *buf, size_t count);
  47. /* For stupid historical reasons, windows sockets have an independent
  48. * set of errnos, and an independent way to get them. Also, you can't
  49. * always believe WSAEWOULDBLOCK. Use the macros below to compare
  50. * errnos against expected values, and use tor_socket_errno to find
  51. * the actual errno after a socket operation fails.
  52. */
  53. #if defined(_WIN32)
  54. /** Expands to WSA<b>e</b> on Windows, and to <b>e</b> elsewhere. */
  55. #define SOCK_ERRNO(e) WSA##e
  56. /** Return true if e is EAGAIN or the local equivalent. */
  57. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
  58. /** Return true if e is EINPROGRESS or the local equivalent. */
  59. #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
  60. /** Return true if e is EINPROGRESS or the local equivalent as returned by
  61. * a call to connect(). */
  62. #define ERRNO_IS_CONN_EINPROGRESS(e) \
  63. ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
  64. /** Return true if e is EAGAIN or another error indicating that a call to
  65. * accept() has no pending connections to return. */
  66. #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
  67. /** Return true if e is EMFILE or another error indicating that a call to
  68. * accept() has failed because we're out of fds or something. */
  69. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  70. ((e) == WSAEMFILE || (e) == WSAENOBUFS)
  71. /** Return true if e is EADDRINUSE or the local equivalent. */
  72. #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
  73. /** Return true if e is EINTR or the local equivalent */
  74. #define ERRNO_IS_EINTR(e) ((e) == WSAEINTR || 0)
  75. int tor_socket_errno(tor_socket_t sock);
  76. const char *tor_socket_strerror(int e);
  77. #else /* !(defined(_WIN32)) */
  78. #define SOCK_ERRNO(e) e
  79. #if EAGAIN == EWOULDBLOCK
  80. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  81. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || 0)
  82. #else
  83. #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
  84. #endif /* EAGAIN == EWOULDBLOCK */
  85. #define ERRNO_IS_EINTR(e) ((e) == EINTR || 0)
  86. #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  87. #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS || 0)
  88. #define ERRNO_IS_ACCEPT_EAGAIN(e) \
  89. (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
  90. #define ERRNO_IS_RESOURCE_LIMIT(e) \
  91. ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
  92. #define ERRNO_IS_EADDRINUSE(e) (((e) == EADDRINUSE) || 0)
  93. #define tor_socket_errno(sock) (errno)
  94. #define tor_socket_strerror(e) strerror(e)
  95. #endif /* defined(_WIN32) */
  96. #ifdef SOCKET_PRIVATE
  97. #if !defined(HAVE_SOCKETPAIR) || defined(_WIN32) || defined(TOR_UNIT_TESTS)
  98. #define NEED_ERSATZ_SOCKETPAIR
  99. STATIC int tor_ersatz_socketpair(int family, int type, int protocol,
  100. tor_socket_t fd[2]);
  101. #endif
  102. #endif /* defined(COMPAT_PRIVATE) */
  103. #endif