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