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_inited_globally;
59 int ff_network_init(void)
65 if (!ff_network_inited_globally)
66 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
67 "network initialization. Please use "
68 "avformat_network_init(), this will "
69 "become mandatory later.\n");
71 if (WSAStartup(MAKEWORD(1,1), &wsaData))
77 int ff_network_wait_fd(int fd, int write)
79 int ev = write ? POLLOUT : POLLIN;
80 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
82 ret = poll(&p, 1, POLLING_TIME);
83 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
86 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
89 int64_t wait_start = 0;
92 if (ff_check_interrupt(int_cb))
94 ret = ff_network_wait_fd(fd, write);
95 if (ret != AVERROR(EAGAIN))
99 wait_start = av_gettime_relative();
100 else if (av_gettime_relative() - wait_start > timeout)
101 return AVERROR(ETIMEDOUT);
106 int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
108 int64_t wait_start = av_gettime_relative();
113 if (ff_check_interrupt(int_cb))
116 time_left = timeout - (av_gettime_relative() - wait_start);
118 return AVERROR(ETIMEDOUT);
120 av_usleep(FFMIN(time_left, POLLING_TIME * 1000));
124 void ff_network_close(void)
132 int ff_neterrno(void)
134 int err = WSAGetLastError();
137 return AVERROR(EAGAIN);
139 return AVERROR(EINTR);
140 case WSAEPROTONOSUPPORT:
141 return AVERROR(EPROTONOSUPPORT);
143 return AVERROR(ETIMEDOUT);
144 case WSAECONNREFUSED:
145 return AVERROR(ECONNREFUSED);
147 return AVERROR(EINPROGRESS);
153 int ff_is_multicast_address(struct sockaddr *addr)
155 if (addr->sa_family == AF_INET) {
156 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
158 #if HAVE_STRUCT_SOCKADDR_IN6
159 if (addr->sa_family == AF_INET6) {
160 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
167 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
170 int runs = timeout / POLLING_TIME;
174 if (ff_check_interrupt(cb))
176 ret = poll(p, nfds, POLLING_TIME);
179 } while (timeout <= 0 || runs-- > 0);
182 return AVERROR(ETIMEDOUT);
184 return ff_neterrno();
188 int ff_socket(int af, int type, int proto)
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)
201 av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
207 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int));
212 int ff_listen(int fd, const struct sockaddr *addr,
217 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
218 av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
220 ret = bind(fd, addr, addrlen);
222 return ff_neterrno();
226 return ff_neterrno();
230 int ff_accept(int fd, int timeout, URLContext *h)
233 struct pollfd lp = { fd, POLLIN, 0 };
235 ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
239 ret = accept(fd, NULL, NULL);
241 return ff_neterrno();
242 if (ff_socket_nonblock(ret, 1) < 0)
243 av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
248 int ff_listen_bind(int fd, const struct sockaddr *addr,
249 socklen_t addrlen, int timeout, URLContext *h)
252 if ((ret = ff_listen(fd, addr, addrlen)) < 0)
254 if ((ret = ff_accept(fd, timeout, h)) < 0)
260 int ff_listen_connect(int fd, const struct sockaddr *addr,
261 socklen_t addrlen, int timeout, URLContext *h,
264 struct pollfd p = {fd, POLLOUT, 0};
268 if (ff_socket_nonblock(fd, 1) < 0)
269 av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
271 while ((ret = connect(fd, addr, addrlen))) {
275 if (ff_check_interrupt(&h->interrupt_callback))
278 case AVERROR(EINPROGRESS):
279 case AVERROR(EAGAIN):
280 ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
283 optlen = sizeof(ret);
284 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
285 ret = AVUNERROR(ff_neterrno());
289 av_strerror(ret, errbuf, sizeof(errbuf));
291 av_log(h, AV_LOG_WARNING,
292 "Connection to %s failed (%s), trying next address\n",
293 h->filename, errbuf);
295 av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
296 h->filename, errbuf);
305 static int match_host_pattern(const char *pattern, const char *hostname)
308 if (!strcmp(pattern, "*"))
310 // Skip a possible *. at the start of the pattern
311 if (pattern[0] == '*')
313 if (pattern[0] == '.')
315 len_p = strlen(pattern);
316 len_h = strlen(hostname);
319 // Simply check if the end of hostname is equal to 'pattern'
320 if (!strcmp(pattern, &hostname[len_h - len_p])) {
322 return 1; // Exact match
323 if (hostname[len_h - len_p - 1] == '.')
324 return 1; // The matched substring is a domain and not just a substring of a domain
329 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
337 buf = av_strdup(no_proxy);
342 char *sep, *next = NULL;
343 start += strspn(start, " ,");
344 sep = start + strcspn(start, " ,");
349 if (match_host_pattern(start, hostname)) {