paraslash Paraslash Audio Streaming
About   News   Download   Documentation   Development

Macros | Functions
net.h File Reference

exported symbols from net.c More...

Macros

#define SOL_DCCP   269
 Linux socket level. More...
 
#define MAX_HOSTLEN   256
 The maximum length of the host component in an URL. More...
 
#define BACKLOG   10
 Functions to support listening sockets. More...
 
#define DCCP_MAX_HOST_CCIDS   20
 Functions and definitions to support IPPROTO_DCCP. More...
 

Functions

struct flowopts * flowopt_new (void)
 Allocate and initialize a flowopt queue. More...
 
void flowopt_add (struct flowopts *fo, int level, int opt, const char *name, const void *val, int len)
 Append new socket option to flowopt queue. More...
 
void flowopt_cleanup (struct flowopts *fo)
 Deallocate all resources of a flowopts structure. More...
 
char * parse_cidr (const char *cidr, char *addr, ssize_t addrlen, int32_t *netmask)
 Functions to parse and validate (parts of) URLs. More...
 
char * parse_url (const char *url, char *host, ssize_t hostlen, int32_t *port)
 Parse and validate URL string. More...
 
__malloc char * format_url (const char *url, int default_port)
 Pretty-print a host/port pair. More...
 
const char * stringify_port (int port, const char *transport)
 Stringify port number, resolve into service name where defined. More...
 
int lookup_address (unsigned l4type, bool passive, const char *host, int port_number, struct addrinfo **result)
 Resolve an IPv4/IPv6 address. More...
 
int makesock (unsigned l4type, bool passive, const char *host, uint16_t port_number, struct flowopts *fo)
 Generic socket creation (passive and active sockets). More...
 
int makesock_addrinfo (unsigned l4type, bool passive, struct addrinfo *ai, struct flowopts *fo)
 Create an active or passive socket. More...
 
void extract_v4_addr (const struct sockaddr_storage *ss, struct in_addr *ia)
 Extract IPv4 or IPv6-mapped-IPv4 address from sockaddr_storage. More...
 
bool sockaddr_equal (const struct sockaddr *sa1, const struct sockaddr *sa2)
 Compare the address part of IPv4/6 addresses. More...
 
int para_listen (unsigned l4type, const char *addr, uint16_t port)
 Create a passive / listening socket. More...
 
int para_listen_simple (unsigned l4type, uint16_t port)
 Create a socket which listens on all network addresses. More...
 
char * remote_name (int sockfd)
 Pretty-printing of IPv4/6 socket addresses. More...
 
int generic_max_transport_msg_size (int sockfd)
 Determining maximum payload (packet) size. More...
 
int recv_bin_buffer (int fd, char *buf, size_t size)
 Receive data from a file descriptor. More...
 
int recv_buffer (int fd, char *buf, size_t size)
 Receive and write terminating NULL byte. More...
 
int para_accept (int fd, void *addr, socklen_t size, int *new_fd)
 Wrapper around the accept system call. More...
 
int create_local_socket (const char *name)
 Create a socket for local communication and listen on it. More...
 
int connect_local_socket (const char *name)
 Prepare, create, and connect to a Unix domain socket for local communication. More...
 
int recv_cred_buffer (int, char *, size_t)
 Receive a buffer and the Unix credentials of the sending process. More...
 
ssize_t send_cred_buffer (int, char *)
 Send a buffer and the credentials of the current process to a socket. More...
 
int dccp_available_ccids (uint8_t **ccid_array)
 Probe the list of DCCP CCIDs configured on this host. More...
 

Detailed Description

exported symbols from net.c

Macro Definition Documentation

◆ SOL_DCCP

#define SOL_DCCP   269

Linux socket level.

◆ MAX_HOSTLEN

#define MAX_HOSTLEN   256

The maximum length of the host component in an URL.

◆ BACKLOG

#define BACKLOG   10

Functions to support listening sockets.

How many pending connections queue of a listening server will hold.

◆ DCCP_MAX_HOST_CCIDS

#define DCCP_MAX_HOST_CCIDS   20

Functions and definitions to support IPPROTO_DCCP.

Hardcoded maximum number of separate CCID modules compiled into a host.

Function Documentation

◆ flowopt_new()

struct flowopts* flowopt_new ( void  )

Allocate and initialize a flowopt queue.

