2 * Copyright (c) 2007 The Libav Project
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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/mem.h"
28 void ff_tls_init(void)
30 #if CONFIG_TLS_OPENSSL_PROTOCOL
33 #if CONFIG_TLS_GNUTLS_PROTOCOL
38 void ff_tls_deinit(void)
40 #if CONFIG_TLS_OPENSSL_PROTOCOL
43 #if CONFIG_TLS_GNUTLS_PROTOCOL
48 int ff_network_inited_globally;
50 int ff_network_init(void)
56 if (!ff_network_inited_globally)
57 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
58 "network initialization. Please use "
59 "avformat_network_init(), this will "
60 "become mandatory later.\n");
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, 100);
74 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
77 void ff_network_close(void)
87 int err = WSAGetLastError();
90 return AVERROR(EAGAIN);
92 return AVERROR(EINTR);
93 case WSAEPROTONOSUPPORT:
94 return AVERROR(EPROTONOSUPPORT);
96 return AVERROR(ETIMEDOUT);
98 return AVERROR(ECONNREFUSED);
100 return AVERROR(EINPROGRESS);
106 int ff_is_multicast_address(struct sockaddr *addr)
108 if (addr->sa_family == AF_INET) {
109 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
111 #if HAVE_STRUCT_SOCKADDR_IN6
112 if (addr->sa_family == AF_INET6) {
113 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
120 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
123 int runs = timeout / POLLING_TIME;
127 if (ff_check_interrupt(cb))
129 ret = poll(p, nfds, POLLING_TIME);
132 } while (timeout < 0 || runs-- > 0);
135 return AVERROR(ETIMEDOUT);
137 return AVERROR(errno);
141 int ff_socket(int af, int type, int proto)
146 fd = socket(af, type | SOCK_CLOEXEC, proto);
147 if (fd == -1 && errno == EINVAL)
150 fd = socket(af, type, proto);
153 fcntl(fd, F_SETFD, FD_CLOEXEC);
158 setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int));
163 int ff_listen_bind(int fd, const struct sockaddr *addr,
164 socklen_t addrlen, int timeout, URLContext *h)
168 struct pollfd lp = { fd, POLLIN, 0 };
169 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
170 ret = bind(fd, addr, addrlen);
172 return ff_neterrno();
176 return ff_neterrno();
178 ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
182 ret = accept(fd, NULL, NULL);
184 return ff_neterrno();
188 ff_socket_nonblock(ret, 1);
192 int ff_listen_connect(int fd, const struct sockaddr *addr,
193 socklen_t addrlen, int timeout, URLContext *h,
196 struct pollfd p = {fd, POLLOUT, 0};
200 ff_socket_nonblock(fd, 1);
202 while ((ret = connect(fd, addr, addrlen))) {
206 if (ff_check_interrupt(&h->interrupt_callback))
209 case AVERROR(EINPROGRESS):
210 case AVERROR(EAGAIN):
211 ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
214 optlen = sizeof(ret);
215 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
216 ret = AVUNERROR(ff_neterrno());
220 av_strerror(ret, errbuf, sizeof(errbuf));
222 av_log(h, AV_LOG_WARNING,
223 "Connection to %s failed (%s), trying next address\n",
224 h->filename, errbuf);
226 av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
227 h->filename, errbuf);
236 static int match_host_pattern(const char *pattern, const char *hostname)
239 if (!strcmp(pattern, "*"))
241 // Skip a possible *. at the start of the pattern
242 if (pattern[0] == '*')
244 if (pattern[0] == '.')
246 len_p = strlen(pattern);
247 len_h = strlen(hostname);
250 // Simply check if the end of hostname is equal to 'pattern'
251 if (!strcmp(pattern, &hostname[len_h - len_p])) {
253 return 1; // Exact match
254 if (hostname[len_h - len_p - 1] == '.')
255 return 1; // The matched substring is a domain and not just a substring of a domain
260 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
268 buf = av_strdup(no_proxy);
273 char *sep, *next = NULL;
274 start += strspn(start, " ,");
275 sep = start + strcspn(start, " ,");
280 if (match_host_pattern(start, hostname)) {