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_OPENSSL_PROTOCOL
34 if ((ret = ff_openssl_init()) < 0)
37 #if CONFIG_TLS_GNUTLS_PROTOCOL
43 void ff_tls_deinit(void)
45 #if CONFIG_TLS_OPENSSL_PROTOCOL
48 #if CONFIG_TLS_GNUTLS_PROTOCOL
53 int ff_network_inited_globally;
55 int ff_network_init(void)
61 if (!ff_network_inited_globally)
62 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
63 "network initialization. Please use "
64 "avformat_network_init(), this will "
65 "become mandatory later.\n");
67 if (WSAStartup(MAKEWORD(1,1), &wsaData))
73 int ff_network_wait_fd(int fd, int write)
75 int ev = write ? POLLOUT : POLLIN;
76 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
78 ret = poll(&p, 1, 100);
79 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
82 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
85 int64_t wait_start = 0;
88 if (ff_check_interrupt(int_cb))
90 ret = ff_network_wait_fd(fd, write);
91 if (ret != AVERROR(EAGAIN))
95 wait_start = av_gettime_relative();
96 else if (av_gettime_relative() - wait_start > timeout)
97 return AVERROR(ETIMEDOUT);
102 void ff_network_close(void)
110 int ff_neterrno(void)
112 int err = WSAGetLastError();
115 return AVERROR(EAGAIN);
117 return AVERROR(EINTR);
118 case WSAEPROTONOSUPPORT:
119 return AVERROR(EPROTONOSUPPORT);
121 return AVERROR(ETIMEDOUT);
122 case WSAECONNREFUSED:
123 return AVERROR(ECONNREFUSED);
125 return AVERROR(EINPROGRESS);
131 int ff_is_multicast_address(struct sockaddr *addr)
133 if (addr->sa_family == AF_INET) {
134 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
136 #if HAVE_STRUCT_SOCKADDR_IN6
137 if (addr->sa_family == AF_INET6) {
138 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
145 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
148 int runs = timeout / POLLING_TIME;
152 if (ff_check_interrupt(cb))
154 ret = poll(p, nfds, POLLING_TIME);
157 } while (timeout <= 0 || runs-- > 0);
160 return AVERROR(ETIMEDOUT);
162 return AVERROR(errno);
166 int ff_socket(int af, int type, int proto)
171 fd = socket(af, type | SOCK_CLOEXEC, proto);
172 if (fd == -1 && errno == EINVAL)
175 fd = socket(af, type, proto);
178 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
179 av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
185 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int));
190 int ff_listen(int fd, const struct sockaddr *addr,
195 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
196 av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
198 ret = bind(fd, addr, addrlen);
200 return ff_neterrno();
204 return ff_neterrno();
208 int ff_accept(int fd, int timeout, URLContext *h)
211 struct pollfd lp = { fd, POLLIN, 0 };
213 ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
217 ret = accept(fd, NULL, NULL);
219 return ff_neterrno();
220 if (ff_socket_nonblock(ret, 1) < 0)
221 av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
226 int ff_listen_bind(int fd, const struct sockaddr *addr,
227 socklen_t addrlen, int timeout, URLContext *h)
230 if ((ret = ff_listen(fd, addr, addrlen)) < 0)
232 if ((ret = ff_accept(fd, timeout, h)) < 0)
238 int ff_listen_connect(int fd, const struct sockaddr *addr,
239 socklen_t addrlen, int timeout, URLContext *h,
242 struct pollfd p = {fd, POLLOUT, 0};
246 if (ff_socket_nonblock(fd, 1) < 0)
247 av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
249 while ((ret = connect(fd, addr, addrlen))) {
253 if (ff_check_interrupt(&h->interrupt_callback))
256 case AVERROR(EINPROGRESS):
257 case AVERROR(EAGAIN):
258 ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
261 optlen = sizeof(ret);
262 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
263 ret = AVUNERROR(ff_neterrno());
267 av_strerror(ret, errbuf, sizeof(errbuf));
269 av_log(h, AV_LOG_WARNING,
270 "Connection to %s failed (%s), trying next address\n",
271 h->filename, errbuf);
273 av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
274 h->filename, errbuf);
283 static int match_host_pattern(const char *pattern, const char *hostname)
286 if (!strcmp(pattern, "*"))
288 // Skip a possible *. at the start of the pattern
289 if (pattern[0] == '*')
291 if (pattern[0] == '.')
293 len_p = strlen(pattern);
294 len_h = strlen(hostname);
297 // Simply check if the end of hostname is equal to 'pattern'
298 if (!strcmp(pattern, &hostname[len_h - len_p])) {
300 return 1; // Exact match
301 if (hostname[len_h - len_p - 1] == '.')
302 return 1; // The matched substring is a domain and not just a substring of a domain
307 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
315 buf = av_strdup(no_proxy);
320 char *sep, *next = NULL;
321 start += strspn(start, " ,");
322 sep = start + strcspn(start, " ,");
327 if (match_host_pattern(start, hostname)) {