Tor 0.4.9.0-alpha-dev
Macros | Functions
socket.h File Reference

Header for socket.c. More...

#include "orconfig.h"
#include "lib/cc/torint.h"
#include "lib/net/nettypes.h"
#include "lib/testsupport/testsupport.h"
#include <errno.h>

Go to the source code of this file.

Macros

#define tor_socket_send(s, buf, len, flags)   send(s, buf, len, flags)
 
#define tor_socket_recv(s, buf, len, flags)   recv(s, buf, len, flags)
 
#define SOCK_ERRNO(e)   e
 
#define ERRNO_IS_EAGAIN(e)   ((e) == EAGAIN || 0)
 
#define ERRNO_IS_EINTR(e)   ((e) == EINTR || 0)
 
#define ERRNO_IS_EINPROGRESS(e)   ((e) == EINPROGRESS || 0)
 
#define ERRNO_IS_CONN_EINPROGRESS(e)   ((e) == EINPROGRESS || 0)
 
#define ERRNO_IS_ACCEPT_EAGAIN(e)    (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)
 
#define ERRNO_IS_RESOURCE_LIMIT(e)    ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
 
#define ERRNO_IS_EADDRINUSE(e)   (((e) == EADDRINUSE) || 0)
 
#define tor_socket_errno(sock)   (errno)
 
#define tor_socket_strerror(e)   strerror(e)
 

Functions

int tor_close_socket_simple (tor_socket_t s)
 
int tor_close_socket (tor_socket_t s)
 
void tor_take_socket_ownership (tor_socket_t s)
 
void tor_release_socket_ownership (tor_socket_t s)
 
tor_socket_t tor_open_socket_with_extensions (int domain, int type, int protocol, int cloexec, int nonblock)
 
tor_socket_t tor_open_socket (int domain, int type, int protocol)
 
tor_socket_t tor_open_socket_nonblocking (int domain, int type, int protocol)
 
tor_socket_t tor_accept_socket (tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len)
 
tor_socket_t tor_accept_socket_nonblocking (tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len)
 
tor_socket_t tor_accept_socket_with_extensions (tor_socket_t sockfd, struct sockaddr *addr, socklen_t *len, int cloexec, int nonblock)
 
tor_socket_t tor_connect_socket (tor_socket_t socket, const struct sockaddr *address, socklen_t address_len)
 
int get_n_open_sockets (void)
 
int tor_getsockname (tor_socket_t socket, struct sockaddr *address, socklen_t *address_len)
 
int tor_addr_from_getsockname (struct tor_addr_t *addr_out, tor_socket_t sock)
 
int set_socket_nonblocking (tor_socket_t socket)
 
int tor_socketpair (int family, int type, int protocol, tor_socket_t fd[2])
 
int network_init (void)
 
void check_network_configuration (bool server_mode)
 
int get_max_sockets (void)
 
void set_max_sockets (int)
 
ssize_t write_all_to_socket (tor_socket_t fd, const char *buf, size_t count)
 
ssize_t read_all_from_socket (tor_socket_t fd, char *buf, size_t count)
 

Detailed Description

Header for socket.c.

Definition in file socket.h.

Macro Definition Documentation

◆ ERRNO_IS_ACCEPT_EAGAIN

#define ERRNO_IS_ACCEPT_EAGAIN (   e)     (ERRNO_IS_EAGAIN(e) || (e) == ECONNABORTED)

Definition at line 106 of file socket.h.

◆ ERRNO_IS_CONN_EINPROGRESS

#define ERRNO_IS_CONN_EINPROGRESS (   e)    ((e) == EINPROGRESS || 0)

Definition at line 105 of file socket.h.

◆ ERRNO_IS_EADDRINUSE

#define ERRNO_IS_EADDRINUSE (   e)    (((e) == EADDRINUSE) || 0)

Definition at line 110 of file socket.h.

◆ ERRNO_IS_EAGAIN

#define ERRNO_IS_EAGAIN (   e)    ((e) == EAGAIN || 0)

Definition at line 99 of file socket.h.

◆ ERRNO_IS_EINPROGRESS

