socket.h 5.3 KB

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