FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/network.c
Date: 2026-10-29 23:19:09
Exec Total Coverage
Lines: 46 321 14.3%
Functions: 6 25 24.0%
Branches: 24 196 12.2%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2007 The FFmpeg Project
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <string.h>
22
23 #include "config.h"
24 #include "config_components.h"
25 #include "libavutil/attributes.h"
26
27 #if CONFIG_TLS_PROTOCOL && CONFIG_OPENSSL
28 #include <openssl/opensslv.h>
29 #endif
30
31 #include <fcntl.h>
32 #if HAVE_EVENTFD
33 #include <sys/eventfd.h>
34 #endif
35 #include "network.h"
36 #include "tls.h"
37 #include "url.h"
38 #include "libavutil/avassert.h"
39 #include "libavutil/mem.h"
40 #include "libavutil/time.h"
41
42 9372 int ff_tls_init(void)
43 {
44 #if CONFIG_TLS_PROTOCOL
45 #if CONFIG_GNUTLS
46 ff_gnutls_init();
47 #endif
48 #endif
49 9372 return 0;
50 }
51
52 9372 void ff_tls_deinit(void)
53 {
54 #if CONFIG_TLS_PROTOCOL
55 #if CONFIG_GNUTLS
56 ff_gnutls_deinit();
57 #endif
58 #endif
59 9372 }
60
61 /**
62 * Initialize the network subsystem. On Windows, this calls WSAStartup().
63 *
64 * @return 0 on success, a negative AVERROR code on failure.
65 */
66 9379 int ff_network_init(void)
67 {
68 #if HAVE_WINSOCK2_H
69 WSADATA wsaData;
70
71 if (WSAStartup(MAKEWORD(1,1), &wsaData))
72 return AVERROR(EIO);
73 #endif
74 9379 return 0;
75 }
76
77 ✗ int ff_network_wait_fd(int fd, int write)
78 {
79 ✗ int ev = write ? POLLOUT : POLLIN;
80 ✗ struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
81 int ret;
82 ✗ ret = poll(&p, 1, POLLING_TIME);
83 ✗ return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
84 }
85
86 ✗ int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
87 {
88 int ret;
89 ✗ int64_t wait_start = 0;
90
91 while (1) {
92 ✗ if (ff_check_interrupt(int_cb))
93 ✗ return AVERROR_EXIT;
94 ✗ ret = ff_network_wait_fd(fd, write);
95 ✗ if (ret != AVERROR(EAGAIN))
96 ✗ return ret;
97 ✗ if (timeout > 0) {
98 ✗ if (!wait_start)
99 ✗ wait_start = av_gettime_relative();
100 ✗ else if (av_gettime_relative() - wait_start > timeout)
101 ✗ return AVERROR(ETIMEDOUT);
102 }
103 }
104 }
105
106 ✗ int ff_network_wakeup_open(FFNetWakeup *w)
107 {
108 #if HAVE_WINSOCK2_H
109 /* select() and WSAPoll() only accept sockets, so use a datagram socket
110 * connected to itself: sending a datagram to it makes it readable.
111 */
112 struct sockaddr_in addr = { .sin_family = AF_INET };
113 socklen_t len = sizeof(addr);
114 int ret, fd = ff_socket(AF_INET, SOCK_DGRAM, 0, NULL);
115
116 if (fd < 0)
117 return ff_neterrno();
118
119 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
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) {
123 ret = ff_neterrno();
124 closesocket(fd);
125 return ret;
126 }
127
128 w->rfd = w->wfd = fd;
129 #elif HAVE_EVENTFD
130 ✗ int fd = eventfd(0, EFD_CLOEXEC);
131
132 ✗ if (fd < 0)
133 ✗ return AVERROR(errno);
134
135 ✗ w->rfd = w->wfd = fd;
136 #else
137 int fds[2];
138
139 if (pipe(fds) < 0)
140 return AVERROR(errno);
141
142 #if HAVE_FCNTL
143 for (int i = 0; i < 2; i++) {
144 if (fcntl(fds[i], F_SETFD, FD_CLOEXEC) < 0) {
145 int ret = AVERROR(errno);
146 closesocket(fds[0]);
147 closesocket(fds[1]);
148 return ret;
149 }
150 }
151 #endif
152
153 w->rfd = fds[0];
154 w->wfd = fds[1];
155 #endif
156
157 ✗ atomic_init(&w->signaled, 0);
158
159 ✗ return 0;
160 }
161
162 ✗ int ff_network_wakeup_signal(FFNetWakeup *w)
163 {
164 /* An eventfd only accepts 8-byte writes; a pipe or a socket takes any
165 * payload, so the same value serves every channel type.
166 */
167 ✗ const uint64_t one = 1;
168 int ret;
169
170 /* wfd is blocking; writing at most once keeps the write from blocking.
171 * The flag publishes no other data, so relaxed ordering is enough.
172 */
173 ✗ if (atomic_exchange_explicit(&w->signaled, 1, memory_order_relaxed))
174 ✗ return 0;
175
176 #if HAVE_WINSOCK2_H
177 ret = send(w->wfd, (const char *)&one, sizeof(one), 0);
178 #else
179 ✗ ret = write(w->wfd, &one, sizeof(one));
180 #endif
181
182 ✗ return ret < 0 ? ff_neterrno() : 0;
183 }
184
185 ✗ void ff_network_wakeup_close(FFNetWakeup *w)
186 {
187 ✗ if (w->wfd != w->rfd)
188 ✗ closesocket(w->wfd);
189 ✗ closesocket(w->rfd);
190
191 ✗ w->rfd = w->wfd = -1;
192 ✗ }
193
194 ✗ int ff_network_wait_fd_wakeup(int fd, int write, int timeout_ms,
195 const FFNetWakeup *w)
196 {
197 ✗ int ev = write ? POLLOUT : POLLIN;
198 ✗ struct pollfd p[2] = {
199 ✗ { .fd = w->rfd, .events = POLLIN },
200 { .fd = fd, .events = ev },
201 };
202 int ret;
203
204 /* Return without polling once signaled. This also ends the wait when
205 * the write in ff_network_wakeup_signal() failed and rfd never became
206 * readable.
207 */
208 ✗ if (atomic_load_explicit(&w->signaled, memory_order_relaxed))
209 ✗ return AVERROR_EXIT;
210
211 ✗ ret = poll(p, 2, timeout_ms);
212 ✗ if (ret < 0)
213 ✗ return ff_neterrno();
214
215 ✗ if (p[0].revents & POLLIN)
216 ✗ return AVERROR_EXIT;
217
218 ✗ return p[1].revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
219 }
220
221 ✗ int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
222 {
223 ✗ int64_t wait_start = av_gettime_relative();
224
225 ✗ while (1) {
226 int64_t time_left;
227
228 ✗ if (ff_check_interrupt(int_cb))
229 ✗ return AVERROR_EXIT;
230
231 ✗ time_left = timeout - (av_gettime_relative() - wait_start);
232 ✗ if (time_left <= 0)
233 ✗ return AVERROR(ETIMEDOUT);
234
235 ✗ av_usleep(FFMIN(time_left, POLLING_TIME * 1000));
236 }
237 }
238
239 9379 void ff_network_close(void)
240 {
241 #if HAVE_WINSOCK2_H
242 WSACleanup();
243 #endif
244 9379 }
245
246 #if HAVE_WINSOCK2_H
247 int ff_neterrno(void)
248 {
249 int err = WSAGetLastError();
250 switch (err) {
251 case WSAEWOULDBLOCK:
252 return AVERROR(EAGAIN);
253 case WSAEINTR:
254 return AVERROR(EINTR);
255 case WSAEPROTONOSUPPORT:
256 return AVERROR(EPROTONOSUPPORT);
257 case WSAETIMEDOUT:
258 return AVERROR(ETIMEDOUT);
259 case WSAECONNREFUSED:
260 return AVERROR(ECONNREFUSED);
261 case WSAEINPROGRESS:
262 return AVERROR(EINPROGRESS);
263 }
264 return -err;
265 }
266 #endif
267
268 ✗ int ff_is_multicast_address(struct sockaddr *addr)
269 {
270 ✗ if (addr->sa_family == AF_INET) {
271 ✗ return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
272 }
273 #if HAVE_STRUCT_SOCKADDR_IN6
274 ✗ if (addr->sa_family == AF_INET6) {
275 ✗ return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
276 }
277 #endif
278
279 ✗ return 0;
280 }
281
282 ✗ static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
283 AVIOInterruptCB *cb)
284 {
285 ✗ int runs = timeout / POLLING_TIME;
286 ✗ int ret = 0;
287
288 do {
289 ✗ if (ff_check_interrupt(cb))
290 ✗ return AVERROR_EXIT;
291 ✗ ret = poll(p, nfds, POLLING_TIME);
292 ✗ if (ret != 0) {
293 ✗ if (ret < 0)
294 ✗ ret = ff_neterrno();
295 ✗ if (ret == AVERROR(EINTR))
296 ✗ continue;
297 ✗ break;
298 }
299 ✗ } while (timeout <= 0 || runs-- > 0);
300
301 ✗ if (!ret)
302 ✗ return AVERROR(ETIMEDOUT);
303 ✗ return ret;
304 }
305
306 ✗ int ff_socket(int af, int type, int proto, void *logctx)
307 {
308 int fd;
309
310 #ifdef SOCK_CLOEXEC
311 ✗ fd = socket(af, type | SOCK_CLOEXEC, proto);
312 ✗ if (fd == -1 && errno == EINVAL)
313 #endif
314 {
315 ✗ fd = socket(af, type, proto);
316 #if HAVE_FCNTL
317 ✗ if (fd != -1) {
318 ✗ if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
319 ✗ av_log(logctx, AV_LOG_DEBUG, "Failed to set close on exec\n");
320 }
321 #endif
322 }
323 #ifdef SO_NOSIGPIPE
324 if (fd != -1) {
325 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int))) {
326 av_log(logctx, AV_LOG_WARNING, "setsockopt(SO_NOSIGPIPE) failed\n");
327 }
328 }
329 #endif
330 ✗ return fd;
331 }
332
333 ✗ int ff_listen(int fd, const struct sockaddr *addr,
334 socklen_t addrlen, void *logctx)
335 {
336 int ret;
337 ✗ int reuse = 1;
338 ✗ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
339 ✗ av_log(logctx, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
340 }
341 ✗ ret = bind(fd, addr, addrlen);
342 ✗ if (ret)
343 ✗ return ff_neterrno();
344
345 ✗ ret = listen(fd, 1);
346 ✗ if (ret)
347 ✗ return ff_neterrno();
348 ✗ return ret;
349 }
350
351 ✗ int ff_accept(int fd, int timeout, URLContext *h)
352 {
353 int ret;
354 ✗ struct pollfd lp = { fd, POLLIN, 0 };
355
356 ✗ ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
357 ✗ if (ret < 0)
358 ✗ return ret;
359
360 ✗ ret = accept(fd, NULL, NULL);
361 ✗ if (ret < 0)
362 ✗ return ff_neterrno();
363 ✗ if (ff_socket_nonblock(ret, 1) < 0)
364 ✗ av_log(h, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
365
366 ✗ return ret;
367 }
368
369 ✗ int ff_listen_bind(int fd, const struct sockaddr *addr,
370 socklen_t addrlen, int timeout, URLContext *h)
371 {
372 int ret;
373 ✗ if ((ret = ff_listen(fd, addr, addrlen, h)) < 0)
374 ✗ return ret;
375 ✗ if ((ret = ff_accept(fd, timeout, h)) < 0)
376 ✗ return ret;
377 ✗ closesocket(fd);
378 ✗ return ret;
379 }
380
381 ✗ int ff_listen_connect(int fd, const struct sockaddr *addr,
382 socklen_t addrlen, int timeout, URLContext *h,
383 int will_try_next)
384 {
385 ✗ struct pollfd p = {fd, POLLOUT, 0};
386 int ret;
387 socklen_t optlen;
388
389 ✗ if (ff_socket_nonblock(fd, 1) < 0)
390 ✗ av_log(h, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
391
392 ✗ while ((ret = connect(fd, addr, addrlen))) {
393 ✗ ret = ff_neterrno();
394 ✗ switch (ret) {
395 ✗ case AVERROR(EINTR):
396 ✗ if (ff_check_interrupt(&h->interrupt_callback))
397 ✗ return AVERROR_EXIT;
398 ✗ continue;
399 ✗ case AVERROR(EINPROGRESS):
400 case AVERROR(EAGAIN):
401 ✗ ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
402 ✗ if (ret < 0)
403 ✗ return ret;
404 ✗ optlen = sizeof(ret);
405 ✗ if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
406 ✗ ret = AVUNERROR(ff_neterrno());
407 ✗ if (ret != 0) {
408 ✗ ret = AVERROR(ret);
409 ✗ if (will_try_next)
410 ✗ av_log(h, AV_LOG_WARNING,
411 "Connection to %s failed (%s), trying next address\n",
412 ✗ h->filename, av_err2str(ret));
413 else
414 ✗ av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
415 ✗ h->filename, av_err2str(ret));
416 }
417 av_fallthrough;
418 default:
419 ✗ return ret;
420 }
421 }
422 ✗ return ret;
423 }
424
425 ✗ static void interleave_addrinfo(struct addrinfo *base)
426 {
427 ✗ struct addrinfo **next = &base->ai_next;
428 ✗ while (*next) {
429 ✗ struct addrinfo *cur = *next;
430 // Iterate forward until we find an entry of a different family.
431 ✗ if (cur->ai_family == base->ai_family) {
432 ✗ next = &cur->ai_next;
433 ✗ continue;
434 }
435 ✗ if (cur == base->ai_next) {
436 // If the first one following base is of a different family, just
437 // move base forward one step and continue.
438 ✗ base = cur;
439 ✗ next = &base->ai_next;
440 ✗ continue;
441 }
442 // Unchain cur from the rest of the list from its current spot.
443 ✗ *next = cur->ai_next;
444 // Hook in cur directly after base.
445 ✗ cur->ai_next = base->ai_next;
446 ✗ base->ai_next = cur;
447 // Restart with a new base. We know that before moving the cur element,
448 // everything between the previous base and cur had the same family,
449 // different from cur->ai_family. Therefore, we can keep next pointing
450 // where it was, and continue from there with base at the one after
451 // cur.
452 ✗ base = cur->ai_next;
453 }
454 ✗ }
455
456 ✗ static void print_address_list(void *ctx, const struct addrinfo *addr,
457 const char *title)
458 {
459 char hostbuf[100], portbuf[20];
460 ✗ av_log(ctx, AV_LOG_DEBUG, "%s:\n", title);
461 ✗ while (addr) {
462 ✗ getnameinfo(addr->ai_addr, addr->ai_addrlen,
463 hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
464 NI_NUMERICHOST | NI_NUMERICSERV);
465 ✗ av_log(ctx, AV_LOG_DEBUG, "Address %s port %s\n", hostbuf, portbuf);
466 ✗ addr = addr->ai_next;
467 }
468 ✗ }
469
470 struct ConnectionAttempt {
471 int fd;
472 int64_t deadline_us;
473 struct addrinfo *addr;
474 };
475
476 // Returns < 0 on error, 0 on successfully started connection attempt,
477 // > 0 for a connection that succeeded already.
478 ✗ static int start_connect_attempt(struct ConnectionAttempt *attempt,
479 struct addrinfo **ptr, int timeout_ms,
480 URLContext *h,
481 int (*customize_fd)(void *, int, int), void *customize_ctx)
482 {
483 ✗ struct addrinfo *ai = *ptr;
484 int ret;
485
486 ✗ *ptr = ai->ai_next;
487
488 ✗ attempt->fd = ff_socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol, h);
489 ✗ if (attempt->fd < 0)
490 ✗ return ff_neterrno();
491 ✗ attempt->deadline_us = av_gettime_relative() + timeout_ms * 1000;
492 ✗ attempt->addr = ai;
493
494 ✗ ff_socket_nonblock(attempt->fd, 1);
495
496 ✗ if (customize_fd) {
497 ✗ ret = customize_fd(customize_ctx, attempt->fd, ai->ai_family);
498 ✗ if (ret) {
499 ✗ closesocket(attempt->fd);
500 ✗ attempt->fd = -1;
501 ✗ return ret;
502 }
503 }
504
505 ✗ while ((ret = connect(attempt->fd, ai->ai_addr, ai->ai_addrlen))) {
506 ✗ ret = ff_neterrno();
507 ✗ switch (ret) {
508 ✗ case AVERROR(EINTR):
509 ✗ if (ff_check_interrupt(&h->interrupt_callback)) {
510 ✗ closesocket(attempt->fd);
511 ✗ attempt->fd = -1;
512 ✗ return AVERROR_EXIT;
513 }
514 ✗ continue;
515 ✗ case AVERROR(EINPROGRESS):
516 case AVERROR(EAGAIN):
517 ✗ return 0;
518 ✗ default:
519 ✗ closesocket(attempt->fd);
520 ✗ attempt->fd = -1;
521 ✗ return ret;
522 }
523 }
524 ✗ return 1;
525 }
526
527 // Try a new connection to another address after 200 ms, as suggested in
528 // RFC 8305 (or sooner if an earlier attempt fails).
529 #define NEXT_ATTEMPT_DELAY_MS 200
530
531 ✗ int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address,
532 int parallel, URLContext *h, int *fd,
533 int (*customize_fd)(void *, int, int), void *customize_ctx)
534 {
535 struct ConnectionAttempt attempts[3];
536 struct pollfd pfd[3];
537 ✗ int nb_attempts = 0, i, j;
538 ✗ int64_t next_attempt_us = av_gettime_relative(), next_deadline_us;
539 ✗ int last_err = AVERROR(EIO);
540 socklen_t optlen;
541 char hostbuf[100], portbuf[20];
542
543 ✗ if (parallel > FF_ARRAY_ELEMS(attempts))
544 ✗ parallel = FF_ARRAY_ELEMS(attempts);
545
546 ✗ print_address_list(h, addrs, "Original list of addresses");
547 // This mutates the list, but the head of the list is still the same
548 // element, so the caller, who owns the list, doesn't need to get
549 // an updated pointer.
550 ✗ interleave_addrinfo(addrs);
551 ✗ print_address_list(h, addrs, "Interleaved list of addresses");
552
553 ✗ while (nb_attempts > 0 || addrs) {
554 // Start a new connection attempt, if possible.
555 ✗ if (nb_attempts < parallel && addrs) {
556 ✗ getnameinfo(addrs->ai_addr, addrs->ai_addrlen,
557 hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
558 NI_NUMERICHOST | NI_NUMERICSERV);
559 ✗ av_log(h, AV_LOG_VERBOSE, "Starting connection attempt to %s port %s\n",
560 hostbuf, portbuf);
561 ✗ last_err = start_connect_attempt(&attempts[nb_attempts], &addrs,
562 timeout_ms_per_address, h,
563 customize_fd, customize_ctx);
564 ✗ if (last_err < 0) {
565 ✗ av_log(h, AV_LOG_VERBOSE, "Connected attempt failed: %s\n",
566 ✗ av_err2str(last_err));
567 ✗ continue;
568 }
569 ✗ if (last_err > 0) {
570 ✗ for (i = 0; i < nb_attempts; i++)
571 ✗ closesocket(attempts[i].fd);
572 ✗ *fd = attempts[nb_attempts].fd;
573 ✗ return 0;
574 }
575 ✗ pfd[nb_attempts].fd = attempts[nb_attempts].fd;
576 ✗ pfd[nb_attempts].events = POLLOUT;
577 ✗ next_attempt_us = av_gettime_relative() + NEXT_ATTEMPT_DELAY_MS * 1000;
578 ✗ nb_attempts++;
579 }
580
581 ✗ av_assert0(nb_attempts > 0);
582 // The connection attempts are sorted from oldest to newest, so the
583 // first one will have the earliest deadline.
584 ✗ next_deadline_us = attempts[0].deadline_us;
585 // If we can start another attempt in parallel, wait until that time.
586 ✗ if (nb_attempts < parallel && addrs)
587 ✗ next_deadline_us = FFMIN(next_deadline_us, next_attempt_us);
588 ✗ last_err = ff_poll_interrupt(pfd, nb_attempts,
589 ✗ (next_deadline_us - av_gettime_relative())/1000,
590 &h->interrupt_callback);
591 ✗ if (last_err < 0 && last_err != AVERROR(ETIMEDOUT))
592 ✗ break;
593
594 // Check the status from the poll output.
595 ✗ for (i = 0; i < nb_attempts; i++) {
596 ✗ last_err = 0;
597 ✗ if (pfd[i].revents) {
598 // Some sort of action for this socket, check its status (either
599 // a successful connection or an error).
600 ✗ optlen = sizeof(last_err);
601 ✗ if (getsockopt(attempts[i].fd, SOL_SOCKET, SO_ERROR, &last_err, &optlen))
602 ✗ last_err = ff_neterrno();
603 ✗ else if (last_err != 0)
604 ✗ last_err = AVERROR(last_err);
605 ✗ if (last_err == 0) {
606 // Everything is ok, we seem to have a successful
607 // connection. Close other sockets and return this one.
608 ✗ for (j = 0; j < nb_attempts; j++)
609 ✗ if (j != i)
610 ✗ closesocket(attempts[j].fd);
611 ✗ *fd = attempts[i].fd;
612 ✗ getnameinfo(attempts[i].addr->ai_addr, attempts[i].addr->ai_addrlen,
613 hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
614 NI_NUMERICHOST | NI_NUMERICSERV);
615 ✗ av_log(h, AV_LOG_VERBOSE, "Successfully connected to %s port %s\n",
616 hostbuf, portbuf);
617 ✗ return 0;
618 }
619 }
620 ✗ if (attempts[i].deadline_us < av_gettime_relative() && !last_err)
621 ✗ last_err = AVERROR(ETIMEDOUT);
622 ✗ if (!last_err)
623 ✗ continue;
624 // Error (or timeout) for this socket; close the socket and remove
625 // it from the attempts/pfd arrays, to let a new attempt start
626 // directly.
627 ✗ getnameinfo(attempts[i].addr->ai_addr, attempts[i].addr->ai_addrlen,
628 hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
629 NI_NUMERICHOST | NI_NUMERICSERV);
630 ✗ av_log(h, AV_LOG_VERBOSE, "Connection attempt to %s port %s "
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));
637 ✗ i--;
638 ✗ nb_attempts--;
639 }
640 }
641 ✗ for (i = 0; i < nb_attempts; i++)
642 ✗ closesocket(attempts[i].fd);
643 ✗ if (last_err >= 0)
644 ✗ last_err = AVERROR(ECONNREFUSED);
645 ✗ if (last_err != AVERROR_EXIT) {
646 ✗ av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
647 ✗ h->filename, av_err2str(last_err));
648 }
649 ✗ return last_err;
650 }
651
652 15 static int match_host_pattern(const char *pattern, const char *hostname)
653 {
654 int len_p, len_h;
655
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 if (!strcmp(pattern, "*"))
656 1 return 1;
657 // Skip a possible *. at the start of the pattern
658
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 if (pattern[0] == '*')
659 2 pattern++;
660
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
14 if (pattern[0] == '.')
661 3 pattern++;
662 14 len_p = strlen(pattern);
663 14 len_h = strlen(hostname);
664
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
14 if (len_p > len_h)
665 5 return 0;
666 // Simply check if the end of hostname is equal to 'pattern'
667
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (!strcmp(pattern, &hostname[len_h - len_p])) {
668
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (len_h == len_p)
669 4 return 1; // Exact match
670
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (hostname[len_h - len_p - 1] == '.')
671 1 return 1; // The matched substring is a domain and not just a substring of a domain
672 }
673 4 return 0;
674 }
675
676 9 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
677 {
678 char *buf, *start;
679 9 int ret = 0;
680
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (!no_proxy)
681 1 return 0;
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!hostname)
683 ✗ return 0;
684 8 buf = av_strdup(no_proxy);
685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!buf)
686 ✗ return 0;
687 8 start = buf;
688
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 2 times.
17 while (start) {
689 15 char *sep, *next = NULL;
690 15 start += strspn(start, " ,");
691 15 sep = start + strcspn(start, " ,");
692
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8 times.
15 if (*sep) {
693 7 next = sep + 1;
694 7 *sep = '\0';
695 }
696
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 9 times.
15 if (match_host_pattern(start, hostname)) {
697 6 ret = 1;
698 6 break;
699 }
700 9 start = next;
701 }
702 8 av_free(buf);
703 8 return ret;
704 }
705
706 ✗ void ff_log_net_error(void *ctx, int level, const char* prefix)
707 {
708 ✗ av_log(ctx, level, "%s: %s\n", prefix, av_err2str(ff_neterrno()));
709 ✗ }
710