#define ERRNO_IS_EINPROGRESS (   e)    ((e) == EINPROGRESS || 0)

Definition at line 104 of file socket.h.

◆ ERRNO_IS_EINTR

#define ERRNO_IS_EINTR (   e)    ((e) == EINTR || 0)

Definition at line 103 of file socket.h.

◆ ERRNO_IS_RESOURCE_LIMIT

#define ERRNO_IS_RESOURCE_LIMIT (   e)     ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)

Definition at line 108 of file socket.h.

◆ SOCK_ERRNO

#define SOCK_ERRNO (   e)    e

Definition at line 96 of file socket.h.

◆ tor_socket_errno

#define tor_socket_errno (   sock)    (errno)

Definition at line 111 of file socket.h.

◆ tor_socket_recv

#define tor_socket_recv (   s,
  buf,
  len,
  flags 
)    recv(s, buf, len, flags)

Definition at line 52 of file socket.h.

◆ tor_socket_send

#define tor_socket_send (   s,
  buf,
  len,
  flags 
)    send(s, buf, len, flags)

Definition at line 51 of file socket.h.

◆ tor_socket_strerror

#define tor_socket_strerror (   e)    strerror(e)

Definition at line 112 of file socket.h.

Function Documentation

◆ check_network_configuration()

void check_network_configuration ( bool  server_mode)

Warn the user if any system network parameters should be changed.

Definition at line 69 of file socket.c.

◆ get_max_sockets()

int get_max_sockets ( void  )

Return the maximum number of allowed sockets.

Definition at line 98 of file socket.c.

Referenced by fill_socket_values().

◆ get_n_open_sockets()

int get_n_open_sockets ( void  )

Return the number of sockets we currently have opened.

Definition at line 440 of file socket.c.

Referenced by fill_socket_values(), and tor_accept_socket_with_extensions().

◆ network_init()

int network_init ( void  )

Called before we make any calls to network-related functions. (Some operating systems require their network libraries to be initialized.)

Definition at line 41 of file socket.c.

◆ read_all_from_socket()

ssize_t read_all_from_socket ( tor_socket_t  sock,
char *  buf,
size_t  count 
)

Read from sock to buf, until we get count bytes or reach the end of the file. Return the number of bytes read, or -1 on error. Only use if fd is a blocking fd.

Definition at line 587 of file socket.c.

◆ set_max_sockets()

void set_max_sockets ( int  n)

Set the maximum number of allowed sockets to n

Definition at line 105 of file socket.c.

◆ set_socket_nonblocking()

int set_socket_nonblocking ( tor_socket_t  sock)

Turn socket into a nonblocking socket. Return 0 on success, -1 on failure.

Definition at line 560 of file socket.c.

◆ tor_accept_socket()

tor_socket_t tor_accept_socket ( tor_socket_t  sockfd,
struct sockaddr *  addr,
socklen_t *  len 
)

As accept(), but counts the number of open sockets.

Definition at line 358 of file socket.c.

◆ tor_accept_socket_nonblocking()

tor_socket_t tor_accept_socket_nonblocking ( tor_socket_t  sockfd,
struct sockaddr *  addr,
socklen_t *  len 
)

As accept(), but returns a nonblocking socket and counts the number of open sockets.

Definition at line 366 of file socket.c.

◆ tor_accept_socket_with_extensions()

tor_socket_t tor_accept_socket_with_extensions ( tor_socket_t  sockfd,
struct sockaddr *  addr,
socklen_t *  len,
int  cloexec,
int  nonblock 
)

As accept(), but counts the number of open sockets and handles socket creation with either of SOCK_CLOEXEC and SOCK_NONBLOCK specified. cloexec and nonblock should be either 0 or 1 to indicate if the corresponding extension should be used.

Definition at line 377 of file socket.c.

Referenced by tor_accept_socket(), and tor_accept_socket_nonblocking().

◆ tor_addr_from_getsockname()

int tor_addr_from_getsockname ( struct tor_addr_t addr_out,
tor_socket_t  sock 
)

