]> git.sesse.net Git - ffmpeg/blob - libavformat/network.c
nsvdec: Remove commented-out debug cruft
[ffmpeg] / 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 "url.h"
23 #include "libavcodec/internal.h"
24 #include "libavutil/mem.h"
25
26 #if HAVE_THREADS
27 #if HAVE_PTHREADS
28 #include <pthread.h>
29 #else
30 #include "compat/w32pthreads.h"
31 #endif
32 #endif
33
34 #if CONFIG_OPENSSL
35 #include <openssl/ssl.h>
36 static int openssl_init;
37 #if HAVE_THREADS
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)
42 {
43     if (mode & CRYPTO_LOCK)
44         pthread_mutex_lock(&openssl_mutexes[type]);
45     else
46         pthread_mutex_unlock(&openssl_mutexes[type]);
47 }
48 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
49 static unsigned long openssl_thread_id(void)
50 {
51     return (intptr_t) pthread_self();
52 }
53 #endif
54 #endif
55 #endif
56 #if CONFIG_GNUTLS
57 #include <gnutls/gnutls.h>
58 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
59 #include <gcrypt.h>
60 #include <errno.h>
61 GCRY_THREAD_OPTION_PTHREAD_IMPL;
62 #endif
63 #endif
64
65 void ff_tls_init(void)
66 {
67     avpriv_lock_avformat();
68 #if CONFIG_OPENSSL
69     if (!openssl_init) {
70         SSL_library_init();
71         SSL_load_error_strings();
72 #if HAVE_THREADS
73         if (!CRYPTO_get_locking_callback()) {
74             int i;
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);
81 #endif
82         }
83 #endif
84     }
85     openssl_init++;
86 #endif
87 #if CONFIG_GNUTLS
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);
91 #endif
92     gnutls_global_init();
93 #endif
94     avpriv_unlock_avformat();
95 }
96
97 void ff_tls_deinit(void)
98 {
99     avpriv_lock_avformat();
100 #if CONFIG_OPENSSL
101     openssl_init--;
102     if (!openssl_init) {
103 #if HAVE_THREADS
104         if (CRYPTO_get_locking_callback() == openssl_lock) {
105             int i;
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);
110         }
111 #endif
112     }
113 #endif
114 #if CONFIG_GNUTLS
115     gnutls_global_deinit();
116 #endif
117     avpriv_unlock_avformat();
118 }
119
120 int ff_network_inited_globally;
121
122 int ff_network_init(void)
123 {
124 #if HAVE_WINSOCK2_H
125     WSADATA wsaData;
126 #endif
127
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");
133 #if HAVE_WINSOCK2_H
134     if (WSAStartup(MAKEWORD(1,1), &wsaData))
135         return 0;
136 #endif
137     return 1;
138 }
139
140 int ff_network_wait_fd(int fd, int write)
141 {
142     int ev = write ? POLLOUT : POLLIN;
143     struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
144     int ret;
145     ret = poll(&p, 1, 100);
146     return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
147 }
148
149 void ff_network_close(void)
150 {
151 #if HAVE_WINSOCK2_H
152     WSACleanup();
153 #endif
154 }
155
156 #if HAVE_WINSOCK2_H
157 int ff_neterrno(void)
158 {
159     int err = WSAGetLastError();
160     switch (err) {
161     case WSAEWOULDBLOCK:
162         return AVERROR(EAGAIN);
163     case WSAEINTR:
164         return AVERROR(EINTR);
165     case WSAEPROTONOSUPPORT:
166         return AVERROR(EPROTONOSUPPORT);
167     case WSAETIMEDOUT:
168         return AVERROR(ETIMEDOUT);
169     case WSAECONNREFUSED:
170         return AVERROR(ECONNREFUSED);
171     case WSAEINPROGRESS:
172         return AVERROR(EINPROGRESS);
173     }
174     return -err;
175 }
176 #endif
177
178 int ff_is_multicast_address(struct sockaddr *addr)
179 {
180     if (addr->sa_family == AF_INET) {
181         return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
182     }
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);
186     }
187 #endif
188
189     return 0;
190 }
191
192 int ff_listen_bind(int fd, const struct sockaddr *addr,
193                    socklen_t addrlen, int timeout)
194 {
195     int ret;
196     int reuse = 1;
197     struct pollfd lp = { fd, POLLIN, 0 };
198     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
199     ret = bind(fd, addr, addrlen);
200     if (ret)
201         return ff_neterrno();
202
203     ret = listen(fd, 1);
204     if (ret)
205         return ff_neterrno();
206
207     ret = poll(&lp, 1, timeout >= 0 ? timeout : -1);
208     if (ret <= 0)
209         return AVERROR(ETIMEDOUT);
210
211     ret = accept(fd, NULL, NULL);
212     if (ret < 0)
213         return ff_neterrno();
214
215     closesocket(fd);
216
217     ff_socket_nonblock(ret, 1);
218     return ret;
219 }
220
221 int ff_listen_connect(int fd, const struct sockaddr *addr,
222                       socklen_t addrlen, int timeout, URLContext *h)
223 {
224     struct pollfd p = {fd, POLLOUT, 0};
225     int ret;
226     socklen_t optlen;
227
228     ff_socket_nonblock(fd, 1);
229
230     while ((ret = connect(fd, addr, addrlen))) {
231         ret = ff_neterrno();
232         switch (ret) {
233         case AVERROR(EINTR):
234             if (ff_check_interrupt(&h->interrupt_callback))
235                 return AVERROR_EXIT;
236             continue;
237         case AVERROR(EINPROGRESS):
238         case AVERROR(EAGAIN):
239             while (timeout--) {
240                 if (ff_check_interrupt(&h->interrupt_callback))
241                     return AVERROR_EXIT;
242                 ret = poll(&p, 1, 100);
243                 if (ret > 0)
244                     break;
245             }
246             if (ret <= 0)
247                 return AVERROR(ETIMEDOUT);
248             optlen = sizeof(ret);
249             if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
250                 ret = AVUNERROR(ff_neterrno());
251             if (ret != 0) {
252                 char errbuf[100];
253                 ret = AVERROR(ret);
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);
258             }
259         default:
260             return ret;
261         }
262     }
263     return ret;
264 }