2 * Copyright (c) 2007 The FFmpeg Project
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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
59 * Initialize the network subsystem. On Windows, this calls WSAStartup().
61 * @return 0 on success, a negative AVERROR code on failure.
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",
312 // Iterate forward until we find an entry of a different family.
317 if (cur ==
base->ai_next) {
318 // If the first one following base is of a different family, just
319 // move base forward one step and continue.
321 next = &
base->ai_next;
324 // Unchain cur from the rest of the list from its current spot.
326 // Hook in cur directly after base.
329 // Restart with a new base. We know that before moving the cur element,
330 // everything between the previous base and cur had the same family,
331 // different from cur->ai_family. Therefore, we can keep next pointing
332 // where it was, and continue from there with base at the one after
341 char hostbuf[100], portbuf[20];
345 hostbuf,
sizeof(hostbuf), portbuf,
sizeof(portbuf),
358// Returns < 0 on error, 0 on successfully started connection attempt,
359// > 0 for a connection that succeeded already.
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);
409// Try a new connection to another address after 200 ms, as suggested in
410// RFC 8305 (or sooner if an earlier attempt fails).
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];
429 // This mutates the list, but the head of the list is still the same
430 // element, so the caller, who owns the list, doesn't need to get
431 // an updated pointer.
435 while (nb_attempts > 0 || addrs) {
436 // Start a new connection attempt, if possible.
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;
464 // The connection attempts are sorted from oldest to newest, so the
465 // first one will have the earliest deadline.
467 // If we can start another attempt in parallel, wait until that time.
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))
476 // Check the status from the poll output.
477 for (
i = 0;
i < nb_attempts;
i++) {
479 if (pfd[
i].revents) {
480 // Some sort of action for this socket, check its status (either
481 // a successful connection or an error).
482 optlen =
sizeof(last_err);
483 if (getsockopt(attempts[
i].fd, SOL_SOCKET, SO_ERROR, &last_err, &optlen))
485 else if (last_err != 0)
488 // Everything is ok, we seem to have a successful
489 // connection. Close other sockets and return this one.
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),
506 // Error (or timeout) for this socket; close the socket and remove
507 // it from the attempts/pfd arrays, to let a new attempt start
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,
"*"))
539 // Skip a possible *. at the start of the pattern
540 if (pattern[0] ==
'*')
542 if (pattern[0] ==
'.')
544 len_p = strlen(pattern);
545 len_h = strlen(hostname);
548 // Simply check if the end of hostname is equal to 'pattern'
549 if (!strcmp(pattern, &hostname[len_h - len_p])) {
551 return 1;
// Exact match
552 if (hostname[len_h - len_p - 1] ==
'.')
553 return 1;
// The matched substring is a domain and not just a substring of a domain
571 char *sep, *next =
NULL;
572 start += strspn(start,
" ,");
573 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_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)
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)