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"
24 #include "libavcodec/internal.h"
25 #include "libavutil/mem.h"
27 #include "libavutil/time.h"
33 #include "compat/os2threads.h"
35 #include "compat/w32pthreads.h"
40 #include <openssl/ssl.h>
41 static int openssl_init;
43 #include <openssl/crypto.h>
44 #include "libavutil/avutil.h"
45 pthread_mutex_t *openssl_mutexes;
46 static void openssl_lock(int mode, int type, const char *file, int line)
48 if (mode & CRYPTO_LOCK)
49 pthread_mutex_lock(&openssl_mutexes[type]);
51 pthread_mutex_unlock(&openssl_mutexes[type]);
53 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
54 static unsigned long openssl_thread_id(void)
56 return (intptr_t) pthread_self();
62 #include <gnutls/gnutls.h>
63 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
66 GCRY_THREAD_OPTION_PTHREAD_IMPL;
70 void ff_tls_init(void)
72 avpriv_lock_avformat();
76 SSL_load_error_strings();
78 if (!CRYPTO_get_locking_callback()) {
80 openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
81 for (i = 0; i < CRYPTO_num_locks(); i++)
82 pthread_mutex_init(&openssl_mutexes[i], NULL);
83 CRYPTO_set_locking_callback(openssl_lock);
84 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
85 CRYPTO_set_id_callback(openssl_thread_id);
93 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
94 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
95 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
99 avpriv_unlock_avformat();
102 void ff_tls_deinit(void)
104 avpriv_lock_avformat();
109 if (CRYPTO_get_locking_callback() == openssl_lock) {
111 CRYPTO_set_locking_callback(NULL);
112 for (i = 0; i < CRYPTO_num_locks(); i++)
113 pthread_mutex_destroy(&openssl_mutexes[i]);
114 av_free(openssl_mutexes);
120 gnutls_global_deinit();
122 avpriv_unlock_avformat();
125 int ff_network_inited_globally;
127 int ff_network_init(void)
133 if (!ff_network_inited_globally)
134 av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
135 "network initialization. Please use "
136 "avformat_network_init(), this will "
137 "become mandatory later.\n");
139 if (WSAStartup(MAKEWORD(1,1), &wsaData))
145 int ff_network_wait_fd(int fd, int write)
147 int ev = write ? POLLOUT : POLLIN;
148 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
150 ret = poll(&p, 1, 100);
151 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
154 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
157 int64_t wait_start = 0;
160 ret = ff_network_wait_fd(fd, write);
161 if (ret != AVERROR(EAGAIN))
163 if (ff_check_interrupt(int_cb))
167 wait_start = av_gettime();
168 else if (av_gettime() - wait_start > timeout)
169 return AVERROR(ETIMEDOUT);
174 void ff_network_close(void)
182 int ff_neterrno(void)
184 int err = WSAGetLastError();
187 return AVERROR(EAGAIN);
189 return AVERROR(EINTR);
190 case WSAEPROTONOSUPPORT:
191 return AVERROR(EPROTONOSUPPORT);
193 return AVERROR(ETIMEDOUT);
194 case WSAECONNREFUSED:
195 return AVERROR(ECONNREFUSED);
197 return AVERROR(EINPROGRESS);
203 int ff_is_multicast_address(struct sockaddr *addr)
205 if (addr->sa_family == AF_INET) {
206 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
208 #if HAVE_STRUCT_SOCKADDR_IN6
209 if (addr->sa_family == AF_INET6) {
210 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
217 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
220 int runs = timeout / POLLING_TIME;
224 if (ff_check_interrupt(cb))
226 ret = poll(p, nfds, POLLING_TIME);
229 } while (timeout <= 0 || runs-- > 0);
232 return AVERROR(ETIMEDOUT);
234 return AVERROR(errno);
238 int ff_listen_bind(int fd, const struct sockaddr *addr,
239 socklen_t addrlen, int timeout, URLContext *h)
243 struct pollfd lp = { fd, POLLIN, 0 };
244 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
245 av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
247 ret = bind(fd, addr, addrlen);
249 return ff_neterrno();
253 return ff_neterrno();
255 ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
259 ret = accept(fd, NULL, NULL);
261 return ff_neterrno();
265 ff_socket_nonblock(ret, 1);
269 int ff_listen_connect(int fd, const struct sockaddr *addr,
270 socklen_t addrlen, int timeout, URLContext *h)
272 struct pollfd p = {fd, POLLOUT, 0};
276 ff_socket_nonblock(fd, 1);
278 while ((ret = connect(fd, addr, addrlen))) {
282 if (ff_check_interrupt(&h->interrupt_callback))
285 case AVERROR(EINPROGRESS):
286 case AVERROR(EAGAIN):
287 ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
290 optlen = sizeof(ret);
291 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
292 ret = AVUNERROR(ff_neterrno());
296 av_strerror(ret, errbuf, sizeof(errbuf));
297 av_log(h, AV_LOG_ERROR,
298 "Connection to %s failed: %s\n",
299 h->filename, errbuf);