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