socket.h 5.1 KB

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