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
23 #include "libavcodec/internal.h"
24 #include "libavutil/mem.h"
30 #include "compat/w32pthreads.h"
35 #include <openssl/ssl.h>
36 static int openssl_init;
38 #include <openssl/crypto.h>
39 #include "libavutil/avutil.h"
40 pthread_mutex_t *openssl_mutexes;
41 static void openssl_lock(int mode, int type, const char *file, int line)
43 if (mode & CRYPTO_LOCK)
44 pthread_mutex_lock(&openssl_mutexes[type]);
46 pthread_mutex_unlock(&openssl_mutexes[type]);
48 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
49 static unsigned long openssl_thread_id(void)
51 return (intptr_t) pthread_self();
57 #include <gnutls/gnutls.h>
58 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
61 GCRY_THREAD_OPTION_PTHREAD_IMPL;
65 void ff_tls_init(void)
67 avpriv_lock_avformat();
71 SSL_load_error_strings();
73 if (!CRYPTO_get_locking_callback()) {
75 openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
76 for (i = 0; i < CRYPTO_num_locks(); i++)
77 pthread_mutex_init(&openssl_mutexes[i], NULL);
78 CRYPTO_set_locking_callback(openssl_lock);
79 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
80 CRYPTO_set_id_callback(openssl_thread_id);
88 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
89 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
90 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
94 avpriv_unlock_avformat();
97 void ff_tls_deinit(void)
99 avpriv_lock_avformat();
104 if (CRYPTO_get_locking_callback() == openssl_lock) {
106 CRYPTO_set_locking_callback(NULL);
107 for (i = 0; i < CRYPTO_num_locks(); i++)
108 pthread_mutex_destroy(&openssl_mutexes[i]);
109 av_free(openssl_mutexes);
115 gnutls_global_deinit();
117 avpriv_unlock_avformat();
120 int ff_network_inited_globally;
122 int ff_network_init(void)
128 if (!ff_network_inited_globally)
129 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
130 "network initialization. Please use "
131 "avformat_network_init(), this will "
132 "become mandatory later.\n");
134 if (WSAStartup(MAKEWORD(1,1), &wsaData))
140 int ff_network_wait_fd(int fd, int write)
142 int ev = write ? POLLOUT : POLLIN;
143 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
145 ret = poll(&p, 1, 100);
146 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
149 void ff_network_close(void)
157 int ff_neterrno(void)
159 int err = WSAGetLastError();
162 return AVERROR(EAGAIN);
164 return AVERROR(EINTR);
165 case WSAEPROTONOSUPPORT:
166 return AVERROR(EPROTONOSUPPORT);
168 return AVERROR(ETIMEDOUT);
169 case WSAECONNREFUSED:
170 return AVERROR(ECONNREFUSED);
172 return AVERROR(EINPROGRESS);
178 int ff_is_multicast_address(struct sockaddr *addr)
180 if (addr->sa_family == AF_INET) {
181 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
183 #if HAVE_STRUCT_SOCKADDR_IN6
184 if (addr->sa_family == AF_INET6) {
185 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
192 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
195 int runs = timeout / POLLING_TIME;
199 if (ff_check_interrupt(cb))
201 ret = poll(p, nfds, POLLING_TIME);
204 } while (timeout < 0 || runs-- > 0);
207 return AVERROR(ETIMEDOUT);
209 return AVERROR(errno);
213 int ff_listen_bind(int fd, const struct sockaddr *addr,
214 socklen_t addrlen, int timeout, URLContext *h)
218 struct pollfd lp = { fd, POLLIN, 0 };
219 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
220 ret = bind(fd, addr, addrlen);
222 return ff_neterrno();
226 return ff_neterrno();
228 ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
232 ret = accept(fd, NULL, NULL);
234 return ff_neterrno();
238 ff_socket_nonblock(ret, 1);
242 int ff_listen_connect(int fd, const struct sockaddr *addr,
243 socklen_t addrlen, int timeout, URLContext *h,
246 struct pollfd p = {fd, POLLOUT, 0};
250 ff_socket_nonblock(fd, 1);
252 while ((ret = connect(fd, addr, addrlen))) {
256 if (ff_check_interrupt(&h->interrupt_callback))
259 case AVERROR(EINPROGRESS):
260 case AVERROR(EAGAIN):
261 ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
264 optlen = sizeof(ret);
265 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
266 ret = AVUNERROR(ff_neterrno());
270 av_strerror(ret, errbuf, sizeof(errbuf));
272 av_log(h, AV_LOG_WARNING,
273 "Connection to %s failed (%s), trying next address\n",
274 h->filename, errbuf);
276 av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
277 h->filename, errbuf);
286 static int match_host_pattern(const char *pattern, const char *hostname)
289 if (!strcmp(pattern, "*"))
291 // Skip a possible *. at the start of the pattern
292 if (pattern[0] == '*')
294 if (pattern[0] == '.')
296 len_p = strlen(pattern);
297 len_h = strlen(hostname);
300 // Simply check if the end of hostname is equal to 'pattern'
301 if (!strcmp(pattern, &hostname[len_h - len_p])) {
303 return 1; // Exact match
304 if (hostname[len_h - len_p - 1] == '.')
305 return 1; // The matched substring is a domain and not just a substring of a domain
310 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
318 buf = av_strdup(no_proxy);
323 char *sep, *next = NULL;
324 start += strspn(start, " ,");
325 sep = start + strcspn(start, " ,");
330 if (match_host_pattern(start, hostname)) {