24#include "config_components.h"
27#if CONFIG_TLS_PROTOCOL && CONFIG_OPENSSL
28#include <openssl/opensslv.h>
33#include <sys/eventfd.h>
44#if CONFIG_TLS_PROTOCOL
54#if CONFIG_TLS_PROTOCOL
71 if (WSAStartup(MAKEWORD(1,1), &wsaData))
79 int ev = write ? POLLOUT : POLLIN;
80 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
112 struct sockaddr_in addr = { .sin_family = AF_INET };
113 socklen_t
len =
sizeof(addr);
120 if (bind(fd, (
struct sockaddr *)&addr,
sizeof(addr)) < 0 ||
121 getsockname(fd, (
struct sockaddr *)&addr, &
len) < 0 ||
122 connect(fd, (
struct sockaddr *)&addr,
sizeof(addr)) < 0) {
128 w->rfd =
w->wfd = fd;
130 int fd = eventfd(0, EFD_CLOEXEC);
135 w->rfd =
w->wfd = fd;
143 for (
int i = 0;
i < 2;
i++) {
144 if (fcntl(fds[
i], F_SETFD, FD_CLOEXEC) < 0) {
167 const uint64_t one = 1;
177 ret = send(
w->wfd, (
const char *)&one,
sizeof(one), 0);
179 ret = write(
w->wfd, &one,
sizeof(one));
187 if (
w->wfd !=
w->rfd)
191 w->rfd =
w->wfd = -1;
197 int ev = write ? POLLOUT : POLLIN;
198 struct pollfd p[2] = {
199 { .fd =
w->rfd, .events = POLLIN },
200 { .fd = fd, .events = ev },
211 ret = poll(p, 2, timeout_ms);
215 if (p[0].revents & POLLIN)
218 return p[1].revents & (ev | POLLERR | POLLHUP) ? 0 :
AVERROR(EAGAIN);
249 int err = WSAGetLastError();
255 case WSAEPROTONOSUPPORT:
256 return AVERROR(EPROTONOSUPPORT);
259 case WSAECONNREFUSED:
270 if (addr->sa_family == AF_INET) {
271 return IN_MULTICAST(ntohl(((
struct sockaddr_in *)addr)->sin_addr.s_addr));
273#if HAVE_STRUCT_SOCKADDR_IN6
274 if (addr->sa_family == AF_INET6) {
299 }
while (timeout <= 0 || runs-- > 0);
311 fd = socket(af,
type | SOCK_CLOEXEC, proto);
312 if (fd == -1 && errno == EINVAL)
315 fd = socket(af,
type, proto);
318 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
325 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(
int){1},
sizeof(int))) {
334 socklen_t addrlen,
void *logctx)
338 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(reuse))) {
341 ret = bind(fd, addr, addrlen);
354 struct pollfd lp = { fd, POLLIN, 0 };
385 struct pollfd p = {fd, POLLOUT, 0};
392 while ((
ret = connect(fd, addr, addrlen))) {
404 optlen =
sizeof(
ret);
405 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &
ret, &optlen))
411 "Connection to %s failed (%s), trying next address\n",
435 if (cur ==
base->ai_next) {
439 next = &
base->ai_next;
459 char hostbuf[100], portbuf[20];
463 hostbuf,
sizeof(hostbuf), portbuf,
sizeof(portbuf),
479 struct addrinfo **ptr,
int timeout_ms,
481 int (*
customize_fd)(
void *,
int,
int),
void *customize_ctx)
499 closesocket(attempt->
fd);
510 closesocket(attempt->
fd);
519 closesocket(attempt->
fd);
529#define NEXT_ATTEMPT_DELAY_MS 200
533 int (*
customize_fd)(
void *,
int,
int),
void *customize_ctx)
536 struct pollfd pfd[3];
537 int nb_attempts = 0,
i, j;
541 char hostbuf[100], portbuf[20];
553 while (nb_attempts > 0 || addrs) {
555 if (nb_attempts < parallel && addrs) {
557 hostbuf,
sizeof(hostbuf), portbuf,
sizeof(portbuf),
562 timeout_ms_per_address,
h,
570 for (
i = 0;
i < nb_attempts;
i++)
571 closesocket(attempts[
i].fd);
572 *fd = attempts[nb_attempts].
fd;
575 pfd[nb_attempts].fd = attempts[nb_attempts].
fd;
576 pfd[nb_attempts].events = POLLOUT;
586 if (nb_attempts < parallel && addrs)
587 next_deadline_us =
FFMIN(next_deadline_us, next_attempt_us);
590 &
h->interrupt_callback);
591 if (last_err < 0 && last_err !=
AVERROR(ETIMEDOUT))
595 for (
i = 0;
i < nb_attempts;
i++) {
597 if (pfd[
i].revents) {
600 optlen =
sizeof(last_err);
601 if (getsockopt(attempts[
i].fd, SOL_SOCKET, SO_ERROR, &last_err, &optlen))
603 else if (last_err != 0)
608 for (j = 0; j < nb_attempts; j++)
610 closesocket(attempts[j].fd);
611 *fd = attempts[
i].
fd;
613 hostbuf,
sizeof(hostbuf), portbuf,
sizeof(portbuf),
628 hostbuf,
sizeof(hostbuf), portbuf,
sizeof(portbuf),
631 "failed: %s\n", hostbuf, portbuf,
av_err2str(last_err));
632 closesocket(attempts[
i].fd);
633 memmove(&attempts[
i], &attempts[
i + 1],
634 (nb_attempts -
i - 1) *
sizeof(*attempts));
635 memmove(&pfd[
i], &pfd[
i + 1],
636 (nb_attempts -
i - 1) *
sizeof(*pfd));
641 for (
i = 0;
i < nb_attempts;
i++)
642 closesocket(attempts[
i].fd);
644 last_err =
AVERROR(ECONNREFUSED);
655 if (!strcmp(pattern,
"*"))
658 if (pattern[0] ==
'*')
660 if (pattern[0] ==
'.')
662 len_p = strlen(pattern);
663 len_h = strlen(hostname);
667 if (!strcmp(pattern, &hostname[len_h - len_p])) {
670 if (hostname[len_h - len_p - 1] ==
'.')
689 char *sep, *next =
NULL;
690 start += strspn(start,
" ,");
691 sep = start + strcspn(start,
" ,");
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
#define i(width, name, range_min, range_max)
const AVIOInterruptCB int_cb
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Macro definitions for various function/variable attributes.
Memory handling functions.
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.
int ff_is_multicast_address(struct sockaddr *addr)
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_socket(int af, int type, int proto, void *logctx)
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,...
int ff_network_wakeup_open(FFNetWakeup *w)
Create a wakeup channel.
int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
Waits for up to 'timeout' microseconds.
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.
void ff_log_net_error(void *ctx, int level, const char *prefix)
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.
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...
void ff_network_wakeup_close(FFNetWakeup *w)
Close the channel.
static void print_address_list(void *ctx, const struct addrinfo *addr, const char *title)
static void interleave_addrinfo(struct addrinfo *base)
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.
static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout, AVIOInterruptCB *cb)
int ff_network_wait_fd(int fd, int write)
int ff_network_init(void)
Initialize the network subsystem.
static int match_host_pattern(const char *pattern, const char *hostname)
#define NEXT_ATTEMPT_DELAY_MS
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_wa...
void ff_network_close(void)
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
int ff_accept(int fd, int timeout, URLContext *h)
Poll for a single connection on the passed file descriptor.
int ff_socket_nonblock(int socket, int enable)
#define IN6_IS_ADDR_MULTICAST(a)
#define FF_ARRAY_ELEMS(a)
#define atomic_load_explicit(object, order)
#define atomic_exchange_explicit(object, desired, order)
#define atomic_init(obj, value)
Callback for checking whether to abort blocking functions.
A channel for waking a thread that is blocked in ff_network_wait_fd_wakeup(), from another thread.
struct sockaddr * ai_addr
struct addrinfo * ai_next
static int customize_fd(void *ctx, int fd, int family)
int av_usleep(unsigned usec)
Sleep for a period of time.
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
void ff_gnutls_deinit(void)
void ff_gnutls_init(void)
unbuffered private I/O API
static double cb(void *priv, double x, double y)