]> git.sesse.net Git - ffmpeg/blob - libavformat/network.c
Merge commit '4a2ef39442bf7f0150db07a1fbfcf8286e4d44a3'
[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 "url.h"
24 #include "libavcodec/internal.h"
25 #include "libavutil/mem.h"
26 #include "url.h"
27 #include "libavutil/time.h"
28
29 #if HAVE_THREADS
30 #if HAVE_PTHREADS
31 #include <pthread.h>
32 #elif HAVE_OS2THREADS
33 #include "compat/os2threads.h"
34 #else
35 #include "compat/w32pthreads.h"
36 #endif
37 #endif
38
39 #if CONFIG_OPENSSL
40 #include <openssl/ssl.h>
41 static int openssl_init;
42 #if HAVE_THREADS
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)
47 {
48     if (mode & CRYPTO_LOCK)
49         pthread_mutex_lock(&openssl_mutexes[type]);
50     else
51         pthread_mutex_unlock(&openssl_mutexes[type]);
52 }
53 #if !defined(WIN32) && OPENSSL_VERSION_NUMBER < 0x10000000
54 static unsigned long openssl_thread_id(void)
55 {
56     return (intptr_t) pthread_self();
57 }
58 #endif
59 #endif
60 #endif
61 #if CONFIG_GNUTLS
62 #include <gnutls/gnutls.h>
63 #if HAVE_THREADS && GNUTLS_VERSION_NUMBER <= 0x020b00
64 #include <gcrypt.h>
65 #include <errno.h>
66 GCRY_THREAD_OPTION_PTHREAD_IMPL;
67 #endif
68 #endif
69
70 void ff_tls_init(void)
71 {
72     avpriv_lock_avformat();
73 #if CONFIG_OPENSSL
74     if (!openssl_init) {
75         SSL_library_init();
76         SSL_load_error_strings();
77 #if HAVE_THREADS
78         if (!CRYPTO_get_locking_callback()) {
79             int i;
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);
86 #endif
87         }
88 #endif
89     }
90     openssl_init++;
91 #endif
92 #if CONFIG_GNUTLS
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);
96 #endif
97     gnutls_global_init();
98 #endif
99     avpriv_unlock_avformat();
100 }
101
102 void ff_tls_deinit(void)
103 {
104     avpriv_lock_avformat();
105 #if CONFIG_OPENSSL
106     openssl_init--;
107     if (!openssl_init) {
108 #if HAVE_THREADS
109         if (CRYPTO_get_locking_callback() == openssl_lock) {
110             int i;
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);
115         }
116 #endif
117     }
118 #endif
119 #if CONFIG_GNUTLS
120     gnutls_global_deinit();
121 #endif
122     avpriv_unlock_avformat();
123 }
124
125 int ff_network_inited_globally;
126
127 int ff_network_init(void)
128 {
129 #if HAVE_WINSOCK2_H
130     WSADATA wsaData;
131 #endif
132
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");
138 #if HAVE_WINSOCK2_H
139     if (WSAStartup(MAKEWORD(1,1), &wsaData))
140         return 0;
141 #endif
142     return 1;
143 }
144
145 int ff_network_wait_fd(int fd, int write)
146 {
147     int ev = write ? POLLOUT : POLLIN;
148     struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
149     int ret;
150     ret = poll(&p, 1, 100);
151     return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
152 }
153
154 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
155 {
156     int ret;
157     int64_t wait_start = 0;
158
159     while (1) {
160         if (ff_check_interrupt(int_cb))
161             return AVERROR_EXIT;
162         ret = ff_network_wait_fd(fd, write);
163         if (ret != AVERROR(EAGAIN))
164             return ret;
165         if (timeout > 0) {
166             if (!wait_start)
167                 wait_start = av_gettime();
168             else if (av_gettime() - wait_start > timeout)
169                 return AVERROR(ETIMEDOUT);
170         }
171     }
172 }
173
174 void ff_network_close(void)
175 {
176 #if HAVE_WINSOCK2_H
177     WSACleanup();
178 #endif
179 }
180
181 #if HAVE_WINSOCK2_H
182 int ff_neterrno(void)
183 {
184     int err = WSAGetLastError();
185     switch (err) {
186     case WSAEWOULDBLOCK:
187         return AVERROR(EAGAIN);
188     case WSAEINTR:
189         return AVERROR(EINTR);
190     case WSAEPROTONOSUPPORT:
191         return AVERROR(EPROTONOSUPPORT);
192     case WSAETIMEDOUT:
193         return AVERROR(ETIMEDOUT);
194     case WSAECONNREFUSED:
195         return AVERROR(ECONNREFUSED);
196     case WSAEINPROGRESS:
197         return AVERROR(EINPROGRESS);
198     }
199     return -err;
200 }
201 #endif
202
203 int ff_is_multicast_address(struct sockaddr *addr)
204 {
205     if (addr->sa_family == AF_INET) {
206         return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
207     }
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);
211     }
212 #endif
213
214     return 0;
215 }
216
217 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
218                              AVIOInterruptCB *cb)
219 {
220     int runs = timeout / POLLING_TIME;
221     int ret = 0;
222
223     do {
224         if (ff_check_interrupt(cb))
225             return AVERROR_EXIT;
226         ret = poll(p, nfds, POLLING_TIME);
227         if (ret != 0)
228             break;
229     } while (timeout <= 0 || runs-- > 0);
230
231     if (!ret)
232         return AVERROR(ETIMEDOUT);
233     if (ret < 0)
234         return AVERROR(errno);
235     return ret;
236 }
237
238 int ff_listen_bind(int fd, const struct sockaddr *addr,
239                    socklen_t addrlen, int timeout, URLContext *h)
240 {
241     int ret;
242     int reuse = 1;
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");
246     }
247     ret = bind(fd, addr, addrlen);
248     if (ret)
249         return ff_neterrno();
250
251     ret = listen(fd, 1);
252     if (ret)
253         return ff_neterrno();
254
255     ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
256     if (ret < 0)
257         return ret;
258
259     ret = accept(fd, NULL, NULL);
260     if (ret < 0)
261         return ff_neterrno();
262
263     closesocket(fd);
264
265     ff_socket_nonblock(ret, 1);
266     return ret;
267 }
268
269 int ff_listen_connect(int fd, const struct sockaddr *addr,
270                       socklen_t addrlen, int timeout, URLContext *h)
271 {
272     struct pollfd p = {fd, POLLOUT, 0};
273     int ret;
274     socklen_t optlen;
275
276     ff_socket_nonblock(fd, 1);
277
278     while ((ret = connect(fd, addr, addrlen))) {
279         ret = ff_neterrno();
280         switch (ret) {
281         case AVERROR(EINTR):
282             if (ff_check_interrupt(&h->interrupt_callback))
283                 return AVERROR_EXIT;
284             continue;
285         case AVERROR(EINPROGRESS):
286         case AVERROR(EAGAIN):
287             ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
288             if (ret < 0)
289                 return ret;
290             optlen = sizeof(ret);
291             if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
292                 ret = AVUNERROR(ff_neterrno());
293             if (ret != 0) {
294                 char errbuf[100];
295                 ret = AVERROR(ret);
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);
300             }
301         default:
302             return ret;
303         }
304     }
305     return ret;
306 }
307
308 static int match_host_pattern(const char *pattern, const char *hostname)
309 {
310     int len_p, len_h;
311     if (!strcmp(pattern, "*"))
312         return 1;
313     // Skip a possible *. at the start of the pattern
314     if (pattern[0] == '*')
315         pattern++;
316     if (pattern[0] == '.')
317         pattern++;
318     len_p = strlen(pattern);
319     len_h = strlen(hostname);
320     if (len_p > len_h)
321         return 0;
322     // Simply check if the end of hostname is equal to 'pattern'
323     if (!strcmp(pattern, &hostname[len_h - len_p])) {
324         if (len_h == len_p)
325             return 1; // Exact match
326         if (hostname[len_h - len_p - 1] == '.')
327             return 1; // The matched substring is a domain and not just a substring of a domain
328     }
329     return 0;
330 }
331
332 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
333 {
334     char *buf, *start;
335     int ret = 0;
336     if (!no_proxy)
337         return 0;
338     if (!hostname)
339         return 0;
340     buf = av_strdup(no_proxy);
341     if (!buf)
342         return 0;
343     start = buf;
344     while (start) {
345         char *sep, *next = NULL;
346         start += strspn(start, " ,");
347         sep = start + strcspn(start, " ,");
348         if (*sep) {
349             next = sep + 1;
350             *sep = '\0';
351         }
352         if (match_host_pattern(start, hostname)) {
353             ret = 1;
354             break;
355         }
356         start = next;
357     }
358     av_free(buf);
359     return ret;
360 }