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
25 #include "libavcodec/internal.h"
26 #include "libavutil/avutil.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/time.h"
32 #if CONFIG_TLS_PROTOCOL
35 if ((ret = ff_openssl_init()) < 0)
45 void ff_tls_deinit(void)
47 #if CONFIG_TLS_PROTOCOL
57 int ff_network_init(void)
62 if (WSAStartup(MAKEWORD(1,1), &wsaData))
68 int ff_network_wait_fd(int fd, int write)
70 int ev = write ? POLLOUT : POLLIN;
71 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
73 ret = poll(&p, 1, POLLING_TIME);
74 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
77 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
80 int64_t wait_start = 0;
83 if (ff_check_interrupt(int_cb))
85 ret = ff_network_wait_fd(fd, write);
86 if (ret != AVERROR(EAGAIN))
90 wait_start = av_gettime_relative();
91 else if (av_gettime_relative() - wait_start > timeout)
92 return AVERROR(ETIMEDOUT);
97 int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
99 int64_t wait_start = av_gettime_relative();
104 if (ff_check_interrupt(int_cb))
107 time_left = timeout - (av_gettime_relative() - wait_start);
109 return AVERROR(ETIMEDOUT);
111 av_usleep(FFMIN(time_left, POLLING_TIME * 1000));
115 void ff_network_close(void)
123 int ff_neterrno(void)
125 int err = WSAGetLastError();
128 return AVERROR(EAGAIN);
130 return AVERROR(EINTR);
131 case WSAEPROTONOSUPPORT:
132 return AVERROR(EPROTONOSUPPORT);
134 return AVERROR(ETIMEDOUT);
135 case WSAECONNREFUSED:
136 return AVERROR(ECONNREFUSED);
138 return AVERROR(EINPROGRESS);
144 int ff_is_multicast_address(struct sockaddr *addr)
146 if (addr->sa_family == AF_INET) {
147 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
149 #if HAVE_STRUCT_SOCKADDR_IN6
150 if (addr->sa_family == AF_INET6) {
151 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
158 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
161 int runs = timeout / POLLING_TIME;
165 if (ff_check_interrupt(cb))
167 ret = poll(p, nfds, POLLING_TIME);
170 } while (timeout <= 0 || runs-- > 0);
173 return AVERROR(ETIMEDOUT);
175 return ff_neterrno();
179 int ff_socket(int af, int type, int proto)
184 fd = socket(af, type | SOCK_CLOEXEC, proto);
185 if (fd == -1 && errno == EINVAL)
188 fd = socket(af, type, proto);
191 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
192 av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
198 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int));
203 int ff_listen(int fd, const struct sockaddr *addr,
208 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
209 av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
211 ret = bind(fd, addr, addrlen);
213 return ff_neterrno();
217 return ff_neterrno();
221 int ff_accept(int fd, int timeout, URLContext *h)
224 struct pollfd lp = { fd, POLLIN, 0 };
226 ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
230 ret = accept(fd, NULL, NULL);
232 return ff_neterrno();
233 if (ff_socket_nonblock(ret, 1) < 0)
234 av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
239 int ff_listen_bind(int fd, const struct sockaddr *addr,
240 socklen_t addrlen, int timeout, URLContext *h)
243 if ((ret = ff_listen(fd, addr, addrlen)) < 0)
245 if ((ret = ff_accept(fd, timeout, h)) < 0)
251 int ff_listen_connect(int fd, const struct sockaddr *addr,
252 socklen_t addrlen, int timeout, URLContext *h,
255 struct pollfd p = {fd, POLLOUT, 0};
259 if (ff_socket_nonblock(fd, 1) < 0)
260 av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
262 while ((ret = connect(fd, addr, addrlen))) {
266 if (ff_check_interrupt(&h->interrupt_callback))
269 case AVERROR(EINPROGRESS):
270 case AVERROR(EAGAIN):
271 ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
274 optlen = sizeof(ret);
275 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
276 ret = AVUNERROR(ff_neterrno());
280 av_strerror(ret, errbuf, sizeof(errbuf));
282 av_log(h, AV_LOG_WARNING,
283 "Connection to %s failed (%s), trying next address\n",
284 h->filename, errbuf);
286 av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
287 h->filename, errbuf);
296 static int match_host_pattern(const char *pattern, const char *hostname)
299 if (!strcmp(pattern, "*"))
301 // Skip a possible *. at the start of the pattern
302 if (pattern[0] == '*')
304 if (pattern[0] == '.')
306 len_p = strlen(pattern);
307 len_h = strlen(hostname);
310 // Simply check if the end of hostname is equal to 'pattern'
311 if (!strcmp(pattern, &hostname[len_h - len_p])) {
313 return 1; // Exact match
314 if (hostname[len_h - len_p - 1] == '.')
315 return 1; // The matched substring is a domain and not just a substring of a domain
320 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
328 buf = av_strdup(no_proxy);
333 char *sep, *next = NULL;
334 start += strspn(start, " ,");
335 sep = start + strcspn(start, " ,");
340 if (match_host_pattern(start, hostname)) {