Returns
A new structure to be passed to flowopt_add(). It is automatically deallocated in makesock().

References alloc().

◆ flowopt_add()

void flowopt_add ( struct flowopts *  fo,
int  lev,
int  opt,
const char *  name,
const void *  val,
int  len 
)

Append new socket option to flowopt queue.

Parameters
foThe flowopt queue to append to.
levLevel at which opt resides.
optNew option to add.
nameStringified name of opt.
valThe value to set opt to.
lenLength of val.
See also
setsockopt(2).

References alloc(), and para_strdup().

◆ flowopt_cleanup()

void flowopt_cleanup ( struct flowopts *  fo)

Deallocate all resources of a flowopts structure.

Parameters
foA pointer as returned from flowopt_new().

It's OK to pass NULL here in which case the function does nothing.

References list_for_each_entry_safe.

◆ parse_cidr()

char* parse_cidr ( const char *  cidr,
char *  addr,
ssize_t  addrlen,
int32_t *  netmask 
)

Functions to parse and validate (parts of) URLs.

Functions to parse and validate (parts of) URLs.

Parameters
cidrAddress in CIDR notation
addrCopy of the IPv4 address part of cidr
addrlenSize of addr in bytes
netmaskValue of the netmask part in cidr or the default of 32 if not specified.
Returns
Pointer to addr if successful, NULL on error.
See also
RFC 4632.

References para_atoi32().

◆ parse_url()

char* parse_url ( const char *  url,
char *  host,
ssize_t  hostlen,
int32_t *  port 
)

Parse and validate URL string.

The URL syntax is loosely based on RFC 3986, supporting one of

  • "["host"]"[:port] for native IPv6 addresses and
  • host[:port] for IPv4 hostnames and DNS names.

Native IPv6 addresses must be enclosed in square brackets, since otherwise there is an ambiguity with the port separator ‘:’. The 'port' part is always considered to be a number; if absent, it is set to -1, to indicate that a default port is to be used.

The following are valid examples:

  • 10.10.1.1
  • 10.10.1.2:8000
  • localhost
  • localhost:8001
  • [::1]:8000
  • [badc0de::1]
Parameters
urlThe URL string to take apart.
hostTo return the copied host part of url.
hostlenThe maximum length of host.
portTo return the port number (if any) of url.
Returns
Pointer to host, or NULL if failed. If NULL is returned, host and port are undefined. If no port number was present in url, port is set to -1.
See also
RFC 3986, 3.2.2/3.2.3.

References para_atoi32().

Referenced by format_url(), and para_listen().

◆ format_url()

__malloc char* format_url ( const char *  url,
int  default_port 
)

Pretty-print a host/port pair.

Parameters
urlNULL, or any string accepted by parse_url().
default_portApplies if url has no port.

If the url argument is NULL, the function returns the string 0.0.0.0:default_port. Otherwise it calls parse_url() to check the syntax of the input string given by url. On errors the string "?" is returned. Otherwise, if url contains a port, a copy of url is returned. If no port was supplied, a colon and the default port are appended to url.

Returns
In all cases the returned string is a allocated with malloc(3) and has to be freed by the caller.

References make_message(), MAX_HOSTLEN, para_strdup(), and parse_url().

Referenced by generic_com_on(), and generic_sender_status().

◆ stringify_port()

const char* stringify_port ( int  port,
const char *  transport 
)

Stringify port number, resolve into service name where defined.

Parameters
port2-byte port number, in host-byte-order.
transportTransport protocol name (e.g. "udp", "tcp"), or NULL.
Returns
Pointer to static result buffer.
See also
getservbyport(3), services(5), nsswitch.conf(5).

Referenced by generic_sender_status().

◆ lookup_address()

int lookup_address ( unsigned  l4type,
bool  passive,
const char *  host,
int  port_number,
struct addrinfo **  result 
)

Resolve an IPv4/IPv6 address.

Parameters
l4typeThe layer-4 type (IPPROTO_xxx).
passiveWhether AI_PASSIVE should be included as hint.
hostRemote or local hostname or IPv/6 address string.
port_numberUsed to set the port in each returned address structure.
resultaddrinfo structures are returned here.

The interpretation of host depends on the value of passive. On a passive socket host is interpreted as an interface IPv4/6 address (can be left NULL). On an active socket, host is the peer DNS name or IPv4/6 address to connect to.

