tor  0.4.1.0-alpha-dev
socket.h
Go to the documentation of this file.
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 
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"
18 
19 #include <errno.h>
20 
21 struct sockaddr;
22 
24 MOCK_DECL(int, tor_close_socket, (tor_socket_t s));
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);
35  struct sockaddr *addr,
36  socklen_t *len);
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 
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 
73 #define SOCK_ERRNO(e) WSA##e
74 
75 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
76 
77 #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
78 
80 #define ERRNO_IS_CONN_EINPROGRESS(e) \
81  ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
82 
84 #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
85 
87 #define ERRNO_IS_RESOURCE_LIMIT(e) \
88  ((e) == WSAEMFILE || (e) == WSAENOBUFS)
89 
90 #define ERRNO_IS_EADDRINUSE(e) ((e) == WSAEADDRINUSE)
91 
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
int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2])
Definition: socket.c:470
Integer definitions used throughout Tor.
void tor_release_socket_ownership(tor_socket_t s)
Definition: socket.c:349
int network_init(void)
Definition: socket.c:42
int tor_close_socket_simple(tor_socket_t s)
Definition: socket.c:149
int get_n_open_sockets(void)
Definition: socket.c:441
void set_max_sockets(int)
Definition: socket.c:106
int tor_addr_from_getsockname(struct tor_addr_t *addr_out, tor_socket_t sock)
Definition: socket.c:545
int set_socket_nonblocking(tor_socket_t socket)
Definition: socket.c:561
void tor_take_socket_ownership(tor_socket_t s)
Definition: socket.c:335
void check_network_configuration(bool server_mode)
Definition: socket.c:70
tor_socket_t tor_open_socket_nonblocking(int domain, int type, int protocol)
Definition: socket.c:260
Declarations for types used throughout the Tor networking system.
tor_socket_t tor_accept_socket_nonblocking(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len)
Definition: socket.c:367
#define tor_socket_t
Definition: nettypes.h:36
tor_socket_t tor_open_socket_with_extensions(int domain, int type, int protocol, int cloexec, int nonblock)
Definition: socket.c:270
ssize_t write_all_to_socket(tor_socket_t fd, const char *buf, size_t count)
Definition: socket.c:612
tor_socket_t tor_accept_socket(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len)
Definition: socket.c:359
tor_socket_t tor_accept_socket_with_extensions(tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len, int cloexec, int nonblock)
Definition: socket.c:378
ssize_t read_all_from_socket(tor_socket_t fd, char *buf, size_t count)
Definition: socket.c:588
Macros to implement mocking and selective exposure for the test code.
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
int get_max_sockets(void)
Definition: socket.c:99