]> git.sesse.net Git - ffmpeg/blob - libavformat/network.c
Merge commit '22f98ac19cf29f22b3e1d10314df9503f06fe683'
[ffmpeg] / libavformat / network.c
1 /*
2  * Copyright (c) 2007 The FFmpeg Project
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <fcntl.h>
22 #include "network.h"
23 #include "tls.h"
24 #include "url.h"
25 #include "libavcodec/internal.h"
26 #include "libavutil/avutil.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/time.h"
29
30 int ff_tls_init(void)
31 {
32 #if CONFIG_TLS_PROTOCOL
33 #if CONFIG_OPENSSL
34     int ret;
35     if ((ret = ff_openssl_init()) < 0)
36         return ret;
37 #endif
38 #if CONFIG_GNUTLS
39     ff_gnutls_init();
40 #endif
41 #endif
42     return 0;
43 }
44
45 void ff_tls_deinit(void)
46 {
47 #if CONFIG_TLS_PROTOCOL
48 #if CONFIG_OPENSSL
49     ff_openssl_deinit();
50 #endif
51 #if CONFIG_GNUTLS
52     ff_gnutls_deinit();
53 #endif
54 #endif
55 }
56
57 int ff_network_init(void)
58 {
59 #if HAVE_WINSOCK2_H
60     WSADATA wsaData;
61
62     if (WSAStartup(MAKEWORD(1,1), &wsaData))
63         return 0;
64 #endif
65     return 1;
66 }
67
68 int ff_network_wait_fd(int fd, int write)
69 {
70     int ev = write ? POLLOUT : POLLIN;
71     struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
72     int ret;
73     ret = poll(&p, 1, POLLING_TIME);
74     return ret < 0 ? ff_neterrno() : p.revents & (ev | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
75 }
76
77 int ff_network_wait_fd_timeout(int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
78 {
79     int ret;
80     int64_t wait_start = 0;
81
82     while (1) {
83         if (ff_check_interrupt(int_cb))
84             return AVERROR_EXIT;
85         ret = ff_network_wait_fd(fd, write);
86         if (ret != AVERROR(EAGAIN))
87             return ret;
88         if (timeout > 0) {
89             if (!wait_start)
90                 wait_start = av_gettime_relative();
91             else if (av_gettime_relative() - wait_start > timeout)
92                 return AVERROR(ETIMEDOUT);
93         }
94     }
95 }
96
97 int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
98 {
99     int64_t wait_start = av_gettime_relative();
100
101     while (1) {
102         int64_t time_left;
103
104         if (ff_check_interrupt(int_cb))
105             return AVERROR_EXIT;
106
107         time_left = timeout - (av_gettime_relative() - wait_start);
108         if (time_left <= 0)
109             return AVERROR(ETIMEDOUT);
110
111         av_usleep(FFMIN(time_left, POLLING_TIME * 1000));
112     }
113 }
114
115 void ff_network_close(void)
116 {
117 #if HAVE_WINSOCK2_H
118     WSACleanup();
119 #endif
120 }
121
122 #if HAVE_WINSOCK2_H
123 int ff_neterrno(void)
124 {
125     int err = WSAGetLastError();
126     switch (err) {
127     case WSAEWOULDBLOCK:
128         return AVERROR(EAGAIN);
129     case WSAEINTR:
130         return AVERROR(EINTR);
131     case WSAEPROTONOSUPPORT:
132         return AVERROR(EPROTONOSUPPORT);
133     case WSAETIMEDOUT:
134         return AVERROR(ETIMEDOUT);
135     case WSAECONNREFUSED:
136         return AVERROR(ECONNREFUSED);
137     case WSAEINPROGRESS:
138         return AVERROR(EINPROGRESS);
139     }
140     return -err;
141 }
142 #endif
143
144 int ff_is_multicast_address(struct sockaddr *addr)
145 {
146     if (addr->sa_family == AF_INET) {
147         return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
148     }
149 #if HAVE_STRUCT_SOCKADDR_IN6
150     if (addr->sa_family == AF_INET6) {
151         return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
152     }
153 #endif
154
155     return 0;
156 }
157
158 static int ff_poll_interrupt(struct pollfd *p, nfds_t nfds, int timeout,
159                              AVIOInterruptCB *cb)
160 {
161     int runs = timeout / POLLING_TIME;
162     int ret = 0;
163
164     do {
165         if (ff_check_interrupt(cb))
166             return AVERROR_EXIT;
167         ret = poll(p, nfds, POLLING_TIME);
168         if (ret != 0) {
169             if (ret < 0)
170                 ret = ff_neterrno();
171             if (ret == AVERROR(EINTR))
172                 continue;
173             break;
174         }
175     } while (timeout <= 0 || runs-- > 0);
176
177     if (!ret)
178         return AVERROR(ETIMEDOUT);
179     return ret;
180 }
181
182 int ff_socket(int af, int type, int proto)
183 {
184     int fd;
185
186 #ifdef SOCK_CLOEXEC
187     fd = socket(af, type | SOCK_CLOEXEC, proto);
188     if (fd == -1 && errno == EINVAL)
189 #endif
190     {
191         fd = socket(af, type, proto);
192 #if HAVE_FCNTL
193         if (fd != -1) {
194             if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
195                 av_log(NULL, AV_LOG_DEBUG, "Failed to set close on exec\n");
196         }
197 #endif
198     }
199 #ifdef SO_NOSIGPIPE
200     if (fd != -1) {
201         if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &(int){1}, sizeof(int))) {
202              av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_NOSIGPIPE) failed\n");
203         }
204     }
205 #endif
206     return fd;
207 }
208
209 int ff_listen(int fd, const struct sockaddr *addr,
210               socklen_t addrlen)
211 {
212     int ret;
213     int reuse = 1;
214     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
215         av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
216     }
217     ret = bind(fd, addr, addrlen);
218     if (ret)
219         return ff_neterrno();
220
221     ret = listen(fd, 1);
222     if (ret)
223         return ff_neterrno();
224     return ret;
225 }
226
227 int ff_accept(int fd, int timeout, URLContext *h)
228 {
229     int ret;
230     struct pollfd lp = { fd, POLLIN, 0 };
231
232     ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
233     if (ret < 0)
234         return ret;
235
236     ret = accept(fd, NULL, NULL);
237     if (ret < 0)
238         return ff_neterrno();
239     if (ff_socket_nonblock(ret, 1) < 0)
240         av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
241
242     return ret;
243 }
244
245 int ff_listen_bind(int fd, const struct sockaddr *addr,
246                    socklen_t addrlen, int timeout, URLContext *h)
247 {
248     int ret;
249     if ((ret = ff_listen(fd, addr, addrlen)) < 0)
250         return ret;
251     if ((ret = ff_accept(fd, timeout, h)) < 0)
252         return ret;
253     closesocket(fd);
254     return ret;
255 }
256
257 int ff_listen_connect(int fd, const struct sockaddr *addr,
258                       socklen_t addrlen, int timeout, URLContext *h,
259                       int will_try_next)
260 {
261     struct pollfd p = {fd, POLLOUT, 0};
262     int ret;
263     socklen_t optlen;
264
265     if (ff_socket_nonblock(fd, 1) < 0)
266         av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
267
268     while ((ret = connect(fd, addr, addrlen))) {
269         ret = ff_neterrno();
270         switch (ret) {
271         case AVERROR(EINTR):
272             if (ff_check_interrupt(&h->interrupt_callback))
273                 return AVERROR_EXIT;
274             continue;
275         case AVERROR(EINPROGRESS):
276         case AVERROR(EAGAIN):
277             ret = ff_poll_interrupt(&p, 1, timeout, &h->interrupt_callback);
278             if (ret < 0)
279                 return ret;
280             optlen = sizeof(ret);
281             if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
282                 ret = AVUNERROR(ff_neterrno());
283             if (ret != 0) {
284                 char errbuf[100];
285                 ret = AVERROR(ret);
286                 av_strerror(ret, errbuf, sizeof(errbuf));
287                 if (will_try_next)
288                     av_log(h, AV_LOG_WARNING,
289                            "Connection to %s failed (%s), trying next address\n",
290                            h->filename, errbuf);
291                 else
292                     av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
293                            h->filename, errbuf);
294             }
295         default:
296             return ret;
297         }
298     }
299     return ret;
300 }
301
302 static int match_host_pattern(const char *pattern, const char *hostname)
303 {
304     int len_p, len_h;
305     if (!strcmp(pattern, "*"))
306         return 1;
307     // Skip a possible *. at the start of the pattern
308     if (pattern[0] == '*')
309         pattern++;
310     if (pattern[0] == '.')
311         pattern++;
312     len_p = strlen(pattern);
313     len_h = strlen(hostname);
314     if (len_p > len_h)
315         return 0;
316     // Simply check if the end of hostname is equal to 'pattern'
317     if (!strcmp(pattern, &hostname[len_h - len_p])) {
318         if (len_h == len_p)
319             return 1; // Exact match
320         if (hostname[len_h - len_p - 1] == '.')
321             return 1; // The matched substring is a domain and not just a substring of a domain
322     }
323     return 0;
324 }
325
326 int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
327 {
328     char *buf, *start;
329     int ret = 0;
330     if (!no_proxy)
331         return 0;
332     if (!hostname)
333         return 0;
334     buf = av_strdup(no_proxy);
335     if (!buf)
336         return 0;
337     start = buf;
338     while (start) {
339         char *sep, *next = NULL;
340         start += strspn(start, " ,");
341         sep = start + strcspn(start, " ,");
342         if (*sep) {
343             next = sep + 1;
344             *sep = '\0';
345         }
346         if (match_host_pattern(start, hostname)) {
347             ret = 1;
348             break;
349         }
350         start = next;
351     }
352     av_free(buf);
353     return ret;
354 }
355
356 void ff_log_net_error(void *ctx, int level, const char* prefix)
357 {
358     char errbuf[100];
359     av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
360     av_log(ctx, level, "%s: %s\n", prefix, errbuf);
361 }