Returns
Standard.
See also
getaddrinfo(3).

Referenced by makesock().

◆ makesock()

int makesock ( unsigned  l4type,
bool  passive,
const char *  host,
uint16_t  port_number,
struct flowopts *  fo 
)

Generic socket creation (passive and active sockets).

Generic socket creation (passive and active sockets).

Parameters
l4typeThe layer-4 type (IPPROTO_xxx).
passiveWhether this is a passive or active socket.
hostPassed to lookup_address().
port_numberPassed to lookup_address().
foPassed to makesock_addrinfo().

This creates a ready-made IPv4/v6 socket structure after looking up the necessary parameters. The function first calls lookup_address() and passes the address information to makesock_addrinfo() to create and initialize the socket.

Returns
The newly created file descriptor on success, a negative error code on failure.
See also
lookup_address(), makesock_addrinfo().

References lookup_address(), makesock_addrinfo(), and PARA_NOTICE_LOG.

Referenced by dccp_available_ccids(), and para_listen().

◆ makesock_addrinfo()

int makesock_addrinfo ( unsigned  l4type,
bool  passive,
struct addrinfo *  ai,
struct flowopts *  fo 
)

Create an active or passive socket.

Parameters
l4typeIPPROTO_TCP, IPPROTO_UDP, or IPPROTO_DCCP.
passiveWhether to call bind(2) or connect(2).
aiAddress information as obtained from lookup_address().
foSocket options to be set before making the connection.

bind(2) is called on passive sockets, and connect(2) on active sockets. The algorithm tries all possible address combinations until it succeeds. If fo is supplied, options are set but cleanup must be performed in the caller.

Returns
File descriptor on success, E_MAKESOCK on errors.
See also
lookup_address(), makesock(), ip(7), ipv6(7), bind(2), connect(2).

Referenced by makesock().

◆ extract_v4_addr()

void extract_v4_addr ( const struct sockaddr_storage *  ss,
struct in_addr *  ia 
)

Extract IPv4 or IPv6-mapped-IPv4 address from sockaddr_storage.

Parameters
ssContainer of IPv4/6 address.
iaExtracted IPv4 address (different from 0) or 0 if unsuccessful.
See also
RFC 3493.

◆ sockaddr_equal()

bool sockaddr_equal ( const struct sockaddr *  sa1,
const struct sockaddr *  sa2 
)

Compare the address part of IPv4/6 addresses.

Parameters
sa1First address.
sa2Second address.
Returns
True iff the IP address of sa1 and sa2 match.

◆ para_listen()

int para_listen ( unsigned  l4type,
const char *  addr,
uint16_t  port 
)

Create a passive / listening socket.

Parameters
l4typeThe transport-layer type (IPPROTO_xxx).
addrPassed to parse_url() if not NULL.
portIgnored if addr contains a port number.
Returns
Positive integer (socket descriptor) on success, negative value otherwise.
See also
makesock(), ip(7), ipv6(7), bind(2), listen(2).

References BACKLOG, ERRNO_TO_PARA_ERROR, makesock(), MAX_HOSTLEN, PARA_INFO_LOG, and parse_url().

Referenced by generic_com_on(), and para_listen_simple().

◆ para_listen_simple()

int para_listen_simple ( unsigned  l4type,
uint16_t  port 
)

Create a socket which listens on all network addresses.

Parameters
l4typeSee para_listen().
portSee para_listen().

This is a simple wrapper for para_listen() which passes a NULL pointer as the address information.

Returns
See para_listen().

References para_listen().

◆ remote_name()

char* remote_name ( int  fd)

Pretty-printing of IPv4/6 socket addresses.

Pretty-printing of IPv4/6 socket addresses.

Parameters
fdThe socket descriptor of the connected socket.
Returns
A static character string identifying hostname and port of the chosen side in numeric host:port format.
See also
getsockname(2), getpeername(2), parse_url(), getnameinfo(3), services(5), nsswitch.conf(5).

References PARA_ERROR_LOG.

Referenced by accept_sender_client().

◆ generic_max_transport_msg_size()

int generic_max_transport_msg_size ( int  sockfd)

Determining maximum payload (packet) size.

Determining maximum payload (packet) size.

Parameters
sockfdThe socket file descriptor.

