FFmpeg
Loading...
Searching...
No Matches
network.c File Reference
#include <string.h>
#include "config.h"
#include "config_components.h"
#include "libavutil/attributes.h"
#include <fcntl.h>
#include "network.h"
#include "tls.h"
#include "url.h"
#include "libavutil/avassert.h"
#include "libavutil/mem.h"
#include "libavutil/time.h"

Go to the source code of this file.

Data Structures

struct  ConnectionAttempt
 

Macros

#define NEXT_ATTEMPT_DELAY_MS   200
 

Functions

int ff_tls_init (void)
 
void ff_tls_deinit (void)
 
int ff_network_init (void)
 Initialize the network subsystem.
 
int ff_network_wait_fd (int fd, int write)
 
int ff_network_wait_fd_timeout (int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
 This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds Uses ff_network_wait_fd in a loop.
 
int ff_network_wakeup_open (FFNetWakeup *w)
 Create a wakeup channel.
 
int ff_network_wakeup_signal (FFNetWakeup *w)
 Wake the thread blocked in ff_network_wait_fd_wakeup() on this channel, or the next one that waits on it: the signal is not consumed, so a signal sent before the wait starts is not lost.
 
void ff_network_wakeup_close (FFNetWakeup *w)
 Close the channel.
 
int ff_network_wait_fd_wakeup (int fd, int write, int timeout_ms, FFNetWakeup *w)
 Like ff_network_wait_fd(), but also returns as soon as the wakeup channel is signaled, and with a caller chosen timeout.
 
int ff_network_sleep_interruptible (int64_t timeout, AVIOInterruptCB *int_cb)
 Waits for up to 'timeout' microseconds.
 
void ff_network_close (void)
 
int ff_is_multicast_address (struct sockaddr *addr)
 
static int ff_poll_interrupt (struct pollfd *p, nfds_t nfds, int timeout, AVIOInterruptCB *cb)
 
int ff_socket (int af, int type, int proto, void *logctx)
 
int ff_listen (int fd, const struct sockaddr *addr, socklen_t addrlen, void *logctx)
 Bind to a file descriptor to an address without accepting connections.
 
int ff_accept (int fd, int timeout, URLContext *h)
 Poll for a single connection on the passed file descriptor.
 
int ff_listen_bind (int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h)
 Bind to a file descriptor and poll for a connection.
 
int ff_listen_connect (int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next)
 Connect to a file descriptor and poll for result.
 
static void interleave_addrinfo (struct addrinfo *base)
 
static void print_address_list (void *ctx, const struct addrinfo *addr, const char *title)
 
static int start_connect_attempt (struct ConnectionAttempt *attempt, struct addrinfo **ptr, int timeout_ms, URLContext *h, int(*customize_fd)(void *, int, int), void *customize_ctx)
 
int ff_connect_parallel (struct addrinfo *addrs, int timeout_ms_per_address, int parallel, URLContext *h, int *fd, int(*customize_fd)(void *, int, int), void *customize_ctx)
 Connect to any of the given addrinfo addresses, with multiple attempts running in parallel.
 
static int match_host_pattern (const char *pattern, const char *hostname)
 
int ff_http_match_no_proxy (const char *no_proxy, const char *hostname)
 
void ff_log_net_error (void *ctx, int level, const char *prefix)
 

Macro Definition Documentation

◆ NEXT_ATTEMPT_DELAY_MS

#define NEXT_ATTEMPT_DELAY_MS   200

Definition at line 529 of file network.c.

Referenced by ff_connect_parallel().

Function Documentation

◆ ff_tls_init()

int ff_tls_init ( void )

Definition at line 42 of file network.c.

Referenced by avformat_network_init().

◆ ff_tls_deinit()

void ff_tls_deinit ( void )

Definition at line 52 of file network.c.

Referenced by avformat_network_deinit().

◆ ff_network_init()

int ff_network_init ( void )

Initialize the network subsystem.

On Windows, this calls WSAStartup().

Returns
0 on success, a negative AVERROR code on failure.

Definition at line 66 of file network.c.

Referenced by avformat_network_init(), rtsp_listen(), sap_read_header(), sap_write_header(), and url_alloc_for_protocol().

◆ ff_network_wait_fd()

int ff_network_wait_fd ( int fd,
int write )

◆ ff_network_wait_fd_timeout()

int ff_network_wait_fd_timeout ( int fd,
int write,
int64_t timeout,
AVIOInterruptCB * int_cb )

This works similarly to ff_network_wait_fd, but waits up to 'timeout' microseconds Uses ff_network_wait_fd in a loop.

Parameters
fdSocket descriptor
writeSet 1 to wait for socket able to be read, 0 to be written
timeoutTimeout interval, in microseconds. Actual precision is 100000 mcs, due to ff_network_wait_fd usage
int_cbInterrupt callback, is checked before each ff_network_wait_fd call
Returns
0 if data can be read/written, AVERROR(ETIMEDOUT) if timeout expired, or negative error code

Definition at line 86 of file network.c.

Referenced by amqp_proto_read(), amqp_proto_write(), tcp_read(), and tcp_write().

◆ ff_network_wakeup_open()

int ff_network_wakeup_open ( FFNetWakeup * w)

Create a wakeup channel.

On Windows ff_network_init() must have been called first.

Returns
0 on success, a negative AVERROR code on failure

Definition at line 106 of file network.c.

Referenced by udp_open().

◆ ff_network_wakeup_signal()

int ff_network_wakeup_signal ( FFNetWakeup * w)

Wake the thread blocked in ff_network_wait_fd_wakeup() on this channel, or the next one that waits on it: the signal is not consumed, so a signal sent before the wait starts is not lost.

Only the first call has an effect; the function may be called any number of times, from any thread.

Returns
0 on success, a negative AVERROR code if the first call failed to write to the channel

Definition at line 162 of file network.c.

Referenced by udp_close().

◆ ff_network_wakeup_close()

void ff_network_wakeup_close ( FFNetWakeup * w)

Close the channel.

Call it after the waiting thread has been joined and no thread can signal any more.

Definition at line 185 of file network.c.

Referenced by udp_close(), and udp_open().

◆ ff_network_wait_fd_wakeup()

int ff_network_wait_fd_wakeup ( int fd,
int write,
int timeout_ms,
FFNetWakeup * w )

Like ff_network_wait_fd(), but also returns as soon as the wakeup channel is signaled, and with a caller chosen timeout.

Parameters
fdSocket descriptor
writeSet 1 to wait for the socket to be writable, 0 for readable
timeout_msTimeout in milliseconds, or -1 to wait indefinitely
wWakeup channel to watch, must have been opened
Returns
0 if fd is ready, AVERROR(EAGAIN) if the timeout expired, AVERROR_EXIT if w was signaled (also when fd is ready), or a negative error code

Definition at line 194 of file network.c.

◆ ff_network_sleep_interruptible()

int ff_network_sleep_interruptible ( int64_t timeout,
AVIOInterruptCB * int_cb )

Waits for up to 'timeout' microseconds.

If the usert's int_cb is set and triggered, return before that.

Parameters
timeoutTimeout in microseconds. Maybe have lower actual precision.
int_cbInterrupt callback, is checked regularly.
Returns
AVERROR(ETIMEDOUT) if timeout expirted, AVERROR_EXIT if interrupted by int_cb

Definition at line 221 of file network.c.

Referenced by http_open_cnx(), and http_read_stream().

◆ ff_network_close()

◆ ff_is_multicast_address()

int ff_is_multicast_address ( struct sockaddr * addr)

Definition at line 268 of file network.c.

Referenced by ff_udp_set_remote_addr(), and ff_udp_set_remote_url().

◆ ff_poll_interrupt()

static int ff_poll_interrupt ( struct pollfd * p,
nfds_t nfds,
int timeout,
AVIOInterruptCB * cb )
static

Definition at line 282 of file network.c.

Referenced by ff_accept(), ff_connect_parallel(), and ff_listen_connect().

◆ ff_socket()

int ff_socket ( int af,
int type,
int proto,
void * logctx )

◆ ff_listen()

int ff_listen ( int fd,
const struct sockaddr * addr,
socklen_t addrlen,
void * logctx )

Bind to a file descriptor to an address without accepting connections.

Parameters
fdFirst argument of bind().
addrSecond argument of bind().
addrlenThird argument of bind().
Returns
0 on success or an AVERROR on failure.

Definition at line 333 of file network.c.

Referenced by ff_listen_bind(), and tcp_open().

◆ ff_accept()

int ff_accept ( int fd,
int timeout,
URLContext * h )

Poll for a single connection on the passed file descriptor.

Parameters
fdThe listening socket file descriptor.
timeoutPolling timeout in milliseconds.
hURLContext providing interrupt check callback and logging context.
Returns
A non-blocking file descriptor on success or an AVERROR on failure.

Definition at line 351 of file network.c.

Referenced by ff_listen_bind(), and tcp_accept().

◆ ff_listen_bind()

int ff_listen_bind ( int fd,
const struct sockaddr * addr,
socklen_t addrlen,
int timeout,
URLContext * h )

Bind to a file descriptor and poll for a connection.

Parameters
fdFirst argument of bind().
addrSecond argument of bind().
addrlenThird argument of bind().
timeoutPolling timeout in milliseconds.
hURLContext providing interrupt check callback and logging context.
Returns
A non-blocking file descriptor on success or an AVERROR on failure.

Definition at line 369 of file network.c.

Referenced by sctp_open(), tcp_open(), and unix_open().

◆ ff_listen_connect()

int ff_listen_connect ( int fd,
const struct sockaddr * addr,
socklen_t addrlen,
int timeout,
URLContext * h,
int will_try_next )

Connect to a file descriptor and poll for result.

Parameters
fdFirst argument of connect(), will be set as non-blocking.
addrSecond argument of connect().
addrlenThird argument of connect().
timeoutPolling timeout in milliseconds.
hURLContext providing interrupt check callback and logging context.
will_try_nextWhether the caller will try to connect to another address for the same host name, affecting the form of logged errors.
Returns
0 on success, AVERROR on failure.

Definition at line 381 of file network.c.

Referenced by sctp_open(), and unix_open().

◆ interleave_addrinfo()

static void interleave_addrinfo ( struct addrinfo * base)
static

Definition at line 425 of file network.c.

Referenced by ff_connect_parallel().

◆ print_address_list()

static void print_address_list ( void * ctx,
const struct addrinfo * addr,
const char * title )
static

Definition at line 456 of file network.c.

Referenced by ff_connect_parallel().

◆ start_connect_attempt()

static int start_connect_attempt ( struct ConnectionAttempt * attempt,
struct addrinfo ** ptr,
int timeout_ms,
URLContext * h,
int(* customize_fd )(void *, int, int),
void * customize_ctx )
static

Definition at line 478 of file network.c.

Referenced by ff_connect_parallel().

◆ ff_connect_parallel()

int ff_connect_parallel ( struct addrinfo * addrs,
int timeout_ms_per_address,
int parallel,
URLContext * h,
int * fd,
int(* customize_fd )(void *, int, int),
void * customize_ctx )

Connect to any of the given addrinfo addresses, with multiple attempts running in parallel.

Parameters
addrsThe list of addresses to try to connect to. This list will be mutated internally, but the list head will remain as such, so this doesn't affect the caller freeing the list afterwards.
timeout_ms_per_addressThe number of milliseconds to wait for each connection attempt. Since multiple addresses are tried, some of them in parallel, the total run time will at most be timeout_ms_per_address*ceil(nb_addrs/parallel) + (parallel - 1) * NEXT_ATTEMPT_DELAY_MS.
parallelThe maximum number of connections to attempt in parallel. This is limited to an internal maximum capacity.
hURLContext providing interrupt check callback and logging context.
fdIf successful, the connected socket is returned here.
customize_fdFunction that will be called for each socket created, to allow the caller to set socket options before calling connect() on it, may be NULL.
customize_ctxContext parameter passed to customize_fd.
Returns
0 on success, AVERROR on failure.

Definition at line 531 of file network.c.

Referenced by tcp_open().

◆ match_host_pattern()

static int match_host_pattern ( const char * pattern,
const char * hostname )
static

Definition at line 652 of file network.c.

Referenced by ff_http_match_no_proxy().

◆ ff_http_match_no_proxy()

int ff_http_match_no_proxy ( const char * no_proxy,
const char * hostname )

Definition at line 676 of file network.c.

Referenced by ff_tls_open_underlying(), http_open_cnx_internal(), and test().

◆ ff_log_net_error()

void ff_log_net_error ( void * ctx,
int level,
const char * prefix )