Paraslash Audio Streaming | |
About News Download Documentation Development |
Networking-related helper functions. More...
#include "para.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <regex.h>
#include "error.h"
#include "net.h"
#include "string.h"
#include "list.h"
#include "fd.h"
Macros | |
#define | SOCK_DCCP 6 |
Linux socket type. More... | |
#define | DCCP_SOCKOPT_AVAILABLE_CCIDS 12 |
List of supported CCIDs. More... | |
#define | UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)0)->sun_path)) |
The buffer size of the sun_path component of struct sockaddr_un. More... | |
Functions | |
char * | parse_cidr (const char *cidr, char *addr, ssize_t addrlen, int32_t *netmask) |
Parse and validate IPv4 address/netmask string. 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... | |
struct flowopts * | flowopt_new (void) |
Allocate and initialize a flowopt queue. More... | |
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. More... | |
void | flowopt_cleanup (struct flowopts *fo) |
Deallocate all resources of a flowopts structure. 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_addrinfo (unsigned l4type, bool passive, struct addrinfo *ai, struct flowopts *fo) |
Create an active or passive socket. More... | |
int | makesock (unsigned l4type, bool passive, const char *host, uint16_t port_number, struct flowopts *fo) |
Resolve IPv4/IPv6 address and create a ready-to-use active or passive socket. 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... | |
int | generic_max_transport_msg_size (int sockfd) |
Get the maximum transport-layer message size (MMS_S). More... | |
char * | remote_name (int fd) |
Look up the remote side of a connected socket structure. 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... | |
__must_check 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 | dccp_available_ccids (uint8_t **ccid_array) |
Probe the list of DCCP CCIDs configured on this host. 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... | |
ssize_t | send_cred_buffer (int sock, char *buf) |
Send a buffer and the credentials of the current process to a socket. More... | |
int | recv_cred_buffer (int fd, char *buf, size_t size) |
Receive a buffer and the Unix credentials of the sending process. More... | |
Networking-related helper functions.
#define SOCK_DCCP 6 |
Linux socket type.
#define DCCP_SOCKOPT_AVAILABLE_CCIDS 12 |
List of supported CCIDs.
#define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)0)->sun_path)) |
The buffer size of the sun_path component of struct sockaddr_un.
While glibc doesn't define UNIX_PATH_MAX, it documents it has being limited to 108 bytes. On NetBSD it is only 104 bytes though. We trust UNIX_PATH_MAX if it is defined and use the size of the ->sun_path member otherwise. This should be safe everywhere.
char* parse_cidr | ( | const char * | cidr, |
char * | addr, | ||
ssize_t | addrlen, | ||
int32_t * | netmask | ||
) |
Parse and validate IPv4 address/netmask string.
Functions to parse and validate (parts of) URLs.
cidr | Address in CIDR notation |
addr | Copy of the IPv4 address part of cidr |
addrlen | Size of addr in bytes |
netmask | Value of the netmask part in cidr or the default of 32 if not specified. |
References para_atoi32().
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
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:
url | The URL string to take apart. |
host | To return the copied host part of url. |
hostlen | The maximum length of host. |
port | To return the port number (if any) of url. |
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.References para_atoi32().
Referenced by format_url(), and para_listen().
__malloc char* format_url | ( | const char * | url, |
int | default_port | ||
) |
Pretty-print a host/port pair.
url | NULL, or any string accepted by parse_url(). |
default_port | Applies 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.
References make_message(), MAX_HOSTLEN, para_strdup(), and parse_url().
Referenced by generic_com_on(), and generic_sender_status().
const char* stringify_port | ( | int | port, |
const char * | transport | ||
) |
Stringify port number, resolve into service name where defined.
port | 2-byte port number, in host-byte-order. |
transport | Transport protocol name (e.g. "udp", "tcp"), or NULL. |
Referenced by generic_sender_status().
struct flowopts* flowopt_new | ( | void | ) |
Allocate and initialize a flowopt queue.
References alloc().
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.
fo | The flowopt queue to append to. |
lev | Level at which opt resides. |
opt | New option to add. |
name | Stringified name of opt. |
val | The value to set opt to. |
len | Length of val. |
References alloc(), and para_strdup().
void flowopt_cleanup | ( | struct flowopts * | fo | ) |
Deallocate all resources of a flowopts structure.
fo | A 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.
int lookup_address | ( | unsigned | l4type, |
bool | passive, | ||
const char * | host, | ||
int | port_number, | ||
struct addrinfo ** | result | ||
) |
Resolve an IPv4/IPv6 address.
l4type | The layer-4 type (IPPROTO_xxx ). |
passive | Whether AI_PASSIVE should be included as hint. |
host | Remote or local hostname or IPv/6 address string. |
port_number | Used to set the port in each returned address structure. |
result | addrinfo 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.
Referenced by makesock().
int makesock_addrinfo | ( | unsigned | l4type, |
bool | passive, | ||
struct addrinfo * | ai, | ||
struct flowopts * | fo | ||
) |
Create an active or passive socket.
l4type | IPPROTO_TCP , IPPROTO_UDP , or IPPROTO_DCCP . |
passive | Whether to call bind(2) or connect(2). |
ai | Address information as obtained from lookup_address(). |
fo | Socket 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.
E_MAKESOCK
on errors.Referenced by makesock().
int makesock | ( | unsigned | l4type, |
bool | passive, | ||
const char * | host, | ||
uint16_t | port_number, | ||
struct flowopts * | fo | ||
) |
Resolve IPv4/IPv6 address and create a ready-to-use active or passive socket.
Generic socket creation (passive and active sockets).
l4type | The layer-4 type (IPPROTO_xxx ). |
passive | Whether this is a passive or active socket. |
host | Passed to lookup_address(). |
port_number | Passed to lookup_address(). |
fo | Passed 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.
References lookup_address(), makesock_addrinfo(), and PARA_NOTICE_LOG.
Referenced by dccp_available_ccids(), and para_listen().
int para_listen | ( | unsigned | l4type, |
const char * | addr, | ||
uint16_t | port | ||
) |
Create a passive / listening socket.
l4type | The transport-layer type (IPPROTO_xxx ). |
addr | Passed to parse_url() if not NULL. |
port | Ignored if addr contains a port number. |
References BACKLOG, ERRNO_TO_PARA_ERROR, makesock(), MAX_HOSTLEN, PARA_INFO_LOG, and parse_url().
Referenced by generic_com_on(), and para_listen_simple().
int para_listen_simple | ( | unsigned | l4type, |
uint16_t | port | ||
) |
Create a socket which listens on all network addresses.
l4type | See para_listen(). |
port | See para_listen(). |
This is a simple wrapper for para_listen() which passes a NULL pointer as the address information.
References para_listen().
int generic_max_transport_msg_size | ( | int | sockfd | ) |
Get the maximum transport-layer message size (MMS_S).
Determining maximum payload (packet) size.
sockfd | The 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.
References PARA_ERROR_LOG.
char* remote_name | ( | int | fd | ) |
Look up the remote side of a connected socket structure.
Pretty-printing of IPv4/6 socket addresses.
fd | The socket descriptor of the connected socket. |
References PARA_ERROR_LOG.
Referenced by accept_sender_client().
void extract_v4_addr | ( | const struct sockaddr_storage * | ss, |
struct in_addr * | ia | ||
) |
Extract IPv4 or IPv6-mapped-IPv4 address from sockaddr_storage.
ss | Container of IPv4/6 address. |
ia | Extracted IPv4 address (different from 0) or 0 if unsuccessful. |
bool sockaddr_equal | ( | const struct sockaddr * | sa1, |
const struct sockaddr * | sa2 | ||
) |
Compare the address part of IPv4/6 addresses.
sa1 | First address. |
sa2 | Second address. |
__must_check int recv_bin_buffer | ( | int | fd, |
char * | buf, | ||
size_t | size | ||
) |
Receive data from a file descriptor.
fd | The file descriptor. |
buf | The buffer to write the data to. |
size | The size of buf. |
Receive at most size bytes from file descriptor fd.
References ERRNO_TO_PARA_ERROR.
Referenced by recv_buffer(), recv_sb(), and send_callback_request().
int recv_buffer | ( | int | fd, |
char * | buf, | ||
size_t | size | ||
) |
Receive and write terminating NULL byte.
fd | The file descriptor. |
buf | The buffer to write the data to. |
size | The 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.
References recv_bin_buffer().
int para_accept | ( | int | fd, |
void * | addr, | ||
socklen_t | size, | ||
int * | new_fd | ||
) |
Wrapper around the accept system call.
fd | The listening socket. |
addr | Structure which is filled in with the address of the peer socket. |
size | Should contain the size of the structure pointed to by addr. |
new_fd | Result pointer. |
Accept incoming connections on addr, retry if interrupted.
References ERRNO_TO_PARA_ERROR.
Referenced by accept_sender_client().
int dccp_available_ccids | ( | uint8_t ** | ccid_array | ) |
Probe the list of DCCP CCIDs configured on this host.
ccid_array | Pointer to return statically allocated array in. |
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.
int create_local_socket | ( | const char * | name | ) |
Create a socket for local communication and listen on it.
name | The 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.
int connect_local_socket | ( | const char * | name | ) |
Prepare, create, and connect to a Unix domain socket for local communication.
name | The socket pathname. |
This function creates a local socket for sequenced, reliable, two-way, connection-based byte streams.
References ERRNO_TO_PARA_ERROR, and PARA_DEBUG_LOG.
Referenced by send_callback_request().
ssize_t send_cred_buffer | ( | int | sock, |
char * | buf | ||
) |
Send a buffer and the credentials of the current process to a socket.
sock | The file descriptor of the sending socket. |
buf | The zero-terminated buffer to send. |
-E_SENDMSG
is returned.int recv_cred_buffer | ( | int | fd, |
char * | buf, | ||
size_t | size | ||
) |
Receive a buffer and the Unix credentials of the sending process.
fd | The file descriptor of the receiving socket. |
buf | The buffer to store the received message. |
size | The length of buf in bytes. |