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
28
29 #if HAVE_THREADS
30 #if HAVE_PTHREADS
31 #include <pthread.h>
32 #elif HAVE_OS2THREADS
34 #else
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>
46 static void openssl_lock(
int mode,
int type,
const char *file,
int line)
47 {
48 if (mode & CRYPTO_LOCK)
50 else
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
71 {
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;
81 for (i = 0; i < CRYPTO_num_locks(); i++)
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
100 }
101
103 {
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++)
115 }
116 #endif
117 }
118 #endif
119 #if CONFIG_GNUTLS
120 gnutls_global_deinit();
121 #endif
123 }
124
126
128 {
129 #if HAVE_WINSOCK2_H
130 WSADATA wsaData;
131 #endif
132
133 if (!ff_network_inited_globally)
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
146 {
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);
152 }
153
155 {
157 int64_t wait_start = 0;
158
159 while (1) {
165 if (timeout > 0) {
166 if (!wait_start)
170 }
171 }
172 }
173
175 {
176 #if HAVE_WINSOCK2_H
177 WSACleanup();
178 #endif
179 }
180
181 #if HAVE_WINSOCK2_H
183 {
184 int err = WSAGetLastError();
185 switch (err) {
186 case WSAEWOULDBLOCK:
188 case WSAEINTR:
190 case WSAEPROTONOSUPPORT:
191 return AVERROR(EPROTONOSUPPORT);
192 case WSAETIMEDOUT:
194 case WSAECONNREFUSED:
196 case WSAEINPROGRESS:
198 }
199 return -err;
200 }
201 #endif
202
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) {
211 }
212 #endif
213
214 return 0;
215 }
216
219 {
222
223 do {
227 if (ret != 0)
228 break;
229 } while (timeout <= 0 || runs-- > 0);
230
231 if (!ret)
233 if (ret < 0)
236 }
237
239 socklen_t addrlen,
int timeout,
URLContext *h)
240 {
242 int reuse = 1;
243 struct pollfd lp = { fd, POLLIN, 0 };
244 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
246 }
247 ret = bind(fd, addr, addrlen);
248 if (ret)
250
251 ret = listen(fd, 1);
252 if (ret)
254
256 if (ret < 0)
258
259 ret = accept(fd, NULL, NULL);
260 if (ret < 0)
262
264
267 }
268
270 socklen_t addrlen,
int timeout,
URLContext *h)
271 {
272 struct pollfd p = {fd, POLLOUT, 0};
274 socklen_t optlen;
275
277
278 while ((ret = connect(fd, addr, addrlen))) {
280 switch (ret) {
284 continue;
288 if (ret < 0)
290 optlen =
sizeof(
ret);
291 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
293 if (ret != 0) {
294 char errbuf[100];
298 "Connection to %s failed: %s\n",
300 }
301 default:
303 }
304 }
306 }
307
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
333 {
336 if (!no_proxy)
337 return 0;
338 if (!hostname)
339 return 0;
341 if (!buf)
342 return 0;
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 }
353 ret = 1;
354 break;
355 }
356 start = next;
357 }
360 }