The socket must be connected. See RFC 1122, 3.3.3. If the protocol family could not be determined, AF_INET is assumed.

Returns
The maximum message size of the address family type.

References PARA_ERROR_LOG.

◆ recv_bin_buffer()

int recv_bin_buffer ( int  fd,
char *  buf,
size_t  size 
)

Receive data from a file descriptor.

Parameters
fdThe file descriptor.
bufThe buffer to write the data to.
sizeThe size of buf.

Receive at most size bytes from file descriptor fd.

Returns
The number of bytes received on success, negative on errors, zero if the peer has performed an orderly shutdown.
See also
recv(2).

References ERRNO_TO_PARA_ERROR.

Referenced by recv_buffer(), recv_sb(), and send_callback_request().

◆ recv_buffer()

int recv_buffer ( int  fd,
char *  buf,
size_t  size 
)

Receive and write terminating NULL byte.

Parameters
fdThe file descriptor.
bufThe buffer to write the data to.
sizeThe size of buf.

Read at most size - 1 bytes from file descriptor fd and write a NULL byte at the end of the received data.

Returns
The return value of the underlying call to recv_bin_buffer().
See also
recv_bin_buffer()

References recv_bin_buffer().

◆ para_accept()

int para_accept ( int  fd,
void *  addr,
socklen_t  size,
int *  new_fd 
)

Wrapper around the accept system call.

Parameters
fdThe listening socket.
addrStructure which is filled in with the address of the peer socket.
sizeShould contain the size of the structure pointed to by addr.
new_fdResult pointer.

Accept incoming connections on addr, retry if interrupted.

Returns
Negative on errors, zero if no connections are present to be accepted, one otherwise.
See also
accept(2).

References ERRNO_TO_PARA_ERROR.

Referenced by accept_sender_client().

◆ create_local_socket()

int create_local_socket ( const char *  name)

Create a socket for local communication and listen on it.

Parameters
nameThe socket pathname.

This function creates a passive local socket for sequenced, reliable, two-way, connection-based byte streams. The socket file descriptor is set to nonblocking mode and listen(2) is called to prepare the socket for accepting incoming connection requests.

Returns
The file descriptor on success, negative error code on failure.
See also
socket(2),
bind(2),
chmod(2), listen(2), unix(7).

◆ connect_local_socket()

int connect_local_socket ( const char *  name)

Prepare, create, and connect to a Unix domain socket for local communication.

Parameters
nameThe socket pathname.

This function creates a local socket for sequenced, reliable, two-way, connection-based byte streams.

Returns
The file descriptor of the connected socket on success, negative on errors.
See also
create_local_socket(), unix(7), connect(2).

References ERRNO_TO_PARA_ERROR, and PARA_DEBUG_LOG.

Referenced by send_callback_request().

◆ recv_cred_buffer()

int recv_cred_buffer ( int  fd,
char *  buf,
size_t  size 
)

Receive a buffer and the Unix credentials of the sending process.

Parameters
fdThe file descriptor of the receiving socket.
bufThe buffer to store the received message.
sizeThe length of buf in bytes.
Returns
Negative on errors, the user id of the sending process on success.
See also
send_cred_buffer and the references given there.

◆ send_cred_buffer()

ssize_t send_cred_buffer ( int  sock,
char *  buf 
)

Send a buffer and the credentials of the current process to a socket.

Parameters
sockThe file descriptor of the sending socket.
bufThe zero-terminated buffer to send.
Returns
On success, this call returns the number of bytes sent. On errors, -E_SENDMSG is returned.
See also
recv_cred_buffer, sendmsg(2), socket(7), unix(7).

◆ dccp_available_ccids()

int dccp_available_ccids ( uint8_t **  ccid_array)

Probe the list of DCCP CCIDs configured on this host.

Parameters
ccid_arrayPointer to return statically allocated array in.
Returns
Number of elements returned in ccid_array or error.

NB: This feature is only available on Linux > 2.6.30; on older kernels ENOPROTOOPT ("Protocol not available") will be returned.

< Linux socket level.

< List of supported CCIDs.

References DCCP_MAX_HOST_CCIDS, DCCP_SOCKOPT_AVAILABLE_CCIDS, ERRNO_TO_PARA_ERROR, makesock(), PARA_ERROR_LOG, and SOL_DCCP.