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
21 #include "libavutil/avutil.h"
23 #include "libavcodec/internal.h"
25 #define THREADS (HAVE_PTHREADS || (defined(WIN32) && !defined(__MINGW32CE__)))
31 #include "libavcodec/w32pthreads.h"
36 #include <openssl/ssl.h>
37 static int openssl_init;
39 #include <openssl/crypto.h>
40 #include "libavutil/avutil.h"
41 pthread_mutex_t *openssl_mutexes;
42 static void openssl_lock(int mode, int type, const char *file, int line)
44 if (mode & CRYPTO_LOCK)
45 pthread_mutex_lock(&openssl_mutexes[type]);
47 pthread_mutex_unlock(&openssl_mutexes[type]);
49 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
50 static unsigned long openssl_thread_id(void)
52 return (intptr_t) pthread_self();
58 #include <gnutls/gnutls.h>
59 #if THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
64 GCRY_THREAD_OPTION_PTHREAD_IMPL;
68 void ff_tls_init(void)
70 avpriv_lock_avformat();
74 SSL_load_error_strings();
76 if (!CRYPTO_get_locking_callback()) {
78 openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
79 for (i = 0; i < CRYPTO_num_locks(); i++)
80 pthread_mutex_init(&openssl_mutexes[i], NULL);
81 CRYPTO_set_locking_callback(openssl_lock);
82 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
83 CRYPTO_set_id_callback(openssl_thread_id);
91 #if THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
92 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
93 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
97 avpriv_unlock_avformat();
100 void ff_tls_deinit(void)
102 avpriv_lock_avformat();
107 if (CRYPTO_get_locking_callback() == openssl_lock) {
109 CRYPTO_set_locking_callback(NULL);
110 for (i = 0; i < CRYPTO_num_locks(); i++)
111 pthread_mutex_destroy(&openssl_mutexes[i]);
112 av_free(openssl_mutexes);
118 gnutls_global_deinit();
120 avpriv_unlock_avformat();
123 int ff_network_inited_globally;
125 int ff_network_init(void)
127 if (!ff_network_inited_globally)
128 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
129 "network initialization. Please use "
130 "avformat_network_init(), this will "
131 "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);
170 int ff_is_multicast_address(struct sockaddr *addr)
172 if (addr->sa_family == AF_INET) {
173 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
175 #if HAVE_STRUCT_SOCKADDR_IN6
176 if (addr->sa_family == AF_INET6) {
177 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);