socket.h 5.1 KB

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