w32pthreads: move from lavc to compat/
[libav.git] / libavformat / network.c
1 /*
2 * Copyright (c) 2007 The Libav Project
3 *
4 * This file is part of Libav.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "network.h"
22 #include "libavcodec/internal.h"
23 #include "libavutil/mem.h"
24
25 #if HAVE_THREADS
26 #if HAVE_PTHREADS
27 #include <pthread.h>
28 #else
29 #include "compat/w32pthreads.h"
30 #endif
31 #endif
32
33 #if CONFIG_OPENSSL
34 #include <openssl/ssl.h>
35 static int openssl_init;
36 #if HAVE_THREADS
37 #include <openssl/crypto.h>
38 #include "libavutil/avutil.h"
39 pthread_mutex_t *openssl_mutexes;
40 static void openssl_lock(int mode, int type, const char *file, int line)
41 {
42 if (mode & CRYPTO_LOCK)
43 pthread_mutex_lock(&openssl_mutexes[type]);
44 else
45 pthread_mutex_unlock(&openssl_mutexes[type]);
46 }
47 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
48 static unsigned long openssl_thread_id(void)
49 {
50 return (intptr_t) pthread_self();
51 }
52 #endif
53 #endif
54 #endif
55 #if CONFIG_GNUTLS
56 #include <gnutls/gnutls.h>
57 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
58 #include <gcrypt.h>
59 #include <errno.h>
60 GCRY_THREAD_OPTION_PTHREAD_IMPL;
61 #endif
62 #endif
63
64 void ff_tls_init(void)
65 {
66 avpriv_lock_avformat();
67 #if CONFIG_OPENSSL
68 if (!openssl_init) {
69 SSL_library_init();
70 SSL_load_error_strings();
71 #if HAVE_THREADS
72 if (!CRYPTO_get_locking_callback()) {
73 int i;
74 openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
75 for (i = 0; i < CRYPTO_num_locks(); i++)
76 pthread_mutex_init(&openssl_mutexes[i], NULL);
77 CRYPTO_set_locking_callback(openssl_lock);
78 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
79 CRYPTO_set_id_callback(openssl_thread_id);
80 #endif
81 }
82 #endif
83 }
84 openssl_init++;
85 #endif
86 #if CONFIG_GNUTLS
87 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
88 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
89 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
90 #endif
91 gnutls_global_init();
92 #endif
93 avpriv_unlock_avformat();
94 }
95
96 void ff_tls_deinit(void)
97 {
98 avpriv_lock_avformat();
99 #if CONFIG_OPENSSL
100 openssl_init--;
101 if (!openssl_init) {
102 #if HAVE_THREADS
103 if (CRYPTO_get_locking_callback() == openssl_lock) {
104 int i;
105 CRYPTO_set_locking_callback(NULL);
106 for (i = 0; i < CRYPTO_num_locks(); i++)
107 pthread_mutex_destroy(&openssl_mutexes[i]);
108 av_free(openssl_mutexes);
109 }
110 #endif
111 }
112 #endif
113 #if CONFIG_GNUTLS
114 gnutls_global_deinit();
115 #endif
116 avpriv_unlock_avformat();
117 }
118
119 int ff_network_inited_globally;
120
121 int ff_network_init(void)
122 {
123 #if HAVE_WINSOCK2_H
124 WSADATA wsaData;
125 #endif
126
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");
132 #if HAVE_WINSOCK2_H
133 if (WSAStartup(MAKEWORD(1,1), &wsaData))
134 return 0;
135 #endif
136 return 1;
137 }
138
139 int ff_network_wait_fd(int fd, int write)
140 {
141 int ev = write ? POLLOUT : POLLIN;
142 struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
143 int ret;
144 ret = poll(&p, 1, 100);
145 return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
146 }
147
148 void ff_network_close(void)
149 {
150 #if HAVE_WINSOCK2_H
151 WSACleanup();
152 #endif
153 }
154
155 #if HAVE_WINSOCK2_H
156 int ff_neterrno(void)
157 {
158 int err = WSAGetLastError();
159 switch (err) {
160 case WSAEWOULDBLOCK:
161 return AVERROR(EAGAIN);
162 case WSAEINTR:
163 return AVERROR(EINTR);
164 case WSAEPROTONOSUPPORT:
165 return AVERROR(EPROTONOSUPPORT);
166 case WSAETIMEDOUT:
167 return AVERROR(ETIMEDOUT);
168 case WSAECONNREFUSED:
169 return AVERROR(ECONNREFUSED);
170 case WSAEINPROGRESS:
171 return AVERROR(EINPROGRESS);
172 }
173 return -err;
174 }
175 #endif
176
177 int ff_is_multicast_address(struct sockaddr *addr)
178 {
179 if (addr->sa_family == AF_INET) {
180 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
181 }
182 #if HAVE_STRUCT_SOCKADDR_IN6
183 if (addr->sa_family == AF_INET6) {
184 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
185 }
186 #endif
187
188 return 0;
189 }