Find the local address associated with the socket sock, and place it in *addr_out. Return 0 on success, -1 on failure.

(As tor_getsockname, but instead places the result in a tor_addr_t.)

Definition at line 544 of file socket.c.

Referenced by client_check_address_changed().

◆ tor_close_socket()

int tor_close_socket ( tor_socket_t  s)

As tor_close_socket_simple(), but keeps track of the number of open sockets. Returns 0 on success, -1 on failure.

Definition at line 217 of file socket.c.

Referenced by alert_sockets_close().

◆ tor_close_socket_simple()

int tor_close_socket_simple ( tor_socket_t  s)

As close(), but guaranteed to work for sockets across platforms (including Windows, where close()ing a socket doesn't work. Returns 0 on success and the socket error code on failure.

Definition at line 148 of file socket.c.

Referenced by tor_close_socket().

◆ tor_connect_socket()

tor_socket_t tor_connect_socket ( tor_socket_t  sock,
const struct sockaddr *  address,
socklen_t  address_len 
)

Mockable wrapper for connect().

Definition at line 251 of file socket.c.

◆ tor_getsockname()

int tor_getsockname ( tor_socket_t  sock,
struct sockaddr *  address,
socklen_t *  address_len 
)

Mockable wrapper for getsockname().

Definition at line 533 of file socket.c.

Referenced by tor_addr_from_getsockname().

◆ tor_open_socket()

tor_socket_t tor_open_socket ( int  domain,
int  type,
int  protocol 
)

As socket(), but counts the number of open sockets.

Definition at line 243 of file socket.c.

Referenced by get_interface_address6_via_udp_socket_hack().

◆ tor_open_socket_nonblocking()

tor_socket_t tor_open_socket_nonblocking ( int  domain,
int  type,
int  protocol 
)

As socket(), but creates a nonblocking socket and counts the number of open sockets.

Definition at line 259 of file socket.c.

Referenced by tor_tls_release_socket().

◆ tor_open_socket_with_extensions()

tor_socket_t tor_open_socket_with_extensions ( int  domain,
int  type,
int  protocol,
int  cloexec,
int  nonblock 
)

As socket(), but counts the number of open sockets and handles socket creation with either of SOCK_CLOEXEC and SOCK_NONBLOCK specified. cloexec and nonblock should be either 0 or 1 to indicate if the corresponding extension should be used.

Definition at line 269 of file socket.c.

◆ tor_release_socket_ownership()

void tor_release_socket_ownership ( tor_socket_t  s)

For socket accounting: declare that we are no longer the owner of the socket s. This will prevent us from overallocating sockets, and prevent us from asserting later when we close the socket s.

Definition at line 348 of file socket.c.

◆ tor_socketpair()

int tor_socketpair ( int  family,
int  type,
int  protocol,
tor_socket_t  fd[2] 
)

Allocate a pair of connected sockets. (Like socketpair(family, type,protocol,fd), but works on systems that don't have socketpair.)

Currently, only (AF_UNIX, SOCK_STREAM, 0) sockets are supported.

Note that on systems without socketpair, this call will fail if localhost is inaccessible (for example, if the networking stack is down). And even if it succeeds, the socket pair will not be able to read while localhost is down later (the socket pair may even close, depending on OS-specific timeouts). The socket pair should work on IPv4-only, IPv6-only, and dual-stack systems, as long as they have the standard localhost addresses.

Returns 0 on success and -errno on failure; do not rely on the value of errno or WSAGetLastError().

Definition at line 469 of file socket.c.

◆ tor_take_socket_ownership()

void tor_take_socket_ownership ( tor_socket_t  s)

For socket accounting: remember that we are the owner of the socket s. This will prevent us from overallocating sockets, and prevent us from asserting later when we close the socket s.

Definition at line 334 of file socket.c.

◆ write_all_to_socket()

ssize_t write_all_to_socket ( tor_socket_t  fd,
const char *  buf,
size_t  count 
)

Write count bytes from buf to sock. Return the number of bytes written, or -1 on error. Only use if fd is a blocking fd.

Definition at line 611 of file socket.c.