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"
24 #include "libavutil/mem.h"
26 #include "libavutil/time.h"
32 #include "libavcodec/os2threads.h"
34 #include "libavcodec/w32pthreads.h"
39 #include <openssl/ssl.h>
40 static int openssl_init;
42 #include <openssl/crypto.h>
43 #include "libavutil/avutil.h"
44 pthread_mutex_t *openssl_mutexes;
45 static void openssl_lock(int mode, int type, const char *file, int line)
47 if (mode & CRYPTO_LOCK)
48 pthread_mutex_lock(&openssl_mutexes[type]);
50 pthread_mutex_unlock(&openssl_mutexes[type]);
52 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
53 static unsigned long openssl_thread_id(void)
55 return (intptr_t) pthread_self();
61 #include <gnutls/gnutls.h>
62 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
65 GCRY_THREAD_OPTION_PTHREAD_IMPL;
69 void ff_tls_init(void)
71 avpriv_lock_avformat();
75 SSL_load_error_strings();
77 if (!CRYPTO_get_locking_callback()) {
79 openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
80 for (i = 0; i < CRYPTO_num_locks(); i++)
81 pthread_mutex_init(&openssl_mutexes[i], NULL);
82 CRYPTO_set_locking_callback(openssl_lock);
83 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
84 CRYPTO_set_id_callback(openssl_thread_id);
92 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
93 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
94 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
98 avpriv_unlock_avformat();
101 void ff_tls_deinit(void)
103 avpriv_lock_avformat();
108 if (CRYPTO_get_locking_callback() == openssl_lock) {
110 CRYPTO_set_locking_callback(NULL);
111 for (i = 0; i < CRYPTO_num_locks(); i++)
112 pthread_mutex_destroy(&openssl_mutexes[i]);
113 av_free(openssl_mutexes);
119 gnutls_global_deinit();
121 avpriv_unlock_avformat();
124 int ff_network_inited_globally;
126 int ff_network_init(void)
132 if (!ff_network_inited_globally)
133 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
134 "network initialization. Please use "
135 "avformat_network_init(), this will "
136 "become mandatory later.\n");
138 if (WSAStartup(MAKEWORD(1,1), &wsaData))
144 int ff_network_wait_fd(int fd, int write)
146 int ev = write ? POLLOUT : POLLIN;
147 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
149 ret = poll(&p, 1, 100);
150 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
153 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
156 int64_t wait_start = 0;
159 ret = ff_network_wait_fd(fd, write);
160 if (ret != AVERROR(EAGAIN))
162 if (ff_check_interrupt(int_cb))
166 wait_start = av_gettime();
167 else if (av_gettime() - wait_start > timeout)
168 return AVERROR(ETIMEDOUT);
173 void ff_network_close(void)
181 int ff_neterrno(void)
183 int err = WSAGetLastError();
186 return AVERROR(EAGAIN);
188 return AVERROR(EINTR);
189 case WSAEPROTONOSUPPORT:
190 return AVERROR(EPROTONOSUPPORT);
192 return AVERROR(ETIMEDOUT);
193 case WSAECONNREFUSED:
194 return AVERROR(ECONNREFUSED);
196 return AVERROR(EINPROGRESS);
202 int ff_is_multicast_address(struct sockaddr *addr)
204 if (addr->sa_family == AF_INET) {
205 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
207 #if HAVE_STRUCT_SOCKADDR_IN6
208 if (addr->sa_family == AF_INET6) {
209 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);