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 "config_components.h"
22
23 #include <fcntl.h>
30
32 {
33 #if CONFIG_TLS_PROTOCOL
34 #if CONFIG_OPENSSL
38 #endif
39 #if CONFIG_GNUTLS
41 #endif
42 #endif
43 return 0;
44 }
45
47 {
48 #if CONFIG_TLS_PROTOCOL
49 #if CONFIG_OPENSSL
51 #endif
52 #if CONFIG_GNUTLS
54 #endif
55 #endif
56 }
57
59 {
60 #if HAVE_WINSOCK2_H
61 WSADATA wsaData;
62
63 if (WSAStartup(MAKEWORD(1,1), &wsaData))
64 return 0;
65 #endif
66 return 1;
67 }
68
70 {
71 int ev = write ? POLLOUT : POLLIN;
72 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
76 }
77
79 {
81 int64_t wait_start = 0;
82
83 while (1) {
89 if (timeout > 0) {
90 if (!wait_start)
94 }
95 }
96 }
97
99 {
101
102 while (1) {
103 int64_t time_left;
104
107
109 if (time_left <= 0)
111
113 }
114 }
115
117 {
118 #if HAVE_WINSOCK2_H
119 WSACleanup();
120 #endif
121 }
122
123 #if HAVE_WINSOCK2_H
125 {
126 int err = WSAGetLastError();
127 switch (err) {
128 case WSAEWOULDBLOCK:
130 case WSAEINTR:
132 case WSAEPROTONOSUPPORT:
133 return AVERROR(EPROTONOSUPPORT);
134 case WSAETIMEDOUT:
136 case WSAECONNREFUSED:
138 case WSAEINPROGRESS:
140 }
141 return -err;
142 }
143 #endif
144
146 {
147 if (addr->sa_family == AF_INET) {
148 return IN_MULTICAST(ntohl(((
struct sockaddr_in *)addr)->sin_addr.s_addr));
149 }
150 #if HAVE_STRUCT_SOCKADDR_IN6
151 if (addr->sa_family == AF_INET6) {
153 }
154 #endif
155
156 return 0;
157 }
158
161 {
164
165 do {
173 continue;
174 break;
175 }
176 } while (timeout <= 0 || runs-- > 0);
177
181 }
182
184 {
185 int fd;
186
187 #ifdef SOCK_CLOEXEC
188 fd = socket(af,
type | SOCK_CLOEXEC, proto);
189 if (fd == -1 && errno == EINVAL)
190 #endif
191 {
192 fd = socket(af,
type, proto);
193 #if HAVE_FCNTL
194 if (fd != -1) {
195 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
197 }
198 #endif
199 }
200 #ifdef SO_NOSIGPIPE
201 if (fd != -1) {
202 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(
int){1},
sizeof(
int))) {
204 }
205 }
206 #endif
207 return fd;
208 }
209
211 socklen_t addrlen, void *logctx)
212 {
214 int reuse = 1;
215 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
217 }
218 ret = bind(fd, addr, addrlen);
221
226 }
227
229 {
231 struct pollfd lp = { fd, POLLIN, 0 };
232
236
242
244 }
245
248 {
254 closesocket(fd);
256 }
257
260 int will_try_next)
261 {
262 struct pollfd p = {fd, POLLOUT, 0};
264 socklen_t optlen;
265
268
269 while ((
ret = connect(fd, addr, addrlen))) {
275 continue;
281 optlen =
sizeof(
ret);
282 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &
ret, &optlen))
285 char errbuf[100];
288 if (will_try_next)
290 "Connection to %s failed (%s), trying next address\n",
291 h->filename, errbuf);
292 else
294 h->filename, errbuf);
295 }
296 default:
298 }
299 }
301 }
302
304 {
306 while (*next) {
308 // Iterate forward until we find an entry of a different family.
311 continue;
312 }
313 if (cur ==
base->ai_next) {
314 // If the first one following base is of a different family, just
315 // move base forward one step and continue.
317 next = &
base->ai_next;
318 continue;
319 }
320 // Unchain cur from the rest of the list from its current spot.
322 // Hook in cur directly after base.
325 // Restart with a new base. We know that before moving the cur element,
326 // everything between the previous base and cur had the same family,
327 // different from cur->ai_family. Therefore, we can keep next pointing
328 // where it was, and continue from there with base at the one after
329 // cur.
331 }
332 }
333
335 const char *title)
336 {
337 char hostbuf[100], portbuf[20];
339 while (addr) {
341 hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
345 }
346 }
347
352 };
353
354 // Returns < 0 on error, 0 on successfully started connection attempt,
355 // > 0 for a connection that succeeded already.
357 struct addrinfo **ptr,
int timeout_ms,
360 {
363
365
371
373
376
382 closesocket(attempt->
fd);
385 }
386 continue;
389 return 0;
390 default:
391 closesocket(attempt->
fd);
394 }
395 }
396 return 1;
397 }
398
399 // Try a new connection to another address after 200 ms, as suggested in
400 // RFC 8305 (or sooner if an earlier attempt fails).
401 #define NEXT_ATTEMPT_DELAY_MS 200
402
406 {
408 struct pollfd pfd[3];
409 int nb_attempts = 0,
i, j;
412 socklen_t optlen;
413 char errbuf[100], hostbuf[100], portbuf[20];
414
417
419 // This mutates the list, but the head of the list is still the same
420 // element, so the caller, who owns the list, doesn't need to get
421 // an updated pointer.
424
425 while (nb_attempts > 0 || addrs) {
426 // Start a new connection attempt, if possible.
427 if (nb_attempts < parallel && addrs) {
429 hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
432 hostbuf, portbuf);
434 timeout_ms_per_address,
h,
436 if (last_err < 0) {
439 errbuf);
440 continue;
441 }
442 if (last_err > 0) {
443 for (
i = 0;
i < nb_attempts;
i++)
444 closesocket(attempts[
i].fd);
445 *fd = attempts[nb_attempts].
fd;
446 return 0;
447 }
448 pfd[nb_attempts].fd = attempts[nb_attempts].
fd;
449 pfd[nb_attempts].events = POLLOUT;
451 nb_attempts++;
452 }
453
455 // The connection attempts are sorted from oldest to newest, so the
456 // first one will have the earliest deadline.
458 // If we can start another attempt in parallel, wait until that time.
459 if (nb_attempts < parallel && addrs)
460 next_deadline_us =
FFMIN(next_deadline_us, next_attempt_us);
463 &
h->interrupt_callback);
464 if (last_err < 0 && last_err !=
AVERROR(ETIMEDOUT))
465 break;
466
467 // Check the status from the poll output.
468 for (
i = 0;
i < nb_attempts;
i++) {
469 last_err = 0;
470 if (pfd[
i].revents) {
471 // Some sort of action for this socket, check its status (either
472 // a successful connection or an error).
473 optlen = sizeof(last_err);
474 if (getsockopt(attempts[
i].fd, SOL_SOCKET, SO_ERROR, &last_err, &optlen))
476 else if (last_err != 0)
478 if (last_err == 0) {
479 // Everything is ok, we seem to have a successful
480 // connection. Close other sockets and return this one.
481 for (j = 0; j < nb_attempts; j++)
483 closesocket(attempts[j].fd);
484 *fd = attempts[
i].
fd;
486 hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
489 hostbuf, portbuf);
490 return 0;
491 }
492 }
495 if (!last_err)
496 continue;
497 // Error (or timeout) for this socket; close the socket and remove
498 // it from the attempts/pfd arrays, to let a new attempt start
499 // directly.
501 hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
505 "failed: %s\n", hostbuf, portbuf, errbuf);
506 closesocket(attempts[
i].fd);
507 memmove(&attempts[
i], &attempts[
i + 1],
508 (nb_attempts -
i - 1) *
sizeof(*attempts));
509 memmove(&pfd[
i], &pfd[
i + 1],
510 (nb_attempts -
i - 1) *
sizeof(*pfd));
512 nb_attempts--;
513 }
514 }
515 for (
i = 0;
i < nb_attempts;
i++)
516 closesocket(attempts[
i].fd);
517 if (last_err >= 0)
518 last_err =
AVERROR(ECONNREFUSED);
522 h->filename, errbuf);
523 }
524 return last_err;
525 }
526
528 {
529 int len_p, len_h;
530 if (!strcmp(pattern, "*"))
531 return 1;
532 // Skip a possible *. at the start of the pattern
533 if (pattern[0] == '*')
534 pattern++;
535 if (pattern[0] == '.')
536 pattern++;
537 len_p = strlen(pattern);
538 len_h = strlen(hostname);
539 if (len_p > len_h)
540 return 0;
541 // Simply check if the end of hostname is equal to 'pattern'
542 if (!strcmp(pattern, &hostname[len_h - len_p])) {
543 if (len_h == len_p)
544 return 1; // Exact match
545 if (hostname[len_h - len_p - 1] == '.')
546 return 1; // The matched substring is a domain and not just a substring of a domain
547 }
548 return 0;
549 }
550
552 {
553 char *buf, *start;
555 if (!no_proxy)
556 return 0;
557 if (!hostname)
558 return 0;
560 if (!buf)
561 return 0;
562 start = buf;
563 while (start) {
564 char *sep, *next =
NULL;
565 start += strspn(start, " ,");
566 sep = start + strcspn(start, " ,");
567 if (*sep) {
568 next = sep + 1;
569 *sep = '0円';
570 }
573 break;
574 }
575 start = next;
576 }
579 }
580
582 {
583 char errbuf[100];
586 }