]> git.sesse.net Git - ffmpeg/blob - libavformat/network.c
Merge commit 'bd255f9feb4deea4c990e582f0ba3b90d7b64b4c'
[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 "libavutil/avutil.h"
22 #include "network.h"
23 #include "libavcodec/internal.h"
24 #include "libavutil/mem.h"
25 #include "url.h"
26 #include "libavutil/time.h"
27
28 #if HAVE_THREADS
29 #if HAVE_PTHREADS
30 #include <pthread.h>
31 #else
32 #include "libavcodec/w32pthreads.h"
33 #endif
34 #endif
35
36 #if CONFIG_OPENSSL
37 #include <openssl/ssl.h>
38 static int openssl_init;
39 #if HAVE_THREADS
40 #include <openssl/crypto.h>
41 #include "libavutil/avutil.h"
42 pthread_mutex_t *openssl_mutexes;
43 static void openssl_lock(int mode, int type, const char *file, int line)
44 {
45     if (mode & CRYPTO_LOCK)
46         pthread_mutex_lock(&openssl_mutexes[type]);
47     else
48         pthread_mutex_unlock(&openssl_mutexes[type]);
49 }
50 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
51 static unsigned long openssl_thread_id(void)
52 {
53     return (intptr_t) pthread_self();
54 }
55 #endif
56 #endif
57 #endif
58 #if CONFIG_GNUTLS
59 #include <gnutls/gnutls.h>
60 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
61 #include <gcrypt.h>
62 #include <errno.h>
63 GCRY_THREAD_OPTION_PTHREAD_IMPL;
64 #endif
65 #endif
66
67 void ff_tls_init(void)
68 {
69     avpriv_lock_avformat();
70 #if CONFIG_OPENSSL
71     if (!openssl_init) {
72         SSL_library_init();
73         SSL_load_error_strings();
74 #if HAVE_THREADS
75         if (!CRYPTO_get_locking_callback()) {
76             int i;
77             openssl_mutexes = av_malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
78             for (i = 0; i < CRYPTO_num_locks(); i++)
79                 pthread_mutex_init(&openssl_mutexes[i], NULL);
80             CRYPTO_set_locking_callback(openssl_lock);
81 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
82             CRYPTO_set_id_callback(openssl_thread_id);
83 #endif
84         }
85 #endif
86     }
87     openssl_init++;
88 #endif
89 #if CONFIG_GNUTLS
90 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER < 0x020b00
91     if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
92         gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
93 #endif
94     gnutls_global_init();
95 #endif
96     avpriv_unlock_avformat();
97 }
98
99 void ff_tls_deinit(void)
100 {
101     avpriv_lock_avformat();
102 #if CONFIG_OPENSSL
103     openssl_init--;
104     if (!openssl_init) {
105 #if HAVE_THREADS
106         if (CRYPTO_get_locking_callback() == openssl_lock) {
107             int i;
108             CRYPTO_set_locking_callback(NULL);
109             for (i = 0; i < CRYPTO_num_locks(); i++)
110                 pthread_mutex_destroy(&openssl_mutexes[i]);
111             av_free(openssl_mutexes);
112         }
113 #endif
114     }
115 #endif
116 #if CONFIG_GNUTLS
117     gnutls_global_deinit();
118 #endif
119     avpriv_unlock_avformat();
120 }
121
122 int ff_network_inited_globally;
123
124 int ff_network_init(void)
125 {
126 #if HAVE_WINSOCK2_H
127     WSADATA wsaData;
128 #endif
129
130     if (!ff_network_inited_globally)
131         av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "
132                                      "network initialization. Please use "
133                                      "avformat_network_init(), this will "
134                                      "become mandatory later.\n");
135 #if HAVE_WINSOCK2_H
136     if (WSAStartup(MAKEWORD(1,1), &wsaData))
137         return 0;
138 #endif
139     return 1;
140 }
141
142 int ff_network_wait_fd(int fd, int write)
143 {
144     int ev = write ? POLLOUT : POLLIN;
145     struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
146     int ret;
147     ret = poll(&p, 1, 100);
148     return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
149 }
150
151 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
152 {
153     int ret;
154     int64_t wait_start = 0;
155
156     while (1) {
157         ret = ff_network_wait_fd(fd, write);
158         if (ret != AVERROR(EAGAIN))
159             return ret;
160         if (ff_check_interrupt(int_cb))
161             return AVERROR_EXIT;
162         if (timeout) {
163             if (!wait_start)
164                 wait_start = av_gettime();
165             else if (av_gettime() - wait_start > timeout)
166                 return AVERROR(ETIMEDOUT);
167         }
168     }
169 }
170
171 void ff_network_close(void)
172 {
173 #if HAVE_WINSOCK2_H
174     WSACleanup();
175 #endif
176 }
177
178 #if HAVE_WINSOCK2_H
179 int ff_neterrno(void)
180 {
181     int err = WSAGetLastError();
182     switch (err) {
183     case WSAEWOULDBLOCK:
184         return AVERROR(EAGAIN);
185     case WSAEINTR:
186         return AVERROR(EINTR);
187     case WSAEPROTONOSUPPORT:
188         return AVERROR(EPROTONOSUPPORT);
189     case WSAETIMEDOUT:
190         return AVERROR(ETIMEDOUT);
191     case WSAECONNREFUSED:
192         return AVERROR(ECONNREFUSED);
193     case WSAEINPROGRESS:
194         return AVERROR(EINPROGRESS);
195     }
196     return -err;
197 }
198 #endif
199
200 int ff_is_multicast_address(struct sockaddr *addr)
201 {
202     if (addr->sa_family == AF_INET) {
203         return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
204     }
205 #if HAVE_STRUCT_SOCKADDR_IN6
206     if (addr->sa_family == AF_INET6) {
207         return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
208     }
209 #endif
210
211     return 0;
212 }