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 int ff_listen_bind(int fd, const struct sockaddr *addr,
193 socklen_t addrlen, int timeout)
197 struct pollfd lp = { fd, POLLIN, 0 };
198 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
199 ret = bind(fd, addr, addrlen);
201 return ff_neterrno();
205 return ff_neterrno();
207 ret = poll(&lp, 1, timeout >= 0 ? timeout : -1);
209 return AVERROR(ETIMEDOUT);
211 ret = accept(fd, NULL, NULL);
213 return ff_neterrno();
217 ff_socket_nonblock(ret, 1);
221 int ff_listen_connect(int fd, const struct sockaddr *addr,
222 socklen_t addrlen, int timeout, URLContext *h)
224 struct pollfd p = {fd, POLLOUT, 0};
228 ff_socket_nonblock(fd, 1);
230 while ((ret = connect(fd, addr, addrlen))) {
234 if (ff_check_interrupt(&h->interrupt_callback))
237 case AVERROR(EINPROGRESS):
238 case AVERROR(EAGAIN):
240 if (ff_check_interrupt(&h->interrupt_callback))
242 ret = poll(&p, 1, 100);
247 return AVERROR(ETIMEDOUT);
248 optlen = sizeof(ret);
249 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
250 ret = AVUNERROR(ff_neterrno());
254 av_strerror(ret, errbuf, sizeof(errbuf));
255 av_log(h, AV_LOG_ERROR,
256 "Connection to %s failed: %s\n",
257 h->filename, errbuf);