24#include "config_components.h"
27#if CONFIG_TLS_PROTOCOL && CONFIG_OPENSSL
28#include <openssl/opensslv.h>
41#if CONFIG_TLS_PROTOCOL
51#if CONFIG_TLS_PROTOCOL
68 if (WSAStartup(MAKEWORD(1,1), &wsaData))
76 int ev = write ? POLLOUT : POLLIN;
77 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
80 return ret < 0 ?
ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 :
AVERROR(EAGAIN);
131 int err = WSAGetLastError();
137 case WSAEPROTONOSUPPORT:
138 return AVERROR(EPROTONOSUPPORT);
141 case WSAECONNREFUSED:
152 if (addr->sa_family == AF_INET) {
153 return IN_MULTICAST(ntohl(((
struct sockaddr_in *)addr)->sin_addr.s_addr));
155#if HAVE_STRUCT_SOCKADDR_IN6
156 if (addr->sa_family == AF_INET6) {
181 }
while (timeout <= 0 || runs-- > 0);
193 fd = socket(af,
type | SOCK_CLOEXEC, proto);
194 if (fd == -1 && errno == EINVAL)
197 fd = socket(af,
type, proto);
200 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
207 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(
int){1},
sizeof(int))) {
216 socklen_t addrlen,
void *logctx)
220 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(reuse))) {
223 ret = bind(fd, addr, addrlen);
236 struct pollfd lp = { fd, POLLIN, 0 };
255 if ((ret =
ff_listen(fd, addr, addrlen,
h)) < 0)
267 struct pollfd p = {fd, POLLOUT, 0};
274 while ((ret = connect(fd, addr, addrlen))) {
286 optlen =
sizeof(ret);
287 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
293 "Connection to %s failed (%s), trying next address\n",
317 if (cur ==
base->ai_next) {
321 next = &
base->ai_next;
341 char hostbuf[100], portbuf[20];
345 hostbuf,
sizeof(hostbuf), portbuf,
sizeof(portbuf),
361 struct addrinfo **ptr,
int timeout_ms,
363 int (*
customize_fd)(
void *,
int,
int),
void *customize_ctx)
381 closesocket(attempt->
fd);
392 closesocket(attempt->
fd);
401 closesocket(attempt->
fd);
411#define NEXT_ATTEMPT_DELAY_MS 200
415 int (*
customize_fd)(
void *,
int,
int),
void *customize_ctx)
418 struct pollfd pfd[3];
419 int nb_attempts = 0,
i, j;
423 char hostbuf[100], portbuf[20];
435 while (nb_attempts > 0 || addrs) {
437 if (nb_attempts < parallel && addrs) {
439 hostbuf,
sizeof(hostbuf), portbuf,
sizeof(portbuf),
444 timeout_ms_per_address,
h,
452 for (
i = 0;
i < nb_attempts;
i++)
453 closesocket(attempts[
i].fd);
454 *fd = attempts[nb_attempts].
fd;
457 pfd[nb_attempts].fd = attempts[nb_attempts].
fd;
458 pfd[nb_attempts].events = POLLOUT;
468 if (nb_attempts < parallel && addrs)
469 next_deadline_us =
FFMIN(next_deadline_us, next_attempt_us);
472 &
h->interrupt_callback);
473 if (last_err < 0 && last_err !=
AVERROR(ETIMEDOUT))
477 for (
i = 0;
i < nb_attempts;
i++) {
479 if (pfd[
i].revents) {
482 optlen =
sizeof(last_err);
483 if (getsockopt(attempts[
i].fd, SOL_SOCKET, SO_ERROR, &last_err, &optlen))
485 else if (last_err != 0)
490 for (j = 0; j < nb_attempts; j++)
492 closesocket(attempts[j].fd);
493 *fd = attempts[
i].
fd;
495 hostbuf,
sizeof(hostbuf), portbuf,
sizeof(portbuf),
510 hostbuf,
sizeof(hostbuf), portbuf,
sizeof(portbuf),
513 "failed: %s\n", hostbuf, portbuf,
av_err2str(last_err));
514 closesocket(attempts[
i].fd);
515 memmove(&attempts[
i], &attempts[
i + 1],
516 (nb_attempts -
i - 1) *
sizeof(*attempts));
517 memmove(&pfd[
i], &pfd[
i + 1],
518 (nb_attempts -
i - 1) *
sizeof(*pfd));
523 for (
i = 0;
i < nb_attempts;
i++)
524 closesocket(attempts[
i].fd);
526 last_err =
AVERROR(ECONNREFUSED);
537 if (!strcmp(pattern,
"*"))
540 if (pattern[0] ==
'*')
542 if (pattern[0] ==
'.')
544 len_p = strlen(pattern);
545 len_h = strlen(hostname);
549 if (!strcmp(pattern, &hostname[len_h - len_p])) {
552 if (hostname[len_h - len_p - 1] ==
'.')
571 char *sep, *next =
NULL;
572 start += strspn(start,
" ,");
573 sep = start + strcspn(start,
" ,");
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_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.
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)
Callback for checking whether to abort blocking functions.
struct sockaddr * ai_addr
struct addrinfo * ai_next
static int customize_fd(void *ctx, int fd, int family)
static AVFormatContext * ctx
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)